Implement late-specified return type using 'auto'.
[platform/upstream/gcc.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, 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                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591   (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597   (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599   (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1601   (cp_token *);
1602 static tree cp_parser_new_expression
1603   (cp_parser *);
1604 static tree cp_parser_new_placement
1605   (cp_parser *);
1606 static tree cp_parser_new_type_id
1607   (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609   (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611   (cp_parser *);
1612 static tree cp_parser_new_initializer
1613   (cp_parser *);
1614 static tree cp_parser_delete_expression
1615   (cp_parser *);
1616 static tree cp_parser_cast_expression
1617   (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       /* Floating-point literals are only allowed in an integral
3148          constant expression if they are cast to an integral or
3149          enumeration type.  */
3150       if (TREE_CODE (token->u.value) == REAL_CST
3151           && parser->integral_constant_expression_p
3152           && pedantic)
3153         {
3154           /* CAST_P will be set even in invalid code like "int(2.7 +
3155              ...)".   Therefore, we have to check that the next token
3156              is sure to end the cast.  */
3157           if (cast_p)
3158             {
3159               cp_token *next_token;
3160
3161               next_token = cp_lexer_peek_token (parser->lexer);
3162               if (/* The comma at the end of an
3163                      enumerator-definition.  */
3164                   next_token->type != CPP_COMMA
3165                   /* The curly brace at the end of an enum-specifier.  */
3166                   && next_token->type != CPP_CLOSE_BRACE
3167                   /* The end of a statement.  */
3168                   && next_token->type != CPP_SEMICOLON
3169                   /* The end of the cast-expression.  */
3170                   && next_token->type != CPP_CLOSE_PAREN
3171                   /* The end of an array bound.  */
3172                   && next_token->type != CPP_CLOSE_SQUARE
3173                   /* The closing ">" in a template-argument-list.  */
3174                   && (next_token->type != CPP_GREATER
3175                       || parser->greater_than_is_operator_p)
3176                   /* C++0x only: A ">>" treated like two ">" tokens,
3177                      in a template-argument-list.  */
3178                   && (next_token->type != CPP_RSHIFT
3179                       || (cxx_dialect == cxx98)
3180                       || parser->greater_than_is_operator_p))
3181                 cast_p = false;
3182             }
3183
3184           /* If we are within a cast, then the constraint that the
3185              cast is to an integral or enumeration type will be
3186              checked at that point.  If we are not within a cast, then
3187              this code is invalid.  */
3188           if (!cast_p)
3189             cp_parser_non_integral_constant_expression
3190               (parser, "floating-point literal");
3191         }
3192       return token->u.value;
3193
3194     case CPP_STRING:
3195     case CPP_STRING16:
3196     case CPP_STRING32:
3197     case CPP_WSTRING:
3198       /* ??? Should wide strings be allowed when parser->translate_strings_p
3199          is false (i.e. in attributes)?  If not, we can kill the third
3200          argument to cp_parser_string_literal.  */
3201       return cp_parser_string_literal (parser,
3202                                        parser->translate_strings_p,
3203                                        true);
3204
3205     case CPP_OPEN_PAREN:
3206       {
3207         tree expr;
3208         bool saved_greater_than_is_operator_p;
3209
3210         /* Consume the `('.  */
3211         cp_lexer_consume_token (parser->lexer);
3212         /* Within a parenthesized expression, a `>' token is always
3213            the greater-than operator.  */
3214         saved_greater_than_is_operator_p
3215           = parser->greater_than_is_operator_p;
3216         parser->greater_than_is_operator_p = true;
3217         /* If we see `( { ' then we are looking at the beginning of
3218            a GNU statement-expression.  */
3219         if (cp_parser_allow_gnu_extensions_p (parser)
3220             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221           {
3222             /* Statement-expressions are not allowed by the standard.  */
3223             pedwarn (token->location, OPT_pedantic, 
3224                      "ISO C++ forbids braced-groups within expressions");
3225
3226             /* And they're not allowed outside of a function-body; you
3227                cannot, for example, write:
3228
3229                  int i = ({ int j = 3; j + 1; });
3230
3231                at class or namespace scope.  */
3232             if (!parser->in_function_body
3233                 || parser->in_template_argument_list_p)
3234               {
3235                 error ("%Hstatement-expressions are not allowed outside "
3236                        "functions nor in template-argument lists",
3237                        &token->location);
3238                 cp_parser_skip_to_end_of_block_or_statement (parser);
3239                 expr = error_mark_node;
3240               }
3241             else
3242               {
3243                 /* Start the statement-expression.  */
3244                 expr = begin_stmt_expr ();
3245                 /* Parse the compound-statement.  */
3246                 cp_parser_compound_statement (parser, expr, false);
3247                 /* Finish up.  */
3248                 expr = finish_stmt_expr (expr, false);
3249               }
3250           }
3251         else
3252           {
3253             /* Parse the parenthesized expression.  */
3254             expr = cp_parser_expression (parser, cast_p);
3255             /* Let the front end know that this expression was
3256                enclosed in parentheses. This matters in case, for
3257                example, the expression is of the form `A::B', since
3258                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259                not.  */
3260             finish_parenthesized_expr (expr);
3261           }
3262         /* The `>' token might be the end of a template-id or
3263            template-parameter-list now.  */
3264         parser->greater_than_is_operator_p
3265           = saved_greater_than_is_operator_p;
3266         /* Consume the `)'.  */
3267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268           cp_parser_skip_to_end_of_statement (parser);
3269
3270         return expr;
3271       }
3272
3273     case CPP_KEYWORD:
3274       switch (token->keyword)
3275         {
3276           /* These two are the boolean literals.  */
3277         case RID_TRUE:
3278           cp_lexer_consume_token (parser->lexer);
3279           return boolean_true_node;
3280         case RID_FALSE:
3281           cp_lexer_consume_token (parser->lexer);
3282           return boolean_false_node;
3283
3284           /* The `__null' literal.  */
3285         case RID_NULL:
3286           cp_lexer_consume_token (parser->lexer);
3287           return null_node;
3288
3289           /* Recognize the `this' keyword.  */
3290         case RID_THIS:
3291           cp_lexer_consume_token (parser->lexer);
3292           if (parser->local_variables_forbidden_p)
3293             {
3294               error ("%H%<this%> may not be used in this context",
3295                      &token->location);
3296               return error_mark_node;
3297             }
3298           /* Pointers cannot appear in constant-expressions.  */
3299           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300             return error_mark_node;
3301           return finish_this_expr ();
3302
3303           /* The `operator' keyword can be the beginning of an
3304              id-expression.  */
3305         case RID_OPERATOR:
3306           goto id_expression;
3307
3308         case RID_FUNCTION_NAME:
3309         case RID_PRETTY_FUNCTION_NAME:
3310         case RID_C99_FUNCTION_NAME:
3311           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312              __func__ are the names of variables -- but they are
3313              treated specially.  Therefore, they are handled here,
3314              rather than relying on the generic id-expression logic
3315              below.  Grammatically, these names are id-expressions.
3316
3317              Consume the token.  */
3318           token = cp_lexer_consume_token (parser->lexer);
3319           /* Look up the name.  */
3320           return finish_fname (token->u.value);
3321
3322         case RID_VA_ARG:
3323           {
3324             tree expression;
3325             tree type;
3326
3327             /* The `__builtin_va_arg' construct is used to handle
3328                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3329             cp_lexer_consume_token (parser->lexer);
3330             /* Look for the opening `('.  */
3331             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332             /* Now, parse the assignment-expression.  */
3333             expression = cp_parser_assignment_expression (parser,
3334                                                           /*cast_p=*/false);
3335             /* Look for the `,'.  */
3336             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337             /* Parse the type-id.  */
3338             type = cp_parser_type_id (parser);
3339             /* Look for the closing `)'.  */
3340             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341             /* Using `va_arg' in a constant-expression is not
3342                allowed.  */
3343             if (cp_parser_non_integral_constant_expression (parser,
3344                                                             "%<va_arg%>"))
3345               return error_mark_node;
3346             return build_x_va_arg (expression, type);
3347           }
3348
3349         case RID_OFFSETOF:
3350           return cp_parser_builtin_offsetof (parser);
3351
3352         case RID_HAS_NOTHROW_ASSIGN:
3353         case RID_HAS_NOTHROW_CONSTRUCTOR:
3354         case RID_HAS_NOTHROW_COPY:        
3355         case RID_HAS_TRIVIAL_ASSIGN:
3356         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357         case RID_HAS_TRIVIAL_COPY:        
3358         case RID_HAS_TRIVIAL_DESTRUCTOR:
3359         case RID_HAS_VIRTUAL_DESTRUCTOR:
3360         case RID_IS_ABSTRACT:
3361         case RID_IS_BASE_OF:
3362         case RID_IS_CLASS:
3363         case RID_IS_CONVERTIBLE_TO:
3364         case RID_IS_EMPTY:
3365         case RID_IS_ENUM:
3366         case RID_IS_POD:
3367         case RID_IS_POLYMORPHIC:
3368         case RID_IS_UNION:
3369           return cp_parser_trait_expr (parser, token->keyword);
3370
3371         /* Objective-C++ expressions.  */
3372         case RID_AT_ENCODE:
3373         case RID_AT_PROTOCOL:
3374         case RID_AT_SELECTOR:
3375           return cp_parser_objc_expression (parser);
3376
3377         default:
3378           cp_parser_error (parser, "expected primary-expression");
3379           return error_mark_node;
3380         }
3381
3382       /* An id-expression can start with either an identifier, a
3383          `::' as the beginning of a qualified-id, or the "operator"
3384          keyword.  */
3385     case CPP_NAME:
3386     case CPP_SCOPE:
3387     case CPP_TEMPLATE_ID:
3388     case CPP_NESTED_NAME_SPECIFIER:
3389       {
3390         tree id_expression;
3391         tree decl;
3392         const char *error_msg;
3393         bool template_p;
3394         bool done;
3395         cp_token *id_expr_token;
3396
3397       id_expression:
3398         /* Parse the id-expression.  */
3399         id_expression
3400           = cp_parser_id_expression (parser,
3401                                      /*template_keyword_p=*/false,
3402                                      /*check_dependency_p=*/true,
3403                                      &template_p,
3404                                      /*declarator_p=*/false,
3405                                      /*optional_p=*/false);
3406         if (id_expression == error_mark_node)
3407           return error_mark_node;
3408         id_expr_token = token;
3409         token = cp_lexer_peek_token (parser->lexer);
3410         done = (token->type != CPP_OPEN_SQUARE
3411                 && token->type != CPP_OPEN_PAREN
3412                 && token->type != CPP_DOT
3413                 && token->type != CPP_DEREF
3414                 && token->type != CPP_PLUS_PLUS
3415                 && token->type != CPP_MINUS_MINUS);
3416         /* If we have a template-id, then no further lookup is
3417            required.  If the template-id was for a template-class, we
3418            will sometimes have a TYPE_DECL at this point.  */
3419         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420                  || TREE_CODE (id_expression) == TYPE_DECL)
3421           decl = id_expression;
3422         /* Look up the name.  */
3423         else
3424           {
3425             tree ambiguous_decls;
3426
3427             decl = cp_parser_lookup_name (parser, id_expression,
3428                                           none_type,
3429                                           template_p,
3430                                           /*is_namespace=*/false,
3431                                           /*check_dependency=*/true,
3432                                           &ambiguous_decls,
3433                                           id_expr_token->location);
3434             /* If the lookup was ambiguous, an error will already have
3435                been issued.  */
3436             if (ambiguous_decls)
3437               return error_mark_node;
3438
3439             /* In Objective-C++, an instance variable (ivar) may be preferred
3440                to whatever cp_parser_lookup_name() found.  */
3441             decl = objc_lookup_ivar (decl, id_expression);
3442
3443             /* If name lookup gives us a SCOPE_REF, then the
3444                qualifying scope was dependent.  */
3445             if (TREE_CODE (decl) == SCOPE_REF)
3446               {
3447                 /* At this point, we do not know if DECL is a valid
3448                    integral constant expression.  We assume that it is
3449                    in fact such an expression, so that code like:
3450
3451                       template <int N> struct A {
3452                         int a[B<N>::i];
3453                       };
3454                      
3455                    is accepted.  At template-instantiation time, we
3456                    will check that B<N>::i is actually a constant.  */
3457                 return decl;
3458               }
3459             /* Check to see if DECL is a local variable in a context
3460                where that is forbidden.  */
3461             if (parser->local_variables_forbidden_p
3462                 && local_variable_p (decl))
3463               {
3464                 /* It might be that we only found DECL because we are
3465                    trying to be generous with pre-ISO scoping rules.
3466                    For example, consider:
3467
3468                      int i;
3469                      void g() {
3470                        for (int i = 0; i < 10; ++i) {}
3471                        extern void f(int j = i);
3472                      }
3473
3474                    Here, name look up will originally find the out
3475                    of scope `i'.  We need to issue a warning message,
3476                    but then use the global `i'.  */
3477                 decl = check_for_out_of_scope_variable (decl);
3478                 if (local_variable_p (decl))
3479                   {
3480                     error ("%Hlocal variable %qD may not appear in this context",
3481                            &id_expr_token->location, decl);
3482                     return error_mark_node;
3483                   }
3484               }
3485           }
3486
3487         decl = (finish_id_expression
3488                 (id_expression, decl, parser->scope,
3489                  idk,
3490                  parser->integral_constant_expression_p,
3491                  parser->allow_non_integral_constant_expression_p,
3492                  &parser->non_integral_constant_expression_p,
3493                  template_p, done, address_p,
3494                  template_arg_p,
3495                  &error_msg,
3496                  id_expr_token->location));
3497         if (error_msg)
3498           cp_parser_error (parser, error_msg);
3499         return decl;
3500       }
3501
3502       /* Anything else is an error.  */
3503     default:
3504       /* ...unless we have an Objective-C++ message or string literal,
3505          that is.  */
3506       if (c_dialect_objc ()
3507           && (token->type == CPP_OPEN_SQUARE
3508               || token->type == CPP_OBJC_STRING))
3509         return cp_parser_objc_expression (parser);
3510
3511       cp_parser_error (parser, "expected primary-expression");
3512       return error_mark_node;
3513     }
3514 }
3515
3516 /* Parse an id-expression.
3517
3518    id-expression:
3519      unqualified-id
3520      qualified-id
3521
3522    qualified-id:
3523      :: [opt] nested-name-specifier template [opt] unqualified-id
3524      :: identifier
3525      :: operator-function-id
3526      :: template-id
3527
3528    Return a representation of the unqualified portion of the
3529    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3530    a `::' or nested-name-specifier.
3531
3532    Often, if the id-expression was a qualified-id, the caller will
3533    want to make a SCOPE_REF to represent the qualified-id.  This
3534    function does not do this in order to avoid wastefully creating
3535    SCOPE_REFs when they are not required.
3536
3537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538    `template' keyword.
3539
3540    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541    uninstantiated templates.
3542
3543    If *TEMPLATE_P is non-NULL, it is set to true iff the
3544    `template' keyword is used to explicitly indicate that the entity
3545    named is a template.
3546
3547    If DECLARATOR_P is true, the id-expression is appearing as part of
3548    a declarator, rather than as part of an expression.  */
3549
3550 static tree
3551 cp_parser_id_expression (cp_parser *parser,
3552                          bool template_keyword_p,
3553                          bool check_dependency_p,
3554                          bool *template_p,
3555                          bool declarator_p,
3556                          bool optional_p)
3557 {
3558   bool global_scope_p;
3559   bool nested_name_specifier_p;
3560
3561   /* Assume the `template' keyword was not used.  */
3562   if (template_p)
3563     *template_p = template_keyword_p;
3564
3565   /* Look for the optional `::' operator.  */
3566   global_scope_p
3567     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3568        != NULL_TREE);
3569   /* Look for the optional nested-name-specifier.  */
3570   nested_name_specifier_p
3571     = (cp_parser_nested_name_specifier_opt (parser,
3572                                             /*typename_keyword_p=*/false,
3573                                             check_dependency_p,
3574                                             /*type_p=*/false,
3575                                             declarator_p)
3576        != NULL_TREE);
3577   /* If there is a nested-name-specifier, then we are looking at
3578      the first qualified-id production.  */
3579   if (nested_name_specifier_p)
3580     {
3581       tree saved_scope;
3582       tree saved_object_scope;
3583       tree saved_qualifying_scope;
3584       tree unqualified_id;
3585       bool is_template;
3586
3587       /* See if the next token is the `template' keyword.  */
3588       if (!template_p)
3589         template_p = &is_template;
3590       *template_p = cp_parser_optional_template_keyword (parser);
3591       /* Name lookup we do during the processing of the
3592          unqualified-id might obliterate SCOPE.  */
3593       saved_scope = parser->scope;
3594       saved_object_scope = parser->object_scope;
3595       saved_qualifying_scope = parser->qualifying_scope;
3596       /* Process the final unqualified-id.  */
3597       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598                                                  check_dependency_p,
3599                                                  declarator_p,
3600                                                  /*optional_p=*/false);
3601       /* Restore the SAVED_SCOPE for our caller.  */
3602       parser->scope = saved_scope;
3603       parser->object_scope = saved_object_scope;
3604       parser->qualifying_scope = saved_qualifying_scope;
3605
3606       return unqualified_id;
3607     }
3608   /* Otherwise, if we are in global scope, then we are looking at one
3609      of the other qualified-id productions.  */
3610   else if (global_scope_p)
3611     {
3612       cp_token *token;
3613       tree id;
3614
3615       /* Peek at the next token.  */
3616       token = cp_lexer_peek_token (parser->lexer);
3617
3618       /* If it's an identifier, and the next token is not a "<", then
3619          we can avoid the template-id case.  This is an optimization
3620          for this common case.  */
3621       if (token->type == CPP_NAME
3622           && !cp_parser_nth_token_starts_template_argument_list_p
3623                (parser, 2))
3624         return cp_parser_identifier (parser);
3625
3626       cp_parser_parse_tentatively (parser);
3627       /* Try a template-id.  */
3628       id = cp_parser_template_id (parser,
3629                                   /*template_keyword_p=*/false,
3630                                   /*check_dependency_p=*/true,
3631                                   declarator_p);
3632       /* If that worked, we're done.  */
3633       if (cp_parser_parse_definitely (parser))
3634         return id;
3635
3636       /* Peek at the next token.  (Changes in the token buffer may
3637          have invalidated the pointer obtained above.)  */
3638       token = cp_lexer_peek_token (parser->lexer);
3639
3640       switch (token->type)
3641         {
3642         case CPP_NAME:
3643           return cp_parser_identifier (parser);
3644
3645         case CPP_KEYWORD:
3646           if (token->keyword == RID_OPERATOR)
3647             return cp_parser_operator_function_id (parser);
3648           /* Fall through.  */
3649
3650         default:
3651           cp_parser_error (parser, "expected id-expression");
3652           return error_mark_node;
3653         }
3654     }
3655   else
3656     return cp_parser_unqualified_id (parser, template_keyword_p,
3657                                      /*check_dependency_p=*/true,
3658                                      declarator_p,
3659                                      optional_p);
3660 }
3661
3662 /* Parse an unqualified-id.
3663
3664    unqualified-id:
3665      identifier
3666      operator-function-id
3667      conversion-function-id
3668      ~ class-name
3669      template-id
3670
3671    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672    keyword, in a construct like `A::template ...'.
3673
3674    Returns a representation of unqualified-id.  For the `identifier'
3675    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3676    production a BIT_NOT_EXPR is returned; the operand of the
3677    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3678    other productions, see the documentation accompanying the
3679    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3680    names are looked up in uninstantiated templates.  If DECLARATOR_P
3681    is true, the unqualified-id is appearing as part of a declarator,
3682    rather than as part of an expression.  */
3683
3684 static tree
3685 cp_parser_unqualified_id (cp_parser* parser,
3686                           bool template_keyword_p,
3687                           bool check_dependency_p,
3688                           bool declarator_p,
3689                           bool optional_p)
3690 {
3691   cp_token *token;
3692
3693   /* Peek at the next token.  */
3694   token = cp_lexer_peek_token (parser->lexer);
3695
3696   switch (token->type)
3697     {
3698     case CPP_NAME:
3699       {
3700         tree id;
3701
3702         /* We don't know yet whether or not this will be a
3703            template-id.  */
3704         cp_parser_parse_tentatively (parser);
3705         /* Try a template-id.  */
3706         id = cp_parser_template_id (parser, template_keyword_p,
3707                                     check_dependency_p,
3708                                     declarator_p);
3709         /* If it worked, we're done.  */
3710         if (cp_parser_parse_definitely (parser))
3711           return id;
3712         /* Otherwise, it's an ordinary identifier.  */
3713         return cp_parser_identifier (parser);
3714       }
3715
3716     case CPP_TEMPLATE_ID:
3717       return cp_parser_template_id (parser, template_keyword_p,
3718                                     check_dependency_p,
3719                                     declarator_p);
3720
3721     case CPP_COMPL:
3722       {
3723         tree type_decl;
3724         tree qualifying_scope;
3725         tree object_scope;
3726         tree scope;
3727         bool done;
3728
3729         /* Consume the `~' token.  */
3730         cp_lexer_consume_token (parser->lexer);
3731         /* Parse the class-name.  The standard, as written, seems to
3732            say that:
3733
3734              template <typename T> struct S { ~S (); };
3735              template <typename T> S<T>::~S() {}
3736
3737            is invalid, since `~' must be followed by a class-name, but
3738            `S<T>' is dependent, and so not known to be a class.
3739            That's not right; we need to look in uninstantiated
3740            templates.  A further complication arises from:
3741
3742              template <typename T> void f(T t) {
3743                t.T::~T();
3744              }
3745
3746            Here, it is not possible to look up `T' in the scope of `T'
3747            itself.  We must look in both the current scope, and the
3748            scope of the containing complete expression.
3749
3750            Yet another issue is:
3751
3752              struct S {
3753                int S;
3754                ~S();
3755              };
3756
3757              S::~S() {}
3758
3759            The standard does not seem to say that the `S' in `~S'
3760            should refer to the type `S' and not the data member
3761            `S::S'.  */
3762
3763         /* DR 244 says that we look up the name after the "~" in the
3764            same scope as we looked up the qualifying name.  That idea
3765            isn't fully worked out; it's more complicated than that.  */
3766         scope = parser->scope;
3767         object_scope = parser->object_scope;
3768         qualifying_scope = parser->qualifying_scope;
3769
3770         /* Check for invalid scopes.  */
3771         if (scope == error_mark_node)
3772           {
3773             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774               cp_lexer_consume_token (parser->lexer);
3775             return error_mark_node;
3776           }
3777         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3778           {
3779             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780               error ("%Hscope %qT before %<~%> is not a class-name",
3781                      &token->location, scope);
3782             cp_parser_simulate_error (parser);
3783             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784               cp_lexer_consume_token (parser->lexer);
3785             return error_mark_node;
3786           }
3787         gcc_assert (!scope || TYPE_P (scope));
3788
3789         /* If the name is of the form "X::~X" it's OK.  */
3790         token = cp_lexer_peek_token (parser->lexer);
3791         if (scope
3792             && token->type == CPP_NAME
3793             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3794                 == CPP_OPEN_PAREN)
3795             && constructor_name_p (token->u.value, scope))
3796           {
3797             cp_lexer_consume_token (parser->lexer);
3798             return build_nt (BIT_NOT_EXPR, scope);
3799           }
3800
3801         /* If there was an explicit qualification (S::~T), first look
3802            in the scope given by the qualification (i.e., S).  */
3803         done = false;
3804         type_decl = NULL_TREE;
3805         if (scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             type_decl = cp_parser_class_name (parser,
3809                                               /*typename_keyword_p=*/false,
3810                                               /*template_keyword_p=*/false,
3811                                               none_type,
3812                                               /*check_dependency=*/false,
3813                                               /*class_head_p=*/false,
3814                                               declarator_p);
3815             if (cp_parser_parse_definitely (parser))
3816               done = true;
3817           }
3818         /* In "N::S::~S", look in "N" as well.  */
3819         if (!done && scope && qualifying_scope)
3820           {
3821             cp_parser_parse_tentatively (parser);
3822             parser->scope = qualifying_scope;
3823             parser->object_scope = NULL_TREE;
3824             parser->qualifying_scope = NULL_TREE;
3825             type_decl
3826               = cp_parser_class_name (parser,
3827                                       /*typename_keyword_p=*/false,
3828                                       /*template_keyword_p=*/false,
3829                                       none_type,
3830                                       /*check_dependency=*/false,
3831                                       /*class_head_p=*/false,
3832                                       declarator_p);
3833             if (cp_parser_parse_definitely (parser))
3834               done = true;
3835           }
3836         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3837         else if (!done && object_scope)
3838           {
3839             cp_parser_parse_tentatively (parser);
3840             parser->scope = object_scope;
3841             parser->object_scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             type_decl
3844               = cp_parser_class_name (parser,
3845                                       /*typename_keyword_p=*/false,
3846                                       /*template_keyword_p=*/false,
3847                                       none_type,
3848                                       /*check_dependency=*/false,
3849                                       /*class_head_p=*/false,
3850                                       declarator_p);
3851             if (cp_parser_parse_definitely (parser))
3852               done = true;
3853           }
3854         /* Look in the surrounding context.  */
3855         if (!done)
3856           {
3857             parser->scope = NULL_TREE;
3858             parser->object_scope = NULL_TREE;
3859             parser->qualifying_scope = NULL_TREE;
3860             type_decl
3861               = cp_parser_class_name (parser,
3862                                       /*typename_keyword_p=*/false,
3863                                       /*template_keyword_p=*/false,
3864                                       none_type,
3865                                       /*check_dependency=*/false,
3866                                       /*class_head_p=*/false,
3867                                       declarator_p);
3868           }
3869         /* If an error occurred, assume that the name of the
3870            destructor is the same as the name of the qualifying
3871            class.  That allows us to keep parsing after running
3872            into ill-formed destructor names.  */
3873         if (type_decl == error_mark_node && scope)
3874           return build_nt (BIT_NOT_EXPR, scope);
3875         else if (type_decl == error_mark_node)
3876           return error_mark_node;
3877
3878         /* Check that destructor name and scope match.  */
3879         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3880           {
3881             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882               error ("%Hdeclaration of %<~%T%> as member of %qT",
3883                      &token->location, type_decl, scope);
3884             cp_parser_simulate_error (parser);
3885             return error_mark_node;
3886           }
3887
3888         /* [class.dtor]
3889
3890            A typedef-name that names a class shall not be used as the
3891            identifier in the declarator for a destructor declaration.  */
3892         if (declarator_p
3893             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894             && !DECL_SELF_REFERENCE_P (type_decl)
3895             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896           error ("%Htypedef-name %qD used as destructor declarator",
3897                  &token->location, type_decl);
3898
3899         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3900       }
3901
3902     case CPP_KEYWORD:
3903       if (token->keyword == RID_OPERATOR)
3904         {
3905           tree id;
3906
3907           /* This could be a template-id, so we try that first.  */
3908           cp_parser_parse_tentatively (parser);
3909           /* Try a template-id.  */
3910           id = cp_parser_template_id (parser, template_keyword_p,
3911                                       /*check_dependency_p=*/true,
3912                                       declarator_p);
3913           /* If that worked, we're done.  */
3914           if (cp_parser_parse_definitely (parser))
3915             return id;
3916           /* We still don't know whether we're looking at an
3917              operator-function-id or a conversion-function-id.  */
3918           cp_parser_parse_tentatively (parser);
3919           /* Try an operator-function-id.  */
3920           id = cp_parser_operator_function_id (parser);
3921           /* If that didn't work, try a conversion-function-id.  */
3922           if (!cp_parser_parse_definitely (parser))
3923             id = cp_parser_conversion_function_id (parser);
3924
3925           return id;
3926         }
3927       /* Fall through.  */
3928
3929     default:
3930       if (optional_p)
3931         return NULL_TREE;
3932       cp_parser_error (parser, "expected unqualified-id");
3933       return error_mark_node;
3934     }
3935 }
3936
3937 /* Parse an (optional) nested-name-specifier.
3938
3939    nested-name-specifier: [C++98]
3940      class-or-namespace-name :: nested-name-specifier [opt]
3941      class-or-namespace-name :: template nested-name-specifier [opt]
3942
3943    nested-name-specifier: [C++0x]
3944      type-name ::
3945      namespace-name ::
3946      nested-name-specifier identifier ::
3947      nested-name-specifier template [opt] simple-template-id ::
3948
3949    PARSER->SCOPE should be set appropriately before this function is
3950    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3951    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3952    in name lookups.
3953
3954    Sets PARSER->SCOPE to the class (TYPE) or namespace
3955    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3956    it unchanged if there is no nested-name-specifier.  Returns the new
3957    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3958
3959    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3960    part of a declaration and/or decl-specifier.  */
3961
3962 static tree
3963 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3964                                      bool typename_keyword_p,
3965                                      bool check_dependency_p,
3966                                      bool type_p,
3967                                      bool is_declaration)
3968 {
3969   bool success = false;
3970   cp_token_position start = 0;
3971   cp_token *token;
3972
3973   /* Remember where the nested-name-specifier starts.  */
3974   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3975     {
3976       start = cp_lexer_token_position (parser->lexer, false);
3977       push_deferring_access_checks (dk_deferred);
3978     }
3979
3980   while (true)
3981     {
3982       tree new_scope;
3983       tree old_scope;
3984       tree saved_qualifying_scope;
3985       bool template_keyword_p;
3986
3987       /* Spot cases that cannot be the beginning of a
3988          nested-name-specifier.  */
3989       token = cp_lexer_peek_token (parser->lexer);
3990
3991       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3992          the already parsed nested-name-specifier.  */
3993       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3994         {
3995           /* Grab the nested-name-specifier and continue the loop.  */
3996           cp_parser_pre_parsed_nested_name_specifier (parser);
3997           /* If we originally encountered this nested-name-specifier
3998              with IS_DECLARATION set to false, we will not have
3999              resolved TYPENAME_TYPEs, so we must do so here.  */
4000           if (is_declaration
4001               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4002             {
4003               new_scope = resolve_typename_type (parser->scope,
4004                                                  /*only_current_p=*/false);
4005               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4006                 parser->scope = new_scope;
4007             }
4008           success = true;
4009           continue;
4010         }
4011
4012       /* Spot cases that cannot be the beginning of a
4013          nested-name-specifier.  On the second and subsequent times
4014          through the loop, we look for the `template' keyword.  */
4015       if (success && token->keyword == RID_TEMPLATE)
4016         ;
4017       /* A template-id can start a nested-name-specifier.  */
4018       else if (token->type == CPP_TEMPLATE_ID)
4019         ;
4020       else
4021         {
4022           /* If the next token is not an identifier, then it is
4023              definitely not a type-name or namespace-name.  */
4024           if (token->type != CPP_NAME)
4025             break;
4026           /* If the following token is neither a `<' (to begin a
4027              template-id), nor a `::', then we are not looking at a
4028              nested-name-specifier.  */
4029           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4030           if (token->type != CPP_SCOPE
4031               && !cp_parser_nth_token_starts_template_argument_list_p
4032                   (parser, 2))
4033             break;
4034         }
4035
4036       /* The nested-name-specifier is optional, so we parse
4037          tentatively.  */
4038       cp_parser_parse_tentatively (parser);
4039
4040       /* Look for the optional `template' keyword, if this isn't the
4041          first time through the loop.  */
4042       if (success)
4043         template_keyword_p = cp_parser_optional_template_keyword (parser);
4044       else
4045         template_keyword_p = false;
4046
4047       /* Save the old scope since the name lookup we are about to do
4048          might destroy it.  */
4049       old_scope = parser->scope;
4050       saved_qualifying_scope = parser->qualifying_scope;
4051       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4052          look up names in "X<T>::I" in order to determine that "Y" is
4053          a template.  So, if we have a typename at this point, we make
4054          an effort to look through it.  */
4055       if (is_declaration
4056           && !typename_keyword_p
4057           && parser->scope
4058           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059         parser->scope = resolve_typename_type (parser->scope,
4060                                                /*only_current_p=*/false);
4061       /* Parse the qualifying entity.  */
4062       new_scope
4063         = cp_parser_qualifying_entity (parser,
4064                                        typename_keyword_p,
4065                                        template_keyword_p,
4066                                        check_dependency_p,
4067                                        type_p,
4068                                        is_declaration);
4069       /* Look for the `::' token.  */
4070       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4071
4072       /* If we found what we wanted, we keep going; otherwise, we're
4073          done.  */
4074       if (!cp_parser_parse_definitely (parser))
4075         {
4076           bool error_p = false;
4077
4078           /* Restore the OLD_SCOPE since it was valid before the
4079              failed attempt at finding the last
4080              class-or-namespace-name.  */
4081           parser->scope = old_scope;
4082           parser->qualifying_scope = saved_qualifying_scope;
4083           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4084             break;
4085           /* If the next token is an identifier, and the one after
4086              that is a `::', then any valid interpretation would have
4087              found a class-or-namespace-name.  */
4088           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4089                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4090                      == CPP_SCOPE)
4091                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4092                      != CPP_COMPL))
4093             {
4094               token = cp_lexer_consume_token (parser->lexer);
4095               if (!error_p)
4096                 {
4097                   if (!token->ambiguous_p)
4098                     {
4099                       tree decl;
4100                       tree ambiguous_decls;
4101
4102                       decl = cp_parser_lookup_name (parser, token->u.value,
4103                                                     none_type,
4104                                                     /*is_template=*/false,
4105                                                     /*is_namespace=*/false,
4106                                                     /*check_dependency=*/true,
4107                                                     &ambiguous_decls,
4108                                                     token->location);
4109                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4110                         error ("%H%qD used without template parameters",
4111                                &token->location, decl);
4112                       else if (ambiguous_decls)
4113                         {
4114                           error ("%Hreference to %qD is ambiguous",
4115                                  &token->location, token->u.value);
4116                           print_candidates (ambiguous_decls);
4117                           decl = error_mark_node;
4118                         }
4119                       else
4120                         {
4121                           const char* msg = "is not a class or namespace";
4122                           if (cxx_dialect != cxx98)
4123                             msg = "is not a class, namespace, or enumeration";
4124                           cp_parser_name_lookup_error
4125                             (parser, token->u.value, decl, msg,
4126                              token->location);
4127                         }
4128                     }
4129                   parser->scope = error_mark_node;
4130                   error_p = true;
4131                   /* Treat this as a successful nested-name-specifier
4132                      due to:
4133
4134                      [basic.lookup.qual]
4135
4136                      If the name found is not a class-name (clause
4137                      _class_) or namespace-name (_namespace.def_), the
4138                      program is ill-formed.  */
4139                   success = true;
4140                 }
4141               cp_lexer_consume_token (parser->lexer);
4142             }
4143           break;
4144         }
4145       /* We've found one valid nested-name-specifier.  */
4146       success = true;
4147       /* Name lookup always gives us a DECL.  */
4148       if (TREE_CODE (new_scope) == TYPE_DECL)
4149         new_scope = TREE_TYPE (new_scope);
4150       /* Uses of "template" must be followed by actual templates.  */
4151       if (template_keyword_p
4152           && !(CLASS_TYPE_P (new_scope)
4153                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4154                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4155                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4156           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4157                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4158                    == TEMPLATE_ID_EXPR)))
4159         permerror (input_location, TYPE_P (new_scope)
4160                    ? "%qT is not a template"
4161                    : "%qD is not a template",
4162                    new_scope);
4163       /* If it is a class scope, try to complete it; we are about to
4164          be looking up names inside the class.  */
4165       if (TYPE_P (new_scope)
4166           /* Since checking types for dependency can be expensive,
4167              avoid doing it if the type is already complete.  */
4168           && !COMPLETE_TYPE_P (new_scope)
4169           /* Do not try to complete dependent types.  */
4170           && !dependent_type_p (new_scope))
4171         {
4172           new_scope = complete_type (new_scope);
4173           /* If it is a typedef to current class, use the current
4174              class instead, as the typedef won't have any names inside
4175              it yet.  */
4176           if (!COMPLETE_TYPE_P (new_scope)
4177               && currently_open_class (new_scope))
4178             new_scope = TYPE_MAIN_VARIANT (new_scope);
4179         }
4180       /* Make sure we look in the right scope the next time through
4181          the loop.  */
4182       parser->scope = new_scope;
4183     }
4184
4185   /* If parsing tentatively, replace the sequence of tokens that makes
4186      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4187      token.  That way, should we re-parse the token stream, we will
4188      not have to repeat the effort required to do the parse, nor will
4189      we issue duplicate error messages.  */
4190   if (success && start)
4191     {
4192       cp_token *token;
4193
4194       token = cp_lexer_token_at (parser->lexer, start);
4195       /* Reset the contents of the START token.  */
4196       token->type = CPP_NESTED_NAME_SPECIFIER;
4197       /* Retrieve any deferred checks.  Do not pop this access checks yet
4198          so the memory will not be reclaimed during token replacing below.  */
4199       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4200       token->u.tree_check_value->value = parser->scope;
4201       token->u.tree_check_value->checks = get_deferred_access_checks ();
4202       token->u.tree_check_value->qualifying_scope =
4203         parser->qualifying_scope;
4204       token->keyword = RID_MAX;
4205
4206       /* Purge all subsequent tokens.  */
4207       cp_lexer_purge_tokens_after (parser->lexer, start);
4208     }
4209
4210   if (start)
4211     pop_to_parent_deferring_access_checks ();
4212
4213   return success ? parser->scope : NULL_TREE;
4214 }
4215
4216 /* Parse a nested-name-specifier.  See
4217    cp_parser_nested_name_specifier_opt for details.  This function
4218    behaves identically, except that it will an issue an error if no
4219    nested-name-specifier is present.  */
4220
4221 static tree
4222 cp_parser_nested_name_specifier (cp_parser *parser,
4223                                  bool typename_keyword_p,
4224                                  bool check_dependency_p,
4225                                  bool type_p,
4226                                  bool is_declaration)
4227 {
4228   tree scope;
4229
4230   /* Look for the nested-name-specifier.  */
4231   scope = cp_parser_nested_name_specifier_opt (parser,
4232                                                typename_keyword_p,
4233                                                check_dependency_p,
4234                                                type_p,
4235                                                is_declaration);
4236   /* If it was not present, issue an error message.  */
4237   if (!scope)
4238     {
4239       cp_parser_error (parser, "expected nested-name-specifier");
4240       parser->scope = NULL_TREE;
4241     }
4242
4243   return scope;
4244 }
4245
4246 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4247    this is either a class-name or a namespace-name (which corresponds
4248    to the class-or-namespace-name production in the grammar). For
4249    C++0x, it can also be a type-name that refers to an enumeration
4250    type.
4251
4252    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4253    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4254    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4255    TYPE_P is TRUE iff the next name should be taken as a class-name,
4256    even the same name is declared to be another entity in the same
4257    scope.
4258
4259    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4260    specified by the class-or-namespace-name.  If neither is found the
4261    ERROR_MARK_NODE is returned.  */
4262
4263 static tree
4264 cp_parser_qualifying_entity (cp_parser *parser,
4265                              bool typename_keyword_p,
4266                              bool template_keyword_p,
4267                              bool check_dependency_p,
4268                              bool type_p,
4269                              bool is_declaration)
4270 {
4271   tree saved_scope;
4272   tree saved_qualifying_scope;
4273   tree saved_object_scope;
4274   tree scope;
4275   bool only_class_p;
4276   bool successful_parse_p;
4277
4278   /* Before we try to parse the class-name, we must save away the
4279      current PARSER->SCOPE since cp_parser_class_name will destroy
4280      it.  */
4281   saved_scope = parser->scope;
4282   saved_qualifying_scope = parser->qualifying_scope;
4283   saved_object_scope = parser->object_scope;
4284   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4285      there is no need to look for a namespace-name.  */
4286   only_class_p = template_keyword_p 
4287     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4288   if (!only_class_p)
4289     cp_parser_parse_tentatively (parser);
4290   scope = cp_parser_class_name (parser,
4291                                 typename_keyword_p,
4292                                 template_keyword_p,
4293                                 type_p ? class_type : none_type,
4294                                 check_dependency_p,
4295                                 /*class_head_p=*/false,
4296                                 is_declaration);
4297   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4298   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4299   if (!only_class_p 
4300       && cxx_dialect != cxx98
4301       && !successful_parse_p)
4302     {
4303       /* Restore the saved scope.  */
4304       parser->scope = saved_scope;
4305       parser->qualifying_scope = saved_qualifying_scope;
4306       parser->object_scope = saved_object_scope;
4307
4308       /* Parse tentatively.  */
4309       cp_parser_parse_tentatively (parser);
4310      
4311       /* Parse a typedef-name or enum-name.  */
4312       scope = cp_parser_nonclass_name (parser);
4313       successful_parse_p = cp_parser_parse_definitely (parser);
4314     }
4315   /* If that didn't work, try for a namespace-name.  */
4316   if (!only_class_p && !successful_parse_p)
4317     {
4318       /* Restore the saved scope.  */
4319       parser->scope = saved_scope;
4320       parser->qualifying_scope = saved_qualifying_scope;
4321       parser->object_scope = saved_object_scope;
4322       /* If we are not looking at an identifier followed by the scope
4323          resolution operator, then this is not part of a
4324          nested-name-specifier.  (Note that this function is only used
4325          to parse the components of a nested-name-specifier.)  */
4326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4327           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4328         return error_mark_node;
4329       scope = cp_parser_namespace_name (parser);
4330     }
4331
4332   return scope;
4333 }
4334
4335 /* Parse a postfix-expression.
4336
4337    postfix-expression:
4338      primary-expression
4339      postfix-expression [ expression ]
4340      postfix-expression ( expression-list [opt] )
4341      simple-type-specifier ( expression-list [opt] )
4342      typename :: [opt] nested-name-specifier identifier
4343        ( expression-list [opt] )
4344      typename :: [opt] nested-name-specifier template [opt] template-id
4345        ( expression-list [opt] )
4346      postfix-expression . template [opt] id-expression
4347      postfix-expression -> template [opt] id-expression
4348      postfix-expression . pseudo-destructor-name
4349      postfix-expression -> pseudo-destructor-name
4350      postfix-expression ++
4351      postfix-expression --
4352      dynamic_cast < type-id > ( expression )
4353      static_cast < type-id > ( expression )
4354      reinterpret_cast < type-id > ( expression )
4355      const_cast < type-id > ( expression )
4356      typeid ( expression )
4357      typeid ( type-id )
4358
4359    GNU Extension:
4360
4361    postfix-expression:
4362      ( type-id ) { initializer-list , [opt] }
4363
4364    This extension is a GNU version of the C99 compound-literal
4365    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4366    but they are essentially the same concept.)
4367
4368    If ADDRESS_P is true, the postfix expression is the operand of the
4369    `&' operator.  CAST_P is true if this expression is the target of a
4370    cast.
4371
4372    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4373    class member access expressions [expr.ref].
4374
4375    Returns a representation of the expression.  */
4376
4377 static tree
4378 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4379                               bool member_access_only_p)
4380 {
4381   cp_token *token;
4382   enum rid keyword;
4383   cp_id_kind idk = CP_ID_KIND_NONE;
4384   tree postfix_expression = NULL_TREE;
4385   bool is_member_access = false;
4386
4387   /* Peek at the next token.  */
4388   token = cp_lexer_peek_token (parser->lexer);
4389   /* Some of the productions are determined by keywords.  */
4390   keyword = token->keyword;
4391   switch (keyword)
4392     {
4393     case RID_DYNCAST:
4394     case RID_STATCAST:
4395     case RID_REINTCAST:
4396     case RID_CONSTCAST:
4397       {
4398         tree type;
4399         tree expression;
4400         const char *saved_message;
4401
4402         /* All of these can be handled in the same way from the point
4403            of view of parsing.  Begin by consuming the token
4404            identifying the cast.  */
4405         cp_lexer_consume_token (parser->lexer);
4406
4407         /* New types cannot be defined in the cast.  */
4408         saved_message = parser->type_definition_forbidden_message;
4409         parser->type_definition_forbidden_message
4410           = "types may not be defined in casts";
4411
4412         /* Look for the opening `<'.  */
4413         cp_parser_require (parser, CPP_LESS, "%<<%>");
4414         /* Parse the type to which we are casting.  */
4415         type = cp_parser_type_id (parser);
4416         /* Look for the closing `>'.  */
4417         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4418         /* Restore the old message.  */
4419         parser->type_definition_forbidden_message = saved_message;
4420
4421         /* And the expression which is being cast.  */
4422         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4423         expression = cp_parser_expression (parser, /*cast_p=*/true);
4424         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4425
4426         /* Only type conversions to integral or enumeration types
4427            can be used in constant-expressions.  */
4428         if (!cast_valid_in_integral_constant_expression_p (type)
4429             && (cp_parser_non_integral_constant_expression
4430                 (parser,
4431                  "a cast to a type other than an integral or "
4432                  "enumeration type")))
4433           return error_mark_node;
4434
4435         switch (keyword)
4436           {
4437           case RID_DYNCAST:
4438             postfix_expression
4439               = build_dynamic_cast (type, expression, tf_warning_or_error);
4440             break;
4441           case RID_STATCAST:
4442             postfix_expression
4443               = build_static_cast (type, expression, tf_warning_or_error);
4444             break;
4445           case RID_REINTCAST:
4446             postfix_expression
4447               = build_reinterpret_cast (type, expression, 
4448                                         tf_warning_or_error);
4449             break;
4450           case RID_CONSTCAST:
4451             postfix_expression
4452               = build_const_cast (type, expression, tf_warning_or_error);
4453             break;
4454           default:
4455             gcc_unreachable ();
4456           }
4457       }
4458       break;
4459
4460     case RID_TYPEID:
4461       {
4462         tree type;
4463         const char *saved_message;
4464         bool saved_in_type_id_in_expr_p;
4465
4466         /* Consume the `typeid' token.  */
4467         cp_lexer_consume_token (parser->lexer);
4468         /* Look for the `(' token.  */
4469         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470         /* Types cannot be defined in a `typeid' expression.  */
4471         saved_message = parser->type_definition_forbidden_message;
4472         parser->type_definition_forbidden_message
4473           = "types may not be defined in a %<typeid%> expression";
4474         /* We can't be sure yet whether we're looking at a type-id or an
4475            expression.  */
4476         cp_parser_parse_tentatively (parser);
4477         /* Try a type-id first.  */
4478         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4479         parser->in_type_id_in_expr_p = true;
4480         type = cp_parser_type_id (parser);
4481         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4482         /* Look for the `)' token.  Otherwise, we can't be sure that
4483            we're not looking at an expression: consider `typeid (int
4484            (3))', for example.  */
4485         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4486         /* If all went well, simply lookup the type-id.  */
4487         if (cp_parser_parse_definitely (parser))
4488           postfix_expression = get_typeid (type);
4489         /* Otherwise, fall back to the expression variant.  */
4490         else
4491           {
4492             tree expression;
4493
4494             /* Look for an expression.  */
4495             expression = cp_parser_expression (parser, /*cast_p=*/false);
4496             /* Compute its typeid.  */
4497             postfix_expression = build_typeid (expression);
4498             /* Look for the `)' token.  */
4499             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4500           }
4501         /* Restore the saved message.  */
4502         parser->type_definition_forbidden_message = saved_message;
4503         /* `typeid' may not appear in an integral constant expression.  */
4504         if (cp_parser_non_integral_constant_expression(parser,
4505                                                        "%<typeid%> operator"))
4506           return error_mark_node;
4507       }
4508       break;
4509
4510     case RID_TYPENAME:
4511       {
4512         tree type;
4513         /* The syntax permitted here is the same permitted for an
4514            elaborated-type-specifier.  */
4515         type = cp_parser_elaborated_type_specifier (parser,
4516                                                     /*is_friend=*/false,
4517                                                     /*is_declaration=*/false);
4518         postfix_expression = cp_parser_functional_cast (parser, type);
4519       }
4520       break;
4521
4522     default:
4523       {
4524         tree type;
4525
4526         /* If the next thing is a simple-type-specifier, we may be
4527            looking at a functional cast.  We could also be looking at
4528            an id-expression.  So, we try the functional cast, and if
4529            that doesn't work we fall back to the primary-expression.  */
4530         cp_parser_parse_tentatively (parser);
4531         /* Look for the simple-type-specifier.  */
4532         type = cp_parser_simple_type_specifier (parser,
4533                                                 /*decl_specs=*/NULL,
4534                                                 CP_PARSER_FLAGS_NONE);
4535         /* Parse the cast itself.  */
4536         if (!cp_parser_error_occurred (parser))
4537           postfix_expression
4538             = cp_parser_functional_cast (parser, type);
4539         /* If that worked, we're done.  */
4540         if (cp_parser_parse_definitely (parser))
4541           break;
4542
4543         /* If the functional-cast didn't work out, try a
4544            compound-literal.  */
4545         if (cp_parser_allow_gnu_extensions_p (parser)
4546             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4547           {
4548             VEC(constructor_elt,gc) *initializer_list = NULL;
4549             bool saved_in_type_id_in_expr_p;
4550
4551             cp_parser_parse_tentatively (parser);
4552             /* Consume the `('.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Parse the type.  */
4555             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4556             parser->in_type_id_in_expr_p = true;
4557             type = cp_parser_type_id (parser);
4558             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4559             /* Look for the `)'.  */
4560             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4561             /* Look for the `{'.  */
4562             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4563             /* If things aren't going well, there's no need to
4564                keep going.  */
4565             if (!cp_parser_error_occurred (parser))
4566               {
4567                 bool non_constant_p;
4568                 /* Parse the initializer-list.  */
4569                 initializer_list
4570                   = cp_parser_initializer_list (parser, &non_constant_p);
4571                 /* Allow a trailing `,'.  */
4572                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4573                   cp_lexer_consume_token (parser->lexer);
4574                 /* Look for the final `}'.  */
4575                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4576               }
4577             /* If that worked, we're definitely looking at a
4578                compound-literal expression.  */
4579             if (cp_parser_parse_definitely (parser))
4580               {
4581                 /* Warn the user that a compound literal is not
4582                    allowed in standard C++.  */
4583                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4584                 /* For simplicity, we disallow compound literals in
4585                    constant-expressions.  We could
4586                    allow compound literals of integer type, whose
4587                    initializer was a constant, in constant
4588                    expressions.  Permitting that usage, as a further
4589                    extension, would not change the meaning of any
4590                    currently accepted programs.  (Of course, as
4591                    compound literals are not part of ISO C++, the
4592                    standard has nothing to say.)  */
4593                 if (cp_parser_non_integral_constant_expression 
4594                     (parser, "non-constant compound literals"))
4595                   {
4596                     postfix_expression = error_mark_node;
4597                     break;
4598                   }
4599                 /* Form the representation of the compound-literal.  */
4600                 postfix_expression
4601                   = (finish_compound_literal
4602                      (type, build_constructor (init_list_type_node,
4603                                                initializer_list)));
4604                 break;
4605               }
4606           }
4607
4608         /* It must be a primary-expression.  */
4609         postfix_expression
4610           = cp_parser_primary_expression (parser, address_p, cast_p,
4611                                           /*template_arg_p=*/false,
4612                                           &idk);
4613       }
4614       break;
4615     }
4616
4617   /* Keep looping until the postfix-expression is complete.  */
4618   while (true)
4619     {
4620       if (idk == CP_ID_KIND_UNQUALIFIED
4621           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4622           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4623         /* It is not a Koenig lookup function call.  */
4624         postfix_expression
4625           = unqualified_name_lookup_error (postfix_expression);
4626
4627       /* Peek at the next token.  */
4628       token = cp_lexer_peek_token (parser->lexer);
4629
4630       switch (token->type)
4631         {
4632         case CPP_OPEN_SQUARE:
4633           postfix_expression
4634             = cp_parser_postfix_open_square_expression (parser,
4635                                                         postfix_expression,
4636                                                         false);
4637           idk = CP_ID_KIND_NONE;
4638           is_member_access = false;
4639           break;
4640
4641         case CPP_OPEN_PAREN:
4642           /* postfix-expression ( expression-list [opt] ) */
4643           {
4644             bool koenig_p;
4645             bool is_builtin_constant_p;
4646             bool saved_integral_constant_expression_p = false;
4647             bool saved_non_integral_constant_expression_p = false;
4648             tree args;
4649
4650             is_member_access = false;
4651
4652             is_builtin_constant_p
4653               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4654             if (is_builtin_constant_p)
4655               {
4656                 /* The whole point of __builtin_constant_p is to allow
4657                    non-constant expressions to appear as arguments.  */
4658                 saved_integral_constant_expression_p
4659                   = parser->integral_constant_expression_p;
4660                 saved_non_integral_constant_expression_p
4661                   = parser->non_integral_constant_expression_p;
4662                 parser->integral_constant_expression_p = false;
4663               }
4664             args = (cp_parser_parenthesized_expression_list
4665                     (parser, /*is_attribute_list=*/false,
4666                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4667                      /*non_constant_p=*/NULL));
4668             if (is_builtin_constant_p)
4669               {
4670                 parser->integral_constant_expression_p
4671                   = saved_integral_constant_expression_p;
4672                 parser->non_integral_constant_expression_p
4673                   = saved_non_integral_constant_expression_p;
4674               }
4675
4676             if (args == error_mark_node)
4677               {
4678                 postfix_expression = error_mark_node;
4679                 break;
4680               }
4681
4682             /* Function calls are not permitted in
4683                constant-expressions.  */
4684             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4685                 && cp_parser_non_integral_constant_expression (parser,
4686                                                                "a function call"))
4687               {
4688                 postfix_expression = error_mark_node;
4689                 break;
4690               }
4691
4692             koenig_p = false;
4693             if (idk == CP_ID_KIND_UNQUALIFIED)
4694               {
4695                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4696                   {
4697                     if (args)
4698                       {
4699                         koenig_p = true;
4700                         postfix_expression
4701                           = perform_koenig_lookup (postfix_expression, args);
4702                       }
4703                     else
4704                       postfix_expression
4705                         = unqualified_fn_lookup_error (postfix_expression);
4706                   }
4707                 /* We do not perform argument-dependent lookup if
4708                    normal lookup finds a non-function, in accordance
4709                    with the expected resolution of DR 218.  */
4710                 else if (args && is_overloaded_fn (postfix_expression))
4711                   {
4712                     tree fn = get_first_fn (postfix_expression);
4713
4714                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4715                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4716
4717                     /* Only do argument dependent lookup if regular
4718                        lookup does not find a set of member functions.
4719                        [basic.lookup.koenig]/2a  */
4720                     if (!DECL_FUNCTION_MEMBER_P (fn))
4721                       {
4722                         koenig_p = true;
4723                         postfix_expression
4724                           = perform_koenig_lookup (postfix_expression, args);
4725                       }
4726                   }
4727               }
4728
4729             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4730               {
4731                 tree instance = TREE_OPERAND (postfix_expression, 0);
4732                 tree fn = TREE_OPERAND (postfix_expression, 1);
4733
4734                 if (processing_template_decl
4735                     && (type_dependent_expression_p (instance)
4736                         || (!BASELINK_P (fn)
4737                             && TREE_CODE (fn) != FIELD_DECL)
4738                         || type_dependent_expression_p (fn)
4739                         || any_type_dependent_arguments_p (args)))
4740                   {
4741                     postfix_expression
4742                       = build_nt_call_list (postfix_expression, args);
4743                     break;
4744                   }
4745
4746                 if (BASELINK_P (fn))
4747                   postfix_expression
4748                     = (build_new_method_call
4749                        (instance, fn, args, NULL_TREE,
4750                         (idk == CP_ID_KIND_QUALIFIED
4751                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4752                         /*fn_p=*/NULL,
4753                         tf_warning_or_error));
4754                 else
4755                   postfix_expression
4756                     = finish_call_expr (postfix_expression, args,
4757                                         /*disallow_virtual=*/false,
4758                                         /*koenig_p=*/false,
4759                                         tf_warning_or_error);
4760               }
4761             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4762                      || TREE_CODE (postfix_expression) == MEMBER_REF
4763                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4764               postfix_expression = (build_offset_ref_call_from_tree
4765                                     (postfix_expression, args));
4766             else if (idk == CP_ID_KIND_QUALIFIED)
4767               /* A call to a static class member, or a namespace-scope
4768                  function.  */
4769               postfix_expression
4770                 = finish_call_expr (postfix_expression, args,
4771                                     /*disallow_virtual=*/true,
4772                                     koenig_p,
4773                                     tf_warning_or_error);
4774             else
4775               /* All other function calls.  */
4776               postfix_expression
4777                 = finish_call_expr (postfix_expression, args,
4778                                     /*disallow_virtual=*/false,
4779                                     koenig_p,
4780                                     tf_warning_or_error);
4781
4782             if (warn_disallowed_functions)
4783               warn_if_disallowed_function_p (postfix_expression);
4784
4785             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4786             idk = CP_ID_KIND_NONE;
4787           }
4788           break;
4789
4790         case CPP_DOT:
4791         case CPP_DEREF:
4792           /* postfix-expression . template [opt] id-expression
4793              postfix-expression . pseudo-destructor-name
4794              postfix-expression -> template [opt] id-expression
4795              postfix-expression -> pseudo-destructor-name */
4796
4797           /* Consume the `.' or `->' operator.  */
4798           cp_lexer_consume_token (parser->lexer);
4799
4800           postfix_expression
4801             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4802                                                       postfix_expression,
4803                                                       false, &idk,
4804                                                       token->location);
4805
4806           is_member_access = true;
4807           break;
4808
4809         case CPP_PLUS_PLUS:
4810           /* postfix-expression ++  */
4811           /* Consume the `++' token.  */
4812           cp_lexer_consume_token (parser->lexer);
4813           /* Generate a representation for the complete expression.  */
4814           postfix_expression
4815             = finish_increment_expr (postfix_expression,
4816                                      POSTINCREMENT_EXPR);
4817           /* Increments may not appear in constant-expressions.  */
4818           if (cp_parser_non_integral_constant_expression (parser,
4819                                                           "an increment"))
4820             postfix_expression = error_mark_node;
4821           idk = CP_ID_KIND_NONE;
4822           is_member_access = false;
4823           break;
4824
4825         case CPP_MINUS_MINUS:
4826           /* postfix-expression -- */
4827           /* Consume the `--' token.  */
4828           cp_lexer_consume_token (parser->lexer);
4829           /* Generate a representation for the complete expression.  */
4830           postfix_expression
4831             = finish_increment_expr (postfix_expression,
4832                                      POSTDECREMENT_EXPR);
4833           /* Decrements may not appear in constant-expressions.  */
4834           if (cp_parser_non_integral_constant_expression (parser,
4835                                                           "a decrement"))
4836             postfix_expression = error_mark_node;
4837           idk = CP_ID_KIND_NONE;
4838           is_member_access = false;
4839           break;
4840
4841         default:
4842           if (member_access_only_p)
4843             return is_member_access? postfix_expression : error_mark_node;
4844           else
4845             return postfix_expression;
4846         }
4847     }
4848
4849   /* We should never get here.  */
4850   gcc_unreachable ();
4851   return error_mark_node;
4852 }
4853
4854 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4855    by cp_parser_builtin_offsetof.  We're looking for
4856
4857      postfix-expression [ expression ]
4858
4859    FOR_OFFSETOF is set if we're being called in that context, which
4860    changes how we deal with integer constant expressions.  */
4861
4862 static tree
4863 cp_parser_postfix_open_square_expression (cp_parser *parser,
4864                                           tree postfix_expression,
4865                                           bool for_offsetof)
4866 {
4867   tree index;
4868
4869   /* Consume the `[' token.  */
4870   cp_lexer_consume_token (parser->lexer);
4871
4872   /* Parse the index expression.  */
4873   /* ??? For offsetof, there is a question of what to allow here.  If
4874      offsetof is not being used in an integral constant expression context,
4875      then we *could* get the right answer by computing the value at runtime.
4876      If we are in an integral constant expression context, then we might
4877      could accept any constant expression; hard to say without analysis.
4878      Rather than open the barn door too wide right away, allow only integer
4879      constant expressions here.  */
4880   if (for_offsetof)
4881     index = cp_parser_constant_expression (parser, false, NULL);
4882   else
4883     index = cp_parser_expression (parser, /*cast_p=*/false);
4884
4885   /* Look for the closing `]'.  */
4886   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4887
4888   /* Build the ARRAY_REF.  */
4889   postfix_expression = grok_array_decl (postfix_expression, index);
4890
4891   /* When not doing offsetof, array references are not permitted in
4892      constant-expressions.  */
4893   if (!for_offsetof
4894       && (cp_parser_non_integral_constant_expression
4895           (parser, "an array reference")))
4896     postfix_expression = error_mark_node;
4897
4898   return postfix_expression;
4899 }
4900
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902    by cp_parser_builtin_offsetof.  We're looking for
4903
4904      postfix-expression . template [opt] id-expression
4905      postfix-expression . pseudo-destructor-name
4906      postfix-expression -> template [opt] id-expression
4907      postfix-expression -> pseudo-destructor-name
4908
4909    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4910    limits what of the above we'll actually accept, but nevermind.
4911    TOKEN_TYPE is the "." or "->" token, which will already have been
4912    removed from the stream.  */
4913
4914 static tree
4915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4916                                         enum cpp_ttype token_type,
4917                                         tree postfix_expression,
4918                                         bool for_offsetof, cp_id_kind *idk,
4919                                         location_t location)
4920 {
4921   tree name;
4922   bool dependent_p;
4923   bool pseudo_destructor_p;
4924   tree scope = NULL_TREE;
4925
4926   /* If this is a `->' operator, dereference the pointer.  */
4927   if (token_type == CPP_DEREF)
4928     postfix_expression = build_x_arrow (postfix_expression);
4929   /* Check to see whether or not the expression is type-dependent.  */
4930   dependent_p = type_dependent_expression_p (postfix_expression);
4931   /* The identifier following the `->' or `.' is not qualified.  */
4932   parser->scope = NULL_TREE;
4933   parser->qualifying_scope = NULL_TREE;
4934   parser->object_scope = NULL_TREE;
4935   *idk = CP_ID_KIND_NONE;
4936   /* Enter the scope corresponding to the type of the object
4937      given by the POSTFIX_EXPRESSION.  */
4938   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4939     {
4940       scope = TREE_TYPE (postfix_expression);
4941       /* According to the standard, no expression should ever have
4942          reference type.  Unfortunately, we do not currently match
4943          the standard in this respect in that our internal representation
4944          of an expression may have reference type even when the standard
4945          says it does not.  Therefore, we have to manually obtain the
4946          underlying type here.  */
4947       scope = non_reference (scope);
4948       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4949       if (scope == unknown_type_node)
4950         {
4951           error ("%H%qE does not have class type", &location, postfix_expression);
4952           scope = NULL_TREE;
4953         }
4954       else
4955         scope = complete_type_or_else (scope, NULL_TREE);
4956       /* Let the name lookup machinery know that we are processing a
4957          class member access expression.  */
4958       parser->context->object_type = scope;
4959       /* If something went wrong, we want to be able to discern that case,
4960          as opposed to the case where there was no SCOPE due to the type
4961          of expression being dependent.  */
4962       if (!scope)
4963         scope = error_mark_node;
4964       /* If the SCOPE was erroneous, make the various semantic analysis
4965          functions exit quickly -- and without issuing additional error
4966          messages.  */
4967       if (scope == error_mark_node)
4968         postfix_expression = error_mark_node;
4969     }
4970
4971   /* Assume this expression is not a pseudo-destructor access.  */
4972   pseudo_destructor_p = false;
4973
4974   /* If the SCOPE is a scalar type, then, if this is a valid program,
4975      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4976      is type dependent, it can be pseudo-destructor-name or something else.
4977      Try to parse it as pseudo-destructor-name first.  */
4978   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4979     {
4980       tree s;
4981       tree type;
4982
4983       cp_parser_parse_tentatively (parser);
4984       /* Parse the pseudo-destructor-name.  */
4985       s = NULL_TREE;
4986       cp_parser_pseudo_destructor_name (parser, &s, &type);
4987       if (dependent_p
4988           && (cp_parser_error_occurred (parser)
4989               || TREE_CODE (type) != TYPE_DECL
4990               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4991         cp_parser_abort_tentative_parse (parser);
4992       else if (cp_parser_parse_definitely (parser))
4993         {
4994           pseudo_destructor_p = true;
4995           postfix_expression
4996             = finish_pseudo_destructor_expr (postfix_expression,
4997                                              s, TREE_TYPE (type));
4998         }
4999     }
5000
5001   if (!pseudo_destructor_p)
5002     {
5003       /* If the SCOPE is not a scalar type, we are looking at an
5004          ordinary class member access expression, rather than a
5005          pseudo-destructor-name.  */
5006       bool template_p;
5007       cp_token *token = cp_lexer_peek_token (parser->lexer);
5008       /* Parse the id-expression.  */
5009       name = (cp_parser_id_expression
5010               (parser,
5011                cp_parser_optional_template_keyword (parser),
5012                /*check_dependency_p=*/true,
5013                &template_p,
5014                /*declarator_p=*/false,
5015                /*optional_p=*/false));
5016       /* In general, build a SCOPE_REF if the member name is qualified.
5017          However, if the name was not dependent and has already been
5018          resolved; there is no need to build the SCOPE_REF.  For example;
5019
5020              struct X { void f(); };
5021              template <typename T> void f(T* t) { t->X::f(); }
5022
5023          Even though "t" is dependent, "X::f" is not and has been resolved
5024          to a BASELINK; there is no need to include scope information.  */
5025
5026       /* But we do need to remember that there was an explicit scope for
5027          virtual function calls.  */
5028       if (parser->scope)
5029         *idk = CP_ID_KIND_QUALIFIED;
5030
5031       /* If the name is a template-id that names a type, we will get a
5032          TYPE_DECL here.  That is invalid code.  */
5033       if (TREE_CODE (name) == TYPE_DECL)
5034         {
5035           error ("%Hinvalid use of %qD", &token->location, name);
5036           postfix_expression = error_mark_node;
5037         }
5038       else
5039         {
5040           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5041             {
5042               name = build_qualified_name (/*type=*/NULL_TREE,
5043                                            parser->scope,
5044                                            name,
5045                                            template_p);
5046               parser->scope = NULL_TREE;
5047               parser->qualifying_scope = NULL_TREE;
5048               parser->object_scope = NULL_TREE;
5049             }
5050           if (scope && name && BASELINK_P (name))
5051             adjust_result_of_qualified_name_lookup
5052               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5053           postfix_expression
5054             = finish_class_member_access_expr (postfix_expression, name,
5055                                                template_p, 
5056                                                tf_warning_or_error);
5057         }
5058     }
5059
5060   /* We no longer need to look up names in the scope of the object on
5061      the left-hand side of the `.' or `->' operator.  */
5062   parser->context->object_type = NULL_TREE;
5063
5064   /* Outside of offsetof, these operators may not appear in
5065      constant-expressions.  */
5066   if (!for_offsetof
5067       && (cp_parser_non_integral_constant_expression
5068           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5069     postfix_expression = error_mark_node;
5070
5071   return postfix_expression;
5072 }
5073
5074 /* Parse a parenthesized expression-list.
5075
5076    expression-list:
5077      assignment-expression
5078      expression-list, assignment-expression
5079
5080    attribute-list:
5081      expression-list
5082      identifier
5083      identifier, expression-list
5084
5085    CAST_P is true if this expression is the target of a cast.
5086
5087    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5088    argument pack.
5089
5090    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5091    representation of an assignment-expression.  Note that a TREE_LIST
5092    is returned even if there is only a single expression in the list.
5093    error_mark_node is returned if the ( and or ) are
5094    missing. NULL_TREE is returned on no expressions. The parentheses
5095    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5096    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5097    indicates whether or not all of the expressions in the list were
5098    constant.  */
5099
5100 static tree
5101 cp_parser_parenthesized_expression_list (cp_parser* parser,
5102                                          bool is_attribute_list,
5103                                          bool cast_p,
5104                                          bool allow_expansion_p,
5105                                          bool *non_constant_p)
5106 {
5107   tree expression_list = NULL_TREE;
5108   bool fold_expr_p = is_attribute_list;
5109   tree identifier = NULL_TREE;
5110   bool saved_greater_than_is_operator_p;
5111
5112   /* Assume all the expressions will be constant.  */
5113   if (non_constant_p)
5114     *non_constant_p = false;
5115
5116   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5117     return error_mark_node;
5118
5119   /* Within a parenthesized expression, a `>' token is always
5120      the greater-than operator.  */
5121   saved_greater_than_is_operator_p
5122     = parser->greater_than_is_operator_p;
5123   parser->greater_than_is_operator_p = true;
5124
5125   /* Consume expressions until there are no more.  */
5126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5127     while (true)
5128       {
5129         tree expr;
5130
5131         /* At the beginning of attribute lists, check to see if the
5132            next token is an identifier.  */
5133         if (is_attribute_list
5134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5135           {
5136             cp_token *token;
5137
5138             /* Consume the identifier.  */
5139             token = cp_lexer_consume_token (parser->lexer);
5140             /* Save the identifier.  */
5141             identifier = token->u.value;
5142           }
5143         else
5144           {
5145             bool expr_non_constant_p;
5146
5147             /* Parse the next assignment-expression.  */
5148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5149               {
5150                 /* A braced-init-list.  */
5151                 maybe_warn_cpp0x ("extended initializer lists");
5152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5153                 if (non_constant_p && expr_non_constant_p)
5154                   *non_constant_p = true;
5155               }
5156             else if (non_constant_p)
5157               {
5158                 expr = (cp_parser_constant_expression
5159                         (parser, /*allow_non_constant_p=*/true,
5160                          &expr_non_constant_p));
5161                 if (expr_non_constant_p)
5162                   *non_constant_p = true;
5163               }
5164             else
5165               expr = cp_parser_assignment_expression (parser, cast_p);
5166
5167             if (fold_expr_p)
5168               expr = fold_non_dependent_expr (expr);
5169
5170             /* If we have an ellipsis, then this is an expression
5171                expansion.  */
5172             if (allow_expansion_p
5173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5174               {
5175                 /* Consume the `...'.  */
5176                 cp_lexer_consume_token (parser->lexer);
5177
5178                 /* Build the argument pack.  */
5179                 expr = make_pack_expansion (expr);
5180               }
5181
5182              /* Add it to the list.  We add error_mark_node
5183                 expressions to the list, so that we can still tell if
5184                 the correct form for a parenthesized expression-list
5185                 is found. That gives better errors.  */
5186             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5187
5188             if (expr == error_mark_node)
5189               goto skip_comma;
5190           }
5191
5192         /* After the first item, attribute lists look the same as
5193            expression lists.  */
5194         is_attribute_list = false;
5195
5196       get_comma:;
5197         /* If the next token isn't a `,', then we are done.  */
5198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5199           break;
5200
5201         /* Otherwise, consume the `,' and keep going.  */
5202         cp_lexer_consume_token (parser->lexer);
5203       }
5204
5205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5206     {
5207       int ending;
5208
5209     skip_comma:;
5210       /* We try and resync to an unnested comma, as that will give the
5211          user better diagnostics.  */
5212       ending = cp_parser_skip_to_closing_parenthesis (parser,
5213                                                       /*recovering=*/true,
5214                                                       /*or_comma=*/true,
5215                                                       /*consume_paren=*/true);
5216       if (ending < 0)
5217         goto get_comma;
5218       if (!ending)
5219         {
5220           parser->greater_than_is_operator_p
5221             = saved_greater_than_is_operator_p;
5222           return error_mark_node;
5223         }
5224     }
5225
5226   parser->greater_than_is_operator_p
5227     = saved_greater_than_is_operator_p;
5228
5229   /* We built up the list in reverse order so we must reverse it now.  */
5230   expression_list = nreverse (expression_list);
5231   if (identifier)
5232     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5233
5234   return expression_list;
5235 }
5236
5237 /* Parse a pseudo-destructor-name.
5238
5239    pseudo-destructor-name:
5240      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5241      :: [opt] nested-name-specifier template template-id :: ~ type-name
5242      :: [opt] nested-name-specifier [opt] ~ type-name
5243
5244    If either of the first two productions is used, sets *SCOPE to the
5245    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5246    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5247    or ERROR_MARK_NODE if the parse fails.  */
5248
5249 static void
5250 cp_parser_pseudo_destructor_name (cp_parser* parser,
5251                                   tree* scope,
5252                                   tree* type)
5253 {
5254   bool nested_name_specifier_p;
5255
5256   /* Assume that things will not work out.  */
5257   *type = error_mark_node;
5258
5259   /* Look for the optional `::' operator.  */
5260   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5261   /* Look for the optional nested-name-specifier.  */
5262   nested_name_specifier_p
5263     = (cp_parser_nested_name_specifier_opt (parser,
5264                                             /*typename_keyword_p=*/false,
5265                                             /*check_dependency_p=*/true,
5266                                             /*type_p=*/false,
5267                                             /*is_declaration=*/true)
5268        != NULL_TREE);
5269   /* Now, if we saw a nested-name-specifier, we might be doing the
5270      second production.  */
5271   if (nested_name_specifier_p
5272       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5273     {
5274       /* Consume the `template' keyword.  */
5275       cp_lexer_consume_token (parser->lexer);
5276       /* Parse the template-id.  */
5277       cp_parser_template_id (parser,
5278                              /*template_keyword_p=*/true,
5279                              /*check_dependency_p=*/false,
5280                              /*is_declaration=*/true);
5281       /* Look for the `::' token.  */
5282       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5283     }
5284   /* If the next token is not a `~', then there might be some
5285      additional qualification.  */
5286   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5287     {
5288       /* At this point, we're looking for "type-name :: ~".  The type-name
5289          must not be a class-name, since this is a pseudo-destructor.  So,
5290          it must be either an enum-name, or a typedef-name -- both of which
5291          are just identifiers.  So, we peek ahead to check that the "::"
5292          and "~" tokens are present; if they are not, then we can avoid
5293          calling type_name.  */
5294       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5295           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5296           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5297         {
5298           cp_parser_error (parser, "non-scalar type");
5299           return;
5300         }
5301
5302       /* Look for the type-name.  */
5303       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5304       if (*scope == error_mark_node)
5305         return;
5306
5307       /* Look for the `::' token.  */
5308       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5309     }
5310   else
5311     *scope = NULL_TREE;
5312
5313   /* Look for the `~'.  */
5314   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5315   /* Look for the type-name again.  We are not responsible for
5316      checking that it matches the first type-name.  */
5317   *type = cp_parser_nonclass_name (parser);
5318 }
5319
5320 /* Parse a unary-expression.
5321
5322    unary-expression:
5323      postfix-expression
5324      ++ cast-expression
5325      -- cast-expression
5326      unary-operator cast-expression
5327      sizeof unary-expression
5328      sizeof ( type-id )
5329      new-expression
5330      delete-expression
5331
5332    GNU Extensions:
5333
5334    unary-expression:
5335      __extension__ cast-expression
5336      __alignof__ unary-expression
5337      __alignof__ ( type-id )
5338      __real__ cast-expression
5339      __imag__ cast-expression
5340      && identifier
5341
5342    ADDRESS_P is true iff the unary-expression is appearing as the
5343    operand of the `&' operator.   CAST_P is true if this expression is
5344    the target of a cast.
5345
5346    Returns a representation of the expression.  */
5347
5348 static tree
5349 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5350 {
5351   cp_token *token;
5352   enum tree_code unary_operator;
5353
5354   /* Peek at the next token.  */
5355   token = cp_lexer_peek_token (parser->lexer);
5356   /* Some keywords give away the kind of expression.  */
5357   if (token->type == CPP_KEYWORD)
5358     {
5359       enum rid keyword = token->keyword;
5360
5361       switch (keyword)
5362         {
5363         case RID_ALIGNOF:
5364         case RID_SIZEOF:
5365           {
5366             tree operand;
5367             enum tree_code op;
5368
5369             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5370             /* Consume the token.  */
5371             cp_lexer_consume_token (parser->lexer);
5372             /* Parse the operand.  */
5373             operand = cp_parser_sizeof_operand (parser, keyword);
5374
5375             if (TYPE_P (operand))
5376               return cxx_sizeof_or_alignof_type (operand, op, true);
5377             else
5378               return cxx_sizeof_or_alignof_expr (operand, op, true);
5379           }
5380
5381         case RID_NEW:
5382           return cp_parser_new_expression (parser);
5383
5384         case RID_DELETE:
5385           return cp_parser_delete_expression (parser);
5386
5387         case RID_EXTENSION:
5388           {
5389             /* The saved value of the PEDANTIC flag.  */
5390             int saved_pedantic;
5391             tree expr;
5392
5393             /* Save away the PEDANTIC flag.  */
5394             cp_parser_extension_opt (parser, &saved_pedantic);
5395             /* Parse the cast-expression.  */
5396             expr = cp_parser_simple_cast_expression (parser);
5397             /* Restore the PEDANTIC flag.  */
5398             pedantic = saved_pedantic;
5399
5400             return expr;
5401           }
5402
5403         case RID_REALPART:
5404         case RID_IMAGPART:
5405           {
5406             tree expression;
5407
5408             /* Consume the `__real__' or `__imag__' token.  */
5409             cp_lexer_consume_token (parser->lexer);
5410             /* Parse the cast-expression.  */
5411             expression = cp_parser_simple_cast_expression (parser);
5412             /* Create the complete representation.  */
5413             return build_x_unary_op ((keyword == RID_REALPART
5414                                       ? REALPART_EXPR : IMAGPART_EXPR),
5415                                      expression,
5416                                      tf_warning_or_error);
5417           }
5418           break;
5419
5420         default:
5421           break;
5422         }
5423     }
5424
5425   /* Look for the `:: new' and `:: delete', which also signal the
5426      beginning of a new-expression, or delete-expression,
5427      respectively.  If the next token is `::', then it might be one of
5428      these.  */
5429   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5430     {
5431       enum rid keyword;
5432
5433       /* See if the token after the `::' is one of the keywords in
5434          which we're interested.  */
5435       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5436       /* If it's `new', we have a new-expression.  */
5437       if (keyword == RID_NEW)
5438         return cp_parser_new_expression (parser);
5439       /* Similarly, for `delete'.  */
5440       else if (keyword == RID_DELETE)
5441         return cp_parser_delete_expression (parser);
5442     }
5443
5444   /* Look for a unary operator.  */
5445   unary_operator = cp_parser_unary_operator (token);
5446   /* The `++' and `--' operators can be handled similarly, even though
5447      they are not technically unary-operators in the grammar.  */
5448   if (unary_operator == ERROR_MARK)
5449     {
5450       if (token->type == CPP_PLUS_PLUS)
5451         unary_operator = PREINCREMENT_EXPR;
5452       else if (token->type == CPP_MINUS_MINUS)
5453         unary_operator = PREDECREMENT_EXPR;
5454       /* Handle the GNU address-of-label extension.  */
5455       else if (cp_parser_allow_gnu_extensions_p (parser)
5456                && token->type == CPP_AND_AND)
5457         {
5458           tree identifier;
5459           tree expression;
5460
5461           /* Consume the '&&' token.  */
5462           cp_lexer_consume_token (parser->lexer);
5463           /* Look for the identifier.  */
5464           identifier = cp_parser_identifier (parser);
5465           /* Create an expression representing the address.  */
5466           expression = finish_label_address_expr (identifier);
5467           if (cp_parser_non_integral_constant_expression (parser,
5468                                                 "the address of a label"))
5469             expression = error_mark_node;
5470           return expression;
5471         }
5472     }
5473   if (unary_operator != ERROR_MARK)
5474     {
5475       tree cast_expression;
5476       tree expression = error_mark_node;
5477       const char *non_constant_p = NULL;
5478
5479       /* Consume the operator token.  */
5480       token = cp_lexer_consume_token (parser->lexer);
5481       /* Parse the cast-expression.  */
5482       cast_expression
5483         = cp_parser_cast_expression (parser,
5484                                      unary_operator == ADDR_EXPR,
5485                                      /*cast_p=*/false);
5486       /* Now, build an appropriate representation.  */
5487       switch (unary_operator)
5488         {
5489         case INDIRECT_REF:
5490           non_constant_p = "%<*%>";
5491           expression = build_x_indirect_ref (cast_expression, "unary *",
5492                                              tf_warning_or_error);
5493           break;
5494
5495         case ADDR_EXPR:
5496           non_constant_p = "%<&%>";
5497           /* Fall through.  */
5498         case BIT_NOT_EXPR:
5499           expression = build_x_unary_op (unary_operator, cast_expression,
5500                                          tf_warning_or_error);
5501           break;
5502
5503         case PREINCREMENT_EXPR:
5504         case PREDECREMENT_EXPR:
5505           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5506                             ? "%<++%>" : "%<--%>");
5507           /* Fall through.  */
5508         case UNARY_PLUS_EXPR:
5509         case NEGATE_EXPR:
5510         case TRUTH_NOT_EXPR:
5511           expression = finish_unary_op_expr (unary_operator, cast_expression);
5512           break;
5513
5514         default:
5515           gcc_unreachable ();
5516         }
5517
5518       if (non_constant_p
5519           && cp_parser_non_integral_constant_expression (parser,
5520                                                          non_constant_p))
5521         expression = error_mark_node;
5522
5523       return expression;
5524     }
5525
5526   return cp_parser_postfix_expression (parser, address_p, cast_p,
5527                                        /*member_access_only_p=*/false);
5528 }
5529
5530 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5531    unary-operator, the corresponding tree code is returned.  */
5532
5533 static enum tree_code
5534 cp_parser_unary_operator (cp_token* token)
5535 {
5536   switch (token->type)
5537     {
5538     case CPP_MULT:
5539       return INDIRECT_REF;
5540
5541     case CPP_AND:
5542       return ADDR_EXPR;
5543
5544     case CPP_PLUS:
5545       return UNARY_PLUS_EXPR;
5546
5547     case CPP_MINUS:
5548       return NEGATE_EXPR;
5549
5550     case CPP_NOT:
5551       return TRUTH_NOT_EXPR;
5552
5553     case CPP_COMPL:
5554       return BIT_NOT_EXPR;
5555
5556     default:
5557       return ERROR_MARK;
5558     }
5559 }
5560
5561 /* Parse a new-expression.
5562
5563    new-expression:
5564      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5565      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5566
5567    Returns a representation of the expression.  */
5568
5569 static tree
5570 cp_parser_new_expression (cp_parser* parser)
5571 {
5572   bool global_scope_p;
5573   tree placement;
5574   tree type;
5575   tree initializer;
5576   tree nelts;
5577
5578   /* Look for the optional `::' operator.  */
5579   global_scope_p
5580     = (cp_parser_global_scope_opt (parser,
5581                                    /*current_scope_valid_p=*/false)
5582        != NULL_TREE);
5583   /* Look for the `new' operator.  */
5584   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5585   /* There's no easy way to tell a new-placement from the
5586      `( type-id )' construct.  */
5587   cp_parser_parse_tentatively (parser);
5588   /* Look for a new-placement.  */
5589   placement = cp_parser_new_placement (parser);
5590   /* If that didn't work out, there's no new-placement.  */
5591   if (!cp_parser_parse_definitely (parser))
5592     placement = NULL_TREE;
5593
5594   /* If the next token is a `(', then we have a parenthesized
5595      type-id.  */
5596   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5597     {
5598       cp_token *token;
5599       /* Consume the `('.  */
5600       cp_lexer_consume_token (parser->lexer);
5601       /* Parse the type-id.  */
5602       type = cp_parser_type_id (parser);
5603       /* Look for the closing `)'.  */
5604       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5605       token = cp_lexer_peek_token (parser->lexer);
5606       /* There should not be a direct-new-declarator in this production,
5607          but GCC used to allowed this, so we check and emit a sensible error
5608          message for this case.  */
5609       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5610         {
5611           error ("%Harray bound forbidden after parenthesized type-id",
5612                  &token->location);
5613           inform (token->location, 
5614                   "try removing the parentheses around the type-id");
5615           cp_parser_direct_new_declarator (parser);
5616         }
5617       nelts = NULL_TREE;
5618     }
5619   /* Otherwise, there must be a new-type-id.  */
5620   else
5621     type = cp_parser_new_type_id (parser, &nelts);
5622
5623   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5624   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5625       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5626     initializer = cp_parser_new_initializer (parser);
5627   else
5628     initializer = NULL_TREE;
5629
5630   /* A new-expression may not appear in an integral constant
5631      expression.  */
5632   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5633     return error_mark_node;
5634
5635   /* Create a representation of the new-expression.  */
5636   return build_new (placement, type, nelts, initializer, global_scope_p,
5637                     tf_warning_or_error);
5638 }
5639
5640 /* Parse a new-placement.
5641
5642    new-placement:
5643      ( expression-list )
5644
5645    Returns the same representation as for an expression-list.  */
5646
5647 static tree
5648 cp_parser_new_placement (cp_parser* parser)
5649 {
5650   tree expression_list;
5651
5652   /* Parse the expression-list.  */
5653   expression_list = (cp_parser_parenthesized_expression_list
5654                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5655                       /*non_constant_p=*/NULL));
5656
5657   return expression_list;
5658 }
5659
5660 /* Parse a new-type-id.
5661
5662    new-type-id:
5663      type-specifier-seq new-declarator [opt]
5664
5665    Returns the TYPE allocated.  If the new-type-id indicates an array
5666    type, *NELTS is set to the number of elements in the last array
5667    bound; the TYPE will not include the last array bound.  */
5668
5669 static tree
5670 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5671 {
5672   cp_decl_specifier_seq type_specifier_seq;
5673   cp_declarator *new_declarator;
5674   cp_declarator *declarator;
5675   cp_declarator *outer_declarator;
5676   const char *saved_message;
5677   tree type;
5678
5679   /* The type-specifier sequence must not contain type definitions.
5680      (It cannot contain declarations of new types either, but if they
5681      are not definitions we will catch that because they are not
5682      complete.)  */
5683   saved_message = parser->type_definition_forbidden_message;
5684   parser->type_definition_forbidden_message
5685     = "types may not be defined in a new-type-id";
5686   /* Parse the type-specifier-seq.  */
5687   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5688                                 &type_specifier_seq);
5689   /* Restore the old message.  */
5690   parser->type_definition_forbidden_message = saved_message;
5691   /* Parse the new-declarator.  */
5692   new_declarator = cp_parser_new_declarator_opt (parser);
5693
5694   /* Determine the number of elements in the last array dimension, if
5695      any.  */
5696   *nelts = NULL_TREE;
5697   /* Skip down to the last array dimension.  */
5698   declarator = new_declarator;
5699   outer_declarator = NULL;
5700   while (declarator && (declarator->kind == cdk_pointer
5701                         || declarator->kind == cdk_ptrmem))
5702     {
5703       outer_declarator = declarator;
5704       declarator = declarator->declarator;
5705     }
5706   while (declarator
5707          && declarator->kind == cdk_array
5708          && declarator->declarator
5709          && declarator->declarator->kind == cdk_array)
5710     {
5711       outer_declarator = declarator;
5712       declarator = declarator->declarator;
5713     }
5714
5715   if (declarator && declarator->kind == cdk_array)
5716     {
5717       *nelts = declarator->u.array.bounds;
5718       if (*nelts == error_mark_node)
5719         *nelts = integer_one_node;
5720
5721       if (outer_declarator)
5722         outer_declarator->declarator = declarator->declarator;
5723       else
5724         new_declarator = NULL;
5725     }
5726
5727   type = groktypename (&type_specifier_seq, new_declarator);
5728   return type;
5729 }
5730
5731 /* Parse an (optional) new-declarator.
5732
5733    new-declarator:
5734      ptr-operator new-declarator [opt]
5735      direct-new-declarator
5736
5737    Returns the declarator.  */
5738
5739 static cp_declarator *
5740 cp_parser_new_declarator_opt (cp_parser* parser)
5741 {
5742   enum tree_code code;
5743   tree type;
5744   cp_cv_quals cv_quals;
5745
5746   /* We don't know if there's a ptr-operator next, or not.  */
5747   cp_parser_parse_tentatively (parser);
5748   /* Look for a ptr-operator.  */
5749   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5750   /* If that worked, look for more new-declarators.  */
5751   if (cp_parser_parse_definitely (parser))
5752     {
5753       cp_declarator *declarator;
5754
5755       /* Parse another optional declarator.  */
5756       declarator = cp_parser_new_declarator_opt (parser);
5757
5758       return cp_parser_make_indirect_declarator
5759         (code, type, cv_quals, declarator);
5760     }
5761
5762   /* If the next token is a `[', there is a direct-new-declarator.  */
5763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5764     return cp_parser_direct_new_declarator (parser);
5765
5766   return NULL;
5767 }
5768
5769 /* Parse a direct-new-declarator.
5770
5771    direct-new-declarator:
5772      [ expression ]
5773      direct-new-declarator [constant-expression]
5774
5775    */
5776
5777 static cp_declarator *
5778 cp_parser_direct_new_declarator (cp_parser* parser)
5779 {
5780   cp_declarator *declarator = NULL;
5781
5782   while (true)
5783     {
5784       tree expression;
5785
5786       /* Look for the opening `['.  */
5787       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5788       /* The first expression is not required to be constant.  */
5789       if (!declarator)
5790         {
5791           cp_token *token = cp_lexer_peek_token (parser->lexer);
5792           expression = cp_parser_expression (parser, /*cast_p=*/false);
5793           /* The standard requires that the expression have integral
5794              type.  DR 74 adds enumeration types.  We believe that the
5795              real intent is that these expressions be handled like the
5796              expression in a `switch' condition, which also allows
5797              classes with a single conversion to integral or
5798              enumeration type.  */
5799           if (!processing_template_decl)
5800             {
5801               expression
5802                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5803                                               expression,
5804                                               /*complain=*/true);
5805               if (!expression)
5806                 {
5807                   error ("%Hexpression in new-declarator must have integral "
5808                          "or enumeration type", &token->location);
5809                   expression = error_mark_node;
5810                 }
5811             }
5812         }
5813       /* But all the other expressions must be.  */
5814       else
5815         expression
5816           = cp_parser_constant_expression (parser,
5817                                            /*allow_non_constant=*/false,
5818                                            NULL);
5819       /* Look for the closing `]'.  */
5820       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5821
5822       /* Add this bound to the declarator.  */
5823       declarator = make_array_declarator (declarator, expression);
5824
5825       /* If the next token is not a `[', then there are no more
5826          bounds.  */
5827       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5828         break;
5829     }
5830
5831   return declarator;
5832 }
5833
5834 /* Parse a new-initializer.
5835
5836    new-initializer:
5837      ( expression-list [opt] )
5838      braced-init-list
5839
5840    Returns a representation of the expression-list.  If there is no
5841    expression-list, VOID_ZERO_NODE is returned.  */
5842
5843 static tree
5844 cp_parser_new_initializer (cp_parser* parser)
5845 {
5846   tree expression_list;
5847
5848   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5849     {
5850       bool expr_non_constant_p;
5851       maybe_warn_cpp0x ("extended initializer lists");
5852       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5853       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5854       expression_list = build_tree_list (NULL_TREE, expression_list);
5855     }
5856   else
5857     expression_list = (cp_parser_parenthesized_expression_list
5858                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5859                         /*non_constant_p=*/NULL));
5860   if (!expression_list)
5861     expression_list = void_zero_node;
5862
5863   return expression_list;
5864 }
5865
5866 /* Parse a delete-expression.
5867
5868    delete-expression:
5869      :: [opt] delete cast-expression
5870      :: [opt] delete [ ] cast-expression
5871
5872    Returns a representation of the expression.  */
5873
5874 static tree
5875 cp_parser_delete_expression (cp_parser* parser)
5876 {
5877   bool global_scope_p;
5878   bool array_p;
5879   tree expression;
5880
5881   /* Look for the optional `::' operator.  */
5882   global_scope_p
5883     = (cp_parser_global_scope_opt (parser,
5884                                    /*current_scope_valid_p=*/false)
5885        != NULL_TREE);
5886   /* Look for the `delete' keyword.  */
5887   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5888   /* See if the array syntax is in use.  */
5889   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5890     {
5891       /* Consume the `[' token.  */
5892       cp_lexer_consume_token (parser->lexer);
5893       /* Look for the `]' token.  */
5894       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5895       /* Remember that this is the `[]' construct.  */
5896       array_p = true;
5897     }
5898   else
5899     array_p = false;
5900
5901   /* Parse the cast-expression.  */
5902   expression = cp_parser_simple_cast_expression (parser);
5903
5904   /* A delete-expression may not appear in an integral constant
5905      expression.  */
5906   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5907     return error_mark_node;
5908
5909   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5910 }
5911
5912 /* Parse a cast-expression.
5913
5914    cast-expression:
5915      unary-expression
5916      ( type-id ) cast-expression
5917
5918    ADDRESS_P is true iff the unary-expression is appearing as the
5919    operand of the `&' operator.   CAST_P is true if this expression is
5920    the target of a cast.
5921
5922    Returns a representation of the expression.  */
5923
5924 static tree
5925 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5926 {
5927   /* If it's a `(', then we might be looking at a cast.  */
5928   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5929     {
5930       tree type = NULL_TREE;
5931       tree expr = NULL_TREE;
5932       bool compound_literal_p;
5933       const char *saved_message;
5934
5935       /* There's no way to know yet whether or not this is a cast.
5936          For example, `(int (3))' is a unary-expression, while `(int)
5937          3' is a cast.  So, we resort to parsing tentatively.  */
5938       cp_parser_parse_tentatively (parser);
5939       /* Types may not be defined in a cast.  */
5940       saved_message = parser->type_definition_forbidden_message;
5941       parser->type_definition_forbidden_message
5942         = "types may not be defined in casts";
5943       /* Consume the `('.  */
5944       cp_lexer_consume_token (parser->lexer);
5945       /* A very tricky bit is that `(struct S) { 3 }' is a
5946          compound-literal (which we permit in C++ as an extension).
5947          But, that construct is not a cast-expression -- it is a
5948          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5949          is legal; if the compound-literal were a cast-expression,
5950          you'd need an extra set of parentheses.)  But, if we parse
5951          the type-id, and it happens to be a class-specifier, then we
5952          will commit to the parse at that point, because we cannot
5953          undo the action that is done when creating a new class.  So,
5954          then we cannot back up and do a postfix-expression.
5955
5956          Therefore, we scan ahead to the closing `)', and check to see
5957          if the token after the `)' is a `{'.  If so, we are not
5958          looking at a cast-expression.
5959
5960          Save tokens so that we can put them back.  */
5961       cp_lexer_save_tokens (parser->lexer);
5962       /* Skip tokens until the next token is a closing parenthesis.
5963          If we find the closing `)', and the next token is a `{', then
5964          we are looking at a compound-literal.  */
5965       compound_literal_p
5966         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5967                                                   /*consume_paren=*/true)
5968            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5969       /* Roll back the tokens we skipped.  */
5970       cp_lexer_rollback_tokens (parser->lexer);
5971       /* If we were looking at a compound-literal, simulate an error
5972          so that the call to cp_parser_parse_definitely below will
5973          fail.  */
5974       if (compound_literal_p)
5975         cp_parser_simulate_error (parser);
5976       else
5977         {
5978           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5979           parser->in_type_id_in_expr_p = true;
5980           /* Look for the type-id.  */
5981           type = cp_parser_type_id (parser);
5982           /* Look for the closing `)'.  */
5983           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5984           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5985         }
5986
5987       /* Restore the saved message.  */
5988       parser->type_definition_forbidden_message = saved_message;
5989
5990       /* If ok so far, parse the dependent expression. We cannot be
5991          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5992          ctor of T, but looks like a cast to function returning T
5993          without a dependent expression.  */
5994       if (!cp_parser_error_occurred (parser))
5995         expr = cp_parser_cast_expression (parser,
5996                                           /*address_p=*/false,
5997                                           /*cast_p=*/true);
5998
5999       if (cp_parser_parse_definitely (parser))
6000         {
6001           /* Warn about old-style casts, if so requested.  */
6002           if (warn_old_style_cast
6003               && !in_system_header
6004               && !VOID_TYPE_P (type)
6005               && current_lang_name != lang_name_c)
6006             warning (OPT_Wold_style_cast, "use of old-style cast");
6007
6008           /* Only type conversions to integral or enumeration types
6009              can be used in constant-expressions.  */
6010           if (!cast_valid_in_integral_constant_expression_p (type)
6011               && (cp_parser_non_integral_constant_expression
6012                   (parser,
6013                    "a cast to a type other than an integral or "
6014                    "enumeration type")))
6015             return error_mark_node;
6016
6017           /* Perform the cast.  */
6018           expr = build_c_cast (type, expr);
6019           return expr;
6020         }
6021     }
6022
6023   /* If we get here, then it's not a cast, so it must be a
6024      unary-expression.  */
6025   return cp_parser_unary_expression (parser, address_p, cast_p);
6026 }
6027
6028 /* Parse a binary expression of the general form:
6029
6030    pm-expression:
6031      cast-expression
6032      pm-expression .* cast-expression
6033      pm-expression ->* cast-expression
6034
6035    multiplicative-expression:
6036      pm-expression
6037      multiplicative-expression * pm-expression
6038      multiplicative-expression / pm-expression
6039      multiplicative-expression % pm-expression
6040
6041    additive-expression:
6042      multiplicative-expression
6043      additive-expression + multiplicative-expression
6044      additive-expression - multiplicative-expression
6045
6046    shift-expression:
6047      additive-expression
6048      shift-expression << additive-expression
6049      shift-expression >> additive-expression
6050
6051    relational-expression:
6052      shift-expression
6053      relational-expression < shift-expression
6054      relational-expression > shift-expression
6055      relational-expression <= shift-expression
6056      relational-expression >= shift-expression
6057
6058   GNU Extension:
6059
6060    relational-expression:
6061      relational-expression <? shift-expression
6062      relational-expression >? shift-expression
6063
6064    equality-expression:
6065      relational-expression
6066      equality-expression == relational-expression
6067      equality-expression != relational-expression
6068
6069    and-expression:
6070      equality-expression
6071      and-expression & equality-expression
6072
6073    exclusive-or-expression:
6074      and-expression
6075      exclusive-or-expression ^ and-expression
6076
6077    inclusive-or-expression:
6078      exclusive-or-expression
6079      inclusive-or-expression | exclusive-or-expression
6080
6081    logical-and-expression:
6082      inclusive-or-expression
6083      logical-and-expression && inclusive-or-expression
6084
6085    logical-or-expression:
6086      logical-and-expression
6087      logical-or-expression || logical-and-expression
6088
6089    All these are implemented with a single function like:
6090
6091    binary-expression:
6092      simple-cast-expression
6093      binary-expression <token> binary-expression
6094
6095    CAST_P is true if this expression is the target of a cast.
6096
6097    The binops_by_token map is used to get the tree codes for each <token> type.
6098    binary-expressions are associated according to a precedence table.  */
6099
6100 #define TOKEN_PRECEDENCE(token)                              \
6101 (((token->type == CPP_GREATER                                \
6102    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6103   && !parser->greater_than_is_operator_p)                    \
6104  ? PREC_NOT_OPERATOR                                         \
6105  : binops_by_token[token->type].prec)
6106
6107 static tree
6108 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6109                              enum cp_parser_prec prec)
6110 {
6111   cp_parser_expression_stack stack;
6112   cp_parser_expression_stack_entry *sp = &stack[0];
6113   tree lhs, rhs;
6114   cp_token *token;
6115   enum tree_code tree_type, lhs_type, rhs_type;
6116   enum cp_parser_prec new_prec, lookahead_prec;
6117   bool overloaded_p;
6118
6119   /* Parse the first expression.  */
6120   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6121   lhs_type = ERROR_MARK;
6122
6123   for (;;)
6124     {
6125       /* Get an operator token.  */
6126       token = cp_lexer_peek_token (parser->lexer);
6127
6128       if (warn_cxx0x_compat
6129           && token->type == CPP_RSHIFT
6130           && !parser->greater_than_is_operator_p)
6131         {
6132           warning (OPT_Wc__0x_compat, 
6133                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6134                    &token->location);
6135           warning (OPT_Wc__0x_compat, 
6136                    "suggest parentheses around %<>>%> expression");
6137         }
6138
6139       new_prec = TOKEN_PRECEDENCE (token);
6140
6141       /* Popping an entry off the stack means we completed a subexpression:
6142          - either we found a token which is not an operator (`>' where it is not
6143            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6144            will happen repeatedly;
6145          - or, we found an operator which has lower priority.  This is the case
6146            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6147            parsing `3 * 4'.  */
6148       if (new_prec <= prec)
6149         {
6150           if (sp == stack)
6151             break;
6152           else
6153             goto pop;
6154         }
6155
6156      get_rhs:
6157       tree_type = binops_by_token[token->type].tree_type;
6158
6159       /* We used the operator token.  */
6160       cp_lexer_consume_token (parser->lexer);
6161
6162       /* Extract another operand.  It may be the RHS of this expression
6163          or the LHS of a new, higher priority expression.  */
6164       rhs = cp_parser_simple_cast_expression (parser);
6165       rhs_type = ERROR_MARK;
6166
6167       /* Get another operator token.  Look up its precedence to avoid
6168          building a useless (immediately popped) stack entry for common
6169          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6170       token = cp_lexer_peek_token (parser->lexer);
6171       lookahead_prec = TOKEN_PRECEDENCE (token);
6172       if (lookahead_prec > new_prec)
6173         {
6174           /* ... and prepare to parse the RHS of the new, higher priority
6175              expression.  Since precedence levels on the stack are
6176              monotonically increasing, we do not have to care about
6177              stack overflows.  */
6178           sp->prec = prec;
6179           sp->tree_type = tree_type;
6180           sp->lhs = lhs;
6181           sp->lhs_type = lhs_type;
6182           sp++;
6183           lhs = rhs;
6184           lhs_type = rhs_type;
6185           prec = new_prec;
6186           new_prec = lookahead_prec;
6187           goto get_rhs;
6188
6189          pop:
6190           /* If the stack is not empty, we have parsed into LHS the right side
6191              (`4' in the example above) of an expression we had suspended.
6192              We can use the information on the stack to recover the LHS (`3')
6193              from the stack together with the tree code (`MULT_EXPR'), and
6194              the precedence of the higher level subexpression
6195              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6196              which will be used to actually build the additive expression.  */
6197           --sp;
6198           prec = sp->prec;
6199           tree_type = sp->tree_type;
6200           rhs = lhs;
6201           rhs_type = lhs_type;
6202           lhs = sp->lhs;
6203           lhs_type = sp->lhs_type;
6204         }
6205
6206       overloaded_p = false;
6207       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6208                                &overloaded_p, tf_warning_or_error);
6209       lhs_type = tree_type;
6210
6211       /* If the binary operator required the use of an overloaded operator,
6212          then this expression cannot be an integral constant-expression.
6213          An overloaded operator can be used even if both operands are
6214          otherwise permissible in an integral constant-expression if at
6215          least one of the operands is of enumeration type.  */
6216
6217       if (overloaded_p
6218           && (cp_parser_non_integral_constant_expression
6219               (parser, "calls to overloaded operators")))
6220         return error_mark_node;
6221     }
6222
6223   return lhs;
6224 }
6225
6226
6227 /* Parse the `? expression : assignment-expression' part of a
6228    conditional-expression.  The LOGICAL_OR_EXPR is the
6229    logical-or-expression that started the conditional-expression.
6230    Returns a representation of the entire conditional-expression.
6231
6232    This routine is used by cp_parser_assignment_expression.
6233
6234      ? expression : assignment-expression
6235
6236    GNU Extensions:
6237
6238      ? : assignment-expression */
6239
6240 static tree
6241 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6242 {
6243   tree expr;
6244   tree assignment_expr;
6245
6246   /* Consume the `?' token.  */
6247   cp_lexer_consume_token (parser->lexer);
6248   if (cp_parser_allow_gnu_extensions_p (parser)
6249       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6250     /* Implicit true clause.  */
6251     expr = NULL_TREE;
6252   else
6253     /* Parse the expression.  */
6254     expr = cp_parser_expression (parser, /*cast_p=*/false);
6255
6256   /* The next token should be a `:'.  */
6257   cp_parser_require (parser, CPP_COLON, "%<:%>");
6258   /* Parse the assignment-expression.  */
6259   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6260
6261   /* Build the conditional-expression.  */
6262   return build_x_conditional_expr (logical_or_expr,
6263                                    expr,
6264                                    assignment_expr,
6265                                    tf_warning_or_error);
6266 }
6267
6268 /* Parse an assignment-expression.
6269
6270    assignment-expression:
6271      conditional-expression
6272      logical-or-expression assignment-operator assignment_expression
6273      throw-expression
6274
6275    CAST_P is true if this expression is the target of a cast.
6276
6277    Returns a representation for the expression.  */
6278
6279 static tree
6280 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6281 {
6282   tree expr;
6283
6284   /* If the next token is the `throw' keyword, then we're looking at
6285      a throw-expression.  */
6286   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6287     expr = cp_parser_throw_expression (parser);
6288   /* Otherwise, it must be that we are looking at a
6289      logical-or-expression.  */
6290   else
6291     {
6292       /* Parse the binary expressions (logical-or-expression).  */
6293       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6294       /* If the next token is a `?' then we're actually looking at a
6295          conditional-expression.  */
6296       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6297         return cp_parser_question_colon_clause (parser, expr);
6298       else
6299         {
6300           enum tree_code assignment_operator;
6301
6302           /* If it's an assignment-operator, we're using the second
6303              production.  */
6304           assignment_operator
6305             = cp_parser_assignment_operator_opt (parser);
6306           if (assignment_operator != ERROR_MARK)
6307             {
6308               bool non_constant_p;
6309
6310               /* Parse the right-hand side of the assignment.  */
6311               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6312
6313               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6314                 maybe_warn_cpp0x ("extended initializer lists");
6315
6316               /* An assignment may not appear in a
6317                  constant-expression.  */
6318               if (cp_parser_non_integral_constant_expression (parser,
6319                                                               "an assignment"))
6320                 return error_mark_node;
6321               /* Build the assignment expression.  */
6322               expr = build_x_modify_expr (expr,
6323                                           assignment_operator,
6324                                           rhs,
6325                                           tf_warning_or_error);
6326             }
6327         }
6328     }
6329
6330   return expr;
6331 }
6332
6333 /* Parse an (optional) assignment-operator.
6334
6335    assignment-operator: one of
6336      = *= /= %= += -= >>= <<= &= ^= |=
6337
6338    GNU Extension:
6339
6340    assignment-operator: one of
6341      <?= >?=
6342
6343    If the next token is an assignment operator, the corresponding tree
6344    code is returned, and the token is consumed.  For example, for
6345    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6346    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6347    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6348    operator, ERROR_MARK is returned.  */
6349
6350 static enum tree_code
6351 cp_parser_assignment_operator_opt (cp_parser* parser)
6352 {
6353   enum tree_code op;
6354   cp_token *token;
6355
6356   /* Peek at the next token.  */
6357   token = cp_lexer_peek_token (parser->lexer);
6358
6359   switch (token->type)
6360     {
6361     case CPP_EQ:
6362       op = NOP_EXPR;
6363       break;
6364
6365     case CPP_MULT_EQ:
6366       op = MULT_EXPR;
6367       break;
6368
6369     case CPP_DIV_EQ:
6370       op = TRUNC_DIV_EXPR;
6371       break;
6372
6373     case CPP_MOD_EQ:
6374       op = TRUNC_MOD_EXPR;
6375       break;
6376
6377     case CPP_PLUS_EQ:
6378       op = PLUS_EXPR;
6379       break;
6380
6381     case CPP_MINUS_EQ:
6382       op = MINUS_EXPR;
6383       break;
6384
6385     case CPP_RSHIFT_EQ:
6386       op = RSHIFT_EXPR;
6387       break;
6388
6389     case CPP_LSHIFT_EQ:
6390       op = LSHIFT_EXPR;
6391       break;
6392
6393     case CPP_AND_EQ:
6394       op = BIT_AND_EXPR;
6395       break;
6396
6397     case CPP_XOR_EQ:
6398       op = BIT_XOR_EXPR;
6399       break;
6400
6401     case CPP_OR_EQ:
6402       op = BIT_IOR_EXPR;
6403       break;
6404
6405     default:
6406       /* Nothing else is an assignment operator.  */
6407       op = ERROR_MARK;
6408     }
6409
6410   /* If it was an assignment operator, consume it.  */
6411   if (op != ERROR_MARK)
6412     cp_lexer_consume_token (parser->lexer);
6413
6414   return op;
6415 }
6416
6417 /* Parse an expression.
6418
6419    expression:
6420      assignment-expression
6421      expression , assignment-expression
6422
6423    CAST_P is true if this expression is the target of a cast.
6424
6425    Returns a representation of the expression.  */
6426
6427 static tree
6428 cp_parser_expression (cp_parser* parser, bool cast_p)
6429 {
6430   tree expression = NULL_TREE;
6431
6432   while (true)
6433     {
6434       tree assignment_expression;
6435
6436       /* Parse the next assignment-expression.  */
6437       assignment_expression
6438         = cp_parser_assignment_expression (parser, cast_p);
6439       /* If this is the first assignment-expression, we can just
6440          save it away.  */
6441       if (!expression)
6442         expression = assignment_expression;
6443       else
6444         expression = build_x_compound_expr (expression,
6445                                             assignment_expression,
6446                                             tf_warning_or_error);
6447       /* If the next token is not a comma, then we are done with the
6448          expression.  */
6449       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6450         break;
6451       /* Consume the `,'.  */
6452       cp_lexer_consume_token (parser->lexer);
6453       /* A comma operator cannot appear in a constant-expression.  */
6454       if (cp_parser_non_integral_constant_expression (parser,
6455                                                       "a comma operator"))
6456         expression = error_mark_node;
6457     }
6458
6459   return expression;
6460 }
6461
6462 /* Parse a constant-expression.
6463
6464    constant-expression:
6465      conditional-expression
6466
6467   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6468   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6469   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6470   is false, NON_CONSTANT_P should be NULL.  */
6471
6472 static tree
6473 cp_parser_constant_expression (cp_parser* parser,
6474                                bool allow_non_constant_p,
6475                                bool *non_constant_p)
6476 {
6477   bool saved_integral_constant_expression_p;
6478   bool saved_allow_non_integral_constant_expression_p;
6479   bool saved_non_integral_constant_expression_p;
6480   tree expression;
6481
6482   /* It might seem that we could simply parse the
6483      conditional-expression, and then check to see if it were
6484      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6485      one that the compiler can figure out is constant, possibly after
6486      doing some simplifications or optimizations.  The standard has a
6487      precise definition of constant-expression, and we must honor
6488      that, even though it is somewhat more restrictive.
6489
6490      For example:
6491
6492        int i[(2, 3)];
6493
6494      is not a legal declaration, because `(2, 3)' is not a
6495      constant-expression.  The `,' operator is forbidden in a
6496      constant-expression.  However, GCC's constant-folding machinery
6497      will fold this operation to an INTEGER_CST for `3'.  */
6498
6499   /* Save the old settings.  */
6500   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6501   saved_allow_non_integral_constant_expression_p
6502     = parser->allow_non_integral_constant_expression_p;
6503   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6504   /* We are now parsing a constant-expression.  */
6505   parser->integral_constant_expression_p = true;
6506   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6507   parser->non_integral_constant_expression_p = false;
6508   /* Although the grammar says "conditional-expression", we parse an
6509      "assignment-expression", which also permits "throw-expression"
6510      and the use of assignment operators.  In the case that
6511      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6512      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6513      actually essential that we look for an assignment-expression.
6514      For example, cp_parser_initializer_clauses uses this function to
6515      determine whether a particular assignment-expression is in fact
6516      constant.  */
6517   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6518   /* Restore the old settings.  */
6519   parser->integral_constant_expression_p
6520     = saved_integral_constant_expression_p;
6521   parser->allow_non_integral_constant_expression_p
6522     = saved_allow_non_integral_constant_expression_p;
6523   if (allow_non_constant_p)
6524     *non_constant_p = parser->non_integral_constant_expression_p;
6525   else if (parser->non_integral_constant_expression_p)
6526     expression = error_mark_node;
6527   parser->non_integral_constant_expression_p
6528     = saved_non_integral_constant_expression_p;
6529
6530   return expression;
6531 }
6532
6533 /* Parse __builtin_offsetof.
6534
6535    offsetof-expression:
6536      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6537
6538    offsetof-member-designator:
6539      id-expression
6540      | offsetof-member-designator "." id-expression
6541      | offsetof-member-designator "[" expression "]"  */
6542
6543 static tree
6544 cp_parser_builtin_offsetof (cp_parser *parser)
6545 {
6546   int save_ice_p, save_non_ice_p;
6547   tree type, expr;
6548   cp_id_kind dummy;
6549   cp_token *token;
6550
6551   /* We're about to accept non-integral-constant things, but will
6552      definitely yield an integral constant expression.  Save and
6553      restore these values around our local parsing.  */
6554   save_ice_p = parser->integral_constant_expression_p;
6555   save_non_ice_p = parser->non_integral_constant_expression_p;
6556
6557   /* Consume the "__builtin_offsetof" token.  */
6558   cp_lexer_consume_token (parser->lexer);
6559   /* Consume the opening `('.  */
6560   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6561   /* Parse the type-id.  */
6562   type = cp_parser_type_id (parser);
6563   /* Look for the `,'.  */
6564   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6565   token = cp_lexer_peek_token (parser->lexer);
6566
6567   /* Build the (type *)null that begins the traditional offsetof macro.  */
6568   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6569                             tf_warning_or_error);
6570
6571   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6572   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6573                                                  true, &dummy, token->location);
6574   while (true)
6575     {
6576       token = cp_lexer_peek_token (parser->lexer);
6577       switch (token->type)
6578         {
6579         case CPP_OPEN_SQUARE:
6580           /* offsetof-member-designator "[" expression "]" */
6581           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6582           break;
6583
6584         case CPP_DOT:
6585           /* offsetof-member-designator "." identifier */
6586           cp_lexer_consume_token (parser->lexer);
6587           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6588                                                          true, &dummy,
6589                                                          token->location);
6590           break;
6591
6592         case CPP_CLOSE_PAREN:
6593           /* Consume the ")" token.  */
6594           cp_lexer_consume_token (parser->lexer);
6595           goto success;
6596
6597         default:
6598           /* Error.  We know the following require will fail, but
6599              that gives the proper error message.  */
6600           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6601           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6602           expr = error_mark_node;
6603           goto failure;
6604         }
6605     }
6606
6607  success:
6608   /* If we're processing a template, we can't finish the semantics yet.
6609      Otherwise we can fold the entire expression now.  */
6610   if (processing_template_decl)
6611     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6612   else
6613     expr = finish_offsetof (expr);
6614
6615  failure:
6616   parser->integral_constant_expression_p = save_ice_p;
6617   parser->non_integral_constant_expression_p = save_non_ice_p;
6618
6619   return expr;
6620 }
6621
6622 /* Parse a trait expression.  */
6623
6624 static tree
6625 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6626 {
6627   cp_trait_kind kind;
6628   tree type1, type2 = NULL_TREE;
6629   bool binary = false;
6630   cp_decl_specifier_seq decl_specs;
6631
6632   switch (keyword)
6633     {
6634     case RID_HAS_NOTHROW_ASSIGN:
6635       kind = CPTK_HAS_NOTHROW_ASSIGN;
6636       break;
6637     case RID_HAS_NOTHROW_CONSTRUCTOR:
6638       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6639       break;
6640     case RID_HAS_NOTHROW_COPY:
6641       kind = CPTK_HAS_NOTHROW_COPY;
6642       break;
6643     case RID_HAS_TRIVIAL_ASSIGN:
6644       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6645       break;
6646     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6647       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6648       break;
6649     case RID_HAS_TRIVIAL_COPY:
6650       kind = CPTK_HAS_TRIVIAL_COPY;
6651       break;
6652     case RID_HAS_TRIVIAL_DESTRUCTOR:
6653       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6654       break;
6655     case RID_HAS_VIRTUAL_DESTRUCTOR:
6656       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6657       break;
6658     case RID_IS_ABSTRACT:
6659       kind = CPTK_IS_ABSTRACT;
6660       break;
6661     case RID_IS_BASE_OF:
6662       kind = CPTK_IS_BASE_OF;
6663       binary = true;
6664       break;
6665     case RID_IS_CLASS:
6666       kind = CPTK_IS_CLASS;
6667       break;
6668     case RID_IS_CONVERTIBLE_TO:
6669       kind = CPTK_IS_CONVERTIBLE_TO;
6670       binary = true;
6671       break;
6672     case RID_IS_EMPTY:
6673       kind = CPTK_IS_EMPTY;
6674       break;
6675     case RID_IS_ENUM:
6676       kind = CPTK_IS_ENUM;
6677       break;
6678     case RID_IS_POD:
6679       kind = CPTK_IS_POD;
6680       break;
6681     case RID_IS_POLYMORPHIC:
6682       kind = CPTK_IS_POLYMORPHIC;
6683       break;
6684     case RID_IS_UNION:
6685       kind = CPTK_IS_UNION;
6686       break;
6687     default:
6688       gcc_unreachable ();
6689     }
6690
6691   /* Consume the token.  */
6692   cp_lexer_consume_token (parser->lexer);
6693
6694   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6695
6696   type1 = cp_parser_type_id (parser);
6697
6698   if (type1 == error_mark_node)
6699     return error_mark_node;
6700
6701   /* Build a trivial decl-specifier-seq.  */
6702   clear_decl_specs (&decl_specs);
6703   decl_specs.type = type1;
6704
6705   /* Call grokdeclarator to figure out what type this is.  */
6706   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6707                           /*initialized=*/0, /*attrlist=*/NULL);
6708
6709   if (binary)
6710     {
6711       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6712  
6713       type2 = cp_parser_type_id (parser);
6714
6715       if (type2 == error_mark_node)
6716         return error_mark_node;
6717
6718       /* Build a trivial decl-specifier-seq.  */
6719       clear_decl_specs (&decl_specs);
6720       decl_specs.type = type2;
6721
6722       /* Call grokdeclarator to figure out what type this is.  */
6723       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6724                               /*initialized=*/0, /*attrlist=*/NULL);
6725     }
6726
6727   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6728
6729   /* Complete the trait expression, which may mean either processing
6730      the trait expr now or saving it for template instantiation.  */
6731   return finish_trait_expr (kind, type1, type2);
6732 }
6733
6734 /* Statements [gram.stmt.stmt]  */
6735
6736 /* Parse a statement.
6737
6738    statement:
6739      labeled-statement
6740      expression-statement
6741      compound-statement
6742      selection-statement
6743      iteration-statement
6744      jump-statement
6745      declaration-statement
6746      try-block
6747
6748   IN_COMPOUND is true when the statement is nested inside a
6749   cp_parser_compound_statement; this matters for certain pragmas.
6750
6751   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6752   is a (possibly labeled) if statement which is not enclosed in braces
6753   and has an else clause.  This is used to implement -Wparentheses.  */
6754
6755 static void
6756 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6757                      bool in_compound, bool *if_p)
6758 {
6759   tree statement;
6760   cp_token *token;
6761   location_t statement_location;
6762
6763  restart:
6764   if (if_p != NULL)
6765     *if_p = false;
6766   /* There is no statement yet.  */
6767   statement = NULL_TREE;
6768   /* Peek at the next token.  */
6769   token = cp_lexer_peek_token (parser->lexer);
6770   /* Remember the location of the first token in the statement.  */
6771   statement_location = token->location;
6772   /* If this is a keyword, then that will often determine what kind of
6773      statement we have.  */
6774   if (token->type == CPP_KEYWORD)
6775     {
6776       enum rid keyword = token->keyword;
6777
6778       switch (keyword)
6779         {
6780         case RID_CASE:
6781         case RID_DEFAULT:
6782           /* Looks like a labeled-statement with a case label.
6783              Parse the label, and then use tail recursion to parse
6784              the statement.  */
6785           cp_parser_label_for_labeled_statement (parser);
6786           goto restart;
6787
6788         case RID_IF:
6789         case RID_SWITCH:
6790           statement = cp_parser_selection_statement (parser, if_p);
6791           break;
6792
6793         case RID_WHILE:
6794         case RID_DO:
6795         case RID_FOR:
6796           statement = cp_parser_iteration_statement (parser);
6797           break;
6798
6799         case RID_BREAK:
6800         case RID_CONTINUE:
6801         case RID_RETURN:
6802         case RID_GOTO:
6803           statement = cp_parser_jump_statement (parser);
6804           break;
6805
6806           /* Objective-C++ exception-handling constructs.  */
6807         case RID_AT_TRY:
6808         case RID_AT_CATCH:
6809         case RID_AT_FINALLY:
6810         case RID_AT_SYNCHRONIZED:
6811         case RID_AT_THROW:
6812           statement = cp_parser_objc_statement (parser);
6813           break;
6814
6815         case RID_TRY:
6816           statement = cp_parser_try_block (parser);
6817           break;
6818
6819         case RID_NAMESPACE:
6820           /* This must be a namespace alias definition.  */
6821           cp_parser_declaration_statement (parser);
6822           return;
6823           
6824         default:
6825           /* It might be a keyword like `int' that can start a
6826              declaration-statement.  */
6827           break;
6828         }
6829     }
6830   else if (token->type == CPP_NAME)
6831     {
6832       /* If the next token is a `:', then we are looking at a
6833          labeled-statement.  */
6834       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6835       if (token->type == CPP_COLON)
6836         {
6837           /* Looks like a labeled-statement with an ordinary label.
6838              Parse the label, and then use tail recursion to parse
6839              the statement.  */
6840           cp_parser_label_for_labeled_statement (parser);
6841           goto restart;
6842         }
6843     }
6844   /* Anything that starts with a `{' must be a compound-statement.  */
6845   else if (token->type == CPP_OPEN_BRACE)
6846     statement = cp_parser_compound_statement (parser, NULL, false);
6847   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6848      a statement all its own.  */
6849   else if (token->type == CPP_PRAGMA)
6850     {
6851       /* Only certain OpenMP pragmas are attached to statements, and thus
6852          are considered statements themselves.  All others are not.  In
6853          the context of a compound, accept the pragma as a "statement" and
6854          return so that we can check for a close brace.  Otherwise we
6855          require a real statement and must go back and read one.  */
6856       if (in_compound)
6857         cp_parser_pragma (parser, pragma_compound);
6858       else if (!cp_parser_pragma (parser, pragma_stmt))
6859         goto restart;
6860       return;
6861     }
6862   else if (token->type == CPP_EOF)
6863     {
6864       cp_parser_error (parser, "expected statement");
6865       return;
6866     }
6867
6868   /* Everything else must be a declaration-statement or an
6869      expression-statement.  Try for the declaration-statement
6870      first, unless we are looking at a `;', in which case we know that
6871      we have an expression-statement.  */
6872   if (!statement)
6873     {
6874       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6875         {
6876           cp_parser_parse_tentatively (parser);
6877           /* Try to parse the declaration-statement.  */
6878           cp_parser_declaration_statement (parser);
6879           /* If that worked, we're done.  */
6880           if (cp_parser_parse_definitely (parser))
6881             return;
6882         }
6883       /* Look for an expression-statement instead.  */
6884       statement = cp_parser_expression_statement (parser, in_statement_expr);
6885     }
6886
6887   /* Set the line number for the statement.  */
6888   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6889     SET_EXPR_LOCATION (statement, statement_location);
6890 }
6891
6892 /* Parse the label for a labeled-statement, i.e.
6893
6894    identifier :
6895    case constant-expression :
6896    default :
6897
6898    GNU Extension:
6899    case constant-expression ... constant-expression : statement
6900
6901    When a label is parsed without errors, the label is added to the
6902    parse tree by the finish_* functions, so this function doesn't
6903    have to return the label.  */
6904
6905 static void
6906 cp_parser_label_for_labeled_statement (cp_parser* parser)
6907 {
6908   cp_token *token;
6909
6910   /* The next token should be an identifier.  */
6911   token = cp_lexer_peek_token (parser->lexer);
6912   if (token->type != CPP_NAME
6913       && token->type != CPP_KEYWORD)
6914     {
6915       cp_parser_error (parser, "expected labeled-statement");
6916       return;
6917     }
6918
6919   switch (token->keyword)
6920     {
6921     case RID_CASE:
6922       {
6923         tree expr, expr_hi;
6924         cp_token *ellipsis;
6925
6926         /* Consume the `case' token.  */
6927         cp_lexer_consume_token (parser->lexer);
6928         /* Parse the constant-expression.  */
6929         expr = cp_parser_constant_expression (parser,
6930                                               /*allow_non_constant_p=*/false,
6931                                               NULL);
6932
6933         ellipsis = cp_lexer_peek_token (parser->lexer);
6934         if (ellipsis->type == CPP_ELLIPSIS)
6935           {
6936             /* Consume the `...' token.  */
6937             cp_lexer_consume_token (parser->lexer);
6938             expr_hi =
6939               cp_parser_constant_expression (parser,
6940                                              /*allow_non_constant_p=*/false,
6941                                              NULL);
6942             /* We don't need to emit warnings here, as the common code
6943                will do this for us.  */
6944           }
6945         else
6946           expr_hi = NULL_TREE;
6947
6948         if (parser->in_switch_statement_p)
6949           finish_case_label (expr, expr_hi);
6950         else
6951           error ("%Hcase label %qE not within a switch statement",
6952                  &token->location, expr);
6953       }
6954       break;
6955
6956     case RID_DEFAULT:
6957       /* Consume the `default' token.  */
6958       cp_lexer_consume_token (parser->lexer);
6959
6960       if (parser->in_switch_statement_p)
6961         finish_case_label (NULL_TREE, NULL_TREE);
6962       else
6963         error ("%Hcase label not within a switch statement", &token->location);
6964       break;
6965
6966     default:
6967       /* Anything else must be an ordinary label.  */
6968       finish_label_stmt (cp_parser_identifier (parser));
6969       break;
6970     }
6971
6972   /* Require the `:' token.  */
6973   cp_parser_require (parser, CPP_COLON, "%<:%>");
6974 }
6975
6976 /* Parse an expression-statement.
6977
6978    expression-statement:
6979      expression [opt] ;
6980
6981    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6982    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6983    indicates whether this expression-statement is part of an
6984    expression statement.  */
6985
6986 static tree
6987 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6988 {
6989   tree statement = NULL_TREE;
6990
6991   /* If the next token is a ';', then there is no expression
6992      statement.  */
6993   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6994     statement = cp_parser_expression (parser, /*cast_p=*/false);
6995
6996   /* Consume the final `;'.  */
6997   cp_parser_consume_semicolon_at_end_of_statement (parser);
6998
6999   if (in_statement_expr
7000       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7001     /* This is the final expression statement of a statement
7002        expression.  */
7003     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7004   else if (statement)
7005     statement = finish_expr_stmt (statement);
7006   else
7007     finish_stmt ();
7008
7009   return statement;
7010 }
7011
7012 /* Parse a compound-statement.
7013
7014    compound-statement:
7015      { statement-seq [opt] }
7016
7017    GNU extension:
7018
7019    compound-statement:
7020      { label-declaration-seq [opt] statement-seq [opt] }
7021
7022    label-declaration-seq:
7023      label-declaration
7024      label-declaration-seq label-declaration
7025
7026    Returns a tree representing the statement.  */
7027
7028 static tree
7029 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7030                               bool in_try)
7031 {
7032   tree compound_stmt;
7033
7034   /* Consume the `{'.  */
7035   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7036     return error_mark_node;
7037   /* Begin the compound-statement.  */
7038   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7039   /* If the next keyword is `__label__' we have a label declaration.  */
7040   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7041     cp_parser_label_declaration (parser);
7042   /* Parse an (optional) statement-seq.  */
7043   cp_parser_statement_seq_opt (parser, in_statement_expr);
7044   /* Finish the compound-statement.  */
7045   finish_compound_stmt (compound_stmt);
7046   /* Consume the `}'.  */
7047   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7048
7049   return compound_stmt;
7050 }
7051
7052 /* Parse an (optional) statement-seq.
7053
7054    statement-seq:
7055      statement
7056      statement-seq [opt] statement  */
7057
7058 static void
7059 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7060 {
7061   /* Scan statements until there aren't any more.  */
7062   while (true)
7063     {
7064       cp_token *token = cp_lexer_peek_token (parser->lexer);
7065
7066       /* If we're looking at a `}', then we've run out of statements.  */
7067       if (token->type == CPP_CLOSE_BRACE
7068           || token->type == CPP_EOF
7069           || token->type == CPP_PRAGMA_EOL)
7070         break;
7071       
7072       /* If we are in a compound statement and find 'else' then
7073          something went wrong.  */
7074       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7075         {
7076           if (parser->in_statement & IN_IF_STMT) 
7077             break;
7078           else
7079             {
7080               token = cp_lexer_consume_token (parser->lexer);
7081               error ("%H%<else%> without a previous %<if%>", &token->location);
7082             }
7083         }
7084
7085       /* Parse the statement.  */
7086       cp_parser_statement (parser, in_statement_expr, true, NULL);
7087     }
7088 }
7089
7090 /* Parse a selection-statement.
7091
7092    selection-statement:
7093      if ( condition ) statement
7094      if ( condition ) statement else statement
7095      switch ( condition ) statement
7096
7097    Returns the new IF_STMT or SWITCH_STMT.
7098
7099    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7100    is a (possibly labeled) if statement which is not enclosed in
7101    braces and has an else clause.  This is used to implement
7102    -Wparentheses.  */
7103
7104 static tree
7105 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7106 {
7107   cp_token *token;
7108   enum rid keyword;
7109
7110   if (if_p != NULL)
7111     *if_p = false;
7112
7113   /* Peek at the next token.  */
7114   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7115
7116   /* See what kind of keyword it is.  */
7117   keyword = token->keyword;
7118   switch (keyword)
7119     {
7120     case RID_IF:
7121     case RID_SWITCH:
7122       {
7123         tree statement;
7124         tree condition;
7125
7126         /* Look for the `('.  */
7127         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7128           {
7129             cp_parser_skip_to_end_of_statement (parser);
7130             return error_mark_node;
7131           }
7132
7133         /* Begin the selection-statement.  */
7134         if (keyword == RID_IF)
7135           statement = begin_if_stmt ();
7136         else
7137           statement = begin_switch_stmt ();
7138
7139         /* Parse the condition.  */
7140         condition = cp_parser_condition (parser);
7141         /* Look for the `)'.  */
7142         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7143           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7144                                                  /*consume_paren=*/true);
7145
7146         if (keyword == RID_IF)
7147           {
7148             bool nested_if;
7149             unsigned char in_statement;
7150
7151             /* Add the condition.  */
7152             finish_if_stmt_cond (condition, statement);
7153
7154             /* Parse the then-clause.  */
7155             in_statement = parser->in_statement;
7156             parser->in_statement |= IN_IF_STMT;
7157             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7158             parser->in_statement = in_statement;
7159
7160             finish_then_clause (statement);
7161
7162             /* If the next token is `else', parse the else-clause.  */
7163             if (cp_lexer_next_token_is_keyword (parser->lexer,
7164                                                 RID_ELSE))
7165               {
7166                 /* Consume the `else' keyword.  */
7167                 cp_lexer_consume_token (parser->lexer);
7168                 begin_else_clause (statement);
7169                 /* Parse the else-clause.  */
7170                 cp_parser_implicitly_scoped_statement (parser, NULL);
7171                 finish_else_clause (statement);
7172
7173                 /* If we are currently parsing a then-clause, then
7174                    IF_P will not be NULL.  We set it to true to
7175                    indicate that this if statement has an else clause.
7176                    This may trigger the Wparentheses warning below
7177                    when we get back up to the parent if statement.  */
7178                 if (if_p != NULL)
7179                   *if_p = true;
7180               }
7181             else
7182               {
7183                 /* This if statement does not have an else clause.  If
7184                    NESTED_IF is true, then the then-clause is an if
7185                    statement which does have an else clause.  We warn
7186                    about the potential ambiguity.  */
7187                 if (nested_if)
7188                   warning (OPT_Wparentheses,
7189                            ("%Hsuggest explicit braces "
7190                             "to avoid ambiguous %<else%>"),
7191                            EXPR_LOCUS (statement));
7192               }
7193
7194             /* Now we're all done with the if-statement.  */
7195             finish_if_stmt (statement);
7196           }
7197         else
7198           {
7199             bool in_switch_statement_p;
7200             unsigned char in_statement;
7201
7202             /* Add the condition.  */
7203             finish_switch_cond (condition, statement);
7204
7205             /* Parse the body of the switch-statement.  */
7206             in_switch_statement_p = parser->in_switch_statement_p;
7207             in_statement = parser->in_statement;
7208             parser->in_switch_statement_p = true;
7209             parser->in_statement |= IN_SWITCH_STMT;
7210             cp_parser_implicitly_scoped_statement (parser, NULL);
7211             parser->in_switch_statement_p = in_switch_statement_p;
7212             parser->in_statement = in_statement;
7213
7214             /* Now we're all done with the switch-statement.  */
7215             finish_switch_stmt (statement);
7216           }
7217
7218         return statement;
7219       }
7220       break;
7221
7222     default:
7223       cp_parser_error (parser, "expected selection-statement");
7224       return error_mark_node;
7225     }
7226 }
7227
7228 /* Parse a condition.
7229
7230    condition:
7231      expression
7232      type-specifier-seq declarator = initializer-clause
7233      type-specifier-seq declarator braced-init-list
7234
7235    GNU Extension:
7236
7237    condition:
7238      type-specifier-seq declarator asm-specification [opt]
7239        attributes [opt] = assignment-expression
7240
7241    Returns the expression that should be tested.  */
7242
7243 static tree
7244 cp_parser_condition (cp_parser* parser)
7245 {
7246   cp_decl_specifier_seq type_specifiers;
7247   const char *saved_message;
7248
7249   /* Try the declaration first.  */
7250   cp_parser_parse_tentatively (parser);
7251   /* New types are not allowed in the type-specifier-seq for a
7252      condition.  */
7253   saved_message = parser->type_definition_forbidden_message;
7254   parser->type_definition_forbidden_message
7255     = "types may not be defined in conditions";
7256   /* Parse the type-specifier-seq.  */
7257   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7258                                 &type_specifiers);
7259   /* Restore the saved message.  */
7260   parser->type_definition_forbidden_message = saved_message;
7261   /* If all is well, we might be looking at a declaration.  */
7262   if (!cp_parser_error_occurred (parser))
7263     {
7264       tree decl;
7265       tree asm_specification;
7266       tree attributes;
7267       cp_declarator *declarator;
7268       tree initializer = NULL_TREE;
7269
7270       /* Parse the declarator.  */
7271       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7272                                          /*ctor_dtor_or_conv_p=*/NULL,
7273                                          /*parenthesized_p=*/NULL,
7274                                          /*member_p=*/false);
7275       /* Parse the attributes.  */
7276       attributes = cp_parser_attributes_opt (parser);
7277       /* Parse the asm-specification.  */
7278       asm_specification = cp_parser_asm_specification_opt (parser);
7279       /* If the next token is not an `=' or '{', then we might still be
7280          looking at an expression.  For example:
7281
7282            if (A(a).x)
7283
7284          looks like a decl-specifier-seq and a declarator -- but then
7285          there is no `=', so this is an expression.  */
7286       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7287           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7288         cp_parser_simulate_error (parser);
7289         
7290       /* If we did see an `=' or '{', then we are looking at a declaration
7291          for sure.  */
7292       if (cp_parser_parse_definitely (parser))
7293         {
7294           tree pushed_scope;
7295           bool non_constant_p;
7296           bool flags = LOOKUP_ONLYCONVERTING;
7297
7298           /* Create the declaration.  */
7299           decl = start_decl (declarator, &type_specifiers,
7300                              /*initialized_p=*/true,
7301                              attributes, /*prefix_attributes=*/NULL_TREE,
7302                              &pushed_scope);
7303
7304           /* Parse the initializer.  */
7305           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7306             {
7307               initializer = cp_parser_braced_list (parser, &non_constant_p);
7308               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7309               flags = 0;
7310             }
7311           else
7312             {
7313               /* Consume the `='.  */
7314               cp_lexer_consume_token (parser->lexer);
7315               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7316             }
7317           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7318             maybe_warn_cpp0x ("extended initializer lists");
7319
7320           if (!non_constant_p)
7321             initializer = fold_non_dependent_expr (initializer);
7322
7323           /* Process the initializer.  */
7324           cp_finish_decl (decl,
7325                           initializer, !non_constant_p,
7326                           asm_specification,
7327                           flags);
7328
7329           if (pushed_scope)
7330             pop_scope (pushed_scope);
7331
7332           return convert_from_reference (decl);
7333         }
7334     }
7335   /* If we didn't even get past the declarator successfully, we are
7336      definitely not looking at a declaration.  */
7337   else
7338     cp_parser_abort_tentative_parse (parser);
7339
7340   /* Otherwise, we are looking at an expression.  */
7341   return cp_parser_expression (parser, /*cast_p=*/false);
7342 }
7343
7344 /* We check for a ) immediately followed by ; with no whitespacing
7345    between.  This is used to issue a warning for:
7346
7347      while (...);
7348
7349    and:
7350
7351      for (...);
7352
7353    as the semicolon is probably extraneous.
7354
7355    On parse errors, the next token might not be a ), so do nothing in
7356    that case. */
7357
7358 static void
7359 check_empty_body (cp_parser* parser, const char* type)
7360 {
7361   cp_token *token;
7362   cp_token *close_paren;
7363   expanded_location close_loc;
7364   expanded_location semi_loc;
7365   
7366   close_paren = cp_lexer_peek_token (parser->lexer);
7367   if (close_paren->type != CPP_CLOSE_PAREN)
7368     return;
7369
7370   close_loc = expand_location (close_paren->location);
7371   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7372
7373   if (token->type != CPP_SEMICOLON
7374       || (token->flags & PREV_WHITE))
7375     return;
7376
7377   semi_loc =  expand_location (token->location);
7378   if (close_loc.line == semi_loc.line
7379       && close_loc.column+1 == semi_loc.column)
7380     warning (OPT_Wempty_body,
7381              "suggest a space before %<;%> or explicit braces around empty "
7382              "body in %<%s%> statement",
7383              type);
7384 }
7385
7386 /* Parse an iteration-statement.
7387
7388    iteration-statement:
7389      while ( condition ) statement
7390      do statement while ( expression ) ;
7391      for ( for-init-statement condition [opt] ; expression [opt] )
7392        statement
7393
7394    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7395
7396 static tree
7397 cp_parser_iteration_statement (cp_parser* parser)
7398 {
7399   cp_token *token;
7400   enum rid keyword;
7401   tree statement;
7402   unsigned char in_statement;
7403
7404   /* Peek at the next token.  */
7405   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7406   if (!token)
7407     return error_mark_node;
7408
7409   /* Remember whether or not we are already within an iteration
7410      statement.  */
7411   in_statement = parser->in_statement;
7412
7413   /* See what kind of keyword it is.  */
7414   keyword = token->keyword;
7415   switch (keyword)
7416     {
7417     case RID_WHILE:
7418       {
7419         tree condition;
7420
7421         /* Begin the while-statement.  */
7422         statement = begin_while_stmt ();
7423         /* Look for the `('.  */
7424         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7425         /* Parse the condition.  */
7426         condition = cp_parser_condition (parser);
7427         finish_while_stmt_cond (condition, statement);
7428         check_empty_body (parser, "while");
7429         /* Look for the `)'.  */
7430         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7431         /* Parse the dependent statement.  */
7432         parser->in_statement = IN_ITERATION_STMT;
7433         cp_parser_already_scoped_statement (parser);
7434         parser->in_statement = in_statement;
7435         /* We're done with the while-statement.  */
7436         finish_while_stmt (statement);
7437       }
7438       break;
7439
7440     case RID_DO:
7441       {
7442         tree expression;
7443
7444         /* Begin the do-statement.  */
7445         statement = begin_do_stmt ();
7446         /* Parse the body of the do-statement.  */
7447         parser->in_statement = IN_ITERATION_STMT;
7448         cp_parser_implicitly_scoped_statement (parser, NULL);
7449         parser->in_statement = in_statement;
7450         finish_do_body (statement);
7451         /* Look for the `while' keyword.  */
7452         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7453         /* Look for the `('.  */
7454         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7455         /* Parse the expression.  */
7456         expression = cp_parser_expression (parser, /*cast_p=*/false);
7457         /* We're done with the do-statement.  */
7458         finish_do_stmt (expression, statement);
7459         /* Look for the `)'.  */
7460         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7461         /* Look for the `;'.  */
7462         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7463       }
7464       break;
7465
7466     case RID_FOR:
7467       {
7468         tree condition = NULL_TREE;
7469         tree expression = NULL_TREE;
7470
7471         /* Begin the for-statement.  */
7472         statement = begin_for_stmt ();
7473         /* Look for the `('.  */
7474         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7475         /* Parse the initialization.  */
7476         cp_parser_for_init_statement (parser);
7477         finish_for_init_stmt (statement);
7478
7479         /* If there's a condition, process it.  */
7480         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7481           condition = cp_parser_condition (parser);
7482         finish_for_cond (condition, statement);
7483         /* Look for the `;'.  */
7484         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7485
7486         /* If there's an expression, process it.  */
7487         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7488           expression = cp_parser_expression (parser, /*cast_p=*/false);
7489         finish_for_expr (expression, statement);
7490         check_empty_body (parser, "for");
7491         /* Look for the `)'.  */
7492         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7493
7494         /* Parse the body of the for-statement.  */
7495         parser->in_statement = IN_ITERATION_STMT;
7496         cp_parser_already_scoped_statement (parser);
7497         parser->in_statement = in_statement;
7498
7499         /* We're done with the for-statement.  */
7500         finish_for_stmt (statement);
7501       }
7502       break;
7503
7504     default:
7505       cp_parser_error (parser, "expected iteration-statement");
7506       statement = error_mark_node;
7507       break;
7508     }
7509
7510   return statement;
7511 }
7512
7513 /* Parse a for-init-statement.
7514
7515    for-init-statement:
7516      expression-statement
7517      simple-declaration  */
7518
7519 static void
7520 cp_parser_for_init_statement (cp_parser* parser)
7521 {
7522   /* If the next token is a `;', then we have an empty
7523      expression-statement.  Grammatically, this is also a
7524      simple-declaration, but an invalid one, because it does not
7525      declare anything.  Therefore, if we did not handle this case
7526      specially, we would issue an error message about an invalid
7527      declaration.  */
7528   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7529     {
7530       /* We're going to speculatively look for a declaration, falling back
7531          to an expression, if necessary.  */
7532       cp_parser_parse_tentatively (parser);
7533       /* Parse the declaration.  */
7534       cp_parser_simple_declaration (parser,
7535                                     /*function_definition_allowed_p=*/false);
7536       /* If the tentative parse failed, then we shall need to look for an
7537          expression-statement.  */
7538       if (cp_parser_parse_definitely (parser))
7539         return;
7540     }
7541
7542   cp_parser_expression_statement (parser, false);
7543 }
7544
7545 /* Parse a jump-statement.
7546
7547    jump-statement:
7548      break ;
7549      continue ;
7550      return expression [opt] ;
7551      return braced-init-list ;
7552      goto identifier ;
7553
7554    GNU extension:
7555
7556    jump-statement:
7557      goto * expression ;
7558
7559    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7560
7561 static tree
7562 cp_parser_jump_statement (cp_parser* parser)
7563 {
7564   tree statement = error_mark_node;
7565   cp_token *token;
7566   enum rid keyword;
7567   unsigned char in_statement;
7568
7569   /* Peek at the next token.  */
7570   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7571   if (!token)
7572     return error_mark_node;
7573
7574   /* See what kind of keyword it is.  */
7575   keyword = token->keyword;
7576   switch (keyword)
7577     {
7578     case RID_BREAK:
7579       in_statement = parser->in_statement & ~IN_IF_STMT;      
7580       switch (in_statement)
7581         {
7582         case 0:
7583           error ("%Hbreak statement not within loop or switch", &token->location);
7584           break;
7585         default:
7586           gcc_assert ((in_statement & IN_SWITCH_STMT)
7587                       || in_statement == IN_ITERATION_STMT);
7588           statement = finish_break_stmt ();
7589           break;
7590         case IN_OMP_BLOCK:
7591           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7592           break;
7593         case IN_OMP_FOR:
7594           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7595           break;
7596         }
7597       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7598       break;
7599
7600     case RID_CONTINUE:
7601       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7602         {
7603         case 0:
7604           error ("%Hcontinue statement not within a loop", &token->location);
7605           break;
7606         case IN_ITERATION_STMT:
7607         case IN_OMP_FOR:
7608           statement = finish_continue_stmt ();
7609           break;
7610         case IN_OMP_BLOCK:
7611           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7612           break;
7613         default:
7614           gcc_unreachable ();
7615         }
7616       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7617       break;
7618
7619     case RID_RETURN:
7620       {
7621         tree expr;
7622         bool expr_non_constant_p;
7623
7624         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7625           {
7626             maybe_warn_cpp0x ("extended initializer lists");
7627             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7628           }
7629         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7630           expr = cp_parser_expression (parser, /*cast_p=*/false);
7631         else
7632           /* If the next token is a `;', then there is no
7633              expression.  */
7634           expr = NULL_TREE;
7635         /* Build the return-statement.  */
7636         statement = finish_return_stmt (expr);
7637         /* Look for the final `;'.  */
7638         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7639       }
7640       break;
7641
7642     case RID_GOTO:
7643       /* Create the goto-statement.  */
7644       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7645         {
7646           /* Issue a warning about this use of a GNU extension.  */
7647           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7648           /* Consume the '*' token.  */
7649           cp_lexer_consume_token (parser->lexer);
7650           /* Parse the dependent expression.  */
7651           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7652         }
7653       else
7654         finish_goto_stmt (cp_parser_identifier (parser));
7655       /* Look for the final `;'.  */
7656       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7657       break;
7658
7659     default:
7660       cp_parser_error (parser, "expected jump-statement");
7661       break;
7662     }
7663
7664   return statement;
7665 }
7666
7667 /* Parse a declaration-statement.
7668
7669    declaration-statement:
7670      block-declaration  */
7671
7672 static void
7673 cp_parser_declaration_statement (cp_parser* parser)
7674 {
7675   void *p;
7676
7677   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7678   p = obstack_alloc (&declarator_obstack, 0);
7679
7680  /* Parse the block-declaration.  */
7681   cp_parser_block_declaration (parser, /*statement_p=*/true);
7682
7683   /* Free any declarators allocated.  */
7684   obstack_free (&declarator_obstack, p);
7685
7686   /* Finish off the statement.  */
7687   finish_stmt ();
7688 }
7689
7690 /* Some dependent statements (like `if (cond) statement'), are
7691    implicitly in their own scope.  In other words, if the statement is
7692    a single statement (as opposed to a compound-statement), it is
7693    none-the-less treated as if it were enclosed in braces.  Any
7694    declarations appearing in the dependent statement are out of scope
7695    after control passes that point.  This function parses a statement,
7696    but ensures that is in its own scope, even if it is not a
7697    compound-statement.
7698
7699    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7700    is a (possibly labeled) if statement which is not enclosed in
7701    braces and has an else clause.  This is used to implement
7702    -Wparentheses.
7703
7704    Returns the new statement.  */
7705
7706 static tree
7707 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7708 {
7709   tree statement;
7710
7711   if (if_p != NULL)
7712     *if_p = false;
7713
7714   /* Mark if () ; with a special NOP_EXPR.  */
7715   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7716     {
7717       cp_lexer_consume_token (parser->lexer);
7718       statement = add_stmt (build_empty_stmt ());
7719     }
7720   /* if a compound is opened, we simply parse the statement directly.  */
7721   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7722     statement = cp_parser_compound_statement (parser, NULL, false);
7723   /* If the token is not a `{', then we must take special action.  */
7724   else
7725     {
7726       /* Create a compound-statement.  */
7727       statement = begin_compound_stmt (0);
7728       /* Parse the dependent-statement.  */
7729       cp_parser_statement (parser, NULL_TREE, false, if_p);
7730       /* Finish the dummy compound-statement.  */
7731       finish_compound_stmt (statement);
7732     }
7733
7734   /* Return the statement.  */
7735   return statement;
7736 }
7737
7738 /* For some dependent statements (like `while (cond) statement'), we
7739    have already created a scope.  Therefore, even if the dependent
7740    statement is a compound-statement, we do not want to create another
7741    scope.  */
7742
7743 static void
7744 cp_parser_already_scoped_statement (cp_parser* parser)
7745 {
7746   /* If the token is a `{', then we must take special action.  */
7747   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7748     cp_parser_statement (parser, NULL_TREE, false, NULL);
7749   else
7750     {
7751       /* Avoid calling cp_parser_compound_statement, so that we
7752          don't create a new scope.  Do everything else by hand.  */
7753       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7754       cp_parser_statement_seq_opt (parser, NULL_TREE);
7755       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7756     }
7757 }
7758
7759 /* Declarations [gram.dcl.dcl] */
7760
7761 /* Parse an optional declaration-sequence.
7762
7763    declaration-seq:
7764      declaration
7765      declaration-seq declaration  */
7766
7767 static void
7768 cp_parser_declaration_seq_opt (cp_parser* parser)
7769 {
7770   while (true)
7771     {
7772       cp_token *token;
7773
7774       token = cp_lexer_peek_token (parser->lexer);
7775
7776       if (token->type == CPP_CLOSE_BRACE
7777           || token->type == CPP_EOF
7778           || token->type == CPP_PRAGMA_EOL)
7779         break;
7780
7781       if (token->type == CPP_SEMICOLON)
7782         {
7783           /* A declaration consisting of a single semicolon is
7784              invalid.  Allow it unless we're being pedantic.  */
7785           cp_lexer_consume_token (parser->lexer);
7786           if (!in_system_header)
7787             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7788           continue;
7789         }
7790
7791       /* If we're entering or exiting a region that's implicitly
7792          extern "C", modify the lang context appropriately.  */
7793       if (!parser->implicit_extern_c && token->implicit_extern_c)
7794         {
7795           push_lang_context (lang_name_c);
7796           parser->implicit_extern_c = true;
7797         }
7798       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7799         {
7800           pop_lang_context ();
7801           parser->implicit_extern_c = false;
7802         }
7803
7804       if (token->type == CPP_PRAGMA)
7805         {
7806           /* A top-level declaration can consist solely of a #pragma.
7807              A nested declaration cannot, so this is done here and not
7808              in cp_parser_declaration.  (A #pragma at block scope is
7809              handled in cp_parser_statement.)  */
7810           cp_parser_pragma (parser, pragma_external);
7811           continue;
7812         }
7813
7814       /* Parse the declaration itself.  */
7815       cp_parser_declaration (parser);
7816     }
7817 }
7818
7819 /* Parse a declaration.
7820
7821    declaration:
7822      block-declaration
7823      function-definition
7824      template-declaration
7825      explicit-instantiation
7826      explicit-specialization
7827      linkage-specification
7828      namespace-definition
7829
7830    GNU extension:
7831
7832    declaration:
7833       __extension__ declaration */
7834
7835 static void
7836 cp_parser_declaration (cp_parser* parser)
7837 {
7838   cp_token token1;
7839   cp_token token2;
7840   int saved_pedantic;
7841   void *p;
7842
7843   /* Check for the `__extension__' keyword.  */
7844   if (cp_parser_extension_opt (parser, &saved_pedantic))
7845     {
7846       /* Parse the qualified declaration.  */
7847       cp_parser_declaration (parser);
7848       /* Restore the PEDANTIC flag.  */
7849       pedantic = saved_pedantic;
7850
7851       return;
7852     }
7853
7854   /* Try to figure out what kind of declaration is present.  */
7855   token1 = *cp_lexer_peek_token (parser->lexer);
7856
7857   if (token1.type != CPP_EOF)
7858     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7859   else
7860     {
7861       token2.type = CPP_EOF;
7862       token2.keyword = RID_MAX;
7863     }
7864
7865   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7866   p = obstack_alloc (&declarator_obstack, 0);
7867
7868   /* If the next token is `extern' and the following token is a string
7869      literal, then we have a linkage specification.  */
7870   if (token1.keyword == RID_EXTERN
7871       && cp_parser_is_string_literal (&token2))
7872     cp_parser_linkage_specification (parser);
7873   /* If the next token is `template', then we have either a template
7874      declaration, an explicit instantiation, or an explicit
7875      specialization.  */
7876   else if (token1.keyword == RID_TEMPLATE)
7877     {
7878       /* `template <>' indicates a template specialization.  */
7879       if (token2.type == CPP_LESS
7880           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7881         cp_parser_explicit_specialization (parser);
7882       /* `template <' indicates a template declaration.  */
7883       else if (token2.type == CPP_LESS)
7884         cp_parser_template_declaration (parser, /*member_p=*/false);
7885       /* Anything else must be an explicit instantiation.  */
7886       else
7887         cp_parser_explicit_instantiation (parser);
7888     }
7889   /* If the next token is `export', then we have a template
7890      declaration.  */
7891   else if (token1.keyword == RID_EXPORT)
7892     cp_parser_template_declaration (parser, /*member_p=*/false);
7893   /* If the next token is `extern', 'static' or 'inline' and the one
7894      after that is `template', we have a GNU extended explicit
7895      instantiation directive.  */
7896   else if (cp_parser_allow_gnu_extensions_p (parser)
7897            && (token1.keyword == RID_EXTERN
7898                || token1.keyword == RID_STATIC
7899                || token1.keyword == RID_INLINE)
7900            && token2.keyword == RID_TEMPLATE)
7901     cp_parser_explicit_instantiation (parser);
7902   /* If the next token is `namespace', check for a named or unnamed
7903      namespace definition.  */
7904   else if (token1.keyword == RID_NAMESPACE
7905            && (/* A named namespace definition.  */
7906                (token2.type == CPP_NAME
7907                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7908                     != CPP_EQ))
7909                /* An unnamed namespace definition.  */
7910                || token2.type == CPP_OPEN_BRACE
7911                || token2.keyword == RID_ATTRIBUTE))
7912     cp_parser_namespace_definition (parser);
7913   /* An inline (associated) namespace definition.  */
7914   else if (token1.keyword == RID_INLINE
7915            && token2.keyword == RID_NAMESPACE)
7916     cp_parser_namespace_definition (parser);
7917   /* Objective-C++ declaration/definition.  */
7918   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7919     cp_parser_objc_declaration (parser);
7920   /* We must have either a block declaration or a function
7921      definition.  */
7922   else
7923     /* Try to parse a block-declaration, or a function-definition.  */
7924     cp_parser_block_declaration (parser, /*statement_p=*/false);
7925
7926   /* Free any declarators allocated.  */
7927   obstack_free (&declarator_obstack, p);
7928 }
7929
7930 /* Parse a block-declaration.
7931
7932    block-declaration:
7933      simple-declaration
7934      asm-definition
7935      namespace-alias-definition
7936      using-declaration
7937      using-directive
7938
7939    GNU Extension:
7940
7941    block-declaration:
7942      __extension__ block-declaration
7943
7944    C++0x Extension:
7945
7946    block-declaration:
7947      static_assert-declaration
7948
7949    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7950    part of a declaration-statement.  */
7951
7952 static void
7953 cp_parser_block_declaration (cp_parser *parser,
7954                              bool      statement_p)
7955 {
7956   cp_token *token1;
7957   int saved_pedantic;
7958
7959   /* Check for the `__extension__' keyword.  */
7960   if (cp_parser_extension_opt (parser, &saved_pedantic))
7961     {
7962       /* Parse the qualified declaration.  */
7963       cp_parser_block_declaration (parser, statement_p);
7964       /* Restore the PEDANTIC flag.  */
7965       pedantic = saved_pedantic;
7966
7967       return;
7968     }
7969
7970   /* Peek at the next token to figure out which kind of declaration is
7971      present.  */
7972   token1 = cp_lexer_peek_token (parser->lexer);
7973
7974   /* If the next keyword is `asm', we have an asm-definition.  */
7975   if (token1->keyword == RID_ASM)
7976     {
7977       if (statement_p)
7978         cp_parser_commit_to_tentative_parse (parser);
7979       cp_parser_asm_definition (parser);
7980     }
7981   /* If the next keyword is `namespace', we have a
7982      namespace-alias-definition.  */
7983   else if (token1->keyword == RID_NAMESPACE)
7984     cp_parser_namespace_alias_definition (parser);
7985   /* If the next keyword is `using', we have either a
7986      using-declaration or a using-directive.  */
7987   else if (token1->keyword == RID_USING)
7988     {
7989       cp_token *token2;
7990
7991       if (statement_p)
7992         cp_parser_commit_to_tentative_parse (parser);
7993       /* If the token after `using' is `namespace', then we have a
7994          using-directive.  */
7995       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7996       if (token2->keyword == RID_NAMESPACE)
7997         cp_parser_using_directive (parser);
7998       /* Otherwise, it's a using-declaration.  */
7999       else
8000         cp_parser_using_declaration (parser,
8001                                      /*access_declaration_p=*/false);
8002     }
8003   /* If the next keyword is `__label__' we have a misplaced label
8004      declaration.  */
8005   else if (token1->keyword == RID_LABEL)
8006     {
8007       cp_lexer_consume_token (parser->lexer);
8008       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8009       cp_parser_skip_to_end_of_statement (parser);
8010       /* If the next token is now a `;', consume it.  */
8011       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8012         cp_lexer_consume_token (parser->lexer);
8013     }
8014   /* If the next token is `static_assert' we have a static assertion.  */
8015   else if (token1->keyword == RID_STATIC_ASSERT)
8016     cp_parser_static_assert (parser, /*member_p=*/false);
8017   /* Anything else must be a simple-declaration.  */
8018   else
8019     cp_parser_simple_declaration (parser, !statement_p);
8020 }
8021
8022 /* Parse a simple-declaration.
8023
8024    simple-declaration:
8025      decl-specifier-seq [opt] init-declarator-list [opt] ;
8026
8027    init-declarator-list:
8028      init-declarator
8029      init-declarator-list , init-declarator
8030
8031    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8032    function-definition as a simple-declaration.  */
8033
8034 static void
8035 cp_parser_simple_declaration (cp_parser* parser,
8036                               bool function_definition_allowed_p)
8037 {
8038   cp_decl_specifier_seq decl_specifiers;
8039   int declares_class_or_enum;
8040   bool saw_declarator;
8041
8042   /* Defer access checks until we know what is being declared; the
8043      checks for names appearing in the decl-specifier-seq should be
8044      done as if we were in the scope of the thing being declared.  */
8045   push_deferring_access_checks (dk_deferred);
8046
8047   /* Parse the decl-specifier-seq.  We have to keep track of whether
8048      or not the decl-specifier-seq declares a named class or
8049      enumeration type, since that is the only case in which the
8050      init-declarator-list is allowed to be empty.
8051
8052      [dcl.dcl]
8053
8054      In a simple-declaration, the optional init-declarator-list can be
8055      omitted only when declaring a class or enumeration, that is when
8056      the decl-specifier-seq contains either a class-specifier, an
8057      elaborated-type-specifier, or an enum-specifier.  */
8058   cp_parser_decl_specifier_seq (parser,
8059                                 CP_PARSER_FLAGS_OPTIONAL,
8060                                 &decl_specifiers,
8061                                 &declares_class_or_enum);
8062   /* We no longer need to defer access checks.  */
8063   stop_deferring_access_checks ();
8064
8065   /* In a block scope, a valid declaration must always have a
8066      decl-specifier-seq.  By not trying to parse declarators, we can
8067      resolve the declaration/expression ambiguity more quickly.  */
8068   if (!function_definition_allowed_p
8069       && !decl_specifiers.any_specifiers_p)
8070     {
8071       cp_parser_error (parser, "expected declaration");
8072       goto done;
8073     }
8074
8075   /* If the next two tokens are both identifiers, the code is
8076      erroneous. The usual cause of this situation is code like:
8077
8078        T t;
8079
8080      where "T" should name a type -- but does not.  */
8081   if (!decl_specifiers.type
8082       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8083     {
8084       /* If parsing tentatively, we should commit; we really are
8085          looking at a declaration.  */
8086       cp_parser_commit_to_tentative_parse (parser);
8087       /* Give up.  */
8088       goto done;
8089     }
8090
8091   /* If we have seen at least one decl-specifier, and the next token
8092      is not a parenthesis, then we must be looking at a declaration.
8093      (After "int (" we might be looking at a functional cast.)  */
8094   if (decl_specifiers.any_specifiers_p
8095       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8096       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8097     cp_parser_commit_to_tentative_parse (parser);
8098
8099   /* Keep going until we hit the `;' at the end of the simple
8100      declaration.  */
8101   saw_declarator = false;
8102   while (cp_lexer_next_token_is_not (parser->lexer,
8103                                      CPP_SEMICOLON))
8104     {
8105       cp_token *token;
8106       bool function_definition_p;
8107       tree decl;
8108
8109       if (saw_declarator)
8110         {
8111           /* If we are processing next declarator, coma is expected */
8112           token = cp_lexer_peek_token (parser->lexer);
8113           gcc_assert (token->type == CPP_COMMA);
8114           cp_lexer_consume_token (parser->lexer);
8115         }
8116       else
8117         saw_declarator = true;
8118
8119       /* Parse the init-declarator.  */
8120       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8121                                         /*checks=*/NULL,
8122                                         function_definition_allowed_p,
8123                                         /*member_p=*/false,
8124                                         declares_class_or_enum,
8125                                         &function_definition_p);
8126       /* If an error occurred while parsing tentatively, exit quickly.
8127          (That usually happens when in the body of a function; each
8128          statement is treated as a declaration-statement until proven
8129          otherwise.)  */
8130       if (cp_parser_error_occurred (parser))
8131         goto done;
8132       /* Handle function definitions specially.  */
8133       if (function_definition_p)
8134         {
8135           /* If the next token is a `,', then we are probably
8136              processing something like:
8137
8138                void f() {}, *p;
8139
8140              which is erroneous.  */
8141           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8142             {
8143               cp_token *token = cp_lexer_peek_token (parser->lexer);
8144               error ("%Hmixing declarations and function-definitions is forbidden",
8145                      &token->location);
8146             }
8147           /* Otherwise, we're done with the list of declarators.  */
8148           else
8149             {
8150               pop_deferring_access_checks ();
8151               return;
8152             }
8153         }
8154       /* The next token should be either a `,' or a `;'.  */
8155       token = cp_lexer_peek_token (parser->lexer);
8156       /* If it's a `,', there are more declarators to come.  */
8157       if (token->type == CPP_COMMA)
8158         /* will be consumed next time around */;
8159       /* If it's a `;', we are done.  */
8160       else if (token->type == CPP_SEMICOLON)
8161         break;
8162       /* Anything else is an error.  */
8163       else
8164         {
8165           /* If we have already issued an error message we don't need
8166              to issue another one.  */
8167           if (decl != error_mark_node
8168               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8169             cp_parser_error (parser, "expected %<,%> or %<;%>");
8170           /* Skip tokens until we reach the end of the statement.  */
8171           cp_parser_skip_to_end_of_statement (parser);
8172           /* If the next token is now a `;', consume it.  */
8173           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8174             cp_lexer_consume_token (parser->lexer);
8175           goto done;
8176         }
8177       /* After the first time around, a function-definition is not
8178          allowed -- even if it was OK at first.  For example:
8179
8180            int i, f() {}
8181
8182          is not valid.  */
8183       function_definition_allowed_p = false;
8184     }
8185
8186   /* Issue an error message if no declarators are present, and the
8187      decl-specifier-seq does not itself declare a class or
8188      enumeration.  */
8189   if (!saw_declarator)
8190     {
8191       if (cp_parser_declares_only_class_p (parser))
8192         shadow_tag (&decl_specifiers);
8193       /* Perform any deferred access checks.  */
8194       perform_deferred_access_checks ();
8195     }
8196
8197   /* Consume the `;'.  */
8198   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8199
8200  done:
8201   pop_deferring_access_checks ();
8202 }
8203
8204 /* Parse a decl-specifier-seq.
8205
8206    decl-specifier-seq:
8207      decl-specifier-seq [opt] decl-specifier
8208
8209    decl-specifier:
8210      storage-class-specifier
8211      type-specifier
8212      function-specifier
8213      friend
8214      typedef
8215
8216    GNU Extension:
8217
8218    decl-specifier:
8219      attributes
8220
8221    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8222
8223    The parser flags FLAGS is used to control type-specifier parsing.
8224
8225    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8226    flags:
8227
8228      1: one of the decl-specifiers is an elaborated-type-specifier
8229         (i.e., a type declaration)
8230      2: one of the decl-specifiers is an enum-specifier or a
8231         class-specifier (i.e., a type definition)
8232
8233    */
8234
8235 static void
8236 cp_parser_decl_specifier_seq (cp_parser* parser,
8237                               cp_parser_flags flags,
8238                               cp_decl_specifier_seq *decl_specs,
8239                               int* declares_class_or_enum)
8240 {
8241   bool constructor_possible_p = !parser->in_declarator_p;
8242   cp_token *start_token = NULL;
8243
8244   /* Clear DECL_SPECS.  */
8245   clear_decl_specs (decl_specs);
8246
8247   /* Assume no class or enumeration type is declared.  */
8248   *declares_class_or_enum = 0;
8249
8250   /* Keep reading specifiers until there are no more to read.  */
8251   while (true)
8252     {
8253       bool constructor_p;
8254       bool found_decl_spec;
8255       cp_token *token;
8256
8257       /* Peek at the next token.  */
8258       token = cp_lexer_peek_token (parser->lexer);
8259
8260       /* Save the first token of the decl spec list for error
8261          reporting.  */
8262       if (!start_token)
8263         start_token = token;
8264       /* Handle attributes.  */
8265       if (token->keyword == RID_ATTRIBUTE)
8266         {
8267           /* Parse the attributes.  */
8268           decl_specs->attributes
8269             = chainon (decl_specs->attributes,
8270                        cp_parser_attributes_opt (parser));
8271           continue;
8272         }
8273       /* Assume we will find a decl-specifier keyword.  */
8274       found_decl_spec = true;
8275       /* If the next token is an appropriate keyword, we can simply
8276          add it to the list.  */
8277       switch (token->keyword)
8278         {
8279           /* decl-specifier:
8280                friend  */
8281         case RID_FRIEND:
8282           if (!at_class_scope_p ())
8283             {
8284               error ("%H%<friend%> used outside of class", &token->location);
8285               cp_lexer_purge_token (parser->lexer);
8286             }
8287           else
8288             {
8289               ++decl_specs->specs[(int) ds_friend];
8290               /* Consume the token.  */
8291               cp_lexer_consume_token (parser->lexer);
8292             }
8293           break;
8294
8295           /* function-specifier:
8296                inline
8297                virtual
8298                explicit  */
8299         case RID_INLINE:
8300         case RID_VIRTUAL:
8301         case RID_EXPLICIT:
8302           cp_parser_function_specifier_opt (parser, decl_specs);
8303           break;
8304
8305           /* decl-specifier:
8306                typedef  */
8307         case RID_TYPEDEF:
8308           ++decl_specs->specs[(int) ds_typedef];
8309           /* Consume the token.  */
8310           cp_lexer_consume_token (parser->lexer);
8311           /* A constructor declarator cannot appear in a typedef.  */
8312           constructor_possible_p = false;
8313           /* The "typedef" keyword can only occur in a declaration; we
8314              may as well commit at this point.  */
8315           cp_parser_commit_to_tentative_parse (parser);
8316
8317           if (decl_specs->storage_class != sc_none)
8318             decl_specs->conflicting_specifiers_p = true;
8319           break;
8320
8321           /* storage-class-specifier:
8322                auto
8323                register
8324                static
8325                extern
8326                mutable
8327
8328              GNU Extension:
8329                thread  */
8330         case RID_AUTO:
8331           if (cxx_dialect == cxx98) 
8332             {
8333               /* Consume the token.  */
8334               cp_lexer_consume_token (parser->lexer);
8335
8336               /* Complain about `auto' as a storage specifier, if
8337                  we're complaining about C++0x compatibility.  */
8338               warning 
8339                 (OPT_Wc__0x_compat, 
8340                  "%H%<auto%> will change meaning in C++0x; please remove it",
8341                  &token->location);
8342
8343               /* Set the storage class anyway.  */
8344               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8345                                            token->location);
8346             }
8347           else
8348             /* C++0x auto type-specifier.  */
8349             found_decl_spec = false;
8350           break;
8351
8352         case RID_REGISTER:
8353         case RID_STATIC:
8354         case RID_EXTERN:
8355         case RID_MUTABLE:
8356           /* Consume the token.  */
8357           cp_lexer_consume_token (parser->lexer);
8358           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8359                                        token->location);
8360           break;
8361         case RID_THREAD:
8362           /* Consume the token.  */
8363           cp_lexer_consume_token (parser->lexer);
8364           ++decl_specs->specs[(int) ds_thread];
8365           break;
8366
8367         default:
8368           /* We did not yet find a decl-specifier yet.  */
8369           found_decl_spec = false;
8370           break;
8371         }
8372
8373       /* Constructors are a special case.  The `S' in `S()' is not a
8374          decl-specifier; it is the beginning of the declarator.  */
8375       constructor_p
8376         = (!found_decl_spec
8377            && constructor_possible_p
8378            && (cp_parser_constructor_declarator_p
8379                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8380
8381       /* If we don't have a DECL_SPEC yet, then we must be looking at
8382          a type-specifier.  */
8383       if (!found_decl_spec && !constructor_p)
8384         {
8385           int decl_spec_declares_class_or_enum;
8386           bool is_cv_qualifier;
8387           tree type_spec;
8388
8389           type_spec
8390             = cp_parser_type_specifier (parser, flags,
8391                                         decl_specs,
8392                                         /*is_declaration=*/true,
8393                                         &decl_spec_declares_class_or_enum,
8394                                         &is_cv_qualifier);
8395           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8396
8397           /* If this type-specifier referenced a user-defined type
8398              (a typedef, class-name, etc.), then we can't allow any
8399              more such type-specifiers henceforth.
8400
8401              [dcl.spec]
8402
8403              The longest sequence of decl-specifiers that could
8404              possibly be a type name is taken as the
8405              decl-specifier-seq of a declaration.  The sequence shall
8406              be self-consistent as described below.
8407
8408              [dcl.type]
8409
8410              As a general rule, at most one type-specifier is allowed
8411              in the complete decl-specifier-seq of a declaration.  The
8412              only exceptions are the following:
8413
8414              -- const or volatile can be combined with any other
8415                 type-specifier.
8416
8417              -- signed or unsigned can be combined with char, long,
8418                 short, or int.
8419
8420              -- ..
8421
8422              Example:
8423
8424                typedef char* Pc;
8425                void g (const int Pc);
8426
8427              Here, Pc is *not* part of the decl-specifier seq; it's
8428              the declarator.  Therefore, once we see a type-specifier
8429              (other than a cv-qualifier), we forbid any additional
8430              user-defined types.  We *do* still allow things like `int
8431              int' to be considered a decl-specifier-seq, and issue the
8432              error message later.  */
8433           if (type_spec && !is_cv_qualifier)
8434             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8435           /* A constructor declarator cannot follow a type-specifier.  */
8436           if (type_spec)
8437             {
8438               constructor_possible_p = false;
8439               found_decl_spec = true;
8440             }
8441         }
8442
8443       /* If we still do not have a DECL_SPEC, then there are no more
8444          decl-specifiers.  */
8445       if (!found_decl_spec)
8446         break;
8447
8448       decl_specs->any_specifiers_p = true;
8449       /* After we see one decl-specifier, further decl-specifiers are
8450          always optional.  */
8451       flags |= CP_PARSER_FLAGS_OPTIONAL;
8452     }
8453
8454   cp_parser_check_decl_spec (decl_specs, start_token->location);
8455
8456   /* Don't allow a friend specifier with a class definition.  */
8457   if (decl_specs->specs[(int) ds_friend] != 0
8458       && (*declares_class_or_enum & 2))
8459     error ("%Hclass definition may not be declared a friend",
8460             &start_token->location);
8461 }
8462
8463 /* Parse an (optional) storage-class-specifier.
8464
8465    storage-class-specifier:
8466      auto
8467      register
8468      static
8469      extern
8470      mutable
8471
8472    GNU Extension:
8473
8474    storage-class-specifier:
8475      thread
8476
8477    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8478
8479 static tree
8480 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8481 {
8482   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8483     {
8484     case RID_AUTO:
8485       if (cxx_dialect != cxx98)
8486         return NULL_TREE;
8487       /* Fall through for C++98.  */
8488
8489     case RID_REGISTER:
8490     case RID_STATIC:
8491     case RID_EXTERN:
8492     case RID_MUTABLE:
8493     case RID_THREAD:
8494       /* Consume the token.  */
8495       return cp_lexer_consume_token (parser->lexer)->u.value;
8496
8497     default:
8498       return NULL_TREE;
8499     }
8500 }
8501
8502 /* Parse an (optional) function-specifier.
8503
8504    function-specifier:
8505      inline
8506      virtual
8507      explicit
8508
8509    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8510    Updates DECL_SPECS, if it is non-NULL.  */
8511
8512 static tree
8513 cp_parser_function_specifier_opt (cp_parser* parser,
8514                                   cp_decl_specifier_seq *decl_specs)
8515 {
8516   cp_token *token = cp_lexer_peek_token (parser->lexer);
8517   switch (token->keyword)
8518     {
8519     case RID_INLINE:
8520       if (decl_specs)
8521         ++decl_specs->specs[(int) ds_inline];
8522       break;
8523
8524     case RID_VIRTUAL:
8525       /* 14.5.2.3 [temp.mem]
8526
8527          A member function template shall not be virtual.  */
8528       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8529         error ("%Htemplates may not be %<virtual%>", &token->location);
8530       else if (decl_specs)
8531         ++decl_specs->specs[(int) ds_virtual];
8532       break;
8533
8534     case RID_EXPLICIT:
8535       if (decl_specs)
8536         ++decl_specs->specs[(int) ds_explicit];
8537       break;
8538
8539     default:
8540       return NULL_TREE;
8541     }
8542
8543   /* Consume the token.  */
8544   return cp_lexer_consume_token (parser->lexer)->u.value;
8545 }
8546
8547 /* Parse a linkage-specification.
8548
8549    linkage-specification:
8550      extern string-literal { declaration-seq [opt] }
8551      extern string-literal declaration  */
8552
8553 static void
8554 cp_parser_linkage_specification (cp_parser* parser)
8555 {
8556   tree linkage;
8557
8558   /* Look for the `extern' keyword.  */
8559   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8560
8561   /* Look for the string-literal.  */
8562   linkage = cp_parser_string_literal (parser, false, false);
8563
8564   /* Transform the literal into an identifier.  If the literal is a
8565      wide-character string, or contains embedded NULs, then we can't
8566      handle it as the user wants.  */
8567   if (strlen (TREE_STRING_POINTER (linkage))
8568       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8569     {
8570       cp_parser_error (parser, "invalid linkage-specification");
8571       /* Assume C++ linkage.  */
8572       linkage = lang_name_cplusplus;
8573     }
8574   else
8575     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8576
8577   /* We're now using the new linkage.  */
8578   push_lang_context (linkage);
8579
8580   /* If the next token is a `{', then we're using the first
8581      production.  */
8582   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8583     {
8584       /* Consume the `{' token.  */
8585       cp_lexer_consume_token (parser->lexer);
8586       /* Parse the declarations.  */
8587       cp_parser_declaration_seq_opt (parser);
8588       /* Look for the closing `}'.  */
8589       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8590     }
8591   /* Otherwise, there's just one declaration.  */
8592   else
8593     {
8594       bool saved_in_unbraced_linkage_specification_p;
8595
8596       saved_in_unbraced_linkage_specification_p
8597         = parser->in_unbraced_linkage_specification_p;
8598       parser->in_unbraced_linkage_specification_p = true;
8599       cp_parser_declaration (parser);
8600       parser->in_unbraced_linkage_specification_p
8601         = saved_in_unbraced_linkage_specification_p;
8602     }
8603
8604   /* We're done with the linkage-specification.  */
8605   pop_lang_context ();
8606 }
8607
8608 /* Parse a static_assert-declaration.
8609
8610    static_assert-declaration:
8611      static_assert ( constant-expression , string-literal ) ; 
8612
8613    If MEMBER_P, this static_assert is a class member.  */
8614
8615 static void 
8616 cp_parser_static_assert(cp_parser *parser, bool member_p)
8617 {
8618   tree condition;
8619   tree message;
8620   cp_token *token;
8621   location_t saved_loc;
8622
8623   /* Peek at the `static_assert' token so we can keep track of exactly
8624      where the static assertion started.  */
8625   token = cp_lexer_peek_token (parser->lexer);
8626   saved_loc = token->location;
8627
8628   /* Look for the `static_assert' keyword.  */
8629   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8630                                   "%<static_assert%>"))
8631     return;
8632
8633   /*  We know we are in a static assertion; commit to any tentative
8634       parse.  */
8635   if (cp_parser_parsing_tentatively (parser))
8636     cp_parser_commit_to_tentative_parse (parser);
8637
8638   /* Parse the `(' starting the static assertion condition.  */
8639   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8640
8641   /* Parse the constant-expression.  */
8642   condition = 
8643     cp_parser_constant_expression (parser,
8644                                    /*allow_non_constant_p=*/false,
8645                                    /*non_constant_p=*/NULL);
8646
8647   /* Parse the separating `,'.  */
8648   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8649
8650   /* Parse the string-literal message.  */
8651   message = cp_parser_string_literal (parser, 
8652                                       /*translate=*/false,
8653                                       /*wide_ok=*/true);
8654
8655   /* A `)' completes the static assertion.  */
8656   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8657     cp_parser_skip_to_closing_parenthesis (parser, 
8658                                            /*recovering=*/true, 
8659                                            /*or_comma=*/false,
8660                                            /*consume_paren=*/true);
8661
8662   /* A semicolon terminates the declaration.  */
8663   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8664
8665   /* Complete the static assertion, which may mean either processing 
8666      the static assert now or saving it for template instantiation.  */
8667   finish_static_assert (condition, message, saved_loc, member_p);
8668 }
8669
8670 /* Parse a `decltype' type. Returns the type. 
8671
8672    simple-type-specifier:
8673      decltype ( expression )  */
8674
8675 static tree
8676 cp_parser_decltype (cp_parser *parser)
8677 {
8678   tree expr;
8679   bool id_expression_or_member_access_p = false;
8680   const char *saved_message;
8681   bool saved_integral_constant_expression_p;
8682   bool saved_non_integral_constant_expression_p;
8683   cp_token *id_expr_start_token;
8684
8685   /* Look for the `decltype' token.  */
8686   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8687     return error_mark_node;
8688
8689   /* Types cannot be defined in a `decltype' expression.  Save away the
8690      old message.  */
8691   saved_message = parser->type_definition_forbidden_message;
8692
8693   /* And create the new one.  */
8694   parser->type_definition_forbidden_message
8695     = "types may not be defined in %<decltype%> expressions";
8696
8697   /* The restrictions on constant-expressions do not apply inside
8698      decltype expressions.  */
8699   saved_integral_constant_expression_p
8700     = parser->integral_constant_expression_p;
8701   saved_non_integral_constant_expression_p
8702     = parser->non_integral_constant_expression_p;
8703   parser->integral_constant_expression_p = false;
8704
8705   /* Do not actually evaluate the expression.  */
8706   ++skip_evaluation;
8707
8708   /* Parse the opening `('.  */
8709   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8710     return error_mark_node;
8711   
8712   /* First, try parsing an id-expression.  */
8713   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8714   cp_parser_parse_tentatively (parser);
8715   expr = cp_parser_id_expression (parser,
8716                                   /*template_keyword_p=*/false,
8717                                   /*check_dependency_p=*/true,
8718                                   /*template_p=*/NULL,
8719                                   /*declarator_p=*/false,
8720                                   /*optional_p=*/false);
8721
8722   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8723     {
8724       bool non_integral_constant_expression_p = false;
8725       tree id_expression = expr;
8726       cp_id_kind idk;
8727       const char *error_msg;
8728
8729       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8730         /* Lookup the name we got back from the id-expression.  */
8731         expr = cp_parser_lookup_name (parser, expr,
8732                                       none_type,
8733                                       /*is_template=*/false,
8734                                       /*is_namespace=*/false,
8735                                       /*check_dependency=*/true,
8736                                       /*ambiguous_decls=*/NULL,
8737                                       id_expr_start_token->location);
8738
8739       if (expr
8740           && expr != error_mark_node
8741           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8742           && TREE_CODE (expr) != TYPE_DECL
8743           && (TREE_CODE (expr) != BIT_NOT_EXPR
8744               || !TYPE_P (TREE_OPERAND (expr, 0)))
8745           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8746         {
8747           /* Complete lookup of the id-expression.  */
8748           expr = (finish_id_expression
8749                   (id_expression, expr, parser->scope, &idk,
8750                    /*integral_constant_expression_p=*/false,
8751                    /*allow_non_integral_constant_expression_p=*/true,
8752                    &non_integral_constant_expression_p,
8753                    /*template_p=*/false,
8754                    /*done=*/true,
8755                    /*address_p=*/false,
8756                    /*template_arg_p=*/false,
8757                    &error_msg,
8758                    id_expr_start_token->location));
8759
8760           if (expr == error_mark_node)
8761             /* We found an id-expression, but it was something that we
8762                should not have found. This is an error, not something
8763                we can recover from, so note that we found an
8764                id-expression and we'll recover as gracefully as
8765                possible.  */
8766             id_expression_or_member_access_p = true;
8767         }
8768
8769       if (expr 
8770           && expr != error_mark_node
8771           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8772         /* We have an id-expression.  */
8773         id_expression_or_member_access_p = true;
8774     }
8775
8776   if (!id_expression_or_member_access_p)
8777     {
8778       /* Abort the id-expression parse.  */
8779       cp_parser_abort_tentative_parse (parser);
8780
8781       /* Parsing tentatively, again.  */
8782       cp_parser_parse_tentatively (parser);
8783
8784       /* Parse a class member access.  */
8785       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8786                                            /*cast_p=*/false,
8787                                            /*member_access_only_p=*/true);
8788
8789       if (expr 
8790           && expr != error_mark_node
8791           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8792         /* We have an id-expression.  */
8793         id_expression_or_member_access_p = true;
8794     }
8795
8796   if (id_expression_or_member_access_p)
8797     /* We have parsed the complete id-expression or member access.  */
8798     cp_parser_parse_definitely (parser);
8799   else
8800     {
8801       /* Abort our attempt to parse an id-expression or member access
8802          expression.  */
8803       cp_parser_abort_tentative_parse (parser);
8804
8805       /* Parse a full expression.  */
8806       expr = cp_parser_expression (parser, /*cast_p=*/false);
8807     }
8808
8809   /* Go back to evaluating expressions.  */
8810   --skip_evaluation;
8811
8812   /* Restore the old message and the integral constant expression
8813      flags.  */
8814   parser->type_definition_forbidden_message = saved_message;
8815   parser->integral_constant_expression_p
8816     = saved_integral_constant_expression_p;
8817   parser->non_integral_constant_expression_p
8818     = saved_non_integral_constant_expression_p;
8819
8820   if (expr == error_mark_node)
8821     {
8822       /* Skip everything up to the closing `)'.  */
8823       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8824                                              /*consume_paren=*/true);
8825       return error_mark_node;
8826     }
8827   
8828   /* Parse to the closing `)'.  */
8829   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8830     {
8831       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8832                                              /*consume_paren=*/true);
8833       return error_mark_node;
8834     }
8835
8836   return finish_decltype_type (expr, id_expression_or_member_access_p);
8837 }
8838
8839 /* Special member functions [gram.special] */
8840
8841 /* Parse a conversion-function-id.
8842
8843    conversion-function-id:
8844      operator conversion-type-id
8845
8846    Returns an IDENTIFIER_NODE representing the operator.  */
8847
8848 static tree
8849 cp_parser_conversion_function_id (cp_parser* parser)
8850 {
8851   tree type;
8852   tree saved_scope;
8853   tree saved_qualifying_scope;
8854   tree saved_object_scope;
8855   tree pushed_scope = NULL_TREE;
8856
8857   /* Look for the `operator' token.  */
8858   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8859     return error_mark_node;
8860   /* When we parse the conversion-type-id, the current scope will be
8861      reset.  However, we need that information in able to look up the
8862      conversion function later, so we save it here.  */
8863   saved_scope = parser->scope;
8864   saved_qualifying_scope = parser->qualifying_scope;
8865   saved_object_scope = parser->object_scope;
8866   /* We must enter the scope of the class so that the names of
8867      entities declared within the class are available in the
8868      conversion-type-id.  For example, consider:
8869
8870        struct S {
8871          typedef int I;
8872          operator I();
8873        };
8874
8875        S::operator I() { ... }
8876
8877      In order to see that `I' is a type-name in the definition, we
8878      must be in the scope of `S'.  */
8879   if (saved_scope)
8880     pushed_scope = push_scope (saved_scope);
8881   /* Parse the conversion-type-id.  */
8882   type = cp_parser_conversion_type_id (parser);
8883   /* Leave the scope of the class, if any.  */
8884   if (pushed_scope)
8885     pop_scope (pushed_scope);
8886   /* Restore the saved scope.  */
8887   parser->scope = saved_scope;
8888   parser->qualifying_scope = saved_qualifying_scope;
8889   parser->object_scope = saved_object_scope;
8890   /* If the TYPE is invalid, indicate failure.  */
8891   if (type == error_mark_node)
8892     return error_mark_node;
8893   return mangle_conv_op_name_for_type (type);
8894 }
8895
8896 /* Parse a conversion-type-id:
8897
8898    conversion-type-id:
8899      type-specifier-seq conversion-declarator [opt]
8900
8901    Returns the TYPE specified.  */
8902
8903 static tree
8904 cp_parser_conversion_type_id (cp_parser* parser)
8905 {
8906   tree attributes;
8907   cp_decl_specifier_seq type_specifiers;
8908   cp_declarator *declarator;
8909   tree type_specified;
8910
8911   /* Parse the attributes.  */
8912   attributes = cp_parser_attributes_opt (parser);
8913   /* Parse the type-specifiers.  */
8914   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8915                                 &type_specifiers);
8916   /* If that didn't work, stop.  */
8917   if (type_specifiers.type == error_mark_node)
8918     return error_mark_node;
8919   /* Parse the conversion-declarator.  */
8920   declarator = cp_parser_conversion_declarator_opt (parser);
8921
8922   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8923                                     /*initialized=*/0, &attributes);
8924   if (attributes)
8925     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8926   return type_specified;
8927 }
8928
8929 /* Parse an (optional) conversion-declarator.
8930
8931    conversion-declarator:
8932      ptr-operator conversion-declarator [opt]
8933
8934    */
8935
8936 static cp_declarator *
8937 cp_parser_conversion_declarator_opt (cp_parser* parser)
8938 {
8939   enum tree_code code;
8940   tree class_type;
8941   cp_cv_quals cv_quals;
8942
8943   /* We don't know if there's a ptr-operator next, or not.  */
8944   cp_parser_parse_tentatively (parser);
8945   /* Try the ptr-operator.  */
8946   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8947   /* If it worked, look for more conversion-declarators.  */
8948   if (cp_parser_parse_definitely (parser))
8949     {
8950       cp_declarator *declarator;
8951
8952       /* Parse another optional declarator.  */
8953       declarator = cp_parser_conversion_declarator_opt (parser);
8954
8955       return cp_parser_make_indirect_declarator
8956         (code, class_type, cv_quals, declarator);
8957    }
8958
8959   return NULL;
8960 }
8961
8962 /* Parse an (optional) ctor-initializer.
8963
8964    ctor-initializer:
8965      : mem-initializer-list
8966
8967    Returns TRUE iff the ctor-initializer was actually present.  */
8968
8969 static bool
8970 cp_parser_ctor_initializer_opt (cp_parser* parser)
8971 {
8972   /* If the next token is not a `:', then there is no
8973      ctor-initializer.  */
8974   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8975     {
8976       /* Do default initialization of any bases and members.  */
8977       if (DECL_CONSTRUCTOR_P (current_function_decl))
8978         finish_mem_initializers (NULL_TREE);
8979
8980       return false;
8981     }
8982
8983   /* Consume the `:' token.  */
8984   cp_lexer_consume_token (parser->lexer);
8985   /* And the mem-initializer-list.  */
8986   cp_parser_mem_initializer_list (parser);
8987
8988   return true;
8989 }
8990
8991 /* Parse a mem-initializer-list.
8992
8993    mem-initializer-list:
8994      mem-initializer ... [opt]
8995      mem-initializer ... [opt] , mem-initializer-list  */
8996
8997 static void
8998 cp_parser_mem_initializer_list (cp_parser* parser)
8999 {
9000   tree mem_initializer_list = NULL_TREE;
9001   cp_token *token = cp_lexer_peek_token (parser->lexer);
9002
9003   /* Let the semantic analysis code know that we are starting the
9004      mem-initializer-list.  */
9005   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9006     error ("%Honly constructors take base initializers",
9007            &token->location);
9008
9009   /* Loop through the list.  */
9010   while (true)
9011     {
9012       tree mem_initializer;
9013
9014       token = cp_lexer_peek_token (parser->lexer);
9015       /* Parse the mem-initializer.  */
9016       mem_initializer = cp_parser_mem_initializer (parser);
9017       /* If the next token is a `...', we're expanding member initializers. */
9018       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9019         {
9020           /* Consume the `...'. */
9021           cp_lexer_consume_token (parser->lexer);
9022
9023           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9024              can be expanded but members cannot. */
9025           if (mem_initializer != error_mark_node
9026               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9027             {
9028               error ("%Hcannot expand initializer for member %<%D%>",
9029                      &token->location, TREE_PURPOSE (mem_initializer));
9030               mem_initializer = error_mark_node;
9031             }
9032
9033           /* Construct the pack expansion type. */
9034           if (mem_initializer != error_mark_node)
9035             mem_initializer = make_pack_expansion (mem_initializer);
9036         }
9037       /* Add it to the list, unless it was erroneous.  */
9038       if (mem_initializer != error_mark_node)
9039         {
9040           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9041           mem_initializer_list = mem_initializer;
9042         }
9043       /* If the next token is not a `,', we're done.  */
9044       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9045         break;
9046       /* Consume the `,' token.  */
9047       cp_lexer_consume_token (parser->lexer);
9048     }
9049
9050   /* Perform semantic analysis.  */
9051   if (DECL_CONSTRUCTOR_P (current_function_decl))
9052     finish_mem_initializers (mem_initializer_list);
9053 }
9054
9055 /* Parse a mem-initializer.
9056
9057    mem-initializer:
9058      mem-initializer-id ( expression-list [opt] )
9059      mem-initializer-id braced-init-list
9060
9061    GNU extension:
9062
9063    mem-initializer:
9064      ( expression-list [opt] )
9065
9066    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9067    class) or FIELD_DECL (for a non-static data member) to initialize;
9068    the TREE_VALUE is the expression-list.  An empty initialization
9069    list is represented by void_list_node.  */
9070
9071 static tree
9072 cp_parser_mem_initializer (cp_parser* parser)
9073 {
9074   tree mem_initializer_id;
9075   tree expression_list;
9076   tree member;
9077   cp_token *token = cp_lexer_peek_token (parser->lexer);
9078
9079   /* Find out what is being initialized.  */
9080   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9081     {
9082       permerror (token->location,
9083                  "anachronistic old-style base class initializer");
9084       mem_initializer_id = NULL_TREE;
9085     }
9086   else
9087     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9088   member = expand_member_init (mem_initializer_id);
9089   if (member && !DECL_P (member))
9090     in_base_initializer = 1;
9091
9092   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9093     {
9094       bool expr_non_constant_p;
9095       maybe_warn_cpp0x ("extended initializer lists");
9096       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9097       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9098       expression_list = build_tree_list (NULL_TREE, expression_list);
9099     }
9100   else
9101     expression_list
9102       = cp_parser_parenthesized_expression_list (parser, false,
9103                                                  /*cast_p=*/false,
9104                                                  /*allow_expansion_p=*/true,
9105                                                  /*non_constant_p=*/NULL);
9106   if (expression_list == error_mark_node)
9107     return error_mark_node;
9108   if (!expression_list)
9109     expression_list = void_type_node;
9110
9111   in_base_initializer = 0;
9112
9113   return member ? build_tree_list (member, expression_list) : error_mark_node;
9114 }
9115
9116 /* Parse a mem-initializer-id.
9117
9118    mem-initializer-id:
9119      :: [opt] nested-name-specifier [opt] class-name
9120      identifier
9121
9122    Returns a TYPE indicating the class to be initializer for the first
9123    production.  Returns an IDENTIFIER_NODE indicating the data member
9124    to be initialized for the second production.  */
9125
9126 static tree
9127 cp_parser_mem_initializer_id (cp_parser* parser)
9128 {
9129   bool global_scope_p;
9130   bool nested_name_specifier_p;
9131   bool template_p = false;
9132   tree id;
9133
9134   cp_token *token = cp_lexer_peek_token (parser->lexer);
9135
9136   /* `typename' is not allowed in this context ([temp.res]).  */
9137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9138     {
9139       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9140              "member initializer is implicitly a type)",
9141              &token->location);
9142       cp_lexer_consume_token (parser->lexer);
9143     }
9144   /* Look for the optional `::' operator.  */
9145   global_scope_p
9146     = (cp_parser_global_scope_opt (parser,
9147                                    /*current_scope_valid_p=*/false)
9148        != NULL_TREE);
9149   /* Look for the optional nested-name-specifier.  The simplest way to
9150      implement:
9151
9152        [temp.res]
9153
9154        The keyword `typename' is not permitted in a base-specifier or
9155        mem-initializer; in these contexts a qualified name that
9156        depends on a template-parameter is implicitly assumed to be a
9157        type name.
9158
9159      is to assume that we have seen the `typename' keyword at this
9160      point.  */
9161   nested_name_specifier_p
9162     = (cp_parser_nested_name_specifier_opt (parser,
9163                                             /*typename_keyword_p=*/true,
9164                                             /*check_dependency_p=*/true,
9165                                             /*type_p=*/true,
9166                                             /*is_declaration=*/true)
9167        != NULL_TREE);
9168   if (nested_name_specifier_p)
9169     template_p = cp_parser_optional_template_keyword (parser);
9170   /* If there is a `::' operator or a nested-name-specifier, then we
9171      are definitely looking for a class-name.  */
9172   if (global_scope_p || nested_name_specifier_p)
9173     return cp_parser_class_name (parser,
9174                                  /*typename_keyword_p=*/true,
9175                                  /*template_keyword_p=*/template_p,
9176                                  none_type,
9177                                  /*check_dependency_p=*/true,
9178                                  /*class_head_p=*/false,
9179                                  /*is_declaration=*/true);
9180   /* Otherwise, we could also be looking for an ordinary identifier.  */
9181   cp_parser_parse_tentatively (parser);
9182   /* Try a class-name.  */
9183   id = cp_parser_class_name (parser,
9184                              /*typename_keyword_p=*/true,
9185                              /*template_keyword_p=*/false,
9186                              none_type,
9187                              /*check_dependency_p=*/true,
9188                              /*class_head_p=*/false,
9189                              /*is_declaration=*/true);
9190   /* If we found one, we're done.  */
9191   if (cp_parser_parse_definitely (parser))
9192     return id;
9193   /* Otherwise, look for an ordinary identifier.  */
9194   return cp_parser_identifier (parser);
9195 }
9196
9197 /* Overloading [gram.over] */
9198
9199 /* Parse an operator-function-id.
9200
9201    operator-function-id:
9202      operator operator
9203
9204    Returns an IDENTIFIER_NODE for the operator which is a
9205    human-readable spelling of the identifier, e.g., `operator +'.  */
9206
9207 static tree
9208 cp_parser_operator_function_id (cp_parser* parser)
9209 {
9210   /* Look for the `operator' keyword.  */
9211   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9212     return error_mark_node;
9213   /* And then the name of the operator itself.  */
9214   return cp_parser_operator (parser);
9215 }
9216
9217 /* Parse an operator.
9218
9219    operator:
9220      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9221      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9222      || ++ -- , ->* -> () []
9223
9224    GNU Extensions:
9225
9226    operator:
9227      <? >? <?= >?=
9228
9229    Returns an IDENTIFIER_NODE for the operator which is a
9230    human-readable spelling of the identifier, e.g., `operator +'.  */
9231
9232 static tree
9233 cp_parser_operator (cp_parser* parser)
9234 {
9235   tree id = NULL_TREE;
9236   cp_token *token;
9237
9238   /* Peek at the next token.  */
9239   token = cp_lexer_peek_token (parser->lexer);
9240   /* Figure out which operator we have.  */
9241   switch (token->type)
9242     {
9243     case CPP_KEYWORD:
9244       {
9245         enum tree_code op;
9246
9247         /* The keyword should be either `new' or `delete'.  */
9248         if (token->keyword == RID_NEW)
9249           op = NEW_EXPR;
9250         else if (token->keyword == RID_DELETE)
9251           op = DELETE_EXPR;
9252         else
9253           break;
9254
9255         /* Consume the `new' or `delete' token.  */
9256         cp_lexer_consume_token (parser->lexer);
9257
9258         /* Peek at the next token.  */
9259         token = cp_lexer_peek_token (parser->lexer);
9260         /* If it's a `[' token then this is the array variant of the
9261            operator.  */
9262         if (token->type == CPP_OPEN_SQUARE)
9263           {
9264             /* Consume the `[' token.  */
9265             cp_lexer_consume_token (parser->lexer);
9266             /* Look for the `]' token.  */
9267             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9268             id = ansi_opname (op == NEW_EXPR
9269                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9270           }
9271         /* Otherwise, we have the non-array variant.  */
9272         else
9273           id = ansi_opname (op);
9274
9275         return id;
9276       }
9277
9278     case CPP_PLUS:
9279       id = ansi_opname (PLUS_EXPR);
9280       break;
9281
9282     case CPP_MINUS:
9283       id = ansi_opname (MINUS_EXPR);
9284       break;
9285
9286     case CPP_MULT:
9287       id = ansi_opname (MULT_EXPR);
9288       break;
9289
9290     case CPP_DIV:
9291       id = ansi_opname (TRUNC_DIV_EXPR);
9292       break;
9293
9294     case CPP_MOD:
9295       id = ansi_opname (TRUNC_MOD_EXPR);
9296       break;
9297
9298     case CPP_XOR:
9299       id = ansi_opname (BIT_XOR_EXPR);
9300       break;
9301
9302     case CPP_AND:
9303       id = ansi_opname (BIT_AND_EXPR);
9304       break;
9305
9306     case CPP_OR:
9307       id = ansi_opname (BIT_IOR_EXPR);
9308       break;
9309
9310     case CPP_COMPL:
9311       id = ansi_opname (BIT_NOT_EXPR);
9312       break;
9313
9314     case CPP_NOT:
9315       id = ansi_opname (TRUTH_NOT_EXPR);
9316       break;
9317
9318     case CPP_EQ:
9319       id = ansi_assopname (NOP_EXPR);
9320       break;
9321
9322     case CPP_LESS:
9323       id = ansi_opname (LT_EXPR);
9324       break;
9325
9326     case CPP_GREATER:
9327       id = ansi_opname (GT_EXPR);
9328       break;
9329
9330     case CPP_PLUS_EQ:
9331       id = ansi_assopname (PLUS_EXPR);
9332       break;
9333
9334     case CPP_MINUS_EQ:
9335       id = ansi_assopname (MINUS_EXPR);
9336       break;
9337
9338     case CPP_MULT_EQ:
9339       id = ansi_assopname (MULT_EXPR);
9340       break;
9341
9342     case CPP_DIV_EQ:
9343       id = ansi_assopname (TRUNC_DIV_EXPR);
9344       break;
9345
9346     case CPP_MOD_EQ:
9347       id = ansi_assopname (TRUNC_MOD_EXPR);
9348       break;
9349
9350     case CPP_XOR_EQ:
9351       id = ansi_assopname (BIT_XOR_EXPR);
9352       break;
9353
9354     case CPP_AND_EQ:
9355       id = ansi_assopname (BIT_AND_EXPR);
9356       break;
9357
9358     case CPP_OR_EQ:
9359       id = ansi_assopname (BIT_IOR_EXPR);
9360       break;
9361
9362     case CPP_LSHIFT:
9363       id = ansi_opname (LSHIFT_EXPR);
9364       break;
9365
9366     case CPP_RSHIFT:
9367       id = ansi_opname (RSHIFT_EXPR);
9368       break;
9369
9370     case CPP_LSHIFT_EQ:
9371       id = ansi_assopname (LSHIFT_EXPR);
9372       break;
9373
9374     case CPP_RSHIFT_EQ:
9375       id = ansi_assopname (RSHIFT_EXPR);
9376       break;
9377
9378     case CPP_EQ_EQ:
9379       id = ansi_opname (EQ_EXPR);
9380       break;
9381
9382     case CPP_NOT_EQ:
9383       id = ansi_opname (NE_EXPR);
9384       break;
9385
9386     case CPP_LESS_EQ:
9387       id = ansi_opname (LE_EXPR);
9388       break;
9389
9390     case CPP_GREATER_EQ:
9391       id = ansi_opname (GE_EXPR);
9392       break;
9393
9394     case CPP_AND_AND:
9395       id = ansi_opname (TRUTH_ANDIF_EXPR);
9396       break;
9397
9398     case CPP_OR_OR:
9399       id = ansi_opname (TRUTH_ORIF_EXPR);
9400       break;
9401
9402     case CPP_PLUS_PLUS:
9403       id = ansi_opname (POSTINCREMENT_EXPR);
9404       break;
9405
9406     case CPP_MINUS_MINUS:
9407       id = ansi_opname (PREDECREMENT_EXPR);
9408       break;
9409
9410     case CPP_COMMA:
9411       id = ansi_opname (COMPOUND_EXPR);
9412       break;
9413
9414     case CPP_DEREF_STAR:
9415       id = ansi_opname (MEMBER_REF);
9416       break;
9417
9418     case CPP_DEREF:
9419       id = ansi_opname (COMPONENT_REF);
9420       break;
9421
9422     case CPP_OPEN_PAREN:
9423       /* Consume the `('.  */
9424       cp_lexer_consume_token (parser->lexer);
9425       /* Look for the matching `)'.  */
9426       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9427       return ansi_opname (CALL_EXPR);
9428
9429     case CPP_OPEN_SQUARE:
9430       /* Consume the `['.  */
9431       cp_lexer_consume_token (parser->lexer);
9432       /* Look for the matching `]'.  */
9433       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9434       return ansi_opname (ARRAY_REF);
9435
9436     default:
9437       /* Anything else is an error.  */
9438       break;
9439     }
9440
9441   /* If we have selected an identifier, we need to consume the
9442      operator token.  */
9443   if (id)
9444     cp_lexer_consume_token (parser->lexer);
9445   /* Otherwise, no valid operator name was present.  */
9446   else
9447     {
9448       cp_parser_error (parser, "expected operator");
9449       id = error_mark_node;
9450     }
9451
9452   return id;
9453 }
9454
9455 /* Parse a template-declaration.
9456
9457    template-declaration:
9458      export [opt] template < template-parameter-list > declaration
9459
9460    If MEMBER_P is TRUE, this template-declaration occurs within a
9461    class-specifier.
9462
9463    The grammar rule given by the standard isn't correct.  What
9464    is really meant is:
9465
9466    template-declaration:
9467      export [opt] template-parameter-list-seq
9468        decl-specifier-seq [opt] init-declarator [opt] ;
9469      export [opt] template-parameter-list-seq
9470        function-definition
9471
9472    template-parameter-list-seq:
9473      template-parameter-list-seq [opt]
9474      template < template-parameter-list >  */
9475
9476 static void
9477 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9478 {
9479   /* Check for `export'.  */
9480   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9481     {
9482       /* Consume the `export' token.  */
9483       cp_lexer_consume_token (parser->lexer);
9484       /* Warn that we do not support `export'.  */
9485       warning (0, "keyword %<export%> not implemented, and will be ignored");
9486     }
9487
9488   cp_parser_template_declaration_after_export (parser, member_p);
9489 }
9490
9491 /* Parse a template-parameter-list.
9492
9493    template-parameter-list:
9494      template-parameter
9495      template-parameter-list , template-parameter
9496
9497    Returns a TREE_LIST.  Each node represents a template parameter.
9498    The nodes are connected via their TREE_CHAINs.  */
9499
9500 static tree
9501 cp_parser_template_parameter_list (cp_parser* parser)
9502 {
9503   tree parameter_list = NULL_TREE;
9504
9505   begin_template_parm_list ();
9506   while (true)
9507     {
9508       tree parameter;
9509       bool is_non_type;
9510       bool is_parameter_pack;
9511
9512       /* Parse the template-parameter.  */
9513       parameter = cp_parser_template_parameter (parser, 
9514                                                 &is_non_type,
9515                                                 &is_parameter_pack);
9516       /* Add it to the list.  */
9517       if (parameter != error_mark_node)
9518         parameter_list = process_template_parm (parameter_list,
9519                                                 parameter,
9520                                                 is_non_type,
9521                                                 is_parameter_pack);
9522       else
9523        {
9524          tree err_parm = build_tree_list (parameter, parameter);
9525          TREE_VALUE (err_parm) = error_mark_node;
9526          parameter_list = chainon (parameter_list, err_parm);
9527        }
9528
9529       /* If the next token is not a `,', we're done.  */
9530       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9531         break;
9532       /* Otherwise, consume the `,' token.  */
9533       cp_lexer_consume_token (parser->lexer);
9534     }
9535
9536   return end_template_parm_list (parameter_list);
9537 }
9538
9539 /* Parse a template-parameter.
9540
9541    template-parameter:
9542      type-parameter
9543      parameter-declaration
9544
9545    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9546    the parameter.  The TREE_PURPOSE is the default value, if any.
9547    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9548    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9549    set to true iff this parameter is a parameter pack. */
9550
9551 static tree
9552 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9553                               bool *is_parameter_pack)
9554 {
9555   cp_token *token;
9556   cp_parameter_declarator *parameter_declarator;
9557   cp_declarator *id_declarator;
9558   tree parm;
9559
9560   /* Assume it is a type parameter or a template parameter.  */
9561   *is_non_type = false;
9562   /* Assume it not a parameter pack. */
9563   *is_parameter_pack = false;
9564   /* Peek at the next token.  */
9565   token = cp_lexer_peek_token (parser->lexer);
9566   /* If it is `class' or `template', we have a type-parameter.  */
9567   if (token->keyword == RID_TEMPLATE)
9568     return cp_parser_type_parameter (parser, is_parameter_pack);
9569   /* If it is `class' or `typename' we do not know yet whether it is a
9570      type parameter or a non-type parameter.  Consider:
9571
9572        template <typename T, typename T::X X> ...
9573
9574      or:
9575
9576        template <class C, class D*> ...
9577
9578      Here, the first parameter is a type parameter, and the second is
9579      a non-type parameter.  We can tell by looking at the token after
9580      the identifier -- if it is a `,', `=', or `>' then we have a type
9581      parameter.  */
9582   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9583     {
9584       /* Peek at the token after `class' or `typename'.  */
9585       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9586       /* If it's an ellipsis, we have a template type parameter
9587          pack. */
9588       if (token->type == CPP_ELLIPSIS)
9589         return cp_parser_type_parameter (parser, is_parameter_pack);
9590       /* If it's an identifier, skip it.  */
9591       if (token->type == CPP_NAME)
9592         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9593       /* Now, see if the token looks like the end of a template
9594          parameter.  */
9595       if (token->type == CPP_COMMA
9596           || token->type == CPP_EQ
9597           || token->type == CPP_GREATER)
9598         return cp_parser_type_parameter (parser, is_parameter_pack);
9599     }
9600
9601   /* Otherwise, it is a non-type parameter.
9602
9603      [temp.param]
9604
9605      When parsing a default template-argument for a non-type
9606      template-parameter, the first non-nested `>' is taken as the end
9607      of the template parameter-list rather than a greater-than
9608      operator.  */
9609   *is_non_type = true;
9610   parameter_declarator
9611      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9612                                         /*parenthesized_p=*/NULL);
9613
9614   /* If the parameter declaration is marked as a parameter pack, set
9615      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9616      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9617      grokdeclarator. */
9618   if (parameter_declarator
9619       && parameter_declarator->declarator
9620       && parameter_declarator->declarator->parameter_pack_p)
9621     {
9622       *is_parameter_pack = true;
9623       parameter_declarator->declarator->parameter_pack_p = false;
9624     }
9625
9626   /* If the next token is an ellipsis, and we don't already have it
9627      marked as a parameter pack, then we have a parameter pack (that
9628      has no declarator).  */
9629   if (!*is_parameter_pack
9630       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9631       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9632     {
9633       /* Consume the `...'.  */
9634       cp_lexer_consume_token (parser->lexer);
9635       maybe_warn_variadic_templates ();
9636       
9637       *is_parameter_pack = true;
9638     }
9639   /* We might end up with a pack expansion as the type of the non-type
9640      template parameter, in which case this is a non-type template
9641      parameter pack.  */
9642   else if (parameter_declarator
9643            && parameter_declarator->decl_specifiers.type
9644            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9645     {
9646       *is_parameter_pack = true;
9647       parameter_declarator->decl_specifiers.type = 
9648         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9649     }
9650
9651   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9652     {
9653       /* Parameter packs cannot have default arguments.  However, a
9654          user may try to do so, so we'll parse them and give an
9655          appropriate diagnostic here.  */
9656
9657       /* Consume the `='.  */
9658       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9659       cp_lexer_consume_token (parser->lexer);
9660       
9661       /* Find the name of the parameter pack.  */     
9662       id_declarator = parameter_declarator->declarator;
9663       while (id_declarator && id_declarator->kind != cdk_id)
9664         id_declarator = id_declarator->declarator;
9665       
9666       if (id_declarator && id_declarator->kind == cdk_id)
9667         error ("%Htemplate parameter pack %qD cannot have a default argument",
9668                &start_token->location, id_declarator->u.id.unqualified_name);
9669       else
9670         error ("%Htemplate parameter pack cannot have a default argument",
9671                &start_token->location);
9672       
9673       /* Parse the default argument, but throw away the result.  */
9674       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9675     }
9676
9677   parm = grokdeclarator (parameter_declarator->declarator,
9678                          &parameter_declarator->decl_specifiers,
9679                          PARM, /*initialized=*/0,
9680                          /*attrlist=*/NULL);
9681   if (parm == error_mark_node)
9682     return error_mark_node;
9683
9684   return build_tree_list (parameter_declarator->default_argument, parm);
9685 }
9686
9687 /* Parse a type-parameter.
9688
9689    type-parameter:
9690      class identifier [opt]
9691      class identifier [opt] = type-id
9692      typename identifier [opt]
9693      typename identifier [opt] = type-id
9694      template < template-parameter-list > class identifier [opt]
9695      template < template-parameter-list > class identifier [opt]
9696        = id-expression
9697
9698    GNU Extension (variadic templates):
9699
9700    type-parameter:
9701      class ... identifier [opt]
9702      typename ... identifier [opt]
9703
9704    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9705    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9706    the declaration of the parameter.
9707
9708    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9709
9710 static tree
9711 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9712 {
9713   cp_token *token;
9714   tree parameter;
9715
9716   /* Look for a keyword to tell us what kind of parameter this is.  */
9717   token = cp_parser_require (parser, CPP_KEYWORD,
9718                              "%<class%>, %<typename%>, or %<template%>");
9719   if (!token)
9720     return error_mark_node;
9721
9722   switch (token->keyword)
9723     {
9724     case RID_CLASS:
9725     case RID_TYPENAME:
9726       {
9727         tree identifier;
9728         tree default_argument;
9729
9730         /* If the next token is an ellipsis, we have a template
9731            argument pack. */
9732         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9733           {
9734             /* Consume the `...' token. */
9735             cp_lexer_consume_token (parser->lexer);
9736             maybe_warn_variadic_templates ();
9737
9738             *is_parameter_pack = true;
9739           }
9740
9741         /* If the next token is an identifier, then it names the
9742            parameter.  */
9743         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9744           identifier = cp_parser_identifier (parser);
9745         else
9746           identifier = NULL_TREE;
9747
9748         /* Create the parameter.  */
9749         parameter = finish_template_type_parm (class_type_node, identifier);
9750
9751         /* If the next token is an `=', we have a default argument.  */
9752         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9753           {
9754             /* Consume the `=' token.  */
9755             cp_lexer_consume_token (parser->lexer);
9756             /* Parse the default-argument.  */
9757             push_deferring_access_checks (dk_no_deferred);
9758             default_argument = cp_parser_type_id (parser);
9759
9760             /* Template parameter packs cannot have default
9761                arguments. */
9762             if (*is_parameter_pack)
9763               {
9764                 if (identifier)
9765                   error ("%Htemplate parameter pack %qD cannot have a "
9766                          "default argument", &token->location, identifier);
9767                 else
9768                   error ("%Htemplate parameter packs cannot have "
9769                          "default arguments", &token->location);
9770                 default_argument = NULL_TREE;
9771               }
9772             pop_deferring_access_checks ();
9773           }
9774         else
9775           default_argument = NULL_TREE;
9776
9777         /* Create the combined representation of the parameter and the
9778            default argument.  */
9779         parameter = build_tree_list (default_argument, parameter);
9780       }
9781       break;
9782
9783     case RID_TEMPLATE:
9784       {
9785         tree parameter_list;
9786         tree identifier;
9787         tree default_argument;
9788
9789         /* Look for the `<'.  */
9790         cp_parser_require (parser, CPP_LESS, "%<<%>");
9791         /* Parse the template-parameter-list.  */
9792         parameter_list = cp_parser_template_parameter_list (parser);
9793         /* Look for the `>'.  */
9794         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9795         /* Look for the `class' keyword.  */
9796         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9797         /* If the next token is an ellipsis, we have a template
9798            argument pack. */
9799         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9800           {
9801             /* Consume the `...' token. */
9802             cp_lexer_consume_token (parser->lexer);
9803             maybe_warn_variadic_templates ();
9804
9805             *is_parameter_pack = true;
9806           }
9807         /* If the next token is an `=', then there is a
9808            default-argument.  If the next token is a `>', we are at
9809            the end of the parameter-list.  If the next token is a `,',
9810            then we are at the end of this parameter.  */
9811         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9812             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9813             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9814           {
9815             identifier = cp_parser_identifier (parser);
9816             /* Treat invalid names as if the parameter were nameless.  */
9817             if (identifier == error_mark_node)
9818               identifier = NULL_TREE;
9819           }
9820         else
9821           identifier = NULL_TREE;
9822
9823         /* Create the template parameter.  */
9824         parameter = finish_template_template_parm (class_type_node,
9825                                                    identifier);
9826
9827         /* If the next token is an `=', then there is a
9828            default-argument.  */
9829         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9830           {
9831             bool is_template;
9832
9833             /* Consume the `='.  */
9834             cp_lexer_consume_token (parser->lexer);
9835             /* Parse the id-expression.  */
9836             push_deferring_access_checks (dk_no_deferred);
9837             /* save token before parsing the id-expression, for error
9838                reporting */
9839             token = cp_lexer_peek_token (parser->lexer);
9840             default_argument
9841               = cp_parser_id_expression (parser,
9842                                          /*template_keyword_p=*/false,
9843                                          /*check_dependency_p=*/true,
9844                                          /*template_p=*/&is_template,
9845                                          /*declarator_p=*/false,
9846                                          /*optional_p=*/false);
9847             if (TREE_CODE (default_argument) == TYPE_DECL)
9848               /* If the id-expression was a template-id that refers to
9849                  a template-class, we already have the declaration here,
9850                  so no further lookup is needed.  */
9851                  ;
9852             else
9853               /* Look up the name.  */
9854               default_argument
9855                 = cp_parser_lookup_name (parser, default_argument,
9856                                          none_type,
9857                                          /*is_template=*/is_template,
9858                                          /*is_namespace=*/false,
9859                                          /*check_dependency=*/true,
9860                                          /*ambiguous_decls=*/NULL,
9861                                          token->location);
9862             /* See if the default argument is valid.  */
9863             default_argument
9864               = check_template_template_default_arg (default_argument);
9865
9866             /* Template parameter packs cannot have default
9867                arguments. */
9868             if (*is_parameter_pack)
9869               {
9870                 if (identifier)
9871                   error ("%Htemplate parameter pack %qD cannot "
9872                          "have a default argument",
9873                          &token->location, identifier);
9874                 else
9875                   error ("%Htemplate parameter packs cannot "
9876                          "have default arguments",
9877                          &token->location);
9878                 default_argument = NULL_TREE;
9879               }
9880             pop_deferring_access_checks ();
9881           }
9882         else
9883           default_argument = NULL_TREE;
9884
9885         /* Create the combined representation of the parameter and the
9886            default argument.  */
9887         parameter = build_tree_list (default_argument, parameter);
9888       }
9889       break;
9890
9891     default:
9892       gcc_unreachable ();
9893       break;
9894     }
9895
9896   return parameter;
9897 }
9898
9899 /* Parse a template-id.
9900
9901    template-id:
9902      template-name < template-argument-list [opt] >
9903
9904    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9905    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9906    returned.  Otherwise, if the template-name names a function, or set
9907    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9908    names a class, returns a TYPE_DECL for the specialization.
9909
9910    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9911    uninstantiated templates.  */
9912
9913 static tree
9914 cp_parser_template_id (cp_parser *parser,
9915                        bool template_keyword_p,
9916                        bool check_dependency_p,
9917                        bool is_declaration)
9918 {
9919   int i;
9920   tree templ;
9921   tree arguments;
9922   tree template_id;
9923   cp_token_position start_of_id = 0;
9924   deferred_access_check *chk;
9925   VEC (deferred_access_check,gc) *access_check;
9926   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9927   bool is_identifier;
9928
9929   /* If the next token corresponds to a template-id, there is no need
9930      to reparse it.  */
9931   next_token = cp_lexer_peek_token (parser->lexer);
9932   if (next_token->type == CPP_TEMPLATE_ID)
9933     {
9934       struct tree_check *check_value;
9935
9936       /* Get the stored value.  */
9937       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9938       /* Perform any access checks that were deferred.  */
9939       access_check = check_value->checks;
9940       if (access_check)
9941         {
9942           for (i = 0 ;
9943                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9944                ++i)
9945             {
9946               perform_or_defer_access_check (chk->binfo,
9947                                              chk->decl,
9948                                              chk->diag_decl);
9949             }
9950         }
9951       /* Return the stored value.  */
9952       return check_value->value;
9953     }
9954
9955   /* Avoid performing name lookup if there is no possibility of
9956      finding a template-id.  */
9957   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9958       || (next_token->type == CPP_NAME
9959           && !cp_parser_nth_token_starts_template_argument_list_p
9960                (parser, 2)))
9961     {
9962       cp_parser_error (parser, "expected template-id");
9963       return error_mark_node;
9964     }
9965
9966   /* Remember where the template-id starts.  */
9967   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9968     start_of_id = cp_lexer_token_position (parser->lexer, false);
9969
9970   push_deferring_access_checks (dk_deferred);
9971
9972   /* Parse the template-name.  */
9973   is_identifier = false;
9974   token = cp_lexer_peek_token (parser->lexer);
9975   templ = cp_parser_template_name (parser, template_keyword_p,
9976                                    check_dependency_p,
9977                                    is_declaration,
9978                                    &is_identifier);
9979   if (templ == error_mark_node || is_identifier)
9980     {
9981       pop_deferring_access_checks ();
9982       return templ;
9983     }
9984
9985   /* If we find the sequence `[:' after a template-name, it's probably
9986      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9987      parse correctly the argument list.  */
9988   next_token = cp_lexer_peek_token (parser->lexer);
9989   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9990   if (next_token->type == CPP_OPEN_SQUARE
9991       && next_token->flags & DIGRAPH
9992       && next_token_2->type == CPP_COLON
9993       && !(next_token_2->flags & PREV_WHITE))
9994     {
9995       cp_parser_parse_tentatively (parser);
9996       /* Change `:' into `::'.  */
9997       next_token_2->type = CPP_SCOPE;
9998       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9999          CPP_LESS.  */
10000       cp_lexer_consume_token (parser->lexer);
10001
10002       /* Parse the arguments.  */
10003       arguments = cp_parser_enclosed_template_argument_list (parser);
10004       if (!cp_parser_parse_definitely (parser))
10005         {
10006           /* If we couldn't parse an argument list, then we revert our changes
10007              and return simply an error. Maybe this is not a template-id
10008              after all.  */
10009           next_token_2->type = CPP_COLON;
10010           cp_parser_error (parser, "expected %<<%>");
10011           pop_deferring_access_checks ();
10012           return error_mark_node;
10013         }
10014       /* Otherwise, emit an error about the invalid digraph, but continue
10015          parsing because we got our argument list.  */
10016       if (permerror (next_token->location,
10017                      "%<<::%> cannot begin a template-argument list"))
10018         {
10019           static bool hint = false;
10020           inform (next_token->location,
10021                   "%<<:%> is an alternate spelling for %<[%>."
10022                   " Insert whitespace between %<<%> and %<::%>");
10023           if (!hint && !flag_permissive)
10024             {
10025               inform (next_token->location, "(if you use %<-fpermissive%>"
10026                       " G++ will accept your code)");
10027               hint = true;
10028             }
10029         }
10030     }
10031   else
10032     {
10033       /* Look for the `<' that starts the template-argument-list.  */
10034       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10035         {
10036           pop_deferring_access_checks ();
10037           return error_mark_node;
10038         }
10039       /* Parse the arguments.  */
10040       arguments = cp_parser_enclosed_template_argument_list (parser);
10041     }
10042
10043   /* Build a representation of the specialization.  */
10044   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10045     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10046   else if (DECL_CLASS_TEMPLATE_P (templ)
10047            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10048     {
10049       bool entering_scope;
10050       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10051          template (rather than some instantiation thereof) only if
10052          is not nested within some other construct.  For example, in
10053          "template <typename T> void f(T) { A<T>::", A<T> is just an
10054          instantiation of A.  */
10055       entering_scope = (template_parm_scope_p ()
10056                         && cp_lexer_next_token_is (parser->lexer,
10057                                                    CPP_SCOPE));
10058       template_id
10059         = finish_template_type (templ, arguments, entering_scope);
10060     }
10061   else
10062     {
10063       /* If it's not a class-template or a template-template, it should be
10064          a function-template.  */
10065       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10066                    || TREE_CODE (templ) == OVERLOAD
10067                    || BASELINK_P (templ)));
10068
10069       template_id = lookup_template_function (templ, arguments);
10070     }
10071
10072   /* If parsing tentatively, replace the sequence of tokens that makes
10073      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10074      should we re-parse the token stream, we will not have to repeat
10075      the effort required to do the parse, nor will we issue duplicate
10076      error messages about problems during instantiation of the
10077      template.  */
10078   if (start_of_id)
10079     {
10080       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10081
10082       /* Reset the contents of the START_OF_ID token.  */
10083       token->type = CPP_TEMPLATE_ID;
10084       /* Retrieve any deferred checks.  Do not pop this access checks yet
10085          so the memory will not be reclaimed during token replacing below.  */
10086       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10087       token->u.tree_check_value->value = template_id;
10088       token->u.tree_check_value->checks = get_deferred_access_checks ();
10089       token->keyword = RID_MAX;
10090
10091       /* Purge all subsequent tokens.  */
10092       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10093
10094       /* ??? Can we actually assume that, if template_id ==
10095          error_mark_node, we will have issued a diagnostic to the
10096          user, as opposed to simply marking the tentative parse as
10097          failed?  */
10098       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10099         error ("%Hparse error in template argument list",
10100                &token->location);
10101     }
10102
10103   pop_deferring_access_checks ();
10104   return template_id;
10105 }
10106
10107 /* Parse a template-name.
10108
10109    template-name:
10110      identifier
10111
10112    The standard should actually say:
10113
10114    template-name:
10115      identifier
10116      operator-function-id
10117
10118    A defect report has been filed about this issue.
10119
10120    A conversion-function-id cannot be a template name because they cannot
10121    be part of a template-id. In fact, looking at this code:
10122
10123    a.operator K<int>()
10124
10125    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10126    It is impossible to call a templated conversion-function-id with an
10127    explicit argument list, since the only allowed template parameter is
10128    the type to which it is converting.
10129
10130    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10131    `template' keyword, in a construction like:
10132
10133      T::template f<3>()
10134
10135    In that case `f' is taken to be a template-name, even though there
10136    is no way of knowing for sure.
10137
10138    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10139    name refers to a set of overloaded functions, at least one of which
10140    is a template, or an IDENTIFIER_NODE with the name of the template,
10141    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10142    names are looked up inside uninstantiated templates.  */
10143
10144 static tree
10145 cp_parser_template_name (cp_parser* parser,
10146                          bool template_keyword_p,
10147                          bool check_dependency_p,
10148                          bool is_declaration,
10149                          bool *is_identifier)
10150 {
10151   tree identifier;
10152   tree decl;
10153   tree fns;
10154   cp_token *token = cp_lexer_peek_token (parser->lexer);
10155
10156   /* If the next token is `operator', then we have either an
10157      operator-function-id or a conversion-function-id.  */
10158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10159     {
10160       /* We don't know whether we're looking at an
10161          operator-function-id or a conversion-function-id.  */
10162       cp_parser_parse_tentatively (parser);
10163       /* Try an operator-function-id.  */
10164       identifier = cp_parser_operator_function_id (parser);
10165       /* If that didn't work, try a conversion-function-id.  */
10166       if (!cp_parser_parse_definitely (parser))
10167         {
10168           cp_parser_error (parser, "expected template-name");
10169           return error_mark_node;
10170         }
10171     }
10172   /* Look for the identifier.  */
10173   else
10174     identifier = cp_parser_identifier (parser);
10175
10176   /* If we didn't find an identifier, we don't have a template-id.  */
10177   if (identifier == error_mark_node)
10178     return error_mark_node;
10179
10180   /* If the name immediately followed the `template' keyword, then it
10181      is a template-name.  However, if the next token is not `<', then
10182      we do not treat it as a template-name, since it is not being used
10183      as part of a template-id.  This enables us to handle constructs
10184      like:
10185
10186        template <typename T> struct S { S(); };
10187        template <typename T> S<T>::S();
10188
10189      correctly.  We would treat `S' as a template -- if it were `S<T>'
10190      -- but we do not if there is no `<'.  */
10191
10192   if (processing_template_decl
10193       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10194     {
10195       /* In a declaration, in a dependent context, we pretend that the
10196          "template" keyword was present in order to improve error
10197          recovery.  For example, given:
10198
10199            template <typename T> void f(T::X<int>);
10200
10201          we want to treat "X<int>" as a template-id.  */
10202       if (is_declaration
10203           && !template_keyword_p
10204           && parser->scope && TYPE_P (parser->scope)
10205           && check_dependency_p
10206           && dependent_type_p (parser->scope)
10207           /* Do not do this for dtors (or ctors), since they never
10208              need the template keyword before their name.  */
10209           && !constructor_name_p (identifier, parser->scope))
10210         {
10211           cp_token_position start = 0;
10212
10213           /* Explain what went wrong.  */
10214           error ("%Hnon-template %qD used as template",
10215                  &token->location, identifier);
10216           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10217                   parser->scope, identifier);
10218           /* If parsing tentatively, find the location of the "<" token.  */
10219           if (cp_parser_simulate_error (parser))
10220             start = cp_lexer_token_position (parser->lexer, true);
10221           /* Parse the template arguments so that we can issue error
10222              messages about them.  */
10223           cp_lexer_consume_token (parser->lexer);
10224           cp_parser_enclosed_template_argument_list (parser);
10225           /* Skip tokens until we find a good place from which to
10226              continue parsing.  */
10227           cp_parser_skip_to_closing_parenthesis (parser,
10228                                                  /*recovering=*/true,
10229                                                  /*or_comma=*/true,
10230                                                  /*consume_paren=*/false);
10231           /* If parsing tentatively, permanently remove the
10232              template argument list.  That will prevent duplicate
10233              error messages from being issued about the missing
10234              "template" keyword.  */
10235           if (start)
10236             cp_lexer_purge_tokens_after (parser->lexer, start);
10237           if (is_identifier)
10238             *is_identifier = true;
10239           return identifier;
10240         }
10241
10242       /* If the "template" keyword is present, then there is generally
10243          no point in doing name-lookup, so we just return IDENTIFIER.
10244          But, if the qualifying scope is non-dependent then we can
10245          (and must) do name-lookup normally.  */
10246       if (template_keyword_p
10247           && (!parser->scope
10248               || (TYPE_P (parser->scope)
10249                   && dependent_type_p (parser->scope))))
10250         return identifier;
10251     }
10252
10253   /* Look up the name.  */
10254   decl = cp_parser_lookup_name (parser, identifier,
10255                                 none_type,
10256                                 /*is_template=*/false,
10257                                 /*is_namespace=*/false,
10258                                 check_dependency_p,
10259                                 /*ambiguous_decls=*/NULL,
10260                                 token->location);
10261   decl = maybe_get_template_decl_from_type_decl (decl);
10262
10263   /* If DECL is a template, then the name was a template-name.  */
10264   if (TREE_CODE (decl) == TEMPLATE_DECL)
10265     ;
10266   else
10267     {
10268       tree fn = NULL_TREE;
10269
10270       /* The standard does not explicitly indicate whether a name that
10271          names a set of overloaded declarations, some of which are
10272          templates, is a template-name.  However, such a name should
10273          be a template-name; otherwise, there is no way to form a
10274          template-id for the overloaded templates.  */
10275       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10276       if (TREE_CODE (fns) == OVERLOAD)
10277         for (fn = fns; fn; fn = OVL_NEXT (fn))
10278           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10279             break;
10280
10281       if (!fn)
10282         {
10283           /* The name does not name a template.  */
10284           cp_parser_error (parser, "expected template-name");
10285           return error_mark_node;
10286         }
10287     }
10288
10289   /* If DECL is dependent, and refers to a function, then just return
10290      its name; we will look it up again during template instantiation.  */
10291   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10292     {
10293       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10294       if (TYPE_P (scope) && dependent_type_p (scope))
10295         return identifier;
10296     }
10297
10298   return decl;
10299 }
10300
10301 /* Parse a template-argument-list.
10302
10303    template-argument-list:
10304      template-argument ... [opt]
10305      template-argument-list , template-argument ... [opt]
10306
10307    Returns a TREE_VEC containing the arguments.  */
10308
10309 static tree
10310 cp_parser_template_argument_list (cp_parser* parser)
10311 {
10312   tree fixed_args[10];
10313   unsigned n_args = 0;
10314   unsigned alloced = 10;
10315   tree *arg_ary = fixed_args;
10316   tree vec;
10317   bool saved_in_template_argument_list_p;
10318   bool saved_ice_p;
10319   bool saved_non_ice_p;
10320
10321   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10322   parser->in_template_argument_list_p = true;
10323   /* Even if the template-id appears in an integral
10324      constant-expression, the contents of the argument list do
10325      not.  */
10326   saved_ice_p = parser->integral_constant_expression_p;
10327   parser->integral_constant_expression_p = false;
10328   saved_non_ice_p = parser->non_integral_constant_expression_p;
10329   parser->non_integral_constant_expression_p = false;
10330   /* Parse the arguments.  */
10331   do
10332     {
10333       tree argument;
10334
10335       if (n_args)
10336         /* Consume the comma.  */
10337         cp_lexer_consume_token (parser->lexer);
10338
10339       /* Parse the template-argument.  */
10340       argument = cp_parser_template_argument (parser);
10341
10342       /* If the next token is an ellipsis, we're expanding a template
10343          argument pack. */
10344       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10345         {
10346           /* Consume the `...' token. */
10347           cp_lexer_consume_token (parser->lexer);
10348
10349           /* Make the argument into a TYPE_PACK_EXPANSION or
10350              EXPR_PACK_EXPANSION. */
10351           argument = make_pack_expansion (argument);
10352         }
10353
10354       if (n_args == alloced)
10355         {
10356           alloced *= 2;
10357
10358           if (arg_ary == fixed_args)
10359             {
10360               arg_ary = XNEWVEC (tree, alloced);
10361               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10362             }
10363           else
10364             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10365         }
10366       arg_ary[n_args++] = argument;
10367     }
10368   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10369
10370   vec = make_tree_vec (n_args);
10371
10372   while (n_args--)
10373     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10374
10375   if (arg_ary != fixed_args)
10376     free (arg_ary);
10377   parser->non_integral_constant_expression_p = saved_non_ice_p;
10378   parser->integral_constant_expression_p = saved_ice_p;
10379   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10380   return vec;
10381 }
10382
10383 /* Parse a template-argument.
10384
10385    template-argument:
10386      assignment-expression
10387      type-id
10388      id-expression
10389
10390    The representation is that of an assignment-expression, type-id, or
10391    id-expression -- except that the qualified id-expression is
10392    evaluated, so that the value returned is either a DECL or an
10393    OVERLOAD.
10394
10395    Although the standard says "assignment-expression", it forbids
10396    throw-expressions or assignments in the template argument.
10397    Therefore, we use "conditional-expression" instead.  */
10398
10399 static tree
10400 cp_parser_template_argument (cp_parser* parser)
10401 {
10402   tree argument;
10403   bool template_p;
10404   bool address_p;
10405   bool maybe_type_id = false;
10406   cp_token *token = NULL, *argument_start_token = NULL;
10407   cp_id_kind idk;
10408
10409   /* There's really no way to know what we're looking at, so we just
10410      try each alternative in order.
10411
10412        [temp.arg]
10413
10414        In a template-argument, an ambiguity between a type-id and an
10415        expression is resolved to a type-id, regardless of the form of
10416        the corresponding template-parameter.
10417
10418      Therefore, we try a type-id first.  */
10419   cp_parser_parse_tentatively (parser);
10420   argument = cp_parser_type_id (parser);
10421   /* If there was no error parsing the type-id but the next token is a
10422      '>>', our behavior depends on which dialect of C++ we're
10423      parsing. In C++98, we probably found a typo for '> >'. But there
10424      are type-id which are also valid expressions. For instance:
10425
10426      struct X { int operator >> (int); };
10427      template <int V> struct Foo {};
10428      Foo<X () >> 5> r;
10429
10430      Here 'X()' is a valid type-id of a function type, but the user just
10431      wanted to write the expression "X() >> 5". Thus, we remember that we
10432      found a valid type-id, but we still try to parse the argument as an
10433      expression to see what happens. 
10434
10435      In C++0x, the '>>' will be considered two separate '>'
10436      tokens.  */
10437   if (!cp_parser_error_occurred (parser)
10438       && cxx_dialect == cxx98
10439       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10440     {
10441       maybe_type_id = true;
10442       cp_parser_abort_tentative_parse (parser);
10443     }
10444   else
10445     {
10446       /* If the next token isn't a `,' or a `>', then this argument wasn't
10447       really finished. This means that the argument is not a valid
10448       type-id.  */
10449       if (!cp_parser_next_token_ends_template_argument_p (parser))
10450         cp_parser_error (parser, "expected template-argument");
10451       /* If that worked, we're done.  */
10452       if (cp_parser_parse_definitely (parser))
10453         return argument;
10454     }
10455   /* We're still not sure what the argument will be.  */
10456   cp_parser_parse_tentatively (parser);
10457   /* Try a template.  */
10458   argument_start_token = cp_lexer_peek_token (parser->lexer);
10459   argument = cp_parser_id_expression (parser,
10460                                       /*template_keyword_p=*/false,
10461                                       /*check_dependency_p=*/true,
10462                                       &template_p,
10463                                       /*declarator_p=*/false,
10464                                       /*optional_p=*/false);
10465   /* If the next token isn't a `,' or a `>', then this argument wasn't
10466      really finished.  */
10467   if (!cp_parser_next_token_ends_template_argument_p (parser))
10468     cp_parser_error (parser, "expected template-argument");
10469   if (!cp_parser_error_occurred (parser))
10470     {
10471       /* Figure out what is being referred to.  If the id-expression
10472          was for a class template specialization, then we will have a
10473          TYPE_DECL at this point.  There is no need to do name lookup
10474          at this point in that case.  */
10475       if (TREE_CODE (argument) != TYPE_DECL)
10476         argument = cp_parser_lookup_name (parser, argument,
10477                                           none_type,
10478                                           /*is_template=*/template_p,
10479                                           /*is_namespace=*/false,
10480                                           /*check_dependency=*/true,
10481                                           /*ambiguous_decls=*/NULL,
10482                                           argument_start_token->location);
10483       if (TREE_CODE (argument) != TEMPLATE_DECL
10484           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10485         cp_parser_error (parser, "expected template-name");
10486     }
10487   if (cp_parser_parse_definitely (parser))
10488     return argument;
10489   /* It must be a non-type argument.  There permitted cases are given
10490      in [temp.arg.nontype]:
10491
10492      -- an integral constant-expression of integral or enumeration
10493         type; or
10494
10495      -- the name of a non-type template-parameter; or
10496
10497      -- the name of an object or function with external linkage...
10498
10499      -- the address of an object or function with external linkage...
10500
10501      -- a pointer to member...  */
10502   /* Look for a non-type template parameter.  */
10503   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10504     {
10505       cp_parser_parse_tentatively (parser);
10506       argument = cp_parser_primary_expression (parser,
10507                                                /*address_p=*/false,
10508                                                /*cast_p=*/false,
10509                                                /*template_arg_p=*/true,
10510                                                &idk);
10511       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10512           || !cp_parser_next_token_ends_template_argument_p (parser))
10513         cp_parser_simulate_error (parser);
10514       if (cp_parser_parse_definitely (parser))
10515         return argument;
10516     }
10517
10518   /* If the next token is "&", the argument must be the address of an
10519      object or function with external linkage.  */
10520   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10521   if (address_p)
10522     cp_lexer_consume_token (parser->lexer);
10523   /* See if we might have an id-expression.  */
10524   token = cp_lexer_peek_token (parser->lexer);
10525   if (token->type == CPP_NAME
10526       || token->keyword == RID_OPERATOR
10527       || token->type == CPP_SCOPE
10528       || token->type == CPP_TEMPLATE_ID
10529       || token->type == CPP_NESTED_NAME_SPECIFIER)
10530     {
10531       cp_parser_parse_tentatively (parser);
10532       argument = cp_parser_primary_expression (parser,
10533                                                address_p,
10534                                                /*cast_p=*/false,
10535                                                /*template_arg_p=*/true,
10536                                                &idk);
10537       if (cp_parser_error_occurred (parser)
10538           || !cp_parser_next_token_ends_template_argument_p (parser))
10539         cp_parser_abort_tentative_parse (parser);
10540       else
10541         {
10542           if (TREE_CODE (argument) == INDIRECT_REF)
10543             {
10544               gcc_assert (REFERENCE_REF_P (argument));
10545               argument = TREE_OPERAND (argument, 0);
10546             }
10547
10548           if (TREE_CODE (argument) == VAR_DECL)
10549             {
10550               /* A variable without external linkage might still be a
10551                  valid constant-expression, so no error is issued here
10552                  if the external-linkage check fails.  */
10553               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10554                 cp_parser_simulate_error (parser);
10555             }
10556           else if (is_overloaded_fn (argument))
10557             /* All overloaded functions are allowed; if the external
10558                linkage test does not pass, an error will be issued
10559                later.  */
10560             ;
10561           else if (address_p
10562                    && (TREE_CODE (argument) == OFFSET_REF
10563                        || TREE_CODE (argument) == SCOPE_REF))
10564             /* A pointer-to-member.  */
10565             ;
10566           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10567             ;
10568           else
10569             cp_parser_simulate_error (parser);
10570
10571           if (cp_parser_parse_definitely (parser))
10572             {
10573               if (address_p)
10574                 argument = build_x_unary_op (ADDR_EXPR, argument,
10575                                              tf_warning_or_error);
10576               return argument;
10577             }
10578         }
10579     }
10580   /* If the argument started with "&", there are no other valid
10581      alternatives at this point.  */
10582   if (address_p)
10583     {
10584       cp_parser_error (parser, "invalid non-type template argument");
10585       return error_mark_node;
10586     }
10587
10588   /* If the argument wasn't successfully parsed as a type-id followed
10589      by '>>', the argument can only be a constant expression now.
10590      Otherwise, we try parsing the constant-expression tentatively,
10591      because the argument could really be a type-id.  */
10592   if (maybe_type_id)
10593     cp_parser_parse_tentatively (parser);
10594   argument = cp_parser_constant_expression (parser,
10595                                             /*allow_non_constant_p=*/false,
10596                                             /*non_constant_p=*/NULL);
10597   argument = fold_non_dependent_expr (argument);
10598   if (!maybe_type_id)
10599     return argument;
10600   if (!cp_parser_next_token_ends_template_argument_p (parser))
10601     cp_parser_error (parser, "expected template-argument");
10602   if (cp_parser_parse_definitely (parser))
10603     return argument;
10604   /* We did our best to parse the argument as a non type-id, but that
10605      was the only alternative that matched (albeit with a '>' after
10606      it). We can assume it's just a typo from the user, and a
10607      diagnostic will then be issued.  */
10608   return cp_parser_type_id (parser);
10609 }
10610
10611 /* Parse an explicit-instantiation.
10612
10613    explicit-instantiation:
10614      template declaration
10615
10616    Although the standard says `declaration', what it really means is:
10617
10618    explicit-instantiation:
10619      template decl-specifier-seq [opt] declarator [opt] ;
10620
10621    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10622    supposed to be allowed.  A defect report has been filed about this
10623    issue.
10624
10625    GNU Extension:
10626
10627    explicit-instantiation:
10628      storage-class-specifier template
10629        decl-specifier-seq [opt] declarator [opt] ;
10630      function-specifier template
10631        decl-specifier-seq [opt] declarator [opt] ;  */
10632
10633 static void
10634 cp_parser_explicit_instantiation (cp_parser* parser)
10635 {
10636   int declares_class_or_enum;
10637   cp_decl_specifier_seq decl_specifiers;
10638   tree extension_specifier = NULL_TREE;
10639   cp_token *token;
10640
10641   /* Look for an (optional) storage-class-specifier or
10642      function-specifier.  */
10643   if (cp_parser_allow_gnu_extensions_p (parser))
10644     {
10645       extension_specifier
10646         = cp_parser_storage_class_specifier_opt (parser);
10647       if (!extension_specifier)
10648         extension_specifier
10649           = cp_parser_function_specifier_opt (parser,
10650                                               /*decl_specs=*/NULL);
10651     }
10652
10653   /* Look for the `template' keyword.  */
10654   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10655   /* Let the front end know that we are processing an explicit
10656      instantiation.  */
10657   begin_explicit_instantiation ();
10658   /* [temp.explicit] says that we are supposed to ignore access
10659      control while processing explicit instantiation directives.  */
10660   push_deferring_access_checks (dk_no_check);
10661   /* Parse a decl-specifier-seq.  */
10662   token = cp_lexer_peek_token (parser->lexer);
10663   cp_parser_decl_specifier_seq (parser,
10664                                 CP_PARSER_FLAGS_OPTIONAL,
10665                                 &decl_specifiers,
10666                                 &declares_class_or_enum);
10667   /* If there was exactly one decl-specifier, and it declared a class,
10668      and there's no declarator, then we have an explicit type
10669      instantiation.  */
10670   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10671     {
10672       tree type;
10673
10674       type = check_tag_decl (&decl_specifiers);
10675       /* Turn access control back on for names used during
10676          template instantiation.  */
10677       pop_deferring_access_checks ();
10678       if (type)
10679         do_type_instantiation (type, extension_specifier,
10680                                /*complain=*/tf_error);
10681     }
10682   else
10683     {
10684       cp_declarator *declarator;
10685       tree decl;
10686
10687       /* Parse the declarator.  */
10688       declarator
10689         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10690                                 /*ctor_dtor_or_conv_p=*/NULL,
10691                                 /*parenthesized_p=*/NULL,
10692                                 /*member_p=*/false);
10693       if (declares_class_or_enum & 2)
10694         cp_parser_check_for_definition_in_return_type (declarator,
10695                                                        decl_specifiers.type,
10696                                                        decl_specifiers.type_location);
10697       if (declarator != cp_error_declarator)
10698         {
10699           decl = grokdeclarator (declarator, &decl_specifiers,
10700                                  NORMAL, 0, &decl_specifiers.attributes);
10701           /* Turn access control back on for names used during
10702              template instantiation.  */
10703           pop_deferring_access_checks ();
10704           /* Do the explicit instantiation.  */
10705           do_decl_instantiation (decl, extension_specifier);
10706         }
10707       else
10708         {
10709           pop_deferring_access_checks ();
10710           /* Skip the body of the explicit instantiation.  */
10711           cp_parser_skip_to_end_of_statement (parser);
10712         }
10713     }
10714   /* We're done with the instantiation.  */
10715   end_explicit_instantiation ();
10716
10717   cp_parser_consume_semicolon_at_end_of_statement (parser);
10718 }
10719
10720 /* Parse an explicit-specialization.
10721
10722    explicit-specialization:
10723      template < > declaration
10724
10725    Although the standard says `declaration', what it really means is:
10726
10727    explicit-specialization:
10728      template <> decl-specifier [opt] init-declarator [opt] ;
10729      template <> function-definition
10730      template <> explicit-specialization
10731      template <> template-declaration  */
10732
10733 static void
10734 cp_parser_explicit_specialization (cp_parser* parser)
10735 {
10736   bool need_lang_pop;
10737   cp_token *token = cp_lexer_peek_token (parser->lexer);
10738
10739   /* Look for the `template' keyword.  */
10740   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10741   /* Look for the `<'.  */
10742   cp_parser_require (parser, CPP_LESS, "%<<%>");
10743   /* Look for the `>'.  */
10744   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10745   /* We have processed another parameter list.  */
10746   ++parser->num_template_parameter_lists;
10747   /* [temp]
10748
10749      A template ... explicit specialization ... shall not have C
10750      linkage.  */
10751   if (current_lang_name == lang_name_c)
10752     {
10753       error ("%Htemplate specialization with C linkage", &token->location);
10754       /* Give it C++ linkage to avoid confusing other parts of the
10755          front end.  */
10756       push_lang_context (lang_name_cplusplus);
10757       need_lang_pop = true;
10758     }
10759   else
10760     need_lang_pop = false;
10761   /* Let the front end know that we are beginning a specialization.  */
10762   if (!begin_specialization ())
10763     {
10764       end_specialization ();
10765       cp_parser_skip_to_end_of_block_or_statement (parser);
10766       return;
10767     }
10768
10769   /* If the next keyword is `template', we need to figure out whether
10770      or not we're looking a template-declaration.  */
10771   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10772     {
10773       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10774           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10775         cp_parser_template_declaration_after_export (parser,
10776                                                      /*member_p=*/false);
10777       else
10778         cp_parser_explicit_specialization (parser);
10779     }
10780   else
10781     /* Parse the dependent declaration.  */
10782     cp_parser_single_declaration (parser,
10783                                   /*checks=*/NULL,
10784                                   /*member_p=*/false,
10785                                   /*explicit_specialization_p=*/true,
10786                                   /*friend_p=*/NULL);
10787   /* We're done with the specialization.  */
10788   end_specialization ();
10789   /* For the erroneous case of a template with C linkage, we pushed an
10790      implicit C++ linkage scope; exit that scope now.  */
10791   if (need_lang_pop)
10792     pop_lang_context ();
10793   /* We're done with this parameter list.  */
10794   --parser->num_template_parameter_lists;
10795 }
10796
10797 /* Parse a type-specifier.
10798
10799    type-specifier:
10800      simple-type-specifier
10801      class-specifier
10802      enum-specifier
10803      elaborated-type-specifier
10804      cv-qualifier
10805
10806    GNU Extension:
10807
10808    type-specifier:
10809      __complex__
10810
10811    Returns a representation of the type-specifier.  For a
10812    class-specifier, enum-specifier, or elaborated-type-specifier, a
10813    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10814
10815    The parser flags FLAGS is used to control type-specifier parsing.
10816
10817    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10818    in a decl-specifier-seq.
10819
10820    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10821    class-specifier, enum-specifier, or elaborated-type-specifier, then
10822    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10823    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10824    zero.
10825
10826    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10827    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10828    is set to FALSE.  */
10829
10830 static tree
10831 cp_parser_type_specifier (cp_parser* parser,
10832                           cp_parser_flags flags,
10833                           cp_decl_specifier_seq *decl_specs,
10834                           bool is_declaration,
10835                           int* declares_class_or_enum,
10836                           bool* is_cv_qualifier)
10837 {
10838   tree type_spec = NULL_TREE;
10839   cp_token *token;
10840   enum rid keyword;
10841   cp_decl_spec ds = ds_last;
10842
10843   /* Assume this type-specifier does not declare a new type.  */
10844   if (declares_class_or_enum)
10845     *declares_class_or_enum = 0;
10846   /* And that it does not specify a cv-qualifier.  */
10847   if (is_cv_qualifier)
10848     *is_cv_qualifier = false;
10849   /* Peek at the next token.  */
10850   token = cp_lexer_peek_token (parser->lexer);
10851
10852   /* If we're looking at a keyword, we can use that to guide the
10853      production we choose.  */
10854   keyword = token->keyword;
10855   switch (keyword)
10856     {
10857     case RID_ENUM:
10858       /* Look for the enum-specifier.  */
10859       type_spec = cp_parser_enum_specifier (parser);
10860       /* If that worked, we're done.  */
10861       if (type_spec)
10862         {
10863           if (declares_class_or_enum)
10864             *declares_class_or_enum = 2;
10865           if (decl_specs)
10866             cp_parser_set_decl_spec_type (decl_specs,
10867                                           type_spec,
10868                                           token->location,
10869                                           /*user_defined_p=*/true);
10870           return type_spec;
10871         }
10872       else
10873         goto elaborated_type_specifier;
10874
10875       /* Any of these indicate either a class-specifier, or an
10876          elaborated-type-specifier.  */
10877     case RID_CLASS:
10878     case RID_STRUCT:
10879     case RID_UNION:
10880       /* Parse tentatively so that we can back up if we don't find a
10881          class-specifier.  */
10882       cp_parser_parse_tentatively (parser);
10883       /* Look for the class-specifier.  */
10884       type_spec = cp_parser_class_specifier (parser);
10885       /* If that worked, we're done.  */
10886       if (cp_parser_parse_definitely (parser))
10887         {
10888           if (declares_class_or_enum)
10889             *declares_class_or_enum = 2;
10890           if (decl_specs)
10891             cp_parser_set_decl_spec_type (decl_specs,
10892                                           type_spec,
10893                                           token->location,
10894                                           /*user_defined_p=*/true);
10895           return type_spec;
10896         }
10897
10898       /* Fall through.  */
10899     elaborated_type_specifier:
10900       /* We're declaring (not defining) a class or enum.  */
10901       if (declares_class_or_enum)
10902         *declares_class_or_enum = 1;
10903
10904       /* Fall through.  */
10905     case RID_TYPENAME:
10906       /* Look for an elaborated-type-specifier.  */
10907       type_spec
10908         = (cp_parser_elaborated_type_specifier
10909            (parser,
10910             decl_specs && decl_specs->specs[(int) ds_friend],
10911             is_declaration));
10912       if (decl_specs)
10913         cp_parser_set_decl_spec_type (decl_specs,
10914                                       type_spec,
10915                                       token->location,
10916                                       /*user_defined_p=*/true);
10917       return type_spec;
10918
10919     case RID_CONST:
10920       ds = ds_const;
10921       if (is_cv_qualifier)
10922         *is_cv_qualifier = true;
10923       break;
10924
10925     case RID_VOLATILE:
10926       ds = ds_volatile;
10927       if (is_cv_qualifier)
10928         *is_cv_qualifier = true;
10929       break;
10930
10931     case RID_RESTRICT:
10932       ds = ds_restrict;
10933       if (is_cv_qualifier)
10934         *is_cv_qualifier = true;
10935       break;
10936
10937     case RID_COMPLEX:
10938       /* The `__complex__' keyword is a GNU extension.  */
10939       ds = ds_complex;
10940       break;
10941
10942     default:
10943       break;
10944     }
10945
10946   /* Handle simple keywords.  */
10947   if (ds != ds_last)
10948     {
10949       if (decl_specs)
10950         {
10951           ++decl_specs->specs[(int)ds];
10952           decl_specs->any_specifiers_p = true;
10953         }
10954       return cp_lexer_consume_token (parser->lexer)->u.value;
10955     }
10956
10957   /* If we do not already have a type-specifier, assume we are looking
10958      at a simple-type-specifier.  */
10959   type_spec = cp_parser_simple_type_specifier (parser,
10960                                                decl_specs,
10961                                                flags);
10962
10963   /* If we didn't find a type-specifier, and a type-specifier was not
10964      optional in this context, issue an error message.  */
10965   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10966     {
10967       cp_parser_error (parser, "expected type specifier");
10968       return error_mark_node;
10969     }
10970
10971   return type_spec;
10972 }
10973
10974 /* Parse a simple-type-specifier.
10975
10976    simple-type-specifier:
10977      :: [opt] nested-name-specifier [opt] type-name
10978      :: [opt] nested-name-specifier template template-id
10979      char
10980      wchar_t
10981      bool
10982      short
10983      int
10984      long
10985      signed
10986      unsigned
10987      float
10988      double
10989      void
10990
10991    C++0x Extension:
10992
10993    simple-type-specifier:
10994      auto
10995      decltype ( expression )   
10996      char16_t
10997      char32_t
10998
10999    GNU Extension:
11000
11001    simple-type-specifier:
11002      __typeof__ unary-expression
11003      __typeof__ ( type-id )
11004
11005    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11006    appropriately updated.  */
11007
11008 static tree
11009 cp_parser_simple_type_specifier (cp_parser* parser,
11010                                  cp_decl_specifier_seq *decl_specs,
11011                                  cp_parser_flags flags)
11012 {
11013   tree type = NULL_TREE;
11014   cp_token *token;
11015
11016   /* Peek at the next token.  */
11017   token = cp_lexer_peek_token (parser->lexer);
11018
11019   /* If we're looking at a keyword, things are easy.  */
11020   switch (token->keyword)
11021     {
11022     case RID_CHAR:
11023       if (decl_specs)
11024         decl_specs->explicit_char_p = true;
11025       type = char_type_node;
11026       break;
11027     case RID_CHAR16:
11028       type = char16_type_node;
11029       break;
11030     case RID_CHAR32:
11031       type = char32_type_node;
11032       break;
11033     case RID_WCHAR:
11034       type = wchar_type_node;
11035       break;
11036     case RID_BOOL:
11037       type = boolean_type_node;
11038       break;
11039     case RID_SHORT:
11040       if (decl_specs)
11041         ++decl_specs->specs[(int) ds_short];
11042       type = short_integer_type_node;
11043       break;
11044     case RID_INT:
11045       if (decl_specs)
11046         decl_specs->explicit_int_p = true;
11047       type = integer_type_node;
11048       break;
11049     case RID_LONG:
11050       if (decl_specs)
11051         ++decl_specs->specs[(int) ds_long];
11052       type = long_integer_type_node;
11053       break;
11054     case RID_SIGNED:
11055       if (decl_specs)
11056         ++decl_specs->specs[(int) ds_signed];
11057       type = integer_type_node;
11058       break;
11059     case RID_UNSIGNED:
11060       if (decl_specs)
11061         ++decl_specs->specs[(int) ds_unsigned];
11062       type = unsigned_type_node;
11063       break;
11064     case RID_FLOAT:
11065       type = float_type_node;
11066       break;
11067     case RID_DOUBLE:
11068       type = double_type_node;
11069       break;
11070     case RID_VOID:
11071       type = void_type_node;
11072       break;
11073       
11074     case RID_AUTO:
11075       maybe_warn_cpp0x ("C++0x auto");
11076       type = make_auto ();
11077       break;
11078
11079     case RID_DECLTYPE:
11080       /* Parse the `decltype' type.  */
11081       type = cp_parser_decltype (parser);
11082
11083       if (decl_specs)
11084         cp_parser_set_decl_spec_type (decl_specs, type,
11085                                       token->location,
11086                                       /*user_defined_p=*/true);
11087
11088       return type;
11089
11090     case RID_TYPEOF:
11091       /* Consume the `typeof' token.  */
11092       cp_lexer_consume_token (parser->lexer);
11093       /* Parse the operand to `typeof'.  */
11094       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11095       /* If it is not already a TYPE, take its type.  */
11096       if (!TYPE_P (type))
11097         type = finish_typeof (type);
11098
11099       if (decl_specs)
11100         cp_parser_set_decl_spec_type (decl_specs, type,
11101                                       token->location,
11102                                       /*user_defined_p=*/true);
11103
11104       return type;
11105
11106     default:
11107       break;
11108     }
11109
11110   /* If the type-specifier was for a built-in type, we're done.  */
11111   if (type)
11112     {
11113       tree id;
11114
11115       /* Record the type.  */
11116       if (decl_specs
11117           && (token->keyword != RID_SIGNED
11118               && token->keyword != RID_UNSIGNED
11119               && token->keyword != RID_SHORT
11120               && token->keyword != RID_LONG))
11121         cp_parser_set_decl_spec_type (decl_specs,
11122                                       type,
11123                                       token->location,
11124                                       /*user_defined=*/false);
11125       if (decl_specs)
11126         decl_specs->any_specifiers_p = true;
11127
11128       /* Consume the token.  */
11129       id = cp_lexer_consume_token (parser->lexer)->u.value;
11130
11131       /* There is no valid C++ program where a non-template type is
11132          followed by a "<".  That usually indicates that the user thought
11133          that the type was a template.  */
11134       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11135
11136       return TYPE_NAME (type);
11137     }
11138
11139   /* The type-specifier must be a user-defined type.  */
11140   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11141     {
11142       bool qualified_p;
11143       bool global_p;
11144
11145       /* Don't gobble tokens or issue error messages if this is an
11146          optional type-specifier.  */
11147       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11148         cp_parser_parse_tentatively (parser);
11149
11150       /* Look for the optional `::' operator.  */
11151       global_p
11152         = (cp_parser_global_scope_opt (parser,
11153                                        /*current_scope_valid_p=*/false)
11154            != NULL_TREE);
11155       /* Look for the nested-name specifier.  */
11156       qualified_p
11157         = (cp_parser_nested_name_specifier_opt (parser,
11158                                                 /*typename_keyword_p=*/false,
11159                                                 /*check_dependency_p=*/true,
11160                                                 /*type_p=*/false,
11161                                                 /*is_declaration=*/false)
11162            != NULL_TREE);
11163       token = cp_lexer_peek_token (parser->lexer);
11164       /* If we have seen a nested-name-specifier, and the next token
11165          is `template', then we are using the template-id production.  */
11166       if (parser->scope
11167           && cp_parser_optional_template_keyword (parser))
11168         {
11169           /* Look for the template-id.  */
11170           type = cp_parser_template_id (parser,
11171                                         /*template_keyword_p=*/true,
11172                                         /*check_dependency_p=*/true,
11173                                         /*is_declaration=*/false);
11174           /* If the template-id did not name a type, we are out of
11175              luck.  */
11176           if (TREE_CODE (type) != TYPE_DECL)
11177             {
11178               cp_parser_error (parser, "expected template-id for type");
11179               type = NULL_TREE;
11180             }
11181         }
11182       /* Otherwise, look for a type-name.  */
11183       else
11184         type = cp_parser_type_name (parser);
11185       /* Keep track of all name-lookups performed in class scopes.  */
11186       if (type
11187           && !global_p
11188           && !qualified_p
11189           && TREE_CODE (type) == TYPE_DECL
11190           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11191         maybe_note_name_used_in_class (DECL_NAME (type), type);
11192       /* If it didn't work out, we don't have a TYPE.  */
11193       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11194           && !cp_parser_parse_definitely (parser))
11195         type = NULL_TREE;
11196       if (type && decl_specs)
11197         cp_parser_set_decl_spec_type (decl_specs, type,
11198                                       token->location,
11199                                       /*user_defined=*/true);
11200     }
11201
11202   /* If we didn't get a type-name, issue an error message.  */
11203   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11204     {
11205       cp_parser_error (parser, "expected type-name");
11206       return error_mark_node;
11207     }
11208
11209   /* There is no valid C++ program where a non-template type is
11210      followed by a "<".  That usually indicates that the user thought
11211      that the type was a template.  */
11212   if (type && type != error_mark_node)
11213     {
11214       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11215          If it is, then the '<'...'>' enclose protocol names rather than
11216          template arguments, and so everything is fine.  */
11217       if (c_dialect_objc ()
11218           && (objc_is_id (type) || objc_is_class_name (type)))
11219         {
11220           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11221           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11222
11223           /* Clobber the "unqualified" type previously entered into
11224              DECL_SPECS with the new, improved protocol-qualified version.  */
11225           if (decl_specs)
11226             decl_specs->type = qual_type;
11227
11228           return qual_type;
11229         }
11230
11231       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11232                                                token->location);
11233     }
11234
11235   return type;
11236 }
11237
11238 /* Parse a type-name.
11239
11240    type-name:
11241      class-name
11242      enum-name
11243      typedef-name
11244
11245    enum-name:
11246      identifier
11247
11248    typedef-name:
11249      identifier
11250
11251    Returns a TYPE_DECL for the type.  */
11252
11253 static tree
11254 cp_parser_type_name (cp_parser* parser)
11255 {
11256   tree type_decl;
11257
11258   /* We can't know yet whether it is a class-name or not.  */
11259   cp_parser_parse_tentatively (parser);
11260   /* Try a class-name.  */
11261   type_decl = cp_parser_class_name (parser,
11262                                     /*typename_keyword_p=*/false,
11263                                     /*template_keyword_p=*/false,
11264                                     none_type,
11265                                     /*check_dependency_p=*/true,
11266                                     /*class_head_p=*/false,
11267                                     /*is_declaration=*/false);
11268   /* If it's not a class-name, keep looking.  */
11269   if (!cp_parser_parse_definitely (parser))
11270     {
11271       /* It must be a typedef-name or an enum-name.  */
11272       return cp_parser_nonclass_name (parser);
11273     }
11274
11275   return type_decl;
11276 }
11277
11278 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11279
11280    enum-name:
11281      identifier
11282
11283    typedef-name:
11284      identifier
11285
11286    Returns a TYPE_DECL for the type.  */
11287
11288 static tree
11289 cp_parser_nonclass_name (cp_parser* parser)
11290 {
11291   tree type_decl;
11292   tree identifier;
11293
11294   cp_token *token = cp_lexer_peek_token (parser->lexer);
11295   identifier = cp_parser_identifier (parser);
11296   if (identifier == error_mark_node)
11297     return error_mark_node;
11298
11299   /* Look up the type-name.  */
11300   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11301
11302   if (TREE_CODE (type_decl) != TYPE_DECL
11303       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11304     {
11305       /* See if this is an Objective-C type.  */
11306       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11307       tree type = objc_get_protocol_qualified_type (identifier, protos);
11308       if (type)
11309         type_decl = TYPE_NAME (type);
11310     }
11311   
11312   /* Issue an error if we did not find a type-name.  */
11313   if (TREE_CODE (type_decl) != TYPE_DECL)
11314     {
11315       if (!cp_parser_simulate_error (parser))
11316         cp_parser_name_lookup_error (parser, identifier, type_decl,
11317                                      "is not a type", token->location);
11318       return error_mark_node;
11319     }
11320   /* Remember that the name was used in the definition of the
11321      current class so that we can check later to see if the
11322      meaning would have been different after the class was
11323      entirely defined.  */
11324   else if (type_decl != error_mark_node
11325            && !parser->scope)
11326     maybe_note_name_used_in_class (identifier, type_decl);
11327   
11328   return type_decl;
11329 }
11330
11331 /* Parse an elaborated-type-specifier.  Note that the grammar given
11332    here incorporates the resolution to DR68.
11333
11334    elaborated-type-specifier:
11335      class-key :: [opt] nested-name-specifier [opt] identifier
11336      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11337      enum-key :: [opt] nested-name-specifier [opt] identifier
11338      typename :: [opt] nested-name-specifier identifier
11339      typename :: [opt] nested-name-specifier template [opt]
11340        template-id
11341
11342    GNU extension:
11343
11344    elaborated-type-specifier:
11345      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11346      class-key attributes :: [opt] nested-name-specifier [opt]
11347                template [opt] template-id
11348      enum attributes :: [opt] nested-name-specifier [opt] identifier
11349
11350    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11351    declared `friend'.  If IS_DECLARATION is TRUE, then this
11352    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11353    something is being declared.
11354
11355    Returns the TYPE specified.  */
11356
11357 static tree
11358 cp_parser_elaborated_type_specifier (cp_parser* parser,
11359                                      bool is_friend,
11360                                      bool is_declaration)
11361 {
11362   enum tag_types tag_type;
11363   tree identifier;
11364   tree type = NULL_TREE;
11365   tree attributes = NULL_TREE;
11366   cp_token *token = NULL;
11367
11368   /* See if we're looking at the `enum' keyword.  */
11369   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11370     {
11371       /* Consume the `enum' token.  */
11372       cp_lexer_consume_token (parser->lexer);
11373       /* Remember that it's an enumeration type.  */
11374       tag_type = enum_type;
11375       /* Parse the optional `struct' or `class' key (for C++0x scoped
11376          enums).  */
11377       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11378           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11379         {
11380           if (cxx_dialect == cxx98)
11381             maybe_warn_cpp0x ("scoped enums");
11382
11383           /* Consume the `struct' or `class'.  */
11384           cp_lexer_consume_token (parser->lexer);
11385         }
11386       /* Parse the attributes.  */
11387       attributes = cp_parser_attributes_opt (parser);
11388     }
11389   /* Or, it might be `typename'.  */
11390   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11391                                            RID_TYPENAME))
11392     {
11393       /* Consume the `typename' token.  */
11394       cp_lexer_consume_token (parser->lexer);
11395       /* Remember that it's a `typename' type.  */
11396       tag_type = typename_type;
11397       /* The `typename' keyword is only allowed in templates.  */
11398       if (!processing_template_decl)
11399         permerror (input_location, "using %<typename%> outside of template");
11400     }
11401   /* Otherwise it must be a class-key.  */
11402   else
11403     {
11404       tag_type = cp_parser_class_key (parser);
11405       if (tag_type == none_type)
11406         return error_mark_node;
11407       /* Parse the attributes.  */
11408       attributes = cp_parser_attributes_opt (parser);
11409     }
11410
11411   /* Look for the `::' operator.  */
11412   cp_parser_global_scope_opt (parser,
11413                               /*current_scope_valid_p=*/false);
11414   /* Look for the nested-name-specifier.  */
11415   if (tag_type == typename_type)
11416     {
11417       if (!cp_parser_nested_name_specifier (parser,
11418                                            /*typename_keyword_p=*/true,
11419                                            /*check_dependency_p=*/true,
11420                                            /*type_p=*/true,
11421                                             is_declaration))
11422         return error_mark_node;
11423     }
11424   else
11425     /* Even though `typename' is not present, the proposed resolution
11426        to Core Issue 180 says that in `class A<T>::B', `B' should be
11427        considered a type-name, even if `A<T>' is dependent.  */
11428     cp_parser_nested_name_specifier_opt (parser,
11429                                          /*typename_keyword_p=*/true,
11430                                          /*check_dependency_p=*/true,
11431                                          /*type_p=*/true,
11432                                          is_declaration);
11433  /* For everything but enumeration types, consider a template-id.
11434     For an enumeration type, consider only a plain identifier.  */
11435   if (tag_type != enum_type)
11436     {
11437       bool template_p = false;
11438       tree decl;
11439
11440       /* Allow the `template' keyword.  */
11441       template_p = cp_parser_optional_template_keyword (parser);
11442       /* If we didn't see `template', we don't know if there's a
11443          template-id or not.  */
11444       if (!template_p)
11445         cp_parser_parse_tentatively (parser);
11446       /* Parse the template-id.  */
11447       token = cp_lexer_peek_token (parser->lexer);
11448       decl = cp_parser_template_id (parser, template_p,
11449                                     /*check_dependency_p=*/true,
11450                                     is_declaration);
11451       /* If we didn't find a template-id, look for an ordinary
11452          identifier.  */
11453       if (!template_p && !cp_parser_parse_definitely (parser))
11454         ;
11455       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11456          in effect, then we must assume that, upon instantiation, the
11457          template will correspond to a class.  */
11458       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11459                && tag_type == typename_type)
11460         type = make_typename_type (parser->scope, decl,
11461                                    typename_type,
11462                                    /*complain=*/tf_error);
11463       else
11464         type = TREE_TYPE (decl);
11465     }
11466
11467   if (!type)
11468     {
11469       token = cp_lexer_peek_token (parser->lexer);
11470       identifier = cp_parser_identifier (parser);
11471
11472       if (identifier == error_mark_node)
11473         {
11474           parser->scope = NULL_TREE;
11475           return error_mark_node;
11476         }
11477
11478       /* For a `typename', we needn't call xref_tag.  */
11479       if (tag_type == typename_type
11480           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11481         return cp_parser_make_typename_type (parser, parser->scope,
11482                                              identifier,
11483                                              token->location);
11484       /* Look up a qualified name in the usual way.  */
11485       if (parser->scope)
11486         {
11487           tree decl;
11488           tree ambiguous_decls;
11489
11490           decl = cp_parser_lookup_name (parser, identifier,
11491                                         tag_type,
11492                                         /*is_template=*/false,
11493                                         /*is_namespace=*/false,
11494                                         /*check_dependency=*/true,
11495                                         &ambiguous_decls,
11496                                         token->location);
11497
11498           /* If the lookup was ambiguous, an error will already have been
11499              issued.  */
11500           if (ambiguous_decls)
11501             return error_mark_node;
11502
11503           /* If we are parsing friend declaration, DECL may be a
11504              TEMPLATE_DECL tree node here.  However, we need to check
11505              whether this TEMPLATE_DECL results in valid code.  Consider
11506              the following example:
11507
11508                namespace N {
11509                  template <class T> class C {};
11510                }
11511                class X {
11512                  template <class T> friend class N::C; // #1, valid code
11513                };
11514                template <class T> class Y {
11515                  friend class N::C;                    // #2, invalid code
11516                };
11517
11518              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11519              name lookup of `N::C'.  We see that friend declaration must
11520              be template for the code to be valid.  Note that
11521              processing_template_decl does not work here since it is
11522              always 1 for the above two cases.  */
11523
11524           decl = (cp_parser_maybe_treat_template_as_class
11525                   (decl, /*tag_name_p=*/is_friend
11526                          && parser->num_template_parameter_lists));
11527
11528           if (TREE_CODE (decl) != TYPE_DECL)
11529             {
11530               cp_parser_diagnose_invalid_type_name (parser,
11531                                                     parser->scope,
11532                                                     identifier,
11533                                                     token->location);
11534               return error_mark_node;
11535             }
11536
11537           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11538             {
11539               bool allow_template = (parser->num_template_parameter_lists
11540                                       || DECL_SELF_REFERENCE_P (decl));
11541               type = check_elaborated_type_specifier (tag_type, decl, 
11542                                                       allow_template);
11543
11544               if (type == error_mark_node)
11545                 return error_mark_node;
11546             }
11547
11548           /* Forward declarations of nested types, such as
11549
11550                class C1::C2;
11551                class C1::C2::C3;
11552
11553              are invalid unless all components preceding the final '::'
11554              are complete.  If all enclosing types are complete, these
11555              declarations become merely pointless.
11556
11557              Invalid forward declarations of nested types are errors
11558              caught elsewhere in parsing.  Those that are pointless arrive
11559              here.  */
11560
11561           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11562               && !is_friend && !processing_explicit_instantiation)
11563             warning (0, "declaration %qD does not declare anything", decl);
11564
11565           type = TREE_TYPE (decl);
11566         }
11567       else
11568         {
11569           /* An elaborated-type-specifier sometimes introduces a new type and
11570              sometimes names an existing type.  Normally, the rule is that it
11571              introduces a new type only if there is not an existing type of
11572              the same name already in scope.  For example, given:
11573
11574                struct S {};
11575                void f() { struct S s; }
11576
11577              the `struct S' in the body of `f' is the same `struct S' as in
11578              the global scope; the existing definition is used.  However, if
11579              there were no global declaration, this would introduce a new
11580              local class named `S'.
11581
11582              An exception to this rule applies to the following code:
11583
11584                namespace N { struct S; }
11585
11586              Here, the elaborated-type-specifier names a new type
11587              unconditionally; even if there is already an `S' in the
11588              containing scope this declaration names a new type.
11589              This exception only applies if the elaborated-type-specifier
11590              forms the complete declaration:
11591
11592                [class.name]
11593
11594                A declaration consisting solely of `class-key identifier ;' is
11595                either a redeclaration of the name in the current scope or a
11596                forward declaration of the identifier as a class name.  It
11597                introduces the name into the current scope.
11598
11599              We are in this situation precisely when the next token is a `;'.
11600
11601              An exception to the exception is that a `friend' declaration does
11602              *not* name a new type; i.e., given:
11603
11604                struct S { friend struct T; };
11605
11606              `T' is not a new type in the scope of `S'.
11607
11608              Also, `new struct S' or `sizeof (struct S)' never results in the
11609              definition of a new type; a new type can only be declared in a
11610              declaration context.  */
11611
11612           tag_scope ts;
11613           bool template_p;
11614
11615           if (is_friend)
11616             /* Friends have special name lookup rules.  */
11617             ts = ts_within_enclosing_non_class;
11618           else if (is_declaration
11619                    && cp_lexer_next_token_is (parser->lexer,
11620                                               CPP_SEMICOLON))
11621             /* This is a `class-key identifier ;' */
11622             ts = ts_current;
11623           else
11624             ts = ts_global;
11625
11626           template_p =
11627             (parser->num_template_parameter_lists
11628              && (cp_parser_next_token_starts_class_definition_p (parser)
11629                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11630           /* An unqualified name was used to reference this type, so
11631              there were no qualifying templates.  */
11632           if (!cp_parser_check_template_parameters (parser,
11633                                                     /*num_templates=*/0,
11634                                                     token->location))
11635             return error_mark_node;
11636           type = xref_tag (tag_type, identifier, ts, template_p);
11637         }
11638     }
11639
11640   if (type == error_mark_node)
11641     return error_mark_node;
11642
11643   /* Allow attributes on forward declarations of classes.  */
11644   if (attributes)
11645     {
11646       if (TREE_CODE (type) == TYPENAME_TYPE)
11647         warning (OPT_Wattributes,
11648                  "attributes ignored on uninstantiated type");
11649       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11650                && ! processing_explicit_instantiation)
11651         warning (OPT_Wattributes,
11652                  "attributes ignored on template instantiation");
11653       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11654         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11655       else
11656         warning (OPT_Wattributes,
11657                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11658     }
11659
11660   if (tag_type != enum_type)
11661     cp_parser_check_class_key (tag_type, type);
11662
11663   /* A "<" cannot follow an elaborated type specifier.  If that
11664      happens, the user was probably trying to form a template-id.  */
11665   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11666
11667   return type;
11668 }
11669
11670 /* Parse an enum-specifier.
11671
11672    enum-specifier:
11673      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11674
11675    enum-key:
11676      enum
11677      enum class   [C++0x]
11678      enum struct  [C++0x]
11679
11680    enum-base:   [C++0x]
11681      : type-specifier-seq
11682
11683    GNU Extensions:
11684      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11685        { enumerator-list [opt] }attributes[opt]
11686
11687    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11688    if the token stream isn't an enum-specifier after all.  */
11689
11690 static tree
11691 cp_parser_enum_specifier (cp_parser* parser)
11692 {
11693   tree identifier;
11694   tree type;
11695   tree attributes;
11696   bool scoped_enum_p = false;
11697   tree underlying_type = NULL_TREE;
11698
11699   /* Parse tentatively so that we can back up if we don't find a
11700      enum-specifier.  */
11701   cp_parser_parse_tentatively (parser);
11702
11703   /* Caller guarantees that the current token is 'enum', an identifier
11704      possibly follows, and the token after that is an opening brace.
11705      If we don't have an identifier, fabricate an anonymous name for
11706      the enumeration being defined.  */
11707   cp_lexer_consume_token (parser->lexer);
11708
11709   /* Parse the "class" or "struct", which indicates a scoped
11710      enumeration type in C++0x.  */
11711   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11712       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11713     {
11714       if (cxx_dialect == cxx98)
11715         maybe_warn_cpp0x ("scoped enums");
11716
11717       /* Consume the `struct' or `class' token.  */
11718       cp_lexer_consume_token (parser->lexer);
11719
11720       scoped_enum_p = true;
11721     }
11722       
11723   attributes = cp_parser_attributes_opt (parser);
11724
11725   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11726     identifier = cp_parser_identifier (parser);
11727   else
11728     identifier = make_anon_name ();
11729
11730   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11731   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11732     {
11733       cp_decl_specifier_seq type_specifiers;
11734
11735       if (cxx_dialect == cxx98)
11736         maybe_warn_cpp0x ("scoped enums");
11737
11738       /* Consume the `:'.  */
11739       cp_lexer_consume_token (parser->lexer);
11740
11741       /* Parse the type-specifier-seq.  */
11742       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11743                                     &type_specifiers);
11744       if (type_specifiers.type == error_mark_node)
11745         return error_mark_node;
11746      
11747       /* If that didn't work, stop.  */
11748       if (type_specifiers.type != error_mark_node)
11749         {
11750           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11751                                             /*initialized=*/0, NULL);
11752           if (underlying_type == error_mark_node)
11753             underlying_type = NULL_TREE;
11754         }
11755       else
11756         cp_parser_error (parser, "expected underlying type of enumeration");
11757     }
11758
11759   /* Look for the `{' but don't consume it yet.  */
11760   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11761     cp_parser_simulate_error (parser);
11762
11763   if (!cp_parser_parse_definitely (parser))
11764     return NULL_TREE;
11765
11766   /* Issue an error message if type-definitions are forbidden here.  */
11767   if (!cp_parser_check_type_definition (parser))
11768     type = error_mark_node;
11769   else
11770     /* Create the new type.  We do this before consuming the opening
11771        brace so the enum will be recorded as being on the line of its
11772        tag (or the 'enum' keyword, if there is no tag).  */
11773     type = start_enum (identifier, underlying_type, scoped_enum_p);
11774   
11775   /* Consume the opening brace.  */
11776   cp_lexer_consume_token (parser->lexer);
11777
11778   if (type == error_mark_node)
11779     {
11780       cp_parser_skip_to_end_of_block_or_statement (parser);
11781       return error_mark_node;
11782     }
11783
11784   /* If the next token is not '}', then there are some enumerators.  */
11785   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11786     cp_parser_enumerator_list (parser, type);
11787
11788   /* Consume the final '}'.  */
11789   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11790
11791   /* Look for trailing attributes to apply to this enumeration, and
11792      apply them if appropriate.  */
11793   if (cp_parser_allow_gnu_extensions_p (parser))
11794     {
11795       tree trailing_attr = cp_parser_attributes_opt (parser);
11796       cplus_decl_attributes (&type,
11797                              trailing_attr,
11798                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11799     }
11800
11801   /* Finish up the enumeration.  */
11802   finish_enum (type);
11803
11804   return type;
11805 }
11806
11807 /* Parse an enumerator-list.  The enumerators all have the indicated
11808    TYPE.
11809
11810    enumerator-list:
11811      enumerator-definition
11812      enumerator-list , enumerator-definition  */
11813
11814 static void
11815 cp_parser_enumerator_list (cp_parser* parser, tree type)
11816 {
11817   while (true)
11818     {
11819       /* Parse an enumerator-definition.  */
11820       cp_parser_enumerator_definition (parser, type);
11821
11822       /* If the next token is not a ',', we've reached the end of
11823          the list.  */
11824       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11825         break;
11826       /* Otherwise, consume the `,' and keep going.  */
11827       cp_lexer_consume_token (parser->lexer);
11828       /* If the next token is a `}', there is a trailing comma.  */
11829       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11830         {
11831           if (!in_system_header)
11832             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11833           break;
11834         }
11835     }
11836 }
11837
11838 /* Parse an enumerator-definition.  The enumerator has the indicated
11839    TYPE.
11840
11841    enumerator-definition:
11842      enumerator
11843      enumerator = constant-expression
11844
11845    enumerator:
11846      identifier  */
11847
11848 static void
11849 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11850 {
11851   tree identifier;
11852   tree value;
11853
11854   /* Look for the identifier.  */
11855   identifier = cp_parser_identifier (parser);
11856   if (identifier == error_mark_node)
11857     return;
11858
11859   /* If the next token is an '=', then there is an explicit value.  */
11860   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11861     {
11862       /* Consume the `=' token.  */
11863       cp_lexer_consume_token (parser->lexer);
11864       /* Parse the value.  */
11865       value = cp_parser_constant_expression (parser,
11866                                              /*allow_non_constant_p=*/false,
11867                                              NULL);
11868     }
11869   else
11870     value = NULL_TREE;
11871
11872   /* Create the enumerator.  */
11873   build_enumerator (identifier, value, type);
11874 }
11875
11876 /* Parse a namespace-name.
11877
11878    namespace-name:
11879      original-namespace-name
11880      namespace-alias
11881
11882    Returns the NAMESPACE_DECL for the namespace.  */
11883
11884 static tree
11885 cp_parser_namespace_name (cp_parser* parser)
11886 {
11887   tree identifier;
11888   tree namespace_decl;
11889
11890   cp_token *token = cp_lexer_peek_token (parser->lexer);
11891
11892   /* Get the name of the namespace.  */
11893   identifier = cp_parser_identifier (parser);
11894   if (identifier == error_mark_node)
11895     return error_mark_node;
11896
11897   /* Look up the identifier in the currently active scope.  Look only
11898      for namespaces, due to:
11899
11900        [basic.lookup.udir]
11901
11902        When looking up a namespace-name in a using-directive or alias
11903        definition, only namespace names are considered.
11904
11905      And:
11906
11907        [basic.lookup.qual]
11908
11909        During the lookup of a name preceding the :: scope resolution
11910        operator, object, function, and enumerator names are ignored.
11911
11912      (Note that cp_parser_qualifying_entity only calls this
11913      function if the token after the name is the scope resolution
11914      operator.)  */
11915   namespace_decl = cp_parser_lookup_name (parser, identifier,
11916                                           none_type,
11917                                           /*is_template=*/false,
11918                                           /*is_namespace=*/true,
11919                                           /*check_dependency=*/true,
11920                                           /*ambiguous_decls=*/NULL,
11921                                           token->location);
11922   /* If it's not a namespace, issue an error.  */
11923   if (namespace_decl == error_mark_node
11924       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11925     {
11926       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11927         error ("%H%qD is not a namespace-name", &token->location, identifier);
11928       cp_parser_error (parser, "expected namespace-name");
11929       namespace_decl = error_mark_node;
11930     }
11931
11932   return namespace_decl;
11933 }
11934
11935 /* Parse a namespace-definition.
11936
11937    namespace-definition:
11938      named-namespace-definition
11939      unnamed-namespace-definition
11940
11941    named-namespace-definition:
11942      original-namespace-definition
11943      extension-namespace-definition
11944
11945    original-namespace-definition:
11946      namespace identifier { namespace-body }
11947
11948    extension-namespace-definition:
11949      namespace original-namespace-name { namespace-body }
11950
11951    unnamed-namespace-definition:
11952      namespace { namespace-body } */
11953
11954 static void
11955 cp_parser_namespace_definition (cp_parser* parser)
11956 {
11957   tree identifier, attribs;
11958   bool has_visibility;
11959   bool is_inline;
11960
11961   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11962     {
11963       is_inline = true;
11964       cp_lexer_consume_token (parser->lexer);
11965     }
11966   else
11967     is_inline = false;
11968
11969   /* Look for the `namespace' keyword.  */
11970   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11971
11972   /* Get the name of the namespace.  We do not attempt to distinguish
11973      between an original-namespace-definition and an
11974      extension-namespace-definition at this point.  The semantic
11975      analysis routines are responsible for that.  */
11976   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11977     identifier = cp_parser_identifier (parser);
11978   else
11979     identifier = NULL_TREE;
11980
11981   /* Parse any specified attributes.  */
11982   attribs = cp_parser_attributes_opt (parser);
11983
11984   /* Look for the `{' to start the namespace.  */
11985   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11986   /* Start the namespace.  */
11987   push_namespace (identifier);
11988
11989   /* "inline namespace" is equivalent to a stub namespace definition
11990      followed by a strong using directive.  */
11991   if (is_inline)
11992     {
11993       tree name_space = current_namespace;
11994       /* Set up namespace association.  */
11995       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11996         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11997                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11998       /* Import the contents of the inline namespace.  */
11999       pop_namespace ();
12000       do_using_directive (name_space);
12001       push_namespace (identifier);
12002     }
12003
12004   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12005
12006   /* Parse the body of the namespace.  */
12007   cp_parser_namespace_body (parser);
12008
12009 #ifdef HANDLE_PRAGMA_VISIBILITY
12010   if (has_visibility)
12011     pop_visibility ();
12012 #endif
12013
12014   /* Finish the namespace.  */
12015   pop_namespace ();
12016   /* Look for the final `}'.  */
12017   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12018 }
12019
12020 /* Parse a namespace-body.
12021
12022    namespace-body:
12023      declaration-seq [opt]  */
12024
12025 static void
12026 cp_parser_namespace_body (cp_parser* parser)
12027 {
12028   cp_parser_declaration_seq_opt (parser);
12029 }
12030
12031 /* Parse a namespace-alias-definition.
12032
12033    namespace-alias-definition:
12034      namespace identifier = qualified-namespace-specifier ;  */
12035
12036 static void
12037 cp_parser_namespace_alias_definition (cp_parser* parser)
12038 {
12039   tree identifier;
12040   tree namespace_specifier;
12041
12042   cp_token *token = cp_lexer_peek_token (parser->lexer);
12043
12044   /* Look for the `namespace' keyword.  */
12045   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12046   /* Look for the identifier.  */
12047   identifier = cp_parser_identifier (parser);
12048   if (identifier == error_mark_node)
12049     return;
12050   /* Look for the `=' token.  */
12051   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12052       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12053     {
12054       error ("%H%<namespace%> definition is not allowed here", &token->location);
12055       /* Skip the definition.  */
12056       cp_lexer_consume_token (parser->lexer);
12057       if (cp_parser_skip_to_closing_brace (parser))
12058         cp_lexer_consume_token (parser->lexer);
12059       return;
12060     }
12061   cp_parser_require (parser, CPP_EQ, "%<=%>");
12062   /* Look for the qualified-namespace-specifier.  */
12063   namespace_specifier
12064     = cp_parser_qualified_namespace_specifier (parser);
12065   /* Look for the `;' token.  */
12066   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12067
12068   /* Register the alias in the symbol table.  */
12069   do_namespace_alias (identifier, namespace_specifier);
12070 }
12071
12072 /* Parse a qualified-namespace-specifier.
12073
12074    qualified-namespace-specifier:
12075      :: [opt] nested-name-specifier [opt] namespace-name
12076
12077    Returns a NAMESPACE_DECL corresponding to the specified
12078    namespace.  */
12079
12080 static tree
12081 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12082 {
12083   /* Look for the optional `::'.  */
12084   cp_parser_global_scope_opt (parser,
12085                               /*current_scope_valid_p=*/false);
12086
12087   /* Look for the optional nested-name-specifier.  */
12088   cp_parser_nested_name_specifier_opt (parser,
12089                                        /*typename_keyword_p=*/false,
12090                                        /*check_dependency_p=*/true,
12091                                        /*type_p=*/false,
12092                                        /*is_declaration=*/true);
12093
12094   return cp_parser_namespace_name (parser);
12095 }
12096
12097 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12098    access declaration.
12099
12100    using-declaration:
12101      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12102      using :: unqualified-id ;  
12103
12104    access-declaration:
12105      qualified-id ;  
12106
12107    */
12108
12109 static bool
12110 cp_parser_using_declaration (cp_parser* parser, 
12111                              bool access_declaration_p)
12112 {
12113   cp_token *token;
12114   bool typename_p = false;
12115   bool global_scope_p;
12116   tree decl;
12117   tree identifier;
12118   tree qscope;
12119
12120   if (access_declaration_p)
12121     cp_parser_parse_tentatively (parser);
12122   else
12123     {
12124       /* Look for the `using' keyword.  */
12125       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12126       
12127       /* Peek at the next token.  */
12128       token = cp_lexer_peek_token (parser->lexer);
12129       /* See if it's `typename'.  */
12130       if (token->keyword == RID_TYPENAME)
12131         {
12132           /* Remember that we've seen it.  */
12133           typename_p = true;
12134           /* Consume the `typename' token.  */
12135           cp_lexer_consume_token (parser->lexer);
12136         }
12137     }
12138
12139   /* Look for the optional global scope qualification.  */
12140   global_scope_p
12141     = (cp_parser_global_scope_opt (parser,
12142                                    /*current_scope_valid_p=*/false)
12143        != NULL_TREE);
12144
12145   /* If we saw `typename', or didn't see `::', then there must be a
12146      nested-name-specifier present.  */
12147   if (typename_p || !global_scope_p)
12148     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12149                                               /*check_dependency_p=*/true,
12150                                               /*type_p=*/false,
12151                                               /*is_declaration=*/true);
12152   /* Otherwise, we could be in either of the two productions.  In that
12153      case, treat the nested-name-specifier as optional.  */
12154   else
12155     qscope = cp_parser_nested_name_specifier_opt (parser,
12156                                                   /*typename_keyword_p=*/false,
12157                                                   /*check_dependency_p=*/true,
12158                                                   /*type_p=*/false,
12159                                                   /*is_declaration=*/true);
12160   if (!qscope)
12161     qscope = global_namespace;
12162
12163   if (access_declaration_p && cp_parser_error_occurred (parser))
12164     /* Something has already gone wrong; there's no need to parse
12165        further.  Since an error has occurred, the return value of
12166        cp_parser_parse_definitely will be false, as required.  */
12167     return cp_parser_parse_definitely (parser);
12168
12169   token = cp_lexer_peek_token (parser->lexer);
12170   /* Parse the unqualified-id.  */
12171   identifier = cp_parser_unqualified_id (parser,
12172                                          /*template_keyword_p=*/false,
12173                                          /*check_dependency_p=*/true,
12174                                          /*declarator_p=*/true,
12175                                          /*optional_p=*/false);
12176
12177   if (access_declaration_p)
12178     {
12179       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12180         cp_parser_simulate_error (parser);
12181       if (!cp_parser_parse_definitely (parser))
12182         return false;
12183     }
12184
12185   /* The function we call to handle a using-declaration is different
12186      depending on what scope we are in.  */
12187   if (qscope == error_mark_node || identifier == error_mark_node)
12188     ;
12189   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12190            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12191     /* [namespace.udecl]
12192
12193        A using declaration shall not name a template-id.  */
12194     error ("%Ha template-id may not appear in a using-declaration",
12195             &token->location);
12196   else
12197     {
12198       if (at_class_scope_p ())
12199         {
12200           /* Create the USING_DECL.  */
12201           decl = do_class_using_decl (parser->scope, identifier);
12202
12203           if (check_for_bare_parameter_packs (decl))
12204             return false;
12205           else
12206             /* Add it to the list of members in this class.  */
12207             finish_member_declaration (decl);
12208         }
12209       else
12210         {
12211           decl = cp_parser_lookup_name_simple (parser,
12212                                                identifier,
12213                                                token->location);
12214           if (decl == error_mark_node)
12215             cp_parser_name_lookup_error (parser, identifier,
12216                                          decl, NULL,
12217                                          token->location);
12218           else if (check_for_bare_parameter_packs (decl))
12219             return false;
12220           else if (!at_namespace_scope_p ())
12221             do_local_using_decl (decl, qscope, identifier);
12222           else
12223             do_toplevel_using_decl (decl, qscope, identifier);
12224         }
12225     }
12226
12227   /* Look for the final `;'.  */
12228   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12229   
12230   return true;
12231 }
12232
12233 /* Parse a using-directive.
12234
12235    using-directive:
12236      using namespace :: [opt] nested-name-specifier [opt]
12237        namespace-name ;  */
12238
12239 static void
12240 cp_parser_using_directive (cp_parser* parser)
12241 {
12242   tree namespace_decl;
12243   tree attribs;
12244
12245   /* Look for the `using' keyword.  */
12246   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12247   /* And the `namespace' keyword.  */
12248   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12249   /* Look for the optional `::' operator.  */
12250   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12251   /* And the optional nested-name-specifier.  */
12252   cp_parser_nested_name_specifier_opt (parser,
12253                                        /*typename_keyword_p=*/false,
12254                                        /*check_dependency_p=*/true,
12255                                        /*type_p=*/false,
12256                                        /*is_declaration=*/true);
12257   /* Get the namespace being used.  */
12258   namespace_decl = cp_parser_namespace_name (parser);
12259   /* And any specified attributes.  */
12260   attribs = cp_parser_attributes_opt (parser);
12261   /* Update the symbol table.  */
12262   parse_using_directive (namespace_decl, attribs);
12263   /* Look for the final `;'.  */
12264   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12265 }
12266
12267 /* Parse an asm-definition.
12268
12269    asm-definition:
12270      asm ( string-literal ) ;
12271
12272    GNU Extension:
12273
12274    asm-definition:
12275      asm volatile [opt] ( string-literal ) ;
12276      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12277      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12278                           : asm-operand-list [opt] ) ;
12279      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12280                           : asm-operand-list [opt]
12281                           : asm-operand-list [opt] ) ;  */
12282
12283 static void
12284 cp_parser_asm_definition (cp_parser* parser)
12285 {
12286   tree string;
12287   tree outputs = NULL_TREE;
12288   tree inputs = NULL_TREE;
12289   tree clobbers = NULL_TREE;
12290   tree asm_stmt;
12291   bool volatile_p = false;
12292   bool extended_p = false;
12293   bool invalid_inputs_p = false;
12294   bool invalid_outputs_p = false;
12295
12296   /* Look for the `asm' keyword.  */
12297   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12298   /* See if the next token is `volatile'.  */
12299   if (cp_parser_allow_gnu_extensions_p (parser)
12300       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12301     {
12302       /* Remember that we saw the `volatile' keyword.  */
12303       volatile_p = true;
12304       /* Consume the token.  */
12305       cp_lexer_consume_token (parser->lexer);
12306     }
12307   /* Look for the opening `('.  */
12308   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12309     return;
12310   /* Look for the string.  */
12311   string = cp_parser_string_literal (parser, false, false);
12312   if (string == error_mark_node)
12313     {
12314       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12315                                              /*consume_paren=*/true);
12316       return;
12317     }
12318
12319   /* If we're allowing GNU extensions, check for the extended assembly
12320      syntax.  Unfortunately, the `:' tokens need not be separated by
12321      a space in C, and so, for compatibility, we tolerate that here
12322      too.  Doing that means that we have to treat the `::' operator as
12323      two `:' tokens.  */
12324   if (cp_parser_allow_gnu_extensions_p (parser)
12325       && parser->in_function_body
12326       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12327           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12328     {
12329       bool inputs_p = false;
12330       bool clobbers_p = false;
12331
12332       /* The extended syntax was used.  */
12333       extended_p = true;
12334
12335       /* Look for outputs.  */
12336       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12337         {
12338           /* Consume the `:'.  */
12339           cp_lexer_consume_token (parser->lexer);
12340           /* Parse the output-operands.  */
12341           if (cp_lexer_next_token_is_not (parser->lexer,
12342                                           CPP_COLON)
12343               && cp_lexer_next_token_is_not (parser->lexer,
12344                                              CPP_SCOPE)
12345               && cp_lexer_next_token_is_not (parser->lexer,
12346                                              CPP_CLOSE_PAREN))
12347             outputs = cp_parser_asm_operand_list (parser);
12348
12349             if (outputs == error_mark_node)
12350               invalid_outputs_p = true;
12351         }
12352       /* If the next token is `::', there are no outputs, and the
12353          next token is the beginning of the inputs.  */
12354       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12355         /* The inputs are coming next.  */
12356         inputs_p = true;
12357
12358       /* Look for inputs.  */
12359       if (inputs_p
12360           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12361         {
12362           /* Consume the `:' or `::'.  */
12363           cp_lexer_consume_token (parser->lexer);
12364           /* Parse the output-operands.  */
12365           if (cp_lexer_next_token_is_not (parser->lexer,
12366                                           CPP_COLON)
12367               && cp_lexer_next_token_is_not (parser->lexer,
12368                                              CPP_CLOSE_PAREN))
12369             inputs = cp_parser_asm_operand_list (parser);
12370
12371             if (inputs == error_mark_node)
12372               invalid_inputs_p = true;
12373         }
12374       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12375         /* The clobbers are coming next.  */
12376         clobbers_p = true;
12377
12378       /* Look for clobbers.  */
12379       if (clobbers_p
12380           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12381         {
12382           /* Consume the `:' or `::'.  */
12383           cp_lexer_consume_token (parser->lexer);
12384           /* Parse the clobbers.  */
12385           if (cp_lexer_next_token_is_not (parser->lexer,
12386                                           CPP_CLOSE_PAREN))
12387             clobbers = cp_parser_asm_clobber_list (parser);
12388         }
12389     }
12390   /* Look for the closing `)'.  */
12391   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12392     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12393                                            /*consume_paren=*/true);
12394   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12395
12396   if (!invalid_inputs_p && !invalid_outputs_p)
12397     {
12398       /* Create the ASM_EXPR.  */
12399       if (parser->in_function_body)
12400         {
12401           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12402                                       inputs, clobbers);
12403           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12404           if (!extended_p)
12405             {
12406               tree temp = asm_stmt;
12407               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12408                 temp = TREE_OPERAND (temp, 0);
12409
12410               ASM_INPUT_P (temp) = 1;
12411             }
12412         }
12413       else
12414         cgraph_add_asm_node (string);
12415     }
12416 }
12417
12418 /* Declarators [gram.dcl.decl] */
12419
12420 /* Parse an init-declarator.
12421
12422    init-declarator:
12423      declarator initializer [opt]
12424
12425    GNU Extension:
12426
12427    init-declarator:
12428      declarator asm-specification [opt] attributes [opt] initializer [opt]
12429
12430    function-definition:
12431      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12432        function-body
12433      decl-specifier-seq [opt] declarator function-try-block
12434
12435    GNU Extension:
12436
12437    function-definition:
12438      __extension__ function-definition
12439
12440    The DECL_SPECIFIERS apply to this declarator.  Returns a
12441    representation of the entity declared.  If MEMBER_P is TRUE, then
12442    this declarator appears in a class scope.  The new DECL created by
12443    this declarator is returned.
12444
12445    The CHECKS are access checks that should be performed once we know
12446    what entity is being declared (and, therefore, what classes have
12447    befriended it).
12448
12449    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12450    for a function-definition here as well.  If the declarator is a
12451    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12452    be TRUE upon return.  By that point, the function-definition will
12453    have been completely parsed.
12454
12455    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12456    is FALSE.  */
12457
12458 static tree
12459 cp_parser_init_declarator (cp_parser* parser,
12460                            cp_decl_specifier_seq *decl_specifiers,
12461                            VEC (deferred_access_check,gc)* checks,
12462                            bool function_definition_allowed_p,
12463                            bool member_p,
12464                            int declares_class_or_enum,
12465                            bool* function_definition_p)
12466 {
12467   cp_token *token = NULL, *asm_spec_start_token = NULL,
12468            *attributes_start_token = NULL;
12469   cp_declarator *declarator;
12470   tree prefix_attributes;
12471   tree attributes;
12472   tree asm_specification;
12473   tree initializer;
12474   tree decl = NULL_TREE;
12475   tree scope;
12476   int is_initialized;
12477   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12478      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12479      "(...)".  */
12480   enum cpp_ttype initialization_kind;
12481   bool is_direct_init = false;
12482   bool is_non_constant_init;
12483   int ctor_dtor_or_conv_p;
12484   bool friend_p;
12485   tree pushed_scope = NULL;
12486
12487   /* Gather the attributes that were provided with the
12488      decl-specifiers.  */
12489   prefix_attributes = decl_specifiers->attributes;
12490
12491   /* Assume that this is not the declarator for a function
12492      definition.  */
12493   if (function_definition_p)
12494     *function_definition_p = false;
12495
12496   /* Defer access checks while parsing the declarator; we cannot know
12497      what names are accessible until we know what is being
12498      declared.  */
12499   resume_deferring_access_checks ();
12500
12501   /* Parse the declarator.  */
12502   token = cp_lexer_peek_token (parser->lexer);
12503   declarator
12504     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12505                             &ctor_dtor_or_conv_p,
12506                             /*parenthesized_p=*/NULL,
12507                             /*member_p=*/false);
12508   /* Gather up the deferred checks.  */
12509   stop_deferring_access_checks ();
12510
12511   /* If the DECLARATOR was erroneous, there's no need to go
12512      further.  */
12513   if (declarator == cp_error_declarator)
12514     return error_mark_node;
12515
12516   /* Check that the number of template-parameter-lists is OK.  */
12517   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12518                                                        token->location))
12519     return error_mark_node;
12520
12521   if (declares_class_or_enum & 2)
12522     cp_parser_check_for_definition_in_return_type (declarator,
12523                                                    decl_specifiers->type,
12524                                                    decl_specifiers->type_location);
12525
12526   /* Figure out what scope the entity declared by the DECLARATOR is
12527      located in.  `grokdeclarator' sometimes changes the scope, so
12528      we compute it now.  */
12529   scope = get_scope_of_declarator (declarator);
12530
12531   /* If we're allowing GNU extensions, look for an asm-specification
12532      and attributes.  */
12533   if (cp_parser_allow_gnu_extensions_p (parser))
12534     {
12535       /* Look for an asm-specification.  */
12536       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12537       asm_specification = cp_parser_asm_specification_opt (parser);
12538       /* And attributes.  */
12539       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12540       attributes = cp_parser_attributes_opt (parser);
12541     }
12542   else
12543     {
12544       asm_specification = NULL_TREE;
12545       attributes = NULL_TREE;
12546     }
12547
12548   /* Peek at the next token.  */
12549   token = cp_lexer_peek_token (parser->lexer);
12550   /* Check to see if the token indicates the start of a
12551      function-definition.  */
12552   if (function_declarator_p (declarator)
12553       && cp_parser_token_starts_function_definition_p (token))
12554     {
12555       if (!function_definition_allowed_p)
12556         {
12557           /* If a function-definition should not appear here, issue an
12558              error message.  */
12559           cp_parser_error (parser,
12560                            "a function-definition is not allowed here");
12561           return error_mark_node;
12562         }
12563       else
12564         {
12565           /* Neither attributes nor an asm-specification are allowed
12566              on a function-definition.  */
12567           if (asm_specification)
12568             error ("%Han asm-specification is not allowed "
12569                    "on a function-definition",
12570                    &asm_spec_start_token->location);
12571           if (attributes)
12572             error ("%Hattributes are not allowed on a function-definition",
12573                    &attributes_start_token->location);
12574           /* This is a function-definition.  */
12575           *function_definition_p = true;
12576
12577           /* Parse the function definition.  */
12578           if (member_p)
12579             decl = cp_parser_save_member_function_body (parser,
12580                                                         decl_specifiers,
12581                                                         declarator,
12582                                                         prefix_attributes);
12583           else
12584             decl
12585               = (cp_parser_function_definition_from_specifiers_and_declarator
12586                  (parser, decl_specifiers, prefix_attributes, declarator));
12587
12588           return decl;
12589         }
12590     }
12591
12592   /* [dcl.dcl]
12593
12594      Only in function declarations for constructors, destructors, and
12595      type conversions can the decl-specifier-seq be omitted.
12596
12597      We explicitly postpone this check past the point where we handle
12598      function-definitions because we tolerate function-definitions
12599      that are missing their return types in some modes.  */
12600   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12601     {
12602       cp_parser_error (parser,
12603                        "expected constructor, destructor, or type conversion");
12604       return error_mark_node;
12605     }
12606
12607   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12608   if (token->type == CPP_EQ
12609       || token->type == CPP_OPEN_PAREN
12610       || token->type == CPP_OPEN_BRACE)
12611     {
12612       is_initialized = 1;
12613       initialization_kind = token->type;
12614
12615       if (token->type == CPP_EQ
12616           && function_declarator_p (declarator))
12617         {
12618           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12619           if (t2->keyword == RID_DEFAULT)
12620             is_initialized = 2;
12621           else if (t2->keyword == RID_DELETE)
12622             is_initialized = 3;
12623         }
12624     }
12625   else
12626     {
12627       /* If the init-declarator isn't initialized and isn't followed by a
12628          `,' or `;', it's not a valid init-declarator.  */
12629       if (token->type != CPP_COMMA
12630           && token->type != CPP_SEMICOLON)
12631         {
12632           cp_parser_error (parser, "expected initializer");
12633           return error_mark_node;
12634         }
12635       is_initialized = 0;
12636       initialization_kind = CPP_EOF;
12637     }
12638
12639   /* Because start_decl has side-effects, we should only call it if we
12640      know we're going ahead.  By this point, we know that we cannot
12641      possibly be looking at any other construct.  */
12642   cp_parser_commit_to_tentative_parse (parser);
12643
12644   /* If the decl specifiers were bad, issue an error now that we're
12645      sure this was intended to be a declarator.  Then continue
12646      declaring the variable(s), as int, to try to cut down on further
12647      errors.  */
12648   if (decl_specifiers->any_specifiers_p
12649       && decl_specifiers->type == error_mark_node)
12650     {
12651       cp_parser_error (parser, "invalid type in declaration");
12652       decl_specifiers->type = integer_type_node;
12653     }
12654
12655   /* Check to see whether or not this declaration is a friend.  */
12656   friend_p = cp_parser_friend_p (decl_specifiers);
12657
12658   /* Enter the newly declared entry in the symbol table.  If we're
12659      processing a declaration in a class-specifier, we wait until
12660      after processing the initializer.  */
12661   if (!member_p)
12662     {
12663       if (parser->in_unbraced_linkage_specification_p)
12664         decl_specifiers->storage_class = sc_extern;
12665       decl = start_decl (declarator, decl_specifiers,
12666                          is_initialized, attributes, prefix_attributes,
12667                          &pushed_scope);
12668     }
12669   else if (scope)
12670     /* Enter the SCOPE.  That way unqualified names appearing in the
12671        initializer will be looked up in SCOPE.  */
12672     pushed_scope = push_scope (scope);
12673
12674   /* Perform deferred access control checks, now that we know in which
12675      SCOPE the declared entity resides.  */
12676   if (!member_p && decl)
12677     {
12678       tree saved_current_function_decl = NULL_TREE;
12679
12680       /* If the entity being declared is a function, pretend that we
12681          are in its scope.  If it is a `friend', it may have access to
12682          things that would not otherwise be accessible.  */
12683       if (TREE_CODE (decl) == FUNCTION_DECL)
12684         {
12685           saved_current_function_decl = current_function_decl;
12686           current_function_decl = decl;
12687         }
12688
12689       /* Perform access checks for template parameters.  */
12690       cp_parser_perform_template_parameter_access_checks (checks);
12691
12692       /* Perform the access control checks for the declarator and the
12693          decl-specifiers.  */
12694       perform_deferred_access_checks ();
12695
12696       /* Restore the saved value.  */
12697       if (TREE_CODE (decl) == FUNCTION_DECL)
12698         current_function_decl = saved_current_function_decl;
12699     }
12700
12701   /* Parse the initializer.  */
12702   initializer = NULL_TREE;
12703   is_direct_init = false;
12704   is_non_constant_init = true;
12705   if (is_initialized)
12706     {
12707       if (function_declarator_p (declarator))
12708         {
12709           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12710            if (initialization_kind == CPP_EQ)
12711              initializer = cp_parser_pure_specifier (parser);
12712            else
12713              {
12714                /* If the declaration was erroneous, we don't really
12715                   know what the user intended, so just silently
12716                   consume the initializer.  */
12717                if (decl != error_mark_node)
12718                  error ("%Hinitializer provided for function",
12719                         &initializer_start_token->location);
12720                cp_parser_skip_to_closing_parenthesis (parser,
12721                                                       /*recovering=*/true,
12722                                                       /*or_comma=*/false,
12723                                                       /*consume_paren=*/true);
12724              }
12725         }
12726       else
12727         initializer = cp_parser_initializer (parser,
12728                                              &is_direct_init,
12729                                              &is_non_constant_init);
12730     }
12731
12732   /* The old parser allows attributes to appear after a parenthesized
12733      initializer.  Mark Mitchell proposed removing this functionality
12734      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12735      attributes -- but ignores them.  */
12736   if (cp_parser_allow_gnu_extensions_p (parser)
12737       && initialization_kind == CPP_OPEN_PAREN)
12738     if (cp_parser_attributes_opt (parser))
12739       warning (OPT_Wattributes,
12740                "attributes after parenthesized initializer ignored");
12741
12742   /* For an in-class declaration, use `grokfield' to create the
12743      declaration.  */
12744   if (member_p)
12745     {
12746       if (pushed_scope)
12747         {
12748           pop_scope (pushed_scope);
12749           pushed_scope = false;
12750         }
12751       decl = grokfield (declarator, decl_specifiers,
12752                         initializer, !is_non_constant_init,
12753                         /*asmspec=*/NULL_TREE,
12754                         prefix_attributes);
12755       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12756         cp_parser_save_default_args (parser, decl);
12757     }
12758
12759   /* Finish processing the declaration.  But, skip friend
12760      declarations.  */
12761   if (!friend_p && decl && decl != error_mark_node)
12762     {
12763       cp_finish_decl (decl,
12764                       initializer, !is_non_constant_init,
12765                       asm_specification,
12766                       /* If the initializer is in parentheses, then this is
12767                          a direct-initialization, which means that an
12768                          `explicit' constructor is OK.  Otherwise, an
12769                          `explicit' constructor cannot be used.  */
12770                       ((is_direct_init || !is_initialized)
12771                        ? 0 : LOOKUP_ONLYCONVERTING));
12772     }
12773   else if ((cxx_dialect != cxx98) && friend_p
12774            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12775     /* Core issue #226 (C++0x only): A default template-argument
12776        shall not be specified in a friend class template
12777        declaration. */
12778     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12779                              /*is_partial=*/0, /*is_friend_decl=*/1);
12780
12781   if (!friend_p && pushed_scope)
12782     pop_scope (pushed_scope);
12783
12784   return decl;
12785 }
12786
12787 /* Parse a declarator.
12788
12789    declarator:
12790      direct-declarator
12791      ptr-operator declarator
12792
12793    abstract-declarator:
12794      ptr-operator abstract-declarator [opt]
12795      direct-abstract-declarator
12796
12797    GNU Extensions:
12798
12799    declarator:
12800      attributes [opt] direct-declarator
12801      attributes [opt] ptr-operator declarator
12802
12803    abstract-declarator:
12804      attributes [opt] ptr-operator abstract-declarator [opt]
12805      attributes [opt] direct-abstract-declarator
12806
12807    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12808    detect constructor, destructor or conversion operators. It is set
12809    to -1 if the declarator is a name, and +1 if it is a
12810    function. Otherwise it is set to zero. Usually you just want to
12811    test for >0, but internally the negative value is used.
12812
12813    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12814    a decl-specifier-seq unless it declares a constructor, destructor,
12815    or conversion.  It might seem that we could check this condition in
12816    semantic analysis, rather than parsing, but that makes it difficult
12817    to handle something like `f()'.  We want to notice that there are
12818    no decl-specifiers, and therefore realize that this is an
12819    expression, not a declaration.)
12820
12821    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12822    the declarator is a direct-declarator of the form "(...)".
12823
12824    MEMBER_P is true iff this declarator is a member-declarator.  */
12825
12826 static cp_declarator *
12827 cp_parser_declarator (cp_parser* parser,
12828                       cp_parser_declarator_kind dcl_kind,
12829                       int* ctor_dtor_or_conv_p,
12830                       bool* parenthesized_p,
12831                       bool member_p)
12832 {
12833   cp_token *token;
12834   cp_declarator *declarator;
12835   enum tree_code code;
12836   cp_cv_quals cv_quals;
12837   tree class_type;
12838   tree attributes = NULL_TREE;
12839
12840   /* Assume this is not a constructor, destructor, or type-conversion
12841      operator.  */
12842   if (ctor_dtor_or_conv_p)
12843     *ctor_dtor_or_conv_p = 0;
12844
12845   if (cp_parser_allow_gnu_extensions_p (parser))
12846     attributes = cp_parser_attributes_opt (parser);
12847
12848   /* Peek at the next token.  */
12849   token = cp_lexer_peek_token (parser->lexer);
12850
12851   /* Check for the ptr-operator production.  */
12852   cp_parser_parse_tentatively (parser);
12853   /* Parse the ptr-operator.  */
12854   code = cp_parser_ptr_operator (parser,
12855                                  &class_type,
12856                                  &cv_quals);
12857   /* If that worked, then we have a ptr-operator.  */
12858   if (cp_parser_parse_definitely (parser))
12859     {
12860       /* If a ptr-operator was found, then this declarator was not
12861          parenthesized.  */
12862       if (parenthesized_p)
12863         *parenthesized_p = true;
12864       /* The dependent declarator is optional if we are parsing an
12865          abstract-declarator.  */
12866       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12867         cp_parser_parse_tentatively (parser);
12868
12869       /* Parse the dependent declarator.  */
12870       declarator = cp_parser_declarator (parser, dcl_kind,
12871                                          /*ctor_dtor_or_conv_p=*/NULL,
12872                                          /*parenthesized_p=*/NULL,
12873                                          /*member_p=*/false);
12874
12875       /* If we are parsing an abstract-declarator, we must handle the
12876          case where the dependent declarator is absent.  */
12877       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12878           && !cp_parser_parse_definitely (parser))
12879         declarator = NULL;
12880
12881       declarator = cp_parser_make_indirect_declarator
12882         (code, class_type, cv_quals, declarator);
12883     }
12884   /* Everything else is a direct-declarator.  */
12885   else
12886     {
12887       if (parenthesized_p)
12888         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12889                                                    CPP_OPEN_PAREN);
12890       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12891                                                 ctor_dtor_or_conv_p,
12892                                                 member_p);
12893     }
12894
12895   if (attributes && declarator && declarator != cp_error_declarator)
12896     declarator->attributes = attributes;
12897
12898   return declarator;
12899 }
12900
12901 /* Parse a direct-declarator or direct-abstract-declarator.
12902
12903    direct-declarator:
12904      declarator-id
12905      direct-declarator ( parameter-declaration-clause )
12906        cv-qualifier-seq [opt]
12907        exception-specification [opt]
12908      direct-declarator [ constant-expression [opt] ]
12909      ( declarator )
12910
12911    direct-abstract-declarator:
12912      direct-abstract-declarator [opt]
12913        ( parameter-declaration-clause )
12914        cv-qualifier-seq [opt]
12915        exception-specification [opt]
12916      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12917      ( abstract-declarator )
12918
12919    Returns a representation of the declarator.  DCL_KIND is
12920    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12921    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12922    we are parsing a direct-declarator.  It is
12923    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12924    of ambiguity we prefer an abstract declarator, as per
12925    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12926    cp_parser_declarator.  */
12927
12928 static cp_declarator *
12929 cp_parser_direct_declarator (cp_parser* parser,
12930                              cp_parser_declarator_kind dcl_kind,
12931                              int* ctor_dtor_or_conv_p,
12932                              bool member_p)
12933 {
12934   cp_token *token;
12935   cp_declarator *declarator = NULL;
12936   tree scope = NULL_TREE;
12937   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12938   bool saved_in_declarator_p = parser->in_declarator_p;
12939   bool first = true;
12940   tree pushed_scope = NULL_TREE;
12941
12942   while (true)
12943     {
12944       /* Peek at the next token.  */
12945       token = cp_lexer_peek_token (parser->lexer);
12946       if (token->type == CPP_OPEN_PAREN)
12947         {
12948           /* This is either a parameter-declaration-clause, or a
12949              parenthesized declarator. When we know we are parsing a
12950              named declarator, it must be a parenthesized declarator
12951              if FIRST is true. For instance, `(int)' is a
12952              parameter-declaration-clause, with an omitted
12953              direct-abstract-declarator. But `((*))', is a
12954              parenthesized abstract declarator. Finally, when T is a
12955              template parameter `(T)' is a
12956              parameter-declaration-clause, and not a parenthesized
12957              named declarator.
12958
12959              We first try and parse a parameter-declaration-clause,
12960              and then try a nested declarator (if FIRST is true).
12961
12962              It is not an error for it not to be a
12963              parameter-declaration-clause, even when FIRST is
12964              false. Consider,
12965
12966                int i (int);
12967                int i (3);
12968
12969              The first is the declaration of a function while the
12970              second is the definition of a variable, including its
12971              initializer.
12972
12973              Having seen only the parenthesis, we cannot know which of
12974              these two alternatives should be selected.  Even more
12975              complex are examples like:
12976
12977                int i (int (a));
12978                int i (int (3));
12979
12980              The former is a function-declaration; the latter is a
12981              variable initialization.
12982
12983              Thus again, we try a parameter-declaration-clause, and if
12984              that fails, we back out and return.  */
12985
12986           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12987             {
12988               cp_parameter_declarator *params;
12989               unsigned saved_num_template_parameter_lists;
12990
12991               /* In a member-declarator, the only valid interpretation
12992                  of a parenthesis is the start of a
12993                  parameter-declaration-clause.  (It is invalid to
12994                  initialize a static data member with a parenthesized
12995                  initializer; only the "=" form of initialization is
12996                  permitted.)  */
12997               if (!member_p)
12998                 cp_parser_parse_tentatively (parser);
12999
13000               /* Consume the `('.  */
13001               cp_lexer_consume_token (parser->lexer);
13002               if (first)
13003                 {
13004                   /* If this is going to be an abstract declarator, we're
13005                      in a declarator and we can't have default args.  */
13006                   parser->default_arg_ok_p = false;
13007                   parser->in_declarator_p = true;
13008                 }
13009
13010               /* Inside the function parameter list, surrounding
13011                  template-parameter-lists do not apply.  */
13012               saved_num_template_parameter_lists
13013                 = parser->num_template_parameter_lists;
13014               parser->num_template_parameter_lists = 0;
13015
13016               /* Parse the parameter-declaration-clause.  */
13017               params = cp_parser_parameter_declaration_clause (parser);
13018
13019               parser->num_template_parameter_lists
13020                 = saved_num_template_parameter_lists;
13021
13022               /* If all went well, parse the cv-qualifier-seq and the
13023                  exception-specification.  */
13024               if (member_p || cp_parser_parse_definitely (parser))
13025                 {
13026                   cp_cv_quals cv_quals;
13027                   tree exception_specification;
13028                   tree late_return;
13029
13030                   if (ctor_dtor_or_conv_p)
13031                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13032                   first = false;
13033                   /* Consume the `)'.  */
13034                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13035
13036                   /* Parse the cv-qualifier-seq.  */
13037                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13038                   /* And the exception-specification.  */
13039                   exception_specification
13040                     = cp_parser_exception_specification_opt (parser);
13041
13042                   late_return
13043                     = cp_parser_late_return_type_opt (parser);
13044
13045                   /* Create the function-declarator.  */
13046                   declarator = make_call_declarator (declarator,
13047                                                      params,
13048                                                      cv_quals,
13049                                                      exception_specification,
13050                                                      late_return);
13051                   /* Any subsequent parameter lists are to do with
13052                      return type, so are not those of the declared
13053                      function.  */
13054                   parser->default_arg_ok_p = false;
13055
13056                   /* Repeat the main loop.  */
13057                   continue;
13058                 }
13059             }
13060
13061           /* If this is the first, we can try a parenthesized
13062              declarator.  */
13063           if (first)
13064             {
13065               bool saved_in_type_id_in_expr_p;
13066
13067               parser->default_arg_ok_p = saved_default_arg_ok_p;
13068               parser->in_declarator_p = saved_in_declarator_p;
13069
13070               /* Consume the `('.  */
13071               cp_lexer_consume_token (parser->lexer);
13072               /* Parse the nested declarator.  */
13073               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13074               parser->in_type_id_in_expr_p = true;
13075               declarator
13076                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13077                                         /*parenthesized_p=*/NULL,
13078                                         member_p);
13079               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13080               first = false;
13081               /* Expect a `)'.  */
13082               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13083                 declarator = cp_error_declarator;
13084               if (declarator == cp_error_declarator)
13085                 break;
13086
13087               goto handle_declarator;
13088             }
13089           /* Otherwise, we must be done.  */
13090           else
13091             break;
13092         }
13093       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13094                && token->type == CPP_OPEN_SQUARE)
13095         {
13096           /* Parse an array-declarator.  */
13097           tree bounds;
13098
13099           if (ctor_dtor_or_conv_p)
13100             *ctor_dtor_or_conv_p = 0;
13101
13102           first = false;
13103           parser->default_arg_ok_p = false;
13104           parser->in_declarator_p = true;
13105           /* Consume the `['.  */
13106           cp_lexer_consume_token (parser->lexer);
13107           /* Peek at the next token.  */
13108           token = cp_lexer_peek_token (parser->lexer);
13109           /* If the next token is `]', then there is no
13110              constant-expression.  */
13111           if (token->type != CPP_CLOSE_SQUARE)
13112             {
13113               bool non_constant_p;
13114
13115               bounds
13116                 = cp_parser_constant_expression (parser,
13117                                                  /*allow_non_constant=*/true,
13118                                                  &non_constant_p);
13119               if (!non_constant_p)
13120                 bounds = fold_non_dependent_expr (bounds);
13121               /* Normally, the array bound must be an integral constant
13122                  expression.  However, as an extension, we allow VLAs
13123                  in function scopes.  */
13124               else if (!parser->in_function_body)
13125                 {
13126                   error ("%Harray bound is not an integer constant",
13127                          &token->location);
13128                   bounds = error_mark_node;
13129                 }
13130             }
13131           else
13132             bounds = NULL_TREE;
13133           /* Look for the closing `]'.  */
13134           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13135             {
13136               declarator = cp_error_declarator;
13137               break;
13138             }
13139
13140           declarator = make_array_declarator (declarator, bounds);
13141         }
13142       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13143         {
13144           tree qualifying_scope;
13145           tree unqualified_name;
13146           special_function_kind sfk;
13147           bool abstract_ok;
13148           bool pack_expansion_p = false;
13149           cp_token *declarator_id_start_token;
13150
13151           /* Parse a declarator-id */
13152           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13153           if (abstract_ok)
13154             {
13155               cp_parser_parse_tentatively (parser);
13156
13157               /* If we see an ellipsis, we should be looking at a
13158                  parameter pack. */
13159               if (token->type == CPP_ELLIPSIS)
13160                 {
13161                   /* Consume the `...' */
13162                   cp_lexer_consume_token (parser->lexer);
13163
13164                   pack_expansion_p = true;
13165                 }
13166             }
13167
13168           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13169           unqualified_name
13170             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13171           qualifying_scope = parser->scope;
13172           if (abstract_ok)
13173             {
13174               bool okay = false;
13175
13176               if (!unqualified_name && pack_expansion_p)
13177                 {
13178                   /* Check whether an error occurred. */
13179                   okay = !cp_parser_error_occurred (parser);
13180
13181                   /* We already consumed the ellipsis to mark a
13182                      parameter pack, but we have no way to report it,
13183                      so abort the tentative parse. We will be exiting
13184                      immediately anyway. */
13185                   cp_parser_abort_tentative_parse (parser);
13186                 }
13187               else
13188                 okay = cp_parser_parse_definitely (parser);
13189
13190               if (!okay)
13191                 unqualified_name = error_mark_node;
13192               else if (unqualified_name
13193                        && (qualifying_scope
13194                            || (TREE_CODE (unqualified_name)
13195                                != IDENTIFIER_NODE)))
13196                 {
13197                   cp_parser_error (parser, "expected unqualified-id");
13198                   unqualified_name = error_mark_node;
13199                 }
13200             }
13201
13202           if (!unqualified_name)
13203             return NULL;
13204           if (unqualified_name == error_mark_node)
13205             {
13206               declarator = cp_error_declarator;
13207               pack_expansion_p = false;
13208               declarator->parameter_pack_p = false;
13209               break;
13210             }
13211
13212           if (qualifying_scope && at_namespace_scope_p ()
13213               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13214             {
13215               /* In the declaration of a member of a template class
13216                  outside of the class itself, the SCOPE will sometimes
13217                  be a TYPENAME_TYPE.  For example, given:
13218
13219                  template <typename T>
13220                  int S<T>::R::i = 3;
13221
13222                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13223                  this context, we must resolve S<T>::R to an ordinary
13224                  type, rather than a typename type.
13225
13226                  The reason we normally avoid resolving TYPENAME_TYPEs
13227                  is that a specialization of `S' might render
13228                  `S<T>::R' not a type.  However, if `S' is
13229                  specialized, then this `i' will not be used, so there
13230                  is no harm in resolving the types here.  */
13231               tree type;
13232
13233               /* Resolve the TYPENAME_TYPE.  */
13234               type = resolve_typename_type (qualifying_scope,
13235                                             /*only_current_p=*/false);
13236               /* If that failed, the declarator is invalid.  */
13237               if (TREE_CODE (type) == TYPENAME_TYPE)
13238                 error ("%H%<%T::%E%> is not a type",
13239                        &declarator_id_start_token->location,
13240                        TYPE_CONTEXT (qualifying_scope),
13241                        TYPE_IDENTIFIER (qualifying_scope));
13242               qualifying_scope = type;
13243             }
13244
13245           sfk = sfk_none;
13246
13247           if (unqualified_name)
13248             {
13249               tree class_type;
13250
13251               if (qualifying_scope
13252                   && CLASS_TYPE_P (qualifying_scope))
13253                 class_type = qualifying_scope;
13254               else
13255                 class_type = current_class_type;
13256
13257               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13258                 {
13259                   tree name_type = TREE_TYPE (unqualified_name);
13260                   if (class_type && same_type_p (name_type, class_type))
13261                     {
13262                       if (qualifying_scope
13263                           && CLASSTYPE_USE_TEMPLATE (name_type))
13264                         {
13265                           error ("%Hinvalid use of constructor as a template",
13266                                  &declarator_id_start_token->location);
13267                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13268                                   "name the constructor in a qualified name",
13269                                   class_type,
13270                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13271                                   class_type, name_type);
13272                           declarator = cp_error_declarator;
13273                           break;
13274                         }
13275                       else
13276                         unqualified_name = constructor_name (class_type);
13277                     }
13278                   else
13279                     {
13280                       /* We do not attempt to print the declarator
13281                          here because we do not have enough
13282                          information about its original syntactic
13283                          form.  */
13284                       cp_parser_error (parser, "invalid declarator");
13285                       declarator = cp_error_declarator;
13286                       break;
13287                     }
13288                 }
13289
13290               if (class_type)
13291                 {
13292                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13293                     sfk = sfk_destructor;
13294                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13295                     sfk = sfk_conversion;
13296                   else if (/* There's no way to declare a constructor
13297                               for an anonymous type, even if the type
13298                               got a name for linkage purposes.  */
13299                            !TYPE_WAS_ANONYMOUS (class_type)
13300                            && constructor_name_p (unqualified_name,
13301                                                   class_type))
13302                     {
13303                       unqualified_name = constructor_name (class_type);
13304                       sfk = sfk_constructor;
13305                     }
13306
13307                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13308                     *ctor_dtor_or_conv_p = -1;
13309                 }
13310             }
13311           declarator = make_id_declarator (qualifying_scope,
13312                                            unqualified_name,
13313                                            sfk);
13314           declarator->id_loc = token->location;
13315           declarator->parameter_pack_p = pack_expansion_p;
13316
13317           if (pack_expansion_p)
13318             maybe_warn_variadic_templates ();
13319
13320         handle_declarator:;
13321           scope = get_scope_of_declarator (declarator);
13322           if (scope)
13323             /* Any names that appear after the declarator-id for a
13324                member are looked up in the containing scope.  */
13325             pushed_scope = push_scope (scope);
13326           parser->in_declarator_p = true;
13327           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13328               || (declarator && declarator->kind == cdk_id))
13329             /* Default args are only allowed on function
13330                declarations.  */
13331             parser->default_arg_ok_p = saved_default_arg_ok_p;
13332           else
13333             parser->default_arg_ok_p = false;
13334
13335           first = false;
13336         }
13337       /* We're done.  */
13338       else
13339         break;
13340     }
13341
13342   /* For an abstract declarator, we might wind up with nothing at this
13343      point.  That's an error; the declarator is not optional.  */
13344   if (!declarator)
13345     cp_parser_error (parser, "expected declarator");
13346
13347   /* If we entered a scope, we must exit it now.  */
13348   if (pushed_scope)
13349     pop_scope (pushed_scope);
13350
13351   parser->default_arg_ok_p = saved_default_arg_ok_p;
13352   parser->in_declarator_p = saved_in_declarator_p;
13353
13354   return declarator;
13355 }
13356
13357 /* Parse a ptr-operator.
13358
13359    ptr-operator:
13360      * cv-qualifier-seq [opt]
13361      &
13362      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13363
13364    GNU Extension:
13365
13366    ptr-operator:
13367      & cv-qualifier-seq [opt]
13368
13369    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13370    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13371    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13372    filled in with the TYPE containing the member.  *CV_QUALS is
13373    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13374    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13375    Note that the tree codes returned by this function have nothing
13376    to do with the types of trees that will be eventually be created
13377    to represent the pointer or reference type being parsed. They are
13378    just constants with suggestive names. */
13379 static enum tree_code
13380 cp_parser_ptr_operator (cp_parser* parser,
13381                         tree* type,
13382                         cp_cv_quals *cv_quals)
13383 {
13384   enum tree_code code = ERROR_MARK;
13385   cp_token *token;
13386
13387   /* Assume that it's not a pointer-to-member.  */
13388   *type = NULL_TREE;
13389   /* And that there are no cv-qualifiers.  */
13390   *cv_quals = TYPE_UNQUALIFIED;
13391
13392   /* Peek at the next token.  */
13393   token = cp_lexer_peek_token (parser->lexer);
13394
13395   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13396   if (token->type == CPP_MULT)
13397     code = INDIRECT_REF;
13398   else if (token->type == CPP_AND)
13399     code = ADDR_EXPR;
13400   else if ((cxx_dialect != cxx98) &&
13401            token->type == CPP_AND_AND) /* C++0x only */
13402     code = NON_LVALUE_EXPR;
13403
13404   if (code != ERROR_MARK)
13405     {
13406       /* Consume the `*', `&' or `&&'.  */
13407       cp_lexer_consume_token (parser->lexer);
13408
13409       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13410          `&', if we are allowing GNU extensions.  (The only qualifier
13411          that can legally appear after `&' is `restrict', but that is
13412          enforced during semantic analysis.  */
13413       if (code == INDIRECT_REF
13414           || cp_parser_allow_gnu_extensions_p (parser))
13415         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13416     }
13417   else
13418     {
13419       /* Try the pointer-to-member case.  */
13420       cp_parser_parse_tentatively (parser);
13421       /* Look for the optional `::' operator.  */
13422       cp_parser_global_scope_opt (parser,
13423                                   /*current_scope_valid_p=*/false);
13424       /* Look for the nested-name specifier.  */
13425       token = cp_lexer_peek_token (parser->lexer);
13426       cp_parser_nested_name_specifier (parser,
13427                                        /*typename_keyword_p=*/false,
13428                                        /*check_dependency_p=*/true,
13429                                        /*type_p=*/false,
13430                                        /*is_declaration=*/false);
13431       /* If we found it, and the next token is a `*', then we are
13432          indeed looking at a pointer-to-member operator.  */
13433       if (!cp_parser_error_occurred (parser)
13434           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13435         {
13436           /* Indicate that the `*' operator was used.  */
13437           code = INDIRECT_REF;
13438
13439           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13440             error ("%H%qD is a namespace", &token->location, parser->scope);
13441           else
13442             {
13443               /* The type of which the member is a member is given by the
13444                  current SCOPE.  */
13445               *type = parser->scope;
13446               /* The next name will not be qualified.  */
13447               parser->scope = NULL_TREE;
13448               parser->qualifying_scope = NULL_TREE;
13449               parser->object_scope = NULL_TREE;
13450               /* Look for the optional cv-qualifier-seq.  */
13451               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13452             }
13453         }
13454       /* If that didn't work we don't have a ptr-operator.  */
13455       if (!cp_parser_parse_definitely (parser))
13456         cp_parser_error (parser, "expected ptr-operator");
13457     }
13458
13459   return code;
13460 }
13461
13462 /* Parse an (optional) cv-qualifier-seq.
13463
13464    cv-qualifier-seq:
13465      cv-qualifier cv-qualifier-seq [opt]
13466
13467    cv-qualifier:
13468      const
13469      volatile
13470
13471    GNU Extension:
13472
13473    cv-qualifier:
13474      __restrict__
13475
13476    Returns a bitmask representing the cv-qualifiers.  */
13477
13478 static cp_cv_quals
13479 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13480 {
13481   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13482
13483   while (true)
13484     {
13485       cp_token *token;
13486       cp_cv_quals cv_qualifier;
13487
13488       /* Peek at the next token.  */
13489       token = cp_lexer_peek_token (parser->lexer);
13490       /* See if it's a cv-qualifier.  */
13491       switch (token->keyword)
13492         {
13493         case RID_CONST:
13494           cv_qualifier = TYPE_QUAL_CONST;
13495           break;
13496
13497         case RID_VOLATILE:
13498           cv_qualifier = TYPE_QUAL_VOLATILE;
13499           break;
13500
13501         case RID_RESTRICT:
13502           cv_qualifier = TYPE_QUAL_RESTRICT;
13503           break;
13504
13505         default:
13506           cv_qualifier = TYPE_UNQUALIFIED;
13507           break;
13508         }
13509
13510       if (!cv_qualifier)
13511         break;
13512
13513       if (cv_quals & cv_qualifier)
13514         {
13515           error ("%Hduplicate cv-qualifier", &token->location);
13516           cp_lexer_purge_token (parser->lexer);
13517         }
13518       else
13519         {
13520           cp_lexer_consume_token (parser->lexer);
13521           cv_quals |= cv_qualifier;
13522         }
13523     }
13524
13525   return cv_quals;
13526 }
13527
13528 /* Parse a late-specified return type, if any.  This is not a separate
13529    non-terminal, but part of a function declarator, which looks like
13530
13531    -> type-id
13532
13533    Returns the type indicated by the type-id.  */
13534
13535 static tree
13536 cp_parser_late_return_type_opt (cp_parser* parser)
13537 {
13538   cp_token *token;
13539
13540   /* Peek at the next token.  */
13541   token = cp_lexer_peek_token (parser->lexer);
13542   /* A late-specified return type is indicated by an initial '->'. */
13543   if (token->type != CPP_DEREF)
13544     return NULL_TREE;
13545
13546   /* Consume the ->.  */
13547   cp_lexer_consume_token (parser->lexer);
13548
13549   return cp_parser_type_id (parser);
13550 }
13551
13552 /* Parse a declarator-id.
13553
13554    declarator-id:
13555      id-expression
13556      :: [opt] nested-name-specifier [opt] type-name
13557
13558    In the `id-expression' case, the value returned is as for
13559    cp_parser_id_expression if the id-expression was an unqualified-id.
13560    If the id-expression was a qualified-id, then a SCOPE_REF is
13561    returned.  The first operand is the scope (either a NAMESPACE_DECL
13562    or TREE_TYPE), but the second is still just a representation of an
13563    unqualified-id.  */
13564
13565 static tree
13566 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13567 {
13568   tree id;
13569   /* The expression must be an id-expression.  Assume that qualified
13570      names are the names of types so that:
13571
13572        template <class T>
13573        int S<T>::R::i = 3;
13574
13575      will work; we must treat `S<T>::R' as the name of a type.
13576      Similarly, assume that qualified names are templates, where
13577      required, so that:
13578
13579        template <class T>
13580        int S<T>::R<T>::i = 3;
13581
13582      will work, too.  */
13583   id = cp_parser_id_expression (parser,
13584                                 /*template_keyword_p=*/false,
13585                                 /*check_dependency_p=*/false,
13586                                 /*template_p=*/NULL,
13587                                 /*declarator_p=*/true,
13588                                 optional_p);
13589   if (id && BASELINK_P (id))
13590     id = BASELINK_FUNCTIONS (id);
13591   return id;
13592 }
13593
13594 /* Parse a type-id.
13595
13596    type-id:
13597      type-specifier-seq abstract-declarator [opt]
13598
13599    Returns the TYPE specified.  */
13600
13601 static tree
13602 cp_parser_type_id (cp_parser* parser)
13603 {
13604   cp_decl_specifier_seq type_specifier_seq;
13605   cp_declarator *abstract_declarator;
13606
13607   /* Parse the type-specifier-seq.  */
13608   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13609                                 &type_specifier_seq);
13610   if (type_specifier_seq.type == error_mark_node)
13611     return error_mark_node;
13612
13613   /* There might or might not be an abstract declarator.  */
13614   cp_parser_parse_tentatively (parser);
13615   /* Look for the declarator.  */
13616   abstract_declarator
13617     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13618                             /*parenthesized_p=*/NULL,
13619                             /*member_p=*/false);
13620   /* Check to see if there really was a declarator.  */
13621   if (!cp_parser_parse_definitely (parser))
13622     abstract_declarator = NULL;
13623
13624   return groktypename (&type_specifier_seq, abstract_declarator);
13625 }
13626
13627 /* Parse a type-specifier-seq.
13628
13629    type-specifier-seq:
13630      type-specifier type-specifier-seq [opt]
13631
13632    GNU extension:
13633
13634    type-specifier-seq:
13635      attributes type-specifier-seq [opt]
13636
13637    If IS_CONDITION is true, we are at the start of a "condition",
13638    e.g., we've just seen "if (".
13639
13640    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13641
13642 static void
13643 cp_parser_type_specifier_seq (cp_parser* parser,
13644                               bool is_condition,
13645                               cp_decl_specifier_seq *type_specifier_seq)
13646 {
13647   bool seen_type_specifier = false;
13648   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13649   cp_token *start_token = NULL;
13650
13651   /* Clear the TYPE_SPECIFIER_SEQ.  */
13652   clear_decl_specs (type_specifier_seq);
13653
13654   /* Parse the type-specifiers and attributes.  */
13655   while (true)
13656     {
13657       tree type_specifier;
13658       bool is_cv_qualifier;
13659
13660       /* Check for attributes first.  */
13661       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13662         {
13663           type_specifier_seq->attributes =
13664             chainon (type_specifier_seq->attributes,
13665                      cp_parser_attributes_opt (parser));
13666           continue;
13667         }
13668
13669       /* record the token of the beginning of the type specifier seq,
13670          for error reporting purposes*/
13671      if (!start_token)
13672        start_token = cp_lexer_peek_token (parser->lexer);
13673
13674       /* Look for the type-specifier.  */
13675       type_specifier = cp_parser_type_specifier (parser,
13676                                                  flags,
13677                                                  type_specifier_seq,
13678                                                  /*is_declaration=*/false,
13679                                                  NULL,
13680                                                  &is_cv_qualifier);
13681       if (!type_specifier)
13682         {
13683           /* If the first type-specifier could not be found, this is not a
13684              type-specifier-seq at all.  */
13685           if (!seen_type_specifier)
13686             {
13687               cp_parser_error (parser, "expected type-specifier");
13688               type_specifier_seq->type = error_mark_node;
13689               return;
13690             }
13691           /* If subsequent type-specifiers could not be found, the
13692              type-specifier-seq is complete.  */
13693           break;
13694         }
13695
13696       seen_type_specifier = true;
13697       /* The standard says that a condition can be:
13698
13699             type-specifier-seq declarator = assignment-expression
13700
13701          However, given:
13702
13703            struct S {};
13704            if (int S = ...)
13705
13706          we should treat the "S" as a declarator, not as a
13707          type-specifier.  The standard doesn't say that explicitly for
13708          type-specifier-seq, but it does say that for
13709          decl-specifier-seq in an ordinary declaration.  Perhaps it
13710          would be clearer just to allow a decl-specifier-seq here, and
13711          then add a semantic restriction that if any decl-specifiers
13712          that are not type-specifiers appear, the program is invalid.  */
13713       if (is_condition && !is_cv_qualifier)
13714         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13715     }
13716
13717   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13718 }
13719
13720 /* Parse a parameter-declaration-clause.
13721
13722    parameter-declaration-clause:
13723      parameter-declaration-list [opt] ... [opt]
13724      parameter-declaration-list , ...
13725
13726    Returns a representation for the parameter declarations.  A return
13727    value of NULL indicates a parameter-declaration-clause consisting
13728    only of an ellipsis.  */
13729
13730 static cp_parameter_declarator *
13731 cp_parser_parameter_declaration_clause (cp_parser* parser)
13732 {
13733   cp_parameter_declarator *parameters;
13734   cp_token *token;
13735   bool ellipsis_p;
13736   bool is_error;
13737
13738   /* Peek at the next token.  */
13739   token = cp_lexer_peek_token (parser->lexer);
13740   /* Check for trivial parameter-declaration-clauses.  */
13741   if (token->type == CPP_ELLIPSIS)
13742     {
13743       /* Consume the `...' token.  */
13744       cp_lexer_consume_token (parser->lexer);
13745       return NULL;
13746     }
13747   else if (token->type == CPP_CLOSE_PAREN)
13748     /* There are no parameters.  */
13749     {
13750 #ifndef NO_IMPLICIT_EXTERN_C
13751       if (in_system_header && current_class_type == NULL
13752           && current_lang_name == lang_name_c)
13753         return NULL;
13754       else
13755 #endif
13756         return no_parameters;
13757     }
13758   /* Check for `(void)', too, which is a special case.  */
13759   else if (token->keyword == RID_VOID
13760            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13761                == CPP_CLOSE_PAREN))
13762     {
13763       /* Consume the `void' token.  */
13764       cp_lexer_consume_token (parser->lexer);
13765       /* There are no parameters.  */
13766       return no_parameters;
13767     }
13768
13769   /* Parse the parameter-declaration-list.  */
13770   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13771   /* If a parse error occurred while parsing the
13772      parameter-declaration-list, then the entire
13773      parameter-declaration-clause is erroneous.  */
13774   if (is_error)
13775     return NULL;
13776
13777   /* Peek at the next token.  */
13778   token = cp_lexer_peek_token (parser->lexer);
13779   /* If it's a `,', the clause should terminate with an ellipsis.  */
13780   if (token->type == CPP_COMMA)
13781     {
13782       /* Consume the `,'.  */
13783       cp_lexer_consume_token (parser->lexer);
13784       /* Expect an ellipsis.  */
13785       ellipsis_p
13786         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13787     }
13788   /* It might also be `...' if the optional trailing `,' was
13789      omitted.  */
13790   else if (token->type == CPP_ELLIPSIS)
13791     {
13792       /* Consume the `...' token.  */
13793       cp_lexer_consume_token (parser->lexer);
13794       /* And remember that we saw it.  */
13795       ellipsis_p = true;
13796     }
13797   else
13798     ellipsis_p = false;
13799
13800   /* Finish the parameter list.  */
13801   if (parameters && ellipsis_p)
13802     parameters->ellipsis_p = true;
13803
13804   return parameters;
13805 }
13806
13807 /* Parse a parameter-declaration-list.
13808
13809    parameter-declaration-list:
13810      parameter-declaration
13811      parameter-declaration-list , parameter-declaration
13812
13813    Returns a representation of the parameter-declaration-list, as for
13814    cp_parser_parameter_declaration_clause.  However, the
13815    `void_list_node' is never appended to the list.  Upon return,
13816    *IS_ERROR will be true iff an error occurred.  */
13817
13818 static cp_parameter_declarator *
13819 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13820 {
13821   cp_parameter_declarator *parameters = NULL;
13822   cp_parameter_declarator **tail = &parameters;
13823   bool saved_in_unbraced_linkage_specification_p;
13824
13825   /* Assume all will go well.  */
13826   *is_error = false;
13827   /* The special considerations that apply to a function within an
13828      unbraced linkage specifications do not apply to the parameters
13829      to the function.  */
13830   saved_in_unbraced_linkage_specification_p 
13831     = parser->in_unbraced_linkage_specification_p;
13832   parser->in_unbraced_linkage_specification_p = false;
13833
13834   /* Look for more parameters.  */
13835   while (true)
13836     {
13837       cp_parameter_declarator *parameter;
13838       bool parenthesized_p;
13839       /* Parse the parameter.  */
13840       parameter
13841         = cp_parser_parameter_declaration (parser,
13842                                            /*template_parm_p=*/false,
13843                                            &parenthesized_p);
13844
13845       /* If a parse error occurred parsing the parameter declaration,
13846          then the entire parameter-declaration-list is erroneous.  */
13847       if (!parameter)
13848         {
13849           *is_error = true;
13850           parameters = NULL;
13851           break;
13852         }
13853       /* Add the new parameter to the list.  */
13854       *tail = parameter;
13855       tail = &parameter->next;
13856
13857       /* Peek at the next token.  */
13858       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13859           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13860           /* These are for Objective-C++ */
13861           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13862           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13863         /* The parameter-declaration-list is complete.  */
13864         break;
13865       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13866         {
13867           cp_token *token;
13868
13869           /* Peek at the next token.  */
13870           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13871           /* If it's an ellipsis, then the list is complete.  */
13872           if (token->type == CPP_ELLIPSIS)
13873             break;
13874           /* Otherwise, there must be more parameters.  Consume the
13875              `,'.  */
13876           cp_lexer_consume_token (parser->lexer);
13877           /* When parsing something like:
13878
13879                 int i(float f, double d)
13880
13881              we can tell after seeing the declaration for "f" that we
13882              are not looking at an initialization of a variable "i",
13883              but rather at the declaration of a function "i".
13884
13885              Due to the fact that the parsing of template arguments
13886              (as specified to a template-id) requires backtracking we
13887              cannot use this technique when inside a template argument
13888              list.  */
13889           if (!parser->in_template_argument_list_p
13890               && !parser->in_type_id_in_expr_p
13891               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13892               /* However, a parameter-declaration of the form
13893                  "foat(f)" (which is a valid declaration of a
13894                  parameter "f") can also be interpreted as an
13895                  expression (the conversion of "f" to "float").  */
13896               && !parenthesized_p)
13897             cp_parser_commit_to_tentative_parse (parser);
13898         }
13899       else
13900         {
13901           cp_parser_error (parser, "expected %<,%> or %<...%>");
13902           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13903             cp_parser_skip_to_closing_parenthesis (parser,
13904                                                    /*recovering=*/true,
13905                                                    /*or_comma=*/false,
13906                                                    /*consume_paren=*/false);
13907           break;
13908         }
13909     }
13910
13911   parser->in_unbraced_linkage_specification_p
13912     = saved_in_unbraced_linkage_specification_p;
13913
13914   return parameters;
13915 }
13916
13917 /* Parse a parameter declaration.
13918
13919    parameter-declaration:
13920      decl-specifier-seq ... [opt] declarator
13921      decl-specifier-seq declarator = assignment-expression
13922      decl-specifier-seq ... [opt] abstract-declarator [opt]
13923      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13924
13925    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13926    declares a template parameter.  (In that case, a non-nested `>'
13927    token encountered during the parsing of the assignment-expression
13928    is not interpreted as a greater-than operator.)
13929
13930    Returns a representation of the parameter, or NULL if an error
13931    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13932    true iff the declarator is of the form "(p)".  */
13933
13934 static cp_parameter_declarator *
13935 cp_parser_parameter_declaration (cp_parser *parser,
13936                                  bool template_parm_p,
13937                                  bool *parenthesized_p)
13938 {
13939   int declares_class_or_enum;
13940   bool greater_than_is_operator_p;
13941   cp_decl_specifier_seq decl_specifiers;
13942   cp_declarator *declarator;
13943   tree default_argument;
13944   cp_token *token = NULL, *declarator_token_start = NULL;
13945   const char *saved_message;
13946
13947   /* In a template parameter, `>' is not an operator.
13948
13949      [temp.param]
13950
13951      When parsing a default template-argument for a non-type
13952      template-parameter, the first non-nested `>' is taken as the end
13953      of the template parameter-list rather than a greater-than
13954      operator.  */
13955   greater_than_is_operator_p = !template_parm_p;
13956
13957   /* Type definitions may not appear in parameter types.  */
13958   saved_message = parser->type_definition_forbidden_message;
13959   parser->type_definition_forbidden_message
13960     = "types may not be defined in parameter types";
13961
13962   /* Parse the declaration-specifiers.  */
13963   cp_parser_decl_specifier_seq (parser,
13964                                 CP_PARSER_FLAGS_NONE,
13965                                 &decl_specifiers,
13966                                 &declares_class_or_enum);
13967   /* If an error occurred, there's no reason to attempt to parse the
13968      rest of the declaration.  */
13969   if (cp_parser_error_occurred (parser))
13970     {
13971       parser->type_definition_forbidden_message = saved_message;
13972       return NULL;
13973     }
13974
13975   /* Peek at the next token.  */
13976   token = cp_lexer_peek_token (parser->lexer);
13977
13978   /* If the next token is a `)', `,', `=', `>', or `...', then there
13979      is no declarator. However, when variadic templates are enabled,
13980      there may be a declarator following `...'.  */
13981   if (token->type == CPP_CLOSE_PAREN
13982       || token->type == CPP_COMMA
13983       || token->type == CPP_EQ
13984       || token->type == CPP_GREATER)
13985     {
13986       declarator = NULL;
13987       if (parenthesized_p)
13988         *parenthesized_p = false;
13989     }
13990   /* Otherwise, there should be a declarator.  */
13991   else
13992     {
13993       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13994       parser->default_arg_ok_p = false;
13995
13996       /* After seeing a decl-specifier-seq, if the next token is not a
13997          "(", there is no possibility that the code is a valid
13998          expression.  Therefore, if parsing tentatively, we commit at
13999          this point.  */
14000       if (!parser->in_template_argument_list_p
14001           /* In an expression context, having seen:
14002
14003                (int((char ...
14004
14005              we cannot be sure whether we are looking at a
14006              function-type (taking a "char" as a parameter) or a cast
14007              of some object of type "char" to "int".  */
14008           && !parser->in_type_id_in_expr_p
14009           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14010           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14011         cp_parser_commit_to_tentative_parse (parser);
14012       /* Parse the declarator.  */
14013       declarator_token_start = token;
14014       declarator = cp_parser_declarator (parser,
14015                                          CP_PARSER_DECLARATOR_EITHER,
14016                                          /*ctor_dtor_or_conv_p=*/NULL,
14017                                          parenthesized_p,
14018                                          /*member_p=*/false);
14019       parser->default_arg_ok_p = saved_default_arg_ok_p;
14020       /* After the declarator, allow more attributes.  */
14021       decl_specifiers.attributes
14022         = chainon (decl_specifiers.attributes,
14023                    cp_parser_attributes_opt (parser));
14024     }
14025
14026   /* If the next token is an ellipsis, and we have not seen a
14027      declarator name, and the type of the declarator contains parameter
14028      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14029      a parameter pack expansion expression. Otherwise, leave the
14030      ellipsis for a C-style variadic function. */
14031   token = cp_lexer_peek_token (parser->lexer);
14032   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14033     {
14034       tree type = decl_specifiers.type;
14035
14036       if (type && DECL_P (type))
14037         type = TREE_TYPE (type);
14038
14039       if (type
14040           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14041           && declarator_can_be_parameter_pack (declarator)
14042           && (!declarator || !declarator->parameter_pack_p)
14043           && uses_parameter_packs (type))
14044         {
14045           /* Consume the `...'. */
14046           cp_lexer_consume_token (parser->lexer);
14047           maybe_warn_variadic_templates ();
14048           
14049           /* Build a pack expansion type */
14050           if (declarator)
14051             declarator->parameter_pack_p = true;
14052           else
14053             decl_specifiers.type = make_pack_expansion (type);
14054         }
14055     }
14056
14057   /* The restriction on defining new types applies only to the type
14058      of the parameter, not to the default argument.  */
14059   parser->type_definition_forbidden_message = saved_message;
14060
14061   /* If the next token is `=', then process a default argument.  */
14062   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14063     {
14064       /* Consume the `='.  */
14065       cp_lexer_consume_token (parser->lexer);
14066
14067       /* If we are defining a class, then the tokens that make up the
14068          default argument must be saved and processed later.  */
14069       if (!template_parm_p && at_class_scope_p ()
14070           && TYPE_BEING_DEFINED (current_class_type))
14071         {
14072           unsigned depth = 0;
14073           int maybe_template_id = 0;
14074           cp_token *first_token;
14075           cp_token *token;
14076
14077           /* Add tokens until we have processed the entire default
14078              argument.  We add the range [first_token, token).  */
14079           first_token = cp_lexer_peek_token (parser->lexer);
14080           while (true)
14081             {
14082               bool done = false;
14083
14084               /* Peek at the next token.  */
14085               token = cp_lexer_peek_token (parser->lexer);
14086               /* What we do depends on what token we have.  */
14087               switch (token->type)
14088                 {
14089                   /* In valid code, a default argument must be
14090                      immediately followed by a `,' `)', or `...'.  */
14091                 case CPP_COMMA:
14092                   if (depth == 0 && maybe_template_id)
14093                     {
14094                       /* If we've seen a '<', we might be in a
14095                          template-argument-list.  Until Core issue 325 is
14096                          resolved, we don't know how this situation ought
14097                          to be handled, so try to DTRT.  We check whether
14098                          what comes after the comma is a valid parameter
14099                          declaration list.  If it is, then the comma ends
14100                          the default argument; otherwise the default
14101                          argument continues.  */
14102                       bool error = false;
14103
14104                       /* Set ITALP so cp_parser_parameter_declaration_list
14105                          doesn't decide to commit to this parse.  */
14106                       bool saved_italp = parser->in_template_argument_list_p;
14107                       parser->in_template_argument_list_p = true;
14108
14109                       cp_parser_parse_tentatively (parser);
14110                       cp_lexer_consume_token (parser->lexer);
14111                       cp_parser_parameter_declaration_list (parser, &error);
14112                       if (!cp_parser_error_occurred (parser) && !error)
14113                         done = true;
14114                       cp_parser_abort_tentative_parse (parser);
14115
14116                       parser->in_template_argument_list_p = saved_italp;
14117                       break;
14118                     }
14119                 case CPP_CLOSE_PAREN:
14120                 case CPP_ELLIPSIS:
14121                   /* If we run into a non-nested `;', `}', or `]',
14122                      then the code is invalid -- but the default
14123                      argument is certainly over.  */
14124                 case CPP_SEMICOLON:
14125                 case CPP_CLOSE_BRACE:
14126                 case CPP_CLOSE_SQUARE:
14127                   if (depth == 0)
14128                     done = true;
14129                   /* Update DEPTH, if necessary.  */
14130                   else if (token->type == CPP_CLOSE_PAREN
14131                            || token->type == CPP_CLOSE_BRACE
14132                            || token->type == CPP_CLOSE_SQUARE)
14133                     --depth;
14134                   break;
14135
14136                 case CPP_OPEN_PAREN:
14137                 case CPP_OPEN_SQUARE:
14138                 case CPP_OPEN_BRACE:
14139                   ++depth;
14140                   break;
14141
14142                 case CPP_LESS:
14143                   if (depth == 0)
14144                     /* This might be the comparison operator, or it might
14145                        start a template argument list.  */
14146                     ++maybe_template_id;
14147                   break;
14148
14149                 case CPP_RSHIFT:
14150                   if (cxx_dialect == cxx98)
14151                     break;
14152                   /* Fall through for C++0x, which treats the `>>'
14153                      operator like two `>' tokens in certain
14154                      cases.  */
14155
14156                 case CPP_GREATER:
14157                   if (depth == 0)
14158                     {
14159                       /* This might be an operator, or it might close a
14160                          template argument list.  But if a previous '<'
14161                          started a template argument list, this will have
14162                          closed it, so we can't be in one anymore.  */
14163                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14164                       if (maybe_template_id < 0)
14165                         maybe_template_id = 0;
14166                     }
14167                   break;
14168
14169                   /* If we run out of tokens, issue an error message.  */
14170                 case CPP_EOF:
14171                 case CPP_PRAGMA_EOL:
14172                   error ("%Hfile ends in default argument", &token->location);
14173                   done = true;
14174                   break;
14175
14176                 case CPP_NAME:
14177                 case CPP_SCOPE:
14178                   /* In these cases, we should look for template-ids.
14179                      For example, if the default argument is
14180                      `X<int, double>()', we need to do name lookup to
14181                      figure out whether or not `X' is a template; if
14182                      so, the `,' does not end the default argument.
14183
14184                      That is not yet done.  */
14185                   break;
14186
14187                 default:
14188                   break;
14189                 }
14190
14191               /* If we've reached the end, stop.  */
14192               if (done)
14193                 break;
14194
14195               /* Add the token to the token block.  */
14196               token = cp_lexer_consume_token (parser->lexer);
14197             }
14198
14199           /* Create a DEFAULT_ARG to represent the unparsed default
14200              argument.  */
14201           default_argument = make_node (DEFAULT_ARG);
14202           DEFARG_TOKENS (default_argument)
14203             = cp_token_cache_new (first_token, token);
14204           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14205         }
14206       /* Outside of a class definition, we can just parse the
14207          assignment-expression.  */
14208       else
14209         {
14210           token = cp_lexer_peek_token (parser->lexer);
14211           default_argument 
14212             = cp_parser_default_argument (parser, template_parm_p);
14213         }
14214
14215       if (!parser->default_arg_ok_p)
14216         {
14217           if (flag_permissive)
14218             warning (0, "deprecated use of default argument for parameter of non-function");
14219           else
14220             {
14221               error ("%Hdefault arguments are only "
14222                      "permitted for function parameters",
14223                      &token->location);
14224               default_argument = NULL_TREE;
14225             }
14226         }
14227       else if ((declarator && declarator->parameter_pack_p)
14228                || (decl_specifiers.type
14229                    && PACK_EXPANSION_P (decl_specifiers.type)))
14230         {
14231           const char* kind = template_parm_p? "template " : "";
14232           
14233           /* Find the name of the parameter pack.  */     
14234           cp_declarator *id_declarator = declarator;
14235           while (id_declarator && id_declarator->kind != cdk_id)
14236             id_declarator = id_declarator->declarator;
14237           
14238           if (id_declarator && id_declarator->kind == cdk_id)
14239             error ("%H%sparameter pack %qD cannot have a default argument",
14240                    &declarator_token_start->location,
14241                    kind, id_declarator->u.id.unqualified_name);
14242           else
14243             error ("%H%sparameter pack cannot have a default argument",
14244                    &declarator_token_start->location, kind);
14245           
14246           default_argument = NULL_TREE;
14247         }
14248     }
14249   else
14250     default_argument = NULL_TREE;
14251
14252   return make_parameter_declarator (&decl_specifiers,
14253                                     declarator,
14254                                     default_argument);
14255 }
14256
14257 /* Parse a default argument and return it.
14258
14259    TEMPLATE_PARM_P is true if this is a default argument for a
14260    non-type template parameter.  */
14261 static tree
14262 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14263 {
14264   tree default_argument = NULL_TREE;
14265   bool saved_greater_than_is_operator_p;
14266   bool saved_local_variables_forbidden_p;
14267
14268   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14269      set correctly.  */
14270   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14271   parser->greater_than_is_operator_p = !template_parm_p;
14272   /* Local variable names (and the `this' keyword) may not
14273      appear in a default argument.  */
14274   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14275   parser->local_variables_forbidden_p = true;
14276   /* The default argument expression may cause implicitly
14277      defined member functions to be synthesized, which will
14278      result in garbage collection.  We must treat this
14279      situation as if we were within the body of function so as
14280      to avoid collecting live data on the stack.  */
14281   ++function_depth;
14282   /* Parse the assignment-expression.  */
14283   if (template_parm_p)
14284     push_deferring_access_checks (dk_no_deferred);
14285   default_argument
14286     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14287   if (template_parm_p)
14288     pop_deferring_access_checks ();
14289   /* Restore saved state.  */
14290   --function_depth;
14291   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14292   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14293
14294   return default_argument;
14295 }
14296
14297 /* Parse a function-body.
14298
14299    function-body:
14300      compound_statement  */
14301
14302 static void
14303 cp_parser_function_body (cp_parser *parser)
14304 {
14305   cp_parser_compound_statement (parser, NULL, false);
14306 }
14307
14308 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14309    true if a ctor-initializer was present.  */
14310
14311 static bool
14312 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14313 {
14314   tree body;
14315   bool ctor_initializer_p;
14316
14317   /* Begin the function body.  */
14318   body = begin_function_body ();
14319   /* Parse the optional ctor-initializer.  */
14320   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14321   /* Parse the function-body.  */
14322   cp_parser_function_body (parser);
14323   /* Finish the function body.  */
14324   finish_function_body (body);
14325
14326   return ctor_initializer_p;
14327 }
14328
14329 /* Parse an initializer.
14330
14331    initializer:
14332      = initializer-clause
14333      ( expression-list )
14334
14335    Returns an expression representing the initializer.  If no
14336    initializer is present, NULL_TREE is returned.
14337
14338    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14339    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14340    set to TRUE if there is no initializer present.  If there is an
14341    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14342    is set to true; otherwise it is set to false.  */
14343
14344 static tree
14345 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14346                        bool* non_constant_p)
14347 {
14348   cp_token *token;
14349   tree init;
14350
14351   /* Peek at the next token.  */
14352   token = cp_lexer_peek_token (parser->lexer);
14353
14354   /* Let our caller know whether or not this initializer was
14355      parenthesized.  */
14356   *is_direct_init = (token->type != CPP_EQ);
14357   /* Assume that the initializer is constant.  */
14358   *non_constant_p = false;
14359
14360   if (token->type == CPP_EQ)
14361     {
14362       /* Consume the `='.  */
14363       cp_lexer_consume_token (parser->lexer);
14364       /* Parse the initializer-clause.  */
14365       init = cp_parser_initializer_clause (parser, non_constant_p);
14366     }
14367   else if (token->type == CPP_OPEN_PAREN)
14368     init = cp_parser_parenthesized_expression_list (parser, false,
14369                                                     /*cast_p=*/false,
14370                                                     /*allow_expansion_p=*/true,
14371                                                     non_constant_p);
14372   else if (token->type == CPP_OPEN_BRACE)
14373     {
14374       maybe_warn_cpp0x ("extended initializer lists");
14375       init = cp_parser_braced_list (parser, non_constant_p);
14376       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14377     }
14378   else
14379     {
14380       /* Anything else is an error.  */
14381       cp_parser_error (parser, "expected initializer");
14382       init = error_mark_node;
14383     }
14384
14385   return init;
14386 }
14387
14388 /* Parse an initializer-clause.
14389
14390    initializer-clause:
14391      assignment-expression
14392      braced-init-list
14393
14394    Returns an expression representing the initializer.
14395
14396    If the `assignment-expression' production is used the value
14397    returned is simply a representation for the expression.
14398
14399    Otherwise, calls cp_parser_braced_list.  */
14400
14401 static tree
14402 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14403 {
14404   tree initializer;
14405
14406   /* Assume the expression is constant.  */
14407   *non_constant_p = false;
14408
14409   /* If it is not a `{', then we are looking at an
14410      assignment-expression.  */
14411   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14412     {
14413       initializer
14414         = cp_parser_constant_expression (parser,
14415                                         /*allow_non_constant_p=*/true,
14416                                         non_constant_p);
14417       if (!*non_constant_p)
14418         initializer = fold_non_dependent_expr (initializer);
14419     }
14420   else
14421     initializer = cp_parser_braced_list (parser, non_constant_p);
14422
14423   return initializer;
14424 }
14425
14426 /* Parse a brace-enclosed initializer list.
14427
14428    braced-init-list:
14429      { initializer-list , [opt] }
14430      { }
14431
14432    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14433    the elements of the initializer-list (or NULL, if the last
14434    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14435    NULL_TREE.  There is no way to detect whether or not the optional
14436    trailing `,' was provided.  NON_CONSTANT_P is as for
14437    cp_parser_initializer.  */     
14438
14439 static tree
14440 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14441 {
14442   tree initializer;
14443
14444   /* Consume the `{' token.  */
14445   cp_lexer_consume_token (parser->lexer);
14446   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14447   initializer = make_node (CONSTRUCTOR);
14448   /* If it's not a `}', then there is a non-trivial initializer.  */
14449   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14450     {
14451       /* Parse the initializer list.  */
14452       CONSTRUCTOR_ELTS (initializer)
14453         = cp_parser_initializer_list (parser, non_constant_p);
14454       /* A trailing `,' token is allowed.  */
14455       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14456         cp_lexer_consume_token (parser->lexer);
14457     }
14458   /* Now, there should be a trailing `}'.  */
14459   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14460   TREE_TYPE (initializer) = init_list_type_node;
14461   return initializer;
14462 }
14463
14464 /* Parse an initializer-list.
14465
14466    initializer-list:
14467      initializer-clause ... [opt]
14468      initializer-list , initializer-clause ... [opt]
14469
14470    GNU Extension:
14471
14472    initializer-list:
14473      identifier : initializer-clause
14474      initializer-list, identifier : initializer-clause
14475
14476    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14477    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14478    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14479    as for cp_parser_initializer.  */
14480
14481 static VEC(constructor_elt,gc) *
14482 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14483 {
14484   VEC(constructor_elt,gc) *v = NULL;
14485
14486   /* Assume all of the expressions are constant.  */
14487   *non_constant_p = false;
14488
14489   /* Parse the rest of the list.  */
14490   while (true)
14491     {
14492       cp_token *token;
14493       tree identifier;
14494       tree initializer;
14495       bool clause_non_constant_p;
14496
14497       /* If the next token is an identifier and the following one is a
14498          colon, we are looking at the GNU designated-initializer
14499          syntax.  */
14500       if (cp_parser_allow_gnu_extensions_p (parser)
14501           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14502           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14503         {
14504           /* Warn the user that they are using an extension.  */
14505           pedwarn (input_location, OPT_pedantic, 
14506                    "ISO C++ does not allow designated initializers");
14507           /* Consume the identifier.  */
14508           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14509           /* Consume the `:'.  */
14510           cp_lexer_consume_token (parser->lexer);
14511         }
14512       else
14513         identifier = NULL_TREE;
14514
14515       /* Parse the initializer.  */
14516       initializer = cp_parser_initializer_clause (parser,
14517                                                   &clause_non_constant_p);
14518       /* If any clause is non-constant, so is the entire initializer.  */
14519       if (clause_non_constant_p)
14520         *non_constant_p = true;
14521
14522       /* If we have an ellipsis, this is an initializer pack
14523          expansion.  */
14524       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14525         {
14526           /* Consume the `...'.  */
14527           cp_lexer_consume_token (parser->lexer);
14528
14529           /* Turn the initializer into an initializer expansion.  */
14530           initializer = make_pack_expansion (initializer);
14531         }
14532
14533       /* Add it to the vector.  */
14534       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14535
14536       /* If the next token is not a comma, we have reached the end of
14537          the list.  */
14538       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14539         break;
14540
14541       /* Peek at the next token.  */
14542       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14543       /* If the next token is a `}', then we're still done.  An
14544          initializer-clause can have a trailing `,' after the
14545          initializer-list and before the closing `}'.  */
14546       if (token->type == CPP_CLOSE_BRACE)
14547         break;
14548
14549       /* Consume the `,' token.  */
14550       cp_lexer_consume_token (parser->lexer);
14551     }
14552
14553   return v;
14554 }
14555
14556 /* Classes [gram.class] */
14557
14558 /* Parse a class-name.
14559
14560    class-name:
14561      identifier
14562      template-id
14563
14564    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14565    to indicate that names looked up in dependent types should be
14566    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14567    keyword has been used to indicate that the name that appears next
14568    is a template.  TAG_TYPE indicates the explicit tag given before
14569    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14570    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14571    is the class being defined in a class-head.
14572
14573    Returns the TYPE_DECL representing the class.  */
14574
14575 static tree
14576 cp_parser_class_name (cp_parser *parser,
14577                       bool typename_keyword_p,
14578                       bool template_keyword_p,
14579                       enum tag_types tag_type,
14580                       bool check_dependency_p,
14581                       bool class_head_p,
14582                       bool is_declaration)
14583 {
14584   tree decl;
14585   tree scope;
14586   bool typename_p;
14587   cp_token *token;
14588
14589   /* All class-names start with an identifier.  */
14590   token = cp_lexer_peek_token (parser->lexer);
14591   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14592     {
14593       cp_parser_error (parser, "expected class-name");
14594       return error_mark_node;
14595     }
14596
14597   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14598      to a template-id, so we save it here.  */
14599   scope = parser->scope;
14600   if (scope == error_mark_node)
14601     return error_mark_node;
14602
14603   /* Any name names a type if we're following the `typename' keyword
14604      in a qualified name where the enclosing scope is type-dependent.  */
14605   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14606                 && dependent_type_p (scope));
14607   /* Handle the common case (an identifier, but not a template-id)
14608      efficiently.  */
14609   if (token->type == CPP_NAME
14610       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14611     {
14612       cp_token *identifier_token;
14613       tree identifier;
14614       bool ambiguous_p;
14615
14616       /* Look for the identifier.  */
14617       identifier_token = cp_lexer_peek_token (parser->lexer);
14618       ambiguous_p = identifier_token->ambiguous_p;
14619       identifier = cp_parser_identifier (parser);
14620       /* If the next token isn't an identifier, we are certainly not
14621          looking at a class-name.  */
14622       if (identifier == error_mark_node)
14623         decl = error_mark_node;
14624       /* If we know this is a type-name, there's no need to look it
14625          up.  */
14626       else if (typename_p)
14627         decl = identifier;
14628       else
14629         {
14630           tree ambiguous_decls;
14631           /* If we already know that this lookup is ambiguous, then
14632              we've already issued an error message; there's no reason
14633              to check again.  */
14634           if (ambiguous_p)
14635             {
14636               cp_parser_simulate_error (parser);
14637               return error_mark_node;
14638             }
14639           /* If the next token is a `::', then the name must be a type
14640              name.
14641
14642              [basic.lookup.qual]
14643
14644              During the lookup for a name preceding the :: scope
14645              resolution operator, object, function, and enumerator
14646              names are ignored.  */
14647           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14648             tag_type = typename_type;
14649           /* Look up the name.  */
14650           decl = cp_parser_lookup_name (parser, identifier,
14651                                         tag_type,
14652                                         /*is_template=*/false,
14653                                         /*is_namespace=*/false,
14654                                         check_dependency_p,
14655                                         &ambiguous_decls,
14656                                         identifier_token->location);
14657           if (ambiguous_decls)
14658             {
14659               error ("%Hreference to %qD is ambiguous",
14660                      &identifier_token->location, identifier);
14661               print_candidates (ambiguous_decls);
14662               if (cp_parser_parsing_tentatively (parser))
14663                 {
14664                   identifier_token->ambiguous_p = true;
14665                   cp_parser_simulate_error (parser);
14666                 }
14667               return error_mark_node;
14668             }
14669         }
14670     }
14671   else
14672     {
14673       /* Try a template-id.  */
14674       decl = cp_parser_template_id (parser, template_keyword_p,
14675                                     check_dependency_p,
14676                                     is_declaration);
14677       if (decl == error_mark_node)
14678         return error_mark_node;
14679     }
14680
14681   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14682
14683   /* If this is a typename, create a TYPENAME_TYPE.  */
14684   if (typename_p && decl != error_mark_node)
14685     {
14686       decl = make_typename_type (scope, decl, typename_type,
14687                                  /*complain=*/tf_error);
14688       if (decl != error_mark_node)
14689         decl = TYPE_NAME (decl);
14690     }
14691
14692   /* Check to see that it is really the name of a class.  */
14693   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14694       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14695       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14696     /* Situations like this:
14697
14698          template <typename T> struct A {
14699            typename T::template X<int>::I i;
14700          };
14701
14702        are problematic.  Is `T::template X<int>' a class-name?  The
14703        standard does not seem to be definitive, but there is no other
14704        valid interpretation of the following `::'.  Therefore, those
14705        names are considered class-names.  */
14706     {
14707       decl = make_typename_type (scope, decl, tag_type, tf_error);
14708       if (decl != error_mark_node)
14709         decl = TYPE_NAME (decl);
14710     }
14711   else if (TREE_CODE (decl) != TYPE_DECL
14712            || TREE_TYPE (decl) == error_mark_node
14713            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14714     decl = error_mark_node;
14715
14716   if (decl == error_mark_node)
14717     cp_parser_error (parser, "expected class-name");
14718
14719   return decl;
14720 }
14721
14722 /* Parse a class-specifier.
14723
14724    class-specifier:
14725      class-head { member-specification [opt] }
14726
14727    Returns the TREE_TYPE representing the class.  */
14728
14729 static tree
14730 cp_parser_class_specifier (cp_parser* parser)
14731 {
14732   cp_token *token;
14733   tree type;
14734   tree attributes = NULL_TREE;
14735   int has_trailing_semicolon;
14736   bool nested_name_specifier_p;
14737   unsigned saved_num_template_parameter_lists;
14738   bool saved_in_function_body;
14739   tree old_scope = NULL_TREE;
14740   tree scope = NULL_TREE;
14741   tree bases;
14742
14743   push_deferring_access_checks (dk_no_deferred);
14744
14745   /* Parse the class-head.  */
14746   type = cp_parser_class_head (parser,
14747                                &nested_name_specifier_p,
14748                                &attributes,
14749                                &bases);
14750   /* If the class-head was a semantic disaster, skip the entire body
14751      of the class.  */
14752   if (!type)
14753     {
14754       cp_parser_skip_to_end_of_block_or_statement (parser);
14755       pop_deferring_access_checks ();
14756       return error_mark_node;
14757     }
14758
14759   /* Look for the `{'.  */
14760   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14761     {
14762       pop_deferring_access_checks ();
14763       return error_mark_node;
14764     }
14765
14766   /* Process the base classes. If they're invalid, skip the 
14767      entire class body.  */
14768   if (!xref_basetypes (type, bases))
14769     {
14770       /* Consuming the closing brace yields better error messages
14771          later on.  */
14772       if (cp_parser_skip_to_closing_brace (parser))
14773         cp_lexer_consume_token (parser->lexer);
14774       pop_deferring_access_checks ();
14775       return error_mark_node;
14776     }
14777
14778   /* Issue an error message if type-definitions are forbidden here.  */
14779   cp_parser_check_type_definition (parser);
14780   /* Remember that we are defining one more class.  */
14781   ++parser->num_classes_being_defined;
14782   /* Inside the class, surrounding template-parameter-lists do not
14783      apply.  */
14784   saved_num_template_parameter_lists
14785     = parser->num_template_parameter_lists;
14786   parser->num_template_parameter_lists = 0;
14787   /* We are not in a function body.  */
14788   saved_in_function_body = parser->in_function_body;
14789   parser->in_function_body = false;
14790
14791   /* Start the class.  */
14792   if (nested_name_specifier_p)
14793     {
14794       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14795       old_scope = push_inner_scope (scope);
14796     }
14797   type = begin_class_definition (type, attributes);
14798
14799   if (type == error_mark_node)
14800     /* If the type is erroneous, skip the entire body of the class.  */
14801     cp_parser_skip_to_closing_brace (parser);
14802   else
14803     /* Parse the member-specification.  */
14804     cp_parser_member_specification_opt (parser);
14805
14806   /* Look for the trailing `}'.  */
14807   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14808   /* We get better error messages by noticing a common problem: a
14809      missing trailing `;'.  */
14810   token = cp_lexer_peek_token (parser->lexer);
14811   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14812   /* Look for trailing attributes to apply to this class.  */
14813   if (cp_parser_allow_gnu_extensions_p (parser))
14814     attributes = cp_parser_attributes_opt (parser);
14815   if (type != error_mark_node)
14816     type = finish_struct (type, attributes);
14817   if (nested_name_specifier_p)
14818     pop_inner_scope (old_scope, scope);
14819   /* If this class is not itself within the scope of another class,
14820      then we need to parse the bodies of all of the queued function
14821      definitions.  Note that the queued functions defined in a class
14822      are not always processed immediately following the
14823      class-specifier for that class.  Consider:
14824
14825        struct A {
14826          struct B { void f() { sizeof (A); } };
14827        };
14828
14829      If `f' were processed before the processing of `A' were
14830      completed, there would be no way to compute the size of `A'.
14831      Note that the nesting we are interested in here is lexical --
14832      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14833      for:
14834
14835        struct A { struct B; };
14836        struct A::B { void f() { } };
14837
14838      there is no need to delay the parsing of `A::B::f'.  */
14839   if (--parser->num_classes_being_defined == 0)
14840     {
14841       tree queue_entry;
14842       tree fn;
14843       tree class_type = NULL_TREE;
14844       tree pushed_scope = NULL_TREE;
14845
14846       /* In a first pass, parse default arguments to the functions.
14847          Then, in a second pass, parse the bodies of the functions.
14848          This two-phased approach handles cases like:
14849
14850             struct S {
14851               void f() { g(); }
14852               void g(int i = 3);
14853             };
14854
14855          */
14856       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14857              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14858            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14859            TREE_PURPOSE (parser->unparsed_functions_queues)
14860              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14861         {
14862           fn = TREE_VALUE (queue_entry);
14863           /* If there are default arguments that have not yet been processed,
14864              take care of them now.  */
14865           if (class_type != TREE_PURPOSE (queue_entry))
14866             {
14867               if (pushed_scope)
14868                 pop_scope (pushed_scope);
14869               class_type = TREE_PURPOSE (queue_entry);
14870               pushed_scope = push_scope (class_type);
14871             }
14872           /* Make sure that any template parameters are in scope.  */
14873           maybe_begin_member_template_processing (fn);
14874           /* Parse the default argument expressions.  */
14875           cp_parser_late_parsing_default_args (parser, fn);
14876           /* Remove any template parameters from the symbol table.  */
14877           maybe_end_member_template_processing ();
14878         }
14879       if (pushed_scope)
14880         pop_scope (pushed_scope);
14881       /* Now parse the body of the functions.  */
14882       for (TREE_VALUE (parser->unparsed_functions_queues)
14883              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14884            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14885            TREE_VALUE (parser->unparsed_functions_queues)
14886              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14887         {
14888           /* Figure out which function we need to process.  */
14889           fn = TREE_VALUE (queue_entry);
14890           /* Parse the function.  */
14891           cp_parser_late_parsing_for_member (parser, fn);
14892         }
14893     }
14894
14895   /* Put back any saved access checks.  */
14896   pop_deferring_access_checks ();
14897
14898   /* Restore saved state.  */
14899   parser->in_function_body = saved_in_function_body;
14900   parser->num_template_parameter_lists
14901     = saved_num_template_parameter_lists;
14902
14903   return type;
14904 }
14905
14906 /* Parse a class-head.
14907
14908    class-head:
14909      class-key identifier [opt] base-clause [opt]
14910      class-key nested-name-specifier identifier base-clause [opt]
14911      class-key nested-name-specifier [opt] template-id
14912        base-clause [opt]
14913
14914    GNU Extensions:
14915      class-key attributes identifier [opt] base-clause [opt]
14916      class-key attributes nested-name-specifier identifier base-clause [opt]
14917      class-key attributes nested-name-specifier [opt] template-id
14918        base-clause [opt]
14919
14920    Upon return BASES is initialized to the list of base classes (or
14921    NULL, if there are none) in the same form returned by
14922    cp_parser_base_clause.
14923
14924    Returns the TYPE of the indicated class.  Sets
14925    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14926    involving a nested-name-specifier was used, and FALSE otherwise.
14927
14928    Returns error_mark_node if this is not a class-head.
14929
14930    Returns NULL_TREE if the class-head is syntactically valid, but
14931    semantically invalid in a way that means we should skip the entire
14932    body of the class.  */
14933
14934 static tree
14935 cp_parser_class_head (cp_parser* parser,
14936                       bool* nested_name_specifier_p,
14937                       tree *attributes_p,
14938                       tree *bases)
14939 {
14940   tree nested_name_specifier;
14941   enum tag_types class_key;
14942   tree id = NULL_TREE;
14943   tree type = NULL_TREE;
14944   tree attributes;
14945   bool template_id_p = false;
14946   bool qualified_p = false;
14947   bool invalid_nested_name_p = false;
14948   bool invalid_explicit_specialization_p = false;
14949   tree pushed_scope = NULL_TREE;
14950   unsigned num_templates;
14951   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14952   /* Assume no nested-name-specifier will be present.  */
14953   *nested_name_specifier_p = false;
14954   /* Assume no template parameter lists will be used in defining the
14955      type.  */
14956   num_templates = 0;
14957
14958   *bases = NULL_TREE;
14959
14960   /* Look for the class-key.  */
14961   class_key = cp_parser_class_key (parser);
14962   if (class_key == none_type)
14963     return error_mark_node;
14964
14965   /* Parse the attributes.  */
14966   attributes = cp_parser_attributes_opt (parser);
14967
14968   /* If the next token is `::', that is invalid -- but sometimes
14969      people do try to write:
14970
14971        struct ::S {};
14972
14973      Handle this gracefully by accepting the extra qualifier, and then
14974      issuing an error about it later if this really is a
14975      class-head.  If it turns out just to be an elaborated type
14976      specifier, remain silent.  */
14977   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14978     qualified_p = true;
14979
14980   push_deferring_access_checks (dk_no_check);
14981
14982   /* Determine the name of the class.  Begin by looking for an
14983      optional nested-name-specifier.  */
14984   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14985   nested_name_specifier
14986     = cp_parser_nested_name_specifier_opt (parser,
14987                                            /*typename_keyword_p=*/false,
14988                                            /*check_dependency_p=*/false,
14989                                            /*type_p=*/false,
14990                                            /*is_declaration=*/false);
14991   /* If there was a nested-name-specifier, then there *must* be an
14992      identifier.  */
14993   if (nested_name_specifier)
14994     {
14995       type_start_token = cp_lexer_peek_token (parser->lexer);
14996       /* Although the grammar says `identifier', it really means
14997          `class-name' or `template-name'.  You are only allowed to
14998          define a class that has already been declared with this
14999          syntax.
15000
15001          The proposed resolution for Core Issue 180 says that wherever
15002          you see `class T::X' you should treat `X' as a type-name.
15003
15004          It is OK to define an inaccessible class; for example:
15005
15006            class A { class B; };
15007            class A::B {};
15008
15009          We do not know if we will see a class-name, or a
15010          template-name.  We look for a class-name first, in case the
15011          class-name is a template-id; if we looked for the
15012          template-name first we would stop after the template-name.  */
15013       cp_parser_parse_tentatively (parser);
15014       type = cp_parser_class_name (parser,
15015                                    /*typename_keyword_p=*/false,
15016                                    /*template_keyword_p=*/false,
15017                                    class_type,
15018                                    /*check_dependency_p=*/false,
15019                                    /*class_head_p=*/true,
15020                                    /*is_declaration=*/false);
15021       /* If that didn't work, ignore the nested-name-specifier.  */
15022       if (!cp_parser_parse_definitely (parser))
15023         {
15024           invalid_nested_name_p = true;
15025           type_start_token = cp_lexer_peek_token (parser->lexer);
15026           id = cp_parser_identifier (parser);
15027           if (id == error_mark_node)
15028             id = NULL_TREE;
15029         }
15030       /* If we could not find a corresponding TYPE, treat this
15031          declaration like an unqualified declaration.  */
15032       if (type == error_mark_node)
15033         nested_name_specifier = NULL_TREE;
15034       /* Otherwise, count the number of templates used in TYPE and its
15035          containing scopes.  */
15036       else
15037         {
15038           tree scope;
15039
15040           for (scope = TREE_TYPE (type);
15041                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15042                scope = (TYPE_P (scope)
15043                         ? TYPE_CONTEXT (scope)
15044                         : DECL_CONTEXT (scope)))
15045             if (TYPE_P (scope)
15046                 && CLASS_TYPE_P (scope)
15047                 && CLASSTYPE_TEMPLATE_INFO (scope)
15048                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15049                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15050               ++num_templates;
15051         }
15052     }
15053   /* Otherwise, the identifier is optional.  */
15054   else
15055     {
15056       /* We don't know whether what comes next is a template-id,
15057          an identifier, or nothing at all.  */
15058       cp_parser_parse_tentatively (parser);
15059       /* Check for a template-id.  */
15060       type_start_token = cp_lexer_peek_token (parser->lexer);
15061       id = cp_parser_template_id (parser,
15062                                   /*template_keyword_p=*/false,
15063                                   /*check_dependency_p=*/true,
15064                                   /*is_declaration=*/true);
15065       /* If that didn't work, it could still be an identifier.  */
15066       if (!cp_parser_parse_definitely (parser))
15067         {
15068           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15069             {
15070               type_start_token = cp_lexer_peek_token (parser->lexer);
15071               id = cp_parser_identifier (parser);
15072             }
15073           else
15074             id = NULL_TREE;
15075         }
15076       else
15077         {
15078           template_id_p = true;
15079           ++num_templates;
15080         }
15081     }
15082
15083   pop_deferring_access_checks ();
15084
15085   if (id)
15086     cp_parser_check_for_invalid_template_id (parser, id,
15087                                              type_start_token->location);
15088
15089   /* If it's not a `:' or a `{' then we can't really be looking at a
15090      class-head, since a class-head only appears as part of a
15091      class-specifier.  We have to detect this situation before calling
15092      xref_tag, since that has irreversible side-effects.  */
15093   if (!cp_parser_next_token_starts_class_definition_p (parser))
15094     {
15095       cp_parser_error (parser, "expected %<{%> or %<:%>");
15096       return error_mark_node;
15097     }
15098
15099   /* At this point, we're going ahead with the class-specifier, even
15100      if some other problem occurs.  */
15101   cp_parser_commit_to_tentative_parse (parser);
15102   /* Issue the error about the overly-qualified name now.  */
15103   if (qualified_p)
15104     {
15105       cp_parser_error (parser,
15106                        "global qualification of class name is invalid");
15107       return error_mark_node;
15108     }
15109   else if (invalid_nested_name_p)
15110     {
15111       cp_parser_error (parser,
15112                        "qualified name does not name a class");
15113       return error_mark_node;
15114     }
15115   else if (nested_name_specifier)
15116     {
15117       tree scope;
15118
15119       /* Reject typedef-names in class heads.  */
15120       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15121         {
15122           error ("%Hinvalid class name in declaration of %qD",
15123                  &type_start_token->location, type);
15124           type = NULL_TREE;
15125           goto done;
15126         }
15127
15128       /* Figure out in what scope the declaration is being placed.  */
15129       scope = current_scope ();
15130       /* If that scope does not contain the scope in which the
15131          class was originally declared, the program is invalid.  */
15132       if (scope && !is_ancestor (scope, nested_name_specifier))
15133         {
15134           if (at_namespace_scope_p ())
15135             error ("%Hdeclaration of %qD in namespace %qD which does not "
15136                    "enclose %qD",
15137                    &type_start_token->location,
15138                    type, scope, nested_name_specifier);
15139           else
15140             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15141                    &type_start_token->location,
15142                    type, scope, nested_name_specifier);
15143           type = NULL_TREE;
15144           goto done;
15145         }
15146       /* [dcl.meaning]
15147
15148          A declarator-id shall not be qualified except for the
15149          definition of a ... nested class outside of its class
15150          ... [or] the definition or explicit instantiation of a
15151          class member of a namespace outside of its namespace.  */
15152       if (scope == nested_name_specifier)
15153         {
15154           permerror (input_location, "%Hextra qualification not allowed",
15155                      &nested_name_specifier_token_start->location);
15156           nested_name_specifier = NULL_TREE;
15157           num_templates = 0;
15158         }
15159     }
15160   /* An explicit-specialization must be preceded by "template <>".  If
15161      it is not, try to recover gracefully.  */
15162   if (at_namespace_scope_p ()
15163       && parser->num_template_parameter_lists == 0
15164       && template_id_p)
15165     {
15166       error ("%Han explicit specialization must be preceded by %<template <>%>",
15167              &type_start_token->location);
15168       invalid_explicit_specialization_p = true;
15169       /* Take the same action that would have been taken by
15170          cp_parser_explicit_specialization.  */
15171       ++parser->num_template_parameter_lists;
15172       begin_specialization ();
15173     }
15174   /* There must be no "return" statements between this point and the
15175      end of this function; set "type "to the correct return value and
15176      use "goto done;" to return.  */
15177   /* Make sure that the right number of template parameters were
15178      present.  */
15179   if (!cp_parser_check_template_parameters (parser, num_templates,
15180                                             type_start_token->location))
15181     {
15182       /* If something went wrong, there is no point in even trying to
15183          process the class-definition.  */
15184       type = NULL_TREE;
15185       goto done;
15186     }
15187
15188   /* Look up the type.  */
15189   if (template_id_p)
15190     {
15191       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15192           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15193               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15194         {
15195           error ("%Hfunction template %qD redeclared as a class template",
15196                  &type_start_token->location, id);
15197           type = error_mark_node;
15198         }
15199       else
15200         {
15201           type = TREE_TYPE (id);
15202           type = maybe_process_partial_specialization (type);
15203         }
15204       if (nested_name_specifier)
15205         pushed_scope = push_scope (nested_name_specifier);
15206     }
15207   else if (nested_name_specifier)
15208     {
15209       tree class_type;
15210
15211       /* Given:
15212
15213             template <typename T> struct S { struct T };
15214             template <typename T> struct S<T>::T { };
15215
15216          we will get a TYPENAME_TYPE when processing the definition of
15217          `S::T'.  We need to resolve it to the actual type before we
15218          try to define it.  */
15219       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15220         {
15221           class_type = resolve_typename_type (TREE_TYPE (type),
15222                                               /*only_current_p=*/false);
15223           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15224             type = TYPE_NAME (class_type);
15225           else
15226             {
15227               cp_parser_error (parser, "could not resolve typename type");
15228               type = error_mark_node;
15229             }
15230         }
15231
15232       if (maybe_process_partial_specialization (TREE_TYPE (type))
15233           == error_mark_node)
15234         {
15235           type = NULL_TREE;
15236           goto done;
15237         }
15238
15239       class_type = current_class_type;
15240       /* Enter the scope indicated by the nested-name-specifier.  */
15241       pushed_scope = push_scope (nested_name_specifier);
15242       /* Get the canonical version of this type.  */
15243       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15244       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15245           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15246         {
15247           type = push_template_decl (type);
15248           if (type == error_mark_node)
15249             {
15250               type = NULL_TREE;
15251               goto done;
15252             }
15253         }
15254
15255       type = TREE_TYPE (type);
15256       *nested_name_specifier_p = true;
15257     }
15258   else      /* The name is not a nested name.  */
15259     {
15260       /* If the class was unnamed, create a dummy name.  */
15261       if (!id)
15262         id = make_anon_name ();
15263       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15264                        parser->num_template_parameter_lists);
15265     }
15266
15267   /* Indicate whether this class was declared as a `class' or as a
15268      `struct'.  */
15269   if (TREE_CODE (type) == RECORD_TYPE)
15270     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15271   cp_parser_check_class_key (class_key, type);
15272
15273   /* If this type was already complete, and we see another definition,
15274      that's an error.  */
15275   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15276     {
15277       error ("%Hredefinition of %q#T",
15278              &type_start_token->location, type);
15279       error ("%Hprevious definition of %q+#T",
15280              &type_start_token->location, type);
15281       type = NULL_TREE;
15282       goto done;
15283     }
15284   else if (type == error_mark_node)
15285     type = NULL_TREE;
15286
15287   /* We will have entered the scope containing the class; the names of
15288      base classes should be looked up in that context.  For example:
15289
15290        struct A { struct B {}; struct C; };
15291        struct A::C : B {};
15292
15293      is valid.  */
15294
15295   /* Get the list of base-classes, if there is one.  */
15296   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15297     *bases = cp_parser_base_clause (parser);
15298
15299  done:
15300   /* Leave the scope given by the nested-name-specifier.  We will
15301      enter the class scope itself while processing the members.  */
15302   if (pushed_scope)
15303     pop_scope (pushed_scope);
15304
15305   if (invalid_explicit_specialization_p)
15306     {
15307       end_specialization ();
15308       --parser->num_template_parameter_lists;
15309     }
15310   *attributes_p = attributes;
15311   return type;
15312 }
15313
15314 /* Parse a class-key.
15315
15316    class-key:
15317      class
15318      struct
15319      union
15320
15321    Returns the kind of class-key specified, or none_type to indicate
15322    error.  */
15323
15324 static enum tag_types
15325 cp_parser_class_key (cp_parser* parser)
15326 {
15327   cp_token *token;
15328   enum tag_types tag_type;
15329
15330   /* Look for the class-key.  */
15331   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15332   if (!token)
15333     return none_type;
15334
15335   /* Check to see if the TOKEN is a class-key.  */
15336   tag_type = cp_parser_token_is_class_key (token);
15337   if (!tag_type)
15338     cp_parser_error (parser, "expected class-key");
15339   return tag_type;
15340 }
15341
15342 /* Parse an (optional) member-specification.
15343
15344    member-specification:
15345      member-declaration member-specification [opt]
15346      access-specifier : member-specification [opt]  */
15347
15348 static void
15349 cp_parser_member_specification_opt (cp_parser* parser)
15350 {
15351   while (true)
15352     {
15353       cp_token *token;
15354       enum rid keyword;
15355
15356       /* Peek at the next token.  */
15357       token = cp_lexer_peek_token (parser->lexer);
15358       /* If it's a `}', or EOF then we've seen all the members.  */
15359       if (token->type == CPP_CLOSE_BRACE
15360           || token->type == CPP_EOF
15361           || token->type == CPP_PRAGMA_EOL)
15362         break;
15363
15364       /* See if this token is a keyword.  */
15365       keyword = token->keyword;
15366       switch (keyword)
15367         {
15368         case RID_PUBLIC:
15369         case RID_PROTECTED:
15370         case RID_PRIVATE:
15371           /* Consume the access-specifier.  */
15372           cp_lexer_consume_token (parser->lexer);
15373           /* Remember which access-specifier is active.  */
15374           current_access_specifier = token->u.value;
15375           /* Look for the `:'.  */
15376           cp_parser_require (parser, CPP_COLON, "%<:%>");
15377           break;
15378
15379         default:
15380           /* Accept #pragmas at class scope.  */
15381           if (token->type == CPP_PRAGMA)
15382             {
15383               cp_parser_pragma (parser, pragma_external);
15384               break;
15385             }
15386
15387           /* Otherwise, the next construction must be a
15388              member-declaration.  */
15389           cp_parser_member_declaration (parser);
15390         }
15391     }
15392 }
15393
15394 /* Parse a member-declaration.
15395
15396    member-declaration:
15397      decl-specifier-seq [opt] member-declarator-list [opt] ;
15398      function-definition ; [opt]
15399      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15400      using-declaration
15401      template-declaration
15402
15403    member-declarator-list:
15404      member-declarator
15405      member-declarator-list , member-declarator
15406
15407    member-declarator:
15408      declarator pure-specifier [opt]
15409      declarator constant-initializer [opt]
15410      identifier [opt] : constant-expression
15411
15412    GNU Extensions:
15413
15414    member-declaration:
15415      __extension__ member-declaration
15416
15417    member-declarator:
15418      declarator attributes [opt] pure-specifier [opt]
15419      declarator attributes [opt] constant-initializer [opt]
15420      identifier [opt] attributes [opt] : constant-expression  
15421
15422    C++0x Extensions:
15423
15424    member-declaration:
15425      static_assert-declaration  */
15426
15427 static void
15428 cp_parser_member_declaration (cp_parser* parser)
15429 {
15430   cp_decl_specifier_seq decl_specifiers;
15431   tree prefix_attributes;
15432   tree decl;
15433   int declares_class_or_enum;
15434   bool friend_p;
15435   cp_token *token = NULL;
15436   cp_token *decl_spec_token_start = NULL;
15437   cp_token *initializer_token_start = NULL;
15438   int saved_pedantic;
15439
15440   /* Check for the `__extension__' keyword.  */
15441   if (cp_parser_extension_opt (parser, &saved_pedantic))
15442     {
15443       /* Recurse.  */
15444       cp_parser_member_declaration (parser);
15445       /* Restore the old value of the PEDANTIC flag.  */
15446       pedantic = saved_pedantic;
15447
15448       return;
15449     }
15450
15451   /* Check for a template-declaration.  */
15452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15453     {
15454       /* An explicit specialization here is an error condition, and we
15455          expect the specialization handler to detect and report this.  */
15456       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15457           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15458         cp_parser_explicit_specialization (parser);
15459       else
15460         cp_parser_template_declaration (parser, /*member_p=*/true);
15461
15462       return;
15463     }
15464
15465   /* Check for a using-declaration.  */
15466   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15467     {
15468       /* Parse the using-declaration.  */
15469       cp_parser_using_declaration (parser,
15470                                    /*access_declaration_p=*/false);
15471       return;
15472     }
15473
15474   /* Check for @defs.  */
15475   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15476     {
15477       tree ivar, member;
15478       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15479       ivar = ivar_chains;
15480       while (ivar)
15481         {
15482           member = ivar;
15483           ivar = TREE_CHAIN (member);
15484           TREE_CHAIN (member) = NULL_TREE;
15485           finish_member_declaration (member);
15486         }
15487       return;
15488     }
15489
15490   /* If the next token is `static_assert' we have a static assertion.  */
15491   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15492     {
15493       cp_parser_static_assert (parser, /*member_p=*/true);
15494       return;
15495     }
15496
15497   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15498     return;
15499
15500   /* Parse the decl-specifier-seq.  */
15501   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15502   cp_parser_decl_specifier_seq (parser,
15503                                 CP_PARSER_FLAGS_OPTIONAL,
15504                                 &decl_specifiers,
15505                                 &declares_class_or_enum);
15506   prefix_attributes = decl_specifiers.attributes;
15507   decl_specifiers.attributes = NULL_TREE;
15508   /* Check for an invalid type-name.  */
15509   if (!decl_specifiers.type
15510       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15511     return;
15512   /* If there is no declarator, then the decl-specifier-seq should
15513      specify a type.  */
15514   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15515     {
15516       /* If there was no decl-specifier-seq, and the next token is a
15517          `;', then we have something like:
15518
15519            struct S { ; };
15520
15521          [class.mem]
15522
15523          Each member-declaration shall declare at least one member
15524          name of the class.  */
15525       if (!decl_specifiers.any_specifiers_p)
15526         {
15527           cp_token *token = cp_lexer_peek_token (parser->lexer);
15528           if (!in_system_header_at (token->location))
15529             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15530         }
15531       else
15532         {
15533           tree type;
15534
15535           /* See if this declaration is a friend.  */
15536           friend_p = cp_parser_friend_p (&decl_specifiers);
15537           /* If there were decl-specifiers, check to see if there was
15538              a class-declaration.  */
15539           type = check_tag_decl (&decl_specifiers);
15540           /* Nested classes have already been added to the class, but
15541              a `friend' needs to be explicitly registered.  */
15542           if (friend_p)
15543             {
15544               /* If the `friend' keyword was present, the friend must
15545                  be introduced with a class-key.  */
15546                if (!declares_class_or_enum)
15547                  error ("%Ha class-key must be used when declaring a friend",
15548                         &decl_spec_token_start->location);
15549                /* In this case:
15550
15551                     template <typename T> struct A {
15552                       friend struct A<T>::B;
15553                     };
15554
15555                   A<T>::B will be represented by a TYPENAME_TYPE, and
15556                   therefore not recognized by check_tag_decl.  */
15557                if (!type
15558                    && decl_specifiers.type
15559                    && TYPE_P (decl_specifiers.type))
15560                  type = decl_specifiers.type;
15561                if (!type || !TYPE_P (type))
15562                  error ("%Hfriend declaration does not name a class or "
15563                         "function", &decl_spec_token_start->location);
15564                else
15565                  make_friend_class (current_class_type, type,
15566                                     /*complain=*/true);
15567             }
15568           /* If there is no TYPE, an error message will already have
15569              been issued.  */
15570           else if (!type || type == error_mark_node)
15571             ;
15572           /* An anonymous aggregate has to be handled specially; such
15573              a declaration really declares a data member (with a
15574              particular type), as opposed to a nested class.  */
15575           else if (ANON_AGGR_TYPE_P (type))
15576             {
15577               /* Remove constructors and such from TYPE, now that we
15578                  know it is an anonymous aggregate.  */
15579               fixup_anonymous_aggr (type);
15580               /* And make the corresponding data member.  */
15581               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15582               /* Add it to the class.  */
15583               finish_member_declaration (decl);
15584             }
15585           else
15586             cp_parser_check_access_in_redeclaration
15587                                               (TYPE_NAME (type),
15588                                                decl_spec_token_start->location);
15589         }
15590     }
15591   else
15592     {
15593       /* See if these declarations will be friends.  */
15594       friend_p = cp_parser_friend_p (&decl_specifiers);
15595
15596       /* Keep going until we hit the `;' at the end of the
15597          declaration.  */
15598       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15599         {
15600           tree attributes = NULL_TREE;
15601           tree first_attribute;
15602
15603           /* Peek at the next token.  */
15604           token = cp_lexer_peek_token (parser->lexer);
15605
15606           /* Check for a bitfield declaration.  */
15607           if (token->type == CPP_COLON
15608               || (token->type == CPP_NAME
15609                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15610                   == CPP_COLON))
15611             {
15612               tree identifier;
15613               tree width;
15614
15615               /* Get the name of the bitfield.  Note that we cannot just
15616                  check TOKEN here because it may have been invalidated by
15617                  the call to cp_lexer_peek_nth_token above.  */
15618               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15619                 identifier = cp_parser_identifier (parser);
15620               else
15621                 identifier = NULL_TREE;
15622
15623               /* Consume the `:' token.  */
15624               cp_lexer_consume_token (parser->lexer);
15625               /* Get the width of the bitfield.  */
15626               width
15627                 = cp_parser_constant_expression (parser,
15628                                                  /*allow_non_constant=*/false,
15629                                                  NULL);
15630
15631               /* Look for attributes that apply to the bitfield.  */
15632               attributes = cp_parser_attributes_opt (parser);
15633               /* Remember which attributes are prefix attributes and
15634                  which are not.  */
15635               first_attribute = attributes;
15636               /* Combine the attributes.  */
15637               attributes = chainon (prefix_attributes, attributes);
15638
15639               /* Create the bitfield declaration.  */
15640               decl = grokbitfield (identifier
15641                                    ? make_id_declarator (NULL_TREE,
15642                                                          identifier,
15643                                                          sfk_none)
15644                                    : NULL,
15645                                    &decl_specifiers,
15646                                    width,
15647                                    attributes);
15648             }
15649           else
15650             {
15651               cp_declarator *declarator;
15652               tree initializer;
15653               tree asm_specification;
15654               int ctor_dtor_or_conv_p;
15655
15656               /* Parse the declarator.  */
15657               declarator
15658                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15659                                         &ctor_dtor_or_conv_p,
15660                                         /*parenthesized_p=*/NULL,
15661                                         /*member_p=*/true);
15662
15663               /* If something went wrong parsing the declarator, make sure
15664                  that we at least consume some tokens.  */
15665               if (declarator == cp_error_declarator)
15666                 {
15667                   /* Skip to the end of the statement.  */
15668                   cp_parser_skip_to_end_of_statement (parser);
15669                   /* If the next token is not a semicolon, that is
15670                      probably because we just skipped over the body of
15671                      a function.  So, we consume a semicolon if
15672                      present, but do not issue an error message if it
15673                      is not present.  */
15674                   if (cp_lexer_next_token_is (parser->lexer,
15675                                               CPP_SEMICOLON))
15676                     cp_lexer_consume_token (parser->lexer);
15677                   return;
15678                 }
15679
15680               if (declares_class_or_enum & 2)
15681                 cp_parser_check_for_definition_in_return_type
15682                                             (declarator, decl_specifiers.type,
15683                                              decl_specifiers.type_location);
15684
15685               /* Look for an asm-specification.  */
15686               asm_specification = cp_parser_asm_specification_opt (parser);
15687               /* Look for attributes that apply to the declaration.  */
15688               attributes = cp_parser_attributes_opt (parser);
15689               /* Remember which attributes are prefix attributes and
15690                  which are not.  */
15691               first_attribute = attributes;
15692               /* Combine the attributes.  */
15693               attributes = chainon (prefix_attributes, attributes);
15694
15695               /* If it's an `=', then we have a constant-initializer or a
15696                  pure-specifier.  It is not correct to parse the
15697                  initializer before registering the member declaration
15698                  since the member declaration should be in scope while
15699                  its initializer is processed.  However, the rest of the
15700                  front end does not yet provide an interface that allows
15701                  us to handle this correctly.  */
15702               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15703                 {
15704                   /* In [class.mem]:
15705
15706                      A pure-specifier shall be used only in the declaration of
15707                      a virtual function.
15708
15709                      A member-declarator can contain a constant-initializer
15710                      only if it declares a static member of integral or
15711                      enumeration type.
15712
15713                      Therefore, if the DECLARATOR is for a function, we look
15714                      for a pure-specifier; otherwise, we look for a
15715                      constant-initializer.  When we call `grokfield', it will
15716                      perform more stringent semantics checks.  */
15717                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15718                   if (function_declarator_p (declarator))
15719                     initializer = cp_parser_pure_specifier (parser);
15720                   else
15721                     /* Parse the initializer.  */
15722                     initializer = cp_parser_constant_initializer (parser);
15723                 }
15724               /* Otherwise, there is no initializer.  */
15725               else
15726                 initializer = NULL_TREE;
15727
15728               /* See if we are probably looking at a function
15729                  definition.  We are certainly not looking at a
15730                  member-declarator.  Calling `grokfield' has
15731                  side-effects, so we must not do it unless we are sure
15732                  that we are looking at a member-declarator.  */
15733               if (cp_parser_token_starts_function_definition_p
15734                   (cp_lexer_peek_token (parser->lexer)))
15735                 {
15736                   /* The grammar does not allow a pure-specifier to be
15737                      used when a member function is defined.  (It is
15738                      possible that this fact is an oversight in the
15739                      standard, since a pure function may be defined
15740                      outside of the class-specifier.  */
15741                   if (initializer)
15742                     error ("%Hpure-specifier on function-definition",
15743                            &initializer_token_start->location);
15744                   decl = cp_parser_save_member_function_body (parser,
15745                                                               &decl_specifiers,
15746                                                               declarator,
15747                                                               attributes);
15748                   /* If the member was not a friend, declare it here.  */
15749                   if (!friend_p)
15750                     finish_member_declaration (decl);
15751                   /* Peek at the next token.  */
15752                   token = cp_lexer_peek_token (parser->lexer);
15753                   /* If the next token is a semicolon, consume it.  */
15754                   if (token->type == CPP_SEMICOLON)
15755                     cp_lexer_consume_token (parser->lexer);
15756                   return;
15757                 }
15758               else
15759                 /* Create the declaration.  */
15760                 decl = grokfield (declarator, &decl_specifiers,
15761                                   initializer, /*init_const_expr_p=*/true,
15762                                   asm_specification,
15763                                   attributes);
15764             }
15765
15766           /* Reset PREFIX_ATTRIBUTES.  */
15767           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15768             attributes = TREE_CHAIN (attributes);
15769           if (attributes)
15770             TREE_CHAIN (attributes) = NULL_TREE;
15771
15772           /* If there is any qualification still in effect, clear it
15773              now; we will be starting fresh with the next declarator.  */
15774           parser->scope = NULL_TREE;
15775           parser->qualifying_scope = NULL_TREE;
15776           parser->object_scope = NULL_TREE;
15777           /* If it's a `,', then there are more declarators.  */
15778           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15779             cp_lexer_consume_token (parser->lexer);
15780           /* If the next token isn't a `;', then we have a parse error.  */
15781           else if (cp_lexer_next_token_is_not (parser->lexer,
15782                                                CPP_SEMICOLON))
15783             {
15784               cp_parser_error (parser, "expected %<;%>");
15785               /* Skip tokens until we find a `;'.  */
15786               cp_parser_skip_to_end_of_statement (parser);
15787
15788               break;
15789             }
15790
15791           if (decl)
15792             {
15793               /* Add DECL to the list of members.  */
15794               if (!friend_p)
15795                 finish_member_declaration (decl);
15796
15797               if (TREE_CODE (decl) == FUNCTION_DECL)
15798                 cp_parser_save_default_args (parser, decl);
15799             }
15800         }
15801     }
15802
15803   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15804 }
15805
15806 /* Parse a pure-specifier.
15807
15808    pure-specifier:
15809      = 0
15810
15811    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15812    Otherwise, ERROR_MARK_NODE is returned.  */
15813
15814 static tree
15815 cp_parser_pure_specifier (cp_parser* parser)
15816 {
15817   cp_token *token;
15818
15819   /* Look for the `=' token.  */
15820   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15821     return error_mark_node;
15822   /* Look for the `0' token.  */
15823   token = cp_lexer_consume_token (parser->lexer);
15824
15825   /* Accept = default or = delete in c++0x mode.  */
15826   if (token->keyword == RID_DEFAULT
15827       || token->keyword == RID_DELETE)
15828     {
15829       maybe_warn_cpp0x ("defaulted and deleted functions");
15830       return token->u.value;
15831     }
15832
15833   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15834   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15835     {
15836       cp_parser_error (parser,
15837                        "invalid pure specifier (only %<= 0%> is allowed)");
15838       cp_parser_skip_to_end_of_statement (parser);
15839       return error_mark_node;
15840     }
15841   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15842     {
15843       error ("%Htemplates may not be %<virtual%>", &token->location);
15844       return error_mark_node;
15845     }
15846
15847   return integer_zero_node;
15848 }
15849
15850 /* Parse a constant-initializer.
15851
15852    constant-initializer:
15853      = constant-expression
15854
15855    Returns a representation of the constant-expression.  */
15856
15857 static tree
15858 cp_parser_constant_initializer (cp_parser* parser)
15859 {
15860   /* Look for the `=' token.  */
15861   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15862     return error_mark_node;
15863
15864   /* It is invalid to write:
15865
15866        struct S { static const int i = { 7 }; };
15867
15868      */
15869   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15870     {
15871       cp_parser_error (parser,
15872                        "a brace-enclosed initializer is not allowed here");
15873       /* Consume the opening brace.  */
15874       cp_lexer_consume_token (parser->lexer);
15875       /* Skip the initializer.  */
15876       cp_parser_skip_to_closing_brace (parser);
15877       /* Look for the trailing `}'.  */
15878       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15879
15880       return error_mark_node;
15881     }
15882
15883   return cp_parser_constant_expression (parser,
15884                                         /*allow_non_constant=*/false,
15885                                         NULL);
15886 }
15887
15888 /* Derived classes [gram.class.derived] */
15889
15890 /* Parse a base-clause.
15891
15892    base-clause:
15893      : base-specifier-list
15894
15895    base-specifier-list:
15896      base-specifier ... [opt]
15897      base-specifier-list , base-specifier ... [opt]
15898
15899    Returns a TREE_LIST representing the base-classes, in the order in
15900    which they were declared.  The representation of each node is as
15901    described by cp_parser_base_specifier.
15902
15903    In the case that no bases are specified, this function will return
15904    NULL_TREE, not ERROR_MARK_NODE.  */
15905
15906 static tree
15907 cp_parser_base_clause (cp_parser* parser)
15908 {
15909   tree bases = NULL_TREE;
15910
15911   /* Look for the `:' that begins the list.  */
15912   cp_parser_require (parser, CPP_COLON, "%<:%>");
15913
15914   /* Scan the base-specifier-list.  */
15915   while (true)
15916     {
15917       cp_token *token;
15918       tree base;
15919       bool pack_expansion_p = false;
15920
15921       /* Look for the base-specifier.  */
15922       base = cp_parser_base_specifier (parser);
15923       /* Look for the (optional) ellipsis. */
15924       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15925         {
15926           /* Consume the `...'. */
15927           cp_lexer_consume_token (parser->lexer);
15928
15929           pack_expansion_p = true;
15930         }
15931
15932       /* Add BASE to the front of the list.  */
15933       if (base != error_mark_node)
15934         {
15935           if (pack_expansion_p)
15936             /* Make this a pack expansion type. */
15937             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15938           
15939
15940           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15941             {
15942               TREE_CHAIN (base) = bases;
15943               bases = base;
15944             }
15945         }
15946       /* Peek at the next token.  */
15947       token = cp_lexer_peek_token (parser->lexer);
15948       /* If it's not a comma, then the list is complete.  */
15949       if (token->type != CPP_COMMA)
15950         break;
15951       /* Consume the `,'.  */
15952       cp_lexer_consume_token (parser->lexer);
15953     }
15954
15955   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15956      base class had a qualified name.  However, the next name that
15957      appears is certainly not qualified.  */
15958   parser->scope = NULL_TREE;
15959   parser->qualifying_scope = NULL_TREE;
15960   parser->object_scope = NULL_TREE;
15961
15962   return nreverse (bases);
15963 }
15964
15965 /* Parse a base-specifier.
15966
15967    base-specifier:
15968      :: [opt] nested-name-specifier [opt] class-name
15969      virtual access-specifier [opt] :: [opt] nested-name-specifier
15970        [opt] class-name
15971      access-specifier virtual [opt] :: [opt] nested-name-specifier
15972        [opt] class-name
15973
15974    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15975    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15976    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15977    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15978
15979 static tree
15980 cp_parser_base_specifier (cp_parser* parser)
15981 {
15982   cp_token *token;
15983   bool done = false;
15984   bool virtual_p = false;
15985   bool duplicate_virtual_error_issued_p = false;
15986   bool duplicate_access_error_issued_p = false;
15987   bool class_scope_p, template_p;
15988   tree access = access_default_node;
15989   tree type;
15990
15991   /* Process the optional `virtual' and `access-specifier'.  */
15992   while (!done)
15993     {
15994       /* Peek at the next token.  */
15995       token = cp_lexer_peek_token (parser->lexer);
15996       /* Process `virtual'.  */
15997       switch (token->keyword)
15998         {
15999         case RID_VIRTUAL:
16000           /* If `virtual' appears more than once, issue an error.  */
16001           if (virtual_p && !duplicate_virtual_error_issued_p)
16002             {
16003               cp_parser_error (parser,
16004                                "%<virtual%> specified more than once in base-specified");
16005               duplicate_virtual_error_issued_p = true;
16006             }
16007
16008           virtual_p = true;
16009
16010           /* Consume the `virtual' token.  */
16011           cp_lexer_consume_token (parser->lexer);
16012
16013           break;
16014
16015         case RID_PUBLIC:
16016         case RID_PROTECTED:
16017         case RID_PRIVATE:
16018           /* If more than one access specifier appears, issue an
16019              error.  */
16020           if (access != access_default_node
16021               && !duplicate_access_error_issued_p)
16022             {
16023               cp_parser_error (parser,
16024                                "more than one access specifier in base-specified");
16025               duplicate_access_error_issued_p = true;
16026             }
16027
16028           access = ridpointers[(int) token->keyword];
16029
16030           /* Consume the access-specifier.  */
16031           cp_lexer_consume_token (parser->lexer);
16032
16033           break;
16034
16035         default:
16036           done = true;
16037           break;
16038         }
16039     }
16040   /* It is not uncommon to see programs mechanically, erroneously, use
16041      the 'typename' keyword to denote (dependent) qualified types
16042      as base classes.  */
16043   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16044     {
16045       token = cp_lexer_peek_token (parser->lexer);
16046       if (!processing_template_decl)
16047         error ("%Hkeyword %<typename%> not allowed outside of templates",
16048                &token->location);
16049       else
16050         error ("%Hkeyword %<typename%> not allowed in this context "
16051                "(the base class is implicitly a type)",
16052                &token->location);
16053       cp_lexer_consume_token (parser->lexer);
16054     }
16055
16056   /* Look for the optional `::' operator.  */
16057   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16058   /* Look for the nested-name-specifier.  The simplest way to
16059      implement:
16060
16061        [temp.res]
16062
16063        The keyword `typename' is not permitted in a base-specifier or
16064        mem-initializer; in these contexts a qualified name that
16065        depends on a template-parameter is implicitly assumed to be a
16066        type name.
16067
16068      is to pretend that we have seen the `typename' keyword at this
16069      point.  */
16070   cp_parser_nested_name_specifier_opt (parser,
16071                                        /*typename_keyword_p=*/true,
16072                                        /*check_dependency_p=*/true,
16073                                        typename_type,
16074                                        /*is_declaration=*/true);
16075   /* If the base class is given by a qualified name, assume that names
16076      we see are type names or templates, as appropriate.  */
16077   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16078   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16079
16080   /* Finally, look for the class-name.  */
16081   type = cp_parser_class_name (parser,
16082                                class_scope_p,
16083                                template_p,
16084                                typename_type,
16085                                /*check_dependency_p=*/true,
16086                                /*class_head_p=*/false,
16087                                /*is_declaration=*/true);
16088
16089   if (type == error_mark_node)
16090     return error_mark_node;
16091
16092   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16093 }
16094
16095 /* Exception handling [gram.exception] */
16096
16097 /* Parse an (optional) exception-specification.
16098
16099    exception-specification:
16100      throw ( type-id-list [opt] )
16101
16102    Returns a TREE_LIST representing the exception-specification.  The
16103    TREE_VALUE of each node is a type.  */
16104
16105 static tree
16106 cp_parser_exception_specification_opt (cp_parser* parser)
16107 {
16108   cp_token *token;
16109   tree type_id_list;
16110
16111   /* Peek at the next token.  */
16112   token = cp_lexer_peek_token (parser->lexer);
16113   /* If it's not `throw', then there's no exception-specification.  */
16114   if (!cp_parser_is_keyword (token, RID_THROW))
16115     return NULL_TREE;
16116
16117   /* Consume the `throw'.  */
16118   cp_lexer_consume_token (parser->lexer);
16119
16120   /* Look for the `('.  */
16121   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16122
16123   /* Peek at the next token.  */
16124   token = cp_lexer_peek_token (parser->lexer);
16125   /* If it's not a `)', then there is a type-id-list.  */
16126   if (token->type != CPP_CLOSE_PAREN)
16127     {
16128       const char *saved_message;
16129
16130       /* Types may not be defined in an exception-specification.  */
16131       saved_message = parser->type_definition_forbidden_message;
16132       parser->type_definition_forbidden_message
16133         = "types may not be defined in an exception-specification";
16134       /* Parse the type-id-list.  */
16135       type_id_list = cp_parser_type_id_list (parser);
16136       /* Restore the saved message.  */
16137       parser->type_definition_forbidden_message = saved_message;
16138     }
16139   else
16140     type_id_list = empty_except_spec;
16141
16142   /* Look for the `)'.  */
16143   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16144
16145   return type_id_list;
16146 }
16147
16148 /* Parse an (optional) type-id-list.
16149
16150    type-id-list:
16151      type-id ... [opt]
16152      type-id-list , type-id ... [opt]
16153
16154    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16155    in the order that the types were presented.  */
16156
16157 static tree
16158 cp_parser_type_id_list (cp_parser* parser)
16159 {
16160   tree types = NULL_TREE;
16161
16162   while (true)
16163     {
16164       cp_token *token;
16165       tree type;
16166
16167       /* Get the next type-id.  */
16168       type = cp_parser_type_id (parser);
16169       /* Parse the optional ellipsis. */
16170       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16171         {
16172           /* Consume the `...'. */
16173           cp_lexer_consume_token (parser->lexer);
16174
16175           /* Turn the type into a pack expansion expression. */
16176           type = make_pack_expansion (type);
16177         }
16178       /* Add it to the list.  */
16179       types = add_exception_specifier (types, type, /*complain=*/1);
16180       /* Peek at the next token.  */
16181       token = cp_lexer_peek_token (parser->lexer);
16182       /* If it is not a `,', we are done.  */
16183       if (token->type != CPP_COMMA)
16184         break;
16185       /* Consume the `,'.  */
16186       cp_lexer_consume_token (parser->lexer);
16187     }
16188
16189   return nreverse (types);
16190 }
16191
16192 /* Parse a try-block.
16193
16194    try-block:
16195      try compound-statement handler-seq  */
16196
16197 static tree
16198 cp_parser_try_block (cp_parser* parser)
16199 {
16200   tree try_block;
16201
16202   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16203   try_block = begin_try_block ();
16204   cp_parser_compound_statement (parser, NULL, true);
16205   finish_try_block (try_block);
16206   cp_parser_handler_seq (parser);
16207   finish_handler_sequence (try_block);
16208
16209   return try_block;
16210 }
16211
16212 /* Parse a function-try-block.
16213
16214    function-try-block:
16215      try ctor-initializer [opt] function-body handler-seq  */
16216
16217 static bool
16218 cp_parser_function_try_block (cp_parser* parser)
16219 {
16220   tree compound_stmt;
16221   tree try_block;
16222   bool ctor_initializer_p;
16223
16224   /* Look for the `try' keyword.  */
16225   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16226     return false;
16227   /* Let the rest of the front end know where we are.  */
16228   try_block = begin_function_try_block (&compound_stmt);
16229   /* Parse the function-body.  */
16230   ctor_initializer_p
16231     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16232   /* We're done with the `try' part.  */
16233   finish_function_try_block (try_block);
16234   /* Parse the handlers.  */
16235   cp_parser_handler_seq (parser);
16236   /* We're done with the handlers.  */
16237   finish_function_handler_sequence (try_block, compound_stmt);
16238
16239   return ctor_initializer_p;
16240 }
16241
16242 /* Parse a handler-seq.
16243
16244    handler-seq:
16245      handler handler-seq [opt]  */
16246
16247 static void
16248 cp_parser_handler_seq (cp_parser* parser)
16249 {
16250   while (true)
16251     {
16252       cp_token *token;
16253
16254       /* Parse the handler.  */
16255       cp_parser_handler (parser);
16256       /* Peek at the next token.  */
16257       token = cp_lexer_peek_token (parser->lexer);
16258       /* If it's not `catch' then there are no more handlers.  */
16259       if (!cp_parser_is_keyword (token, RID_CATCH))
16260         break;
16261     }
16262 }
16263
16264 /* Parse a handler.
16265
16266    handler:
16267      catch ( exception-declaration ) compound-statement  */
16268
16269 static void
16270 cp_parser_handler (cp_parser* parser)
16271 {
16272   tree handler;
16273   tree declaration;
16274
16275   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16276   handler = begin_handler ();
16277   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16278   declaration = cp_parser_exception_declaration (parser);
16279   finish_handler_parms (declaration, handler);
16280   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16281   cp_parser_compound_statement (parser, NULL, false);
16282   finish_handler (handler);
16283 }
16284
16285 /* Parse an exception-declaration.
16286
16287    exception-declaration:
16288      type-specifier-seq declarator
16289      type-specifier-seq abstract-declarator
16290      type-specifier-seq
16291      ...
16292
16293    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16294    ellipsis variant is used.  */
16295
16296 static tree
16297 cp_parser_exception_declaration (cp_parser* parser)
16298 {
16299   cp_decl_specifier_seq type_specifiers;
16300   cp_declarator *declarator;
16301   const char *saved_message;
16302
16303   /* If it's an ellipsis, it's easy to handle.  */
16304   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16305     {
16306       /* Consume the `...' token.  */
16307       cp_lexer_consume_token (parser->lexer);
16308       return NULL_TREE;
16309     }
16310
16311   /* Types may not be defined in exception-declarations.  */
16312   saved_message = parser->type_definition_forbidden_message;
16313   parser->type_definition_forbidden_message
16314     = "types may not be defined in exception-declarations";
16315
16316   /* Parse the type-specifier-seq.  */
16317   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16318                                 &type_specifiers);
16319   /* If it's a `)', then there is no declarator.  */
16320   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16321     declarator = NULL;
16322   else
16323     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16324                                        /*ctor_dtor_or_conv_p=*/NULL,
16325                                        /*parenthesized_p=*/NULL,
16326                                        /*member_p=*/false);
16327
16328   /* Restore the saved message.  */
16329   parser->type_definition_forbidden_message = saved_message;
16330
16331   if (!type_specifiers.any_specifiers_p)
16332     return error_mark_node;
16333
16334   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16335 }
16336
16337 /* Parse a throw-expression.
16338
16339    throw-expression:
16340      throw assignment-expression [opt]
16341
16342    Returns a THROW_EXPR representing the throw-expression.  */
16343
16344 static tree
16345 cp_parser_throw_expression (cp_parser* parser)
16346 {
16347   tree expression;
16348   cp_token* token;
16349
16350   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16351   token = cp_lexer_peek_token (parser->lexer);
16352   /* Figure out whether or not there is an assignment-expression
16353      following the "throw" keyword.  */
16354   if (token->type == CPP_COMMA
16355       || token->type == CPP_SEMICOLON
16356       || token->type == CPP_CLOSE_PAREN
16357       || token->type == CPP_CLOSE_SQUARE
16358       || token->type == CPP_CLOSE_BRACE
16359       || token->type == CPP_COLON)
16360     expression = NULL_TREE;
16361   else
16362     expression = cp_parser_assignment_expression (parser,
16363                                                   /*cast_p=*/false);
16364
16365   return build_throw (expression);
16366 }
16367
16368 /* GNU Extensions */
16369
16370 /* Parse an (optional) asm-specification.
16371
16372    asm-specification:
16373      asm ( string-literal )
16374
16375    If the asm-specification is present, returns a STRING_CST
16376    corresponding to the string-literal.  Otherwise, returns
16377    NULL_TREE.  */
16378
16379 static tree
16380 cp_parser_asm_specification_opt (cp_parser* parser)
16381 {
16382   cp_token *token;
16383   tree asm_specification;
16384
16385   /* Peek at the next token.  */
16386   token = cp_lexer_peek_token (parser->lexer);
16387   /* If the next token isn't the `asm' keyword, then there's no
16388      asm-specification.  */
16389   if (!cp_parser_is_keyword (token, RID_ASM))
16390     return NULL_TREE;
16391
16392   /* Consume the `asm' token.  */
16393   cp_lexer_consume_token (parser->lexer);
16394   /* Look for the `('.  */
16395   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16396
16397   /* Look for the string-literal.  */
16398   asm_specification = cp_parser_string_literal (parser, false, false);
16399
16400   /* Look for the `)'.  */
16401   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16402
16403   return asm_specification;
16404 }
16405
16406 /* Parse an asm-operand-list.
16407
16408    asm-operand-list:
16409      asm-operand
16410      asm-operand-list , asm-operand
16411
16412    asm-operand:
16413      string-literal ( expression )
16414      [ string-literal ] string-literal ( expression )
16415
16416    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16417    each node is the expression.  The TREE_PURPOSE is itself a
16418    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16419    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16420    is a STRING_CST for the string literal before the parenthesis. Returns
16421    ERROR_MARK_NODE if any of the operands are invalid.  */
16422
16423 static tree
16424 cp_parser_asm_operand_list (cp_parser* parser)
16425 {
16426   tree asm_operands = NULL_TREE;
16427   bool invalid_operands = false;
16428
16429   while (true)
16430     {
16431       tree string_literal;
16432       tree expression;
16433       tree name;
16434
16435       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16436         {
16437           /* Consume the `[' token.  */
16438           cp_lexer_consume_token (parser->lexer);
16439           /* Read the operand name.  */
16440           name = cp_parser_identifier (parser);
16441           if (name != error_mark_node)
16442             name = build_string (IDENTIFIER_LENGTH (name),
16443                                  IDENTIFIER_POINTER (name));
16444           /* Look for the closing `]'.  */
16445           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16446         }
16447       else
16448         name = NULL_TREE;
16449       /* Look for the string-literal.  */
16450       string_literal = cp_parser_string_literal (parser, false, false);
16451
16452       /* Look for the `('.  */
16453       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16454       /* Parse the expression.  */
16455       expression = cp_parser_expression (parser, /*cast_p=*/false);
16456       /* Look for the `)'.  */
16457       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16458
16459       if (name == error_mark_node 
16460           || string_literal == error_mark_node 
16461           || expression == error_mark_node)
16462         invalid_operands = true;
16463
16464       /* Add this operand to the list.  */
16465       asm_operands = tree_cons (build_tree_list (name, string_literal),
16466                                 expression,
16467                                 asm_operands);
16468       /* If the next token is not a `,', there are no more
16469          operands.  */
16470       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16471         break;
16472       /* Consume the `,'.  */
16473       cp_lexer_consume_token (parser->lexer);
16474     }
16475
16476   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16477 }
16478
16479 /* Parse an asm-clobber-list.
16480
16481    asm-clobber-list:
16482      string-literal
16483      asm-clobber-list , string-literal
16484
16485    Returns a TREE_LIST, indicating the clobbers in the order that they
16486    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16487
16488 static tree
16489 cp_parser_asm_clobber_list (cp_parser* parser)
16490 {
16491   tree clobbers = NULL_TREE;
16492
16493   while (true)
16494     {
16495       tree string_literal;
16496
16497       /* Look for the string literal.  */
16498       string_literal = cp_parser_string_literal (parser, false, false);
16499       /* Add it to the list.  */
16500       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16501       /* If the next token is not a `,', then the list is
16502          complete.  */
16503       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16504         break;
16505       /* Consume the `,' token.  */
16506       cp_lexer_consume_token (parser->lexer);
16507     }
16508
16509   return clobbers;
16510 }
16511
16512 /* Parse an (optional) series of attributes.
16513
16514    attributes:
16515      attributes attribute
16516
16517    attribute:
16518      __attribute__ (( attribute-list [opt] ))
16519
16520    The return value is as for cp_parser_attribute_list.  */
16521
16522 static tree
16523 cp_parser_attributes_opt (cp_parser* parser)
16524 {
16525   tree attributes = NULL_TREE;
16526
16527   while (true)
16528     {
16529       cp_token *token;
16530       tree attribute_list;
16531
16532       /* Peek at the next token.  */
16533       token = cp_lexer_peek_token (parser->lexer);
16534       /* If it's not `__attribute__', then we're done.  */
16535       if (token->keyword != RID_ATTRIBUTE)
16536         break;
16537
16538       /* Consume the `__attribute__' keyword.  */
16539       cp_lexer_consume_token (parser->lexer);
16540       /* Look for the two `(' tokens.  */
16541       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16542       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16543
16544       /* Peek at the next token.  */
16545       token = cp_lexer_peek_token (parser->lexer);
16546       if (token->type != CPP_CLOSE_PAREN)
16547         /* Parse the attribute-list.  */
16548         attribute_list = cp_parser_attribute_list (parser);
16549       else
16550         /* If the next token is a `)', then there is no attribute
16551            list.  */
16552         attribute_list = NULL;
16553
16554       /* Look for the two `)' tokens.  */
16555       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16556       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16557
16558       /* Add these new attributes to the list.  */
16559       attributes = chainon (attributes, attribute_list);
16560     }
16561
16562   return attributes;
16563 }
16564
16565 /* Parse an attribute-list.
16566
16567    attribute-list:
16568      attribute
16569      attribute-list , attribute
16570
16571    attribute:
16572      identifier
16573      identifier ( identifier )
16574      identifier ( identifier , expression-list )
16575      identifier ( expression-list )
16576
16577    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16578    to an attribute.  The TREE_PURPOSE of each node is the identifier
16579    indicating which attribute is in use.  The TREE_VALUE represents
16580    the arguments, if any.  */
16581
16582 static tree
16583 cp_parser_attribute_list (cp_parser* parser)
16584 {
16585   tree attribute_list = NULL_TREE;
16586   bool save_translate_strings_p = parser->translate_strings_p;
16587
16588   parser->translate_strings_p = false;
16589   while (true)
16590     {
16591       cp_token *token;
16592       tree identifier;
16593       tree attribute;
16594
16595       /* Look for the identifier.  We also allow keywords here; for
16596          example `__attribute__ ((const))' is legal.  */
16597       token = cp_lexer_peek_token (parser->lexer);
16598       if (token->type == CPP_NAME
16599           || token->type == CPP_KEYWORD)
16600         {
16601           tree arguments = NULL_TREE;
16602
16603           /* Consume the token.  */
16604           token = cp_lexer_consume_token (parser->lexer);
16605
16606           /* Save away the identifier that indicates which attribute
16607              this is.  */
16608           identifier = token->u.value;
16609           attribute = build_tree_list (identifier, NULL_TREE);
16610
16611           /* Peek at the next token.  */
16612           token = cp_lexer_peek_token (parser->lexer);
16613           /* If it's an `(', then parse the attribute arguments.  */
16614           if (token->type == CPP_OPEN_PAREN)
16615             {
16616               arguments = cp_parser_parenthesized_expression_list
16617                           (parser, true, /*cast_p=*/false,
16618                            /*allow_expansion_p=*/false,
16619                            /*non_constant_p=*/NULL);
16620               /* Save the arguments away.  */
16621               TREE_VALUE (attribute) = arguments;
16622             }
16623
16624           if (arguments != error_mark_node)
16625             {
16626               /* Add this attribute to the list.  */
16627               TREE_CHAIN (attribute) = attribute_list;
16628               attribute_list = attribute;
16629             }
16630
16631           token = cp_lexer_peek_token (parser->lexer);
16632         }
16633       /* Now, look for more attributes.  If the next token isn't a
16634          `,', we're done.  */
16635       if (token->type != CPP_COMMA)
16636         break;
16637
16638       /* Consume the comma and keep going.  */
16639       cp_lexer_consume_token (parser->lexer);
16640     }
16641   parser->translate_strings_p = save_translate_strings_p;
16642
16643   /* We built up the list in reverse order.  */
16644   return nreverse (attribute_list);
16645 }
16646
16647 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16648    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16649    current value of the PEDANTIC flag, regardless of whether or not
16650    the `__extension__' keyword is present.  The caller is responsible
16651    for restoring the value of the PEDANTIC flag.  */
16652
16653 static bool
16654 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16655 {
16656   /* Save the old value of the PEDANTIC flag.  */
16657   *saved_pedantic = pedantic;
16658
16659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16660     {
16661       /* Consume the `__extension__' token.  */
16662       cp_lexer_consume_token (parser->lexer);
16663       /* We're not being pedantic while the `__extension__' keyword is
16664          in effect.  */
16665       pedantic = 0;
16666
16667       return true;
16668     }
16669
16670   return false;
16671 }
16672
16673 /* Parse a label declaration.
16674
16675    label-declaration:
16676      __label__ label-declarator-seq ;
16677
16678    label-declarator-seq:
16679      identifier , label-declarator-seq
16680      identifier  */
16681
16682 static void
16683 cp_parser_label_declaration (cp_parser* parser)
16684 {
16685   /* Look for the `__label__' keyword.  */
16686   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16687
16688   while (true)
16689     {
16690       tree identifier;
16691
16692       /* Look for an identifier.  */
16693       identifier = cp_parser_identifier (parser);
16694       /* If we failed, stop.  */
16695       if (identifier == error_mark_node)
16696         break;
16697       /* Declare it as a label.  */
16698       finish_label_decl (identifier);
16699       /* If the next token is a `;', stop.  */
16700       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16701         break;
16702       /* Look for the `,' separating the label declarations.  */
16703       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16704     }
16705
16706   /* Look for the final `;'.  */
16707   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16708 }
16709
16710 /* Support Functions */
16711
16712 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16713    NAME should have one of the representations used for an
16714    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16715    is returned.  If PARSER->SCOPE is a dependent type, then a
16716    SCOPE_REF is returned.
16717
16718    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16719    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16720    was formed.  Abstractly, such entities should not be passed to this
16721    function, because they do not need to be looked up, but it is
16722    simpler to check for this special case here, rather than at the
16723    call-sites.
16724
16725    In cases not explicitly covered above, this function returns a
16726    DECL, OVERLOAD, or baselink representing the result of the lookup.
16727    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16728    is returned.
16729
16730    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16731    (e.g., "struct") that was used.  In that case bindings that do not
16732    refer to types are ignored.
16733
16734    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16735    ignored.
16736
16737    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16738    are ignored.
16739
16740    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16741    types.
16742
16743    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16744    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16745    NULL_TREE otherwise.  */
16746
16747 static tree
16748 cp_parser_lookup_name (cp_parser *parser, tree name,
16749                        enum tag_types tag_type,
16750                        bool is_template,
16751                        bool is_namespace,
16752                        bool check_dependency,
16753                        tree *ambiguous_decls,
16754                        location_t name_location)
16755 {
16756   int flags = 0;
16757   tree decl;
16758   tree object_type = parser->context->object_type;
16759
16760   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16761     flags |= LOOKUP_COMPLAIN;
16762
16763   /* Assume that the lookup will be unambiguous.  */
16764   if (ambiguous_decls)
16765     *ambiguous_decls = NULL_TREE;
16766
16767   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16768      no longer valid.  Note that if we are parsing tentatively, and
16769      the parse fails, OBJECT_TYPE will be automatically restored.  */
16770   parser->context->object_type = NULL_TREE;
16771
16772   if (name == error_mark_node)
16773     return error_mark_node;
16774
16775   /* A template-id has already been resolved; there is no lookup to
16776      do.  */
16777   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16778     return name;
16779   if (BASELINK_P (name))
16780     {
16781       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16782                   == TEMPLATE_ID_EXPR);
16783       return name;
16784     }
16785
16786   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16787      it should already have been checked to make sure that the name
16788      used matches the type being destroyed.  */
16789   if (TREE_CODE (name) == BIT_NOT_EXPR)
16790     {
16791       tree type;
16792
16793       /* Figure out to which type this destructor applies.  */
16794       if (parser->scope)
16795         type = parser->scope;
16796       else if (object_type)
16797         type = object_type;
16798       else
16799         type = current_class_type;
16800       /* If that's not a class type, there is no destructor.  */
16801       if (!type || !CLASS_TYPE_P (type))
16802         return error_mark_node;
16803       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16804         lazily_declare_fn (sfk_destructor, type);
16805       if (!CLASSTYPE_DESTRUCTORS (type))
16806           return error_mark_node;
16807       /* If it was a class type, return the destructor.  */
16808       return CLASSTYPE_DESTRUCTORS (type);
16809     }
16810
16811   /* By this point, the NAME should be an ordinary identifier.  If
16812      the id-expression was a qualified name, the qualifying scope is
16813      stored in PARSER->SCOPE at this point.  */
16814   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16815
16816   /* Perform the lookup.  */
16817   if (parser->scope)
16818     {
16819       bool dependent_p;
16820
16821       if (parser->scope == error_mark_node)
16822         return error_mark_node;
16823
16824       /* If the SCOPE is dependent, the lookup must be deferred until
16825          the template is instantiated -- unless we are explicitly
16826          looking up names in uninstantiated templates.  Even then, we
16827          cannot look up the name if the scope is not a class type; it
16828          might, for example, be a template type parameter.  */
16829       dependent_p = (TYPE_P (parser->scope)
16830                      && !(parser->in_declarator_p
16831                           && currently_open_class (parser->scope))
16832                      && dependent_type_p (parser->scope));
16833       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16834            && dependent_p)
16835         {
16836           if (tag_type)
16837             {
16838               tree type;
16839
16840               /* The resolution to Core Issue 180 says that `struct
16841                  A::B' should be considered a type-name, even if `A'
16842                  is dependent.  */
16843               type = make_typename_type (parser->scope, name, tag_type,
16844                                          /*complain=*/tf_error);
16845               decl = TYPE_NAME (type);
16846             }
16847           else if (is_template
16848                    && (cp_parser_next_token_ends_template_argument_p (parser)
16849                        || cp_lexer_next_token_is (parser->lexer,
16850                                                   CPP_CLOSE_PAREN)))
16851             decl = make_unbound_class_template (parser->scope,
16852                                                 name, NULL_TREE,
16853                                                 /*complain=*/tf_error);
16854           else
16855             decl = build_qualified_name (/*type=*/NULL_TREE,
16856                                          parser->scope, name,
16857                                          is_template);
16858         }
16859       else
16860         {
16861           tree pushed_scope = NULL_TREE;
16862
16863           /* If PARSER->SCOPE is a dependent type, then it must be a
16864              class type, and we must not be checking dependencies;
16865              otherwise, we would have processed this lookup above.  So
16866              that PARSER->SCOPE is not considered a dependent base by
16867              lookup_member, we must enter the scope here.  */
16868           if (dependent_p)
16869             pushed_scope = push_scope (parser->scope);
16870           /* If the PARSER->SCOPE is a template specialization, it
16871              may be instantiated during name lookup.  In that case,
16872              errors may be issued.  Even if we rollback the current
16873              tentative parse, those errors are valid.  */
16874           decl = lookup_qualified_name (parser->scope, name,
16875                                         tag_type != none_type,
16876                                         /*complain=*/true);
16877
16878           /* If we have a single function from a using decl, pull it out.  */
16879           if (decl
16880               && TREE_CODE (decl) == OVERLOAD
16881               && !really_overloaded_fn (decl))
16882             decl = OVL_FUNCTION (decl);
16883
16884           if (pushed_scope)
16885             pop_scope (pushed_scope);
16886         }
16887       parser->qualifying_scope = parser->scope;
16888       parser->object_scope = NULL_TREE;
16889     }
16890   else if (object_type)
16891     {
16892       tree object_decl = NULL_TREE;
16893       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16894          OBJECT_TYPE is not a class.  */
16895       if (CLASS_TYPE_P (object_type))
16896         /* If the OBJECT_TYPE is a template specialization, it may
16897            be instantiated during name lookup.  In that case, errors
16898            may be issued.  Even if we rollback the current tentative
16899            parse, those errors are valid.  */
16900         object_decl = lookup_member (object_type,
16901                                      name,
16902                                      /*protect=*/0,
16903                                      tag_type != none_type);
16904       /* Look it up in the enclosing context, too.  */
16905       decl = lookup_name_real (name, tag_type != none_type,
16906                                /*nonclass=*/0,
16907                                /*block_p=*/true, is_namespace, flags);
16908       parser->object_scope = object_type;
16909       parser->qualifying_scope = NULL_TREE;
16910       if (object_decl)
16911         decl = object_decl;
16912     }
16913   else
16914     {
16915       decl = lookup_name_real (name, tag_type != none_type,
16916                                /*nonclass=*/0,
16917                                /*block_p=*/true, is_namespace, flags);
16918       parser->qualifying_scope = NULL_TREE;
16919       parser->object_scope = NULL_TREE;
16920     }
16921
16922   /* If the lookup failed, let our caller know.  */
16923   if (!decl || decl == error_mark_node)
16924     return error_mark_node;
16925
16926   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16927   if (TREE_CODE (decl) == TREE_LIST)
16928     {
16929       if (ambiguous_decls)
16930         *ambiguous_decls = decl;
16931       /* The error message we have to print is too complicated for
16932          cp_parser_error, so we incorporate its actions directly.  */
16933       if (!cp_parser_simulate_error (parser))
16934         {
16935           error ("%Hreference to %qD is ambiguous",
16936                  &name_location, name);
16937           print_candidates (decl);
16938         }
16939       return error_mark_node;
16940     }
16941
16942   gcc_assert (DECL_P (decl)
16943               || TREE_CODE (decl) == OVERLOAD
16944               || TREE_CODE (decl) == SCOPE_REF
16945               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16946               || BASELINK_P (decl));
16947
16948   /* If we have resolved the name of a member declaration, check to
16949      see if the declaration is accessible.  When the name resolves to
16950      set of overloaded functions, accessibility is checked when
16951      overload resolution is done.
16952
16953      During an explicit instantiation, access is not checked at all,
16954      as per [temp.explicit].  */
16955   if (DECL_P (decl))
16956     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16957
16958   return decl;
16959 }
16960
16961 /* Like cp_parser_lookup_name, but for use in the typical case where
16962    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16963    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16964
16965 static tree
16966 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16967 {
16968   return cp_parser_lookup_name (parser, name,
16969                                 none_type,
16970                                 /*is_template=*/false,
16971                                 /*is_namespace=*/false,
16972                                 /*check_dependency=*/true,
16973                                 /*ambiguous_decls=*/NULL,
16974                                 location);
16975 }
16976
16977 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16978    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16979    true, the DECL indicates the class being defined in a class-head,
16980    or declared in an elaborated-type-specifier.
16981
16982    Otherwise, return DECL.  */
16983
16984 static tree
16985 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16986 {
16987   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16988      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16989
16990        struct A {
16991          template <typename T> struct B;
16992        };
16993
16994        template <typename T> struct A::B {};
16995
16996      Similarly, in an elaborated-type-specifier:
16997
16998        namespace N { struct X{}; }
16999
17000        struct A {
17001          template <typename T> friend struct N::X;
17002        };
17003
17004      However, if the DECL refers to a class type, and we are in
17005      the scope of the class, then the name lookup automatically
17006      finds the TYPE_DECL created by build_self_reference rather
17007      than a TEMPLATE_DECL.  For example, in:
17008
17009        template <class T> struct S {
17010          S s;
17011        };
17012
17013      there is no need to handle such case.  */
17014
17015   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17016     return DECL_TEMPLATE_RESULT (decl);
17017
17018   return decl;
17019 }
17020
17021 /* If too many, or too few, template-parameter lists apply to the
17022    declarator, issue an error message.  Returns TRUE if all went well,
17023    and FALSE otherwise.  */
17024
17025 static bool
17026 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17027                                                 cp_declarator *declarator,
17028                                                 location_t declarator_location)
17029 {
17030   unsigned num_templates;
17031
17032   /* We haven't seen any classes that involve template parameters yet.  */
17033   num_templates = 0;
17034
17035   switch (declarator->kind)
17036     {
17037     case cdk_id:
17038       if (declarator->u.id.qualifying_scope)
17039         {
17040           tree scope;
17041           tree member;
17042
17043           scope = declarator->u.id.qualifying_scope;
17044           member = declarator->u.id.unqualified_name;
17045
17046           while (scope && CLASS_TYPE_P (scope))
17047             {
17048               /* You're supposed to have one `template <...>'
17049                  for every template class, but you don't need one
17050                  for a full specialization.  For example:
17051
17052                  template <class T> struct S{};
17053                  template <> struct S<int> { void f(); };
17054                  void S<int>::f () {}
17055
17056                  is correct; there shouldn't be a `template <>' for
17057                  the definition of `S<int>::f'.  */
17058               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17059                 /* If SCOPE does not have template information of any
17060                    kind, then it is not a template, nor is it nested
17061                    within a template.  */
17062                 break;
17063               if (explicit_class_specialization_p (scope))
17064                 break;
17065               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17066                 ++num_templates;
17067
17068               scope = TYPE_CONTEXT (scope);
17069             }
17070         }
17071       else if (TREE_CODE (declarator->u.id.unqualified_name)
17072                == TEMPLATE_ID_EXPR)
17073         /* If the DECLARATOR has the form `X<y>' then it uses one
17074            additional level of template parameters.  */
17075         ++num_templates;
17076
17077       return cp_parser_check_template_parameters (parser,
17078                                                   num_templates,
17079                                                   declarator_location);
17080
17081     case cdk_function:
17082     case cdk_array:
17083     case cdk_pointer:
17084     case cdk_reference:
17085     case cdk_ptrmem:
17086       return (cp_parser_check_declarator_template_parameters
17087               (parser, declarator->declarator, declarator_location));
17088
17089     case cdk_error:
17090       return true;
17091
17092     default:
17093       gcc_unreachable ();
17094     }
17095   return false;
17096 }
17097
17098 /* NUM_TEMPLATES were used in the current declaration.  If that is
17099    invalid, return FALSE and issue an error messages.  Otherwise,
17100    return TRUE.  */
17101
17102 static bool
17103 cp_parser_check_template_parameters (cp_parser* parser,
17104                                      unsigned num_templates,
17105                                      location_t location)
17106 {
17107   /* If there are more template classes than parameter lists, we have
17108      something like:
17109
17110        template <class T> void S<T>::R<T>::f ();  */
17111   if (parser->num_template_parameter_lists < num_templates)
17112     {
17113       error ("%Htoo few template-parameter-lists", &location);
17114       return false;
17115     }
17116   /* If there are the same number of template classes and parameter
17117      lists, that's OK.  */
17118   if (parser->num_template_parameter_lists == num_templates)
17119     return true;
17120   /* If there are more, but only one more, then we are referring to a
17121      member template.  That's OK too.  */
17122   if (parser->num_template_parameter_lists == num_templates + 1)
17123       return true;
17124   /* Otherwise, there are too many template parameter lists.  We have
17125      something like:
17126
17127      template <class T> template <class U> void S::f();  */
17128   error ("%Htoo many template-parameter-lists", &location);
17129   return false;
17130 }
17131
17132 /* Parse an optional `::' token indicating that the following name is
17133    from the global namespace.  If so, PARSER->SCOPE is set to the
17134    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17135    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17136    Returns the new value of PARSER->SCOPE, if the `::' token is
17137    present, and NULL_TREE otherwise.  */
17138
17139 static tree
17140 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17141 {
17142   cp_token *token;
17143
17144   /* Peek at the next token.  */
17145   token = cp_lexer_peek_token (parser->lexer);
17146   /* If we're looking at a `::' token then we're starting from the
17147      global namespace, not our current location.  */
17148   if (token->type == CPP_SCOPE)
17149     {
17150       /* Consume the `::' token.  */
17151       cp_lexer_consume_token (parser->lexer);
17152       /* Set the SCOPE so that we know where to start the lookup.  */
17153       parser->scope = global_namespace;
17154       parser->qualifying_scope = global_namespace;
17155       parser->object_scope = NULL_TREE;
17156
17157       return parser->scope;
17158     }
17159   else if (!current_scope_valid_p)
17160     {
17161       parser->scope = NULL_TREE;
17162       parser->qualifying_scope = NULL_TREE;
17163       parser->object_scope = NULL_TREE;
17164     }
17165
17166   return NULL_TREE;
17167 }
17168
17169 /* Returns TRUE if the upcoming token sequence is the start of a
17170    constructor declarator.  If FRIEND_P is true, the declarator is
17171    preceded by the `friend' specifier.  */
17172
17173 static bool
17174 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17175 {
17176   bool constructor_p;
17177   tree type_decl = NULL_TREE;
17178   bool nested_name_p;
17179   cp_token *next_token;
17180
17181   /* The common case is that this is not a constructor declarator, so
17182      try to avoid doing lots of work if at all possible.  It's not
17183      valid declare a constructor at function scope.  */
17184   if (parser->in_function_body)
17185     return false;
17186   /* And only certain tokens can begin a constructor declarator.  */
17187   next_token = cp_lexer_peek_token (parser->lexer);
17188   if (next_token->type != CPP_NAME
17189       && next_token->type != CPP_SCOPE
17190       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17191       && next_token->type != CPP_TEMPLATE_ID)
17192     return false;
17193
17194   /* Parse tentatively; we are going to roll back all of the tokens
17195      consumed here.  */
17196   cp_parser_parse_tentatively (parser);
17197   /* Assume that we are looking at a constructor declarator.  */
17198   constructor_p = true;
17199
17200   /* Look for the optional `::' operator.  */
17201   cp_parser_global_scope_opt (parser,
17202                               /*current_scope_valid_p=*/false);
17203   /* Look for the nested-name-specifier.  */
17204   nested_name_p
17205     = (cp_parser_nested_name_specifier_opt (parser,
17206                                             /*typename_keyword_p=*/false,
17207                                             /*check_dependency_p=*/false,
17208                                             /*type_p=*/false,
17209                                             /*is_declaration=*/false)
17210        != NULL_TREE);
17211   /* Outside of a class-specifier, there must be a
17212      nested-name-specifier.  */
17213   if (!nested_name_p &&
17214       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17215        || friend_p))
17216     constructor_p = false;
17217   /* If we still think that this might be a constructor-declarator,
17218      look for a class-name.  */
17219   if (constructor_p)
17220     {
17221       /* If we have:
17222
17223            template <typename T> struct S { S(); };
17224            template <typename T> S<T>::S ();
17225
17226          we must recognize that the nested `S' names a class.
17227          Similarly, for:
17228
17229            template <typename T> S<T>::S<T> ();
17230
17231          we must recognize that the nested `S' names a template.  */
17232       type_decl = cp_parser_class_name (parser,
17233                                         /*typename_keyword_p=*/false,
17234                                         /*template_keyword_p=*/false,
17235                                         none_type,
17236                                         /*check_dependency_p=*/false,
17237                                         /*class_head_p=*/false,
17238                                         /*is_declaration=*/false);
17239       /* If there was no class-name, then this is not a constructor.  */
17240       constructor_p = !cp_parser_error_occurred (parser);
17241     }
17242
17243   /* If we're still considering a constructor, we have to see a `(',
17244      to begin the parameter-declaration-clause, followed by either a
17245      `)', an `...', or a decl-specifier.  We need to check for a
17246      type-specifier to avoid being fooled into thinking that:
17247
17248        S::S (f) (int);
17249
17250      is a constructor.  (It is actually a function named `f' that
17251      takes one parameter (of type `int') and returns a value of type
17252      `S::S'.  */
17253   if (constructor_p
17254       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17255     {
17256       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17257           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17258           /* A parameter declaration begins with a decl-specifier,
17259              which is either the "attribute" keyword, a storage class
17260              specifier, or (usually) a type-specifier.  */
17261           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17262         {
17263           tree type;
17264           tree pushed_scope = NULL_TREE;
17265           unsigned saved_num_template_parameter_lists;
17266
17267           /* Names appearing in the type-specifier should be looked up
17268              in the scope of the class.  */
17269           if (current_class_type)
17270             type = NULL_TREE;
17271           else
17272             {
17273               type = TREE_TYPE (type_decl);
17274               if (TREE_CODE (type) == TYPENAME_TYPE)
17275                 {
17276                   type = resolve_typename_type (type,
17277                                                 /*only_current_p=*/false);
17278                   if (TREE_CODE (type) == TYPENAME_TYPE)
17279                     {
17280                       cp_parser_abort_tentative_parse (parser);
17281                       return false;
17282                     }
17283                 }
17284               pushed_scope = push_scope (type);
17285             }
17286
17287           /* Inside the constructor parameter list, surrounding
17288              template-parameter-lists do not apply.  */
17289           saved_num_template_parameter_lists
17290             = parser->num_template_parameter_lists;
17291           parser->num_template_parameter_lists = 0;
17292
17293           /* Look for the type-specifier.  */
17294           cp_parser_type_specifier (parser,
17295                                     CP_PARSER_FLAGS_NONE,
17296                                     /*decl_specs=*/NULL,
17297                                     /*is_declarator=*/true,
17298                                     /*declares_class_or_enum=*/NULL,
17299                                     /*is_cv_qualifier=*/NULL);
17300
17301           parser->num_template_parameter_lists
17302             = saved_num_template_parameter_lists;
17303
17304           /* Leave the scope of the class.  */
17305           if (pushed_scope)
17306             pop_scope (pushed_scope);
17307
17308           constructor_p = !cp_parser_error_occurred (parser);
17309         }
17310     }
17311   else
17312     constructor_p = false;
17313   /* We did not really want to consume any tokens.  */
17314   cp_parser_abort_tentative_parse (parser);
17315
17316   return constructor_p;
17317 }
17318
17319 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17320    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17321    they must be performed once we are in the scope of the function.
17322
17323    Returns the function defined.  */
17324
17325 static tree
17326 cp_parser_function_definition_from_specifiers_and_declarator
17327   (cp_parser* parser,
17328    cp_decl_specifier_seq *decl_specifiers,
17329    tree attributes,
17330    const cp_declarator *declarator)
17331 {
17332   tree fn;
17333   bool success_p;
17334
17335   /* Begin the function-definition.  */
17336   success_p = start_function (decl_specifiers, declarator, attributes);
17337
17338   /* The things we're about to see are not directly qualified by any
17339      template headers we've seen thus far.  */
17340   reset_specialization ();
17341
17342   /* If there were names looked up in the decl-specifier-seq that we
17343      did not check, check them now.  We must wait until we are in the
17344      scope of the function to perform the checks, since the function
17345      might be a friend.  */
17346   perform_deferred_access_checks ();
17347
17348   if (!success_p)
17349     {
17350       /* Skip the entire function.  */
17351       cp_parser_skip_to_end_of_block_or_statement (parser);
17352       fn = error_mark_node;
17353     }
17354   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17355     {
17356       /* Seen already, skip it.  An error message has already been output.  */
17357       cp_parser_skip_to_end_of_block_or_statement (parser);
17358       fn = current_function_decl;
17359       current_function_decl = NULL_TREE;
17360       /* If this is a function from a class, pop the nested class.  */
17361       if (current_class_name)
17362         pop_nested_class ();
17363     }
17364   else
17365     fn = cp_parser_function_definition_after_declarator (parser,
17366                                                          /*inline_p=*/false);
17367
17368   return fn;
17369 }
17370
17371 /* Parse the part of a function-definition that follows the
17372    declarator.  INLINE_P is TRUE iff this function is an inline
17373    function defined with a class-specifier.
17374
17375    Returns the function defined.  */
17376
17377 static tree
17378 cp_parser_function_definition_after_declarator (cp_parser* parser,
17379                                                 bool inline_p)
17380 {
17381   tree fn;
17382   bool ctor_initializer_p = false;
17383   bool saved_in_unbraced_linkage_specification_p;
17384   bool saved_in_function_body;
17385   unsigned saved_num_template_parameter_lists;
17386   cp_token *token;
17387
17388   saved_in_function_body = parser->in_function_body;
17389   parser->in_function_body = true;
17390   /* If the next token is `return', then the code may be trying to
17391      make use of the "named return value" extension that G++ used to
17392      support.  */
17393   token = cp_lexer_peek_token (parser->lexer);
17394   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17395     {
17396       /* Consume the `return' keyword.  */
17397       cp_lexer_consume_token (parser->lexer);
17398       /* Look for the identifier that indicates what value is to be
17399          returned.  */
17400       cp_parser_identifier (parser);
17401       /* Issue an error message.  */
17402       error ("%Hnamed return values are no longer supported",
17403              &token->location);
17404       /* Skip tokens until we reach the start of the function body.  */
17405       while (true)
17406         {
17407           cp_token *token = cp_lexer_peek_token (parser->lexer);
17408           if (token->type == CPP_OPEN_BRACE
17409               || token->type == CPP_EOF
17410               || token->type == CPP_PRAGMA_EOL)
17411             break;
17412           cp_lexer_consume_token (parser->lexer);
17413         }
17414     }
17415   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17416      anything declared inside `f'.  */
17417   saved_in_unbraced_linkage_specification_p
17418     = parser->in_unbraced_linkage_specification_p;
17419   parser->in_unbraced_linkage_specification_p = false;
17420   /* Inside the function, surrounding template-parameter-lists do not
17421      apply.  */
17422   saved_num_template_parameter_lists
17423     = parser->num_template_parameter_lists;
17424   parser->num_template_parameter_lists = 0;
17425   /* If the next token is `try', then we are looking at a
17426      function-try-block.  */
17427   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17428     ctor_initializer_p = cp_parser_function_try_block (parser);
17429   /* A function-try-block includes the function-body, so we only do
17430      this next part if we're not processing a function-try-block.  */
17431   else
17432     ctor_initializer_p
17433       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17434
17435   /* Finish the function.  */
17436   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17437                         (inline_p ? 2 : 0));
17438   /* Generate code for it, if necessary.  */
17439   expand_or_defer_fn (fn);
17440   /* Restore the saved values.  */
17441   parser->in_unbraced_linkage_specification_p
17442     = saved_in_unbraced_linkage_specification_p;
17443   parser->num_template_parameter_lists
17444     = saved_num_template_parameter_lists;
17445   parser->in_function_body = saved_in_function_body;
17446
17447   return fn;
17448 }
17449
17450 /* Parse a template-declaration, assuming that the `export' (and
17451    `extern') keywords, if present, has already been scanned.  MEMBER_P
17452    is as for cp_parser_template_declaration.  */
17453
17454 static void
17455 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17456 {
17457   tree decl = NULL_TREE;
17458   VEC (deferred_access_check,gc) *checks;
17459   tree parameter_list;
17460   bool friend_p = false;
17461   bool need_lang_pop;
17462   cp_token *token;
17463
17464   /* Look for the `template' keyword.  */
17465   token = cp_lexer_peek_token (parser->lexer);
17466   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17467     return;
17468
17469   /* And the `<'.  */
17470   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17471     return;
17472   if (at_class_scope_p () && current_function_decl)
17473     {
17474       /* 14.5.2.2 [temp.mem]
17475
17476          A local class shall not have member templates.  */
17477       error ("%Hinvalid declaration of member template in local class",
17478              &token->location);
17479       cp_parser_skip_to_end_of_block_or_statement (parser);
17480       return;
17481     }
17482   /* [temp]
17483
17484      A template ... shall not have C linkage.  */
17485   if (current_lang_name == lang_name_c)
17486     {
17487       error ("%Htemplate with C linkage", &token->location);
17488       /* Give it C++ linkage to avoid confusing other parts of the
17489          front end.  */
17490       push_lang_context (lang_name_cplusplus);
17491       need_lang_pop = true;
17492     }
17493   else
17494     need_lang_pop = false;
17495
17496   /* We cannot perform access checks on the template parameter
17497      declarations until we know what is being declared, just as we
17498      cannot check the decl-specifier list.  */
17499   push_deferring_access_checks (dk_deferred);
17500
17501   /* If the next token is `>', then we have an invalid
17502      specialization.  Rather than complain about an invalid template
17503      parameter, issue an error message here.  */
17504   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17505     {
17506       cp_parser_error (parser, "invalid explicit specialization");
17507       begin_specialization ();
17508       parameter_list = NULL_TREE;
17509     }
17510   else
17511     /* Parse the template parameters.  */
17512     parameter_list = cp_parser_template_parameter_list (parser);
17513
17514   /* Get the deferred access checks from the parameter list.  These
17515      will be checked once we know what is being declared, as for a
17516      member template the checks must be performed in the scope of the
17517      class containing the member.  */
17518   checks = get_deferred_access_checks ();
17519
17520   /* Look for the `>'.  */
17521   cp_parser_skip_to_end_of_template_parameter_list (parser);
17522   /* We just processed one more parameter list.  */
17523   ++parser->num_template_parameter_lists;
17524   /* If the next token is `template', there are more template
17525      parameters.  */
17526   if (cp_lexer_next_token_is_keyword (parser->lexer,
17527                                       RID_TEMPLATE))
17528     cp_parser_template_declaration_after_export (parser, member_p);
17529   else
17530     {
17531       /* There are no access checks when parsing a template, as we do not
17532          know if a specialization will be a friend.  */
17533       push_deferring_access_checks (dk_no_check);
17534       token = cp_lexer_peek_token (parser->lexer);
17535       decl = cp_parser_single_declaration (parser,
17536                                            checks,
17537                                            member_p,
17538                                            /*explicit_specialization_p=*/false,
17539                                            &friend_p);
17540       pop_deferring_access_checks ();
17541
17542       /* If this is a member template declaration, let the front
17543          end know.  */
17544       if (member_p && !friend_p && decl)
17545         {
17546           if (TREE_CODE (decl) == TYPE_DECL)
17547             cp_parser_check_access_in_redeclaration (decl, token->location);
17548
17549           decl = finish_member_template_decl (decl);
17550         }
17551       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17552         make_friend_class (current_class_type, TREE_TYPE (decl),
17553                            /*complain=*/true);
17554     }
17555   /* We are done with the current parameter list.  */
17556   --parser->num_template_parameter_lists;
17557
17558   pop_deferring_access_checks ();
17559
17560   /* Finish up.  */
17561   finish_template_decl (parameter_list);
17562
17563   /* Register member declarations.  */
17564   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17565     finish_member_declaration (decl);
17566   /* For the erroneous case of a template with C linkage, we pushed an
17567      implicit C++ linkage scope; exit that scope now.  */
17568   if (need_lang_pop)
17569     pop_lang_context ();
17570   /* If DECL is a function template, we must return to parse it later.
17571      (Even though there is no definition, there might be default
17572      arguments that need handling.)  */
17573   if (member_p && decl
17574       && (TREE_CODE (decl) == FUNCTION_DECL
17575           || DECL_FUNCTION_TEMPLATE_P (decl)))
17576     TREE_VALUE (parser->unparsed_functions_queues)
17577       = tree_cons (NULL_TREE, decl,
17578                    TREE_VALUE (parser->unparsed_functions_queues));
17579 }
17580
17581 /* Perform the deferred access checks from a template-parameter-list.
17582    CHECKS is a TREE_LIST of access checks, as returned by
17583    get_deferred_access_checks.  */
17584
17585 static void
17586 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17587 {
17588   ++processing_template_parmlist;
17589   perform_access_checks (checks);
17590   --processing_template_parmlist;
17591 }
17592
17593 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17594    `function-definition' sequence.  MEMBER_P is true, this declaration
17595    appears in a class scope.
17596
17597    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17598    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17599
17600 static tree
17601 cp_parser_single_declaration (cp_parser* parser,
17602                               VEC (deferred_access_check,gc)* checks,
17603                               bool member_p,
17604                               bool explicit_specialization_p,
17605                               bool* friend_p)
17606 {
17607   int declares_class_or_enum;
17608   tree decl = NULL_TREE;
17609   cp_decl_specifier_seq decl_specifiers;
17610   bool function_definition_p = false;
17611   cp_token *decl_spec_token_start;
17612
17613   /* This function is only used when processing a template
17614      declaration.  */
17615   gcc_assert (innermost_scope_kind () == sk_template_parms
17616               || innermost_scope_kind () == sk_template_spec);
17617
17618   /* Defer access checks until we know what is being declared.  */
17619   push_deferring_access_checks (dk_deferred);
17620
17621   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17622      alternative.  */
17623   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17624   cp_parser_decl_specifier_seq (parser,
17625                                 CP_PARSER_FLAGS_OPTIONAL,
17626                                 &decl_specifiers,
17627                                 &declares_class_or_enum);
17628   if (friend_p)
17629     *friend_p = cp_parser_friend_p (&decl_specifiers);
17630
17631   /* There are no template typedefs.  */
17632   if (decl_specifiers.specs[(int) ds_typedef])
17633     {
17634       error ("%Htemplate declaration of %qs",
17635              &decl_spec_token_start->location, "typedef");
17636       decl = error_mark_node;
17637     }
17638
17639   /* Gather up the access checks that occurred the
17640      decl-specifier-seq.  */
17641   stop_deferring_access_checks ();
17642
17643   /* Check for the declaration of a template class.  */
17644   if (declares_class_or_enum)
17645     {
17646       if (cp_parser_declares_only_class_p (parser))
17647         {
17648           decl = shadow_tag (&decl_specifiers);
17649
17650           /* In this case:
17651
17652                struct C {
17653                  friend template <typename T> struct A<T>::B;
17654                };
17655
17656              A<T>::B will be represented by a TYPENAME_TYPE, and
17657              therefore not recognized by shadow_tag.  */
17658           if (friend_p && *friend_p
17659               && !decl
17660               && decl_specifiers.type
17661               && TYPE_P (decl_specifiers.type))
17662             decl = decl_specifiers.type;
17663
17664           if (decl && decl != error_mark_node)
17665             decl = TYPE_NAME (decl);
17666           else
17667             decl = error_mark_node;
17668
17669           /* Perform access checks for template parameters.  */
17670           cp_parser_perform_template_parameter_access_checks (checks);
17671         }
17672     }
17673   /* If it's not a template class, try for a template function.  If
17674      the next token is a `;', then this declaration does not declare
17675      anything.  But, if there were errors in the decl-specifiers, then
17676      the error might well have come from an attempted class-specifier.
17677      In that case, there's no need to warn about a missing declarator.  */
17678   if (!decl
17679       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17680           || decl_specifiers.type != error_mark_node))
17681     {
17682       decl = cp_parser_init_declarator (parser,
17683                                         &decl_specifiers,
17684                                         checks,
17685                                         /*function_definition_allowed_p=*/true,
17686                                         member_p,
17687                                         declares_class_or_enum,
17688                                         &function_definition_p);
17689
17690     /* 7.1.1-1 [dcl.stc]
17691
17692        A storage-class-specifier shall not be specified in an explicit
17693        specialization...  */
17694     if (decl
17695         && explicit_specialization_p
17696         && decl_specifiers.storage_class != sc_none)
17697       {
17698         error ("%Hexplicit template specialization cannot have a storage class",
17699                &decl_spec_token_start->location);
17700         decl = error_mark_node;
17701       }
17702     }
17703
17704   pop_deferring_access_checks ();
17705
17706   /* Clear any current qualification; whatever comes next is the start
17707      of something new.  */
17708   parser->scope = NULL_TREE;
17709   parser->qualifying_scope = NULL_TREE;
17710   parser->object_scope = NULL_TREE;
17711   /* Look for a trailing `;' after the declaration.  */
17712   if (!function_definition_p
17713       && (decl == error_mark_node
17714           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17715     cp_parser_skip_to_end_of_block_or_statement (parser);
17716
17717   return decl;
17718 }
17719
17720 /* Parse a cast-expression that is not the operand of a unary "&".  */
17721
17722 static tree
17723 cp_parser_simple_cast_expression (cp_parser *parser)
17724 {
17725   return cp_parser_cast_expression (parser, /*address_p=*/false,
17726                                     /*cast_p=*/false);
17727 }
17728
17729 /* Parse a functional cast to TYPE.  Returns an expression
17730    representing the cast.  */
17731
17732 static tree
17733 cp_parser_functional_cast (cp_parser* parser, tree type)
17734 {
17735   tree expression_list;
17736   tree cast;
17737   bool nonconst_p;
17738
17739   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17740     {
17741       maybe_warn_cpp0x ("extended initializer lists");
17742       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17743       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17744       if (TREE_CODE (type) == TYPE_DECL)
17745         type = TREE_TYPE (type);
17746       return finish_compound_literal (type, expression_list);
17747     }
17748
17749   expression_list
17750     = cp_parser_parenthesized_expression_list (parser, false,
17751                                                /*cast_p=*/true,
17752                                                /*allow_expansion_p=*/true,
17753                                                /*non_constant_p=*/NULL);
17754
17755   cast = build_functional_cast (type, expression_list,
17756                                 tf_warning_or_error);
17757   /* [expr.const]/1: In an integral constant expression "only type
17758      conversions to integral or enumeration type can be used".  */
17759   if (TREE_CODE (type) == TYPE_DECL)
17760     type = TREE_TYPE (type);
17761   if (cast != error_mark_node
17762       && !cast_valid_in_integral_constant_expression_p (type)
17763       && (cp_parser_non_integral_constant_expression
17764           (parser, "a call to a constructor")))
17765     return error_mark_node;
17766   return cast;
17767 }
17768
17769 /* Save the tokens that make up the body of a member function defined
17770    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17771    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17772    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17773    for the member function.  */
17774
17775 static tree
17776 cp_parser_save_member_function_body (cp_parser* parser,
17777                                      cp_decl_specifier_seq *decl_specifiers,
17778                                      cp_declarator *declarator,
17779                                      tree attributes)
17780 {
17781   cp_token *first;
17782   cp_token *last;
17783   tree fn;
17784
17785   /* Create the function-declaration.  */
17786   fn = start_method (decl_specifiers, declarator, attributes);
17787   /* If something went badly wrong, bail out now.  */
17788   if (fn == error_mark_node)
17789     {
17790       /* If there's a function-body, skip it.  */
17791       if (cp_parser_token_starts_function_definition_p
17792           (cp_lexer_peek_token (parser->lexer)))
17793         cp_parser_skip_to_end_of_block_or_statement (parser);
17794       return error_mark_node;
17795     }
17796
17797   /* Remember it, if there default args to post process.  */
17798   cp_parser_save_default_args (parser, fn);
17799
17800   /* Save away the tokens that make up the body of the
17801      function.  */
17802   first = parser->lexer->next_token;
17803   /* We can have braced-init-list mem-initializers before the fn body.  */
17804   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17805     {
17806       cp_lexer_consume_token (parser->lexer);
17807       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17808              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17809         {
17810           /* cache_group will stop after an un-nested { } pair, too.  */
17811           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17812             break;
17813
17814           /* variadic mem-inits have ... after the ')'.  */
17815           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17816             cp_lexer_consume_token (parser->lexer);
17817         }
17818     }
17819   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17820   /* Handle function try blocks.  */
17821   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17822     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17823   last = parser->lexer->next_token;
17824
17825   /* Save away the inline definition; we will process it when the
17826      class is complete.  */
17827   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17828   DECL_PENDING_INLINE_P (fn) = 1;
17829
17830   /* We need to know that this was defined in the class, so that
17831      friend templates are handled correctly.  */
17832   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17833
17834   /* We're done with the inline definition.  */
17835   finish_method (fn);
17836
17837   /* Add FN to the queue of functions to be parsed later.  */
17838   TREE_VALUE (parser->unparsed_functions_queues)
17839     = tree_cons (NULL_TREE, fn,
17840                  TREE_VALUE (parser->unparsed_functions_queues));
17841
17842   return fn;
17843 }
17844
17845 /* Parse a template-argument-list, as well as the trailing ">" (but
17846    not the opening ">").  See cp_parser_template_argument_list for the
17847    return value.  */
17848
17849 static tree
17850 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17851 {
17852   tree arguments;
17853   tree saved_scope;
17854   tree saved_qualifying_scope;
17855   tree saved_object_scope;
17856   bool saved_greater_than_is_operator_p;
17857   bool saved_skip_evaluation;
17858
17859   /* [temp.names]
17860
17861      When parsing a template-id, the first non-nested `>' is taken as
17862      the end of the template-argument-list rather than a greater-than
17863      operator.  */
17864   saved_greater_than_is_operator_p
17865     = parser->greater_than_is_operator_p;
17866   parser->greater_than_is_operator_p = false;
17867   /* Parsing the argument list may modify SCOPE, so we save it
17868      here.  */
17869   saved_scope = parser->scope;
17870   saved_qualifying_scope = parser->qualifying_scope;
17871   saved_object_scope = parser->object_scope;
17872   /* We need to evaluate the template arguments, even though this
17873      template-id may be nested within a "sizeof".  */
17874   saved_skip_evaluation = skip_evaluation;
17875   skip_evaluation = false;
17876   /* Parse the template-argument-list itself.  */
17877   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17878       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17879     arguments = NULL_TREE;
17880   else
17881     arguments = cp_parser_template_argument_list (parser);
17882   /* Look for the `>' that ends the template-argument-list. If we find
17883      a '>>' instead, it's probably just a typo.  */
17884   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17885     {
17886       if (cxx_dialect != cxx98)
17887         {
17888           /* In C++0x, a `>>' in a template argument list or cast
17889              expression is considered to be two separate `>'
17890              tokens. So, change the current token to a `>', but don't
17891              consume it: it will be consumed later when the outer
17892              template argument list (or cast expression) is parsed.
17893              Note that this replacement of `>' for `>>' is necessary
17894              even if we are parsing tentatively: in the tentative
17895              case, after calling
17896              cp_parser_enclosed_template_argument_list we will always
17897              throw away all of the template arguments and the first
17898              closing `>', either because the template argument list
17899              was erroneous or because we are replacing those tokens
17900              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17901              not have been thrown away) is needed either to close an
17902              outer template argument list or to complete a new-style
17903              cast.  */
17904           cp_token *token = cp_lexer_peek_token (parser->lexer);
17905           token->type = CPP_GREATER;
17906         }
17907       else if (!saved_greater_than_is_operator_p)
17908         {
17909           /* If we're in a nested template argument list, the '>>' has
17910             to be a typo for '> >'. We emit the error message, but we
17911             continue parsing and we push a '>' as next token, so that
17912             the argument list will be parsed correctly.  Note that the
17913             global source location is still on the token before the
17914             '>>', so we need to say explicitly where we want it.  */
17915           cp_token *token = cp_lexer_peek_token (parser->lexer);
17916           error ("%H%<>>%> should be %<> >%> "
17917                  "within a nested template argument list",
17918                  &token->location);
17919
17920           token->type = CPP_GREATER;
17921         }
17922       else
17923         {
17924           /* If this is not a nested template argument list, the '>>'
17925             is a typo for '>'. Emit an error message and continue.
17926             Same deal about the token location, but here we can get it
17927             right by consuming the '>>' before issuing the diagnostic.  */
17928           cp_token *token = cp_lexer_consume_token (parser->lexer);
17929           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17930                  "a template argument list", &token->location);
17931         }
17932     }
17933   else
17934     cp_parser_skip_to_end_of_template_parameter_list (parser);
17935   /* The `>' token might be a greater-than operator again now.  */
17936   parser->greater_than_is_operator_p
17937     = saved_greater_than_is_operator_p;
17938   /* Restore the SAVED_SCOPE.  */
17939   parser->scope = saved_scope;
17940   parser->qualifying_scope = saved_qualifying_scope;
17941   parser->object_scope = saved_object_scope;
17942   skip_evaluation = saved_skip_evaluation;
17943
17944   return arguments;
17945 }
17946
17947 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17948    arguments, or the body of the function have not yet been parsed,
17949    parse them now.  */
17950
17951 static void
17952 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17953 {
17954   /* If this member is a template, get the underlying
17955      FUNCTION_DECL.  */
17956   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17957     member_function = DECL_TEMPLATE_RESULT (member_function);
17958
17959   /* There should not be any class definitions in progress at this
17960      point; the bodies of members are only parsed outside of all class
17961      definitions.  */
17962   gcc_assert (parser->num_classes_being_defined == 0);
17963   /* While we're parsing the member functions we might encounter more
17964      classes.  We want to handle them right away, but we don't want
17965      them getting mixed up with functions that are currently in the
17966      queue.  */
17967   parser->unparsed_functions_queues
17968     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17969
17970   /* Make sure that any template parameters are in scope.  */
17971   maybe_begin_member_template_processing (member_function);
17972
17973   /* If the body of the function has not yet been parsed, parse it
17974      now.  */
17975   if (DECL_PENDING_INLINE_P (member_function))
17976     {
17977       tree function_scope;
17978       cp_token_cache *tokens;
17979
17980       /* The function is no longer pending; we are processing it.  */
17981       tokens = DECL_PENDING_INLINE_INFO (member_function);
17982       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17983       DECL_PENDING_INLINE_P (member_function) = 0;
17984
17985       /* If this is a local class, enter the scope of the containing
17986          function.  */
17987       function_scope = current_function_decl;
17988       if (function_scope)
17989         push_function_context ();
17990
17991       /* Push the body of the function onto the lexer stack.  */
17992       cp_parser_push_lexer_for_tokens (parser, tokens);
17993
17994       /* Let the front end know that we going to be defining this
17995          function.  */
17996       start_preparsed_function (member_function, NULL_TREE,
17997                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17998
17999       /* Don't do access checking if it is a templated function.  */
18000       if (processing_template_decl)
18001         push_deferring_access_checks (dk_no_check);
18002
18003       /* Now, parse the body of the function.  */
18004       cp_parser_function_definition_after_declarator (parser,
18005                                                       /*inline_p=*/true);
18006
18007       if (processing_template_decl)
18008         pop_deferring_access_checks ();
18009
18010       /* Leave the scope of the containing function.  */
18011       if (function_scope)
18012         pop_function_context ();
18013       cp_parser_pop_lexer (parser);
18014     }
18015
18016   /* Remove any template parameters from the symbol table.  */
18017   maybe_end_member_template_processing ();
18018
18019   /* Restore the queue.  */
18020   parser->unparsed_functions_queues
18021     = TREE_CHAIN (parser->unparsed_functions_queues);
18022 }
18023
18024 /* If DECL contains any default args, remember it on the unparsed
18025    functions queue.  */
18026
18027 static void
18028 cp_parser_save_default_args (cp_parser* parser, tree decl)
18029 {
18030   tree probe;
18031
18032   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18033        probe;
18034        probe = TREE_CHAIN (probe))
18035     if (TREE_PURPOSE (probe))
18036       {
18037         TREE_PURPOSE (parser->unparsed_functions_queues)
18038           = tree_cons (current_class_type, decl,
18039                        TREE_PURPOSE (parser->unparsed_functions_queues));
18040         break;
18041       }
18042 }
18043
18044 /* FN is a FUNCTION_DECL which may contains a parameter with an
18045    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18046    assumes that the current scope is the scope in which the default
18047    argument should be processed.  */
18048
18049 static void
18050 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18051 {
18052   bool saved_local_variables_forbidden_p;
18053   tree parm;
18054
18055   /* While we're parsing the default args, we might (due to the
18056      statement expression extension) encounter more classes.  We want
18057      to handle them right away, but we don't want them getting mixed
18058      up with default args that are currently in the queue.  */
18059   parser->unparsed_functions_queues
18060     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18061
18062   /* Local variable names (and the `this' keyword) may not appear
18063      in a default argument.  */
18064   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18065   parser->local_variables_forbidden_p = true;
18066
18067   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18068        parm;
18069        parm = TREE_CHAIN (parm))
18070     {
18071       cp_token_cache *tokens;
18072       tree default_arg = TREE_PURPOSE (parm);
18073       tree parsed_arg;
18074       VEC(tree,gc) *insts;
18075       tree copy;
18076       unsigned ix;
18077
18078       if (!default_arg)
18079         continue;
18080
18081       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18082         /* This can happen for a friend declaration for a function
18083            already declared with default arguments.  */
18084         continue;
18085
18086        /* Push the saved tokens for the default argument onto the parser's
18087           lexer stack.  */
18088       tokens = DEFARG_TOKENS (default_arg);
18089       cp_parser_push_lexer_for_tokens (parser, tokens);
18090
18091       /* Parse the assignment-expression.  */
18092       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18093
18094       if (!processing_template_decl)
18095         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18096
18097       TREE_PURPOSE (parm) = parsed_arg;
18098
18099       /* Update any instantiations we've already created.  */
18100       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18101            VEC_iterate (tree, insts, ix, copy); ix++)
18102         TREE_PURPOSE (copy) = parsed_arg;
18103
18104       /* If the token stream has not been completely used up, then
18105          there was extra junk after the end of the default
18106          argument.  */
18107       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18108         cp_parser_error (parser, "expected %<,%>");
18109
18110       /* Revert to the main lexer.  */
18111       cp_parser_pop_lexer (parser);
18112     }
18113
18114   /* Make sure no default arg is missing.  */
18115   check_default_args (fn);
18116
18117   /* Restore the state of local_variables_forbidden_p.  */
18118   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18119
18120   /* Restore the queue.  */
18121   parser->unparsed_functions_queues
18122     = TREE_CHAIN (parser->unparsed_functions_queues);
18123 }
18124
18125 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18126    either a TYPE or an expression, depending on the form of the
18127    input.  The KEYWORD indicates which kind of expression we have
18128    encountered.  */
18129
18130 static tree
18131 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18132 {
18133   tree expr = NULL_TREE;
18134   const char *saved_message;
18135   char *tmp;
18136   bool saved_integral_constant_expression_p;
18137   bool saved_non_integral_constant_expression_p;
18138   bool pack_expansion_p = false;
18139
18140   /* Types cannot be defined in a `sizeof' expression.  Save away the
18141      old message.  */
18142   saved_message = parser->type_definition_forbidden_message;
18143   /* And create the new one.  */
18144   tmp = concat ("types may not be defined in %<",
18145                 IDENTIFIER_POINTER (ridpointers[keyword]),
18146                 "%> expressions", NULL);
18147   parser->type_definition_forbidden_message = tmp;
18148
18149   /* The restrictions on constant-expressions do not apply inside
18150      sizeof expressions.  */
18151   saved_integral_constant_expression_p
18152     = parser->integral_constant_expression_p;
18153   saved_non_integral_constant_expression_p
18154     = parser->non_integral_constant_expression_p;
18155   parser->integral_constant_expression_p = false;
18156
18157   /* If it's a `...', then we are computing the length of a parameter
18158      pack.  */
18159   if (keyword == RID_SIZEOF
18160       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18161     {
18162       /* Consume the `...'.  */
18163       cp_lexer_consume_token (parser->lexer);
18164       maybe_warn_variadic_templates ();
18165
18166       /* Note that this is an expansion.  */
18167       pack_expansion_p = true;
18168     }
18169
18170   /* Do not actually evaluate the expression.  */
18171   ++skip_evaluation;
18172   /* If it's a `(', then we might be looking at the type-id
18173      construction.  */
18174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18175     {
18176       tree type;
18177       bool saved_in_type_id_in_expr_p;
18178
18179       /* We can't be sure yet whether we're looking at a type-id or an
18180          expression.  */
18181       cp_parser_parse_tentatively (parser);
18182       /* Consume the `('.  */
18183       cp_lexer_consume_token (parser->lexer);
18184       /* Parse the type-id.  */
18185       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18186       parser->in_type_id_in_expr_p = true;
18187       type = cp_parser_type_id (parser);
18188       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18189       /* Now, look for the trailing `)'.  */
18190       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18191       /* If all went well, then we're done.  */
18192       if (cp_parser_parse_definitely (parser))
18193         {
18194           cp_decl_specifier_seq decl_specs;
18195
18196           /* Build a trivial decl-specifier-seq.  */
18197           clear_decl_specs (&decl_specs);
18198           decl_specs.type = type;
18199
18200           /* Call grokdeclarator to figure out what type this is.  */
18201           expr = grokdeclarator (NULL,
18202                                  &decl_specs,
18203                                  TYPENAME,
18204                                  /*initialized=*/0,
18205                                  /*attrlist=*/NULL);
18206         }
18207     }
18208
18209   /* If the type-id production did not work out, then we must be
18210      looking at the unary-expression production.  */
18211   if (!expr)
18212     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18213                                        /*cast_p=*/false);
18214
18215   if (pack_expansion_p)
18216     /* Build a pack expansion. */
18217     expr = make_pack_expansion (expr);
18218
18219   /* Go back to evaluating expressions.  */
18220   --skip_evaluation;
18221
18222   /* Free the message we created.  */
18223   free (tmp);
18224   /* And restore the old one.  */
18225   parser->type_definition_forbidden_message = saved_message;
18226   parser->integral_constant_expression_p
18227     = saved_integral_constant_expression_p;
18228   parser->non_integral_constant_expression_p
18229     = saved_non_integral_constant_expression_p;
18230
18231   return expr;
18232 }
18233
18234 /* If the current declaration has no declarator, return true.  */
18235
18236 static bool
18237 cp_parser_declares_only_class_p (cp_parser *parser)
18238 {
18239   /* If the next token is a `;' or a `,' then there is no
18240      declarator.  */
18241   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18242           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18243 }
18244
18245 /* Update the DECL_SPECS to reflect the storage class indicated by
18246    KEYWORD.  */
18247
18248 static void
18249 cp_parser_set_storage_class (cp_parser *parser,
18250                              cp_decl_specifier_seq *decl_specs,
18251                              enum rid keyword,
18252                              location_t location)
18253 {
18254   cp_storage_class storage_class;
18255
18256   if (parser->in_unbraced_linkage_specification_p)
18257     {
18258       error ("%Hinvalid use of %qD in linkage specification",
18259              &location, ridpointers[keyword]);
18260       return;
18261     }
18262   else if (decl_specs->storage_class != sc_none)
18263     {
18264       decl_specs->conflicting_specifiers_p = true;
18265       return;
18266     }
18267
18268   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18269       && decl_specs->specs[(int) ds_thread])
18270     {
18271       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18272       decl_specs->specs[(int) ds_thread] = 0;
18273     }
18274
18275   switch (keyword)
18276     {
18277     case RID_AUTO:
18278       storage_class = sc_auto;
18279       break;
18280     case RID_REGISTER:
18281       storage_class = sc_register;
18282       break;
18283     case RID_STATIC:
18284       storage_class = sc_static;
18285       break;
18286     case RID_EXTERN:
18287       storage_class = sc_extern;
18288       break;
18289     case RID_MUTABLE:
18290       storage_class = sc_mutable;
18291       break;
18292     default:
18293       gcc_unreachable ();
18294     }
18295   decl_specs->storage_class = storage_class;
18296
18297   /* A storage class specifier cannot be applied alongside a typedef 
18298      specifier. If there is a typedef specifier present then set 
18299      conflicting_specifiers_p which will trigger an error later
18300      on in grokdeclarator. */
18301   if (decl_specs->specs[(int)ds_typedef])
18302     decl_specs->conflicting_specifiers_p = true;
18303 }
18304
18305 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18306    is true, the type is a user-defined type; otherwise it is a
18307    built-in type specified by a keyword.  */
18308
18309 static void
18310 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18311                               tree type_spec,
18312                               location_t location,
18313                               bool user_defined_p)
18314 {
18315   decl_specs->any_specifiers_p = true;
18316
18317   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18318      (with, for example, in "typedef int wchar_t;") we remember that
18319      this is what happened.  In system headers, we ignore these
18320      declarations so that G++ can work with system headers that are not
18321      C++-safe.  */
18322   if (decl_specs->specs[(int) ds_typedef]
18323       && !user_defined_p
18324       && (type_spec == boolean_type_node
18325           || type_spec == char16_type_node
18326           || type_spec == char32_type_node
18327           || type_spec == wchar_type_node)
18328       && (decl_specs->type
18329           || decl_specs->specs[(int) ds_long]
18330           || decl_specs->specs[(int) ds_short]
18331           || decl_specs->specs[(int) ds_unsigned]
18332           || decl_specs->specs[(int) ds_signed]))
18333     {
18334       decl_specs->redefined_builtin_type = type_spec;
18335       if (!decl_specs->type)
18336         {
18337           decl_specs->type = type_spec;
18338           decl_specs->user_defined_type_p = false;
18339           decl_specs->type_location = location;
18340         }
18341     }
18342   else if (decl_specs->type)
18343     decl_specs->multiple_types_p = true;
18344   else
18345     {
18346       decl_specs->type = type_spec;
18347       decl_specs->user_defined_type_p = user_defined_p;
18348       decl_specs->redefined_builtin_type = NULL_TREE;
18349       decl_specs->type_location = location;
18350     }
18351 }
18352
18353 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18354    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18355
18356 static bool
18357 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18358 {
18359   return decl_specifiers->specs[(int) ds_friend] != 0;
18360 }
18361
18362 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18363    issue an error message indicating that TOKEN_DESC was expected.
18364
18365    Returns the token consumed, if the token had the appropriate type.
18366    Otherwise, returns NULL.  */
18367
18368 static cp_token *
18369 cp_parser_require (cp_parser* parser,
18370                    enum cpp_ttype type,
18371                    const char* token_desc)
18372 {
18373   if (cp_lexer_next_token_is (parser->lexer, type))
18374     return cp_lexer_consume_token (parser->lexer);
18375   else
18376     {
18377       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18378       if (!cp_parser_simulate_error (parser))
18379         {
18380           char *message = concat ("expected ", token_desc, NULL);
18381           cp_parser_error (parser, message);
18382           free (message);
18383         }
18384       return NULL;
18385     }
18386 }
18387
18388 /* An error message is produced if the next token is not '>'.
18389    All further tokens are skipped until the desired token is
18390    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18391
18392 static void
18393 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18394 {
18395   /* Current level of '< ... >'.  */
18396   unsigned level = 0;
18397   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18398   unsigned nesting_depth = 0;
18399
18400   /* Are we ready, yet?  If not, issue error message.  */
18401   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18402     return;
18403
18404   /* Skip tokens until the desired token is found.  */
18405   while (true)
18406     {
18407       /* Peek at the next token.  */
18408       switch (cp_lexer_peek_token (parser->lexer)->type)
18409         {
18410         case CPP_LESS:
18411           if (!nesting_depth)
18412             ++level;
18413           break;
18414
18415         case CPP_RSHIFT:
18416           if (cxx_dialect == cxx98)
18417             /* C++0x views the `>>' operator as two `>' tokens, but
18418                C++98 does not. */
18419             break;
18420           else if (!nesting_depth && level-- == 0)
18421             {
18422               /* We've hit a `>>' where the first `>' closes the
18423                  template argument list, and the second `>' is
18424                  spurious.  Just consume the `>>' and stop; we've
18425                  already produced at least one error.  */
18426               cp_lexer_consume_token (parser->lexer);
18427               return;
18428             }
18429           /* Fall through for C++0x, so we handle the second `>' in
18430              the `>>'.  */
18431
18432         case CPP_GREATER:
18433           if (!nesting_depth && level-- == 0)
18434             {
18435               /* We've reached the token we want, consume it and stop.  */
18436               cp_lexer_consume_token (parser->lexer);
18437               return;
18438             }
18439           break;
18440
18441         case CPP_OPEN_PAREN:
18442         case CPP_OPEN_SQUARE:
18443           ++nesting_depth;
18444           break;
18445
18446         case CPP_CLOSE_PAREN:
18447         case CPP_CLOSE_SQUARE:
18448           if (nesting_depth-- == 0)
18449             return;
18450           break;
18451
18452         case CPP_EOF:
18453         case CPP_PRAGMA_EOL:
18454         case CPP_SEMICOLON:
18455         case CPP_OPEN_BRACE:
18456         case CPP_CLOSE_BRACE:
18457           /* The '>' was probably forgotten, don't look further.  */
18458           return;
18459
18460         default:
18461           break;
18462         }
18463
18464       /* Consume this token.  */
18465       cp_lexer_consume_token (parser->lexer);
18466     }
18467 }
18468
18469 /* If the next token is the indicated keyword, consume it.  Otherwise,
18470    issue an error message indicating that TOKEN_DESC was expected.
18471
18472    Returns the token consumed, if the token had the appropriate type.
18473    Otherwise, returns NULL.  */
18474
18475 static cp_token *
18476 cp_parser_require_keyword (cp_parser* parser,
18477                            enum rid keyword,
18478                            const char* token_desc)
18479 {
18480   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18481
18482   if (token && token->keyword != keyword)
18483     {
18484       dyn_string_t error_msg;
18485
18486       /* Format the error message.  */
18487       error_msg = dyn_string_new (0);
18488       dyn_string_append_cstr (error_msg, "expected ");
18489       dyn_string_append_cstr (error_msg, token_desc);
18490       cp_parser_error (parser, error_msg->s);
18491       dyn_string_delete (error_msg);
18492       return NULL;
18493     }
18494
18495   return token;
18496 }
18497
18498 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18499    function-definition.  */
18500
18501 static bool
18502 cp_parser_token_starts_function_definition_p (cp_token* token)
18503 {
18504   return (/* An ordinary function-body begins with an `{'.  */
18505           token->type == CPP_OPEN_BRACE
18506           /* A ctor-initializer begins with a `:'.  */
18507           || token->type == CPP_COLON
18508           /* A function-try-block begins with `try'.  */
18509           || token->keyword == RID_TRY
18510           /* The named return value extension begins with `return'.  */
18511           || token->keyword == RID_RETURN);
18512 }
18513
18514 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18515    definition.  */
18516
18517 static bool
18518 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18519 {
18520   cp_token *token;
18521
18522   token = cp_lexer_peek_token (parser->lexer);
18523   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18524 }
18525
18526 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18527    C++0x) ending a template-argument.  */
18528
18529 static bool
18530 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18531 {
18532   cp_token *token;
18533
18534   token = cp_lexer_peek_token (parser->lexer);
18535   return (token->type == CPP_COMMA 
18536           || token->type == CPP_GREATER
18537           || token->type == CPP_ELLIPSIS
18538           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18539 }
18540
18541 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18542    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18543
18544 static bool
18545 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18546                                                      size_t n)
18547 {
18548   cp_token *token;
18549
18550   token = cp_lexer_peek_nth_token (parser->lexer, n);
18551   if (token->type == CPP_LESS)
18552     return true;
18553   /* Check for the sequence `<::' in the original code. It would be lexed as
18554      `[:', where `[' is a digraph, and there is no whitespace before
18555      `:'.  */
18556   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18557     {
18558       cp_token *token2;
18559       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18560       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18561         return true;
18562     }
18563   return false;
18564 }
18565
18566 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18567    or none_type otherwise.  */
18568
18569 static enum tag_types
18570 cp_parser_token_is_class_key (cp_token* token)
18571 {
18572   switch (token->keyword)
18573     {
18574     case RID_CLASS:
18575       return class_type;
18576     case RID_STRUCT:
18577       return record_type;
18578     case RID_UNION:
18579       return union_type;
18580
18581     default:
18582       return none_type;
18583     }
18584 }
18585
18586 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18587
18588 static void
18589 cp_parser_check_class_key (enum tag_types class_key, tree type)
18590 {
18591   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18592     permerror (input_location, "%qs tag used in naming %q#T",
18593             class_key == union_type ? "union"
18594              : class_key == record_type ? "struct" : "class",
18595              type);
18596 }
18597
18598 /* Issue an error message if DECL is redeclared with different
18599    access than its original declaration [class.access.spec/3].
18600    This applies to nested classes and nested class templates.
18601    [class.mem/1].  */
18602
18603 static void
18604 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18605 {
18606   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18607     return;
18608
18609   if ((TREE_PRIVATE (decl)
18610        != (current_access_specifier == access_private_node))
18611       || (TREE_PROTECTED (decl)
18612           != (current_access_specifier == access_protected_node)))
18613     error ("%H%qD redeclared with different access", &location, decl);
18614 }
18615
18616 /* Look for the `template' keyword, as a syntactic disambiguator.
18617    Return TRUE iff it is present, in which case it will be
18618    consumed.  */
18619
18620 static bool
18621 cp_parser_optional_template_keyword (cp_parser *parser)
18622 {
18623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18624     {
18625       /* The `template' keyword can only be used within templates;
18626          outside templates the parser can always figure out what is a
18627          template and what is not.  */
18628       if (!processing_template_decl)
18629         {
18630           cp_token *token = cp_lexer_peek_token (parser->lexer);
18631           error ("%H%<template%> (as a disambiguator) is only allowed "
18632                  "within templates", &token->location);
18633           /* If this part of the token stream is rescanned, the same
18634              error message would be generated.  So, we purge the token
18635              from the stream.  */
18636           cp_lexer_purge_token (parser->lexer);
18637           return false;
18638         }
18639       else
18640         {
18641           /* Consume the `template' keyword.  */
18642           cp_lexer_consume_token (parser->lexer);
18643           return true;
18644         }
18645     }
18646
18647   return false;
18648 }
18649
18650 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18651    set PARSER->SCOPE, and perform other related actions.  */
18652
18653 static void
18654 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18655 {
18656   int i;
18657   struct tree_check *check_value;
18658   deferred_access_check *chk;
18659   VEC (deferred_access_check,gc) *checks;
18660
18661   /* Get the stored value.  */
18662   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18663   /* Perform any access checks that were deferred.  */
18664   checks = check_value->checks;
18665   if (checks)
18666     {
18667       for (i = 0 ;
18668            VEC_iterate (deferred_access_check, checks, i, chk) ;
18669            ++i)
18670         {
18671           perform_or_defer_access_check (chk->binfo,
18672                                          chk->decl,
18673                                          chk->diag_decl);
18674         }
18675     }
18676   /* Set the scope from the stored value.  */
18677   parser->scope = check_value->value;
18678   parser->qualifying_scope = check_value->qualifying_scope;
18679   parser->object_scope = NULL_TREE;
18680 }
18681
18682 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18683    encounter the end of a block before what we were looking for.  */
18684
18685 static bool
18686 cp_parser_cache_group (cp_parser *parser,
18687                        enum cpp_ttype end,
18688                        unsigned depth)
18689 {
18690   while (true)
18691     {
18692       cp_token *token = cp_lexer_peek_token (parser->lexer);
18693
18694       /* Abort a parenthesized expression if we encounter a semicolon.  */
18695       if ((end == CPP_CLOSE_PAREN || depth == 0)
18696           && token->type == CPP_SEMICOLON)
18697         return true;
18698       /* If we've reached the end of the file, stop.  */
18699       if (token->type == CPP_EOF
18700           || (end != CPP_PRAGMA_EOL
18701               && token->type == CPP_PRAGMA_EOL))
18702         return true;
18703       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18704         /* We've hit the end of an enclosing block, so there's been some
18705            kind of syntax error.  */
18706         return true;
18707
18708       /* Consume the token.  */
18709       cp_lexer_consume_token (parser->lexer);
18710       /* See if it starts a new group.  */
18711       if (token->type == CPP_OPEN_BRACE)
18712         {
18713           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18714           /* In theory this should probably check end == '}', but
18715              cp_parser_save_member_function_body needs it to exit
18716              after either '}' or ')' when called with ')'.  */
18717           if (depth == 0)
18718             return false;
18719         }
18720       else if (token->type == CPP_OPEN_PAREN)
18721         {
18722           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18723           if (depth == 0 && end == CPP_CLOSE_PAREN)
18724             return false;
18725         }
18726       else if (token->type == CPP_PRAGMA)
18727         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18728       else if (token->type == end)
18729         return false;
18730     }
18731 }
18732
18733 /* Begin parsing tentatively.  We always save tokens while parsing
18734    tentatively so that if the tentative parsing fails we can restore the
18735    tokens.  */
18736
18737 static void
18738 cp_parser_parse_tentatively (cp_parser* parser)
18739 {
18740   /* Enter a new parsing context.  */
18741   parser->context = cp_parser_context_new (parser->context);
18742   /* Begin saving tokens.  */
18743   cp_lexer_save_tokens (parser->lexer);
18744   /* In order to avoid repetitive access control error messages,
18745      access checks are queued up until we are no longer parsing
18746      tentatively.  */
18747   push_deferring_access_checks (dk_deferred);
18748 }
18749
18750 /* Commit to the currently active tentative parse.  */
18751
18752 static void
18753 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18754 {
18755   cp_parser_context *context;
18756   cp_lexer *lexer;
18757
18758   /* Mark all of the levels as committed.  */
18759   lexer = parser->lexer;
18760   for (context = parser->context; context->next; context = context->next)
18761     {
18762       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18763         break;
18764       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18765       while (!cp_lexer_saving_tokens (lexer))
18766         lexer = lexer->next;
18767       cp_lexer_commit_tokens (lexer);
18768     }
18769 }
18770
18771 /* Abort the currently active tentative parse.  All consumed tokens
18772    will be rolled back, and no diagnostics will be issued.  */
18773
18774 static void
18775 cp_parser_abort_tentative_parse (cp_parser* parser)
18776 {
18777   cp_parser_simulate_error (parser);
18778   /* Now, pretend that we want to see if the construct was
18779      successfully parsed.  */
18780   cp_parser_parse_definitely (parser);
18781 }
18782
18783 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18784    token stream.  Otherwise, commit to the tokens we have consumed.
18785    Returns true if no error occurred; false otherwise.  */
18786
18787 static bool
18788 cp_parser_parse_definitely (cp_parser* parser)
18789 {
18790   bool error_occurred;
18791   cp_parser_context *context;
18792
18793   /* Remember whether or not an error occurred, since we are about to
18794      destroy that information.  */
18795   error_occurred = cp_parser_error_occurred (parser);
18796   /* Remove the topmost context from the stack.  */
18797   context = parser->context;
18798   parser->context = context->next;
18799   /* If no parse errors occurred, commit to the tentative parse.  */
18800   if (!error_occurred)
18801     {
18802       /* Commit to the tokens read tentatively, unless that was
18803          already done.  */
18804       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18805         cp_lexer_commit_tokens (parser->lexer);
18806
18807       pop_to_parent_deferring_access_checks ();
18808     }
18809   /* Otherwise, if errors occurred, roll back our state so that things
18810      are just as they were before we began the tentative parse.  */
18811   else
18812     {
18813       cp_lexer_rollback_tokens (parser->lexer);
18814       pop_deferring_access_checks ();
18815     }
18816   /* Add the context to the front of the free list.  */
18817   context->next = cp_parser_context_free_list;
18818   cp_parser_context_free_list = context;
18819
18820   return !error_occurred;
18821 }
18822
18823 /* Returns true if we are parsing tentatively and are not committed to
18824    this tentative parse.  */
18825
18826 static bool
18827 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18828 {
18829   return (cp_parser_parsing_tentatively (parser)
18830           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18831 }
18832
18833 /* Returns nonzero iff an error has occurred during the most recent
18834    tentative parse.  */
18835
18836 static bool
18837 cp_parser_error_occurred (cp_parser* parser)
18838 {
18839   return (cp_parser_parsing_tentatively (parser)
18840           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18841 }
18842
18843 /* Returns nonzero if GNU extensions are allowed.  */
18844
18845 static bool
18846 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18847 {
18848   return parser->allow_gnu_extensions_p;
18849 }
18850 \f
18851 /* Objective-C++ Productions */
18852
18853
18854 /* Parse an Objective-C expression, which feeds into a primary-expression
18855    above.
18856
18857    objc-expression:
18858      objc-message-expression
18859      objc-string-literal
18860      objc-encode-expression
18861      objc-protocol-expression
18862      objc-selector-expression
18863
18864   Returns a tree representation of the expression.  */
18865
18866 static tree
18867 cp_parser_objc_expression (cp_parser* parser)
18868 {
18869   /* Try to figure out what kind of declaration is present.  */
18870   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18871
18872   switch (kwd->type)
18873     {
18874     case CPP_OPEN_SQUARE:
18875       return cp_parser_objc_message_expression (parser);
18876
18877     case CPP_OBJC_STRING:
18878       kwd = cp_lexer_consume_token (parser->lexer);
18879       return objc_build_string_object (kwd->u.value);
18880
18881     case CPP_KEYWORD:
18882       switch (kwd->keyword)
18883         {
18884         case RID_AT_ENCODE:
18885           return cp_parser_objc_encode_expression (parser);
18886
18887         case RID_AT_PROTOCOL:
18888           return cp_parser_objc_protocol_expression (parser);
18889
18890         case RID_AT_SELECTOR:
18891           return cp_parser_objc_selector_expression (parser);
18892
18893         default:
18894           break;
18895         }
18896     default:
18897       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18898              &kwd->location, kwd->u.value);
18899       cp_parser_skip_to_end_of_block_or_statement (parser);
18900     }
18901
18902   return error_mark_node;
18903 }
18904
18905 /* Parse an Objective-C message expression.
18906
18907    objc-message-expression:
18908      [ objc-message-receiver objc-message-args ]
18909
18910    Returns a representation of an Objective-C message.  */
18911
18912 static tree
18913 cp_parser_objc_message_expression (cp_parser* parser)
18914 {
18915   tree receiver, messageargs;
18916
18917   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18918   receiver = cp_parser_objc_message_receiver (parser);
18919   messageargs = cp_parser_objc_message_args (parser);
18920   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18921
18922   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18923 }
18924
18925 /* Parse an objc-message-receiver.
18926
18927    objc-message-receiver:
18928      expression
18929      simple-type-specifier
18930
18931   Returns a representation of the type or expression.  */
18932
18933 static tree
18934 cp_parser_objc_message_receiver (cp_parser* parser)
18935 {
18936   tree rcv;
18937
18938   /* An Objective-C message receiver may be either (1) a type
18939      or (2) an expression.  */
18940   cp_parser_parse_tentatively (parser);
18941   rcv = cp_parser_expression (parser, false);
18942
18943   if (cp_parser_parse_definitely (parser))
18944     return rcv;
18945
18946   rcv = cp_parser_simple_type_specifier (parser,
18947                                          /*decl_specs=*/NULL,
18948                                          CP_PARSER_FLAGS_NONE);
18949
18950   return objc_get_class_reference (rcv);
18951 }
18952
18953 /* Parse the arguments and selectors comprising an Objective-C message.
18954
18955    objc-message-args:
18956      objc-selector
18957      objc-selector-args
18958      objc-selector-args , objc-comma-args
18959
18960    objc-selector-args:
18961      objc-selector [opt] : assignment-expression
18962      objc-selector-args objc-selector [opt] : assignment-expression
18963
18964    objc-comma-args:
18965      assignment-expression
18966      objc-comma-args , assignment-expression
18967
18968    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18969    selector arguments and TREE_VALUE containing a list of comma
18970    arguments.  */
18971
18972 static tree
18973 cp_parser_objc_message_args (cp_parser* parser)
18974 {
18975   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18976   bool maybe_unary_selector_p = true;
18977   cp_token *token = cp_lexer_peek_token (parser->lexer);
18978
18979   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18980     {
18981       tree selector = NULL_TREE, arg;
18982
18983       if (token->type != CPP_COLON)
18984         selector = cp_parser_objc_selector (parser);
18985
18986       /* Detect if we have a unary selector.  */
18987       if (maybe_unary_selector_p
18988           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18989         return build_tree_list (selector, NULL_TREE);
18990
18991       maybe_unary_selector_p = false;
18992       cp_parser_require (parser, CPP_COLON, "%<:%>");
18993       arg = cp_parser_assignment_expression (parser, false);
18994
18995       sel_args
18996         = chainon (sel_args,
18997                    build_tree_list (selector, arg));
18998
18999       token = cp_lexer_peek_token (parser->lexer);
19000     }
19001
19002   /* Handle non-selector arguments, if any. */
19003   while (token->type == CPP_COMMA)
19004     {
19005       tree arg;
19006
19007       cp_lexer_consume_token (parser->lexer);
19008       arg = cp_parser_assignment_expression (parser, false);
19009
19010       addl_args
19011         = chainon (addl_args,
19012                    build_tree_list (NULL_TREE, arg));
19013
19014       token = cp_lexer_peek_token (parser->lexer);
19015     }
19016
19017   return build_tree_list (sel_args, addl_args);
19018 }
19019
19020 /* Parse an Objective-C encode expression.
19021
19022    objc-encode-expression:
19023      @encode objc-typename
19024
19025    Returns an encoded representation of the type argument.  */
19026
19027 static tree
19028 cp_parser_objc_encode_expression (cp_parser* parser)
19029 {
19030   tree type;
19031   cp_token *token;
19032
19033   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19034   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19035   token = cp_lexer_peek_token (parser->lexer);
19036   type = complete_type (cp_parser_type_id (parser));
19037   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19038
19039   if (!type)
19040     {
19041       error ("%H%<@encode%> must specify a type as an argument",
19042              &token->location);
19043       return error_mark_node;
19044     }
19045
19046   return objc_build_encode_expr (type);
19047 }
19048
19049 /* Parse an Objective-C @defs expression.  */
19050
19051 static tree
19052 cp_parser_objc_defs_expression (cp_parser *parser)
19053 {
19054   tree name;
19055
19056   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19057   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19058   name = cp_parser_identifier (parser);
19059   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19060
19061   return objc_get_class_ivars (name);
19062 }
19063
19064 /* Parse an Objective-C protocol expression.
19065
19066   objc-protocol-expression:
19067     @protocol ( identifier )
19068
19069   Returns a representation of the protocol expression.  */
19070
19071 static tree
19072 cp_parser_objc_protocol_expression (cp_parser* parser)
19073 {
19074   tree proto;
19075
19076   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19077   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19078   proto = cp_parser_identifier (parser);
19079   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19080
19081   return objc_build_protocol_expr (proto);
19082 }
19083
19084 /* Parse an Objective-C selector expression.
19085
19086    objc-selector-expression:
19087      @selector ( objc-method-signature )
19088
19089    objc-method-signature:
19090      objc-selector
19091      objc-selector-seq
19092
19093    objc-selector-seq:
19094      objc-selector :
19095      objc-selector-seq objc-selector :
19096
19097   Returns a representation of the method selector.  */
19098
19099 static tree
19100 cp_parser_objc_selector_expression (cp_parser* parser)
19101 {
19102   tree sel_seq = NULL_TREE;
19103   bool maybe_unary_selector_p = true;
19104   cp_token *token;
19105
19106   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19107   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19108   token = cp_lexer_peek_token (parser->lexer);
19109
19110   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19111          || token->type == CPP_SCOPE)
19112     {
19113       tree selector = NULL_TREE;
19114
19115       if (token->type != CPP_COLON
19116           || token->type == CPP_SCOPE)
19117         selector = cp_parser_objc_selector (parser);
19118
19119       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19120           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19121         {
19122           /* Detect if we have a unary selector.  */
19123           if (maybe_unary_selector_p)
19124             {
19125               sel_seq = selector;
19126               goto finish_selector;
19127             }
19128           else
19129             {
19130               cp_parser_error (parser, "expected %<:%>");
19131             }
19132         }
19133       maybe_unary_selector_p = false;
19134       token = cp_lexer_consume_token (parser->lexer);
19135
19136       if (token->type == CPP_SCOPE)
19137         {
19138           sel_seq
19139             = chainon (sel_seq,
19140                        build_tree_list (selector, NULL_TREE));
19141           sel_seq
19142             = chainon (sel_seq,
19143                        build_tree_list (NULL_TREE, NULL_TREE));
19144         }
19145       else
19146         sel_seq
19147           = chainon (sel_seq,
19148                      build_tree_list (selector, NULL_TREE));
19149
19150       token = cp_lexer_peek_token (parser->lexer);
19151     }
19152
19153  finish_selector:
19154   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19155
19156   return objc_build_selector_expr (sel_seq);
19157 }
19158
19159 /* Parse a list of identifiers.
19160
19161    objc-identifier-list:
19162      identifier
19163      objc-identifier-list , identifier
19164
19165    Returns a TREE_LIST of identifier nodes.  */
19166
19167 static tree
19168 cp_parser_objc_identifier_list (cp_parser* parser)
19169 {
19170   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19171   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19172
19173   while (sep->type == CPP_COMMA)
19174     {
19175       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19176       list = chainon (list,
19177                       build_tree_list (NULL_TREE,
19178                                        cp_parser_identifier (parser)));
19179       sep = cp_lexer_peek_token (parser->lexer);
19180     }
19181
19182   return list;
19183 }
19184
19185 /* Parse an Objective-C alias declaration.
19186
19187    objc-alias-declaration:
19188      @compatibility_alias identifier identifier ;
19189
19190    This function registers the alias mapping with the Objective-C front end.
19191    It returns nothing.  */
19192
19193 static void
19194 cp_parser_objc_alias_declaration (cp_parser* parser)
19195 {
19196   tree alias, orig;
19197
19198   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19199   alias = cp_parser_identifier (parser);
19200   orig = cp_parser_identifier (parser);
19201   objc_declare_alias (alias, orig);
19202   cp_parser_consume_semicolon_at_end_of_statement (parser);
19203 }
19204
19205 /* Parse an Objective-C class forward-declaration.
19206
19207    objc-class-declaration:
19208      @class objc-identifier-list ;
19209
19210    The function registers the forward declarations with the Objective-C
19211    front end.  It returns nothing.  */
19212
19213 static void
19214 cp_parser_objc_class_declaration (cp_parser* parser)
19215 {
19216   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19217   objc_declare_class (cp_parser_objc_identifier_list (parser));
19218   cp_parser_consume_semicolon_at_end_of_statement (parser);
19219 }
19220
19221 /* Parse a list of Objective-C protocol references.
19222
19223    objc-protocol-refs-opt:
19224      objc-protocol-refs [opt]
19225
19226    objc-protocol-refs:
19227      < objc-identifier-list >
19228
19229    Returns a TREE_LIST of identifiers, if any.  */
19230
19231 static tree
19232 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19233 {
19234   tree protorefs = NULL_TREE;
19235
19236   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19237     {
19238       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19239       protorefs = cp_parser_objc_identifier_list (parser);
19240       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19241     }
19242
19243   return protorefs;
19244 }
19245
19246 /* Parse a Objective-C visibility specification.  */
19247
19248 static void
19249 cp_parser_objc_visibility_spec (cp_parser* parser)
19250 {
19251   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19252
19253   switch (vis->keyword)
19254     {
19255     case RID_AT_PRIVATE:
19256       objc_set_visibility (2);
19257       break;
19258     case RID_AT_PROTECTED:
19259       objc_set_visibility (0);
19260       break;
19261     case RID_AT_PUBLIC:
19262       objc_set_visibility (1);
19263       break;
19264     default:
19265       return;
19266     }
19267
19268   /* Eat '@private'/'@protected'/'@public'.  */
19269   cp_lexer_consume_token (parser->lexer);
19270 }
19271
19272 /* Parse an Objective-C method type.  */
19273
19274 static void
19275 cp_parser_objc_method_type (cp_parser* parser)
19276 {
19277   objc_set_method_type
19278    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19279     ? PLUS_EXPR
19280     : MINUS_EXPR);
19281 }
19282
19283 /* Parse an Objective-C protocol qualifier.  */
19284
19285 static tree
19286 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19287 {
19288   tree quals = NULL_TREE, node;
19289   cp_token *token = cp_lexer_peek_token (parser->lexer);
19290
19291   node = token->u.value;
19292
19293   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19294          && (node == ridpointers [(int) RID_IN]
19295              || node == ridpointers [(int) RID_OUT]
19296              || node == ridpointers [(int) RID_INOUT]
19297              || node == ridpointers [(int) RID_BYCOPY]
19298              || node == ridpointers [(int) RID_BYREF]
19299              || node == ridpointers [(int) RID_ONEWAY]))
19300     {
19301       quals = tree_cons (NULL_TREE, node, quals);
19302       cp_lexer_consume_token (parser->lexer);
19303       token = cp_lexer_peek_token (parser->lexer);
19304       node = token->u.value;
19305     }
19306
19307   return quals;
19308 }
19309
19310 /* Parse an Objective-C typename.  */
19311
19312 static tree
19313 cp_parser_objc_typename (cp_parser* parser)
19314 {
19315   tree type_name = NULL_TREE;
19316
19317   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19318     {
19319       tree proto_quals, cp_type = NULL_TREE;
19320
19321       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19322       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19323
19324       /* An ObjC type name may consist of just protocol qualifiers, in which
19325          case the type shall default to 'id'.  */
19326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19327         cp_type = cp_parser_type_id (parser);
19328
19329       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19330       type_name = build_tree_list (proto_quals, cp_type);
19331     }
19332
19333   return type_name;
19334 }
19335
19336 /* Check to see if TYPE refers to an Objective-C selector name.  */
19337
19338 static bool
19339 cp_parser_objc_selector_p (enum cpp_ttype type)
19340 {
19341   return (type == CPP_NAME || type == CPP_KEYWORD
19342           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19343           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19344           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19345           || type == CPP_XOR || type == CPP_XOR_EQ);
19346 }
19347
19348 /* Parse an Objective-C selector.  */
19349
19350 static tree
19351 cp_parser_objc_selector (cp_parser* parser)
19352 {
19353   cp_token *token = cp_lexer_consume_token (parser->lexer);
19354
19355   if (!cp_parser_objc_selector_p (token->type))
19356     {
19357       error ("%Hinvalid Objective-C++ selector name", &token->location);
19358       return error_mark_node;
19359     }
19360
19361   /* C++ operator names are allowed to appear in ObjC selectors.  */
19362   switch (token->type)
19363     {
19364     case CPP_AND_AND: return get_identifier ("and");
19365     case CPP_AND_EQ: return get_identifier ("and_eq");
19366     case CPP_AND: return get_identifier ("bitand");
19367     case CPP_OR: return get_identifier ("bitor");
19368     case CPP_COMPL: return get_identifier ("compl");
19369     case CPP_NOT: return get_identifier ("not");
19370     case CPP_NOT_EQ: return get_identifier ("not_eq");
19371     case CPP_OR_OR: return get_identifier ("or");
19372     case CPP_OR_EQ: return get_identifier ("or_eq");
19373     case CPP_XOR: return get_identifier ("xor");
19374     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19375     default: return token->u.value;
19376     }
19377 }
19378
19379 /* Parse an Objective-C params list.  */
19380
19381 static tree
19382 cp_parser_objc_method_keyword_params (cp_parser* parser)
19383 {
19384   tree params = NULL_TREE;
19385   bool maybe_unary_selector_p = true;
19386   cp_token *token = cp_lexer_peek_token (parser->lexer);
19387
19388   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19389     {
19390       tree selector = NULL_TREE, type_name, identifier;
19391
19392       if (token->type != CPP_COLON)
19393         selector = cp_parser_objc_selector (parser);
19394
19395       /* Detect if we have a unary selector.  */
19396       if (maybe_unary_selector_p
19397           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19398         return selector;
19399
19400       maybe_unary_selector_p = false;
19401       cp_parser_require (parser, CPP_COLON, "%<:%>");
19402       type_name = cp_parser_objc_typename (parser);
19403       identifier = cp_parser_identifier (parser);
19404
19405       params
19406         = chainon (params,
19407                    objc_build_keyword_decl (selector,
19408                                             type_name,
19409                                             identifier));
19410
19411       token = cp_lexer_peek_token (parser->lexer);
19412     }
19413
19414   return params;
19415 }
19416
19417 /* Parse the non-keyword Objective-C params.  */
19418
19419 static tree
19420 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19421 {
19422   tree params = make_node (TREE_LIST);
19423   cp_token *token = cp_lexer_peek_token (parser->lexer);
19424   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19425
19426   while (token->type == CPP_COMMA)
19427     {
19428       cp_parameter_declarator *parmdecl;
19429       tree parm;
19430
19431       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19432       token = cp_lexer_peek_token (parser->lexer);
19433
19434       if (token->type == CPP_ELLIPSIS)
19435         {
19436           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19437           *ellipsisp = true;
19438           break;
19439         }
19440
19441       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19442       parm = grokdeclarator (parmdecl->declarator,
19443                              &parmdecl->decl_specifiers,
19444                              PARM, /*initialized=*/0,
19445                              /*attrlist=*/NULL);
19446
19447       chainon (params, build_tree_list (NULL_TREE, parm));
19448       token = cp_lexer_peek_token (parser->lexer);
19449     }
19450
19451   return params;
19452 }
19453
19454 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19455
19456 static void
19457 cp_parser_objc_interstitial_code (cp_parser* parser)
19458 {
19459   cp_token *token = cp_lexer_peek_token (parser->lexer);
19460
19461   /* If the next token is `extern' and the following token is a string
19462      literal, then we have a linkage specification.  */
19463   if (token->keyword == RID_EXTERN
19464       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19465     cp_parser_linkage_specification (parser);
19466   /* Handle #pragma, if any.  */
19467   else if (token->type == CPP_PRAGMA)
19468     cp_parser_pragma (parser, pragma_external);
19469   /* Allow stray semicolons.  */
19470   else if (token->type == CPP_SEMICOLON)
19471     cp_lexer_consume_token (parser->lexer);
19472   /* Finally, try to parse a block-declaration, or a function-definition.  */
19473   else
19474     cp_parser_block_declaration (parser, /*statement_p=*/false);
19475 }
19476
19477 /* Parse a method signature.  */
19478
19479 static tree
19480 cp_parser_objc_method_signature (cp_parser* parser)
19481 {
19482   tree rettype, kwdparms, optparms;
19483   bool ellipsis = false;
19484
19485   cp_parser_objc_method_type (parser);
19486   rettype = cp_parser_objc_typename (parser);
19487   kwdparms = cp_parser_objc_method_keyword_params (parser);
19488   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19489
19490   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19491 }
19492
19493 /* Pars an Objective-C method prototype list.  */
19494
19495 static void
19496 cp_parser_objc_method_prototype_list (cp_parser* parser)
19497 {
19498   cp_token *token = cp_lexer_peek_token (parser->lexer);
19499
19500   while (token->keyword != RID_AT_END)
19501     {
19502       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19503         {
19504           objc_add_method_declaration
19505            (cp_parser_objc_method_signature (parser));
19506           cp_parser_consume_semicolon_at_end_of_statement (parser);
19507         }
19508       else
19509         /* Allow for interspersed non-ObjC++ code.  */
19510         cp_parser_objc_interstitial_code (parser);
19511
19512       token = cp_lexer_peek_token (parser->lexer);
19513     }
19514
19515   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19516   objc_finish_interface ();
19517 }
19518
19519 /* Parse an Objective-C method definition list.  */
19520
19521 static void
19522 cp_parser_objc_method_definition_list (cp_parser* parser)
19523 {
19524   cp_token *token = cp_lexer_peek_token (parser->lexer);
19525
19526   while (token->keyword != RID_AT_END)
19527     {
19528       tree meth;
19529
19530       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19531         {
19532           push_deferring_access_checks (dk_deferred);
19533           objc_start_method_definition
19534            (cp_parser_objc_method_signature (parser));
19535
19536           /* For historical reasons, we accept an optional semicolon.  */
19537           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19538             cp_lexer_consume_token (parser->lexer);
19539
19540           perform_deferred_access_checks ();
19541           stop_deferring_access_checks ();
19542           meth = cp_parser_function_definition_after_declarator (parser,
19543                                                                  false);
19544           pop_deferring_access_checks ();
19545           objc_finish_method_definition (meth);
19546         }
19547       else
19548         /* Allow for interspersed non-ObjC++ code.  */
19549         cp_parser_objc_interstitial_code (parser);
19550
19551       token = cp_lexer_peek_token (parser->lexer);
19552     }
19553
19554   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19555   objc_finish_implementation ();
19556 }
19557
19558 /* Parse Objective-C ivars.  */
19559
19560 static void
19561 cp_parser_objc_class_ivars (cp_parser* parser)
19562 {
19563   cp_token *token = cp_lexer_peek_token (parser->lexer);
19564
19565   if (token->type != CPP_OPEN_BRACE)
19566     return;     /* No ivars specified.  */
19567
19568   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19569   token = cp_lexer_peek_token (parser->lexer);
19570
19571   while (token->type != CPP_CLOSE_BRACE)
19572     {
19573       cp_decl_specifier_seq declspecs;
19574       int decl_class_or_enum_p;
19575       tree prefix_attributes;
19576
19577       cp_parser_objc_visibility_spec (parser);
19578
19579       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19580         break;
19581
19582       cp_parser_decl_specifier_seq (parser,
19583                                     CP_PARSER_FLAGS_OPTIONAL,
19584                                     &declspecs,
19585                                     &decl_class_or_enum_p);
19586       prefix_attributes = declspecs.attributes;
19587       declspecs.attributes = NULL_TREE;
19588
19589       /* Keep going until we hit the `;' at the end of the
19590          declaration.  */
19591       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19592         {
19593           tree width = NULL_TREE, attributes, first_attribute, decl;
19594           cp_declarator *declarator = NULL;
19595           int ctor_dtor_or_conv_p;
19596
19597           /* Check for a (possibly unnamed) bitfield declaration.  */
19598           token = cp_lexer_peek_token (parser->lexer);
19599           if (token->type == CPP_COLON)
19600             goto eat_colon;
19601
19602           if (token->type == CPP_NAME
19603               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19604                   == CPP_COLON))
19605             {
19606               /* Get the name of the bitfield.  */
19607               declarator = make_id_declarator (NULL_TREE,
19608                                                cp_parser_identifier (parser),
19609                                                sfk_none);
19610
19611              eat_colon:
19612               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19613               /* Get the width of the bitfield.  */
19614               width
19615                 = cp_parser_constant_expression (parser,
19616                                                  /*allow_non_constant=*/false,
19617                                                  NULL);
19618             }
19619           else
19620             {
19621               /* Parse the declarator.  */
19622               declarator
19623                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19624                                         &ctor_dtor_or_conv_p,
19625                                         /*parenthesized_p=*/NULL,
19626                                         /*member_p=*/false);
19627             }
19628
19629           /* Look for attributes that apply to the ivar.  */
19630           attributes = cp_parser_attributes_opt (parser);
19631           /* Remember which attributes are prefix attributes and
19632              which are not.  */
19633           first_attribute = attributes;
19634           /* Combine the attributes.  */
19635           attributes = chainon (prefix_attributes, attributes);
19636
19637           if (width)
19638               /* Create the bitfield declaration.  */
19639               decl = grokbitfield (declarator, &declspecs,
19640                                    width,
19641                                    attributes);
19642           else
19643             decl = grokfield (declarator, &declspecs,
19644                               NULL_TREE, /*init_const_expr_p=*/false,
19645                               NULL_TREE, attributes);
19646
19647           /* Add the instance variable.  */
19648           objc_add_instance_variable (decl);
19649
19650           /* Reset PREFIX_ATTRIBUTES.  */
19651           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19652             attributes = TREE_CHAIN (attributes);
19653           if (attributes)
19654             TREE_CHAIN (attributes) = NULL_TREE;
19655
19656           token = cp_lexer_peek_token (parser->lexer);
19657
19658           if (token->type == CPP_COMMA)
19659             {
19660               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19661               continue;
19662             }
19663           break;
19664         }
19665
19666       cp_parser_consume_semicolon_at_end_of_statement (parser);
19667       token = cp_lexer_peek_token (parser->lexer);
19668     }
19669
19670   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19671   /* For historical reasons, we accept an optional semicolon.  */
19672   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19673     cp_lexer_consume_token (parser->lexer);
19674 }
19675
19676 /* Parse an Objective-C protocol declaration.  */
19677
19678 static void
19679 cp_parser_objc_protocol_declaration (cp_parser* parser)
19680 {
19681   tree proto, protorefs;
19682   cp_token *tok;
19683
19684   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19685   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19686     {
19687       tok = cp_lexer_peek_token (parser->lexer);
19688       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19689       goto finish;
19690     }
19691
19692   /* See if we have a forward declaration or a definition.  */
19693   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19694
19695   /* Try a forward declaration first.  */
19696   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19697     {
19698       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19699      finish:
19700       cp_parser_consume_semicolon_at_end_of_statement (parser);
19701     }
19702
19703   /* Ok, we got a full-fledged definition (or at least should).  */
19704   else
19705     {
19706       proto = cp_parser_identifier (parser);
19707       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19708       objc_start_protocol (proto, protorefs);
19709       cp_parser_objc_method_prototype_list (parser);
19710     }
19711 }
19712
19713 /* Parse an Objective-C superclass or category.  */
19714
19715 static void
19716 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19717                                                           tree *categ)
19718 {
19719   cp_token *next = cp_lexer_peek_token (parser->lexer);
19720
19721   *super = *categ = NULL_TREE;
19722   if (next->type == CPP_COLON)
19723     {
19724       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19725       *super = cp_parser_identifier (parser);
19726     }
19727   else if (next->type == CPP_OPEN_PAREN)
19728     {
19729       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19730       *categ = cp_parser_identifier (parser);
19731       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19732     }
19733 }
19734
19735 /* Parse an Objective-C class interface.  */
19736
19737 static void
19738 cp_parser_objc_class_interface (cp_parser* parser)
19739 {
19740   tree name, super, categ, protos;
19741
19742   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19743   name = cp_parser_identifier (parser);
19744   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19745   protos = cp_parser_objc_protocol_refs_opt (parser);
19746
19747   /* We have either a class or a category on our hands.  */
19748   if (categ)
19749     objc_start_category_interface (name, categ, protos);
19750   else
19751     {
19752       objc_start_class_interface (name, super, protos);
19753       /* Handle instance variable declarations, if any.  */
19754       cp_parser_objc_class_ivars (parser);
19755       objc_continue_interface ();
19756     }
19757
19758   cp_parser_objc_method_prototype_list (parser);
19759 }
19760
19761 /* Parse an Objective-C class implementation.  */
19762
19763 static void
19764 cp_parser_objc_class_implementation (cp_parser* parser)
19765 {
19766   tree name, super, categ;
19767
19768   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19769   name = cp_parser_identifier (parser);
19770   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19771
19772   /* We have either a class or a category on our hands.  */
19773   if (categ)
19774     objc_start_category_implementation (name, categ);
19775   else
19776     {
19777       objc_start_class_implementation (name, super);
19778       /* Handle instance variable declarations, if any.  */
19779       cp_parser_objc_class_ivars (parser);
19780       objc_continue_implementation ();
19781     }
19782
19783   cp_parser_objc_method_definition_list (parser);
19784 }
19785
19786 /* Consume the @end token and finish off the implementation.  */
19787
19788 static void
19789 cp_parser_objc_end_implementation (cp_parser* parser)
19790 {
19791   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19792   objc_finish_implementation ();
19793 }
19794
19795 /* Parse an Objective-C declaration.  */
19796
19797 static void
19798 cp_parser_objc_declaration (cp_parser* parser)
19799 {
19800   /* Try to figure out what kind of declaration is present.  */
19801   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19802
19803   switch (kwd->keyword)
19804     {
19805     case RID_AT_ALIAS:
19806       cp_parser_objc_alias_declaration (parser);
19807       break;
19808     case RID_AT_CLASS:
19809       cp_parser_objc_class_declaration (parser);
19810       break;
19811     case RID_AT_PROTOCOL:
19812       cp_parser_objc_protocol_declaration (parser);
19813       break;
19814     case RID_AT_INTERFACE:
19815       cp_parser_objc_class_interface (parser);
19816       break;
19817     case RID_AT_IMPLEMENTATION:
19818       cp_parser_objc_class_implementation (parser);
19819       break;
19820     case RID_AT_END:
19821       cp_parser_objc_end_implementation (parser);
19822       break;
19823     default:
19824       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19825              &kwd->location, kwd->u.value);
19826       cp_parser_skip_to_end_of_block_or_statement (parser);
19827     }
19828 }
19829
19830 /* Parse an Objective-C try-catch-finally statement.
19831
19832    objc-try-catch-finally-stmt:
19833      @try compound-statement objc-catch-clause-seq [opt]
19834        objc-finally-clause [opt]
19835
19836    objc-catch-clause-seq:
19837      objc-catch-clause objc-catch-clause-seq [opt]
19838
19839    objc-catch-clause:
19840      @catch ( exception-declaration ) compound-statement
19841
19842    objc-finally-clause
19843      @finally compound-statement
19844
19845    Returns NULL_TREE.  */
19846
19847 static tree
19848 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19849   location_t location;
19850   tree stmt;
19851
19852   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19853   location = cp_lexer_peek_token (parser->lexer)->location;
19854   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19855      node, lest it get absorbed into the surrounding block.  */
19856   stmt = push_stmt_list ();
19857   cp_parser_compound_statement (parser, NULL, false);
19858   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19859
19860   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19861     {
19862       cp_parameter_declarator *parmdecl;
19863       tree parm;
19864
19865       cp_lexer_consume_token (parser->lexer);
19866       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19867       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19868       parm = grokdeclarator (parmdecl->declarator,
19869                              &parmdecl->decl_specifiers,
19870                              PARM, /*initialized=*/0,
19871                              /*attrlist=*/NULL);
19872       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19873       objc_begin_catch_clause (parm);
19874       cp_parser_compound_statement (parser, NULL, false);
19875       objc_finish_catch_clause ();
19876     }
19877
19878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19879     {
19880       cp_lexer_consume_token (parser->lexer);
19881       location = cp_lexer_peek_token (parser->lexer)->location;
19882       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19883          node, lest it get absorbed into the surrounding block.  */
19884       stmt = push_stmt_list ();
19885       cp_parser_compound_statement (parser, NULL, false);
19886       objc_build_finally_clause (location, pop_stmt_list (stmt));
19887     }
19888
19889   return objc_finish_try_stmt ();
19890 }
19891
19892 /* Parse an Objective-C synchronized statement.
19893
19894    objc-synchronized-stmt:
19895      @synchronized ( expression ) compound-statement
19896
19897    Returns NULL_TREE.  */
19898
19899 static tree
19900 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19901   location_t location;
19902   tree lock, stmt;
19903
19904   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19905
19906   location = cp_lexer_peek_token (parser->lexer)->location;
19907   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19908   lock = cp_parser_expression (parser, false);
19909   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19910
19911   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19912      node, lest it get absorbed into the surrounding block.  */
19913   stmt = push_stmt_list ();
19914   cp_parser_compound_statement (parser, NULL, false);
19915
19916   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19917 }
19918
19919 /* Parse an Objective-C throw statement.
19920
19921    objc-throw-stmt:
19922      @throw assignment-expression [opt] ;
19923
19924    Returns a constructed '@throw' statement.  */
19925
19926 static tree
19927 cp_parser_objc_throw_statement (cp_parser *parser) {
19928   tree expr = NULL_TREE;
19929
19930   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19931
19932   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19933     expr = cp_parser_assignment_expression (parser, false);
19934
19935   cp_parser_consume_semicolon_at_end_of_statement (parser);
19936
19937   return objc_build_throw_stmt (expr);
19938 }
19939
19940 /* Parse an Objective-C statement.  */
19941
19942 static tree
19943 cp_parser_objc_statement (cp_parser * parser) {
19944   /* Try to figure out what kind of declaration is present.  */
19945   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19946
19947   switch (kwd->keyword)
19948     {
19949     case RID_AT_TRY:
19950       return cp_parser_objc_try_catch_finally_statement (parser);
19951     case RID_AT_SYNCHRONIZED:
19952       return cp_parser_objc_synchronized_statement (parser);
19953     case RID_AT_THROW:
19954       return cp_parser_objc_throw_statement (parser);
19955     default:
19956       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19957              &kwd->location, kwd->u.value);
19958       cp_parser_skip_to_end_of_block_or_statement (parser);
19959     }
19960
19961   return error_mark_node;
19962 }
19963 \f
19964 /* OpenMP 2.5 parsing routines.  */
19965
19966 /* Returns name of the next clause.
19967    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19968    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19969    returned and the token is consumed.  */
19970
19971 static pragma_omp_clause
19972 cp_parser_omp_clause_name (cp_parser *parser)
19973 {
19974   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19975
19976   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19977     result = PRAGMA_OMP_CLAUSE_IF;
19978   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19979     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19980   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19981     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19982   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19983     {
19984       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19985       const char *p = IDENTIFIER_POINTER (id);
19986
19987       switch (p[0])
19988         {
19989         case 'c':
19990           if (!strcmp ("collapse", p))
19991             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19992           else if (!strcmp ("copyin", p))
19993             result = PRAGMA_OMP_CLAUSE_COPYIN;
19994           else if (!strcmp ("copyprivate", p))
19995             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19996           break;
19997         case 'f':
19998           if (!strcmp ("firstprivate", p))
19999             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20000           break;
20001         case 'l':
20002           if (!strcmp ("lastprivate", p))
20003             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20004           break;
20005         case 'n':
20006           if (!strcmp ("nowait", p))
20007             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20008           else if (!strcmp ("num_threads", p))
20009             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20010           break;
20011         case 'o':
20012           if (!strcmp ("ordered", p))
20013             result = PRAGMA_OMP_CLAUSE_ORDERED;
20014           break;
20015         case 'r':
20016           if (!strcmp ("reduction", p))
20017             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20018           break;
20019         case 's':
20020           if (!strcmp ("schedule", p))
20021             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20022           else if (!strcmp ("shared", p))
20023             result = PRAGMA_OMP_CLAUSE_SHARED;
20024           break;
20025         case 'u':
20026           if (!strcmp ("untied", p))
20027             result = PRAGMA_OMP_CLAUSE_UNTIED;
20028           break;
20029         }
20030     }
20031
20032   if (result != PRAGMA_OMP_CLAUSE_NONE)
20033     cp_lexer_consume_token (parser->lexer);
20034
20035   return result;
20036 }
20037
20038 /* Validate that a clause of the given type does not already exist.  */
20039
20040 static void
20041 check_no_duplicate_clause (tree clauses, enum tree_code code,
20042                            const char *name, location_t location)
20043 {
20044   tree c;
20045
20046   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20047     if (OMP_CLAUSE_CODE (c) == code)
20048       {
20049         error ("%Htoo many %qs clauses", &location, name);
20050         break;
20051       }
20052 }
20053
20054 /* OpenMP 2.5:
20055    variable-list:
20056      identifier
20057      variable-list , identifier
20058
20059    In addition, we match a closing parenthesis.  An opening parenthesis
20060    will have been consumed by the caller.
20061
20062    If KIND is nonzero, create the appropriate node and install the decl
20063    in OMP_CLAUSE_DECL and add the node to the head of the list.
20064
20065    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20066    return the list created.  */
20067
20068 static tree
20069 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20070                                 tree list)
20071 {
20072   cp_token *token;
20073   while (1)
20074     {
20075       tree name, decl;
20076
20077       token = cp_lexer_peek_token (parser->lexer);
20078       name = cp_parser_id_expression (parser, /*template_p=*/false,
20079                                       /*check_dependency_p=*/true,
20080                                       /*template_p=*/NULL,
20081                                       /*declarator_p=*/false,
20082                                       /*optional_p=*/false);
20083       if (name == error_mark_node)
20084         goto skip_comma;
20085
20086       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20087       if (decl == error_mark_node)
20088         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20089       else if (kind != 0)
20090         {
20091           tree u = build_omp_clause (kind);
20092           OMP_CLAUSE_DECL (u) = decl;
20093           OMP_CLAUSE_CHAIN (u) = list;
20094           list = u;
20095         }
20096       else
20097         list = tree_cons (decl, NULL_TREE, list);
20098
20099     get_comma:
20100       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20101         break;
20102       cp_lexer_consume_token (parser->lexer);
20103     }
20104
20105   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20106     {
20107       int ending;
20108
20109       /* Try to resync to an unnested comma.  Copied from
20110          cp_parser_parenthesized_expression_list.  */
20111     skip_comma:
20112       ending = cp_parser_skip_to_closing_parenthesis (parser,
20113                                                       /*recovering=*/true,
20114                                                       /*or_comma=*/true,
20115                                                       /*consume_paren=*/true);
20116       if (ending < 0)
20117         goto get_comma;
20118     }
20119
20120   return list;
20121 }
20122
20123 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20124    common case for omp clauses.  */
20125
20126 static tree
20127 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20128 {
20129   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20130     return cp_parser_omp_var_list_no_open (parser, kind, list);
20131   return list;
20132 }
20133
20134 /* OpenMP 3.0:
20135    collapse ( constant-expression ) */
20136
20137 static tree
20138 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20139 {
20140   tree c, num;
20141   location_t loc;
20142   HOST_WIDE_INT n;
20143
20144   loc = cp_lexer_peek_token (parser->lexer)->location;
20145   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20146     return list;
20147
20148   num = cp_parser_constant_expression (parser, false, NULL);
20149
20150   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20151     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20152                                            /*or_comma=*/false,
20153                                            /*consume_paren=*/true);
20154
20155   if (num == error_mark_node)
20156     return list;
20157   num = fold_non_dependent_expr (num);
20158   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20159       || !host_integerp (num, 0)
20160       || (n = tree_low_cst (num, 0)) <= 0
20161       || (int) n != n)
20162     {
20163       error ("%Hcollapse argument needs positive constant integer expression",
20164              &loc);
20165       return list;
20166     }
20167
20168   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20169   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20170   OMP_CLAUSE_CHAIN (c) = list;
20171   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20172
20173   return c;
20174 }
20175
20176 /* OpenMP 2.5:
20177    default ( shared | none ) */
20178
20179 static tree
20180 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20181 {
20182   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20183   tree c;
20184
20185   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20186     return list;
20187   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20188     {
20189       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20190       const char *p = IDENTIFIER_POINTER (id);
20191
20192       switch (p[0])
20193         {
20194         case 'n':
20195           if (strcmp ("none", p) != 0)
20196             goto invalid_kind;
20197           kind = OMP_CLAUSE_DEFAULT_NONE;
20198           break;
20199
20200         case 's':
20201           if (strcmp ("shared", p) != 0)
20202             goto invalid_kind;
20203           kind = OMP_CLAUSE_DEFAULT_SHARED;
20204           break;
20205
20206         default:
20207           goto invalid_kind;
20208         }
20209
20210       cp_lexer_consume_token (parser->lexer);
20211     }
20212   else
20213     {
20214     invalid_kind:
20215       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20216     }
20217
20218   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20219     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20220                                            /*or_comma=*/false,
20221                                            /*consume_paren=*/true);
20222
20223   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20224     return list;
20225
20226   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20227   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20228   OMP_CLAUSE_CHAIN (c) = list;
20229   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20230
20231   return c;
20232 }
20233
20234 /* OpenMP 2.5:
20235    if ( expression ) */
20236
20237 static tree
20238 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20239 {
20240   tree t, c;
20241
20242   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20243     return list;
20244
20245   t = cp_parser_condition (parser);
20246
20247   if (t == error_mark_node
20248       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20249     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20250                                            /*or_comma=*/false,
20251                                            /*consume_paren=*/true);
20252
20253   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20254
20255   c = build_omp_clause (OMP_CLAUSE_IF);
20256   OMP_CLAUSE_IF_EXPR (c) = t;
20257   OMP_CLAUSE_CHAIN (c) = list;
20258
20259   return c;
20260 }
20261
20262 /* OpenMP 2.5:
20263    nowait */
20264
20265 static tree
20266 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20267                              tree list, location_t location)
20268 {
20269   tree c;
20270
20271   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20272
20273   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20274   OMP_CLAUSE_CHAIN (c) = list;
20275   return c;
20276 }
20277
20278 /* OpenMP 2.5:
20279    num_threads ( expression ) */
20280
20281 static tree
20282 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20283                                   location_t location)
20284 {
20285   tree t, c;
20286
20287   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20288     return list;
20289
20290   t = cp_parser_expression (parser, false);
20291
20292   if (t == error_mark_node
20293       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20294     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20295                                            /*or_comma=*/false,
20296                                            /*consume_paren=*/true);
20297
20298   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20299                              "num_threads", location);
20300
20301   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20302   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20303   OMP_CLAUSE_CHAIN (c) = list;
20304
20305   return c;
20306 }
20307
20308 /* OpenMP 2.5:
20309    ordered */
20310
20311 static tree
20312 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20313                               tree list, location_t location)
20314 {
20315   tree c;
20316
20317   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20318                              "ordered", location);
20319
20320   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20321   OMP_CLAUSE_CHAIN (c) = list;
20322   return c;
20323 }
20324
20325 /* OpenMP 2.5:
20326    reduction ( reduction-operator : variable-list )
20327
20328    reduction-operator:
20329      One of: + * - & ^ | && || */
20330
20331 static tree
20332 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20333 {
20334   enum tree_code code;
20335   tree nlist, c;
20336
20337   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20338     return list;
20339
20340   switch (cp_lexer_peek_token (parser->lexer)->type)
20341     {
20342     case CPP_PLUS:
20343       code = PLUS_EXPR;
20344       break;
20345     case CPP_MULT:
20346       code = MULT_EXPR;
20347       break;
20348     case CPP_MINUS:
20349       code = MINUS_EXPR;
20350       break;
20351     case CPP_AND:
20352       code = BIT_AND_EXPR;
20353       break;
20354     case CPP_XOR:
20355       code = BIT_XOR_EXPR;
20356       break;
20357     case CPP_OR:
20358       code = BIT_IOR_EXPR;
20359       break;
20360     case CPP_AND_AND:
20361       code = TRUTH_ANDIF_EXPR;
20362       break;
20363     case CPP_OR_OR:
20364       code = TRUTH_ORIF_EXPR;
20365       break;
20366     default:
20367       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20368                                "%<|%>, %<&&%>, or %<||%>");
20369     resync_fail:
20370       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20371                                              /*or_comma=*/false,
20372                                              /*consume_paren=*/true);
20373       return list;
20374     }
20375   cp_lexer_consume_token (parser->lexer);
20376
20377   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20378     goto resync_fail;
20379
20380   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20381   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20382     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20383
20384   return nlist;
20385 }
20386
20387 /* OpenMP 2.5:
20388    schedule ( schedule-kind )
20389    schedule ( schedule-kind , expression )
20390
20391    schedule-kind:
20392      static | dynamic | guided | runtime | auto  */
20393
20394 static tree
20395 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20396 {
20397   tree c, t;
20398
20399   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20400     return list;
20401
20402   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20403
20404   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20405     {
20406       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20407       const char *p = IDENTIFIER_POINTER (id);
20408
20409       switch (p[0])
20410         {
20411         case 'd':
20412           if (strcmp ("dynamic", p) != 0)
20413             goto invalid_kind;
20414           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20415           break;
20416
20417         case 'g':
20418           if (strcmp ("guided", p) != 0)
20419             goto invalid_kind;
20420           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20421           break;
20422
20423         case 'r':
20424           if (strcmp ("runtime", p) != 0)
20425             goto invalid_kind;
20426           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20427           break;
20428
20429         default:
20430           goto invalid_kind;
20431         }
20432     }
20433   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20434     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20435   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20436     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20437   else
20438     goto invalid_kind;
20439   cp_lexer_consume_token (parser->lexer);
20440
20441   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20442     {
20443       cp_token *token;
20444       cp_lexer_consume_token (parser->lexer);
20445
20446       token = cp_lexer_peek_token (parser->lexer);
20447       t = cp_parser_assignment_expression (parser, false);
20448
20449       if (t == error_mark_node)
20450         goto resync_fail;
20451       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20452         error ("%Hschedule %<runtime%> does not take "
20453                "a %<chunk_size%> parameter", &token->location);
20454       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20455         error ("%Hschedule %<auto%> does not take "
20456                "a %<chunk_size%> parameter", &token->location);
20457       else
20458         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20459
20460       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20461         goto resync_fail;
20462     }
20463   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20464     goto resync_fail;
20465
20466   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20467   OMP_CLAUSE_CHAIN (c) = list;
20468   return c;
20469
20470  invalid_kind:
20471   cp_parser_error (parser, "invalid schedule kind");
20472  resync_fail:
20473   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20474                                          /*or_comma=*/false,
20475                                          /*consume_paren=*/true);
20476   return list;
20477 }
20478
20479 /* OpenMP 3.0:
20480    untied */
20481
20482 static tree
20483 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20484                              tree list, location_t location)
20485 {
20486   tree c;
20487
20488   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20489
20490   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20491   OMP_CLAUSE_CHAIN (c) = list;
20492   return c;
20493 }
20494
20495 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20496    is a bitmask in MASK.  Return the list of clauses found; the result
20497    of clause default goes in *pdefault.  */
20498
20499 static tree
20500 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20501                            const char *where, cp_token *pragma_tok)
20502 {
20503   tree clauses = NULL;
20504   bool first = true;
20505   cp_token *token = NULL;
20506
20507   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20508     {
20509       pragma_omp_clause c_kind;
20510       const char *c_name;
20511       tree prev = clauses;
20512
20513       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20514         cp_lexer_consume_token (parser->lexer);
20515
20516       token = cp_lexer_peek_token (parser->lexer);
20517       c_kind = cp_parser_omp_clause_name (parser);
20518       first = false;
20519
20520       switch (c_kind)
20521         {
20522         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20523           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20524                                                    token->location);
20525           c_name = "collapse";
20526           break;
20527         case PRAGMA_OMP_CLAUSE_COPYIN:
20528           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20529           c_name = "copyin";
20530           break;
20531         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20532           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20533                                             clauses);
20534           c_name = "copyprivate";
20535           break;
20536         case PRAGMA_OMP_CLAUSE_DEFAULT:
20537           clauses = cp_parser_omp_clause_default (parser, clauses,
20538                                                   token->location);
20539           c_name = "default";
20540           break;
20541         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20542           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20543                                             clauses);
20544           c_name = "firstprivate";
20545           break;
20546         case PRAGMA_OMP_CLAUSE_IF:
20547           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20548           c_name = "if";
20549           break;
20550         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20551           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20552                                             clauses);
20553           c_name = "lastprivate";
20554           break;
20555         case PRAGMA_OMP_CLAUSE_NOWAIT:
20556           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20557           c_name = "nowait";
20558           break;
20559         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20560           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20561                                                       token->location);
20562           c_name = "num_threads";
20563           break;
20564         case PRAGMA_OMP_CLAUSE_ORDERED:
20565           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20566                                                   token->location);
20567           c_name = "ordered";
20568           break;
20569         case PRAGMA_OMP_CLAUSE_PRIVATE:
20570           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20571                                             clauses);
20572           c_name = "private";
20573           break;
20574         case PRAGMA_OMP_CLAUSE_REDUCTION:
20575           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20576           c_name = "reduction";
20577           break;
20578         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20579           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20580                                                    token->location);
20581           c_name = "schedule";
20582           break;
20583         case PRAGMA_OMP_CLAUSE_SHARED:
20584           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20585                                             clauses);
20586           c_name = "shared";
20587           break;
20588         case PRAGMA_OMP_CLAUSE_UNTIED:
20589           clauses = cp_parser_omp_clause_untied (parser, clauses,
20590                                                  token->location);
20591           c_name = "nowait";
20592           break;
20593         default:
20594           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20595           goto saw_error;
20596         }
20597
20598       if (((mask >> c_kind) & 1) == 0)
20599         {
20600           /* Remove the invalid clause(s) from the list to avoid
20601              confusing the rest of the compiler.  */
20602           clauses = prev;
20603           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20604         }
20605     }
20606  saw_error:
20607   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20608   return finish_omp_clauses (clauses);
20609 }
20610
20611 /* OpenMP 2.5:
20612    structured-block:
20613      statement
20614
20615    In practice, we're also interested in adding the statement to an
20616    outer node.  So it is convenient if we work around the fact that
20617    cp_parser_statement calls add_stmt.  */
20618
20619 static unsigned
20620 cp_parser_begin_omp_structured_block (cp_parser *parser)
20621 {
20622   unsigned save = parser->in_statement;
20623
20624   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20625      This preserves the "not within loop or switch" style error messages
20626      for nonsense cases like
20627         void foo() {
20628         #pragma omp single
20629           break;
20630         }
20631   */
20632   if (parser->in_statement)
20633     parser->in_statement = IN_OMP_BLOCK;
20634
20635   return save;
20636 }
20637
20638 static void
20639 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20640 {
20641   parser->in_statement = save;
20642 }
20643
20644 static tree
20645 cp_parser_omp_structured_block (cp_parser *parser)
20646 {
20647   tree stmt = begin_omp_structured_block ();
20648   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20649
20650   cp_parser_statement (parser, NULL_TREE, false, NULL);
20651
20652   cp_parser_end_omp_structured_block (parser, save);
20653   return finish_omp_structured_block (stmt);
20654 }
20655
20656 /* OpenMP 2.5:
20657    # pragma omp atomic new-line
20658      expression-stmt
20659
20660    expression-stmt:
20661      x binop= expr | x++ | ++x | x-- | --x
20662    binop:
20663      +, *, -, /, &, ^, |, <<, >>
20664
20665   where x is an lvalue expression with scalar type.  */
20666
20667 static void
20668 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20669 {
20670   tree lhs, rhs;
20671   enum tree_code code;
20672
20673   cp_parser_require_pragma_eol (parser, pragma_tok);
20674
20675   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20676                                     /*cast_p=*/false);
20677   switch (TREE_CODE (lhs))
20678     {
20679     case ERROR_MARK:
20680       goto saw_error;
20681
20682     case PREINCREMENT_EXPR:
20683     case POSTINCREMENT_EXPR:
20684       lhs = TREE_OPERAND (lhs, 0);
20685       code = PLUS_EXPR;
20686       rhs = integer_one_node;
20687       break;
20688
20689     case PREDECREMENT_EXPR:
20690     case POSTDECREMENT_EXPR:
20691       lhs = TREE_OPERAND (lhs, 0);
20692       code = MINUS_EXPR;
20693       rhs = integer_one_node;
20694       break;
20695
20696     default:
20697       switch (cp_lexer_peek_token (parser->lexer)->type)
20698         {
20699         case CPP_MULT_EQ:
20700           code = MULT_EXPR;
20701           break;
20702         case CPP_DIV_EQ:
20703           code = TRUNC_DIV_EXPR;
20704           break;
20705         case CPP_PLUS_EQ:
20706           code = PLUS_EXPR;
20707           break;
20708         case CPP_MINUS_EQ:
20709           code = MINUS_EXPR;
20710           break;
20711         case CPP_LSHIFT_EQ:
20712           code = LSHIFT_EXPR;
20713           break;
20714         case CPP_RSHIFT_EQ:
20715           code = RSHIFT_EXPR;
20716           break;
20717         case CPP_AND_EQ:
20718           code = BIT_AND_EXPR;
20719           break;
20720         case CPP_OR_EQ:
20721           code = BIT_IOR_EXPR;
20722           break;
20723         case CPP_XOR_EQ:
20724           code = BIT_XOR_EXPR;
20725           break;
20726         default:
20727           cp_parser_error (parser,
20728                            "invalid operator for %<#pragma omp atomic%>");
20729           goto saw_error;
20730         }
20731       cp_lexer_consume_token (parser->lexer);
20732
20733       rhs = cp_parser_expression (parser, false);
20734       if (rhs == error_mark_node)
20735         goto saw_error;
20736       break;
20737     }
20738   finish_omp_atomic (code, lhs, rhs);
20739   cp_parser_consume_semicolon_at_end_of_statement (parser);
20740   return;
20741
20742  saw_error:
20743   cp_parser_skip_to_end_of_block_or_statement (parser);
20744 }
20745
20746
20747 /* OpenMP 2.5:
20748    # pragma omp barrier new-line  */
20749
20750 static void
20751 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20752 {
20753   cp_parser_require_pragma_eol (parser, pragma_tok);
20754   finish_omp_barrier ();
20755 }
20756
20757 /* OpenMP 2.5:
20758    # pragma omp critical [(name)] new-line
20759      structured-block  */
20760
20761 static tree
20762 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20763 {
20764   tree stmt, name = NULL;
20765
20766   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20767     {
20768       cp_lexer_consume_token (parser->lexer);
20769
20770       name = cp_parser_identifier (parser);
20771
20772       if (name == error_mark_node
20773           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20774         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20775                                                /*or_comma=*/false,
20776                                                /*consume_paren=*/true);
20777       if (name == error_mark_node)
20778         name = NULL;
20779     }
20780   cp_parser_require_pragma_eol (parser, pragma_tok);
20781
20782   stmt = cp_parser_omp_structured_block (parser);
20783   return c_finish_omp_critical (stmt, name);
20784 }
20785
20786 /* OpenMP 2.5:
20787    # pragma omp flush flush-vars[opt] new-line
20788
20789    flush-vars:
20790      ( variable-list ) */
20791
20792 static void
20793 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20794 {
20795   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20796     (void) cp_parser_omp_var_list (parser, 0, NULL);
20797   cp_parser_require_pragma_eol (parser, pragma_tok);
20798
20799   finish_omp_flush ();
20800 }
20801
20802 /* Helper function, to parse omp for increment expression.  */
20803
20804 static tree
20805 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20806 {
20807   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20808   enum tree_code op;
20809   cp_token *token;
20810
20811   if (lhs != decl)
20812     {
20813       cp_parser_skip_to_end_of_statement (parser);
20814       return error_mark_node;
20815     }
20816
20817   token = cp_lexer_peek_token (parser->lexer);
20818   op = binops_by_token [token->type].tree_type;
20819   switch (op)
20820     {
20821     case LT_EXPR:
20822     case LE_EXPR:
20823     case GT_EXPR:
20824     case GE_EXPR:
20825       break;
20826     default:
20827       cp_parser_skip_to_end_of_statement (parser);
20828       return error_mark_node;
20829     }
20830
20831   cp_lexer_consume_token (parser->lexer);
20832   rhs = cp_parser_binary_expression (parser, false,
20833                                      PREC_RELATIONAL_EXPRESSION);
20834   if (rhs == error_mark_node
20835       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20836     {
20837       cp_parser_skip_to_end_of_statement (parser);
20838       return error_mark_node;
20839     }
20840
20841   return build2 (op, boolean_type_node, lhs, rhs);
20842 }
20843
20844 /* Helper function, to parse omp for increment expression.  */
20845
20846 static tree
20847 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20848 {
20849   cp_token *token = cp_lexer_peek_token (parser->lexer);
20850   enum tree_code op;
20851   tree lhs, rhs;
20852   cp_id_kind idk;
20853   bool decl_first;
20854
20855   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20856     {
20857       op = (token->type == CPP_PLUS_PLUS
20858             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20859       cp_lexer_consume_token (parser->lexer);
20860       lhs = cp_parser_cast_expression (parser, false, false);
20861       if (lhs != decl)
20862         return error_mark_node;
20863       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20864     }
20865
20866   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20867   if (lhs != decl)
20868     return error_mark_node;
20869
20870   token = cp_lexer_peek_token (parser->lexer);
20871   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20872     {
20873       op = (token->type == CPP_PLUS_PLUS
20874             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20875       cp_lexer_consume_token (parser->lexer);
20876       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20877     }
20878
20879   op = cp_parser_assignment_operator_opt (parser);
20880   if (op == ERROR_MARK)
20881     return error_mark_node;
20882
20883   if (op != NOP_EXPR)
20884     {
20885       rhs = cp_parser_assignment_expression (parser, false);
20886       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20887       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20888     }
20889
20890   lhs = cp_parser_binary_expression (parser, false,
20891                                      PREC_ADDITIVE_EXPRESSION);
20892   token = cp_lexer_peek_token (parser->lexer);
20893   decl_first = lhs == decl;
20894   if (decl_first)
20895     lhs = NULL_TREE;
20896   if (token->type != CPP_PLUS
20897       && token->type != CPP_MINUS)
20898     return error_mark_node;
20899
20900   do
20901     {
20902       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20903       cp_lexer_consume_token (parser->lexer);
20904       rhs = cp_parser_binary_expression (parser, false,
20905                                          PREC_ADDITIVE_EXPRESSION);
20906       token = cp_lexer_peek_token (parser->lexer);
20907       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20908         {
20909           if (lhs == NULL_TREE)
20910             {
20911               if (op == PLUS_EXPR)
20912                 lhs = rhs;
20913               else
20914                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20915             }
20916           else
20917             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20918                                      NULL, tf_warning_or_error);
20919         }
20920     }
20921   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20922
20923   if (!decl_first)
20924     {
20925       if (rhs != decl || op == MINUS_EXPR)
20926         return error_mark_node;
20927       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20928     }
20929   else
20930     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20931
20932   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20933 }
20934
20935 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20936
20937 static tree
20938 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20939 {
20940   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20941   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20942   tree this_pre_body, cl;
20943   location_t loc_first;
20944   bool collapse_err = false;
20945   int i, collapse = 1, nbraces = 0;
20946
20947   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20948     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20949       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20950
20951   gcc_assert (collapse >= 1);
20952
20953   declv = make_tree_vec (collapse);
20954   initv = make_tree_vec (collapse);
20955   condv = make_tree_vec (collapse);
20956   incrv = make_tree_vec (collapse);
20957
20958   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20959
20960   for (i = 0; i < collapse; i++)
20961     {
20962       int bracecount = 0;
20963       bool add_private_clause = false;
20964       location_t loc;
20965
20966       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20967         {
20968           cp_parser_error (parser, "for statement expected");
20969           return NULL;
20970         }
20971       loc = cp_lexer_consume_token (parser->lexer)->location;
20972
20973       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20974         return NULL;
20975
20976       init = decl = real_decl = NULL;
20977       this_pre_body = push_stmt_list ();
20978       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20979         {
20980           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20981
20982              init-expr:
20983                        var = lb
20984                        integer-type var = lb
20985                        random-access-iterator-type var = lb
20986                        pointer-type var = lb
20987           */
20988           cp_decl_specifier_seq type_specifiers;
20989
20990           /* First, try to parse as an initialized declaration.  See
20991              cp_parser_condition, from whence the bulk of this is copied.  */
20992
20993           cp_parser_parse_tentatively (parser);
20994           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20995                                         &type_specifiers);
20996           if (cp_parser_parse_definitely (parser))
20997             {
20998               /* If parsing a type specifier seq succeeded, then this
20999                  MUST be a initialized declaration.  */
21000               tree asm_specification, attributes;
21001               cp_declarator *declarator;
21002
21003               declarator = cp_parser_declarator (parser,
21004                                                  CP_PARSER_DECLARATOR_NAMED,
21005                                                  /*ctor_dtor_or_conv_p=*/NULL,
21006                                                  /*parenthesized_p=*/NULL,
21007                                                  /*member_p=*/false);
21008               attributes = cp_parser_attributes_opt (parser);
21009               asm_specification = cp_parser_asm_specification_opt (parser);
21010
21011               if (declarator == cp_error_declarator) 
21012                 cp_parser_skip_to_end_of_statement (parser);
21013
21014               else 
21015                 {
21016                   tree pushed_scope;
21017
21018                   decl = start_decl (declarator, &type_specifiers,
21019                                      /*initialized_p=*/false, attributes,
21020                                      /*prefix_attributes=*/NULL_TREE,
21021                                      &pushed_scope);
21022
21023                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21024                     {
21025                       if (cp_lexer_next_token_is (parser->lexer, 
21026                                                   CPP_OPEN_PAREN))
21027                         error ("parenthesized initialization is not allowed in "
21028                                "OpenMP %<for%> loop");
21029                       else
21030                         /* Trigger an error.  */
21031                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21032
21033                       init = error_mark_node;
21034                       cp_parser_skip_to_end_of_statement (parser);
21035                     }
21036                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21037                            || type_dependent_expression_p (decl))
21038                     {
21039                       bool is_direct_init, is_non_constant_init;
21040
21041                       init = cp_parser_initializer (parser,
21042                                                     &is_direct_init,
21043                                                     &is_non_constant_init);
21044
21045                       cp_finish_decl (decl, init, !is_non_constant_init,
21046                                       asm_specification,
21047                                       LOOKUP_ONLYCONVERTING);
21048                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21049                         {
21050                           for_block
21051                             = tree_cons (NULL, this_pre_body, for_block);
21052                           init = NULL_TREE;
21053                         }
21054                       else
21055                         init = pop_stmt_list (this_pre_body);
21056                       this_pre_body = NULL_TREE;
21057                     }
21058                   else
21059                     {
21060                       /* Consume '='.  */
21061                       cp_lexer_consume_token (parser->lexer);
21062                       init = cp_parser_assignment_expression (parser, false);
21063
21064                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21065                         init = error_mark_node;
21066                       else
21067                         cp_finish_decl (decl, NULL_TREE,
21068                                         /*init_const_expr_p=*/false,
21069                                         asm_specification,
21070                                         LOOKUP_ONLYCONVERTING);
21071                     }
21072
21073                   if (pushed_scope)
21074                     pop_scope (pushed_scope);
21075                 }
21076             }
21077           else 
21078             {
21079               cp_id_kind idk;
21080               /* If parsing a type specifier sequence failed, then
21081                  this MUST be a simple expression.  */
21082               cp_parser_parse_tentatively (parser);
21083               decl = cp_parser_primary_expression (parser, false, false,
21084                                                    false, &idk);
21085               if (!cp_parser_error_occurred (parser)
21086                   && decl
21087                   && DECL_P (decl)
21088                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21089                 {
21090                   tree rhs;
21091
21092                   cp_parser_parse_definitely (parser);
21093                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21094                   rhs = cp_parser_assignment_expression (parser, false);
21095                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21096                                                          rhs,
21097                                                          tf_warning_or_error));
21098                   add_private_clause = true;
21099                 }
21100               else
21101                 {
21102                   decl = NULL;
21103                   cp_parser_abort_tentative_parse (parser);
21104                   init = cp_parser_expression (parser, false);
21105                   if (init)
21106                     {
21107                       if (TREE_CODE (init) == MODIFY_EXPR
21108                           || TREE_CODE (init) == MODOP_EXPR)
21109                         real_decl = TREE_OPERAND (init, 0);
21110                     }
21111                 }
21112             }
21113         }
21114       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21115       if (this_pre_body)
21116         {
21117           this_pre_body = pop_stmt_list (this_pre_body);
21118           if (pre_body)
21119             {
21120               tree t = pre_body;
21121               pre_body = push_stmt_list ();
21122               add_stmt (t);
21123               add_stmt (this_pre_body);
21124               pre_body = pop_stmt_list (pre_body);
21125             }
21126           else
21127             pre_body = this_pre_body;
21128         }
21129
21130       if (decl)
21131         real_decl = decl;
21132       if (par_clauses != NULL && real_decl != NULL_TREE)
21133         {
21134           tree *c;
21135           for (c = par_clauses; *c ; )
21136             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21137                 && OMP_CLAUSE_DECL (*c) == real_decl)
21138               {
21139                 error ("%Hiteration variable %qD should not be firstprivate",
21140                        &loc, real_decl);
21141                 *c = OMP_CLAUSE_CHAIN (*c);
21142               }
21143             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21144                      && OMP_CLAUSE_DECL (*c) == real_decl)
21145               {
21146                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21147                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21148                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21149                 OMP_CLAUSE_DECL (l) = real_decl;
21150                 OMP_CLAUSE_CHAIN (l) = clauses;
21151                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21152                 clauses = l;
21153                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21154                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21155                 add_private_clause = false;
21156               }
21157             else
21158               {
21159                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21160                     && OMP_CLAUSE_DECL (*c) == real_decl)
21161                   add_private_clause = false;
21162                 c = &OMP_CLAUSE_CHAIN (*c);
21163               }
21164         }
21165
21166       if (add_private_clause)
21167         {
21168           tree c;
21169           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21170             {
21171               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21172                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21173                   && OMP_CLAUSE_DECL (c) == decl)
21174                 break;
21175               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21176                        && OMP_CLAUSE_DECL (c) == decl)
21177                 error ("%Hiteration variable %qD should not be firstprivate",
21178                        &loc, decl);
21179               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21180                        && OMP_CLAUSE_DECL (c) == decl)
21181                 error ("%Hiteration variable %qD should not be reduction",
21182                        &loc, decl);
21183             }
21184           if (c == NULL)
21185             {
21186               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21187               OMP_CLAUSE_DECL (c) = decl;
21188               c = finish_omp_clauses (c);
21189               if (c)
21190                 {
21191                   OMP_CLAUSE_CHAIN (c) = clauses;
21192                   clauses = c;
21193                 }
21194             }
21195         }
21196
21197       cond = NULL;
21198       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21199         {
21200           /* If decl is an iterator, preserve LHS and RHS of the relational
21201              expr until finish_omp_for.  */
21202           if (decl
21203               && (type_dependent_expression_p (decl)
21204                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21205             cond = cp_parser_omp_for_cond (parser, decl);
21206           else
21207             cond = cp_parser_condition (parser);
21208         }
21209       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21210
21211       incr = NULL;
21212       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21213         {
21214           /* If decl is an iterator, preserve the operator on decl
21215              until finish_omp_for.  */
21216           if (decl
21217               && (type_dependent_expression_p (decl)
21218                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21219             incr = cp_parser_omp_for_incr (parser, decl);
21220           else
21221             incr = cp_parser_expression (parser, false);
21222         }
21223
21224       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21225         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21226                                                /*or_comma=*/false,
21227                                                /*consume_paren=*/true);
21228
21229       TREE_VEC_ELT (declv, i) = decl;
21230       TREE_VEC_ELT (initv, i) = init;
21231       TREE_VEC_ELT (condv, i) = cond;
21232       TREE_VEC_ELT (incrv, i) = incr;
21233
21234       if (i == collapse - 1)
21235         break;
21236
21237       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21238          in between the collapsed for loops to be still considered perfectly
21239          nested.  Hopefully the final version clarifies this.
21240          For now handle (multiple) {'s and empty statements.  */
21241       cp_parser_parse_tentatively (parser);
21242       do
21243         {
21244           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21245             break;
21246           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21247             {
21248               cp_lexer_consume_token (parser->lexer);
21249               bracecount++;
21250             }
21251           else if (bracecount
21252                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21253             cp_lexer_consume_token (parser->lexer);
21254           else
21255             {
21256               loc = cp_lexer_peek_token (parser->lexer)->location;
21257               error ("%Hnot enough collapsed for loops", &loc);
21258               collapse_err = true;
21259               cp_parser_abort_tentative_parse (parser);
21260               declv = NULL_TREE;
21261               break;
21262             }
21263         }
21264       while (1);
21265
21266       if (declv)
21267         {
21268           cp_parser_parse_definitely (parser);
21269           nbraces += bracecount;
21270         }
21271     }
21272
21273   /* Note that we saved the original contents of this flag when we entered
21274      the structured block, and so we don't need to re-save it here.  */
21275   parser->in_statement = IN_OMP_FOR;
21276
21277   /* Note that the grammar doesn't call for a structured block here,
21278      though the loop as a whole is a structured block.  */
21279   body = push_stmt_list ();
21280   cp_parser_statement (parser, NULL_TREE, false, NULL);
21281   body = pop_stmt_list (body);
21282
21283   if (declv == NULL_TREE)
21284     ret = NULL_TREE;
21285   else
21286     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21287                           pre_body, clauses);
21288
21289   while (nbraces)
21290     {
21291       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21292         {
21293           cp_lexer_consume_token (parser->lexer);
21294           nbraces--;
21295         }
21296       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21297         cp_lexer_consume_token (parser->lexer);
21298       else
21299         {
21300           if (!collapse_err)
21301             {
21302               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21303               error ("%Hcollapsed loops not perfectly nested", &loc);
21304             }
21305           collapse_err = true;
21306           cp_parser_statement_seq_opt (parser, NULL);
21307           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21308         }
21309     }
21310
21311   while (for_block)
21312     {
21313       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21314       for_block = TREE_CHAIN (for_block);
21315     }
21316
21317   return ret;
21318 }
21319
21320 /* OpenMP 2.5:
21321    #pragma omp for for-clause[optseq] new-line
21322      for-loop  */
21323
21324 #define OMP_FOR_CLAUSE_MASK                             \
21325         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21326         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21327         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21328         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21329         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21330         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21331         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21332         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21333
21334 static tree
21335 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21336 {
21337   tree clauses, sb, ret;
21338   unsigned int save;
21339
21340   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21341                                        "#pragma omp for", pragma_tok);
21342
21343   sb = begin_omp_structured_block ();
21344   save = cp_parser_begin_omp_structured_block (parser);
21345
21346   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21347
21348   cp_parser_end_omp_structured_block (parser, save);
21349   add_stmt (finish_omp_structured_block (sb));
21350
21351   return ret;
21352 }
21353
21354 /* OpenMP 2.5:
21355    # pragma omp master new-line
21356      structured-block  */
21357
21358 static tree
21359 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21360 {
21361   cp_parser_require_pragma_eol (parser, pragma_tok);
21362   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21363 }
21364
21365 /* OpenMP 2.5:
21366    # pragma omp ordered new-line
21367      structured-block  */
21368
21369 static tree
21370 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21371 {
21372   cp_parser_require_pragma_eol (parser, pragma_tok);
21373   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21374 }
21375
21376 /* OpenMP 2.5:
21377
21378    section-scope:
21379      { section-sequence }
21380
21381    section-sequence:
21382      section-directive[opt] structured-block
21383      section-sequence section-directive structured-block  */
21384
21385 static tree
21386 cp_parser_omp_sections_scope (cp_parser *parser)
21387 {
21388   tree stmt, substmt;
21389   bool error_suppress = false;
21390   cp_token *tok;
21391
21392   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21393     return NULL_TREE;
21394
21395   stmt = push_stmt_list ();
21396
21397   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21398     {
21399       unsigned save;
21400
21401       substmt = begin_omp_structured_block ();
21402       save = cp_parser_begin_omp_structured_block (parser);
21403
21404       while (1)
21405         {
21406           cp_parser_statement (parser, NULL_TREE, false, NULL);
21407
21408           tok = cp_lexer_peek_token (parser->lexer);
21409           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21410             break;
21411           if (tok->type == CPP_CLOSE_BRACE)
21412             break;
21413           if (tok->type == CPP_EOF)
21414             break;
21415         }
21416
21417       cp_parser_end_omp_structured_block (parser, save);
21418       substmt = finish_omp_structured_block (substmt);
21419       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21420       add_stmt (substmt);
21421     }
21422
21423   while (1)
21424     {
21425       tok = cp_lexer_peek_token (parser->lexer);
21426       if (tok->type == CPP_CLOSE_BRACE)
21427         break;
21428       if (tok->type == CPP_EOF)
21429         break;
21430
21431       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21432         {
21433           cp_lexer_consume_token (parser->lexer);
21434           cp_parser_require_pragma_eol (parser, tok);
21435           error_suppress = false;
21436         }
21437       else if (!error_suppress)
21438         {
21439           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21440           error_suppress = true;
21441         }
21442
21443       substmt = cp_parser_omp_structured_block (parser);
21444       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21445       add_stmt (substmt);
21446     }
21447   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21448
21449   substmt = pop_stmt_list (stmt);
21450
21451   stmt = make_node (OMP_SECTIONS);
21452   TREE_TYPE (stmt) = void_type_node;
21453   OMP_SECTIONS_BODY (stmt) = substmt;
21454
21455   add_stmt (stmt);
21456   return stmt;
21457 }
21458
21459 /* OpenMP 2.5:
21460    # pragma omp sections sections-clause[optseq] newline
21461      sections-scope  */
21462
21463 #define OMP_SECTIONS_CLAUSE_MASK                        \
21464         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21465         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21466         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21467         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21468         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21469
21470 static tree
21471 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21472 {
21473   tree clauses, ret;
21474
21475   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21476                                        "#pragma omp sections", pragma_tok);
21477
21478   ret = cp_parser_omp_sections_scope (parser);
21479   if (ret)
21480     OMP_SECTIONS_CLAUSES (ret) = clauses;
21481
21482   return ret;
21483 }
21484
21485 /* OpenMP 2.5:
21486    # pragma parallel parallel-clause new-line
21487    # pragma parallel for parallel-for-clause new-line
21488    # pragma parallel sections parallel-sections-clause new-line  */
21489
21490 #define OMP_PARALLEL_CLAUSE_MASK                        \
21491         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21492         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21493         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21494         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21495         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21496         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21497         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21498         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21499
21500 static tree
21501 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21502 {
21503   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21504   const char *p_name = "#pragma omp parallel";
21505   tree stmt, clauses, par_clause, ws_clause, block;
21506   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21507   unsigned int save;
21508
21509   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21510     {
21511       cp_lexer_consume_token (parser->lexer);
21512       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21513       p_name = "#pragma omp parallel for";
21514       mask |= OMP_FOR_CLAUSE_MASK;
21515       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21516     }
21517   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21518     {
21519       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21520       const char *p = IDENTIFIER_POINTER (id);
21521       if (strcmp (p, "sections") == 0)
21522         {
21523           cp_lexer_consume_token (parser->lexer);
21524           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21525           p_name = "#pragma omp parallel sections";
21526           mask |= OMP_SECTIONS_CLAUSE_MASK;
21527           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21528         }
21529     }
21530
21531   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21532   block = begin_omp_parallel ();
21533   save = cp_parser_begin_omp_structured_block (parser);
21534
21535   switch (p_kind)
21536     {
21537     case PRAGMA_OMP_PARALLEL:
21538       cp_parser_statement (parser, NULL_TREE, false, NULL);
21539       par_clause = clauses;
21540       break;
21541
21542     case PRAGMA_OMP_PARALLEL_FOR:
21543       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21544       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21545       break;
21546
21547     case PRAGMA_OMP_PARALLEL_SECTIONS:
21548       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21549       stmt = cp_parser_omp_sections_scope (parser);
21550       if (stmt)
21551         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21552       break;
21553
21554     default:
21555       gcc_unreachable ();
21556     }
21557
21558   cp_parser_end_omp_structured_block (parser, save);
21559   stmt = finish_omp_parallel (par_clause, block);
21560   if (p_kind != PRAGMA_OMP_PARALLEL)
21561     OMP_PARALLEL_COMBINED (stmt) = 1;
21562   return stmt;
21563 }
21564
21565 /* OpenMP 2.5:
21566    # pragma omp single single-clause[optseq] new-line
21567      structured-block  */
21568
21569 #define OMP_SINGLE_CLAUSE_MASK                          \
21570         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21571         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21572         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21573         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21574
21575 static tree
21576 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21577 {
21578   tree stmt = make_node (OMP_SINGLE);
21579   TREE_TYPE (stmt) = void_type_node;
21580
21581   OMP_SINGLE_CLAUSES (stmt)
21582     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21583                                  "#pragma omp single", pragma_tok);
21584   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21585
21586   return add_stmt (stmt);
21587 }
21588
21589 /* OpenMP 3.0:
21590    # pragma omp task task-clause[optseq] new-line
21591      structured-block  */
21592
21593 #define OMP_TASK_CLAUSE_MASK                            \
21594         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21595         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21596         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21597         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21598         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21599         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21600
21601 static tree
21602 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21603 {
21604   tree clauses, block;
21605   unsigned int save;
21606
21607   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21608                                        "#pragma omp task", pragma_tok);
21609   block = begin_omp_task ();
21610   save = cp_parser_begin_omp_structured_block (parser);
21611   cp_parser_statement (parser, NULL_TREE, false, NULL);
21612   cp_parser_end_omp_structured_block (parser, save);
21613   return finish_omp_task (clauses, block);
21614 }
21615
21616 /* OpenMP 3.0:
21617    # pragma omp taskwait new-line  */
21618
21619 static void
21620 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21621 {
21622   cp_parser_require_pragma_eol (parser, pragma_tok);
21623   finish_omp_taskwait ();
21624 }
21625
21626 /* OpenMP 2.5:
21627    # pragma omp threadprivate (variable-list) */
21628
21629 static void
21630 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21631 {
21632   tree vars;
21633
21634   vars = cp_parser_omp_var_list (parser, 0, NULL);
21635   cp_parser_require_pragma_eol (parser, pragma_tok);
21636
21637   finish_omp_threadprivate (vars);
21638 }
21639
21640 /* Main entry point to OpenMP statement pragmas.  */
21641
21642 static void
21643 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21644 {
21645   tree stmt;
21646
21647   switch (pragma_tok->pragma_kind)
21648     {
21649     case PRAGMA_OMP_ATOMIC:
21650       cp_parser_omp_atomic (parser, pragma_tok);
21651       return;
21652     case PRAGMA_OMP_CRITICAL:
21653       stmt = cp_parser_omp_critical (parser, pragma_tok);
21654       break;
21655     case PRAGMA_OMP_FOR:
21656       stmt = cp_parser_omp_for (parser, pragma_tok);
21657       break;
21658     case PRAGMA_OMP_MASTER:
21659       stmt = cp_parser_omp_master (parser, pragma_tok);
21660       break;
21661     case PRAGMA_OMP_ORDERED:
21662       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21663       break;
21664     case PRAGMA_OMP_PARALLEL:
21665       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21666       break;
21667     case PRAGMA_OMP_SECTIONS:
21668       stmt = cp_parser_omp_sections (parser, pragma_tok);
21669       break;
21670     case PRAGMA_OMP_SINGLE:
21671       stmt = cp_parser_omp_single (parser, pragma_tok);
21672       break;
21673     case PRAGMA_OMP_TASK:
21674       stmt = cp_parser_omp_task (parser, pragma_tok);
21675       break;
21676     default:
21677       gcc_unreachable ();
21678     }
21679
21680   if (stmt)
21681     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21682 }
21683 \f
21684 /* The parser.  */
21685
21686 static GTY (()) cp_parser *the_parser;
21687
21688 \f
21689 /* Special handling for the first token or line in the file.  The first
21690    thing in the file might be #pragma GCC pch_preprocess, which loads a
21691    PCH file, which is a GC collection point.  So we need to handle this
21692    first pragma without benefit of an existing lexer structure.
21693
21694    Always returns one token to the caller in *FIRST_TOKEN.  This is
21695    either the true first token of the file, or the first token after
21696    the initial pragma.  */
21697
21698 static void
21699 cp_parser_initial_pragma (cp_token *first_token)
21700 {
21701   tree name = NULL;
21702
21703   cp_lexer_get_preprocessor_token (NULL, first_token);
21704   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21705     return;
21706
21707   cp_lexer_get_preprocessor_token (NULL, first_token);
21708   if (first_token->type == CPP_STRING)
21709     {
21710       name = first_token->u.value;
21711
21712       cp_lexer_get_preprocessor_token (NULL, first_token);
21713       if (first_token->type != CPP_PRAGMA_EOL)
21714         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21715                &first_token->location);
21716     }
21717   else
21718     error ("%Hexpected string literal", &first_token->location);
21719
21720   /* Skip to the end of the pragma.  */
21721   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21722     cp_lexer_get_preprocessor_token (NULL, first_token);
21723
21724   /* Now actually load the PCH file.  */
21725   if (name)
21726     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21727
21728   /* Read one more token to return to our caller.  We have to do this
21729      after reading the PCH file in, since its pointers have to be
21730      live.  */
21731   cp_lexer_get_preprocessor_token (NULL, first_token);
21732 }
21733
21734 /* Normal parsing of a pragma token.  Here we can (and must) use the
21735    regular lexer.  */
21736
21737 static bool
21738 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21739 {
21740   cp_token *pragma_tok;
21741   unsigned int id;
21742
21743   pragma_tok = cp_lexer_consume_token (parser->lexer);
21744   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21745   parser->lexer->in_pragma = true;
21746
21747   id = pragma_tok->pragma_kind;
21748   switch (id)
21749     {
21750     case PRAGMA_GCC_PCH_PREPROCESS:
21751       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21752              &pragma_tok->location);
21753       break;
21754
21755     case PRAGMA_OMP_BARRIER:
21756       switch (context)
21757         {
21758         case pragma_compound:
21759           cp_parser_omp_barrier (parser, pragma_tok);
21760           return false;
21761         case pragma_stmt:
21762           error ("%H%<#pragma omp barrier%> may only be "
21763                  "used in compound statements", &pragma_tok->location);
21764           break;
21765         default:
21766           goto bad_stmt;
21767         }
21768       break;
21769
21770     case PRAGMA_OMP_FLUSH:
21771       switch (context)
21772         {
21773         case pragma_compound:
21774           cp_parser_omp_flush (parser, pragma_tok);
21775           return false;
21776         case pragma_stmt:
21777           error ("%H%<#pragma omp flush%> may only be "
21778                  "used in compound statements", &pragma_tok->location);
21779           break;
21780         default:
21781           goto bad_stmt;
21782         }
21783       break;
21784
21785     case PRAGMA_OMP_TASKWAIT:
21786       switch (context)
21787         {
21788         case pragma_compound:
21789           cp_parser_omp_taskwait (parser, pragma_tok);
21790           return false;
21791         case pragma_stmt:
21792           error ("%H%<#pragma omp taskwait%> may only be "
21793                  "used in compound statements",
21794                  &pragma_tok->location);
21795           break;
21796         default:
21797           goto bad_stmt;
21798         }
21799       break;
21800
21801     case PRAGMA_OMP_THREADPRIVATE:
21802       cp_parser_omp_threadprivate (parser, pragma_tok);
21803       return false;
21804
21805     case PRAGMA_OMP_ATOMIC:
21806     case PRAGMA_OMP_CRITICAL:
21807     case PRAGMA_OMP_FOR:
21808     case PRAGMA_OMP_MASTER:
21809     case PRAGMA_OMP_ORDERED:
21810     case PRAGMA_OMP_PARALLEL:
21811     case PRAGMA_OMP_SECTIONS:
21812     case PRAGMA_OMP_SINGLE:
21813     case PRAGMA_OMP_TASK:
21814       if (context == pragma_external)
21815         goto bad_stmt;
21816       cp_parser_omp_construct (parser, pragma_tok);
21817       return true;
21818
21819     case PRAGMA_OMP_SECTION:
21820       error ("%H%<#pragma omp section%> may only be used in "
21821              "%<#pragma omp sections%> construct", &pragma_tok->location);
21822       break;
21823
21824     default:
21825       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21826       c_invoke_pragma_handler (id);
21827       break;
21828
21829     bad_stmt:
21830       cp_parser_error (parser, "expected declaration specifiers");
21831       break;
21832     }
21833
21834   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21835   return false;
21836 }
21837
21838 /* The interface the pragma parsers have to the lexer.  */
21839
21840 enum cpp_ttype
21841 pragma_lex (tree *value)
21842 {
21843   cp_token *tok;
21844   enum cpp_ttype ret;
21845
21846   tok = cp_lexer_peek_token (the_parser->lexer);
21847
21848   ret = tok->type;
21849   *value = tok->u.value;
21850
21851   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21852     ret = CPP_EOF;
21853   else if (ret == CPP_STRING)
21854     *value = cp_parser_string_literal (the_parser, false, false);
21855   else
21856     {
21857       cp_lexer_consume_token (the_parser->lexer);
21858       if (ret == CPP_KEYWORD)
21859         ret = CPP_NAME;
21860     }
21861
21862   return ret;
21863 }
21864
21865 \f
21866 /* External interface.  */
21867
21868 /* Parse one entire translation unit.  */
21869
21870 void
21871 c_parse_file (void)
21872 {
21873   bool error_occurred;
21874   static bool already_called = false;
21875
21876   if (already_called)
21877     {
21878       sorry ("inter-module optimizations not implemented for C++");
21879       return;
21880     }
21881   already_called = true;
21882
21883   the_parser = cp_parser_new ();
21884   push_deferring_access_checks (flag_access_control
21885                                 ? dk_no_deferred : dk_no_check);
21886   error_occurred = cp_parser_translation_unit (the_parser);
21887   the_parser = NULL;
21888 }
21889
21890 #include "gt-cp-parser.h"