OSDN Git Service

gcc/cp/
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       tree parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification,
1019                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool, cp_id_kind *);
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, cp_id_kind *);
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, cp_id_kind *);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec, cp_id_kind *);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool, cp_id_kind *);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool, cp_id_kind *);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       if (TREE_CODE (token->u.value) == FIXED_CST)
3148         {
3149           error ("%Hfixed-point types not supported in C++",
3150                  &token->location);
3151           return error_mark_node;
3152         }
3153       /* Floating-point literals are only allowed in an integral
3154          constant expression if they are cast to an integral or
3155          enumeration type.  */
3156       if (TREE_CODE (token->u.value) == REAL_CST
3157           && parser->integral_constant_expression_p
3158           && pedantic)
3159         {
3160           /* CAST_P will be set even in invalid code like "int(2.7 +
3161              ...)".   Therefore, we have to check that the next token
3162              is sure to end the cast.  */
3163           if (cast_p)
3164             {
3165               cp_token *next_token;
3166
3167               next_token = cp_lexer_peek_token (parser->lexer);
3168               if (/* The comma at the end of an
3169                      enumerator-definition.  */
3170                   next_token->type != CPP_COMMA
3171                   /* The curly brace at the end of an enum-specifier.  */
3172                   && next_token->type != CPP_CLOSE_BRACE
3173                   /* The end of a statement.  */
3174                   && next_token->type != CPP_SEMICOLON
3175                   /* The end of the cast-expression.  */
3176                   && next_token->type != CPP_CLOSE_PAREN
3177                   /* The end of an array bound.  */
3178                   && next_token->type != CPP_CLOSE_SQUARE
3179                   /* The closing ">" in a template-argument-list.  */
3180                   && (next_token->type != CPP_GREATER
3181                       || parser->greater_than_is_operator_p)
3182                   /* C++0x only: A ">>" treated like two ">" tokens,
3183                      in a template-argument-list.  */
3184                   && (next_token->type != CPP_RSHIFT
3185                       || (cxx_dialect == cxx98)
3186                       || parser->greater_than_is_operator_p))
3187                 cast_p = false;
3188             }
3189
3190           /* If we are within a cast, then the constraint that the
3191              cast is to an integral or enumeration type will be
3192              checked at that point.  If we are not within a cast, then
3193              this code is invalid.  */
3194           if (!cast_p)
3195             cp_parser_non_integral_constant_expression
3196               (parser, "floating-point literal");
3197         }
3198       return token->u.value;
3199
3200     case CPP_STRING:
3201     case CPP_STRING16:
3202     case CPP_STRING32:
3203     case CPP_WSTRING:
3204       /* ??? Should wide strings be allowed when parser->translate_strings_p
3205          is false (i.e. in attributes)?  If not, we can kill the third
3206          argument to cp_parser_string_literal.  */
3207       return cp_parser_string_literal (parser,
3208                                        parser->translate_strings_p,
3209                                        true);
3210
3211     case CPP_OPEN_PAREN:
3212       {
3213         tree expr;
3214         bool saved_greater_than_is_operator_p;
3215
3216         /* Consume the `('.  */
3217         cp_lexer_consume_token (parser->lexer);
3218         /* Within a parenthesized expression, a `>' token is always
3219            the greater-than operator.  */
3220         saved_greater_than_is_operator_p
3221           = parser->greater_than_is_operator_p;
3222         parser->greater_than_is_operator_p = true;
3223         /* If we see `( { ' then we are looking at the beginning of
3224            a GNU statement-expression.  */
3225         if (cp_parser_allow_gnu_extensions_p (parser)
3226             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3227           {
3228             /* Statement-expressions are not allowed by the standard.  */
3229             pedwarn (token->location, OPT_pedantic, 
3230                      "ISO C++ forbids braced-groups within expressions");
3231
3232             /* And they're not allowed outside of a function-body; you
3233                cannot, for example, write:
3234
3235                  int i = ({ int j = 3; j + 1; });
3236
3237                at class or namespace scope.  */
3238             if (!parser->in_function_body
3239                 || parser->in_template_argument_list_p)
3240               {
3241                 error ("%Hstatement-expressions are not allowed outside "
3242                        "functions nor in template-argument lists",
3243                        &token->location);
3244                 cp_parser_skip_to_end_of_block_or_statement (parser);
3245                 expr = error_mark_node;
3246               }
3247             else
3248               {
3249                 /* Start the statement-expression.  */
3250                 expr = begin_stmt_expr ();
3251                 /* Parse the compound-statement.  */
3252                 cp_parser_compound_statement (parser, expr, false);
3253                 /* Finish up.  */
3254                 expr = finish_stmt_expr (expr, false);
3255               }
3256           }
3257         else
3258           {
3259             /* Parse the parenthesized expression.  */
3260             expr = cp_parser_expression (parser, cast_p, idk);
3261             /* Let the front end know that this expression was
3262                enclosed in parentheses. This matters in case, for
3263                example, the expression is of the form `A::B', since
3264                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3265                not.  */
3266             finish_parenthesized_expr (expr);
3267           }
3268         /* The `>' token might be the end of a template-id or
3269            template-parameter-list now.  */
3270         parser->greater_than_is_operator_p
3271           = saved_greater_than_is_operator_p;
3272         /* Consume the `)'.  */
3273         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3274           cp_parser_skip_to_end_of_statement (parser);
3275
3276         return expr;
3277       }
3278
3279     case CPP_KEYWORD:
3280       switch (token->keyword)
3281         {
3282           /* These two are the boolean literals.  */
3283         case RID_TRUE:
3284           cp_lexer_consume_token (parser->lexer);
3285           return boolean_true_node;
3286         case RID_FALSE:
3287           cp_lexer_consume_token (parser->lexer);
3288           return boolean_false_node;
3289
3290           /* The `__null' literal.  */
3291         case RID_NULL:
3292           cp_lexer_consume_token (parser->lexer);
3293           return null_node;
3294
3295           /* Recognize the `this' keyword.  */
3296         case RID_THIS:
3297           cp_lexer_consume_token (parser->lexer);
3298           if (parser->local_variables_forbidden_p)
3299             {
3300               error ("%H%<this%> may not be used in this context",
3301                      &token->location);
3302               return error_mark_node;
3303             }
3304           /* Pointers cannot appear in constant-expressions.  */
3305           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3306             return error_mark_node;
3307           return finish_this_expr ();
3308
3309           /* The `operator' keyword can be the beginning of an
3310              id-expression.  */
3311         case RID_OPERATOR:
3312           goto id_expression;
3313
3314         case RID_FUNCTION_NAME:
3315         case RID_PRETTY_FUNCTION_NAME:
3316         case RID_C99_FUNCTION_NAME:
3317           {
3318             const char *name;
3319
3320             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3321                __func__ are the names of variables -- but they are
3322                treated specially.  Therefore, they are handled here,
3323                rather than relying on the generic id-expression logic
3324                below.  Grammatically, these names are id-expressions.
3325
3326                Consume the token.  */
3327             token = cp_lexer_consume_token (parser->lexer);
3328
3329             switch (token->keyword)
3330               {
3331               case RID_FUNCTION_NAME:
3332                 name = "%<__FUNCTION__%>";
3333                 break;
3334               case RID_PRETTY_FUNCTION_NAME:
3335                 name = "%<__PRETTY_FUNCTION__%>";
3336                 break;
3337               case RID_C99_FUNCTION_NAME:
3338                 name = "%<__func__%>";
3339                 break;
3340               default:
3341                 gcc_unreachable ();
3342               }
3343
3344             if (cp_parser_non_integral_constant_expression (parser, name))
3345               return error_mark_node;
3346
3347             /* Look up the name.  */
3348             return finish_fname (token->u.value);
3349           }
3350
3351         case RID_VA_ARG:
3352           {
3353             tree expression;
3354             tree type;
3355
3356             /* The `__builtin_va_arg' construct is used to handle
3357                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3358             cp_lexer_consume_token (parser->lexer);
3359             /* Look for the opening `('.  */
3360             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3361             /* Now, parse the assignment-expression.  */
3362             expression = cp_parser_assignment_expression (parser,
3363                                                           /*cast_p=*/false, NULL);
3364             /* Look for the `,'.  */
3365             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3366             /* Parse the type-id.  */
3367             type = cp_parser_type_id (parser);
3368             /* Look for the closing `)'.  */
3369             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3370             /* Using `va_arg' in a constant-expression is not
3371                allowed.  */
3372             if (cp_parser_non_integral_constant_expression (parser,
3373                                                             "%<va_arg%>"))
3374               return error_mark_node;
3375             return build_x_va_arg (expression, type);
3376           }
3377
3378         case RID_OFFSETOF:
3379           return cp_parser_builtin_offsetof (parser);
3380
3381         case RID_HAS_NOTHROW_ASSIGN:
3382         case RID_HAS_NOTHROW_CONSTRUCTOR:
3383         case RID_HAS_NOTHROW_COPY:        
3384         case RID_HAS_TRIVIAL_ASSIGN:
3385         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3386         case RID_HAS_TRIVIAL_COPY:        
3387         case RID_HAS_TRIVIAL_DESTRUCTOR:
3388         case RID_HAS_VIRTUAL_DESTRUCTOR:
3389         case RID_IS_ABSTRACT:
3390         case RID_IS_BASE_OF:
3391         case RID_IS_CLASS:
3392         case RID_IS_CONVERTIBLE_TO:
3393         case RID_IS_EMPTY:
3394         case RID_IS_ENUM:
3395         case RID_IS_POD:
3396         case RID_IS_POLYMORPHIC:
3397         case RID_IS_UNION:
3398           return cp_parser_trait_expr (parser, token->keyword);
3399
3400         /* Objective-C++ expressions.  */
3401         case RID_AT_ENCODE:
3402         case RID_AT_PROTOCOL:
3403         case RID_AT_SELECTOR:
3404           return cp_parser_objc_expression (parser);
3405
3406         default:
3407           cp_parser_error (parser, "expected primary-expression");
3408           return error_mark_node;
3409         }
3410
3411       /* An id-expression can start with either an identifier, a
3412          `::' as the beginning of a qualified-id, or the "operator"
3413          keyword.  */
3414     case CPP_NAME:
3415     case CPP_SCOPE:
3416     case CPP_TEMPLATE_ID:
3417     case CPP_NESTED_NAME_SPECIFIER:
3418       {
3419         tree id_expression;
3420         tree decl;
3421         const char *error_msg;
3422         bool template_p;
3423         bool done;
3424         cp_token *id_expr_token;
3425
3426       id_expression:
3427         /* Parse the id-expression.  */
3428         id_expression
3429           = cp_parser_id_expression (parser,
3430                                      /*template_keyword_p=*/false,
3431                                      /*check_dependency_p=*/true,
3432                                      &template_p,
3433                                      /*declarator_p=*/false,
3434                                      /*optional_p=*/false);
3435         if (id_expression == error_mark_node)
3436           return error_mark_node;
3437         id_expr_token = token;
3438         token = cp_lexer_peek_token (parser->lexer);
3439         done = (token->type != CPP_OPEN_SQUARE
3440                 && token->type != CPP_OPEN_PAREN
3441                 && token->type != CPP_DOT
3442                 && token->type != CPP_DEREF
3443                 && token->type != CPP_PLUS_PLUS
3444                 && token->type != CPP_MINUS_MINUS);
3445         /* If we have a template-id, then no further lookup is
3446            required.  If the template-id was for a template-class, we
3447            will sometimes have a TYPE_DECL at this point.  */
3448         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3449                  || TREE_CODE (id_expression) == TYPE_DECL)
3450           decl = id_expression;
3451         /* Look up the name.  */
3452         else
3453           {
3454             tree ambiguous_decls;
3455
3456             decl = cp_parser_lookup_name (parser, id_expression,
3457                                           none_type,
3458                                           template_p,
3459                                           /*is_namespace=*/false,
3460                                           /*check_dependency=*/true,
3461                                           &ambiguous_decls,
3462                                           id_expr_token->location);
3463             /* If the lookup was ambiguous, an error will already have
3464                been issued.  */
3465             if (ambiguous_decls)
3466               return error_mark_node;
3467
3468             /* In Objective-C++, an instance variable (ivar) may be preferred
3469                to whatever cp_parser_lookup_name() found.  */
3470             decl = objc_lookup_ivar (decl, id_expression);
3471
3472             /* If name lookup gives us a SCOPE_REF, then the
3473                qualifying scope was dependent.  */
3474             if (TREE_CODE (decl) == SCOPE_REF)
3475               {
3476                 /* At this point, we do not know if DECL is a valid
3477                    integral constant expression.  We assume that it is
3478                    in fact such an expression, so that code like:
3479
3480                       template <int N> struct A {
3481                         int a[B<N>::i];
3482                       };
3483                      
3484                    is accepted.  At template-instantiation time, we
3485                    will check that B<N>::i is actually a constant.  */
3486                 return decl;
3487               }
3488             /* Check to see if DECL is a local variable in a context
3489                where that is forbidden.  */
3490             if (parser->local_variables_forbidden_p
3491                 && local_variable_p (decl))
3492               {
3493                 /* It might be that we only found DECL because we are
3494                    trying to be generous with pre-ISO scoping rules.
3495                    For example, consider:
3496
3497                      int i;
3498                      void g() {
3499                        for (int i = 0; i < 10; ++i) {}
3500                        extern void f(int j = i);
3501                      }
3502
3503                    Here, name look up will originally find the out
3504                    of scope `i'.  We need to issue a warning message,
3505                    but then use the global `i'.  */
3506                 decl = check_for_out_of_scope_variable (decl);
3507                 if (local_variable_p (decl))
3508                   {
3509                     error ("%Hlocal variable %qD may not appear in this context",
3510                            &id_expr_token->location, decl);
3511                     return error_mark_node;
3512                   }
3513               }
3514           }
3515
3516         decl = (finish_id_expression
3517                 (id_expression, decl, parser->scope,
3518                  idk,
3519                  parser->integral_constant_expression_p,
3520                  parser->allow_non_integral_constant_expression_p,
3521                  &parser->non_integral_constant_expression_p,
3522                  template_p, done, address_p,
3523                  template_arg_p,
3524                  &error_msg,
3525                  id_expr_token->location));
3526         if (error_msg)
3527           cp_parser_error (parser, error_msg);
3528         return decl;
3529       }
3530
3531       /* Anything else is an error.  */
3532     default:
3533       /* ...unless we have an Objective-C++ message or string literal,
3534          that is.  */
3535       if (c_dialect_objc ()
3536           && (token->type == CPP_OPEN_SQUARE
3537               || token->type == CPP_OBJC_STRING))
3538         return cp_parser_objc_expression (parser);
3539
3540       cp_parser_error (parser, "expected primary-expression");
3541       return error_mark_node;
3542     }
3543 }
3544
3545 /* Parse an id-expression.
3546
3547    id-expression:
3548      unqualified-id
3549      qualified-id
3550
3551    qualified-id:
3552      :: [opt] nested-name-specifier template [opt] unqualified-id
3553      :: identifier
3554      :: operator-function-id
3555      :: template-id
3556
3557    Return a representation of the unqualified portion of the
3558    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3559    a `::' or nested-name-specifier.
3560
3561    Often, if the id-expression was a qualified-id, the caller will
3562    want to make a SCOPE_REF to represent the qualified-id.  This
3563    function does not do this in order to avoid wastefully creating
3564    SCOPE_REFs when they are not required.
3565
3566    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567    `template' keyword.
3568
3569    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3570    uninstantiated templates.
3571
3572    If *TEMPLATE_P is non-NULL, it is set to true iff the
3573    `template' keyword is used to explicitly indicate that the entity
3574    named is a template.
3575
3576    If DECLARATOR_P is true, the id-expression is appearing as part of
3577    a declarator, rather than as part of an expression.  */
3578
3579 static tree
3580 cp_parser_id_expression (cp_parser *parser,
3581                          bool template_keyword_p,
3582                          bool check_dependency_p,
3583                          bool *template_p,
3584                          bool declarator_p,
3585                          bool optional_p)
3586 {
3587   bool global_scope_p;
3588   bool nested_name_specifier_p;
3589
3590   /* Assume the `template' keyword was not used.  */
3591   if (template_p)
3592     *template_p = template_keyword_p;
3593
3594   /* Look for the optional `::' operator.  */
3595   global_scope_p
3596     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3597        != NULL_TREE);
3598   /* Look for the optional nested-name-specifier.  */
3599   nested_name_specifier_p
3600     = (cp_parser_nested_name_specifier_opt (parser,
3601                                             /*typename_keyword_p=*/false,
3602                                             check_dependency_p,
3603                                             /*type_p=*/false,
3604                                             declarator_p)
3605        != NULL_TREE);
3606   /* If there is a nested-name-specifier, then we are looking at
3607      the first qualified-id production.  */
3608   if (nested_name_specifier_p)
3609     {
3610       tree saved_scope;
3611       tree saved_object_scope;
3612       tree saved_qualifying_scope;
3613       tree unqualified_id;
3614       bool is_template;
3615
3616       /* See if the next token is the `template' keyword.  */
3617       if (!template_p)
3618         template_p = &is_template;
3619       *template_p = cp_parser_optional_template_keyword (parser);
3620       /* Name lookup we do during the processing of the
3621          unqualified-id might obliterate SCOPE.  */
3622       saved_scope = parser->scope;
3623       saved_object_scope = parser->object_scope;
3624       saved_qualifying_scope = parser->qualifying_scope;
3625       /* Process the final unqualified-id.  */
3626       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627                                                  check_dependency_p,
3628                                                  declarator_p,
3629                                                  /*optional_p=*/false);
3630       /* Restore the SAVED_SCOPE for our caller.  */
3631       parser->scope = saved_scope;
3632       parser->object_scope = saved_object_scope;
3633       parser->qualifying_scope = saved_qualifying_scope;
3634
3635       return unqualified_id;
3636     }
3637   /* Otherwise, if we are in global scope, then we are looking at one
3638      of the other qualified-id productions.  */
3639   else if (global_scope_p)
3640     {
3641       cp_token *token;
3642       tree id;
3643
3644       /* Peek at the next token.  */
3645       token = cp_lexer_peek_token (parser->lexer);
3646
3647       /* If it's an identifier, and the next token is not a "<", then
3648          we can avoid the template-id case.  This is an optimization
3649          for this common case.  */
3650       if (token->type == CPP_NAME
3651           && !cp_parser_nth_token_starts_template_argument_list_p
3652                (parser, 2))
3653         return cp_parser_identifier (parser);
3654
3655       cp_parser_parse_tentatively (parser);
3656       /* Try a template-id.  */
3657       id = cp_parser_template_id (parser,
3658                                   /*template_keyword_p=*/false,
3659                                   /*check_dependency_p=*/true,
3660                                   declarator_p);
3661       /* If that worked, we're done.  */
3662       if (cp_parser_parse_definitely (parser))
3663         return id;
3664
3665       /* Peek at the next token.  (Changes in the token buffer may
3666          have invalidated the pointer obtained above.)  */
3667       token = cp_lexer_peek_token (parser->lexer);
3668
3669       switch (token->type)
3670         {
3671         case CPP_NAME:
3672           return cp_parser_identifier (parser);
3673
3674         case CPP_KEYWORD:
3675           if (token->keyword == RID_OPERATOR)
3676             return cp_parser_operator_function_id (parser);
3677           /* Fall through.  */
3678
3679         default:
3680           cp_parser_error (parser, "expected id-expression");
3681           return error_mark_node;
3682         }
3683     }
3684   else
3685     return cp_parser_unqualified_id (parser, template_keyword_p,
3686                                      /*check_dependency_p=*/true,
3687                                      declarator_p,
3688                                      optional_p);
3689 }
3690
3691 /* Parse an unqualified-id.
3692
3693    unqualified-id:
3694      identifier
3695      operator-function-id
3696      conversion-function-id
3697      ~ class-name
3698      template-id
3699
3700    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3701    keyword, in a construct like `A::template ...'.
3702
3703    Returns a representation of unqualified-id.  For the `identifier'
3704    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3705    production a BIT_NOT_EXPR is returned; the operand of the
3706    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3707    other productions, see the documentation accompanying the
3708    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3709    names are looked up in uninstantiated templates.  If DECLARATOR_P
3710    is true, the unqualified-id is appearing as part of a declarator,
3711    rather than as part of an expression.  */
3712
3713 static tree
3714 cp_parser_unqualified_id (cp_parser* parser,
3715                           bool template_keyword_p,
3716                           bool check_dependency_p,
3717                           bool declarator_p,
3718                           bool optional_p)
3719 {
3720   cp_token *token;
3721
3722   /* Peek at the next token.  */
3723   token = cp_lexer_peek_token (parser->lexer);
3724
3725   switch (token->type)
3726     {
3727     case CPP_NAME:
3728       {
3729         tree id;
3730
3731         /* We don't know yet whether or not this will be a
3732            template-id.  */
3733         cp_parser_parse_tentatively (parser);
3734         /* Try a template-id.  */
3735         id = cp_parser_template_id (parser, template_keyword_p,
3736                                     check_dependency_p,
3737                                     declarator_p);
3738         /* If it worked, we're done.  */
3739         if (cp_parser_parse_definitely (parser))
3740           return id;
3741         /* Otherwise, it's an ordinary identifier.  */
3742         return cp_parser_identifier (parser);
3743       }
3744
3745     case CPP_TEMPLATE_ID:
3746       return cp_parser_template_id (parser, template_keyword_p,
3747                                     check_dependency_p,
3748                                     declarator_p);
3749
3750     case CPP_COMPL:
3751       {
3752         tree type_decl;
3753         tree qualifying_scope;
3754         tree object_scope;
3755         tree scope;
3756         bool done;
3757
3758         /* Consume the `~' token.  */
3759         cp_lexer_consume_token (parser->lexer);
3760         /* Parse the class-name.  The standard, as written, seems to
3761            say that:
3762
3763              template <typename T> struct S { ~S (); };
3764              template <typename T> S<T>::~S() {}
3765
3766            is invalid, since `~' must be followed by a class-name, but
3767            `S<T>' is dependent, and so not known to be a class.
3768            That's not right; we need to look in uninstantiated
3769            templates.  A further complication arises from:
3770
3771              template <typename T> void f(T t) {
3772                t.T::~T();
3773              }
3774
3775            Here, it is not possible to look up `T' in the scope of `T'
3776            itself.  We must look in both the current scope, and the
3777            scope of the containing complete expression.
3778
3779            Yet another issue is:
3780
3781              struct S {
3782                int S;
3783                ~S();
3784              };
3785
3786              S::~S() {}
3787
3788            The standard does not seem to say that the `S' in `~S'
3789            should refer to the type `S' and not the data member
3790            `S::S'.  */
3791
3792         /* DR 244 says that we look up the name after the "~" in the
3793            same scope as we looked up the qualifying name.  That idea
3794            isn't fully worked out; it's more complicated than that.  */
3795         scope = parser->scope;
3796         object_scope = parser->object_scope;
3797         qualifying_scope = parser->qualifying_scope;
3798
3799         /* Check for invalid scopes.  */
3800         if (scope == error_mark_node)
3801           {
3802             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3803               cp_lexer_consume_token (parser->lexer);
3804             return error_mark_node;
3805           }
3806         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3807           {
3808             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3809               error ("%Hscope %qT before %<~%> is not a class-name",
3810                      &token->location, scope);
3811             cp_parser_simulate_error (parser);
3812             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813               cp_lexer_consume_token (parser->lexer);
3814             return error_mark_node;
3815           }
3816         gcc_assert (!scope || TYPE_P (scope));
3817
3818         /* If the name is of the form "X::~X" it's OK.  */
3819         token = cp_lexer_peek_token (parser->lexer);
3820         if (scope
3821             && token->type == CPP_NAME
3822             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3823                 == CPP_OPEN_PAREN)
3824             && constructor_name_p (token->u.value, scope))
3825           {
3826             cp_lexer_consume_token (parser->lexer);
3827             return build_nt (BIT_NOT_EXPR, scope);
3828           }
3829
3830         /* If there was an explicit qualification (S::~T), first look
3831            in the scope given by the qualification (i.e., S).  */
3832         done = false;
3833         type_decl = NULL_TREE;
3834         if (scope)
3835           {
3836             cp_parser_parse_tentatively (parser);
3837             type_decl = cp_parser_class_name (parser,
3838                                               /*typename_keyword_p=*/false,
3839                                               /*template_keyword_p=*/false,
3840                                               none_type,
3841                                               /*check_dependency=*/false,
3842                                               /*class_head_p=*/false,
3843                                               declarator_p);
3844             if (cp_parser_parse_definitely (parser))
3845               done = true;
3846           }
3847         /* In "N::S::~S", look in "N" as well.  */
3848         if (!done && scope && qualifying_scope)
3849           {
3850             cp_parser_parse_tentatively (parser);
3851             parser->scope = qualifying_scope;
3852             parser->object_scope = NULL_TREE;
3853             parser->qualifying_scope = NULL_TREE;
3854             type_decl
3855               = cp_parser_class_name (parser,
3856                                       /*typename_keyword_p=*/false,
3857                                       /*template_keyword_p=*/false,
3858                                       none_type,
3859                                       /*check_dependency=*/false,
3860                                       /*class_head_p=*/false,
3861                                       declarator_p);
3862             if (cp_parser_parse_definitely (parser))
3863               done = true;
3864           }
3865         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3866         else if (!done && object_scope)
3867           {
3868             cp_parser_parse_tentatively (parser);
3869             parser->scope = object_scope;
3870             parser->object_scope = NULL_TREE;
3871             parser->qualifying_scope = NULL_TREE;
3872             type_decl
3873               = cp_parser_class_name (parser,
3874                                       /*typename_keyword_p=*/false,
3875                                       /*template_keyword_p=*/false,
3876                                       none_type,
3877                                       /*check_dependency=*/false,
3878                                       /*class_head_p=*/false,
3879                                       declarator_p);
3880             if (cp_parser_parse_definitely (parser))
3881               done = true;
3882           }
3883         /* Look in the surrounding context.  */
3884         if (!done)
3885           {
3886             parser->scope = NULL_TREE;
3887             parser->object_scope = NULL_TREE;
3888             parser->qualifying_scope = NULL_TREE;
3889             if (processing_template_decl)
3890               cp_parser_parse_tentatively (parser);
3891             type_decl
3892               = cp_parser_class_name (parser,
3893                                       /*typename_keyword_p=*/false,
3894                                       /*template_keyword_p=*/false,
3895                                       none_type,
3896                                       /*check_dependency=*/false,
3897                                       /*class_head_p=*/false,
3898                                       declarator_p);
3899             if (processing_template_decl
3900                 && ! cp_parser_parse_definitely (parser))
3901               {
3902                 /* We couldn't find a type with this name, so just accept
3903                    it and check for a match at instantiation time.  */
3904                 type_decl = cp_parser_identifier (parser);
3905                 if (type_decl != error_mark_node)
3906                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3907                 return type_decl;
3908               }
3909           }
3910         /* If an error occurred, assume that the name of the
3911            destructor is the same as the name of the qualifying
3912            class.  That allows us to keep parsing after running
3913            into ill-formed destructor names.  */
3914         if (type_decl == error_mark_node && scope)
3915           return build_nt (BIT_NOT_EXPR, scope);
3916         else if (type_decl == error_mark_node)
3917           return error_mark_node;
3918
3919         /* Check that destructor name and scope match.  */
3920         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3921           {
3922             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3923               error ("%Hdeclaration of %<~%T%> as member of %qT",
3924                      &token->location, type_decl, scope);
3925             cp_parser_simulate_error (parser);
3926             return error_mark_node;
3927           }
3928
3929         /* [class.dtor]
3930
3931            A typedef-name that names a class shall not be used as the
3932            identifier in the declarator for a destructor declaration.  */
3933         if (declarator_p
3934             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3935             && !DECL_SELF_REFERENCE_P (type_decl)
3936             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3937           error ("%Htypedef-name %qD used as destructor declarator",
3938                  &token->location, type_decl);
3939
3940         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3941       }
3942
3943     case CPP_KEYWORD:
3944       if (token->keyword == RID_OPERATOR)
3945         {
3946           tree id;
3947
3948           /* This could be a template-id, so we try that first.  */
3949           cp_parser_parse_tentatively (parser);
3950           /* Try a template-id.  */
3951           id = cp_parser_template_id (parser, template_keyword_p,
3952                                       /*check_dependency_p=*/true,
3953                                       declarator_p);
3954           /* If that worked, we're done.  */
3955           if (cp_parser_parse_definitely (parser))
3956             return id;
3957           /* We still don't know whether we're looking at an
3958              operator-function-id or a conversion-function-id.  */
3959           cp_parser_parse_tentatively (parser);
3960           /* Try an operator-function-id.  */
3961           id = cp_parser_operator_function_id (parser);
3962           /* If that didn't work, try a conversion-function-id.  */
3963           if (!cp_parser_parse_definitely (parser))
3964             id = cp_parser_conversion_function_id (parser);
3965
3966           return id;
3967         }
3968       /* Fall through.  */
3969
3970     default:
3971       if (optional_p)
3972         return NULL_TREE;
3973       cp_parser_error (parser, "expected unqualified-id");
3974       return error_mark_node;
3975     }
3976 }
3977
3978 /* Parse an (optional) nested-name-specifier.
3979
3980    nested-name-specifier: [C++98]
3981      class-or-namespace-name :: nested-name-specifier [opt]
3982      class-or-namespace-name :: template nested-name-specifier [opt]
3983
3984    nested-name-specifier: [C++0x]
3985      type-name ::
3986      namespace-name ::
3987      nested-name-specifier identifier ::
3988      nested-name-specifier template [opt] simple-template-id ::
3989
3990    PARSER->SCOPE should be set appropriately before this function is
3991    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3992    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3993    in name lookups.
3994
3995    Sets PARSER->SCOPE to the class (TYPE) or namespace
3996    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3997    it unchanged if there is no nested-name-specifier.  Returns the new
3998    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3999
4000    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4001    part of a declaration and/or decl-specifier.  */
4002
4003 static tree
4004 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4005                                      bool typename_keyword_p,
4006                                      bool check_dependency_p,
4007                                      bool type_p,
4008                                      bool is_declaration)
4009 {
4010   bool success = false;
4011   cp_token_position start = 0;
4012   cp_token *token;
4013
4014   /* Remember where the nested-name-specifier starts.  */
4015   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4016     {
4017       start = cp_lexer_token_position (parser->lexer, false);
4018       push_deferring_access_checks (dk_deferred);
4019     }
4020
4021   while (true)
4022     {
4023       tree new_scope;
4024       tree old_scope;
4025       tree saved_qualifying_scope;
4026       bool template_keyword_p;
4027
4028       /* Spot cases that cannot be the beginning of a
4029          nested-name-specifier.  */
4030       token = cp_lexer_peek_token (parser->lexer);
4031
4032       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4033          the already parsed nested-name-specifier.  */
4034       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4035         {
4036           /* Grab the nested-name-specifier and continue the loop.  */
4037           cp_parser_pre_parsed_nested_name_specifier (parser);
4038           /* If we originally encountered this nested-name-specifier
4039              with IS_DECLARATION set to false, we will not have
4040              resolved TYPENAME_TYPEs, so we must do so here.  */
4041           if (is_declaration
4042               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4043             {
4044               new_scope = resolve_typename_type (parser->scope,
4045                                                  /*only_current_p=*/false);
4046               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4047                 parser->scope = new_scope;
4048             }
4049           success = true;
4050           continue;
4051         }
4052
4053       /* Spot cases that cannot be the beginning of a
4054          nested-name-specifier.  On the second and subsequent times
4055          through the loop, we look for the `template' keyword.  */
4056       if (success && token->keyword == RID_TEMPLATE)
4057         ;
4058       /* A template-id can start a nested-name-specifier.  */
4059       else if (token->type == CPP_TEMPLATE_ID)
4060         ;
4061       else
4062         {
4063           /* If the next token is not an identifier, then it is
4064              definitely not a type-name or namespace-name.  */
4065           if (token->type != CPP_NAME)
4066             break;
4067           /* If the following token is neither a `<' (to begin a
4068              template-id), nor a `::', then we are not looking at a
4069              nested-name-specifier.  */
4070           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4071           if (token->type != CPP_SCOPE
4072               && !cp_parser_nth_token_starts_template_argument_list_p
4073                   (parser, 2))
4074             break;
4075         }
4076
4077       /* The nested-name-specifier is optional, so we parse
4078          tentatively.  */
4079       cp_parser_parse_tentatively (parser);
4080
4081       /* Look for the optional `template' keyword, if this isn't the
4082          first time through the loop.  */
4083       if (success)
4084         template_keyword_p = cp_parser_optional_template_keyword (parser);
4085       else
4086         template_keyword_p = false;
4087
4088       /* Save the old scope since the name lookup we are about to do
4089          might destroy it.  */
4090       old_scope = parser->scope;
4091       saved_qualifying_scope = parser->qualifying_scope;
4092       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4093          look up names in "X<T>::I" in order to determine that "Y" is
4094          a template.  So, if we have a typename at this point, we make
4095          an effort to look through it.  */
4096       if (is_declaration
4097           && !typename_keyword_p
4098           && parser->scope
4099           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4100         parser->scope = resolve_typename_type (parser->scope,
4101                                                /*only_current_p=*/false);
4102       /* Parse the qualifying entity.  */
4103       new_scope
4104         = cp_parser_qualifying_entity (parser,
4105                                        typename_keyword_p,
4106                                        template_keyword_p,
4107                                        check_dependency_p,
4108                                        type_p,
4109                                        is_declaration);
4110       /* Look for the `::' token.  */
4111       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4112
4113       /* If we found what we wanted, we keep going; otherwise, we're
4114          done.  */
4115       if (!cp_parser_parse_definitely (parser))
4116         {
4117           bool error_p = false;
4118
4119           /* Restore the OLD_SCOPE since it was valid before the
4120              failed attempt at finding the last
4121              class-or-namespace-name.  */
4122           parser->scope = old_scope;
4123           parser->qualifying_scope = saved_qualifying_scope;
4124           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4125             break;
4126           /* If the next token is an identifier, and the one after
4127              that is a `::', then any valid interpretation would have
4128              found a class-or-namespace-name.  */
4129           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4130                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4131                      == CPP_SCOPE)
4132                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133                      != CPP_COMPL))
4134             {
4135               token = cp_lexer_consume_token (parser->lexer);
4136               if (!error_p)
4137                 {
4138                   if (!token->ambiguous_p)
4139                     {
4140                       tree decl;
4141                       tree ambiguous_decls;
4142
4143                       decl = cp_parser_lookup_name (parser, token->u.value,
4144                                                     none_type,
4145                                                     /*is_template=*/false,
4146                                                     /*is_namespace=*/false,
4147                                                     /*check_dependency=*/true,
4148                                                     &ambiguous_decls,
4149                                                     token->location);
4150                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4151                         error ("%H%qD used without template parameters",
4152                                &token->location, decl);
4153                       else if (ambiguous_decls)
4154                         {
4155                           error ("%Hreference to %qD is ambiguous",
4156                                  &token->location, token->u.value);
4157                           print_candidates (ambiguous_decls);
4158                           decl = error_mark_node;
4159                         }
4160                       else
4161                         {
4162                           const char* msg = "is not a class or namespace";
4163                           if (cxx_dialect != cxx98)
4164                             msg = "is not a class, namespace, or enumeration";
4165                           cp_parser_name_lookup_error
4166                             (parser, token->u.value, decl, msg,
4167                              token->location);
4168                         }
4169                     }
4170                   parser->scope = error_mark_node;
4171                   error_p = true;
4172                   /* Treat this as a successful nested-name-specifier
4173                      due to:
4174
4175                      [basic.lookup.qual]
4176
4177                      If the name found is not a class-name (clause
4178                      _class_) or namespace-name (_namespace.def_), the
4179                      program is ill-formed.  */
4180                   success = true;
4181                 }
4182               cp_lexer_consume_token (parser->lexer);
4183             }
4184           break;
4185         }
4186       /* We've found one valid nested-name-specifier.  */
4187       success = true;
4188       /* Name lookup always gives us a DECL.  */
4189       if (TREE_CODE (new_scope) == TYPE_DECL)
4190         new_scope = TREE_TYPE (new_scope);
4191       /* Uses of "template" must be followed by actual templates.  */
4192       if (template_keyword_p
4193           && !(CLASS_TYPE_P (new_scope)
4194                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4195                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4196                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4197           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4198                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4199                    == TEMPLATE_ID_EXPR)))
4200         permerror (input_location, TYPE_P (new_scope)
4201                    ? "%qT is not a template"
4202                    : "%qD is not a template",
4203                    new_scope);
4204       /* If it is a class scope, try to complete it; we are about to
4205          be looking up names inside the class.  */
4206       if (TYPE_P (new_scope)
4207           /* Since checking types for dependency can be expensive,
4208              avoid doing it if the type is already complete.  */
4209           && !COMPLETE_TYPE_P (new_scope)
4210           /* Do not try to complete dependent types.  */
4211           && !dependent_type_p (new_scope))
4212         {
4213           new_scope = complete_type (new_scope);
4214           /* If it is a typedef to current class, use the current
4215              class instead, as the typedef won't have any names inside
4216              it yet.  */
4217           if (!COMPLETE_TYPE_P (new_scope)
4218               && currently_open_class (new_scope))
4219             new_scope = TYPE_MAIN_VARIANT (new_scope);
4220         }
4221       /* Make sure we look in the right scope the next time through
4222          the loop.  */
4223       parser->scope = new_scope;
4224     }
4225
4226   /* If parsing tentatively, replace the sequence of tokens that makes
4227      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4228      token.  That way, should we re-parse the token stream, we will
4229      not have to repeat the effort required to do the parse, nor will
4230      we issue duplicate error messages.  */
4231   if (success && start)
4232     {
4233       cp_token *token;
4234
4235       token = cp_lexer_token_at (parser->lexer, start);
4236       /* Reset the contents of the START token.  */
4237       token->type = CPP_NESTED_NAME_SPECIFIER;
4238       /* Retrieve any deferred checks.  Do not pop this access checks yet
4239          so the memory will not be reclaimed during token replacing below.  */
4240       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4241       token->u.tree_check_value->value = parser->scope;
4242       token->u.tree_check_value->checks = get_deferred_access_checks ();
4243       token->u.tree_check_value->qualifying_scope =
4244         parser->qualifying_scope;
4245       token->keyword = RID_MAX;
4246
4247       /* Purge all subsequent tokens.  */
4248       cp_lexer_purge_tokens_after (parser->lexer, start);
4249     }
4250
4251   if (start)
4252     pop_to_parent_deferring_access_checks ();
4253
4254   return success ? parser->scope : NULL_TREE;
4255 }
4256
4257 /* Parse a nested-name-specifier.  See
4258    cp_parser_nested_name_specifier_opt for details.  This function
4259    behaves identically, except that it will an issue an error if no
4260    nested-name-specifier is present.  */
4261
4262 static tree
4263 cp_parser_nested_name_specifier (cp_parser *parser,
4264                                  bool typename_keyword_p,
4265                                  bool check_dependency_p,
4266                                  bool type_p,
4267                                  bool is_declaration)
4268 {
4269   tree scope;
4270
4271   /* Look for the nested-name-specifier.  */
4272   scope = cp_parser_nested_name_specifier_opt (parser,
4273                                                typename_keyword_p,
4274                                                check_dependency_p,
4275                                                type_p,
4276                                                is_declaration);
4277   /* If it was not present, issue an error message.  */
4278   if (!scope)
4279     {
4280       cp_parser_error (parser, "expected nested-name-specifier");
4281       parser->scope = NULL_TREE;
4282     }
4283
4284   return scope;
4285 }
4286
4287 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4288    this is either a class-name or a namespace-name (which corresponds
4289    to the class-or-namespace-name production in the grammar). For
4290    C++0x, it can also be a type-name that refers to an enumeration
4291    type.
4292
4293    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4294    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4295    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4296    TYPE_P is TRUE iff the next name should be taken as a class-name,
4297    even the same name is declared to be another entity in the same
4298    scope.
4299
4300    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4301    specified by the class-or-namespace-name.  If neither is found the
4302    ERROR_MARK_NODE is returned.  */
4303
4304 static tree
4305 cp_parser_qualifying_entity (cp_parser *parser,
4306                              bool typename_keyword_p,
4307                              bool template_keyword_p,
4308                              bool check_dependency_p,
4309                              bool type_p,
4310                              bool is_declaration)
4311 {
4312   tree saved_scope;
4313   tree saved_qualifying_scope;
4314   tree saved_object_scope;
4315   tree scope;
4316   bool only_class_p;
4317   bool successful_parse_p;
4318
4319   /* Before we try to parse the class-name, we must save away the
4320      current PARSER->SCOPE since cp_parser_class_name will destroy
4321      it.  */
4322   saved_scope = parser->scope;
4323   saved_qualifying_scope = parser->qualifying_scope;
4324   saved_object_scope = parser->object_scope;
4325   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4326      there is no need to look for a namespace-name.  */
4327   only_class_p = template_keyword_p 
4328     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4329   if (!only_class_p)
4330     cp_parser_parse_tentatively (parser);
4331   scope = cp_parser_class_name (parser,
4332                                 typename_keyword_p,
4333                                 template_keyword_p,
4334                                 type_p ? class_type : none_type,
4335                                 check_dependency_p,
4336                                 /*class_head_p=*/false,
4337                                 is_declaration);
4338   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4339   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4340   if (!only_class_p 
4341       && cxx_dialect != cxx98
4342       && !successful_parse_p)
4343     {
4344       /* Restore the saved scope.  */
4345       parser->scope = saved_scope;
4346       parser->qualifying_scope = saved_qualifying_scope;
4347       parser->object_scope = saved_object_scope;
4348
4349       /* Parse tentatively.  */
4350       cp_parser_parse_tentatively (parser);
4351      
4352       /* Parse a typedef-name or enum-name.  */
4353       scope = cp_parser_nonclass_name (parser);
4354       successful_parse_p = cp_parser_parse_definitely (parser);
4355     }
4356   /* If that didn't work, try for a namespace-name.  */
4357   if (!only_class_p && !successful_parse_p)
4358     {
4359       /* Restore the saved scope.  */
4360       parser->scope = saved_scope;
4361       parser->qualifying_scope = saved_qualifying_scope;
4362       parser->object_scope = saved_object_scope;
4363       /* If we are not looking at an identifier followed by the scope
4364          resolution operator, then this is not part of a
4365          nested-name-specifier.  (Note that this function is only used
4366          to parse the components of a nested-name-specifier.)  */
4367       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4368           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4369         return error_mark_node;
4370       scope = cp_parser_namespace_name (parser);
4371     }
4372
4373   return scope;
4374 }
4375
4376 /* Parse a postfix-expression.
4377
4378    postfix-expression:
4379      primary-expression
4380      postfix-expression [ expression ]
4381      postfix-expression ( expression-list [opt] )
4382      simple-type-specifier ( expression-list [opt] )
4383      typename :: [opt] nested-name-specifier identifier
4384        ( expression-list [opt] )
4385      typename :: [opt] nested-name-specifier template [opt] template-id
4386        ( expression-list [opt] )
4387      postfix-expression . template [opt] id-expression
4388      postfix-expression -> template [opt] id-expression
4389      postfix-expression . pseudo-destructor-name
4390      postfix-expression -> pseudo-destructor-name
4391      postfix-expression ++
4392      postfix-expression --
4393      dynamic_cast < type-id > ( expression )
4394      static_cast < type-id > ( expression )
4395      reinterpret_cast < type-id > ( expression )
4396      const_cast < type-id > ( expression )
4397      typeid ( expression )
4398      typeid ( type-id )
4399
4400    GNU Extension:
4401
4402    postfix-expression:
4403      ( type-id ) { initializer-list , [opt] }
4404
4405    This extension is a GNU version of the C99 compound-literal
4406    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4407    but they are essentially the same concept.)
4408
4409    If ADDRESS_P is true, the postfix expression is the operand of the
4410    `&' operator.  CAST_P is true if this expression is the target of a
4411    cast.
4412
4413    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4414    class member access expressions [expr.ref].
4415
4416    Returns a representation of the expression.  */
4417
4418 static tree
4419 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4420                               bool member_access_only_p,
4421                               cp_id_kind * pidk_return)
4422 {
4423   cp_token *token;
4424   enum rid keyword;
4425   cp_id_kind idk = CP_ID_KIND_NONE;
4426   tree postfix_expression = NULL_TREE;
4427   bool is_member_access = false;
4428
4429   /* Peek at the next token.  */
4430   token = cp_lexer_peek_token (parser->lexer);
4431   /* Some of the productions are determined by keywords.  */
4432   keyword = token->keyword;
4433   switch (keyword)
4434     {
4435     case RID_DYNCAST:
4436     case RID_STATCAST:
4437     case RID_REINTCAST:
4438     case RID_CONSTCAST:
4439       {
4440         tree type;
4441         tree expression;
4442         const char *saved_message;
4443
4444         /* All of these can be handled in the same way from the point
4445            of view of parsing.  Begin by consuming the token
4446            identifying the cast.  */
4447         cp_lexer_consume_token (parser->lexer);
4448
4449         /* New types cannot be defined in the cast.  */
4450         saved_message = parser->type_definition_forbidden_message;
4451         parser->type_definition_forbidden_message
4452           = "types may not be defined in casts";
4453
4454         /* Look for the opening `<'.  */
4455         cp_parser_require (parser, CPP_LESS, "%<<%>");
4456         /* Parse the type to which we are casting.  */
4457         type = cp_parser_type_id (parser);
4458         /* Look for the closing `>'.  */
4459         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4460         /* Restore the old message.  */
4461         parser->type_definition_forbidden_message = saved_message;
4462
4463         /* And the expression which is being cast.  */
4464         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4465         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4466         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4467
4468         /* Only type conversions to integral or enumeration types
4469            can be used in constant-expressions.  */
4470         if (!cast_valid_in_integral_constant_expression_p (type)
4471             && (cp_parser_non_integral_constant_expression
4472                 (parser,
4473                  "a cast to a type other than an integral or "
4474                  "enumeration type")))
4475           return error_mark_node;
4476
4477         switch (keyword)
4478           {
4479           case RID_DYNCAST:
4480             postfix_expression
4481               = build_dynamic_cast (type, expression, tf_warning_or_error);
4482             break;
4483           case RID_STATCAST:
4484             postfix_expression
4485               = build_static_cast (type, expression, tf_warning_or_error);
4486             break;
4487           case RID_REINTCAST:
4488             postfix_expression
4489               = build_reinterpret_cast (type, expression, 
4490                                         tf_warning_or_error);
4491             break;
4492           case RID_CONSTCAST:
4493             postfix_expression
4494               = build_const_cast (type, expression, tf_warning_or_error);
4495             break;
4496           default:
4497             gcc_unreachable ();
4498           }
4499       }
4500       break;
4501
4502     case RID_TYPEID:
4503       {
4504         tree type;
4505         const char *saved_message;
4506         bool saved_in_type_id_in_expr_p;
4507
4508         /* Consume the `typeid' token.  */
4509         cp_lexer_consume_token (parser->lexer);
4510         /* Look for the `(' token.  */
4511         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4512         /* Types cannot be defined in a `typeid' expression.  */
4513         saved_message = parser->type_definition_forbidden_message;
4514         parser->type_definition_forbidden_message
4515           = "types may not be defined in a %<typeid%> expression";
4516         /* We can't be sure yet whether we're looking at a type-id or an
4517            expression.  */
4518         cp_parser_parse_tentatively (parser);
4519         /* Try a type-id first.  */
4520         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521         parser->in_type_id_in_expr_p = true;
4522         type = cp_parser_type_id (parser);
4523         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524         /* Look for the `)' token.  Otherwise, we can't be sure that
4525            we're not looking at an expression: consider `typeid (int
4526            (3))', for example.  */
4527         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4528         /* If all went well, simply lookup the type-id.  */
4529         if (cp_parser_parse_definitely (parser))
4530           postfix_expression = get_typeid (type);
4531         /* Otherwise, fall back to the expression variant.  */
4532         else
4533           {
4534             tree expression;
4535
4536             /* Look for an expression.  */
4537             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4538             /* Compute its typeid.  */
4539             postfix_expression = build_typeid (expression);
4540             /* Look for the `)' token.  */
4541             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4542           }
4543         /* Restore the saved message.  */
4544         parser->type_definition_forbidden_message = saved_message;
4545         /* `typeid' may not appear in an integral constant expression.  */
4546         if (cp_parser_non_integral_constant_expression(parser,
4547                                                        "%<typeid%> operator"))
4548           return error_mark_node;
4549       }
4550       break;
4551
4552     case RID_TYPENAME:
4553       {
4554         tree type;
4555         /* The syntax permitted here is the same permitted for an
4556            elaborated-type-specifier.  */
4557         type = cp_parser_elaborated_type_specifier (parser,
4558                                                     /*is_friend=*/false,
4559                                                     /*is_declaration=*/false);
4560         postfix_expression = cp_parser_functional_cast (parser, type);
4561       }
4562       break;
4563
4564     default:
4565       {
4566         tree type;
4567
4568         /* If the next thing is a simple-type-specifier, we may be
4569            looking at a functional cast.  We could also be looking at
4570            an id-expression.  So, we try the functional cast, and if
4571            that doesn't work we fall back to the primary-expression.  */
4572         cp_parser_parse_tentatively (parser);
4573         /* Look for the simple-type-specifier.  */
4574         type = cp_parser_simple_type_specifier (parser,
4575                                                 /*decl_specs=*/NULL,
4576                                                 CP_PARSER_FLAGS_NONE);
4577         /* Parse the cast itself.  */
4578         if (!cp_parser_error_occurred (parser))
4579           postfix_expression
4580             = cp_parser_functional_cast (parser, type);
4581         /* If that worked, we're done.  */
4582         if (cp_parser_parse_definitely (parser))
4583           break;
4584
4585         /* If the functional-cast didn't work out, try a
4586            compound-literal.  */
4587         if (cp_parser_allow_gnu_extensions_p (parser)
4588             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4589           {
4590             VEC(constructor_elt,gc) *initializer_list = NULL;
4591             bool saved_in_type_id_in_expr_p;
4592
4593             cp_parser_parse_tentatively (parser);
4594             /* Consume the `('.  */
4595             cp_lexer_consume_token (parser->lexer);
4596             /* Parse the type.  */
4597             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4598             parser->in_type_id_in_expr_p = true;
4599             type = cp_parser_type_id (parser);
4600             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4601             /* Look for the `)'.  */
4602             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4603             /* Look for the `{'.  */
4604             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4605             /* If things aren't going well, there's no need to
4606                keep going.  */
4607             if (!cp_parser_error_occurred (parser))
4608               {
4609                 bool non_constant_p;
4610                 /* Parse the initializer-list.  */
4611                 initializer_list
4612                   = cp_parser_initializer_list (parser, &non_constant_p);
4613                 /* Allow a trailing `,'.  */
4614                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4615                   cp_lexer_consume_token (parser->lexer);
4616                 /* Look for the final `}'.  */
4617                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4618               }
4619             /* If that worked, we're definitely looking at a
4620                compound-literal expression.  */
4621             if (cp_parser_parse_definitely (parser))
4622               {
4623                 /* Warn the user that a compound literal is not
4624                    allowed in standard C++.  */
4625                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4626                 /* For simplicity, we disallow compound literals in
4627                    constant-expressions.  We could
4628                    allow compound literals of integer type, whose
4629                    initializer was a constant, in constant
4630                    expressions.  Permitting that usage, as a further
4631                    extension, would not change the meaning of any
4632                    currently accepted programs.  (Of course, as
4633                    compound literals are not part of ISO C++, the
4634                    standard has nothing to say.)  */
4635                 if (cp_parser_non_integral_constant_expression 
4636                     (parser, "non-constant compound literals"))
4637                   {
4638                     postfix_expression = error_mark_node;
4639                     break;
4640                   }
4641                 /* Form the representation of the compound-literal.  */
4642                 postfix_expression
4643                   = (finish_compound_literal
4644                      (type, build_constructor (init_list_type_node,
4645                                                initializer_list)));
4646                 break;
4647               }
4648           }
4649
4650         /* It must be a primary-expression.  */
4651         postfix_expression
4652           = cp_parser_primary_expression (parser, address_p, cast_p,
4653                                           /*template_arg_p=*/false,
4654                                           &idk);
4655       }
4656       break;
4657     }
4658
4659   /* Keep looping until the postfix-expression is complete.  */
4660   while (true)
4661     {
4662       if (idk == CP_ID_KIND_UNQUALIFIED
4663           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4664           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4665         /* It is not a Koenig lookup function call.  */
4666         postfix_expression
4667           = unqualified_name_lookup_error (postfix_expression);
4668
4669       /* Peek at the next token.  */
4670       token = cp_lexer_peek_token (parser->lexer);
4671
4672       switch (token->type)
4673         {
4674         case CPP_OPEN_SQUARE:
4675           postfix_expression
4676             = cp_parser_postfix_open_square_expression (parser,
4677                                                         postfix_expression,
4678                                                         false);
4679           idk = CP_ID_KIND_NONE;
4680           is_member_access = false;
4681           break;
4682
4683         case CPP_OPEN_PAREN:
4684           /* postfix-expression ( expression-list [opt] ) */
4685           {
4686             bool koenig_p;
4687             bool is_builtin_constant_p;
4688             bool saved_integral_constant_expression_p = false;
4689             bool saved_non_integral_constant_expression_p = false;
4690             tree args;
4691
4692             is_member_access = false;
4693
4694             is_builtin_constant_p
4695               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4696             if (is_builtin_constant_p)
4697               {
4698                 /* The whole point of __builtin_constant_p is to allow
4699                    non-constant expressions to appear as arguments.  */
4700                 saved_integral_constant_expression_p
4701                   = parser->integral_constant_expression_p;
4702                 saved_non_integral_constant_expression_p
4703                   = parser->non_integral_constant_expression_p;
4704                 parser->integral_constant_expression_p = false;
4705               }
4706             args = (cp_parser_parenthesized_expression_list
4707                     (parser, /*is_attribute_list=*/false,
4708                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4709                      /*non_constant_p=*/NULL));
4710             if (is_builtin_constant_p)
4711               {
4712                 parser->integral_constant_expression_p
4713                   = saved_integral_constant_expression_p;
4714                 parser->non_integral_constant_expression_p
4715                   = saved_non_integral_constant_expression_p;
4716               }
4717
4718             if (args == error_mark_node)
4719               {
4720                 postfix_expression = error_mark_node;
4721                 break;
4722               }
4723
4724             /* Function calls are not permitted in
4725                constant-expressions.  */
4726             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4727                 && cp_parser_non_integral_constant_expression (parser,
4728                                                                "a function call"))
4729               {
4730                 postfix_expression = error_mark_node;
4731                 break;
4732               }
4733
4734             koenig_p = false;
4735             if (idk == CP_ID_KIND_UNQUALIFIED
4736                 || idk == CP_ID_KIND_TEMPLATE_ID)
4737               {
4738                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4739                   {
4740                     if (args)
4741                       {
4742                         koenig_p = true;
4743                         postfix_expression
4744                           = perform_koenig_lookup (postfix_expression, args);
4745                       }
4746                     else
4747                       postfix_expression
4748                         = unqualified_fn_lookup_error (postfix_expression);
4749                   }
4750                 /* We do not perform argument-dependent lookup if
4751                    normal lookup finds a non-function, in accordance
4752                    with the expected resolution of DR 218.  */
4753                 else if (args && is_overloaded_fn (postfix_expression))
4754                   {
4755                     tree fn = get_first_fn (postfix_expression);
4756
4757                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4758                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4759
4760                     /* Only do argument dependent lookup if regular
4761                        lookup does not find a set of member functions.
4762                        [basic.lookup.koenig]/2a  */
4763                     if (!DECL_FUNCTION_MEMBER_P (fn))
4764                       {
4765                         koenig_p = true;
4766                         postfix_expression
4767                           = perform_koenig_lookup (postfix_expression, args);
4768                       }
4769                   }
4770               }
4771
4772             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4773               {
4774                 tree instance = TREE_OPERAND (postfix_expression, 0);
4775                 tree fn = TREE_OPERAND (postfix_expression, 1);
4776
4777                 if (processing_template_decl
4778                     && (type_dependent_expression_p (instance)
4779                         || (!BASELINK_P (fn)
4780                             && TREE_CODE (fn) != FIELD_DECL)
4781                         || type_dependent_expression_p (fn)
4782                         || any_type_dependent_arguments_p (args)))
4783                   {
4784                     postfix_expression
4785                       = build_nt_call_list (postfix_expression, args);
4786                     break;
4787                   }
4788
4789                 if (BASELINK_P (fn))
4790                   {
4791                   postfix_expression
4792                     = (build_new_method_call
4793                        (instance, fn, args, NULL_TREE,
4794                         (idk == CP_ID_KIND_QUALIFIED
4795                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4796                         /*fn_p=*/NULL,
4797                         tf_warning_or_error));
4798                   }
4799                 else
4800                   postfix_expression
4801                     = finish_call_expr (postfix_expression, args,
4802                                         /*disallow_virtual=*/false,
4803                                         /*koenig_p=*/false,
4804                                         tf_warning_or_error);
4805               }
4806             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4807                      || TREE_CODE (postfix_expression) == MEMBER_REF
4808                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4809               postfix_expression = (build_offset_ref_call_from_tree
4810                                     (postfix_expression, args));
4811             else if (idk == CP_ID_KIND_QUALIFIED)
4812               /* A call to a static class member, or a namespace-scope
4813                  function.  */
4814               postfix_expression
4815                 = finish_call_expr (postfix_expression, args,
4816                                     /*disallow_virtual=*/true,
4817                                     koenig_p,
4818                                     tf_warning_or_error);
4819             else
4820               /* All other function calls.  */
4821               postfix_expression
4822                 = finish_call_expr (postfix_expression, args,
4823                                     /*disallow_virtual=*/false,
4824                                     koenig_p,
4825                                     tf_warning_or_error);
4826
4827             if (warn_disallowed_functions)
4828               warn_if_disallowed_function_p (postfix_expression);
4829
4830             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4831             idk = CP_ID_KIND_NONE;
4832           }
4833           break;
4834
4835         case CPP_DOT:
4836         case CPP_DEREF:
4837           /* postfix-expression . template [opt] id-expression
4838              postfix-expression . pseudo-destructor-name
4839              postfix-expression -> template [opt] id-expression
4840              postfix-expression -> pseudo-destructor-name */
4841
4842           /* Consume the `.' or `->' operator.  */
4843           cp_lexer_consume_token (parser->lexer);
4844
4845           postfix_expression
4846             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4847                                                       postfix_expression,
4848                                                       false, &idk,
4849                                                       token->location);
4850
4851           is_member_access = true;
4852           break;
4853
4854         case CPP_PLUS_PLUS:
4855           /* postfix-expression ++  */
4856           /* Consume the `++' token.  */
4857           cp_lexer_consume_token (parser->lexer);
4858           /* Generate a representation for the complete expression.  */
4859           postfix_expression
4860             = finish_increment_expr (postfix_expression,
4861                                      POSTINCREMENT_EXPR);
4862           /* Increments may not appear in constant-expressions.  */
4863           if (cp_parser_non_integral_constant_expression (parser,
4864                                                           "an increment"))
4865             postfix_expression = error_mark_node;
4866           idk = CP_ID_KIND_NONE;
4867           is_member_access = false;
4868           break;
4869
4870         case CPP_MINUS_MINUS:
4871           /* postfix-expression -- */
4872           /* Consume the `--' token.  */
4873           cp_lexer_consume_token (parser->lexer);
4874           /* Generate a representation for the complete expression.  */
4875           postfix_expression
4876             = finish_increment_expr (postfix_expression,
4877                                      POSTDECREMENT_EXPR);
4878           /* Decrements may not appear in constant-expressions.  */
4879           if (cp_parser_non_integral_constant_expression (parser,
4880                                                           "a decrement"))
4881             postfix_expression = error_mark_node;
4882           idk = CP_ID_KIND_NONE;
4883           is_member_access = false;
4884           break;
4885
4886         default:
4887           if (pidk_return != NULL)
4888             * pidk_return = idk;
4889           if (member_access_only_p)
4890             return is_member_access? postfix_expression : error_mark_node;
4891           else
4892             return postfix_expression;
4893         }
4894     }
4895
4896   /* We should never get here.  */
4897   gcc_unreachable ();
4898   return error_mark_node;
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 [ expression ]
4905
4906    FOR_OFFSETOF is set if we're being called in that context, which
4907    changes how we deal with integer constant expressions.  */
4908
4909 static tree
4910 cp_parser_postfix_open_square_expression (cp_parser *parser,
4911                                           tree postfix_expression,
4912                                           bool for_offsetof)
4913 {
4914   tree index;
4915
4916   /* Consume the `[' token.  */
4917   cp_lexer_consume_token (parser->lexer);
4918
4919   /* Parse the index expression.  */
4920   /* ??? For offsetof, there is a question of what to allow here.  If
4921      offsetof is not being used in an integral constant expression context,
4922      then we *could* get the right answer by computing the value at runtime.
4923      If we are in an integral constant expression context, then we might
4924      could accept any constant expression; hard to say without analysis.
4925      Rather than open the barn door too wide right away, allow only integer
4926      constant expressions here.  */
4927   if (for_offsetof)
4928     index = cp_parser_constant_expression (parser, false, NULL);
4929   else
4930     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4931
4932   /* Look for the closing `]'.  */
4933   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4934
4935   /* Build the ARRAY_REF.  */
4936   postfix_expression = grok_array_decl (postfix_expression, index);
4937
4938   /* When not doing offsetof, array references are not permitted in
4939      constant-expressions.  */
4940   if (!for_offsetof
4941       && (cp_parser_non_integral_constant_expression
4942           (parser, "an array reference")))
4943     postfix_expression = error_mark_node;
4944
4945   return postfix_expression;
4946 }
4947
4948 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4949    by cp_parser_builtin_offsetof.  We're looking for
4950
4951      postfix-expression . template [opt] id-expression
4952      postfix-expression . pseudo-destructor-name
4953      postfix-expression -> template [opt] id-expression
4954      postfix-expression -> pseudo-destructor-name
4955
4956    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4957    limits what of the above we'll actually accept, but nevermind.
4958    TOKEN_TYPE is the "." or "->" token, which will already have been
4959    removed from the stream.  */
4960
4961 static tree
4962 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4963                                         enum cpp_ttype token_type,
4964                                         tree postfix_expression,
4965                                         bool for_offsetof, cp_id_kind *idk,
4966                                         location_t location)
4967 {
4968   tree name;
4969   bool dependent_p;
4970   bool pseudo_destructor_p;
4971   tree scope = NULL_TREE;
4972
4973   /* If this is a `->' operator, dereference the pointer.  */
4974   if (token_type == CPP_DEREF)
4975     postfix_expression = build_x_arrow (postfix_expression);
4976   /* Check to see whether or not the expression is type-dependent.  */
4977   dependent_p = type_dependent_expression_p (postfix_expression);
4978   /* The identifier following the `->' or `.' is not qualified.  */
4979   parser->scope = NULL_TREE;
4980   parser->qualifying_scope = NULL_TREE;
4981   parser->object_scope = NULL_TREE;
4982   *idk = CP_ID_KIND_NONE;
4983
4984   /* Enter the scope corresponding to the type of the object
4985      given by the POSTFIX_EXPRESSION.  */
4986   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4987     {
4988       scope = TREE_TYPE (postfix_expression);
4989       /* According to the standard, no expression should ever have
4990          reference type.  Unfortunately, we do not currently match
4991          the standard in this respect in that our internal representation
4992          of an expression may have reference type even when the standard
4993          says it does not.  Therefore, we have to manually obtain the
4994          underlying type here.  */
4995       scope = non_reference (scope);
4996       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4997       if (scope == unknown_type_node)
4998         {
4999           error ("%H%qE does not have class type", &location, postfix_expression);
5000           scope = NULL_TREE;
5001         }
5002       else
5003         scope = complete_type_or_else (scope, NULL_TREE);
5004       /* Let the name lookup machinery know that we are processing a
5005          class member access expression.  */
5006       parser->context->object_type = scope;
5007       /* If something went wrong, we want to be able to discern that case,
5008          as opposed to the case where there was no SCOPE due to the type
5009          of expression being dependent.  */
5010       if (!scope)
5011         scope = error_mark_node;
5012       /* If the SCOPE was erroneous, make the various semantic analysis
5013          functions exit quickly -- and without issuing additional error
5014          messages.  */
5015       if (scope == error_mark_node)
5016         postfix_expression = error_mark_node;
5017     }
5018
5019   /* Assume this expression is not a pseudo-destructor access.  */
5020   pseudo_destructor_p = false;
5021
5022   /* If the SCOPE is a scalar type, then, if this is a valid program,
5023      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5024      is type dependent, it can be pseudo-destructor-name or something else.
5025      Try to parse it as pseudo-destructor-name first.  */
5026   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5027     {
5028       tree s;
5029       tree type;
5030
5031       cp_parser_parse_tentatively (parser);
5032       /* Parse the pseudo-destructor-name.  */
5033       s = NULL_TREE;
5034       cp_parser_pseudo_destructor_name (parser, &s, &type);
5035       if (dependent_p
5036           && (cp_parser_error_occurred (parser)
5037               || TREE_CODE (type) != TYPE_DECL
5038               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5039         cp_parser_abort_tentative_parse (parser);
5040       else if (cp_parser_parse_definitely (parser))
5041         {
5042           pseudo_destructor_p = true;
5043           postfix_expression
5044             = finish_pseudo_destructor_expr (postfix_expression,
5045                                              s, TREE_TYPE (type));
5046         }
5047     }
5048
5049   if (!pseudo_destructor_p)
5050     {
5051       /* If the SCOPE is not a scalar type, we are looking at an
5052          ordinary class member access expression, rather than a
5053          pseudo-destructor-name.  */
5054       bool template_p;
5055       cp_token *token = cp_lexer_peek_token (parser->lexer);
5056       /* Parse the id-expression.  */
5057       name = (cp_parser_id_expression
5058               (parser,
5059                cp_parser_optional_template_keyword (parser),
5060                /*check_dependency_p=*/true,
5061                &template_p,
5062                /*declarator_p=*/false,
5063                /*optional_p=*/false));
5064       /* In general, build a SCOPE_REF if the member name is qualified.
5065          However, if the name was not dependent and has already been
5066          resolved; there is no need to build the SCOPE_REF.  For example;
5067
5068              struct X { void f(); };
5069              template <typename T> void f(T* t) { t->X::f(); }
5070
5071          Even though "t" is dependent, "X::f" is not and has been resolved
5072          to a BASELINK; there is no need to include scope information.  */
5073
5074       /* But we do need to remember that there was an explicit scope for
5075          virtual function calls.  */
5076       if (parser->scope)
5077         *idk = CP_ID_KIND_QUALIFIED;
5078
5079       /* If the name is a template-id that names a type, we will get a
5080          TYPE_DECL here.  That is invalid code.  */
5081       if (TREE_CODE (name) == TYPE_DECL)
5082         {
5083           error ("%Hinvalid use of %qD", &token->location, name);
5084           postfix_expression = error_mark_node;
5085         }
5086       else
5087         {
5088           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5089             {
5090               name = build_qualified_name (/*type=*/NULL_TREE,
5091                                            parser->scope,
5092                                            name,
5093                                            template_p);
5094               parser->scope = NULL_TREE;
5095               parser->qualifying_scope = NULL_TREE;
5096               parser->object_scope = NULL_TREE;
5097             }
5098           if (scope && name && BASELINK_P (name))
5099             adjust_result_of_qualified_name_lookup
5100               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5101           postfix_expression
5102             = finish_class_member_access_expr (postfix_expression, name,
5103                                                template_p, 
5104                                                tf_warning_or_error);
5105         }
5106     }
5107
5108   /* We no longer need to look up names in the scope of the object on
5109      the left-hand side of the `.' or `->' operator.  */
5110   parser->context->object_type = NULL_TREE;
5111
5112   /* Outside of offsetof, these operators may not appear in
5113      constant-expressions.  */
5114   if (!for_offsetof
5115       && (cp_parser_non_integral_constant_expression
5116           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5117     postfix_expression = error_mark_node;
5118
5119   return postfix_expression;
5120 }
5121
5122 /* Parse a parenthesized expression-list.
5123
5124    expression-list:
5125      assignment-expression
5126      expression-list, assignment-expression
5127
5128    attribute-list:
5129      expression-list
5130      identifier
5131      identifier, expression-list
5132
5133    CAST_P is true if this expression is the target of a cast.
5134
5135    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5136    argument pack.
5137
5138    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5139    representation of an assignment-expression.  Note that a TREE_LIST
5140    is returned even if there is only a single expression in the list.
5141    error_mark_node is returned if the ( and or ) are
5142    missing. NULL_TREE is returned on no expressions. The parentheses
5143    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5144    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5145    indicates whether or not all of the expressions in the list were
5146    constant.  */
5147
5148 static tree
5149 cp_parser_parenthesized_expression_list (cp_parser* parser,
5150                                          bool is_attribute_list,
5151                                          bool cast_p,
5152                                          bool allow_expansion_p,
5153                                          bool *non_constant_p)
5154 {
5155   tree expression_list = NULL_TREE;
5156   bool fold_expr_p = is_attribute_list;
5157   tree identifier = NULL_TREE;
5158   bool saved_greater_than_is_operator_p;
5159
5160   /* Assume all the expressions will be constant.  */
5161   if (non_constant_p)
5162     *non_constant_p = false;
5163
5164   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5165     return error_mark_node;
5166
5167   /* Within a parenthesized expression, a `>' token is always
5168      the greater-than operator.  */
5169   saved_greater_than_is_operator_p
5170     = parser->greater_than_is_operator_p;
5171   parser->greater_than_is_operator_p = true;
5172
5173   /* Consume expressions until there are no more.  */
5174   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5175     while (true)
5176       {
5177         tree expr;
5178
5179         /* At the beginning of attribute lists, check to see if the
5180            next token is an identifier.  */
5181         if (is_attribute_list
5182             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5183           {
5184             cp_token *token;
5185
5186             /* Consume the identifier.  */
5187             token = cp_lexer_consume_token (parser->lexer);
5188             /* Save the identifier.  */
5189             identifier = token->u.value;
5190           }
5191         else
5192           {
5193             bool expr_non_constant_p;
5194
5195             /* Parse the next assignment-expression.  */
5196             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5197               {
5198                 /* A braced-init-list.  */
5199                 maybe_warn_cpp0x ("extended initializer lists");
5200                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5201                 if (non_constant_p && expr_non_constant_p)
5202                   *non_constant_p = true;
5203               }
5204             else if (non_constant_p)
5205               {
5206                 expr = (cp_parser_constant_expression
5207                         (parser, /*allow_non_constant_p=*/true,
5208                          &expr_non_constant_p));
5209                 if (expr_non_constant_p)
5210                   *non_constant_p = true;
5211               }
5212             else
5213               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5214
5215             if (fold_expr_p)
5216               expr = fold_non_dependent_expr (expr);
5217
5218             /* If we have an ellipsis, then this is an expression
5219                expansion.  */
5220             if (allow_expansion_p
5221                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5222               {
5223                 /* Consume the `...'.  */
5224                 cp_lexer_consume_token (parser->lexer);
5225
5226                 /* Build the argument pack.  */
5227                 expr = make_pack_expansion (expr);
5228               }
5229
5230              /* Add it to the list.  We add error_mark_node
5231                 expressions to the list, so that we can still tell if
5232                 the correct form for a parenthesized expression-list
5233                 is found. That gives better errors.  */
5234             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5235
5236             if (expr == error_mark_node)
5237               goto skip_comma;
5238           }
5239
5240         /* After the first item, attribute lists look the same as
5241            expression lists.  */
5242         is_attribute_list = false;
5243
5244       get_comma:;
5245         /* If the next token isn't a `,', then we are done.  */
5246         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5247           break;
5248
5249         /* Otherwise, consume the `,' and keep going.  */
5250         cp_lexer_consume_token (parser->lexer);
5251       }
5252
5253   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5254     {
5255       int ending;
5256
5257     skip_comma:;
5258       /* We try and resync to an unnested comma, as that will give the
5259          user better diagnostics.  */
5260       ending = cp_parser_skip_to_closing_parenthesis (parser,
5261                                                       /*recovering=*/true,
5262                                                       /*or_comma=*/true,
5263                                                       /*consume_paren=*/true);
5264       if (ending < 0)
5265         goto get_comma;
5266       if (!ending)
5267         {
5268           parser->greater_than_is_operator_p
5269             = saved_greater_than_is_operator_p;
5270           return error_mark_node;
5271         }
5272     }
5273
5274   parser->greater_than_is_operator_p
5275     = saved_greater_than_is_operator_p;
5276
5277   /* We built up the list in reverse order so we must reverse it now.  */
5278   expression_list = nreverse (expression_list);
5279   if (identifier)
5280     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5281
5282   return expression_list;
5283 }
5284
5285 /* Parse a pseudo-destructor-name.
5286
5287    pseudo-destructor-name:
5288      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5289      :: [opt] nested-name-specifier template template-id :: ~ type-name
5290      :: [opt] nested-name-specifier [opt] ~ type-name
5291
5292    If either of the first two productions is used, sets *SCOPE to the
5293    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5294    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5295    or ERROR_MARK_NODE if the parse fails.  */
5296
5297 static void
5298 cp_parser_pseudo_destructor_name (cp_parser* parser,
5299                                   tree* scope,
5300                                   tree* type)
5301 {
5302   bool nested_name_specifier_p;
5303
5304   /* Assume that things will not work out.  */
5305   *type = error_mark_node;
5306
5307   /* Look for the optional `::' operator.  */
5308   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5309   /* Look for the optional nested-name-specifier.  */
5310   nested_name_specifier_p
5311     = (cp_parser_nested_name_specifier_opt (parser,
5312                                             /*typename_keyword_p=*/false,
5313                                             /*check_dependency_p=*/true,
5314                                             /*type_p=*/false,
5315                                             /*is_declaration=*/false)
5316        != NULL_TREE);
5317   /* Now, if we saw a nested-name-specifier, we might be doing the
5318      second production.  */
5319   if (nested_name_specifier_p
5320       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5321     {
5322       /* Consume the `template' keyword.  */
5323       cp_lexer_consume_token (parser->lexer);
5324       /* Parse the template-id.  */
5325       cp_parser_template_id (parser,
5326                              /*template_keyword_p=*/true,
5327                              /*check_dependency_p=*/false,
5328                              /*is_declaration=*/true);
5329       /* Look for the `::' token.  */
5330       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5331     }
5332   /* If the next token is not a `~', then there might be some
5333      additional qualification.  */
5334   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5335     {
5336       /* At this point, we're looking for "type-name :: ~".  The type-name
5337          must not be a class-name, since this is a pseudo-destructor.  So,
5338          it must be either an enum-name, or a typedef-name -- both of which
5339          are just identifiers.  So, we peek ahead to check that the "::"
5340          and "~" tokens are present; if they are not, then we can avoid
5341          calling type_name.  */
5342       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5343           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5344           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5345         {
5346           cp_parser_error (parser, "non-scalar type");
5347           return;
5348         }
5349
5350       /* Look for the type-name.  */
5351       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5352       if (*scope == error_mark_node)
5353         return;
5354
5355       /* Look for the `::' token.  */
5356       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5357     }
5358   else
5359     *scope = NULL_TREE;
5360
5361   /* Look for the `~'.  */
5362   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5363   /* Look for the type-name again.  We are not responsible for
5364      checking that it matches the first type-name.  */
5365   *type = cp_parser_nonclass_name (parser);
5366 }
5367
5368 /* Parse a unary-expression.
5369
5370    unary-expression:
5371      postfix-expression
5372      ++ cast-expression
5373      -- cast-expression
5374      unary-operator cast-expression
5375      sizeof unary-expression
5376      sizeof ( type-id )
5377      new-expression
5378      delete-expression
5379
5380    GNU Extensions:
5381
5382    unary-expression:
5383      __extension__ cast-expression
5384      __alignof__ unary-expression
5385      __alignof__ ( type-id )
5386      __real__ cast-expression
5387      __imag__ cast-expression
5388      && identifier
5389
5390    ADDRESS_P is true iff the unary-expression is appearing as the
5391    operand of the `&' operator.   CAST_P is true if this expression is
5392    the target of a cast.
5393
5394    Returns a representation of the expression.  */
5395
5396 static tree
5397 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5398                             cp_id_kind * pidk)
5399 {
5400   cp_token *token;
5401   enum tree_code unary_operator;
5402
5403   /* Peek at the next token.  */
5404   token = cp_lexer_peek_token (parser->lexer);
5405   /* Some keywords give away the kind of expression.  */
5406   if (token->type == CPP_KEYWORD)
5407     {
5408       enum rid keyword = token->keyword;
5409
5410       switch (keyword)
5411         {
5412         case RID_ALIGNOF:
5413         case RID_SIZEOF:
5414           {
5415             tree operand;
5416             enum tree_code op;
5417
5418             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5419             /* Consume the token.  */
5420             cp_lexer_consume_token (parser->lexer);
5421             /* Parse the operand.  */
5422             operand = cp_parser_sizeof_operand (parser, keyword);
5423
5424             if (TYPE_P (operand))
5425               return cxx_sizeof_or_alignof_type (operand, op, true);
5426             else
5427               return cxx_sizeof_or_alignof_expr (operand, op, true);
5428           }
5429
5430         case RID_NEW:
5431           return cp_parser_new_expression (parser);
5432
5433         case RID_DELETE:
5434           return cp_parser_delete_expression (parser);
5435
5436         case RID_EXTENSION:
5437           {
5438             /* The saved value of the PEDANTIC flag.  */
5439             int saved_pedantic;
5440             tree expr;
5441
5442             /* Save away the PEDANTIC flag.  */
5443             cp_parser_extension_opt (parser, &saved_pedantic);
5444             /* Parse the cast-expression.  */
5445             expr = cp_parser_simple_cast_expression (parser);
5446             /* Restore the PEDANTIC flag.  */
5447             pedantic = saved_pedantic;
5448
5449             return expr;
5450           }
5451
5452         case RID_REALPART:
5453         case RID_IMAGPART:
5454           {
5455             tree expression;
5456
5457             /* Consume the `__real__' or `__imag__' token.  */
5458             cp_lexer_consume_token (parser->lexer);
5459             /* Parse the cast-expression.  */
5460             expression = cp_parser_simple_cast_expression (parser);
5461             /* Create the complete representation.  */
5462             return build_x_unary_op ((keyword == RID_REALPART
5463                                       ? REALPART_EXPR : IMAGPART_EXPR),
5464                                      expression,
5465                                      tf_warning_or_error);
5466           }
5467           break;
5468
5469         default:
5470           break;
5471         }
5472     }
5473
5474   /* Look for the `:: new' and `:: delete', which also signal the
5475      beginning of a new-expression, or delete-expression,
5476      respectively.  If the next token is `::', then it might be one of
5477      these.  */
5478   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5479     {
5480       enum rid keyword;
5481
5482       /* See if the token after the `::' is one of the keywords in
5483          which we're interested.  */
5484       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5485       /* If it's `new', we have a new-expression.  */
5486       if (keyword == RID_NEW)
5487         return cp_parser_new_expression (parser);
5488       /* Similarly, for `delete'.  */
5489       else if (keyword == RID_DELETE)
5490         return cp_parser_delete_expression (parser);
5491     }
5492
5493   /* Look for a unary operator.  */
5494   unary_operator = cp_parser_unary_operator (token);
5495   /* The `++' and `--' operators can be handled similarly, even though
5496      they are not technically unary-operators in the grammar.  */
5497   if (unary_operator == ERROR_MARK)
5498     {
5499       if (token->type == CPP_PLUS_PLUS)
5500         unary_operator = PREINCREMENT_EXPR;
5501       else if (token->type == CPP_MINUS_MINUS)
5502         unary_operator = PREDECREMENT_EXPR;
5503       /* Handle the GNU address-of-label extension.  */
5504       else if (cp_parser_allow_gnu_extensions_p (parser)
5505                && token->type == CPP_AND_AND)
5506         {
5507           tree identifier;
5508           tree expression;
5509           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5510
5511           /* Consume the '&&' token.  */
5512           cp_lexer_consume_token (parser->lexer);
5513           /* Look for the identifier.  */
5514           identifier = cp_parser_identifier (parser);
5515           /* Create an expression representing the address.  */
5516           expression = finish_label_address_expr (identifier, loc);
5517           if (cp_parser_non_integral_constant_expression (parser,
5518                                                 "the address of a label"))
5519             expression = error_mark_node;
5520           return expression;
5521         }
5522     }
5523   if (unary_operator != ERROR_MARK)
5524     {
5525       tree cast_expression;
5526       tree expression = error_mark_node;
5527       const char *non_constant_p = NULL;
5528
5529       /* Consume the operator token.  */
5530       token = cp_lexer_consume_token (parser->lexer);
5531       /* Parse the cast-expression.  */
5532       cast_expression
5533         = cp_parser_cast_expression (parser,
5534                                      unary_operator == ADDR_EXPR,
5535                                      /*cast_p=*/false, pidk);
5536       /* Now, build an appropriate representation.  */
5537       switch (unary_operator)
5538         {
5539         case INDIRECT_REF:
5540           non_constant_p = "%<*%>";
5541           expression = build_x_indirect_ref (cast_expression, "unary *",
5542                                              tf_warning_or_error);
5543           break;
5544
5545         case ADDR_EXPR:
5546           non_constant_p = "%<&%>";
5547           /* Fall through.  */
5548         case BIT_NOT_EXPR:
5549           expression = build_x_unary_op (unary_operator, cast_expression,
5550                                          tf_warning_or_error);
5551           break;
5552
5553         case PREINCREMENT_EXPR:
5554         case PREDECREMENT_EXPR:
5555           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5556                             ? "%<++%>" : "%<--%>");
5557           /* Fall through.  */
5558         case UNARY_PLUS_EXPR:
5559         case NEGATE_EXPR:
5560         case TRUTH_NOT_EXPR:
5561           expression = finish_unary_op_expr (unary_operator, cast_expression);
5562           break;
5563
5564         default:
5565           gcc_unreachable ();
5566         }
5567
5568       if (non_constant_p
5569           && cp_parser_non_integral_constant_expression (parser,
5570                                                          non_constant_p))
5571         expression = error_mark_node;
5572
5573       return expression;
5574     }
5575
5576   return cp_parser_postfix_expression (parser, address_p, cast_p,
5577                                        /*member_access_only_p=*/false,
5578                                        pidk);
5579 }
5580
5581 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5582    unary-operator, the corresponding tree code is returned.  */
5583
5584 static enum tree_code
5585 cp_parser_unary_operator (cp_token* token)
5586 {
5587   switch (token->type)
5588     {
5589     case CPP_MULT:
5590       return INDIRECT_REF;
5591
5592     case CPP_AND:
5593       return ADDR_EXPR;
5594
5595     case CPP_PLUS:
5596       return UNARY_PLUS_EXPR;
5597
5598     case CPP_MINUS:
5599       return NEGATE_EXPR;
5600
5601     case CPP_NOT:
5602       return TRUTH_NOT_EXPR;
5603
5604     case CPP_COMPL:
5605       return BIT_NOT_EXPR;
5606
5607     default:
5608       return ERROR_MARK;
5609     }
5610 }
5611
5612 /* Parse a new-expression.
5613
5614    new-expression:
5615      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5616      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5617
5618    Returns a representation of the expression.  */
5619
5620 static tree
5621 cp_parser_new_expression (cp_parser* parser)
5622 {
5623   bool global_scope_p;
5624   tree placement;
5625   tree type;
5626   tree initializer;
5627   tree nelts;
5628
5629   /* Look for the optional `::' operator.  */
5630   global_scope_p
5631     = (cp_parser_global_scope_opt (parser,
5632                                    /*current_scope_valid_p=*/false)
5633        != NULL_TREE);
5634   /* Look for the `new' operator.  */
5635   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5636   /* There's no easy way to tell a new-placement from the
5637      `( type-id )' construct.  */
5638   cp_parser_parse_tentatively (parser);
5639   /* Look for a new-placement.  */
5640   placement = cp_parser_new_placement (parser);
5641   /* If that didn't work out, there's no new-placement.  */
5642   if (!cp_parser_parse_definitely (parser))
5643     placement = NULL_TREE;
5644
5645   /* If the next token is a `(', then we have a parenthesized
5646      type-id.  */
5647   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5648     {
5649       cp_token *token;
5650       /* Consume the `('.  */
5651       cp_lexer_consume_token (parser->lexer);
5652       /* Parse the type-id.  */
5653       type = cp_parser_type_id (parser);
5654       /* Look for the closing `)'.  */
5655       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5656       token = cp_lexer_peek_token (parser->lexer);
5657       /* There should not be a direct-new-declarator in this production,
5658          but GCC used to allowed this, so we check and emit a sensible error
5659          message for this case.  */
5660       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5661         {
5662           error ("%Harray bound forbidden after parenthesized type-id",
5663                  &token->location);
5664           inform (token->location, 
5665                   "try removing the parentheses around the type-id");
5666           cp_parser_direct_new_declarator (parser);
5667         }
5668       nelts = NULL_TREE;
5669     }
5670   /* Otherwise, there must be a new-type-id.  */
5671   else
5672     type = cp_parser_new_type_id (parser, &nelts);
5673
5674   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5675   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5676       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5677     initializer = cp_parser_new_initializer (parser);
5678   else
5679     initializer = NULL_TREE;
5680
5681   /* A new-expression may not appear in an integral constant
5682      expression.  */
5683   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5684     return error_mark_node;
5685
5686   /* Create a representation of the new-expression.  */
5687   return build_new (placement, type, nelts, initializer, global_scope_p,
5688                     tf_warning_or_error);
5689 }
5690
5691 /* Parse a new-placement.
5692
5693    new-placement:
5694      ( expression-list )
5695
5696    Returns the same representation as for an expression-list.  */
5697
5698 static tree
5699 cp_parser_new_placement (cp_parser* parser)
5700 {
5701   tree expression_list;
5702
5703   /* Parse the expression-list.  */
5704   expression_list = (cp_parser_parenthesized_expression_list
5705                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5706                       /*non_constant_p=*/NULL));
5707
5708   return expression_list;
5709 }
5710
5711 /* Parse a new-type-id.
5712
5713    new-type-id:
5714      type-specifier-seq new-declarator [opt]
5715
5716    Returns the TYPE allocated.  If the new-type-id indicates an array
5717    type, *NELTS is set to the number of elements in the last array
5718    bound; the TYPE will not include the last array bound.  */
5719
5720 static tree
5721 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5722 {
5723   cp_decl_specifier_seq type_specifier_seq;
5724   cp_declarator *new_declarator;
5725   cp_declarator *declarator;
5726   cp_declarator *outer_declarator;
5727   const char *saved_message;
5728   tree type;
5729
5730   /* The type-specifier sequence must not contain type definitions.
5731      (It cannot contain declarations of new types either, but if they
5732      are not definitions we will catch that because they are not
5733      complete.)  */
5734   saved_message = parser->type_definition_forbidden_message;
5735   parser->type_definition_forbidden_message
5736     = "types may not be defined in a new-type-id";
5737   /* Parse the type-specifier-seq.  */
5738   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5739                                 &type_specifier_seq);
5740   /* Restore the old message.  */
5741   parser->type_definition_forbidden_message = saved_message;
5742   /* Parse the new-declarator.  */
5743   new_declarator = cp_parser_new_declarator_opt (parser);
5744
5745   /* Determine the number of elements in the last array dimension, if
5746      any.  */
5747   *nelts = NULL_TREE;
5748   /* Skip down to the last array dimension.  */
5749   declarator = new_declarator;
5750   outer_declarator = NULL;
5751   while (declarator && (declarator->kind == cdk_pointer
5752                         || declarator->kind == cdk_ptrmem))
5753     {
5754       outer_declarator = declarator;
5755       declarator = declarator->declarator;
5756     }
5757   while (declarator
5758          && declarator->kind == cdk_array
5759          && declarator->declarator
5760          && declarator->declarator->kind == cdk_array)
5761     {
5762       outer_declarator = declarator;
5763       declarator = declarator->declarator;
5764     }
5765
5766   if (declarator && declarator->kind == cdk_array)
5767     {
5768       *nelts = declarator->u.array.bounds;
5769       if (*nelts == error_mark_node)
5770         *nelts = integer_one_node;
5771
5772       if (outer_declarator)
5773         outer_declarator->declarator = declarator->declarator;
5774       else
5775         new_declarator = NULL;
5776     }
5777
5778   type = groktypename (&type_specifier_seq, new_declarator);
5779   return type;
5780 }
5781
5782 /* Parse an (optional) new-declarator.
5783
5784    new-declarator:
5785      ptr-operator new-declarator [opt]
5786      direct-new-declarator
5787
5788    Returns the declarator.  */
5789
5790 static cp_declarator *
5791 cp_parser_new_declarator_opt (cp_parser* parser)
5792 {
5793   enum tree_code code;
5794   tree type;
5795   cp_cv_quals cv_quals;
5796
5797   /* We don't know if there's a ptr-operator next, or not.  */
5798   cp_parser_parse_tentatively (parser);
5799   /* Look for a ptr-operator.  */
5800   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5801   /* If that worked, look for more new-declarators.  */
5802   if (cp_parser_parse_definitely (parser))
5803     {
5804       cp_declarator *declarator;
5805
5806       /* Parse another optional declarator.  */
5807       declarator = cp_parser_new_declarator_opt (parser);
5808
5809       return cp_parser_make_indirect_declarator
5810         (code, type, cv_quals, declarator);
5811     }
5812
5813   /* If the next token is a `[', there is a direct-new-declarator.  */
5814   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5815     return cp_parser_direct_new_declarator (parser);
5816
5817   return NULL;
5818 }
5819
5820 /* Parse a direct-new-declarator.
5821
5822    direct-new-declarator:
5823      [ expression ]
5824      direct-new-declarator [constant-expression]
5825
5826    */
5827
5828 static cp_declarator *
5829 cp_parser_direct_new_declarator (cp_parser* parser)
5830 {
5831   cp_declarator *declarator = NULL;
5832
5833   while (true)
5834     {
5835       tree expression;
5836
5837       /* Look for the opening `['.  */
5838       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5839       /* The first expression is not required to be constant.  */
5840       if (!declarator)
5841         {
5842           cp_token *token = cp_lexer_peek_token (parser->lexer);
5843           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5844           /* The standard requires that the expression have integral
5845              type.  DR 74 adds enumeration types.  We believe that the
5846              real intent is that these expressions be handled like the
5847              expression in a `switch' condition, which also allows
5848              classes with a single conversion to integral or
5849              enumeration type.  */
5850           if (!processing_template_decl)
5851             {
5852               expression
5853                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5854                                               expression,
5855                                               /*complain=*/true);
5856               if (!expression)
5857                 {
5858                   error ("%Hexpression in new-declarator must have integral "
5859                          "or enumeration type", &token->location);
5860                   expression = error_mark_node;
5861                 }
5862             }
5863         }
5864       /* But all the other expressions must be.  */
5865       else
5866         expression
5867           = cp_parser_constant_expression (parser,
5868                                            /*allow_non_constant=*/false,
5869                                            NULL);
5870       /* Look for the closing `]'.  */
5871       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5872
5873       /* Add this bound to the declarator.  */
5874       declarator = make_array_declarator (declarator, expression);
5875
5876       /* If the next token is not a `[', then there are no more
5877          bounds.  */
5878       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5879         break;
5880     }
5881
5882   return declarator;
5883 }
5884
5885 /* Parse a new-initializer.
5886
5887    new-initializer:
5888      ( expression-list [opt] )
5889      braced-init-list
5890
5891    Returns a representation of the expression-list.  If there is no
5892    expression-list, VOID_ZERO_NODE is returned.  */
5893
5894 static tree
5895 cp_parser_new_initializer (cp_parser* parser)
5896 {
5897   tree expression_list;
5898
5899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5900     {
5901       bool expr_non_constant_p;
5902       maybe_warn_cpp0x ("extended initializer lists");
5903       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5904       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5905       expression_list = build_tree_list (NULL_TREE, expression_list);
5906     }
5907   else
5908     expression_list = (cp_parser_parenthesized_expression_list
5909                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5910                         /*non_constant_p=*/NULL));
5911   if (!expression_list)
5912     expression_list = void_zero_node;
5913
5914   return expression_list;
5915 }
5916
5917 /* Parse a delete-expression.
5918
5919    delete-expression:
5920      :: [opt] delete cast-expression
5921      :: [opt] delete [ ] cast-expression
5922
5923    Returns a representation of the expression.  */
5924
5925 static tree
5926 cp_parser_delete_expression (cp_parser* parser)
5927 {
5928   bool global_scope_p;
5929   bool array_p;
5930   tree expression;
5931
5932   /* Look for the optional `::' operator.  */
5933   global_scope_p
5934     = (cp_parser_global_scope_opt (parser,
5935                                    /*current_scope_valid_p=*/false)
5936        != NULL_TREE);
5937   /* Look for the `delete' keyword.  */
5938   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5939   /* See if the array syntax is in use.  */
5940   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5941     {
5942       /* Consume the `[' token.  */
5943       cp_lexer_consume_token (parser->lexer);
5944       /* Look for the `]' token.  */
5945       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5946       /* Remember that this is the `[]' construct.  */
5947       array_p = true;
5948     }
5949   else
5950     array_p = false;
5951
5952   /* Parse the cast-expression.  */
5953   expression = cp_parser_simple_cast_expression (parser);
5954
5955   /* A delete-expression may not appear in an integral constant
5956      expression.  */
5957   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5958     return error_mark_node;
5959
5960   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5961 }
5962
5963 /* Returns true if TOKEN may start a cast-expression and false
5964    otherwise.  */
5965
5966 static bool
5967 cp_parser_token_starts_cast_expression (cp_token *token)
5968 {
5969   switch (token->type)
5970     {
5971     case CPP_COMMA:
5972     case CPP_SEMICOLON:
5973     case CPP_QUERY:
5974     case CPP_COLON:
5975     case CPP_CLOSE_SQUARE:
5976     case CPP_CLOSE_PAREN:
5977     case CPP_CLOSE_BRACE:
5978     case CPP_DOT:
5979     case CPP_DOT_STAR:
5980     case CPP_DEREF:
5981     case CPP_DEREF_STAR:
5982     case CPP_DIV:
5983     case CPP_MOD:
5984     case CPP_LSHIFT:
5985     case CPP_RSHIFT:
5986     case CPP_LESS:
5987     case CPP_GREATER:
5988     case CPP_LESS_EQ:
5989     case CPP_GREATER_EQ:
5990     case CPP_EQ_EQ:
5991     case CPP_NOT_EQ:
5992     case CPP_EQ:
5993     case CPP_MULT_EQ:
5994     case CPP_DIV_EQ:
5995     case CPP_MOD_EQ:
5996     case CPP_PLUS_EQ:
5997     case CPP_MINUS_EQ:
5998     case CPP_RSHIFT_EQ:
5999     case CPP_LSHIFT_EQ:
6000     case CPP_AND_EQ:
6001     case CPP_XOR_EQ:
6002     case CPP_OR_EQ:
6003     case CPP_XOR:
6004     case CPP_OR:
6005     case CPP_OR_OR:
6006     case CPP_EOF:
6007       return false;
6008
6009       /* '[' may start a primary-expression in obj-c++.  */
6010     case CPP_OPEN_SQUARE:
6011       return c_dialect_objc ();
6012
6013     default:
6014       return true;
6015     }
6016 }
6017
6018 /* Parse a cast-expression.
6019
6020    cast-expression:
6021      unary-expression
6022      ( type-id ) cast-expression
6023
6024    ADDRESS_P is true iff the unary-expression is appearing as the
6025    operand of the `&' operator.   CAST_P is true if this expression is
6026    the target of a cast.
6027
6028    Returns a representation of the expression.  */
6029
6030 static tree
6031 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6032                            cp_id_kind * pidk)
6033 {
6034   /* If it's a `(', then we might be looking at a cast.  */
6035   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6036     {
6037       tree type = NULL_TREE;
6038       tree expr = NULL_TREE;
6039       bool compound_literal_p;
6040       const char *saved_message;
6041
6042       /* There's no way to know yet whether or not this is a cast.
6043          For example, `(int (3))' is a unary-expression, while `(int)
6044          3' is a cast.  So, we resort to parsing tentatively.  */
6045       cp_parser_parse_tentatively (parser);
6046       /* Types may not be defined in a cast.  */
6047       saved_message = parser->type_definition_forbidden_message;
6048       parser->type_definition_forbidden_message
6049         = "types may not be defined in casts";
6050       /* Consume the `('.  */
6051       cp_lexer_consume_token (parser->lexer);
6052       /* A very tricky bit is that `(struct S) { 3 }' is a
6053          compound-literal (which we permit in C++ as an extension).
6054          But, that construct is not a cast-expression -- it is a
6055          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6056          is legal; if the compound-literal were a cast-expression,
6057          you'd need an extra set of parentheses.)  But, if we parse
6058          the type-id, and it happens to be a class-specifier, then we
6059          will commit to the parse at that point, because we cannot
6060          undo the action that is done when creating a new class.  So,
6061          then we cannot back up and do a postfix-expression.
6062
6063          Therefore, we scan ahead to the closing `)', and check to see
6064          if the token after the `)' is a `{'.  If so, we are not
6065          looking at a cast-expression.
6066
6067          Save tokens so that we can put them back.  */
6068       cp_lexer_save_tokens (parser->lexer);
6069       /* Skip tokens until the next token is a closing parenthesis.
6070          If we find the closing `)', and the next token is a `{', then
6071          we are looking at a compound-literal.  */
6072       compound_literal_p
6073         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6074                                                   /*consume_paren=*/true)
6075            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6076       /* Roll back the tokens we skipped.  */
6077       cp_lexer_rollback_tokens (parser->lexer);
6078       /* If we were looking at a compound-literal, simulate an error
6079          so that the call to cp_parser_parse_definitely below will
6080          fail.  */
6081       if (compound_literal_p)
6082         cp_parser_simulate_error (parser);
6083       else
6084         {
6085           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6086           parser->in_type_id_in_expr_p = true;
6087           /* Look for the type-id.  */
6088           type = cp_parser_type_id (parser);
6089           /* Look for the closing `)'.  */
6090           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6091           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6092         }
6093
6094       /* Restore the saved message.  */
6095       parser->type_definition_forbidden_message = saved_message;
6096
6097       /* At this point this can only be either a cast or a
6098          parenthesized ctor such as `(T ())' that looks like a cast to
6099          function returning T.  */
6100       if (!cp_parser_error_occurred (parser)
6101           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6102                                                      (parser->lexer)))
6103         {
6104           cp_parser_parse_definitely (parser);
6105           expr = cp_parser_cast_expression (parser,
6106                                             /*address_p=*/false,
6107                                             /*cast_p=*/true, pidk);
6108
6109           /* Warn about old-style casts, if so requested.  */
6110           if (warn_old_style_cast
6111               && !in_system_header
6112               && !VOID_TYPE_P (type)
6113               && current_lang_name != lang_name_c)
6114             warning (OPT_Wold_style_cast, "use of old-style cast");
6115
6116           /* Only type conversions to integral or enumeration types
6117              can be used in constant-expressions.  */
6118           if (!cast_valid_in_integral_constant_expression_p (type)
6119               && (cp_parser_non_integral_constant_expression
6120                   (parser,
6121                    "a cast to a type other than an integral or "
6122                    "enumeration type")))
6123             return error_mark_node;
6124
6125           /* Perform the cast.  */
6126           expr = build_c_cast (type, expr);
6127           return expr;
6128         }
6129       else 
6130         cp_parser_abort_tentative_parse (parser);
6131     }
6132
6133   /* If we get here, then it's not a cast, so it must be a
6134      unary-expression.  */
6135   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6136 }
6137
6138 /* Parse a binary expression of the general form:
6139
6140    pm-expression:
6141      cast-expression
6142      pm-expression .* cast-expression
6143      pm-expression ->* cast-expression
6144
6145    multiplicative-expression:
6146      pm-expression
6147      multiplicative-expression * pm-expression
6148      multiplicative-expression / pm-expression
6149      multiplicative-expression % pm-expression
6150
6151    additive-expression:
6152      multiplicative-expression
6153      additive-expression + multiplicative-expression
6154      additive-expression - multiplicative-expression
6155
6156    shift-expression:
6157      additive-expression
6158      shift-expression << additive-expression
6159      shift-expression >> additive-expression
6160
6161    relational-expression:
6162      shift-expression
6163      relational-expression < shift-expression
6164      relational-expression > shift-expression
6165      relational-expression <= shift-expression
6166      relational-expression >= shift-expression
6167
6168   GNU Extension:
6169
6170    relational-expression:
6171      relational-expression <? shift-expression
6172      relational-expression >? shift-expression
6173
6174    equality-expression:
6175      relational-expression
6176      equality-expression == relational-expression
6177      equality-expression != relational-expression
6178
6179    and-expression:
6180      equality-expression
6181      and-expression & equality-expression
6182
6183    exclusive-or-expression:
6184      and-expression
6185      exclusive-or-expression ^ and-expression
6186
6187    inclusive-or-expression:
6188      exclusive-or-expression
6189      inclusive-or-expression | exclusive-or-expression
6190
6191    logical-and-expression:
6192      inclusive-or-expression
6193      logical-and-expression && inclusive-or-expression
6194
6195    logical-or-expression:
6196      logical-and-expression
6197      logical-or-expression || logical-and-expression
6198
6199    All these are implemented with a single function like:
6200
6201    binary-expression:
6202      simple-cast-expression
6203      binary-expression <token> binary-expression
6204
6205    CAST_P is true if this expression is the target of a cast.
6206
6207    The binops_by_token map is used to get the tree codes for each <token> type.
6208    binary-expressions are associated according to a precedence table.  */
6209
6210 #define TOKEN_PRECEDENCE(token)                              \
6211 (((token->type == CPP_GREATER                                \
6212    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6213   && !parser->greater_than_is_operator_p)                    \
6214  ? PREC_NOT_OPERATOR                                         \
6215  : binops_by_token[token->type].prec)
6216
6217 static tree
6218 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6219                              enum cp_parser_prec prec,
6220                              cp_id_kind * pidk)
6221 {
6222   cp_parser_expression_stack stack;
6223   cp_parser_expression_stack_entry *sp = &stack[0];
6224   tree lhs, rhs;
6225   cp_token *token;
6226   enum tree_code tree_type, lhs_type, rhs_type;
6227   enum cp_parser_prec new_prec, lookahead_prec;
6228   bool overloaded_p;
6229
6230   /* Parse the first expression.  */
6231   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6232   lhs_type = ERROR_MARK;
6233
6234   for (;;)
6235     {
6236       /* Get an operator token.  */
6237       token = cp_lexer_peek_token (parser->lexer);
6238
6239       if (warn_cxx0x_compat
6240           && token->type == CPP_RSHIFT
6241           && !parser->greater_than_is_operator_p)
6242         {
6243           warning (OPT_Wc__0x_compat, 
6244                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6245                    &token->location);
6246           warning (OPT_Wc__0x_compat, 
6247                    "suggest parentheses around %<>>%> expression");
6248         }
6249
6250       new_prec = TOKEN_PRECEDENCE (token);
6251
6252       /* Popping an entry off the stack means we completed a subexpression:
6253          - either we found a token which is not an operator (`>' where it is not
6254            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6255            will happen repeatedly;
6256          - or, we found an operator which has lower priority.  This is the case
6257            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6258            parsing `3 * 4'.  */
6259       if (new_prec <= prec)
6260         {
6261           if (sp == stack)
6262             break;
6263           else
6264             goto pop;
6265         }
6266
6267      get_rhs:
6268       tree_type = binops_by_token[token->type].tree_type;
6269
6270       /* We used the operator token.  */
6271       cp_lexer_consume_token (parser->lexer);
6272
6273       /* Extract another operand.  It may be the RHS of this expression
6274          or the LHS of a new, higher priority expression.  */
6275       rhs = cp_parser_simple_cast_expression (parser);
6276       rhs_type = ERROR_MARK;
6277
6278       /* Get another operator token.  Look up its precedence to avoid
6279          building a useless (immediately popped) stack entry for common
6280          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6281       token = cp_lexer_peek_token (parser->lexer);
6282       lookahead_prec = TOKEN_PRECEDENCE (token);
6283       if (lookahead_prec > new_prec)
6284         {
6285           /* ... and prepare to parse the RHS of the new, higher priority
6286              expression.  Since precedence levels on the stack are
6287              monotonically increasing, we do not have to care about
6288              stack overflows.  */
6289           sp->prec = prec;
6290           sp->tree_type = tree_type;
6291           sp->lhs = lhs;
6292           sp->lhs_type = lhs_type;
6293           sp++;
6294           lhs = rhs;
6295           lhs_type = rhs_type;
6296           prec = new_prec;
6297           new_prec = lookahead_prec;
6298           goto get_rhs;
6299
6300          pop:
6301           /* If the stack is not empty, we have parsed into LHS the right side
6302              (`4' in the example above) of an expression we had suspended.
6303              We can use the information on the stack to recover the LHS (`3')
6304              from the stack together with the tree code (`MULT_EXPR'), and
6305              the precedence of the higher level subexpression
6306              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6307              which will be used to actually build the additive expression.  */
6308           --sp;
6309           prec = sp->prec;
6310           tree_type = sp->tree_type;
6311           rhs = lhs;
6312           rhs_type = lhs_type;
6313           lhs = sp->lhs;
6314           lhs_type = sp->lhs_type;
6315         }
6316
6317       overloaded_p = false;
6318       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6319          ERROR_MARK for everything that is not a binary expression.
6320          This makes warn_about_parentheses miss some warnings that
6321          involve unary operators.  For unary expressions we should
6322          pass the correct tree_code unless the unary expression was
6323          surrounded by parentheses.
6324       */
6325       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6326                                &overloaded_p, tf_warning_or_error);
6327       lhs_type = tree_type;
6328
6329       /* If the binary operator required the use of an overloaded operator,
6330          then this expression cannot be an integral constant-expression.
6331          An overloaded operator can be used even if both operands are
6332          otherwise permissible in an integral constant-expression if at
6333          least one of the operands is of enumeration type.  */
6334
6335       if (overloaded_p
6336           && (cp_parser_non_integral_constant_expression
6337               (parser, "calls to overloaded operators")))
6338         return error_mark_node;
6339     }
6340
6341   return lhs;
6342 }
6343
6344
6345 /* Parse the `? expression : assignment-expression' part of a
6346    conditional-expression.  The LOGICAL_OR_EXPR is the
6347    logical-or-expression that started the conditional-expression.
6348    Returns a representation of the entire conditional-expression.
6349
6350    This routine is used by cp_parser_assignment_expression.
6351
6352      ? expression : assignment-expression
6353
6354    GNU Extensions:
6355
6356      ? : assignment-expression */
6357
6358 static tree
6359 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6360 {
6361   tree expr;
6362   tree assignment_expr;
6363
6364   /* Consume the `?' token.  */
6365   cp_lexer_consume_token (parser->lexer);
6366   if (cp_parser_allow_gnu_extensions_p (parser)
6367       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6368     /* Implicit true clause.  */
6369     expr = NULL_TREE;
6370   else
6371     /* Parse the expression.  */
6372     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6373
6374   /* The next token should be a `:'.  */
6375   cp_parser_require (parser, CPP_COLON, "%<:%>");
6376   /* Parse the assignment-expression.  */
6377   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6378
6379   /* Build the conditional-expression.  */
6380   return build_x_conditional_expr (logical_or_expr,
6381                                    expr,
6382                                    assignment_expr,
6383                                    tf_warning_or_error);
6384 }
6385
6386 /* Parse an assignment-expression.
6387
6388    assignment-expression:
6389      conditional-expression
6390      logical-or-expression assignment-operator assignment_expression
6391      throw-expression
6392
6393    CAST_P is true if this expression is the target of a cast.
6394
6395    Returns a representation for the expression.  */
6396
6397 static tree
6398 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6399                                  cp_id_kind * pidk)
6400 {
6401   tree expr;
6402
6403   /* If the next token is the `throw' keyword, then we're looking at
6404      a throw-expression.  */
6405   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6406     expr = cp_parser_throw_expression (parser);
6407   /* Otherwise, it must be that we are looking at a
6408      logical-or-expression.  */
6409   else
6410     {
6411       /* Parse the binary expressions (logical-or-expression).  */
6412       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR, pidk);
6413       /* If the next token is a `?' then we're actually looking at a
6414          conditional-expression.  */
6415       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6416         return cp_parser_question_colon_clause (parser, expr);
6417       else
6418         {
6419           enum tree_code assignment_operator;
6420
6421           /* If it's an assignment-operator, we're using the second
6422              production.  */
6423           assignment_operator
6424             = cp_parser_assignment_operator_opt (parser);
6425           if (assignment_operator != ERROR_MARK)
6426             {
6427               bool non_constant_p;
6428
6429               /* Parse the right-hand side of the assignment.  */
6430               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6431
6432               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6433                 maybe_warn_cpp0x ("extended initializer lists");
6434
6435               /* An assignment may not appear in a
6436                  constant-expression.  */
6437               if (cp_parser_non_integral_constant_expression (parser,
6438                                                               "an assignment"))
6439                 return error_mark_node;
6440               /* Build the assignment expression.  */
6441               expr = build_x_modify_expr (expr,
6442                                           assignment_operator,
6443                                           rhs,
6444                                           tf_warning_or_error);
6445             }
6446         }
6447     }
6448
6449   return expr;
6450 }
6451
6452 /* Parse an (optional) assignment-operator.
6453
6454    assignment-operator: one of
6455      = *= /= %= += -= >>= <<= &= ^= |=
6456
6457    GNU Extension:
6458
6459    assignment-operator: one of
6460      <?= >?=
6461
6462    If the next token is an assignment operator, the corresponding tree
6463    code is returned, and the token is consumed.  For example, for
6464    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6465    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6466    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6467    operator, ERROR_MARK is returned.  */
6468
6469 static enum tree_code
6470 cp_parser_assignment_operator_opt (cp_parser* parser)
6471 {
6472   enum tree_code op;
6473   cp_token *token;
6474
6475   /* Peek at the next token.  */
6476   token = cp_lexer_peek_token (parser->lexer);
6477
6478   switch (token->type)
6479     {
6480     case CPP_EQ:
6481       op = NOP_EXPR;
6482       break;
6483
6484     case CPP_MULT_EQ:
6485       op = MULT_EXPR;
6486       break;
6487
6488     case CPP_DIV_EQ:
6489       op = TRUNC_DIV_EXPR;
6490       break;
6491
6492     case CPP_MOD_EQ:
6493       op = TRUNC_MOD_EXPR;
6494       break;
6495
6496     case CPP_PLUS_EQ:
6497       op = PLUS_EXPR;
6498       break;
6499
6500     case CPP_MINUS_EQ:
6501       op = MINUS_EXPR;
6502       break;
6503
6504     case CPP_RSHIFT_EQ:
6505       op = RSHIFT_EXPR;
6506       break;
6507
6508     case CPP_LSHIFT_EQ:
6509       op = LSHIFT_EXPR;
6510       break;
6511
6512     case CPP_AND_EQ:
6513       op = BIT_AND_EXPR;
6514       break;
6515
6516     case CPP_XOR_EQ:
6517       op = BIT_XOR_EXPR;
6518       break;
6519
6520     case CPP_OR_EQ:
6521       op = BIT_IOR_EXPR;
6522       break;
6523
6524     default:
6525       /* Nothing else is an assignment operator.  */
6526       op = ERROR_MARK;
6527     }
6528
6529   /* If it was an assignment operator, consume it.  */
6530   if (op != ERROR_MARK)
6531     cp_lexer_consume_token (parser->lexer);
6532
6533   return op;
6534 }
6535
6536 /* Parse an expression.
6537
6538    expression:
6539      assignment-expression
6540      expression , assignment-expression
6541
6542    CAST_P is true if this expression is the target of a cast.
6543
6544    Returns a representation of the expression.  */
6545
6546 static tree
6547 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6548 {
6549   tree expression = NULL_TREE;
6550
6551   while (true)
6552     {
6553       tree assignment_expression;
6554
6555       /* Parse the next assignment-expression.  */
6556       assignment_expression
6557         = cp_parser_assignment_expression (parser, cast_p, pidk);
6558       /* If this is the first assignment-expression, we can just
6559          save it away.  */
6560       if (!expression)
6561         expression = assignment_expression;
6562       else
6563         expression = build_x_compound_expr (expression,
6564                                             assignment_expression,
6565                                             tf_warning_or_error);
6566       /* If the next token is not a comma, then we are done with the
6567          expression.  */
6568       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6569         break;
6570       /* Consume the `,'.  */
6571       cp_lexer_consume_token (parser->lexer);
6572       /* A comma operator cannot appear in a constant-expression.  */
6573       if (cp_parser_non_integral_constant_expression (parser,
6574                                                       "a comma operator"))
6575         expression = error_mark_node;
6576     }
6577
6578   return expression;
6579 }
6580
6581 /* Parse a constant-expression.
6582
6583    constant-expression:
6584      conditional-expression
6585
6586   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6587   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6588   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6589   is false, NON_CONSTANT_P should be NULL.  */
6590
6591 static tree
6592 cp_parser_constant_expression (cp_parser* parser,
6593                                bool allow_non_constant_p,
6594                                bool *non_constant_p)
6595 {
6596   bool saved_integral_constant_expression_p;
6597   bool saved_allow_non_integral_constant_expression_p;
6598   bool saved_non_integral_constant_expression_p;
6599   tree expression;
6600
6601   /* It might seem that we could simply parse the
6602      conditional-expression, and then check to see if it were
6603      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6604      one that the compiler can figure out is constant, possibly after
6605      doing some simplifications or optimizations.  The standard has a
6606      precise definition of constant-expression, and we must honor
6607      that, even though it is somewhat more restrictive.
6608
6609      For example:
6610
6611        int i[(2, 3)];
6612
6613      is not a legal declaration, because `(2, 3)' is not a
6614      constant-expression.  The `,' operator is forbidden in a
6615      constant-expression.  However, GCC's constant-folding machinery
6616      will fold this operation to an INTEGER_CST for `3'.  */
6617
6618   /* Save the old settings.  */
6619   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6620   saved_allow_non_integral_constant_expression_p
6621     = parser->allow_non_integral_constant_expression_p;
6622   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6623   /* We are now parsing a constant-expression.  */
6624   parser->integral_constant_expression_p = true;
6625   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6626   parser->non_integral_constant_expression_p = false;
6627   /* Although the grammar says "conditional-expression", we parse an
6628      "assignment-expression", which also permits "throw-expression"
6629      and the use of assignment operators.  In the case that
6630      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6631      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6632      actually essential that we look for an assignment-expression.
6633      For example, cp_parser_initializer_clauses uses this function to
6634      determine whether a particular assignment-expression is in fact
6635      constant.  */
6636   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6637   /* Restore the old settings.  */
6638   parser->integral_constant_expression_p
6639     = saved_integral_constant_expression_p;
6640   parser->allow_non_integral_constant_expression_p
6641     = saved_allow_non_integral_constant_expression_p;
6642   if (allow_non_constant_p)
6643     *non_constant_p = parser->non_integral_constant_expression_p;
6644   else if (parser->non_integral_constant_expression_p)
6645     expression = error_mark_node;
6646   parser->non_integral_constant_expression_p
6647     = saved_non_integral_constant_expression_p;
6648
6649   return expression;
6650 }
6651
6652 /* Parse __builtin_offsetof.
6653
6654    offsetof-expression:
6655      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6656
6657    offsetof-member-designator:
6658      id-expression
6659      | offsetof-member-designator "." id-expression
6660      | offsetof-member-designator "[" expression "]"
6661      | offsetof-member-designator "->" id-expression  */
6662
6663 static tree
6664 cp_parser_builtin_offsetof (cp_parser *parser)
6665 {
6666   int save_ice_p, save_non_ice_p;
6667   tree type, expr;
6668   cp_id_kind dummy;
6669   cp_token *token;
6670
6671   /* We're about to accept non-integral-constant things, but will
6672      definitely yield an integral constant expression.  Save and
6673      restore these values around our local parsing.  */
6674   save_ice_p = parser->integral_constant_expression_p;
6675   save_non_ice_p = parser->non_integral_constant_expression_p;
6676
6677   /* Consume the "__builtin_offsetof" token.  */
6678   cp_lexer_consume_token (parser->lexer);
6679   /* Consume the opening `('.  */
6680   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6681   /* Parse the type-id.  */
6682   type = cp_parser_type_id (parser);
6683   /* Look for the `,'.  */
6684   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6685   token = cp_lexer_peek_token (parser->lexer);
6686
6687   /* Build the (type *)null that begins the traditional offsetof macro.  */
6688   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6689                             tf_warning_or_error);
6690
6691   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6692   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6693                                                  true, &dummy, token->location);
6694   while (true)
6695     {
6696       token = cp_lexer_peek_token (parser->lexer);
6697       switch (token->type)
6698         {
6699         case CPP_OPEN_SQUARE:
6700           /* offsetof-member-designator "[" expression "]" */
6701           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6702           break;
6703
6704         case CPP_DEREF:
6705           /* offsetof-member-designator "->" identifier */
6706           expr = grok_array_decl (expr, integer_zero_node);
6707           /* FALLTHRU */
6708
6709         case CPP_DOT:
6710           /* offsetof-member-designator "." identifier */
6711           cp_lexer_consume_token (parser->lexer);
6712           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6713                                                          expr, true, &dummy,
6714                                                          token->location);
6715           break;
6716
6717         case CPP_CLOSE_PAREN:
6718           /* Consume the ")" token.  */
6719           cp_lexer_consume_token (parser->lexer);
6720           goto success;
6721
6722         default:
6723           /* Error.  We know the following require will fail, but
6724              that gives the proper error message.  */
6725           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6726           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6727           expr = error_mark_node;
6728           goto failure;
6729         }
6730     }
6731
6732  success:
6733   /* If we're processing a template, we can't finish the semantics yet.
6734      Otherwise we can fold the entire expression now.  */
6735   if (processing_template_decl)
6736     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6737   else
6738     expr = finish_offsetof (expr);
6739
6740  failure:
6741   parser->integral_constant_expression_p = save_ice_p;
6742   parser->non_integral_constant_expression_p = save_non_ice_p;
6743
6744   return expr;
6745 }
6746
6747 /* Parse a trait expression.  */
6748
6749 static tree
6750 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6751 {
6752   cp_trait_kind kind;
6753   tree type1, type2 = NULL_TREE;
6754   bool binary = false;
6755   cp_decl_specifier_seq decl_specs;
6756
6757   switch (keyword)
6758     {
6759     case RID_HAS_NOTHROW_ASSIGN:
6760       kind = CPTK_HAS_NOTHROW_ASSIGN;
6761       break;
6762     case RID_HAS_NOTHROW_CONSTRUCTOR:
6763       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6764       break;
6765     case RID_HAS_NOTHROW_COPY:
6766       kind = CPTK_HAS_NOTHROW_COPY;
6767       break;
6768     case RID_HAS_TRIVIAL_ASSIGN:
6769       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6770       break;
6771     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6772       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6773       break;
6774     case RID_HAS_TRIVIAL_COPY:
6775       kind = CPTK_HAS_TRIVIAL_COPY;
6776       break;
6777     case RID_HAS_TRIVIAL_DESTRUCTOR:
6778       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6779       break;
6780     case RID_HAS_VIRTUAL_DESTRUCTOR:
6781       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6782       break;
6783     case RID_IS_ABSTRACT:
6784       kind = CPTK_IS_ABSTRACT;
6785       break;
6786     case RID_IS_BASE_OF:
6787       kind = CPTK_IS_BASE_OF;
6788       binary = true;
6789       break;
6790     case RID_IS_CLASS:
6791       kind = CPTK_IS_CLASS;
6792       break;
6793     case RID_IS_CONVERTIBLE_TO:
6794       kind = CPTK_IS_CONVERTIBLE_TO;
6795       binary = true;
6796       break;
6797     case RID_IS_EMPTY:
6798       kind = CPTK_IS_EMPTY;
6799       break;
6800     case RID_IS_ENUM:
6801       kind = CPTK_IS_ENUM;
6802       break;
6803     case RID_IS_POD:
6804       kind = CPTK_IS_POD;
6805       break;
6806     case RID_IS_POLYMORPHIC:
6807       kind = CPTK_IS_POLYMORPHIC;
6808       break;
6809     case RID_IS_UNION:
6810       kind = CPTK_IS_UNION;
6811       break;
6812     default:
6813       gcc_unreachable ();
6814     }
6815
6816   /* Consume the token.  */
6817   cp_lexer_consume_token (parser->lexer);
6818
6819   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6820
6821   type1 = cp_parser_type_id (parser);
6822
6823   if (type1 == error_mark_node)
6824     return error_mark_node;
6825
6826   /* Build a trivial decl-specifier-seq.  */
6827   clear_decl_specs (&decl_specs);
6828   decl_specs.type = type1;
6829
6830   /* Call grokdeclarator to figure out what type this is.  */
6831   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6832                           /*initialized=*/0, /*attrlist=*/NULL);
6833
6834   if (binary)
6835     {
6836       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6837  
6838       type2 = cp_parser_type_id (parser);
6839
6840       if (type2 == error_mark_node)
6841         return error_mark_node;
6842
6843       /* Build a trivial decl-specifier-seq.  */
6844       clear_decl_specs (&decl_specs);
6845       decl_specs.type = type2;
6846
6847       /* Call grokdeclarator to figure out what type this is.  */
6848       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6849                               /*initialized=*/0, /*attrlist=*/NULL);
6850     }
6851
6852   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6853
6854   /* Complete the trait expression, which may mean either processing
6855      the trait expr now or saving it for template instantiation.  */
6856   return finish_trait_expr (kind, type1, type2);
6857 }
6858
6859 /* Statements [gram.stmt.stmt]  */
6860
6861 /* Parse a statement.
6862
6863    statement:
6864      labeled-statement
6865      expression-statement
6866      compound-statement
6867      selection-statement
6868      iteration-statement
6869      jump-statement
6870      declaration-statement
6871      try-block
6872
6873   IN_COMPOUND is true when the statement is nested inside a
6874   cp_parser_compound_statement; this matters for certain pragmas.
6875
6876   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6877   is a (possibly labeled) if statement which is not enclosed in braces
6878   and has an else clause.  This is used to implement -Wparentheses.  */
6879
6880 static void
6881 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6882                      bool in_compound, bool *if_p)
6883 {
6884   tree statement;
6885   cp_token *token;
6886   location_t statement_location;
6887
6888  restart:
6889   if (if_p != NULL)
6890     *if_p = false;
6891   /* There is no statement yet.  */
6892   statement = NULL_TREE;
6893   /* Peek at the next token.  */
6894   token = cp_lexer_peek_token (parser->lexer);
6895   /* Remember the location of the first token in the statement.  */
6896   statement_location = token->location;
6897   /* If this is a keyword, then that will often determine what kind of
6898      statement we have.  */
6899   if (token->type == CPP_KEYWORD)
6900     {
6901       enum rid keyword = token->keyword;
6902
6903       switch (keyword)
6904         {
6905         case RID_CASE:
6906         case RID_DEFAULT:
6907           /* Looks like a labeled-statement with a case label.
6908              Parse the label, and then use tail recursion to parse
6909              the statement.  */
6910           cp_parser_label_for_labeled_statement (parser);
6911           goto restart;
6912
6913         case RID_IF:
6914         case RID_SWITCH:
6915           statement = cp_parser_selection_statement (parser, if_p);
6916           break;
6917
6918         case RID_WHILE:
6919         case RID_DO:
6920         case RID_FOR:
6921           statement = cp_parser_iteration_statement (parser);
6922           break;
6923
6924         case RID_BREAK:
6925         case RID_CONTINUE:
6926         case RID_RETURN:
6927         case RID_GOTO:
6928           statement = cp_parser_jump_statement (parser);
6929           break;
6930
6931           /* Objective-C++ exception-handling constructs.  */
6932         case RID_AT_TRY:
6933         case RID_AT_CATCH:
6934         case RID_AT_FINALLY:
6935         case RID_AT_SYNCHRONIZED:
6936         case RID_AT_THROW:
6937           statement = cp_parser_objc_statement (parser);
6938           break;
6939
6940         case RID_TRY:
6941           statement = cp_parser_try_block (parser);
6942           break;
6943
6944         case RID_NAMESPACE:
6945           /* This must be a namespace alias definition.  */
6946           cp_parser_declaration_statement (parser);
6947           return;
6948           
6949         default:
6950           /* It might be a keyword like `int' that can start a
6951              declaration-statement.  */
6952           break;
6953         }
6954     }
6955   else if (token->type == CPP_NAME)
6956     {
6957       /* If the next token is a `:', then we are looking at a
6958          labeled-statement.  */
6959       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6960       if (token->type == CPP_COLON)
6961         {
6962           /* Looks like a labeled-statement with an ordinary label.
6963              Parse the label, and then use tail recursion to parse
6964              the statement.  */
6965           cp_parser_label_for_labeled_statement (parser);
6966           goto restart;
6967         }
6968     }
6969   /* Anything that starts with a `{' must be a compound-statement.  */
6970   else if (token->type == CPP_OPEN_BRACE)
6971     statement = cp_parser_compound_statement (parser, NULL, false);
6972   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6973      a statement all its own.  */
6974   else if (token->type == CPP_PRAGMA)
6975     {
6976       /* Only certain OpenMP pragmas are attached to statements, and thus
6977          are considered statements themselves.  All others are not.  In
6978          the context of a compound, accept the pragma as a "statement" and
6979          return so that we can check for a close brace.  Otherwise we
6980          require a real statement and must go back and read one.  */
6981       if (in_compound)
6982         cp_parser_pragma (parser, pragma_compound);
6983       else if (!cp_parser_pragma (parser, pragma_stmt))
6984         goto restart;
6985       return;
6986     }
6987   else if (token->type == CPP_EOF)
6988     {
6989       cp_parser_error (parser, "expected statement");
6990       return;
6991     }
6992
6993   /* Everything else must be a declaration-statement or an
6994      expression-statement.  Try for the declaration-statement
6995      first, unless we are looking at a `;', in which case we know that
6996      we have an expression-statement.  */
6997   if (!statement)
6998     {
6999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7000         {
7001           cp_parser_parse_tentatively (parser);
7002           /* Try to parse the declaration-statement.  */
7003           cp_parser_declaration_statement (parser);
7004           /* If that worked, we're done.  */
7005           if (cp_parser_parse_definitely (parser))
7006             return;
7007         }
7008       /* Look for an expression-statement instead.  */
7009       statement = cp_parser_expression_statement (parser, in_statement_expr);
7010     }
7011
7012   /* Set the line number for the statement.  */
7013   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7014     SET_EXPR_LOCATION (statement, statement_location);
7015 }
7016
7017 /* Parse the label for a labeled-statement, i.e.
7018
7019    identifier :
7020    case constant-expression :
7021    default :
7022
7023    GNU Extension:
7024    case constant-expression ... constant-expression : statement
7025
7026    When a label is parsed without errors, the label is added to the
7027    parse tree by the finish_* functions, so this function doesn't
7028    have to return the label.  */
7029
7030 static void
7031 cp_parser_label_for_labeled_statement (cp_parser* parser)
7032 {
7033   cp_token *token;
7034
7035   /* The next token should be an identifier.  */
7036   token = cp_lexer_peek_token (parser->lexer);
7037   if (token->type != CPP_NAME
7038       && token->type != CPP_KEYWORD)
7039     {
7040       cp_parser_error (parser, "expected labeled-statement");
7041       return;
7042     }
7043
7044   switch (token->keyword)
7045     {
7046     case RID_CASE:
7047       {
7048         tree expr, expr_hi;
7049         cp_token *ellipsis;
7050
7051         /* Consume the `case' token.  */
7052         cp_lexer_consume_token (parser->lexer);
7053         /* Parse the constant-expression.  */
7054         expr = cp_parser_constant_expression (parser,
7055                                               /*allow_non_constant_p=*/false,
7056                                               NULL);
7057
7058         ellipsis = cp_lexer_peek_token (parser->lexer);
7059         if (ellipsis->type == CPP_ELLIPSIS)
7060           {
7061             /* Consume the `...' token.  */
7062             cp_lexer_consume_token (parser->lexer);
7063             expr_hi =
7064               cp_parser_constant_expression (parser,
7065                                              /*allow_non_constant_p=*/false,
7066                                              NULL);
7067             /* We don't need to emit warnings here, as the common code
7068                will do this for us.  */
7069           }
7070         else
7071           expr_hi = NULL_TREE;
7072
7073         if (parser->in_switch_statement_p)
7074           finish_case_label (expr, expr_hi);
7075         else
7076           error ("%Hcase label %qE not within a switch statement",
7077                  &token->location, expr);
7078       }
7079       break;
7080
7081     case RID_DEFAULT:
7082       /* Consume the `default' token.  */
7083       cp_lexer_consume_token (parser->lexer);
7084
7085       if (parser->in_switch_statement_p)
7086         finish_case_label (NULL_TREE, NULL_TREE);
7087       else
7088         error ("%Hcase label not within a switch statement", &token->location);
7089       break;
7090
7091     default:
7092       /* Anything else must be an ordinary label.  */
7093       finish_label_stmt (cp_parser_identifier (parser));
7094       break;
7095     }
7096
7097   /* Require the `:' token.  */
7098   cp_parser_require (parser, CPP_COLON, "%<:%>");
7099 }
7100
7101 /* Parse an expression-statement.
7102
7103    expression-statement:
7104      expression [opt] ;
7105
7106    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7107    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7108    indicates whether this expression-statement is part of an
7109    expression statement.  */
7110
7111 static tree
7112 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7113 {
7114   tree statement = NULL_TREE;
7115
7116   /* If the next token is a ';', then there is no expression
7117      statement.  */
7118   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7119     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7120
7121   /* Consume the final `;'.  */
7122   cp_parser_consume_semicolon_at_end_of_statement (parser);
7123
7124   if (in_statement_expr
7125       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7126     /* This is the final expression statement of a statement
7127        expression.  */
7128     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7129   else if (statement)
7130     statement = finish_expr_stmt (statement);
7131   else
7132     finish_stmt ();
7133
7134   return statement;
7135 }
7136
7137 /* Parse a compound-statement.
7138
7139    compound-statement:
7140      { statement-seq [opt] }
7141
7142    GNU extension:
7143
7144    compound-statement:
7145      { label-declaration-seq [opt] statement-seq [opt] }
7146
7147    label-declaration-seq:
7148      label-declaration
7149      label-declaration-seq label-declaration
7150
7151    Returns a tree representing the statement.  */
7152
7153 static tree
7154 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7155                               bool in_try)
7156 {
7157   tree compound_stmt;
7158
7159   /* Consume the `{'.  */
7160   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7161     return error_mark_node;
7162   /* Begin the compound-statement.  */
7163   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7164   /* If the next keyword is `__label__' we have a label declaration.  */
7165   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7166     cp_parser_label_declaration (parser);
7167   /* Parse an (optional) statement-seq.  */
7168   cp_parser_statement_seq_opt (parser, in_statement_expr);
7169   /* Finish the compound-statement.  */
7170   finish_compound_stmt (compound_stmt);
7171   /* Consume the `}'.  */
7172   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7173
7174   return compound_stmt;
7175 }
7176
7177 /* Parse an (optional) statement-seq.
7178
7179    statement-seq:
7180      statement
7181      statement-seq [opt] statement  */
7182
7183 static void
7184 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7185 {
7186   /* Scan statements until there aren't any more.  */
7187   while (true)
7188     {
7189       cp_token *token = cp_lexer_peek_token (parser->lexer);
7190
7191       /* If we're looking at a `}', then we've run out of statements.  */
7192       if (token->type == CPP_CLOSE_BRACE
7193           || token->type == CPP_EOF
7194           || token->type == CPP_PRAGMA_EOL)
7195         break;
7196       
7197       /* If we are in a compound statement and find 'else' then
7198          something went wrong.  */
7199       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7200         {
7201           if (parser->in_statement & IN_IF_STMT) 
7202             break;
7203           else
7204             {
7205               token = cp_lexer_consume_token (parser->lexer);
7206               error ("%H%<else%> without a previous %<if%>", &token->location);
7207             }
7208         }
7209
7210       /* Parse the statement.  */
7211       cp_parser_statement (parser, in_statement_expr, true, NULL);
7212     }
7213 }
7214
7215 /* Parse a selection-statement.
7216
7217    selection-statement:
7218      if ( condition ) statement
7219      if ( condition ) statement else statement
7220      switch ( condition ) statement
7221
7222    Returns the new IF_STMT or SWITCH_STMT.
7223
7224    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7225    is a (possibly labeled) if statement which is not enclosed in
7226    braces and has an else clause.  This is used to implement
7227    -Wparentheses.  */
7228
7229 static tree
7230 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7231 {
7232   cp_token *token;
7233   enum rid keyword;
7234
7235   if (if_p != NULL)
7236     *if_p = false;
7237
7238   /* Peek at the next token.  */
7239   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7240
7241   /* See what kind of keyword it is.  */
7242   keyword = token->keyword;
7243   switch (keyword)
7244     {
7245     case RID_IF:
7246     case RID_SWITCH:
7247       {
7248         tree statement;
7249         tree condition;
7250
7251         /* Look for the `('.  */
7252         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7253           {
7254             cp_parser_skip_to_end_of_statement (parser);
7255             return error_mark_node;
7256           }
7257
7258         /* Begin the selection-statement.  */
7259         if (keyword == RID_IF)
7260           statement = begin_if_stmt ();
7261         else
7262           statement = begin_switch_stmt ();
7263
7264         /* Parse the condition.  */
7265         condition = cp_parser_condition (parser);
7266         /* Look for the `)'.  */
7267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7268           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7269                                                  /*consume_paren=*/true);
7270
7271         if (keyword == RID_IF)
7272           {
7273             bool nested_if;
7274             unsigned char in_statement;
7275
7276             /* Add the condition.  */
7277             finish_if_stmt_cond (condition, statement);
7278
7279             /* Parse the then-clause.  */
7280             in_statement = parser->in_statement;
7281             parser->in_statement |= IN_IF_STMT;
7282             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7283               {
7284                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7285                 add_stmt (build_empty_stmt ());
7286                 cp_lexer_consume_token (parser->lexer);
7287                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7288                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7289                               "empty body in an %<if%> statement");
7290                 nested_if = false;
7291               }
7292             else
7293               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7294             parser->in_statement = in_statement;
7295
7296             finish_then_clause (statement);
7297
7298             /* If the next token is `else', parse the else-clause.  */
7299             if (cp_lexer_next_token_is_keyword (parser->lexer,
7300                                                 RID_ELSE))
7301               {
7302                 /* Consume the `else' keyword.  */
7303                 cp_lexer_consume_token (parser->lexer);
7304                 begin_else_clause (statement);
7305                 /* Parse the else-clause.  */
7306                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7307                   {
7308                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7309                                 OPT_Wempty_body, "suggest braces around "
7310                                 "empty body in an %<else%> statement");
7311                     add_stmt (build_empty_stmt ());
7312                     cp_lexer_consume_token (parser->lexer);
7313                   }
7314                 else
7315                   cp_parser_implicitly_scoped_statement (parser, NULL);
7316
7317                 finish_else_clause (statement);
7318
7319                 /* If we are currently parsing a then-clause, then
7320                    IF_P will not be NULL.  We set it to true to
7321                    indicate that this if statement has an else clause.
7322                    This may trigger the Wparentheses warning below
7323                    when we get back up to the parent if statement.  */
7324                 if (if_p != NULL)
7325                   *if_p = true;
7326               }
7327             else
7328               {
7329                 /* This if statement does not have an else clause.  If
7330                    NESTED_IF is true, then the then-clause is an if
7331                    statement which does have an else clause.  We warn
7332                    about the potential ambiguity.  */
7333                 if (nested_if)
7334                   warning (OPT_Wparentheses,
7335                            ("%Hsuggest explicit braces "
7336                             "to avoid ambiguous %<else%>"),
7337                            EXPR_LOCUS (statement));
7338               }
7339
7340             /* Now we're all done with the if-statement.  */
7341             finish_if_stmt (statement);
7342           }
7343         else
7344           {
7345             bool in_switch_statement_p;
7346             unsigned char in_statement;
7347
7348             /* Add the condition.  */
7349             finish_switch_cond (condition, statement);
7350
7351             /* Parse the body of the switch-statement.  */
7352             in_switch_statement_p = parser->in_switch_statement_p;
7353             in_statement = parser->in_statement;
7354             parser->in_switch_statement_p = true;
7355             parser->in_statement |= IN_SWITCH_STMT;
7356             cp_parser_implicitly_scoped_statement (parser, NULL);
7357             parser->in_switch_statement_p = in_switch_statement_p;
7358             parser->in_statement = in_statement;
7359
7360             /* Now we're all done with the switch-statement.  */
7361             finish_switch_stmt (statement);
7362           }
7363
7364         return statement;
7365       }
7366       break;
7367
7368     default:
7369       cp_parser_error (parser, "expected selection-statement");
7370       return error_mark_node;
7371     }
7372 }
7373
7374 /* Parse a condition.
7375
7376    condition:
7377      expression
7378      type-specifier-seq declarator = initializer-clause
7379      type-specifier-seq declarator braced-init-list
7380
7381    GNU Extension:
7382
7383    condition:
7384      type-specifier-seq declarator asm-specification [opt]
7385        attributes [opt] = assignment-expression
7386
7387    Returns the expression that should be tested.  */
7388
7389 static tree
7390 cp_parser_condition (cp_parser* parser)
7391 {
7392   cp_decl_specifier_seq type_specifiers;
7393   const char *saved_message;
7394
7395   /* Try the declaration first.  */
7396   cp_parser_parse_tentatively (parser);
7397   /* New types are not allowed in the type-specifier-seq for a
7398      condition.  */
7399   saved_message = parser->type_definition_forbidden_message;
7400   parser->type_definition_forbidden_message
7401     = "types may not be defined in conditions";
7402   /* Parse the type-specifier-seq.  */
7403   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7404                                 &type_specifiers);
7405   /* Restore the saved message.  */
7406   parser->type_definition_forbidden_message = saved_message;
7407   /* If all is well, we might be looking at a declaration.  */
7408   if (!cp_parser_error_occurred (parser))
7409     {
7410       tree decl;
7411       tree asm_specification;
7412       tree attributes;
7413       cp_declarator *declarator;
7414       tree initializer = NULL_TREE;
7415
7416       /* Parse the declarator.  */
7417       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7418                                          /*ctor_dtor_or_conv_p=*/NULL,
7419                                          /*parenthesized_p=*/NULL,
7420                                          /*member_p=*/false);
7421       /* Parse the attributes.  */
7422       attributes = cp_parser_attributes_opt (parser);
7423       /* Parse the asm-specification.  */
7424       asm_specification = cp_parser_asm_specification_opt (parser);
7425       /* If the next token is not an `=' or '{', then we might still be
7426          looking at an expression.  For example:
7427
7428            if (A(a).x)
7429
7430          looks like a decl-specifier-seq and a declarator -- but then
7431          there is no `=', so this is an expression.  */
7432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7433           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7434         cp_parser_simulate_error (parser);
7435         
7436       /* If we did see an `=' or '{', then we are looking at a declaration
7437          for sure.  */
7438       if (cp_parser_parse_definitely (parser))
7439         {
7440           tree pushed_scope;
7441           bool non_constant_p;
7442           bool flags = LOOKUP_ONLYCONVERTING;
7443
7444           /* Create the declaration.  */
7445           decl = start_decl (declarator, &type_specifiers,
7446                              /*initialized_p=*/true,
7447                              attributes, /*prefix_attributes=*/NULL_TREE,
7448                              &pushed_scope);
7449
7450           /* Parse the initializer.  */
7451           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7452             {
7453               initializer = cp_parser_braced_list (parser, &non_constant_p);
7454               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7455               flags = 0;
7456             }
7457           else
7458             {
7459               /* Consume the `='.  */
7460               cp_parser_require (parser, CPP_EQ, "%<=%>");
7461               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7462             }
7463           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7464             maybe_warn_cpp0x ("extended initializer lists");
7465
7466           if (!non_constant_p)
7467             initializer = fold_non_dependent_expr (initializer);
7468
7469           /* Process the initializer.  */
7470           cp_finish_decl (decl,
7471                           initializer, !non_constant_p,
7472                           asm_specification,
7473                           flags);
7474
7475           if (pushed_scope)
7476             pop_scope (pushed_scope);
7477
7478           return convert_from_reference (decl);
7479         }
7480     }
7481   /* If we didn't even get past the declarator successfully, we are
7482      definitely not looking at a declaration.  */
7483   else
7484     cp_parser_abort_tentative_parse (parser);
7485
7486   /* Otherwise, we are looking at an expression.  */
7487   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7488 }
7489
7490 /* Parse an iteration-statement.
7491
7492    iteration-statement:
7493      while ( condition ) statement
7494      do statement while ( expression ) ;
7495      for ( for-init-statement condition [opt] ; expression [opt] )
7496        statement
7497
7498    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7499
7500 static tree
7501 cp_parser_iteration_statement (cp_parser* parser)
7502 {
7503   cp_token *token;
7504   enum rid keyword;
7505   tree statement;
7506   unsigned char in_statement;
7507
7508   /* Peek at the next token.  */
7509   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7510   if (!token)
7511     return error_mark_node;
7512
7513   /* Remember whether or not we are already within an iteration
7514      statement.  */
7515   in_statement = parser->in_statement;
7516
7517   /* See what kind of keyword it is.  */
7518   keyword = token->keyword;
7519   switch (keyword)
7520     {
7521     case RID_WHILE:
7522       {
7523         tree condition;
7524
7525         /* Begin the while-statement.  */
7526         statement = begin_while_stmt ();
7527         /* Look for the `('.  */
7528         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7529         /* Parse the condition.  */
7530         condition = cp_parser_condition (parser);
7531         finish_while_stmt_cond (condition, statement);
7532         /* Look for the `)'.  */
7533         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7534         /* Parse the dependent statement.  */
7535         parser->in_statement = IN_ITERATION_STMT;
7536         cp_parser_already_scoped_statement (parser);
7537         parser->in_statement = in_statement;
7538         /* We're done with the while-statement.  */
7539         finish_while_stmt (statement);
7540       }
7541       break;
7542
7543     case RID_DO:
7544       {
7545         tree expression;
7546
7547         /* Begin the do-statement.  */
7548         statement = begin_do_stmt ();
7549         /* Parse the body of the do-statement.  */
7550         parser->in_statement = IN_ITERATION_STMT;
7551         cp_parser_implicitly_scoped_statement (parser, NULL);
7552         parser->in_statement = in_statement;
7553         finish_do_body (statement);
7554         /* Look for the `while' keyword.  */
7555         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7556         /* Look for the `('.  */
7557         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7558         /* Parse the expression.  */
7559         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7560         /* We're done with the do-statement.  */
7561         finish_do_stmt (expression, statement);
7562         /* Look for the `)'.  */
7563         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7564         /* Look for the `;'.  */
7565         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7566       }
7567       break;
7568
7569     case RID_FOR:
7570       {
7571         tree condition = NULL_TREE;
7572         tree expression = NULL_TREE;
7573
7574         /* Begin the for-statement.  */
7575         statement = begin_for_stmt ();
7576         /* Look for the `('.  */
7577         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7578         /* Parse the initialization.  */
7579         cp_parser_for_init_statement (parser);
7580         finish_for_init_stmt (statement);
7581
7582         /* If there's a condition, process it.  */
7583         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7584           condition = cp_parser_condition (parser);
7585         finish_for_cond (condition, statement);
7586         /* Look for the `;'.  */
7587         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7588
7589         /* If there's an expression, process it.  */
7590         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7591           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7592         finish_for_expr (expression, statement);
7593         /* Look for the `)'.  */
7594         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7595
7596         /* Parse the body of the for-statement.  */
7597         parser->in_statement = IN_ITERATION_STMT;
7598         cp_parser_already_scoped_statement (parser);
7599         parser->in_statement = in_statement;
7600
7601         /* We're done with the for-statement.  */
7602         finish_for_stmt (statement);
7603       }
7604       break;
7605
7606     default:
7607       cp_parser_error (parser, "expected iteration-statement");
7608       statement = error_mark_node;
7609       break;
7610     }
7611
7612   return statement;
7613 }
7614
7615 /* Parse a for-init-statement.
7616
7617    for-init-statement:
7618      expression-statement
7619      simple-declaration  */
7620
7621 static void
7622 cp_parser_for_init_statement (cp_parser* parser)
7623 {
7624   /* If the next token is a `;', then we have an empty
7625      expression-statement.  Grammatically, this is also a
7626      simple-declaration, but an invalid one, because it does not
7627      declare anything.  Therefore, if we did not handle this case
7628      specially, we would issue an error message about an invalid
7629      declaration.  */
7630   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7631     {
7632       /* We're going to speculatively look for a declaration, falling back
7633          to an expression, if necessary.  */
7634       cp_parser_parse_tentatively (parser);
7635       /* Parse the declaration.  */
7636       cp_parser_simple_declaration (parser,
7637                                     /*function_definition_allowed_p=*/false);
7638       /* If the tentative parse failed, then we shall need to look for an
7639          expression-statement.  */
7640       if (cp_parser_parse_definitely (parser))
7641         return;
7642     }
7643
7644   cp_parser_expression_statement (parser, false);
7645 }
7646
7647 /* Parse a jump-statement.
7648
7649    jump-statement:
7650      break ;
7651      continue ;
7652      return expression [opt] ;
7653      return braced-init-list ;
7654      goto identifier ;
7655
7656    GNU extension:
7657
7658    jump-statement:
7659      goto * expression ;
7660
7661    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7662
7663 static tree
7664 cp_parser_jump_statement (cp_parser* parser)
7665 {
7666   tree statement = error_mark_node;
7667   cp_token *token;
7668   enum rid keyword;
7669   unsigned char in_statement;
7670
7671   /* Peek at the next token.  */
7672   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7673   if (!token)
7674     return error_mark_node;
7675
7676   /* See what kind of keyword it is.  */
7677   keyword = token->keyword;
7678   switch (keyword)
7679     {
7680     case RID_BREAK:
7681       in_statement = parser->in_statement & ~IN_IF_STMT;      
7682       switch (in_statement)
7683         {
7684         case 0:
7685           error ("%Hbreak statement not within loop or switch", &token->location);
7686           break;
7687         default:
7688           gcc_assert ((in_statement & IN_SWITCH_STMT)
7689                       || in_statement == IN_ITERATION_STMT);
7690           statement = finish_break_stmt ();
7691           break;
7692         case IN_OMP_BLOCK:
7693           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7694           break;
7695         case IN_OMP_FOR:
7696           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7697           break;
7698         }
7699       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7700       break;
7701
7702     case RID_CONTINUE:
7703       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7704         {
7705         case 0:
7706           error ("%Hcontinue statement not within a loop", &token->location);
7707           break;
7708         case IN_ITERATION_STMT:
7709         case IN_OMP_FOR:
7710           statement = finish_continue_stmt ();
7711           break;
7712         case IN_OMP_BLOCK:
7713           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7714           break;
7715         default:
7716           gcc_unreachable ();
7717         }
7718       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7719       break;
7720
7721     case RID_RETURN:
7722       {
7723         tree expr;
7724         bool expr_non_constant_p;
7725
7726         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7727           {
7728             maybe_warn_cpp0x ("extended initializer lists");
7729             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7730           }
7731         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7732           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7733         else
7734           /* If the next token is a `;', then there is no
7735              expression.  */
7736           expr = NULL_TREE;
7737         /* Build the return-statement.  */
7738         statement = finish_return_stmt (expr);
7739         /* Look for the final `;'.  */
7740         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7741       }
7742       break;
7743
7744     case RID_GOTO:
7745       /* Create the goto-statement.  */
7746       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7747         {
7748           /* Issue a warning about this use of a GNU extension.  */
7749           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7750           /* Consume the '*' token.  */
7751           cp_lexer_consume_token (parser->lexer);
7752           /* Parse the dependent expression.  */
7753           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7754         }
7755       else
7756         finish_goto_stmt (cp_parser_identifier (parser));
7757       /* Look for the final `;'.  */
7758       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7759       break;
7760
7761     default:
7762       cp_parser_error (parser, "expected jump-statement");
7763       break;
7764     }
7765
7766   return statement;
7767 }
7768
7769 /* Parse a declaration-statement.
7770
7771    declaration-statement:
7772      block-declaration  */
7773
7774 static void
7775 cp_parser_declaration_statement (cp_parser* parser)
7776 {
7777   void *p;
7778
7779   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7780   p = obstack_alloc (&declarator_obstack, 0);
7781
7782  /* Parse the block-declaration.  */
7783   cp_parser_block_declaration (parser, /*statement_p=*/true);
7784
7785   /* Free any declarators allocated.  */
7786   obstack_free (&declarator_obstack, p);
7787
7788   /* Finish off the statement.  */
7789   finish_stmt ();
7790 }
7791
7792 /* Some dependent statements (like `if (cond) statement'), are
7793    implicitly in their own scope.  In other words, if the statement is
7794    a single statement (as opposed to a compound-statement), it is
7795    none-the-less treated as if it were enclosed in braces.  Any
7796    declarations appearing in the dependent statement are out of scope
7797    after control passes that point.  This function parses a statement,
7798    but ensures that is in its own scope, even if it is not a
7799    compound-statement.
7800
7801    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7802    is a (possibly labeled) if statement which is not enclosed in
7803    braces and has an else clause.  This is used to implement
7804    -Wparentheses.
7805
7806    Returns the new statement.  */
7807
7808 static tree
7809 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7810 {
7811   tree statement;
7812
7813   if (if_p != NULL)
7814     *if_p = false;
7815
7816   /* Mark if () ; with a special NOP_EXPR.  */
7817   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7818     {
7819       cp_lexer_consume_token (parser->lexer);
7820       statement = add_stmt (build_empty_stmt ());
7821     }
7822   /* if a compound is opened, we simply parse the statement directly.  */
7823   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7824     statement = cp_parser_compound_statement (parser, NULL, false);
7825   /* If the token is not a `{', then we must take special action.  */
7826   else
7827     {
7828       /* Create a compound-statement.  */
7829       statement = begin_compound_stmt (0);
7830       /* Parse the dependent-statement.  */
7831       cp_parser_statement (parser, NULL_TREE, false, if_p);
7832       /* Finish the dummy compound-statement.  */
7833       finish_compound_stmt (statement);
7834     }
7835
7836   /* Return the statement.  */
7837   return statement;
7838 }
7839
7840 /* For some dependent statements (like `while (cond) statement'), we
7841    have already created a scope.  Therefore, even if the dependent
7842    statement is a compound-statement, we do not want to create another
7843    scope.  */
7844
7845 static void
7846 cp_parser_already_scoped_statement (cp_parser* parser)
7847 {
7848   /* If the token is a `{', then we must take special action.  */
7849   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7850     cp_parser_statement (parser, NULL_TREE, false, NULL);
7851   else
7852     {
7853       /* Avoid calling cp_parser_compound_statement, so that we
7854          don't create a new scope.  Do everything else by hand.  */
7855       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7856       /* If the next keyword is `__label__' we have a label declaration.  */
7857       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7858         cp_parser_label_declaration (parser);
7859       /* Parse an (optional) statement-seq.  */
7860       cp_parser_statement_seq_opt (parser, NULL_TREE);
7861       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7862     }
7863 }
7864
7865 /* Declarations [gram.dcl.dcl] */
7866
7867 /* Parse an optional declaration-sequence.
7868
7869    declaration-seq:
7870      declaration
7871      declaration-seq declaration  */
7872
7873 static void
7874 cp_parser_declaration_seq_opt (cp_parser* parser)
7875 {
7876   while (true)
7877     {
7878       cp_token *token;
7879
7880       token = cp_lexer_peek_token (parser->lexer);
7881
7882       if (token->type == CPP_CLOSE_BRACE
7883           || token->type == CPP_EOF
7884           || token->type == CPP_PRAGMA_EOL)
7885         break;
7886
7887       if (token->type == CPP_SEMICOLON)
7888         {
7889           /* A declaration consisting of a single semicolon is
7890              invalid.  Allow it unless we're being pedantic.  */
7891           cp_lexer_consume_token (parser->lexer);
7892           if (!in_system_header)
7893             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7894           continue;
7895         }
7896
7897       /* If we're entering or exiting a region that's implicitly
7898          extern "C", modify the lang context appropriately.  */
7899       if (!parser->implicit_extern_c && token->implicit_extern_c)
7900         {
7901           push_lang_context (lang_name_c);
7902           parser->implicit_extern_c = true;
7903         }
7904       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7905         {
7906           pop_lang_context ();
7907           parser->implicit_extern_c = false;
7908         }
7909
7910       if (token->type == CPP_PRAGMA)
7911         {
7912           /* A top-level declaration can consist solely of a #pragma.
7913              A nested declaration cannot, so this is done here and not
7914              in cp_parser_declaration.  (A #pragma at block scope is
7915              handled in cp_parser_statement.)  */
7916           cp_parser_pragma (parser, pragma_external);
7917           continue;
7918         }
7919
7920       /* Parse the declaration itself.  */
7921       cp_parser_declaration (parser);
7922     }
7923 }
7924
7925 /* Parse a declaration.
7926
7927    declaration:
7928      block-declaration
7929      function-definition
7930      template-declaration
7931      explicit-instantiation
7932      explicit-specialization
7933      linkage-specification
7934      namespace-definition
7935
7936    GNU extension:
7937
7938    declaration:
7939       __extension__ declaration */
7940
7941 static void
7942 cp_parser_declaration (cp_parser* parser)
7943 {
7944   cp_token token1;
7945   cp_token token2;
7946   int saved_pedantic;
7947   void *p;
7948
7949   /* Check for the `__extension__' keyword.  */
7950   if (cp_parser_extension_opt (parser, &saved_pedantic))
7951     {
7952       /* Parse the qualified declaration.  */
7953       cp_parser_declaration (parser);
7954       /* Restore the PEDANTIC flag.  */
7955       pedantic = saved_pedantic;
7956
7957       return;
7958     }
7959
7960   /* Try to figure out what kind of declaration is present.  */
7961   token1 = *cp_lexer_peek_token (parser->lexer);
7962
7963   if (token1.type != CPP_EOF)
7964     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7965   else
7966     {
7967       token2.type = CPP_EOF;
7968       token2.keyword = RID_MAX;
7969     }
7970
7971   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7972   p = obstack_alloc (&declarator_obstack, 0);
7973
7974   /* If the next token is `extern' and the following token is a string
7975      literal, then we have a linkage specification.  */
7976   if (token1.keyword == RID_EXTERN
7977       && cp_parser_is_string_literal (&token2))
7978     cp_parser_linkage_specification (parser);
7979   /* If the next token is `template', then we have either a template
7980      declaration, an explicit instantiation, or an explicit
7981      specialization.  */
7982   else if (token1.keyword == RID_TEMPLATE)
7983     {
7984       /* `template <>' indicates a template specialization.  */
7985       if (token2.type == CPP_LESS
7986           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7987         cp_parser_explicit_specialization (parser);
7988       /* `template <' indicates a template declaration.  */
7989       else if (token2.type == CPP_LESS)
7990         cp_parser_template_declaration (parser, /*member_p=*/false);
7991       /* Anything else must be an explicit instantiation.  */
7992       else
7993         cp_parser_explicit_instantiation (parser);
7994     }
7995   /* If the next token is `export', then we have a template
7996      declaration.  */
7997   else if (token1.keyword == RID_EXPORT)
7998     cp_parser_template_declaration (parser, /*member_p=*/false);
7999   /* If the next token is `extern', 'static' or 'inline' and the one
8000      after that is `template', we have a GNU extended explicit
8001      instantiation directive.  */
8002   else if (cp_parser_allow_gnu_extensions_p (parser)
8003            && (token1.keyword == RID_EXTERN
8004                || token1.keyword == RID_STATIC
8005                || token1.keyword == RID_INLINE)
8006            && token2.keyword == RID_TEMPLATE)
8007     cp_parser_explicit_instantiation (parser);
8008   /* If the next token is `namespace', check for a named or unnamed
8009      namespace definition.  */
8010   else if (token1.keyword == RID_NAMESPACE
8011            && (/* A named namespace definition.  */
8012                (token2.type == CPP_NAME
8013                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8014                     != CPP_EQ))
8015                /* An unnamed namespace definition.  */
8016                || token2.type == CPP_OPEN_BRACE
8017                || token2.keyword == RID_ATTRIBUTE))
8018     cp_parser_namespace_definition (parser);
8019   /* An inline (associated) namespace definition.  */
8020   else if (token1.keyword == RID_INLINE
8021            && token2.keyword == RID_NAMESPACE)
8022     cp_parser_namespace_definition (parser);
8023   /* Objective-C++ declaration/definition.  */
8024   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8025     cp_parser_objc_declaration (parser);
8026   /* We must have either a block declaration or a function
8027      definition.  */
8028   else
8029     /* Try to parse a block-declaration, or a function-definition.  */
8030     cp_parser_block_declaration (parser, /*statement_p=*/false);
8031
8032   /* Free any declarators allocated.  */
8033   obstack_free (&declarator_obstack, p);
8034 }
8035
8036 /* Parse a block-declaration.
8037
8038    block-declaration:
8039      simple-declaration
8040      asm-definition
8041      namespace-alias-definition
8042      using-declaration
8043      using-directive
8044
8045    GNU Extension:
8046
8047    block-declaration:
8048      __extension__ block-declaration
8049
8050    C++0x Extension:
8051
8052    block-declaration:
8053      static_assert-declaration
8054
8055    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8056    part of a declaration-statement.  */
8057
8058 static void
8059 cp_parser_block_declaration (cp_parser *parser,
8060                              bool      statement_p)
8061 {
8062   cp_token *token1;
8063   int saved_pedantic;
8064
8065   /* Check for the `__extension__' keyword.  */
8066   if (cp_parser_extension_opt (parser, &saved_pedantic))
8067     {
8068       /* Parse the qualified declaration.  */
8069       cp_parser_block_declaration (parser, statement_p);
8070       /* Restore the PEDANTIC flag.  */
8071       pedantic = saved_pedantic;
8072
8073       return;
8074     }
8075
8076   /* Peek at the next token to figure out which kind of declaration is
8077      present.  */
8078   token1 = cp_lexer_peek_token (parser->lexer);
8079
8080   /* If the next keyword is `asm', we have an asm-definition.  */
8081   if (token1->keyword == RID_ASM)
8082     {
8083       if (statement_p)
8084         cp_parser_commit_to_tentative_parse (parser);
8085       cp_parser_asm_definition (parser);
8086     }
8087   /* If the next keyword is `namespace', we have a
8088      namespace-alias-definition.  */
8089   else if (token1->keyword == RID_NAMESPACE)
8090     cp_parser_namespace_alias_definition (parser);
8091   /* If the next keyword is `using', we have either a
8092      using-declaration or a using-directive.  */
8093   else if (token1->keyword == RID_USING)
8094     {
8095       cp_token *token2;
8096
8097       if (statement_p)
8098         cp_parser_commit_to_tentative_parse (parser);
8099       /* If the token after `using' is `namespace', then we have a
8100          using-directive.  */
8101       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8102       if (token2->keyword == RID_NAMESPACE)
8103         cp_parser_using_directive (parser);
8104       /* Otherwise, it's a using-declaration.  */
8105       else
8106         cp_parser_using_declaration (parser,
8107                                      /*access_declaration_p=*/false);
8108     }
8109   /* If the next keyword is `__label__' we have a misplaced label
8110      declaration.  */
8111   else if (token1->keyword == RID_LABEL)
8112     {
8113       cp_lexer_consume_token (parser->lexer);
8114       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8115       cp_parser_skip_to_end_of_statement (parser);
8116       /* If the next token is now a `;', consume it.  */
8117       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8118         cp_lexer_consume_token (parser->lexer);
8119     }
8120   /* If the next token is `static_assert' we have a static assertion.  */
8121   else if (token1->keyword == RID_STATIC_ASSERT)
8122     cp_parser_static_assert (parser, /*member_p=*/false);
8123   /* Anything else must be a simple-declaration.  */
8124   else
8125     cp_parser_simple_declaration (parser, !statement_p);
8126 }
8127
8128 /* Parse a simple-declaration.
8129
8130    simple-declaration:
8131      decl-specifier-seq [opt] init-declarator-list [opt] ;
8132
8133    init-declarator-list:
8134      init-declarator
8135      init-declarator-list , init-declarator
8136
8137    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8138    function-definition as a simple-declaration.  */
8139
8140 static void
8141 cp_parser_simple_declaration (cp_parser* parser,
8142                               bool function_definition_allowed_p)
8143 {
8144   cp_decl_specifier_seq decl_specifiers;
8145   int declares_class_or_enum;
8146   bool saw_declarator;
8147
8148   /* Defer access checks until we know what is being declared; the
8149      checks for names appearing in the decl-specifier-seq should be
8150      done as if we were in the scope of the thing being declared.  */
8151   push_deferring_access_checks (dk_deferred);
8152
8153   /* Parse the decl-specifier-seq.  We have to keep track of whether
8154      or not the decl-specifier-seq declares a named class or
8155      enumeration type, since that is the only case in which the
8156      init-declarator-list is allowed to be empty.
8157
8158      [dcl.dcl]
8159
8160      In a simple-declaration, the optional init-declarator-list can be
8161      omitted only when declaring a class or enumeration, that is when
8162      the decl-specifier-seq contains either a class-specifier, an
8163      elaborated-type-specifier, or an enum-specifier.  */
8164   cp_parser_decl_specifier_seq (parser,
8165                                 CP_PARSER_FLAGS_OPTIONAL,
8166                                 &decl_specifiers,
8167                                 &declares_class_or_enum);
8168   /* We no longer need to defer access checks.  */
8169   stop_deferring_access_checks ();
8170
8171   /* In a block scope, a valid declaration must always have a
8172      decl-specifier-seq.  By not trying to parse declarators, we can
8173      resolve the declaration/expression ambiguity more quickly.  */
8174   if (!function_definition_allowed_p
8175       && !decl_specifiers.any_specifiers_p)
8176     {
8177       cp_parser_error (parser, "expected declaration");
8178       goto done;
8179     }
8180
8181   /* If the next two tokens are both identifiers, the code is
8182      erroneous. The usual cause of this situation is code like:
8183
8184        T t;
8185
8186      where "T" should name a type -- but does not.  */
8187   if (!decl_specifiers.type
8188       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8189     {
8190       /* If parsing tentatively, we should commit; we really are
8191          looking at a declaration.  */
8192       cp_parser_commit_to_tentative_parse (parser);
8193       /* Give up.  */
8194       goto done;
8195     }
8196
8197   /* If we have seen at least one decl-specifier, and the next token
8198      is not a parenthesis, then we must be looking at a declaration.
8199      (After "int (" we might be looking at a functional cast.)  */
8200   if (decl_specifiers.any_specifiers_p
8201       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8202       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8203       && !cp_parser_error_occurred (parser))
8204     cp_parser_commit_to_tentative_parse (parser);
8205
8206   /* Keep going until we hit the `;' at the end of the simple
8207      declaration.  */
8208   saw_declarator = false;
8209   while (cp_lexer_next_token_is_not (parser->lexer,
8210                                      CPP_SEMICOLON))
8211     {
8212       cp_token *token;
8213       bool function_definition_p;
8214       tree decl;
8215
8216       if (saw_declarator)
8217         {
8218           /* If we are processing next declarator, coma is expected */
8219           token = cp_lexer_peek_token (parser->lexer);
8220           gcc_assert (token->type == CPP_COMMA);
8221           cp_lexer_consume_token (parser->lexer);
8222         }
8223       else
8224         saw_declarator = true;
8225
8226       /* Parse the init-declarator.  */
8227       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8228                                         /*checks=*/NULL,
8229                                         function_definition_allowed_p,
8230                                         /*member_p=*/false,
8231                                         declares_class_or_enum,
8232                                         &function_definition_p);
8233       /* If an error occurred while parsing tentatively, exit quickly.
8234          (That usually happens when in the body of a function; each
8235          statement is treated as a declaration-statement until proven
8236          otherwise.)  */
8237       if (cp_parser_error_occurred (parser))
8238         goto done;
8239       /* Handle function definitions specially.  */
8240       if (function_definition_p)
8241         {
8242           /* If the next token is a `,', then we are probably
8243              processing something like:
8244
8245                void f() {}, *p;
8246
8247              which is erroneous.  */
8248           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8249             {
8250               cp_token *token = cp_lexer_peek_token (parser->lexer);
8251               error ("%Hmixing declarations and function-definitions is forbidden",
8252                      &token->location);
8253             }
8254           /* Otherwise, we're done with the list of declarators.  */
8255           else
8256             {
8257               pop_deferring_access_checks ();
8258               return;
8259             }
8260         }
8261       /* The next token should be either a `,' or a `;'.  */
8262       token = cp_lexer_peek_token (parser->lexer);
8263       /* If it's a `,', there are more declarators to come.  */
8264       if (token->type == CPP_COMMA)
8265         /* will be consumed next time around */;
8266       /* If it's a `;', we are done.  */
8267       else if (token->type == CPP_SEMICOLON)
8268         break;
8269       /* Anything else is an error.  */
8270       else
8271         {
8272           /* If we have already issued an error message we don't need
8273              to issue another one.  */
8274           if (decl != error_mark_node
8275               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8276             cp_parser_error (parser, "expected %<,%> or %<;%>");
8277           /* Skip tokens until we reach the end of the statement.  */
8278           cp_parser_skip_to_end_of_statement (parser);
8279           /* If the next token is now a `;', consume it.  */
8280           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8281             cp_lexer_consume_token (parser->lexer);
8282           goto done;
8283         }
8284       /* After the first time around, a function-definition is not
8285          allowed -- even if it was OK at first.  For example:
8286
8287            int i, f() {}
8288
8289          is not valid.  */
8290       function_definition_allowed_p = false;
8291     }
8292
8293   /* Issue an error message if no declarators are present, and the
8294      decl-specifier-seq does not itself declare a class or
8295      enumeration.  */
8296   if (!saw_declarator)
8297     {
8298       if (cp_parser_declares_only_class_p (parser))
8299         shadow_tag (&decl_specifiers);
8300       /* Perform any deferred access checks.  */
8301       perform_deferred_access_checks ();
8302     }
8303
8304   /* Consume the `;'.  */
8305   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8306
8307  done:
8308   pop_deferring_access_checks ();
8309 }
8310
8311 /* Parse a decl-specifier-seq.
8312
8313    decl-specifier-seq:
8314      decl-specifier-seq [opt] decl-specifier
8315
8316    decl-specifier:
8317      storage-class-specifier
8318      type-specifier
8319      function-specifier
8320      friend
8321      typedef
8322
8323    GNU Extension:
8324
8325    decl-specifier:
8326      attributes
8327
8328    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8329
8330    The parser flags FLAGS is used to control type-specifier parsing.
8331
8332    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8333    flags:
8334
8335      1: one of the decl-specifiers is an elaborated-type-specifier
8336         (i.e., a type declaration)
8337      2: one of the decl-specifiers is an enum-specifier or a
8338         class-specifier (i.e., a type definition)
8339
8340    */
8341
8342 static void
8343 cp_parser_decl_specifier_seq (cp_parser* parser,
8344                               cp_parser_flags flags,
8345                               cp_decl_specifier_seq *decl_specs,
8346                               int* declares_class_or_enum)
8347 {
8348   bool constructor_possible_p = !parser->in_declarator_p;
8349   cp_token *start_token = NULL;
8350
8351   /* Clear DECL_SPECS.  */
8352   clear_decl_specs (decl_specs);
8353
8354   /* Assume no class or enumeration type is declared.  */
8355   *declares_class_or_enum = 0;
8356
8357   /* Keep reading specifiers until there are no more to read.  */
8358   while (true)
8359     {
8360       bool constructor_p;
8361       bool found_decl_spec;
8362       cp_token *token;
8363
8364       /* Peek at the next token.  */
8365       token = cp_lexer_peek_token (parser->lexer);
8366
8367       /* Save the first token of the decl spec list for error
8368          reporting.  */
8369       if (!start_token)
8370         start_token = token;
8371       /* Handle attributes.  */
8372       if (token->keyword == RID_ATTRIBUTE)
8373         {
8374           /* Parse the attributes.  */
8375           decl_specs->attributes
8376             = chainon (decl_specs->attributes,
8377                        cp_parser_attributes_opt (parser));
8378           continue;
8379         }
8380       /* Assume we will find a decl-specifier keyword.  */
8381       found_decl_spec = true;
8382       /* If the next token is an appropriate keyword, we can simply
8383          add it to the list.  */
8384       switch (token->keyword)
8385         {
8386           /* decl-specifier:
8387                friend  */
8388         case RID_FRIEND:
8389           if (!at_class_scope_p ())
8390             {
8391               error ("%H%<friend%> used outside of class", &token->location);
8392               cp_lexer_purge_token (parser->lexer);
8393             }
8394           else
8395             {
8396               ++decl_specs->specs[(int) ds_friend];
8397               /* Consume the token.  */
8398               cp_lexer_consume_token (parser->lexer);
8399             }
8400           break;
8401
8402           /* function-specifier:
8403                inline
8404                virtual
8405                explicit  */
8406         case RID_INLINE:
8407         case RID_VIRTUAL:
8408         case RID_EXPLICIT:
8409           cp_parser_function_specifier_opt (parser, decl_specs);
8410           break;
8411
8412           /* decl-specifier:
8413                typedef  */
8414         case RID_TYPEDEF:
8415           ++decl_specs->specs[(int) ds_typedef];
8416           /* Consume the token.  */
8417           cp_lexer_consume_token (parser->lexer);
8418           /* A constructor declarator cannot appear in a typedef.  */
8419           constructor_possible_p = false;
8420           /* The "typedef" keyword can only occur in a declaration; we
8421              may as well commit at this point.  */
8422           cp_parser_commit_to_tentative_parse (parser);
8423
8424           if (decl_specs->storage_class != sc_none)
8425             decl_specs->conflicting_specifiers_p = true;
8426           break;
8427
8428           /* storage-class-specifier:
8429                auto
8430                register
8431                static
8432                extern
8433                mutable
8434
8435              GNU Extension:
8436                thread  */
8437         case RID_AUTO:
8438           if (cxx_dialect == cxx98) 
8439             {
8440               /* Consume the token.  */
8441               cp_lexer_consume_token (parser->lexer);
8442
8443               /* Complain about `auto' as a storage specifier, if
8444                  we're complaining about C++0x compatibility.  */
8445               warning 
8446                 (OPT_Wc__0x_compat, 
8447                  "%H%<auto%> will change meaning in C++0x; please remove it",
8448                  &token->location);
8449
8450               /* Set the storage class anyway.  */
8451               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8452                                            token->location);
8453             }
8454           else
8455             /* C++0x auto type-specifier.  */
8456             found_decl_spec = false;
8457           break;
8458
8459         case RID_REGISTER:
8460         case RID_STATIC:
8461         case RID_EXTERN:
8462         case RID_MUTABLE:
8463           /* Consume the token.  */
8464           cp_lexer_consume_token (parser->lexer);
8465           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8466                                        token->location);
8467           break;
8468         case RID_THREAD:
8469           /* Consume the token.  */
8470           cp_lexer_consume_token (parser->lexer);
8471           ++decl_specs->specs[(int) ds_thread];
8472           break;
8473
8474         default:
8475           /* We did not yet find a decl-specifier yet.  */
8476           found_decl_spec = false;
8477           break;
8478         }
8479
8480       /* Constructors are a special case.  The `S' in `S()' is not a
8481          decl-specifier; it is the beginning of the declarator.  */
8482       constructor_p
8483         = (!found_decl_spec
8484            && constructor_possible_p
8485            && (cp_parser_constructor_declarator_p
8486                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8487
8488       /* If we don't have a DECL_SPEC yet, then we must be looking at
8489          a type-specifier.  */
8490       if (!found_decl_spec && !constructor_p)
8491         {
8492           int decl_spec_declares_class_or_enum;
8493           bool is_cv_qualifier;
8494           tree type_spec;
8495
8496           type_spec
8497             = cp_parser_type_specifier (parser, flags,
8498                                         decl_specs,
8499                                         /*is_declaration=*/true,
8500                                         &decl_spec_declares_class_or_enum,
8501                                         &is_cv_qualifier);
8502           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8503
8504           /* If this type-specifier referenced a user-defined type
8505              (a typedef, class-name, etc.), then we can't allow any
8506              more such type-specifiers henceforth.
8507
8508              [dcl.spec]
8509
8510              The longest sequence of decl-specifiers that could
8511              possibly be a type name is taken as the
8512              decl-specifier-seq of a declaration.  The sequence shall
8513              be self-consistent as described below.
8514
8515              [dcl.type]
8516
8517              As a general rule, at most one type-specifier is allowed
8518              in the complete decl-specifier-seq of a declaration.  The
8519              only exceptions are the following:
8520
8521              -- const or volatile can be combined with any other
8522                 type-specifier.
8523
8524              -- signed or unsigned can be combined with char, long,
8525                 short, or int.
8526
8527              -- ..
8528
8529              Example:
8530
8531                typedef char* Pc;
8532                void g (const int Pc);
8533
8534              Here, Pc is *not* part of the decl-specifier seq; it's
8535              the declarator.  Therefore, once we see a type-specifier
8536              (other than a cv-qualifier), we forbid any additional
8537              user-defined types.  We *do* still allow things like `int
8538              int' to be considered a decl-specifier-seq, and issue the
8539              error message later.  */
8540           if (type_spec && !is_cv_qualifier)
8541             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8542           /* A constructor declarator cannot follow a type-specifier.  */
8543           if (type_spec)
8544             {
8545               constructor_possible_p = false;
8546               found_decl_spec = true;
8547             }
8548         }
8549
8550       /* If we still do not have a DECL_SPEC, then there are no more
8551          decl-specifiers.  */
8552       if (!found_decl_spec)
8553         break;
8554
8555       decl_specs->any_specifiers_p = true;
8556       /* After we see one decl-specifier, further decl-specifiers are
8557          always optional.  */
8558       flags |= CP_PARSER_FLAGS_OPTIONAL;
8559     }
8560
8561   cp_parser_check_decl_spec (decl_specs, start_token->location);
8562
8563   /* Don't allow a friend specifier with a class definition.  */
8564   if (decl_specs->specs[(int) ds_friend] != 0
8565       && (*declares_class_or_enum & 2))
8566     error ("%Hclass definition may not be declared a friend",
8567             &start_token->location);
8568 }
8569
8570 /* Parse an (optional) storage-class-specifier.
8571
8572    storage-class-specifier:
8573      auto
8574      register
8575      static
8576      extern
8577      mutable
8578
8579    GNU Extension:
8580
8581    storage-class-specifier:
8582      thread
8583
8584    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8585
8586 static tree
8587 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8588 {
8589   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8590     {
8591     case RID_AUTO:
8592       if (cxx_dialect != cxx98)
8593         return NULL_TREE;
8594       /* Fall through for C++98.  */
8595
8596     case RID_REGISTER:
8597     case RID_STATIC:
8598     case RID_EXTERN:
8599     case RID_MUTABLE:
8600     case RID_THREAD:
8601       /* Consume the token.  */
8602       return cp_lexer_consume_token (parser->lexer)->u.value;
8603
8604     default:
8605       return NULL_TREE;
8606     }
8607 }
8608
8609 /* Parse an (optional) function-specifier.
8610
8611    function-specifier:
8612      inline
8613      virtual
8614      explicit
8615
8616    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8617    Updates DECL_SPECS, if it is non-NULL.  */
8618
8619 static tree
8620 cp_parser_function_specifier_opt (cp_parser* parser,
8621                                   cp_decl_specifier_seq *decl_specs)
8622 {
8623   cp_token *token = cp_lexer_peek_token (parser->lexer);
8624   switch (token->keyword)
8625     {
8626     case RID_INLINE:
8627       if (decl_specs)
8628         ++decl_specs->specs[(int) ds_inline];
8629       break;
8630
8631     case RID_VIRTUAL:
8632       /* 14.5.2.3 [temp.mem]
8633
8634          A member function template shall not be virtual.  */
8635       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8636         error ("%Htemplates may not be %<virtual%>", &token->location);
8637       else if (decl_specs)
8638         ++decl_specs->specs[(int) ds_virtual];
8639       break;
8640
8641     case RID_EXPLICIT:
8642       if (decl_specs)
8643         ++decl_specs->specs[(int) ds_explicit];
8644       break;
8645
8646     default:
8647       return NULL_TREE;
8648     }
8649
8650   /* Consume the token.  */
8651   return cp_lexer_consume_token (parser->lexer)->u.value;
8652 }
8653
8654 /* Parse a linkage-specification.
8655
8656    linkage-specification:
8657      extern string-literal { declaration-seq [opt] }
8658      extern string-literal declaration  */
8659
8660 static void
8661 cp_parser_linkage_specification (cp_parser* parser)
8662 {
8663   tree linkage;
8664
8665   /* Look for the `extern' keyword.  */
8666   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8667
8668   /* Look for the string-literal.  */
8669   linkage = cp_parser_string_literal (parser, false, false);
8670
8671   /* Transform the literal into an identifier.  If the literal is a
8672      wide-character string, or contains embedded NULs, then we can't
8673      handle it as the user wants.  */
8674   if (strlen (TREE_STRING_POINTER (linkage))
8675       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8676     {
8677       cp_parser_error (parser, "invalid linkage-specification");
8678       /* Assume C++ linkage.  */
8679       linkage = lang_name_cplusplus;
8680     }
8681   else
8682     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8683
8684   /* We're now using the new linkage.  */
8685   push_lang_context (linkage);
8686
8687   /* If the next token is a `{', then we're using the first
8688      production.  */
8689   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8690     {
8691       /* Consume the `{' token.  */
8692       cp_lexer_consume_token (parser->lexer);
8693       /* Parse the declarations.  */
8694       cp_parser_declaration_seq_opt (parser);
8695       /* Look for the closing `}'.  */
8696       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8697     }
8698   /* Otherwise, there's just one declaration.  */
8699   else
8700     {
8701       bool saved_in_unbraced_linkage_specification_p;
8702
8703       saved_in_unbraced_linkage_specification_p
8704         = parser->in_unbraced_linkage_specification_p;
8705       parser->in_unbraced_linkage_specification_p = true;
8706       cp_parser_declaration (parser);
8707       parser->in_unbraced_linkage_specification_p
8708         = saved_in_unbraced_linkage_specification_p;
8709     }
8710
8711   /* We're done with the linkage-specification.  */
8712   pop_lang_context ();
8713 }
8714
8715 /* Parse a static_assert-declaration.
8716
8717    static_assert-declaration:
8718      static_assert ( constant-expression , string-literal ) ; 
8719
8720    If MEMBER_P, this static_assert is a class member.  */
8721
8722 static void 
8723 cp_parser_static_assert(cp_parser *parser, bool member_p)
8724 {
8725   tree condition;
8726   tree message;
8727   cp_token *token;
8728   location_t saved_loc;
8729
8730   /* Peek at the `static_assert' token so we can keep track of exactly
8731      where the static assertion started.  */
8732   token = cp_lexer_peek_token (parser->lexer);
8733   saved_loc = token->location;
8734
8735   /* Look for the `static_assert' keyword.  */
8736   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8737                                   "%<static_assert%>"))
8738     return;
8739
8740   /*  We know we are in a static assertion; commit to any tentative
8741       parse.  */
8742   if (cp_parser_parsing_tentatively (parser))
8743     cp_parser_commit_to_tentative_parse (parser);
8744
8745   /* Parse the `(' starting the static assertion condition.  */
8746   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8747
8748   /* Parse the constant-expression.  */
8749   condition = 
8750     cp_parser_constant_expression (parser,
8751                                    /*allow_non_constant_p=*/false,
8752                                    /*non_constant_p=*/NULL);
8753
8754   /* Parse the separating `,'.  */
8755   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8756
8757   /* Parse the string-literal message.  */
8758   message = cp_parser_string_literal (parser, 
8759                                       /*translate=*/false,
8760                                       /*wide_ok=*/true);
8761
8762   /* A `)' completes the static assertion.  */
8763   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8764     cp_parser_skip_to_closing_parenthesis (parser, 
8765                                            /*recovering=*/true, 
8766                                            /*or_comma=*/false,
8767                                            /*consume_paren=*/true);
8768
8769   /* A semicolon terminates the declaration.  */
8770   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8771
8772   /* Complete the static assertion, which may mean either processing 
8773      the static assert now or saving it for template instantiation.  */
8774   finish_static_assert (condition, message, saved_loc, member_p);
8775 }
8776
8777 /* Parse a `decltype' type. Returns the type. 
8778
8779    simple-type-specifier:
8780      decltype ( expression )  */
8781
8782 static tree
8783 cp_parser_decltype (cp_parser *parser)
8784 {
8785   tree expr;
8786   bool id_expression_or_member_access_p = false;
8787   const char *saved_message;
8788   bool saved_integral_constant_expression_p;
8789   bool saved_non_integral_constant_expression_p;
8790   cp_token *id_expr_start_token;
8791
8792   /* Look for the `decltype' token.  */
8793   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8794     return error_mark_node;
8795
8796   /* Types cannot be defined in a `decltype' expression.  Save away the
8797      old message.  */
8798   saved_message = parser->type_definition_forbidden_message;
8799
8800   /* And create the new one.  */
8801   parser->type_definition_forbidden_message
8802     = "types may not be defined in %<decltype%> expressions";
8803
8804   /* The restrictions on constant-expressions do not apply inside
8805      decltype expressions.  */
8806   saved_integral_constant_expression_p
8807     = parser->integral_constant_expression_p;
8808   saved_non_integral_constant_expression_p
8809     = parser->non_integral_constant_expression_p;
8810   parser->integral_constant_expression_p = false;
8811
8812   /* Do not actually evaluate the expression.  */
8813   ++skip_evaluation;
8814
8815   /* Parse the opening `('.  */
8816   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8817     return error_mark_node;
8818   
8819   /* First, try parsing an id-expression.  */
8820   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8821   cp_parser_parse_tentatively (parser);
8822   expr = cp_parser_id_expression (parser,
8823                                   /*template_keyword_p=*/false,
8824                                   /*check_dependency_p=*/true,
8825                                   /*template_p=*/NULL,
8826                                   /*declarator_p=*/false,
8827                                   /*optional_p=*/false);
8828
8829   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8830     {
8831       bool non_integral_constant_expression_p = false;
8832       tree id_expression = expr;
8833       cp_id_kind idk;
8834       const char *error_msg;
8835
8836       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8837         /* Lookup the name we got back from the id-expression.  */
8838         expr = cp_parser_lookup_name (parser, expr,
8839                                       none_type,
8840                                       /*is_template=*/false,
8841                                       /*is_namespace=*/false,
8842                                       /*check_dependency=*/true,
8843                                       /*ambiguous_decls=*/NULL,
8844                                       id_expr_start_token->location);
8845
8846       if (expr
8847           && expr != error_mark_node
8848           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8849           && TREE_CODE (expr) != TYPE_DECL
8850           && (TREE_CODE (expr) != BIT_NOT_EXPR
8851               || !TYPE_P (TREE_OPERAND (expr, 0)))
8852           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8853         {
8854           /* Complete lookup of the id-expression.  */
8855           expr = (finish_id_expression
8856                   (id_expression, expr, parser->scope, &idk,
8857                    /*integral_constant_expression_p=*/false,
8858                    /*allow_non_integral_constant_expression_p=*/true,
8859                    &non_integral_constant_expression_p,
8860                    /*template_p=*/false,
8861                    /*done=*/true,
8862                    /*address_p=*/false,
8863                    /*template_arg_p=*/false,
8864                    &error_msg,
8865                    id_expr_start_token->location));
8866
8867           if (expr == error_mark_node)
8868             /* We found an id-expression, but it was something that we
8869                should not have found. This is an error, not something
8870                we can recover from, so note that we found an
8871                id-expression and we'll recover as gracefully as
8872                possible.  */
8873             id_expression_or_member_access_p = true;
8874         }
8875
8876       if (expr 
8877           && expr != error_mark_node
8878           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8879         /* We have an id-expression.  */
8880         id_expression_or_member_access_p = true;
8881     }
8882
8883   if (!id_expression_or_member_access_p)
8884     {
8885       /* Abort the id-expression parse.  */
8886       cp_parser_abort_tentative_parse (parser);
8887
8888       /* Parsing tentatively, again.  */
8889       cp_parser_parse_tentatively (parser);
8890
8891       /* Parse a class member access.  */
8892       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8893                                            /*cast_p=*/false,
8894                                            /*member_access_only_p=*/true, NULL);
8895
8896       if (expr 
8897           && expr != error_mark_node
8898           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8899         /* We have an id-expression.  */
8900         id_expression_or_member_access_p = true;
8901     }
8902
8903   if (id_expression_or_member_access_p)
8904     /* We have parsed the complete id-expression or member access.  */
8905     cp_parser_parse_definitely (parser);
8906   else
8907     {
8908       /* Abort our attempt to parse an id-expression or member access
8909          expression.  */
8910       cp_parser_abort_tentative_parse (parser);
8911
8912       /* Parse a full expression.  */
8913       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8914     }
8915
8916   /* Go back to evaluating expressions.  */
8917   --skip_evaluation;
8918
8919   /* Restore the old message and the integral constant expression
8920      flags.  */
8921   parser->type_definition_forbidden_message = saved_message;
8922   parser->integral_constant_expression_p
8923     = saved_integral_constant_expression_p;
8924   parser->non_integral_constant_expression_p
8925     = saved_non_integral_constant_expression_p;
8926
8927   if (expr == error_mark_node)
8928     {
8929       /* Skip everything up to the closing `)'.  */
8930       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8931                                              /*consume_paren=*/true);
8932       return error_mark_node;
8933     }
8934   
8935   /* Parse to the closing `)'.  */
8936   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8937     {
8938       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8939                                              /*consume_paren=*/true);
8940       return error_mark_node;
8941     }
8942
8943   return finish_decltype_type (expr, id_expression_or_member_access_p);
8944 }
8945
8946 /* Special member functions [gram.special] */
8947
8948 /* Parse a conversion-function-id.
8949
8950    conversion-function-id:
8951      operator conversion-type-id
8952
8953    Returns an IDENTIFIER_NODE representing the operator.  */
8954
8955 static tree
8956 cp_parser_conversion_function_id (cp_parser* parser)
8957 {
8958   tree type;
8959   tree saved_scope;
8960   tree saved_qualifying_scope;
8961   tree saved_object_scope;
8962   tree pushed_scope = NULL_TREE;
8963
8964   /* Look for the `operator' token.  */
8965   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8966     return error_mark_node;
8967   /* When we parse the conversion-type-id, the current scope will be
8968      reset.  However, we need that information in able to look up the
8969      conversion function later, so we save it here.  */
8970   saved_scope = parser->scope;
8971   saved_qualifying_scope = parser->qualifying_scope;
8972   saved_object_scope = parser->object_scope;
8973   /* We must enter the scope of the class so that the names of
8974      entities declared within the class are available in the
8975      conversion-type-id.  For example, consider:
8976
8977        struct S {
8978          typedef int I;
8979          operator I();
8980        };
8981
8982        S::operator I() { ... }
8983
8984      In order to see that `I' is a type-name in the definition, we
8985      must be in the scope of `S'.  */
8986   if (saved_scope)
8987     pushed_scope = push_scope (saved_scope);
8988   /* Parse the conversion-type-id.  */
8989   type = cp_parser_conversion_type_id (parser);
8990   /* Leave the scope of the class, if any.  */
8991   if (pushed_scope)
8992     pop_scope (pushed_scope);
8993   /* Restore the saved scope.  */
8994   parser->scope = saved_scope;
8995   parser->qualifying_scope = saved_qualifying_scope;
8996   parser->object_scope = saved_object_scope;
8997   /* If the TYPE is invalid, indicate failure.  */
8998   if (type == error_mark_node)
8999     return error_mark_node;
9000   return mangle_conv_op_name_for_type (type);
9001 }
9002
9003 /* Parse a conversion-type-id:
9004
9005    conversion-type-id:
9006      type-specifier-seq conversion-declarator [opt]
9007
9008    Returns the TYPE specified.  */
9009
9010 static tree
9011 cp_parser_conversion_type_id (cp_parser* parser)
9012 {
9013   tree attributes;
9014   cp_decl_specifier_seq type_specifiers;
9015   cp_declarator *declarator;
9016   tree type_specified;
9017
9018   /* Parse the attributes.  */
9019   attributes = cp_parser_attributes_opt (parser);
9020   /* Parse the type-specifiers.  */
9021   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9022                                 &type_specifiers);
9023   /* If that didn't work, stop.  */
9024   if (type_specifiers.type == error_mark_node)
9025     return error_mark_node;
9026   /* Parse the conversion-declarator.  */
9027   declarator = cp_parser_conversion_declarator_opt (parser);
9028
9029   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9030                                     /*initialized=*/0, &attributes);
9031   if (attributes)
9032     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9033
9034   /* Don't give this error when parsing tentatively.  This happens to
9035      work because we always parse this definitively once.  */
9036   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9037       && type_uses_auto (type_specified))
9038     {
9039       error ("invalid use of %<auto%> in conversion operator");
9040       return error_mark_node;
9041     }
9042
9043   return type_specified;
9044 }
9045
9046 /* Parse an (optional) conversion-declarator.
9047
9048    conversion-declarator:
9049      ptr-operator conversion-declarator [opt]
9050
9051    */
9052
9053 static cp_declarator *
9054 cp_parser_conversion_declarator_opt (cp_parser* parser)
9055 {
9056   enum tree_code code;
9057   tree class_type;
9058   cp_cv_quals cv_quals;
9059
9060   /* We don't know if there's a ptr-operator next, or not.  */
9061   cp_parser_parse_tentatively (parser);
9062   /* Try the ptr-operator.  */
9063   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9064   /* If it worked, look for more conversion-declarators.  */
9065   if (cp_parser_parse_definitely (parser))
9066     {
9067       cp_declarator *declarator;
9068
9069       /* Parse another optional declarator.  */
9070       declarator = cp_parser_conversion_declarator_opt (parser);
9071
9072       return cp_parser_make_indirect_declarator
9073         (code, class_type, cv_quals, declarator);
9074    }
9075
9076   return NULL;
9077 }
9078
9079 /* Parse an (optional) ctor-initializer.
9080
9081    ctor-initializer:
9082      : mem-initializer-list
9083
9084    Returns TRUE iff the ctor-initializer was actually present.  */
9085
9086 static bool
9087 cp_parser_ctor_initializer_opt (cp_parser* parser)
9088 {
9089   /* If the next token is not a `:', then there is no
9090      ctor-initializer.  */
9091   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9092     {
9093       /* Do default initialization of any bases and members.  */
9094       if (DECL_CONSTRUCTOR_P (current_function_decl))
9095         finish_mem_initializers (NULL_TREE);
9096
9097       return false;
9098     }
9099
9100   /* Consume the `:' token.  */
9101   cp_lexer_consume_token (parser->lexer);
9102   /* And the mem-initializer-list.  */
9103   cp_parser_mem_initializer_list (parser);
9104
9105   return true;
9106 }
9107
9108 /* Parse a mem-initializer-list.
9109
9110    mem-initializer-list:
9111      mem-initializer ... [opt]
9112      mem-initializer ... [opt] , mem-initializer-list  */
9113
9114 static void
9115 cp_parser_mem_initializer_list (cp_parser* parser)
9116 {
9117   tree mem_initializer_list = NULL_TREE;
9118   cp_token *token = cp_lexer_peek_token (parser->lexer);
9119
9120   /* Let the semantic analysis code know that we are starting the
9121      mem-initializer-list.  */
9122   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9123     error ("%Honly constructors take base initializers",
9124            &token->location);
9125
9126   /* Loop through the list.  */
9127   while (true)
9128     {
9129       tree mem_initializer;
9130
9131       token = cp_lexer_peek_token (parser->lexer);
9132       /* Parse the mem-initializer.  */
9133       mem_initializer = cp_parser_mem_initializer (parser);
9134       /* If the next token is a `...', we're expanding member initializers. */
9135       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9136         {
9137           /* Consume the `...'. */
9138           cp_lexer_consume_token (parser->lexer);
9139
9140           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9141              can be expanded but members cannot. */
9142           if (mem_initializer != error_mark_node
9143               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9144             {
9145               error ("%Hcannot expand initializer for member %<%D%>",
9146                      &token->location, TREE_PURPOSE (mem_initializer));
9147               mem_initializer = error_mark_node;
9148             }
9149
9150           /* Construct the pack expansion type. */
9151           if (mem_initializer != error_mark_node)
9152             mem_initializer = make_pack_expansion (mem_initializer);
9153         }
9154       /* Add it to the list, unless it was erroneous.  */
9155       if (mem_initializer != error_mark_node)
9156         {
9157           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9158           mem_initializer_list = mem_initializer;
9159         }
9160       /* If the next token is not a `,', we're done.  */
9161       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9162         break;
9163       /* Consume the `,' token.  */
9164       cp_lexer_consume_token (parser->lexer);
9165     }
9166
9167   /* Perform semantic analysis.  */
9168   if (DECL_CONSTRUCTOR_P (current_function_decl))
9169     finish_mem_initializers (mem_initializer_list);
9170 }
9171
9172 /* Parse a mem-initializer.
9173
9174    mem-initializer:
9175      mem-initializer-id ( expression-list [opt] )
9176      mem-initializer-id braced-init-list
9177
9178    GNU extension:
9179
9180    mem-initializer:
9181      ( expression-list [opt] )
9182
9183    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9184    class) or FIELD_DECL (for a non-static data member) to initialize;
9185    the TREE_VALUE is the expression-list.  An empty initialization
9186    list is represented by void_list_node.  */
9187
9188 static tree
9189 cp_parser_mem_initializer (cp_parser* parser)
9190 {
9191   tree mem_initializer_id;
9192   tree expression_list;
9193   tree member;
9194   cp_token *token = cp_lexer_peek_token (parser->lexer);
9195
9196   /* Find out what is being initialized.  */
9197   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9198     {
9199       permerror (token->location,
9200                  "anachronistic old-style base class initializer");
9201       mem_initializer_id = NULL_TREE;
9202     }
9203   else
9204     {
9205       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9206       if (mem_initializer_id == error_mark_node)
9207         return mem_initializer_id;
9208     }
9209   member = expand_member_init (mem_initializer_id);
9210   if (member && !DECL_P (member))
9211     in_base_initializer = 1;
9212
9213   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9214     {
9215       bool expr_non_constant_p;
9216       maybe_warn_cpp0x ("extended initializer lists");
9217       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9218       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9219       expression_list = build_tree_list (NULL_TREE, expression_list);
9220     }
9221   else
9222     expression_list
9223       = cp_parser_parenthesized_expression_list (parser, false,
9224                                                  /*cast_p=*/false,
9225                                                  /*allow_expansion_p=*/true,
9226                                                  /*non_constant_p=*/NULL);
9227   if (expression_list == error_mark_node)
9228     return error_mark_node;
9229   if (!expression_list)
9230     expression_list = void_type_node;
9231
9232   in_base_initializer = 0;
9233
9234   return member ? build_tree_list (member, expression_list) : error_mark_node;
9235 }
9236
9237 /* Parse a mem-initializer-id.
9238
9239    mem-initializer-id:
9240      :: [opt] nested-name-specifier [opt] class-name
9241      identifier
9242
9243    Returns a TYPE indicating the class to be initializer for the first
9244    production.  Returns an IDENTIFIER_NODE indicating the data member
9245    to be initialized for the second production.  */
9246
9247 static tree
9248 cp_parser_mem_initializer_id (cp_parser* parser)
9249 {
9250   bool global_scope_p;
9251   bool nested_name_specifier_p;
9252   bool template_p = false;
9253   tree id;
9254
9255   cp_token *token = cp_lexer_peek_token (parser->lexer);
9256
9257   /* `typename' is not allowed in this context ([temp.res]).  */
9258   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9259     {
9260       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9261              "member initializer is implicitly a type)",
9262              &token->location);
9263       cp_lexer_consume_token (parser->lexer);
9264     }
9265   /* Look for the optional `::' operator.  */
9266   global_scope_p
9267     = (cp_parser_global_scope_opt (parser,
9268                                    /*current_scope_valid_p=*/false)
9269        != NULL_TREE);
9270   /* Look for the optional nested-name-specifier.  The simplest way to
9271      implement:
9272
9273        [temp.res]
9274
9275        The keyword `typename' is not permitted in a base-specifier or
9276        mem-initializer; in these contexts a qualified name that
9277        depends on a template-parameter is implicitly assumed to be a
9278        type name.
9279
9280      is to assume that we have seen the `typename' keyword at this
9281      point.  */
9282   nested_name_specifier_p
9283     = (cp_parser_nested_name_specifier_opt (parser,
9284                                             /*typename_keyword_p=*/true,
9285                                             /*check_dependency_p=*/true,
9286                                             /*type_p=*/true,
9287                                             /*is_declaration=*/true)
9288        != NULL_TREE);
9289   if (nested_name_specifier_p)
9290     template_p = cp_parser_optional_template_keyword (parser);
9291   /* If there is a `::' operator or a nested-name-specifier, then we
9292      are definitely looking for a class-name.  */
9293   if (global_scope_p || nested_name_specifier_p)
9294     return cp_parser_class_name (parser,
9295                                  /*typename_keyword_p=*/true,
9296                                  /*template_keyword_p=*/template_p,
9297                                  none_type,
9298                                  /*check_dependency_p=*/true,
9299                                  /*class_head_p=*/false,
9300                                  /*is_declaration=*/true);
9301   /* Otherwise, we could also be looking for an ordinary identifier.  */
9302   cp_parser_parse_tentatively (parser);
9303   /* Try a class-name.  */
9304   id = cp_parser_class_name (parser,
9305                              /*typename_keyword_p=*/true,
9306                              /*template_keyword_p=*/false,
9307                              none_type,
9308                              /*check_dependency_p=*/true,
9309                              /*class_head_p=*/false,
9310                              /*is_declaration=*/true);
9311   /* If we found one, we're done.  */
9312   if (cp_parser_parse_definitely (parser))
9313     return id;
9314   /* Otherwise, look for an ordinary identifier.  */
9315   return cp_parser_identifier (parser);
9316 }
9317
9318 /* Overloading [gram.over] */
9319
9320 /* Parse an operator-function-id.
9321
9322    operator-function-id:
9323      operator operator
9324
9325    Returns an IDENTIFIER_NODE for the operator which is a
9326    human-readable spelling of the identifier, e.g., `operator +'.  */
9327
9328 static tree
9329 cp_parser_operator_function_id (cp_parser* parser)
9330 {
9331   /* Look for the `operator' keyword.  */
9332   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9333     return error_mark_node;
9334   /* And then the name of the operator itself.  */
9335   return cp_parser_operator (parser);
9336 }
9337
9338 /* Parse an operator.
9339
9340    operator:
9341      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9342      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9343      || ++ -- , ->* -> () []
9344
9345    GNU Extensions:
9346
9347    operator:
9348      <? >? <?= >?=
9349
9350    Returns an IDENTIFIER_NODE for the operator which is a
9351    human-readable spelling of the identifier, e.g., `operator +'.  */
9352
9353 static tree
9354 cp_parser_operator (cp_parser* parser)
9355 {
9356   tree id = NULL_TREE;
9357   cp_token *token;
9358
9359   /* Peek at the next token.  */
9360   token = cp_lexer_peek_token (parser->lexer);
9361   /* Figure out which operator we have.  */
9362   switch (token->type)
9363     {
9364     case CPP_KEYWORD:
9365       {
9366         enum tree_code op;
9367
9368         /* The keyword should be either `new' or `delete'.  */
9369         if (token->keyword == RID_NEW)
9370           op = NEW_EXPR;
9371         else if (token->keyword == RID_DELETE)
9372           op = DELETE_EXPR;
9373         else
9374           break;
9375
9376         /* Consume the `new' or `delete' token.  */
9377         cp_lexer_consume_token (parser->lexer);
9378
9379         /* Peek at the next token.  */
9380         token = cp_lexer_peek_token (parser->lexer);
9381         /* If it's a `[' token then this is the array variant of the
9382            operator.  */
9383         if (token->type == CPP_OPEN_SQUARE)
9384           {
9385             /* Consume the `[' token.  */
9386             cp_lexer_consume_token (parser->lexer);
9387             /* Look for the `]' token.  */
9388             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9389             id = ansi_opname (op == NEW_EXPR
9390                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9391           }
9392         /* Otherwise, we have the non-array variant.  */
9393         else
9394           id = ansi_opname (op);
9395
9396         return id;
9397       }
9398
9399     case CPP_PLUS:
9400       id = ansi_opname (PLUS_EXPR);
9401       break;
9402
9403     case CPP_MINUS:
9404       id = ansi_opname (MINUS_EXPR);
9405       break;
9406
9407     case CPP_MULT:
9408       id = ansi_opname (MULT_EXPR);
9409       break;
9410
9411     case CPP_DIV:
9412       id = ansi_opname (TRUNC_DIV_EXPR);
9413       break;
9414
9415     case CPP_MOD:
9416       id = ansi_opname (TRUNC_MOD_EXPR);
9417       break;
9418
9419     case CPP_XOR:
9420       id = ansi_opname (BIT_XOR_EXPR);
9421       break;
9422
9423     case CPP_AND:
9424       id = ansi_opname (BIT_AND_EXPR);
9425       break;
9426
9427     case CPP_OR:
9428       id = ansi_opname (BIT_IOR_EXPR);
9429       break;
9430
9431     case CPP_COMPL:
9432       id = ansi_opname (BIT_NOT_EXPR);
9433       break;
9434
9435     case CPP_NOT:
9436       id = ansi_opname (TRUTH_NOT_EXPR);
9437       break;
9438
9439     case CPP_EQ:
9440       id = ansi_assopname (NOP_EXPR);
9441       break;
9442
9443     case CPP_LESS:
9444       id = ansi_opname (LT_EXPR);
9445       break;
9446
9447     case CPP_GREATER:
9448       id = ansi_opname (GT_EXPR);
9449       break;
9450
9451     case CPP_PLUS_EQ:
9452       id = ansi_assopname (PLUS_EXPR);
9453       break;
9454
9455     case CPP_MINUS_EQ:
9456       id = ansi_assopname (MINUS_EXPR);
9457       break;
9458
9459     case CPP_MULT_EQ:
9460       id = ansi_assopname (MULT_EXPR);
9461       break;
9462
9463     case CPP_DIV_EQ:
9464       id = ansi_assopname (TRUNC_DIV_EXPR);
9465       break;
9466
9467     case CPP_MOD_EQ:
9468       id = ansi_assopname (TRUNC_MOD_EXPR);
9469       break;
9470
9471     case CPP_XOR_EQ:
9472       id = ansi_assopname (BIT_XOR_EXPR);
9473       break;
9474
9475     case CPP_AND_EQ:
9476       id = ansi_assopname (BIT_AND_EXPR);
9477       break;
9478
9479     case CPP_OR_EQ:
9480       id = ansi_assopname (BIT_IOR_EXPR);
9481       break;
9482
9483     case CPP_LSHIFT:
9484       id = ansi_opname (LSHIFT_EXPR);
9485       break;
9486
9487     case CPP_RSHIFT:
9488       id = ansi_opname (RSHIFT_EXPR);
9489       break;
9490
9491     case CPP_LSHIFT_EQ:
9492       id = ansi_assopname (LSHIFT_EXPR);
9493       break;
9494
9495     case CPP_RSHIFT_EQ:
9496       id = ansi_assopname (RSHIFT_EXPR);
9497       break;
9498
9499     case CPP_EQ_EQ:
9500       id = ansi_opname (EQ_EXPR);
9501       break;
9502
9503     case CPP_NOT_EQ:
9504       id = ansi_opname (NE_EXPR);
9505       break;
9506
9507     case CPP_LESS_EQ:
9508       id = ansi_opname (LE_EXPR);
9509       break;
9510
9511     case CPP_GREATER_EQ:
9512       id = ansi_opname (GE_EXPR);
9513       break;
9514
9515     case CPP_AND_AND:
9516       id = ansi_opname (TRUTH_ANDIF_EXPR);
9517       break;
9518
9519     case CPP_OR_OR:
9520       id = ansi_opname (TRUTH_ORIF_EXPR);
9521       break;
9522
9523     case CPP_PLUS_PLUS:
9524       id = ansi_opname (POSTINCREMENT_EXPR);
9525       break;
9526
9527     case CPP_MINUS_MINUS:
9528       id = ansi_opname (PREDECREMENT_EXPR);
9529       break;
9530
9531     case CPP_COMMA:
9532       id = ansi_opname (COMPOUND_EXPR);
9533       break;
9534
9535     case CPP_DEREF_STAR:
9536       id = ansi_opname (MEMBER_REF);
9537       break;
9538
9539     case CPP_DEREF:
9540       id = ansi_opname (COMPONENT_REF);
9541       break;
9542
9543     case CPP_OPEN_PAREN:
9544       /* Consume the `('.  */
9545       cp_lexer_consume_token (parser->lexer);
9546       /* Look for the matching `)'.  */
9547       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9548       return ansi_opname (CALL_EXPR);
9549
9550     case CPP_OPEN_SQUARE:
9551       /* Consume the `['.  */
9552       cp_lexer_consume_token (parser->lexer);
9553       /* Look for the matching `]'.  */
9554       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9555       return ansi_opname (ARRAY_REF);
9556
9557     default:
9558       /* Anything else is an error.  */
9559       break;
9560     }
9561
9562   /* If we have selected an identifier, we need to consume the
9563      operator token.  */
9564   if (id)
9565     cp_lexer_consume_token (parser->lexer);
9566   /* Otherwise, no valid operator name was present.  */
9567   else
9568     {
9569       cp_parser_error (parser, "expected operator");
9570       id = error_mark_node;
9571     }
9572
9573   return id;
9574 }
9575
9576 /* Parse a template-declaration.
9577
9578    template-declaration:
9579      export [opt] template < template-parameter-list > declaration
9580
9581    If MEMBER_P is TRUE, this template-declaration occurs within a
9582    class-specifier.
9583
9584    The grammar rule given by the standard isn't correct.  What
9585    is really meant is:
9586
9587    template-declaration:
9588      export [opt] template-parameter-list-seq
9589        decl-specifier-seq [opt] init-declarator [opt] ;
9590      export [opt] template-parameter-list-seq
9591        function-definition
9592
9593    template-parameter-list-seq:
9594      template-parameter-list-seq [opt]
9595      template < template-parameter-list >  */
9596
9597 static void
9598 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9599 {
9600   /* Check for `export'.  */
9601   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9602     {
9603       /* Consume the `export' token.  */
9604       cp_lexer_consume_token (parser->lexer);
9605       /* Warn that we do not support `export'.  */
9606       warning (0, "keyword %<export%> not implemented, and will be ignored");
9607     }
9608
9609   cp_parser_template_declaration_after_export (parser, member_p);
9610 }
9611
9612 /* Parse a template-parameter-list.
9613
9614    template-parameter-list:
9615      template-parameter
9616      template-parameter-list , template-parameter
9617
9618    Returns a TREE_LIST.  Each node represents a template parameter.
9619    The nodes are connected via their TREE_CHAINs.  */
9620
9621 static tree
9622 cp_parser_template_parameter_list (cp_parser* parser)
9623 {
9624   tree parameter_list = NULL_TREE;
9625
9626   begin_template_parm_list ();
9627   while (true)
9628     {
9629       tree parameter;
9630       bool is_non_type;
9631       bool is_parameter_pack;
9632
9633       /* Parse the template-parameter.  */
9634       parameter = cp_parser_template_parameter (parser, 
9635                                                 &is_non_type,
9636                                                 &is_parameter_pack);
9637       /* Add it to the list.  */
9638       if (parameter != error_mark_node)
9639         parameter_list = process_template_parm (parameter_list,
9640                                                 parameter,
9641                                                 is_non_type,
9642                                                 is_parameter_pack);
9643       else
9644        {
9645          tree err_parm = build_tree_list (parameter, parameter);
9646          TREE_VALUE (err_parm) = error_mark_node;
9647          parameter_list = chainon (parameter_list, err_parm);
9648        }
9649
9650       /* If the next token is not a `,', we're done.  */
9651       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9652         break;
9653       /* Otherwise, consume the `,' token.  */
9654       cp_lexer_consume_token (parser->lexer);
9655     }
9656
9657   return end_template_parm_list (parameter_list);
9658 }
9659
9660 /* Parse a template-parameter.
9661
9662    template-parameter:
9663      type-parameter
9664      parameter-declaration
9665
9666    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9667    the parameter.  The TREE_PURPOSE is the default value, if any.
9668    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9669    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9670    set to true iff this parameter is a parameter pack. */
9671
9672 static tree
9673 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9674                               bool *is_parameter_pack)
9675 {
9676   cp_token *token;
9677   cp_parameter_declarator *parameter_declarator;
9678   cp_declarator *id_declarator;
9679   tree parm;
9680
9681   /* Assume it is a type parameter or a template parameter.  */
9682   *is_non_type = false;
9683   /* Assume it not a parameter pack. */
9684   *is_parameter_pack = false;
9685   /* Peek at the next token.  */
9686   token = cp_lexer_peek_token (parser->lexer);
9687   /* If it is `class' or `template', we have a type-parameter.  */
9688   if (token->keyword == RID_TEMPLATE)
9689     return cp_parser_type_parameter (parser, is_parameter_pack);
9690   /* If it is `class' or `typename' we do not know yet whether it is a
9691      type parameter or a non-type parameter.  Consider:
9692
9693        template <typename T, typename T::X X> ...
9694
9695      or:
9696
9697        template <class C, class D*> ...
9698
9699      Here, the first parameter is a type parameter, and the second is
9700      a non-type parameter.  We can tell by looking at the token after
9701      the identifier -- if it is a `,', `=', or `>' then we have a type
9702      parameter.  */
9703   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9704     {
9705       /* Peek at the token after `class' or `typename'.  */
9706       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9707       /* If it's an ellipsis, we have a template type parameter
9708          pack. */
9709       if (token->type == CPP_ELLIPSIS)
9710         return cp_parser_type_parameter (parser, is_parameter_pack);
9711       /* If it's an identifier, skip it.  */
9712       if (token->type == CPP_NAME)
9713         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9714       /* Now, see if the token looks like the end of a template
9715          parameter.  */
9716       if (token->type == CPP_COMMA
9717           || token->type == CPP_EQ
9718           || token->type == CPP_GREATER)
9719         return cp_parser_type_parameter (parser, is_parameter_pack);
9720     }
9721
9722   /* Otherwise, it is a non-type parameter.
9723
9724      [temp.param]
9725
9726      When parsing a default template-argument for a non-type
9727      template-parameter, the first non-nested `>' is taken as the end
9728      of the template parameter-list rather than a greater-than
9729      operator.  */
9730   *is_non_type = true;
9731   parameter_declarator
9732      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9733                                         /*parenthesized_p=*/NULL);
9734
9735   /* If the parameter declaration is marked as a parameter pack, set
9736      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9737      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9738      grokdeclarator. */
9739   if (parameter_declarator
9740       && parameter_declarator->declarator
9741       && parameter_declarator->declarator->parameter_pack_p)
9742     {
9743       *is_parameter_pack = true;
9744       parameter_declarator->declarator->parameter_pack_p = false;
9745     }
9746
9747   /* If the next token is an ellipsis, and we don't already have it
9748      marked as a parameter pack, then we have a parameter pack (that
9749      has no declarator).  */
9750   if (!*is_parameter_pack
9751       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9752       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9753     {
9754       /* Consume the `...'.  */
9755       cp_lexer_consume_token (parser->lexer);
9756       maybe_warn_variadic_templates ();
9757       
9758       *is_parameter_pack = true;
9759     }
9760   /* We might end up with a pack expansion as the type of the non-type
9761      template parameter, in which case this is a non-type template
9762      parameter pack.  */
9763   else if (parameter_declarator
9764            && parameter_declarator->decl_specifiers.type
9765            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9766     {
9767       *is_parameter_pack = true;
9768       parameter_declarator->decl_specifiers.type = 
9769         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9770     }
9771
9772   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9773     {
9774       /* Parameter packs cannot have default arguments.  However, a
9775          user may try to do so, so we'll parse them and give an
9776          appropriate diagnostic here.  */
9777
9778       /* Consume the `='.  */
9779       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9780       cp_lexer_consume_token (parser->lexer);
9781       
9782       /* Find the name of the parameter pack.  */     
9783       id_declarator = parameter_declarator->declarator;
9784       while (id_declarator && id_declarator->kind != cdk_id)
9785         id_declarator = id_declarator->declarator;
9786       
9787       if (id_declarator && id_declarator->kind == cdk_id)
9788         error ("%Htemplate parameter pack %qD cannot have a default argument",
9789                &start_token->location, id_declarator->u.id.unqualified_name);
9790       else
9791         error ("%Htemplate parameter pack cannot have a default argument",
9792                &start_token->location);
9793       
9794       /* Parse the default argument, but throw away the result.  */
9795       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9796     }
9797
9798   parm = grokdeclarator (parameter_declarator->declarator,
9799                          &parameter_declarator->decl_specifiers,
9800                          PARM, /*initialized=*/0,
9801                          /*attrlist=*/NULL);
9802   if (parm == error_mark_node)
9803     return error_mark_node;
9804
9805   return build_tree_list (parameter_declarator->default_argument, parm);
9806 }
9807
9808 /* Parse a type-parameter.
9809
9810    type-parameter:
9811      class identifier [opt]
9812      class identifier [opt] = type-id
9813      typename identifier [opt]
9814      typename identifier [opt] = type-id
9815      template < template-parameter-list > class identifier [opt]
9816      template < template-parameter-list > class identifier [opt]
9817        = id-expression
9818
9819    GNU Extension (variadic templates):
9820
9821    type-parameter:
9822      class ... identifier [opt]
9823      typename ... identifier [opt]
9824
9825    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9826    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9827    the declaration of the parameter.
9828
9829    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9830
9831 static tree
9832 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9833 {
9834   cp_token *token;
9835   tree parameter;
9836
9837   /* Look for a keyword to tell us what kind of parameter this is.  */
9838   token = cp_parser_require (parser, CPP_KEYWORD,
9839                              "%<class%>, %<typename%>, or %<template%>");
9840   if (!token)
9841     return error_mark_node;
9842
9843   switch (token->keyword)
9844     {
9845     case RID_CLASS:
9846     case RID_TYPENAME:
9847       {
9848         tree identifier;
9849         tree default_argument;
9850
9851         /* If the next token is an ellipsis, we have a template
9852            argument pack. */
9853         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9854           {
9855             /* Consume the `...' token. */
9856             cp_lexer_consume_token (parser->lexer);
9857             maybe_warn_variadic_templates ();
9858
9859             *is_parameter_pack = true;
9860           }
9861
9862         /* If the next token is an identifier, then it names the
9863            parameter.  */
9864         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9865           identifier = cp_parser_identifier (parser);
9866         else
9867           identifier = NULL_TREE;
9868
9869         /* Create the parameter.  */
9870         parameter = finish_template_type_parm (class_type_node, identifier);
9871
9872         /* If the next token is an `=', we have a default argument.  */
9873         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9874           {
9875             /* Consume the `=' token.  */
9876             cp_lexer_consume_token (parser->lexer);
9877             /* Parse the default-argument.  */
9878             push_deferring_access_checks (dk_no_deferred);
9879             default_argument = cp_parser_type_id (parser);
9880
9881             /* Template parameter packs cannot have default
9882                arguments. */
9883             if (*is_parameter_pack)
9884               {
9885                 if (identifier)
9886                   error ("%Htemplate parameter pack %qD cannot have a "
9887                          "default argument", &token->location, identifier);
9888                 else
9889                   error ("%Htemplate parameter packs cannot have "
9890                          "default arguments", &token->location);
9891                 default_argument = NULL_TREE;
9892               }
9893             pop_deferring_access_checks ();
9894           }
9895         else
9896           default_argument = NULL_TREE;
9897
9898         /* Create the combined representation of the parameter and the
9899            default argument.  */
9900         parameter = build_tree_list (default_argument, parameter);
9901       }
9902       break;
9903
9904     case RID_TEMPLATE:
9905       {
9906         tree parameter_list;
9907         tree identifier;
9908         tree default_argument;
9909
9910         /* Look for the `<'.  */
9911         cp_parser_require (parser, CPP_LESS, "%<<%>");
9912         /* Parse the template-parameter-list.  */
9913         parameter_list = cp_parser_template_parameter_list (parser);
9914         /* Look for the `>'.  */
9915         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9916         /* Look for the `class' keyword.  */
9917         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9918         /* If the next token is an ellipsis, we have a template
9919            argument pack. */
9920         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9921           {
9922             /* Consume the `...' token. */
9923             cp_lexer_consume_token (parser->lexer);
9924             maybe_warn_variadic_templates ();
9925
9926             *is_parameter_pack = true;
9927           }
9928         /* If the next token is an `=', then there is a
9929            default-argument.  If the next token is a `>', we are at
9930            the end of the parameter-list.  If the next token is a `,',
9931            then we are at the end of this parameter.  */
9932         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9933             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9934             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9935           {
9936             identifier = cp_parser_identifier (parser);
9937             /* Treat invalid names as if the parameter were nameless.  */
9938             if (identifier == error_mark_node)
9939               identifier = NULL_TREE;
9940           }
9941         else
9942           identifier = NULL_TREE;
9943
9944         /* Create the template parameter.  */
9945         parameter = finish_template_template_parm (class_type_node,
9946                                                    identifier);
9947
9948         /* If the next token is an `=', then there is a
9949            default-argument.  */
9950         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9951           {
9952             bool is_template;
9953
9954             /* Consume the `='.  */
9955             cp_lexer_consume_token (parser->lexer);
9956             /* Parse the id-expression.  */
9957             push_deferring_access_checks (dk_no_deferred);
9958             /* save token before parsing the id-expression, for error
9959                reporting */
9960             token = cp_lexer_peek_token (parser->lexer);
9961             default_argument
9962               = cp_parser_id_expression (parser,
9963                                          /*template_keyword_p=*/false,
9964                                          /*check_dependency_p=*/true,
9965                                          /*template_p=*/&is_template,
9966                                          /*declarator_p=*/false,
9967                                          /*optional_p=*/false);
9968             if (TREE_CODE (default_argument) == TYPE_DECL)
9969               /* If the id-expression was a template-id that refers to
9970                  a template-class, we already have the declaration here,
9971                  so no further lookup is needed.  */
9972                  ;
9973             else
9974               /* Look up the name.  */
9975               default_argument
9976                 = cp_parser_lookup_name (parser, default_argument,
9977                                          none_type,
9978                                          /*is_template=*/is_template,
9979                                          /*is_namespace=*/false,
9980                                          /*check_dependency=*/true,
9981                                          /*ambiguous_decls=*/NULL,
9982                                          token->location);
9983             /* See if the default argument is valid.  */
9984             default_argument
9985               = check_template_template_default_arg (default_argument);
9986
9987             /* Template parameter packs cannot have default
9988                arguments. */
9989             if (*is_parameter_pack)
9990               {
9991                 if (identifier)
9992                   error ("%Htemplate parameter pack %qD cannot "
9993                          "have a default argument",
9994                          &token->location, identifier);
9995                 else
9996                   error ("%Htemplate parameter packs cannot "
9997                          "have default arguments",
9998                          &token->location);
9999                 default_argument = NULL_TREE;
10000               }
10001             pop_deferring_access_checks ();
10002           }
10003         else
10004           default_argument = NULL_TREE;
10005
10006         /* Create the combined representation of the parameter and the
10007            default argument.  */
10008         parameter = build_tree_list (default_argument, parameter);
10009       }
10010       break;
10011
10012     default:
10013       gcc_unreachable ();
10014       break;
10015     }
10016
10017   return parameter;
10018 }
10019
10020 /* Parse a template-id.
10021
10022    template-id:
10023      template-name < template-argument-list [opt] >
10024
10025    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10026    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10027    returned.  Otherwise, if the template-name names a function, or set
10028    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10029    names a class, returns a TYPE_DECL for the specialization.
10030
10031    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10032    uninstantiated templates.  */
10033
10034 static tree
10035 cp_parser_template_id (cp_parser *parser,
10036                        bool template_keyword_p,
10037                        bool check_dependency_p,
10038                        bool is_declaration)
10039 {
10040   int i;
10041   tree templ;
10042   tree arguments;
10043   tree template_id;
10044   cp_token_position start_of_id = 0;
10045   deferred_access_check *chk;
10046   VEC (deferred_access_check,gc) *access_check;
10047   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10048   bool is_identifier;
10049
10050   /* If the next token corresponds to a template-id, there is no need
10051      to reparse it.  */
10052   next_token = cp_lexer_peek_token (parser->lexer);
10053   if (next_token->type == CPP_TEMPLATE_ID)
10054     {
10055       struct tree_check *check_value;
10056
10057       /* Get the stored value.  */
10058       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10059       /* Perform any access checks that were deferred.  */
10060       access_check = check_value->checks;
10061       if (access_check)
10062         {
10063           for (i = 0 ;
10064                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10065                ++i)
10066             {
10067               perform_or_defer_access_check (chk->binfo,
10068                                              chk->decl,
10069                                              chk->diag_decl);
10070             }
10071         }
10072       /* Return the stored value.  */
10073       return check_value->value;
10074     }
10075
10076   /* Avoid performing name lookup if there is no possibility of
10077      finding a template-id.  */
10078   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10079       || (next_token->type == CPP_NAME
10080           && !cp_parser_nth_token_starts_template_argument_list_p
10081                (parser, 2)))
10082     {
10083       cp_parser_error (parser, "expected template-id");
10084       return error_mark_node;
10085     }
10086
10087   /* Remember where the template-id starts.  */
10088   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10089     start_of_id = cp_lexer_token_position (parser->lexer, false);
10090
10091   push_deferring_access_checks (dk_deferred);
10092
10093   /* Parse the template-name.  */
10094   is_identifier = false;
10095   token = cp_lexer_peek_token (parser->lexer);
10096   templ = cp_parser_template_name (parser, template_keyword_p,
10097                                    check_dependency_p,
10098                                    is_declaration,
10099                                    &is_identifier);
10100   if (templ == error_mark_node || is_identifier)
10101     {
10102       pop_deferring_access_checks ();
10103       return templ;
10104     }
10105
10106   /* If we find the sequence `[:' after a template-name, it's probably
10107      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10108      parse correctly the argument list.  */
10109   next_token = cp_lexer_peek_token (parser->lexer);
10110   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10111   if (next_token->type == CPP_OPEN_SQUARE
10112       && next_token->flags & DIGRAPH
10113       && next_token_2->type == CPP_COLON
10114       && !(next_token_2->flags & PREV_WHITE))
10115     {
10116       cp_parser_parse_tentatively (parser);
10117       /* Change `:' into `::'.  */
10118       next_token_2->type = CPP_SCOPE;
10119       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10120          CPP_LESS.  */
10121       cp_lexer_consume_token (parser->lexer);
10122
10123       /* Parse the arguments.  */
10124       arguments = cp_parser_enclosed_template_argument_list (parser);
10125       if (!cp_parser_parse_definitely (parser))
10126         {
10127           /* If we couldn't parse an argument list, then we revert our changes
10128              and return simply an error. Maybe this is not a template-id
10129              after all.  */
10130           next_token_2->type = CPP_COLON;
10131           cp_parser_error (parser, "expected %<<%>");
10132           pop_deferring_access_checks ();
10133           return error_mark_node;
10134         }
10135       /* Otherwise, emit an error about the invalid digraph, but continue
10136          parsing because we got our argument list.  */
10137       if (permerror (next_token->location,
10138                      "%<<::%> cannot begin a template-argument list"))
10139         {
10140           static bool hint = false;
10141           inform (next_token->location,
10142                   "%<<:%> is an alternate spelling for %<[%>."
10143                   " Insert whitespace between %<<%> and %<::%>");
10144           if (!hint && !flag_permissive)
10145             {
10146               inform (next_token->location, "(if you use %<-fpermissive%>"
10147                       " G++ will accept your code)");
10148               hint = true;
10149             }
10150         }
10151     }
10152   else
10153     {
10154       /* Look for the `<' that starts the template-argument-list.  */
10155       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10156         {
10157           pop_deferring_access_checks ();
10158           return error_mark_node;
10159         }
10160       /* Parse the arguments.  */
10161       arguments = cp_parser_enclosed_template_argument_list (parser);
10162     }
10163
10164   /* Build a representation of the specialization.  */
10165   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10166     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10167   else if (DECL_CLASS_TEMPLATE_P (templ)
10168            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10169     {
10170       bool entering_scope;
10171       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10172          template (rather than some instantiation thereof) only if
10173          is not nested within some other construct.  For example, in
10174          "template <typename T> void f(T) { A<T>::", A<T> is just an
10175          instantiation of A.  */
10176       entering_scope = (template_parm_scope_p ()
10177                         && cp_lexer_next_token_is (parser->lexer,
10178                                                    CPP_SCOPE));
10179       template_id
10180         = finish_template_type (templ, arguments, entering_scope);
10181     }
10182   else
10183     {
10184       /* If it's not a class-template or a template-template, it should be
10185          a function-template.  */
10186       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10187                    || TREE_CODE (templ) == OVERLOAD
10188                    || BASELINK_P (templ)));
10189
10190       template_id = lookup_template_function (templ, arguments);
10191     }
10192
10193   /* If parsing tentatively, replace the sequence of tokens that makes
10194      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10195      should we re-parse the token stream, we will not have to repeat
10196      the effort required to do the parse, nor will we issue duplicate
10197      error messages about problems during instantiation of the
10198      template.  */
10199   if (start_of_id)
10200     {
10201       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10202
10203       /* Reset the contents of the START_OF_ID token.  */
10204       token->type = CPP_TEMPLATE_ID;
10205       /* Retrieve any deferred checks.  Do not pop this access checks yet
10206          so the memory will not be reclaimed during token replacing below.  */
10207       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10208       token->u.tree_check_value->value = template_id;
10209       token->u.tree_check_value->checks = get_deferred_access_checks ();
10210       token->keyword = RID_MAX;
10211
10212       /* Purge all subsequent tokens.  */
10213       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10214
10215       /* ??? Can we actually assume that, if template_id ==
10216          error_mark_node, we will have issued a diagnostic to the
10217          user, as opposed to simply marking the tentative parse as
10218          failed?  */
10219       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10220         error ("%Hparse error in template argument list",
10221                &token->location);
10222     }
10223
10224   pop_deferring_access_checks ();
10225   return template_id;
10226 }
10227
10228 /* Parse a template-name.
10229
10230    template-name:
10231      identifier
10232
10233    The standard should actually say:
10234
10235    template-name:
10236      identifier
10237      operator-function-id
10238
10239    A defect report has been filed about this issue.
10240
10241    A conversion-function-id cannot be a template name because they cannot
10242    be part of a template-id. In fact, looking at this code:
10243
10244    a.operator K<int>()
10245
10246    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10247    It is impossible to call a templated conversion-function-id with an
10248    explicit argument list, since the only allowed template parameter is
10249    the type to which it is converting.
10250
10251    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10252    `template' keyword, in a construction like:
10253
10254      T::template f<3>()
10255
10256    In that case `f' is taken to be a template-name, even though there
10257    is no way of knowing for sure.
10258
10259    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10260    name refers to a set of overloaded functions, at least one of which
10261    is a template, or an IDENTIFIER_NODE with the name of the template,
10262    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10263    names are looked up inside uninstantiated templates.  */
10264
10265 static tree
10266 cp_parser_template_name (cp_parser* parser,
10267                          bool template_keyword_p,
10268                          bool check_dependency_p,
10269                          bool is_declaration,
10270                          bool *is_identifier)
10271 {
10272   tree identifier;
10273   tree decl;
10274   tree fns;
10275   cp_token *token = cp_lexer_peek_token (parser->lexer);
10276
10277   /* If the next token is `operator', then we have either an
10278      operator-function-id or a conversion-function-id.  */
10279   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10280     {
10281       /* We don't know whether we're looking at an
10282          operator-function-id or a conversion-function-id.  */
10283       cp_parser_parse_tentatively (parser);
10284       /* Try an operator-function-id.  */
10285       identifier = cp_parser_operator_function_id (parser);
10286       /* If that didn't work, try a conversion-function-id.  */
10287       if (!cp_parser_parse_definitely (parser))
10288         {
10289           cp_parser_error (parser, "expected template-name");
10290           return error_mark_node;
10291         }
10292     }
10293   /* Look for the identifier.  */
10294   else
10295     identifier = cp_parser_identifier (parser);
10296
10297   /* If we didn't find an identifier, we don't have a template-id.  */
10298   if (identifier == error_mark_node)
10299     return error_mark_node;
10300
10301   /* If the name immediately followed the `template' keyword, then it
10302      is a template-name.  However, if the next token is not `<', then
10303      we do not treat it as a template-name, since it is not being used
10304      as part of a template-id.  This enables us to handle constructs
10305      like:
10306
10307        template <typename T> struct S { S(); };
10308        template <typename T> S<T>::S();
10309
10310      correctly.  We would treat `S' as a template -- if it were `S<T>'
10311      -- but we do not if there is no `<'.  */
10312
10313   if (processing_template_decl
10314       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10315     {
10316       /* In a declaration, in a dependent context, we pretend that the
10317          "template" keyword was present in order to improve error
10318          recovery.  For example, given:
10319
10320            template <typename T> void f(T::X<int>);
10321
10322          we want to treat "X<int>" as a template-id.  */
10323       if (is_declaration
10324           && !template_keyword_p
10325           && parser->scope && TYPE_P (parser->scope)
10326           && check_dependency_p
10327           && dependent_scope_p (parser->scope)
10328           /* Do not do this for dtors (or ctors), since they never
10329              need the template keyword before their name.  */
10330           && !constructor_name_p (identifier, parser->scope))
10331         {
10332           cp_token_position start = 0;
10333
10334           /* Explain what went wrong.  */
10335           error ("%Hnon-template %qD used as template",
10336                  &token->location, identifier);
10337           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10338                   parser->scope, identifier);
10339           /* If parsing tentatively, find the location of the "<" token.  */
10340           if (cp_parser_simulate_error (parser))
10341             start = cp_lexer_token_position (parser->lexer, true);
10342           /* Parse the template arguments so that we can issue error
10343              messages about them.  */
10344           cp_lexer_consume_token (parser->lexer);
10345           cp_parser_enclosed_template_argument_list (parser);
10346           /* Skip tokens until we find a good place from which to
10347              continue parsing.  */
10348           cp_parser_skip_to_closing_parenthesis (parser,
10349                                                  /*recovering=*/true,
10350                                                  /*or_comma=*/true,
10351                                                  /*consume_paren=*/false);
10352           /* If parsing tentatively, permanently remove the
10353              template argument list.  That will prevent duplicate
10354              error messages from being issued about the missing
10355              "template" keyword.  */
10356           if (start)
10357             cp_lexer_purge_tokens_after (parser->lexer, start);
10358           if (is_identifier)
10359             *is_identifier = true;
10360           return identifier;
10361         }
10362
10363       /* If the "template" keyword is present, then there is generally
10364          no point in doing name-lookup, so we just return IDENTIFIER.
10365          But, if the qualifying scope is non-dependent then we can
10366          (and must) do name-lookup normally.  */
10367       if (template_keyword_p
10368           && (!parser->scope
10369               || (TYPE_P (parser->scope)
10370                   && dependent_type_p (parser->scope))))
10371         return identifier;
10372     }
10373
10374   /* Look up the name.  */
10375   decl = cp_parser_lookup_name (parser, identifier,
10376                                 none_type,
10377                                 /*is_template=*/false,
10378                                 /*is_namespace=*/false,
10379                                 check_dependency_p,
10380                                 /*ambiguous_decls=*/NULL,
10381                                 token->location);
10382   decl = maybe_get_template_decl_from_type_decl (decl);
10383
10384   /* If DECL is a template, then the name was a template-name.  */
10385   if (TREE_CODE (decl) == TEMPLATE_DECL)
10386     ;
10387   else
10388     {
10389       tree fn = NULL_TREE;
10390
10391       /* The standard does not explicitly indicate whether a name that
10392          names a set of overloaded declarations, some of which are
10393          templates, is a template-name.  However, such a name should
10394          be a template-name; otherwise, there is no way to form a
10395          template-id for the overloaded templates.  */
10396       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10397       if (TREE_CODE (fns) == OVERLOAD)
10398         for (fn = fns; fn; fn = OVL_NEXT (fn))
10399           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10400             break;
10401
10402       if (!fn)
10403         {
10404           /* The name does not name a template.  */
10405           cp_parser_error (parser, "expected template-name");
10406           return error_mark_node;
10407         }
10408     }
10409
10410   /* If DECL is dependent, and refers to a function, then just return
10411      its name; we will look it up again during template instantiation.  */
10412   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10413     {
10414       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10415       if (TYPE_P (scope) && dependent_type_p (scope))
10416         return identifier;
10417     }
10418
10419   return decl;
10420 }
10421
10422 /* Parse a template-argument-list.
10423
10424    template-argument-list:
10425      template-argument ... [opt]
10426      template-argument-list , template-argument ... [opt]
10427
10428    Returns a TREE_VEC containing the arguments.  */
10429
10430 static tree
10431 cp_parser_template_argument_list (cp_parser* parser)
10432 {
10433   tree fixed_args[10];
10434   unsigned n_args = 0;
10435   unsigned alloced = 10;
10436   tree *arg_ary = fixed_args;
10437   tree vec;
10438   bool saved_in_template_argument_list_p;
10439   bool saved_ice_p;
10440   bool saved_non_ice_p;
10441
10442   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10443   parser->in_template_argument_list_p = true;
10444   /* Even if the template-id appears in an integral
10445      constant-expression, the contents of the argument list do
10446      not.  */
10447   saved_ice_p = parser->integral_constant_expression_p;
10448   parser->integral_constant_expression_p = false;
10449   saved_non_ice_p = parser->non_integral_constant_expression_p;
10450   parser->non_integral_constant_expression_p = false;
10451   /* Parse the arguments.  */
10452   do
10453     {
10454       tree argument;
10455
10456       if (n_args)
10457         /* Consume the comma.  */
10458         cp_lexer_consume_token (parser->lexer);
10459
10460       /* Parse the template-argument.  */
10461       argument = cp_parser_template_argument (parser);
10462
10463       /* If the next token is an ellipsis, we're expanding a template
10464          argument pack. */
10465       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10466         {
10467           /* Consume the `...' token. */
10468           cp_lexer_consume_token (parser->lexer);
10469
10470           /* Make the argument into a TYPE_PACK_EXPANSION or
10471              EXPR_PACK_EXPANSION. */
10472           argument = make_pack_expansion (argument);
10473         }
10474
10475       if (n_args == alloced)
10476         {
10477           alloced *= 2;
10478
10479           if (arg_ary == fixed_args)
10480             {
10481               arg_ary = XNEWVEC (tree, alloced);
10482               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10483             }
10484           else
10485             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10486         }
10487       arg_ary[n_args++] = argument;
10488     }
10489   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10490
10491   vec = make_tree_vec (n_args);
10492
10493   while (n_args--)
10494     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10495
10496   if (arg_ary != fixed_args)
10497     free (arg_ary);
10498   parser->non_integral_constant_expression_p = saved_non_ice_p;
10499   parser->integral_constant_expression_p = saved_ice_p;
10500   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10501   return vec;
10502 }
10503
10504 /* Parse a template-argument.
10505
10506    template-argument:
10507      assignment-expression
10508      type-id
10509      id-expression
10510
10511    The representation is that of an assignment-expression, type-id, or
10512    id-expression -- except that the qualified id-expression is
10513    evaluated, so that the value returned is either a DECL or an
10514    OVERLOAD.
10515
10516    Although the standard says "assignment-expression", it forbids
10517    throw-expressions or assignments in the template argument.
10518    Therefore, we use "conditional-expression" instead.  */
10519
10520 static tree
10521 cp_parser_template_argument (cp_parser* parser)
10522 {
10523   tree argument;
10524   bool template_p;
10525   bool address_p;
10526   bool maybe_type_id = false;
10527   cp_token *token = NULL, *argument_start_token = NULL;
10528   cp_id_kind idk;
10529
10530   /* There's really no way to know what we're looking at, so we just
10531      try each alternative in order.
10532
10533        [temp.arg]
10534
10535        In a template-argument, an ambiguity between a type-id and an
10536        expression is resolved to a type-id, regardless of the form of
10537        the corresponding template-parameter.
10538
10539      Therefore, we try a type-id first.  */
10540   cp_parser_parse_tentatively (parser);
10541   argument = cp_parser_type_id (parser);
10542   /* If there was no error parsing the type-id but the next token is a
10543      '>>', our behavior depends on which dialect of C++ we're
10544      parsing. In C++98, we probably found a typo for '> >'. But there
10545      are type-id which are also valid expressions. For instance:
10546
10547      struct X { int operator >> (int); };
10548      template <int V> struct Foo {};
10549      Foo<X () >> 5> r;
10550
10551      Here 'X()' is a valid type-id of a function type, but the user just
10552      wanted to write the expression "X() >> 5". Thus, we remember that we
10553      found a valid type-id, but we still try to parse the argument as an
10554      expression to see what happens. 
10555
10556      In C++0x, the '>>' will be considered two separate '>'
10557      tokens.  */
10558   if (!cp_parser_error_occurred (parser)
10559       && cxx_dialect == cxx98
10560       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10561     {
10562       maybe_type_id = true;
10563       cp_parser_abort_tentative_parse (parser);
10564     }
10565   else
10566     {
10567       /* If the next token isn't a `,' or a `>', then this argument wasn't
10568       really finished. This means that the argument is not a valid
10569       type-id.  */
10570       if (!cp_parser_next_token_ends_template_argument_p (parser))
10571         cp_parser_error (parser, "expected template-argument");
10572       /* If that worked, we're done.  */
10573       if (cp_parser_parse_definitely (parser))
10574         return argument;
10575     }
10576   /* We're still not sure what the argument will be.  */
10577   cp_parser_parse_tentatively (parser);
10578   /* Try a template.  */
10579   argument_start_token = cp_lexer_peek_token (parser->lexer);
10580   argument = cp_parser_id_expression (parser,
10581                                       /*template_keyword_p=*/false,
10582                                       /*check_dependency_p=*/true,
10583                                       &template_p,
10584                                       /*declarator_p=*/false,
10585                                       /*optional_p=*/false);
10586   /* If the next token isn't a `,' or a `>', then this argument wasn't
10587      really finished.  */
10588   if (!cp_parser_next_token_ends_template_argument_p (parser))
10589     cp_parser_error (parser, "expected template-argument");
10590   if (!cp_parser_error_occurred (parser))
10591     {
10592       /* Figure out what is being referred to.  If the id-expression
10593          was for a class template specialization, then we will have a
10594          TYPE_DECL at this point.  There is no need to do name lookup
10595          at this point in that case.  */
10596       if (TREE_CODE (argument) != TYPE_DECL)
10597         argument = cp_parser_lookup_name (parser, argument,
10598                                           none_type,
10599                                           /*is_template=*/template_p,
10600                                           /*is_namespace=*/false,
10601                                           /*check_dependency=*/true,
10602                                           /*ambiguous_decls=*/NULL,
10603                                           argument_start_token->location);
10604       if (TREE_CODE (argument) != TEMPLATE_DECL
10605           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10606         cp_parser_error (parser, "expected template-name");
10607     }
10608   if (cp_parser_parse_definitely (parser))
10609     return argument;
10610   /* It must be a non-type argument.  There permitted cases are given
10611      in [temp.arg.nontype]:
10612
10613      -- an integral constant-expression of integral or enumeration
10614         type; or
10615
10616      -- the name of a non-type template-parameter; or
10617
10618      -- the name of an object or function with external linkage...
10619
10620      -- the address of an object or function with external linkage...
10621
10622      -- a pointer to member...  */
10623   /* Look for a non-type template parameter.  */
10624   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10625     {
10626       cp_parser_parse_tentatively (parser);
10627       argument = cp_parser_primary_expression (parser,
10628                                                /*address_p=*/false,
10629                                                /*cast_p=*/false,
10630                                                /*template_arg_p=*/true,
10631                                                &idk);
10632       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10633           || !cp_parser_next_token_ends_template_argument_p (parser))
10634         cp_parser_simulate_error (parser);
10635       if (cp_parser_parse_definitely (parser))
10636         return argument;
10637     }
10638
10639   /* If the next token is "&", the argument must be the address of an
10640      object or function with external linkage.  */
10641   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10642   if (address_p)
10643     cp_lexer_consume_token (parser->lexer);
10644   /* See if we might have an id-expression.  */
10645   token = cp_lexer_peek_token (parser->lexer);
10646   if (token->type == CPP_NAME
10647       || token->keyword == RID_OPERATOR
10648       || token->type == CPP_SCOPE
10649       || token->type == CPP_TEMPLATE_ID
10650       || token->type == CPP_NESTED_NAME_SPECIFIER)
10651     {
10652       cp_parser_parse_tentatively (parser);
10653       argument = cp_parser_primary_expression (parser,
10654                                                address_p,
10655                                                /*cast_p=*/false,
10656                                                /*template_arg_p=*/true,
10657                                                &idk);
10658       if (cp_parser_error_occurred (parser)
10659           || !cp_parser_next_token_ends_template_argument_p (parser))
10660         cp_parser_abort_tentative_parse (parser);
10661       else
10662         {
10663           if (TREE_CODE (argument) == INDIRECT_REF)
10664             {
10665               gcc_assert (REFERENCE_REF_P (argument));
10666               argument = TREE_OPERAND (argument, 0);
10667             }
10668
10669           if (TREE_CODE (argument) == VAR_DECL)
10670             {
10671               /* A variable without external linkage might still be a
10672                  valid constant-expression, so no error is issued here
10673                  if the external-linkage check fails.  */
10674               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10675                 cp_parser_simulate_error (parser);
10676             }
10677           else if (is_overloaded_fn (argument))
10678             /* All overloaded functions are allowed; if the external
10679                linkage test does not pass, an error will be issued
10680                later.  */
10681             ;
10682           else if (address_p
10683                    && (TREE_CODE (argument) == OFFSET_REF
10684                        || TREE_CODE (argument) == SCOPE_REF))
10685             /* A pointer-to-member.  */
10686             ;
10687           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10688             ;
10689           else
10690             cp_parser_simulate_error (parser);
10691
10692           if (cp_parser_parse_definitely (parser))
10693             {
10694               if (address_p)
10695                 argument = build_x_unary_op (ADDR_EXPR, argument,
10696                                              tf_warning_or_error);
10697               return argument;
10698             }
10699         }
10700     }
10701   /* If the argument started with "&", there are no other valid
10702      alternatives at this point.  */
10703   if (address_p)
10704     {
10705       cp_parser_error (parser, "invalid non-type template argument");
10706       return error_mark_node;
10707     }
10708
10709   /* If the argument wasn't successfully parsed as a type-id followed
10710      by '>>', the argument can only be a constant expression now.
10711      Otherwise, we try parsing the constant-expression tentatively,
10712      because the argument could really be a type-id.  */
10713   if (maybe_type_id)
10714     cp_parser_parse_tentatively (parser);
10715   argument = cp_parser_constant_expression (parser,
10716                                             /*allow_non_constant_p=*/false,
10717                                             /*non_constant_p=*/NULL);
10718   argument = fold_non_dependent_expr (argument);
10719   if (!maybe_type_id)
10720     return argument;
10721   if (!cp_parser_next_token_ends_template_argument_p (parser))
10722     cp_parser_error (parser, "expected template-argument");
10723   if (cp_parser_parse_definitely (parser))
10724     return argument;
10725   /* We did our best to parse the argument as a non type-id, but that
10726      was the only alternative that matched (albeit with a '>' after
10727      it). We can assume it's just a typo from the user, and a
10728      diagnostic will then be issued.  */
10729   return cp_parser_type_id (parser);
10730 }
10731
10732 /* Parse an explicit-instantiation.
10733
10734    explicit-instantiation:
10735      template declaration
10736
10737    Although the standard says `declaration', what it really means is:
10738
10739    explicit-instantiation:
10740      template decl-specifier-seq [opt] declarator [opt] ;
10741
10742    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10743    supposed to be allowed.  A defect report has been filed about this
10744    issue.
10745
10746    GNU Extension:
10747
10748    explicit-instantiation:
10749      storage-class-specifier template
10750        decl-specifier-seq [opt] declarator [opt] ;
10751      function-specifier template
10752        decl-specifier-seq [opt] declarator [opt] ;  */
10753
10754 static void
10755 cp_parser_explicit_instantiation (cp_parser* parser)
10756 {
10757   int declares_class_or_enum;
10758   cp_decl_specifier_seq decl_specifiers;
10759   tree extension_specifier = NULL_TREE;
10760   cp_token *token;
10761
10762   /* Look for an (optional) storage-class-specifier or
10763      function-specifier.  */
10764   if (cp_parser_allow_gnu_extensions_p (parser))
10765     {
10766       extension_specifier
10767         = cp_parser_storage_class_specifier_opt (parser);
10768       if (!extension_specifier)
10769         extension_specifier
10770           = cp_parser_function_specifier_opt (parser,
10771                                               /*decl_specs=*/NULL);
10772     }
10773
10774   /* Look for the `template' keyword.  */
10775   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10776   /* Let the front end know that we are processing an explicit
10777      instantiation.  */
10778   begin_explicit_instantiation ();
10779   /* [temp.explicit] says that we are supposed to ignore access
10780      control while processing explicit instantiation directives.  */
10781   push_deferring_access_checks (dk_no_check);
10782   /* Parse a decl-specifier-seq.  */
10783   token = cp_lexer_peek_token (parser->lexer);
10784   cp_parser_decl_specifier_seq (parser,
10785                                 CP_PARSER_FLAGS_OPTIONAL,
10786                                 &decl_specifiers,
10787                                 &declares_class_or_enum);
10788   /* If there was exactly one decl-specifier, and it declared a class,
10789      and there's no declarator, then we have an explicit type
10790      instantiation.  */
10791   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10792     {
10793       tree type;
10794
10795       type = check_tag_decl (&decl_specifiers);
10796       /* Turn access control back on for names used during
10797          template instantiation.  */
10798       pop_deferring_access_checks ();
10799       if (type)
10800         do_type_instantiation (type, extension_specifier,
10801                                /*complain=*/tf_error);
10802     }
10803   else
10804     {
10805       cp_declarator *declarator;
10806       tree decl;
10807
10808       /* Parse the declarator.  */
10809       declarator
10810         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10811                                 /*ctor_dtor_or_conv_p=*/NULL,
10812                                 /*parenthesized_p=*/NULL,
10813                                 /*member_p=*/false);
10814       if (declares_class_or_enum & 2)
10815         cp_parser_check_for_definition_in_return_type (declarator,
10816                                                        decl_specifiers.type,
10817                                                        decl_specifiers.type_location);
10818       if (declarator != cp_error_declarator)
10819         {
10820           decl = grokdeclarator (declarator, &decl_specifiers,
10821                                  NORMAL, 0, &decl_specifiers.attributes);
10822           /* Turn access control back on for names used during
10823              template instantiation.  */
10824           pop_deferring_access_checks ();
10825           /* Do the explicit instantiation.  */
10826           do_decl_instantiation (decl, extension_specifier);
10827         }
10828       else
10829         {
10830           pop_deferring_access_checks ();
10831           /* Skip the body of the explicit instantiation.  */
10832           cp_parser_skip_to_end_of_statement (parser);
10833         }
10834     }
10835   /* We're done with the instantiation.  */
10836   end_explicit_instantiation ();
10837
10838   cp_parser_consume_semicolon_at_end_of_statement (parser);
10839 }
10840
10841 /* Parse an explicit-specialization.
10842
10843    explicit-specialization:
10844      template < > declaration
10845
10846    Although the standard says `declaration', what it really means is:
10847
10848    explicit-specialization:
10849      template <> decl-specifier [opt] init-declarator [opt] ;
10850      template <> function-definition
10851      template <> explicit-specialization
10852      template <> template-declaration  */
10853
10854 static void
10855 cp_parser_explicit_specialization (cp_parser* parser)
10856 {
10857   bool need_lang_pop;
10858   cp_token *token = cp_lexer_peek_token (parser->lexer);
10859
10860   /* Look for the `template' keyword.  */
10861   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10862   /* Look for the `<'.  */
10863   cp_parser_require (parser, CPP_LESS, "%<<%>");
10864   /* Look for the `>'.  */
10865   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10866   /* We have processed another parameter list.  */
10867   ++parser->num_template_parameter_lists;
10868   /* [temp]
10869
10870      A template ... explicit specialization ... shall not have C
10871      linkage.  */
10872   if (current_lang_name == lang_name_c)
10873     {
10874       error ("%Htemplate specialization with C linkage", &token->location);
10875       /* Give it C++ linkage to avoid confusing other parts of the
10876          front end.  */
10877       push_lang_context (lang_name_cplusplus);
10878       need_lang_pop = true;
10879     }
10880   else
10881     need_lang_pop = false;
10882   /* Let the front end know that we are beginning a specialization.  */
10883   if (!begin_specialization ())
10884     {
10885       end_specialization ();
10886       cp_parser_skip_to_end_of_block_or_statement (parser);
10887       return;
10888     }
10889
10890   /* If the next keyword is `template', we need to figure out whether
10891      or not we're looking a template-declaration.  */
10892   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10893     {
10894       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10895           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10896         cp_parser_template_declaration_after_export (parser,
10897                                                      /*member_p=*/false);
10898       else
10899         cp_parser_explicit_specialization (parser);
10900     }
10901   else
10902     /* Parse the dependent declaration.  */
10903     cp_parser_single_declaration (parser,
10904                                   /*checks=*/NULL,
10905                                   /*member_p=*/false,
10906                                   /*explicit_specialization_p=*/true,
10907                                   /*friend_p=*/NULL);
10908   /* We're done with the specialization.  */
10909   end_specialization ();
10910   /* For the erroneous case of a template with C linkage, we pushed an
10911      implicit C++ linkage scope; exit that scope now.  */
10912   if (need_lang_pop)
10913     pop_lang_context ();
10914   /* We're done with this parameter list.  */
10915   --parser->num_template_parameter_lists;
10916 }
10917
10918 /* Parse a type-specifier.
10919
10920    type-specifier:
10921      simple-type-specifier
10922      class-specifier
10923      enum-specifier
10924      elaborated-type-specifier
10925      cv-qualifier
10926
10927    GNU Extension:
10928
10929    type-specifier:
10930      __complex__
10931
10932    Returns a representation of the type-specifier.  For a
10933    class-specifier, enum-specifier, or elaborated-type-specifier, a
10934    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10935
10936    The parser flags FLAGS is used to control type-specifier parsing.
10937
10938    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10939    in a decl-specifier-seq.
10940
10941    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10942    class-specifier, enum-specifier, or elaborated-type-specifier, then
10943    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10944    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10945    zero.
10946
10947    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10948    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10949    is set to FALSE.  */
10950
10951 static tree
10952 cp_parser_type_specifier (cp_parser* parser,
10953                           cp_parser_flags flags,
10954                           cp_decl_specifier_seq *decl_specs,
10955                           bool is_declaration,
10956                           int* declares_class_or_enum,
10957                           bool* is_cv_qualifier)
10958 {
10959   tree type_spec = NULL_TREE;
10960   cp_token *token;
10961   enum rid keyword;
10962   cp_decl_spec ds = ds_last;
10963
10964   /* Assume this type-specifier does not declare a new type.  */
10965   if (declares_class_or_enum)
10966     *declares_class_or_enum = 0;
10967   /* And that it does not specify a cv-qualifier.  */
10968   if (is_cv_qualifier)
10969     *is_cv_qualifier = false;
10970   /* Peek at the next token.  */
10971   token = cp_lexer_peek_token (parser->lexer);
10972
10973   /* If we're looking at a keyword, we can use that to guide the
10974      production we choose.  */
10975   keyword = token->keyword;
10976   switch (keyword)
10977     {
10978     case RID_ENUM:
10979       /* Look for the enum-specifier.  */
10980       type_spec = cp_parser_enum_specifier (parser);
10981       /* If that worked, we're done.  */
10982       if (type_spec)
10983         {
10984           if (declares_class_or_enum)
10985             *declares_class_or_enum = 2;
10986           if (decl_specs)
10987             cp_parser_set_decl_spec_type (decl_specs,
10988                                           type_spec,
10989                                           token->location,
10990                                           /*user_defined_p=*/true);
10991           return type_spec;
10992         }
10993       else
10994         goto elaborated_type_specifier;
10995
10996       /* Any of these indicate either a class-specifier, or an
10997          elaborated-type-specifier.  */
10998     case RID_CLASS:
10999     case RID_STRUCT:
11000     case RID_UNION:
11001       /* Parse tentatively so that we can back up if we don't find a
11002          class-specifier.  */
11003       cp_parser_parse_tentatively (parser);
11004       /* Look for the class-specifier.  */
11005       type_spec = cp_parser_class_specifier (parser);
11006       /* If that worked, we're done.  */
11007       if (cp_parser_parse_definitely (parser))
11008         {
11009           if (declares_class_or_enum)
11010             *declares_class_or_enum = 2;
11011           if (decl_specs)
11012             cp_parser_set_decl_spec_type (decl_specs,
11013                                           type_spec,
11014                                           token->location,
11015                                           /*user_defined_p=*/true);
11016           return type_spec;
11017         }
11018
11019       /* Fall through.  */
11020     elaborated_type_specifier:
11021       /* We're declaring (not defining) a class or enum.  */
11022       if (declares_class_or_enum)
11023         *declares_class_or_enum = 1;
11024
11025       /* Fall through.  */
11026     case RID_TYPENAME:
11027       /* Look for an elaborated-type-specifier.  */
11028       type_spec
11029         = (cp_parser_elaborated_type_specifier
11030            (parser,
11031             decl_specs && decl_specs->specs[(int) ds_friend],
11032             is_declaration));
11033       if (decl_specs)
11034         cp_parser_set_decl_spec_type (decl_specs,
11035                                       type_spec,
11036                                       token->location,
11037                                       /*user_defined_p=*/true);
11038       return type_spec;
11039
11040     case RID_CONST:
11041       ds = ds_const;
11042       if (is_cv_qualifier)
11043         *is_cv_qualifier = true;
11044       break;
11045
11046     case RID_VOLATILE:
11047       ds = ds_volatile;
11048       if (is_cv_qualifier)
11049         *is_cv_qualifier = true;
11050       break;
11051
11052     case RID_RESTRICT:
11053       ds = ds_restrict;
11054       if (is_cv_qualifier)
11055         *is_cv_qualifier = true;
11056       break;
11057
11058     case RID_COMPLEX:
11059       /* The `__complex__' keyword is a GNU extension.  */
11060       ds = ds_complex;
11061       break;
11062
11063     default:
11064       break;
11065     }
11066
11067   /* Handle simple keywords.  */
11068   if (ds != ds_last)
11069     {
11070       if (decl_specs)
11071         {
11072           ++decl_specs->specs[(int)ds];
11073           decl_specs->any_specifiers_p = true;
11074         }
11075       return cp_lexer_consume_token (parser->lexer)->u.value;
11076     }
11077
11078   /* If we do not already have a type-specifier, assume we are looking
11079      at a simple-type-specifier.  */
11080   type_spec = cp_parser_simple_type_specifier (parser,
11081                                                decl_specs,
11082                                                flags);
11083
11084   /* If we didn't find a type-specifier, and a type-specifier was not
11085      optional in this context, issue an error message.  */
11086   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11087     {
11088       cp_parser_error (parser, "expected type specifier");
11089       return error_mark_node;
11090     }
11091
11092   return type_spec;
11093 }
11094
11095 /* Parse a simple-type-specifier.
11096
11097    simple-type-specifier:
11098      :: [opt] nested-name-specifier [opt] type-name
11099      :: [opt] nested-name-specifier template template-id
11100      char
11101      wchar_t
11102      bool
11103      short
11104      int
11105      long
11106      signed
11107      unsigned
11108      float
11109      double
11110      void
11111
11112    C++0x Extension:
11113
11114    simple-type-specifier:
11115      auto
11116      decltype ( expression )   
11117      char16_t
11118      char32_t
11119
11120    GNU Extension:
11121
11122    simple-type-specifier:
11123      __typeof__ unary-expression
11124      __typeof__ ( type-id )
11125
11126    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11127    appropriately updated.  */
11128
11129 static tree
11130 cp_parser_simple_type_specifier (cp_parser* parser,
11131                                  cp_decl_specifier_seq *decl_specs,
11132                                  cp_parser_flags flags)
11133 {
11134   tree type = NULL_TREE;
11135   cp_token *token;
11136
11137   /* Peek at the next token.  */
11138   token = cp_lexer_peek_token (parser->lexer);
11139
11140   /* If we're looking at a keyword, things are easy.  */
11141   switch (token->keyword)
11142     {
11143     case RID_CHAR:
11144       if (decl_specs)
11145         decl_specs->explicit_char_p = true;
11146       type = char_type_node;
11147       break;
11148     case RID_CHAR16:
11149       type = char16_type_node;
11150       break;
11151     case RID_CHAR32:
11152       type = char32_type_node;
11153       break;
11154     case RID_WCHAR:
11155       type = wchar_type_node;
11156       break;
11157     case RID_BOOL:
11158       type = boolean_type_node;
11159       break;
11160     case RID_SHORT:
11161       if (decl_specs)
11162         ++decl_specs->specs[(int) ds_short];
11163       type = short_integer_type_node;
11164       break;
11165     case RID_INT:
11166       if (decl_specs)
11167         decl_specs->explicit_int_p = true;
11168       type = integer_type_node;
11169       break;
11170     case RID_LONG:
11171       if (decl_specs)
11172         ++decl_specs->specs[(int) ds_long];
11173       type = long_integer_type_node;
11174       break;
11175     case RID_SIGNED:
11176       if (decl_specs)
11177         ++decl_specs->specs[(int) ds_signed];
11178       type = integer_type_node;
11179       break;
11180     case RID_UNSIGNED:
11181       if (decl_specs)
11182         ++decl_specs->specs[(int) ds_unsigned];
11183       type = unsigned_type_node;
11184       break;
11185     case RID_FLOAT:
11186       type = float_type_node;
11187       break;
11188     case RID_DOUBLE:
11189       type = double_type_node;
11190       break;
11191     case RID_VOID:
11192       type = void_type_node;
11193       break;
11194       
11195     case RID_AUTO:
11196       maybe_warn_cpp0x ("C++0x auto");
11197       type = make_auto ();
11198       break;
11199
11200     case RID_DECLTYPE:
11201       /* Parse the `decltype' type.  */
11202       type = cp_parser_decltype (parser);
11203
11204       if (decl_specs)
11205         cp_parser_set_decl_spec_type (decl_specs, type,
11206                                       token->location,
11207                                       /*user_defined_p=*/true);
11208
11209       return type;
11210
11211     case RID_TYPEOF:
11212       /* Consume the `typeof' token.  */
11213       cp_lexer_consume_token (parser->lexer);
11214       /* Parse the operand to `typeof'.  */
11215       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11216       /* If it is not already a TYPE, take its type.  */
11217       if (!TYPE_P (type))
11218         type = finish_typeof (type);
11219
11220       if (decl_specs)
11221         cp_parser_set_decl_spec_type (decl_specs, type,
11222                                       token->location,
11223                                       /*user_defined_p=*/true);
11224
11225       return type;
11226
11227     default:
11228       break;
11229     }
11230
11231   /* If the type-specifier was for a built-in type, we're done.  */
11232   if (type)
11233     {
11234       tree id;
11235
11236       /* Record the type.  */
11237       if (decl_specs
11238           && (token->keyword != RID_SIGNED
11239               && token->keyword != RID_UNSIGNED
11240               && token->keyword != RID_SHORT
11241               && token->keyword != RID_LONG))
11242         cp_parser_set_decl_spec_type (decl_specs,
11243                                       type,
11244                                       token->location,
11245                                       /*user_defined=*/false);
11246       if (decl_specs)
11247         decl_specs->any_specifiers_p = true;
11248
11249       /* Consume the token.  */
11250       id = cp_lexer_consume_token (parser->lexer)->u.value;
11251
11252       /* There is no valid C++ program where a non-template type is
11253          followed by a "<".  That usually indicates that the user thought
11254          that the type was a template.  */
11255       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11256
11257       return TYPE_NAME (type);
11258     }
11259
11260   /* The type-specifier must be a user-defined type.  */
11261   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11262     {
11263       bool qualified_p;
11264       bool global_p;
11265
11266       /* Don't gobble tokens or issue error messages if this is an
11267          optional type-specifier.  */
11268       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11269         cp_parser_parse_tentatively (parser);
11270
11271       /* Look for the optional `::' operator.  */
11272       global_p
11273         = (cp_parser_global_scope_opt (parser,
11274                                        /*current_scope_valid_p=*/false)
11275            != NULL_TREE);
11276       /* Look for the nested-name specifier.  */
11277       qualified_p
11278         = (cp_parser_nested_name_specifier_opt (parser,
11279                                                 /*typename_keyword_p=*/false,
11280                                                 /*check_dependency_p=*/true,
11281                                                 /*type_p=*/false,
11282                                                 /*is_declaration=*/false)
11283            != NULL_TREE);
11284       token = cp_lexer_peek_token (parser->lexer);
11285       /* If we have seen a nested-name-specifier, and the next token
11286          is `template', then we are using the template-id production.  */
11287       if (parser->scope
11288           && cp_parser_optional_template_keyword (parser))
11289         {
11290           /* Look for the template-id.  */
11291           type = cp_parser_template_id (parser,
11292                                         /*template_keyword_p=*/true,
11293                                         /*check_dependency_p=*/true,
11294                                         /*is_declaration=*/false);
11295           /* If the template-id did not name a type, we are out of
11296              luck.  */
11297           if (TREE_CODE (type) != TYPE_DECL)
11298             {
11299               cp_parser_error (parser, "expected template-id for type");
11300               type = NULL_TREE;
11301             }
11302         }
11303       /* Otherwise, look for a type-name.  */
11304       else
11305         type = cp_parser_type_name (parser);
11306       /* Keep track of all name-lookups performed in class scopes.  */
11307       if (type
11308           && !global_p
11309           && !qualified_p
11310           && TREE_CODE (type) == TYPE_DECL
11311           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11312         maybe_note_name_used_in_class (DECL_NAME (type), type);
11313       /* If it didn't work out, we don't have a TYPE.  */
11314       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11315           && !cp_parser_parse_definitely (parser))
11316         type = NULL_TREE;
11317       if (type && decl_specs)
11318         cp_parser_set_decl_spec_type (decl_specs, type,
11319                                       token->location,
11320                                       /*user_defined=*/true);
11321     }
11322
11323   /* If we didn't get a type-name, issue an error message.  */
11324   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11325     {
11326       cp_parser_error (parser, "expected type-name");
11327       return error_mark_node;
11328     }
11329
11330   /* There is no valid C++ program where a non-template type is
11331      followed by a "<".  That usually indicates that the user thought
11332      that the type was a template.  */
11333   if (type && type != error_mark_node)
11334     {
11335       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11336          If it is, then the '<'...'>' enclose protocol names rather than
11337          template arguments, and so everything is fine.  */
11338       if (c_dialect_objc ()
11339           && (objc_is_id (type) || objc_is_class_name (type)))
11340         {
11341           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11342           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11343
11344           /* Clobber the "unqualified" type previously entered into
11345              DECL_SPECS with the new, improved protocol-qualified version.  */
11346           if (decl_specs)
11347             decl_specs->type = qual_type;
11348
11349           return qual_type;
11350         }
11351
11352       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11353                                                token->location);
11354     }
11355
11356   return type;
11357 }
11358
11359 /* Parse a type-name.
11360
11361    type-name:
11362      class-name
11363      enum-name
11364      typedef-name
11365
11366    enum-name:
11367      identifier
11368
11369    typedef-name:
11370      identifier
11371
11372    Returns a TYPE_DECL for the type.  */
11373
11374 static tree
11375 cp_parser_type_name (cp_parser* parser)
11376 {
11377   tree type_decl;
11378
11379   /* We can't know yet whether it is a class-name or not.  */
11380   cp_parser_parse_tentatively (parser);
11381   /* Try a class-name.  */
11382   type_decl = cp_parser_class_name (parser,
11383                                     /*typename_keyword_p=*/false,
11384                                     /*template_keyword_p=*/false,
11385                                     none_type,
11386                                     /*check_dependency_p=*/true,
11387                                     /*class_head_p=*/false,
11388                                     /*is_declaration=*/false);
11389   /* If it's not a class-name, keep looking.  */
11390   if (!cp_parser_parse_definitely (parser))
11391     {
11392       /* It must be a typedef-name or an enum-name.  */
11393       return cp_parser_nonclass_name (parser);
11394     }
11395
11396   return type_decl;
11397 }
11398
11399 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11400
11401    enum-name:
11402      identifier
11403
11404    typedef-name:
11405      identifier
11406
11407    Returns a TYPE_DECL for the type.  */
11408
11409 static tree
11410 cp_parser_nonclass_name (cp_parser* parser)
11411 {
11412   tree type_decl;
11413   tree identifier;
11414
11415   cp_token *token = cp_lexer_peek_token (parser->lexer);
11416   identifier = cp_parser_identifier (parser);
11417   if (identifier == error_mark_node)
11418     return error_mark_node;
11419
11420   /* Look up the type-name.  */
11421   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11422
11423   if (TREE_CODE (type_decl) != TYPE_DECL
11424       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11425     {
11426       /* See if this is an Objective-C type.  */
11427       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11428       tree type = objc_get_protocol_qualified_type (identifier, protos);
11429       if (type)
11430         type_decl = TYPE_NAME (type);
11431     }
11432   
11433   /* Issue an error if we did not find a type-name.  */
11434   if (TREE_CODE (type_decl) != TYPE_DECL)
11435     {
11436       if (!cp_parser_simulate_error (parser))
11437         cp_parser_name_lookup_error (parser, identifier, type_decl,
11438                                      "is not a type", token->location);
11439       return error_mark_node;
11440     }
11441   /* Remember that the name was used in the definition of the
11442      current class so that we can check later to see if the
11443      meaning would have been different after the class was
11444      entirely defined.  */
11445   else if (type_decl != error_mark_node
11446            && !parser->scope)
11447     maybe_note_name_used_in_class (identifier, type_decl);
11448   
11449   return type_decl;
11450 }
11451
11452 /* Parse an elaborated-type-specifier.  Note that the grammar given
11453    here incorporates the resolution to DR68.
11454
11455    elaborated-type-specifier:
11456      class-key :: [opt] nested-name-specifier [opt] identifier
11457      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11458      enum-key :: [opt] nested-name-specifier [opt] identifier
11459      typename :: [opt] nested-name-specifier identifier
11460      typename :: [opt] nested-name-specifier template [opt]
11461        template-id
11462
11463    GNU extension:
11464
11465    elaborated-type-specifier:
11466      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11467      class-key attributes :: [opt] nested-name-specifier [opt]
11468                template [opt] template-id
11469      enum attributes :: [opt] nested-name-specifier [opt] identifier
11470
11471    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11472    declared `friend'.  If IS_DECLARATION is TRUE, then this
11473    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11474    something is being declared.
11475
11476    Returns the TYPE specified.  */
11477
11478 static tree
11479 cp_parser_elaborated_type_specifier (cp_parser* parser,
11480                                      bool is_friend,
11481                                      bool is_declaration)
11482 {
11483   enum tag_types tag_type;
11484   tree identifier;
11485   tree type = NULL_TREE;
11486   tree attributes = NULL_TREE;
11487   cp_token *token = NULL;
11488
11489   /* See if we're looking at the `enum' keyword.  */
11490   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11491     {
11492       /* Consume the `enum' token.  */
11493       cp_lexer_consume_token (parser->lexer);
11494       /* Remember that it's an enumeration type.  */
11495       tag_type = enum_type;
11496       /* Parse the optional `struct' or `class' key (for C++0x scoped
11497          enums).  */
11498       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11499           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11500         {
11501           if (cxx_dialect == cxx98)
11502             maybe_warn_cpp0x ("scoped enums");
11503
11504           /* Consume the `struct' or `class'.  */
11505           cp_lexer_consume_token (parser->lexer);
11506         }
11507       /* Parse the attributes.  */
11508       attributes = cp_parser_attributes_opt (parser);
11509     }
11510   /* Or, it might be `typename'.  */
11511   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11512                                            RID_TYPENAME))
11513     {
11514       /* Consume the `typename' token.  */
11515       cp_lexer_consume_token (parser->lexer);
11516       /* Remember that it's a `typename' type.  */
11517       tag_type = typename_type;
11518       /* The `typename' keyword is only allowed in templates.  */
11519       if (!processing_template_decl)
11520         permerror (input_location, "using %<typename%> outside of template");
11521     }
11522   /* Otherwise it must be a class-key.  */
11523   else
11524     {
11525       tag_type = cp_parser_class_key (parser);
11526       if (tag_type == none_type)
11527         return error_mark_node;
11528       /* Parse the attributes.  */
11529       attributes = cp_parser_attributes_opt (parser);
11530     }
11531
11532   /* Look for the `::' operator.  */
11533   cp_parser_global_scope_opt (parser,
11534                               /*current_scope_valid_p=*/false);
11535   /* Look for the nested-name-specifier.  */
11536   if (tag_type == typename_type)
11537     {
11538       if (!cp_parser_nested_name_specifier (parser,
11539                                            /*typename_keyword_p=*/true,
11540                                            /*check_dependency_p=*/true,
11541                                            /*type_p=*/true,
11542                                             is_declaration))
11543         return error_mark_node;
11544     }
11545   else
11546     /* Even though `typename' is not present, the proposed resolution
11547        to Core Issue 180 says that in `class A<T>::B', `B' should be
11548        considered a type-name, even if `A<T>' is dependent.  */
11549     cp_parser_nested_name_specifier_opt (parser,
11550                                          /*typename_keyword_p=*/true,
11551                                          /*check_dependency_p=*/true,
11552                                          /*type_p=*/true,
11553                                          is_declaration);
11554  /* For everything but enumeration types, consider a template-id.
11555     For an enumeration type, consider only a plain identifier.  */
11556   if (tag_type != enum_type)
11557     {
11558       bool template_p = false;
11559       tree decl;
11560
11561       /* Allow the `template' keyword.  */
11562       template_p = cp_parser_optional_template_keyword (parser);
11563       /* If we didn't see `template', we don't know if there's a
11564          template-id or not.  */
11565       if (!template_p)
11566         cp_parser_parse_tentatively (parser);
11567       /* Parse the template-id.  */
11568       token = cp_lexer_peek_token (parser->lexer);
11569       decl = cp_parser_template_id (parser, template_p,
11570                                     /*check_dependency_p=*/true,
11571                                     is_declaration);
11572       /* If we didn't find a template-id, look for an ordinary
11573          identifier.  */
11574       if (!template_p && !cp_parser_parse_definitely (parser))
11575         ;
11576       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11577          in effect, then we must assume that, upon instantiation, the
11578          template will correspond to a class.  */
11579       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11580                && tag_type == typename_type)
11581         type = make_typename_type (parser->scope, decl,
11582                                    typename_type,
11583                                    /*complain=*/tf_error);
11584       else
11585         type = TREE_TYPE (decl);
11586     }
11587
11588   if (!type)
11589     {
11590       token = cp_lexer_peek_token (parser->lexer);
11591       identifier = cp_parser_identifier (parser);
11592
11593       if (identifier == error_mark_node)
11594         {
11595           parser->scope = NULL_TREE;
11596           return error_mark_node;
11597         }
11598
11599       /* For a `typename', we needn't call xref_tag.  */
11600       if (tag_type == typename_type
11601           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11602         return cp_parser_make_typename_type (parser, parser->scope,
11603                                              identifier,
11604                                              token->location);
11605       /* Look up a qualified name in the usual way.  */
11606       if (parser->scope)
11607         {
11608           tree decl;
11609           tree ambiguous_decls;
11610
11611           decl = cp_parser_lookup_name (parser, identifier,
11612                                         tag_type,
11613                                         /*is_template=*/false,
11614                                         /*is_namespace=*/false,
11615                                         /*check_dependency=*/true,
11616                                         &ambiguous_decls,
11617                                         token->location);
11618
11619           /* If the lookup was ambiguous, an error will already have been
11620              issued.  */
11621           if (ambiguous_decls)
11622             return error_mark_node;
11623
11624           /* If we are parsing friend declaration, DECL may be a
11625              TEMPLATE_DECL tree node here.  However, we need to check
11626              whether this TEMPLATE_DECL results in valid code.  Consider
11627              the following example:
11628
11629                namespace N {
11630                  template <class T> class C {};
11631                }
11632                class X {
11633                  template <class T> friend class N::C; // #1, valid code
11634                };
11635                template <class T> class Y {
11636                  friend class N::C;                    // #2, invalid code
11637                };
11638
11639              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11640              name lookup of `N::C'.  We see that friend declaration must
11641              be template for the code to be valid.  Note that
11642              processing_template_decl does not work here since it is
11643              always 1 for the above two cases.  */
11644
11645           decl = (cp_parser_maybe_treat_template_as_class
11646                   (decl, /*tag_name_p=*/is_friend
11647                          && parser->num_template_parameter_lists));
11648
11649           if (TREE_CODE (decl) != TYPE_DECL)
11650             {
11651               cp_parser_diagnose_invalid_type_name (parser,
11652                                                     parser->scope,
11653                                                     identifier,
11654                                                     token->location);
11655               return error_mark_node;
11656             }
11657
11658           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11659             {
11660               bool allow_template = (parser->num_template_parameter_lists
11661                                       || DECL_SELF_REFERENCE_P (decl));
11662               type = check_elaborated_type_specifier (tag_type, decl, 
11663                                                       allow_template);
11664
11665               if (type == error_mark_node)
11666                 return error_mark_node;
11667             }
11668
11669           /* Forward declarations of nested types, such as
11670
11671                class C1::C2;
11672                class C1::C2::C3;
11673
11674              are invalid unless all components preceding the final '::'
11675              are complete.  If all enclosing types are complete, these
11676              declarations become merely pointless.
11677
11678              Invalid forward declarations of nested types are errors
11679              caught elsewhere in parsing.  Those that are pointless arrive
11680              here.  */
11681
11682           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11683               && !is_friend && !processing_explicit_instantiation)
11684             warning (0, "declaration %qD does not declare anything", decl);
11685
11686           type = TREE_TYPE (decl);
11687         }
11688       else
11689         {
11690           /* An elaborated-type-specifier sometimes introduces a new type and
11691              sometimes names an existing type.  Normally, the rule is that it
11692              introduces a new type only if there is not an existing type of
11693              the same name already in scope.  For example, given:
11694
11695                struct S {};
11696                void f() { struct S s; }
11697
11698              the `struct S' in the body of `f' is the same `struct S' as in
11699              the global scope; the existing definition is used.  However, if
11700              there were no global declaration, this would introduce a new
11701              local class named `S'.
11702
11703              An exception to this rule applies to the following code:
11704
11705                namespace N { struct S; }
11706
11707              Here, the elaborated-type-specifier names a new type
11708              unconditionally; even if there is already an `S' in the
11709              containing scope this declaration names a new type.
11710              This exception only applies if the elaborated-type-specifier
11711              forms the complete declaration:
11712
11713                [class.name]
11714
11715                A declaration consisting solely of `class-key identifier ;' is
11716                either a redeclaration of the name in the current scope or a
11717                forward declaration of the identifier as a class name.  It
11718                introduces the name into the current scope.
11719
11720              We are in this situation precisely when the next token is a `;'.
11721
11722              An exception to the exception is that a `friend' declaration does
11723              *not* name a new type; i.e., given:
11724
11725                struct S { friend struct T; };
11726
11727              `T' is not a new type in the scope of `S'.
11728
11729              Also, `new struct S' or `sizeof (struct S)' never results in the
11730              definition of a new type; a new type can only be declared in a
11731              declaration context.  */
11732
11733           tag_scope ts;
11734           bool template_p;
11735
11736           if (is_friend)
11737             /* Friends have special name lookup rules.  */
11738             ts = ts_within_enclosing_non_class;
11739           else if (is_declaration
11740                    && cp_lexer_next_token_is (parser->lexer,
11741                                               CPP_SEMICOLON))
11742             /* This is a `class-key identifier ;' */
11743             ts = ts_current;
11744           else
11745             ts = ts_global;
11746
11747           template_p =
11748             (parser->num_template_parameter_lists
11749              && (cp_parser_next_token_starts_class_definition_p (parser)
11750                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11751           /* An unqualified name was used to reference this type, so
11752              there were no qualifying templates.  */
11753           if (!cp_parser_check_template_parameters (parser,
11754                                                     /*num_templates=*/0,
11755                                                     token->location))
11756             return error_mark_node;
11757           type = xref_tag (tag_type, identifier, ts, template_p);
11758         }
11759     }
11760
11761   if (type == error_mark_node)
11762     return error_mark_node;
11763
11764   /* Allow attributes on forward declarations of classes.  */
11765   if (attributes)
11766     {
11767       if (TREE_CODE (type) == TYPENAME_TYPE)
11768         warning (OPT_Wattributes,
11769                  "attributes ignored on uninstantiated type");
11770       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11771                && ! processing_explicit_instantiation)
11772         warning (OPT_Wattributes,
11773                  "attributes ignored on template instantiation");
11774       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11775         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11776       else
11777         warning (OPT_Wattributes,
11778                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11779     }
11780
11781   if (tag_type != enum_type)
11782     cp_parser_check_class_key (tag_type, type);
11783
11784   /* A "<" cannot follow an elaborated type specifier.  If that
11785      happens, the user was probably trying to form a template-id.  */
11786   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11787
11788   return type;
11789 }
11790
11791 /* Parse an enum-specifier.
11792
11793    enum-specifier:
11794      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11795
11796    enum-key:
11797      enum
11798      enum class   [C++0x]
11799      enum struct  [C++0x]
11800
11801    enum-base:   [C++0x]
11802      : type-specifier-seq
11803
11804    GNU Extensions:
11805      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11806        { enumerator-list [opt] }attributes[opt]
11807
11808    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11809    if the token stream isn't an enum-specifier after all.  */
11810
11811 static tree
11812 cp_parser_enum_specifier (cp_parser* parser)
11813 {
11814   tree identifier;
11815   tree type;
11816   tree attributes;
11817   bool scoped_enum_p = false;
11818   bool has_underlying_type = false;
11819   tree underlying_type = NULL_TREE;
11820
11821   /* Parse tentatively so that we can back up if we don't find a
11822      enum-specifier.  */
11823   cp_parser_parse_tentatively (parser);
11824
11825   /* Caller guarantees that the current token is 'enum', an identifier
11826      possibly follows, and the token after that is an opening brace.
11827      If we don't have an identifier, fabricate an anonymous name for
11828      the enumeration being defined.  */
11829   cp_lexer_consume_token (parser->lexer);
11830
11831   /* Parse the "class" or "struct", which indicates a scoped
11832      enumeration type in C++0x.  */
11833   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11834       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11835     {
11836       if (cxx_dialect == cxx98)
11837         maybe_warn_cpp0x ("scoped enums");
11838
11839       /* Consume the `struct' or `class' token.  */
11840       cp_lexer_consume_token (parser->lexer);
11841
11842       scoped_enum_p = true;
11843     }
11844
11845   attributes = cp_parser_attributes_opt (parser);
11846
11847   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11848     identifier = cp_parser_identifier (parser);
11849   else
11850     identifier = make_anon_name ();
11851
11852   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11853   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11854     {
11855       cp_decl_specifier_seq type_specifiers;
11856
11857       /* At this point this is surely not elaborated type specifier.  */
11858       if (!cp_parser_parse_definitely (parser))
11859         return NULL_TREE;
11860
11861       if (cxx_dialect == cxx98)
11862         maybe_warn_cpp0x ("scoped enums");
11863
11864       /* Consume the `:'.  */
11865       cp_lexer_consume_token (parser->lexer);
11866
11867       has_underlying_type = true;
11868
11869       /* Parse the type-specifier-seq.  */
11870       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11871                                     &type_specifiers);
11872
11873       /* If that didn't work, stop.  */
11874       if (type_specifiers.type != error_mark_node)
11875         {
11876           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11877                                             /*initialized=*/0, NULL);
11878           if (underlying_type == error_mark_node)
11879             underlying_type = NULL_TREE;
11880         }
11881     }
11882
11883   /* Look for the `{' but don't consume it yet.  */
11884   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11885     {
11886       cp_parser_error (parser, "expected %<{%>");
11887       if (has_underlying_type)
11888         return NULL_TREE;
11889     }
11890
11891   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11892     return NULL_TREE;
11893
11894   /* Issue an error message if type-definitions are forbidden here.  */
11895   if (!cp_parser_check_type_definition (parser))
11896     type = error_mark_node;
11897   else
11898     /* Create the new type.  We do this before consuming the opening
11899        brace so the enum will be recorded as being on the line of its
11900        tag (or the 'enum' keyword, if there is no tag).  */
11901     type = start_enum (identifier, underlying_type, scoped_enum_p);
11902   
11903   /* Consume the opening brace.  */
11904   cp_lexer_consume_token (parser->lexer);
11905
11906   if (type == error_mark_node)
11907     {
11908       cp_parser_skip_to_end_of_block_or_statement (parser);
11909       return error_mark_node;
11910     }
11911
11912   /* If the next token is not '}', then there are some enumerators.  */
11913   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11914     cp_parser_enumerator_list (parser, type);
11915
11916   /* Consume the final '}'.  */
11917   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11918
11919   /* Look for trailing attributes to apply to this enumeration, and
11920      apply them if appropriate.  */
11921   if (cp_parser_allow_gnu_extensions_p (parser))
11922     {
11923       tree trailing_attr = cp_parser_attributes_opt (parser);
11924       trailing_attr = chainon (trailing_attr, attributes);
11925       cplus_decl_attributes (&type,
11926                              trailing_attr,
11927                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11928     }
11929
11930   /* Finish up the enumeration.  */
11931   finish_enum (type);
11932
11933   return type;
11934 }
11935
11936 /* Parse an enumerator-list.  The enumerators all have the indicated
11937    TYPE.
11938
11939    enumerator-list:
11940      enumerator-definition
11941      enumerator-list , enumerator-definition  */
11942
11943 static void
11944 cp_parser_enumerator_list (cp_parser* parser, tree type)
11945 {
11946   while (true)
11947     {
11948       /* Parse an enumerator-definition.  */
11949       cp_parser_enumerator_definition (parser, type);
11950
11951       /* If the next token is not a ',', we've reached the end of
11952          the list.  */
11953       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11954         break;
11955       /* Otherwise, consume the `,' and keep going.  */
11956       cp_lexer_consume_token (parser->lexer);
11957       /* If the next token is a `}', there is a trailing comma.  */
11958       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11959         {
11960           if (!in_system_header)
11961             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11962           break;
11963         }
11964     }
11965 }
11966
11967 /* Parse an enumerator-definition.  The enumerator has the indicated
11968    TYPE.
11969
11970    enumerator-definition:
11971      enumerator
11972      enumerator = constant-expression
11973
11974    enumerator:
11975      identifier  */
11976
11977 static void
11978 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11979 {
11980   tree identifier;
11981   tree value;
11982
11983   /* Look for the identifier.  */
11984   identifier = cp_parser_identifier (parser);
11985   if (identifier == error_mark_node)
11986     return;
11987
11988   /* If the next token is an '=', then there is an explicit value.  */
11989   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11990     {
11991       /* Consume the `=' token.  */
11992       cp_lexer_consume_token (parser->lexer);
11993       /* Parse the value.  */
11994       value = cp_parser_constant_expression (parser,
11995                                              /*allow_non_constant_p=*/false,
11996                                              NULL);
11997     }
11998   else
11999     value = NULL_TREE;
12000
12001   /* Create the enumerator.  */
12002   build_enumerator (identifier, value, type);
12003 }
12004
12005 /* Parse a namespace-name.
12006
12007    namespace-name:
12008      original-namespace-name
12009      namespace-alias
12010
12011    Returns the NAMESPACE_DECL for the namespace.  */
12012
12013 static tree
12014 cp_parser_namespace_name (cp_parser* parser)
12015 {
12016   tree identifier;
12017   tree namespace_decl;
12018
12019   cp_token *token = cp_lexer_peek_token (parser->lexer);
12020
12021   /* Get the name of the namespace.  */
12022   identifier = cp_parser_identifier (parser);
12023   if (identifier == error_mark_node)
12024     return error_mark_node;
12025
12026   /* Look up the identifier in the currently active scope.  Look only
12027      for namespaces, due to:
12028
12029        [basic.lookup.udir]
12030
12031        When looking up a namespace-name in a using-directive or alias
12032        definition, only namespace names are considered.
12033
12034      And:
12035
12036        [basic.lookup.qual]
12037
12038        During the lookup of a name preceding the :: scope resolution
12039        operator, object, function, and enumerator names are ignored.
12040
12041      (Note that cp_parser_qualifying_entity only calls this
12042      function if the token after the name is the scope resolution
12043      operator.)  */
12044   namespace_decl = cp_parser_lookup_name (parser, identifier,
12045                                           none_type,
12046                                           /*is_template=*/false,
12047                                           /*is_namespace=*/true,
12048                                           /*check_dependency=*/true,
12049                                           /*ambiguous_decls=*/NULL,
12050                                           token->location);
12051   /* If it's not a namespace, issue an error.  */
12052   if (namespace_decl == error_mark_node
12053       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12054     {
12055       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12056         error ("%H%qD is not a namespace-name", &token->location, identifier);
12057       cp_parser_error (parser, "expected namespace-name");
12058       namespace_decl = error_mark_node;
12059     }
12060
12061   return namespace_decl;
12062 }
12063
12064 /* Parse a namespace-definition.
12065
12066    namespace-definition:
12067      named-namespace-definition
12068      unnamed-namespace-definition
12069
12070    named-namespace-definition:
12071      original-namespace-definition
12072      extension-namespace-definition
12073
12074    original-namespace-definition:
12075      namespace identifier { namespace-body }
12076
12077    extension-namespace-definition:
12078      namespace original-namespace-name { namespace-body }
12079
12080    unnamed-namespace-definition:
12081      namespace { namespace-body } */
12082
12083 static void
12084 cp_parser_namespace_definition (cp_parser* parser)
12085 {
12086   tree identifier, attribs;
12087   bool has_visibility;
12088   bool is_inline;
12089
12090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12091     {
12092       is_inline = true;
12093       cp_lexer_consume_token (parser->lexer);
12094     }
12095   else
12096     is_inline = false;
12097
12098   /* Look for the `namespace' keyword.  */
12099   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12100
12101   /* Get the name of the namespace.  We do not attempt to distinguish
12102      between an original-namespace-definition and an
12103      extension-namespace-definition at this point.  The semantic
12104      analysis routines are responsible for that.  */
12105   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12106     identifier = cp_parser_identifier (parser);
12107   else
12108     identifier = NULL_TREE;
12109
12110   /* Parse any specified attributes.  */
12111   attribs = cp_parser_attributes_opt (parser);
12112
12113   /* Look for the `{' to start the namespace.  */
12114   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12115   /* Start the namespace.  */
12116   push_namespace (identifier);
12117
12118   /* "inline namespace" is equivalent to a stub namespace definition
12119      followed by a strong using directive.  */
12120   if (is_inline)
12121     {
12122       tree name_space = current_namespace;
12123       /* Set up namespace association.  */
12124       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12125         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12126                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12127       /* Import the contents of the inline namespace.  */
12128       pop_namespace ();
12129       do_using_directive (name_space);
12130       push_namespace (identifier);
12131     }
12132
12133   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12134
12135   /* Parse the body of the namespace.  */
12136   cp_parser_namespace_body (parser);
12137
12138 #ifdef HANDLE_PRAGMA_VISIBILITY
12139   if (has_visibility)
12140     pop_visibility ();
12141 #endif
12142
12143   /* Finish the namespace.  */
12144   pop_namespace ();
12145   /* Look for the final `}'.  */
12146   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12147 }
12148
12149 /* Parse a namespace-body.
12150
12151    namespace-body:
12152      declaration-seq [opt]  */
12153
12154 static void
12155 cp_parser_namespace_body (cp_parser* parser)
12156 {
12157   cp_parser_declaration_seq_opt (parser);
12158 }
12159
12160 /* Parse a namespace-alias-definition.
12161
12162    namespace-alias-definition:
12163      namespace identifier = qualified-namespace-specifier ;  */
12164
12165 static void
12166 cp_parser_namespace_alias_definition (cp_parser* parser)
12167 {
12168   tree identifier;
12169   tree namespace_specifier;
12170
12171   cp_token *token = cp_lexer_peek_token (parser->lexer);
12172
12173   /* Look for the `namespace' keyword.  */
12174   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12175   /* Look for the identifier.  */
12176   identifier = cp_parser_identifier (parser);
12177   if (identifier == error_mark_node)
12178     return;
12179   /* Look for the `=' token.  */
12180   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12181       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12182     {
12183       error ("%H%<namespace%> definition is not allowed here", &token->location);
12184       /* Skip the definition.  */
12185       cp_lexer_consume_token (parser->lexer);
12186       if (cp_parser_skip_to_closing_brace (parser))
12187         cp_lexer_consume_token (parser->lexer);
12188       return;
12189     }
12190   cp_parser_require (parser, CPP_EQ, "%<=%>");
12191   /* Look for the qualified-namespace-specifier.  */
12192   namespace_specifier
12193     = cp_parser_qualified_namespace_specifier (parser);
12194   /* Look for the `;' token.  */
12195   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12196
12197   /* Register the alias in the symbol table.  */
12198   do_namespace_alias (identifier, namespace_specifier);
12199 }
12200
12201 /* Parse a qualified-namespace-specifier.
12202
12203    qualified-namespace-specifier:
12204      :: [opt] nested-name-specifier [opt] namespace-name
12205
12206    Returns a NAMESPACE_DECL corresponding to the specified
12207    namespace.  */
12208
12209 static tree
12210 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12211 {
12212   /* Look for the optional `::'.  */
12213   cp_parser_global_scope_opt (parser,
12214                               /*current_scope_valid_p=*/false);
12215
12216   /* Look for the optional nested-name-specifier.  */
12217   cp_parser_nested_name_specifier_opt (parser,
12218                                        /*typename_keyword_p=*/false,
12219                                        /*check_dependency_p=*/true,
12220                                        /*type_p=*/false,
12221                                        /*is_declaration=*/true);
12222
12223   return cp_parser_namespace_name (parser);
12224 }
12225
12226 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12227    access declaration.
12228
12229    using-declaration:
12230      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12231      using :: unqualified-id ;  
12232
12233    access-declaration:
12234      qualified-id ;  
12235
12236    */
12237
12238 static bool
12239 cp_parser_using_declaration (cp_parser* parser, 
12240                              bool access_declaration_p)
12241 {
12242   cp_token *token;
12243   bool typename_p = false;
12244   bool global_scope_p;
12245   tree decl;
12246   tree identifier;
12247   tree qscope;
12248
12249   if (access_declaration_p)
12250     cp_parser_parse_tentatively (parser);
12251   else
12252     {
12253       /* Look for the `using' keyword.  */
12254       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12255       
12256       /* Peek at the next token.  */
12257       token = cp_lexer_peek_token (parser->lexer);
12258       /* See if it's `typename'.  */
12259       if (token->keyword == RID_TYPENAME)
12260         {
12261           /* Remember that we've seen it.  */
12262           typename_p = true;
12263           /* Consume the `typename' token.  */
12264           cp_lexer_consume_token (parser->lexer);
12265         }
12266     }
12267
12268   /* Look for the optional global scope qualification.  */
12269   global_scope_p
12270     = (cp_parser_global_scope_opt (parser,
12271                                    /*current_scope_valid_p=*/false)
12272        != NULL_TREE);
12273
12274   /* If we saw `typename', or didn't see `::', then there must be a
12275      nested-name-specifier present.  */
12276   if (typename_p || !global_scope_p)
12277     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12278                                               /*check_dependency_p=*/true,
12279                                               /*type_p=*/false,
12280                                               /*is_declaration=*/true);
12281   /* Otherwise, we could be in either of the two productions.  In that
12282      case, treat the nested-name-specifier as optional.  */
12283   else
12284     qscope = cp_parser_nested_name_specifier_opt (parser,
12285                                                   /*typename_keyword_p=*/false,
12286                                                   /*check_dependency_p=*/true,
12287                                                   /*type_p=*/false,
12288                                                   /*is_declaration=*/true);
12289   if (!qscope)
12290     qscope = global_namespace;
12291
12292   if (access_declaration_p && cp_parser_error_occurred (parser))
12293     /* Something has already gone wrong; there's no need to parse
12294        further.  Since an error has occurred, the return value of
12295        cp_parser_parse_definitely will be false, as required.  */
12296     return cp_parser_parse_definitely (parser);
12297
12298   token = cp_lexer_peek_token (parser->lexer);
12299   /* Parse the unqualified-id.  */
12300   identifier = cp_parser_unqualified_id (parser,
12301                                          /*template_keyword_p=*/false,
12302                                          /*check_dependency_p=*/true,
12303                                          /*declarator_p=*/true,
12304                                          /*optional_p=*/false);
12305
12306   if (access_declaration_p)
12307     {
12308       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12309         cp_parser_simulate_error (parser);
12310       if (!cp_parser_parse_definitely (parser))
12311         return false;
12312     }
12313
12314   /* The function we call to handle a using-declaration is different
12315      depending on what scope we are in.  */
12316   if (qscope == error_mark_node || identifier == error_mark_node)
12317     ;
12318   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12319            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12320     /* [namespace.udecl]
12321
12322        A using declaration shall not name a template-id.  */
12323     error ("%Ha template-id may not appear in a using-declaration",
12324             &token->location);
12325   else
12326     {
12327       if (at_class_scope_p ())
12328         {
12329           /* Create the USING_DECL.  */
12330           decl = do_class_using_decl (parser->scope, identifier);
12331
12332           if (check_for_bare_parameter_packs (decl))
12333             return false;
12334           else
12335             /* Add it to the list of members in this class.  */
12336             finish_member_declaration (decl);
12337         }
12338       else
12339         {
12340           decl = cp_parser_lookup_name_simple (parser,
12341                                                identifier,
12342                                                token->location);
12343           if (decl == error_mark_node)
12344             cp_parser_name_lookup_error (parser, identifier,
12345                                          decl, NULL,
12346                                          token->location);
12347           else if (check_for_bare_parameter_packs (decl))
12348             return false;
12349           else if (!at_namespace_scope_p ())
12350             do_local_using_decl (decl, qscope, identifier);
12351           else
12352             do_toplevel_using_decl (decl, qscope, identifier);
12353         }
12354     }
12355
12356   /* Look for the final `;'.  */
12357   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12358   
12359   return true;
12360 }
12361
12362 /* Parse a using-directive.
12363
12364    using-directive:
12365      using namespace :: [opt] nested-name-specifier [opt]
12366        namespace-name ;  */
12367
12368 static void
12369 cp_parser_using_directive (cp_parser* parser)
12370 {
12371   tree namespace_decl;
12372   tree attribs;
12373
12374   /* Look for the `using' keyword.  */
12375   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12376   /* And the `namespace' keyword.  */
12377   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12378   /* Look for the optional `::' operator.  */
12379   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12380   /* And the optional nested-name-specifier.  */
12381   cp_parser_nested_name_specifier_opt (parser,
12382                                        /*typename_keyword_p=*/false,
12383                                        /*check_dependency_p=*/true,
12384                                        /*type_p=*/false,
12385                                        /*is_declaration=*/true);
12386   /* Get the namespace being used.  */
12387   namespace_decl = cp_parser_namespace_name (parser);
12388   /* And any specified attributes.  */
12389   attribs = cp_parser_attributes_opt (parser);
12390   /* Update the symbol table.  */
12391   parse_using_directive (namespace_decl, attribs);
12392   /* Look for the final `;'.  */
12393   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12394 }
12395
12396 /* Parse an asm-definition.
12397
12398    asm-definition:
12399      asm ( string-literal ) ;
12400
12401    GNU Extension:
12402
12403    asm-definition:
12404      asm volatile [opt] ( string-literal ) ;
12405      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12406      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12407                           : asm-operand-list [opt] ) ;
12408      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12409                           : asm-operand-list [opt]
12410                           : asm-operand-list [opt] ) ;  */
12411
12412 static void
12413 cp_parser_asm_definition (cp_parser* parser)
12414 {
12415   tree string;
12416   tree outputs = NULL_TREE;
12417   tree inputs = NULL_TREE;
12418   tree clobbers = NULL_TREE;
12419   tree asm_stmt;
12420   bool volatile_p = false;
12421   bool extended_p = false;
12422   bool invalid_inputs_p = false;
12423   bool invalid_outputs_p = false;
12424
12425   /* Look for the `asm' keyword.  */
12426   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12427   /* See if the next token is `volatile'.  */
12428   if (cp_parser_allow_gnu_extensions_p (parser)
12429       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12430     {
12431       /* Remember that we saw the `volatile' keyword.  */
12432       volatile_p = true;
12433       /* Consume the token.  */
12434       cp_lexer_consume_token (parser->lexer);
12435     }
12436   /* Look for the opening `('.  */
12437   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12438     return;
12439   /* Look for the string.  */
12440   string = cp_parser_string_literal (parser, false, false);
12441   if (string == error_mark_node)
12442     {
12443       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12444                                              /*consume_paren=*/true);
12445       return;
12446     }
12447
12448   /* If we're allowing GNU extensions, check for the extended assembly
12449      syntax.  Unfortunately, the `:' tokens need not be separated by
12450      a space in C, and so, for compatibility, we tolerate that here
12451      too.  Doing that means that we have to treat the `::' operator as
12452      two `:' tokens.  */
12453   if (cp_parser_allow_gnu_extensions_p (parser)
12454       && parser->in_function_body
12455       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12456           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12457     {
12458       bool inputs_p = false;
12459       bool clobbers_p = false;
12460
12461       /* The extended syntax was used.  */
12462       extended_p = true;
12463
12464       /* Look for outputs.  */
12465       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12466         {
12467           /* Consume the `:'.  */
12468           cp_lexer_consume_token (parser->lexer);
12469           /* Parse the output-operands.  */
12470           if (cp_lexer_next_token_is_not (parser->lexer,
12471                                           CPP_COLON)
12472               && cp_lexer_next_token_is_not (parser->lexer,
12473                                              CPP_SCOPE)
12474               && cp_lexer_next_token_is_not (parser->lexer,
12475                                              CPP_CLOSE_PAREN))
12476             outputs = cp_parser_asm_operand_list (parser);
12477
12478             if (outputs == error_mark_node)
12479               invalid_outputs_p = true;
12480         }
12481       /* If the next token is `::', there are no outputs, and the
12482          next token is the beginning of the inputs.  */
12483       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12484         /* The inputs are coming next.  */
12485         inputs_p = true;
12486
12487       /* Look for inputs.  */
12488       if (inputs_p
12489           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12490         {
12491           /* Consume the `:' or `::'.  */
12492           cp_lexer_consume_token (parser->lexer);
12493           /* Parse the output-operands.  */
12494           if (cp_lexer_next_token_is_not (parser->lexer,
12495                                           CPP_COLON)
12496               && cp_lexer_next_token_is_not (parser->lexer,
12497                                              CPP_CLOSE_PAREN))
12498             inputs = cp_parser_asm_operand_list (parser);
12499
12500             if (inputs == error_mark_node)
12501               invalid_inputs_p = true;
12502         }
12503       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12504         /* The clobbers are coming next.  */
12505         clobbers_p = true;
12506
12507       /* Look for clobbers.  */
12508       if (clobbers_p
12509           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12510         {
12511           /* Consume the `:' or `::'.  */
12512           cp_lexer_consume_token (parser->lexer);
12513           /* Parse the clobbers.  */
12514           if (cp_lexer_next_token_is_not (parser->lexer,
12515                                           CPP_CLOSE_PAREN))
12516             clobbers = cp_parser_asm_clobber_list (parser);
12517         }
12518     }
12519   /* Look for the closing `)'.  */
12520   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12521     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12522                                            /*consume_paren=*/true);
12523   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12524
12525   if (!invalid_inputs_p && !invalid_outputs_p)
12526     {
12527       /* Create the ASM_EXPR.  */
12528       if (parser->in_function_body)
12529         {
12530           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12531                                       inputs, clobbers);
12532           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12533           if (!extended_p)
12534             {
12535               tree temp = asm_stmt;
12536               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12537                 temp = TREE_OPERAND (temp, 0);
12538
12539               ASM_INPUT_P (temp) = 1;
12540             }
12541         }
12542       else
12543         cgraph_add_asm_node (string);
12544     }
12545 }
12546
12547 /* Declarators [gram.dcl.decl] */
12548
12549 /* Parse an init-declarator.
12550
12551    init-declarator:
12552      declarator initializer [opt]
12553
12554    GNU Extension:
12555
12556    init-declarator:
12557      declarator asm-specification [opt] attributes [opt] initializer [opt]
12558
12559    function-definition:
12560      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12561        function-body
12562      decl-specifier-seq [opt] declarator function-try-block
12563
12564    GNU Extension:
12565
12566    function-definition:
12567      __extension__ function-definition
12568
12569    The DECL_SPECIFIERS apply to this declarator.  Returns a
12570    representation of the entity declared.  If MEMBER_P is TRUE, then
12571    this declarator appears in a class scope.  The new DECL created by
12572    this declarator is returned.
12573
12574    The CHECKS are access checks that should be performed once we know
12575    what entity is being declared (and, therefore, what classes have
12576    befriended it).
12577
12578    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12579    for a function-definition here as well.  If the declarator is a
12580    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12581    be TRUE upon return.  By that point, the function-definition will
12582    have been completely parsed.
12583
12584    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12585    is FALSE.  */
12586
12587 static tree
12588 cp_parser_init_declarator (cp_parser* parser,
12589                            cp_decl_specifier_seq *decl_specifiers,
12590                            VEC (deferred_access_check,gc)* checks,
12591                            bool function_definition_allowed_p,
12592                            bool member_p,
12593                            int declares_class_or_enum,
12594                            bool* function_definition_p)
12595 {
12596   cp_token *token = NULL, *asm_spec_start_token = NULL,
12597            *attributes_start_token = NULL;
12598   cp_declarator *declarator;
12599   tree prefix_attributes;
12600   tree attributes;
12601   tree asm_specification;
12602   tree initializer;
12603   tree decl = NULL_TREE;
12604   tree scope;
12605   int is_initialized;
12606   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12607      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12608      "(...)".  */
12609   enum cpp_ttype initialization_kind;
12610   bool is_direct_init = false;
12611   bool is_non_constant_init;
12612   int ctor_dtor_or_conv_p;
12613   bool friend_p;
12614   tree pushed_scope = NULL;
12615
12616   /* Gather the attributes that were provided with the
12617      decl-specifiers.  */
12618   prefix_attributes = decl_specifiers->attributes;
12619
12620   /* Assume that this is not the declarator for a function
12621      definition.  */
12622   if (function_definition_p)
12623     *function_definition_p = false;
12624
12625   /* Defer access checks while parsing the declarator; we cannot know
12626      what names are accessible until we know what is being
12627      declared.  */
12628   resume_deferring_access_checks ();
12629
12630   /* Parse the declarator.  */
12631   token = cp_lexer_peek_token (parser->lexer);
12632   declarator
12633     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12634                             &ctor_dtor_or_conv_p,
12635                             /*parenthesized_p=*/NULL,
12636                             /*member_p=*/false);
12637   /* Gather up the deferred checks.  */
12638   stop_deferring_access_checks ();
12639
12640   /* If the DECLARATOR was erroneous, there's no need to go
12641      further.  */
12642   if (declarator == cp_error_declarator)
12643     return error_mark_node;
12644
12645   /* Check that the number of template-parameter-lists is OK.  */
12646   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12647                                                        token->location))
12648     return error_mark_node;
12649
12650   if (declares_class_or_enum & 2)
12651     cp_parser_check_for_definition_in_return_type (declarator,
12652                                                    decl_specifiers->type,
12653                                                    decl_specifiers->type_location);
12654
12655   /* Figure out what scope the entity declared by the DECLARATOR is
12656      located in.  `grokdeclarator' sometimes changes the scope, so
12657      we compute it now.  */
12658   scope = get_scope_of_declarator (declarator);
12659
12660   /* If we're allowing GNU extensions, look for an asm-specification
12661      and attributes.  */
12662   if (cp_parser_allow_gnu_extensions_p (parser))
12663     {
12664       /* Look for an asm-specification.  */
12665       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12666       asm_specification = cp_parser_asm_specification_opt (parser);
12667       /* And attributes.  */
12668       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12669       attributes = cp_parser_attributes_opt (parser);
12670     }
12671   else
12672     {
12673       asm_specification = NULL_TREE;
12674       attributes = NULL_TREE;
12675     }
12676
12677   /* Peek at the next token.  */
12678   token = cp_lexer_peek_token (parser->lexer);
12679   /* Check to see if the token indicates the start of a
12680      function-definition.  */
12681   if (function_declarator_p (declarator)
12682       && cp_parser_token_starts_function_definition_p (token))
12683     {
12684       if (!function_definition_allowed_p)
12685         {
12686           /* If a function-definition should not appear here, issue an
12687              error message.  */
12688           cp_parser_error (parser,
12689                            "a function-definition is not allowed here");
12690           return error_mark_node;
12691         }
12692       else
12693         {
12694           location_t func_brace_location
12695             = cp_lexer_peek_token (parser->lexer)->location;
12696
12697           /* Neither attributes nor an asm-specification are allowed
12698              on a function-definition.  */
12699           if (asm_specification)
12700             error ("%Han asm-specification is not allowed "
12701                    "on a function-definition",
12702                    &asm_spec_start_token->location);
12703           if (attributes)
12704             error ("%Hattributes are not allowed on a function-definition",
12705                    &attributes_start_token->location);
12706           /* This is a function-definition.  */
12707           *function_definition_p = true;
12708
12709           /* Parse the function definition.  */
12710           if (member_p)
12711             decl = cp_parser_save_member_function_body (parser,
12712                                                         decl_specifiers,
12713                                                         declarator,
12714                                                         prefix_attributes);
12715           else
12716             decl
12717               = (cp_parser_function_definition_from_specifiers_and_declarator
12718                  (parser, decl_specifiers, prefix_attributes, declarator));
12719
12720           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12721             {
12722               /* This is where the prologue starts...  */
12723               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12724                 = func_brace_location;
12725             }
12726
12727           return decl;
12728         }
12729     }
12730
12731   /* [dcl.dcl]
12732
12733      Only in function declarations for constructors, destructors, and
12734      type conversions can the decl-specifier-seq be omitted.
12735
12736      We explicitly postpone this check past the point where we handle
12737      function-definitions because we tolerate function-definitions
12738      that are missing their return types in some modes.  */
12739   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12740     {
12741       cp_parser_error (parser,
12742                        "expected constructor, destructor, or type conversion");
12743       return error_mark_node;
12744     }
12745
12746   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12747   if (token->type == CPP_EQ
12748       || token->type == CPP_OPEN_PAREN
12749       || token->type == CPP_OPEN_BRACE)
12750     {
12751       is_initialized = SD_INITIALIZED;
12752       initialization_kind = token->type;
12753
12754       if (token->type == CPP_EQ
12755           && function_declarator_p (declarator))
12756         {
12757           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12758           if (t2->keyword == RID_DEFAULT)
12759             is_initialized = SD_DEFAULTED;
12760           else if (t2->keyword == RID_DELETE)
12761             is_initialized = SD_DELETED;
12762         }
12763     }
12764   else
12765     {
12766       /* If the init-declarator isn't initialized and isn't followed by a
12767          `,' or `;', it's not a valid init-declarator.  */
12768       if (token->type != CPP_COMMA
12769           && token->type != CPP_SEMICOLON)
12770         {
12771           cp_parser_error (parser, "expected initializer");
12772           return error_mark_node;
12773         }
12774       is_initialized = SD_UNINITIALIZED;
12775       initialization_kind = CPP_EOF;
12776     }
12777
12778   /* Because start_decl has side-effects, we should only call it if we
12779      know we're going ahead.  By this point, we know that we cannot
12780      possibly be looking at any other construct.  */
12781   cp_parser_commit_to_tentative_parse (parser);
12782
12783   /* If the decl specifiers were bad, issue an error now that we're
12784      sure this was intended to be a declarator.  Then continue
12785      declaring the variable(s), as int, to try to cut down on further
12786      errors.  */
12787   if (decl_specifiers->any_specifiers_p
12788       && decl_specifiers->type == error_mark_node)
12789     {
12790       cp_parser_error (parser, "invalid type in declaration");
12791       decl_specifiers->type = integer_type_node;
12792     }
12793
12794   /* Check to see whether or not this declaration is a friend.  */
12795   friend_p = cp_parser_friend_p (decl_specifiers);
12796
12797   /* Enter the newly declared entry in the symbol table.  If we're
12798      processing a declaration in a class-specifier, we wait until
12799      after processing the initializer.  */
12800   if (!member_p)
12801     {
12802       if (parser->in_unbraced_linkage_specification_p)
12803         decl_specifiers->storage_class = sc_extern;
12804       decl = start_decl (declarator, decl_specifiers,
12805                          is_initialized, attributes, prefix_attributes,
12806                          &pushed_scope);
12807     }
12808   else if (scope)
12809     /* Enter the SCOPE.  That way unqualified names appearing in the
12810        initializer will be looked up in SCOPE.  */
12811     pushed_scope = push_scope (scope);
12812
12813   /* Perform deferred access control checks, now that we know in which
12814      SCOPE the declared entity resides.  */
12815   if (!member_p && decl)
12816     {
12817       tree saved_current_function_decl = NULL_TREE;
12818
12819       /* If the entity being declared is a function, pretend that we
12820          are in its scope.  If it is a `friend', it may have access to
12821          things that would not otherwise be accessible.  */
12822       if (TREE_CODE (decl) == FUNCTION_DECL)
12823         {
12824           saved_current_function_decl = current_function_decl;
12825           current_function_decl = decl;
12826         }
12827
12828       /* Perform access checks for template parameters.  */
12829       cp_parser_perform_template_parameter_access_checks (checks);
12830
12831       /* Perform the access control checks for the declarator and the
12832          decl-specifiers.  */
12833       perform_deferred_access_checks ();
12834
12835       /* Restore the saved value.  */
12836       if (TREE_CODE (decl) == FUNCTION_DECL)
12837         current_function_decl = saved_current_function_decl;
12838     }
12839
12840   /* Parse the initializer.  */
12841   initializer = NULL_TREE;
12842   is_direct_init = false;
12843   is_non_constant_init = true;
12844   if (is_initialized)
12845     {
12846       if (function_declarator_p (declarator))
12847         {
12848           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12849            if (initialization_kind == CPP_EQ)
12850              initializer = cp_parser_pure_specifier (parser);
12851            else
12852              {
12853                /* If the declaration was erroneous, we don't really
12854                   know what the user intended, so just silently
12855                   consume the initializer.  */
12856                if (decl != error_mark_node)
12857                  error ("%Hinitializer provided for function",
12858                         &initializer_start_token->location);
12859                cp_parser_skip_to_closing_parenthesis (parser,
12860                                                       /*recovering=*/true,
12861                                                       /*or_comma=*/false,
12862                                                       /*consume_paren=*/true);
12863              }
12864         }
12865       else
12866         initializer = cp_parser_initializer (parser,
12867                                              &is_direct_init,
12868                                              &is_non_constant_init);
12869     }
12870
12871   /* The old parser allows attributes to appear after a parenthesized
12872      initializer.  Mark Mitchell proposed removing this functionality
12873      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12874      attributes -- but ignores them.  */
12875   if (cp_parser_allow_gnu_extensions_p (parser)
12876       && initialization_kind == CPP_OPEN_PAREN)
12877     if (cp_parser_attributes_opt (parser))
12878       warning (OPT_Wattributes,
12879                "attributes after parenthesized initializer ignored");
12880
12881   /* For an in-class declaration, use `grokfield' to create the
12882      declaration.  */
12883   if (member_p)
12884     {
12885       if (pushed_scope)
12886         {
12887           pop_scope (pushed_scope);
12888           pushed_scope = false;
12889         }
12890       decl = grokfield (declarator, decl_specifiers,
12891                         initializer, !is_non_constant_init,
12892                         /*asmspec=*/NULL_TREE,
12893                         prefix_attributes);
12894       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12895         cp_parser_save_default_args (parser, decl);
12896     }
12897
12898   /* Finish processing the declaration.  But, skip friend
12899      declarations.  */
12900   if (!friend_p && decl && decl != error_mark_node)
12901     {
12902       cp_finish_decl (decl,
12903                       initializer, !is_non_constant_init,
12904                       asm_specification,
12905                       /* If the initializer is in parentheses, then this is
12906                          a direct-initialization, which means that an
12907                          `explicit' constructor is OK.  Otherwise, an
12908                          `explicit' constructor cannot be used.  */
12909                       ((is_direct_init || !is_initialized)
12910                        ? 0 : LOOKUP_ONLYCONVERTING));
12911     }
12912   else if ((cxx_dialect != cxx98) && friend_p
12913            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12914     /* Core issue #226 (C++0x only): A default template-argument
12915        shall not be specified in a friend class template
12916        declaration. */
12917     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12918                              /*is_partial=*/0, /*is_friend_decl=*/1);
12919
12920   if (!friend_p && pushed_scope)
12921     pop_scope (pushed_scope);
12922
12923   return decl;
12924 }
12925
12926 /* Parse a declarator.
12927
12928    declarator:
12929      direct-declarator
12930      ptr-operator declarator
12931
12932    abstract-declarator:
12933      ptr-operator abstract-declarator [opt]
12934      direct-abstract-declarator
12935
12936    GNU Extensions:
12937
12938    declarator:
12939      attributes [opt] direct-declarator
12940      attributes [opt] ptr-operator declarator
12941
12942    abstract-declarator:
12943      attributes [opt] ptr-operator abstract-declarator [opt]
12944      attributes [opt] direct-abstract-declarator
12945
12946    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12947    detect constructor, destructor or conversion operators. It is set
12948    to -1 if the declarator is a name, and +1 if it is a
12949    function. Otherwise it is set to zero. Usually you just want to
12950    test for >0, but internally the negative value is used.
12951
12952    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12953    a decl-specifier-seq unless it declares a constructor, destructor,
12954    or conversion.  It might seem that we could check this condition in
12955    semantic analysis, rather than parsing, but that makes it difficult
12956    to handle something like `f()'.  We want to notice that there are
12957    no decl-specifiers, and therefore realize that this is an
12958    expression, not a declaration.)
12959
12960    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12961    the declarator is a direct-declarator of the form "(...)".
12962
12963    MEMBER_P is true iff this declarator is a member-declarator.  */
12964
12965 static cp_declarator *
12966 cp_parser_declarator (cp_parser* parser,
12967                       cp_parser_declarator_kind dcl_kind,
12968                       int* ctor_dtor_or_conv_p,
12969                       bool* parenthesized_p,
12970                       bool member_p)
12971 {
12972   cp_token *token;
12973   cp_declarator *declarator;
12974   enum tree_code code;
12975   cp_cv_quals cv_quals;
12976   tree class_type;
12977   tree attributes = NULL_TREE;
12978
12979   /* Assume this is not a constructor, destructor, or type-conversion
12980      operator.  */
12981   if (ctor_dtor_or_conv_p)
12982     *ctor_dtor_or_conv_p = 0;
12983
12984   if (cp_parser_allow_gnu_extensions_p (parser))
12985     attributes = cp_parser_attributes_opt (parser);
12986
12987   /* Peek at the next token.  */
12988   token = cp_lexer_peek_token (parser->lexer);
12989
12990   /* Check for the ptr-operator production.  */
12991   cp_parser_parse_tentatively (parser);
12992   /* Parse the ptr-operator.  */
12993   code = cp_parser_ptr_operator (parser,
12994                                  &class_type,
12995                                  &cv_quals);
12996   /* If that worked, then we have a ptr-operator.  */
12997   if (cp_parser_parse_definitely (parser))
12998     {
12999       /* If a ptr-operator was found, then this declarator was not
13000          parenthesized.  */
13001       if (parenthesized_p)
13002         *parenthesized_p = true;
13003       /* The dependent declarator is optional if we are parsing an
13004          abstract-declarator.  */
13005       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13006         cp_parser_parse_tentatively (parser);
13007
13008       /* Parse the dependent declarator.  */
13009       declarator = cp_parser_declarator (parser, dcl_kind,
13010                                          /*ctor_dtor_or_conv_p=*/NULL,
13011                                          /*parenthesized_p=*/NULL,
13012                                          /*member_p=*/false);
13013
13014       /* If we are parsing an abstract-declarator, we must handle the
13015          case where the dependent declarator is absent.  */
13016       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13017           && !cp_parser_parse_definitely (parser))
13018         declarator = NULL;
13019
13020       declarator = cp_parser_make_indirect_declarator
13021         (code, class_type, cv_quals, declarator);
13022     }
13023   /* Everything else is a direct-declarator.  */
13024   else
13025     {
13026       if (parenthesized_p)
13027         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13028                                                    CPP_OPEN_PAREN);
13029       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13030                                                 ctor_dtor_or_conv_p,
13031                                                 member_p);
13032     }
13033
13034   if (attributes && declarator && declarator != cp_error_declarator)
13035     declarator->attributes = attributes;
13036
13037   return declarator;
13038 }
13039
13040 /* Parse a direct-declarator or direct-abstract-declarator.
13041
13042    direct-declarator:
13043      declarator-id
13044      direct-declarator ( parameter-declaration-clause )
13045        cv-qualifier-seq [opt]
13046        exception-specification [opt]
13047      direct-declarator [ constant-expression [opt] ]
13048      ( declarator )
13049
13050    direct-abstract-declarator:
13051      direct-abstract-declarator [opt]
13052        ( parameter-declaration-clause )
13053        cv-qualifier-seq [opt]
13054        exception-specification [opt]
13055      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13056      ( abstract-declarator )
13057
13058    Returns a representation of the declarator.  DCL_KIND is
13059    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13060    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13061    we are parsing a direct-declarator.  It is
13062    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13063    of ambiguity we prefer an abstract declarator, as per
13064    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13065    cp_parser_declarator.  */
13066
13067 static cp_declarator *
13068 cp_parser_direct_declarator (cp_parser* parser,
13069                              cp_parser_declarator_kind dcl_kind,
13070                              int* ctor_dtor_or_conv_p,
13071                              bool member_p)
13072 {
13073   cp_token *token;
13074   cp_declarator *declarator = NULL;
13075   tree scope = NULL_TREE;
13076   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13077   bool saved_in_declarator_p = parser->in_declarator_p;
13078   bool first = true;
13079   tree pushed_scope = NULL_TREE;
13080
13081   while (true)
13082     {
13083       /* Peek at the next token.  */
13084       token = cp_lexer_peek_token (parser->lexer);
13085       if (token->type == CPP_OPEN_PAREN)
13086         {
13087           /* This is either a parameter-declaration-clause, or a
13088              parenthesized declarator. When we know we are parsing a
13089              named declarator, it must be a parenthesized declarator
13090              if FIRST is true. For instance, `(int)' is a
13091              parameter-declaration-clause, with an omitted
13092              direct-abstract-declarator. But `((*))', is a
13093              parenthesized abstract declarator. Finally, when T is a
13094              template parameter `(T)' is a
13095              parameter-declaration-clause, and not a parenthesized
13096              named declarator.
13097
13098              We first try and parse a parameter-declaration-clause,
13099              and then try a nested declarator (if FIRST is true).
13100
13101              It is not an error for it not to be a
13102              parameter-declaration-clause, even when FIRST is
13103              false. Consider,
13104
13105                int i (int);
13106                int i (3);
13107
13108              The first is the declaration of a function while the
13109              second is the definition of a variable, including its
13110              initializer.
13111
13112              Having seen only the parenthesis, we cannot know which of
13113              these two alternatives should be selected.  Even more
13114              complex are examples like:
13115
13116                int i (int (a));
13117                int i (int (3));
13118
13119              The former is a function-declaration; the latter is a
13120              variable initialization.
13121
13122              Thus again, we try a parameter-declaration-clause, and if
13123              that fails, we back out and return.  */
13124
13125           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13126             {
13127               tree params;
13128               unsigned saved_num_template_parameter_lists;
13129               bool is_declarator = false;
13130               tree t;
13131
13132               /* In a member-declarator, the only valid interpretation
13133                  of a parenthesis is the start of a
13134                  parameter-declaration-clause.  (It is invalid to
13135                  initialize a static data member with a parenthesized
13136                  initializer; only the "=" form of initialization is
13137                  permitted.)  */
13138               if (!member_p)
13139                 cp_parser_parse_tentatively (parser);
13140
13141               /* Consume the `('.  */
13142               cp_lexer_consume_token (parser->lexer);
13143               if (first)
13144                 {
13145                   /* If this is going to be an abstract declarator, we're
13146                      in a declarator and we can't have default args.  */
13147                   parser->default_arg_ok_p = false;
13148                   parser->in_declarator_p = true;
13149                 }
13150
13151               /* Inside the function parameter list, surrounding
13152                  template-parameter-lists do not apply.  */
13153               saved_num_template_parameter_lists
13154                 = parser->num_template_parameter_lists;
13155               parser->num_template_parameter_lists = 0;
13156
13157               begin_scope (sk_function_parms, NULL_TREE);
13158
13159               /* Parse the parameter-declaration-clause.  */
13160               params = cp_parser_parameter_declaration_clause (parser);
13161
13162               parser->num_template_parameter_lists
13163                 = saved_num_template_parameter_lists;
13164
13165               /* If all went well, parse the cv-qualifier-seq and the
13166                  exception-specification.  */
13167               if (member_p || cp_parser_parse_definitely (parser))
13168                 {
13169                   cp_cv_quals cv_quals;
13170                   tree exception_specification;
13171                   tree late_return;
13172
13173                   is_declarator = true;
13174
13175                   if (ctor_dtor_or_conv_p)
13176                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13177                   first = false;
13178                   /* Consume the `)'.  */
13179                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13180
13181                   /* Parse the cv-qualifier-seq.  */
13182                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13183                   /* And the exception-specification.  */
13184                   exception_specification
13185                     = cp_parser_exception_specification_opt (parser);
13186
13187                   late_return
13188                     = cp_parser_late_return_type_opt (parser);
13189
13190                   /* Create the function-declarator.  */
13191                   declarator = make_call_declarator (declarator,
13192                                                      params,
13193                                                      cv_quals,
13194                                                      exception_specification,
13195                                                      late_return);
13196                   /* Any subsequent parameter lists are to do with
13197                      return type, so are not those of the declared
13198                      function.  */
13199                   parser->default_arg_ok_p = false;
13200                 }
13201
13202               /* Remove the function parms from scope.  */
13203               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13204                 pop_binding (DECL_NAME (t), t);
13205               leave_scope();
13206
13207               if (is_declarator)
13208                 /* Repeat the main loop.  */
13209                 continue;
13210             }
13211
13212           /* If this is the first, we can try a parenthesized
13213              declarator.  */
13214           if (first)
13215             {
13216               bool saved_in_type_id_in_expr_p;
13217
13218               parser->default_arg_ok_p = saved_default_arg_ok_p;
13219               parser->in_declarator_p = saved_in_declarator_p;
13220
13221               /* Consume the `('.  */
13222               cp_lexer_consume_token (parser->lexer);
13223               /* Parse the nested declarator.  */
13224               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13225               parser->in_type_id_in_expr_p = true;
13226               declarator
13227                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13228                                         /*parenthesized_p=*/NULL,
13229                                         member_p);
13230               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13231               first = false;
13232               /* Expect a `)'.  */
13233               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13234                 declarator = cp_error_declarator;
13235               if (declarator == cp_error_declarator)
13236                 break;
13237
13238               goto handle_declarator;
13239             }
13240           /* Otherwise, we must be done.  */
13241           else
13242             break;
13243         }
13244       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13245                && token->type == CPP_OPEN_SQUARE)
13246         {
13247           /* Parse an array-declarator.  */
13248           tree bounds;
13249
13250           if (ctor_dtor_or_conv_p)
13251             *ctor_dtor_or_conv_p = 0;
13252
13253           first = false;
13254           parser->default_arg_ok_p = false;
13255           parser->in_declarator_p = true;
13256           /* Consume the `['.  */
13257           cp_lexer_consume_token (parser->lexer);
13258           /* Peek at the next token.  */
13259           token = cp_lexer_peek_token (parser->lexer);
13260           /* If the next token is `]', then there is no
13261              constant-expression.  */
13262           if (token->type != CPP_CLOSE_SQUARE)
13263             {
13264               bool non_constant_p;
13265
13266               bounds
13267                 = cp_parser_constant_expression (parser,
13268                                                  /*allow_non_constant=*/true,
13269                                                  &non_constant_p);
13270               if (!non_constant_p)
13271                 bounds = fold_non_dependent_expr (bounds);
13272               /* Normally, the array bound must be an integral constant
13273                  expression.  However, as an extension, we allow VLAs
13274                  in function scopes.  */
13275               else if (!parser->in_function_body)
13276                 {
13277                   error ("%Harray bound is not an integer constant",
13278                          &token->location);
13279                   bounds = error_mark_node;
13280                 }
13281             }
13282           else
13283             bounds = NULL_TREE;
13284           /* Look for the closing `]'.  */
13285           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13286             {
13287               declarator = cp_error_declarator;
13288               break;
13289             }
13290
13291           declarator = make_array_declarator (declarator, bounds);
13292         }
13293       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13294         {
13295           tree qualifying_scope;
13296           tree unqualified_name;
13297           special_function_kind sfk;
13298           bool abstract_ok;
13299           bool pack_expansion_p = false;
13300           cp_token *declarator_id_start_token;
13301
13302           /* Parse a declarator-id */
13303           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13304           if (abstract_ok)
13305             {
13306               cp_parser_parse_tentatively (parser);
13307
13308               /* If we see an ellipsis, we should be looking at a
13309                  parameter pack. */
13310               if (token->type == CPP_ELLIPSIS)
13311                 {
13312                   /* Consume the `...' */
13313                   cp_lexer_consume_token (parser->lexer);
13314
13315                   pack_expansion_p = true;
13316                 }
13317             }
13318
13319           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13320           unqualified_name
13321             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13322           qualifying_scope = parser->scope;
13323           if (abstract_ok)
13324             {
13325               bool okay = false;
13326
13327               if (!unqualified_name && pack_expansion_p)
13328                 {
13329                   /* Check whether an error occurred. */
13330                   okay = !cp_parser_error_occurred (parser);
13331
13332                   /* We already consumed the ellipsis to mark a
13333                      parameter pack, but we have no way to report it,
13334                      so abort the tentative parse. We will be exiting
13335                      immediately anyway. */
13336                   cp_parser_abort_tentative_parse (parser);
13337                 }
13338               else
13339                 okay = cp_parser_parse_definitely (parser);
13340
13341               if (!okay)
13342                 unqualified_name = error_mark_node;
13343               else if (unqualified_name
13344                        && (qualifying_scope
13345                            || (TREE_CODE (unqualified_name)
13346                                != IDENTIFIER_NODE)))
13347                 {
13348                   cp_parser_error (parser, "expected unqualified-id");
13349                   unqualified_name = error_mark_node;
13350                 }
13351             }
13352
13353           if (!unqualified_name)
13354             return NULL;
13355           if (unqualified_name == error_mark_node)
13356             {
13357               declarator = cp_error_declarator;
13358               pack_expansion_p = false;
13359               declarator->parameter_pack_p = false;
13360               break;
13361             }
13362
13363           if (qualifying_scope && at_namespace_scope_p ()
13364               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13365             {
13366               /* In the declaration of a member of a template class
13367                  outside of the class itself, the SCOPE will sometimes
13368                  be a TYPENAME_TYPE.  For example, given:
13369
13370                  template <typename T>
13371                  int S<T>::R::i = 3;
13372
13373                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13374                  this context, we must resolve S<T>::R to an ordinary
13375                  type, rather than a typename type.
13376
13377                  The reason we normally avoid resolving TYPENAME_TYPEs
13378                  is that a specialization of `S' might render
13379                  `S<T>::R' not a type.  However, if `S' is
13380                  specialized, then this `i' will not be used, so there
13381                  is no harm in resolving the types here.  */
13382               tree type;
13383
13384               /* Resolve the TYPENAME_TYPE.  */
13385               type = resolve_typename_type (qualifying_scope,
13386                                             /*only_current_p=*/false);
13387               /* If that failed, the declarator is invalid.  */
13388               if (TREE_CODE (type) == TYPENAME_TYPE)
13389                 error ("%H%<%T::%E%> is not a type",
13390                        &declarator_id_start_token->location,
13391                        TYPE_CONTEXT (qualifying_scope),
13392                        TYPE_IDENTIFIER (qualifying_scope));
13393               qualifying_scope = type;
13394             }
13395
13396           sfk = sfk_none;
13397
13398           if (unqualified_name)
13399             {
13400               tree class_type;
13401
13402               if (qualifying_scope
13403                   && CLASS_TYPE_P (qualifying_scope))
13404                 class_type = qualifying_scope;
13405               else
13406                 class_type = current_class_type;
13407
13408               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13409                 {
13410                   tree name_type = TREE_TYPE (unqualified_name);
13411                   if (class_type && same_type_p (name_type, class_type))
13412                     {
13413                       if (qualifying_scope
13414                           && CLASSTYPE_USE_TEMPLATE (name_type))
13415                         {
13416                           error ("%Hinvalid use of constructor as a template",
13417                                  &declarator_id_start_token->location);
13418                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13419                                   "name the constructor in a qualified name",
13420                                   class_type,
13421                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13422                                   class_type, name_type);
13423                           declarator = cp_error_declarator;
13424                           break;
13425                         }
13426                       else
13427                         unqualified_name = constructor_name (class_type);
13428                     }
13429                   else
13430                     {
13431                       /* We do not attempt to print the declarator
13432                          here because we do not have enough
13433                          information about its original syntactic
13434                          form.  */
13435                       cp_parser_error (parser, "invalid declarator");
13436                       declarator = cp_error_declarator;
13437                       break;
13438                     }
13439                 }
13440
13441               if (class_type)
13442                 {
13443                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13444                     sfk = sfk_destructor;
13445                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13446                     sfk = sfk_conversion;
13447                   else if (/* There's no way to declare a constructor
13448                               for an anonymous type, even if the type
13449                               got a name for linkage purposes.  */
13450                            !TYPE_WAS_ANONYMOUS (class_type)
13451                            && constructor_name_p (unqualified_name,
13452                                                   class_type))
13453                     {
13454                       unqualified_name = constructor_name (class_type);
13455                       sfk = sfk_constructor;
13456                     }
13457
13458                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13459                     *ctor_dtor_or_conv_p = -1;
13460                 }
13461             }
13462           declarator = make_id_declarator (qualifying_scope,
13463                                            unqualified_name,
13464                                            sfk);
13465           declarator->id_loc = token->location;
13466           declarator->parameter_pack_p = pack_expansion_p;
13467
13468           if (pack_expansion_p)
13469             maybe_warn_variadic_templates ();
13470
13471         handle_declarator:;
13472           scope = get_scope_of_declarator (declarator);
13473           if (scope)
13474             /* Any names that appear after the declarator-id for a
13475                member are looked up in the containing scope.  */
13476             pushed_scope = push_scope (scope);
13477           parser->in_declarator_p = true;
13478           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13479               || (declarator && declarator->kind == cdk_id))
13480             /* Default args are only allowed on function
13481                declarations.  */
13482             parser->default_arg_ok_p = saved_default_arg_ok_p;
13483           else
13484             parser->default_arg_ok_p = false;
13485
13486           first = false;
13487         }
13488       /* We're done.  */
13489       else
13490         break;
13491     }
13492
13493   /* For an abstract declarator, we might wind up with nothing at this
13494      point.  That's an error; the declarator is not optional.  */
13495   if (!declarator)
13496     cp_parser_error (parser, "expected declarator");
13497
13498   /* If we entered a scope, we must exit it now.  */
13499   if (pushed_scope)
13500     pop_scope (pushed_scope);
13501
13502   parser->default_arg_ok_p = saved_default_arg_ok_p;
13503   parser->in_declarator_p = saved_in_declarator_p;
13504
13505   return declarator;
13506 }
13507
13508 /* Parse a ptr-operator.
13509
13510    ptr-operator:
13511      * cv-qualifier-seq [opt]
13512      &
13513      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13514
13515    GNU Extension:
13516
13517    ptr-operator:
13518      & cv-qualifier-seq [opt]
13519
13520    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13521    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13522    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13523    filled in with the TYPE containing the member.  *CV_QUALS is
13524    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13525    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13526    Note that the tree codes returned by this function have nothing
13527    to do with the types of trees that will be eventually be created
13528    to represent the pointer or reference type being parsed. They are
13529    just constants with suggestive names. */
13530 static enum tree_code
13531 cp_parser_ptr_operator (cp_parser* parser,
13532                         tree* type,
13533                         cp_cv_quals *cv_quals)
13534 {
13535   enum tree_code code = ERROR_MARK;
13536   cp_token *token;
13537
13538   /* Assume that it's not a pointer-to-member.  */
13539   *type = NULL_TREE;
13540   /* And that there are no cv-qualifiers.  */
13541   *cv_quals = TYPE_UNQUALIFIED;
13542
13543   /* Peek at the next token.  */
13544   token = cp_lexer_peek_token (parser->lexer);
13545
13546   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13547   if (token->type == CPP_MULT)
13548     code = INDIRECT_REF;
13549   else if (token->type == CPP_AND)
13550     code = ADDR_EXPR;
13551   else if ((cxx_dialect != cxx98) &&
13552            token->type == CPP_AND_AND) /* C++0x only */
13553     code = NON_LVALUE_EXPR;
13554
13555   if (code != ERROR_MARK)
13556     {
13557       /* Consume the `*', `&' or `&&'.  */
13558       cp_lexer_consume_token (parser->lexer);
13559
13560       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13561          `&', if we are allowing GNU extensions.  (The only qualifier
13562          that can legally appear after `&' is `restrict', but that is
13563          enforced during semantic analysis.  */
13564       if (code == INDIRECT_REF
13565           || cp_parser_allow_gnu_extensions_p (parser))
13566         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13567     }
13568   else
13569     {
13570       /* Try the pointer-to-member case.  */
13571       cp_parser_parse_tentatively (parser);
13572       /* Look for the optional `::' operator.  */
13573       cp_parser_global_scope_opt (parser,
13574                                   /*current_scope_valid_p=*/false);
13575       /* Look for the nested-name specifier.  */
13576       token = cp_lexer_peek_token (parser->lexer);
13577       cp_parser_nested_name_specifier (parser,
13578                                        /*typename_keyword_p=*/false,
13579                                        /*check_dependency_p=*/true,
13580                                        /*type_p=*/false,
13581                                        /*is_declaration=*/false);
13582       /* If we found it, and the next token is a `*', then we are
13583          indeed looking at a pointer-to-member operator.  */
13584       if (!cp_parser_error_occurred (parser)
13585           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13586         {
13587           /* Indicate that the `*' operator was used.  */
13588           code = INDIRECT_REF;
13589
13590           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13591             error ("%H%qD is a namespace", &token->location, parser->scope);
13592           else
13593             {
13594               /* The type of which the member is a member is given by the
13595                  current SCOPE.  */
13596               *type = parser->scope;
13597               /* The next name will not be qualified.  */
13598               parser->scope = NULL_TREE;
13599               parser->qualifying_scope = NULL_TREE;
13600               parser->object_scope = NULL_TREE;
13601               /* Look for the optional cv-qualifier-seq.  */
13602               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13603             }
13604         }
13605       /* If that didn't work we don't have a ptr-operator.  */
13606       if (!cp_parser_parse_definitely (parser))
13607         cp_parser_error (parser, "expected ptr-operator");
13608     }
13609
13610   return code;
13611 }
13612
13613 /* Parse an (optional) cv-qualifier-seq.
13614
13615    cv-qualifier-seq:
13616      cv-qualifier cv-qualifier-seq [opt]
13617
13618    cv-qualifier:
13619      const
13620      volatile
13621
13622    GNU Extension:
13623
13624    cv-qualifier:
13625      __restrict__
13626
13627    Returns a bitmask representing the cv-qualifiers.  */
13628
13629 static cp_cv_quals
13630 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13631 {
13632   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13633
13634   while (true)
13635     {
13636       cp_token *token;
13637       cp_cv_quals cv_qualifier;
13638
13639       /* Peek at the next token.  */
13640       token = cp_lexer_peek_token (parser->lexer);
13641       /* See if it's a cv-qualifier.  */
13642       switch (token->keyword)
13643         {
13644         case RID_CONST:
13645           cv_qualifier = TYPE_QUAL_CONST;
13646           break;
13647
13648         case RID_VOLATILE:
13649           cv_qualifier = TYPE_QUAL_VOLATILE;
13650           break;
13651
13652         case RID_RESTRICT:
13653           cv_qualifier = TYPE_QUAL_RESTRICT;
13654           break;
13655
13656         default:
13657           cv_qualifier = TYPE_UNQUALIFIED;
13658           break;
13659         }
13660
13661       if (!cv_qualifier)
13662         break;
13663
13664       if (cv_quals & cv_qualifier)
13665         {
13666           error ("%Hduplicate cv-qualifier", &token->location);
13667           cp_lexer_purge_token (parser->lexer);
13668         }
13669       else
13670         {
13671           cp_lexer_consume_token (parser->lexer);
13672           cv_quals |= cv_qualifier;
13673         }
13674     }
13675
13676   return cv_quals;
13677 }
13678
13679 /* Parse a late-specified return type, if any.  This is not a separate
13680    non-terminal, but part of a function declarator, which looks like
13681
13682    -> type-id
13683
13684    Returns the type indicated by the type-id.  */
13685
13686 static tree
13687 cp_parser_late_return_type_opt (cp_parser* parser)
13688 {
13689   cp_token *token;
13690
13691   /* Peek at the next token.  */
13692   token = cp_lexer_peek_token (parser->lexer);
13693   /* A late-specified return type is indicated by an initial '->'. */
13694   if (token->type != CPP_DEREF)
13695     return NULL_TREE;
13696
13697   /* Consume the ->.  */
13698   cp_lexer_consume_token (parser->lexer);
13699
13700   return cp_parser_type_id (parser);
13701 }
13702
13703 /* Parse a declarator-id.
13704
13705    declarator-id:
13706      id-expression
13707      :: [opt] nested-name-specifier [opt] type-name
13708
13709    In the `id-expression' case, the value returned is as for
13710    cp_parser_id_expression if the id-expression was an unqualified-id.
13711    If the id-expression was a qualified-id, then a SCOPE_REF is
13712    returned.  The first operand is the scope (either a NAMESPACE_DECL
13713    or TREE_TYPE), but the second is still just a representation of an
13714    unqualified-id.  */
13715
13716 static tree
13717 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13718 {
13719   tree id;
13720   /* The expression must be an id-expression.  Assume that qualified
13721      names are the names of types so that:
13722
13723        template <class T>
13724        int S<T>::R::i = 3;
13725
13726      will work; we must treat `S<T>::R' as the name of a type.
13727      Similarly, assume that qualified names are templates, where
13728      required, so that:
13729
13730        template <class T>
13731        int S<T>::R<T>::i = 3;
13732
13733      will work, too.  */
13734   id = cp_parser_id_expression (parser,
13735                                 /*template_keyword_p=*/false,
13736                                 /*check_dependency_p=*/false,
13737                                 /*template_p=*/NULL,
13738                                 /*declarator_p=*/true,
13739                                 optional_p);
13740   if (id && BASELINK_P (id))
13741     id = BASELINK_FUNCTIONS (id);
13742   return id;
13743 }
13744
13745 /* Parse a type-id.
13746
13747    type-id:
13748      type-specifier-seq abstract-declarator [opt]
13749
13750    Returns the TYPE specified.  */
13751
13752 static tree
13753 cp_parser_type_id (cp_parser* parser)
13754 {
13755   cp_decl_specifier_seq type_specifier_seq;
13756   cp_declarator *abstract_declarator;
13757
13758   /* Parse the type-specifier-seq.  */
13759   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13760                                 &type_specifier_seq);
13761   if (type_specifier_seq.type == error_mark_node)
13762     return error_mark_node;
13763
13764   /* There might or might not be an abstract declarator.  */
13765   cp_parser_parse_tentatively (parser);
13766   /* Look for the declarator.  */
13767   abstract_declarator
13768     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13769                             /*parenthesized_p=*/NULL,
13770                             /*member_p=*/false);
13771   /* Check to see if there really was a declarator.  */
13772   if (!cp_parser_parse_definitely (parser))
13773     abstract_declarator = NULL;
13774
13775   if (type_specifier_seq.type
13776       && type_uses_auto (type_specifier_seq.type))
13777     {
13778       error ("invalid use of %<auto%>");
13779       return error_mark_node;
13780     }
13781   
13782   return groktypename (&type_specifier_seq, abstract_declarator);
13783 }
13784
13785 /* Parse a type-specifier-seq.
13786
13787    type-specifier-seq:
13788      type-specifier type-specifier-seq [opt]
13789
13790    GNU extension:
13791
13792    type-specifier-seq:
13793      attributes type-specifier-seq [opt]
13794
13795    If IS_CONDITION is true, we are at the start of a "condition",
13796    e.g., we've just seen "if (".
13797
13798    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13799
13800 static void
13801 cp_parser_type_specifier_seq (cp_parser* parser,
13802                               bool is_condition,
13803                               cp_decl_specifier_seq *type_specifier_seq)
13804 {
13805   bool seen_type_specifier = false;
13806   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13807   cp_token *start_token = NULL;
13808
13809   /* Clear the TYPE_SPECIFIER_SEQ.  */
13810   clear_decl_specs (type_specifier_seq);
13811
13812   /* Parse the type-specifiers and attributes.  */
13813   while (true)
13814     {
13815       tree type_specifier;
13816       bool is_cv_qualifier;
13817
13818       /* Check for attributes first.  */
13819       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13820         {
13821           type_specifier_seq->attributes =
13822             chainon (type_specifier_seq->attributes,
13823                      cp_parser_attributes_opt (parser));
13824           continue;
13825         }
13826
13827       /* record the token of the beginning of the type specifier seq,
13828          for error reporting purposes*/
13829      if (!start_token)
13830        start_token = cp_lexer_peek_token (parser->lexer);
13831
13832       /* Look for the type-specifier.  */
13833       type_specifier = cp_parser_type_specifier (parser,
13834                                                  flags,
13835                                                  type_specifier_seq,
13836                                                  /*is_declaration=*/false,
13837                                                  NULL,
13838                                                  &is_cv_qualifier);
13839       if (!type_specifier)
13840         {
13841           /* If the first type-specifier could not be found, this is not a
13842              type-specifier-seq at all.  */
13843           if (!seen_type_specifier)
13844             {
13845               cp_parser_error (parser, "expected type-specifier");
13846               type_specifier_seq->type = error_mark_node;
13847               return;
13848             }
13849           /* If subsequent type-specifiers could not be found, the
13850              type-specifier-seq is complete.  */
13851           break;
13852         }
13853
13854       seen_type_specifier = true;
13855       /* The standard says that a condition can be:
13856
13857             type-specifier-seq declarator = assignment-expression
13858
13859          However, given:
13860
13861            struct S {};
13862            if (int S = ...)
13863
13864          we should treat the "S" as a declarator, not as a
13865          type-specifier.  The standard doesn't say that explicitly for
13866          type-specifier-seq, but it does say that for
13867          decl-specifier-seq in an ordinary declaration.  Perhaps it
13868          would be clearer just to allow a decl-specifier-seq here, and
13869          then add a semantic restriction that if any decl-specifiers
13870          that are not type-specifiers appear, the program is invalid.  */
13871       if (is_condition && !is_cv_qualifier)
13872         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13873     }
13874
13875   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13876 }
13877
13878 /* Parse a parameter-declaration-clause.
13879
13880    parameter-declaration-clause:
13881      parameter-declaration-list [opt] ... [opt]
13882      parameter-declaration-list , ...
13883
13884    Returns a representation for the parameter declarations.  A return
13885    value of NULL indicates a parameter-declaration-clause consisting
13886    only of an ellipsis.  */
13887
13888 static tree
13889 cp_parser_parameter_declaration_clause (cp_parser* parser)
13890 {
13891   tree parameters;
13892   cp_token *token;
13893   bool ellipsis_p;
13894   bool is_error;
13895
13896   /* Peek at the next token.  */
13897   token = cp_lexer_peek_token (parser->lexer);
13898   /* Check for trivial parameter-declaration-clauses.  */
13899   if (token->type == CPP_ELLIPSIS)
13900     {
13901       /* Consume the `...' token.  */
13902       cp_lexer_consume_token (parser->lexer);
13903       return NULL_TREE;
13904     }
13905   else if (token->type == CPP_CLOSE_PAREN)
13906     /* There are no parameters.  */
13907     {
13908 #ifndef NO_IMPLICIT_EXTERN_C
13909       if (in_system_header && current_class_type == NULL
13910           && current_lang_name == lang_name_c)
13911         return NULL_TREE;
13912       else
13913 #endif
13914         return void_list_node;
13915     }
13916   /* Check for `(void)', too, which is a special case.  */
13917   else if (token->keyword == RID_VOID
13918            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13919                == CPP_CLOSE_PAREN))
13920     {
13921       /* Consume the `void' token.  */
13922       cp_lexer_consume_token (parser->lexer);
13923       /* There are no parameters.  */
13924       return void_list_node;
13925     }
13926
13927   /* Parse the parameter-declaration-list.  */
13928   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13929   /* If a parse error occurred while parsing the
13930      parameter-declaration-list, then the entire
13931      parameter-declaration-clause is erroneous.  */
13932   if (is_error)
13933     return NULL;
13934
13935   /* Peek at the next token.  */
13936   token = cp_lexer_peek_token (parser->lexer);
13937   /* If it's a `,', the clause should terminate with an ellipsis.  */
13938   if (token->type == CPP_COMMA)
13939     {
13940       /* Consume the `,'.  */
13941       cp_lexer_consume_token (parser->lexer);
13942       /* Expect an ellipsis.  */
13943       ellipsis_p
13944         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13945     }
13946   /* It might also be `...' if the optional trailing `,' was
13947      omitted.  */
13948   else if (token->type == CPP_ELLIPSIS)
13949     {
13950       /* Consume the `...' token.  */
13951       cp_lexer_consume_token (parser->lexer);
13952       /* And remember that we saw it.  */
13953       ellipsis_p = true;
13954     }
13955   else
13956     ellipsis_p = false;
13957
13958   /* Finish the parameter list.  */
13959   if (!ellipsis_p)
13960     parameters = chainon (parameters, void_list_node);
13961
13962   return parameters;
13963 }
13964
13965 /* Parse a parameter-declaration-list.
13966
13967    parameter-declaration-list:
13968      parameter-declaration
13969      parameter-declaration-list , parameter-declaration
13970
13971    Returns a representation of the parameter-declaration-list, as for
13972    cp_parser_parameter_declaration_clause.  However, the
13973    `void_list_node' is never appended to the list.  Upon return,
13974    *IS_ERROR will be true iff an error occurred.  */
13975
13976 static tree
13977 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13978 {
13979   tree parameters = NULL_TREE;
13980   tree *tail = &parameters; 
13981   bool saved_in_unbraced_linkage_specification_p;
13982
13983   /* Assume all will go well.  */
13984   *is_error = false;
13985   /* The special considerations that apply to a function within an
13986      unbraced linkage specifications do not apply to the parameters
13987      to the function.  */
13988   saved_in_unbraced_linkage_specification_p 
13989     = parser->in_unbraced_linkage_specification_p;
13990   parser->in_unbraced_linkage_specification_p = false;
13991
13992   /* Look for more parameters.  */
13993   while (true)
13994     {
13995       cp_parameter_declarator *parameter;
13996       tree decl = error_mark_node;
13997       bool parenthesized_p;
13998       /* Parse the parameter.  */
13999       parameter
14000         = cp_parser_parameter_declaration (parser,
14001                                            /*template_parm_p=*/false,
14002                                            &parenthesized_p);
14003
14004       /* We don't know yet if the enclosing context is deprecated, so wait
14005          and warn in grokparms if appropriate.  */
14006       deprecated_state = DEPRECATED_SUPPRESS;
14007
14008       if (parameter)
14009         decl = grokdeclarator (parameter->declarator,
14010                                &parameter->decl_specifiers,
14011                                PARM,
14012                                parameter->default_argument != NULL_TREE,
14013                                &parameter->decl_specifiers.attributes);
14014
14015       deprecated_state = DEPRECATED_NORMAL;
14016
14017       /* If a parse error occurred parsing the parameter declaration,
14018          then the entire parameter-declaration-list is erroneous.  */
14019       if (decl == error_mark_node)
14020         {
14021           *is_error = true;
14022           parameters = error_mark_node;
14023           break;
14024         }
14025
14026       if (parameter->decl_specifiers.attributes)
14027         cplus_decl_attributes (&decl,
14028                                parameter->decl_specifiers.attributes,
14029                                0);
14030       if (DECL_NAME (decl))
14031         decl = pushdecl (decl);
14032
14033       /* Add the new parameter to the list.  */
14034       *tail = build_tree_list (parameter->default_argument, decl);
14035       tail = &TREE_CHAIN (*tail);
14036
14037       /* Peek at the next token.  */
14038       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14039           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14040           /* These are for Objective-C++ */
14041           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14042           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14043         /* The parameter-declaration-list is complete.  */
14044         break;
14045       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14046         {
14047           cp_token *token;
14048
14049           /* Peek at the next token.  */
14050           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14051           /* If it's an ellipsis, then the list is complete.  */
14052           if (token->type == CPP_ELLIPSIS)
14053             break;
14054           /* Otherwise, there must be more parameters.  Consume the
14055              `,'.  */
14056           cp_lexer_consume_token (parser->lexer);
14057           /* When parsing something like:
14058
14059                 int i(float f, double d)
14060
14061              we can tell after seeing the declaration for "f" that we
14062              are not looking at an initialization of a variable "i",
14063              but rather at the declaration of a function "i".
14064
14065              Due to the fact that the parsing of template arguments
14066              (as specified to a template-id) requires backtracking we
14067              cannot use this technique when inside a template argument
14068              list.  */
14069           if (!parser->in_template_argument_list_p
14070               && !parser->in_type_id_in_expr_p
14071               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14072               /* However, a parameter-declaration of the form
14073                  "foat(f)" (which is a valid declaration of a
14074                  parameter "f") can also be interpreted as an
14075                  expression (the conversion of "f" to "float").  */
14076               && !parenthesized_p)
14077             cp_parser_commit_to_tentative_parse (parser);
14078         }
14079       else
14080         {
14081           cp_parser_error (parser, "expected %<,%> or %<...%>");
14082           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14083             cp_parser_skip_to_closing_parenthesis (parser,
14084                                                    /*recovering=*/true,
14085                                                    /*or_comma=*/false,
14086                                                    /*consume_paren=*/false);
14087           break;
14088         }
14089     }
14090
14091   parser->in_unbraced_linkage_specification_p
14092     = saved_in_unbraced_linkage_specification_p;
14093
14094   return parameters;
14095 }
14096
14097 /* Parse a parameter declaration.
14098
14099    parameter-declaration:
14100      decl-specifier-seq ... [opt] declarator
14101      decl-specifier-seq declarator = assignment-expression
14102      decl-specifier-seq ... [opt] abstract-declarator [opt]
14103      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14104
14105    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14106    declares a template parameter.  (In that case, a non-nested `>'
14107    token encountered during the parsing of the assignment-expression
14108    is not interpreted as a greater-than operator.)
14109
14110    Returns a representation of the parameter, or NULL if an error
14111    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14112    true iff the declarator is of the form "(p)".  */
14113
14114 static cp_parameter_declarator *
14115 cp_parser_parameter_declaration (cp_parser *parser,
14116                                  bool template_parm_p,
14117                                  bool *parenthesized_p)
14118 {
14119   int declares_class_or_enum;
14120   bool greater_than_is_operator_p;
14121   cp_decl_specifier_seq decl_specifiers;
14122   cp_declarator *declarator;
14123   tree default_argument;
14124   cp_token *token = NULL, *declarator_token_start = NULL;
14125   const char *saved_message;
14126
14127   /* In a template parameter, `>' is not an operator.
14128
14129      [temp.param]
14130
14131      When parsing a default template-argument for a non-type
14132      template-parameter, the first non-nested `>' is taken as the end
14133      of the template parameter-list rather than a greater-than
14134      operator.  */
14135   greater_than_is_operator_p = !template_parm_p;
14136
14137   /* Type definitions may not appear in parameter types.  */
14138   saved_message = parser->type_definition_forbidden_message;
14139   parser->type_definition_forbidden_message
14140     = "types may not be defined in parameter types";
14141
14142   /* Parse the declaration-specifiers.  */
14143   cp_parser_decl_specifier_seq (parser,
14144                                 CP_PARSER_FLAGS_NONE,
14145                                 &decl_specifiers,
14146                                 &declares_class_or_enum);
14147   /* If an error occurred, there's no reason to attempt to parse the
14148      rest of the declaration.  */
14149   if (cp_parser_error_occurred (parser))
14150     {
14151       parser->type_definition_forbidden_message = saved_message;
14152       return NULL;
14153     }
14154
14155   /* Peek at the next token.  */
14156   token = cp_lexer_peek_token (parser->lexer);
14157
14158   /* If the next token is a `)', `,', `=', `>', or `...', then there
14159      is no declarator. However, when variadic templates are enabled,
14160      there may be a declarator following `...'.  */
14161   if (token->type == CPP_CLOSE_PAREN
14162       || token->type == CPP_COMMA
14163       || token->type == CPP_EQ
14164       || token->type == CPP_GREATER)
14165     {
14166       declarator = NULL;
14167       if (parenthesized_p)
14168         *parenthesized_p = false;
14169     }
14170   /* Otherwise, there should be a declarator.  */
14171   else
14172     {
14173       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14174       parser->default_arg_ok_p = false;
14175
14176       /* After seeing a decl-specifier-seq, if the next token is not a
14177          "(", there is no possibility that the code is a valid
14178          expression.  Therefore, if parsing tentatively, we commit at
14179          this point.  */
14180       if (!parser->in_template_argument_list_p
14181           /* In an expression context, having seen:
14182
14183                (int((char ...
14184
14185              we cannot be sure whether we are looking at a
14186              function-type (taking a "char" as a parameter) or a cast
14187              of some object of type "char" to "int".  */
14188           && !parser->in_type_id_in_expr_p
14189           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14190           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14191         cp_parser_commit_to_tentative_parse (parser);
14192       /* Parse the declarator.  */
14193       declarator_token_start = token;
14194       declarator = cp_parser_declarator (parser,
14195                                          CP_PARSER_DECLARATOR_EITHER,
14196                                          /*ctor_dtor_or_conv_p=*/NULL,
14197                                          parenthesized_p,
14198                                          /*member_p=*/false);
14199       parser->default_arg_ok_p = saved_default_arg_ok_p;
14200       /* After the declarator, allow more attributes.  */
14201       decl_specifiers.attributes
14202         = chainon (decl_specifiers.attributes,
14203                    cp_parser_attributes_opt (parser));
14204     }
14205
14206   /* If the next token is an ellipsis, and we have not seen a
14207      declarator name, and the type of the declarator contains parameter
14208      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14209      a parameter pack expansion expression. Otherwise, leave the
14210      ellipsis for a C-style variadic function. */
14211   token = cp_lexer_peek_token (parser->lexer);
14212   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14213     {
14214       tree type = decl_specifiers.type;
14215
14216       if (type && DECL_P (type))
14217         type = TREE_TYPE (type);
14218
14219       if (type
14220           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14221           && declarator_can_be_parameter_pack (declarator)
14222           && (!declarator || !declarator->parameter_pack_p)
14223           && uses_parameter_packs (type))
14224         {
14225           /* Consume the `...'. */
14226           cp_lexer_consume_token (parser->lexer);
14227           maybe_warn_variadic_templates ();
14228           
14229           /* Build a pack expansion type */
14230           if (declarator)
14231             declarator->parameter_pack_p = true;
14232           else
14233             decl_specifiers.type = make_pack_expansion (type);
14234         }
14235     }
14236
14237   /* The restriction on defining new types applies only to the type
14238      of the parameter, not to the default argument.  */
14239   parser->type_definition_forbidden_message = saved_message;
14240
14241   /* If the next token is `=', then process a default argument.  */
14242   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14243     {
14244       /* Consume the `='.  */
14245       cp_lexer_consume_token (parser->lexer);
14246
14247       /* If we are defining a class, then the tokens that make up the
14248          default argument must be saved and processed later.  */
14249       if (!template_parm_p && at_class_scope_p ()
14250           && TYPE_BEING_DEFINED (current_class_type))
14251         {
14252           unsigned depth = 0;
14253           int maybe_template_id = 0;
14254           cp_token *first_token;
14255           cp_token *token;
14256
14257           /* Add tokens until we have processed the entire default
14258              argument.  We add the range [first_token, token).  */
14259           first_token = cp_lexer_peek_token (parser->lexer);
14260           while (true)
14261             {
14262               bool done = false;
14263
14264               /* Peek at the next token.  */
14265               token = cp_lexer_peek_token (parser->lexer);
14266               /* What we do depends on what token we have.  */
14267               switch (token->type)
14268                 {
14269                   /* In valid code, a default argument must be
14270                      immediately followed by a `,' `)', or `...'.  */
14271                 case CPP_COMMA:
14272                   if (depth == 0 && maybe_template_id)
14273                     {
14274                       /* If we've seen a '<', we might be in a
14275                          template-argument-list.  Until Core issue 325 is
14276                          resolved, we don't know how this situation ought
14277                          to be handled, so try to DTRT.  We check whether
14278                          what comes after the comma is a valid parameter
14279                          declaration list.  If it is, then the comma ends
14280                          the default argument; otherwise the default
14281                          argument continues.  */
14282                       bool error = false;
14283
14284                       /* Set ITALP so cp_parser_parameter_declaration_list
14285                          doesn't decide to commit to this parse.  */
14286                       bool saved_italp = parser->in_template_argument_list_p;
14287                       parser->in_template_argument_list_p = true;
14288
14289                       cp_parser_parse_tentatively (parser);
14290                       cp_lexer_consume_token (parser->lexer);
14291                       cp_parser_parameter_declaration_list (parser, &error);
14292                       if (!cp_parser_error_occurred (parser) && !error)
14293                         done = true;
14294                       cp_parser_abort_tentative_parse (parser);
14295
14296                       parser->in_template_argument_list_p = saved_italp;
14297                       break;
14298                     }
14299                 case CPP_CLOSE_PAREN:
14300                 case CPP_ELLIPSIS:
14301                   /* If we run into a non-nested `;', `}', or `]',
14302                      then the code is invalid -- but the default
14303                      argument is certainly over.  */
14304                 case CPP_SEMICOLON:
14305                 case CPP_CLOSE_BRACE:
14306                 case CPP_CLOSE_SQUARE:
14307                   if (depth == 0)
14308                     done = true;
14309                   /* Update DEPTH, if necessary.  */
14310                   else if (token->type == CPP_CLOSE_PAREN
14311                            || token->type == CPP_CLOSE_BRACE
14312                            || token->type == CPP_CLOSE_SQUARE)
14313                     --depth;
14314                   break;
14315
14316                 case CPP_OPEN_PAREN:
14317                 case CPP_OPEN_SQUARE:
14318                 case CPP_OPEN_BRACE:
14319                   ++depth;
14320                   break;
14321
14322                 case CPP_LESS:
14323                   if (depth == 0)
14324                     /* This might be the comparison operator, or it might
14325                        start a template argument list.  */
14326                     ++maybe_template_id;
14327                   break;
14328
14329                 case CPP_RSHIFT:
14330                   if (cxx_dialect == cxx98)
14331                     break;
14332                   /* Fall through for C++0x, which treats the `>>'
14333                      operator like two `>' tokens in certain
14334                      cases.  */
14335
14336                 case CPP_GREATER:
14337                   if (depth == 0)
14338                     {
14339                       /* This might be an operator, or it might close a
14340                          template argument list.  But if a previous '<'
14341                          started a template argument list, this will have
14342                          closed it, so we can't be in one anymore.  */
14343                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14344                       if (maybe_template_id < 0)
14345                         maybe_template_id = 0;
14346                     }
14347                   break;
14348
14349                   /* If we run out of tokens, issue an error message.  */
14350                 case CPP_EOF:
14351                 case CPP_PRAGMA_EOL:
14352                   error ("%Hfile ends in default argument", &token->location);
14353                   done = true;
14354                   break;
14355
14356                 case CPP_NAME:
14357                 case CPP_SCOPE:
14358                   /* In these cases, we should look for template-ids.
14359                      For example, if the default argument is
14360                      `X<int, double>()', we need to do name lookup to
14361                      figure out whether or not `X' is a template; if
14362                      so, the `,' does not end the default argument.
14363
14364                      That is not yet done.  */
14365                   break;
14366
14367                 default:
14368                   break;
14369                 }
14370
14371               /* If we've reached the end, stop.  */
14372               if (done)
14373                 break;
14374
14375               /* Add the token to the token block.  */
14376               token = cp_lexer_consume_token (parser->lexer);
14377             }
14378
14379           /* Create a DEFAULT_ARG to represent the unparsed default
14380              argument.  */
14381           default_argument = make_node (DEFAULT_ARG);
14382           DEFARG_TOKENS (default_argument)
14383             = cp_token_cache_new (first_token, token);
14384           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14385         }
14386       /* Outside of a class definition, we can just parse the
14387          assignment-expression.  */
14388       else
14389         {
14390           token = cp_lexer_peek_token (parser->lexer);
14391           default_argument 
14392             = cp_parser_default_argument (parser, template_parm_p);
14393         }
14394
14395       if (!parser->default_arg_ok_p)
14396         {
14397           if (flag_permissive)
14398             warning (0, "deprecated use of default argument for parameter of non-function");
14399           else
14400             {
14401               error ("%Hdefault arguments are only "
14402                      "permitted for function parameters",
14403                      &token->location);
14404               default_argument = NULL_TREE;
14405             }
14406         }
14407       else if ((declarator && declarator->parameter_pack_p)
14408                || (decl_specifiers.type
14409                    && PACK_EXPANSION_P (decl_specifiers.type)))
14410         {
14411           const char* kind = template_parm_p? "template " : "";
14412           
14413           /* Find the name of the parameter pack.  */     
14414           cp_declarator *id_declarator = declarator;
14415           while (id_declarator && id_declarator->kind != cdk_id)
14416             id_declarator = id_declarator->declarator;
14417           
14418           if (id_declarator && id_declarator->kind == cdk_id)
14419             error ("%H%sparameter pack %qD cannot have a default argument",
14420                    &declarator_token_start->location,
14421                    kind, id_declarator->u.id.unqualified_name);
14422           else
14423             error ("%H%sparameter pack cannot have a default argument",
14424                    &declarator_token_start->location, kind);
14425           
14426           default_argument = NULL_TREE;
14427         }
14428     }
14429   else
14430     default_argument = NULL_TREE;
14431
14432   return make_parameter_declarator (&decl_specifiers,
14433                                     declarator,
14434                                     default_argument);
14435 }
14436
14437 /* Parse a default argument and return it.
14438
14439    TEMPLATE_PARM_P is true if this is a default argument for a
14440    non-type template parameter.  */
14441 static tree
14442 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14443 {
14444   tree default_argument = NULL_TREE;
14445   bool saved_greater_than_is_operator_p;
14446   bool saved_local_variables_forbidden_p;
14447
14448   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14449      set correctly.  */
14450   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14451   parser->greater_than_is_operator_p = !template_parm_p;
14452   /* Local variable names (and the `this' keyword) may not
14453      appear in a default argument.  */
14454   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14455   parser->local_variables_forbidden_p = true;
14456   /* The default argument expression may cause implicitly
14457      defined member functions to be synthesized, which will
14458      result in garbage collection.  We must treat this
14459      situation as if we were within the body of function so as
14460      to avoid collecting live data on the stack.  */
14461   ++function_depth;
14462   /* Parse the assignment-expression.  */
14463   if (template_parm_p)
14464     push_deferring_access_checks (dk_no_deferred);
14465   default_argument
14466     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14467   if (template_parm_p)
14468     pop_deferring_access_checks ();
14469   /* Restore saved state.  */
14470   --function_depth;
14471   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14472   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14473
14474   return default_argument;
14475 }
14476
14477 /* Parse a function-body.
14478
14479    function-body:
14480      compound_statement  */
14481
14482 static void
14483 cp_parser_function_body (cp_parser *parser)
14484 {
14485   cp_parser_compound_statement (parser, NULL, false);
14486 }
14487
14488 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14489    true if a ctor-initializer was present.  */
14490
14491 static bool
14492 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14493 {
14494   tree body;
14495   bool ctor_initializer_p;
14496
14497   /* Begin the function body.  */
14498   body = begin_function_body ();
14499   /* Parse the optional ctor-initializer.  */
14500   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14501   /* Parse the function-body.  */
14502   cp_parser_function_body (parser);
14503   /* Finish the function body.  */
14504   finish_function_body (body);
14505
14506   return ctor_initializer_p;
14507 }
14508
14509 /* Parse an initializer.
14510
14511    initializer:
14512      = initializer-clause
14513      ( expression-list )
14514
14515    Returns an expression representing the initializer.  If no
14516    initializer is present, NULL_TREE is returned.
14517
14518    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14519    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14520    set to TRUE if there is no initializer present.  If there is an
14521    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14522    is set to true; otherwise it is set to false.  */
14523
14524 static tree
14525 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14526                        bool* non_constant_p)
14527 {
14528   cp_token *token;
14529   tree init;
14530
14531   /* Peek at the next token.  */
14532   token = cp_lexer_peek_token (parser->lexer);
14533
14534   /* Let our caller know whether or not this initializer was
14535      parenthesized.  */
14536   *is_direct_init = (token->type != CPP_EQ);
14537   /* Assume that the initializer is constant.  */
14538   *non_constant_p = false;
14539
14540   if (token->type == CPP_EQ)
14541     {
14542       /* Consume the `='.  */
14543       cp_lexer_consume_token (parser->lexer);
14544       /* Parse the initializer-clause.  */
14545       init = cp_parser_initializer_clause (parser, non_constant_p);
14546     }
14547   else if (token->type == CPP_OPEN_PAREN)
14548     init = cp_parser_parenthesized_expression_list (parser, false,
14549                                                     /*cast_p=*/false,
14550                                                     /*allow_expansion_p=*/true,
14551                                                     non_constant_p);
14552   else if (token->type == CPP_OPEN_BRACE)
14553     {
14554       maybe_warn_cpp0x ("extended initializer lists");
14555       init = cp_parser_braced_list (parser, non_constant_p);
14556       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14557     }
14558   else
14559     {
14560       /* Anything else is an error.  */
14561       cp_parser_error (parser, "expected initializer");
14562       init = error_mark_node;
14563     }
14564
14565   return init;
14566 }
14567
14568 /* Parse an initializer-clause.
14569
14570    initializer-clause:
14571      assignment-expression
14572      braced-init-list
14573
14574    Returns an expression representing the initializer.
14575
14576    If the `assignment-expression' production is used the value
14577    returned is simply a representation for the expression.
14578
14579    Otherwise, calls cp_parser_braced_list.  */
14580
14581 static tree
14582 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14583 {
14584   tree initializer;
14585
14586   /* Assume the expression is constant.  */
14587   *non_constant_p = false;
14588
14589   /* If it is not a `{', then we are looking at an
14590      assignment-expression.  */
14591   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14592     {
14593       initializer
14594         = cp_parser_constant_expression (parser,
14595                                         /*allow_non_constant_p=*/true,
14596                                         non_constant_p);
14597       if (!*non_constant_p)
14598         initializer = fold_non_dependent_expr (initializer);
14599     }
14600   else
14601     initializer = cp_parser_braced_list (parser, non_constant_p);
14602
14603   return initializer;
14604 }
14605
14606 /* Parse a brace-enclosed initializer list.
14607
14608    braced-init-list:
14609      { initializer-list , [opt] }
14610      { }
14611
14612    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14613    the elements of the initializer-list (or NULL, if the last
14614    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14615    NULL_TREE.  There is no way to detect whether or not the optional
14616    trailing `,' was provided.  NON_CONSTANT_P is as for
14617    cp_parser_initializer.  */     
14618
14619 static tree
14620 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14621 {
14622   tree initializer;
14623
14624   /* Consume the `{' token.  */
14625   cp_lexer_consume_token (parser->lexer);
14626   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14627   initializer = make_node (CONSTRUCTOR);
14628   /* If it's not a `}', then there is a non-trivial initializer.  */
14629   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14630     {
14631       /* Parse the initializer list.  */
14632       CONSTRUCTOR_ELTS (initializer)
14633         = cp_parser_initializer_list (parser, non_constant_p);
14634       /* A trailing `,' token is allowed.  */
14635       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14636         cp_lexer_consume_token (parser->lexer);
14637     }
14638   /* Now, there should be a trailing `}'.  */
14639   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14640   TREE_TYPE (initializer) = init_list_type_node;
14641   return initializer;
14642 }
14643
14644 /* Parse an initializer-list.
14645
14646    initializer-list:
14647      initializer-clause ... [opt]
14648      initializer-list , initializer-clause ... [opt]
14649
14650    GNU Extension:
14651
14652    initializer-list:
14653      identifier : initializer-clause
14654      initializer-list, identifier : initializer-clause
14655
14656    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14657    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14658    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14659    as for cp_parser_initializer.  */
14660
14661 static VEC(constructor_elt,gc) *
14662 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14663 {
14664   VEC(constructor_elt,gc) *v = NULL;
14665
14666   /* Assume all of the expressions are constant.  */
14667   *non_constant_p = false;
14668
14669   /* Parse the rest of the list.  */
14670   while (true)
14671     {
14672       cp_token *token;
14673       tree identifier;
14674       tree initializer;
14675       bool clause_non_constant_p;
14676
14677       /* If the next token is an identifier and the following one is a
14678          colon, we are looking at the GNU designated-initializer
14679          syntax.  */
14680       if (cp_parser_allow_gnu_extensions_p (parser)
14681           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14682           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14683         {
14684           /* Warn the user that they are using an extension.  */
14685           pedwarn (input_location, OPT_pedantic, 
14686                    "ISO C++ does not allow designated initializers");
14687           /* Consume the identifier.  */
14688           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14689           /* Consume the `:'.  */
14690           cp_lexer_consume_token (parser->lexer);
14691         }
14692       else
14693         identifier = NULL_TREE;
14694
14695       /* Parse the initializer.  */
14696       initializer = cp_parser_initializer_clause (parser,
14697                                                   &clause_non_constant_p);
14698       /* If any clause is non-constant, so is the entire initializer.  */
14699       if (clause_non_constant_p)
14700         *non_constant_p = true;
14701
14702       /* If we have an ellipsis, this is an initializer pack
14703          expansion.  */
14704       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14705         {
14706           /* Consume the `...'.  */
14707           cp_lexer_consume_token (parser->lexer);
14708
14709           /* Turn the initializer into an initializer expansion.  */
14710           initializer = make_pack_expansion (initializer);
14711         }
14712
14713       /* Add it to the vector.  */
14714       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14715
14716       /* If the next token is not a comma, we have reached the end of
14717          the list.  */
14718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14719         break;
14720
14721       /* Peek at the next token.  */
14722       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14723       /* If the next token is a `}', then we're still done.  An
14724          initializer-clause can have a trailing `,' after the
14725          initializer-list and before the closing `}'.  */
14726       if (token->type == CPP_CLOSE_BRACE)
14727         break;
14728
14729       /* Consume the `,' token.  */
14730       cp_lexer_consume_token (parser->lexer);
14731     }
14732
14733   return v;
14734 }
14735
14736 /* Classes [gram.class] */
14737
14738 /* Parse a class-name.
14739
14740    class-name:
14741      identifier
14742      template-id
14743
14744    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14745    to indicate that names looked up in dependent types should be
14746    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14747    keyword has been used to indicate that the name that appears next
14748    is a template.  TAG_TYPE indicates the explicit tag given before
14749    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14750    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14751    is the class being defined in a class-head.
14752
14753    Returns the TYPE_DECL representing the class.  */
14754
14755 static tree
14756 cp_parser_class_name (cp_parser *parser,
14757                       bool typename_keyword_p,
14758                       bool template_keyword_p,
14759                       enum tag_types tag_type,
14760                       bool check_dependency_p,
14761                       bool class_head_p,
14762                       bool is_declaration)
14763 {
14764   tree decl;
14765   tree scope;
14766   bool typename_p;
14767   cp_token *token;
14768   tree identifier = NULL_TREE;
14769
14770   /* All class-names start with an identifier.  */
14771   token = cp_lexer_peek_token (parser->lexer);
14772   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14773     {
14774       cp_parser_error (parser, "expected class-name");
14775       return error_mark_node;
14776     }
14777
14778   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14779      to a template-id, so we save it here.  */
14780   scope = parser->scope;
14781   if (scope == error_mark_node)
14782     return error_mark_node;
14783
14784   /* Any name names a type if we're following the `typename' keyword
14785      in a qualified name where the enclosing scope is type-dependent.  */
14786   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14787                 && dependent_type_p (scope));
14788   /* Handle the common case (an identifier, but not a template-id)
14789      efficiently.  */
14790   if (token->type == CPP_NAME
14791       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14792     {
14793       cp_token *identifier_token;
14794       bool ambiguous_p;
14795
14796       /* Look for the identifier.  */
14797       identifier_token = cp_lexer_peek_token (parser->lexer);
14798       ambiguous_p = identifier_token->ambiguous_p;
14799       identifier = cp_parser_identifier (parser);
14800       /* If the next token isn't an identifier, we are certainly not
14801          looking at a class-name.  */
14802       if (identifier == error_mark_node)
14803         decl = error_mark_node;
14804       /* If we know this is a type-name, there's no need to look it
14805          up.  */
14806       else if (typename_p)
14807         decl = identifier;
14808       else
14809         {
14810           tree ambiguous_decls;
14811           /* If we already know that this lookup is ambiguous, then
14812              we've already issued an error message; there's no reason
14813              to check again.  */
14814           if (ambiguous_p)
14815             {
14816               cp_parser_simulate_error (parser);
14817               return error_mark_node;
14818             }
14819           /* If the next token is a `::', then the name must be a type
14820              name.
14821
14822              [basic.lookup.qual]
14823
14824              During the lookup for a name preceding the :: scope
14825              resolution operator, object, function, and enumerator
14826              names are ignored.  */
14827           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14828             tag_type = typename_type;
14829           /* Look up the name.  */
14830           decl = cp_parser_lookup_name (parser, identifier,
14831                                         tag_type,
14832                                         /*is_template=*/false,
14833                                         /*is_namespace=*/false,
14834                                         check_dependency_p,
14835                                         &ambiguous_decls,
14836                                         identifier_token->location);
14837           if (ambiguous_decls)
14838             {
14839               error ("%Hreference to %qD is ambiguous",
14840                      &identifier_token->location, identifier);
14841               print_candidates (ambiguous_decls);
14842               if (cp_parser_parsing_tentatively (parser))
14843                 {
14844                   identifier_token->ambiguous_p = true;
14845                   cp_parser_simulate_error (parser);
14846                 }
14847               return error_mark_node;
14848             }
14849         }
14850     }
14851   else
14852     {
14853       /* Try a template-id.  */
14854       decl = cp_parser_template_id (parser, template_keyword_p,
14855                                     check_dependency_p,
14856                                     is_declaration);
14857       if (decl == error_mark_node)
14858         return error_mark_node;
14859     }
14860
14861   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14862
14863   /* If this is a typename, create a TYPENAME_TYPE.  */
14864   if (typename_p && decl != error_mark_node)
14865     {
14866       decl = make_typename_type (scope, decl, typename_type,
14867                                  /*complain=*/tf_error);
14868       if (decl != error_mark_node)
14869         decl = TYPE_NAME (decl);
14870     }
14871
14872   /* Check to see that it is really the name of a class.  */
14873   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14874       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14875       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14876     /* Situations like this:
14877
14878          template <typename T> struct A {
14879            typename T::template X<int>::I i;
14880          };
14881
14882        are problematic.  Is `T::template X<int>' a class-name?  The
14883        standard does not seem to be definitive, but there is no other
14884        valid interpretation of the following `::'.  Therefore, those
14885        names are considered class-names.  */
14886     {
14887       decl = make_typename_type (scope, decl, tag_type, tf_error);
14888       if (decl != error_mark_node)
14889         decl = TYPE_NAME (decl);
14890     }
14891   else if (TREE_CODE (decl) != TYPE_DECL
14892            || TREE_TYPE (decl) == error_mark_node
14893            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14894     decl = error_mark_node;
14895
14896   if (decl == error_mark_node)
14897     cp_parser_error (parser, "expected class-name");
14898   else if (identifier && !parser->scope)
14899     maybe_note_name_used_in_class (identifier, decl);
14900
14901   return decl;
14902 }
14903
14904 /* Parse a class-specifier.
14905
14906    class-specifier:
14907      class-head { member-specification [opt] }
14908
14909    Returns the TREE_TYPE representing the class.  */
14910
14911 static tree
14912 cp_parser_class_specifier (cp_parser* parser)
14913 {
14914   cp_token *token;
14915   tree type;
14916   tree attributes = NULL_TREE;
14917   int has_trailing_semicolon;
14918   bool nested_name_specifier_p;
14919   unsigned saved_num_template_parameter_lists;
14920   bool saved_in_function_body;
14921   bool saved_in_unbraced_linkage_specification_p;
14922   tree old_scope = NULL_TREE;
14923   tree scope = NULL_TREE;
14924   tree bases;
14925
14926   push_deferring_access_checks (dk_no_deferred);
14927
14928   /* Parse the class-head.  */
14929   type = cp_parser_class_head (parser,
14930                                &nested_name_specifier_p,
14931                                &attributes,
14932                                &bases);
14933   /* If the class-head was a semantic disaster, skip the entire body
14934      of the class.  */
14935   if (!type)
14936     {
14937       cp_parser_skip_to_end_of_block_or_statement (parser);
14938       pop_deferring_access_checks ();
14939       return error_mark_node;
14940     }
14941
14942   /* Look for the `{'.  */
14943   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14944     {
14945       pop_deferring_access_checks ();
14946       return error_mark_node;
14947     }
14948
14949   /* Process the base classes. If they're invalid, skip the 
14950      entire class body.  */
14951   if (!xref_basetypes (type, bases))
14952     {
14953       /* Consuming the closing brace yields better error messages
14954          later on.  */
14955       if (cp_parser_skip_to_closing_brace (parser))
14956         cp_lexer_consume_token (parser->lexer);
14957       pop_deferring_access_checks ();
14958       return error_mark_node;
14959     }
14960
14961   /* Issue an error message if type-definitions are forbidden here.  */
14962   cp_parser_check_type_definition (parser);
14963   /* Remember that we are defining one more class.  */
14964   ++parser->num_classes_being_defined;
14965   /* Inside the class, surrounding template-parameter-lists do not
14966      apply.  */
14967   saved_num_template_parameter_lists
14968     = parser->num_template_parameter_lists;
14969   parser->num_template_parameter_lists = 0;
14970   /* We are not in a function body.  */
14971   saved_in_function_body = parser->in_function_body;
14972   parser->in_function_body = false;
14973   /* We are not immediately inside an extern "lang" block.  */
14974   saved_in_unbraced_linkage_specification_p
14975     = parser->in_unbraced_linkage_specification_p;
14976   parser->in_unbraced_linkage_specification_p = false;
14977
14978   /* Start the class.  */
14979   if (nested_name_specifier_p)
14980     {
14981       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14982       old_scope = push_inner_scope (scope);
14983     }
14984   type = begin_class_definition (type, attributes);
14985
14986   if (type == error_mark_node)
14987     /* If the type is erroneous, skip the entire body of the class.  */
14988     cp_parser_skip_to_closing_brace (parser);
14989   else
14990     /* Parse the member-specification.  */
14991     cp_parser_member_specification_opt (parser);
14992
14993   /* Look for the trailing `}'.  */
14994   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14995   /* We get better error messages by noticing a common problem: a
14996      missing trailing `;'.  */
14997   token = cp_lexer_peek_token (parser->lexer);
14998   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14999   /* Look for trailing attributes to apply to this class.  */
15000   if (cp_parser_allow_gnu_extensions_p (parser))
15001     attributes = cp_parser_attributes_opt (parser);
15002   if (type != error_mark_node)
15003     type = finish_struct (type, attributes);
15004   if (nested_name_specifier_p)
15005     pop_inner_scope (old_scope, scope);
15006   /* If this class is not itself within the scope of another class,
15007      then we need to parse the bodies of all of the queued function
15008      definitions.  Note that the queued functions defined in a class
15009      are not always processed immediately following the
15010      class-specifier for that class.  Consider:
15011
15012        struct A {
15013          struct B { void f() { sizeof (A); } };
15014        };
15015
15016      If `f' were processed before the processing of `A' were
15017      completed, there would be no way to compute the size of `A'.
15018      Note that the nesting we are interested in here is lexical --
15019      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15020      for:
15021
15022        struct A { struct B; };
15023        struct A::B { void f() { } };
15024
15025      there is no need to delay the parsing of `A::B::f'.  */
15026   if (--parser->num_classes_being_defined == 0)
15027     {
15028       tree queue_entry;
15029       tree fn;
15030       tree class_type = NULL_TREE;
15031       tree pushed_scope = NULL_TREE;
15032
15033       /* In a first pass, parse default arguments to the functions.
15034          Then, in a second pass, parse the bodies of the functions.
15035          This two-phased approach handles cases like:
15036
15037             struct S {
15038               void f() { g(); }
15039               void g(int i = 3);
15040             };
15041
15042          */
15043       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15044              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15045            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15046            TREE_PURPOSE (parser->unparsed_functions_queues)
15047              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15048         {
15049           fn = TREE_VALUE (queue_entry);
15050           /* If there are default arguments that have not yet been processed,
15051              take care of them now.  */
15052           if (class_type != TREE_PURPOSE (queue_entry))
15053             {
15054               if (pushed_scope)
15055                 pop_scope (pushed_scope);
15056               class_type = TREE_PURPOSE (queue_entry);
15057               pushed_scope = push_scope (class_type);
15058             }
15059           /* Make sure that any template parameters are in scope.  */
15060           maybe_begin_member_template_processing (fn);
15061           /* Parse the default argument expressions.  */
15062           cp_parser_late_parsing_default_args (parser, fn);
15063           /* Remove any template parameters from the symbol table.  */
15064           maybe_end_member_template_processing ();
15065         }
15066       if (pushed_scope)
15067         pop_scope (pushed_scope);
15068       /* Now parse the body of the functions.  */
15069       for (TREE_VALUE (parser->unparsed_functions_queues)
15070              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15071            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15072            TREE_VALUE (parser->unparsed_functions_queues)
15073              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15074         {
15075           /* Figure out which function we need to process.  */
15076           fn = TREE_VALUE (queue_entry);
15077           /* Parse the function.  */
15078           cp_parser_late_parsing_for_member (parser, fn);
15079         }
15080     }
15081
15082   /* Put back any saved access checks.  */
15083   pop_deferring_access_checks ();
15084
15085   /* Restore saved state.  */
15086   parser->in_function_body = saved_in_function_body;
15087   parser->num_template_parameter_lists
15088     = saved_num_template_parameter_lists;
15089   parser->in_unbraced_linkage_specification_p
15090     = saved_in_unbraced_linkage_specification_p;
15091
15092   return type;
15093 }
15094
15095 /* Parse a class-head.
15096
15097    class-head:
15098      class-key identifier [opt] base-clause [opt]
15099      class-key nested-name-specifier identifier base-clause [opt]
15100      class-key nested-name-specifier [opt] template-id
15101        base-clause [opt]
15102
15103    GNU Extensions:
15104      class-key attributes identifier [opt] base-clause [opt]
15105      class-key attributes nested-name-specifier identifier base-clause [opt]
15106      class-key attributes nested-name-specifier [opt] template-id
15107        base-clause [opt]
15108
15109    Upon return BASES is initialized to the list of base classes (or
15110    NULL, if there are none) in the same form returned by
15111    cp_parser_base_clause.
15112
15113    Returns the TYPE of the indicated class.  Sets
15114    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15115    involving a nested-name-specifier was used, and FALSE otherwise.
15116
15117    Returns error_mark_node if this is not a class-head.
15118
15119    Returns NULL_TREE if the class-head is syntactically valid, but
15120    semantically invalid in a way that means we should skip the entire
15121    body of the class.  */
15122
15123 static tree
15124 cp_parser_class_head (cp_parser* parser,
15125                       bool* nested_name_specifier_p,
15126                       tree *attributes_p,
15127                       tree *bases)
15128 {
15129   tree nested_name_specifier;
15130   enum tag_types class_key;
15131   tree id = NULL_TREE;
15132   tree type = NULL_TREE;
15133   tree attributes;
15134   bool template_id_p = false;
15135   bool qualified_p = false;
15136   bool invalid_nested_name_p = false;
15137   bool invalid_explicit_specialization_p = false;
15138   tree pushed_scope = NULL_TREE;
15139   unsigned num_templates;
15140   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15141   /* Assume no nested-name-specifier will be present.  */
15142   *nested_name_specifier_p = false;
15143   /* Assume no template parameter lists will be used in defining the
15144      type.  */
15145   num_templates = 0;
15146
15147   *bases = NULL_TREE;
15148
15149   /* Look for the class-key.  */
15150   class_key = cp_parser_class_key (parser);
15151   if (class_key == none_type)
15152     return error_mark_node;
15153
15154   /* Parse the attributes.  */
15155   attributes = cp_parser_attributes_opt (parser);
15156
15157   /* If the next token is `::', that is invalid -- but sometimes
15158      people do try to write:
15159
15160        struct ::S {};
15161
15162      Handle this gracefully by accepting the extra qualifier, and then
15163      issuing an error about it later if this really is a
15164      class-head.  If it turns out just to be an elaborated type
15165      specifier, remain silent.  */
15166   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15167     qualified_p = true;
15168
15169   push_deferring_access_checks (dk_no_check);
15170
15171   /* Determine the name of the class.  Begin by looking for an
15172      optional nested-name-specifier.  */
15173   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15174   nested_name_specifier
15175     = cp_parser_nested_name_specifier_opt (parser,
15176                                            /*typename_keyword_p=*/false,
15177                                            /*check_dependency_p=*/false,
15178                                            /*type_p=*/false,
15179                                            /*is_declaration=*/false);
15180   /* If there was a nested-name-specifier, then there *must* be an
15181      identifier.  */
15182   if (nested_name_specifier)
15183     {
15184       type_start_token = cp_lexer_peek_token (parser->lexer);
15185       /* Although the grammar says `identifier', it really means
15186          `class-name' or `template-name'.  You are only allowed to
15187          define a class that has already been declared with this
15188          syntax.
15189
15190          The proposed resolution for Core Issue 180 says that wherever
15191          you see `class T::X' you should treat `X' as a type-name.
15192
15193          It is OK to define an inaccessible class; for example:
15194
15195            class A { class B; };
15196            class A::B {};
15197
15198          We do not know if we will see a class-name, or a
15199          template-name.  We look for a class-name first, in case the
15200          class-name is a template-id; if we looked for the
15201          template-name first we would stop after the template-name.  */
15202       cp_parser_parse_tentatively (parser);
15203       type = cp_parser_class_name (parser,
15204                                    /*typename_keyword_p=*/false,
15205                                    /*template_keyword_p=*/false,
15206                                    class_type,
15207                                    /*check_dependency_p=*/false,
15208                                    /*class_head_p=*/true,
15209                                    /*is_declaration=*/false);
15210       /* If that didn't work, ignore the nested-name-specifier.  */
15211       if (!cp_parser_parse_definitely (parser))
15212         {
15213           invalid_nested_name_p = true;
15214           type_start_token = cp_lexer_peek_token (parser->lexer);
15215           id = cp_parser_identifier (parser);
15216           if (id == error_mark_node)
15217             id = NULL_TREE;
15218         }
15219       /* If we could not find a corresponding TYPE, treat this
15220          declaration like an unqualified declaration.  */
15221       if (type == error_mark_node)
15222         nested_name_specifier = NULL_TREE;
15223       /* Otherwise, count the number of templates used in TYPE and its
15224          containing scopes.  */
15225       else
15226         {
15227           tree scope;
15228
15229           for (scope = TREE_TYPE (type);
15230                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15231                scope = (TYPE_P (scope)
15232                         ? TYPE_CONTEXT (scope)
15233                         : DECL_CONTEXT (scope)))
15234             if (TYPE_P (scope)
15235                 && CLASS_TYPE_P (scope)
15236                 && CLASSTYPE_TEMPLATE_INFO (scope)
15237                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15238                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15239               ++num_templates;
15240         }
15241     }
15242   /* Otherwise, the identifier is optional.  */
15243   else
15244     {
15245       /* We don't know whether what comes next is a template-id,
15246          an identifier, or nothing at all.  */
15247       cp_parser_parse_tentatively (parser);
15248       /* Check for a template-id.  */
15249       type_start_token = cp_lexer_peek_token (parser->lexer);
15250       id = cp_parser_template_id (parser,
15251                                   /*template_keyword_p=*/false,
15252                                   /*check_dependency_p=*/true,
15253                                   /*is_declaration=*/true);
15254       /* If that didn't work, it could still be an identifier.  */
15255       if (!cp_parser_parse_definitely (parser))
15256         {
15257           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15258             {
15259               type_start_token = cp_lexer_peek_token (parser->lexer);
15260               id = cp_parser_identifier (parser);
15261             }
15262           else
15263             id = NULL_TREE;
15264         }
15265       else
15266         {
15267           template_id_p = true;
15268           ++num_templates;
15269         }
15270     }
15271
15272   pop_deferring_access_checks ();
15273
15274   if (id)
15275     cp_parser_check_for_invalid_template_id (parser, id,
15276                                              type_start_token->location);
15277
15278   /* If it's not a `:' or a `{' then we can't really be looking at a
15279      class-head, since a class-head only appears as part of a
15280      class-specifier.  We have to detect this situation before calling
15281      xref_tag, since that has irreversible side-effects.  */
15282   if (!cp_parser_next_token_starts_class_definition_p (parser))
15283     {
15284       cp_parser_error (parser, "expected %<{%> or %<:%>");
15285       return error_mark_node;
15286     }
15287
15288   /* At this point, we're going ahead with the class-specifier, even
15289      if some other problem occurs.  */
15290   cp_parser_commit_to_tentative_parse (parser);
15291   /* Issue the error about the overly-qualified name now.  */
15292   if (qualified_p)
15293     {
15294       cp_parser_error (parser,
15295                        "global qualification of class name is invalid");
15296       return error_mark_node;
15297     }
15298   else if (invalid_nested_name_p)
15299     {
15300       cp_parser_error (parser,
15301                        "qualified name does not name a class");
15302       return error_mark_node;
15303     }
15304   else if (nested_name_specifier)
15305     {
15306       tree scope;
15307
15308       /* Reject typedef-names in class heads.  */
15309       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15310         {
15311           error ("%Hinvalid class name in declaration of %qD",
15312                  &type_start_token->location, type);
15313           type = NULL_TREE;
15314           goto done;
15315         }
15316
15317       /* Figure out in what scope the declaration is being placed.  */
15318       scope = current_scope ();
15319       /* If that scope does not contain the scope in which the
15320          class was originally declared, the program is invalid.  */
15321       if (scope && !is_ancestor (scope, nested_name_specifier))
15322         {
15323           if (at_namespace_scope_p ())
15324             error ("%Hdeclaration of %qD in namespace %qD which does not "
15325                    "enclose %qD",
15326                    &type_start_token->location,
15327                    type, scope, nested_name_specifier);
15328           else
15329             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15330                    &type_start_token->location,
15331                    type, scope, nested_name_specifier);
15332           type = NULL_TREE;
15333           goto done;
15334         }
15335       /* [dcl.meaning]
15336
15337          A declarator-id shall not be qualified except for the
15338          definition of a ... nested class outside of its class
15339          ... [or] the definition or explicit instantiation of a
15340          class member of a namespace outside of its namespace.  */
15341       if (scope == nested_name_specifier)
15342         {
15343           permerror (input_location, "%Hextra qualification not allowed",
15344                      &nested_name_specifier_token_start->location);
15345           nested_name_specifier = NULL_TREE;
15346           num_templates = 0;
15347         }
15348     }
15349   /* An explicit-specialization must be preceded by "template <>".  If
15350      it is not, try to recover gracefully.  */
15351   if (at_namespace_scope_p ()
15352       && parser->num_template_parameter_lists == 0
15353       && template_id_p)
15354     {
15355       error ("%Han explicit specialization must be preceded by %<template <>%>",
15356              &type_start_token->location);
15357       invalid_explicit_specialization_p = true;
15358       /* Take the same action that would have been taken by
15359          cp_parser_explicit_specialization.  */
15360       ++parser->num_template_parameter_lists;
15361       begin_specialization ();
15362     }
15363   /* There must be no "return" statements between this point and the
15364      end of this function; set "type "to the correct return value and
15365      use "goto done;" to return.  */
15366   /* Make sure that the right number of template parameters were
15367      present.  */
15368   if (!cp_parser_check_template_parameters (parser, num_templates,
15369                                             type_start_token->location))
15370     {
15371       /* If something went wrong, there is no point in even trying to
15372          process the class-definition.  */
15373       type = NULL_TREE;
15374       goto done;
15375     }
15376
15377   /* Look up the type.  */
15378   if (template_id_p)
15379     {
15380       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15381           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15382               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15383         {
15384           error ("%Hfunction template %qD redeclared as a class template",
15385                  &type_start_token->location, id);
15386           type = error_mark_node;
15387         }
15388       else
15389         {
15390           type = TREE_TYPE (id);
15391           type = maybe_process_partial_specialization (type);
15392         }
15393       if (nested_name_specifier)
15394         pushed_scope = push_scope (nested_name_specifier);
15395     }
15396   else if (nested_name_specifier)
15397     {
15398       tree class_type;
15399
15400       /* Given:
15401
15402             template <typename T> struct S { struct T };
15403             template <typename T> struct S<T>::T { };
15404
15405          we will get a TYPENAME_TYPE when processing the definition of
15406          `S::T'.  We need to resolve it to the actual type before we
15407          try to define it.  */
15408       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15409         {
15410           class_type = resolve_typename_type (TREE_TYPE (type),
15411                                               /*only_current_p=*/false);
15412           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15413             type = TYPE_NAME (class_type);
15414           else
15415             {
15416               cp_parser_error (parser, "could not resolve typename type");
15417               type = error_mark_node;
15418             }
15419         }
15420
15421       if (maybe_process_partial_specialization (TREE_TYPE (type))
15422           == error_mark_node)
15423         {
15424           type = NULL_TREE;
15425           goto done;
15426         }
15427
15428       class_type = current_class_type;
15429       /* Enter the scope indicated by the nested-name-specifier.  */
15430       pushed_scope = push_scope (nested_name_specifier);
15431       /* Get the canonical version of this type.  */
15432       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15433       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15434           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15435         {
15436           type = push_template_decl (type);
15437           if (type == error_mark_node)
15438             {
15439               type = NULL_TREE;
15440               goto done;
15441             }
15442         }
15443
15444       type = TREE_TYPE (type);
15445       *nested_name_specifier_p = true;
15446     }
15447   else      /* The name is not a nested name.  */
15448     {
15449       /* If the class was unnamed, create a dummy name.  */
15450       if (!id)
15451         id = make_anon_name ();
15452       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15453                        parser->num_template_parameter_lists);
15454     }
15455
15456   /* Indicate whether this class was declared as a `class' or as a
15457      `struct'.  */
15458   if (TREE_CODE (type) == RECORD_TYPE)
15459     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15460   cp_parser_check_class_key (class_key, type);
15461
15462   /* If this type was already complete, and we see another definition,
15463      that's an error.  */
15464   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15465     {
15466       error ("%Hredefinition of %q#T",
15467              &type_start_token->location, type);
15468       error ("%Hprevious definition of %q+#T",
15469              &type_start_token->location, type);
15470       type = NULL_TREE;
15471       goto done;
15472     }
15473   else if (type == error_mark_node)
15474     type = NULL_TREE;
15475
15476   /* We will have entered the scope containing the class; the names of
15477      base classes should be looked up in that context.  For example:
15478
15479        struct A { struct B {}; struct C; };
15480        struct A::C : B {};
15481
15482      is valid.  */
15483
15484   /* Get the list of base-classes, if there is one.  */
15485   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15486     *bases = cp_parser_base_clause (parser);
15487
15488  done:
15489   /* Leave the scope given by the nested-name-specifier.  We will
15490      enter the class scope itself while processing the members.  */
15491   if (pushed_scope)
15492     pop_scope (pushed_scope);
15493
15494   if (invalid_explicit_specialization_p)
15495     {
15496       end_specialization ();
15497       --parser->num_template_parameter_lists;
15498     }
15499   *attributes_p = attributes;
15500   return type;
15501 }
15502
15503 /* Parse a class-key.
15504
15505    class-key:
15506      class
15507      struct
15508      union
15509
15510    Returns the kind of class-key specified, or none_type to indicate
15511    error.  */
15512
15513 static enum tag_types
15514 cp_parser_class_key (cp_parser* parser)
15515 {
15516   cp_token *token;
15517   enum tag_types tag_type;
15518
15519   /* Look for the class-key.  */
15520   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15521   if (!token)
15522     return none_type;
15523
15524   /* Check to see if the TOKEN is a class-key.  */
15525   tag_type = cp_parser_token_is_class_key (token);
15526   if (!tag_type)
15527     cp_parser_error (parser, "expected class-key");
15528   return tag_type;
15529 }
15530
15531 /* Parse an (optional) member-specification.
15532
15533    member-specification:
15534      member-declaration member-specification [opt]
15535      access-specifier : member-specification [opt]  */
15536
15537 static void
15538 cp_parser_member_specification_opt (cp_parser* parser)
15539 {
15540   while (true)
15541     {
15542       cp_token *token;
15543       enum rid keyword;
15544
15545       /* Peek at the next token.  */
15546       token = cp_lexer_peek_token (parser->lexer);
15547       /* If it's a `}', or EOF then we've seen all the members.  */
15548       if (token->type == CPP_CLOSE_BRACE
15549           || token->type == CPP_EOF
15550           || token->type == CPP_PRAGMA_EOL)
15551         break;
15552
15553       /* See if this token is a keyword.  */
15554       keyword = token->keyword;
15555       switch (keyword)
15556         {
15557         case RID_PUBLIC:
15558         case RID_PROTECTED:
15559         case RID_PRIVATE:
15560           /* Consume the access-specifier.  */
15561           cp_lexer_consume_token (parser->lexer);
15562           /* Remember which access-specifier is active.  */
15563           current_access_specifier = token->u.value;
15564           /* Look for the `:'.  */
15565           cp_parser_require (parser, CPP_COLON, "%<:%>");
15566           break;
15567
15568         default:
15569           /* Accept #pragmas at class scope.  */
15570           if (token->type == CPP_PRAGMA)
15571             {
15572               cp_parser_pragma (parser, pragma_external);
15573               break;
15574             }
15575
15576           /* Otherwise, the next construction must be a
15577              member-declaration.  */
15578           cp_parser_member_declaration (parser);
15579         }
15580     }
15581 }
15582
15583 /* Parse a member-declaration.
15584
15585    member-declaration:
15586      decl-specifier-seq [opt] member-declarator-list [opt] ;
15587      function-definition ; [opt]
15588      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15589      using-declaration
15590      template-declaration
15591
15592    member-declarator-list:
15593      member-declarator
15594      member-declarator-list , member-declarator
15595
15596    member-declarator:
15597      declarator pure-specifier [opt]
15598      declarator constant-initializer [opt]
15599      identifier [opt] : constant-expression
15600
15601    GNU Extensions:
15602
15603    member-declaration:
15604      __extension__ member-declaration
15605
15606    member-declarator:
15607      declarator attributes [opt] pure-specifier [opt]
15608      declarator attributes [opt] constant-initializer [opt]
15609      identifier [opt] attributes [opt] : constant-expression  
15610
15611    C++0x Extensions:
15612
15613    member-declaration:
15614      static_assert-declaration  */
15615
15616 static void
15617 cp_parser_member_declaration (cp_parser* parser)
15618 {
15619   cp_decl_specifier_seq decl_specifiers;
15620   tree prefix_attributes;
15621   tree decl;
15622   int declares_class_or_enum;
15623   bool friend_p;
15624   cp_token *token = NULL;
15625   cp_token *decl_spec_token_start = NULL;
15626   cp_token *initializer_token_start = NULL;
15627   int saved_pedantic;
15628
15629   /* Check for the `__extension__' keyword.  */
15630   if (cp_parser_extension_opt (parser, &saved_pedantic))
15631     {
15632       /* Recurse.  */
15633       cp_parser_member_declaration (parser);
15634       /* Restore the old value of the PEDANTIC flag.  */
15635       pedantic = saved_pedantic;
15636
15637       return;
15638     }
15639
15640   /* Check for a template-declaration.  */
15641   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15642     {
15643       /* An explicit specialization here is an error condition, and we
15644          expect the specialization handler to detect and report this.  */
15645       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15646           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15647         cp_parser_explicit_specialization (parser);
15648       else
15649         cp_parser_template_declaration (parser, /*member_p=*/true);
15650
15651       return;
15652     }
15653
15654   /* Check for a using-declaration.  */
15655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15656     {
15657       /* Parse the using-declaration.  */
15658       cp_parser_using_declaration (parser,
15659                                    /*access_declaration_p=*/false);
15660       return;
15661     }
15662
15663   /* Check for @defs.  */
15664   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15665     {
15666       tree ivar, member;
15667       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15668       ivar = ivar_chains;
15669       while (ivar)
15670         {
15671           member = ivar;
15672           ivar = TREE_CHAIN (member);
15673           TREE_CHAIN (member) = NULL_TREE;
15674           finish_member_declaration (member);
15675         }
15676       return;
15677     }
15678
15679   /* If the next token is `static_assert' we have a static assertion.  */
15680   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15681     {
15682       cp_parser_static_assert (parser, /*member_p=*/true);
15683       return;
15684     }
15685
15686   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15687     return;
15688
15689   /* Parse the decl-specifier-seq.  */
15690   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15691   cp_parser_decl_specifier_seq (parser,
15692                                 CP_PARSER_FLAGS_OPTIONAL,
15693                                 &decl_specifiers,
15694                                 &declares_class_or_enum);
15695   prefix_attributes = decl_specifiers.attributes;
15696   decl_specifiers.attributes = NULL_TREE;
15697   /* Check for an invalid type-name.  */
15698   if (!decl_specifiers.type
15699       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15700     return;
15701   /* If there is no declarator, then the decl-specifier-seq should
15702      specify a type.  */
15703   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15704     {
15705       /* If there was no decl-specifier-seq, and the next token is a
15706          `;', then we have something like:
15707
15708            struct S { ; };
15709
15710          [class.mem]
15711
15712          Each member-declaration shall declare at least one member
15713          name of the class.  */
15714       if (!decl_specifiers.any_specifiers_p)
15715         {
15716           cp_token *token = cp_lexer_peek_token (parser->lexer);
15717           if (!in_system_header_at (token->location))
15718             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15719         }
15720       else
15721         {
15722           tree type;
15723
15724           /* See if this declaration is a friend.  */
15725           friend_p = cp_parser_friend_p (&decl_specifiers);
15726           /* If there were decl-specifiers, check to see if there was
15727              a class-declaration.  */
15728           type = check_tag_decl (&decl_specifiers);
15729           /* Nested classes have already been added to the class, but
15730              a `friend' needs to be explicitly registered.  */
15731           if (friend_p)
15732             {
15733               /* If the `friend' keyword was present, the friend must
15734                  be introduced with a class-key.  */
15735                if (!declares_class_or_enum)
15736                  error ("%Ha class-key must be used when declaring a friend",
15737                         &decl_spec_token_start->location);
15738                /* In this case:
15739
15740                     template <typename T> struct A {
15741                       friend struct A<T>::B;
15742                     };
15743
15744                   A<T>::B will be represented by a TYPENAME_TYPE, and
15745                   therefore not recognized by check_tag_decl.  */
15746                if (!type
15747                    && decl_specifiers.type
15748                    && TYPE_P (decl_specifiers.type))
15749                  type = decl_specifiers.type;
15750                if (!type || !TYPE_P (type))
15751                  error ("%Hfriend declaration does not name a class or "
15752                         "function", &decl_spec_token_start->location);
15753                else
15754                  make_friend_class (current_class_type, type,
15755                                     /*complain=*/true);
15756             }
15757           /* If there is no TYPE, an error message will already have
15758              been issued.  */
15759           else if (!type || type == error_mark_node)
15760             ;
15761           /* An anonymous aggregate has to be handled specially; such
15762              a declaration really declares a data member (with a
15763              particular type), as opposed to a nested class.  */
15764           else if (ANON_AGGR_TYPE_P (type))
15765             {
15766               /* Remove constructors and such from TYPE, now that we
15767                  know it is an anonymous aggregate.  */
15768               fixup_anonymous_aggr (type);
15769               /* And make the corresponding data member.  */
15770               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15771               /* Add it to the class.  */
15772               finish_member_declaration (decl);
15773             }
15774           else
15775             cp_parser_check_access_in_redeclaration
15776                                               (TYPE_NAME (type),
15777                                                decl_spec_token_start->location);
15778         }
15779     }
15780   else
15781     {
15782       /* See if these declarations will be friends.  */
15783       friend_p = cp_parser_friend_p (&decl_specifiers);
15784
15785       /* Keep going until we hit the `;' at the end of the
15786          declaration.  */
15787       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15788         {
15789           tree attributes = NULL_TREE;
15790           tree first_attribute;
15791
15792           /* Peek at the next token.  */
15793           token = cp_lexer_peek_token (parser->lexer);
15794
15795           /* Check for a bitfield declaration.  */
15796           if (token->type == CPP_COLON
15797               || (token->type == CPP_NAME
15798                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15799                   == CPP_COLON))
15800             {
15801               tree identifier;
15802               tree width;
15803
15804               /* Get the name of the bitfield.  Note that we cannot just
15805                  check TOKEN here because it may have been invalidated by
15806                  the call to cp_lexer_peek_nth_token above.  */
15807               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15808                 identifier = cp_parser_identifier (parser);
15809               else
15810                 identifier = NULL_TREE;
15811
15812               /* Consume the `:' token.  */
15813               cp_lexer_consume_token (parser->lexer);
15814               /* Get the width of the bitfield.  */
15815               width
15816                 = cp_parser_constant_expression (parser,
15817                                                  /*allow_non_constant=*/false,
15818                                                  NULL);
15819
15820               /* Look for attributes that apply to the bitfield.  */
15821               attributes = cp_parser_attributes_opt (parser);
15822               /* Remember which attributes are prefix attributes and
15823                  which are not.  */
15824               first_attribute = attributes;
15825               /* Combine the attributes.  */
15826               attributes = chainon (prefix_attributes, attributes);
15827
15828               /* Create the bitfield declaration.  */
15829               decl = grokbitfield (identifier
15830                                    ? make_id_declarator (NULL_TREE,
15831                                                          identifier,
15832                                                          sfk_none)
15833                                    : NULL,
15834                                    &decl_specifiers,
15835                                    width,
15836                                    attributes);
15837             }
15838           else
15839             {
15840               cp_declarator *declarator;
15841               tree initializer;
15842               tree asm_specification;
15843               int ctor_dtor_or_conv_p;
15844
15845               /* Parse the declarator.  */
15846               declarator
15847                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15848                                         &ctor_dtor_or_conv_p,
15849                                         /*parenthesized_p=*/NULL,
15850                                         /*member_p=*/true);
15851
15852               /* If something went wrong parsing the declarator, make sure
15853                  that we at least consume some tokens.  */
15854               if (declarator == cp_error_declarator)
15855                 {
15856                   /* Skip to the end of the statement.  */
15857                   cp_parser_skip_to_end_of_statement (parser);
15858                   /* If the next token is not a semicolon, that is
15859                      probably because we just skipped over the body of
15860                      a function.  So, we consume a semicolon if
15861                      present, but do not issue an error message if it
15862                      is not present.  */
15863                   if (cp_lexer_next_token_is (parser->lexer,
15864                                               CPP_SEMICOLON))
15865                     cp_lexer_consume_token (parser->lexer);
15866                   return;
15867                 }
15868
15869               if (declares_class_or_enum & 2)
15870                 cp_parser_check_for_definition_in_return_type
15871                                             (declarator, decl_specifiers.type,
15872                                              decl_specifiers.type_location);
15873
15874               /* Look for an asm-specification.  */
15875               asm_specification = cp_parser_asm_specification_opt (parser);
15876               /* Look for attributes that apply to the declaration.  */
15877               attributes = cp_parser_attributes_opt (parser);
15878               /* Remember which attributes are prefix attributes and
15879                  which are not.  */
15880               first_attribute = attributes;
15881               /* Combine the attributes.  */
15882               attributes = chainon (prefix_attributes, attributes);
15883
15884               /* If it's an `=', then we have a constant-initializer or a
15885                  pure-specifier.  It is not correct to parse the
15886                  initializer before registering the member declaration
15887                  since the member declaration should be in scope while
15888                  its initializer is processed.  However, the rest of the
15889                  front end does not yet provide an interface that allows
15890                  us to handle this correctly.  */
15891               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15892                 {
15893                   /* In [class.mem]:
15894
15895                      A pure-specifier shall be used only in the declaration of
15896                      a virtual function.
15897
15898                      A member-declarator can contain a constant-initializer
15899                      only if it declares a static member of integral or
15900                      enumeration type.
15901
15902                      Therefore, if the DECLARATOR is for a function, we look
15903                      for a pure-specifier; otherwise, we look for a
15904                      constant-initializer.  When we call `grokfield', it will
15905                      perform more stringent semantics checks.  */
15906                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15907                   if (function_declarator_p (declarator))
15908                     initializer = cp_parser_pure_specifier (parser);
15909                   else
15910                     /* Parse the initializer.  */
15911                     initializer = cp_parser_constant_initializer (parser);
15912                 }
15913               /* Otherwise, there is no initializer.  */
15914               else
15915                 initializer = NULL_TREE;
15916
15917               /* See if we are probably looking at a function
15918                  definition.  We are certainly not looking at a
15919                  member-declarator.  Calling `grokfield' has
15920                  side-effects, so we must not do it unless we are sure
15921                  that we are looking at a member-declarator.  */
15922               if (cp_parser_token_starts_function_definition_p
15923                   (cp_lexer_peek_token (parser->lexer)))
15924                 {
15925                   /* The grammar does not allow a pure-specifier to be
15926                      used when a member function is defined.  (It is
15927                      possible that this fact is an oversight in the
15928                      standard, since a pure function may be defined
15929                      outside of the class-specifier.  */
15930                   if (initializer)
15931                     error ("%Hpure-specifier on function-definition",
15932                            &initializer_token_start->location);
15933                   decl = cp_parser_save_member_function_body (parser,
15934                                                               &decl_specifiers,
15935                                                               declarator,
15936                                                               attributes);
15937                   /* If the member was not a friend, declare it here.  */
15938                   if (!friend_p)
15939                     finish_member_declaration (decl);
15940                   /* Peek at the next token.  */
15941                   token = cp_lexer_peek_token (parser->lexer);
15942                   /* If the next token is a semicolon, consume it.  */
15943                   if (token->type == CPP_SEMICOLON)
15944                     cp_lexer_consume_token (parser->lexer);
15945                   return;
15946                 }
15947               else
15948                 if (declarator->kind == cdk_function)
15949                   declarator->id_loc = token->location;
15950                 /* Create the declaration.  */
15951                 decl = grokfield (declarator, &decl_specifiers,
15952                                   initializer, /*init_const_expr_p=*/true,
15953                                   asm_specification,
15954                                   attributes);
15955             }
15956
15957           /* Reset PREFIX_ATTRIBUTES.  */
15958           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15959             attributes = TREE_CHAIN (attributes);
15960           if (attributes)
15961             TREE_CHAIN (attributes) = NULL_TREE;
15962
15963           /* If there is any qualification still in effect, clear it
15964              now; we will be starting fresh with the next declarator.  */
15965           parser->scope = NULL_TREE;
15966           parser->qualifying_scope = NULL_TREE;
15967           parser->object_scope = NULL_TREE;
15968           /* If it's a `,', then there are more declarators.  */
15969           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15970             cp_lexer_consume_token (parser->lexer);
15971           /* If the next token isn't a `;', then we have a parse error.  */
15972           else if (cp_lexer_next_token_is_not (parser->lexer,
15973                                                CPP_SEMICOLON))
15974             {
15975               cp_parser_error (parser, "expected %<;%>");
15976               /* Skip tokens until we find a `;'.  */
15977               cp_parser_skip_to_end_of_statement (parser);
15978
15979               break;
15980             }
15981
15982           if (decl)
15983             {
15984               /* Add DECL to the list of members.  */
15985               if (!friend_p)
15986                 finish_member_declaration (decl);
15987
15988               if (TREE_CODE (decl) == FUNCTION_DECL)
15989                 cp_parser_save_default_args (parser, decl);
15990             }
15991         }
15992     }
15993
15994   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15995 }
15996
15997 /* Parse a pure-specifier.
15998
15999    pure-specifier:
16000      = 0
16001
16002    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16003    Otherwise, ERROR_MARK_NODE is returned.  */
16004
16005 static tree
16006 cp_parser_pure_specifier (cp_parser* parser)
16007 {
16008   cp_token *token;
16009
16010   /* Look for the `=' token.  */
16011   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16012     return error_mark_node;
16013   /* Look for the `0' token.  */
16014   token = cp_lexer_peek_token (parser->lexer);
16015
16016   if (token->type == CPP_EOF
16017       || token->type == CPP_PRAGMA_EOL)
16018     return error_mark_node;
16019
16020   cp_lexer_consume_token (parser->lexer);
16021
16022   /* Accept = default or = delete in c++0x mode.  */
16023   if (token->keyword == RID_DEFAULT
16024       || token->keyword == RID_DELETE)
16025     {
16026       maybe_warn_cpp0x ("defaulted and deleted functions");
16027       return token->u.value;
16028     }
16029
16030   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16031   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16032     {
16033       cp_parser_error (parser,
16034                        "invalid pure specifier (only %<= 0%> is allowed)");
16035       cp_parser_skip_to_end_of_statement (parser);
16036       return error_mark_node;
16037     }
16038   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16039     {
16040       error ("%Htemplates may not be %<virtual%>", &token->location);
16041       return error_mark_node;
16042     }
16043
16044   return integer_zero_node;
16045 }
16046
16047 /* Parse a constant-initializer.
16048
16049    constant-initializer:
16050      = constant-expression
16051
16052    Returns a representation of the constant-expression.  */
16053
16054 static tree
16055 cp_parser_constant_initializer (cp_parser* parser)
16056 {
16057   /* Look for the `=' token.  */
16058   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16059     return error_mark_node;
16060
16061   /* It is invalid to write:
16062
16063        struct S { static const int i = { 7 }; };
16064
16065      */
16066   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16067     {
16068       cp_parser_error (parser,
16069                        "a brace-enclosed initializer is not allowed here");
16070       /* Consume the opening brace.  */
16071       cp_lexer_consume_token (parser->lexer);
16072       /* Skip the initializer.  */
16073       cp_parser_skip_to_closing_brace (parser);
16074       /* Look for the trailing `}'.  */
16075       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16076
16077       return error_mark_node;
16078     }
16079
16080   return cp_parser_constant_expression (parser,
16081                                         /*allow_non_constant=*/false,
16082                                         NULL);
16083 }
16084
16085 /* Derived classes [gram.class.derived] */
16086
16087 /* Parse a base-clause.
16088
16089    base-clause:
16090      : base-specifier-list
16091
16092    base-specifier-list:
16093      base-specifier ... [opt]
16094      base-specifier-list , base-specifier ... [opt]
16095
16096    Returns a TREE_LIST representing the base-classes, in the order in
16097    which they were declared.  The representation of each node is as
16098    described by cp_parser_base_specifier.
16099
16100    In the case that no bases are specified, this function will return
16101    NULL_TREE, not ERROR_MARK_NODE.  */
16102
16103 static tree
16104 cp_parser_base_clause (cp_parser* parser)
16105 {
16106   tree bases = NULL_TREE;
16107
16108   /* Look for the `:' that begins the list.  */
16109   cp_parser_require (parser, CPP_COLON, "%<:%>");
16110
16111   /* Scan the base-specifier-list.  */
16112   while (true)
16113     {
16114       cp_token *token;
16115       tree base;
16116       bool pack_expansion_p = false;
16117
16118       /* Look for the base-specifier.  */
16119       base = cp_parser_base_specifier (parser);
16120       /* Look for the (optional) ellipsis. */
16121       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16122         {
16123           /* Consume the `...'. */
16124           cp_lexer_consume_token (parser->lexer);
16125
16126           pack_expansion_p = true;
16127         }
16128
16129       /* Add BASE to the front of the list.  */
16130       if (base != error_mark_node)
16131         {
16132           if (pack_expansion_p)
16133             /* Make this a pack expansion type. */
16134             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16135           
16136
16137           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16138             {
16139               TREE_CHAIN (base) = bases;
16140               bases = base;
16141             }
16142         }
16143       /* Peek at the next token.  */
16144       token = cp_lexer_peek_token (parser->lexer);
16145       /* If it's not a comma, then the list is complete.  */
16146       if (token->type != CPP_COMMA)
16147         break;
16148       /* Consume the `,'.  */
16149       cp_lexer_consume_token (parser->lexer);
16150     }
16151
16152   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16153      base class had a qualified name.  However, the next name that
16154      appears is certainly not qualified.  */
16155   parser->scope = NULL_TREE;
16156   parser->qualifying_scope = NULL_TREE;
16157   parser->object_scope = NULL_TREE;
16158
16159   return nreverse (bases);
16160 }
16161
16162 /* Parse a base-specifier.
16163
16164    base-specifier:
16165      :: [opt] nested-name-specifier [opt] class-name
16166      virtual access-specifier [opt] :: [opt] nested-name-specifier
16167        [opt] class-name
16168      access-specifier virtual [opt] :: [opt] nested-name-specifier
16169        [opt] class-name
16170
16171    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16172    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16173    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16174    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16175
16176 static tree
16177 cp_parser_base_specifier (cp_parser* parser)
16178 {
16179   cp_token *token;
16180   bool done = false;
16181   bool virtual_p = false;
16182   bool duplicate_virtual_error_issued_p = false;
16183   bool duplicate_access_error_issued_p = false;
16184   bool class_scope_p, template_p;
16185   tree access = access_default_node;
16186   tree type;
16187
16188   /* Process the optional `virtual' and `access-specifier'.  */
16189   while (!done)
16190     {
16191       /* Peek at the next token.  */
16192       token = cp_lexer_peek_token (parser->lexer);
16193       /* Process `virtual'.  */
16194       switch (token->keyword)
16195         {
16196         case RID_VIRTUAL:
16197           /* If `virtual' appears more than once, issue an error.  */
16198           if (virtual_p && !duplicate_virtual_error_issued_p)
16199             {
16200               cp_parser_error (parser,
16201                                "%<virtual%> specified more than once in base-specified");
16202               duplicate_virtual_error_issued_p = true;
16203             }
16204
16205           virtual_p = true;
16206
16207           /* Consume the `virtual' token.  */
16208           cp_lexer_consume_token (parser->lexer);
16209
16210           break;
16211
16212         case RID_PUBLIC:
16213         case RID_PROTECTED:
16214         case RID_PRIVATE:
16215           /* If more than one access specifier appears, issue an
16216              error.  */
16217           if (access != access_default_node
16218               && !duplicate_access_error_issued_p)
16219             {
16220               cp_parser_error (parser,
16221                                "more than one access specifier in base-specified");
16222               duplicate_access_error_issued_p = true;
16223             }
16224
16225           access = ridpointers[(int) token->keyword];
16226
16227           /* Consume the access-specifier.  */
16228           cp_lexer_consume_token (parser->lexer);
16229
16230           break;
16231
16232         default:
16233           done = true;
16234           break;
16235         }
16236     }
16237   /* It is not uncommon to see programs mechanically, erroneously, use
16238      the 'typename' keyword to denote (dependent) qualified types
16239      as base classes.  */
16240   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16241     {
16242       token = cp_lexer_peek_token (parser->lexer);
16243       if (!processing_template_decl)
16244         error ("%Hkeyword %<typename%> not allowed outside of templates",
16245                &token->location);
16246       else
16247         error ("%Hkeyword %<typename%> not allowed in this context "
16248                "(the base class is implicitly a type)",
16249                &token->location);
16250       cp_lexer_consume_token (parser->lexer);
16251     }
16252
16253   /* Look for the optional `::' operator.  */
16254   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16255   /* Look for the nested-name-specifier.  The simplest way to
16256      implement:
16257
16258        [temp.res]
16259
16260        The keyword `typename' is not permitted in a base-specifier or
16261        mem-initializer; in these contexts a qualified name that
16262        depends on a template-parameter is implicitly assumed to be a
16263        type name.
16264
16265      is to pretend that we have seen the `typename' keyword at this
16266      point.  */
16267   cp_parser_nested_name_specifier_opt (parser,
16268                                        /*typename_keyword_p=*/true,
16269                                        /*check_dependency_p=*/true,
16270                                        typename_type,
16271                                        /*is_declaration=*/true);
16272   /* If the base class is given by a qualified name, assume that names
16273      we see are type names or templates, as appropriate.  */
16274   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16275   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16276
16277   /* Finally, look for the class-name.  */
16278   type = cp_parser_class_name (parser,
16279                                class_scope_p,
16280                                template_p,
16281                                typename_type,
16282                                /*check_dependency_p=*/true,
16283                                /*class_head_p=*/false,
16284                                /*is_declaration=*/true);
16285
16286   if (type == error_mark_node)
16287     return error_mark_node;
16288
16289   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16290 }
16291
16292 /* Exception handling [gram.exception] */
16293
16294 /* Parse an (optional) exception-specification.
16295
16296    exception-specification:
16297      throw ( type-id-list [opt] )
16298
16299    Returns a TREE_LIST representing the exception-specification.  The
16300    TREE_VALUE of each node is a type.  */
16301
16302 static tree
16303 cp_parser_exception_specification_opt (cp_parser* parser)
16304 {
16305   cp_token *token;
16306   tree type_id_list;
16307
16308   /* Peek at the next token.  */
16309   token = cp_lexer_peek_token (parser->lexer);
16310   /* If it's not `throw', then there's no exception-specification.  */
16311   if (!cp_parser_is_keyword (token, RID_THROW))
16312     return NULL_TREE;
16313
16314   /* Consume the `throw'.  */
16315   cp_lexer_consume_token (parser->lexer);
16316
16317   /* Look for the `('.  */
16318   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16319
16320   /* Peek at the next token.  */
16321   token = cp_lexer_peek_token (parser->lexer);
16322   /* If it's not a `)', then there is a type-id-list.  */
16323   if (token->type != CPP_CLOSE_PAREN)
16324     {
16325       const char *saved_message;
16326
16327       /* Types may not be defined in an exception-specification.  */
16328       saved_message = parser->type_definition_forbidden_message;
16329       parser->type_definition_forbidden_message
16330         = "types may not be defined in an exception-specification";
16331       /* Parse the type-id-list.  */
16332       type_id_list = cp_parser_type_id_list (parser);
16333       /* Restore the saved message.  */
16334       parser->type_definition_forbidden_message = saved_message;
16335     }
16336   else
16337     type_id_list = empty_except_spec;
16338
16339   /* Look for the `)'.  */
16340   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16341
16342   return type_id_list;
16343 }
16344
16345 /* Parse an (optional) type-id-list.
16346
16347    type-id-list:
16348      type-id ... [opt]
16349      type-id-list , type-id ... [opt]
16350
16351    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16352    in the order that the types were presented.  */
16353
16354 static tree
16355 cp_parser_type_id_list (cp_parser* parser)
16356 {
16357   tree types = NULL_TREE;
16358
16359   while (true)
16360     {
16361       cp_token *token;
16362       tree type;
16363
16364       /* Get the next type-id.  */
16365       type = cp_parser_type_id (parser);
16366       /* Parse the optional ellipsis. */
16367       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16368         {
16369           /* Consume the `...'. */
16370           cp_lexer_consume_token (parser->lexer);
16371
16372           /* Turn the type into a pack expansion expression. */
16373           type = make_pack_expansion (type);
16374         }
16375       /* Add it to the list.  */
16376       types = add_exception_specifier (types, type, /*complain=*/1);
16377       /* Peek at the next token.  */
16378       token = cp_lexer_peek_token (parser->lexer);
16379       /* If it is not a `,', we are done.  */
16380       if (token->type != CPP_COMMA)
16381         break;
16382       /* Consume the `,'.  */
16383       cp_lexer_consume_token (parser->lexer);
16384     }
16385
16386   return nreverse (types);
16387 }
16388
16389 /* Parse a try-block.
16390
16391    try-block:
16392      try compound-statement handler-seq  */
16393
16394 static tree
16395 cp_parser_try_block (cp_parser* parser)
16396 {
16397   tree try_block;
16398
16399   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16400   try_block = begin_try_block ();
16401   cp_parser_compound_statement (parser, NULL, true);
16402   finish_try_block (try_block);
16403   cp_parser_handler_seq (parser);
16404   finish_handler_sequence (try_block);
16405
16406   return try_block;
16407 }
16408
16409 /* Parse a function-try-block.
16410
16411    function-try-block:
16412      try ctor-initializer [opt] function-body handler-seq  */
16413
16414 static bool
16415 cp_parser_function_try_block (cp_parser* parser)
16416 {
16417   tree compound_stmt;
16418   tree try_block;
16419   bool ctor_initializer_p;
16420
16421   /* Look for the `try' keyword.  */
16422   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16423     return false;
16424   /* Let the rest of the front end know where we are.  */
16425   try_block = begin_function_try_block (&compound_stmt);
16426   /* Parse the function-body.  */
16427   ctor_initializer_p
16428     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16429   /* We're done with the `try' part.  */
16430   finish_function_try_block (try_block);
16431   /* Parse the handlers.  */
16432   cp_parser_handler_seq (parser);
16433   /* We're done with the handlers.  */
16434   finish_function_handler_sequence (try_block, compound_stmt);
16435
16436   return ctor_initializer_p;
16437 }
16438
16439 /* Parse a handler-seq.
16440
16441    handler-seq:
16442      handler handler-seq [opt]  */
16443
16444 static void
16445 cp_parser_handler_seq (cp_parser* parser)
16446 {
16447   while (true)
16448     {
16449       cp_token *token;
16450
16451       /* Parse the handler.  */
16452       cp_parser_handler (parser);
16453       /* Peek at the next token.  */
16454       token = cp_lexer_peek_token (parser->lexer);
16455       /* If it's not `catch' then there are no more handlers.  */
16456       if (!cp_parser_is_keyword (token, RID_CATCH))
16457         break;
16458     }
16459 }
16460
16461 /* Parse a handler.
16462
16463    handler:
16464      catch ( exception-declaration ) compound-statement  */
16465
16466 static void
16467 cp_parser_handler (cp_parser* parser)
16468 {
16469   tree handler;
16470   tree declaration;
16471
16472   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16473   handler = begin_handler ();
16474   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16475   declaration = cp_parser_exception_declaration (parser);
16476   finish_handler_parms (declaration, handler);
16477   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16478   cp_parser_compound_statement (parser, NULL, false);
16479   finish_handler (handler);
16480 }
16481
16482 /* Parse an exception-declaration.
16483
16484    exception-declaration:
16485      type-specifier-seq declarator
16486      type-specifier-seq abstract-declarator
16487      type-specifier-seq
16488      ...
16489
16490    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16491    ellipsis variant is used.  */
16492
16493 static tree
16494 cp_parser_exception_declaration (cp_parser* parser)
16495 {
16496   cp_decl_specifier_seq type_specifiers;
16497   cp_declarator *declarator;
16498   const char *saved_message;
16499
16500   /* If it's an ellipsis, it's easy to handle.  */
16501   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16502     {
16503       /* Consume the `...' token.  */
16504       cp_lexer_consume_token (parser->lexer);
16505       return NULL_TREE;
16506     }
16507
16508   /* Types may not be defined in exception-declarations.  */
16509   saved_message = parser->type_definition_forbidden_message;
16510   parser->type_definition_forbidden_message
16511     = "types may not be defined in exception-declarations";
16512
16513   /* Parse the type-specifier-seq.  */
16514   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16515                                 &type_specifiers);
16516   /* If it's a `)', then there is no declarator.  */
16517   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16518     declarator = NULL;
16519   else
16520     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16521                                        /*ctor_dtor_or_conv_p=*/NULL,
16522                                        /*parenthesized_p=*/NULL,
16523                                        /*member_p=*/false);
16524
16525   /* Restore the saved message.  */
16526   parser->type_definition_forbidden_message = saved_message;
16527
16528   if (!type_specifiers.any_specifiers_p)
16529     return error_mark_node;
16530
16531   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16532 }
16533
16534 /* Parse a throw-expression.
16535
16536    throw-expression:
16537      throw assignment-expression [opt]
16538
16539    Returns a THROW_EXPR representing the throw-expression.  */
16540
16541 static tree
16542 cp_parser_throw_expression (cp_parser* parser)
16543 {
16544   tree expression;
16545   cp_token* token;
16546
16547   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16548   token = cp_lexer_peek_token (parser->lexer);
16549   /* Figure out whether or not there is an assignment-expression
16550      following the "throw" keyword.  */
16551   if (token->type == CPP_COMMA
16552       || token->type == CPP_SEMICOLON
16553       || token->type == CPP_CLOSE_PAREN
16554       || token->type == CPP_CLOSE_SQUARE
16555       || token->type == CPP_CLOSE_BRACE
16556       || token->type == CPP_COLON)
16557     expression = NULL_TREE;
16558   else
16559     expression = cp_parser_assignment_expression (parser,
16560                                                   /*cast_p=*/false, NULL);
16561
16562   return build_throw (expression);
16563 }
16564
16565 /* GNU Extensions */
16566
16567 /* Parse an (optional) asm-specification.
16568
16569    asm-specification:
16570      asm ( string-literal )
16571
16572    If the asm-specification is present, returns a STRING_CST
16573    corresponding to the string-literal.  Otherwise, returns
16574    NULL_TREE.  */
16575
16576 static tree
16577 cp_parser_asm_specification_opt (cp_parser* parser)
16578 {
16579   cp_token *token;
16580   tree asm_specification;
16581
16582   /* Peek at the next token.  */
16583   token = cp_lexer_peek_token (parser->lexer);
16584   /* If the next token isn't the `asm' keyword, then there's no
16585      asm-specification.  */
16586   if (!cp_parser_is_keyword (token, RID_ASM))
16587     return NULL_TREE;
16588
16589   /* Consume the `asm' token.  */
16590   cp_lexer_consume_token (parser->lexer);
16591   /* Look for the `('.  */
16592   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16593
16594   /* Look for the string-literal.  */
16595   asm_specification = cp_parser_string_literal (parser, false, false);
16596
16597   /* Look for the `)'.  */
16598   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16599
16600   return asm_specification;
16601 }
16602
16603 /* Parse an asm-operand-list.
16604
16605    asm-operand-list:
16606      asm-operand
16607      asm-operand-list , asm-operand
16608
16609    asm-operand:
16610      string-literal ( expression )
16611      [ string-literal ] string-literal ( expression )
16612
16613    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16614    each node is the expression.  The TREE_PURPOSE is itself a
16615    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16616    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16617    is a STRING_CST for the string literal before the parenthesis. Returns
16618    ERROR_MARK_NODE if any of the operands are invalid.  */
16619
16620 static tree
16621 cp_parser_asm_operand_list (cp_parser* parser)
16622 {
16623   tree asm_operands = NULL_TREE;
16624   bool invalid_operands = false;
16625
16626   while (true)
16627     {
16628       tree string_literal;
16629       tree expression;
16630       tree name;
16631
16632       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16633         {
16634           /* Consume the `[' token.  */
16635           cp_lexer_consume_token (parser->lexer);
16636           /* Read the operand name.  */
16637           name = cp_parser_identifier (parser);
16638           if (name != error_mark_node)
16639             name = build_string (IDENTIFIER_LENGTH (name),
16640                                  IDENTIFIER_POINTER (name));
16641           /* Look for the closing `]'.  */
16642           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16643         }
16644       else
16645         name = NULL_TREE;
16646       /* Look for the string-literal.  */
16647       string_literal = cp_parser_string_literal (parser, false, false);
16648
16649       /* Look for the `('.  */
16650       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16651       /* Parse the expression.  */
16652       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16653       /* Look for the `)'.  */
16654       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16655
16656       if (name == error_mark_node 
16657           || string_literal == error_mark_node 
16658           || expression == error_mark_node)
16659         invalid_operands = true;
16660
16661       /* Add this operand to the list.  */
16662       asm_operands = tree_cons (build_tree_list (name, string_literal),
16663                                 expression,
16664                                 asm_operands);
16665       /* If the next token is not a `,', there are no more
16666          operands.  */
16667       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16668         break;
16669       /* Consume the `,'.  */
16670       cp_lexer_consume_token (parser->lexer);
16671     }
16672
16673   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16674 }
16675
16676 /* Parse an asm-clobber-list.
16677
16678    asm-clobber-list:
16679      string-literal
16680      asm-clobber-list , string-literal
16681
16682    Returns a TREE_LIST, indicating the clobbers in the order that they
16683    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16684
16685 static tree
16686 cp_parser_asm_clobber_list (cp_parser* parser)
16687 {
16688   tree clobbers = NULL_TREE;
16689
16690   while (true)
16691     {
16692       tree string_literal;
16693
16694       /* Look for the string literal.  */
16695       string_literal = cp_parser_string_literal (parser, false, false);
16696       /* Add it to the list.  */
16697       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16698       /* If the next token is not a `,', then the list is
16699          complete.  */
16700       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16701         break;
16702       /* Consume the `,' token.  */
16703       cp_lexer_consume_token (parser->lexer);
16704     }
16705
16706   return clobbers;
16707 }
16708
16709 /* Parse an (optional) series of attributes.
16710
16711    attributes:
16712      attributes attribute
16713
16714    attribute:
16715      __attribute__ (( attribute-list [opt] ))
16716
16717    The return value is as for cp_parser_attribute_list.  */
16718
16719 static tree
16720 cp_parser_attributes_opt (cp_parser* parser)
16721 {
16722   tree attributes = NULL_TREE;
16723
16724   while (true)
16725     {
16726       cp_token *token;
16727       tree attribute_list;
16728
16729       /* Peek at the next token.  */
16730       token = cp_lexer_peek_token (parser->lexer);
16731       /* If it's not `__attribute__', then we're done.  */
16732       if (token->keyword != RID_ATTRIBUTE)
16733         break;
16734
16735       /* Consume the `__attribute__' keyword.  */
16736       cp_lexer_consume_token (parser->lexer);
16737       /* Look for the two `(' tokens.  */
16738       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16739       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16740
16741       /* Peek at the next token.  */
16742       token = cp_lexer_peek_token (parser->lexer);
16743       if (token->type != CPP_CLOSE_PAREN)
16744         /* Parse the attribute-list.  */
16745         attribute_list = cp_parser_attribute_list (parser);
16746       else
16747         /* If the next token is a `)', then there is no attribute
16748            list.  */
16749         attribute_list = NULL;
16750
16751       /* Look for the two `)' tokens.  */
16752       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16753       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16754
16755       /* Add these new attributes to the list.  */
16756       attributes = chainon (attributes, attribute_list);
16757     }
16758
16759   return attributes;
16760 }
16761
16762 /* Parse an attribute-list.
16763
16764    attribute-list:
16765      attribute
16766      attribute-list , attribute
16767
16768    attribute:
16769      identifier
16770      identifier ( identifier )
16771      identifier ( identifier , expression-list )
16772      identifier ( expression-list )
16773
16774    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16775    to an attribute.  The TREE_PURPOSE of each node is the identifier
16776    indicating which attribute is in use.  The TREE_VALUE represents
16777    the arguments, if any.  */
16778
16779 static tree
16780 cp_parser_attribute_list (cp_parser* parser)
16781 {
16782   tree attribute_list = NULL_TREE;
16783   bool save_translate_strings_p = parser->translate_strings_p;
16784
16785   parser->translate_strings_p = false;
16786   while (true)
16787     {
16788       cp_token *token;
16789       tree identifier;
16790       tree attribute;
16791
16792       /* Look for the identifier.  We also allow keywords here; for
16793          example `__attribute__ ((const))' is legal.  */
16794       token = cp_lexer_peek_token (parser->lexer);
16795       if (token->type == CPP_NAME
16796           || token->type == CPP_KEYWORD)
16797         {
16798           tree arguments = NULL_TREE;
16799
16800           /* Consume the token.  */
16801           token = cp_lexer_consume_token (parser->lexer);
16802
16803           /* Save away the identifier that indicates which attribute
16804              this is.  */
16805           identifier = token->u.value;
16806           attribute = build_tree_list (identifier, NULL_TREE);
16807
16808           /* Peek at the next token.  */
16809           token = cp_lexer_peek_token (parser->lexer);
16810           /* If it's an `(', then parse the attribute arguments.  */
16811           if (token->type == CPP_OPEN_PAREN)
16812             {
16813               arguments = cp_parser_parenthesized_expression_list
16814                           (parser, true, /*cast_p=*/false,
16815                            /*allow_expansion_p=*/false,
16816                            /*non_constant_p=*/NULL);
16817               /* Save the arguments away.  */
16818               TREE_VALUE (attribute) = arguments;
16819             }
16820
16821           if (arguments != error_mark_node)
16822             {
16823               /* Add this attribute to the list.  */
16824               TREE_CHAIN (attribute) = attribute_list;
16825               attribute_list = attribute;
16826             }
16827
16828           token = cp_lexer_peek_token (parser->lexer);
16829         }
16830       /* Now, look for more attributes.  If the next token isn't a
16831          `,', we're done.  */
16832       if (token->type != CPP_COMMA)
16833         break;
16834
16835       /* Consume the comma and keep going.  */
16836       cp_lexer_consume_token (parser->lexer);
16837     }
16838   parser->translate_strings_p = save_translate_strings_p;
16839
16840   /* We built up the list in reverse order.  */
16841   return nreverse (attribute_list);
16842 }
16843
16844 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16845    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16846    current value of the PEDANTIC flag, regardless of whether or not
16847    the `__extension__' keyword is present.  The caller is responsible
16848    for restoring the value of the PEDANTIC flag.  */
16849
16850 static bool
16851 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16852 {
16853   /* Save the old value of the PEDANTIC flag.  */
16854   *saved_pedantic = pedantic;
16855
16856   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16857     {
16858       /* Consume the `__extension__' token.  */
16859       cp_lexer_consume_token (parser->lexer);
16860       /* We're not being pedantic while the `__extension__' keyword is
16861          in effect.  */
16862       pedantic = 0;
16863
16864       return true;
16865     }
16866
16867   return false;
16868 }
16869
16870 /* Parse a label declaration.
16871
16872    label-declaration:
16873      __label__ label-declarator-seq ;
16874
16875    label-declarator-seq:
16876      identifier , label-declarator-seq
16877      identifier  */
16878
16879 static void
16880 cp_parser_label_declaration (cp_parser* parser)
16881 {
16882   /* Look for the `__label__' keyword.  */
16883   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16884
16885   while (true)
16886     {
16887       tree identifier;
16888
16889       /* Look for an identifier.  */
16890       identifier = cp_parser_identifier (parser);
16891       /* If we failed, stop.  */
16892       if (identifier == error_mark_node)
16893         break;
16894       /* Declare it as a label.  */
16895       finish_label_decl (identifier);
16896       /* If the next token is a `;', stop.  */
16897       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16898         break;
16899       /* Look for the `,' separating the label declarations.  */
16900       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16901     }
16902
16903   /* Look for the final `;'.  */
16904   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16905 }
16906
16907 /* Support Functions */
16908
16909 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16910    NAME should have one of the representations used for an
16911    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16912    is returned.  If PARSER->SCOPE is a dependent type, then a
16913    SCOPE_REF is returned.
16914
16915    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16916    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16917    was formed.  Abstractly, such entities should not be passed to this
16918    function, because they do not need to be looked up, but it is
16919    simpler to check for this special case here, rather than at the
16920    call-sites.
16921
16922    In cases not explicitly covered above, this function returns a
16923    DECL, OVERLOAD, or baselink representing the result of the lookup.
16924    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16925    is returned.
16926
16927    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16928    (e.g., "struct") that was used.  In that case bindings that do not
16929    refer to types are ignored.
16930
16931    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16932    ignored.
16933
16934    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16935    are ignored.
16936
16937    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16938    types.
16939
16940    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16941    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16942    NULL_TREE otherwise.  */
16943
16944 static tree
16945 cp_parser_lookup_name (cp_parser *parser, tree name,
16946                        enum tag_types tag_type,
16947                        bool is_template,
16948                        bool is_namespace,
16949                        bool check_dependency,
16950                        tree *ambiguous_decls,
16951                        location_t name_location)
16952 {
16953   int flags = 0;
16954   tree decl;
16955   tree object_type = parser->context->object_type;
16956
16957   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16958     flags |= LOOKUP_COMPLAIN;
16959
16960   /* Assume that the lookup will be unambiguous.  */
16961   if (ambiguous_decls)
16962     *ambiguous_decls = NULL_TREE;
16963
16964   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16965      no longer valid.  Note that if we are parsing tentatively, and
16966      the parse fails, OBJECT_TYPE will be automatically restored.  */
16967   parser->context->object_type = NULL_TREE;
16968
16969   if (name == error_mark_node)
16970     return error_mark_node;
16971
16972   /* A template-id has already been resolved; there is no lookup to
16973      do.  */
16974   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16975     return name;
16976   if (BASELINK_P (name))
16977     {
16978       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16979                   == TEMPLATE_ID_EXPR);
16980       return name;
16981     }
16982
16983   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16984      it should already have been checked to make sure that the name
16985      used matches the type being destroyed.  */
16986   if (TREE_CODE (name) == BIT_NOT_EXPR)
16987     {
16988       tree type;
16989
16990       /* Figure out to which type this destructor applies.  */
16991       if (parser->scope)
16992         type = parser->scope;
16993       else if (object_type)
16994         type = object_type;
16995       else
16996         type = current_class_type;
16997       /* If that's not a class type, there is no destructor.  */
16998       if (!type || !CLASS_TYPE_P (type))
16999         return error_mark_node;
17000       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17001         lazily_declare_fn (sfk_destructor, type);
17002       if (!CLASSTYPE_DESTRUCTORS (type))
17003           return error_mark_node;
17004       /* If it was a class type, return the destructor.  */
17005       return CLASSTYPE_DESTRUCTORS (type);
17006     }
17007
17008   /* By this point, the NAME should be an ordinary identifier.  If
17009      the id-expression was a qualified name, the qualifying scope is
17010      stored in PARSER->SCOPE at this point.  */
17011   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17012
17013   /* Perform the lookup.  */
17014   if (parser->scope)
17015     {
17016       bool dependent_p;
17017
17018       if (parser->scope == error_mark_node)
17019         return error_mark_node;
17020
17021       /* If the SCOPE is dependent, the lookup must be deferred until
17022          the template is instantiated -- unless we are explicitly
17023          looking up names in uninstantiated templates.  Even then, we
17024          cannot look up the name if the scope is not a class type; it
17025          might, for example, be a template type parameter.  */
17026       dependent_p = (TYPE_P (parser->scope)
17027                      && dependent_scope_p (parser->scope));
17028       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17029           && dependent_p)
17030         /* Defer lookup.  */
17031         decl = error_mark_node;
17032       else
17033         {
17034           tree pushed_scope = NULL_TREE;
17035
17036           /* If PARSER->SCOPE is a dependent type, then it must be a
17037              class type, and we must not be checking dependencies;
17038              otherwise, we would have processed this lookup above.  So
17039              that PARSER->SCOPE is not considered a dependent base by
17040              lookup_member, we must enter the scope here.  */
17041           if (dependent_p)
17042             pushed_scope = push_scope (parser->scope);
17043           /* If the PARSER->SCOPE is a template specialization, it
17044              may be instantiated during name lookup.  In that case,
17045              errors may be issued.  Even if we rollback the current
17046              tentative parse, those errors are valid.  */
17047           decl = lookup_qualified_name (parser->scope, name,
17048                                         tag_type != none_type,
17049                                         /*complain=*/true);
17050
17051           /* If we have a single function from a using decl, pull it out.  */
17052           if (TREE_CODE (decl) == OVERLOAD
17053               && !really_overloaded_fn (decl))
17054             decl = OVL_FUNCTION (decl);
17055
17056           if (pushed_scope)
17057             pop_scope (pushed_scope);
17058         }
17059
17060       /* If the scope is a dependent type and either we deferred lookup or
17061          we did lookup but didn't find the name, rememeber the name.  */
17062       if (decl == error_mark_node && TYPE_P (parser->scope)
17063           && dependent_type_p (parser->scope))
17064         {
17065           if (tag_type)
17066             {
17067               tree type;
17068
17069               /* The resolution to Core Issue 180 says that `struct
17070                  A::B' should be considered a type-name, even if `A'
17071                  is dependent.  */
17072               type = make_typename_type (parser->scope, name, tag_type,
17073                                          /*complain=*/tf_error);
17074               decl = TYPE_NAME (type);
17075             }
17076           else if (is_template
17077                    && (cp_parser_next_token_ends_template_argument_p (parser)
17078                        || cp_lexer_next_token_is (parser->lexer,
17079                                                   CPP_CLOSE_PAREN)))
17080             decl = make_unbound_class_template (parser->scope,
17081                                                 name, NULL_TREE,
17082                                                 /*complain=*/tf_error);
17083           else
17084             decl = build_qualified_name (/*type=*/NULL_TREE,
17085                                          parser->scope, name,
17086                                          is_template);
17087         }
17088       parser->qualifying_scope = parser->scope;
17089       parser->object_scope = NULL_TREE;
17090     }
17091   else if (object_type)
17092     {
17093       tree object_decl = NULL_TREE;
17094       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17095          OBJECT_TYPE is not a class.  */
17096       if (CLASS_TYPE_P (object_type))
17097         /* If the OBJECT_TYPE is a template specialization, it may
17098            be instantiated during name lookup.  In that case, errors
17099            may be issued.  Even if we rollback the current tentative
17100            parse, those errors are valid.  */
17101         object_decl = lookup_member (object_type,
17102                                      name,
17103                                      /*protect=*/0,
17104                                      tag_type != none_type);
17105       /* Look it up in the enclosing context, too.  */
17106       decl = lookup_name_real (name, tag_type != none_type,
17107                                /*nonclass=*/0,
17108                                /*block_p=*/true, is_namespace, flags);
17109       parser->object_scope = object_type;
17110       parser->qualifying_scope = NULL_TREE;
17111       if (object_decl)
17112         decl = object_decl;
17113     }
17114   else
17115     {
17116       decl = lookup_name_real (name, tag_type != none_type,
17117                                /*nonclass=*/0,
17118                                /*block_p=*/true, is_namespace, flags);
17119       parser->qualifying_scope = NULL_TREE;
17120       parser->object_scope = NULL_TREE;
17121     }
17122
17123   /* If the lookup failed, let our caller know.  */
17124   if (!decl || decl == error_mark_node)
17125     return error_mark_node;
17126
17127   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17128   if (TREE_CODE (decl) == TREE_LIST)
17129     {
17130       if (ambiguous_decls)
17131         *ambiguous_decls = decl;
17132       /* The error message we have to print is too complicated for
17133          cp_parser_error, so we incorporate its actions directly.  */
17134       if (!cp_parser_simulate_error (parser))
17135         {
17136           error ("%Hreference to %qD is ambiguous",
17137                  &name_location, name);
17138           print_candidates (decl);
17139         }
17140       return error_mark_node;
17141     }
17142
17143   gcc_assert (DECL_P (decl)
17144               || TREE_CODE (decl) == OVERLOAD
17145               || TREE_CODE (decl) == SCOPE_REF
17146               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17147               || BASELINK_P (decl));
17148
17149   /* If we have resolved the name of a member declaration, check to
17150      see if the declaration is accessible.  When the name resolves to
17151      set of overloaded functions, accessibility is checked when
17152      overload resolution is done.
17153
17154      During an explicit instantiation, access is not checked at all,
17155      as per [temp.explicit].  */
17156   if (DECL_P (decl))
17157     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17158
17159   return decl;
17160 }
17161
17162 /* Like cp_parser_lookup_name, but for use in the typical case where
17163    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17164    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17165
17166 static tree
17167 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17168 {
17169   return cp_parser_lookup_name (parser, name,
17170                                 none_type,
17171                                 /*is_template=*/false,
17172                                 /*is_namespace=*/false,
17173                                 /*check_dependency=*/true,
17174                                 /*ambiguous_decls=*/NULL,
17175                                 location);
17176 }
17177
17178 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17179    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17180    true, the DECL indicates the class being defined in a class-head,
17181    or declared in an elaborated-type-specifier.
17182
17183    Otherwise, return DECL.  */
17184
17185 static tree
17186 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17187 {
17188   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17189      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17190
17191        struct A {
17192          template <typename T> struct B;
17193        };
17194
17195        template <typename T> struct A::B {};
17196
17197      Similarly, in an elaborated-type-specifier:
17198
17199        namespace N { struct X{}; }
17200
17201        struct A {
17202          template <typename T> friend struct N::X;
17203        };
17204
17205      However, if the DECL refers to a class type, and we are in
17206      the scope of the class, then the name lookup automatically
17207      finds the TYPE_DECL created by build_self_reference rather
17208      than a TEMPLATE_DECL.  For example, in:
17209
17210        template <class T> struct S {
17211          S s;
17212        };
17213
17214      there is no need to handle such case.  */
17215
17216   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17217     return DECL_TEMPLATE_RESULT (decl);
17218
17219   return decl;
17220 }
17221
17222 /* If too many, or too few, template-parameter lists apply to the
17223    declarator, issue an error message.  Returns TRUE if all went well,
17224    and FALSE otherwise.  */
17225
17226 static bool
17227 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17228                                                 cp_declarator *declarator,
17229                                                 location_t declarator_location)
17230 {
17231   unsigned num_templates;
17232
17233   /* We haven't seen any classes that involve template parameters yet.  */
17234   num_templates = 0;
17235
17236   switch (declarator->kind)
17237     {
17238     case cdk_id:
17239       if (declarator->u.id.qualifying_scope)
17240         {
17241           tree scope;
17242           tree member;
17243
17244           scope = declarator->u.id.qualifying_scope;
17245           member = declarator->u.id.unqualified_name;
17246
17247           while (scope && CLASS_TYPE_P (scope))
17248             {
17249               /* You're supposed to have one `template <...>'
17250                  for every template class, but you don't need one
17251                  for a full specialization.  For example:
17252
17253                  template <class T> struct S{};
17254                  template <> struct S<int> { void f(); };
17255                  void S<int>::f () {}
17256
17257                  is correct; there shouldn't be a `template <>' for
17258                  the definition of `S<int>::f'.  */
17259               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17260                 /* If SCOPE does not have template information of any
17261                    kind, then it is not a template, nor is it nested
17262                    within a template.  */
17263                 break;
17264               if (explicit_class_specialization_p (scope))
17265                 break;
17266               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17267                 ++num_templates;
17268
17269               scope = TYPE_CONTEXT (scope);
17270             }
17271         }
17272       else if (TREE_CODE (declarator->u.id.unqualified_name)
17273                == TEMPLATE_ID_EXPR)
17274         /* If the DECLARATOR has the form `X<y>' then it uses one
17275            additional level of template parameters.  */
17276         ++num_templates;
17277
17278       return cp_parser_check_template_parameters (parser,
17279                                                   num_templates,
17280                                                   declarator_location);
17281
17282     case cdk_function:
17283     case cdk_array:
17284     case cdk_pointer:
17285     case cdk_reference:
17286     case cdk_ptrmem:
17287       return (cp_parser_check_declarator_template_parameters
17288               (parser, declarator->declarator, declarator_location));
17289
17290     case cdk_error:
17291       return true;
17292
17293     default:
17294       gcc_unreachable ();
17295     }
17296   return false;
17297 }
17298
17299 /* NUM_TEMPLATES were used in the current declaration.  If that is
17300    invalid, return FALSE and issue an error messages.  Otherwise,
17301    return TRUE.  */
17302
17303 static bool
17304 cp_parser_check_template_parameters (cp_parser* parser,
17305                                      unsigned num_templates,
17306                                      location_t location)
17307 {
17308   /* If there are more template classes than parameter lists, we have
17309      something like:
17310
17311        template <class T> void S<T>::R<T>::f ();  */
17312   if (parser->num_template_parameter_lists < num_templates)
17313     {
17314       error ("%Htoo few template-parameter-lists", &location);
17315       return false;
17316     }
17317   /* If there are the same number of template classes and parameter
17318      lists, that's OK.  */
17319   if (parser->num_template_parameter_lists == num_templates)
17320     return true;
17321   /* If there are more, but only one more, then we are referring to a
17322      member template.  That's OK too.  */
17323   if (parser->num_template_parameter_lists == num_templates + 1)
17324       return true;
17325   /* Otherwise, there are too many template parameter lists.  We have
17326      something like:
17327
17328      template <class T> template <class U> void S::f();  */
17329   error ("%Htoo many template-parameter-lists", &location);
17330   return false;
17331 }
17332
17333 /* Parse an optional `::' token indicating that the following name is
17334    from the global namespace.  If so, PARSER->SCOPE is set to the
17335    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17336    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17337    Returns the new value of PARSER->SCOPE, if the `::' token is
17338    present, and NULL_TREE otherwise.  */
17339
17340 static tree
17341 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17342 {
17343   cp_token *token;
17344
17345   /* Peek at the next token.  */
17346   token = cp_lexer_peek_token (parser->lexer);
17347   /* If we're looking at a `::' token then we're starting from the
17348      global namespace, not our current location.  */
17349   if (token->type == CPP_SCOPE)
17350     {
17351       /* Consume the `::' token.  */
17352       cp_lexer_consume_token (parser->lexer);
17353       /* Set the SCOPE so that we know where to start the lookup.  */
17354       parser->scope = global_namespace;
17355       parser->qualifying_scope = global_namespace;
17356       parser->object_scope = NULL_TREE;
17357
17358       return parser->scope;
17359     }
17360   else if (!current_scope_valid_p)
17361     {
17362       parser->scope = NULL_TREE;
17363       parser->qualifying_scope = NULL_TREE;
17364       parser->object_scope = NULL_TREE;
17365     }
17366
17367   return NULL_TREE;
17368 }
17369
17370 /* Returns TRUE if the upcoming token sequence is the start of a
17371    constructor declarator.  If FRIEND_P is true, the declarator is
17372    preceded by the `friend' specifier.  */
17373
17374 static bool
17375 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17376 {
17377   bool constructor_p;
17378   tree type_decl = NULL_TREE;
17379   bool nested_name_p;
17380   cp_token *next_token;
17381
17382   /* The common case is that this is not a constructor declarator, so
17383      try to avoid doing lots of work if at all possible.  It's not
17384      valid declare a constructor at function scope.  */
17385   if (parser->in_function_body)
17386     return false;
17387   /* And only certain tokens can begin a constructor declarator.  */
17388   next_token = cp_lexer_peek_token (parser->lexer);
17389   if (next_token->type != CPP_NAME
17390       && next_token->type != CPP_SCOPE
17391       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17392       && next_token->type != CPP_TEMPLATE_ID)
17393     return false;
17394
17395   /* Parse tentatively; we are going to roll back all of the tokens
17396      consumed here.  */
17397   cp_parser_parse_tentatively (parser);
17398   /* Assume that we are looking at a constructor declarator.  */
17399   constructor_p = true;
17400
17401   /* Look for the optional `::' operator.  */
17402   cp_parser_global_scope_opt (parser,
17403                               /*current_scope_valid_p=*/false);
17404   /* Look for the nested-name-specifier.  */
17405   nested_name_p
17406     = (cp_parser_nested_name_specifier_opt (parser,
17407                                             /*typename_keyword_p=*/false,
17408                                             /*check_dependency_p=*/false,
17409                                             /*type_p=*/false,
17410                                             /*is_declaration=*/false)
17411        != NULL_TREE);
17412   /* Outside of a class-specifier, there must be a
17413      nested-name-specifier.  */
17414   if (!nested_name_p &&
17415       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17416        || friend_p))
17417     constructor_p = false;
17418   /* If we still think that this might be a constructor-declarator,
17419      look for a class-name.  */
17420   if (constructor_p)
17421     {
17422       /* If we have:
17423
17424            template <typename T> struct S { S(); };
17425            template <typename T> S<T>::S ();
17426
17427          we must recognize that the nested `S' names a class.
17428          Similarly, for:
17429
17430            template <typename T> S<T>::S<T> ();
17431
17432          we must recognize that the nested `S' names a template.  */
17433       type_decl = cp_parser_class_name (parser,
17434                                         /*typename_keyword_p=*/false,
17435                                         /*template_keyword_p=*/false,
17436                                         none_type,
17437                                         /*check_dependency_p=*/false,
17438                                         /*class_head_p=*/false,
17439                                         /*is_declaration=*/false);
17440       /* If there was no class-name, then this is not a constructor.  */
17441       constructor_p = !cp_parser_error_occurred (parser);
17442     }
17443
17444   /* If we're still considering a constructor, we have to see a `(',
17445      to begin the parameter-declaration-clause, followed by either a
17446      `)', an `...', or a decl-specifier.  We need to check for a
17447      type-specifier to avoid being fooled into thinking that:
17448
17449        S::S (f) (int);
17450
17451      is a constructor.  (It is actually a function named `f' that
17452      takes one parameter (of type `int') and returns a value of type
17453      `S::S'.  */
17454   if (constructor_p
17455       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17456     {
17457       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17458           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17459           /* A parameter declaration begins with a decl-specifier,
17460              which is either the "attribute" keyword, a storage class
17461              specifier, or (usually) a type-specifier.  */
17462           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17463         {
17464           tree type;
17465           tree pushed_scope = NULL_TREE;
17466           unsigned saved_num_template_parameter_lists;
17467
17468           /* Names appearing in the type-specifier should be looked up
17469              in the scope of the class.  */
17470           if (current_class_type)
17471             type = NULL_TREE;
17472           else
17473             {
17474               type = TREE_TYPE (type_decl);
17475               if (TREE_CODE (type) == TYPENAME_TYPE)
17476                 {
17477                   type = resolve_typename_type (type,
17478                                                 /*only_current_p=*/false);
17479                   if (TREE_CODE (type) == TYPENAME_TYPE)
17480                     {
17481                       cp_parser_abort_tentative_parse (parser);
17482                       return false;
17483                     }
17484                 }
17485               pushed_scope = push_scope (type);
17486             }
17487
17488           /* Inside the constructor parameter list, surrounding
17489              template-parameter-lists do not apply.  */
17490           saved_num_template_parameter_lists
17491             = parser->num_template_parameter_lists;
17492           parser->num_template_parameter_lists = 0;
17493
17494           /* Look for the type-specifier.  */
17495           cp_parser_type_specifier (parser,
17496                                     CP_PARSER_FLAGS_NONE,
17497                                     /*decl_specs=*/NULL,
17498                                     /*is_declarator=*/true,
17499                                     /*declares_class_or_enum=*/NULL,
17500                                     /*is_cv_qualifier=*/NULL);
17501
17502           parser->num_template_parameter_lists
17503             = saved_num_template_parameter_lists;
17504
17505           /* Leave the scope of the class.  */
17506           if (pushed_scope)
17507             pop_scope (pushed_scope);
17508
17509           constructor_p = !cp_parser_error_occurred (parser);
17510         }
17511     }
17512   else
17513     constructor_p = false;
17514   /* We did not really want to consume any tokens.  */
17515   cp_parser_abort_tentative_parse (parser);
17516
17517   return constructor_p;
17518 }
17519
17520 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17521    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17522    they must be performed once we are in the scope of the function.
17523
17524    Returns the function defined.  */
17525
17526 static tree
17527 cp_parser_function_definition_from_specifiers_and_declarator
17528   (cp_parser* parser,
17529    cp_decl_specifier_seq *decl_specifiers,
17530    tree attributes,
17531    const cp_declarator *declarator)
17532 {
17533   tree fn;
17534   bool success_p;
17535
17536   /* Begin the function-definition.  */
17537   success_p = start_function (decl_specifiers, declarator, attributes);
17538
17539   /* The things we're about to see are not directly qualified by any
17540      template headers we've seen thus far.  */
17541   reset_specialization ();
17542
17543   /* If there were names looked up in the decl-specifier-seq that we
17544      did not check, check them now.  We must wait until we are in the
17545      scope of the function to perform the checks, since the function
17546      might be a friend.  */
17547   perform_deferred_access_checks ();
17548
17549   if (!success_p)
17550     {
17551       /* Skip the entire function.  */
17552       cp_parser_skip_to_end_of_block_or_statement (parser);
17553       fn = error_mark_node;
17554     }
17555   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17556     {
17557       /* Seen already, skip it.  An error message has already been output.  */
17558       cp_parser_skip_to_end_of_block_or_statement (parser);
17559       fn = current_function_decl;
17560       current_function_decl = NULL_TREE;
17561       /* If this is a function from a class, pop the nested class.  */
17562       if (current_class_name)
17563         pop_nested_class ();
17564     }
17565   else
17566     fn = cp_parser_function_definition_after_declarator (parser,
17567                                                          /*inline_p=*/false);
17568
17569   return fn;
17570 }
17571
17572 /* Parse the part of a function-definition that follows the
17573    declarator.  INLINE_P is TRUE iff this function is an inline
17574    function defined with a class-specifier.
17575
17576    Returns the function defined.  */
17577
17578 static tree
17579 cp_parser_function_definition_after_declarator (cp_parser* parser,
17580                                                 bool inline_p)
17581 {
17582   tree fn;
17583   bool ctor_initializer_p = false;
17584   bool saved_in_unbraced_linkage_specification_p;
17585   bool saved_in_function_body;
17586   unsigned saved_num_template_parameter_lists;
17587   cp_token *token;
17588
17589   saved_in_function_body = parser->in_function_body;
17590   parser->in_function_body = true;
17591   /* If the next token is `return', then the code may be trying to
17592      make use of the "named return value" extension that G++ used to
17593      support.  */
17594   token = cp_lexer_peek_token (parser->lexer);
17595   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17596     {
17597       /* Consume the `return' keyword.  */
17598       cp_lexer_consume_token (parser->lexer);
17599       /* Look for the identifier that indicates what value is to be
17600          returned.  */
17601       cp_parser_identifier (parser);
17602       /* Issue an error message.  */
17603       error ("%Hnamed return values are no longer supported",
17604              &token->location);
17605       /* Skip tokens until we reach the start of the function body.  */
17606       while (true)
17607         {
17608           cp_token *token = cp_lexer_peek_token (parser->lexer);
17609           if (token->type == CPP_OPEN_BRACE
17610               || token->type == CPP_EOF
17611               || token->type == CPP_PRAGMA_EOL)
17612             break;
17613           cp_lexer_consume_token (parser->lexer);
17614         }
17615     }
17616   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17617      anything declared inside `f'.  */
17618   saved_in_unbraced_linkage_specification_p
17619     = parser->in_unbraced_linkage_specification_p;
17620   parser->in_unbraced_linkage_specification_p = false;
17621   /* Inside the function, surrounding template-parameter-lists do not
17622      apply.  */
17623   saved_num_template_parameter_lists
17624     = parser->num_template_parameter_lists;
17625   parser->num_template_parameter_lists = 0;
17626   /* If the next token is `try', then we are looking at a
17627      function-try-block.  */
17628   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17629     ctor_initializer_p = cp_parser_function_try_block (parser);
17630   /* A function-try-block includes the function-body, so we only do
17631      this next part if we're not processing a function-try-block.  */
17632   else
17633     ctor_initializer_p
17634       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17635
17636   /* Finish the function.  */
17637   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17638                         (inline_p ? 2 : 0));
17639   /* Generate code for it, if necessary.  */
17640   expand_or_defer_fn (fn);
17641   /* Restore the saved values.  */
17642   parser->in_unbraced_linkage_specification_p
17643     = saved_in_unbraced_linkage_specification_p;
17644   parser->num_template_parameter_lists
17645     = saved_num_template_parameter_lists;
17646   parser->in_function_body = saved_in_function_body;
17647
17648   return fn;
17649 }
17650
17651 /* Parse a template-declaration, assuming that the `export' (and
17652    `extern') keywords, if present, has already been scanned.  MEMBER_P
17653    is as for cp_parser_template_declaration.  */
17654
17655 static void
17656 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17657 {
17658   tree decl = NULL_TREE;
17659   VEC (deferred_access_check,gc) *checks;
17660   tree parameter_list;
17661   bool friend_p = false;
17662   bool need_lang_pop;
17663   cp_token *token;
17664
17665   /* Look for the `template' keyword.  */
17666   token = cp_lexer_peek_token (parser->lexer);
17667   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17668     return;
17669
17670   /* And the `<'.  */
17671   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17672     return;
17673   if (at_class_scope_p () && current_function_decl)
17674     {
17675       /* 14.5.2.2 [temp.mem]
17676
17677          A local class shall not have member templates.  */
17678       error ("%Hinvalid declaration of member template in local class",
17679              &token->location);
17680       cp_parser_skip_to_end_of_block_or_statement (parser);
17681       return;
17682     }
17683   /* [temp]
17684
17685      A template ... shall not have C linkage.  */
17686   if (current_lang_name == lang_name_c)
17687     {
17688       error ("%Htemplate with C linkage", &token->location);
17689       /* Give it C++ linkage to avoid confusing other parts of the
17690          front end.  */
17691       push_lang_context (lang_name_cplusplus);
17692       need_lang_pop = true;
17693     }
17694   else
17695     need_lang_pop = false;
17696
17697   /* We cannot perform access checks on the template parameter
17698      declarations until we know what is being declared, just as we
17699      cannot check the decl-specifier list.  */
17700   push_deferring_access_checks (dk_deferred);
17701
17702   /* If the next token is `>', then we have an invalid
17703      specialization.  Rather than complain about an invalid template
17704      parameter, issue an error message here.  */
17705   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17706     {
17707       cp_parser_error (parser, "invalid explicit specialization");
17708       begin_specialization ();
17709       parameter_list = NULL_TREE;
17710     }
17711   else
17712     /* Parse the template parameters.  */
17713     parameter_list = cp_parser_template_parameter_list (parser);
17714
17715   /* Get the deferred access checks from the parameter list.  These
17716      will be checked once we know what is being declared, as for a
17717      member template the checks must be performed in the scope of the
17718      class containing the member.  */
17719   checks = get_deferred_access_checks ();
17720
17721   /* Look for the `>'.  */
17722   cp_parser_skip_to_end_of_template_parameter_list (parser);
17723   /* We just processed one more parameter list.  */
17724   ++parser->num_template_parameter_lists;
17725   /* If the next token is `template', there are more template
17726      parameters.  */
17727   if (cp_lexer_next_token_is_keyword (parser->lexer,
17728                                       RID_TEMPLATE))
17729     cp_parser_template_declaration_after_export (parser, member_p);
17730   else
17731     {
17732       /* There are no access checks when parsing a template, as we do not
17733          know if a specialization will be a friend.  */
17734       push_deferring_access_checks (dk_no_check);
17735       token = cp_lexer_peek_token (parser->lexer);
17736       decl = cp_parser_single_declaration (parser,
17737                                            checks,
17738                                            member_p,
17739                                            /*explicit_specialization_p=*/false,
17740                                            &friend_p);
17741       pop_deferring_access_checks ();
17742
17743       /* If this is a member template declaration, let the front
17744          end know.  */
17745       if (member_p && !friend_p && decl)
17746         {
17747           if (TREE_CODE (decl) == TYPE_DECL)
17748             cp_parser_check_access_in_redeclaration (decl, token->location);
17749
17750           decl = finish_member_template_decl (decl);
17751         }
17752       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17753         make_friend_class (current_class_type, TREE_TYPE (decl),
17754                            /*complain=*/true);
17755     }
17756   /* We are done with the current parameter list.  */
17757   --parser->num_template_parameter_lists;
17758
17759   pop_deferring_access_checks ();
17760
17761   /* Finish up.  */
17762   finish_template_decl (parameter_list);
17763
17764   /* Register member declarations.  */
17765   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17766     finish_member_declaration (decl);
17767   /* For the erroneous case of a template with C linkage, we pushed an
17768      implicit C++ linkage scope; exit that scope now.  */
17769   if (need_lang_pop)
17770     pop_lang_context ();
17771   /* If DECL is a function template, we must return to parse it later.
17772      (Even though there is no definition, there might be default
17773      arguments that need handling.)  */
17774   if (member_p && decl
17775       && (TREE_CODE (decl) == FUNCTION_DECL
17776           || DECL_FUNCTION_TEMPLATE_P (decl)))
17777     TREE_VALUE (parser->unparsed_functions_queues)
17778       = tree_cons (NULL_TREE, decl,
17779                    TREE_VALUE (parser->unparsed_functions_queues));
17780 }
17781
17782 /* Perform the deferred access checks from a template-parameter-list.
17783    CHECKS is a TREE_LIST of access checks, as returned by
17784    get_deferred_access_checks.  */
17785
17786 static void
17787 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17788 {
17789   ++processing_template_parmlist;
17790   perform_access_checks (checks);
17791   --processing_template_parmlist;
17792 }
17793
17794 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17795    `function-definition' sequence.  MEMBER_P is true, this declaration
17796    appears in a class scope.
17797
17798    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17799    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17800
17801 static tree
17802 cp_parser_single_declaration (cp_parser* parser,
17803                               VEC (deferred_access_check,gc)* checks,
17804                               bool member_p,
17805                               bool explicit_specialization_p,
17806                               bool* friend_p)
17807 {
17808   int declares_class_or_enum;
17809   tree decl = NULL_TREE;
17810   cp_decl_specifier_seq decl_specifiers;
17811   bool function_definition_p = false;
17812   cp_token *decl_spec_token_start;
17813
17814   /* This function is only used when processing a template
17815      declaration.  */
17816   gcc_assert (innermost_scope_kind () == sk_template_parms
17817               || innermost_scope_kind () == sk_template_spec);
17818
17819   /* Defer access checks until we know what is being declared.  */
17820   push_deferring_access_checks (dk_deferred);
17821
17822   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17823      alternative.  */
17824   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17825   cp_parser_decl_specifier_seq (parser,
17826                                 CP_PARSER_FLAGS_OPTIONAL,
17827                                 &decl_specifiers,
17828                                 &declares_class_or_enum);
17829   if (friend_p)
17830     *friend_p = cp_parser_friend_p (&decl_specifiers);
17831
17832   /* There are no template typedefs.  */
17833   if (decl_specifiers.specs[(int) ds_typedef])
17834     {
17835       error ("%Htemplate declaration of %qs",
17836              &decl_spec_token_start->location, "typedef");
17837       decl = error_mark_node;
17838     }
17839
17840   /* Gather up the access checks that occurred the
17841      decl-specifier-seq.  */
17842   stop_deferring_access_checks ();
17843
17844   /* Check for the declaration of a template class.  */
17845   if (declares_class_or_enum)
17846     {
17847       if (cp_parser_declares_only_class_p (parser))
17848         {
17849           decl = shadow_tag (&decl_specifiers);
17850
17851           /* In this case:
17852
17853                struct C {
17854                  friend template <typename T> struct A<T>::B;
17855                };
17856
17857              A<T>::B will be represented by a TYPENAME_TYPE, and
17858              therefore not recognized by shadow_tag.  */
17859           if (friend_p && *friend_p
17860               && !decl
17861               && decl_specifiers.type
17862               && TYPE_P (decl_specifiers.type))
17863             decl = decl_specifiers.type;
17864
17865           if (decl && decl != error_mark_node)
17866             decl = TYPE_NAME (decl);
17867           else
17868             decl = error_mark_node;
17869
17870           /* Perform access checks for template parameters.  */
17871           cp_parser_perform_template_parameter_access_checks (checks);
17872         }
17873     }
17874   /* If it's not a template class, try for a template function.  If
17875      the next token is a `;', then this declaration does not declare
17876      anything.  But, if there were errors in the decl-specifiers, then
17877      the error might well have come from an attempted class-specifier.
17878      In that case, there's no need to warn about a missing declarator.  */
17879   if (!decl
17880       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17881           || decl_specifiers.type != error_mark_node))
17882     {
17883       decl = cp_parser_init_declarator (parser,
17884                                         &decl_specifiers,
17885                                         checks,
17886                                         /*function_definition_allowed_p=*/true,
17887                                         member_p,
17888                                         declares_class_or_enum,
17889                                         &function_definition_p);
17890
17891     /* 7.1.1-1 [dcl.stc]
17892
17893        A storage-class-specifier shall not be specified in an explicit
17894        specialization...  */
17895     if (decl
17896         && explicit_specialization_p
17897         && decl_specifiers.storage_class != sc_none)
17898       {
17899         error ("%Hexplicit template specialization cannot have a storage class",
17900                &decl_spec_token_start->location);
17901         decl = error_mark_node;
17902       }
17903     }
17904
17905   pop_deferring_access_checks ();
17906
17907   /* Clear any current qualification; whatever comes next is the start
17908      of something new.  */
17909   parser->scope = NULL_TREE;
17910   parser->qualifying_scope = NULL_TREE;
17911   parser->object_scope = NULL_TREE;
17912   /* Look for a trailing `;' after the declaration.  */
17913   if (!function_definition_p
17914       && (decl == error_mark_node
17915           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17916     cp_parser_skip_to_end_of_block_or_statement (parser);
17917
17918   return decl;
17919 }
17920
17921 /* Parse a cast-expression that is not the operand of a unary "&".  */
17922
17923 static tree
17924 cp_parser_simple_cast_expression (cp_parser *parser)
17925 {
17926   return cp_parser_cast_expression (parser, /*address_p=*/false,
17927                                     /*cast_p=*/false, NULL);
17928 }
17929
17930 /* Parse a functional cast to TYPE.  Returns an expression
17931    representing the cast.  */
17932
17933 static tree
17934 cp_parser_functional_cast (cp_parser* parser, tree type)
17935 {
17936   tree expression_list;
17937   tree cast;
17938   bool nonconst_p;
17939
17940   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17941     {
17942       maybe_warn_cpp0x ("extended initializer lists");
17943       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17944       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17945       if (TREE_CODE (type) == TYPE_DECL)
17946         type = TREE_TYPE (type);
17947       return finish_compound_literal (type, expression_list);
17948     }
17949
17950   expression_list
17951     = cp_parser_parenthesized_expression_list (parser, false,
17952                                                /*cast_p=*/true,
17953                                                /*allow_expansion_p=*/true,
17954                                                /*non_constant_p=*/NULL);
17955
17956   cast = build_functional_cast (type, expression_list,
17957                                 tf_warning_or_error);
17958   /* [expr.const]/1: In an integral constant expression "only type
17959      conversions to integral or enumeration type can be used".  */
17960   if (TREE_CODE (type) == TYPE_DECL)
17961     type = TREE_TYPE (type);
17962   if (cast != error_mark_node
17963       && !cast_valid_in_integral_constant_expression_p (type)
17964       && (cp_parser_non_integral_constant_expression
17965           (parser, "a call to a constructor")))
17966     return error_mark_node;
17967   return cast;
17968 }
17969
17970 /* Save the tokens that make up the body of a member function defined
17971    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17972    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17973    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17974    for the member function.  */
17975
17976 static tree
17977 cp_parser_save_member_function_body (cp_parser* parser,
17978                                      cp_decl_specifier_seq *decl_specifiers,
17979                                      cp_declarator *declarator,
17980                                      tree attributes)
17981 {
17982   cp_token *first;
17983   cp_token *last;
17984   tree fn;
17985
17986   /* Create the function-declaration.  */
17987   fn = start_method (decl_specifiers, declarator, attributes);
17988   /* If something went badly wrong, bail out now.  */
17989   if (fn == error_mark_node)
17990     {
17991       /* If there's a function-body, skip it.  */
17992       if (cp_parser_token_starts_function_definition_p
17993           (cp_lexer_peek_token (parser->lexer)))
17994         cp_parser_skip_to_end_of_block_or_statement (parser);
17995       return error_mark_node;
17996     }
17997
17998   /* Remember it, if there default args to post process.  */
17999   cp_parser_save_default_args (parser, fn);
18000
18001   /* Save away the tokens that make up the body of the
18002      function.  */
18003   first = parser->lexer->next_token;
18004   /* We can have braced-init-list mem-initializers before the fn body.  */
18005   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18006     {
18007       cp_lexer_consume_token (parser->lexer);
18008       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18009              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18010         {
18011           /* cache_group will stop after an un-nested { } pair, too.  */
18012           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18013             break;
18014
18015           /* variadic mem-inits have ... after the ')'.  */
18016           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18017             cp_lexer_consume_token (parser->lexer);
18018         }
18019     }
18020   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18021   /* Handle function try blocks.  */
18022   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18023     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18024   last = parser->lexer->next_token;
18025
18026   /* Save away the inline definition; we will process it when the
18027      class is complete.  */
18028   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18029   DECL_PENDING_INLINE_P (fn) = 1;
18030
18031   /* We need to know that this was defined in the class, so that
18032      friend templates are handled correctly.  */
18033   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18034
18035   /* We're done with the inline definition.  */
18036   finish_method (fn);
18037
18038   /* Add FN to the queue of functions to be parsed later.  */
18039   TREE_VALUE (parser->unparsed_functions_queues)
18040     = tree_cons (NULL_TREE, fn,
18041                  TREE_VALUE (parser->unparsed_functions_queues));
18042
18043   return fn;
18044 }
18045
18046 /* Parse a template-argument-list, as well as the trailing ">" (but
18047    not the opening ">").  See cp_parser_template_argument_list for the
18048    return value.  */
18049
18050 static tree
18051 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18052 {
18053   tree arguments;
18054   tree saved_scope;
18055   tree saved_qualifying_scope;
18056   tree saved_object_scope;
18057   bool saved_greater_than_is_operator_p;
18058   bool saved_skip_evaluation;
18059
18060   /* [temp.names]
18061
18062      When parsing a template-id, the first non-nested `>' is taken as
18063      the end of the template-argument-list rather than a greater-than
18064      operator.  */
18065   saved_greater_than_is_operator_p
18066     = parser->greater_than_is_operator_p;
18067   parser->greater_than_is_operator_p = false;
18068   /* Parsing the argument list may modify SCOPE, so we save it
18069      here.  */
18070   saved_scope = parser->scope;
18071   saved_qualifying_scope = parser->qualifying_scope;
18072   saved_object_scope = parser->object_scope;
18073   /* We need to evaluate the template arguments, even though this
18074      template-id may be nested within a "sizeof".  */
18075   saved_skip_evaluation = skip_evaluation;
18076   skip_evaluation = false;
18077   /* Parse the template-argument-list itself.  */
18078   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18079       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18080     arguments = NULL_TREE;
18081   else
18082     arguments = cp_parser_template_argument_list (parser);
18083   /* Look for the `>' that ends the template-argument-list. If we find
18084      a '>>' instead, it's probably just a typo.  */
18085   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18086     {
18087       if (cxx_dialect != cxx98)
18088         {
18089           /* In C++0x, a `>>' in a template argument list or cast
18090              expression is considered to be two separate `>'
18091              tokens. So, change the current token to a `>', but don't
18092              consume it: it will be consumed later when the outer
18093              template argument list (or cast expression) is parsed.
18094              Note that this replacement of `>' for `>>' is necessary
18095              even if we are parsing tentatively: in the tentative
18096              case, after calling
18097              cp_parser_enclosed_template_argument_list we will always
18098              throw away all of the template arguments and the first
18099              closing `>', either because the template argument list
18100              was erroneous or because we are replacing those tokens
18101              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18102              not have been thrown away) is needed either to close an
18103              outer template argument list or to complete a new-style
18104              cast.  */
18105           cp_token *token = cp_lexer_peek_token (parser->lexer);
18106           token->type = CPP_GREATER;
18107         }
18108       else if (!saved_greater_than_is_operator_p)
18109         {
18110           /* If we're in a nested template argument list, the '>>' has
18111             to be a typo for '> >'. We emit the error message, but we
18112             continue parsing and we push a '>' as next token, so that
18113             the argument list will be parsed correctly.  Note that the
18114             global source location is still on the token before the
18115             '>>', so we need to say explicitly where we want it.  */
18116           cp_token *token = cp_lexer_peek_token (parser->lexer);
18117           error ("%H%<>>%> should be %<> >%> "
18118                  "within a nested template argument list",
18119                  &token->location);
18120
18121           token->type = CPP_GREATER;
18122         }
18123       else
18124         {
18125           /* If this is not a nested template argument list, the '>>'
18126             is a typo for '>'. Emit an error message and continue.
18127             Same deal about the token location, but here we can get it
18128             right by consuming the '>>' before issuing the diagnostic.  */
18129           cp_token *token = cp_lexer_consume_token (parser->lexer);
18130           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18131                  "a template argument list", &token->location);
18132         }
18133     }
18134   else
18135     cp_parser_skip_to_end_of_template_parameter_list (parser);
18136   /* The `>' token might be a greater-than operator again now.  */
18137   parser->greater_than_is_operator_p
18138     = saved_greater_than_is_operator_p;
18139   /* Restore the SAVED_SCOPE.  */
18140   parser->scope = saved_scope;
18141   parser->qualifying_scope = saved_qualifying_scope;
18142   parser->object_scope = saved_object_scope;
18143   skip_evaluation = saved_skip_evaluation;
18144
18145   return arguments;
18146 }
18147
18148 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18149    arguments, or the body of the function have not yet been parsed,
18150    parse them now.  */
18151
18152 static void
18153 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18154 {
18155   /* If this member is a template, get the underlying
18156      FUNCTION_DECL.  */
18157   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18158     member_function = DECL_TEMPLATE_RESULT (member_function);
18159
18160   /* There should not be any class definitions in progress at this
18161      point; the bodies of members are only parsed outside of all class
18162      definitions.  */
18163   gcc_assert (parser->num_classes_being_defined == 0);
18164   /* While we're parsing the member functions we might encounter more
18165      classes.  We want to handle them right away, but we don't want
18166      them getting mixed up with functions that are currently in the
18167      queue.  */
18168   parser->unparsed_functions_queues
18169     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18170
18171   /* Make sure that any template parameters are in scope.  */
18172   maybe_begin_member_template_processing (member_function);
18173
18174   /* If the body of the function has not yet been parsed, parse it
18175      now.  */
18176   if (DECL_PENDING_INLINE_P (member_function))
18177     {
18178       tree function_scope;
18179       cp_token_cache *tokens;
18180
18181       /* The function is no longer pending; we are processing it.  */
18182       tokens = DECL_PENDING_INLINE_INFO (member_function);
18183       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18184       DECL_PENDING_INLINE_P (member_function) = 0;
18185
18186       /* If this is a local class, enter the scope of the containing
18187          function.  */
18188       function_scope = current_function_decl;
18189       if (function_scope)
18190         push_function_context ();
18191
18192       /* Push the body of the function onto the lexer stack.  */
18193       cp_parser_push_lexer_for_tokens (parser, tokens);
18194
18195       /* Let the front end know that we going to be defining this
18196          function.  */
18197       start_preparsed_function (member_function, NULL_TREE,
18198                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18199
18200       /* Don't do access checking if it is a templated function.  */
18201       if (processing_template_decl)
18202         push_deferring_access_checks (dk_no_check);
18203
18204       /* Now, parse the body of the function.  */
18205       cp_parser_function_definition_after_declarator (parser,
18206                                                       /*inline_p=*/true);
18207
18208       if (processing_template_decl)
18209         pop_deferring_access_checks ();
18210
18211       /* Leave the scope of the containing function.  */
18212       if (function_scope)
18213         pop_function_context ();
18214       cp_parser_pop_lexer (parser);
18215     }
18216
18217   /* Remove any template parameters from the symbol table.  */
18218   maybe_end_member_template_processing ();
18219
18220   /* Restore the queue.  */
18221   parser->unparsed_functions_queues
18222     = TREE_CHAIN (parser->unparsed_functions_queues);
18223 }
18224
18225 /* If DECL contains any default args, remember it on the unparsed
18226    functions queue.  */
18227
18228 static void
18229 cp_parser_save_default_args (cp_parser* parser, tree decl)
18230 {
18231   tree probe;
18232
18233   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18234        probe;
18235        probe = TREE_CHAIN (probe))
18236     if (TREE_PURPOSE (probe))
18237       {
18238         TREE_PURPOSE (parser->unparsed_functions_queues)
18239           = tree_cons (current_class_type, decl,
18240                        TREE_PURPOSE (parser->unparsed_functions_queues));
18241         break;
18242       }
18243 }
18244
18245 /* FN is a FUNCTION_DECL which may contains a parameter with an
18246    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18247    assumes that the current scope is the scope in which the default
18248    argument should be processed.  */
18249
18250 static void
18251 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18252 {
18253   bool saved_local_variables_forbidden_p;
18254   tree parm;
18255
18256   /* While we're parsing the default args, we might (due to the
18257      statement expression extension) encounter more classes.  We want
18258      to handle them right away, but we don't want them getting mixed
18259      up with default args that are currently in the queue.  */
18260   parser->unparsed_functions_queues
18261     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18262
18263   /* Local variable names (and the `this' keyword) may not appear
18264      in a default argument.  */
18265   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18266   parser->local_variables_forbidden_p = true;
18267
18268   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18269        parm;
18270        parm = TREE_CHAIN (parm))
18271     {
18272       cp_token_cache *tokens;
18273       tree default_arg = TREE_PURPOSE (parm);
18274       tree parsed_arg;
18275       VEC(tree,gc) *insts;
18276       tree copy;
18277       unsigned ix;
18278
18279       if (!default_arg)
18280         continue;
18281
18282       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18283         /* This can happen for a friend declaration for a function
18284            already declared with default arguments.  */
18285         continue;
18286
18287        /* Push the saved tokens for the default argument onto the parser's
18288           lexer stack.  */
18289       tokens = DEFARG_TOKENS (default_arg);
18290       cp_parser_push_lexer_for_tokens (parser, tokens);
18291
18292       /* Parse the assignment-expression.  */
18293       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18294       if (parsed_arg == error_mark_node)
18295         {
18296           cp_parser_pop_lexer (parser);
18297           continue;
18298         }
18299
18300       if (!processing_template_decl)
18301         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18302
18303       TREE_PURPOSE (parm) = parsed_arg;
18304
18305       /* Update any instantiations we've already created.  */
18306       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18307            VEC_iterate (tree, insts, ix, copy); ix++)
18308         TREE_PURPOSE (copy) = parsed_arg;
18309
18310       /* If the token stream has not been completely used up, then
18311          there was extra junk after the end of the default
18312          argument.  */
18313       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18314         cp_parser_error (parser, "expected %<,%>");
18315
18316       /* Revert to the main lexer.  */
18317       cp_parser_pop_lexer (parser);
18318     }
18319
18320   /* Make sure no default arg is missing.  */
18321   check_default_args (fn);
18322
18323   /* Restore the state of local_variables_forbidden_p.  */
18324   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18325
18326   /* Restore the queue.  */
18327   parser->unparsed_functions_queues
18328     = TREE_CHAIN (parser->unparsed_functions_queues);
18329 }
18330
18331 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18332    either a TYPE or an expression, depending on the form of the
18333    input.  The KEYWORD indicates which kind of expression we have
18334    encountered.  */
18335
18336 static tree
18337 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18338 {
18339   tree expr = NULL_TREE;
18340   const char *saved_message;
18341   char *tmp;
18342   bool saved_integral_constant_expression_p;
18343   bool saved_non_integral_constant_expression_p;
18344   bool pack_expansion_p = false;
18345
18346   /* Types cannot be defined in a `sizeof' expression.  Save away the
18347      old message.  */
18348   saved_message = parser->type_definition_forbidden_message;
18349   /* And create the new one.  */
18350   tmp = concat ("types may not be defined in %<",
18351                 IDENTIFIER_POINTER (ridpointers[keyword]),
18352                 "%> expressions", NULL);
18353   parser->type_definition_forbidden_message = tmp;
18354
18355   /* The restrictions on constant-expressions do not apply inside
18356      sizeof expressions.  */
18357   saved_integral_constant_expression_p
18358     = parser->integral_constant_expression_p;
18359   saved_non_integral_constant_expression_p
18360     = parser->non_integral_constant_expression_p;
18361   parser->integral_constant_expression_p = false;
18362
18363   /* If it's a `...', then we are computing the length of a parameter
18364      pack.  */
18365   if (keyword == RID_SIZEOF
18366       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18367     {
18368       /* Consume the `...'.  */
18369       cp_lexer_consume_token (parser->lexer);
18370       maybe_warn_variadic_templates ();
18371
18372       /* Note that this is an expansion.  */
18373       pack_expansion_p = true;
18374     }
18375
18376   /* Do not actually evaluate the expression.  */
18377   ++skip_evaluation;
18378   /* If it's a `(', then we might be looking at the type-id
18379      construction.  */
18380   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18381     {
18382       tree type;
18383       bool saved_in_type_id_in_expr_p;
18384
18385       /* We can't be sure yet whether we're looking at a type-id or an
18386          expression.  */
18387       cp_parser_parse_tentatively (parser);
18388       /* Consume the `('.  */
18389       cp_lexer_consume_token (parser->lexer);
18390       /* Parse the type-id.  */
18391       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18392       parser->in_type_id_in_expr_p = true;
18393       type = cp_parser_type_id (parser);
18394       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18395       /* Now, look for the trailing `)'.  */
18396       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18397       /* If all went well, then we're done.  */
18398       if (cp_parser_parse_definitely (parser))
18399         {
18400           cp_decl_specifier_seq decl_specs;
18401
18402           /* Build a trivial decl-specifier-seq.  */
18403           clear_decl_specs (&decl_specs);
18404           decl_specs.type = type;
18405
18406           /* Call grokdeclarator to figure out what type this is.  */
18407           expr = grokdeclarator (NULL,
18408                                  &decl_specs,
18409                                  TYPENAME,
18410                                  /*initialized=*/0,
18411                                  /*attrlist=*/NULL);
18412         }
18413     }
18414
18415   /* If the type-id production did not work out, then we must be
18416      looking at the unary-expression production.  */
18417   if (!expr)
18418     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18419                                        /*cast_p=*/false, NULL);
18420
18421   if (pack_expansion_p)
18422     /* Build a pack expansion. */
18423     expr = make_pack_expansion (expr);
18424
18425   /* Go back to evaluating expressions.  */
18426   --skip_evaluation;
18427
18428   /* Free the message we created.  */
18429   free (tmp);
18430   /* And restore the old one.  */
18431   parser->type_definition_forbidden_message = saved_message;
18432   parser->integral_constant_expression_p
18433     = saved_integral_constant_expression_p;
18434   parser->non_integral_constant_expression_p
18435     = saved_non_integral_constant_expression_p;
18436
18437   return expr;
18438 }
18439
18440 /* If the current declaration has no declarator, return true.  */
18441
18442 static bool
18443 cp_parser_declares_only_class_p (cp_parser *parser)
18444 {
18445   /* If the next token is a `;' or a `,' then there is no
18446      declarator.  */
18447   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18448           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18449 }
18450
18451 /* Update the DECL_SPECS to reflect the storage class indicated by
18452    KEYWORD.  */
18453
18454 static void
18455 cp_parser_set_storage_class (cp_parser *parser,
18456                              cp_decl_specifier_seq *decl_specs,
18457                              enum rid keyword,
18458                              location_t location)
18459 {
18460   cp_storage_class storage_class;
18461
18462   if (parser->in_unbraced_linkage_specification_p)
18463     {
18464       error ("%Hinvalid use of %qD in linkage specification",
18465              &location, ridpointers[keyword]);
18466       return;
18467     }
18468   else if (decl_specs->storage_class != sc_none)
18469     {
18470       decl_specs->conflicting_specifiers_p = true;
18471       return;
18472     }
18473
18474   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18475       && decl_specs->specs[(int) ds_thread])
18476     {
18477       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18478       decl_specs->specs[(int) ds_thread] = 0;
18479     }
18480
18481   switch (keyword)
18482     {
18483     case RID_AUTO:
18484       storage_class = sc_auto;
18485       break;
18486     case RID_REGISTER:
18487       storage_class = sc_register;
18488       break;
18489     case RID_STATIC:
18490       storage_class = sc_static;
18491       break;
18492     case RID_EXTERN:
18493       storage_class = sc_extern;
18494       break;
18495     case RID_MUTABLE:
18496       storage_class = sc_mutable;
18497       break;
18498     default:
18499       gcc_unreachable ();
18500     }
18501   decl_specs->storage_class = storage_class;
18502
18503   /* A storage class specifier cannot be applied alongside a typedef 
18504      specifier. If there is a typedef specifier present then set 
18505      conflicting_specifiers_p which will trigger an error later
18506      on in grokdeclarator. */
18507   if (decl_specs->specs[(int)ds_typedef])
18508     decl_specs->conflicting_specifiers_p = true;
18509 }
18510
18511 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18512    is true, the type is a user-defined type; otherwise it is a
18513    built-in type specified by a keyword.  */
18514
18515 static void
18516 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18517                               tree type_spec,
18518                               location_t location,
18519                               bool user_defined_p)
18520 {
18521   decl_specs->any_specifiers_p = true;
18522
18523   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18524      (with, for example, in "typedef int wchar_t;") we remember that
18525      this is what happened.  In system headers, we ignore these
18526      declarations so that G++ can work with system headers that are not
18527      C++-safe.  */
18528   if (decl_specs->specs[(int) ds_typedef]
18529       && !user_defined_p
18530       && (type_spec == boolean_type_node
18531           || type_spec == char16_type_node
18532           || type_spec == char32_type_node
18533           || type_spec == wchar_type_node)
18534       && (decl_specs->type
18535           || decl_specs->specs[(int) ds_long]
18536           || decl_specs->specs[(int) ds_short]
18537           || decl_specs->specs[(int) ds_unsigned]
18538           || decl_specs->specs[(int) ds_signed]))
18539     {
18540       decl_specs->redefined_builtin_type = type_spec;
18541       if (!decl_specs->type)
18542         {
18543           decl_specs->type = type_spec;
18544           decl_specs->user_defined_type_p = false;
18545           decl_specs->type_location = location;
18546         }
18547     }
18548   else if (decl_specs->type)
18549     decl_specs->multiple_types_p = true;
18550   else
18551     {
18552       decl_specs->type = type_spec;
18553       decl_specs->user_defined_type_p = user_defined_p;
18554       decl_specs->redefined_builtin_type = NULL_TREE;
18555       decl_specs->type_location = location;
18556     }
18557 }
18558
18559 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18560    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18561
18562 static bool
18563 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18564 {
18565   return decl_specifiers->specs[(int) ds_friend] != 0;
18566 }
18567
18568 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18569    issue an error message indicating that TOKEN_DESC was expected.
18570
18571    Returns the token consumed, if the token had the appropriate type.
18572    Otherwise, returns NULL.  */
18573
18574 static cp_token *
18575 cp_parser_require (cp_parser* parser,
18576                    enum cpp_ttype type,
18577                    const char* token_desc)
18578 {
18579   if (cp_lexer_next_token_is (parser->lexer, type))
18580     return cp_lexer_consume_token (parser->lexer);
18581   else
18582     {
18583       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18584       if (!cp_parser_simulate_error (parser))
18585         {
18586           char *message = concat ("expected ", token_desc, NULL);
18587           cp_parser_error (parser, message);
18588           free (message);
18589         }
18590       return NULL;
18591     }
18592 }
18593
18594 /* An error message is produced if the next token is not '>'.
18595    All further tokens are skipped until the desired token is
18596    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18597
18598 static void
18599 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18600 {
18601   /* Current level of '< ... >'.  */
18602   unsigned level = 0;
18603   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18604   unsigned nesting_depth = 0;
18605
18606   /* Are we ready, yet?  If not, issue error message.  */
18607   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18608     return;
18609
18610   /* Skip tokens until the desired token is found.  */
18611   while (true)
18612     {
18613       /* Peek at the next token.  */
18614       switch (cp_lexer_peek_token (parser->lexer)->type)
18615         {
18616         case CPP_LESS:
18617           if (!nesting_depth)
18618             ++level;
18619           break;
18620
18621         case CPP_RSHIFT:
18622           if (cxx_dialect == cxx98)
18623             /* C++0x views the `>>' operator as two `>' tokens, but
18624                C++98 does not. */
18625             break;
18626           else if (!nesting_depth && level-- == 0)
18627             {
18628               /* We've hit a `>>' where the first `>' closes the
18629                  template argument list, and the second `>' is
18630                  spurious.  Just consume the `>>' and stop; we've
18631                  already produced at least one error.  */
18632               cp_lexer_consume_token (parser->lexer);
18633               return;
18634             }
18635           /* Fall through for C++0x, so we handle the second `>' in
18636              the `>>'.  */
18637
18638         case CPP_GREATER:
18639           if (!nesting_depth && level-- == 0)
18640             {
18641               /* We've reached the token we want, consume it and stop.  */
18642               cp_lexer_consume_token (parser->lexer);
18643               return;
18644             }
18645           break;
18646
18647         case CPP_OPEN_PAREN:
18648         case CPP_OPEN_SQUARE:
18649           ++nesting_depth;
18650           break;
18651
18652         case CPP_CLOSE_PAREN:
18653         case CPP_CLOSE_SQUARE:
18654           if (nesting_depth-- == 0)
18655             return;
18656           break;
18657
18658         case CPP_EOF:
18659         case CPP_PRAGMA_EOL:
18660         case CPP_SEMICOLON:
18661         case CPP_OPEN_BRACE:
18662         case CPP_CLOSE_BRACE:
18663           /* The '>' was probably forgotten, don't look further.  */
18664           return;
18665
18666         default:
18667           break;
18668         }
18669
18670       /* Consume this token.  */
18671       cp_lexer_consume_token (parser->lexer);
18672     }
18673 }
18674
18675 /* If the next token is the indicated keyword, consume it.  Otherwise,
18676    issue an error message indicating that TOKEN_DESC was expected.
18677
18678    Returns the token consumed, if the token had the appropriate type.
18679    Otherwise, returns NULL.  */
18680
18681 static cp_token *
18682 cp_parser_require_keyword (cp_parser* parser,
18683                            enum rid keyword,
18684                            const char* token_desc)
18685 {
18686   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18687
18688   if (token && token->keyword != keyword)
18689     {
18690       dyn_string_t error_msg;
18691
18692       /* Format the error message.  */
18693       error_msg = dyn_string_new (0);
18694       dyn_string_append_cstr (error_msg, "expected ");
18695       dyn_string_append_cstr (error_msg, token_desc);
18696       cp_parser_error (parser, error_msg->s);
18697       dyn_string_delete (error_msg);
18698       return NULL;
18699     }
18700
18701   return token;
18702 }
18703
18704 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18705    function-definition.  */
18706
18707 static bool
18708 cp_parser_token_starts_function_definition_p (cp_token* token)
18709 {
18710   return (/* An ordinary function-body begins with an `{'.  */
18711           token->type == CPP_OPEN_BRACE
18712           /* A ctor-initializer begins with a `:'.  */
18713           || token->type == CPP_COLON
18714           /* A function-try-block begins with `try'.  */
18715           || token->keyword == RID_TRY
18716           /* The named return value extension begins with `return'.  */
18717           || token->keyword == RID_RETURN);
18718 }
18719
18720 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18721    definition.  */
18722
18723 static bool
18724 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18725 {
18726   cp_token *token;
18727
18728   token = cp_lexer_peek_token (parser->lexer);
18729   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18730 }
18731
18732 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18733    C++0x) ending a template-argument.  */
18734
18735 static bool
18736 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18737 {
18738   cp_token *token;
18739
18740   token = cp_lexer_peek_token (parser->lexer);
18741   return (token->type == CPP_COMMA 
18742           || token->type == CPP_GREATER
18743           || token->type == CPP_ELLIPSIS
18744           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18745 }
18746
18747 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18748    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18749
18750 static bool
18751 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18752                                                      size_t n)
18753 {
18754   cp_token *token;
18755
18756   token = cp_lexer_peek_nth_token (parser->lexer, n);
18757   if (token->type == CPP_LESS)
18758     return true;
18759   /* Check for the sequence `<::' in the original code. It would be lexed as
18760      `[:', where `[' is a digraph, and there is no whitespace before
18761      `:'.  */
18762   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18763     {
18764       cp_token *token2;
18765       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18766       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18767         return true;
18768     }
18769   return false;
18770 }
18771
18772 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18773    or none_type otherwise.  */
18774
18775 static enum tag_types
18776 cp_parser_token_is_class_key (cp_token* token)
18777 {
18778   switch (token->keyword)
18779     {
18780     case RID_CLASS:
18781       return class_type;
18782     case RID_STRUCT:
18783       return record_type;
18784     case RID_UNION:
18785       return union_type;
18786
18787     default:
18788       return none_type;
18789     }
18790 }
18791
18792 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18793
18794 static void
18795 cp_parser_check_class_key (enum tag_types class_key, tree type)
18796 {
18797   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18798     permerror (input_location, "%qs tag used in naming %q#T",
18799             class_key == union_type ? "union"
18800              : class_key == record_type ? "struct" : "class",
18801              type);
18802 }
18803
18804 /* Issue an error message if DECL is redeclared with different
18805    access than its original declaration [class.access.spec/3].
18806    This applies to nested classes and nested class templates.
18807    [class.mem/1].  */
18808
18809 static void
18810 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18811 {
18812   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18813     return;
18814
18815   if ((TREE_PRIVATE (decl)
18816        != (current_access_specifier == access_private_node))
18817       || (TREE_PROTECTED (decl)
18818           != (current_access_specifier == access_protected_node)))
18819     error ("%H%qD redeclared with different access", &location, decl);
18820 }
18821
18822 /* Look for the `template' keyword, as a syntactic disambiguator.
18823    Return TRUE iff it is present, in which case it will be
18824    consumed.  */
18825
18826 static bool
18827 cp_parser_optional_template_keyword (cp_parser *parser)
18828 {
18829   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18830     {
18831       /* The `template' keyword can only be used within templates;
18832          outside templates the parser can always figure out what is a
18833          template and what is not.  */
18834       if (!processing_template_decl)
18835         {
18836           cp_token *token = cp_lexer_peek_token (parser->lexer);
18837           error ("%H%<template%> (as a disambiguator) is only allowed "
18838                  "within templates", &token->location);
18839           /* If this part of the token stream is rescanned, the same
18840              error message would be generated.  So, we purge the token
18841              from the stream.  */
18842           cp_lexer_purge_token (parser->lexer);
18843           return false;
18844         }
18845       else
18846         {
18847           /* Consume the `template' keyword.  */
18848           cp_lexer_consume_token (parser->lexer);
18849           return true;
18850         }
18851     }
18852
18853   return false;
18854 }
18855
18856 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18857    set PARSER->SCOPE, and perform other related actions.  */
18858
18859 static void
18860 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18861 {
18862   int i;
18863   struct tree_check *check_value;
18864   deferred_access_check *chk;
18865   VEC (deferred_access_check,gc) *checks;
18866
18867   /* Get the stored value.  */
18868   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18869   /* Perform any access checks that were deferred.  */
18870   checks = check_value->checks;
18871   if (checks)
18872     {
18873       for (i = 0 ;
18874            VEC_iterate (deferred_access_check, checks, i, chk) ;
18875            ++i)
18876         {
18877           perform_or_defer_access_check (chk->binfo,
18878                                          chk->decl,
18879                                          chk->diag_decl);
18880         }
18881     }
18882   /* Set the scope from the stored value.  */
18883   parser->scope = check_value->value;
18884   parser->qualifying_scope = check_value->qualifying_scope;
18885   parser->object_scope = NULL_TREE;
18886 }
18887
18888 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18889    encounter the end of a block before what we were looking for.  */
18890
18891 static bool
18892 cp_parser_cache_group (cp_parser *parser,
18893                        enum cpp_ttype end,
18894                        unsigned depth)
18895 {
18896   while (true)
18897     {
18898       cp_token *token = cp_lexer_peek_token (parser->lexer);
18899
18900       /* Abort a parenthesized expression if we encounter a semicolon.  */
18901       if ((end == CPP_CLOSE_PAREN || depth == 0)
18902           && token->type == CPP_SEMICOLON)
18903         return true;
18904       /* If we've reached the end of the file, stop.  */
18905       if (token->type == CPP_EOF
18906           || (end != CPP_PRAGMA_EOL
18907               && token->type == CPP_PRAGMA_EOL))
18908         return true;
18909       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18910         /* We've hit the end of an enclosing block, so there's been some
18911            kind of syntax error.  */
18912         return true;
18913
18914       /* Consume the token.  */
18915       cp_lexer_consume_token (parser->lexer);
18916       /* See if it starts a new group.  */
18917       if (token->type == CPP_OPEN_BRACE)
18918         {
18919           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18920           /* In theory this should probably check end == '}', but
18921              cp_parser_save_member_function_body needs it to exit
18922              after either '}' or ')' when called with ')'.  */
18923           if (depth == 0)
18924             return false;
18925         }
18926       else if (token->type == CPP_OPEN_PAREN)
18927         {
18928           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18929           if (depth == 0 && end == CPP_CLOSE_PAREN)
18930             return false;
18931         }
18932       else if (token->type == CPP_PRAGMA)
18933         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18934       else if (token->type == end)
18935         return false;
18936     }
18937 }
18938
18939 /* Begin parsing tentatively.  We always save tokens while parsing
18940    tentatively so that if the tentative parsing fails we can restore the
18941    tokens.  */
18942
18943 static void
18944 cp_parser_parse_tentatively (cp_parser* parser)
18945 {
18946   /* Enter a new parsing context.  */
18947   parser->context = cp_parser_context_new (parser->context);
18948   /* Begin saving tokens.  */
18949   cp_lexer_save_tokens (parser->lexer);
18950   /* In order to avoid repetitive access control error messages,
18951      access checks are queued up until we are no longer parsing
18952      tentatively.  */
18953   push_deferring_access_checks (dk_deferred);
18954 }
18955
18956 /* Commit to the currently active tentative parse.  */
18957
18958 static void
18959 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18960 {
18961   cp_parser_context *context;
18962   cp_lexer *lexer;
18963
18964   /* Mark all of the levels as committed.  */
18965   lexer = parser->lexer;
18966   for (context = parser->context; context->next; context = context->next)
18967     {
18968       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18969         break;
18970       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18971       while (!cp_lexer_saving_tokens (lexer))
18972         lexer = lexer->next;
18973       cp_lexer_commit_tokens (lexer);
18974     }
18975 }
18976
18977 /* Abort the currently active tentative parse.  All consumed tokens
18978    will be rolled back, and no diagnostics will be issued.  */
18979
18980 static void
18981 cp_parser_abort_tentative_parse (cp_parser* parser)
18982 {
18983   cp_parser_simulate_error (parser);
18984   /* Now, pretend that we want to see if the construct was
18985      successfully parsed.  */
18986   cp_parser_parse_definitely (parser);
18987 }
18988
18989 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18990    token stream.  Otherwise, commit to the tokens we have consumed.
18991    Returns true if no error occurred; false otherwise.  */
18992
18993 static bool
18994 cp_parser_parse_definitely (cp_parser* parser)
18995 {
18996   bool error_occurred;
18997   cp_parser_context *context;
18998
18999   /* Remember whether or not an error occurred, since we are about to
19000      destroy that information.  */
19001   error_occurred = cp_parser_error_occurred (parser);
19002   /* Remove the topmost context from the stack.  */
19003   context = parser->context;
19004   parser->context = context->next;
19005   /* If no parse errors occurred, commit to the tentative parse.  */
19006   if (!error_occurred)
19007     {
19008       /* Commit to the tokens read tentatively, unless that was
19009          already done.  */
19010       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19011         cp_lexer_commit_tokens (parser->lexer);
19012
19013       pop_to_parent_deferring_access_checks ();
19014     }
19015   /* Otherwise, if errors occurred, roll back our state so that things
19016      are just as they were before we began the tentative parse.  */
19017   else
19018     {
19019       cp_lexer_rollback_tokens (parser->lexer);
19020       pop_deferring_access_checks ();
19021     }
19022   /* Add the context to the front of the free list.  */
19023   context->next = cp_parser_context_free_list;
19024   cp_parser_context_free_list = context;
19025
19026   return !error_occurred;
19027 }
19028
19029 /* Returns true if we are parsing tentatively and are not committed to
19030    this tentative parse.  */
19031
19032 static bool
19033 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19034 {
19035   return (cp_parser_parsing_tentatively (parser)
19036           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19037 }
19038
19039 /* Returns nonzero iff an error has occurred during the most recent
19040    tentative parse.  */
19041
19042 static bool
19043 cp_parser_error_occurred (cp_parser* parser)
19044 {
19045   return (cp_parser_parsing_tentatively (parser)
19046           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19047 }
19048
19049 /* Returns nonzero if GNU extensions are allowed.  */
19050
19051 static bool
19052 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19053 {
19054   return parser->allow_gnu_extensions_p;
19055 }
19056 \f
19057 /* Objective-C++ Productions */
19058
19059
19060 /* Parse an Objective-C expression, which feeds into a primary-expression
19061    above.
19062
19063    objc-expression:
19064      objc-message-expression
19065      objc-string-literal
19066      objc-encode-expression
19067      objc-protocol-expression
19068      objc-selector-expression
19069
19070   Returns a tree representation of the expression.  */
19071
19072 static tree
19073 cp_parser_objc_expression (cp_parser* parser)
19074 {
19075   /* Try to figure out what kind of declaration is present.  */
19076   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19077
19078   switch (kwd->type)
19079     {
19080     case CPP_OPEN_SQUARE:
19081       return cp_parser_objc_message_expression (parser);
19082
19083     case CPP_OBJC_STRING:
19084       kwd = cp_lexer_consume_token (parser->lexer);
19085       return objc_build_string_object (kwd->u.value);
19086
19087     case CPP_KEYWORD:
19088       switch (kwd->keyword)
19089         {
19090         case RID_AT_ENCODE:
19091           return cp_parser_objc_encode_expression (parser);
19092
19093         case RID_AT_PROTOCOL:
19094           return cp_parser_objc_protocol_expression (parser);
19095
19096         case RID_AT_SELECTOR:
19097           return cp_parser_objc_selector_expression (parser);
19098
19099         default:
19100           break;
19101         }
19102     default:
19103       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19104              &kwd->location, kwd->u.value);
19105       cp_parser_skip_to_end_of_block_or_statement (parser);
19106     }
19107
19108   return error_mark_node;
19109 }
19110
19111 /* Parse an Objective-C message expression.
19112
19113    objc-message-expression:
19114      [ objc-message-receiver objc-message-args ]
19115
19116    Returns a representation of an Objective-C message.  */
19117
19118 static tree
19119 cp_parser_objc_message_expression (cp_parser* parser)
19120 {
19121   tree receiver, messageargs;
19122
19123   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19124   receiver = cp_parser_objc_message_receiver (parser);
19125   messageargs = cp_parser_objc_message_args (parser);
19126   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19127
19128   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19129 }
19130
19131 /* Parse an objc-message-receiver.
19132
19133    objc-message-receiver:
19134      expression
19135      simple-type-specifier
19136
19137   Returns a representation of the type or expression.  */
19138
19139 static tree
19140 cp_parser_objc_message_receiver (cp_parser* parser)
19141 {
19142   tree rcv;
19143
19144   /* An Objective-C message receiver may be either (1) a type
19145      or (2) an expression.  */
19146   cp_parser_parse_tentatively (parser);
19147   rcv = cp_parser_expression (parser, false, NULL);
19148
19149   if (cp_parser_parse_definitely (parser))
19150     return rcv;
19151
19152   rcv = cp_parser_simple_type_specifier (parser,
19153                                          /*decl_specs=*/NULL,
19154                                          CP_PARSER_FLAGS_NONE);
19155
19156   return objc_get_class_reference (rcv);
19157 }
19158
19159 /* Parse the arguments and selectors comprising an Objective-C message.
19160
19161    objc-message-args:
19162      objc-selector
19163      objc-selector-args
19164      objc-selector-args , objc-comma-args
19165
19166    objc-selector-args:
19167      objc-selector [opt] : assignment-expression
19168      objc-selector-args objc-selector [opt] : assignment-expression
19169
19170    objc-comma-args:
19171      assignment-expression
19172      objc-comma-args , assignment-expression
19173
19174    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19175    selector arguments and TREE_VALUE containing a list of comma
19176    arguments.  */
19177
19178 static tree
19179 cp_parser_objc_message_args (cp_parser* parser)
19180 {
19181   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19182   bool maybe_unary_selector_p = true;
19183   cp_token *token = cp_lexer_peek_token (parser->lexer);
19184
19185   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19186     {
19187       tree selector = NULL_TREE, arg;
19188
19189       if (token->type != CPP_COLON)
19190         selector = cp_parser_objc_selector (parser);
19191
19192       /* Detect if we have a unary selector.  */
19193       if (maybe_unary_selector_p
19194           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19195         return build_tree_list (selector, NULL_TREE);
19196
19197       maybe_unary_selector_p = false;
19198       cp_parser_require (parser, CPP_COLON, "%<:%>");
19199       arg = cp_parser_assignment_expression (parser, false, NULL);
19200
19201       sel_args
19202         = chainon (sel_args,
19203                    build_tree_list (selector, arg));
19204
19205       token = cp_lexer_peek_token (parser->lexer);
19206     }
19207
19208   /* Handle non-selector arguments, if any. */
19209   while (token->type == CPP_COMMA)
19210     {
19211       tree arg;
19212
19213       cp_lexer_consume_token (parser->lexer);
19214       arg = cp_parser_assignment_expression (parser, false, NULL);
19215
19216       addl_args
19217         = chainon (addl_args,
19218                    build_tree_list (NULL_TREE, arg));
19219
19220       token = cp_lexer_peek_token (parser->lexer);
19221     }
19222
19223   return build_tree_list (sel_args, addl_args);
19224 }
19225
19226 /* Parse an Objective-C encode expression.
19227
19228    objc-encode-expression:
19229      @encode objc-typename
19230
19231    Returns an encoded representation of the type argument.  */
19232
19233 static tree
19234 cp_parser_objc_encode_expression (cp_parser* parser)
19235 {
19236   tree type;
19237   cp_token *token;
19238
19239   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19240   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19241   token = cp_lexer_peek_token (parser->lexer);
19242   type = complete_type (cp_parser_type_id (parser));
19243   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19244
19245   if (!type)
19246     {
19247       error ("%H%<@encode%> must specify a type as an argument",
19248              &token->location);
19249       return error_mark_node;
19250     }
19251
19252   return objc_build_encode_expr (type);
19253 }
19254
19255 /* Parse an Objective-C @defs expression.  */
19256
19257 static tree
19258 cp_parser_objc_defs_expression (cp_parser *parser)
19259 {
19260   tree name;
19261
19262   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19263   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19264   name = cp_parser_identifier (parser);
19265   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19266
19267   return objc_get_class_ivars (name);
19268 }
19269
19270 /* Parse an Objective-C protocol expression.
19271
19272   objc-protocol-expression:
19273     @protocol ( identifier )
19274
19275   Returns a representation of the protocol expression.  */
19276
19277 static tree
19278 cp_parser_objc_protocol_expression (cp_parser* parser)
19279 {
19280   tree proto;
19281
19282   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19283   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19284   proto = cp_parser_identifier (parser);
19285   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19286
19287   return objc_build_protocol_expr (proto);
19288 }
19289
19290 /* Parse an Objective-C selector expression.
19291
19292    objc-selector-expression:
19293      @selector ( objc-method-signature )
19294
19295    objc-method-signature:
19296      objc-selector
19297      objc-selector-seq
19298
19299    objc-selector-seq:
19300      objc-selector :
19301      objc-selector-seq objc-selector :
19302
19303   Returns a representation of the method selector.  */
19304
19305 static tree
19306 cp_parser_objc_selector_expression (cp_parser* parser)
19307 {
19308   tree sel_seq = NULL_TREE;
19309   bool maybe_unary_selector_p = true;
19310   cp_token *token;
19311
19312   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19313   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19314   token = cp_lexer_peek_token (parser->lexer);
19315
19316   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19317          || token->type == CPP_SCOPE)
19318     {
19319       tree selector = NULL_TREE;
19320
19321       if (token->type != CPP_COLON
19322           || token->type == CPP_SCOPE)
19323         selector = cp_parser_objc_selector (parser);
19324
19325       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19326           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19327         {
19328           /* Detect if we have a unary selector.  */
19329           if (maybe_unary_selector_p)
19330             {
19331               sel_seq = selector;
19332               goto finish_selector;
19333             }
19334           else
19335             {
19336               cp_parser_error (parser, "expected %<:%>");
19337             }
19338         }
19339       maybe_unary_selector_p = false;
19340       token = cp_lexer_consume_token (parser->lexer);
19341
19342       if (token->type == CPP_SCOPE)
19343         {
19344           sel_seq
19345             = chainon (sel_seq,
19346                        build_tree_list (selector, NULL_TREE));
19347           sel_seq
19348             = chainon (sel_seq,
19349                        build_tree_list (NULL_TREE, NULL_TREE));
19350         }
19351       else
19352         sel_seq
19353           = chainon (sel_seq,
19354                      build_tree_list (selector, NULL_TREE));
19355
19356       token = cp_lexer_peek_token (parser->lexer);
19357     }
19358
19359  finish_selector:
19360   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19361
19362   return objc_build_selector_expr (sel_seq);
19363 }
19364
19365 /* Parse a list of identifiers.
19366
19367    objc-identifier-list:
19368      identifier
19369      objc-identifier-list , identifier
19370
19371    Returns a TREE_LIST of identifier nodes.  */
19372
19373 static tree
19374 cp_parser_objc_identifier_list (cp_parser* parser)
19375 {
19376   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19377   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19378
19379   while (sep->type == CPP_COMMA)
19380     {
19381       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19382       list = chainon (list,
19383                       build_tree_list (NULL_TREE,
19384                                        cp_parser_identifier (parser)));
19385       sep = cp_lexer_peek_token (parser->lexer);
19386     }
19387
19388   return list;
19389 }
19390
19391 /* Parse an Objective-C alias declaration.
19392
19393    objc-alias-declaration:
19394      @compatibility_alias identifier identifier ;
19395
19396    This function registers the alias mapping with the Objective-C front end.
19397    It returns nothing.  */
19398
19399 static void
19400 cp_parser_objc_alias_declaration (cp_parser* parser)
19401 {
19402   tree alias, orig;
19403
19404   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19405   alias = cp_parser_identifier (parser);
19406   orig = cp_parser_identifier (parser);
19407   objc_declare_alias (alias, orig);
19408   cp_parser_consume_semicolon_at_end_of_statement (parser);
19409 }
19410
19411 /* Parse an Objective-C class forward-declaration.
19412
19413    objc-class-declaration:
19414      @class objc-identifier-list ;
19415
19416    The function registers the forward declarations with the Objective-C
19417    front end.  It returns nothing.  */
19418
19419 static void
19420 cp_parser_objc_class_declaration (cp_parser* parser)
19421 {
19422   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19423   objc_declare_class (cp_parser_objc_identifier_list (parser));
19424   cp_parser_consume_semicolon_at_end_of_statement (parser);
19425 }
19426
19427 /* Parse a list of Objective-C protocol references.
19428
19429    objc-protocol-refs-opt:
19430      objc-protocol-refs [opt]
19431
19432    objc-protocol-refs:
19433      < objc-identifier-list >
19434
19435    Returns a TREE_LIST of identifiers, if any.  */
19436
19437 static tree
19438 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19439 {
19440   tree protorefs = NULL_TREE;
19441
19442   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19443     {
19444       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19445       protorefs = cp_parser_objc_identifier_list (parser);
19446       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19447     }
19448
19449   return protorefs;
19450 }
19451
19452 /* Parse a Objective-C visibility specification.  */
19453
19454 static void
19455 cp_parser_objc_visibility_spec (cp_parser* parser)
19456 {
19457   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19458
19459   switch (vis->keyword)
19460     {
19461     case RID_AT_PRIVATE:
19462       objc_set_visibility (2);
19463       break;
19464     case RID_AT_PROTECTED:
19465       objc_set_visibility (0);
19466       break;
19467     case RID_AT_PUBLIC:
19468       objc_set_visibility (1);
19469       break;
19470     default:
19471       return;
19472     }
19473
19474   /* Eat '@private'/'@protected'/'@public'.  */
19475   cp_lexer_consume_token (parser->lexer);
19476 }
19477
19478 /* Parse an Objective-C method type.  */
19479
19480 static void
19481 cp_parser_objc_method_type (cp_parser* parser)
19482 {
19483   objc_set_method_type
19484    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19485     ? PLUS_EXPR
19486     : MINUS_EXPR);
19487 }
19488
19489 /* Parse an Objective-C protocol qualifier.  */
19490
19491 static tree
19492 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19493 {
19494   tree quals = NULL_TREE, node;
19495   cp_token *token = cp_lexer_peek_token (parser->lexer);
19496
19497   node = token->u.value;
19498
19499   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19500          && (node == ridpointers [(int) RID_IN]
19501              || node == ridpointers [(int) RID_OUT]
19502              || node == ridpointers [(int) RID_INOUT]
19503              || node == ridpointers [(int) RID_BYCOPY]
19504              || node == ridpointers [(int) RID_BYREF]
19505              || node == ridpointers [(int) RID_ONEWAY]))
19506     {
19507       quals = tree_cons (NULL_TREE, node, quals);
19508       cp_lexer_consume_token (parser->lexer);
19509       token = cp_lexer_peek_token (parser->lexer);
19510       node = token->u.value;
19511     }
19512
19513   return quals;
19514 }
19515
19516 /* Parse an Objective-C typename.  */
19517
19518 static tree
19519 cp_parser_objc_typename (cp_parser* parser)
19520 {
19521   tree type_name = NULL_TREE;
19522
19523   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19524     {
19525       tree proto_quals, cp_type = NULL_TREE;
19526
19527       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19528       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19529
19530       /* An ObjC type name may consist of just protocol qualifiers, in which
19531          case the type shall default to 'id'.  */
19532       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19533         cp_type = cp_parser_type_id (parser);
19534
19535       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19536       type_name = build_tree_list (proto_quals, cp_type);
19537     }
19538
19539   return type_name;
19540 }
19541
19542 /* Check to see if TYPE refers to an Objective-C selector name.  */
19543
19544 static bool
19545 cp_parser_objc_selector_p (enum cpp_ttype type)
19546 {
19547   return (type == CPP_NAME || type == CPP_KEYWORD
19548           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19549           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19550           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19551           || type == CPP_XOR || type == CPP_XOR_EQ);
19552 }
19553
19554 /* Parse an Objective-C selector.  */
19555
19556 static tree
19557 cp_parser_objc_selector (cp_parser* parser)
19558 {
19559   cp_token *token = cp_lexer_consume_token (parser->lexer);
19560
19561   if (!cp_parser_objc_selector_p (token->type))
19562     {
19563       error ("%Hinvalid Objective-C++ selector name", &token->location);
19564       return error_mark_node;
19565     }
19566
19567   /* C++ operator names are allowed to appear in ObjC selectors.  */
19568   switch (token->type)
19569     {
19570     case CPP_AND_AND: return get_identifier ("and");
19571     case CPP_AND_EQ: return get_identifier ("and_eq");
19572     case CPP_AND: return get_identifier ("bitand");
19573     case CPP_OR: return get_identifier ("bitor");
19574     case CPP_COMPL: return get_identifier ("compl");
19575     case CPP_NOT: return get_identifier ("not");
19576     case CPP_NOT_EQ: return get_identifier ("not_eq");
19577     case CPP_OR_OR: return get_identifier ("or");
19578     case CPP_OR_EQ: return get_identifier ("or_eq");
19579     case CPP_XOR: return get_identifier ("xor");
19580     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19581     default: return token->u.value;
19582     }
19583 }
19584
19585 /* Parse an Objective-C params list.  */
19586
19587 static tree
19588 cp_parser_objc_method_keyword_params (cp_parser* parser)
19589 {
19590   tree params = NULL_TREE;
19591   bool maybe_unary_selector_p = true;
19592   cp_token *token = cp_lexer_peek_token (parser->lexer);
19593
19594   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19595     {
19596       tree selector = NULL_TREE, type_name, identifier;
19597
19598       if (token->type != CPP_COLON)
19599         selector = cp_parser_objc_selector (parser);
19600
19601       /* Detect if we have a unary selector.  */
19602       if (maybe_unary_selector_p
19603           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19604         return selector;
19605
19606       maybe_unary_selector_p = false;
19607       cp_parser_require (parser, CPP_COLON, "%<:%>");
19608       type_name = cp_parser_objc_typename (parser);
19609       identifier = cp_parser_identifier (parser);
19610
19611       params
19612         = chainon (params,
19613                    objc_build_keyword_decl (selector,
19614                                             type_name,
19615                                             identifier));
19616
19617       token = cp_lexer_peek_token (parser->lexer);
19618     }
19619
19620   return params;
19621 }
19622
19623 /* Parse the non-keyword Objective-C params.  */
19624
19625 static tree
19626 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19627 {
19628   tree params = make_node (TREE_LIST);
19629   cp_token *token = cp_lexer_peek_token (parser->lexer);
19630   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19631
19632   while (token->type == CPP_COMMA)
19633     {
19634       cp_parameter_declarator *parmdecl;
19635       tree parm;
19636
19637       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19638       token = cp_lexer_peek_token (parser->lexer);
19639
19640       if (token->type == CPP_ELLIPSIS)
19641         {
19642           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19643           *ellipsisp = true;
19644           break;
19645         }
19646
19647       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19648       parm = grokdeclarator (parmdecl->declarator,
19649                              &parmdecl->decl_specifiers,
19650                              PARM, /*initialized=*/0,
19651                              /*attrlist=*/NULL);
19652
19653       chainon (params, build_tree_list (NULL_TREE, parm));
19654       token = cp_lexer_peek_token (parser->lexer);
19655     }
19656
19657   return params;
19658 }
19659
19660 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19661
19662 static void
19663 cp_parser_objc_interstitial_code (cp_parser* parser)
19664 {
19665   cp_token *token = cp_lexer_peek_token (parser->lexer);
19666
19667   /* If the next token is `extern' and the following token is a string
19668      literal, then we have a linkage specification.  */
19669   if (token->keyword == RID_EXTERN
19670       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19671     cp_parser_linkage_specification (parser);
19672   /* Handle #pragma, if any.  */
19673   else if (token->type == CPP_PRAGMA)
19674     cp_parser_pragma (parser, pragma_external);
19675   /* Allow stray semicolons.  */
19676   else if (token->type == CPP_SEMICOLON)
19677     cp_lexer_consume_token (parser->lexer);
19678   /* Finally, try to parse a block-declaration, or a function-definition.  */
19679   else
19680     cp_parser_block_declaration (parser, /*statement_p=*/false);
19681 }
19682
19683 /* Parse a method signature.  */
19684
19685 static tree
19686 cp_parser_objc_method_signature (cp_parser* parser)
19687 {
19688   tree rettype, kwdparms, optparms;
19689   bool ellipsis = false;
19690
19691   cp_parser_objc_method_type (parser);
19692   rettype = cp_parser_objc_typename (parser);
19693   kwdparms = cp_parser_objc_method_keyword_params (parser);
19694   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19695
19696   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19697 }
19698
19699 /* Pars an Objective-C method prototype list.  */
19700
19701 static void
19702 cp_parser_objc_method_prototype_list (cp_parser* parser)
19703 {
19704   cp_token *token = cp_lexer_peek_token (parser->lexer);
19705
19706   while (token->keyword != RID_AT_END)
19707     {
19708       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19709         {
19710           objc_add_method_declaration
19711            (cp_parser_objc_method_signature (parser));
19712           cp_parser_consume_semicolon_at_end_of_statement (parser);
19713         }
19714       else
19715         /* Allow for interspersed non-ObjC++ code.  */
19716         cp_parser_objc_interstitial_code (parser);
19717
19718       token = cp_lexer_peek_token (parser->lexer);
19719     }
19720
19721   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19722   objc_finish_interface ();
19723 }
19724
19725 /* Parse an Objective-C method definition list.  */
19726
19727 static void
19728 cp_parser_objc_method_definition_list (cp_parser* parser)
19729 {
19730   cp_token *token = cp_lexer_peek_token (parser->lexer);
19731
19732   while (token->keyword != RID_AT_END)
19733     {
19734       tree meth;
19735
19736       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19737         {
19738           push_deferring_access_checks (dk_deferred);
19739           objc_start_method_definition
19740            (cp_parser_objc_method_signature (parser));
19741
19742           /* For historical reasons, we accept an optional semicolon.  */
19743           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19744             cp_lexer_consume_token (parser->lexer);
19745
19746           perform_deferred_access_checks ();
19747           stop_deferring_access_checks ();
19748           meth = cp_parser_function_definition_after_declarator (parser,
19749                                                                  false);
19750           pop_deferring_access_checks ();
19751           objc_finish_method_definition (meth);
19752         }
19753       else
19754         /* Allow for interspersed non-ObjC++ code.  */
19755         cp_parser_objc_interstitial_code (parser);
19756
19757       token = cp_lexer_peek_token (parser->lexer);
19758     }
19759
19760   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19761   objc_finish_implementation ();
19762 }
19763
19764 /* Parse Objective-C ivars.  */
19765
19766 static void
19767 cp_parser_objc_class_ivars (cp_parser* parser)
19768 {
19769   cp_token *token = cp_lexer_peek_token (parser->lexer);
19770
19771   if (token->type != CPP_OPEN_BRACE)
19772     return;     /* No ivars specified.  */
19773
19774   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19775   token = cp_lexer_peek_token (parser->lexer);
19776
19777   while (token->type != CPP_CLOSE_BRACE)
19778     {
19779       cp_decl_specifier_seq declspecs;
19780       int decl_class_or_enum_p;
19781       tree prefix_attributes;
19782
19783       cp_parser_objc_visibility_spec (parser);
19784
19785       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19786         break;
19787
19788       cp_parser_decl_specifier_seq (parser,
19789                                     CP_PARSER_FLAGS_OPTIONAL,
19790                                     &declspecs,
19791                                     &decl_class_or_enum_p);
19792       prefix_attributes = declspecs.attributes;
19793       declspecs.attributes = NULL_TREE;
19794
19795       /* Keep going until we hit the `;' at the end of the
19796          declaration.  */
19797       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19798         {
19799           tree width = NULL_TREE, attributes, first_attribute, decl;
19800           cp_declarator *declarator = NULL;
19801           int ctor_dtor_or_conv_p;
19802
19803           /* Check for a (possibly unnamed) bitfield declaration.  */
19804           token = cp_lexer_peek_token (parser->lexer);
19805           if (token->type == CPP_COLON)
19806             goto eat_colon;
19807
19808           if (token->type == CPP_NAME
19809               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19810                   == CPP_COLON))
19811             {
19812               /* Get the name of the bitfield.  */
19813               declarator = make_id_declarator (NULL_TREE,
19814                                                cp_parser_identifier (parser),
19815                                                sfk_none);
19816
19817              eat_colon:
19818               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19819               /* Get the width of the bitfield.  */
19820               width
19821                 = cp_parser_constant_expression (parser,
19822                                                  /*allow_non_constant=*/false,
19823                                                  NULL);
19824             }
19825           else
19826             {
19827               /* Parse the declarator.  */
19828               declarator
19829                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19830                                         &ctor_dtor_or_conv_p,
19831                                         /*parenthesized_p=*/NULL,
19832                                         /*member_p=*/false);
19833             }
19834
19835           /* Look for attributes that apply to the ivar.  */
19836           attributes = cp_parser_attributes_opt (parser);
19837           /* Remember which attributes are prefix attributes and
19838              which are not.  */
19839           first_attribute = attributes;
19840           /* Combine the attributes.  */
19841           attributes = chainon (prefix_attributes, attributes);
19842
19843           if (width)
19844               /* Create the bitfield declaration.  */
19845               decl = grokbitfield (declarator, &declspecs,
19846                                    width,
19847                                    attributes);
19848           else
19849             decl = grokfield (declarator, &declspecs,
19850                               NULL_TREE, /*init_const_expr_p=*/false,
19851                               NULL_TREE, attributes);
19852
19853           /* Add the instance variable.  */
19854           objc_add_instance_variable (decl);
19855
19856           /* Reset PREFIX_ATTRIBUTES.  */
19857           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19858             attributes = TREE_CHAIN (attributes);
19859           if (attributes)
19860             TREE_CHAIN (attributes) = NULL_TREE;
19861
19862           token = cp_lexer_peek_token (parser->lexer);
19863
19864           if (token->type == CPP_COMMA)
19865             {
19866               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19867               continue;
19868             }
19869           break;
19870         }
19871
19872       cp_parser_consume_semicolon_at_end_of_statement (parser);
19873       token = cp_lexer_peek_token (parser->lexer);
19874     }
19875
19876   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19877   /* For historical reasons, we accept an optional semicolon.  */
19878   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19879     cp_lexer_consume_token (parser->lexer);
19880 }
19881
19882 /* Parse an Objective-C protocol declaration.  */
19883
19884 static void
19885 cp_parser_objc_protocol_declaration (cp_parser* parser)
19886 {
19887   tree proto, protorefs;
19888   cp_token *tok;
19889
19890   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19891   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19892     {
19893       tok = cp_lexer_peek_token (parser->lexer);
19894       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19895       goto finish;
19896     }
19897
19898   /* See if we have a forward declaration or a definition.  */
19899   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19900
19901   /* Try a forward declaration first.  */
19902   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19903     {
19904       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19905      finish:
19906       cp_parser_consume_semicolon_at_end_of_statement (parser);
19907     }
19908
19909   /* Ok, we got a full-fledged definition (or at least should).  */
19910   else
19911     {
19912       proto = cp_parser_identifier (parser);
19913       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19914       objc_start_protocol (proto, protorefs);
19915       cp_parser_objc_method_prototype_list (parser);
19916     }
19917 }
19918
19919 /* Parse an Objective-C superclass or category.  */
19920
19921 static void
19922 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19923                                                           tree *categ)
19924 {
19925   cp_token *next = cp_lexer_peek_token (parser->lexer);
19926
19927   *super = *categ = NULL_TREE;
19928   if (next->type == CPP_COLON)
19929     {
19930       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19931       *super = cp_parser_identifier (parser);
19932     }
19933   else if (next->type == CPP_OPEN_PAREN)
19934     {
19935       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19936       *categ = cp_parser_identifier (parser);
19937       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19938     }
19939 }
19940
19941 /* Parse an Objective-C class interface.  */
19942
19943 static void
19944 cp_parser_objc_class_interface (cp_parser* parser)
19945 {
19946   tree name, super, categ, protos;
19947
19948   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19949   name = cp_parser_identifier (parser);
19950   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19951   protos = cp_parser_objc_protocol_refs_opt (parser);
19952
19953   /* We have either a class or a category on our hands.  */
19954   if (categ)
19955     objc_start_category_interface (name, categ, protos);
19956   else
19957     {
19958       objc_start_class_interface (name, super, protos);
19959       /* Handle instance variable declarations, if any.  */
19960       cp_parser_objc_class_ivars (parser);
19961       objc_continue_interface ();
19962     }
19963
19964   cp_parser_objc_method_prototype_list (parser);
19965 }
19966
19967 /* Parse an Objective-C class implementation.  */
19968
19969 static void
19970 cp_parser_objc_class_implementation (cp_parser* parser)
19971 {
19972   tree name, super, categ;
19973
19974   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19975   name = cp_parser_identifier (parser);
19976   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19977
19978   /* We have either a class or a category on our hands.  */
19979   if (categ)
19980     objc_start_category_implementation (name, categ);
19981   else
19982     {
19983       objc_start_class_implementation (name, super);
19984       /* Handle instance variable declarations, if any.  */
19985       cp_parser_objc_class_ivars (parser);
19986       objc_continue_implementation ();
19987     }
19988
19989   cp_parser_objc_method_definition_list (parser);
19990 }
19991
19992 /* Consume the @end token and finish off the implementation.  */
19993
19994 static void
19995 cp_parser_objc_end_implementation (cp_parser* parser)
19996 {
19997   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19998   objc_finish_implementation ();
19999 }
20000
20001 /* Parse an Objective-C declaration.  */
20002
20003 static void
20004 cp_parser_objc_declaration (cp_parser* parser)
20005 {
20006   /* Try to figure out what kind of declaration is present.  */
20007   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20008
20009   switch (kwd->keyword)
20010     {
20011     case RID_AT_ALIAS:
20012       cp_parser_objc_alias_declaration (parser);
20013       break;
20014     case RID_AT_CLASS:
20015       cp_parser_objc_class_declaration (parser);
20016       break;
20017     case RID_AT_PROTOCOL:
20018       cp_parser_objc_protocol_declaration (parser);
20019       break;
20020     case RID_AT_INTERFACE:
20021       cp_parser_objc_class_interface (parser);
20022       break;
20023     case RID_AT_IMPLEMENTATION:
20024       cp_parser_objc_class_implementation (parser);
20025       break;
20026     case RID_AT_END:
20027       cp_parser_objc_end_implementation (parser);
20028       break;
20029     default:
20030       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20031              &kwd->location, kwd->u.value);
20032       cp_parser_skip_to_end_of_block_or_statement (parser);
20033     }
20034 }
20035
20036 /* Parse an Objective-C try-catch-finally statement.
20037
20038    objc-try-catch-finally-stmt:
20039      @try compound-statement objc-catch-clause-seq [opt]
20040        objc-finally-clause [opt]
20041
20042    objc-catch-clause-seq:
20043      objc-catch-clause objc-catch-clause-seq [opt]
20044
20045    objc-catch-clause:
20046      @catch ( exception-declaration ) compound-statement
20047
20048    objc-finally-clause
20049      @finally compound-statement
20050
20051    Returns NULL_TREE.  */
20052
20053 static tree
20054 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20055   location_t location;
20056   tree stmt;
20057
20058   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20059   location = cp_lexer_peek_token (parser->lexer)->location;
20060   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20061      node, lest it get absorbed into the surrounding block.  */
20062   stmt = push_stmt_list ();
20063   cp_parser_compound_statement (parser, NULL, false);
20064   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20065
20066   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20067     {
20068       cp_parameter_declarator *parmdecl;
20069       tree parm;
20070
20071       cp_lexer_consume_token (parser->lexer);
20072       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20073       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20074       parm = grokdeclarator (parmdecl->declarator,
20075                              &parmdecl->decl_specifiers,
20076                              PARM, /*initialized=*/0,
20077                              /*attrlist=*/NULL);
20078       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20079       objc_begin_catch_clause (parm);
20080       cp_parser_compound_statement (parser, NULL, false);
20081       objc_finish_catch_clause ();
20082     }
20083
20084   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20085     {
20086       cp_lexer_consume_token (parser->lexer);
20087       location = cp_lexer_peek_token (parser->lexer)->location;
20088       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20089          node, lest it get absorbed into the surrounding block.  */
20090       stmt = push_stmt_list ();
20091       cp_parser_compound_statement (parser, NULL, false);
20092       objc_build_finally_clause (location, pop_stmt_list (stmt));
20093     }
20094
20095   return objc_finish_try_stmt ();
20096 }
20097
20098 /* Parse an Objective-C synchronized statement.
20099
20100    objc-synchronized-stmt:
20101      @synchronized ( expression ) compound-statement
20102
20103    Returns NULL_TREE.  */
20104
20105 static tree
20106 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20107   location_t location;
20108   tree lock, stmt;
20109
20110   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20111
20112   location = cp_lexer_peek_token (parser->lexer)->location;
20113   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20114   lock = cp_parser_expression (parser, false, NULL);
20115   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20116
20117   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20118      node, lest it get absorbed into the surrounding block.  */
20119   stmt = push_stmt_list ();
20120   cp_parser_compound_statement (parser, NULL, false);
20121
20122   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20123 }
20124
20125 /* Parse an Objective-C throw statement.
20126
20127    objc-throw-stmt:
20128      @throw assignment-expression [opt] ;
20129
20130    Returns a constructed '@throw' statement.  */
20131
20132 static tree
20133 cp_parser_objc_throw_statement (cp_parser *parser) {
20134   tree expr = NULL_TREE;
20135
20136   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20137
20138   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20139     expr = cp_parser_assignment_expression (parser, false, NULL);
20140
20141   cp_parser_consume_semicolon_at_end_of_statement (parser);
20142
20143   return objc_build_throw_stmt (expr);
20144 }
20145
20146 /* Parse an Objective-C statement.  */
20147
20148 static tree
20149 cp_parser_objc_statement (cp_parser * parser) {
20150   /* Try to figure out what kind of declaration is present.  */
20151   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20152
20153   switch (kwd->keyword)
20154     {
20155     case RID_AT_TRY:
20156       return cp_parser_objc_try_catch_finally_statement (parser);
20157     case RID_AT_SYNCHRONIZED:
20158       return cp_parser_objc_synchronized_statement (parser);
20159     case RID_AT_THROW:
20160       return cp_parser_objc_throw_statement (parser);
20161     default:
20162       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20163              &kwd->location, kwd->u.value);
20164       cp_parser_skip_to_end_of_block_or_statement (parser);
20165     }
20166
20167   return error_mark_node;
20168 }
20169 \f
20170 /* OpenMP 2.5 parsing routines.  */
20171
20172 /* Returns name of the next clause.
20173    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20174    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20175    returned and the token is consumed.  */
20176
20177 static pragma_omp_clause
20178 cp_parser_omp_clause_name (cp_parser *parser)
20179 {
20180   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20181
20182   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20183     result = PRAGMA_OMP_CLAUSE_IF;
20184   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20185     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20186   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20187     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20188   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20189     {
20190       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20191       const char *p = IDENTIFIER_POINTER (id);
20192
20193       switch (p[0])
20194         {
20195         case 'c':
20196           if (!strcmp ("collapse", p))
20197             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20198           else if (!strcmp ("copyin", p))
20199             result = PRAGMA_OMP_CLAUSE_COPYIN;
20200           else if (!strcmp ("copyprivate", p))
20201             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20202           break;
20203         case 'f':
20204           if (!strcmp ("firstprivate", p))
20205             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20206           break;
20207         case 'l':
20208           if (!strcmp ("lastprivate", p))
20209             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20210           break;
20211         case 'n':
20212           if (!strcmp ("nowait", p))
20213             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20214           else if (!strcmp ("num_threads", p))
20215             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20216           break;
20217         case 'o':
20218           if (!strcmp ("ordered", p))
20219             result = PRAGMA_OMP_CLAUSE_ORDERED;
20220           break;
20221         case 'r':
20222           if (!strcmp ("reduction", p))
20223             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20224           break;
20225         case 's':
20226           if (!strcmp ("schedule", p))
20227             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20228           else if (!strcmp ("shared", p))
20229             result = PRAGMA_OMP_CLAUSE_SHARED;
20230           break;
20231         case 'u':
20232           if (!strcmp ("untied", p))
20233             result = PRAGMA_OMP_CLAUSE_UNTIED;
20234           break;
20235         }
20236     }
20237
20238   if (result != PRAGMA_OMP_CLAUSE_NONE)
20239     cp_lexer_consume_token (parser->lexer);
20240
20241   return result;
20242 }
20243
20244 /* Validate that a clause of the given type does not already exist.  */
20245
20246 static void
20247 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20248                            const char *name, location_t location)
20249 {
20250   tree c;
20251
20252   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20253     if (OMP_CLAUSE_CODE (c) == code)
20254       {
20255         error ("%Htoo many %qs clauses", &location, name);
20256         break;
20257       }
20258 }
20259
20260 /* OpenMP 2.5:
20261    variable-list:
20262      identifier
20263      variable-list , identifier
20264
20265    In addition, we match a closing parenthesis.  An opening parenthesis
20266    will have been consumed by the caller.
20267
20268    If KIND is nonzero, create the appropriate node and install the decl
20269    in OMP_CLAUSE_DECL and add the node to the head of the list.
20270
20271    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20272    return the list created.  */
20273
20274 static tree
20275 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20276                                 tree list)
20277 {
20278   cp_token *token;
20279   while (1)
20280     {
20281       tree name, decl;
20282
20283       token = cp_lexer_peek_token (parser->lexer);
20284       name = cp_parser_id_expression (parser, /*template_p=*/false,
20285                                       /*check_dependency_p=*/true,
20286                                       /*template_p=*/NULL,
20287                                       /*declarator_p=*/false,
20288                                       /*optional_p=*/false);
20289       if (name == error_mark_node)
20290         goto skip_comma;
20291
20292       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20293       if (decl == error_mark_node)
20294         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20295       else if (kind != 0)
20296         {
20297           tree u = build_omp_clause (kind);
20298           OMP_CLAUSE_DECL (u) = decl;
20299           OMP_CLAUSE_CHAIN (u) = list;
20300           list = u;
20301         }
20302       else
20303         list = tree_cons (decl, NULL_TREE, list);
20304
20305     get_comma:
20306       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20307         break;
20308       cp_lexer_consume_token (parser->lexer);
20309     }
20310
20311   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20312     {
20313       int ending;
20314
20315       /* Try to resync to an unnested comma.  Copied from
20316          cp_parser_parenthesized_expression_list.  */
20317     skip_comma:
20318       ending = cp_parser_skip_to_closing_parenthesis (parser,
20319                                                       /*recovering=*/true,
20320                                                       /*or_comma=*/true,
20321                                                       /*consume_paren=*/true);
20322       if (ending < 0)
20323         goto get_comma;
20324     }
20325
20326   return list;
20327 }
20328
20329 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20330    common case for omp clauses.  */
20331
20332 static tree
20333 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20334 {
20335   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20336     return cp_parser_omp_var_list_no_open (parser, kind, list);
20337   return list;
20338 }
20339
20340 /* OpenMP 3.0:
20341    collapse ( constant-expression ) */
20342
20343 static tree
20344 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20345 {
20346   tree c, num;
20347   location_t loc;
20348   HOST_WIDE_INT n;
20349
20350   loc = cp_lexer_peek_token (parser->lexer)->location;
20351   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20352     return list;
20353
20354   num = cp_parser_constant_expression (parser, false, NULL);
20355
20356   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20357     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20358                                            /*or_comma=*/false,
20359                                            /*consume_paren=*/true);
20360
20361   if (num == error_mark_node)
20362     return list;
20363   num = fold_non_dependent_expr (num);
20364   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20365       || !host_integerp (num, 0)
20366       || (n = tree_low_cst (num, 0)) <= 0
20367       || (int) n != n)
20368     {
20369       error ("%Hcollapse argument needs positive constant integer expression",
20370              &loc);
20371       return list;
20372     }
20373
20374   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20375   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20376   OMP_CLAUSE_CHAIN (c) = list;
20377   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20378
20379   return c;
20380 }
20381
20382 /* OpenMP 2.5:
20383    default ( shared | none ) */
20384
20385 static tree
20386 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20387 {
20388   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20389   tree c;
20390
20391   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20392     return list;
20393   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20394     {
20395       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20396       const char *p = IDENTIFIER_POINTER (id);
20397
20398       switch (p[0])
20399         {
20400         case 'n':
20401           if (strcmp ("none", p) != 0)
20402             goto invalid_kind;
20403           kind = OMP_CLAUSE_DEFAULT_NONE;
20404           break;
20405
20406         case 's':
20407           if (strcmp ("shared", p) != 0)
20408             goto invalid_kind;
20409           kind = OMP_CLAUSE_DEFAULT_SHARED;
20410           break;
20411
20412         default:
20413           goto invalid_kind;
20414         }
20415
20416       cp_lexer_consume_token (parser->lexer);
20417     }
20418   else
20419     {
20420     invalid_kind:
20421       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20422     }
20423
20424   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20425     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20426                                            /*or_comma=*/false,
20427                                            /*consume_paren=*/true);
20428
20429   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20430     return list;
20431
20432   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20433   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20434   OMP_CLAUSE_CHAIN (c) = list;
20435   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20436
20437   return c;
20438 }
20439
20440 /* OpenMP 2.5:
20441    if ( expression ) */
20442
20443 static tree
20444 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20445 {
20446   tree t, c;
20447
20448   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20449     return list;
20450
20451   t = cp_parser_condition (parser);
20452
20453   if (t == error_mark_node
20454       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20455     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20456                                            /*or_comma=*/false,
20457                                            /*consume_paren=*/true);
20458
20459   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20460
20461   c = build_omp_clause (OMP_CLAUSE_IF);
20462   OMP_CLAUSE_IF_EXPR (c) = t;
20463   OMP_CLAUSE_CHAIN (c) = list;
20464
20465   return c;
20466 }
20467
20468 /* OpenMP 2.5:
20469    nowait */
20470
20471 static tree
20472 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20473                              tree list, location_t location)
20474 {
20475   tree c;
20476
20477   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20478
20479   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20480   OMP_CLAUSE_CHAIN (c) = list;
20481   return c;
20482 }
20483
20484 /* OpenMP 2.5:
20485    num_threads ( expression ) */
20486
20487 static tree
20488 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20489                                   location_t location)
20490 {
20491   tree t, c;
20492
20493   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20494     return list;
20495
20496   t = cp_parser_expression (parser, false, NULL);
20497
20498   if (t == error_mark_node
20499       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20500     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20501                                            /*or_comma=*/false,
20502                                            /*consume_paren=*/true);
20503
20504   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20505                              "num_threads", location);
20506
20507   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20508   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20509   OMP_CLAUSE_CHAIN (c) = list;
20510
20511   return c;
20512 }
20513
20514 /* OpenMP 2.5:
20515    ordered */
20516
20517 static tree
20518 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20519                               tree list, location_t location)
20520 {
20521   tree c;
20522
20523   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20524                              "ordered", location);
20525
20526   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20527   OMP_CLAUSE_CHAIN (c) = list;
20528   return c;
20529 }
20530
20531 /* OpenMP 2.5:
20532    reduction ( reduction-operator : variable-list )
20533
20534    reduction-operator:
20535      One of: + * - & ^ | && || */
20536
20537 static tree
20538 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20539 {
20540   enum tree_code code;
20541   tree nlist, c;
20542
20543   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20544     return list;
20545
20546   switch (cp_lexer_peek_token (parser->lexer)->type)
20547     {
20548     case CPP_PLUS:
20549       code = PLUS_EXPR;
20550       break;
20551     case CPP_MULT:
20552       code = MULT_EXPR;
20553       break;
20554     case CPP_MINUS:
20555       code = MINUS_EXPR;
20556       break;
20557     case CPP_AND:
20558       code = BIT_AND_EXPR;
20559       break;
20560     case CPP_XOR:
20561       code = BIT_XOR_EXPR;
20562       break;
20563     case CPP_OR:
20564       code = BIT_IOR_EXPR;
20565       break;
20566     case CPP_AND_AND:
20567       code = TRUTH_ANDIF_EXPR;
20568       break;
20569     case CPP_OR_OR:
20570       code = TRUTH_ORIF_EXPR;
20571       break;
20572     default:
20573       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20574                                "%<|%>, %<&&%>, or %<||%>");
20575     resync_fail:
20576       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20577                                              /*or_comma=*/false,
20578                                              /*consume_paren=*/true);
20579       return list;
20580     }
20581   cp_lexer_consume_token (parser->lexer);
20582
20583   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20584     goto resync_fail;
20585
20586   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20587   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20588     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20589
20590   return nlist;
20591 }
20592
20593 /* OpenMP 2.5:
20594    schedule ( schedule-kind )
20595    schedule ( schedule-kind , expression )
20596
20597    schedule-kind:
20598      static | dynamic | guided | runtime | auto  */
20599
20600 static tree
20601 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20602 {
20603   tree c, t;
20604
20605   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20606     return list;
20607
20608   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20609
20610   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20611     {
20612       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20613       const char *p = IDENTIFIER_POINTER (id);
20614
20615       switch (p[0])
20616         {
20617         case 'd':
20618           if (strcmp ("dynamic", p) != 0)
20619             goto invalid_kind;
20620           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20621           break;
20622
20623         case 'g':
20624           if (strcmp ("guided", p) != 0)
20625             goto invalid_kind;
20626           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20627           break;
20628
20629         case 'r':
20630           if (strcmp ("runtime", p) != 0)
20631             goto invalid_kind;
20632           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20633           break;
20634
20635         default:
20636           goto invalid_kind;
20637         }
20638     }
20639   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20640     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20641   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20642     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20643   else
20644     goto invalid_kind;
20645   cp_lexer_consume_token (parser->lexer);
20646
20647   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20648     {
20649       cp_token *token;
20650       cp_lexer_consume_token (parser->lexer);
20651
20652       token = cp_lexer_peek_token (parser->lexer);
20653       t = cp_parser_assignment_expression (parser, false, NULL);
20654
20655       if (t == error_mark_node)
20656         goto resync_fail;
20657       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20658         error ("%Hschedule %<runtime%> does not take "
20659                "a %<chunk_size%> parameter", &token->location);
20660       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20661         error ("%Hschedule %<auto%> does not take "
20662                "a %<chunk_size%> parameter", &token->location);
20663       else
20664         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20665
20666       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20667         goto resync_fail;
20668     }
20669   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20670     goto resync_fail;
20671
20672   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20673   OMP_CLAUSE_CHAIN (c) = list;
20674   return c;
20675
20676  invalid_kind:
20677   cp_parser_error (parser, "invalid schedule kind");
20678  resync_fail:
20679   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20680                                          /*or_comma=*/false,
20681                                          /*consume_paren=*/true);
20682   return list;
20683 }
20684
20685 /* OpenMP 3.0:
20686    untied */
20687
20688 static tree
20689 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20690                              tree list, location_t location)
20691 {
20692   tree c;
20693
20694   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20695
20696   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20697   OMP_CLAUSE_CHAIN (c) = list;
20698   return c;
20699 }
20700
20701 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20702    is a bitmask in MASK.  Return the list of clauses found; the result
20703    of clause default goes in *pdefault.  */
20704
20705 static tree
20706 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20707                            const char *where, cp_token *pragma_tok)
20708 {
20709   tree clauses = NULL;
20710   bool first = true;
20711   cp_token *token = NULL;
20712
20713   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20714     {
20715       pragma_omp_clause c_kind;
20716       const char *c_name;
20717       tree prev = clauses;
20718
20719       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20720         cp_lexer_consume_token (parser->lexer);
20721
20722       token = cp_lexer_peek_token (parser->lexer);
20723       c_kind = cp_parser_omp_clause_name (parser);
20724       first = false;
20725
20726       switch (c_kind)
20727         {
20728         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20729           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20730                                                    token->location);
20731           c_name = "collapse";
20732           break;
20733         case PRAGMA_OMP_CLAUSE_COPYIN:
20734           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20735           c_name = "copyin";
20736           break;
20737         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20738           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20739                                             clauses);
20740           c_name = "copyprivate";
20741           break;
20742         case PRAGMA_OMP_CLAUSE_DEFAULT:
20743           clauses = cp_parser_omp_clause_default (parser, clauses,
20744                                                   token->location);
20745           c_name = "default";
20746           break;
20747         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20748           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20749                                             clauses);
20750           c_name = "firstprivate";
20751           break;
20752         case PRAGMA_OMP_CLAUSE_IF:
20753           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20754           c_name = "if";
20755           break;
20756         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20757           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20758                                             clauses);
20759           c_name = "lastprivate";
20760           break;
20761         case PRAGMA_OMP_CLAUSE_NOWAIT:
20762           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20763           c_name = "nowait";
20764           break;
20765         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20766           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20767                                                       token->location);
20768           c_name = "num_threads";
20769           break;
20770         case PRAGMA_OMP_CLAUSE_ORDERED:
20771           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20772                                                   token->location);
20773           c_name = "ordered";
20774           break;
20775         case PRAGMA_OMP_CLAUSE_PRIVATE:
20776           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20777                                             clauses);
20778           c_name = "private";
20779           break;
20780         case PRAGMA_OMP_CLAUSE_REDUCTION:
20781           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20782           c_name = "reduction";
20783           break;
20784         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20785           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20786                                                    token->location);
20787           c_name = "schedule";
20788           break;
20789         case PRAGMA_OMP_CLAUSE_SHARED:
20790           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20791                                             clauses);
20792           c_name = "shared";
20793           break;
20794         case PRAGMA_OMP_CLAUSE_UNTIED:
20795           clauses = cp_parser_omp_clause_untied (parser, clauses,
20796                                                  token->location);
20797           c_name = "nowait";
20798           break;
20799         default:
20800           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20801           goto saw_error;
20802         }
20803
20804       if (((mask >> c_kind) & 1) == 0)
20805         {
20806           /* Remove the invalid clause(s) from the list to avoid
20807              confusing the rest of the compiler.  */
20808           clauses = prev;
20809           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20810         }
20811     }
20812  saw_error:
20813   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20814   return finish_omp_clauses (clauses);
20815 }
20816
20817 /* OpenMP 2.5:
20818    structured-block:
20819      statement
20820
20821    In practice, we're also interested in adding the statement to an
20822    outer node.  So it is convenient if we work around the fact that
20823    cp_parser_statement calls add_stmt.  */
20824
20825 static unsigned
20826 cp_parser_begin_omp_structured_block (cp_parser *parser)
20827 {
20828   unsigned save = parser->in_statement;
20829
20830   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20831      This preserves the "not within loop or switch" style error messages
20832      for nonsense cases like
20833         void foo() {
20834         #pragma omp single
20835           break;
20836         }
20837   */
20838   if (parser->in_statement)
20839     parser->in_statement = IN_OMP_BLOCK;
20840
20841   return save;
20842 }
20843
20844 static void
20845 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20846 {
20847   parser->in_statement = save;
20848 }
20849
20850 static tree
20851 cp_parser_omp_structured_block (cp_parser *parser)
20852 {
20853   tree stmt = begin_omp_structured_block ();
20854   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20855
20856   cp_parser_statement (parser, NULL_TREE, false, NULL);
20857
20858   cp_parser_end_omp_structured_block (parser, save);
20859   return finish_omp_structured_block (stmt);
20860 }
20861
20862 /* OpenMP 2.5:
20863    # pragma omp atomic new-line
20864      expression-stmt
20865
20866    expression-stmt:
20867      x binop= expr | x++ | ++x | x-- | --x
20868    binop:
20869      +, *, -, /, &, ^, |, <<, >>
20870
20871   where x is an lvalue expression with scalar type.  */
20872
20873 static void
20874 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20875 {
20876   tree lhs, rhs;
20877   enum tree_code code;
20878
20879   cp_parser_require_pragma_eol (parser, pragma_tok);
20880
20881   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20882                                     /*cast_p=*/false, NULL);
20883   switch (TREE_CODE (lhs))
20884     {
20885     case ERROR_MARK:
20886       goto saw_error;
20887
20888     case PREINCREMENT_EXPR:
20889     case POSTINCREMENT_EXPR:
20890       lhs = TREE_OPERAND (lhs, 0);
20891       code = PLUS_EXPR;
20892       rhs = integer_one_node;
20893       break;
20894
20895     case PREDECREMENT_EXPR:
20896     case POSTDECREMENT_EXPR:
20897       lhs = TREE_OPERAND (lhs, 0);
20898       code = MINUS_EXPR;
20899       rhs = integer_one_node;
20900       break;
20901
20902     default:
20903       switch (cp_lexer_peek_token (parser->lexer)->type)
20904         {
20905         case CPP_MULT_EQ:
20906           code = MULT_EXPR;
20907           break;
20908         case CPP_DIV_EQ:
20909           code = TRUNC_DIV_EXPR;
20910           break;
20911         case CPP_PLUS_EQ:
20912           code = PLUS_EXPR;
20913           break;
20914         case CPP_MINUS_EQ:
20915           code = MINUS_EXPR;
20916           break;
20917         case CPP_LSHIFT_EQ:
20918           code = LSHIFT_EXPR;
20919           break;
20920         case CPP_RSHIFT_EQ:
20921           code = RSHIFT_EXPR;
20922           break;
20923         case CPP_AND_EQ:
20924           code = BIT_AND_EXPR;
20925           break;
20926         case CPP_OR_EQ:
20927           code = BIT_IOR_EXPR;
20928           break;
20929         case CPP_XOR_EQ:
20930           code = BIT_XOR_EXPR;
20931           break;
20932         default:
20933           cp_parser_error (parser,
20934                            "invalid operator for %<#pragma omp atomic%>");
20935           goto saw_error;
20936         }
20937       cp_lexer_consume_token (parser->lexer);
20938
20939       rhs = cp_parser_expression (parser, false, NULL);
20940       if (rhs == error_mark_node)
20941         goto saw_error;
20942       break;
20943     }
20944   finish_omp_atomic (code, lhs, rhs);
20945   cp_parser_consume_semicolon_at_end_of_statement (parser);
20946   return;
20947
20948  saw_error:
20949   cp_parser_skip_to_end_of_block_or_statement (parser);
20950 }
20951
20952
20953 /* OpenMP 2.5:
20954    # pragma omp barrier new-line  */
20955
20956 static void
20957 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20958 {
20959   cp_parser_require_pragma_eol (parser, pragma_tok);
20960   finish_omp_barrier ();
20961 }
20962
20963 /* OpenMP 2.5:
20964    # pragma omp critical [(name)] new-line
20965      structured-block  */
20966
20967 static tree
20968 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20969 {
20970   tree stmt, name = NULL;
20971
20972   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20973     {
20974       cp_lexer_consume_token (parser->lexer);
20975
20976       name = cp_parser_identifier (parser);
20977
20978       if (name == error_mark_node
20979           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20980         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20981                                                /*or_comma=*/false,
20982                                                /*consume_paren=*/true);
20983       if (name == error_mark_node)
20984         name = NULL;
20985     }
20986   cp_parser_require_pragma_eol (parser, pragma_tok);
20987
20988   stmt = cp_parser_omp_structured_block (parser);
20989   return c_finish_omp_critical (stmt, name);
20990 }
20991
20992 /* OpenMP 2.5:
20993    # pragma omp flush flush-vars[opt] new-line
20994
20995    flush-vars:
20996      ( variable-list ) */
20997
20998 static void
20999 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21000 {
21001   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21002     (void) cp_parser_omp_var_list (parser, 0, NULL);
21003   cp_parser_require_pragma_eol (parser, pragma_tok);
21004
21005   finish_omp_flush ();
21006 }
21007
21008 /* Helper function, to parse omp for increment expression.  */
21009
21010 static tree
21011 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21012 {
21013   tree lhs = cp_parser_cast_expression (parser, false, false, NULL), rhs;
21014   enum tree_code op;
21015   cp_token *token;
21016
21017   if (lhs != decl)
21018     {
21019       cp_parser_skip_to_end_of_statement (parser);
21020       return error_mark_node;
21021     }
21022
21023   token = cp_lexer_peek_token (parser->lexer);
21024   op = binops_by_token [token->type].tree_type;
21025   switch (op)
21026     {
21027     case LT_EXPR:
21028     case LE_EXPR:
21029     case GT_EXPR:
21030     case GE_EXPR:
21031       break;
21032     default:
21033       cp_parser_skip_to_end_of_statement (parser);
21034       return error_mark_node;
21035     }
21036
21037   cp_lexer_consume_token (parser->lexer);
21038   rhs = cp_parser_binary_expression (parser, false,
21039                                      PREC_RELATIONAL_EXPRESSION, NULL);
21040   if (rhs == error_mark_node
21041       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21042     {
21043       cp_parser_skip_to_end_of_statement (parser);
21044       return error_mark_node;
21045     }
21046
21047   return build2 (op, boolean_type_node, lhs, rhs);
21048 }
21049
21050 /* Helper function, to parse omp for increment expression.  */
21051
21052 static tree
21053 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21054 {
21055   cp_token *token = cp_lexer_peek_token (parser->lexer);
21056   enum tree_code op;
21057   tree lhs, rhs;
21058   cp_id_kind idk;
21059   bool decl_first;
21060
21061   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21062     {
21063       op = (token->type == CPP_PLUS_PLUS
21064             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21065       cp_lexer_consume_token (parser->lexer);
21066       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21067       if (lhs != decl)
21068         return error_mark_node;
21069       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21070     }
21071
21072   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21073   if (lhs != decl)
21074     return error_mark_node;
21075
21076   token = cp_lexer_peek_token (parser->lexer);
21077   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21078     {
21079       op = (token->type == CPP_PLUS_PLUS
21080             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21081       cp_lexer_consume_token (parser->lexer);
21082       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21083     }
21084
21085   op = cp_parser_assignment_operator_opt (parser);
21086   if (op == ERROR_MARK)
21087     return error_mark_node;
21088
21089   if (op != NOP_EXPR)
21090     {
21091       rhs = cp_parser_assignment_expression (parser, false, NULL);
21092       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21093       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21094     }
21095
21096   lhs = cp_parser_binary_expression (parser, false,
21097                                      PREC_ADDITIVE_EXPRESSION, NULL);
21098   token = cp_lexer_peek_token (parser->lexer);
21099   decl_first = lhs == decl;
21100   if (decl_first)
21101     lhs = NULL_TREE;
21102   if (token->type != CPP_PLUS
21103       && token->type != CPP_MINUS)
21104     return error_mark_node;
21105
21106   do
21107     {
21108       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21109       cp_lexer_consume_token (parser->lexer);
21110       rhs = cp_parser_binary_expression (parser, false,
21111                                          PREC_ADDITIVE_EXPRESSION, NULL);
21112       token = cp_lexer_peek_token (parser->lexer);
21113       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21114         {
21115           if (lhs == NULL_TREE)
21116             {
21117               if (op == PLUS_EXPR)
21118                 lhs = rhs;
21119               else
21120                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21121             }
21122           else
21123             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21124                                      NULL, tf_warning_or_error);
21125         }
21126     }
21127   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21128
21129   if (!decl_first)
21130     {
21131       if (rhs != decl || op == MINUS_EXPR)
21132         return error_mark_node;
21133       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21134     }
21135   else
21136     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21137
21138   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21139 }
21140
21141 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21142
21143 static tree
21144 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21145 {
21146   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21147   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21148   tree this_pre_body, cl;
21149   location_t loc_first;
21150   bool collapse_err = false;
21151   int i, collapse = 1, nbraces = 0;
21152
21153   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21154     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21155       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21156
21157   gcc_assert (collapse >= 1);
21158
21159   declv = make_tree_vec (collapse);
21160   initv = make_tree_vec (collapse);
21161   condv = make_tree_vec (collapse);
21162   incrv = make_tree_vec (collapse);
21163
21164   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21165
21166   for (i = 0; i < collapse; i++)
21167     {
21168       int bracecount = 0;
21169       bool add_private_clause = false;
21170       location_t loc;
21171
21172       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21173         {
21174           cp_parser_error (parser, "for statement expected");
21175           return NULL;
21176         }
21177       loc = cp_lexer_consume_token (parser->lexer)->location;
21178
21179       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21180         return NULL;
21181
21182       init = decl = real_decl = NULL;
21183       this_pre_body = push_stmt_list ();
21184       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21185         {
21186           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21187
21188              init-expr:
21189                        var = lb
21190                        integer-type var = lb
21191                        random-access-iterator-type var = lb
21192                        pointer-type var = lb
21193           */
21194           cp_decl_specifier_seq type_specifiers;
21195
21196           /* First, try to parse as an initialized declaration.  See
21197              cp_parser_condition, from whence the bulk of this is copied.  */
21198
21199           cp_parser_parse_tentatively (parser);
21200           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21201                                         &type_specifiers);
21202           if (cp_parser_parse_definitely (parser))
21203             {
21204               /* If parsing a type specifier seq succeeded, then this
21205                  MUST be a initialized declaration.  */
21206               tree asm_specification, attributes;
21207               cp_declarator *declarator;
21208
21209               declarator = cp_parser_declarator (parser,
21210                                                  CP_PARSER_DECLARATOR_NAMED,
21211                                                  /*ctor_dtor_or_conv_p=*/NULL,
21212                                                  /*parenthesized_p=*/NULL,
21213                                                  /*member_p=*/false);
21214               attributes = cp_parser_attributes_opt (parser);
21215               asm_specification = cp_parser_asm_specification_opt (parser);
21216
21217               if (declarator == cp_error_declarator) 
21218                 cp_parser_skip_to_end_of_statement (parser);
21219
21220               else 
21221                 {
21222                   tree pushed_scope, auto_node;
21223
21224                   decl = start_decl (declarator, &type_specifiers,
21225                                      SD_INITIALIZED, attributes,
21226                                      /*prefix_attributes=*/NULL_TREE,
21227                                      &pushed_scope);
21228
21229                   auto_node = type_uses_auto (TREE_TYPE (decl));
21230                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21231                     {
21232                       if (cp_lexer_next_token_is (parser->lexer, 
21233                                                   CPP_OPEN_PAREN))
21234                         error ("parenthesized initialization is not allowed in "
21235                                "OpenMP %<for%> loop");
21236                       else
21237                         /* Trigger an error.  */
21238                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21239
21240                       init = error_mark_node;
21241                       cp_parser_skip_to_end_of_statement (parser);
21242                     }
21243                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21244                            || type_dependent_expression_p (decl)
21245                            || auto_node)
21246                     {
21247                       bool is_direct_init, is_non_constant_init;
21248
21249                       init = cp_parser_initializer (parser,
21250                                                     &is_direct_init,
21251                                                     &is_non_constant_init);
21252
21253                       if (auto_node && describable_type (init))
21254                         {
21255                           TREE_TYPE (decl)
21256                             = do_auto_deduction (TREE_TYPE (decl), init,
21257                                                  auto_node);
21258
21259                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21260                               && !type_dependent_expression_p (decl))
21261                             goto non_class;
21262                         }
21263                       
21264                       cp_finish_decl (decl, init, !is_non_constant_init,
21265                                       asm_specification,
21266                                       LOOKUP_ONLYCONVERTING);
21267                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21268                         {
21269                           for_block
21270                             = tree_cons (NULL, this_pre_body, for_block);
21271                           init = NULL_TREE;
21272                         }
21273                       else
21274                         init = pop_stmt_list (this_pre_body);
21275                       this_pre_body = NULL_TREE;
21276                     }
21277                   else
21278                     {
21279                       /* Consume '='.  */
21280                       cp_lexer_consume_token (parser->lexer);
21281                       init = cp_parser_assignment_expression (parser, false, NULL);
21282
21283                     non_class:
21284                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21285                         init = error_mark_node;
21286                       else
21287                         cp_finish_decl (decl, NULL_TREE,
21288                                         /*init_const_expr_p=*/false,
21289                                         asm_specification,
21290                                         LOOKUP_ONLYCONVERTING);
21291                     }
21292
21293                   if (pushed_scope)
21294                     pop_scope (pushed_scope);
21295                 }
21296             }
21297           else 
21298             {
21299               cp_id_kind idk;
21300               /* If parsing a type specifier sequence failed, then
21301                  this MUST be a simple expression.  */
21302               cp_parser_parse_tentatively (parser);
21303               decl = cp_parser_primary_expression (parser, false, false,
21304                                                    false, &idk);
21305               if (!cp_parser_error_occurred (parser)
21306                   && decl
21307                   && DECL_P (decl)
21308                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21309                 {
21310                   tree rhs;
21311
21312                   cp_parser_parse_definitely (parser);
21313                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21314                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21315                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21316                                                          rhs,
21317                                                          tf_warning_or_error));
21318                   add_private_clause = true;
21319                 }
21320               else
21321                 {
21322                   decl = NULL;
21323                   cp_parser_abort_tentative_parse (parser);
21324                   init = cp_parser_expression (parser, false, NULL);
21325                   if (init)
21326                     {
21327                       if (TREE_CODE (init) == MODIFY_EXPR
21328                           || TREE_CODE (init) == MODOP_EXPR)
21329                         real_decl = TREE_OPERAND (init, 0);
21330                     }
21331                 }
21332             }
21333         }
21334       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21335       if (this_pre_body)
21336         {
21337           this_pre_body = pop_stmt_list (this_pre_body);
21338           if (pre_body)
21339             {
21340               tree t = pre_body;
21341               pre_body = push_stmt_list ();
21342               add_stmt (t);
21343               add_stmt (this_pre_body);
21344               pre_body = pop_stmt_list (pre_body);
21345             }
21346           else
21347             pre_body = this_pre_body;
21348         }
21349
21350       if (decl)
21351         real_decl = decl;
21352       if (par_clauses != NULL && real_decl != NULL_TREE)
21353         {
21354           tree *c;
21355           for (c = par_clauses; *c ; )
21356             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21357                 && OMP_CLAUSE_DECL (*c) == real_decl)
21358               {
21359                 error ("%Hiteration variable %qD should not be firstprivate",
21360                        &loc, real_decl);
21361                 *c = OMP_CLAUSE_CHAIN (*c);
21362               }
21363             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21364                      && OMP_CLAUSE_DECL (*c) == real_decl)
21365               {
21366                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21367                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21368                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21369                 OMP_CLAUSE_DECL (l) = real_decl;
21370                 OMP_CLAUSE_CHAIN (l) = clauses;
21371                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21372                 clauses = l;
21373                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21374                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21375                 add_private_clause = false;
21376               }
21377             else
21378               {
21379                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21380                     && OMP_CLAUSE_DECL (*c) == real_decl)
21381                   add_private_clause = false;
21382                 c = &OMP_CLAUSE_CHAIN (*c);
21383               }
21384         }
21385
21386       if (add_private_clause)
21387         {
21388           tree c;
21389           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21390             {
21391               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21392                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21393                   && OMP_CLAUSE_DECL (c) == decl)
21394                 break;
21395               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21396                        && OMP_CLAUSE_DECL (c) == decl)
21397                 error ("%Hiteration variable %qD should not be firstprivate",
21398                        &loc, decl);
21399               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21400                        && OMP_CLAUSE_DECL (c) == decl)
21401                 error ("%Hiteration variable %qD should not be reduction",
21402                        &loc, decl);
21403             }
21404           if (c == NULL)
21405             {
21406               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21407               OMP_CLAUSE_DECL (c) = decl;
21408               c = finish_omp_clauses (c);
21409               if (c)
21410                 {
21411                   OMP_CLAUSE_CHAIN (c) = clauses;
21412                   clauses = c;
21413                 }
21414             }
21415         }
21416
21417       cond = NULL;
21418       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21419         {
21420           /* If decl is an iterator, preserve LHS and RHS of the relational
21421              expr until finish_omp_for.  */
21422           if (decl
21423               && (type_dependent_expression_p (decl)
21424                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21425             cond = cp_parser_omp_for_cond (parser, decl);
21426           else
21427             cond = cp_parser_condition (parser);
21428         }
21429       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21430
21431       incr = NULL;
21432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21433         {
21434           /* If decl is an iterator, preserve the operator on decl
21435              until finish_omp_for.  */
21436           if (decl
21437               && (type_dependent_expression_p (decl)
21438                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21439             incr = cp_parser_omp_for_incr (parser, decl);
21440           else
21441             incr = cp_parser_expression (parser, false, NULL);
21442         }
21443
21444       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21445         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21446                                                /*or_comma=*/false,
21447                                                /*consume_paren=*/true);
21448
21449       TREE_VEC_ELT (declv, i) = decl;
21450       TREE_VEC_ELT (initv, i) = init;
21451       TREE_VEC_ELT (condv, i) = cond;
21452       TREE_VEC_ELT (incrv, i) = incr;
21453
21454       if (i == collapse - 1)
21455         break;
21456
21457       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21458          in between the collapsed for loops to be still considered perfectly
21459          nested.  Hopefully the final version clarifies this.
21460          For now handle (multiple) {'s and empty statements.  */
21461       cp_parser_parse_tentatively (parser);
21462       do
21463         {
21464           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21465             break;
21466           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21467             {
21468               cp_lexer_consume_token (parser->lexer);
21469               bracecount++;
21470             }
21471           else if (bracecount
21472                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21473             cp_lexer_consume_token (parser->lexer);
21474           else
21475             {
21476               loc = cp_lexer_peek_token (parser->lexer)->location;
21477               error ("%Hnot enough collapsed for loops", &loc);
21478               collapse_err = true;
21479               cp_parser_abort_tentative_parse (parser);
21480               declv = NULL_TREE;
21481               break;
21482             }
21483         }
21484       while (1);
21485
21486       if (declv)
21487         {
21488           cp_parser_parse_definitely (parser);
21489           nbraces += bracecount;
21490         }
21491     }
21492
21493   /* Note that we saved the original contents of this flag when we entered
21494      the structured block, and so we don't need to re-save it here.  */
21495   parser->in_statement = IN_OMP_FOR;
21496
21497   /* Note that the grammar doesn't call for a structured block here,
21498      though the loop as a whole is a structured block.  */
21499   body = push_stmt_list ();
21500   cp_parser_statement (parser, NULL_TREE, false, NULL);
21501   body = pop_stmt_list (body);
21502
21503   if (declv == NULL_TREE)
21504     ret = NULL_TREE;
21505   else
21506     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21507                           pre_body, clauses);
21508
21509   while (nbraces)
21510     {
21511       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21512         {
21513           cp_lexer_consume_token (parser->lexer);
21514           nbraces--;
21515         }
21516       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21517         cp_lexer_consume_token (parser->lexer);
21518       else
21519         {
21520           if (!collapse_err)
21521             {
21522               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21523               error ("%Hcollapsed loops not perfectly nested", &loc);
21524             }
21525           collapse_err = true;
21526           cp_parser_statement_seq_opt (parser, NULL);
21527           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21528         }
21529     }
21530
21531   while (for_block)
21532     {
21533       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21534       for_block = TREE_CHAIN (for_block);
21535     }
21536
21537   return ret;
21538 }
21539
21540 /* OpenMP 2.5:
21541    #pragma omp for for-clause[optseq] new-line
21542      for-loop  */
21543
21544 #define OMP_FOR_CLAUSE_MASK                             \
21545         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21546         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21547         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21548         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21549         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21550         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21551         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21552         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21553
21554 static tree
21555 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21556 {
21557   tree clauses, sb, ret;
21558   unsigned int save;
21559
21560   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21561                                        "#pragma omp for", pragma_tok);
21562
21563   sb = begin_omp_structured_block ();
21564   save = cp_parser_begin_omp_structured_block (parser);
21565
21566   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21567
21568   cp_parser_end_omp_structured_block (parser, save);
21569   add_stmt (finish_omp_structured_block (sb));
21570
21571   return ret;
21572 }
21573
21574 /* OpenMP 2.5:
21575    # pragma omp master new-line
21576      structured-block  */
21577
21578 static tree
21579 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21580 {
21581   cp_parser_require_pragma_eol (parser, pragma_tok);
21582   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21583 }
21584
21585 /* OpenMP 2.5:
21586    # pragma omp ordered new-line
21587      structured-block  */
21588
21589 static tree
21590 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21591 {
21592   cp_parser_require_pragma_eol (parser, pragma_tok);
21593   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21594 }
21595
21596 /* OpenMP 2.5:
21597
21598    section-scope:
21599      { section-sequence }
21600
21601    section-sequence:
21602      section-directive[opt] structured-block
21603      section-sequence section-directive structured-block  */
21604
21605 static tree
21606 cp_parser_omp_sections_scope (cp_parser *parser)
21607 {
21608   tree stmt, substmt;
21609   bool error_suppress = false;
21610   cp_token *tok;
21611
21612   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21613     return NULL_TREE;
21614
21615   stmt = push_stmt_list ();
21616
21617   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21618     {
21619       unsigned save;
21620
21621       substmt = begin_omp_structured_block ();
21622       save = cp_parser_begin_omp_structured_block (parser);
21623
21624       while (1)
21625         {
21626           cp_parser_statement (parser, NULL_TREE, false, NULL);
21627
21628           tok = cp_lexer_peek_token (parser->lexer);
21629           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21630             break;
21631           if (tok->type == CPP_CLOSE_BRACE)
21632             break;
21633           if (tok->type == CPP_EOF)
21634             break;
21635         }
21636
21637       cp_parser_end_omp_structured_block (parser, save);
21638       substmt = finish_omp_structured_block (substmt);
21639       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21640       add_stmt (substmt);
21641     }
21642
21643   while (1)
21644     {
21645       tok = cp_lexer_peek_token (parser->lexer);
21646       if (tok->type == CPP_CLOSE_BRACE)
21647         break;
21648       if (tok->type == CPP_EOF)
21649         break;
21650
21651       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21652         {
21653           cp_lexer_consume_token (parser->lexer);
21654           cp_parser_require_pragma_eol (parser, tok);
21655           error_suppress = false;
21656         }
21657       else if (!error_suppress)
21658         {
21659           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21660           error_suppress = true;
21661         }
21662
21663       substmt = cp_parser_omp_structured_block (parser);
21664       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21665       add_stmt (substmt);
21666     }
21667   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21668
21669   substmt = pop_stmt_list (stmt);
21670
21671   stmt = make_node (OMP_SECTIONS);
21672   TREE_TYPE (stmt) = void_type_node;
21673   OMP_SECTIONS_BODY (stmt) = substmt;
21674
21675   add_stmt (stmt);
21676   return stmt;
21677 }
21678
21679 /* OpenMP 2.5:
21680    # pragma omp sections sections-clause[optseq] newline
21681      sections-scope  */
21682
21683 #define OMP_SECTIONS_CLAUSE_MASK                        \
21684         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21685         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21686         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21687         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21688         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21689
21690 static tree
21691 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21692 {
21693   tree clauses, ret;
21694
21695   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21696                                        "#pragma omp sections", pragma_tok);
21697
21698   ret = cp_parser_omp_sections_scope (parser);
21699   if (ret)
21700     OMP_SECTIONS_CLAUSES (ret) = clauses;
21701
21702   return ret;
21703 }
21704
21705 /* OpenMP 2.5:
21706    # pragma parallel parallel-clause new-line
21707    # pragma parallel for parallel-for-clause new-line
21708    # pragma parallel sections parallel-sections-clause new-line  */
21709
21710 #define OMP_PARALLEL_CLAUSE_MASK                        \
21711         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21712         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21713         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21714         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21715         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21716         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21717         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21718         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21719
21720 static tree
21721 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21722 {
21723   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21724   const char *p_name = "#pragma omp parallel";
21725   tree stmt, clauses, par_clause, ws_clause, block;
21726   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21727   unsigned int save;
21728
21729   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21730     {
21731       cp_lexer_consume_token (parser->lexer);
21732       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21733       p_name = "#pragma omp parallel for";
21734       mask |= OMP_FOR_CLAUSE_MASK;
21735       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21736     }
21737   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21738     {
21739       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21740       const char *p = IDENTIFIER_POINTER (id);
21741       if (strcmp (p, "sections") == 0)
21742         {
21743           cp_lexer_consume_token (parser->lexer);
21744           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21745           p_name = "#pragma omp parallel sections";
21746           mask |= OMP_SECTIONS_CLAUSE_MASK;
21747           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21748         }
21749     }
21750
21751   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21752   block = begin_omp_parallel ();
21753   save = cp_parser_begin_omp_structured_block (parser);
21754
21755   switch (p_kind)
21756     {
21757     case PRAGMA_OMP_PARALLEL:
21758       cp_parser_statement (parser, NULL_TREE, false, NULL);
21759       par_clause = clauses;
21760       break;
21761
21762     case PRAGMA_OMP_PARALLEL_FOR:
21763       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21764       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21765       break;
21766
21767     case PRAGMA_OMP_PARALLEL_SECTIONS:
21768       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21769       stmt = cp_parser_omp_sections_scope (parser);
21770       if (stmt)
21771         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21772       break;
21773
21774     default:
21775       gcc_unreachable ();
21776     }
21777
21778   cp_parser_end_omp_structured_block (parser, save);
21779   stmt = finish_omp_parallel (par_clause, block);
21780   if (p_kind != PRAGMA_OMP_PARALLEL)
21781     OMP_PARALLEL_COMBINED (stmt) = 1;
21782   return stmt;
21783 }
21784
21785 /* OpenMP 2.5:
21786    # pragma omp single single-clause[optseq] new-line
21787      structured-block  */
21788
21789 #define OMP_SINGLE_CLAUSE_MASK                          \
21790         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21791         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21792         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21793         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21794
21795 static tree
21796 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21797 {
21798   tree stmt = make_node (OMP_SINGLE);
21799   TREE_TYPE (stmt) = void_type_node;
21800
21801   OMP_SINGLE_CLAUSES (stmt)
21802     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21803                                  "#pragma omp single", pragma_tok);
21804   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21805
21806   return add_stmt (stmt);
21807 }
21808
21809 /* OpenMP 3.0:
21810    # pragma omp task task-clause[optseq] new-line
21811      structured-block  */
21812
21813 #define OMP_TASK_CLAUSE_MASK                            \
21814         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21815         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21816         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21817         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21818         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21819         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21820
21821 static tree
21822 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21823 {
21824   tree clauses, block;
21825   unsigned int save;
21826
21827   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21828                                        "#pragma omp task", pragma_tok);
21829   block = begin_omp_task ();
21830   save = cp_parser_begin_omp_structured_block (parser);
21831   cp_parser_statement (parser, NULL_TREE, false, NULL);
21832   cp_parser_end_omp_structured_block (parser, save);
21833   return finish_omp_task (clauses, block);
21834 }
21835
21836 /* OpenMP 3.0:
21837    # pragma omp taskwait new-line  */
21838
21839 static void
21840 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21841 {
21842   cp_parser_require_pragma_eol (parser, pragma_tok);
21843   finish_omp_taskwait ();
21844 }
21845
21846 /* OpenMP 2.5:
21847    # pragma omp threadprivate (variable-list) */
21848
21849 static void
21850 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21851 {
21852   tree vars;
21853
21854   vars = cp_parser_omp_var_list (parser, 0, NULL);
21855   cp_parser_require_pragma_eol (parser, pragma_tok);
21856
21857   finish_omp_threadprivate (vars);
21858 }
21859
21860 /* Main entry point to OpenMP statement pragmas.  */
21861
21862 static void
21863 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21864 {
21865   tree stmt;
21866
21867   switch (pragma_tok->pragma_kind)
21868     {
21869     case PRAGMA_OMP_ATOMIC:
21870       cp_parser_omp_atomic (parser, pragma_tok);
21871       return;
21872     case PRAGMA_OMP_CRITICAL:
21873       stmt = cp_parser_omp_critical (parser, pragma_tok);
21874       break;
21875     case PRAGMA_OMP_FOR:
21876       stmt = cp_parser_omp_for (parser, pragma_tok);
21877       break;
21878     case PRAGMA_OMP_MASTER:
21879       stmt = cp_parser_omp_master (parser, pragma_tok);
21880       break;
21881     case PRAGMA_OMP_ORDERED:
21882       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21883       break;
21884     case PRAGMA_OMP_PARALLEL:
21885       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21886       break;
21887     case PRAGMA_OMP_SECTIONS:
21888       stmt = cp_parser_omp_sections (parser, pragma_tok);
21889       break;
21890     case PRAGMA_OMP_SINGLE:
21891       stmt = cp_parser_omp_single (parser, pragma_tok);
21892       break;
21893     case PRAGMA_OMP_TASK:
21894       stmt = cp_parser_omp_task (parser, pragma_tok);
21895       break;
21896     default:
21897       gcc_unreachable ();
21898     }
21899
21900   if (stmt)
21901     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21902 }
21903 \f
21904 /* The parser.  */
21905
21906 static GTY (()) cp_parser *the_parser;
21907
21908 \f
21909 /* Special handling for the first token or line in the file.  The first
21910    thing in the file might be #pragma GCC pch_preprocess, which loads a
21911    PCH file, which is a GC collection point.  So we need to handle this
21912    first pragma without benefit of an existing lexer structure.
21913
21914    Always returns one token to the caller in *FIRST_TOKEN.  This is
21915    either the true first token of the file, or the first token after
21916    the initial pragma.  */
21917
21918 static void
21919 cp_parser_initial_pragma (cp_token *first_token)
21920 {
21921   tree name = NULL;
21922
21923   cp_lexer_get_preprocessor_token (NULL, first_token);
21924   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21925     return;
21926
21927   cp_lexer_get_preprocessor_token (NULL, first_token);
21928   if (first_token->type == CPP_STRING)
21929     {
21930       name = first_token->u.value;
21931
21932       cp_lexer_get_preprocessor_token (NULL, first_token);
21933       if (first_token->type != CPP_PRAGMA_EOL)
21934         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21935                &first_token->location);
21936     }
21937   else
21938     error ("%Hexpected string literal", &first_token->location);
21939
21940   /* Skip to the end of the pragma.  */
21941   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21942     cp_lexer_get_preprocessor_token (NULL, first_token);
21943
21944   /* Now actually load the PCH file.  */
21945   if (name)
21946     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21947
21948   /* Read one more token to return to our caller.  We have to do this
21949      after reading the PCH file in, since its pointers have to be
21950      live.  */
21951   cp_lexer_get_preprocessor_token (NULL, first_token);
21952 }
21953
21954 /* Normal parsing of a pragma token.  Here we can (and must) use the
21955    regular lexer.  */
21956
21957 static bool
21958 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21959 {
21960   cp_token *pragma_tok;
21961   unsigned int id;
21962
21963   pragma_tok = cp_lexer_consume_token (parser->lexer);
21964   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21965   parser->lexer->in_pragma = true;
21966
21967   id = pragma_tok->pragma_kind;
21968   switch (id)
21969     {
21970     case PRAGMA_GCC_PCH_PREPROCESS:
21971       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21972              &pragma_tok->location);
21973       break;
21974
21975     case PRAGMA_OMP_BARRIER:
21976       switch (context)
21977         {
21978         case pragma_compound:
21979           cp_parser_omp_barrier (parser, pragma_tok);
21980           return false;
21981         case pragma_stmt:
21982           error ("%H%<#pragma omp barrier%> may only be "
21983                  "used in compound statements", &pragma_tok->location);
21984           break;
21985         default:
21986           goto bad_stmt;
21987         }
21988       break;
21989
21990     case PRAGMA_OMP_FLUSH:
21991       switch (context)
21992         {
21993         case pragma_compound:
21994           cp_parser_omp_flush (parser, pragma_tok);
21995           return false;
21996         case pragma_stmt:
21997           error ("%H%<#pragma omp flush%> may only be "
21998                  "used in compound statements", &pragma_tok->location);
21999           break;
22000         default:
22001           goto bad_stmt;
22002         }
22003       break;
22004
22005     case PRAGMA_OMP_TASKWAIT:
22006       switch (context)
22007         {
22008         case pragma_compound:
22009           cp_parser_omp_taskwait (parser, pragma_tok);
22010           return false;
22011         case pragma_stmt:
22012           error ("%H%<#pragma omp taskwait%> may only be "
22013                  "used in compound statements",
22014                  &pragma_tok->location);
22015           break;
22016         default:
22017           goto bad_stmt;
22018         }
22019       break;
22020
22021     case PRAGMA_OMP_THREADPRIVATE:
22022       cp_parser_omp_threadprivate (parser, pragma_tok);
22023       return false;
22024
22025     case PRAGMA_OMP_ATOMIC:
22026     case PRAGMA_OMP_CRITICAL:
22027     case PRAGMA_OMP_FOR:
22028     case PRAGMA_OMP_MASTER:
22029     case PRAGMA_OMP_ORDERED:
22030     case PRAGMA_OMP_PARALLEL:
22031     case PRAGMA_OMP_SECTIONS:
22032     case PRAGMA_OMP_SINGLE:
22033     case PRAGMA_OMP_TASK:
22034       if (context == pragma_external)
22035         goto bad_stmt;
22036       cp_parser_omp_construct (parser, pragma_tok);
22037       return true;
22038
22039     case PRAGMA_OMP_SECTION:
22040       error ("%H%<#pragma omp section%> may only be used in "
22041              "%<#pragma omp sections%> construct", &pragma_tok->location);
22042       break;
22043
22044     default:
22045       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22046       c_invoke_pragma_handler (id);
22047       break;
22048
22049     bad_stmt:
22050       cp_parser_error (parser, "expected declaration specifiers");
22051       break;
22052     }
22053
22054   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22055   return false;
22056 }
22057
22058 /* The interface the pragma parsers have to the lexer.  */
22059
22060 enum cpp_ttype
22061 pragma_lex (tree *value)
22062 {
22063   cp_token *tok;
22064   enum cpp_ttype ret;
22065
22066   tok = cp_lexer_peek_token (the_parser->lexer);
22067
22068   ret = tok->type;
22069   *value = tok->u.value;
22070
22071   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22072     ret = CPP_EOF;
22073   else if (ret == CPP_STRING)
22074     *value = cp_parser_string_literal (the_parser, false, false);
22075   else
22076     {
22077       cp_lexer_consume_token (the_parser->lexer);
22078       if (ret == CPP_KEYWORD)
22079         ret = CPP_NAME;
22080     }
22081
22082   return ret;
22083 }
22084
22085 \f
22086 /* External interface.  */
22087
22088 /* Parse one entire translation unit.  */
22089
22090 void
22091 c_parse_file (void)
22092 {
22093   bool error_occurred;
22094   static bool already_called = false;
22095
22096   if (already_called)
22097     {
22098       sorry ("inter-module optimizations not implemented for C++");
22099       return;
22100     }
22101   already_called = true;
22102
22103   the_parser = cp_parser_new ();
22104   push_deferring_access_checks (flag_access_control
22105                                 ? dk_no_deferred : dk_no_check);
22106   error_occurred = cp_parser_translation_unit (the_parser);
22107   the_parser = NULL;
22108 }
22109
22110 #include "gt-cp-parser.h"