OSDN Git Service

806d6f9645aa35ecc87548c2cb63a0a443c2b9e5
[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 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
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 GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Create a new main C++ lexer, the lexer that gets tokens from the
251    preprocessor.  */
252
253 static cp_lexer *
254 cp_lexer_new_main (void)
255 {
256   cp_token first_token;
257   cp_lexer *lexer;
258   cp_token *pos;
259   size_t alloc;
260   size_t space;
261   cp_token *buffer;
262
263   /* It's possible that parsing the first pragma will load a PCH file,
264      which is a GC collection point.  So we have to do that before
265      allocating any memory.  */
266   cp_parser_initial_pragma (&first_token);
267
268   c_common_no_more_pch ();
269
270   /* Allocate the memory.  */
271   lexer = GGC_CNEW (cp_lexer);
272
273 #ifdef ENABLE_CHECKING
274   /* Initially we are not debugging.  */
275   lexer->debugging_p = false;
276 #endif /* ENABLE_CHECKING */
277   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
278                                    CP_SAVED_TOKEN_STACK);
279
280   /* Create the buffer.  */
281   alloc = CP_LEXER_BUFFER_SIZE;
282   buffer = GGC_NEWVEC (cp_token, alloc);
283
284   /* Put the first token in the buffer.  */
285   space = alloc;
286   pos = buffer;
287   *pos = first_token;
288
289   /* Get the remaining tokens from the preprocessor.  */
290   while (pos->type != CPP_EOF)
291     {
292       pos++;
293       if (!--space)
294         {
295           space = alloc;
296           alloc *= 2;
297           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
298           pos = buffer + space;
299         }
300       cp_lexer_get_preprocessor_token (lexer, pos);
301     }
302   lexer->buffer = buffer;
303   lexer->buffer_length = alloc - space;
304   lexer->last_token = pos;
305   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
306
307   /* Subsequent preprocessor diagnostics should use compiler
308      diagnostic functions to get the compiler source location.  */
309   done_lexing = true;
310
311   gcc_assert (lexer->next_token->type != CPP_PURGED);
312   return lexer;
313 }
314
315 /* Create a new lexer whose token stream is primed with the tokens in
316    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
317
318 static cp_lexer *
319 cp_lexer_new_from_tokens (cp_token_cache *cache)
320 {
321   cp_token *first = cache->first;
322   cp_token *last = cache->last;
323   cp_lexer *lexer = GGC_CNEW (cp_lexer);
324
325   /* We do not own the buffer.  */
326   lexer->buffer = NULL;
327   lexer->buffer_length = 0;
328   lexer->next_token = first == last ? &eof_token : first;
329   lexer->last_token = last;
330
331   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
332                                    CP_SAVED_TOKEN_STACK);
333
334 #ifdef ENABLE_CHECKING
335   /* Initially we are not debugging.  */
336   lexer->debugging_p = false;
337 #endif
338
339   gcc_assert (lexer->next_token->type != CPP_PURGED);
340   return lexer;
341 }
342
343 /* Frees all resources associated with LEXER.  */
344
345 static void
346 cp_lexer_destroy (cp_lexer *lexer)
347 {
348   if (lexer->buffer)
349     ggc_free (lexer->buffer);
350   VEC_free (cp_token_position, heap, lexer->saved_tokens);
351   ggc_free (lexer);
352 }
353
354 /* Returns nonzero if debugging information should be output.  */
355
356 #ifdef ENABLE_CHECKING
357
358 static inline bool
359 cp_lexer_debugging_p (cp_lexer *lexer)
360 {
361   return lexer->debugging_p;
362 }
363
364 #endif /* ENABLE_CHECKING */
365
366 static inline cp_token_position
367 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
368 {
369   gcc_assert (!previous_p || lexer->next_token != &eof_token);
370
371   return lexer->next_token - previous_p;
372 }
373
374 static inline cp_token *
375 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
376 {
377   return pos;
378 }
379
380 /* nonzero if we are presently saving tokens.  */
381
382 static inline int
383 cp_lexer_saving_tokens (const cp_lexer* lexer)
384 {
385   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 }
387
388 /* Store the next token from the preprocessor in *TOKEN.  Return true
389    if we reach EOF.  If LEXER is NULL, assume we are handling an
390    initial #pragma pch_preprocess, and thus want the lexer to return
391    processed strings.  */
392
393 static void
394 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
395 {
396   static int is_extern_c = 0;
397
398    /* Get a new token from the preprocessor.  */
399   token->type
400     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
401                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
402   token->keyword = RID_MAX;
403   token->pragma_kind = PRAGMA_NONE;
404
405   /* On some systems, some header files are surrounded by an
406      implicit extern "C" block.  Set a flag in the token if it
407      comes from such a header.  */
408   is_extern_c += pending_lang_change;
409   pending_lang_change = 0;
410   token->implicit_extern_c = is_extern_c > 0;
411
412   /* Check to see if this token is a keyword.  */
413   if (token->type == CPP_NAME)
414     {
415       if (C_IS_RESERVED_WORD (token->u.value))
416         {
417           /* Mark this token as a keyword.  */
418           token->type = CPP_KEYWORD;
419           /* Record which keyword.  */
420           token->keyword = C_RID_CODE (token->u.value);
421           /* Update the value.  Some keywords are mapped to particular
422              entities, rather than simply having the value of the
423              corresponding IDENTIFIER_NODE.  For example, `__const' is
424              mapped to `const'.  */
425           token->u.value = ridpointers[token->keyword];
426         }
427       else
428         {
429           if (warn_cxx0x_compat
430               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432             {
433               /* Warn about the C++0x keyword (but still treat it as
434                  an identifier).  */
435               warning (OPT_Wc__0x_compat, 
436                        "identifier %<%s%> will become a keyword in C++0x",
437                        IDENTIFIER_POINTER (token->u.value));
438
439               /* Clear out the C_RID_CODE so we don't warn about this
440                  particular identifier-turned-keyword again.  */
441               C_SET_RID_CODE (token->u.value, RID_MAX);
442             }
443
444           token->ambiguous_p = false;
445           token->keyword = RID_MAX;
446         }
447     }
448   /* Handle Objective-C++ keywords.  */
449   else if (token->type == CPP_AT_NAME)
450     {
451       token->type = CPP_KEYWORD;
452       switch (C_RID_CODE (token->u.value))
453         {
454         /* Map 'class' to '@class', 'private' to '@private', etc.  */
455         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459         case RID_THROW: token->keyword = RID_AT_THROW; break;
460         case RID_TRY: token->keyword = RID_AT_TRY; break;
461         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462         default: token->keyword = C_RID_CODE (token->u.value);
463         }
464     }
465   else if (token->type == CPP_PRAGMA)
466     {
467       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
468       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
469       token->u.value = NULL_TREE;
470     }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN.  */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477   if (token->type != CPP_EOF)
478     {
479       input_location = token->location;
480     }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484    consume it.  */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489   if (cp_lexer_debugging_p (lexer))
490     {
491       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493       putc ('\n', cp_lexer_debug_stream);
494     }
495   return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE.  */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503   return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE.  */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511   return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD.  */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519   return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD.  */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527   return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier.  */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535   cp_token *token;
536
537   token = cp_lexer_peek_token (lexer);
538   switch (token->keyword) 
539     {
540       /* auto specifier: storage-class-specifier in C++,
541          simple-type-specifier in C++0x.  */
542     case RID_AUTO:
543       /* Storage classes.  */
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_CHAR16:
558     case RID_CHAR32:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_STRING16:
793     case CPP_STRING32:
794     case CPP_WSTRING:
795       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796       break;
797
798     default:
799       break;
800     }
801 }
802
803 /* Start emitting debugging information.  */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808   lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information.  */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816   lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens.  */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826   cp_token_cache *cache = GGC_NEW (cp_token_cache);
827   cache->first = first;
828   cache->last = last;
829   return cache;
830 }
831
832 \f
833 /* Decl-specifiers.  */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators.  */
844
845 /* Nothing other than the parser should be creating declarators;
846    declarators are a semi-syntactic representation of C++ entities.
847    Other parts of the front end that need to create entities (like
848    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
849
850 static cp_declarator *make_call_declarator
851   (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853   (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855   (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857   (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859   (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861   (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator.  */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867    allocated.  */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool.  */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875   return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND.  Clear fields that are
879    common to all declarators.  */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884   cp_declarator *declarator;
885
886   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887   declarator->kind = kind;
888   declarator->attributes = NULL_TREE;
889   declarator->declarator = NULL;
890   declarator->parameter_pack_p = false;
891
892   return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier.  If
896    QUALIFYING_SCOPE is non-NULL, the identifier is
897    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
899    is, if any.   */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903                     special_function_kind sfk)
904 {
905   cp_declarator *declarator;
906
907   /* It is valid to write:
908
909        class C { void f(); };
910        typedef C D;
911        void D::f();
912
913      The standard is not clear about whether `typedef const C D' is
914      legal; as of 2002-09-15 the committee is considering that
915      question.  EDG 3.0 allows that syntax.  Therefore, we do as
916      well.  */
917   if (qualifying_scope && TYPE_P (qualifying_scope))
918     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924   declarator = make_declarator (cdk_id);
925   declarator->u.id.qualifying_scope = qualifying_scope;
926   declarator->u.id.unqualified_name = unqualified_name;
927   declarator->u.id.sfk = sfk;
928   
929   return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
933    of modifiers such as const or volatile to apply to the pointer
934    type, represented as identifiers.  */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_pointer);
942   declarator->declarator = target;
943   declarator->u.pointer.qualifiers = cv_qualifiers;
944   declarator->u.pointer.class_type = NULL_TREE;
945   if (target)
946     {
947       declarator->parameter_pack_p = target->parameter_pack_p;
948       target->parameter_pack_p = false;
949     }
950   else
951     declarator->parameter_pack_p = false;
952
953   return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references.  */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960                            bool rvalue_ref)
961 {
962   cp_declarator *declarator;
963
964   declarator = make_declarator (cdk_reference);
965   declarator->declarator = target;
966   declarator->u.reference.qualifiers = cv_qualifiers;
967   declarator->u.reference.rvalue_ref = rvalue_ref;
968   if (target)
969     {
970       declarator->parameter_pack_p = target->parameter_pack_p;
971       target->parameter_pack_p = false;
972     }
973   else
974     declarator->parameter_pack_p = false;
975
976   return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980    member of CLASS_TYPE.  */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984                         cp_declarator *pointee)
985 {
986   cp_declarator *declarator;
987
988   declarator = make_declarator (cdk_ptrmem);
989   declarator->declarator = pointee;
990   declarator->u.pointer.qualifiers = cv_qualifiers;
991   declarator->u.pointer.class_type = class_type;
992
993   if (pointee)
994     {
995       declarator->parameter_pack_p = pointee->parameter_pack_p;
996       pointee->parameter_pack_p = false;
997     }
998   else
999     declarator->parameter_pack_p = false;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1006    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1007    indicates what exceptions can be thrown.  */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011                       tree parms,
1012                       cp_cv_quals cv_qualifiers,
1013                       tree exception_specification,
1014                       tree late_return_type)
1015 {
1016   cp_declarator *declarator;
1017
1018   declarator = make_declarator (cdk_function);
1019   declarator->declarator = target;
1020   declarator->u.function.parameters = parms;
1021   declarator->u.function.qualifiers = cv_qualifiers;
1022   declarator->u.function.exception_specification = exception_specification;
1023   declarator->u.function.late_return_type = late_return_type;
1024   if (target)
1025     {
1026       declarator->parameter_pack_p = target->parameter_pack_p;
1027       target->parameter_pack_p = false;
1028     }
1029   else
1030     declarator->parameter_pack_p = false;
1031
1032   return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036    defined by ELEMENT.  */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041   cp_declarator *declarator;
1042
1043   declarator = make_declarator (cdk_array);
1044   declarator->declarator = element;
1045   declarator->u.array.bounds = bounds;
1046   if (element)
1047     {
1048       declarator->parameter_pack_p = element->parameter_pack_p;
1049       element->parameter_pack_p = false;
1050     }
1051   else
1052     declarator->parameter_pack_p = false;
1053
1054   return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058    parameter pack, when followed by an ellipsis.  */
1059 static bool 
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062   /* Search for a declarator name, or any other declarator that goes
1063      after the point where the ellipsis could appear in a parameter
1064      pack. If we find any of these, then this declarator can not be
1065      made into a parameter pack.  */
1066   bool found = false;
1067   while (declarator && !found)
1068     {
1069       switch ((int)declarator->kind)
1070         {
1071         case cdk_id:
1072         case cdk_array:
1073           found = true;
1074           break;
1075
1076         case cdk_error:
1077           return true;
1078
1079         default:
1080           declarator = declarator->declarator;
1081           break;
1082         }
1083     }
1084
1085   return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091    DECLARATOR and DEFAULT_ARGUMENT.  */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095                            cp_declarator *declarator,
1096                            tree default_argument)
1097 {
1098   cp_parameter_declarator *parameter;
1099
1100   parameter = ((cp_parameter_declarator *)
1101                alloc_declarator (sizeof (cp_parameter_declarator)));
1102   parameter->next = NULL;
1103   if (decl_specifiers)
1104     parameter->decl_specifiers = *decl_specifiers;
1105   else
1106     clear_decl_specs (&parameter->decl_specifiers);
1107   parameter->declarator = declarator;
1108   parameter->default_argument = default_argument;
1109   parameter->ellipsis_p = false;
1110
1111   return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119   while (declarator)
1120     {
1121       if (declarator->kind == cdk_function
1122           && declarator->declarator->kind == cdk_id)
1123         return true;
1124       if (declarator->kind == cdk_id
1125           || declarator->kind == cdk_error)
1126         return false;
1127       declarator = declarator->declarator;
1128     }
1129   return false;
1130 }
1131  
1132 /* The parser.  */
1133
1134 /* Overview
1135    --------
1136
1137    A cp_parser parses the token stream as specified by the C++
1138    grammar.  Its job is purely parsing, not semantic analysis.  For
1139    example, the parser breaks the token stream into declarators,
1140    expressions, statements, and other similar syntactic constructs.
1141    It does not check that the types of the expressions on either side
1142    of an assignment-statement are compatible, or that a function is
1143    not declared with a parameter of type `void'.
1144
1145    The parser invokes routines elsewhere in the compiler to perform
1146    semantic analysis and to build up the abstract syntax tree for the
1147    code processed.
1148
1149    The parser (and the template instantiation code, which is, in a
1150    way, a close relative of parsing) are the only parts of the
1151    compiler that should be calling push_scope and pop_scope, or
1152    related functions.  The parser (and template instantiation code)
1153    keeps track of what scope is presently active; everything else
1154    should simply honor that.  (The code that generates static
1155    initializers may also need to set the scope, in order to check
1156    access control correctly when emitting the initializers.)
1157
1158    Methodology
1159    -----------
1160
1161    The parser is of the standard recursive-descent variety.  Upcoming
1162    tokens in the token stream are examined in order to determine which
1163    production to use when parsing a non-terminal.  Some C++ constructs
1164    require arbitrary look ahead to disambiguate.  For example, it is
1165    impossible, in the general case, to tell whether a statement is an
1166    expression or declaration without scanning the entire statement.
1167    Therefore, the parser is capable of "parsing tentatively."  When the
1168    parser is not sure what construct comes next, it enters this mode.
1169    Then, while we attempt to parse the construct, the parser queues up
1170    error messages, rather than issuing them immediately, and saves the
1171    tokens it consumes.  If the construct is parsed successfully, the
1172    parser "commits", i.e., it issues any queued error messages and
1173    the tokens that were being preserved are permanently discarded.
1174    If, however, the construct is not parsed successfully, the parser
1175    rolls back its state completely so that it can resume parsing using
1176    a different alternative.
1177
1178    Future Improvements
1179    -------------------
1180
1181    The performance of the parser could probably be improved substantially.
1182    We could often eliminate the need to parse tentatively by looking ahead
1183    a little bit.  In some places, this approach might not entirely eliminate
1184    the need to parse tentatively, but it might still speed up the average
1185    case.  */
1186
1187 /* Flags that are passed to some parsing functions.  These values can
1188    be bitwise-ored together.  */
1189
1190 typedef enum cp_parser_flags
1191 {
1192   /* No flags.  */
1193   CP_PARSER_FLAGS_NONE = 0x0,
1194   /* The construct is optional.  If it is not present, then no error
1195      should be issued.  */
1196   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197   /* When parsing a type-specifier, do not allow user-defined types.  */
1198   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 } cp_parser_flags;
1200
1201 /* The different kinds of declarators we want to parse.  */
1202
1203 typedef enum cp_parser_declarator_kind
1204 {
1205   /* We want an abstract declarator.  */
1206   CP_PARSER_DECLARATOR_ABSTRACT,
1207   /* We want a named declarator.  */
1208   CP_PARSER_DECLARATOR_NAMED,
1209   /* We don't mind, but the name must be an unqualified-id.  */
1210   CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1212
1213 /* The precedence values used to parse binary expressions.  The minimum value
1214    of PREC must be 1, because zero is reserved to quickly discriminate
1215    binary operators from other tokens.  */
1216
1217 enum cp_parser_prec
1218 {
1219   PREC_NOT_OPERATOR,
1220   PREC_LOGICAL_OR_EXPRESSION,
1221   PREC_LOGICAL_AND_EXPRESSION,
1222   PREC_INCLUSIVE_OR_EXPRESSION,
1223   PREC_EXCLUSIVE_OR_EXPRESSION,
1224   PREC_AND_EXPRESSION,
1225   PREC_EQUALITY_EXPRESSION,
1226   PREC_RELATIONAL_EXPRESSION,
1227   PREC_SHIFT_EXPRESSION,
1228   PREC_ADDITIVE_EXPRESSION,
1229   PREC_MULTIPLICATIVE_EXPRESSION,
1230   PREC_PM_EXPRESSION,
1231   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232 };
1233
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235    precedence value.  */
1236
1237 typedef struct cp_parser_binary_operations_map_node
1238 {
1239   /* The token type.  */
1240   enum cpp_ttype token_type;
1241   /* The corresponding tree code.  */
1242   enum tree_code tree_type;
1243   /* The precedence of this operator.  */
1244   enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1246
1247 /* The status of a tentative parse.  */
1248
1249 typedef enum cp_parser_status_kind
1250 {
1251   /* No errors have occurred.  */
1252   CP_PARSER_STATUS_KIND_NO_ERROR,
1253   /* An error has occurred.  */
1254   CP_PARSER_STATUS_KIND_ERROR,
1255   /* We are committed to this tentative parse, whether or not an error
1256      has occurred.  */
1257   CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1259
1260 typedef struct cp_parser_expression_stack_entry
1261 {
1262   /* Left hand side of the binary operation we are currently
1263      parsing.  */
1264   tree lhs;
1265   /* Original tree code for left hand side, if it was a binary
1266      expression itself (used for -Wparentheses).  */
1267   enum tree_code lhs_type;
1268   /* Tree code for the binary operation we are parsing.  */
1269   enum tree_code tree_type;
1270   /* Precedence of the binary operation we are parsing.  */
1271   int prec;
1272 } cp_parser_expression_stack_entry;
1273
1274 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1275    entries because precedence levels on the stack are monotonically
1276    increasing.  */
1277 typedef struct cp_parser_expression_stack_entry
1278   cp_parser_expression_stack[NUM_PREC_VALUES];
1279
1280 /* Context that is saved and restored when parsing tentatively.  */
1281 typedef struct GTY (()) cp_parser_context {
1282   /* If this is a tentative parsing context, the status of the
1283      tentative parse.  */
1284   enum cp_parser_status_kind status;
1285   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1286      that are looked up in this context must be looked up both in the
1287      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1288      the context of the containing expression.  */
1289   tree object_type;
1290
1291   /* The next parsing context in the stack.  */
1292   struct cp_parser_context *next;
1293 } cp_parser_context;
1294
1295 /* Prototypes.  */
1296
1297 /* Constructors and destructors.  */
1298
1299 static cp_parser_context *cp_parser_context_new
1300   (cp_parser_context *);
1301
1302 /* Class variables.  */
1303
1304 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1305
1306 /* The operator-precedence table used by cp_parser_binary_expression.
1307    Transformed into an associative array (binops_by_token) by
1308    cp_parser_new.  */
1309
1310 static const cp_parser_binary_operations_map_node binops[] = {
1311   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1312   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1313
1314   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317
1318   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320
1321   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323
1324   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328
1329   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1330   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1331
1332   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1333
1334   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1335
1336   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1337
1338   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1339
1340   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1341 };
1342
1343 /* The same as binops, but initialized by cp_parser_new so that
1344    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1345    for speed.  */
1346 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1347
1348 /* Constructors and destructors.  */
1349
1350 /* Construct a new context.  The context below this one on the stack
1351    is given by NEXT.  */
1352
1353 static cp_parser_context *
1354 cp_parser_context_new (cp_parser_context* next)
1355 {
1356   cp_parser_context *context;
1357
1358   /* Allocate the storage.  */
1359   if (cp_parser_context_free_list != NULL)
1360     {
1361       /* Pull the first entry from the free list.  */
1362       context = cp_parser_context_free_list;
1363       cp_parser_context_free_list = context->next;
1364       memset (context, 0, sizeof (*context));
1365     }
1366   else
1367     context = GGC_CNEW (cp_parser_context);
1368
1369   /* No errors have occurred yet in this context.  */
1370   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1371   /* If this is not the bottommost context, copy information that we
1372      need from the previous context.  */
1373   if (next)
1374     {
1375       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1376          expression, then we are parsing one in this context, too.  */
1377       context->object_type = next->object_type;
1378       /* Thread the stack.  */
1379       context->next = next;
1380     }
1381
1382   return context;
1383 }
1384
1385 /* The cp_parser structure represents the C++ parser.  */
1386
1387 typedef struct GTY(()) cp_parser {
1388   /* The lexer from which we are obtaining tokens.  */
1389   cp_lexer *lexer;
1390
1391   /* The scope in which names should be looked up.  If NULL_TREE, then
1392      we look up names in the scope that is currently open in the
1393      source program.  If non-NULL, this is either a TYPE or
1394      NAMESPACE_DECL for the scope in which we should look.  It can
1395      also be ERROR_MARK, when we've parsed a bogus scope.
1396
1397      This value is not cleared automatically after a name is looked
1398      up, so we must be careful to clear it before starting a new look
1399      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1400      will look up `Z' in the scope of `X', rather than the current
1401      scope.)  Unfortunately, it is difficult to tell when name lookup
1402      is complete, because we sometimes peek at a token, look it up,
1403      and then decide not to consume it.   */
1404   tree scope;
1405
1406   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407      last lookup took place.  OBJECT_SCOPE is used if an expression
1408      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1409      respectively.  QUALIFYING_SCOPE is used for an expression of the
1410      form "X::Y"; it refers to X.  */
1411   tree object_scope;
1412   tree qualifying_scope;
1413
1414   /* A stack of parsing contexts.  All but the bottom entry on the
1415      stack will be tentative contexts.
1416
1417      We parse tentatively in order to determine which construct is in
1418      use in some situations.  For example, in order to determine
1419      whether a statement is an expression-statement or a
1420      declaration-statement we parse it tentatively as a
1421      declaration-statement.  If that fails, we then reparse the same
1422      token stream as an expression-statement.  */
1423   cp_parser_context *context;
1424
1425   /* True if we are parsing GNU C++.  If this flag is not set, then
1426      GNU extensions are not recognized.  */
1427   bool allow_gnu_extensions_p;
1428
1429   /* TRUE if the `>' token should be interpreted as the greater-than
1430      operator.  FALSE if it is the end of a template-id or
1431      template-parameter-list. In C++0x mode, this flag also applies to
1432      `>>' tokens, which are viewed as two consecutive `>' tokens when
1433      this flag is FALSE.  */
1434   bool greater_than_is_operator_p;
1435
1436   /* TRUE if default arguments are allowed within a parameter list
1437      that starts at this point. FALSE if only a gnu extension makes
1438      them permissible.  */
1439   bool default_arg_ok_p;
1440
1441   /* TRUE if we are parsing an integral constant-expression.  See
1442      [expr.const] for a precise definition.  */
1443   bool integral_constant_expression_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression -- but a
1446      non-constant expression should be permitted as well.  This flag
1447      is used when parsing an array bound so that GNU variable-length
1448      arrays are tolerated.  */
1449   bool allow_non_integral_constant_expression_p;
1450
1451   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452      been seen that makes the expression non-constant.  */
1453   bool non_integral_constant_expression_p;
1454
1455   /* TRUE if local variable names and `this' are forbidden in the
1456      current context.  */
1457   bool local_variables_forbidden_p;
1458
1459   /* TRUE if the declaration we are parsing is part of a
1460      linkage-specification of the form `extern string-literal
1461      declaration'.  */
1462   bool in_unbraced_linkage_specification_p;
1463
1464   /* TRUE if we are presently parsing a declarator, after the
1465      direct-declarator.  */
1466   bool in_declarator_p;
1467
1468   /* TRUE if we are presently parsing a template-argument-list.  */
1469   bool in_template_argument_list_p;
1470
1471   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472      to IN_OMP_BLOCK if parsing OpenMP structured block and
1473      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1474      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475      iteration-statement, OpenMP block or loop within that switch.  */
1476 #define IN_SWITCH_STMT          1
1477 #define IN_ITERATION_STMT       2
1478 #define IN_OMP_BLOCK            4
1479 #define IN_OMP_FOR              8
1480 #define IN_IF_STMT             16
1481   unsigned char in_statement;
1482
1483   /* TRUE if we are presently parsing the body of a switch statement.
1484      Note that this doesn't quite overlap with in_statement above.
1485      The difference relates to giving the right sets of error messages:
1486      "case not in switch" vs "break statement used with OpenMP...".  */
1487   bool in_switch_statement_p;
1488
1489   /* TRUE if we are parsing a type-id in an expression context.  In
1490      such a situation, both "type (expr)" and "type (type)" are valid
1491      alternatives.  */
1492   bool in_type_id_in_expr_p;
1493
1494   /* TRUE if we are currently in a header file where declarations are
1495      implicitly extern "C".  */
1496   bool implicit_extern_c;
1497
1498   /* TRUE if strings in expressions should be translated to the execution
1499      character set.  */
1500   bool translate_strings_p;
1501
1502   /* TRUE if we are presently parsing the body of a function, but not
1503      a local class.  */
1504   bool in_function_body;
1505
1506   /* If non-NULL, then we are parsing a construct where new type
1507      definitions are not permitted.  The string stored here will be
1508      issued as an error message if a type is defined.  */
1509   const char *type_definition_forbidden_message;
1510
1511   /* A list of lists. The outer list is a stack, used for member
1512      functions of local classes. At each level there are two sub-list,
1513      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515      TREE_VALUE's. The functions are chained in reverse declaration
1516      order.
1517
1518      The TREE_PURPOSE sublist contains those functions with default
1519      arguments that need post processing, and the TREE_VALUE sublist
1520      contains those functions with definitions that need post
1521      processing.
1522
1523      These lists can only be processed once the outermost class being
1524      defined is complete.  */
1525   tree unparsed_functions_queues;
1526
1527   /* The number of classes whose definitions are currently in
1528      progress.  */
1529   unsigned num_classes_being_defined;
1530
1531   /* The number of template parameter lists that apply directly to the
1532      current declaration.  */
1533   unsigned num_template_parameter_lists;
1534 } cp_parser;
1535
1536 /* Prototypes.  */
1537
1538 /* Constructors and destructors.  */
1539
1540 static cp_parser *cp_parser_new
1541   (void);
1542
1543 /* Routines to parse various constructs.
1544
1545    Those that return `tree' will return the error_mark_node (rather
1546    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547    Sometimes, they will return an ordinary node if error-recovery was
1548    attempted, even though a parse error occurred.  So, to check
1549    whether or not a parse error occurred, you should always use
1550    cp_parser_error_occurred.  If the construct is optional (indicated
1551    either by an `_opt' in the name of the function that does the
1552    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553    the construct is not present.  */
1554
1555 /* Lexical conventions [gram.lex]  */
1556
1557 static tree cp_parser_identifier
1558   (cp_parser *);
1559 static tree cp_parser_string_literal
1560   (cp_parser *, bool, bool);
1561
1562 /* Basic concepts [gram.basic]  */
1563
1564 static bool cp_parser_translation_unit
1565   (cp_parser *);
1566
1567 /* Expressions [gram.expr]  */
1568
1569 static tree cp_parser_primary_expression
1570   (cp_parser *, bool, bool, bool, cp_id_kind *);
1571 static tree cp_parser_id_expression
1572   (cp_parser *, bool, bool, bool *, bool, bool);
1573 static tree cp_parser_unqualified_id
1574   (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier_opt
1576   (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_qualifying_entity
1580   (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582   (cp_parser *, bool, bool, bool, cp_id_kind *);
1583 static tree cp_parser_postfix_open_square_expression
1584   (cp_parser *, tree, bool);
1585 static tree cp_parser_postfix_dot_deref_expression
1586   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1587 static tree cp_parser_parenthesized_expression_list
1588   (cp_parser *, bool, bool, bool, bool *);
1589 static void cp_parser_pseudo_destructor_name
1590   (cp_parser *, tree *, tree *);
1591 static tree cp_parser_unary_expression
1592   (cp_parser *, bool, bool, cp_id_kind *);
1593 static enum tree_code cp_parser_unary_operator
1594   (cp_token *);
1595 static tree cp_parser_new_expression
1596   (cp_parser *);
1597 static tree cp_parser_new_placement
1598   (cp_parser *);
1599 static tree cp_parser_new_type_id
1600   (cp_parser *, tree *);
1601 static cp_declarator *cp_parser_new_declarator_opt
1602   (cp_parser *);
1603 static cp_declarator *cp_parser_direct_new_declarator
1604   (cp_parser *);
1605 static tree cp_parser_new_initializer
1606   (cp_parser *);
1607 static tree cp_parser_delete_expression
1608   (cp_parser *);
1609 static tree cp_parser_cast_expression
1610   (cp_parser *, bool, bool, cp_id_kind *);
1611 static tree cp_parser_binary_expression
1612   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1613 static tree cp_parser_question_colon_clause
1614   (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616   (cp_parser *, bool, cp_id_kind *);
1617 static enum tree_code cp_parser_assignment_operator_opt
1618   (cp_parser *);
1619 static tree cp_parser_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static tree cp_parser_constant_expression
1622   (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1624   (cp_parser *);
1625
1626 /* Statements [gram.stmt.stmt]  */
1627
1628 static void cp_parser_statement
1629   (cp_parser *, tree, bool, bool *);
1630 static void cp_parser_label_for_labeled_statement
1631   (cp_parser *);
1632 static tree cp_parser_expression_statement
1633   (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635   (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637   (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639   (cp_parser *, bool *);
1640 static tree cp_parser_condition
1641   (cp_parser *);
1642 static tree cp_parser_iteration_statement
1643   (cp_parser *);
1644 static void cp_parser_for_init_statement
1645   (cp_parser *);
1646 static tree cp_parser_jump_statement
1647   (cp_parser *);
1648 static void cp_parser_declaration_statement
1649   (cp_parser *);
1650
1651 static tree cp_parser_implicitly_scoped_statement
1652   (cp_parser *, bool *);
1653 static void cp_parser_already_scoped_statement
1654   (cp_parser *);
1655
1656 /* Declarations [gram.dcl.dcl] */
1657
1658 static void cp_parser_declaration_seq_opt
1659   (cp_parser *);
1660 static void cp_parser_declaration
1661   (cp_parser *);
1662 static void cp_parser_block_declaration
1663   (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665   (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1669   (cp_parser *);
1670 static tree cp_parser_function_specifier_opt
1671   (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1674    int *, bool *);
1675 static tree cp_parser_simple_type_specifier
1676   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1678   (cp_parser *);
1679 static tree cp_parser_nonclass_name 
1680   (cp_parser* parser);
1681 static tree cp_parser_elaborated_type_specifier
1682   (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1684   (cp_parser *);
1685 static void cp_parser_enumerator_list
1686   (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688   (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1690   (cp_parser *);
1691 static void cp_parser_namespace_definition
1692   (cp_parser *);
1693 static void cp_parser_namespace_body
1694   (cp_parser *);
1695 static tree cp_parser_qualified_namespace_specifier
1696   (cp_parser *);
1697 static void cp_parser_namespace_alias_definition
1698   (cp_parser *);
1699 static bool cp_parser_using_declaration
1700   (cp_parser *, bool);
1701 static void cp_parser_using_directive
1702   (cp_parser *);
1703 static void cp_parser_asm_definition
1704   (cp_parser *);
1705 static void cp_parser_linkage_specification
1706   (cp_parser *);
1707 static void cp_parser_static_assert
1708   (cp_parser *, bool);
1709 static tree cp_parser_decltype
1710   (cp_parser *);
1711
1712 /* Declarators [gram.dcl.decl] */
1713
1714 static tree cp_parser_init_declarator
1715   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1716 static cp_declarator *cp_parser_declarator
1717   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1718 static cp_declarator *cp_parser_direct_declarator
1719   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1720 static enum tree_code cp_parser_ptr_operator
1721   (cp_parser *, tree *, cp_cv_quals *);
1722 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1723   (cp_parser *);
1724 static tree cp_parser_late_return_type_opt
1725   (cp_parser *);
1726 static tree cp_parser_declarator_id
1727   (cp_parser *, bool);
1728 static tree cp_parser_type_id
1729   (cp_parser *);
1730 static tree cp_parser_template_type_arg
1731   (cp_parser *);
1732 static tree cp_parser_type_id_1
1733   (cp_parser *, bool);
1734 static void cp_parser_type_specifier_seq
1735   (cp_parser *, bool, cp_decl_specifier_seq *);
1736 static tree cp_parser_parameter_declaration_clause
1737   (cp_parser *);
1738 static tree cp_parser_parameter_declaration_list
1739   (cp_parser *, bool *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration
1741   (cp_parser *, bool, bool *);
1742 static tree cp_parser_default_argument 
1743   (cp_parser *, bool);
1744 static void cp_parser_function_body
1745   (cp_parser *);
1746 static tree cp_parser_initializer
1747   (cp_parser *, bool *, bool *);
1748 static tree cp_parser_initializer_clause
1749   (cp_parser *, bool *);
1750 static tree cp_parser_braced_list
1751   (cp_parser*, bool*);
1752 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1753   (cp_parser *, bool *);
1754
1755 static bool cp_parser_ctor_initializer_opt_and_function_body
1756   (cp_parser *);
1757
1758 /* Classes [gram.class] */
1759
1760 static tree cp_parser_class_name
1761   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1762 static tree cp_parser_class_specifier
1763   (cp_parser *);
1764 static tree cp_parser_class_head
1765   (cp_parser *, bool *, tree *, tree *);
1766 static enum tag_types cp_parser_class_key
1767   (cp_parser *);
1768 static void cp_parser_member_specification_opt
1769   (cp_parser *);
1770 static void cp_parser_member_declaration
1771   (cp_parser *);
1772 static tree cp_parser_pure_specifier
1773   (cp_parser *);
1774 static tree cp_parser_constant_initializer
1775   (cp_parser *);
1776
1777 /* Derived classes [gram.class.derived] */
1778
1779 static tree cp_parser_base_clause
1780   (cp_parser *);
1781 static tree cp_parser_base_specifier
1782   (cp_parser *);
1783
1784 /* Special member functions [gram.special] */
1785
1786 static tree cp_parser_conversion_function_id
1787   (cp_parser *);
1788 static tree cp_parser_conversion_type_id
1789   (cp_parser *);
1790 static cp_declarator *cp_parser_conversion_declarator_opt
1791   (cp_parser *);
1792 static bool cp_parser_ctor_initializer_opt
1793   (cp_parser *);
1794 static void cp_parser_mem_initializer_list
1795   (cp_parser *);
1796 static tree cp_parser_mem_initializer
1797   (cp_parser *);
1798 static tree cp_parser_mem_initializer_id
1799   (cp_parser *);
1800
1801 /* Overloading [gram.over] */
1802
1803 static tree cp_parser_operator_function_id
1804   (cp_parser *);
1805 static tree cp_parser_operator
1806   (cp_parser *);
1807
1808 /* Templates [gram.temp] */
1809
1810 static void cp_parser_template_declaration
1811   (cp_parser *, bool);
1812 static tree cp_parser_template_parameter_list
1813   (cp_parser *);
1814 static tree cp_parser_template_parameter
1815   (cp_parser *, bool *, bool *);
1816 static tree cp_parser_type_parameter
1817   (cp_parser *, bool *);
1818 static tree cp_parser_template_id
1819   (cp_parser *, bool, bool, bool);
1820 static tree cp_parser_template_name
1821   (cp_parser *, bool, bool, bool, bool *);
1822 static tree cp_parser_template_argument_list
1823   (cp_parser *);
1824 static tree cp_parser_template_argument
1825   (cp_parser *);
1826 static void cp_parser_explicit_instantiation
1827   (cp_parser *);
1828 static void cp_parser_explicit_specialization
1829   (cp_parser *);
1830
1831 /* Exception handling [gram.exception] */
1832
1833 static tree cp_parser_try_block
1834   (cp_parser *);
1835 static bool cp_parser_function_try_block
1836   (cp_parser *);
1837 static void cp_parser_handler_seq
1838   (cp_parser *);
1839 static void cp_parser_handler
1840   (cp_parser *);
1841 static tree cp_parser_exception_declaration
1842   (cp_parser *);
1843 static tree cp_parser_throw_expression
1844   (cp_parser *);
1845 static tree cp_parser_exception_specification_opt
1846   (cp_parser *);
1847 static tree cp_parser_type_id_list
1848   (cp_parser *);
1849
1850 /* GNU Extensions */
1851
1852 static tree cp_parser_asm_specification_opt
1853   (cp_parser *);
1854 static tree cp_parser_asm_operand_list
1855   (cp_parser *);
1856 static tree cp_parser_asm_clobber_list
1857   (cp_parser *);
1858 static tree cp_parser_attributes_opt
1859   (cp_parser *);
1860 static tree cp_parser_attribute_list
1861   (cp_parser *);
1862 static bool cp_parser_extension_opt
1863   (cp_parser *, int *);
1864 static void cp_parser_label_declaration
1865   (cp_parser *);
1866
1867 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868 static bool cp_parser_pragma
1869   (cp_parser *, enum pragma_context);
1870
1871 /* Objective-C++ Productions */
1872
1873 static tree cp_parser_objc_message_receiver
1874   (cp_parser *);
1875 static tree cp_parser_objc_message_args
1876   (cp_parser *);
1877 static tree cp_parser_objc_message_expression
1878   (cp_parser *);
1879 static tree cp_parser_objc_encode_expression
1880   (cp_parser *);
1881 static tree cp_parser_objc_defs_expression
1882   (cp_parser *);
1883 static tree cp_parser_objc_protocol_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_selector_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_expression
1888   (cp_parser *);
1889 static bool cp_parser_objc_selector_p
1890   (enum cpp_ttype);
1891 static tree cp_parser_objc_selector
1892   (cp_parser *);
1893 static tree cp_parser_objc_protocol_refs_opt
1894   (cp_parser *);
1895 static void cp_parser_objc_declaration
1896   (cp_parser *);
1897 static tree cp_parser_objc_statement
1898   (cp_parser *);
1899
1900 /* Utility Routines */
1901
1902 static tree cp_parser_lookup_name
1903   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1904 static tree cp_parser_lookup_name_simple
1905   (cp_parser *, tree, location_t);
1906 static tree cp_parser_maybe_treat_template_as_class
1907   (tree, bool);
1908 static bool cp_parser_check_declarator_template_parameters
1909   (cp_parser *, cp_declarator *, location_t);
1910 static bool cp_parser_check_template_parameters
1911   (cp_parser *, unsigned, location_t, cp_declarator *);
1912 static tree cp_parser_simple_cast_expression
1913   (cp_parser *);
1914 static tree cp_parser_global_scope_opt
1915   (cp_parser *, bool);
1916 static bool cp_parser_constructor_declarator_p
1917   (cp_parser *, bool);
1918 static tree cp_parser_function_definition_from_specifiers_and_declarator
1919   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1920 static tree cp_parser_function_definition_after_declarator
1921   (cp_parser *, bool);
1922 static void cp_parser_template_declaration_after_export
1923   (cp_parser *, bool);
1924 static void cp_parser_perform_template_parameter_access_checks
1925   (VEC (deferred_access_check,gc)*);
1926 static tree cp_parser_single_declaration
1927   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1928 static tree cp_parser_functional_cast
1929   (cp_parser *, tree);
1930 static tree cp_parser_save_member_function_body
1931   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1932 static tree cp_parser_enclosed_template_argument_list
1933   (cp_parser *);
1934 static void cp_parser_save_default_args
1935   (cp_parser *, tree);
1936 static void cp_parser_late_parsing_for_member
1937   (cp_parser *, tree);
1938 static void cp_parser_late_parsing_default_args
1939   (cp_parser *, tree);
1940 static tree cp_parser_sizeof_operand
1941   (cp_parser *, enum rid);
1942 static tree cp_parser_trait_expr
1943   (cp_parser *, enum rid);
1944 static bool cp_parser_declares_only_class_p
1945   (cp_parser *);
1946 static void cp_parser_set_storage_class
1947   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1948 static void cp_parser_set_decl_spec_type
1949   (cp_decl_specifier_seq *, tree, location_t, bool);
1950 static bool cp_parser_friend_p
1951   (const cp_decl_specifier_seq *);
1952 static cp_token *cp_parser_require
1953   (cp_parser *, enum cpp_ttype, const char *);
1954 static cp_token *cp_parser_require_keyword
1955   (cp_parser *, enum rid, const char *);
1956 static bool cp_parser_token_starts_function_definition_p
1957   (cp_token *);
1958 static bool cp_parser_next_token_starts_class_definition_p
1959   (cp_parser *);
1960 static bool cp_parser_next_token_ends_template_argument_p
1961   (cp_parser *);
1962 static bool cp_parser_nth_token_starts_template_argument_list_p
1963   (cp_parser *, size_t);
1964 static enum tag_types cp_parser_token_is_class_key
1965   (cp_token *);
1966 static void cp_parser_check_class_key
1967   (enum tag_types, tree type);
1968 static void cp_parser_check_access_in_redeclaration
1969   (tree type, location_t location);
1970 static bool cp_parser_optional_template_keyword
1971   (cp_parser *);
1972 static void cp_parser_pre_parsed_nested_name_specifier
1973   (cp_parser *);
1974 static bool cp_parser_cache_group
1975   (cp_parser *, enum cpp_ttype, unsigned);
1976 static void cp_parser_parse_tentatively
1977   (cp_parser *);
1978 static void cp_parser_commit_to_tentative_parse
1979   (cp_parser *);
1980 static void cp_parser_abort_tentative_parse
1981   (cp_parser *);
1982 static bool cp_parser_parse_definitely
1983   (cp_parser *);
1984 static inline bool cp_parser_parsing_tentatively
1985   (cp_parser *);
1986 static bool cp_parser_uncommitted_to_tentative_parse_p
1987   (cp_parser *);
1988 static void cp_parser_error
1989   (cp_parser *, const char *);
1990 static void cp_parser_name_lookup_error
1991   (cp_parser *, tree, tree, const char *, location_t);
1992 static bool cp_parser_simulate_error
1993   (cp_parser *);
1994 static bool cp_parser_check_type_definition
1995   (cp_parser *);
1996 static void cp_parser_check_for_definition_in_return_type
1997   (cp_declarator *, tree, location_t type_location);
1998 static void cp_parser_check_for_invalid_template_id
1999   (cp_parser *, tree, location_t location);
2000 static bool cp_parser_non_integral_constant_expression
2001   (cp_parser *, const char *);
2002 static void cp_parser_diagnose_invalid_type_name
2003   (cp_parser *, tree, tree, location_t);
2004 static bool cp_parser_parse_and_diagnose_invalid_type_name
2005   (cp_parser *);
2006 static int cp_parser_skip_to_closing_parenthesis
2007   (cp_parser *, bool, bool, bool);
2008 static void cp_parser_skip_to_end_of_statement
2009   (cp_parser *);
2010 static void cp_parser_consume_semicolon_at_end_of_statement
2011   (cp_parser *);
2012 static void cp_parser_skip_to_end_of_block_or_statement
2013   (cp_parser *);
2014 static bool cp_parser_skip_to_closing_brace
2015   (cp_parser *);
2016 static void cp_parser_skip_to_end_of_template_parameter_list
2017   (cp_parser *);
2018 static void cp_parser_skip_to_pragma_eol
2019   (cp_parser*, cp_token *);
2020 static bool cp_parser_error_occurred
2021   (cp_parser *);
2022 static bool cp_parser_allow_gnu_extensions_p
2023   (cp_parser *);
2024 static bool cp_parser_is_string_literal
2025   (cp_token *);
2026 static bool cp_parser_is_keyword
2027   (cp_token *, enum rid);
2028 static tree cp_parser_make_typename_type
2029   (cp_parser *, tree, tree, location_t location);
2030 static cp_declarator * cp_parser_make_indirect_declarator
2031   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2032
2033 /* Returns nonzero if we are parsing tentatively.  */
2034
2035 static inline bool
2036 cp_parser_parsing_tentatively (cp_parser* parser)
2037 {
2038   return parser->context->next != NULL;
2039 }
2040
2041 /* Returns nonzero if TOKEN is a string literal.  */
2042
2043 static bool
2044 cp_parser_is_string_literal (cp_token* token)
2045 {
2046   return (token->type == CPP_STRING ||
2047           token->type == CPP_STRING16 ||
2048           token->type == CPP_STRING32 ||
2049           token->type == CPP_WSTRING);
2050 }
2051
2052 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2053
2054 static bool
2055 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 {
2057   return token->keyword == keyword;
2058 }
2059
2060 /* If not parsing tentatively, issue a diagnostic of the form
2061       FILE:LINE: MESSAGE before TOKEN
2062    where TOKEN is the next token in the input stream.  MESSAGE
2063    (specified by the caller) is usually of the form "expected
2064    OTHER-TOKEN".  */
2065
2066 static void
2067 cp_parser_error (cp_parser* parser, const char* message)
2068 {
2069   if (!cp_parser_simulate_error (parser))
2070     {
2071       cp_token *token = cp_lexer_peek_token (parser->lexer);
2072       /* This diagnostic makes more sense if it is tagged to the line
2073          of the token we just peeked at.  */
2074       cp_lexer_set_source_position_from_token (token);
2075
2076       if (token->type == CPP_PRAGMA)
2077         {
2078           error ("%H%<#pragma%> is not allowed here", &token->location);
2079           cp_parser_skip_to_pragma_eol (parser, token);
2080           return;
2081         }
2082
2083       c_parse_error (message,
2084                      /* Because c_parser_error does not understand
2085                         CPP_KEYWORD, keywords are treated like
2086                         identifiers.  */
2087                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2088                      token->u.value, token->flags);
2089     }
2090 }
2091
2092 /* Issue an error about name-lookup failing.  NAME is the
2093    IDENTIFIER_NODE DECL is the result of
2094    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2095    the thing that we hoped to find.  */
2096
2097 static void
2098 cp_parser_name_lookup_error (cp_parser* parser,
2099                              tree name,
2100                              tree decl,
2101                              const char* desired,
2102                              location_t location)
2103 {
2104   /* If name lookup completely failed, tell the user that NAME was not
2105      declared.  */
2106   if (decl == error_mark_node)
2107     {
2108       if (parser->scope && parser->scope != global_namespace)
2109         error ("%H%<%E::%E%> has not been declared",
2110                &location, parser->scope, name);
2111       else if (parser->scope == global_namespace)
2112         error ("%H%<::%E%> has not been declared", &location, name);
2113       else if (parser->object_scope
2114                && !CLASS_TYPE_P (parser->object_scope))
2115         error ("%Hrequest for member %qE in non-class type %qT",
2116                &location, name, parser->object_scope);
2117       else if (parser->object_scope)
2118         error ("%H%<%T::%E%> has not been declared",
2119                &location, parser->object_scope, name);
2120       else
2121         error ("%H%qE has not been declared", &location, name);
2122     }
2123   else if (parser->scope && parser->scope != global_namespace)
2124     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2125   else if (parser->scope == global_namespace)
2126     error ("%H%<::%E%> %s", &location, name, desired);
2127   else
2128     error ("%H%qE %s", &location, name, desired);
2129 }
2130
2131 /* If we are parsing tentatively, remember that an error has occurred
2132    during this tentative parse.  Returns true if the error was
2133    simulated; false if a message should be issued by the caller.  */
2134
2135 static bool
2136 cp_parser_simulate_error (cp_parser* parser)
2137 {
2138   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2139     {
2140       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2141       return true;
2142     }
2143   return false;
2144 }
2145
2146 /* Check for repeated decl-specifiers.  */
2147
2148 static void
2149 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2150                            location_t location)
2151 {
2152   cp_decl_spec ds;
2153
2154   for (ds = ds_first; ds != ds_last; ++ds)
2155     {
2156       unsigned count = decl_specs->specs[(int)ds];
2157       if (count < 2)
2158         continue;
2159       /* The "long" specifier is a special case because of "long long".  */
2160       if (ds == ds_long)
2161         {
2162           if (count > 2)
2163             error ("%H%<long long long%> is too long for GCC", &location);
2164           else 
2165             pedwarn_cxx98 (location, OPT_Wlong_long, 
2166                            "ISO C++ 1998 does not support %<long long%>");
2167         }
2168       else if (count > 1)
2169         {
2170           static const char *const decl_spec_names[] = {
2171             "signed",
2172             "unsigned",
2173             "short",
2174             "long",
2175             "const",
2176             "volatile",
2177             "restrict",
2178             "inline",
2179             "virtual",
2180             "explicit",
2181             "friend",
2182             "typedef",
2183             "__complex",
2184             "__thread"
2185           };
2186           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2187         }
2188     }
2189 }
2190
2191 /* This function is called when a type is defined.  If type
2192    definitions are forbidden at this point, an error message is
2193    issued.  */
2194
2195 static bool
2196 cp_parser_check_type_definition (cp_parser* parser)
2197 {
2198   /* If types are forbidden here, issue a message.  */
2199   if (parser->type_definition_forbidden_message)
2200     {
2201       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202          in the message need to be interpreted.  */
2203       error (parser->type_definition_forbidden_message);
2204       return false;
2205     }
2206   return true;
2207 }
2208
2209 /* This function is called when the DECLARATOR is processed.  The TYPE
2210    was a type defined in the decl-specifiers.  If it is invalid to
2211    define a type in the decl-specifiers for DECLARATOR, an error is
2212    issued. TYPE_LOCATION is the location of TYPE and is used
2213    for error reporting.  */
2214
2215 static void
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217                                                tree type, location_t type_location)
2218 {
2219   /* [dcl.fct] forbids type definitions in return types.
2220      Unfortunately, it's not easy to know whether or not we are
2221      processing a return type until after the fact.  */
2222   while (declarator
2223          && (declarator->kind == cdk_pointer
2224              || declarator->kind == cdk_reference
2225              || declarator->kind == cdk_ptrmem))
2226     declarator = declarator->declarator;
2227   if (declarator
2228       && declarator->kind == cdk_function)
2229     {
2230       error ("%Hnew types may not be defined in a return type", &type_location);
2231       inform (type_location, 
2232               "(perhaps a semicolon is missing after the definition of %qT)",
2233               type);
2234     }
2235 }
2236
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238    "<" in any valid C++ program.  If the next token is indeed "<",
2239    issue a message warning the user about what appears to be an
2240    invalid attempt to form a template-id. LOCATION is the location
2241    of the type-specifier (TYPE) */
2242
2243 static void
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245                                          tree type, location_t location)
2246 {
2247   cp_token_position start = 0;
2248
2249   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2250     {
2251       if (TYPE_P (type))
2252         error ("%H%qT is not a template", &location, type);
2253       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254         error ("%H%qE is not a template", &location, type);
2255       else
2256         error ("%Hinvalid template-id", &location);
2257       /* Remember the location of the invalid "<".  */
2258       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259         start = cp_lexer_token_position (parser->lexer, true);
2260       /* Consume the "<".  */
2261       cp_lexer_consume_token (parser->lexer);
2262       /* Parse the template arguments.  */
2263       cp_parser_enclosed_template_argument_list (parser);
2264       /* Permanently remove the invalid template arguments so that
2265          this error message is not issued again.  */
2266       if (start)
2267         cp_lexer_purge_tokens_after (parser->lexer, start);
2268     }
2269 }
2270
2271 /* If parsing an integral constant-expression, issue an error message
2272    about the fact that THING appeared and return true.  Otherwise,
2273    return false.  In either case, set
2274    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2275
2276 static bool
2277 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2278                                             const char *thing)
2279 {
2280   parser->non_integral_constant_expression_p = true;
2281   if (parser->integral_constant_expression_p)
2282     {
2283       if (!parser->allow_non_integral_constant_expression_p)
2284         {
2285           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286              in the message need to be interpreted.  */
2287           char *message = concat (thing,
2288                                   " cannot appear in a constant-expression",
2289                                   NULL);
2290           error (message);
2291           free (message);
2292           return true;
2293         }
2294     }
2295   return false;
2296 }
2297
2298 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2299    qualifying scope (or NULL, if none) for ID.  This function commits
2300    to the current active tentative parse, if any.  (Otherwise, the
2301    problematic construct might be encountered again later, resulting
2302    in duplicate error messages.) LOCATION is the location of ID.  */
2303
2304 static void
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306                                       tree scope, tree id,
2307                                       location_t location)
2308 {
2309   tree decl, old_scope;
2310   /* Try to lookup the identifier.  */
2311   old_scope = parser->scope;
2312   parser->scope = scope;
2313   decl = cp_parser_lookup_name_simple (parser, id, location);
2314   parser->scope = old_scope;
2315   /* If the lookup found a template-name, it means that the user forgot
2316   to specify an argument list. Emit a useful error message.  */
2317   if (TREE_CODE (decl) == TEMPLATE_DECL)
2318     error ("%Hinvalid use of template-name %qE without an argument list",
2319            &location, decl);
2320   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322   else if (TREE_CODE (decl) == TYPE_DECL)
2323     /* Something like 'unsigned A a;'  */
2324     error ("%Hinvalid combination of multiple type-specifiers",
2325            &location);
2326   else if (!parser->scope)
2327     {
2328       /* Issue an error message.  */
2329       error ("%H%qE does not name a type", &location, id);
2330       /* If we're in a template class, it's possible that the user was
2331          referring to a type from a base class.  For example:
2332
2333            template <typename T> struct A { typedef T X; };
2334            template <typename T> struct B : public A<T> { X x; };
2335
2336          The user should have said "typename A<T>::X".  */
2337       if (processing_template_decl && current_class_type
2338           && TYPE_BINFO (current_class_type))
2339         {
2340           tree b;
2341
2342           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2343                b;
2344                b = TREE_CHAIN (b))
2345             {
2346               tree base_type = BINFO_TYPE (b);
2347               if (CLASS_TYPE_P (base_type)
2348                   && dependent_type_p (base_type))
2349                 {
2350                   tree field;
2351                   /* Go from a particular instantiation of the
2352                      template (which will have an empty TYPE_FIELDs),
2353                      to the main version.  */
2354                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355                   for (field = TYPE_FIELDS (base_type);
2356                        field;
2357                        field = TREE_CHAIN (field))
2358                     if (TREE_CODE (field) == TYPE_DECL
2359                         && DECL_NAME (field) == id)
2360                       {
2361                         inform (location, 
2362                                 "(perhaps %<typename %T::%E%> was intended)",
2363                                 BINFO_TYPE (b), id);
2364                         break;
2365                       }
2366                   if (field)
2367                     break;
2368                 }
2369             }
2370         }
2371     }
2372   /* Here we diagnose qualified-ids where the scope is actually correct,
2373      but the identifier does not resolve to a valid type name.  */
2374   else if (parser->scope != error_mark_node)
2375     {
2376       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377         error ("%H%qE in namespace %qE does not name a type",
2378                &location, id, parser->scope);
2379       else if (TYPE_P (parser->scope))
2380         error ("%H%qE in class %qT does not name a type",
2381                &location, id, parser->scope);
2382       else
2383         gcc_unreachable ();
2384     }
2385   cp_parser_commit_to_tentative_parse (parser);
2386 }
2387
2388 /* Check for a common situation where a type-name should be present,
2389    but is not, and issue a sensible error message.  Returns true if an
2390    invalid type-name was detected.
2391
2392    The situation handled by this function are variable declarations of the
2393    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394    Usually, `ID' should name a type, but if we got here it means that it
2395    does not. We try to emit the best possible error message depending on
2396    how exactly the id-expression looks like.  */
2397
2398 static bool
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2400 {
2401   tree id;
2402   cp_token *token = cp_lexer_peek_token (parser->lexer);
2403
2404   cp_parser_parse_tentatively (parser);
2405   id = cp_parser_id_expression (parser,
2406                                 /*template_keyword_p=*/false,
2407                                 /*check_dependency_p=*/true,
2408                                 /*template_p=*/NULL,
2409                                 /*declarator_p=*/true,
2410                                 /*optional_p=*/false);
2411   /* After the id-expression, there should be a plain identifier,
2412      otherwise this is not a simple variable declaration. Also, if
2413      the scope is dependent, we cannot do much.  */
2414   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415       || (parser->scope && TYPE_P (parser->scope)
2416           && dependent_type_p (parser->scope))
2417       || TREE_CODE (id) == TYPE_DECL)
2418     {
2419       cp_parser_abort_tentative_parse (parser);
2420       return false;
2421     }
2422   if (!cp_parser_parse_definitely (parser))
2423     return false;
2424
2425   /* Emit a diagnostic for the invalid type.  */
2426   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427                                         id, token->location);
2428   /* Skip to the end of the declaration; there's no point in
2429      trying to process it.  */
2430   cp_parser_skip_to_end_of_block_or_statement (parser);
2431   return true;
2432 }
2433
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2436    are doing error recovery. Returns -1 if OR_COMMA is true and we
2437    found an unnested comma.  */
2438
2439 static int
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2441                                        bool recovering,
2442                                        bool or_comma,
2443                                        bool consume_paren)
2444 {
2445   unsigned paren_depth = 0;
2446   unsigned brace_depth = 0;
2447
2448   if (recovering && !or_comma
2449       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2450     return 0;
2451
2452   while (true)
2453     {
2454       cp_token * token = cp_lexer_peek_token (parser->lexer);
2455
2456       switch (token->type)
2457         {
2458         case CPP_EOF:
2459         case CPP_PRAGMA_EOL:
2460           /* If we've run out of tokens, then there is no closing `)'.  */
2461           return 0;
2462
2463         case CPP_SEMICOLON:
2464           /* This matches the processing in skip_to_end_of_statement.  */
2465           if (!brace_depth)
2466             return 0;
2467           break;
2468
2469         case CPP_OPEN_BRACE:
2470           ++brace_depth;
2471           break;
2472         case CPP_CLOSE_BRACE:
2473           if (!brace_depth--)
2474             return 0;
2475           break;
2476
2477         case CPP_COMMA:
2478           if (recovering && or_comma && !brace_depth && !paren_depth)
2479             return -1;
2480           break;
2481
2482         case CPP_OPEN_PAREN:
2483           if (!brace_depth)
2484             ++paren_depth;
2485           break;
2486
2487         case CPP_CLOSE_PAREN:
2488           if (!brace_depth && !paren_depth--)
2489             {
2490               if (consume_paren)
2491                 cp_lexer_consume_token (parser->lexer);
2492               return 1;
2493             }
2494           break;
2495
2496         default:
2497           break;
2498         }
2499
2500       /* Consume the token.  */
2501       cp_lexer_consume_token (parser->lexer);
2502     }
2503 }
2504
2505 /* Consume tokens until we reach the end of the current statement.
2506    Normally, that will be just before consuming a `;'.  However, if a
2507    non-nested `}' comes first, then we stop before consuming that.  */
2508
2509 static void
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2511 {
2512   unsigned nesting_depth = 0;
2513
2514   while (true)
2515     {
2516       cp_token *token = cp_lexer_peek_token (parser->lexer);
2517
2518       switch (token->type)
2519         {
2520         case CPP_EOF:
2521         case CPP_PRAGMA_EOL:
2522           /* If we've run out of tokens, stop.  */
2523           return;
2524
2525         case CPP_SEMICOLON:
2526           /* If the next token is a `;', we have reached the end of the
2527              statement.  */
2528           if (!nesting_depth)
2529             return;
2530           break;
2531
2532         case CPP_CLOSE_BRACE:
2533           /* If this is a non-nested '}', stop before consuming it.
2534              That way, when confronted with something like:
2535
2536                { 3 + }
2537
2538              we stop before consuming the closing '}', even though we
2539              have not yet reached a `;'.  */
2540           if (nesting_depth == 0)
2541             return;
2542
2543           /* If it is the closing '}' for a block that we have
2544              scanned, stop -- but only after consuming the token.
2545              That way given:
2546
2547                 void f g () { ... }
2548                 typedef int I;
2549
2550              we will stop after the body of the erroneously declared
2551              function, but before consuming the following `typedef'
2552              declaration.  */
2553           if (--nesting_depth == 0)
2554             {
2555               cp_lexer_consume_token (parser->lexer);
2556               return;
2557             }
2558
2559         case CPP_OPEN_BRACE:
2560           ++nesting_depth;
2561           break;
2562
2563         default:
2564           break;
2565         }
2566
2567       /* Consume the token.  */
2568       cp_lexer_consume_token (parser->lexer);
2569     }
2570 }
2571
2572 /* This function is called at the end of a statement or declaration.
2573    If the next token is a semicolon, it is consumed; otherwise, error
2574    recovery is attempted.  */
2575
2576 static void
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578 {
2579   /* Look for the trailing `;'.  */
2580   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2581     {
2582       /* If there is additional (erroneous) input, skip to the end of
2583          the statement.  */
2584       cp_parser_skip_to_end_of_statement (parser);
2585       /* If the next token is now a `;', consume it.  */
2586       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587         cp_lexer_consume_token (parser->lexer);
2588     }
2589 }
2590
2591 /* Skip tokens until we have consumed an entire block, or until we
2592    have consumed a non-nested `;'.  */
2593
2594 static void
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2596 {
2597   int nesting_depth = 0;
2598
2599   while (nesting_depth >= 0)
2600     {
2601       cp_token *token = cp_lexer_peek_token (parser->lexer);
2602
2603       switch (token->type)
2604         {
2605         case CPP_EOF:
2606         case CPP_PRAGMA_EOL:
2607           /* If we've run out of tokens, stop.  */
2608           return;
2609
2610         case CPP_SEMICOLON:
2611           /* Stop if this is an unnested ';'. */
2612           if (!nesting_depth)
2613             nesting_depth = -1;
2614           break;
2615
2616         case CPP_CLOSE_BRACE:
2617           /* Stop if this is an unnested '}', or closes the outermost
2618              nesting level.  */
2619           nesting_depth--;
2620           if (nesting_depth < 0)
2621             return;
2622           if (!nesting_depth)
2623             nesting_depth = -1;
2624           break;
2625
2626         case CPP_OPEN_BRACE:
2627           /* Nest. */
2628           nesting_depth++;
2629           break;
2630
2631         default:
2632           break;
2633         }
2634
2635       /* Consume the token.  */
2636       cp_lexer_consume_token (parser->lexer);
2637     }
2638 }
2639
2640 /* Skip tokens until a non-nested closing curly brace is the next
2641    token, or there are no more tokens. Return true in the first case,
2642    false otherwise.  */
2643
2644 static bool
2645 cp_parser_skip_to_closing_brace (cp_parser *parser)
2646 {
2647   unsigned nesting_depth = 0;
2648
2649   while (true)
2650     {
2651       cp_token *token = cp_lexer_peek_token (parser->lexer);
2652
2653       switch (token->type)
2654         {
2655         case CPP_EOF:
2656         case CPP_PRAGMA_EOL:
2657           /* If we've run out of tokens, stop.  */
2658           return false;
2659
2660         case CPP_CLOSE_BRACE:
2661           /* If the next token is a non-nested `}', then we have reached
2662              the end of the current block.  */
2663           if (nesting_depth-- == 0)
2664             return true;
2665           break;
2666
2667         case CPP_OPEN_BRACE:
2668           /* If it the next token is a `{', then we are entering a new
2669              block.  Consume the entire block.  */
2670           ++nesting_depth;
2671           break;
2672
2673         default:
2674           break;
2675         }
2676
2677       /* Consume the token.  */
2678       cp_lexer_consume_token (parser->lexer);
2679     }
2680 }
2681
2682 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2683    parameter is the PRAGMA token, allowing us to purge the entire pragma
2684    sequence.  */
2685
2686 static void
2687 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2688 {
2689   cp_token *token;
2690
2691   parser->lexer->in_pragma = false;
2692
2693   do
2694     token = cp_lexer_consume_token (parser->lexer);
2695   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2696
2697   /* Ensure that the pragma is not parsed again.  */
2698   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2699 }
2700
2701 /* Require pragma end of line, resyncing with it as necessary.  The
2702    arguments are as for cp_parser_skip_to_pragma_eol.  */
2703
2704 static void
2705 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2706 {
2707   parser->lexer->in_pragma = false;
2708   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2710 }
2711
2712 /* This is a simple wrapper around make_typename_type. When the id is
2713    an unresolved identifier node, we can provide a superior diagnostic
2714    using cp_parser_diagnose_invalid_type_name.  */
2715
2716 static tree
2717 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718                               tree id, location_t id_location)
2719 {
2720   tree result;
2721   if (TREE_CODE (id) == IDENTIFIER_NODE)
2722     {
2723       result = make_typename_type (scope, id, typename_type,
2724                                    /*complain=*/tf_none);
2725       if (result == error_mark_node)
2726         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2727       return result;
2728     }
2729   return make_typename_type (scope, id, typename_type, tf_error);
2730 }
2731
2732 /* This is a wrapper around the
2733    make_{pointer,ptrmem,reference}_declarator functions that decides
2734    which one to call based on the CODE and CLASS_TYPE arguments. The
2735    CODE argument should be one of the values returned by
2736    cp_parser_ptr_operator. */
2737 static cp_declarator *
2738 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739                                     cp_cv_quals cv_qualifiers,
2740                                     cp_declarator *target)
2741 {
2742   if (code == ERROR_MARK)
2743     return cp_error_declarator;
2744
2745   if (code == INDIRECT_REF)
2746     if (class_type == NULL_TREE)
2747       return make_pointer_declarator (cv_qualifiers, target);
2748     else
2749       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751     return make_reference_declarator (cv_qualifiers, target, false);
2752   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, true);
2754   gcc_unreachable ();
2755 }
2756
2757 /* Create a new C++ parser.  */
2758
2759 static cp_parser *
2760 cp_parser_new (void)
2761 {
2762   cp_parser *parser;
2763   cp_lexer *lexer;
2764   unsigned i;
2765
2766   /* cp_lexer_new_main is called before calling ggc_alloc because
2767      cp_lexer_new_main might load a PCH file.  */
2768   lexer = cp_lexer_new_main ();
2769
2770   /* Initialize the binops_by_token so that we can get the tree
2771      directly from the token.  */
2772   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773     binops_by_token[binops[i].token_type] = binops[i];
2774
2775   parser = GGC_CNEW (cp_parser);
2776   parser->lexer = lexer;
2777   parser->context = cp_parser_context_new (NULL);
2778
2779   /* For now, we always accept GNU extensions.  */
2780   parser->allow_gnu_extensions_p = 1;
2781
2782   /* The `>' token is a greater-than operator, not the end of a
2783      template-id.  */
2784   parser->greater_than_is_operator_p = true;
2785
2786   parser->default_arg_ok_p = true;
2787
2788   /* We are not parsing a constant-expression.  */
2789   parser->integral_constant_expression_p = false;
2790   parser->allow_non_integral_constant_expression_p = false;
2791   parser->non_integral_constant_expression_p = false;
2792
2793   /* Local variable names are not forbidden.  */
2794   parser->local_variables_forbidden_p = false;
2795
2796   /* We are not processing an `extern "C"' declaration.  */
2797   parser->in_unbraced_linkage_specification_p = false;
2798
2799   /* We are not processing a declarator.  */
2800   parser->in_declarator_p = false;
2801
2802   /* We are not processing a template-argument-list.  */
2803   parser->in_template_argument_list_p = false;
2804
2805   /* We are not in an iteration statement.  */
2806   parser->in_statement = 0;
2807
2808   /* We are not in a switch statement.  */
2809   parser->in_switch_statement_p = false;
2810
2811   /* We are not parsing a type-id inside an expression.  */
2812   parser->in_type_id_in_expr_p = false;
2813
2814   /* Declarations aren't implicitly extern "C".  */
2815   parser->implicit_extern_c = false;
2816
2817   /* String literals should be translated to the execution character set.  */
2818   parser->translate_strings_p = true;
2819
2820   /* We are not parsing a function body.  */
2821   parser->in_function_body = false;
2822
2823   /* The unparsed function queue is empty.  */
2824   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2825
2826   /* There are no classes being defined.  */
2827   parser->num_classes_being_defined = 0;
2828
2829   /* No template parameters apply.  */
2830   parser->num_template_parameter_lists = 0;
2831
2832   return parser;
2833 }
2834
2835 /* Create a cp_lexer structure which will emit the tokens in CACHE
2836    and push it onto the parser's lexer stack.  This is used for delayed
2837    parsing of in-class method bodies and default arguments, and should
2838    not be confused with tentative parsing.  */
2839 static void
2840 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2841 {
2842   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843   lexer->next = parser->lexer;
2844   parser->lexer = lexer;
2845
2846   /* Move the current source position to that of the first token in the
2847      new lexer.  */
2848   cp_lexer_set_source_position_from_token (lexer->next_token);
2849 }
2850
2851 /* Pop the top lexer off the parser stack.  This is never used for the
2852    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2853 static void
2854 cp_parser_pop_lexer (cp_parser *parser)
2855 {
2856   cp_lexer *lexer = parser->lexer;
2857   parser->lexer = lexer->next;
2858   cp_lexer_destroy (lexer);
2859
2860   /* Put the current source position back where it was before this
2861      lexer was pushed.  */
2862   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2863 }
2864
2865 /* Lexical conventions [gram.lex]  */
2866
2867 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2868    identifier.  */
2869
2870 static tree
2871 cp_parser_identifier (cp_parser* parser)
2872 {
2873   cp_token *token;
2874
2875   /* Look for the identifier.  */
2876   token = cp_parser_require (parser, CPP_NAME, "identifier");
2877   /* Return the value.  */
2878   return token ? token->u.value : error_mark_node;
2879 }
2880
2881 /* Parse a sequence of adjacent string constants.  Returns a
2882    TREE_STRING representing the combined, nul-terminated string
2883    constant.  If TRANSLATE is true, translate the string to the
2884    execution character set.  If WIDE_OK is true, a wide string is
2885    invalid here.
2886
2887    C++98 [lex.string] says that if a narrow string literal token is
2888    adjacent to a wide string literal token, the behavior is undefined.
2889    However, C99 6.4.5p4 says that this results in a wide string literal.
2890    We follow C99 here, for consistency with the C front end.
2891
2892    This code is largely lifted from lex_string() in c-lex.c.
2893
2894    FUTURE: ObjC++ will need to handle @-strings here.  */
2895 static tree
2896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2897 {
2898   tree value;
2899   size_t count;
2900   struct obstack str_ob;
2901   cpp_string str, istr, *strs;
2902   cp_token *tok;
2903   enum cpp_ttype type;
2904
2905   tok = cp_lexer_peek_token (parser->lexer);
2906   if (!cp_parser_is_string_literal (tok))
2907     {
2908       cp_parser_error (parser, "expected string-literal");
2909       return error_mark_node;
2910     }
2911
2912   type = tok->type;
2913
2914   /* Try to avoid the overhead of creating and destroying an obstack
2915      for the common case of just one string.  */
2916   if (!cp_parser_is_string_literal
2917       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2918     {
2919       cp_lexer_consume_token (parser->lexer);
2920
2921       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922       str.len = TREE_STRING_LENGTH (tok->u.value);
2923       count = 1;
2924
2925       strs = &str;
2926     }
2927   else
2928     {
2929       gcc_obstack_init (&str_ob);
2930       count = 0;
2931
2932       do
2933         {
2934           cp_lexer_consume_token (parser->lexer);
2935           count++;
2936           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2937           str.len = TREE_STRING_LENGTH (tok->u.value);
2938
2939           if (type != tok->type)
2940             {
2941               if (type == CPP_STRING)
2942                 type = tok->type;
2943               else if (tok->type != CPP_STRING)
2944                 error ("%Hunsupported non-standard concatenation "
2945                        "of string literals", &tok->location);
2946             }
2947
2948           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2949
2950           tok = cp_lexer_peek_token (parser->lexer);
2951         }
2952       while (cp_parser_is_string_literal (tok));
2953
2954       strs = (cpp_string *) obstack_finish (&str_ob);
2955     }
2956
2957   if (type != CPP_STRING && !wide_ok)
2958     {
2959       cp_parser_error (parser, "a wide string is invalid in this context");
2960       type = CPP_STRING;
2961     }
2962
2963   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2964       (parse_in, strs, count, &istr, type))
2965     {
2966       value = build_string (istr.len, (const char *)istr.text);
2967       free (CONST_CAST (unsigned char *, istr.text));
2968
2969       switch (type)
2970         {
2971         default:
2972         case CPP_STRING:
2973           TREE_TYPE (value) = char_array_type_node;
2974           break;
2975         case CPP_STRING16:
2976           TREE_TYPE (value) = char16_array_type_node;
2977           break;
2978         case CPP_STRING32:
2979           TREE_TYPE (value) = char32_array_type_node;
2980           break;
2981         case CPP_WSTRING:
2982           TREE_TYPE (value) = wchar_array_type_node;
2983           break;
2984         }
2985
2986       value = fix_string_type (value);
2987     }
2988   else
2989     /* cpp_interpret_string has issued an error.  */
2990     value = error_mark_node;
2991
2992   if (count > 1)
2993     obstack_free (&str_ob, 0);
2994
2995   return value;
2996 }
2997
2998
2999 /* Basic concepts [gram.basic]  */
3000
3001 /* Parse a translation-unit.
3002
3003    translation-unit:
3004      declaration-seq [opt]
3005
3006    Returns TRUE if all went well.  */
3007
3008 static bool
3009 cp_parser_translation_unit (cp_parser* parser)
3010 {
3011   /* The address of the first non-permanent object on the declarator
3012      obstack.  */
3013   static void *declarator_obstack_base;
3014
3015   bool success;
3016
3017   /* Create the declarator obstack, if necessary.  */
3018   if (!cp_error_declarator)
3019     {
3020       gcc_obstack_init (&declarator_obstack);
3021       /* Create the error declarator.  */
3022       cp_error_declarator = make_declarator (cdk_error);
3023       /* Create the empty parameter list.  */
3024       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3025       /* Remember where the base of the declarator obstack lies.  */
3026       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3027     }
3028
3029   cp_parser_declaration_seq_opt (parser);
3030
3031   /* If there are no tokens left then all went well.  */
3032   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3033     {
3034       /* Get rid of the token array; we don't need it any more.  */
3035       cp_lexer_destroy (parser->lexer);
3036       parser->lexer = NULL;
3037
3038       /* This file might have been a context that's implicitly extern
3039          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3040       if (parser->implicit_extern_c)
3041         {
3042           pop_lang_context ();
3043           parser->implicit_extern_c = false;
3044         }
3045
3046       /* Finish up.  */
3047       finish_translation_unit ();
3048
3049       success = true;
3050     }
3051   else
3052     {
3053       cp_parser_error (parser, "expected declaration");
3054       success = false;
3055     }
3056
3057   /* Make sure the declarator obstack was fully cleaned up.  */
3058   gcc_assert (obstack_next_free (&declarator_obstack)
3059               == declarator_obstack_base);
3060
3061   /* All went well.  */
3062   return success;
3063 }
3064
3065 /* Expressions [gram.expr] */
3066
3067 /* Parse a primary-expression.
3068
3069    primary-expression:
3070      literal
3071      this
3072      ( expression )
3073      id-expression
3074
3075    GNU Extensions:
3076
3077    primary-expression:
3078      ( compound-statement )
3079      __builtin_va_arg ( assignment-expression , type-id )
3080      __builtin_offsetof ( type-id , offsetof-expression )
3081
3082    C++ Extensions:
3083      __has_nothrow_assign ( type-id )   
3084      __has_nothrow_constructor ( type-id )
3085      __has_nothrow_copy ( type-id )
3086      __has_trivial_assign ( type-id )   
3087      __has_trivial_constructor ( type-id )
3088      __has_trivial_copy ( type-id )
3089      __has_trivial_destructor ( type-id )
3090      __has_virtual_destructor ( type-id )     
3091      __is_abstract ( type-id )
3092      __is_base_of ( type-id , type-id )
3093      __is_class ( type-id )
3094      __is_convertible_to ( type-id , type-id )     
3095      __is_empty ( type-id )
3096      __is_enum ( type-id )
3097      __is_pod ( type-id )
3098      __is_polymorphic ( type-id )
3099      __is_union ( type-id )
3100
3101    Objective-C++ Extension:
3102
3103    primary-expression:
3104      objc-expression
3105
3106    literal:
3107      __null
3108
3109    ADDRESS_P is true iff this expression was immediately preceded by
3110    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3111    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3112    true iff this expression is a template argument.
3113
3114    Returns a representation of the expression.  Upon return, *IDK
3115    indicates what kind of id-expression (if any) was present.  */
3116
3117 static tree
3118 cp_parser_primary_expression (cp_parser *parser,
3119                               bool address_p,
3120                               bool cast_p,
3121                               bool template_arg_p,
3122                               cp_id_kind *idk)
3123 {
3124   cp_token *token = NULL;
3125
3126   /* Assume the primary expression is not an id-expression.  */
3127   *idk = CP_ID_KIND_NONE;
3128
3129   /* Peek at the next token.  */
3130   token = cp_lexer_peek_token (parser->lexer);
3131   switch (token->type)
3132     {
3133       /* literal:
3134            integer-literal
3135            character-literal
3136            floating-literal
3137            string-literal
3138            boolean-literal  */
3139     case CPP_CHAR:
3140     case CPP_CHAR16:
3141     case CPP_CHAR32:
3142     case CPP_WCHAR:
3143     case CPP_NUMBER:
3144       token = cp_lexer_consume_token (parser->lexer);
3145       if (TREE_CODE (token->u.value) == FIXED_CST)
3146         {
3147           error ("%Hfixed-point types not supported in C++",
3148                  &token->location);
3149           return error_mark_node;
3150         }
3151       /* Floating-point literals are only allowed in an integral
3152          constant expression if they are cast to an integral or
3153          enumeration type.  */
3154       if (TREE_CODE (token->u.value) == REAL_CST
3155           && parser->integral_constant_expression_p
3156           && pedantic)
3157         {
3158           /* CAST_P will be set even in invalid code like "int(2.7 +
3159              ...)".   Therefore, we have to check that the next token
3160              is sure to end the cast.  */
3161           if (cast_p)
3162             {
3163               cp_token *next_token;
3164
3165               next_token = cp_lexer_peek_token (parser->lexer);
3166               if (/* The comma at the end of an
3167                      enumerator-definition.  */
3168                   next_token->type != CPP_COMMA
3169                   /* The curly brace at the end of an enum-specifier.  */
3170                   && next_token->type != CPP_CLOSE_BRACE
3171                   /* The end of a statement.  */
3172                   && next_token->type != CPP_SEMICOLON
3173                   /* The end of the cast-expression.  */
3174                   && next_token->type != CPP_CLOSE_PAREN
3175                   /* The end of an array bound.  */
3176                   && next_token->type != CPP_CLOSE_SQUARE
3177                   /* The closing ">" in a template-argument-list.  */
3178                   && (next_token->type != CPP_GREATER
3179                       || parser->greater_than_is_operator_p)
3180                   /* C++0x only: A ">>" treated like two ">" tokens,
3181                      in a template-argument-list.  */
3182                   && (next_token->type != CPP_RSHIFT
3183                       || (cxx_dialect == cxx98)
3184                       || parser->greater_than_is_operator_p))
3185                 cast_p = false;
3186             }
3187
3188           /* If we are within a cast, then the constraint that the
3189              cast is to an integral or enumeration type will be
3190              checked at that point.  If we are not within a cast, then
3191              this code is invalid.  */
3192           if (!cast_p)
3193             cp_parser_non_integral_constant_expression
3194               (parser, "floating-point literal");
3195         }
3196       return token->u.value;
3197
3198     case CPP_STRING:
3199     case CPP_STRING16:
3200     case CPP_STRING32:
3201     case CPP_WSTRING:
3202       /* ??? Should wide strings be allowed when parser->translate_strings_p
3203          is false (i.e. in attributes)?  If not, we can kill the third
3204          argument to cp_parser_string_literal.  */
3205       return cp_parser_string_literal (parser,
3206                                        parser->translate_strings_p,
3207                                        true);
3208
3209     case CPP_OPEN_PAREN:
3210       {
3211         tree expr;
3212         bool saved_greater_than_is_operator_p;
3213
3214         /* Consume the `('.  */
3215         cp_lexer_consume_token (parser->lexer);
3216         /* Within a parenthesized expression, a `>' token is always
3217            the greater-than operator.  */
3218         saved_greater_than_is_operator_p
3219           = parser->greater_than_is_operator_p;
3220         parser->greater_than_is_operator_p = true;
3221         /* If we see `( { ' then we are looking at the beginning of
3222            a GNU statement-expression.  */
3223         if (cp_parser_allow_gnu_extensions_p (parser)
3224             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3225           {
3226             /* Statement-expressions are not allowed by the standard.  */
3227             pedwarn (token->location, OPT_pedantic, 
3228                      "ISO C++ forbids braced-groups within expressions");
3229
3230             /* And they're not allowed outside of a function-body; you
3231                cannot, for example, write:
3232
3233                  int i = ({ int j = 3; j + 1; });
3234
3235                at class or namespace scope.  */
3236             if (!parser->in_function_body
3237                 || parser->in_template_argument_list_p)
3238               {
3239                 error ("%Hstatement-expressions are not allowed outside "
3240                        "functions nor in template-argument lists",
3241                        &token->location);
3242                 cp_parser_skip_to_end_of_block_or_statement (parser);
3243                 expr = error_mark_node;
3244               }
3245             else
3246               {
3247                 /* Start the statement-expression.  */
3248                 expr = begin_stmt_expr ();
3249                 /* Parse the compound-statement.  */
3250                 cp_parser_compound_statement (parser, expr, false);
3251                 /* Finish up.  */
3252                 expr = finish_stmt_expr (expr, false);
3253               }
3254           }
3255         else
3256           {
3257             /* Parse the parenthesized expression.  */
3258             expr = cp_parser_expression (parser, cast_p, idk);
3259             /* Let the front end know that this expression was
3260                enclosed in parentheses. This matters in case, for
3261                example, the expression is of the form `A::B', since
3262                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3263                not.  */
3264             finish_parenthesized_expr (expr);
3265           }
3266         /* The `>' token might be the end of a template-id or
3267            template-parameter-list now.  */
3268         parser->greater_than_is_operator_p
3269           = saved_greater_than_is_operator_p;
3270         /* Consume the `)'.  */
3271         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3272           cp_parser_skip_to_end_of_statement (parser);
3273
3274         return expr;
3275       }
3276
3277     case CPP_KEYWORD:
3278       switch (token->keyword)
3279         {
3280           /* These two are the boolean literals.  */
3281         case RID_TRUE:
3282           cp_lexer_consume_token (parser->lexer);
3283           return boolean_true_node;
3284         case RID_FALSE:
3285           cp_lexer_consume_token (parser->lexer);
3286           return boolean_false_node;
3287
3288           /* The `__null' literal.  */
3289         case RID_NULL:
3290           cp_lexer_consume_token (parser->lexer);
3291           return null_node;
3292
3293           /* Recognize the `this' keyword.  */
3294         case RID_THIS:
3295           cp_lexer_consume_token (parser->lexer);
3296           if (parser->local_variables_forbidden_p)
3297             {
3298               error ("%H%<this%> may not be used in this context",
3299                      &token->location);
3300               return error_mark_node;
3301             }
3302           /* Pointers cannot appear in constant-expressions.  */
3303           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3304             return error_mark_node;
3305           return finish_this_expr ();
3306
3307           /* The `operator' keyword can be the beginning of an
3308              id-expression.  */
3309         case RID_OPERATOR:
3310           goto id_expression;
3311
3312         case RID_FUNCTION_NAME:
3313         case RID_PRETTY_FUNCTION_NAME:
3314         case RID_C99_FUNCTION_NAME:
3315           {
3316             const char *name;
3317
3318             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319                __func__ are the names of variables -- but they are
3320                treated specially.  Therefore, they are handled here,
3321                rather than relying on the generic id-expression logic
3322                below.  Grammatically, these names are id-expressions.
3323
3324                Consume the token.  */
3325             token = cp_lexer_consume_token (parser->lexer);
3326
3327             switch (token->keyword)
3328               {
3329               case RID_FUNCTION_NAME:
3330                 name = "%<__FUNCTION__%>";
3331                 break;
3332               case RID_PRETTY_FUNCTION_NAME:
3333                 name = "%<__PRETTY_FUNCTION__%>";
3334                 break;
3335               case RID_C99_FUNCTION_NAME:
3336                 name = "%<__func__%>";
3337                 break;
3338               default:
3339                 gcc_unreachable ();
3340               }
3341
3342             if (cp_parser_non_integral_constant_expression (parser, name))
3343               return error_mark_node;
3344
3345             /* Look up the name.  */
3346             return finish_fname (token->u.value);
3347           }
3348
3349         case RID_VA_ARG:
3350           {
3351             tree expression;
3352             tree type;
3353
3354             /* The `__builtin_va_arg' construct is used to handle
3355                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3356             cp_lexer_consume_token (parser->lexer);
3357             /* Look for the opening `('.  */
3358             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3359             /* Now, parse the assignment-expression.  */
3360             expression = cp_parser_assignment_expression (parser,
3361                                                           /*cast_p=*/false, NULL);
3362             /* Look for the `,'.  */
3363             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3364             /* Parse the type-id.  */
3365             type = cp_parser_type_id (parser);
3366             /* Look for the closing `)'.  */
3367             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3368             /* Using `va_arg' in a constant-expression is not
3369                allowed.  */
3370             if (cp_parser_non_integral_constant_expression (parser,
3371                                                             "%<va_arg%>"))
3372               return error_mark_node;
3373             return build_x_va_arg (expression, type);
3374           }
3375
3376         case RID_OFFSETOF:
3377           return cp_parser_builtin_offsetof (parser);
3378
3379         case RID_HAS_NOTHROW_ASSIGN:
3380         case RID_HAS_NOTHROW_CONSTRUCTOR:
3381         case RID_HAS_NOTHROW_COPY:        
3382         case RID_HAS_TRIVIAL_ASSIGN:
3383         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384         case RID_HAS_TRIVIAL_COPY:        
3385         case RID_HAS_TRIVIAL_DESTRUCTOR:
3386         case RID_HAS_VIRTUAL_DESTRUCTOR:
3387         case RID_IS_ABSTRACT:
3388         case RID_IS_BASE_OF:
3389         case RID_IS_CLASS:
3390         case RID_IS_CONVERTIBLE_TO:
3391         case RID_IS_EMPTY:
3392         case RID_IS_ENUM:
3393         case RID_IS_POD:
3394         case RID_IS_POLYMORPHIC:
3395         case RID_IS_UNION:
3396           return cp_parser_trait_expr (parser, token->keyword);
3397
3398         /* Objective-C++ expressions.  */
3399         case RID_AT_ENCODE:
3400         case RID_AT_PROTOCOL:
3401         case RID_AT_SELECTOR:
3402           return cp_parser_objc_expression (parser);
3403
3404         default:
3405           cp_parser_error (parser, "expected primary-expression");
3406           return error_mark_node;
3407         }
3408
3409       /* An id-expression can start with either an identifier, a
3410          `::' as the beginning of a qualified-id, or the "operator"
3411          keyword.  */
3412     case CPP_NAME:
3413     case CPP_SCOPE:
3414     case CPP_TEMPLATE_ID:
3415     case CPP_NESTED_NAME_SPECIFIER:
3416       {
3417         tree id_expression;
3418         tree decl;
3419         const char *error_msg;
3420         bool template_p;
3421         bool done;
3422         cp_token *id_expr_token;
3423
3424       id_expression:
3425         /* Parse the id-expression.  */
3426         id_expression
3427           = cp_parser_id_expression (parser,
3428                                      /*template_keyword_p=*/false,
3429                                      /*check_dependency_p=*/true,
3430                                      &template_p,
3431                                      /*declarator_p=*/false,
3432                                      /*optional_p=*/false);
3433         if (id_expression == error_mark_node)
3434           return error_mark_node;
3435         id_expr_token = token;
3436         token = cp_lexer_peek_token (parser->lexer);
3437         done = (token->type != CPP_OPEN_SQUARE
3438                 && token->type != CPP_OPEN_PAREN
3439                 && token->type != CPP_DOT
3440                 && token->type != CPP_DEREF
3441                 && token->type != CPP_PLUS_PLUS
3442                 && token->type != CPP_MINUS_MINUS);
3443         /* If we have a template-id, then no further lookup is
3444            required.  If the template-id was for a template-class, we
3445            will sometimes have a TYPE_DECL at this point.  */
3446         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447                  || TREE_CODE (id_expression) == TYPE_DECL)
3448           decl = id_expression;
3449         /* Look up the name.  */
3450         else
3451           {
3452             tree ambiguous_decls;
3453
3454             decl = cp_parser_lookup_name (parser, id_expression,
3455                                           none_type,
3456                                           template_p,
3457                                           /*is_namespace=*/false,
3458                                           /*check_dependency=*/true,
3459                                           &ambiguous_decls,
3460                                           id_expr_token->location);
3461             /* If the lookup was ambiguous, an error will already have
3462                been issued.  */
3463             if (ambiguous_decls)
3464               return error_mark_node;
3465
3466             /* In Objective-C++, an instance variable (ivar) may be preferred
3467                to whatever cp_parser_lookup_name() found.  */
3468             decl = objc_lookup_ivar (decl, id_expression);
3469
3470             /* If name lookup gives us a SCOPE_REF, then the
3471                qualifying scope was dependent.  */
3472             if (TREE_CODE (decl) == SCOPE_REF)
3473               {
3474                 /* At this point, we do not know if DECL is a valid
3475                    integral constant expression.  We assume that it is
3476                    in fact such an expression, so that code like:
3477
3478                       template <int N> struct A {
3479                         int a[B<N>::i];
3480                       };
3481                      
3482                    is accepted.  At template-instantiation time, we
3483                    will check that B<N>::i is actually a constant.  */
3484                 return decl;
3485               }
3486             /* Check to see if DECL is a local variable in a context
3487                where that is forbidden.  */
3488             if (parser->local_variables_forbidden_p
3489                 && local_variable_p (decl))
3490               {
3491                 /* It might be that we only found DECL because we are
3492                    trying to be generous with pre-ISO scoping rules.
3493                    For example, consider:
3494
3495                      int i;
3496                      void g() {
3497                        for (int i = 0; i < 10; ++i) {}
3498                        extern void f(int j = i);
3499                      }
3500
3501                    Here, name look up will originally find the out
3502                    of scope `i'.  We need to issue a warning message,
3503                    but then use the global `i'.  */
3504                 decl = check_for_out_of_scope_variable (decl);
3505                 if (local_variable_p (decl))
3506                   {
3507                     error ("%Hlocal variable %qD may not appear in this context",
3508                            &id_expr_token->location, decl);
3509                     return error_mark_node;
3510                   }
3511               }
3512           }
3513
3514         decl = (finish_id_expression
3515                 (id_expression, decl, parser->scope,
3516                  idk,
3517                  parser->integral_constant_expression_p,
3518                  parser->allow_non_integral_constant_expression_p,
3519                  &parser->non_integral_constant_expression_p,
3520                  template_p, done, address_p,
3521                  template_arg_p,
3522                  &error_msg,
3523                  id_expr_token->location));
3524         if (error_msg)
3525           cp_parser_error (parser, error_msg);
3526         return decl;
3527       }
3528
3529       /* Anything else is an error.  */
3530     default:
3531       /* ...unless we have an Objective-C++ message or string literal,
3532          that is.  */
3533       if (c_dialect_objc ()
3534           && (token->type == CPP_OPEN_SQUARE
3535               || token->type == CPP_OBJC_STRING))
3536         return cp_parser_objc_expression (parser);
3537
3538       cp_parser_error (parser, "expected primary-expression");
3539       return error_mark_node;
3540     }
3541 }
3542
3543 /* Parse an id-expression.
3544
3545    id-expression:
3546      unqualified-id
3547      qualified-id
3548
3549    qualified-id:
3550      :: [opt] nested-name-specifier template [opt] unqualified-id
3551      :: identifier
3552      :: operator-function-id
3553      :: template-id
3554
3555    Return a representation of the unqualified portion of the
3556    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3557    a `::' or nested-name-specifier.
3558
3559    Often, if the id-expression was a qualified-id, the caller will
3560    want to make a SCOPE_REF to represent the qualified-id.  This
3561    function does not do this in order to avoid wastefully creating
3562    SCOPE_REFs when they are not required.
3563
3564    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3565    `template' keyword.
3566
3567    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3568    uninstantiated templates.
3569
3570    If *TEMPLATE_P is non-NULL, it is set to true iff the
3571    `template' keyword is used to explicitly indicate that the entity
3572    named is a template.
3573
3574    If DECLARATOR_P is true, the id-expression is appearing as part of
3575    a declarator, rather than as part of an expression.  */
3576
3577 static tree
3578 cp_parser_id_expression (cp_parser *parser,
3579                          bool template_keyword_p,
3580                          bool check_dependency_p,
3581                          bool *template_p,
3582                          bool declarator_p,
3583                          bool optional_p)
3584 {
3585   bool global_scope_p;
3586   bool nested_name_specifier_p;
3587
3588   /* Assume the `template' keyword was not used.  */
3589   if (template_p)
3590     *template_p = template_keyword_p;
3591
3592   /* Look for the optional `::' operator.  */
3593   global_scope_p
3594     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3595        != NULL_TREE);
3596   /* Look for the optional nested-name-specifier.  */
3597   nested_name_specifier_p
3598     = (cp_parser_nested_name_specifier_opt (parser,
3599                                             /*typename_keyword_p=*/false,
3600                                             check_dependency_p,
3601                                             /*type_p=*/false,
3602                                             declarator_p)
3603        != NULL_TREE);
3604   /* If there is a nested-name-specifier, then we are looking at
3605      the first qualified-id production.  */
3606   if (nested_name_specifier_p)
3607     {
3608       tree saved_scope;
3609       tree saved_object_scope;
3610       tree saved_qualifying_scope;
3611       tree unqualified_id;
3612       bool is_template;
3613
3614       /* See if the next token is the `template' keyword.  */
3615       if (!template_p)
3616         template_p = &is_template;
3617       *template_p = cp_parser_optional_template_keyword (parser);
3618       /* Name lookup we do during the processing of the
3619          unqualified-id might obliterate SCOPE.  */
3620       saved_scope = parser->scope;
3621       saved_object_scope = parser->object_scope;
3622       saved_qualifying_scope = parser->qualifying_scope;
3623       /* Process the final unqualified-id.  */
3624       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3625                                                  check_dependency_p,
3626                                                  declarator_p,
3627                                                  /*optional_p=*/false);
3628       /* Restore the SAVED_SCOPE for our caller.  */
3629       parser->scope = saved_scope;
3630       parser->object_scope = saved_object_scope;
3631       parser->qualifying_scope = saved_qualifying_scope;
3632
3633       return unqualified_id;
3634     }
3635   /* Otherwise, if we are in global scope, then we are looking at one
3636      of the other qualified-id productions.  */
3637   else if (global_scope_p)
3638     {
3639       cp_token *token;
3640       tree id;
3641
3642       /* Peek at the next token.  */
3643       token = cp_lexer_peek_token (parser->lexer);
3644
3645       /* If it's an identifier, and the next token is not a "<", then
3646          we can avoid the template-id case.  This is an optimization
3647          for this common case.  */
3648       if (token->type == CPP_NAME
3649           && !cp_parser_nth_token_starts_template_argument_list_p
3650                (parser, 2))
3651         return cp_parser_identifier (parser);
3652
3653       cp_parser_parse_tentatively (parser);
3654       /* Try a template-id.  */
3655       id = cp_parser_template_id (parser,
3656                                   /*template_keyword_p=*/false,
3657                                   /*check_dependency_p=*/true,
3658                                   declarator_p);
3659       /* If that worked, we're done.  */
3660       if (cp_parser_parse_definitely (parser))
3661         return id;
3662
3663       /* Peek at the next token.  (Changes in the token buffer may
3664          have invalidated the pointer obtained above.)  */
3665       token = cp_lexer_peek_token (parser->lexer);
3666
3667       switch (token->type)
3668         {
3669         case CPP_NAME:
3670           return cp_parser_identifier (parser);
3671
3672         case CPP_KEYWORD:
3673           if (token->keyword == RID_OPERATOR)
3674             return cp_parser_operator_function_id (parser);
3675           /* Fall through.  */
3676
3677         default:
3678           cp_parser_error (parser, "expected id-expression");
3679           return error_mark_node;
3680         }
3681     }
3682   else
3683     return cp_parser_unqualified_id (parser, template_keyword_p,
3684                                      /*check_dependency_p=*/true,
3685                                      declarator_p,
3686                                      optional_p);
3687 }
3688
3689 /* Parse an unqualified-id.
3690
3691    unqualified-id:
3692      identifier
3693      operator-function-id
3694      conversion-function-id
3695      ~ class-name
3696      template-id
3697
3698    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699    keyword, in a construct like `A::template ...'.
3700
3701    Returns a representation of unqualified-id.  For the `identifier'
3702    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3703    production a BIT_NOT_EXPR is returned; the operand of the
3704    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3705    other productions, see the documentation accompanying the
3706    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3707    names are looked up in uninstantiated templates.  If DECLARATOR_P
3708    is true, the unqualified-id is appearing as part of a declarator,
3709    rather than as part of an expression.  */
3710
3711 static tree
3712 cp_parser_unqualified_id (cp_parser* parser,
3713                           bool template_keyword_p,
3714                           bool check_dependency_p,
3715                           bool declarator_p,
3716                           bool optional_p)
3717 {
3718   cp_token *token;
3719
3720   /* Peek at the next token.  */
3721   token = cp_lexer_peek_token (parser->lexer);
3722
3723   switch (token->type)
3724     {
3725     case CPP_NAME:
3726       {
3727         tree id;
3728
3729         /* We don't know yet whether or not this will be a
3730            template-id.  */
3731         cp_parser_parse_tentatively (parser);
3732         /* Try a template-id.  */
3733         id = cp_parser_template_id (parser, template_keyword_p,
3734                                     check_dependency_p,
3735                                     declarator_p);
3736         /* If it worked, we're done.  */
3737         if (cp_parser_parse_definitely (parser))
3738           return id;
3739         /* Otherwise, it's an ordinary identifier.  */
3740         return cp_parser_identifier (parser);
3741       }
3742
3743     case CPP_TEMPLATE_ID:
3744       return cp_parser_template_id (parser, template_keyword_p,
3745                                     check_dependency_p,
3746                                     declarator_p);
3747
3748     case CPP_COMPL:
3749       {
3750         tree type_decl;
3751         tree qualifying_scope;
3752         tree object_scope;
3753         tree scope;
3754         bool done;
3755
3756         /* Consume the `~' token.  */
3757         cp_lexer_consume_token (parser->lexer);
3758         /* Parse the class-name.  The standard, as written, seems to
3759            say that:
3760
3761              template <typename T> struct S { ~S (); };
3762              template <typename T> S<T>::~S() {}
3763
3764            is invalid, since `~' must be followed by a class-name, but
3765            `S<T>' is dependent, and so not known to be a class.
3766            That's not right; we need to look in uninstantiated
3767            templates.  A further complication arises from:
3768
3769              template <typename T> void f(T t) {
3770                t.T::~T();
3771              }
3772
3773            Here, it is not possible to look up `T' in the scope of `T'
3774            itself.  We must look in both the current scope, and the
3775            scope of the containing complete expression.
3776
3777            Yet another issue is:
3778
3779              struct S {
3780                int S;
3781                ~S();
3782              };
3783
3784              S::~S() {}
3785
3786            The standard does not seem to say that the `S' in `~S'
3787            should refer to the type `S' and not the data member
3788            `S::S'.  */
3789
3790         /* DR 244 says that we look up the name after the "~" in the
3791            same scope as we looked up the qualifying name.  That idea
3792            isn't fully worked out; it's more complicated than that.  */
3793         scope = parser->scope;
3794         object_scope = parser->object_scope;
3795         qualifying_scope = parser->qualifying_scope;
3796
3797         /* Check for invalid scopes.  */
3798         if (scope == error_mark_node)
3799           {
3800             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801               cp_lexer_consume_token (parser->lexer);
3802             return error_mark_node;
3803           }
3804         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3805           {
3806             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3807               error ("%Hscope %qT before %<~%> is not a class-name",
3808                      &token->location, scope);
3809             cp_parser_simulate_error (parser);
3810             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811               cp_lexer_consume_token (parser->lexer);
3812             return error_mark_node;
3813           }
3814         gcc_assert (!scope || TYPE_P (scope));
3815
3816         /* If the name is of the form "X::~X" it's OK.  */
3817         token = cp_lexer_peek_token (parser->lexer);
3818         if (scope
3819             && token->type == CPP_NAME
3820             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3821                 == CPP_OPEN_PAREN)
3822             && constructor_name_p (token->u.value, scope))
3823           {
3824             cp_lexer_consume_token (parser->lexer);
3825             return build_nt (BIT_NOT_EXPR, scope);
3826           }
3827
3828         /* If there was an explicit qualification (S::~T), first look
3829            in the scope given by the qualification (i.e., S).  */
3830         done = false;
3831         type_decl = NULL_TREE;
3832         if (scope)
3833           {
3834             cp_parser_parse_tentatively (parser);
3835             type_decl = cp_parser_class_name (parser,
3836                                               /*typename_keyword_p=*/false,
3837                                               /*template_keyword_p=*/false,
3838                                               none_type,
3839                                               /*check_dependency=*/false,
3840                                               /*class_head_p=*/false,
3841                                               declarator_p);
3842             if (cp_parser_parse_definitely (parser))
3843               done = true;
3844           }
3845         /* In "N::S::~S", look in "N" as well.  */
3846         if (!done && scope && qualifying_scope)
3847           {
3848             cp_parser_parse_tentatively (parser);
3849             parser->scope = qualifying_scope;
3850             parser->object_scope = NULL_TREE;
3851             parser->qualifying_scope = NULL_TREE;
3852             type_decl
3853               = cp_parser_class_name (parser,
3854                                       /*typename_keyword_p=*/false,
3855                                       /*template_keyword_p=*/false,
3856                                       none_type,
3857                                       /*check_dependency=*/false,
3858                                       /*class_head_p=*/false,
3859                                       declarator_p);
3860             if (cp_parser_parse_definitely (parser))
3861               done = true;
3862           }
3863         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3864         else if (!done && object_scope)
3865           {
3866             cp_parser_parse_tentatively (parser);
3867             parser->scope = object_scope;
3868             parser->object_scope = NULL_TREE;
3869             parser->qualifying_scope = NULL_TREE;
3870             type_decl
3871               = cp_parser_class_name (parser,
3872                                       /*typename_keyword_p=*/false,
3873                                       /*template_keyword_p=*/false,
3874                                       none_type,
3875                                       /*check_dependency=*/false,
3876                                       /*class_head_p=*/false,
3877                                       declarator_p);
3878             if (cp_parser_parse_definitely (parser))
3879               done = true;
3880           }
3881         /* Look in the surrounding context.  */
3882         if (!done)
3883           {
3884             parser->scope = NULL_TREE;
3885             parser->object_scope = NULL_TREE;
3886             parser->qualifying_scope = NULL_TREE;
3887             if (processing_template_decl)
3888               cp_parser_parse_tentatively (parser);
3889             type_decl
3890               = cp_parser_class_name (parser,
3891                                       /*typename_keyword_p=*/false,
3892                                       /*template_keyword_p=*/false,
3893                                       none_type,
3894                                       /*check_dependency=*/false,
3895                                       /*class_head_p=*/false,
3896                                       declarator_p);
3897             if (processing_template_decl
3898                 && ! cp_parser_parse_definitely (parser))
3899               {
3900                 /* We couldn't find a type with this name, so just accept
3901                    it and check for a match at instantiation time.  */
3902                 type_decl = cp_parser_identifier (parser);
3903                 if (type_decl != error_mark_node)
3904                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3905                 return type_decl;
3906               }
3907           }
3908         /* If an error occurred, assume that the name of the
3909            destructor is the same as the name of the qualifying
3910            class.  That allows us to keep parsing after running
3911            into ill-formed destructor names.  */
3912         if (type_decl == error_mark_node && scope)
3913           return build_nt (BIT_NOT_EXPR, scope);
3914         else if (type_decl == error_mark_node)
3915           return error_mark_node;
3916
3917         /* Check that destructor name and scope match.  */
3918         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3919           {
3920             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3921               error ("%Hdeclaration of %<~%T%> as member of %qT",
3922                      &token->location, type_decl, scope);
3923             cp_parser_simulate_error (parser);
3924             return error_mark_node;
3925           }
3926
3927         /* [class.dtor]
3928
3929            A typedef-name that names a class shall not be used as the
3930            identifier in the declarator for a destructor declaration.  */
3931         if (declarator_p
3932             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3933             && !DECL_SELF_REFERENCE_P (type_decl)
3934             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3935           error ("%Htypedef-name %qD used as destructor declarator",
3936                  &token->location, type_decl);
3937
3938         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3939       }
3940
3941     case CPP_KEYWORD:
3942       if (token->keyword == RID_OPERATOR)
3943         {
3944           tree id;
3945
3946           /* This could be a template-id, so we try that first.  */
3947           cp_parser_parse_tentatively (parser);
3948           /* Try a template-id.  */
3949           id = cp_parser_template_id (parser, template_keyword_p,
3950                                       /*check_dependency_p=*/true,
3951                                       declarator_p);
3952           /* If that worked, we're done.  */
3953           if (cp_parser_parse_definitely (parser))
3954             return id;
3955           /* We still don't know whether we're looking at an
3956              operator-function-id or a conversion-function-id.  */
3957           cp_parser_parse_tentatively (parser);
3958           /* Try an operator-function-id.  */
3959           id = cp_parser_operator_function_id (parser);
3960           /* If that didn't work, try a conversion-function-id.  */
3961           if (!cp_parser_parse_definitely (parser))
3962             id = cp_parser_conversion_function_id (parser);
3963
3964           return id;
3965         }
3966       /* Fall through.  */
3967
3968     default:
3969       if (optional_p)
3970         return NULL_TREE;
3971       cp_parser_error (parser, "expected unqualified-id");
3972       return error_mark_node;
3973     }
3974 }
3975
3976 /* Parse an (optional) nested-name-specifier.
3977
3978    nested-name-specifier: [C++98]
3979      class-or-namespace-name :: nested-name-specifier [opt]
3980      class-or-namespace-name :: template nested-name-specifier [opt]
3981
3982    nested-name-specifier: [C++0x]
3983      type-name ::
3984      namespace-name ::
3985      nested-name-specifier identifier ::
3986      nested-name-specifier template [opt] simple-template-id ::
3987
3988    PARSER->SCOPE should be set appropriately before this function is
3989    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3991    in name lookups.
3992
3993    Sets PARSER->SCOPE to the class (TYPE) or namespace
3994    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995    it unchanged if there is no nested-name-specifier.  Returns the new
3996    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3997
3998    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999    part of a declaration and/or decl-specifier.  */
4000
4001 static tree
4002 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003                                      bool typename_keyword_p,
4004                                      bool check_dependency_p,
4005                                      bool type_p,
4006                                      bool is_declaration)
4007 {
4008   bool success = false;
4009   cp_token_position start = 0;
4010   cp_token *token;
4011
4012   /* Remember where the nested-name-specifier starts.  */
4013   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4014     {
4015       start = cp_lexer_token_position (parser->lexer, false);
4016       push_deferring_access_checks (dk_deferred);
4017     }
4018
4019   while (true)
4020     {
4021       tree new_scope;
4022       tree old_scope;
4023       tree saved_qualifying_scope;
4024       bool template_keyword_p;
4025
4026       /* Spot cases that cannot be the beginning of a
4027          nested-name-specifier.  */
4028       token = cp_lexer_peek_token (parser->lexer);
4029
4030       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031          the already parsed nested-name-specifier.  */
4032       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4033         {
4034           /* Grab the nested-name-specifier and continue the loop.  */
4035           cp_parser_pre_parsed_nested_name_specifier (parser);
4036           /* If we originally encountered this nested-name-specifier
4037              with IS_DECLARATION set to false, we will not have
4038              resolved TYPENAME_TYPEs, so we must do so here.  */
4039           if (is_declaration
4040               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4041             {
4042               new_scope = resolve_typename_type (parser->scope,
4043                                                  /*only_current_p=*/false);
4044               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4045                 parser->scope = new_scope;
4046             }
4047           success = true;
4048           continue;
4049         }
4050
4051       /* Spot cases that cannot be the beginning of a
4052          nested-name-specifier.  On the second and subsequent times
4053          through the loop, we look for the `template' keyword.  */
4054       if (success && token->keyword == RID_TEMPLATE)
4055         ;
4056       /* A template-id can start a nested-name-specifier.  */
4057       else if (token->type == CPP_TEMPLATE_ID)
4058         ;
4059       else
4060         {
4061           /* If the next token is not an identifier, then it is
4062              definitely not a type-name or namespace-name.  */
4063           if (token->type != CPP_NAME)
4064             break;
4065           /* If the following token is neither a `<' (to begin a
4066              template-id), nor a `::', then we are not looking at a
4067              nested-name-specifier.  */
4068           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4069           if (token->type != CPP_SCOPE
4070               && !cp_parser_nth_token_starts_template_argument_list_p
4071                   (parser, 2))
4072             break;
4073         }
4074
4075       /* The nested-name-specifier is optional, so we parse
4076          tentatively.  */
4077       cp_parser_parse_tentatively (parser);
4078
4079       /* Look for the optional `template' keyword, if this isn't the
4080          first time through the loop.  */
4081       if (success)
4082         template_keyword_p = cp_parser_optional_template_keyword (parser);
4083       else
4084         template_keyword_p = false;
4085
4086       /* Save the old scope since the name lookup we are about to do
4087          might destroy it.  */
4088       old_scope = parser->scope;
4089       saved_qualifying_scope = parser->qualifying_scope;
4090       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091          look up names in "X<T>::I" in order to determine that "Y" is
4092          a template.  So, if we have a typename at this point, we make
4093          an effort to look through it.  */
4094       if (is_declaration
4095           && !typename_keyword_p
4096           && parser->scope
4097           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4098         parser->scope = resolve_typename_type (parser->scope,
4099                                                /*only_current_p=*/false);
4100       /* Parse the qualifying entity.  */
4101       new_scope
4102         = cp_parser_qualifying_entity (parser,
4103                                        typename_keyword_p,
4104                                        template_keyword_p,
4105                                        check_dependency_p,
4106                                        type_p,
4107                                        is_declaration);
4108       /* Look for the `::' token.  */
4109       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4110
4111       /* If we found what we wanted, we keep going; otherwise, we're
4112          done.  */
4113       if (!cp_parser_parse_definitely (parser))
4114         {
4115           bool error_p = false;
4116
4117           /* Restore the OLD_SCOPE since it was valid before the
4118              failed attempt at finding the last
4119              class-or-namespace-name.  */
4120           parser->scope = old_scope;
4121           parser->qualifying_scope = saved_qualifying_scope;
4122           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4123             break;
4124           /* If the next token is an identifier, and the one after
4125              that is a `::', then any valid interpretation would have
4126              found a class-or-namespace-name.  */
4127           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4128                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4129                      == CPP_SCOPE)
4130                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4131                      != CPP_COMPL))
4132             {
4133               token = cp_lexer_consume_token (parser->lexer);
4134               if (!error_p)
4135                 {
4136                   if (!token->ambiguous_p)
4137                     {
4138                       tree decl;
4139                       tree ambiguous_decls;
4140
4141                       decl = cp_parser_lookup_name (parser, token->u.value,
4142                                                     none_type,
4143                                                     /*is_template=*/false,
4144                                                     /*is_namespace=*/false,
4145                                                     /*check_dependency=*/true,
4146                                                     &ambiguous_decls,
4147                                                     token->location);
4148                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4149                         error ("%H%qD used without template parameters",
4150                                &token->location, decl);
4151                       else if (ambiguous_decls)
4152                         {
4153                           error ("%Hreference to %qD is ambiguous",
4154                                  &token->location, token->u.value);
4155                           print_candidates (ambiguous_decls);
4156                           decl = error_mark_node;
4157                         }
4158                       else
4159                         {
4160                           const char* msg = "is not a class or namespace";
4161                           if (cxx_dialect != cxx98)
4162                             msg = "is not a class, namespace, or enumeration";
4163                           cp_parser_name_lookup_error
4164                             (parser, token->u.value, decl, msg,
4165                              token->location);
4166                         }
4167                     }
4168                   parser->scope = error_mark_node;
4169                   error_p = true;
4170                   /* Treat this as a successful nested-name-specifier
4171                      due to:
4172
4173                      [basic.lookup.qual]
4174
4175                      If the name found is not a class-name (clause
4176                      _class_) or namespace-name (_namespace.def_), the
4177                      program is ill-formed.  */
4178                   success = true;
4179                 }
4180               cp_lexer_consume_token (parser->lexer);
4181             }
4182           break;
4183         }
4184       /* We've found one valid nested-name-specifier.  */
4185       success = true;
4186       /* Name lookup always gives us a DECL.  */
4187       if (TREE_CODE (new_scope) == TYPE_DECL)
4188         new_scope = TREE_TYPE (new_scope);
4189       /* Uses of "template" must be followed by actual templates.  */
4190       if (template_keyword_p
4191           && !(CLASS_TYPE_P (new_scope)
4192                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197                    == TEMPLATE_ID_EXPR)))
4198         permerror (input_location, TYPE_P (new_scope)
4199                    ? "%qT is not a template"
4200                    : "%qD is not a template",
4201                    new_scope);
4202       /* If it is a class scope, try to complete it; we are about to
4203          be looking up names inside the class.  */
4204       if (TYPE_P (new_scope)
4205           /* Since checking types for dependency can be expensive,
4206              avoid doing it if the type is already complete.  */
4207           && !COMPLETE_TYPE_P (new_scope)
4208           /* Do not try to complete dependent types.  */
4209           && !dependent_type_p (new_scope))
4210         {
4211           new_scope = complete_type (new_scope);
4212           /* If it is a typedef to current class, use the current
4213              class instead, as the typedef won't have any names inside
4214              it yet.  */
4215           if (!COMPLETE_TYPE_P (new_scope)
4216               && currently_open_class (new_scope))
4217             new_scope = TYPE_MAIN_VARIANT (new_scope);
4218         }
4219       /* Make sure we look in the right scope the next time through
4220          the loop.  */
4221       parser->scope = new_scope;
4222     }
4223
4224   /* If parsing tentatively, replace the sequence of tokens that makes
4225      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226      token.  That way, should we re-parse the token stream, we will
4227      not have to repeat the effort required to do the parse, nor will
4228      we issue duplicate error messages.  */
4229   if (success && start)
4230     {
4231       cp_token *token;
4232
4233       token = cp_lexer_token_at (parser->lexer, start);
4234       /* Reset the contents of the START token.  */
4235       token->type = CPP_NESTED_NAME_SPECIFIER;
4236       /* Retrieve any deferred checks.  Do not pop this access checks yet
4237          so the memory will not be reclaimed during token replacing below.  */
4238       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239       token->u.tree_check_value->value = parser->scope;
4240       token->u.tree_check_value->checks = get_deferred_access_checks ();
4241       token->u.tree_check_value->qualifying_scope =
4242         parser->qualifying_scope;
4243       token->keyword = RID_MAX;
4244
4245       /* Purge all subsequent tokens.  */
4246       cp_lexer_purge_tokens_after (parser->lexer, start);
4247     }
4248
4249   if (start)
4250     pop_to_parent_deferring_access_checks ();
4251
4252   return success ? parser->scope : NULL_TREE;
4253 }
4254
4255 /* Parse a nested-name-specifier.  See
4256    cp_parser_nested_name_specifier_opt for details.  This function
4257    behaves identically, except that it will an issue an error if no
4258    nested-name-specifier is present.  */
4259
4260 static tree
4261 cp_parser_nested_name_specifier (cp_parser *parser,
4262                                  bool typename_keyword_p,
4263                                  bool check_dependency_p,
4264                                  bool type_p,
4265                                  bool is_declaration)
4266 {
4267   tree scope;
4268
4269   /* Look for the nested-name-specifier.  */
4270   scope = cp_parser_nested_name_specifier_opt (parser,
4271                                                typename_keyword_p,
4272                                                check_dependency_p,
4273                                                type_p,
4274                                                is_declaration);
4275   /* If it was not present, issue an error message.  */
4276   if (!scope)
4277     {
4278       cp_parser_error (parser, "expected nested-name-specifier");
4279       parser->scope = NULL_TREE;
4280     }
4281
4282   return scope;
4283 }
4284
4285 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286    this is either a class-name or a namespace-name (which corresponds
4287    to the class-or-namespace-name production in the grammar). For
4288    C++0x, it can also be a type-name that refers to an enumeration
4289    type.
4290
4291    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294    TYPE_P is TRUE iff the next name should be taken as a class-name,
4295    even the same name is declared to be another entity in the same
4296    scope.
4297
4298    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4299    specified by the class-or-namespace-name.  If neither is found the
4300    ERROR_MARK_NODE is returned.  */
4301
4302 static tree
4303 cp_parser_qualifying_entity (cp_parser *parser,
4304                              bool typename_keyword_p,
4305                              bool template_keyword_p,
4306                              bool check_dependency_p,
4307                              bool type_p,
4308                              bool is_declaration)
4309 {
4310   tree saved_scope;
4311   tree saved_qualifying_scope;
4312   tree saved_object_scope;
4313   tree scope;
4314   bool only_class_p;
4315   bool successful_parse_p;
4316
4317   /* Before we try to parse the class-name, we must save away the
4318      current PARSER->SCOPE since cp_parser_class_name will destroy
4319      it.  */
4320   saved_scope = parser->scope;
4321   saved_qualifying_scope = parser->qualifying_scope;
4322   saved_object_scope = parser->object_scope;
4323   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4324      there is no need to look for a namespace-name.  */
4325   only_class_p = template_keyword_p 
4326     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4327   if (!only_class_p)
4328     cp_parser_parse_tentatively (parser);
4329   scope = cp_parser_class_name (parser,
4330                                 typename_keyword_p,
4331                                 template_keyword_p,
4332                                 type_p ? class_type : none_type,
4333                                 check_dependency_p,
4334                                 /*class_head_p=*/false,
4335                                 is_declaration);
4336   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4338   if (!only_class_p 
4339       && cxx_dialect != cxx98
4340       && !successful_parse_p)
4341     {
4342       /* Restore the saved scope.  */
4343       parser->scope = saved_scope;
4344       parser->qualifying_scope = saved_qualifying_scope;
4345       parser->object_scope = saved_object_scope;
4346
4347       /* Parse tentatively.  */
4348       cp_parser_parse_tentatively (parser);
4349      
4350       /* Parse a typedef-name or enum-name.  */
4351       scope = cp_parser_nonclass_name (parser);
4352       successful_parse_p = cp_parser_parse_definitely (parser);
4353     }
4354   /* If that didn't work, try for a namespace-name.  */
4355   if (!only_class_p && !successful_parse_p)
4356     {
4357       /* Restore the saved scope.  */
4358       parser->scope = saved_scope;
4359       parser->qualifying_scope = saved_qualifying_scope;
4360       parser->object_scope = saved_object_scope;
4361       /* If we are not looking at an identifier followed by the scope
4362          resolution operator, then this is not part of a
4363          nested-name-specifier.  (Note that this function is only used
4364          to parse the components of a nested-name-specifier.)  */
4365       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367         return error_mark_node;
4368       scope = cp_parser_namespace_name (parser);
4369     }
4370
4371   return scope;
4372 }
4373
4374 /* Parse a postfix-expression.
4375
4376    postfix-expression:
4377      primary-expression
4378      postfix-expression [ expression ]
4379      postfix-expression ( expression-list [opt] )
4380      simple-type-specifier ( expression-list [opt] )
4381      typename :: [opt] nested-name-specifier identifier
4382        ( expression-list [opt] )
4383      typename :: [opt] nested-name-specifier template [opt] template-id
4384        ( expression-list [opt] )
4385      postfix-expression . template [opt] id-expression
4386      postfix-expression -> template [opt] id-expression
4387      postfix-expression . pseudo-destructor-name
4388      postfix-expression -> pseudo-destructor-name
4389      postfix-expression ++
4390      postfix-expression --
4391      dynamic_cast < type-id > ( expression )
4392      static_cast < type-id > ( expression )
4393      reinterpret_cast < type-id > ( expression )
4394      const_cast < type-id > ( expression )
4395      typeid ( expression )
4396      typeid ( type-id )
4397
4398    GNU Extension:
4399
4400    postfix-expression:
4401      ( type-id ) { initializer-list , [opt] }
4402
4403    This extension is a GNU version of the C99 compound-literal
4404    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4405    but they are essentially the same concept.)
4406
4407    If ADDRESS_P is true, the postfix expression is the operand of the
4408    `&' operator.  CAST_P is true if this expression is the target of a
4409    cast.
4410
4411    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412    class member access expressions [expr.ref].
4413
4414    Returns a representation of the expression.  */
4415
4416 static tree
4417 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4418                               bool member_access_only_p,
4419                               cp_id_kind * pidk_return)
4420 {
4421   cp_token *token;
4422   enum rid keyword;
4423   cp_id_kind idk = CP_ID_KIND_NONE;
4424   tree postfix_expression = NULL_TREE;
4425   bool is_member_access = false;
4426
4427   /* Peek at the next token.  */
4428   token = cp_lexer_peek_token (parser->lexer);
4429   /* Some of the productions are determined by keywords.  */
4430   keyword = token->keyword;
4431   switch (keyword)
4432     {
4433     case RID_DYNCAST:
4434     case RID_STATCAST:
4435     case RID_REINTCAST:
4436     case RID_CONSTCAST:
4437       {
4438         tree type;
4439         tree expression;
4440         const char *saved_message;
4441
4442         /* All of these can be handled in the same way from the point
4443            of view of parsing.  Begin by consuming the token
4444            identifying the cast.  */
4445         cp_lexer_consume_token (parser->lexer);
4446
4447         /* New types cannot be defined in the cast.  */
4448         saved_message = parser->type_definition_forbidden_message;
4449         parser->type_definition_forbidden_message
4450           = "types may not be defined in casts";
4451
4452         /* Look for the opening `<'.  */
4453         cp_parser_require (parser, CPP_LESS, "%<<%>");
4454         /* Parse the type to which we are casting.  */
4455         type = cp_parser_type_id (parser);
4456         /* Look for the closing `>'.  */
4457         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4458         /* Restore the old message.  */
4459         parser->type_definition_forbidden_message = saved_message;
4460
4461         /* And the expression which is being cast.  */
4462         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4463         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4464         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4465
4466         /* Only type conversions to integral or enumeration types
4467            can be used in constant-expressions.  */
4468         if (!cast_valid_in_integral_constant_expression_p (type)
4469             && (cp_parser_non_integral_constant_expression
4470                 (parser,
4471                  "a cast to a type other than an integral or "
4472                  "enumeration type")))
4473           return error_mark_node;
4474
4475         switch (keyword)
4476           {
4477           case RID_DYNCAST:
4478             postfix_expression
4479               = build_dynamic_cast (type, expression, tf_warning_or_error);
4480             break;
4481           case RID_STATCAST:
4482             postfix_expression
4483               = build_static_cast (type, expression, tf_warning_or_error);
4484             break;
4485           case RID_REINTCAST:
4486             postfix_expression
4487               = build_reinterpret_cast (type, expression, 
4488                                         tf_warning_or_error);
4489             break;
4490           case RID_CONSTCAST:
4491             postfix_expression
4492               = build_const_cast (type, expression, tf_warning_or_error);
4493             break;
4494           default:
4495             gcc_unreachable ();
4496           }
4497       }
4498       break;
4499
4500     case RID_TYPEID:
4501       {
4502         tree type;
4503         const char *saved_message;
4504         bool saved_in_type_id_in_expr_p;
4505
4506         /* Consume the `typeid' token.  */
4507         cp_lexer_consume_token (parser->lexer);
4508         /* Look for the `(' token.  */
4509         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4510         /* Types cannot be defined in a `typeid' expression.  */
4511         saved_message = parser->type_definition_forbidden_message;
4512         parser->type_definition_forbidden_message
4513           = "types may not be defined in a %<typeid%> expression";
4514         /* We can't be sure yet whether we're looking at a type-id or an
4515            expression.  */
4516         cp_parser_parse_tentatively (parser);
4517         /* Try a type-id first.  */
4518         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519         parser->in_type_id_in_expr_p = true;
4520         type = cp_parser_type_id (parser);
4521         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4522         /* Look for the `)' token.  Otherwise, we can't be sure that
4523            we're not looking at an expression: consider `typeid (int
4524            (3))', for example.  */
4525         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526         /* If all went well, simply lookup the type-id.  */
4527         if (cp_parser_parse_definitely (parser))
4528           postfix_expression = get_typeid (type);
4529         /* Otherwise, fall back to the expression variant.  */
4530         else
4531           {
4532             tree expression;
4533
4534             /* Look for an expression.  */
4535             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4536             /* Compute its typeid.  */
4537             postfix_expression = build_typeid (expression);
4538             /* Look for the `)' token.  */
4539             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4540           }
4541         /* Restore the saved message.  */
4542         parser->type_definition_forbidden_message = saved_message;
4543         /* `typeid' may not appear in an integral constant expression.  */
4544         if (cp_parser_non_integral_constant_expression(parser,
4545                                                        "%<typeid%> operator"))
4546           return error_mark_node;
4547       }
4548       break;
4549
4550     case RID_TYPENAME:
4551       {
4552         tree type;
4553         /* The syntax permitted here is the same permitted for an
4554            elaborated-type-specifier.  */
4555         type = cp_parser_elaborated_type_specifier (parser,
4556                                                     /*is_friend=*/false,
4557                                                     /*is_declaration=*/false);
4558         postfix_expression = cp_parser_functional_cast (parser, type);
4559       }
4560       break;
4561
4562     default:
4563       {
4564         tree type;
4565
4566         /* If the next thing is a simple-type-specifier, we may be
4567            looking at a functional cast.  We could also be looking at
4568            an id-expression.  So, we try the functional cast, and if
4569            that doesn't work we fall back to the primary-expression.  */
4570         cp_parser_parse_tentatively (parser);
4571         /* Look for the simple-type-specifier.  */
4572         type = cp_parser_simple_type_specifier (parser,
4573                                                 /*decl_specs=*/NULL,
4574                                                 CP_PARSER_FLAGS_NONE);
4575         /* Parse the cast itself.  */
4576         if (!cp_parser_error_occurred (parser))
4577           postfix_expression
4578             = cp_parser_functional_cast (parser, type);
4579         /* If that worked, we're done.  */
4580         if (cp_parser_parse_definitely (parser))
4581           break;
4582
4583         /* If the functional-cast didn't work out, try a
4584            compound-literal.  */
4585         if (cp_parser_allow_gnu_extensions_p (parser)
4586             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4587           {
4588             VEC(constructor_elt,gc) *initializer_list = NULL;
4589             bool saved_in_type_id_in_expr_p;
4590
4591             cp_parser_parse_tentatively (parser);
4592             /* Consume the `('.  */
4593             cp_lexer_consume_token (parser->lexer);
4594             /* Parse the type.  */
4595             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596             parser->in_type_id_in_expr_p = true;
4597             type = cp_parser_type_id (parser);
4598             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4599             /* Look for the `)'.  */
4600             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4601             /* Look for the `{'.  */
4602             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4603             /* If things aren't going well, there's no need to
4604                keep going.  */
4605             if (!cp_parser_error_occurred (parser))
4606               {
4607                 bool non_constant_p;
4608                 /* Parse the initializer-list.  */
4609                 initializer_list
4610                   = cp_parser_initializer_list (parser, &non_constant_p);
4611                 /* Allow a trailing `,'.  */
4612                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613                   cp_lexer_consume_token (parser->lexer);
4614                 /* Look for the final `}'.  */
4615                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4616               }
4617             /* If that worked, we're definitely looking at a
4618                compound-literal expression.  */
4619             if (cp_parser_parse_definitely (parser))
4620               {
4621                 /* Warn the user that a compound literal is not
4622                    allowed in standard C++.  */
4623                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4624                 /* For simplicity, we disallow compound literals in
4625                    constant-expressions.  We could
4626                    allow compound literals of integer type, whose
4627                    initializer was a constant, in constant
4628                    expressions.  Permitting that usage, as a further
4629                    extension, would not change the meaning of any
4630                    currently accepted programs.  (Of course, as
4631                    compound literals are not part of ISO C++, the
4632                    standard has nothing to say.)  */
4633                 if (cp_parser_non_integral_constant_expression 
4634                     (parser, "non-constant compound literals"))
4635                   {
4636                     postfix_expression = error_mark_node;
4637                     break;
4638                   }
4639                 /* Form the representation of the compound-literal.  */
4640                 postfix_expression
4641                   = (finish_compound_literal
4642                      (type, build_constructor (init_list_type_node,
4643                                                initializer_list)));
4644                 break;
4645               }
4646           }
4647
4648         /* It must be a primary-expression.  */
4649         postfix_expression
4650           = cp_parser_primary_expression (parser, address_p, cast_p,
4651                                           /*template_arg_p=*/false,
4652                                           &idk);
4653       }
4654       break;
4655     }
4656
4657   /* Keep looping until the postfix-expression is complete.  */
4658   while (true)
4659     {
4660       if (idk == CP_ID_KIND_UNQUALIFIED
4661           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4662           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4663         /* It is not a Koenig lookup function call.  */
4664         postfix_expression
4665           = unqualified_name_lookup_error (postfix_expression);
4666
4667       /* Peek at the next token.  */
4668       token = cp_lexer_peek_token (parser->lexer);
4669
4670       switch (token->type)
4671         {
4672         case CPP_OPEN_SQUARE:
4673           postfix_expression
4674             = cp_parser_postfix_open_square_expression (parser,
4675                                                         postfix_expression,
4676                                                         false);
4677           idk = CP_ID_KIND_NONE;
4678           is_member_access = false;
4679           break;
4680
4681         case CPP_OPEN_PAREN:
4682           /* postfix-expression ( expression-list [opt] ) */
4683           {
4684             bool koenig_p;
4685             bool is_builtin_constant_p;
4686             bool saved_integral_constant_expression_p = false;
4687             bool saved_non_integral_constant_expression_p = false;
4688             tree args;
4689
4690             is_member_access = false;
4691
4692             is_builtin_constant_p
4693               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694             if (is_builtin_constant_p)
4695               {
4696                 /* The whole point of __builtin_constant_p is to allow
4697                    non-constant expressions to appear as arguments.  */
4698                 saved_integral_constant_expression_p
4699                   = parser->integral_constant_expression_p;
4700                 saved_non_integral_constant_expression_p
4701                   = parser->non_integral_constant_expression_p;
4702                 parser->integral_constant_expression_p = false;
4703               }
4704             args = (cp_parser_parenthesized_expression_list
4705                     (parser, /*is_attribute_list=*/false,
4706                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4707                      /*non_constant_p=*/NULL));
4708             if (is_builtin_constant_p)
4709               {
4710                 parser->integral_constant_expression_p
4711                   = saved_integral_constant_expression_p;
4712                 parser->non_integral_constant_expression_p
4713                   = saved_non_integral_constant_expression_p;
4714               }
4715
4716             if (args == error_mark_node)
4717               {
4718                 postfix_expression = error_mark_node;
4719                 break;
4720               }
4721
4722             /* Function calls are not permitted in
4723                constant-expressions.  */
4724             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725                 && cp_parser_non_integral_constant_expression (parser,
4726                                                                "a function call"))
4727               {
4728                 postfix_expression = error_mark_node;
4729                 break;
4730               }
4731
4732             koenig_p = false;
4733             if (idk == CP_ID_KIND_UNQUALIFIED
4734                 || idk == CP_ID_KIND_TEMPLATE_ID)
4735               {
4736                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4737                   {
4738                     if (args)
4739                       {
4740                         koenig_p = true;
4741                         if (!any_type_dependent_arguments_p (args))
4742                           postfix_expression
4743                             = perform_koenig_lookup (postfix_expression, args);
4744                       }
4745                     else
4746                       postfix_expression
4747                         = unqualified_fn_lookup_error (postfix_expression);
4748                   }
4749                 /* We do not perform argument-dependent lookup if
4750                    normal lookup finds a non-function, in accordance
4751                    with the expected resolution of DR 218.  */
4752                 else if (args && is_overloaded_fn (postfix_expression))
4753                   {
4754                     tree fn = get_first_fn (postfix_expression);
4755
4756                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4757                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4758
4759                     /* Only do argument dependent lookup if regular
4760                        lookup does not find a set of member functions.
4761                        [basic.lookup.koenig]/2a  */
4762                     if (!DECL_FUNCTION_MEMBER_P (fn))
4763                       {
4764                         koenig_p = true;
4765                         if (!any_type_dependent_arguments_p (args))
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             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4828             idk = CP_ID_KIND_NONE;
4829           }
4830           break;
4831
4832         case CPP_DOT:
4833         case CPP_DEREF:
4834           /* postfix-expression . template [opt] id-expression
4835              postfix-expression . pseudo-destructor-name
4836              postfix-expression -> template [opt] id-expression
4837              postfix-expression -> pseudo-destructor-name */
4838
4839           /* Consume the `.' or `->' operator.  */
4840           cp_lexer_consume_token (parser->lexer);
4841
4842           postfix_expression
4843             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4844                                                       postfix_expression,
4845                                                       false, &idk,
4846                                                       token->location);
4847
4848           is_member_access = true;
4849           break;
4850
4851         case CPP_PLUS_PLUS:
4852           /* postfix-expression ++  */
4853           /* Consume the `++' token.  */
4854           cp_lexer_consume_token (parser->lexer);
4855           /* Generate a representation for the complete expression.  */
4856           postfix_expression
4857             = finish_increment_expr (postfix_expression,
4858                                      POSTINCREMENT_EXPR);
4859           /* Increments may not appear in constant-expressions.  */
4860           if (cp_parser_non_integral_constant_expression (parser,
4861                                                           "an increment"))
4862             postfix_expression = error_mark_node;
4863           idk = CP_ID_KIND_NONE;
4864           is_member_access = false;
4865           break;
4866
4867         case CPP_MINUS_MINUS:
4868           /* postfix-expression -- */
4869           /* Consume the `--' token.  */
4870           cp_lexer_consume_token (parser->lexer);
4871           /* Generate a representation for the complete expression.  */
4872           postfix_expression
4873             = finish_increment_expr (postfix_expression,
4874                                      POSTDECREMENT_EXPR);
4875           /* Decrements may not appear in constant-expressions.  */
4876           if (cp_parser_non_integral_constant_expression (parser,
4877                                                           "a decrement"))
4878             postfix_expression = error_mark_node;
4879           idk = CP_ID_KIND_NONE;
4880           is_member_access = false;
4881           break;
4882
4883         default:
4884           if (pidk_return != NULL)
4885             * pidk_return = idk;
4886           if (member_access_only_p)
4887             return is_member_access? postfix_expression : error_mark_node;
4888           else
4889             return postfix_expression;
4890         }
4891     }
4892
4893   /* We should never get here.  */
4894   gcc_unreachable ();
4895   return error_mark_node;
4896 }
4897
4898 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4899    by cp_parser_builtin_offsetof.  We're looking for
4900
4901      postfix-expression [ expression ]
4902
4903    FOR_OFFSETOF is set if we're being called in that context, which
4904    changes how we deal with integer constant expressions.  */
4905
4906 static tree
4907 cp_parser_postfix_open_square_expression (cp_parser *parser,
4908                                           tree postfix_expression,
4909                                           bool for_offsetof)
4910 {
4911   tree index;
4912
4913   /* Consume the `[' token.  */
4914   cp_lexer_consume_token (parser->lexer);
4915
4916   /* Parse the index expression.  */
4917   /* ??? For offsetof, there is a question of what to allow here.  If
4918      offsetof is not being used in an integral constant expression context,
4919      then we *could* get the right answer by computing the value at runtime.
4920      If we are in an integral constant expression context, then we might
4921      could accept any constant expression; hard to say without analysis.
4922      Rather than open the barn door too wide right away, allow only integer
4923      constant expressions here.  */
4924   if (for_offsetof)
4925     index = cp_parser_constant_expression (parser, false, NULL);
4926   else
4927     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4928
4929   /* Look for the closing `]'.  */
4930   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4931
4932   /* Build the ARRAY_REF.  */
4933   postfix_expression = grok_array_decl (postfix_expression, index);
4934
4935   /* When not doing offsetof, array references are not permitted in
4936      constant-expressions.  */
4937   if (!for_offsetof
4938       && (cp_parser_non_integral_constant_expression
4939           (parser, "an array reference")))
4940     postfix_expression = error_mark_node;
4941
4942   return postfix_expression;
4943 }
4944
4945 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4946    by cp_parser_builtin_offsetof.  We're looking for
4947
4948      postfix-expression . template [opt] id-expression
4949      postfix-expression . pseudo-destructor-name
4950      postfix-expression -> template [opt] id-expression
4951      postfix-expression -> pseudo-destructor-name
4952
4953    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4954    limits what of the above we'll actually accept, but nevermind.
4955    TOKEN_TYPE is the "." or "->" token, which will already have been
4956    removed from the stream.  */
4957
4958 static tree
4959 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4960                                         enum cpp_ttype token_type,
4961                                         tree postfix_expression,
4962                                         bool for_offsetof, cp_id_kind *idk,
4963                                         location_t location)
4964 {
4965   tree name;
4966   bool dependent_p;
4967   bool pseudo_destructor_p;
4968   tree scope = NULL_TREE;
4969
4970   /* If this is a `->' operator, dereference the pointer.  */
4971   if (token_type == CPP_DEREF)
4972     postfix_expression = build_x_arrow (postfix_expression);
4973   /* Check to see whether or not the expression is type-dependent.  */
4974   dependent_p = type_dependent_expression_p (postfix_expression);
4975   /* The identifier following the `->' or `.' is not qualified.  */
4976   parser->scope = NULL_TREE;
4977   parser->qualifying_scope = NULL_TREE;
4978   parser->object_scope = NULL_TREE;
4979   *idk = CP_ID_KIND_NONE;
4980
4981   /* Enter the scope corresponding to the type of the object
4982      given by the POSTFIX_EXPRESSION.  */
4983   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4984     {
4985       scope = TREE_TYPE (postfix_expression);
4986       /* According to the standard, no expression should ever have
4987          reference type.  Unfortunately, we do not currently match
4988          the standard in this respect in that our internal representation
4989          of an expression may have reference type even when the standard
4990          says it does not.  Therefore, we have to manually obtain the
4991          underlying type here.  */
4992       scope = non_reference (scope);
4993       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4994       if (scope == unknown_type_node)
4995         {
4996           error ("%H%qE does not have class type", &location, postfix_expression);
4997           scope = NULL_TREE;
4998         }
4999       else
5000         scope = complete_type_or_else (scope, NULL_TREE);
5001       /* Let the name lookup machinery know that we are processing a
5002          class member access expression.  */
5003       parser->context->object_type = scope;
5004       /* If something went wrong, we want to be able to discern that case,
5005          as opposed to the case where there was no SCOPE due to the type
5006          of expression being dependent.  */
5007       if (!scope)
5008         scope = error_mark_node;
5009       /* If the SCOPE was erroneous, make the various semantic analysis
5010          functions exit quickly -- and without issuing additional error
5011          messages.  */
5012       if (scope == error_mark_node)
5013         postfix_expression = error_mark_node;
5014     }
5015
5016   /* Assume this expression is not a pseudo-destructor access.  */
5017   pseudo_destructor_p = false;
5018
5019   /* If the SCOPE is a scalar type, then, if this is a valid program,
5020      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5021      is type dependent, it can be pseudo-destructor-name or something else.
5022      Try to parse it as pseudo-destructor-name first.  */
5023   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5024     {
5025       tree s;
5026       tree type;
5027
5028       cp_parser_parse_tentatively (parser);
5029       /* Parse the pseudo-destructor-name.  */
5030       s = NULL_TREE;
5031       cp_parser_pseudo_destructor_name (parser, &s, &type);
5032       if (dependent_p
5033           && (cp_parser_error_occurred (parser)
5034               || TREE_CODE (type) != TYPE_DECL
5035               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5036         cp_parser_abort_tentative_parse (parser);
5037       else if (cp_parser_parse_definitely (parser))
5038         {
5039           pseudo_destructor_p = true;
5040           postfix_expression
5041             = finish_pseudo_destructor_expr (postfix_expression,
5042                                              s, TREE_TYPE (type));
5043         }
5044     }
5045
5046   if (!pseudo_destructor_p)
5047     {
5048       /* If the SCOPE is not a scalar type, we are looking at an
5049          ordinary class member access expression, rather than a
5050          pseudo-destructor-name.  */
5051       bool template_p;
5052       cp_token *token = cp_lexer_peek_token (parser->lexer);
5053       /* Parse the id-expression.  */
5054       name = (cp_parser_id_expression
5055               (parser,
5056                cp_parser_optional_template_keyword (parser),
5057                /*check_dependency_p=*/true,
5058                &template_p,
5059                /*declarator_p=*/false,
5060                /*optional_p=*/false));
5061       /* In general, build a SCOPE_REF if the member name is qualified.
5062          However, if the name was not dependent and has already been
5063          resolved; there is no need to build the SCOPE_REF.  For example;
5064
5065              struct X { void f(); };
5066              template <typename T> void f(T* t) { t->X::f(); }
5067
5068          Even though "t" is dependent, "X::f" is not and has been resolved
5069          to a BASELINK; there is no need to include scope information.  */
5070
5071       /* But we do need to remember that there was an explicit scope for
5072          virtual function calls.  */
5073       if (parser->scope)
5074         *idk = CP_ID_KIND_QUALIFIED;
5075
5076       /* If the name is a template-id that names a type, we will get a
5077          TYPE_DECL here.  That is invalid code.  */
5078       if (TREE_CODE (name) == TYPE_DECL)
5079         {
5080           error ("%Hinvalid use of %qD", &token->location, name);
5081           postfix_expression = error_mark_node;
5082         }
5083       else
5084         {
5085           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5086             {
5087               name = build_qualified_name (/*type=*/NULL_TREE,
5088                                            parser->scope,
5089                                            name,
5090                                            template_p);
5091               parser->scope = NULL_TREE;
5092               parser->qualifying_scope = NULL_TREE;
5093               parser->object_scope = NULL_TREE;
5094             }
5095           if (scope && name && BASELINK_P (name))
5096             adjust_result_of_qualified_name_lookup
5097               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5098           postfix_expression
5099             = finish_class_member_access_expr (postfix_expression, name,
5100                                                template_p, 
5101                                                tf_warning_or_error);
5102         }
5103     }
5104
5105   /* We no longer need to look up names in the scope of the object on
5106      the left-hand side of the `.' or `->' operator.  */
5107   parser->context->object_type = NULL_TREE;
5108
5109   /* Outside of offsetof, these operators may not appear in
5110      constant-expressions.  */
5111   if (!for_offsetof
5112       && (cp_parser_non_integral_constant_expression
5113           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5114     postfix_expression = error_mark_node;
5115
5116   return postfix_expression;
5117 }
5118
5119 /* Parse a parenthesized expression-list.
5120
5121    expression-list:
5122      assignment-expression
5123      expression-list, assignment-expression
5124
5125    attribute-list:
5126      expression-list
5127      identifier
5128      identifier, expression-list
5129
5130    CAST_P is true if this expression is the target of a cast.
5131
5132    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5133    argument pack.
5134
5135    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5136    representation of an assignment-expression.  Note that a TREE_LIST
5137    is returned even if there is only a single expression in the list.
5138    error_mark_node is returned if the ( and or ) are
5139    missing. NULL_TREE is returned on no expressions. The parentheses
5140    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5141    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5142    indicates whether or not all of the expressions in the list were
5143    constant.  */
5144
5145 static tree
5146 cp_parser_parenthesized_expression_list (cp_parser* parser,
5147                                          bool is_attribute_list,
5148                                          bool cast_p,
5149                                          bool allow_expansion_p,
5150                                          bool *non_constant_p)
5151 {
5152   tree expression_list = NULL_TREE;
5153   bool fold_expr_p = is_attribute_list;
5154   tree identifier = NULL_TREE;
5155   bool saved_greater_than_is_operator_p;
5156
5157   /* Assume all the expressions will be constant.  */
5158   if (non_constant_p)
5159     *non_constant_p = false;
5160
5161   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5162     return error_mark_node;
5163
5164   /* Within a parenthesized expression, a `>' token is always
5165      the greater-than operator.  */
5166   saved_greater_than_is_operator_p
5167     = parser->greater_than_is_operator_p;
5168   parser->greater_than_is_operator_p = true;
5169
5170   /* Consume expressions until there are no more.  */
5171   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5172     while (true)
5173       {
5174         tree expr;
5175
5176         /* At the beginning of attribute lists, check to see if the
5177            next token is an identifier.  */
5178         if (is_attribute_list
5179             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5180           {
5181             cp_token *token;
5182
5183             /* Consume the identifier.  */
5184             token = cp_lexer_consume_token (parser->lexer);
5185             /* Save the identifier.  */
5186             identifier = token->u.value;
5187           }
5188         else
5189           {
5190             bool expr_non_constant_p;
5191
5192             /* Parse the next assignment-expression.  */
5193             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5194               {
5195                 /* A braced-init-list.  */
5196                 maybe_warn_cpp0x ("extended initializer lists");
5197                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5198                 if (non_constant_p && expr_non_constant_p)
5199                   *non_constant_p = true;
5200               }
5201             else if (non_constant_p)
5202               {
5203                 expr = (cp_parser_constant_expression
5204                         (parser, /*allow_non_constant_p=*/true,
5205                          &expr_non_constant_p));
5206                 if (expr_non_constant_p)
5207                   *non_constant_p = true;
5208               }
5209             else
5210               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5211
5212             if (fold_expr_p)
5213               expr = fold_non_dependent_expr (expr);
5214
5215             /* If we have an ellipsis, then this is an expression
5216                expansion.  */
5217             if (allow_expansion_p
5218                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5219               {
5220                 /* Consume the `...'.  */
5221                 cp_lexer_consume_token (parser->lexer);
5222
5223                 /* Build the argument pack.  */
5224                 expr = make_pack_expansion (expr);
5225               }
5226
5227              /* Add it to the list.  We add error_mark_node
5228                 expressions to the list, so that we can still tell if
5229                 the correct form for a parenthesized expression-list
5230                 is found. That gives better errors.  */
5231             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5232
5233             if (expr == error_mark_node)
5234               goto skip_comma;
5235           }
5236
5237         /* After the first item, attribute lists look the same as
5238            expression lists.  */
5239         is_attribute_list = false;
5240
5241       get_comma:;
5242         /* If the next token isn't a `,', then we are done.  */
5243         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5244           break;
5245
5246         /* Otherwise, consume the `,' and keep going.  */
5247         cp_lexer_consume_token (parser->lexer);
5248       }
5249
5250   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5251     {
5252       int ending;
5253
5254     skip_comma:;
5255       /* We try and resync to an unnested comma, as that will give the
5256          user better diagnostics.  */
5257       ending = cp_parser_skip_to_closing_parenthesis (parser,
5258                                                       /*recovering=*/true,
5259                                                       /*or_comma=*/true,
5260                                                       /*consume_paren=*/true);
5261       if (ending < 0)
5262         goto get_comma;
5263       if (!ending)
5264         {
5265           parser->greater_than_is_operator_p
5266             = saved_greater_than_is_operator_p;
5267           return error_mark_node;
5268         }
5269     }
5270
5271   parser->greater_than_is_operator_p
5272     = saved_greater_than_is_operator_p;
5273
5274   /* We built up the list in reverse order so we must reverse it now.  */
5275   expression_list = nreverse (expression_list);
5276   if (identifier)
5277     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5278
5279   return expression_list;
5280 }
5281
5282 /* Parse a pseudo-destructor-name.
5283
5284    pseudo-destructor-name:
5285      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5286      :: [opt] nested-name-specifier template template-id :: ~ type-name
5287      :: [opt] nested-name-specifier [opt] ~ type-name
5288
5289    If either of the first two productions is used, sets *SCOPE to the
5290    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5291    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5292    or ERROR_MARK_NODE if the parse fails.  */
5293
5294 static void
5295 cp_parser_pseudo_destructor_name (cp_parser* parser,
5296                                   tree* scope,
5297                                   tree* type)
5298 {
5299   bool nested_name_specifier_p;
5300
5301   /* Assume that things will not work out.  */
5302   *type = error_mark_node;
5303
5304   /* Look for the optional `::' operator.  */
5305   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5306   /* Look for the optional nested-name-specifier.  */
5307   nested_name_specifier_p
5308     = (cp_parser_nested_name_specifier_opt (parser,
5309                                             /*typename_keyword_p=*/false,
5310                                             /*check_dependency_p=*/true,
5311                                             /*type_p=*/false,
5312                                             /*is_declaration=*/false)
5313        != NULL_TREE);
5314   /* Now, if we saw a nested-name-specifier, we might be doing the
5315      second production.  */
5316   if (nested_name_specifier_p
5317       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5318     {
5319       /* Consume the `template' keyword.  */
5320       cp_lexer_consume_token (parser->lexer);
5321       /* Parse the template-id.  */
5322       cp_parser_template_id (parser,
5323                              /*template_keyword_p=*/true,
5324                              /*check_dependency_p=*/false,
5325                              /*is_declaration=*/true);
5326       /* Look for the `::' token.  */
5327       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5328     }
5329   /* If the next token is not a `~', then there might be some
5330      additional qualification.  */
5331   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5332     {
5333       /* At this point, we're looking for "type-name :: ~".  The type-name
5334          must not be a class-name, since this is a pseudo-destructor.  So,
5335          it must be either an enum-name, or a typedef-name -- both of which
5336          are just identifiers.  So, we peek ahead to check that the "::"
5337          and "~" tokens are present; if they are not, then we can avoid
5338          calling type_name.  */
5339       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5340           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5341           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5342         {
5343           cp_parser_error (parser, "non-scalar type");
5344           return;
5345         }
5346
5347       /* Look for the type-name.  */
5348       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5349       if (*scope == error_mark_node)
5350         return;
5351
5352       /* Look for the `::' token.  */
5353       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5354     }
5355   else
5356     *scope = NULL_TREE;
5357
5358   /* Look for the `~'.  */
5359   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5360   /* Look for the type-name again.  We are not responsible for
5361      checking that it matches the first type-name.  */
5362   *type = cp_parser_nonclass_name (parser);
5363 }
5364
5365 /* Parse a unary-expression.
5366
5367    unary-expression:
5368      postfix-expression
5369      ++ cast-expression
5370      -- cast-expression
5371      unary-operator cast-expression
5372      sizeof unary-expression
5373      sizeof ( type-id )
5374      new-expression
5375      delete-expression
5376
5377    GNU Extensions:
5378
5379    unary-expression:
5380      __extension__ cast-expression
5381      __alignof__ unary-expression
5382      __alignof__ ( type-id )
5383      __real__ cast-expression
5384      __imag__ cast-expression
5385      && identifier
5386
5387    ADDRESS_P is true iff the unary-expression is appearing as the
5388    operand of the `&' operator.   CAST_P is true if this expression is
5389    the target of a cast.
5390
5391    Returns a representation of the expression.  */
5392
5393 static tree
5394 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5395                             cp_id_kind * pidk)
5396 {
5397   cp_token *token;
5398   enum tree_code unary_operator;
5399
5400   /* Peek at the next token.  */
5401   token = cp_lexer_peek_token (parser->lexer);
5402   /* Some keywords give away the kind of expression.  */
5403   if (token->type == CPP_KEYWORD)
5404     {
5405       enum rid keyword = token->keyword;
5406
5407       switch (keyword)
5408         {
5409         case RID_ALIGNOF:
5410         case RID_SIZEOF:
5411           {
5412             tree operand;
5413             enum tree_code op;
5414
5415             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5416             /* Consume the token.  */
5417             cp_lexer_consume_token (parser->lexer);
5418             /* Parse the operand.  */
5419             operand = cp_parser_sizeof_operand (parser, keyword);
5420
5421             if (TYPE_P (operand))
5422               return cxx_sizeof_or_alignof_type (operand, op, true);
5423             else
5424               return cxx_sizeof_or_alignof_expr (operand, op, true);
5425           }
5426
5427         case RID_NEW:
5428           return cp_parser_new_expression (parser);
5429
5430         case RID_DELETE:
5431           return cp_parser_delete_expression (parser);
5432
5433         case RID_EXTENSION:
5434           {
5435             /* The saved value of the PEDANTIC flag.  */
5436             int saved_pedantic;
5437             tree expr;
5438
5439             /* Save away the PEDANTIC flag.  */
5440             cp_parser_extension_opt (parser, &saved_pedantic);
5441             /* Parse the cast-expression.  */
5442             expr = cp_parser_simple_cast_expression (parser);
5443             /* Restore the PEDANTIC flag.  */
5444             pedantic = saved_pedantic;
5445
5446             return expr;
5447           }
5448
5449         case RID_REALPART:
5450         case RID_IMAGPART:
5451           {
5452             tree expression;
5453
5454             /* Consume the `__real__' or `__imag__' token.  */
5455             cp_lexer_consume_token (parser->lexer);
5456             /* Parse the cast-expression.  */
5457             expression = cp_parser_simple_cast_expression (parser);
5458             /* Create the complete representation.  */
5459             return build_x_unary_op ((keyword == RID_REALPART
5460                                       ? REALPART_EXPR : IMAGPART_EXPR),
5461                                      expression,
5462                                      tf_warning_or_error);
5463           }
5464           break;
5465
5466         default:
5467           break;
5468         }
5469     }
5470
5471   /* Look for the `:: new' and `:: delete', which also signal the
5472      beginning of a new-expression, or delete-expression,
5473      respectively.  If the next token is `::', then it might be one of
5474      these.  */
5475   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5476     {
5477       enum rid keyword;
5478
5479       /* See if the token after the `::' is one of the keywords in
5480          which we're interested.  */
5481       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5482       /* If it's `new', we have a new-expression.  */
5483       if (keyword == RID_NEW)
5484         return cp_parser_new_expression (parser);
5485       /* Similarly, for `delete'.  */
5486       else if (keyword == RID_DELETE)
5487         return cp_parser_delete_expression (parser);
5488     }
5489
5490   /* Look for a unary operator.  */
5491   unary_operator = cp_parser_unary_operator (token);
5492   /* The `++' and `--' operators can be handled similarly, even though
5493      they are not technically unary-operators in the grammar.  */
5494   if (unary_operator == ERROR_MARK)
5495     {
5496       if (token->type == CPP_PLUS_PLUS)
5497         unary_operator = PREINCREMENT_EXPR;
5498       else if (token->type == CPP_MINUS_MINUS)
5499         unary_operator = PREDECREMENT_EXPR;
5500       /* Handle the GNU address-of-label extension.  */
5501       else if (cp_parser_allow_gnu_extensions_p (parser)
5502                && token->type == CPP_AND_AND)
5503         {
5504           tree identifier;
5505           tree expression;
5506           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5507
5508           /* Consume the '&&' token.  */
5509           cp_lexer_consume_token (parser->lexer);
5510           /* Look for the identifier.  */
5511           identifier = cp_parser_identifier (parser);
5512           /* Create an expression representing the address.  */
5513           expression = finish_label_address_expr (identifier, loc);
5514           if (cp_parser_non_integral_constant_expression (parser,
5515                                                 "the address of a label"))
5516             expression = error_mark_node;
5517           return expression;
5518         }
5519     }
5520   if (unary_operator != ERROR_MARK)
5521     {
5522       tree cast_expression;
5523       tree expression = error_mark_node;
5524       const char *non_constant_p = NULL;
5525
5526       /* Consume the operator token.  */
5527       token = cp_lexer_consume_token (parser->lexer);
5528       /* Parse the cast-expression.  */
5529       cast_expression
5530         = cp_parser_cast_expression (parser,
5531                                      unary_operator == ADDR_EXPR,
5532                                      /*cast_p=*/false, pidk);
5533       /* Now, build an appropriate representation.  */
5534       switch (unary_operator)
5535         {
5536         case INDIRECT_REF:
5537           non_constant_p = "%<*%>";
5538           expression = build_x_indirect_ref (cast_expression, "unary *",
5539                                              tf_warning_or_error);
5540           break;
5541
5542         case ADDR_EXPR:
5543           non_constant_p = "%<&%>";
5544           /* Fall through.  */
5545         case BIT_NOT_EXPR:
5546           expression = build_x_unary_op (unary_operator, cast_expression,
5547                                          tf_warning_or_error);
5548           break;
5549
5550         case PREINCREMENT_EXPR:
5551         case PREDECREMENT_EXPR:
5552           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5553                             ? "%<++%>" : "%<--%>");
5554           /* Fall through.  */
5555         case UNARY_PLUS_EXPR:
5556         case NEGATE_EXPR:
5557         case TRUTH_NOT_EXPR:
5558           expression = finish_unary_op_expr (unary_operator, cast_expression);
5559           break;
5560
5561         default:
5562           gcc_unreachable ();
5563         }
5564
5565       if (non_constant_p
5566           && cp_parser_non_integral_constant_expression (parser,
5567                                                          non_constant_p))
5568         expression = error_mark_node;
5569
5570       return expression;
5571     }
5572
5573   return cp_parser_postfix_expression (parser, address_p, cast_p,
5574                                        /*member_access_only_p=*/false,
5575                                        pidk);
5576 }
5577
5578 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5579    unary-operator, the corresponding tree code is returned.  */
5580
5581 static enum tree_code
5582 cp_parser_unary_operator (cp_token* token)
5583 {
5584   switch (token->type)
5585     {
5586     case CPP_MULT:
5587       return INDIRECT_REF;
5588
5589     case CPP_AND:
5590       return ADDR_EXPR;
5591
5592     case CPP_PLUS:
5593       return UNARY_PLUS_EXPR;
5594
5595     case CPP_MINUS:
5596       return NEGATE_EXPR;
5597
5598     case CPP_NOT:
5599       return TRUTH_NOT_EXPR;
5600
5601     case CPP_COMPL:
5602       return BIT_NOT_EXPR;
5603
5604     default:
5605       return ERROR_MARK;
5606     }
5607 }
5608
5609 /* Parse a new-expression.
5610
5611    new-expression:
5612      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5613      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5614
5615    Returns a representation of the expression.  */
5616
5617 static tree
5618 cp_parser_new_expression (cp_parser* parser)
5619 {
5620   bool global_scope_p;
5621   tree placement;
5622   tree type;
5623   tree initializer;
5624   tree nelts;
5625
5626   /* Look for the optional `::' operator.  */
5627   global_scope_p
5628     = (cp_parser_global_scope_opt (parser,
5629                                    /*current_scope_valid_p=*/false)
5630        != NULL_TREE);
5631   /* Look for the `new' operator.  */
5632   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5633   /* There's no easy way to tell a new-placement from the
5634      `( type-id )' construct.  */
5635   cp_parser_parse_tentatively (parser);
5636   /* Look for a new-placement.  */
5637   placement = cp_parser_new_placement (parser);
5638   /* If that didn't work out, there's no new-placement.  */
5639   if (!cp_parser_parse_definitely (parser))
5640     placement = NULL_TREE;
5641
5642   /* If the next token is a `(', then we have a parenthesized
5643      type-id.  */
5644   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5645     {
5646       cp_token *token;
5647       /* Consume the `('.  */
5648       cp_lexer_consume_token (parser->lexer);
5649       /* Parse the type-id.  */
5650       type = cp_parser_type_id (parser);
5651       /* Look for the closing `)'.  */
5652       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5653       token = cp_lexer_peek_token (parser->lexer);
5654       /* There should not be a direct-new-declarator in this production,
5655          but GCC used to allowed this, so we check and emit a sensible error
5656          message for this case.  */
5657       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5658         {
5659           error ("%Harray bound forbidden after parenthesized type-id",
5660                  &token->location);
5661           inform (token->location, 
5662                   "try removing the parentheses around the type-id");
5663           cp_parser_direct_new_declarator (parser);
5664         }
5665       nelts = NULL_TREE;
5666     }
5667   /* Otherwise, there must be a new-type-id.  */
5668   else
5669     type = cp_parser_new_type_id (parser, &nelts);
5670
5671   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5672   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5673       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5674     initializer = cp_parser_new_initializer (parser);
5675   else
5676     initializer = NULL_TREE;
5677
5678   /* A new-expression may not appear in an integral constant
5679      expression.  */
5680   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5681     return error_mark_node;
5682
5683   /* Create a representation of the new-expression.  */
5684   return build_new (placement, type, nelts, initializer, global_scope_p,
5685                     tf_warning_or_error);
5686 }
5687
5688 /* Parse a new-placement.
5689
5690    new-placement:
5691      ( expression-list )
5692
5693    Returns the same representation as for an expression-list.  */
5694
5695 static tree
5696 cp_parser_new_placement (cp_parser* parser)
5697 {
5698   tree expression_list;
5699
5700   /* Parse the expression-list.  */
5701   expression_list = (cp_parser_parenthesized_expression_list
5702                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5703                       /*non_constant_p=*/NULL));
5704
5705   return expression_list;
5706 }
5707
5708 /* Parse a new-type-id.
5709
5710    new-type-id:
5711      type-specifier-seq new-declarator [opt]
5712
5713    Returns the TYPE allocated.  If the new-type-id indicates an array
5714    type, *NELTS is set to the number of elements in the last array
5715    bound; the TYPE will not include the last array bound.  */
5716
5717 static tree
5718 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5719 {
5720   cp_decl_specifier_seq type_specifier_seq;
5721   cp_declarator *new_declarator;
5722   cp_declarator *declarator;
5723   cp_declarator *outer_declarator;
5724   const char *saved_message;
5725   tree type;
5726
5727   /* The type-specifier sequence must not contain type definitions.
5728      (It cannot contain declarations of new types either, but if they
5729      are not definitions we will catch that because they are not
5730      complete.)  */
5731   saved_message = parser->type_definition_forbidden_message;
5732   parser->type_definition_forbidden_message
5733     = "types may not be defined in a new-type-id";
5734   /* Parse the type-specifier-seq.  */
5735   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5736                                 &type_specifier_seq);
5737   /* Restore the old message.  */
5738   parser->type_definition_forbidden_message = saved_message;
5739   /* Parse the new-declarator.  */
5740   new_declarator = cp_parser_new_declarator_opt (parser);
5741
5742   /* Determine the number of elements in the last array dimension, if
5743      any.  */
5744   *nelts = NULL_TREE;
5745   /* Skip down to the last array dimension.  */
5746   declarator = new_declarator;
5747   outer_declarator = NULL;
5748   while (declarator && (declarator->kind == cdk_pointer
5749                         || declarator->kind == cdk_ptrmem))
5750     {
5751       outer_declarator = declarator;
5752       declarator = declarator->declarator;
5753     }
5754   while (declarator
5755          && declarator->kind == cdk_array
5756          && declarator->declarator
5757          && declarator->declarator->kind == cdk_array)
5758     {
5759       outer_declarator = declarator;
5760       declarator = declarator->declarator;
5761     }
5762
5763   if (declarator && declarator->kind == cdk_array)
5764     {
5765       *nelts = declarator->u.array.bounds;
5766       if (*nelts == error_mark_node)
5767         *nelts = integer_one_node;
5768
5769       if (outer_declarator)
5770         outer_declarator->declarator = declarator->declarator;
5771       else
5772         new_declarator = NULL;
5773     }
5774
5775   type = groktypename (&type_specifier_seq, new_declarator, false);
5776   return type;
5777 }
5778
5779 /* Parse an (optional) new-declarator.
5780
5781    new-declarator:
5782      ptr-operator new-declarator [opt]
5783      direct-new-declarator
5784
5785    Returns the declarator.  */
5786
5787 static cp_declarator *
5788 cp_parser_new_declarator_opt (cp_parser* parser)
5789 {
5790   enum tree_code code;
5791   tree type;
5792   cp_cv_quals cv_quals;
5793
5794   /* We don't know if there's a ptr-operator next, or not.  */
5795   cp_parser_parse_tentatively (parser);
5796   /* Look for a ptr-operator.  */
5797   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5798   /* If that worked, look for more new-declarators.  */
5799   if (cp_parser_parse_definitely (parser))
5800     {
5801       cp_declarator *declarator;
5802
5803       /* Parse another optional declarator.  */
5804       declarator = cp_parser_new_declarator_opt (parser);
5805
5806       return cp_parser_make_indirect_declarator
5807         (code, type, cv_quals, declarator);
5808     }
5809
5810   /* If the next token is a `[', there is a direct-new-declarator.  */
5811   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5812     return cp_parser_direct_new_declarator (parser);
5813
5814   return NULL;
5815 }
5816
5817 /* Parse a direct-new-declarator.
5818
5819    direct-new-declarator:
5820      [ expression ]
5821      direct-new-declarator [constant-expression]
5822
5823    */
5824
5825 static cp_declarator *
5826 cp_parser_direct_new_declarator (cp_parser* parser)
5827 {
5828   cp_declarator *declarator = NULL;
5829
5830   while (true)
5831     {
5832       tree expression;
5833
5834       /* Look for the opening `['.  */
5835       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5836       /* The first expression is not required to be constant.  */
5837       if (!declarator)
5838         {
5839           cp_token *token = cp_lexer_peek_token (parser->lexer);
5840           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5841           /* The standard requires that the expression have integral
5842              type.  DR 74 adds enumeration types.  We believe that the
5843              real intent is that these expressions be handled like the
5844              expression in a `switch' condition, which also allows
5845              classes with a single conversion to integral or
5846              enumeration type.  */
5847           if (!processing_template_decl)
5848             {
5849               expression
5850                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5851                                               expression,
5852                                               /*complain=*/true);
5853               if (!expression)
5854                 {
5855                   error ("%Hexpression in new-declarator must have integral "
5856                          "or enumeration type", &token->location);
5857                   expression = error_mark_node;
5858                 }
5859             }
5860         }
5861       /* But all the other expressions must be.  */
5862       else
5863         expression
5864           = cp_parser_constant_expression (parser,
5865                                            /*allow_non_constant=*/false,
5866                                            NULL);
5867       /* Look for the closing `]'.  */
5868       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5869
5870       /* Add this bound to the declarator.  */
5871       declarator = make_array_declarator (declarator, expression);
5872
5873       /* If the next token is not a `[', then there are no more
5874          bounds.  */
5875       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5876         break;
5877     }
5878
5879   return declarator;
5880 }
5881
5882 /* Parse a new-initializer.
5883
5884    new-initializer:
5885      ( expression-list [opt] )
5886      braced-init-list
5887
5888    Returns a representation of the expression-list.  If there is no
5889    expression-list, VOID_ZERO_NODE is returned.  */
5890
5891 static tree
5892 cp_parser_new_initializer (cp_parser* parser)
5893 {
5894   tree expression_list;
5895
5896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5897     {
5898       bool expr_non_constant_p;
5899       maybe_warn_cpp0x ("extended initializer lists");
5900       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5901       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5902       expression_list = build_tree_list (NULL_TREE, expression_list);
5903     }
5904   else
5905     expression_list = (cp_parser_parenthesized_expression_list
5906                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5907                         /*non_constant_p=*/NULL));
5908   if (!expression_list)
5909     expression_list = void_zero_node;
5910
5911   return expression_list;
5912 }
5913
5914 /* Parse a delete-expression.
5915
5916    delete-expression:
5917      :: [opt] delete cast-expression
5918      :: [opt] delete [ ] cast-expression
5919
5920    Returns a representation of the expression.  */
5921
5922 static tree
5923 cp_parser_delete_expression (cp_parser* parser)
5924 {
5925   bool global_scope_p;
5926   bool array_p;
5927   tree expression;
5928
5929   /* Look for the optional `::' operator.  */
5930   global_scope_p
5931     = (cp_parser_global_scope_opt (parser,
5932                                    /*current_scope_valid_p=*/false)
5933        != NULL_TREE);
5934   /* Look for the `delete' keyword.  */
5935   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5936   /* See if the array syntax is in use.  */
5937   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5938     {
5939       /* Consume the `[' token.  */
5940       cp_lexer_consume_token (parser->lexer);
5941       /* Look for the `]' token.  */
5942       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5943       /* Remember that this is the `[]' construct.  */
5944       array_p = true;
5945     }
5946   else
5947     array_p = false;
5948
5949   /* Parse the cast-expression.  */
5950   expression = cp_parser_simple_cast_expression (parser);
5951
5952   /* A delete-expression may not appear in an integral constant
5953      expression.  */
5954   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5955     return error_mark_node;
5956
5957   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5958 }
5959
5960 /* Returns true if TOKEN may start a cast-expression and false
5961    otherwise.  */
5962
5963 static bool
5964 cp_parser_token_starts_cast_expression (cp_token *token)
5965 {
5966   switch (token->type)
5967     {
5968     case CPP_COMMA:
5969     case CPP_SEMICOLON:
5970     case CPP_QUERY:
5971     case CPP_COLON:
5972     case CPP_CLOSE_SQUARE:
5973     case CPP_CLOSE_PAREN:
5974     case CPP_CLOSE_BRACE:
5975     case CPP_DOT:
5976     case CPP_DOT_STAR:
5977     case CPP_DEREF:
5978     case CPP_DEREF_STAR:
5979     case CPP_DIV:
5980     case CPP_MOD:
5981     case CPP_LSHIFT:
5982     case CPP_RSHIFT:
5983     case CPP_LESS:
5984     case CPP_GREATER:
5985     case CPP_LESS_EQ:
5986     case CPP_GREATER_EQ:
5987     case CPP_EQ_EQ:
5988     case CPP_NOT_EQ:
5989     case CPP_EQ:
5990     case CPP_MULT_EQ:
5991     case CPP_DIV_EQ:
5992     case CPP_MOD_EQ:
5993     case CPP_PLUS_EQ:
5994     case CPP_MINUS_EQ:
5995     case CPP_RSHIFT_EQ:
5996     case CPP_LSHIFT_EQ:
5997     case CPP_AND_EQ:
5998     case CPP_XOR_EQ:
5999     case CPP_OR_EQ:
6000     case CPP_XOR:
6001     case CPP_OR:
6002     case CPP_OR_OR:
6003     case CPP_EOF:
6004       return false;
6005
6006       /* '[' may start a primary-expression in obj-c++.  */
6007     case CPP_OPEN_SQUARE:
6008       return c_dialect_objc ();
6009
6010     default:
6011       return true;
6012     }
6013 }
6014
6015 /* Parse a cast-expression.
6016
6017    cast-expression:
6018      unary-expression
6019      ( type-id ) cast-expression
6020
6021    ADDRESS_P is true iff the unary-expression is appearing as the
6022    operand of the `&' operator.   CAST_P is true if this expression is
6023    the target of a cast.
6024
6025    Returns a representation of the expression.  */
6026
6027 static tree
6028 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6029                            cp_id_kind * pidk)
6030 {
6031   /* If it's a `(', then we might be looking at a cast.  */
6032   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6033     {
6034       tree type = NULL_TREE;
6035       tree expr = NULL_TREE;
6036       bool compound_literal_p;
6037       const char *saved_message;
6038
6039       /* There's no way to know yet whether or not this is a cast.
6040          For example, `(int (3))' is a unary-expression, while `(int)
6041          3' is a cast.  So, we resort to parsing tentatively.  */
6042       cp_parser_parse_tentatively (parser);
6043       /* Types may not be defined in a cast.  */
6044       saved_message = parser->type_definition_forbidden_message;
6045       parser->type_definition_forbidden_message
6046         = "types may not be defined in casts";
6047       /* Consume the `('.  */
6048       cp_lexer_consume_token (parser->lexer);
6049       /* A very tricky bit is that `(struct S) { 3 }' is a
6050          compound-literal (which we permit in C++ as an extension).
6051          But, that construct is not a cast-expression -- it is a
6052          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6053          is legal; if the compound-literal were a cast-expression,
6054          you'd need an extra set of parentheses.)  But, if we parse
6055          the type-id, and it happens to be a class-specifier, then we
6056          will commit to the parse at that point, because we cannot
6057          undo the action that is done when creating a new class.  So,
6058          then we cannot back up and do a postfix-expression.
6059
6060          Therefore, we scan ahead to the closing `)', and check to see
6061          if the token after the `)' is a `{'.  If so, we are not
6062          looking at a cast-expression.
6063
6064          Save tokens so that we can put them back.  */
6065       cp_lexer_save_tokens (parser->lexer);
6066       /* Skip tokens until the next token is a closing parenthesis.
6067          If we find the closing `)', and the next token is a `{', then
6068          we are looking at a compound-literal.  */
6069       compound_literal_p
6070         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6071                                                   /*consume_paren=*/true)
6072            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6073       /* Roll back the tokens we skipped.  */
6074       cp_lexer_rollback_tokens (parser->lexer);
6075       /* If we were looking at a compound-literal, simulate an error
6076          so that the call to cp_parser_parse_definitely below will
6077          fail.  */
6078       if (compound_literal_p)
6079         cp_parser_simulate_error (parser);
6080       else
6081         {
6082           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6083           parser->in_type_id_in_expr_p = true;
6084           /* Look for the type-id.  */
6085           type = cp_parser_type_id (parser);
6086           /* Look for the closing `)'.  */
6087           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6088           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6089         }
6090
6091       /* Restore the saved message.  */
6092       parser->type_definition_forbidden_message = saved_message;
6093
6094       /* At this point this can only be either a cast or a
6095          parenthesized ctor such as `(T ())' that looks like a cast to
6096          function returning T.  */
6097       if (!cp_parser_error_occurred (parser)
6098           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6099                                                      (parser->lexer)))
6100         {
6101           cp_parser_parse_definitely (parser);
6102           expr = cp_parser_cast_expression (parser,
6103                                             /*address_p=*/false,
6104                                             /*cast_p=*/true, pidk);
6105
6106           /* Warn about old-style casts, if so requested.  */
6107           if (warn_old_style_cast
6108               && !in_system_header
6109               && !VOID_TYPE_P (type)
6110               && current_lang_name != lang_name_c)
6111             warning (OPT_Wold_style_cast, "use of old-style cast");
6112
6113           /* Only type conversions to integral or enumeration types
6114              can be used in constant-expressions.  */
6115           if (!cast_valid_in_integral_constant_expression_p (type)
6116               && (cp_parser_non_integral_constant_expression
6117                   (parser,
6118                    "a cast to a type other than an integral or "
6119                    "enumeration type")))
6120             return error_mark_node;
6121
6122           /* Perform the cast.  */
6123           expr = build_c_cast (type, expr);
6124           return expr;
6125         }
6126       else 
6127         cp_parser_abort_tentative_parse (parser);
6128     }
6129
6130   /* If we get here, then it's not a cast, so it must be a
6131      unary-expression.  */
6132   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6133 }
6134
6135 /* Parse a binary expression of the general form:
6136
6137    pm-expression:
6138      cast-expression
6139      pm-expression .* cast-expression
6140      pm-expression ->* cast-expression
6141
6142    multiplicative-expression:
6143      pm-expression
6144      multiplicative-expression * pm-expression
6145      multiplicative-expression / pm-expression
6146      multiplicative-expression % pm-expression
6147
6148    additive-expression:
6149      multiplicative-expression
6150      additive-expression + multiplicative-expression
6151      additive-expression - multiplicative-expression
6152
6153    shift-expression:
6154      additive-expression
6155      shift-expression << additive-expression
6156      shift-expression >> additive-expression
6157
6158    relational-expression:
6159      shift-expression
6160      relational-expression < shift-expression
6161      relational-expression > shift-expression
6162      relational-expression <= shift-expression
6163      relational-expression >= shift-expression
6164
6165   GNU Extension:
6166
6167    relational-expression:
6168      relational-expression <? shift-expression
6169      relational-expression >? shift-expression
6170
6171    equality-expression:
6172      relational-expression
6173      equality-expression == relational-expression
6174      equality-expression != relational-expression
6175
6176    and-expression:
6177      equality-expression
6178      and-expression & equality-expression
6179
6180    exclusive-or-expression:
6181      and-expression
6182      exclusive-or-expression ^ and-expression
6183
6184    inclusive-or-expression:
6185      exclusive-or-expression
6186      inclusive-or-expression | exclusive-or-expression
6187
6188    logical-and-expression:
6189      inclusive-or-expression
6190      logical-and-expression && inclusive-or-expression
6191
6192    logical-or-expression:
6193      logical-and-expression
6194      logical-or-expression || logical-and-expression
6195
6196    All these are implemented with a single function like:
6197
6198    binary-expression:
6199      simple-cast-expression
6200      binary-expression <token> binary-expression
6201
6202    CAST_P is true if this expression is the target of a cast.
6203
6204    The binops_by_token map is used to get the tree codes for each <token> type.
6205    binary-expressions are associated according to a precedence table.  */
6206
6207 #define TOKEN_PRECEDENCE(token)                              \
6208 (((token->type == CPP_GREATER                                \
6209    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6210   && !parser->greater_than_is_operator_p)                    \
6211  ? PREC_NOT_OPERATOR                                         \
6212  : binops_by_token[token->type].prec)
6213
6214 static tree
6215 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6216                              bool no_toplevel_fold_p,
6217                              enum cp_parser_prec prec,
6218                              cp_id_kind * pidk)
6219 {
6220   cp_parser_expression_stack stack;
6221   cp_parser_expression_stack_entry *sp = &stack[0];
6222   tree lhs, rhs;
6223   cp_token *token;
6224   enum tree_code tree_type, lhs_type, rhs_type;
6225   enum cp_parser_prec new_prec, lookahead_prec;
6226   bool overloaded_p;
6227
6228   /* Parse the first expression.  */
6229   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6230   lhs_type = ERROR_MARK;
6231
6232   for (;;)
6233     {
6234       /* Get an operator token.  */
6235       token = cp_lexer_peek_token (parser->lexer);
6236
6237       if (warn_cxx0x_compat
6238           && token->type == CPP_RSHIFT
6239           && !parser->greater_than_is_operator_p)
6240         {
6241           warning (OPT_Wc__0x_compat, 
6242                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6243                    &token->location);
6244           warning (OPT_Wc__0x_compat, 
6245                    "suggest parentheses around %<>>%> expression");
6246         }
6247
6248       new_prec = TOKEN_PRECEDENCE (token);
6249
6250       /* Popping an entry off the stack means we completed a subexpression:
6251          - either we found a token which is not an operator (`>' where it is not
6252            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6253            will happen repeatedly;
6254          - or, we found an operator which has lower priority.  This is the case
6255            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6256            parsing `3 * 4'.  */
6257       if (new_prec <= prec)
6258         {
6259           if (sp == stack)
6260             break;
6261           else
6262             goto pop;
6263         }
6264
6265      get_rhs:
6266       tree_type = binops_by_token[token->type].tree_type;
6267
6268       /* We used the operator token.  */
6269       cp_lexer_consume_token (parser->lexer);
6270
6271       /* Extract another operand.  It may be the RHS of this expression
6272          or the LHS of a new, higher priority expression.  */
6273       rhs = cp_parser_simple_cast_expression (parser);
6274       rhs_type = ERROR_MARK;
6275
6276       /* Get another operator token.  Look up its precedence to avoid
6277          building a useless (immediately popped) stack entry for common
6278          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6279       token = cp_lexer_peek_token (parser->lexer);
6280       lookahead_prec = TOKEN_PRECEDENCE (token);
6281       if (lookahead_prec > new_prec)
6282         {
6283           /* ... and prepare to parse the RHS of the new, higher priority
6284              expression.  Since precedence levels on the stack are
6285              monotonically increasing, we do not have to care about
6286              stack overflows.  */
6287           sp->prec = prec;
6288           sp->tree_type = tree_type;
6289           sp->lhs = lhs;
6290           sp->lhs_type = lhs_type;
6291           sp++;
6292           lhs = rhs;
6293           lhs_type = rhs_type;
6294           prec = new_prec;
6295           new_prec = lookahead_prec;
6296           goto get_rhs;
6297
6298          pop:
6299           lookahead_prec = new_prec;
6300           /* If the stack is not empty, we have parsed into LHS the right side
6301              (`4' in the example above) of an expression we had suspended.
6302              We can use the information on the stack to recover the LHS (`3')
6303              from the stack together with the tree code (`MULT_EXPR'), and
6304              the precedence of the higher level subexpression
6305              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6306              which will be used to actually build the additive expression.  */
6307           --sp;
6308           prec = sp->prec;
6309           tree_type = sp->tree_type;
6310           rhs = lhs;
6311           rhs_type = lhs_type;
6312           lhs = sp->lhs;
6313           lhs_type = sp->lhs_type;
6314         }
6315
6316       overloaded_p = false;
6317       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6318          ERROR_MARK for everything that is not a binary expression.
6319          This makes warn_about_parentheses miss some warnings that
6320          involve unary operators.  For unary expressions we should
6321          pass the correct tree_code unless the unary expression was
6322          surrounded by parentheses.
6323       */
6324       if (no_toplevel_fold_p
6325           && lookahead_prec <= prec
6326           && sp == stack
6327           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6328         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6329       else
6330         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6331                                  &overloaded_p, tf_warning_or_error);
6332       lhs_type = tree_type;
6333
6334       /* If the binary operator required the use of an overloaded operator,
6335          then this expression cannot be an integral constant-expression.
6336          An overloaded operator can be used even if both operands are
6337          otherwise permissible in an integral constant-expression if at
6338          least one of the operands is of enumeration type.  */
6339
6340       if (overloaded_p
6341           && (cp_parser_non_integral_constant_expression
6342               (parser, "calls to overloaded operators")))
6343         return error_mark_node;
6344     }
6345
6346   return lhs;
6347 }
6348
6349
6350 /* Parse the `? expression : assignment-expression' part of a
6351    conditional-expression.  The LOGICAL_OR_EXPR is the
6352    logical-or-expression that started the conditional-expression.
6353    Returns a representation of the entire conditional-expression.
6354
6355    This routine is used by cp_parser_assignment_expression.
6356
6357      ? expression : assignment-expression
6358
6359    GNU Extensions:
6360
6361      ? : assignment-expression */
6362
6363 static tree
6364 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6365 {
6366   tree expr;
6367   tree assignment_expr;
6368
6369   /* Consume the `?' token.  */
6370   cp_lexer_consume_token (parser->lexer);
6371   if (cp_parser_allow_gnu_extensions_p (parser)
6372       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6373     /* Implicit true clause.  */
6374     expr = NULL_TREE;
6375   else
6376     /* Parse the expression.  */
6377     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6378
6379   /* The next token should be a `:'.  */
6380   cp_parser_require (parser, CPP_COLON, "%<:%>");
6381   /* Parse the assignment-expression.  */
6382   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6383
6384   /* Build the conditional-expression.  */
6385   return build_x_conditional_expr (logical_or_expr,
6386                                    expr,
6387                                    assignment_expr,
6388                                    tf_warning_or_error);
6389 }
6390
6391 /* Parse an assignment-expression.
6392
6393    assignment-expression:
6394      conditional-expression
6395      logical-or-expression assignment-operator assignment_expression
6396      throw-expression
6397
6398    CAST_P is true if this expression is the target of a cast.
6399
6400    Returns a representation for the expression.  */
6401
6402 static tree
6403 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6404                                  cp_id_kind * pidk)
6405 {
6406   tree expr;
6407
6408   /* If the next token is the `throw' keyword, then we're looking at
6409      a throw-expression.  */
6410   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6411     expr = cp_parser_throw_expression (parser);
6412   /* Otherwise, it must be that we are looking at a
6413      logical-or-expression.  */
6414   else
6415     {
6416       /* Parse the binary expressions (logical-or-expression).  */
6417       expr = cp_parser_binary_expression (parser, cast_p, false,
6418                                           PREC_NOT_OPERATOR, pidk);
6419       /* If the next token is a `?' then we're actually looking at a
6420          conditional-expression.  */
6421       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6422         return cp_parser_question_colon_clause (parser, expr);
6423       else
6424         {
6425           enum tree_code assignment_operator;
6426
6427           /* If it's an assignment-operator, we're using the second
6428              production.  */
6429           assignment_operator
6430             = cp_parser_assignment_operator_opt (parser);
6431           if (assignment_operator != ERROR_MARK)
6432             {
6433               bool non_constant_p;
6434
6435               /* Parse the right-hand side of the assignment.  */
6436               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6437
6438               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6439                 maybe_warn_cpp0x ("extended initializer lists");
6440
6441               /* An assignment may not appear in a
6442                  constant-expression.  */
6443               if (cp_parser_non_integral_constant_expression (parser,
6444                                                               "an assignment"))
6445                 return error_mark_node;
6446               /* Build the assignment expression.  */
6447               expr = build_x_modify_expr (expr,
6448                                           assignment_operator,
6449                                           rhs,
6450                                           tf_warning_or_error);
6451             }
6452         }
6453     }
6454
6455   return expr;
6456 }
6457
6458 /* Parse an (optional) assignment-operator.
6459
6460    assignment-operator: one of
6461      = *= /= %= += -= >>= <<= &= ^= |=
6462
6463    GNU Extension:
6464
6465    assignment-operator: one of
6466      <?= >?=
6467
6468    If the next token is an assignment operator, the corresponding tree
6469    code is returned, and the token is consumed.  For example, for
6470    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6471    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6472    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6473    operator, ERROR_MARK is returned.  */
6474
6475 static enum tree_code
6476 cp_parser_assignment_operator_opt (cp_parser* parser)
6477 {
6478   enum tree_code op;
6479   cp_token *token;
6480
6481   /* Peek at the next token.  */
6482   token = cp_lexer_peek_token (parser->lexer);
6483
6484   switch (token->type)
6485     {
6486     case CPP_EQ:
6487       op = NOP_EXPR;
6488       break;
6489
6490     case CPP_MULT_EQ:
6491       op = MULT_EXPR;
6492       break;
6493
6494     case CPP_DIV_EQ:
6495       op = TRUNC_DIV_EXPR;
6496       break;
6497
6498     case CPP_MOD_EQ:
6499       op = TRUNC_MOD_EXPR;
6500       break;
6501
6502     case CPP_PLUS_EQ:
6503       op = PLUS_EXPR;
6504       break;
6505
6506     case CPP_MINUS_EQ:
6507       op = MINUS_EXPR;
6508       break;
6509
6510     case CPP_RSHIFT_EQ:
6511       op = RSHIFT_EXPR;
6512       break;
6513
6514     case CPP_LSHIFT_EQ:
6515       op = LSHIFT_EXPR;
6516       break;
6517
6518     case CPP_AND_EQ:
6519       op = BIT_AND_EXPR;
6520       break;
6521
6522     case CPP_XOR_EQ:
6523       op = BIT_XOR_EXPR;
6524       break;
6525
6526     case CPP_OR_EQ:
6527       op = BIT_IOR_EXPR;
6528       break;
6529
6530     default:
6531       /* Nothing else is an assignment operator.  */
6532       op = ERROR_MARK;
6533     }
6534
6535   /* If it was an assignment operator, consume it.  */
6536   if (op != ERROR_MARK)
6537     cp_lexer_consume_token (parser->lexer);
6538
6539   return op;
6540 }
6541
6542 /* Parse an expression.
6543
6544    expression:
6545      assignment-expression
6546      expression , assignment-expression
6547
6548    CAST_P is true if this expression is the target of a cast.
6549
6550    Returns a representation of the expression.  */
6551
6552 static tree
6553 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6554 {
6555   tree expression = NULL_TREE;
6556
6557   while (true)
6558     {
6559       tree assignment_expression;
6560
6561       /* Parse the next assignment-expression.  */
6562       assignment_expression
6563         = cp_parser_assignment_expression (parser, cast_p, pidk);
6564       /* If this is the first assignment-expression, we can just
6565          save it away.  */
6566       if (!expression)
6567         expression = assignment_expression;
6568       else
6569         expression = build_x_compound_expr (expression,
6570                                             assignment_expression,
6571                                             tf_warning_or_error);
6572       /* If the next token is not a comma, then we are done with the
6573          expression.  */
6574       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6575         break;
6576       /* Consume the `,'.  */
6577       cp_lexer_consume_token (parser->lexer);
6578       /* A comma operator cannot appear in a constant-expression.  */
6579       if (cp_parser_non_integral_constant_expression (parser,
6580                                                       "a comma operator"))
6581         expression = error_mark_node;
6582     }
6583
6584   return expression;
6585 }
6586
6587 /* Parse a constant-expression.
6588
6589    constant-expression:
6590      conditional-expression
6591
6592   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6593   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6594   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6595   is false, NON_CONSTANT_P should be NULL.  */
6596
6597 static tree
6598 cp_parser_constant_expression (cp_parser* parser,
6599                                bool allow_non_constant_p,
6600                                bool *non_constant_p)
6601 {
6602   bool saved_integral_constant_expression_p;
6603   bool saved_allow_non_integral_constant_expression_p;
6604   bool saved_non_integral_constant_expression_p;
6605   tree expression;
6606
6607   /* It might seem that we could simply parse the
6608      conditional-expression, and then check to see if it were
6609      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6610      one that the compiler can figure out is constant, possibly after
6611      doing some simplifications or optimizations.  The standard has a
6612      precise definition of constant-expression, and we must honor
6613      that, even though it is somewhat more restrictive.
6614
6615      For example:
6616
6617        int i[(2, 3)];
6618
6619      is not a legal declaration, because `(2, 3)' is not a
6620      constant-expression.  The `,' operator is forbidden in a
6621      constant-expression.  However, GCC's constant-folding machinery
6622      will fold this operation to an INTEGER_CST for `3'.  */
6623
6624   /* Save the old settings.  */
6625   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6626   saved_allow_non_integral_constant_expression_p
6627     = parser->allow_non_integral_constant_expression_p;
6628   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6629   /* We are now parsing a constant-expression.  */
6630   parser->integral_constant_expression_p = true;
6631   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6632   parser->non_integral_constant_expression_p = false;
6633   /* Although the grammar says "conditional-expression", we parse an
6634      "assignment-expression", which also permits "throw-expression"
6635      and the use of assignment operators.  In the case that
6636      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6637      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6638      actually essential that we look for an assignment-expression.
6639      For example, cp_parser_initializer_clauses uses this function to
6640      determine whether a particular assignment-expression is in fact
6641      constant.  */
6642   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6643   /* Restore the old settings.  */
6644   parser->integral_constant_expression_p
6645     = saved_integral_constant_expression_p;
6646   parser->allow_non_integral_constant_expression_p
6647     = saved_allow_non_integral_constant_expression_p;
6648   if (allow_non_constant_p)
6649     *non_constant_p = parser->non_integral_constant_expression_p;
6650   else if (parser->non_integral_constant_expression_p)
6651     expression = error_mark_node;
6652   parser->non_integral_constant_expression_p
6653     = saved_non_integral_constant_expression_p;
6654
6655   return expression;
6656 }
6657
6658 /* Parse __builtin_offsetof.
6659
6660    offsetof-expression:
6661      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6662
6663    offsetof-member-designator:
6664      id-expression
6665      | offsetof-member-designator "." id-expression
6666      | offsetof-member-designator "[" expression "]"
6667      | offsetof-member-designator "->" id-expression  */
6668
6669 static tree
6670 cp_parser_builtin_offsetof (cp_parser *parser)
6671 {
6672   int save_ice_p, save_non_ice_p;
6673   tree type, expr;
6674   cp_id_kind dummy;
6675   cp_token *token;
6676
6677   /* We're about to accept non-integral-constant things, but will
6678      definitely yield an integral constant expression.  Save and
6679      restore these values around our local parsing.  */
6680   save_ice_p = parser->integral_constant_expression_p;
6681   save_non_ice_p = parser->non_integral_constant_expression_p;
6682
6683   /* Consume the "__builtin_offsetof" token.  */
6684   cp_lexer_consume_token (parser->lexer);
6685   /* Consume the opening `('.  */
6686   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6687   /* Parse the type-id.  */
6688   type = cp_parser_type_id (parser);
6689   /* Look for the `,'.  */
6690   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6691   token = cp_lexer_peek_token (parser->lexer);
6692
6693   /* Build the (type *)null that begins the traditional offsetof macro.  */
6694   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6695                             tf_warning_or_error);
6696
6697   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6698   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6699                                                  true, &dummy, token->location);
6700   while (true)
6701     {
6702       token = cp_lexer_peek_token (parser->lexer);
6703       switch (token->type)
6704         {
6705         case CPP_OPEN_SQUARE:
6706           /* offsetof-member-designator "[" expression "]" */
6707           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6708           break;
6709
6710         case CPP_DEREF:
6711           /* offsetof-member-designator "->" identifier */
6712           expr = grok_array_decl (expr, integer_zero_node);
6713           /* FALLTHRU */
6714
6715         case CPP_DOT:
6716           /* offsetof-member-designator "." identifier */
6717           cp_lexer_consume_token (parser->lexer);
6718           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6719                                                          expr, true, &dummy,
6720                                                          token->location);
6721           break;
6722
6723         case CPP_CLOSE_PAREN:
6724           /* Consume the ")" token.  */
6725           cp_lexer_consume_token (parser->lexer);
6726           goto success;
6727
6728         default:
6729           /* Error.  We know the following require will fail, but
6730              that gives the proper error message.  */
6731           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6732           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6733           expr = error_mark_node;
6734           goto failure;
6735         }
6736     }
6737
6738  success:
6739   /* If we're processing a template, we can't finish the semantics yet.
6740      Otherwise we can fold the entire expression now.  */
6741   if (processing_template_decl)
6742     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6743   else
6744     expr = finish_offsetof (expr);
6745
6746  failure:
6747   parser->integral_constant_expression_p = save_ice_p;
6748   parser->non_integral_constant_expression_p = save_non_ice_p;
6749
6750   return expr;
6751 }
6752
6753 /* Parse a trait expression.  */
6754
6755 static tree
6756 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6757 {
6758   cp_trait_kind kind;
6759   tree type1, type2 = NULL_TREE;
6760   bool binary = false;
6761   cp_decl_specifier_seq decl_specs;
6762
6763   switch (keyword)
6764     {
6765     case RID_HAS_NOTHROW_ASSIGN:
6766       kind = CPTK_HAS_NOTHROW_ASSIGN;
6767       break;
6768     case RID_HAS_NOTHROW_CONSTRUCTOR:
6769       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6770       break;
6771     case RID_HAS_NOTHROW_COPY:
6772       kind = CPTK_HAS_NOTHROW_COPY;
6773       break;
6774     case RID_HAS_TRIVIAL_ASSIGN:
6775       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6776       break;
6777     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6778       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6779       break;
6780     case RID_HAS_TRIVIAL_COPY:
6781       kind = CPTK_HAS_TRIVIAL_COPY;
6782       break;
6783     case RID_HAS_TRIVIAL_DESTRUCTOR:
6784       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6785       break;
6786     case RID_HAS_VIRTUAL_DESTRUCTOR:
6787       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6788       break;
6789     case RID_IS_ABSTRACT:
6790       kind = CPTK_IS_ABSTRACT;
6791       break;
6792     case RID_IS_BASE_OF:
6793       kind = CPTK_IS_BASE_OF;
6794       binary = true;
6795       break;
6796     case RID_IS_CLASS:
6797       kind = CPTK_IS_CLASS;
6798       break;
6799     case RID_IS_CONVERTIBLE_TO:
6800       kind = CPTK_IS_CONVERTIBLE_TO;
6801       binary = true;
6802       break;
6803     case RID_IS_EMPTY:
6804       kind = CPTK_IS_EMPTY;
6805       break;
6806     case RID_IS_ENUM:
6807       kind = CPTK_IS_ENUM;
6808       break;
6809     case RID_IS_POD:
6810       kind = CPTK_IS_POD;
6811       break;
6812     case RID_IS_POLYMORPHIC:
6813       kind = CPTK_IS_POLYMORPHIC;
6814       break;
6815     case RID_IS_UNION:
6816       kind = CPTK_IS_UNION;
6817       break;
6818     default:
6819       gcc_unreachable ();
6820     }
6821
6822   /* Consume the token.  */
6823   cp_lexer_consume_token (parser->lexer);
6824
6825   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6826
6827   type1 = cp_parser_type_id (parser);
6828
6829   if (type1 == error_mark_node)
6830     return error_mark_node;
6831
6832   /* Build a trivial decl-specifier-seq.  */
6833   clear_decl_specs (&decl_specs);
6834   decl_specs.type = type1;
6835
6836   /* Call grokdeclarator to figure out what type this is.  */
6837   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6838                           /*initialized=*/0, /*attrlist=*/NULL);
6839
6840   if (binary)
6841     {
6842       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6843  
6844       type2 = cp_parser_type_id (parser);
6845
6846       if (type2 == error_mark_node)
6847         return error_mark_node;
6848
6849       /* Build a trivial decl-specifier-seq.  */
6850       clear_decl_specs (&decl_specs);
6851       decl_specs.type = type2;
6852
6853       /* Call grokdeclarator to figure out what type this is.  */
6854       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6855                               /*initialized=*/0, /*attrlist=*/NULL);
6856     }
6857
6858   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6859
6860   /* Complete the trait expression, which may mean either processing
6861      the trait expr now or saving it for template instantiation.  */
6862   return finish_trait_expr (kind, type1, type2);
6863 }
6864
6865 /* Statements [gram.stmt.stmt]  */
6866
6867 /* Parse a statement.
6868
6869    statement:
6870      labeled-statement
6871      expression-statement
6872      compound-statement
6873      selection-statement
6874      iteration-statement
6875      jump-statement
6876      declaration-statement
6877      try-block
6878
6879   IN_COMPOUND is true when the statement is nested inside a
6880   cp_parser_compound_statement; this matters for certain pragmas.
6881
6882   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6883   is a (possibly labeled) if statement which is not enclosed in braces
6884   and has an else clause.  This is used to implement -Wparentheses.  */
6885
6886 static void
6887 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6888                      bool in_compound, bool *if_p)
6889 {
6890   tree statement;
6891   cp_token *token;
6892   location_t statement_location;
6893
6894  restart:
6895   if (if_p != NULL)
6896     *if_p = false;
6897   /* There is no statement yet.  */
6898   statement = NULL_TREE;
6899   /* Peek at the next token.  */
6900   token = cp_lexer_peek_token (parser->lexer);
6901   /* Remember the location of the first token in the statement.  */
6902   statement_location = token->location;
6903   /* If this is a keyword, then that will often determine what kind of
6904      statement we have.  */
6905   if (token->type == CPP_KEYWORD)
6906     {
6907       enum rid keyword = token->keyword;
6908
6909       switch (keyword)
6910         {
6911         case RID_CASE:
6912         case RID_DEFAULT:
6913           /* Looks like a labeled-statement with a case label.
6914              Parse the label, and then use tail recursion to parse
6915              the statement.  */
6916           cp_parser_label_for_labeled_statement (parser);
6917           goto restart;
6918
6919         case RID_IF:
6920         case RID_SWITCH:
6921           statement = cp_parser_selection_statement (parser, if_p);
6922           break;
6923
6924         case RID_WHILE:
6925         case RID_DO:
6926         case RID_FOR:
6927           statement = cp_parser_iteration_statement (parser);
6928           break;
6929
6930         case RID_BREAK:
6931         case RID_CONTINUE:
6932         case RID_RETURN:
6933         case RID_GOTO:
6934           statement = cp_parser_jump_statement (parser);
6935           break;
6936
6937           /* Objective-C++ exception-handling constructs.  */
6938         case RID_AT_TRY:
6939         case RID_AT_CATCH:
6940         case RID_AT_FINALLY:
6941         case RID_AT_SYNCHRONIZED:
6942         case RID_AT_THROW:
6943           statement = cp_parser_objc_statement (parser);
6944           break;
6945
6946         case RID_TRY:
6947           statement = cp_parser_try_block (parser);
6948           break;
6949
6950         case RID_NAMESPACE:
6951           /* This must be a namespace alias definition.  */
6952           cp_parser_declaration_statement (parser);
6953           return;
6954           
6955         default:
6956           /* It might be a keyword like `int' that can start a
6957              declaration-statement.  */
6958           break;
6959         }
6960     }
6961   else if (token->type == CPP_NAME)
6962     {
6963       /* If the next token is a `:', then we are looking at a
6964          labeled-statement.  */
6965       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6966       if (token->type == CPP_COLON)
6967         {
6968           /* Looks like a labeled-statement with an ordinary label.
6969              Parse the label, and then use tail recursion to parse
6970              the statement.  */
6971           cp_parser_label_for_labeled_statement (parser);
6972           goto restart;
6973         }
6974     }
6975   /* Anything that starts with a `{' must be a compound-statement.  */
6976   else if (token->type == CPP_OPEN_BRACE)
6977     statement = cp_parser_compound_statement (parser, NULL, false);
6978   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6979      a statement all its own.  */
6980   else if (token->type == CPP_PRAGMA)
6981     {
6982       /* Only certain OpenMP pragmas are attached to statements, and thus
6983          are considered statements themselves.  All others are not.  In
6984          the context of a compound, accept the pragma as a "statement" and
6985          return so that we can check for a close brace.  Otherwise we
6986          require a real statement and must go back and read one.  */
6987       if (in_compound)
6988         cp_parser_pragma (parser, pragma_compound);
6989       else if (!cp_parser_pragma (parser, pragma_stmt))
6990         goto restart;
6991       return;
6992     }
6993   else if (token->type == CPP_EOF)
6994     {
6995       cp_parser_error (parser, "expected statement");
6996       return;
6997     }
6998
6999   /* Everything else must be a declaration-statement or an
7000      expression-statement.  Try for the declaration-statement
7001      first, unless we are looking at a `;', in which case we know that
7002      we have an expression-statement.  */
7003   if (!statement)
7004     {
7005       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7006         {
7007           cp_parser_parse_tentatively (parser);
7008           /* Try to parse the declaration-statement.  */
7009           cp_parser_declaration_statement (parser);
7010           /* If that worked, we're done.  */
7011           if (cp_parser_parse_definitely (parser))
7012             return;
7013         }
7014       /* Look for an expression-statement instead.  */
7015       statement = cp_parser_expression_statement (parser, in_statement_expr);
7016     }
7017
7018   /* Set the line number for the statement.  */
7019   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7020     SET_EXPR_LOCATION (statement, statement_location);
7021 }
7022
7023 /* Parse the label for a labeled-statement, i.e.
7024
7025    identifier :
7026    case constant-expression :
7027    default :
7028
7029    GNU Extension:
7030    case constant-expression ... constant-expression : statement
7031
7032    When a label is parsed without errors, the label is added to the
7033    parse tree by the finish_* functions, so this function doesn't
7034    have to return the label.  */
7035
7036 static void
7037 cp_parser_label_for_labeled_statement (cp_parser* parser)
7038 {
7039   cp_token *token;
7040
7041   /* The next token should be an identifier.  */
7042   token = cp_lexer_peek_token (parser->lexer);
7043   if (token->type != CPP_NAME
7044       && token->type != CPP_KEYWORD)
7045     {
7046       cp_parser_error (parser, "expected labeled-statement");
7047       return;
7048     }
7049
7050   switch (token->keyword)
7051     {
7052     case RID_CASE:
7053       {
7054         tree expr, expr_hi;
7055         cp_token *ellipsis;
7056
7057         /* Consume the `case' token.  */
7058         cp_lexer_consume_token (parser->lexer);
7059         /* Parse the constant-expression.  */
7060         expr = cp_parser_constant_expression (parser,
7061                                               /*allow_non_constant_p=*/false,
7062                                               NULL);
7063
7064         ellipsis = cp_lexer_peek_token (parser->lexer);
7065         if (ellipsis->type == CPP_ELLIPSIS)
7066           {
7067             /* Consume the `...' token.  */
7068             cp_lexer_consume_token (parser->lexer);
7069             expr_hi =
7070               cp_parser_constant_expression (parser,
7071                                              /*allow_non_constant_p=*/false,
7072                                              NULL);
7073             /* We don't need to emit warnings here, as the common code
7074                will do this for us.  */
7075           }
7076         else
7077           expr_hi = NULL_TREE;
7078
7079         if (parser->in_switch_statement_p)
7080           finish_case_label (expr, expr_hi);
7081         else
7082           error ("%Hcase label %qE not within a switch statement",
7083                  &token->location, expr);
7084       }
7085       break;
7086
7087     case RID_DEFAULT:
7088       /* Consume the `default' token.  */
7089       cp_lexer_consume_token (parser->lexer);
7090
7091       if (parser->in_switch_statement_p)
7092         finish_case_label (NULL_TREE, NULL_TREE);
7093       else
7094         error ("%Hcase label not within a switch statement", &token->location);
7095       break;
7096
7097     default:
7098       /* Anything else must be an ordinary label.  */
7099       finish_label_stmt (cp_parser_identifier (parser));
7100       break;
7101     }
7102
7103   /* Require the `:' token.  */
7104   cp_parser_require (parser, CPP_COLON, "%<:%>");
7105 }
7106
7107 /* Parse an expression-statement.
7108
7109    expression-statement:
7110      expression [opt] ;
7111
7112    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7113    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7114    indicates whether this expression-statement is part of an
7115    expression statement.  */
7116
7117 static tree
7118 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7119 {
7120   tree statement = NULL_TREE;
7121
7122   /* If the next token is a ';', then there is no expression
7123      statement.  */
7124   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7125     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7126
7127   /* Consume the final `;'.  */
7128   cp_parser_consume_semicolon_at_end_of_statement (parser);
7129
7130   if (in_statement_expr
7131       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7132     /* This is the final expression statement of a statement
7133        expression.  */
7134     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7135   else if (statement)
7136     statement = finish_expr_stmt (statement);
7137   else
7138     finish_stmt ();
7139
7140   return statement;
7141 }
7142
7143 /* Parse a compound-statement.
7144
7145    compound-statement:
7146      { statement-seq [opt] }
7147
7148    GNU extension:
7149
7150    compound-statement:
7151      { label-declaration-seq [opt] statement-seq [opt] }
7152
7153    label-declaration-seq:
7154      label-declaration
7155      label-declaration-seq label-declaration
7156
7157    Returns a tree representing the statement.  */
7158
7159 static tree
7160 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7161                               bool in_try)
7162 {
7163   tree compound_stmt;
7164
7165   /* Consume the `{'.  */
7166   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7167     return error_mark_node;
7168   /* Begin the compound-statement.  */
7169   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7170   /* If the next keyword is `__label__' we have a label declaration.  */
7171   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7172     cp_parser_label_declaration (parser);
7173   /* Parse an (optional) statement-seq.  */
7174   cp_parser_statement_seq_opt (parser, in_statement_expr);
7175   /* Finish the compound-statement.  */
7176   finish_compound_stmt (compound_stmt);
7177   /* Consume the `}'.  */
7178   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7179
7180   return compound_stmt;
7181 }
7182
7183 /* Parse an (optional) statement-seq.
7184
7185    statement-seq:
7186      statement
7187      statement-seq [opt] statement  */
7188
7189 static void
7190 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7191 {
7192   /* Scan statements until there aren't any more.  */
7193   while (true)
7194     {
7195       cp_token *token = cp_lexer_peek_token (parser->lexer);
7196
7197       /* If we're looking at a `}', then we've run out of statements.  */
7198       if (token->type == CPP_CLOSE_BRACE
7199           || token->type == CPP_EOF
7200           || token->type == CPP_PRAGMA_EOL)
7201         break;
7202       
7203       /* If we are in a compound statement and find 'else' then
7204          something went wrong.  */
7205       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7206         {
7207           if (parser->in_statement & IN_IF_STMT) 
7208             break;
7209           else
7210             {
7211               token = cp_lexer_consume_token (parser->lexer);
7212               error ("%H%<else%> without a previous %<if%>", &token->location);
7213             }
7214         }
7215
7216       /* Parse the statement.  */
7217       cp_parser_statement (parser, in_statement_expr, true, NULL);
7218     }
7219 }
7220
7221 /* Parse a selection-statement.
7222
7223    selection-statement:
7224      if ( condition ) statement
7225      if ( condition ) statement else statement
7226      switch ( condition ) statement
7227
7228    Returns the new IF_STMT or SWITCH_STMT.
7229
7230    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7231    is a (possibly labeled) if statement which is not enclosed in
7232    braces and has an else clause.  This is used to implement
7233    -Wparentheses.  */
7234
7235 static tree
7236 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7237 {
7238   cp_token *token;
7239   enum rid keyword;
7240
7241   if (if_p != NULL)
7242     *if_p = false;
7243
7244   /* Peek at the next token.  */
7245   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7246
7247   /* See what kind of keyword it is.  */
7248   keyword = token->keyword;
7249   switch (keyword)
7250     {
7251     case RID_IF:
7252     case RID_SWITCH:
7253       {
7254         tree statement;
7255         tree condition;
7256
7257         /* Look for the `('.  */
7258         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7259           {
7260             cp_parser_skip_to_end_of_statement (parser);
7261             return error_mark_node;
7262           }
7263
7264         /* Begin the selection-statement.  */
7265         if (keyword == RID_IF)
7266           statement = begin_if_stmt ();
7267         else
7268           statement = begin_switch_stmt ();
7269
7270         /* Parse the condition.  */
7271         condition = cp_parser_condition (parser);
7272         /* Look for the `)'.  */
7273         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7274           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7275                                                  /*consume_paren=*/true);
7276
7277         if (keyword == RID_IF)
7278           {
7279             bool nested_if;
7280             unsigned char in_statement;
7281
7282             /* Add the condition.  */
7283             finish_if_stmt_cond (condition, statement);
7284
7285             /* Parse the then-clause.  */
7286             in_statement = parser->in_statement;
7287             parser->in_statement |= IN_IF_STMT;
7288             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7289               {
7290                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7291                 add_stmt (build_empty_stmt ());
7292                 cp_lexer_consume_token (parser->lexer);
7293                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7294                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7295                               "empty body in an %<if%> statement");
7296                 nested_if = false;
7297               }
7298             else
7299               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7300             parser->in_statement = in_statement;
7301
7302             finish_then_clause (statement);
7303
7304             /* If the next token is `else', parse the else-clause.  */
7305             if (cp_lexer_next_token_is_keyword (parser->lexer,
7306                                                 RID_ELSE))
7307               {
7308                 /* Consume the `else' keyword.  */
7309                 cp_lexer_consume_token (parser->lexer);
7310                 begin_else_clause (statement);
7311                 /* Parse the else-clause.  */
7312                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7313                   {
7314                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7315                                 OPT_Wempty_body, "suggest braces around "
7316                                 "empty body in an %<else%> statement");
7317                     add_stmt (build_empty_stmt ());
7318                     cp_lexer_consume_token (parser->lexer);
7319                   }
7320                 else
7321                   cp_parser_implicitly_scoped_statement (parser, NULL);
7322
7323                 finish_else_clause (statement);
7324
7325                 /* If we are currently parsing a then-clause, then
7326                    IF_P will not be NULL.  We set it to true to
7327                    indicate that this if statement has an else clause.
7328                    This may trigger the Wparentheses warning below
7329                    when we get back up to the parent if statement.  */
7330                 if (if_p != NULL)
7331                   *if_p = true;
7332               }
7333             else
7334               {
7335                 /* This if statement does not have an else clause.  If
7336                    NESTED_IF is true, then the then-clause is an if
7337                    statement which does have an else clause.  We warn
7338                    about the potential ambiguity.  */
7339                 if (nested_if)
7340                   warning (OPT_Wparentheses,
7341                            ("%Hsuggest explicit braces "
7342                             "to avoid ambiguous %<else%>"),
7343                            EXPR_LOCUS (statement));
7344               }
7345
7346             /* Now we're all done with the if-statement.  */
7347             finish_if_stmt (statement);
7348           }
7349         else
7350           {
7351             bool in_switch_statement_p;
7352             unsigned char in_statement;
7353
7354             /* Add the condition.  */
7355             finish_switch_cond (condition, statement);
7356
7357             /* Parse the body of the switch-statement.  */
7358             in_switch_statement_p = parser->in_switch_statement_p;
7359             in_statement = parser->in_statement;
7360             parser->in_switch_statement_p = true;
7361             parser->in_statement |= IN_SWITCH_STMT;
7362             cp_parser_implicitly_scoped_statement (parser, NULL);
7363             parser->in_switch_statement_p = in_switch_statement_p;
7364             parser->in_statement = in_statement;
7365
7366             /* Now we're all done with the switch-statement.  */
7367             finish_switch_stmt (statement);
7368           }
7369
7370         return statement;
7371       }
7372       break;
7373
7374     default:
7375       cp_parser_error (parser, "expected selection-statement");
7376       return error_mark_node;
7377     }
7378 }
7379
7380 /* Parse a condition.
7381
7382    condition:
7383      expression
7384      type-specifier-seq declarator = initializer-clause
7385      type-specifier-seq declarator braced-init-list
7386
7387    GNU Extension:
7388
7389    condition:
7390      type-specifier-seq declarator asm-specification [opt]
7391        attributes [opt] = assignment-expression
7392
7393    Returns the expression that should be tested.  */
7394
7395 static tree
7396 cp_parser_condition (cp_parser* parser)
7397 {
7398   cp_decl_specifier_seq type_specifiers;
7399   const char *saved_message;
7400
7401   /* Try the declaration first.  */
7402   cp_parser_parse_tentatively (parser);
7403   /* New types are not allowed in the type-specifier-seq for a
7404      condition.  */
7405   saved_message = parser->type_definition_forbidden_message;
7406   parser->type_definition_forbidden_message
7407     = "types may not be defined in conditions";
7408   /* Parse the type-specifier-seq.  */
7409   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7410                                 &type_specifiers);
7411   /* Restore the saved message.  */
7412   parser->type_definition_forbidden_message = saved_message;
7413   /* If all is well, we might be looking at a declaration.  */
7414   if (!cp_parser_error_occurred (parser))
7415     {
7416       tree decl;
7417       tree asm_specification;
7418       tree attributes;
7419       cp_declarator *declarator;
7420       tree initializer = NULL_TREE;
7421
7422       /* Parse the declarator.  */
7423       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7424                                          /*ctor_dtor_or_conv_p=*/NULL,
7425                                          /*parenthesized_p=*/NULL,
7426                                          /*member_p=*/false);
7427       /* Parse the attributes.  */
7428       attributes = cp_parser_attributes_opt (parser);
7429       /* Parse the asm-specification.  */
7430       asm_specification = cp_parser_asm_specification_opt (parser);
7431       /* If the next token is not an `=' or '{', then we might still be
7432          looking at an expression.  For example:
7433
7434            if (A(a).x)
7435
7436          looks like a decl-specifier-seq and a declarator -- but then
7437          there is no `=', so this is an expression.  */
7438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7439           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7440         cp_parser_simulate_error (parser);
7441         
7442       /* If we did see an `=' or '{', then we are looking at a declaration
7443          for sure.  */
7444       if (cp_parser_parse_definitely (parser))
7445         {
7446           tree pushed_scope;
7447           bool non_constant_p;
7448           bool flags = LOOKUP_ONLYCONVERTING;
7449
7450           /* Create the declaration.  */
7451           decl = start_decl (declarator, &type_specifiers,
7452                              /*initialized_p=*/true,
7453                              attributes, /*prefix_attributes=*/NULL_TREE,
7454                              &pushed_scope);
7455
7456           /* Parse the initializer.  */
7457           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7458             {
7459               initializer = cp_parser_braced_list (parser, &non_constant_p);
7460               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7461               flags = 0;
7462             }
7463           else
7464             {
7465               /* Consume the `='.  */
7466               cp_parser_require (parser, CPP_EQ, "%<=%>");
7467               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7468             }
7469           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7470             maybe_warn_cpp0x ("extended initializer lists");
7471
7472           if (!non_constant_p)
7473             initializer = fold_non_dependent_expr (initializer);
7474
7475           /* Process the initializer.  */
7476           cp_finish_decl (decl,
7477                           initializer, !non_constant_p,
7478                           asm_specification,
7479                           flags);
7480
7481           if (pushed_scope)
7482             pop_scope (pushed_scope);
7483
7484           return convert_from_reference (decl);
7485         }
7486     }
7487   /* If we didn't even get past the declarator successfully, we are
7488      definitely not looking at a declaration.  */
7489   else
7490     cp_parser_abort_tentative_parse (parser);
7491
7492   /* Otherwise, we are looking at an expression.  */
7493   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7494 }
7495
7496 /* Parse an iteration-statement.
7497
7498    iteration-statement:
7499      while ( condition ) statement
7500      do statement while ( expression ) ;
7501      for ( for-init-statement condition [opt] ; expression [opt] )
7502        statement
7503
7504    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7505
7506 static tree
7507 cp_parser_iteration_statement (cp_parser* parser)
7508 {
7509   cp_token *token;
7510   enum rid keyword;
7511   tree statement;
7512   unsigned char in_statement;
7513
7514   /* Peek at the next token.  */
7515   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7516   if (!token)
7517     return error_mark_node;
7518
7519   /* Remember whether or not we are already within an iteration
7520      statement.  */
7521   in_statement = parser->in_statement;
7522
7523   /* See what kind of keyword it is.  */
7524   keyword = token->keyword;
7525   switch (keyword)
7526     {
7527     case RID_WHILE:
7528       {
7529         tree condition;
7530
7531         /* Begin the while-statement.  */
7532         statement = begin_while_stmt ();
7533         /* Look for the `('.  */
7534         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7535         /* Parse the condition.  */
7536         condition = cp_parser_condition (parser);
7537         finish_while_stmt_cond (condition, statement);
7538         /* Look for the `)'.  */
7539         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7540         /* Parse the dependent statement.  */
7541         parser->in_statement = IN_ITERATION_STMT;
7542         cp_parser_already_scoped_statement (parser);
7543         parser->in_statement = in_statement;
7544         /* We're done with the while-statement.  */
7545         finish_while_stmt (statement);
7546       }
7547       break;
7548
7549     case RID_DO:
7550       {
7551         tree expression;
7552
7553         /* Begin the do-statement.  */
7554         statement = begin_do_stmt ();
7555         /* Parse the body of the do-statement.  */
7556         parser->in_statement = IN_ITERATION_STMT;
7557         cp_parser_implicitly_scoped_statement (parser, NULL);
7558         parser->in_statement = in_statement;
7559         finish_do_body (statement);
7560         /* Look for the `while' keyword.  */
7561         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7562         /* Look for the `('.  */
7563         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7564         /* Parse the expression.  */
7565         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7566         /* We're done with the do-statement.  */
7567         finish_do_stmt (expression, statement);
7568         /* Look for the `)'.  */
7569         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7570         /* Look for the `;'.  */
7571         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7572       }
7573       break;
7574
7575     case RID_FOR:
7576       {
7577         tree condition = NULL_TREE;
7578         tree expression = NULL_TREE;
7579
7580         /* Begin the for-statement.  */
7581         statement = begin_for_stmt ();
7582         /* Look for the `('.  */
7583         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7584         /* Parse the initialization.  */
7585         cp_parser_for_init_statement (parser);
7586         finish_for_init_stmt (statement);
7587
7588         /* If there's a condition, process it.  */
7589         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7590           condition = cp_parser_condition (parser);
7591         finish_for_cond (condition, statement);
7592         /* Look for the `;'.  */
7593         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7594
7595         /* If there's an expression, process it.  */
7596         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7597           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7598         finish_for_expr (expression, statement);
7599         /* Look for the `)'.  */
7600         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7601
7602         /* Parse the body of the for-statement.  */
7603         parser->in_statement = IN_ITERATION_STMT;
7604         cp_parser_already_scoped_statement (parser);
7605         parser->in_statement = in_statement;
7606
7607         /* We're done with the for-statement.  */
7608         finish_for_stmt (statement);
7609       }
7610       break;
7611
7612     default:
7613       cp_parser_error (parser, "expected iteration-statement");
7614       statement = error_mark_node;
7615       break;
7616     }
7617
7618   return statement;
7619 }
7620
7621 /* Parse a for-init-statement.
7622
7623    for-init-statement:
7624      expression-statement
7625      simple-declaration  */
7626
7627 static void
7628 cp_parser_for_init_statement (cp_parser* parser)
7629 {
7630   /* If the next token is a `;', then we have an empty
7631      expression-statement.  Grammatically, this is also a
7632      simple-declaration, but an invalid one, because it does not
7633      declare anything.  Therefore, if we did not handle this case
7634      specially, we would issue an error message about an invalid
7635      declaration.  */
7636   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7637     {
7638       /* We're going to speculatively look for a declaration, falling back
7639          to an expression, if necessary.  */
7640       cp_parser_parse_tentatively (parser);
7641       /* Parse the declaration.  */
7642       cp_parser_simple_declaration (parser,
7643                                     /*function_definition_allowed_p=*/false);
7644       /* If the tentative parse failed, then we shall need to look for an
7645          expression-statement.  */
7646       if (cp_parser_parse_definitely (parser))
7647         return;
7648     }
7649
7650   cp_parser_expression_statement (parser, false);
7651 }
7652
7653 /* Parse a jump-statement.
7654
7655    jump-statement:
7656      break ;
7657      continue ;
7658      return expression [opt] ;
7659      return braced-init-list ;
7660      goto identifier ;
7661
7662    GNU extension:
7663
7664    jump-statement:
7665      goto * expression ;
7666
7667    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7668
7669 static tree
7670 cp_parser_jump_statement (cp_parser* parser)
7671 {
7672   tree statement = error_mark_node;
7673   cp_token *token;
7674   enum rid keyword;
7675   unsigned char in_statement;
7676
7677   /* Peek at the next token.  */
7678   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7679   if (!token)
7680     return error_mark_node;
7681
7682   /* See what kind of keyword it is.  */
7683   keyword = token->keyword;
7684   switch (keyword)
7685     {
7686     case RID_BREAK:
7687       in_statement = parser->in_statement & ~IN_IF_STMT;      
7688       switch (in_statement)
7689         {
7690         case 0:
7691           error ("%Hbreak statement not within loop or switch", &token->location);
7692           break;
7693         default:
7694           gcc_assert ((in_statement & IN_SWITCH_STMT)
7695                       || in_statement == IN_ITERATION_STMT);
7696           statement = finish_break_stmt ();
7697           break;
7698         case IN_OMP_BLOCK:
7699           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7700           break;
7701         case IN_OMP_FOR:
7702           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7703           break;
7704         }
7705       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7706       break;
7707
7708     case RID_CONTINUE:
7709       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7710         {
7711         case 0:
7712           error ("%Hcontinue statement not within a loop", &token->location);
7713           break;
7714         case IN_ITERATION_STMT:
7715         case IN_OMP_FOR:
7716           statement = finish_continue_stmt ();
7717           break;
7718         case IN_OMP_BLOCK:
7719           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7720           break;
7721         default:
7722           gcc_unreachable ();
7723         }
7724       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7725       break;
7726
7727     case RID_RETURN:
7728       {
7729         tree expr;
7730         bool expr_non_constant_p;
7731
7732         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7733           {
7734             maybe_warn_cpp0x ("extended initializer lists");
7735             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7736           }
7737         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7738           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7739         else
7740           /* If the next token is a `;', then there is no
7741              expression.  */
7742           expr = NULL_TREE;
7743         /* Build the return-statement.  */
7744         statement = finish_return_stmt (expr);
7745         /* Look for the final `;'.  */
7746         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7747       }
7748       break;
7749
7750     case RID_GOTO:
7751       /* Create the goto-statement.  */
7752       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7753         {
7754           /* Issue a warning about this use of a GNU extension.  */
7755           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7756           /* Consume the '*' token.  */
7757           cp_lexer_consume_token (parser->lexer);
7758           /* Parse the dependent expression.  */
7759           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7760         }
7761       else
7762         finish_goto_stmt (cp_parser_identifier (parser));
7763       /* Look for the final `;'.  */
7764       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7765       break;
7766
7767     default:
7768       cp_parser_error (parser, "expected jump-statement");
7769       break;
7770     }
7771
7772   return statement;
7773 }
7774
7775 /* Parse a declaration-statement.
7776
7777    declaration-statement:
7778      block-declaration  */
7779
7780 static void
7781 cp_parser_declaration_statement (cp_parser* parser)
7782 {
7783   void *p;
7784
7785   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7786   p = obstack_alloc (&declarator_obstack, 0);
7787
7788  /* Parse the block-declaration.  */
7789   cp_parser_block_declaration (parser, /*statement_p=*/true);
7790
7791   /* Free any declarators allocated.  */
7792   obstack_free (&declarator_obstack, p);
7793
7794   /* Finish off the statement.  */
7795   finish_stmt ();
7796 }
7797
7798 /* Some dependent statements (like `if (cond) statement'), are
7799    implicitly in their own scope.  In other words, if the statement is
7800    a single statement (as opposed to a compound-statement), it is
7801    none-the-less treated as if it were enclosed in braces.  Any
7802    declarations appearing in the dependent statement are out of scope
7803    after control passes that point.  This function parses a statement,
7804    but ensures that is in its own scope, even if it is not a
7805    compound-statement.
7806
7807    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7808    is a (possibly labeled) if statement which is not enclosed in
7809    braces and has an else clause.  This is used to implement
7810    -Wparentheses.
7811
7812    Returns the new statement.  */
7813
7814 static tree
7815 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7816 {
7817   tree statement;
7818
7819   if (if_p != NULL)
7820     *if_p = false;
7821
7822   /* Mark if () ; with a special NOP_EXPR.  */
7823   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7824     {
7825       cp_lexer_consume_token (parser->lexer);
7826       statement = add_stmt (build_empty_stmt ());
7827     }
7828   /* if a compound is opened, we simply parse the statement directly.  */
7829   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7830     statement = cp_parser_compound_statement (parser, NULL, false);
7831   /* If the token is not a `{', then we must take special action.  */
7832   else
7833     {
7834       /* Create a compound-statement.  */
7835       statement = begin_compound_stmt (0);
7836       /* Parse the dependent-statement.  */
7837       cp_parser_statement (parser, NULL_TREE, false, if_p);
7838       /* Finish the dummy compound-statement.  */
7839       finish_compound_stmt (statement);
7840     }
7841
7842   /* Return the statement.  */
7843   return statement;
7844 }
7845
7846 /* For some dependent statements (like `while (cond) statement'), we
7847    have already created a scope.  Therefore, even if the dependent
7848    statement is a compound-statement, we do not want to create another
7849    scope.  */
7850
7851 static void
7852 cp_parser_already_scoped_statement (cp_parser* parser)
7853 {
7854   /* If the token is a `{', then we must take special action.  */
7855   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7856     cp_parser_statement (parser, NULL_TREE, false, NULL);
7857   else
7858     {
7859       /* Avoid calling cp_parser_compound_statement, so that we
7860          don't create a new scope.  Do everything else by hand.  */
7861       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7862       /* If the next keyword is `__label__' we have a label declaration.  */
7863       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7864         cp_parser_label_declaration (parser);
7865       /* Parse an (optional) statement-seq.  */
7866       cp_parser_statement_seq_opt (parser, NULL_TREE);
7867       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7868     }
7869 }
7870
7871 /* Declarations [gram.dcl.dcl] */
7872
7873 /* Parse an optional declaration-sequence.
7874
7875    declaration-seq:
7876      declaration
7877      declaration-seq declaration  */
7878
7879 static void
7880 cp_parser_declaration_seq_opt (cp_parser* parser)
7881 {
7882   while (true)
7883     {
7884       cp_token *token;
7885
7886       token = cp_lexer_peek_token (parser->lexer);
7887
7888       if (token->type == CPP_CLOSE_BRACE
7889           || token->type == CPP_EOF
7890           || token->type == CPP_PRAGMA_EOL)
7891         break;
7892
7893       if (token->type == CPP_SEMICOLON)
7894         {
7895           /* A declaration consisting of a single semicolon is
7896              invalid.  Allow it unless we're being pedantic.  */
7897           cp_lexer_consume_token (parser->lexer);
7898           if (!in_system_header)
7899             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7900           continue;
7901         }
7902
7903       /* If we're entering or exiting a region that's implicitly
7904          extern "C", modify the lang context appropriately.  */
7905       if (!parser->implicit_extern_c && token->implicit_extern_c)
7906         {
7907           push_lang_context (lang_name_c);
7908           parser->implicit_extern_c = true;
7909         }
7910       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7911         {
7912           pop_lang_context ();
7913           parser->implicit_extern_c = false;
7914         }
7915
7916       if (token->type == CPP_PRAGMA)
7917         {
7918           /* A top-level declaration can consist solely of a #pragma.
7919              A nested declaration cannot, so this is done here and not
7920              in cp_parser_declaration.  (A #pragma at block scope is
7921              handled in cp_parser_statement.)  */
7922           cp_parser_pragma (parser, pragma_external);
7923           continue;
7924         }
7925
7926       /* Parse the declaration itself.  */
7927       cp_parser_declaration (parser);
7928     }
7929 }
7930
7931 /* Parse a declaration.
7932
7933    declaration:
7934      block-declaration
7935      function-definition
7936      template-declaration
7937      explicit-instantiation
7938      explicit-specialization
7939      linkage-specification
7940      namespace-definition
7941
7942    GNU extension:
7943
7944    declaration:
7945       __extension__ declaration */
7946
7947 static void
7948 cp_parser_declaration (cp_parser* parser)
7949 {
7950   cp_token token1;
7951   cp_token token2;
7952   int saved_pedantic;
7953   void *p;
7954
7955   /* Check for the `__extension__' keyword.  */
7956   if (cp_parser_extension_opt (parser, &saved_pedantic))
7957     {
7958       /* Parse the qualified declaration.  */
7959       cp_parser_declaration (parser);
7960       /* Restore the PEDANTIC flag.  */
7961       pedantic = saved_pedantic;
7962
7963       return;
7964     }
7965
7966   /* Try to figure out what kind of declaration is present.  */
7967   token1 = *cp_lexer_peek_token (parser->lexer);
7968
7969   if (token1.type != CPP_EOF)
7970     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7971   else
7972     {
7973       token2.type = CPP_EOF;
7974       token2.keyword = RID_MAX;
7975     }
7976
7977   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7978   p = obstack_alloc (&declarator_obstack, 0);
7979
7980   /* If the next token is `extern' and the following token is a string
7981      literal, then we have a linkage specification.  */
7982   if (token1.keyword == RID_EXTERN
7983       && cp_parser_is_string_literal (&token2))
7984     cp_parser_linkage_specification (parser);
7985   /* If the next token is `template', then we have either a template
7986      declaration, an explicit instantiation, or an explicit
7987      specialization.  */
7988   else if (token1.keyword == RID_TEMPLATE)
7989     {
7990       /* `template <>' indicates a template specialization.  */
7991       if (token2.type == CPP_LESS
7992           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7993         cp_parser_explicit_specialization (parser);
7994       /* `template <' indicates a template declaration.  */
7995       else if (token2.type == CPP_LESS)
7996         cp_parser_template_declaration (parser, /*member_p=*/false);
7997       /* Anything else must be an explicit instantiation.  */
7998       else
7999         cp_parser_explicit_instantiation (parser);
8000     }
8001   /* If the next token is `export', then we have a template
8002      declaration.  */
8003   else if (token1.keyword == RID_EXPORT)
8004     cp_parser_template_declaration (parser, /*member_p=*/false);
8005   /* If the next token is `extern', 'static' or 'inline' and the one
8006      after that is `template', we have a GNU extended explicit
8007      instantiation directive.  */
8008   else if (cp_parser_allow_gnu_extensions_p (parser)
8009            && (token1.keyword == RID_EXTERN
8010                || token1.keyword == RID_STATIC
8011                || token1.keyword == RID_INLINE)
8012            && token2.keyword == RID_TEMPLATE)
8013     cp_parser_explicit_instantiation (parser);
8014   /* If the next token is `namespace', check for a named or unnamed
8015      namespace definition.  */
8016   else if (token1.keyword == RID_NAMESPACE
8017            && (/* A named namespace definition.  */
8018                (token2.type == CPP_NAME
8019                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8020                     != CPP_EQ))
8021                /* An unnamed namespace definition.  */
8022                || token2.type == CPP_OPEN_BRACE
8023                || token2.keyword == RID_ATTRIBUTE))
8024     cp_parser_namespace_definition (parser);
8025   /* An inline (associated) namespace definition.  */
8026   else if (token1.keyword == RID_INLINE
8027            && token2.keyword == RID_NAMESPACE)
8028     cp_parser_namespace_definition (parser);
8029   /* Objective-C++ declaration/definition.  */
8030   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8031     cp_parser_objc_declaration (parser);
8032   /* We must have either a block declaration or a function
8033      definition.  */
8034   else
8035     /* Try to parse a block-declaration, or a function-definition.  */
8036     cp_parser_block_declaration (parser, /*statement_p=*/false);
8037
8038   /* Free any declarators allocated.  */
8039   obstack_free (&declarator_obstack, p);
8040 }
8041
8042 /* Parse a block-declaration.
8043
8044    block-declaration:
8045      simple-declaration
8046      asm-definition
8047      namespace-alias-definition
8048      using-declaration
8049      using-directive
8050
8051    GNU Extension:
8052
8053    block-declaration:
8054      __extension__ block-declaration
8055
8056    C++0x Extension:
8057
8058    block-declaration:
8059      static_assert-declaration
8060
8061    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8062    part of a declaration-statement.  */
8063
8064 static void
8065 cp_parser_block_declaration (cp_parser *parser,
8066                              bool      statement_p)
8067 {
8068   cp_token *token1;
8069   int saved_pedantic;
8070
8071   /* Check for the `__extension__' keyword.  */
8072   if (cp_parser_extension_opt (parser, &saved_pedantic))
8073     {
8074       /* Parse the qualified declaration.  */
8075       cp_parser_block_declaration (parser, statement_p);
8076       /* Restore the PEDANTIC flag.  */
8077       pedantic = saved_pedantic;
8078
8079       return;
8080     }
8081
8082   /* Peek at the next token to figure out which kind of declaration is
8083      present.  */
8084   token1 = cp_lexer_peek_token (parser->lexer);
8085
8086   /* If the next keyword is `asm', we have an asm-definition.  */
8087   if (token1->keyword == RID_ASM)
8088     {
8089       if (statement_p)
8090         cp_parser_commit_to_tentative_parse (parser);
8091       cp_parser_asm_definition (parser);
8092     }
8093   /* If the next keyword is `namespace', we have a
8094      namespace-alias-definition.  */
8095   else if (token1->keyword == RID_NAMESPACE)
8096     cp_parser_namespace_alias_definition (parser);
8097   /* If the next keyword is `using', we have either a
8098      using-declaration or a using-directive.  */
8099   else if (token1->keyword == RID_USING)
8100     {
8101       cp_token *token2;
8102
8103       if (statement_p)
8104         cp_parser_commit_to_tentative_parse (parser);
8105       /* If the token after `using' is `namespace', then we have a
8106          using-directive.  */
8107       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8108       if (token2->keyword == RID_NAMESPACE)
8109         cp_parser_using_directive (parser);
8110       /* Otherwise, it's a using-declaration.  */
8111       else
8112         cp_parser_using_declaration (parser,
8113                                      /*access_declaration_p=*/false);
8114     }
8115   /* If the next keyword is `__label__' we have a misplaced label
8116      declaration.  */
8117   else if (token1->keyword == RID_LABEL)
8118     {
8119       cp_lexer_consume_token (parser->lexer);
8120       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8121       cp_parser_skip_to_end_of_statement (parser);
8122       /* If the next token is now a `;', consume it.  */
8123       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8124         cp_lexer_consume_token (parser->lexer);
8125     }
8126   /* If the next token is `static_assert' we have a static assertion.  */
8127   else if (token1->keyword == RID_STATIC_ASSERT)
8128     cp_parser_static_assert (parser, /*member_p=*/false);
8129   /* Anything else must be a simple-declaration.  */
8130   else
8131     cp_parser_simple_declaration (parser, !statement_p);
8132 }
8133
8134 /* Parse a simple-declaration.
8135
8136    simple-declaration:
8137      decl-specifier-seq [opt] init-declarator-list [opt] ;
8138
8139    init-declarator-list:
8140      init-declarator
8141      init-declarator-list , init-declarator
8142
8143    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8144    function-definition as a simple-declaration.  */
8145
8146 static void
8147 cp_parser_simple_declaration (cp_parser* parser,
8148                               bool function_definition_allowed_p)
8149 {
8150   cp_decl_specifier_seq decl_specifiers;
8151   int declares_class_or_enum;
8152   bool saw_declarator;
8153
8154   /* Defer access checks until we know what is being declared; the
8155      checks for names appearing in the decl-specifier-seq should be
8156      done as if we were in the scope of the thing being declared.  */
8157   push_deferring_access_checks (dk_deferred);
8158
8159   /* Parse the decl-specifier-seq.  We have to keep track of whether
8160      or not the decl-specifier-seq declares a named class or
8161      enumeration type, since that is the only case in which the
8162      init-declarator-list is allowed to be empty.
8163
8164      [dcl.dcl]
8165
8166      In a simple-declaration, the optional init-declarator-list can be
8167      omitted only when declaring a class or enumeration, that is when
8168      the decl-specifier-seq contains either a class-specifier, an
8169      elaborated-type-specifier, or an enum-specifier.  */
8170   cp_parser_decl_specifier_seq (parser,
8171                                 CP_PARSER_FLAGS_OPTIONAL,
8172                                 &decl_specifiers,
8173                                 &declares_class_or_enum);
8174   /* We no longer need to defer access checks.  */
8175   stop_deferring_access_checks ();
8176
8177   /* In a block scope, a valid declaration must always have a
8178      decl-specifier-seq.  By not trying to parse declarators, we can
8179      resolve the declaration/expression ambiguity more quickly.  */
8180   if (!function_definition_allowed_p
8181       && !decl_specifiers.any_specifiers_p)
8182     {
8183       cp_parser_error (parser, "expected declaration");
8184       goto done;
8185     }
8186
8187   /* If the next two tokens are both identifiers, the code is
8188      erroneous. The usual cause of this situation is code like:
8189
8190        T t;
8191
8192      where "T" should name a type -- but does not.  */
8193   if (!decl_specifiers.type
8194       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8195     {
8196       /* If parsing tentatively, we should commit; we really are
8197          looking at a declaration.  */
8198       cp_parser_commit_to_tentative_parse (parser);
8199       /* Give up.  */
8200       goto done;
8201     }
8202
8203   /* If we have seen at least one decl-specifier, and the next token
8204      is not a parenthesis, then we must be looking at a declaration.
8205      (After "int (" we might be looking at a functional cast.)  */
8206   if (decl_specifiers.any_specifiers_p
8207       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8208       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8209       && !cp_parser_error_occurred (parser))
8210     cp_parser_commit_to_tentative_parse (parser);
8211
8212   /* Keep going until we hit the `;' at the end of the simple
8213      declaration.  */
8214   saw_declarator = false;
8215   while (cp_lexer_next_token_is_not (parser->lexer,
8216                                      CPP_SEMICOLON))
8217     {
8218       cp_token *token;
8219       bool function_definition_p;
8220       tree decl;
8221
8222       if (saw_declarator)
8223         {
8224           /* If we are processing next declarator, coma is expected */
8225           token = cp_lexer_peek_token (parser->lexer);
8226           gcc_assert (token->type == CPP_COMMA);
8227           cp_lexer_consume_token (parser->lexer);
8228         }
8229       else
8230         saw_declarator = true;
8231
8232       /* Parse the init-declarator.  */
8233       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8234                                         /*checks=*/NULL,
8235                                         function_definition_allowed_p,
8236                                         /*member_p=*/false,
8237                                         declares_class_or_enum,
8238                                         &function_definition_p);
8239       /* If an error occurred while parsing tentatively, exit quickly.
8240          (That usually happens when in the body of a function; each
8241          statement is treated as a declaration-statement until proven
8242          otherwise.)  */
8243       if (cp_parser_error_occurred (parser))
8244         goto done;
8245       /* Handle function definitions specially.  */
8246       if (function_definition_p)
8247         {
8248           /* If the next token is a `,', then we are probably
8249              processing something like:
8250
8251                void f() {}, *p;
8252
8253              which is erroneous.  */
8254           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8255             {
8256               cp_token *token = cp_lexer_peek_token (parser->lexer);
8257               error ("%Hmixing declarations and function-definitions is forbidden",
8258                      &token->location);
8259             }
8260           /* Otherwise, we're done with the list of declarators.  */
8261           else
8262             {
8263               pop_deferring_access_checks ();
8264               return;
8265             }
8266         }
8267       /* The next token should be either a `,' or a `;'.  */
8268       token = cp_lexer_peek_token (parser->lexer);
8269       /* If it's a `,', there are more declarators to come.  */
8270       if (token->type == CPP_COMMA)
8271         /* will be consumed next time around */;
8272       /* If it's a `;', we are done.  */
8273       else if (token->type == CPP_SEMICOLON)
8274         break;
8275       /* Anything else is an error.  */
8276       else
8277         {
8278           /* If we have already issued an error message we don't need
8279              to issue another one.  */
8280           if (decl != error_mark_node
8281               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8282             cp_parser_error (parser, "expected %<,%> or %<;%>");
8283           /* Skip tokens until we reach the end of the statement.  */
8284           cp_parser_skip_to_end_of_statement (parser);
8285           /* If the next token is now a `;', consume it.  */
8286           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8287             cp_lexer_consume_token (parser->lexer);
8288           goto done;
8289         }
8290       /* After the first time around, a function-definition is not
8291          allowed -- even if it was OK at first.  For example:
8292
8293            int i, f() {}
8294
8295          is not valid.  */
8296       function_definition_allowed_p = false;
8297     }
8298
8299   /* Issue an error message if no declarators are present, and the
8300      decl-specifier-seq does not itself declare a class or
8301      enumeration.  */
8302   if (!saw_declarator)
8303     {
8304       if (cp_parser_declares_only_class_p (parser))
8305         shadow_tag (&decl_specifiers);
8306       /* Perform any deferred access checks.  */
8307       perform_deferred_access_checks ();
8308     }
8309
8310   /* Consume the `;'.  */
8311   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8312
8313  done:
8314   pop_deferring_access_checks ();
8315 }
8316
8317 /* Parse a decl-specifier-seq.
8318
8319    decl-specifier-seq:
8320      decl-specifier-seq [opt] decl-specifier
8321
8322    decl-specifier:
8323      storage-class-specifier
8324      type-specifier
8325      function-specifier
8326      friend
8327      typedef
8328
8329    GNU Extension:
8330
8331    decl-specifier:
8332      attributes
8333
8334    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8335
8336    The parser flags FLAGS is used to control type-specifier parsing.
8337
8338    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8339    flags:
8340
8341      1: one of the decl-specifiers is an elaborated-type-specifier
8342         (i.e., a type declaration)
8343      2: one of the decl-specifiers is an enum-specifier or a
8344         class-specifier (i.e., a type definition)
8345
8346    */
8347
8348 static void
8349 cp_parser_decl_specifier_seq (cp_parser* parser,
8350                               cp_parser_flags flags,
8351                               cp_decl_specifier_seq *decl_specs,
8352                               int* declares_class_or_enum)
8353 {
8354   bool constructor_possible_p = !parser->in_declarator_p;
8355   cp_token *start_token = NULL;
8356
8357   /* Clear DECL_SPECS.  */
8358   clear_decl_specs (decl_specs);
8359
8360   /* Assume no class or enumeration type is declared.  */
8361   *declares_class_or_enum = 0;
8362
8363   /* Keep reading specifiers until there are no more to read.  */
8364   while (true)
8365     {
8366       bool constructor_p;
8367       bool found_decl_spec;
8368       cp_token *token;
8369
8370       /* Peek at the next token.  */
8371       token = cp_lexer_peek_token (parser->lexer);
8372
8373       /* Save the first token of the decl spec list for error
8374          reporting.  */
8375       if (!start_token)
8376         start_token = token;
8377       /* Handle attributes.  */
8378       if (token->keyword == RID_ATTRIBUTE)
8379         {
8380           /* Parse the attributes.  */
8381           decl_specs->attributes
8382             = chainon (decl_specs->attributes,
8383                        cp_parser_attributes_opt (parser));
8384           continue;
8385         }
8386       /* Assume we will find a decl-specifier keyword.  */
8387       found_decl_spec = true;
8388       /* If the next token is an appropriate keyword, we can simply
8389          add it to the list.  */
8390       switch (token->keyword)
8391         {
8392           /* decl-specifier:
8393                friend  */
8394         case RID_FRIEND:
8395           if (!at_class_scope_p ())
8396             {
8397               error ("%H%<friend%> used outside of class", &token->location);
8398               cp_lexer_purge_token (parser->lexer);
8399             }
8400           else
8401             {
8402               ++decl_specs->specs[(int) ds_friend];
8403               /* Consume the token.  */
8404               cp_lexer_consume_token (parser->lexer);
8405             }
8406           break;
8407
8408           /* function-specifier:
8409                inline
8410                virtual
8411                explicit  */
8412         case RID_INLINE:
8413         case RID_VIRTUAL:
8414         case RID_EXPLICIT:
8415           cp_parser_function_specifier_opt (parser, decl_specs);
8416           break;
8417
8418           /* decl-specifier:
8419                typedef  */
8420         case RID_TYPEDEF:
8421           ++decl_specs->specs[(int) ds_typedef];
8422           /* Consume the token.  */
8423           cp_lexer_consume_token (parser->lexer);
8424           /* A constructor declarator cannot appear in a typedef.  */
8425           constructor_possible_p = false;
8426           /* The "typedef" keyword can only occur in a declaration; we
8427              may as well commit at this point.  */
8428           cp_parser_commit_to_tentative_parse (parser);
8429
8430           if (decl_specs->storage_class != sc_none)
8431             decl_specs->conflicting_specifiers_p = true;
8432           break;
8433
8434           /* storage-class-specifier:
8435                auto
8436                register
8437                static
8438                extern
8439                mutable
8440
8441              GNU Extension:
8442                thread  */
8443         case RID_AUTO:
8444           if (cxx_dialect == cxx98) 
8445             {
8446               /* Consume the token.  */
8447               cp_lexer_consume_token (parser->lexer);
8448
8449               /* Complain about `auto' as a storage specifier, if
8450                  we're complaining about C++0x compatibility.  */
8451               warning 
8452                 (OPT_Wc__0x_compat, 
8453                  "%H%<auto%> will change meaning in C++0x; please remove it",
8454                  &token->location);
8455
8456               /* Set the storage class anyway.  */
8457               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8458                                            token->location);
8459             }
8460           else
8461             /* C++0x auto type-specifier.  */
8462             found_decl_spec = false;
8463           break;
8464
8465         case RID_REGISTER:
8466         case RID_STATIC:
8467         case RID_EXTERN:
8468         case RID_MUTABLE:
8469           /* Consume the token.  */
8470           cp_lexer_consume_token (parser->lexer);
8471           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8472                                        token->location);
8473           break;
8474         case RID_THREAD:
8475           /* Consume the token.  */
8476           cp_lexer_consume_token (parser->lexer);
8477           ++decl_specs->specs[(int) ds_thread];
8478           break;
8479
8480         default:
8481           /* We did not yet find a decl-specifier yet.  */
8482           found_decl_spec = false;
8483           break;
8484         }
8485
8486       /* Constructors are a special case.  The `S' in `S()' is not a
8487          decl-specifier; it is the beginning of the declarator.  */
8488       constructor_p
8489         = (!found_decl_spec
8490            && constructor_possible_p
8491            && (cp_parser_constructor_declarator_p
8492                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8493
8494       /* If we don't have a DECL_SPEC yet, then we must be looking at
8495          a type-specifier.  */
8496       if (!found_decl_spec && !constructor_p)
8497         {
8498           int decl_spec_declares_class_or_enum;
8499           bool is_cv_qualifier;
8500           tree type_spec;
8501
8502           type_spec
8503             = cp_parser_type_specifier (parser, flags,
8504                                         decl_specs,
8505                                         /*is_declaration=*/true,
8506                                         &decl_spec_declares_class_or_enum,
8507                                         &is_cv_qualifier);
8508           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8509
8510           /* If this type-specifier referenced a user-defined type
8511              (a typedef, class-name, etc.), then we can't allow any
8512              more such type-specifiers henceforth.
8513
8514              [dcl.spec]
8515
8516              The longest sequence of decl-specifiers that could
8517              possibly be a type name is taken as the
8518              decl-specifier-seq of a declaration.  The sequence shall
8519              be self-consistent as described below.
8520
8521              [dcl.type]
8522
8523              As a general rule, at most one type-specifier is allowed
8524              in the complete decl-specifier-seq of a declaration.  The
8525              only exceptions are the following:
8526
8527              -- const or volatile can be combined with any other
8528                 type-specifier.
8529
8530              -- signed or unsigned can be combined with char, long,
8531                 short, or int.
8532
8533              -- ..
8534
8535              Example:
8536
8537                typedef char* Pc;
8538                void g (const int Pc);
8539
8540              Here, Pc is *not* part of the decl-specifier seq; it's
8541              the declarator.  Therefore, once we see a type-specifier
8542              (other than a cv-qualifier), we forbid any additional
8543              user-defined types.  We *do* still allow things like `int
8544              int' to be considered a decl-specifier-seq, and issue the
8545              error message later.  */
8546           if (type_spec && !is_cv_qualifier)
8547             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8548           /* A constructor declarator cannot follow a type-specifier.  */
8549           if (type_spec)
8550             {
8551               constructor_possible_p = false;
8552               found_decl_spec = true;
8553             }
8554         }
8555
8556       /* If we still do not have a DECL_SPEC, then there are no more
8557          decl-specifiers.  */
8558       if (!found_decl_spec)
8559         break;
8560
8561       decl_specs->any_specifiers_p = true;
8562       /* After we see one decl-specifier, further decl-specifiers are
8563          always optional.  */
8564       flags |= CP_PARSER_FLAGS_OPTIONAL;
8565     }
8566
8567   cp_parser_check_decl_spec (decl_specs, start_token->location);
8568
8569   /* Don't allow a friend specifier with a class definition.  */
8570   if (decl_specs->specs[(int) ds_friend] != 0
8571       && (*declares_class_or_enum & 2))
8572     error ("%Hclass definition may not be declared a friend",
8573             &start_token->location);
8574 }
8575
8576 /* Parse an (optional) storage-class-specifier.
8577
8578    storage-class-specifier:
8579      auto
8580      register
8581      static
8582      extern
8583      mutable
8584
8585    GNU Extension:
8586
8587    storage-class-specifier:
8588      thread
8589
8590    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8591
8592 static tree
8593 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8594 {
8595   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8596     {
8597     case RID_AUTO:
8598       if (cxx_dialect != cxx98)
8599         return NULL_TREE;
8600       /* Fall through for C++98.  */
8601
8602     case RID_REGISTER:
8603     case RID_STATIC:
8604     case RID_EXTERN:
8605     case RID_MUTABLE:
8606     case RID_THREAD:
8607       /* Consume the token.  */
8608       return cp_lexer_consume_token (parser->lexer)->u.value;
8609
8610     default:
8611       return NULL_TREE;
8612     }
8613 }
8614
8615 /* Parse an (optional) function-specifier.
8616
8617    function-specifier:
8618      inline
8619      virtual
8620      explicit
8621
8622    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8623    Updates DECL_SPECS, if it is non-NULL.  */
8624
8625 static tree
8626 cp_parser_function_specifier_opt (cp_parser* parser,
8627                                   cp_decl_specifier_seq *decl_specs)
8628 {
8629   cp_token *token = cp_lexer_peek_token (parser->lexer);
8630   switch (token->keyword)
8631     {
8632     case RID_INLINE:
8633       if (decl_specs)
8634         ++decl_specs->specs[(int) ds_inline];
8635       break;
8636
8637     case RID_VIRTUAL:
8638       /* 14.5.2.3 [temp.mem]
8639
8640          A member function template shall not be virtual.  */
8641       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8642         error ("%Htemplates may not be %<virtual%>", &token->location);
8643       else if (decl_specs)
8644         ++decl_specs->specs[(int) ds_virtual];
8645       break;
8646
8647     case RID_EXPLICIT:
8648       if (decl_specs)
8649         ++decl_specs->specs[(int) ds_explicit];
8650       break;
8651
8652     default:
8653       return NULL_TREE;
8654     }
8655
8656   /* Consume the token.  */
8657   return cp_lexer_consume_token (parser->lexer)->u.value;
8658 }
8659
8660 /* Parse a linkage-specification.
8661
8662    linkage-specification:
8663      extern string-literal { declaration-seq [opt] }
8664      extern string-literal declaration  */
8665
8666 static void
8667 cp_parser_linkage_specification (cp_parser* parser)
8668 {
8669   tree linkage;
8670
8671   /* Look for the `extern' keyword.  */
8672   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8673
8674   /* Look for the string-literal.  */
8675   linkage = cp_parser_string_literal (parser, false, false);
8676
8677   /* Transform the literal into an identifier.  If the literal is a
8678      wide-character string, or contains embedded NULs, then we can't
8679      handle it as the user wants.  */
8680   if (strlen (TREE_STRING_POINTER (linkage))
8681       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8682     {
8683       cp_parser_error (parser, "invalid linkage-specification");
8684       /* Assume C++ linkage.  */
8685       linkage = lang_name_cplusplus;
8686     }
8687   else
8688     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8689
8690   /* We're now using the new linkage.  */
8691   push_lang_context (linkage);
8692
8693   /* If the next token is a `{', then we're using the first
8694      production.  */
8695   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8696     {
8697       /* Consume the `{' token.  */
8698       cp_lexer_consume_token (parser->lexer);
8699       /* Parse the declarations.  */
8700       cp_parser_declaration_seq_opt (parser);
8701       /* Look for the closing `}'.  */
8702       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8703     }
8704   /* Otherwise, there's just one declaration.  */
8705   else
8706     {
8707       bool saved_in_unbraced_linkage_specification_p;
8708
8709       saved_in_unbraced_linkage_specification_p
8710         = parser->in_unbraced_linkage_specification_p;
8711       parser->in_unbraced_linkage_specification_p = true;
8712       cp_parser_declaration (parser);
8713       parser->in_unbraced_linkage_specification_p
8714         = saved_in_unbraced_linkage_specification_p;
8715     }
8716
8717   /* We're done with the linkage-specification.  */
8718   pop_lang_context ();
8719 }
8720
8721 /* Parse a static_assert-declaration.
8722
8723    static_assert-declaration:
8724      static_assert ( constant-expression , string-literal ) ; 
8725
8726    If MEMBER_P, this static_assert is a class member.  */
8727
8728 static void 
8729 cp_parser_static_assert(cp_parser *parser, bool member_p)
8730 {
8731   tree condition;
8732   tree message;
8733   cp_token *token;
8734   location_t saved_loc;
8735
8736   /* Peek at the `static_assert' token so we can keep track of exactly
8737      where the static assertion started.  */
8738   token = cp_lexer_peek_token (parser->lexer);
8739   saved_loc = token->location;
8740
8741   /* Look for the `static_assert' keyword.  */
8742   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8743                                   "%<static_assert%>"))
8744     return;
8745
8746   /*  We know we are in a static assertion; commit to any tentative
8747       parse.  */
8748   if (cp_parser_parsing_tentatively (parser))
8749     cp_parser_commit_to_tentative_parse (parser);
8750
8751   /* Parse the `(' starting the static assertion condition.  */
8752   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8753
8754   /* Parse the constant-expression.  */
8755   condition = 
8756     cp_parser_constant_expression (parser,
8757                                    /*allow_non_constant_p=*/false,
8758                                    /*non_constant_p=*/NULL);
8759
8760   /* Parse the separating `,'.  */
8761   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8762
8763   /* Parse the string-literal message.  */
8764   message = cp_parser_string_literal (parser, 
8765                                       /*translate=*/false,
8766                                       /*wide_ok=*/true);
8767
8768   /* A `)' completes the static assertion.  */
8769   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8770     cp_parser_skip_to_closing_parenthesis (parser, 
8771                                            /*recovering=*/true, 
8772                                            /*or_comma=*/false,
8773                                            /*consume_paren=*/true);
8774
8775   /* A semicolon terminates the declaration.  */
8776   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8777
8778   /* Complete the static assertion, which may mean either processing 
8779      the static assert now or saving it for template instantiation.  */
8780   finish_static_assert (condition, message, saved_loc, member_p);
8781 }
8782
8783 /* Parse a `decltype' type. Returns the type. 
8784
8785    simple-type-specifier:
8786      decltype ( expression )  */
8787
8788 static tree
8789 cp_parser_decltype (cp_parser *parser)
8790 {
8791   tree expr;
8792   bool id_expression_or_member_access_p = false;
8793   const char *saved_message;
8794   bool saved_integral_constant_expression_p;
8795   bool saved_non_integral_constant_expression_p;
8796   cp_token *id_expr_start_token;
8797
8798   /* Look for the `decltype' token.  */
8799   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8800     return error_mark_node;
8801
8802   /* Types cannot be defined in a `decltype' expression.  Save away the
8803      old message.  */
8804   saved_message = parser->type_definition_forbidden_message;
8805
8806   /* And create the new one.  */
8807   parser->type_definition_forbidden_message
8808     = "types may not be defined in %<decltype%> expressions";
8809
8810   /* The restrictions on constant-expressions do not apply inside
8811      decltype expressions.  */
8812   saved_integral_constant_expression_p
8813     = parser->integral_constant_expression_p;
8814   saved_non_integral_constant_expression_p
8815     = parser->non_integral_constant_expression_p;
8816   parser->integral_constant_expression_p = false;
8817
8818   /* Do not actually evaluate the expression.  */
8819   ++skip_evaluation;
8820
8821   /* Parse the opening `('.  */
8822   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8823     return error_mark_node;
8824   
8825   /* First, try parsing an id-expression.  */
8826   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8827   cp_parser_parse_tentatively (parser);
8828   expr = cp_parser_id_expression (parser,
8829                                   /*template_keyword_p=*/false,
8830                                   /*check_dependency_p=*/true,
8831                                   /*template_p=*/NULL,
8832                                   /*declarator_p=*/false,
8833                                   /*optional_p=*/false);
8834
8835   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8836     {
8837       bool non_integral_constant_expression_p = false;
8838       tree id_expression = expr;
8839       cp_id_kind idk;
8840       const char *error_msg;
8841
8842       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8843         /* Lookup the name we got back from the id-expression.  */
8844         expr = cp_parser_lookup_name (parser, expr,
8845                                       none_type,
8846                                       /*is_template=*/false,
8847                                       /*is_namespace=*/false,
8848                                       /*check_dependency=*/true,
8849                                       /*ambiguous_decls=*/NULL,
8850                                       id_expr_start_token->location);
8851
8852       if (expr
8853           && expr != error_mark_node
8854           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8855           && TREE_CODE (expr) != TYPE_DECL
8856           && (TREE_CODE (expr) != BIT_NOT_EXPR
8857               || !TYPE_P (TREE_OPERAND (expr, 0)))
8858           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8859         {
8860           /* Complete lookup of the id-expression.  */
8861           expr = (finish_id_expression
8862                   (id_expression, expr, parser->scope, &idk,
8863                    /*integral_constant_expression_p=*/false,
8864                    /*allow_non_integral_constant_expression_p=*/true,
8865                    &non_integral_constant_expression_p,
8866                    /*template_p=*/false,
8867                    /*done=*/true,
8868                    /*address_p=*/false,
8869                    /*template_arg_p=*/false,
8870                    &error_msg,
8871                    id_expr_start_token->location));
8872
8873           if (expr == error_mark_node)
8874             /* We found an id-expression, but it was something that we
8875                should not have found. This is an error, not something
8876                we can recover from, so note that we found an
8877                id-expression and we'll recover as gracefully as
8878                possible.  */
8879             id_expression_or_member_access_p = true;
8880         }
8881
8882       if (expr 
8883           && expr != error_mark_node
8884           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8885         /* We have an id-expression.  */
8886         id_expression_or_member_access_p = true;
8887     }
8888
8889   if (!id_expression_or_member_access_p)
8890     {
8891       /* Abort the id-expression parse.  */
8892       cp_parser_abort_tentative_parse (parser);
8893
8894       /* Parsing tentatively, again.  */
8895       cp_parser_parse_tentatively (parser);
8896
8897       /* Parse a class member access.  */
8898       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8899                                            /*cast_p=*/false,
8900                                            /*member_access_only_p=*/true, NULL);
8901
8902       if (expr 
8903           && expr != error_mark_node
8904           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8905         /* We have an id-expression.  */
8906         id_expression_or_member_access_p = true;
8907     }
8908
8909   if (id_expression_or_member_access_p)
8910     /* We have parsed the complete id-expression or member access.  */
8911     cp_parser_parse_definitely (parser);
8912   else
8913     {
8914       /* Abort our attempt to parse an id-expression or member access
8915          expression.  */
8916       cp_parser_abort_tentative_parse (parser);
8917
8918       /* Parse a full expression.  */
8919       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8920     }
8921
8922   /* Go back to evaluating expressions.  */
8923   --skip_evaluation;
8924
8925   /* Restore the old message and the integral constant expression
8926      flags.  */
8927   parser->type_definition_forbidden_message = saved_message;
8928   parser->integral_constant_expression_p
8929     = saved_integral_constant_expression_p;
8930   parser->non_integral_constant_expression_p
8931     = saved_non_integral_constant_expression_p;
8932
8933   if (expr == error_mark_node)
8934     {
8935       /* Skip everything up to the closing `)'.  */
8936       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8937                                              /*consume_paren=*/true);
8938       return error_mark_node;
8939     }
8940   
8941   /* Parse to the closing `)'.  */
8942   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8943     {
8944       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8945                                              /*consume_paren=*/true);
8946       return error_mark_node;
8947     }
8948
8949   return finish_decltype_type (expr, id_expression_or_member_access_p);
8950 }
8951
8952 /* Special member functions [gram.special] */
8953
8954 /* Parse a conversion-function-id.
8955
8956    conversion-function-id:
8957      operator conversion-type-id
8958
8959    Returns an IDENTIFIER_NODE representing the operator.  */
8960
8961 static tree
8962 cp_parser_conversion_function_id (cp_parser* parser)
8963 {
8964   tree type;
8965   tree saved_scope;
8966   tree saved_qualifying_scope;
8967   tree saved_object_scope;
8968   tree pushed_scope = NULL_TREE;
8969
8970   /* Look for the `operator' token.  */
8971   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8972     return error_mark_node;
8973   /* When we parse the conversion-type-id, the current scope will be
8974      reset.  However, we need that information in able to look up the
8975      conversion function later, so we save it here.  */
8976   saved_scope = parser->scope;
8977   saved_qualifying_scope = parser->qualifying_scope;
8978   saved_object_scope = parser->object_scope;
8979   /* We must enter the scope of the class so that the names of
8980      entities declared within the class are available in the
8981      conversion-type-id.  For example, consider:
8982
8983        struct S {
8984          typedef int I;
8985          operator I();
8986        };
8987
8988        S::operator I() { ... }
8989
8990      In order to see that `I' is a type-name in the definition, we
8991      must be in the scope of `S'.  */
8992   if (saved_scope)
8993     pushed_scope = push_scope (saved_scope);
8994   /* Parse the conversion-type-id.  */
8995   type = cp_parser_conversion_type_id (parser);
8996   /* Leave the scope of the class, if any.  */
8997   if (pushed_scope)
8998     pop_scope (pushed_scope);
8999   /* Restore the saved scope.  */
9000   parser->scope = saved_scope;
9001   parser->qualifying_scope = saved_qualifying_scope;
9002   parser->object_scope = saved_object_scope;
9003   /* If the TYPE is invalid, indicate failure.  */
9004   if (type == error_mark_node)
9005     return error_mark_node;
9006   return mangle_conv_op_name_for_type (type);
9007 }
9008
9009 /* Parse a conversion-type-id:
9010
9011    conversion-type-id:
9012      type-specifier-seq conversion-declarator [opt]
9013
9014    Returns the TYPE specified.  */
9015
9016 static tree
9017 cp_parser_conversion_type_id (cp_parser* parser)
9018 {
9019   tree attributes;
9020   cp_decl_specifier_seq type_specifiers;
9021   cp_declarator *declarator;
9022   tree type_specified;
9023
9024   /* Parse the attributes.  */
9025   attributes = cp_parser_attributes_opt (parser);
9026   /* Parse the type-specifiers.  */
9027   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9028                                 &type_specifiers);
9029   /* If that didn't work, stop.  */
9030   if (type_specifiers.type == error_mark_node)
9031     return error_mark_node;
9032   /* Parse the conversion-declarator.  */
9033   declarator = cp_parser_conversion_declarator_opt (parser);
9034
9035   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9036                                     /*initialized=*/0, &attributes);
9037   if (attributes)
9038     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9039
9040   /* Don't give this error when parsing tentatively.  This happens to
9041      work because we always parse this definitively once.  */
9042   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9043       && type_uses_auto (type_specified))
9044     {
9045       error ("invalid use of %<auto%> in conversion operator");
9046       return error_mark_node;
9047     }
9048
9049   return type_specified;
9050 }
9051
9052 /* Parse an (optional) conversion-declarator.
9053
9054    conversion-declarator:
9055      ptr-operator conversion-declarator [opt]
9056
9057    */
9058
9059 static cp_declarator *
9060 cp_parser_conversion_declarator_opt (cp_parser* parser)
9061 {
9062   enum tree_code code;
9063   tree class_type;
9064   cp_cv_quals cv_quals;
9065
9066   /* We don't know if there's a ptr-operator next, or not.  */
9067   cp_parser_parse_tentatively (parser);
9068   /* Try the ptr-operator.  */
9069   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9070   /* If it worked, look for more conversion-declarators.  */
9071   if (cp_parser_parse_definitely (parser))
9072     {
9073       cp_declarator *declarator;
9074
9075       /* Parse another optional declarator.  */
9076       declarator = cp_parser_conversion_declarator_opt (parser);
9077
9078       return cp_parser_make_indirect_declarator
9079         (code, class_type, cv_quals, declarator);
9080    }
9081
9082   return NULL;
9083 }
9084
9085 /* Parse an (optional) ctor-initializer.
9086
9087    ctor-initializer:
9088      : mem-initializer-list
9089
9090    Returns TRUE iff the ctor-initializer was actually present.  */
9091
9092 static bool
9093 cp_parser_ctor_initializer_opt (cp_parser* parser)
9094 {
9095   /* If the next token is not a `:', then there is no
9096      ctor-initializer.  */
9097   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9098     {
9099       /* Do default initialization of any bases and members.  */
9100       if (DECL_CONSTRUCTOR_P (current_function_decl))
9101         finish_mem_initializers (NULL_TREE);
9102
9103       return false;
9104     }
9105
9106   /* Consume the `:' token.  */
9107   cp_lexer_consume_token (parser->lexer);
9108   /* And the mem-initializer-list.  */
9109   cp_parser_mem_initializer_list (parser);
9110
9111   return true;
9112 }
9113
9114 /* Parse a mem-initializer-list.
9115
9116    mem-initializer-list:
9117      mem-initializer ... [opt]
9118      mem-initializer ... [opt] , mem-initializer-list  */
9119
9120 static void
9121 cp_parser_mem_initializer_list (cp_parser* parser)
9122 {
9123   tree mem_initializer_list = NULL_TREE;
9124   cp_token *token = cp_lexer_peek_token (parser->lexer);
9125
9126   /* Let the semantic analysis code know that we are starting the
9127      mem-initializer-list.  */
9128   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9129     error ("%Honly constructors take base initializers",
9130            &token->location);
9131
9132   /* Loop through the list.  */
9133   while (true)
9134     {
9135       tree mem_initializer;
9136
9137       token = cp_lexer_peek_token (parser->lexer);
9138       /* Parse the mem-initializer.  */
9139       mem_initializer = cp_parser_mem_initializer (parser);
9140       /* If the next token is a `...', we're expanding member initializers. */
9141       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9142         {
9143           /* Consume the `...'. */
9144           cp_lexer_consume_token (parser->lexer);
9145
9146           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9147              can be expanded but members cannot. */
9148           if (mem_initializer != error_mark_node
9149               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9150             {
9151               error ("%Hcannot expand initializer for member %<%D%>",
9152                      &token->location, TREE_PURPOSE (mem_initializer));
9153               mem_initializer = error_mark_node;
9154             }
9155
9156           /* Construct the pack expansion type. */
9157           if (mem_initializer != error_mark_node)
9158             mem_initializer = make_pack_expansion (mem_initializer);
9159         }
9160       /* Add it to the list, unless it was erroneous.  */
9161       if (mem_initializer != error_mark_node)
9162         {
9163           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9164           mem_initializer_list = mem_initializer;
9165         }
9166       /* If the next token is not a `,', we're done.  */
9167       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9168         break;
9169       /* Consume the `,' token.  */
9170       cp_lexer_consume_token (parser->lexer);
9171     }
9172
9173   /* Perform semantic analysis.  */
9174   if (DECL_CONSTRUCTOR_P (current_function_decl))
9175     finish_mem_initializers (mem_initializer_list);
9176 }
9177
9178 /* Parse a mem-initializer.
9179
9180    mem-initializer:
9181      mem-initializer-id ( expression-list [opt] )
9182      mem-initializer-id braced-init-list
9183
9184    GNU extension:
9185
9186    mem-initializer:
9187      ( expression-list [opt] )
9188
9189    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9190    class) or FIELD_DECL (for a non-static data member) to initialize;
9191    the TREE_VALUE is the expression-list.  An empty initialization
9192    list is represented by void_list_node.  */
9193
9194 static tree
9195 cp_parser_mem_initializer (cp_parser* parser)
9196 {
9197   tree mem_initializer_id;
9198   tree expression_list;
9199   tree member;
9200   cp_token *token = cp_lexer_peek_token (parser->lexer);
9201
9202   /* Find out what is being initialized.  */
9203   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9204     {
9205       permerror (token->location,
9206                  "anachronistic old-style base class initializer");
9207       mem_initializer_id = NULL_TREE;
9208     }
9209   else
9210     {
9211       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9212       if (mem_initializer_id == error_mark_node)
9213         return mem_initializer_id;
9214     }
9215   member = expand_member_init (mem_initializer_id);
9216   if (member && !DECL_P (member))
9217     in_base_initializer = 1;
9218
9219   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9220     {
9221       bool expr_non_constant_p;
9222       maybe_warn_cpp0x ("extended initializer lists");
9223       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9224       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9225       expression_list = build_tree_list (NULL_TREE, expression_list);
9226     }
9227   else
9228     expression_list
9229       = cp_parser_parenthesized_expression_list (parser, false,
9230                                                  /*cast_p=*/false,
9231                                                  /*allow_expansion_p=*/true,
9232                                                  /*non_constant_p=*/NULL);
9233   if (expression_list == error_mark_node)
9234     return error_mark_node;
9235   if (!expression_list)
9236     expression_list = void_type_node;
9237
9238   in_base_initializer = 0;
9239
9240   return member ? build_tree_list (member, expression_list) : error_mark_node;
9241 }
9242
9243 /* Parse a mem-initializer-id.
9244
9245    mem-initializer-id:
9246      :: [opt] nested-name-specifier [opt] class-name
9247      identifier
9248
9249    Returns a TYPE indicating the class to be initializer for the first
9250    production.  Returns an IDENTIFIER_NODE indicating the data member
9251    to be initialized for the second production.  */
9252
9253 static tree
9254 cp_parser_mem_initializer_id (cp_parser* parser)
9255 {
9256   bool global_scope_p;
9257   bool nested_name_specifier_p;
9258   bool template_p = false;
9259   tree id;
9260
9261   cp_token *token = cp_lexer_peek_token (parser->lexer);
9262
9263   /* `typename' is not allowed in this context ([temp.res]).  */
9264   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9265     {
9266       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9267              "member initializer is implicitly a type)",
9268              &token->location);
9269       cp_lexer_consume_token (parser->lexer);
9270     }
9271   /* Look for the optional `::' operator.  */
9272   global_scope_p
9273     = (cp_parser_global_scope_opt (parser,
9274                                    /*current_scope_valid_p=*/false)
9275        != NULL_TREE);
9276   /* Look for the optional nested-name-specifier.  The simplest way to
9277      implement:
9278
9279        [temp.res]
9280
9281        The keyword `typename' is not permitted in a base-specifier or
9282        mem-initializer; in these contexts a qualified name that
9283        depends on a template-parameter is implicitly assumed to be a
9284        type name.
9285
9286      is to assume that we have seen the `typename' keyword at this
9287      point.  */
9288   nested_name_specifier_p
9289     = (cp_parser_nested_name_specifier_opt (parser,
9290                                             /*typename_keyword_p=*/true,
9291                                             /*check_dependency_p=*/true,
9292                                             /*type_p=*/true,
9293                                             /*is_declaration=*/true)
9294        != NULL_TREE);
9295   if (nested_name_specifier_p)
9296     template_p = cp_parser_optional_template_keyword (parser);
9297   /* If there is a `::' operator or a nested-name-specifier, then we
9298      are definitely looking for a class-name.  */
9299   if (global_scope_p || nested_name_specifier_p)
9300     return cp_parser_class_name (parser,
9301                                  /*typename_keyword_p=*/true,
9302                                  /*template_keyword_p=*/template_p,
9303                                  none_type,
9304                                  /*check_dependency_p=*/true,
9305                                  /*class_head_p=*/false,
9306                                  /*is_declaration=*/true);
9307   /* Otherwise, we could also be looking for an ordinary identifier.  */
9308   cp_parser_parse_tentatively (parser);
9309   /* Try a class-name.  */
9310   id = cp_parser_class_name (parser,
9311                              /*typename_keyword_p=*/true,
9312                              /*template_keyword_p=*/false,
9313                              none_type,
9314                              /*check_dependency_p=*/true,
9315                              /*class_head_p=*/false,
9316                              /*is_declaration=*/true);
9317   /* If we found one, we're done.  */
9318   if (cp_parser_parse_definitely (parser))
9319     return id;
9320   /* Otherwise, look for an ordinary identifier.  */
9321   return cp_parser_identifier (parser);
9322 }
9323
9324 /* Overloading [gram.over] */
9325
9326 /* Parse an operator-function-id.
9327
9328    operator-function-id:
9329      operator operator
9330
9331    Returns an IDENTIFIER_NODE for the operator which is a
9332    human-readable spelling of the identifier, e.g., `operator +'.  */
9333
9334 static tree
9335 cp_parser_operator_function_id (cp_parser* parser)
9336 {
9337   /* Look for the `operator' keyword.  */
9338   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9339     return error_mark_node;
9340   /* And then the name of the operator itself.  */
9341   return cp_parser_operator (parser);
9342 }
9343
9344 /* Parse an operator.
9345
9346    operator:
9347      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9348      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9349      || ++ -- , ->* -> () []
9350
9351    GNU Extensions:
9352
9353    operator:
9354      <? >? <?= >?=
9355
9356    Returns an IDENTIFIER_NODE for the operator which is a
9357    human-readable spelling of the identifier, e.g., `operator +'.  */
9358
9359 static tree
9360 cp_parser_operator (cp_parser* parser)
9361 {
9362   tree id = NULL_TREE;
9363   cp_token *token;
9364
9365   /* Peek at the next token.  */
9366   token = cp_lexer_peek_token (parser->lexer);
9367   /* Figure out which operator we have.  */
9368   switch (token->type)
9369     {
9370     case CPP_KEYWORD:
9371       {
9372         enum tree_code op;
9373
9374         /* The keyword should be either `new' or `delete'.  */
9375         if (token->keyword == RID_NEW)
9376           op = NEW_EXPR;
9377         else if (token->keyword == RID_DELETE)
9378           op = DELETE_EXPR;
9379         else
9380           break;
9381
9382         /* Consume the `new' or `delete' token.  */
9383         cp_lexer_consume_token (parser->lexer);
9384
9385         /* Peek at the next token.  */
9386         token = cp_lexer_peek_token (parser->lexer);
9387         /* If it's a `[' token then this is the array variant of the
9388            operator.  */
9389         if (token->type == CPP_OPEN_SQUARE)
9390           {
9391             /* Consume the `[' token.  */
9392             cp_lexer_consume_token (parser->lexer);
9393             /* Look for the `]' token.  */
9394             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9395             id = ansi_opname (op == NEW_EXPR
9396                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9397           }
9398         /* Otherwise, we have the non-array variant.  */
9399         else
9400           id = ansi_opname (op);
9401
9402         return id;
9403       }
9404
9405     case CPP_PLUS:
9406       id = ansi_opname (PLUS_EXPR);
9407       break;
9408
9409     case CPP_MINUS:
9410       id = ansi_opname (MINUS_EXPR);
9411       break;
9412
9413     case CPP_MULT:
9414       id = ansi_opname (MULT_EXPR);
9415       break;
9416
9417     case CPP_DIV:
9418       id = ansi_opname (TRUNC_DIV_EXPR);
9419       break;
9420
9421     case CPP_MOD:
9422       id = ansi_opname (TRUNC_MOD_EXPR);
9423       break;
9424
9425     case CPP_XOR:
9426       id = ansi_opname (BIT_XOR_EXPR);
9427       break;
9428
9429     case CPP_AND:
9430       id = ansi_opname (BIT_AND_EXPR);
9431       break;
9432
9433     case CPP_OR:
9434       id = ansi_opname (BIT_IOR_EXPR);
9435       break;
9436
9437     case CPP_COMPL:
9438       id = ansi_opname (BIT_NOT_EXPR);
9439       break;
9440
9441     case CPP_NOT:
9442       id = ansi_opname (TRUTH_NOT_EXPR);
9443       break;
9444
9445     case CPP_EQ:
9446       id = ansi_assopname (NOP_EXPR);
9447       break;
9448
9449     case CPP_LESS:
9450       id = ansi_opname (LT_EXPR);
9451       break;
9452
9453     case CPP_GREATER:
9454       id = ansi_opname (GT_EXPR);
9455       break;
9456
9457     case CPP_PLUS_EQ:
9458       id = ansi_assopname (PLUS_EXPR);
9459       break;
9460
9461     case CPP_MINUS_EQ:
9462       id = ansi_assopname (MINUS_EXPR);
9463       break;
9464
9465     case CPP_MULT_EQ:
9466       id = ansi_assopname (MULT_EXPR);
9467       break;
9468
9469     case CPP_DIV_EQ:
9470       id = ansi_assopname (TRUNC_DIV_EXPR);
9471       break;
9472
9473     case CPP_MOD_EQ:
9474       id = ansi_assopname (TRUNC_MOD_EXPR);
9475       break;
9476
9477     case CPP_XOR_EQ:
9478       id = ansi_assopname (BIT_XOR_EXPR);
9479       break;
9480
9481     case CPP_AND_EQ:
9482       id = ansi_assopname (BIT_AND_EXPR);
9483       break;
9484
9485     case CPP_OR_EQ:
9486       id = ansi_assopname (BIT_IOR_EXPR);
9487       break;
9488
9489     case CPP_LSHIFT:
9490       id = ansi_opname (LSHIFT_EXPR);
9491       break;
9492
9493     case CPP_RSHIFT:
9494       id = ansi_opname (RSHIFT_EXPR);
9495       break;
9496
9497     case CPP_LSHIFT_EQ:
9498       id = ansi_assopname (LSHIFT_EXPR);
9499       break;
9500
9501     case CPP_RSHIFT_EQ:
9502       id = ansi_assopname (RSHIFT_EXPR);
9503       break;
9504
9505     case CPP_EQ_EQ:
9506       id = ansi_opname (EQ_EXPR);
9507       break;
9508
9509     case CPP_NOT_EQ:
9510       id = ansi_opname (NE_EXPR);
9511       break;
9512
9513     case CPP_LESS_EQ:
9514       id = ansi_opname (LE_EXPR);
9515       break;
9516
9517     case CPP_GREATER_EQ:
9518       id = ansi_opname (GE_EXPR);
9519       break;
9520
9521     case CPP_AND_AND:
9522       id = ansi_opname (TRUTH_ANDIF_EXPR);
9523       break;
9524
9525     case CPP_OR_OR:
9526       id = ansi_opname (TRUTH_ORIF_EXPR);
9527       break;
9528
9529     case CPP_PLUS_PLUS:
9530       id = ansi_opname (POSTINCREMENT_EXPR);
9531       break;
9532
9533     case CPP_MINUS_MINUS:
9534       id = ansi_opname (PREDECREMENT_EXPR);
9535       break;
9536
9537     case CPP_COMMA:
9538       id = ansi_opname (COMPOUND_EXPR);
9539       break;
9540
9541     case CPP_DEREF_STAR:
9542       id = ansi_opname (MEMBER_REF);
9543       break;
9544
9545     case CPP_DEREF:
9546       id = ansi_opname (COMPONENT_REF);
9547       break;
9548
9549     case CPP_OPEN_PAREN:
9550       /* Consume the `('.  */
9551       cp_lexer_consume_token (parser->lexer);
9552       /* Look for the matching `)'.  */
9553       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9554       return ansi_opname (CALL_EXPR);
9555
9556     case CPP_OPEN_SQUARE:
9557       /* Consume the `['.  */
9558       cp_lexer_consume_token (parser->lexer);
9559       /* Look for the matching `]'.  */
9560       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9561       return ansi_opname (ARRAY_REF);
9562
9563     default:
9564       /* Anything else is an error.  */
9565       break;
9566     }
9567
9568   /* If we have selected an identifier, we need to consume the
9569      operator token.  */
9570   if (id)
9571     cp_lexer_consume_token (parser->lexer);
9572   /* Otherwise, no valid operator name was present.  */
9573   else
9574     {
9575       cp_parser_error (parser, "expected operator");
9576       id = error_mark_node;
9577     }
9578
9579   return id;
9580 }
9581
9582 /* Parse a template-declaration.
9583
9584    template-declaration:
9585      export [opt] template < template-parameter-list > declaration
9586
9587    If MEMBER_P is TRUE, this template-declaration occurs within a
9588    class-specifier.
9589
9590    The grammar rule given by the standard isn't correct.  What
9591    is really meant is:
9592
9593    template-declaration:
9594      export [opt] template-parameter-list-seq
9595        decl-specifier-seq [opt] init-declarator [opt] ;
9596      export [opt] template-parameter-list-seq
9597        function-definition
9598
9599    template-parameter-list-seq:
9600      template-parameter-list-seq [opt]
9601      template < template-parameter-list >  */
9602
9603 static void
9604 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9605 {
9606   /* Check for `export'.  */
9607   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9608     {
9609       /* Consume the `export' token.  */
9610       cp_lexer_consume_token (parser->lexer);
9611       /* Warn that we do not support `export'.  */
9612       warning (0, "keyword %<export%> not implemented, and will be ignored");
9613     }
9614
9615   cp_parser_template_declaration_after_export (parser, member_p);
9616 }
9617
9618 /* Parse a template-parameter-list.
9619
9620    template-parameter-list:
9621      template-parameter
9622      template-parameter-list , template-parameter
9623
9624    Returns a TREE_LIST.  Each node represents a template parameter.
9625    The nodes are connected via their TREE_CHAINs.  */
9626
9627 static tree
9628 cp_parser_template_parameter_list (cp_parser* parser)
9629 {
9630   tree parameter_list = NULL_TREE;
9631
9632   begin_template_parm_list ();
9633   while (true)
9634     {
9635       tree parameter;
9636       bool is_non_type;
9637       bool is_parameter_pack;
9638
9639       /* Parse the template-parameter.  */
9640       parameter = cp_parser_template_parameter (parser, 
9641                                                 &is_non_type,
9642                                                 &is_parameter_pack);
9643       /* Add it to the list.  */
9644       if (parameter != error_mark_node)
9645         parameter_list = process_template_parm (parameter_list,
9646                                                 parameter,
9647                                                 is_non_type,
9648                                                 is_parameter_pack);
9649       else
9650        {
9651          tree err_parm = build_tree_list (parameter, parameter);
9652          TREE_VALUE (err_parm) = error_mark_node;
9653          parameter_list = chainon (parameter_list, err_parm);
9654        }
9655
9656       /* If the next token is not a `,', we're done.  */
9657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9658         break;
9659       /* Otherwise, consume the `,' token.  */
9660       cp_lexer_consume_token (parser->lexer);
9661     }
9662
9663   return end_template_parm_list (parameter_list);
9664 }
9665
9666 /* Parse a template-parameter.
9667
9668    template-parameter:
9669      type-parameter
9670      parameter-declaration
9671
9672    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9673    the parameter.  The TREE_PURPOSE is the default value, if any.
9674    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9675    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9676    set to true iff this parameter is a parameter pack. */
9677
9678 static tree
9679 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9680                               bool *is_parameter_pack)
9681 {
9682   cp_token *token;
9683   cp_parameter_declarator *parameter_declarator;
9684   cp_declarator *id_declarator;
9685   tree parm;
9686
9687   /* Assume it is a type parameter or a template parameter.  */
9688   *is_non_type = false;
9689   /* Assume it not a parameter pack. */
9690   *is_parameter_pack = false;
9691   /* Peek at the next token.  */
9692   token = cp_lexer_peek_token (parser->lexer);
9693   /* If it is `class' or `template', we have a type-parameter.  */
9694   if (token->keyword == RID_TEMPLATE)
9695     return cp_parser_type_parameter (parser, is_parameter_pack);
9696   /* If it is `class' or `typename' we do not know yet whether it is a
9697      type parameter or a non-type parameter.  Consider:
9698
9699        template <typename T, typename T::X X> ...
9700
9701      or:
9702
9703        template <class C, class D*> ...
9704
9705      Here, the first parameter is a type parameter, and the second is
9706      a non-type parameter.  We can tell by looking at the token after
9707      the identifier -- if it is a `,', `=', or `>' then we have a type
9708      parameter.  */
9709   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9710     {
9711       /* Peek at the token after `class' or `typename'.  */
9712       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9713       /* If it's an ellipsis, we have a template type parameter
9714          pack. */
9715       if (token->type == CPP_ELLIPSIS)
9716         return cp_parser_type_parameter (parser, is_parameter_pack);
9717       /* If it's an identifier, skip it.  */
9718       if (token->type == CPP_NAME)
9719         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9720       /* Now, see if the token looks like the end of a template
9721          parameter.  */
9722       if (token->type == CPP_COMMA
9723           || token->type == CPP_EQ
9724           || token->type == CPP_GREATER)
9725         return cp_parser_type_parameter (parser, is_parameter_pack);
9726     }
9727
9728   /* Otherwise, it is a non-type parameter.
9729
9730      [temp.param]
9731
9732      When parsing a default template-argument for a non-type
9733      template-parameter, the first non-nested `>' is taken as the end
9734      of the template parameter-list rather than a greater-than
9735      operator.  */
9736   *is_non_type = true;
9737   parameter_declarator
9738      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9739                                         /*parenthesized_p=*/NULL);
9740
9741   /* If the parameter declaration is marked as a parameter pack, set
9742      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9743      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9744      grokdeclarator. */
9745   if (parameter_declarator
9746       && parameter_declarator->declarator
9747       && parameter_declarator->declarator->parameter_pack_p)
9748     {
9749       *is_parameter_pack = true;
9750       parameter_declarator->declarator->parameter_pack_p = false;
9751     }
9752
9753   /* If the next token is an ellipsis, and we don't already have it
9754      marked as a parameter pack, then we have a parameter pack (that
9755      has no declarator).  */
9756   if (!*is_parameter_pack
9757       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9758       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9759     {
9760       /* Consume the `...'.  */
9761       cp_lexer_consume_token (parser->lexer);
9762       maybe_warn_variadic_templates ();
9763       
9764       *is_parameter_pack = true;
9765     }
9766   /* We might end up with a pack expansion as the type of the non-type
9767      template parameter, in which case this is a non-type template
9768      parameter pack.  */
9769   else if (parameter_declarator
9770            && parameter_declarator->decl_specifiers.type
9771            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9772     {
9773       *is_parameter_pack = true;
9774       parameter_declarator->decl_specifiers.type = 
9775         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9776     }
9777
9778   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9779     {
9780       /* Parameter packs cannot have default arguments.  However, a
9781          user may try to do so, so we'll parse them and give an
9782          appropriate diagnostic here.  */
9783
9784       /* Consume the `='.  */
9785       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9786       cp_lexer_consume_token (parser->lexer);
9787       
9788       /* Find the name of the parameter pack.  */     
9789       id_declarator = parameter_declarator->declarator;
9790       while (id_declarator && id_declarator->kind != cdk_id)
9791         id_declarator = id_declarator->declarator;
9792       
9793       if (id_declarator && id_declarator->kind == cdk_id)
9794         error ("%Htemplate parameter pack %qD cannot have a default argument",
9795                &start_token->location, id_declarator->u.id.unqualified_name);
9796       else
9797         error ("%Htemplate parameter pack cannot have a default argument",
9798                &start_token->location);
9799       
9800       /* Parse the default argument, but throw away the result.  */
9801       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9802     }
9803
9804   parm = grokdeclarator (parameter_declarator->declarator,
9805                          &parameter_declarator->decl_specifiers,
9806                          PARM, /*initialized=*/0,
9807                          /*attrlist=*/NULL);
9808   if (parm == error_mark_node)
9809     return error_mark_node;
9810
9811   return build_tree_list (parameter_declarator->default_argument, parm);
9812 }
9813
9814 /* Parse a type-parameter.
9815
9816    type-parameter:
9817      class identifier [opt]
9818      class identifier [opt] = type-id
9819      typename identifier [opt]
9820      typename identifier [opt] = type-id
9821      template < template-parameter-list > class identifier [opt]
9822      template < template-parameter-list > class identifier [opt]
9823        = id-expression
9824
9825    GNU Extension (variadic templates):
9826
9827    type-parameter:
9828      class ... identifier [opt]
9829      typename ... identifier [opt]
9830
9831    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9832    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9833    the declaration of the parameter.
9834
9835    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9836
9837 static tree
9838 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9839 {
9840   cp_token *token;
9841   tree parameter;
9842
9843   /* Look for a keyword to tell us what kind of parameter this is.  */
9844   token = cp_parser_require (parser, CPP_KEYWORD,
9845                              "%<class%>, %<typename%>, or %<template%>");
9846   if (!token)
9847     return error_mark_node;
9848
9849   switch (token->keyword)
9850     {
9851     case RID_CLASS:
9852     case RID_TYPENAME:
9853       {
9854         tree identifier;
9855         tree default_argument;
9856
9857         /* If the next token is an ellipsis, we have a template
9858            argument pack. */
9859         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9860           {
9861             /* Consume the `...' token. */
9862             cp_lexer_consume_token (parser->lexer);
9863             maybe_warn_variadic_templates ();
9864
9865             *is_parameter_pack = true;
9866           }
9867
9868         /* If the next token is an identifier, then it names the
9869            parameter.  */
9870         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9871           identifier = cp_parser_identifier (parser);
9872         else
9873           identifier = NULL_TREE;
9874
9875         /* Create the parameter.  */
9876         parameter = finish_template_type_parm (class_type_node, identifier);
9877
9878         /* If the next token is an `=', we have a default argument.  */
9879         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9880           {
9881             /* Consume the `=' token.  */
9882             cp_lexer_consume_token (parser->lexer);
9883             /* Parse the default-argument.  */
9884             push_deferring_access_checks (dk_no_deferred);
9885             default_argument = cp_parser_type_id (parser);
9886
9887             /* Template parameter packs cannot have default
9888                arguments. */
9889             if (*is_parameter_pack)
9890               {
9891                 if (identifier)
9892                   error ("%Htemplate parameter pack %qD cannot have a "
9893                          "default argument", &token->location, identifier);
9894                 else
9895                   error ("%Htemplate parameter packs cannot have "
9896                          "default arguments", &token->location);
9897                 default_argument = NULL_TREE;
9898               }
9899             pop_deferring_access_checks ();
9900           }
9901         else
9902           default_argument = NULL_TREE;
9903
9904         /* Create the combined representation of the parameter and the
9905            default argument.  */
9906         parameter = build_tree_list (default_argument, parameter);
9907       }
9908       break;
9909
9910     case RID_TEMPLATE:
9911       {
9912         tree parameter_list;
9913         tree identifier;
9914         tree default_argument;
9915
9916         /* Look for the `<'.  */
9917         cp_parser_require (parser, CPP_LESS, "%<<%>");
9918         /* Parse the template-parameter-list.  */
9919         parameter_list = cp_parser_template_parameter_list (parser);
9920         /* Look for the `>'.  */
9921         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9922         /* Look for the `class' keyword.  */
9923         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9924         /* If the next token is an ellipsis, we have a template
9925            argument pack. */
9926         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9927           {
9928             /* Consume the `...' token. */
9929             cp_lexer_consume_token (parser->lexer);
9930             maybe_warn_variadic_templates ();
9931
9932             *is_parameter_pack = true;
9933           }
9934         /* If the next token is an `=', then there is a
9935            default-argument.  If the next token is a `>', we are at
9936            the end of the parameter-list.  If the next token is a `,',
9937            then we are at the end of this parameter.  */
9938         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9939             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9940             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9941           {
9942             identifier = cp_parser_identifier (parser);
9943             /* Treat invalid names as if the parameter were nameless.  */
9944             if (identifier == error_mark_node)
9945               identifier = NULL_TREE;
9946           }
9947         else
9948           identifier = NULL_TREE;
9949
9950         /* Create the template parameter.  */
9951         parameter = finish_template_template_parm (class_type_node,
9952                                                    identifier);
9953
9954         /* If the next token is an `=', then there is a
9955            default-argument.  */
9956         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9957           {
9958             bool is_template;
9959
9960             /* Consume the `='.  */
9961             cp_lexer_consume_token (parser->lexer);
9962             /* Parse the id-expression.  */
9963             push_deferring_access_checks (dk_no_deferred);
9964             /* save token before parsing the id-expression, for error
9965                reporting */
9966             token = cp_lexer_peek_token (parser->lexer);
9967             default_argument
9968               = cp_parser_id_expression (parser,
9969                                          /*template_keyword_p=*/false,
9970                                          /*check_dependency_p=*/true,
9971                                          /*template_p=*/&is_template,
9972                                          /*declarator_p=*/false,
9973                                          /*optional_p=*/false);
9974             if (TREE_CODE (default_argument) == TYPE_DECL)
9975               /* If the id-expression was a template-id that refers to
9976                  a template-class, we already have the declaration here,
9977                  so no further lookup is needed.  */
9978                  ;
9979             else
9980               /* Look up the name.  */
9981               default_argument
9982                 = cp_parser_lookup_name (parser, default_argument,
9983                                          none_type,
9984                                          /*is_template=*/is_template,
9985                                          /*is_namespace=*/false,
9986                                          /*check_dependency=*/true,
9987                                          /*ambiguous_decls=*/NULL,
9988                                          token->location);
9989             /* See if the default argument is valid.  */
9990             default_argument
9991               = check_template_template_default_arg (default_argument);
9992
9993             /* Template parameter packs cannot have default
9994                arguments. */
9995             if (*is_parameter_pack)
9996               {
9997                 if (identifier)
9998                   error ("%Htemplate parameter pack %qD cannot "
9999                          "have a default argument",
10000                          &token->location, identifier);
10001                 else
10002                   error ("%Htemplate parameter packs cannot "
10003                          "have default arguments",
10004                          &token->location);
10005                 default_argument = NULL_TREE;
10006               }
10007             pop_deferring_access_checks ();
10008           }
10009         else
10010           default_argument = NULL_TREE;
10011
10012         /* Create the combined representation of the parameter and the
10013            default argument.  */
10014         parameter = build_tree_list (default_argument, parameter);
10015       }
10016       break;
10017
10018     default:
10019       gcc_unreachable ();
10020       break;
10021     }
10022
10023   return parameter;
10024 }
10025
10026 /* Parse a template-id.
10027
10028    template-id:
10029      template-name < template-argument-list [opt] >
10030
10031    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10032    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10033    returned.  Otherwise, if the template-name names a function, or set
10034    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10035    names a class, returns a TYPE_DECL for the specialization.
10036
10037    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10038    uninstantiated templates.  */
10039
10040 static tree
10041 cp_parser_template_id (cp_parser *parser,
10042                        bool template_keyword_p,
10043                        bool check_dependency_p,
10044                        bool is_declaration)
10045 {
10046   int i;
10047   tree templ;
10048   tree arguments;
10049   tree template_id;
10050   cp_token_position start_of_id = 0;
10051   deferred_access_check *chk;
10052   VEC (deferred_access_check,gc) *access_check;
10053   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10054   bool is_identifier;
10055
10056   /* If the next token corresponds to a template-id, there is no need
10057      to reparse it.  */
10058   next_token = cp_lexer_peek_token (parser->lexer);
10059   if (next_token->type == CPP_TEMPLATE_ID)
10060     {
10061       struct tree_check *check_value;
10062
10063       /* Get the stored value.  */
10064       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10065       /* Perform any access checks that were deferred.  */
10066       access_check = check_value->checks;
10067       if (access_check)
10068         {
10069           for (i = 0 ;
10070                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10071                ++i)
10072             {
10073               perform_or_defer_access_check (chk->binfo,
10074                                              chk->decl,
10075                                              chk->diag_decl);
10076             }
10077         }
10078       /* Return the stored value.  */
10079       return check_value->value;
10080     }
10081
10082   /* Avoid performing name lookup if there is no possibility of
10083      finding a template-id.  */
10084   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10085       || (next_token->type == CPP_NAME
10086           && !cp_parser_nth_token_starts_template_argument_list_p
10087                (parser, 2)))
10088     {
10089       cp_parser_error (parser, "expected template-id");
10090       return error_mark_node;
10091     }
10092
10093   /* Remember where the template-id starts.  */
10094   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10095     start_of_id = cp_lexer_token_position (parser->lexer, false);
10096
10097   push_deferring_access_checks (dk_deferred);
10098
10099   /* Parse the template-name.  */
10100   is_identifier = false;
10101   token = cp_lexer_peek_token (parser->lexer);
10102   templ = cp_parser_template_name (parser, template_keyword_p,
10103                                    check_dependency_p,
10104                                    is_declaration,
10105                                    &is_identifier);
10106   if (templ == error_mark_node || is_identifier)
10107     {
10108       pop_deferring_access_checks ();
10109       return templ;
10110     }
10111
10112   /* If we find the sequence `[:' after a template-name, it's probably
10113      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10114      parse correctly the argument list.  */
10115   next_token = cp_lexer_peek_token (parser->lexer);
10116   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10117   if (next_token->type == CPP_OPEN_SQUARE
10118       && next_token->flags & DIGRAPH
10119       && next_token_2->type == CPP_COLON
10120       && !(next_token_2->flags & PREV_WHITE))
10121     {
10122       cp_parser_parse_tentatively (parser);
10123       /* Change `:' into `::'.  */
10124       next_token_2->type = CPP_SCOPE;
10125       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10126          CPP_LESS.  */
10127       cp_lexer_consume_token (parser->lexer);
10128
10129       /* Parse the arguments.  */
10130       arguments = cp_parser_enclosed_template_argument_list (parser);
10131       if (!cp_parser_parse_definitely (parser))
10132         {
10133           /* If we couldn't parse an argument list, then we revert our changes
10134              and return simply an error. Maybe this is not a template-id
10135              after all.  */
10136           next_token_2->type = CPP_COLON;
10137           cp_parser_error (parser, "expected %<<%>");
10138           pop_deferring_access_checks ();
10139           return error_mark_node;
10140         }
10141       /* Otherwise, emit an error about the invalid digraph, but continue
10142          parsing because we got our argument list.  */
10143       if (permerror (next_token->location,
10144                      "%<<::%> cannot begin a template-argument list"))
10145         {
10146           static bool hint = false;
10147           inform (next_token->location,
10148                   "%<<:%> is an alternate spelling for %<[%>."
10149                   " Insert whitespace between %<<%> and %<::%>");
10150           if (!hint && !flag_permissive)
10151             {
10152               inform (next_token->location, "(if you use %<-fpermissive%>"
10153                       " G++ will accept your code)");
10154               hint = true;
10155             }
10156         }
10157     }
10158   else
10159     {
10160       /* Look for the `<' that starts the template-argument-list.  */
10161       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10162         {
10163           pop_deferring_access_checks ();
10164           return error_mark_node;
10165         }
10166       /* Parse the arguments.  */
10167       arguments = cp_parser_enclosed_template_argument_list (parser);
10168     }
10169
10170   /* Build a representation of the specialization.  */
10171   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10172     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10173   else if (DECL_CLASS_TEMPLATE_P (templ)
10174            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10175     {
10176       bool entering_scope;
10177       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10178          template (rather than some instantiation thereof) only if
10179          is not nested within some other construct.  For example, in
10180          "template <typename T> void f(T) { A<T>::", A<T> is just an
10181          instantiation of A.  */
10182       entering_scope = (template_parm_scope_p ()
10183                         && cp_lexer_next_token_is (parser->lexer,
10184                                                    CPP_SCOPE));
10185       template_id
10186         = finish_template_type (templ, arguments, entering_scope);
10187     }
10188   else
10189     {
10190       /* If it's not a class-template or a template-template, it should be
10191          a function-template.  */
10192       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10193                    || TREE_CODE (templ) == OVERLOAD
10194                    || BASELINK_P (templ)));
10195
10196       template_id = lookup_template_function (templ, arguments);
10197     }
10198
10199   /* If parsing tentatively, replace the sequence of tokens that makes
10200      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10201      should we re-parse the token stream, we will not have to repeat
10202      the effort required to do the parse, nor will we issue duplicate
10203      error messages about problems during instantiation of the
10204      template.  */
10205   if (start_of_id)
10206     {
10207       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10208
10209       /* Reset the contents of the START_OF_ID token.  */
10210       token->type = CPP_TEMPLATE_ID;
10211       /* Retrieve any deferred checks.  Do not pop this access checks yet
10212          so the memory will not be reclaimed during token replacing below.  */
10213       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10214       token->u.tree_check_value->value = template_id;
10215       token->u.tree_check_value->checks = get_deferred_access_checks ();
10216       token->keyword = RID_MAX;
10217
10218       /* Purge all subsequent tokens.  */
10219       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10220
10221       /* ??? Can we actually assume that, if template_id ==
10222          error_mark_node, we will have issued a diagnostic to the
10223          user, as opposed to simply marking the tentative parse as
10224          failed?  */
10225       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10226         error ("%Hparse error in template argument list",
10227                &token->location);
10228     }
10229
10230   pop_deferring_access_checks ();
10231   return template_id;
10232 }
10233
10234 /* Parse a template-name.
10235
10236    template-name:
10237      identifier
10238
10239    The standard should actually say:
10240
10241    template-name:
10242      identifier
10243      operator-function-id
10244
10245    A defect report has been filed about this issue.
10246
10247    A conversion-function-id cannot be a template name because they cannot
10248    be part of a template-id. In fact, looking at this code:
10249
10250    a.operator K<int>()
10251
10252    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10253    It is impossible to call a templated conversion-function-id with an
10254    explicit argument list, since the only allowed template parameter is
10255    the type to which it is converting.
10256
10257    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10258    `template' keyword, in a construction like:
10259
10260      T::template f<3>()
10261
10262    In that case `f' is taken to be a template-name, even though there
10263    is no way of knowing for sure.
10264
10265    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10266    name refers to a set of overloaded functions, at least one of which
10267    is a template, or an IDENTIFIER_NODE with the name of the template,
10268    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10269    names are looked up inside uninstantiated templates.  */
10270
10271 static tree
10272 cp_parser_template_name (cp_parser* parser,
10273                          bool template_keyword_p,
10274                          bool check_dependency_p,
10275                          bool is_declaration,
10276                          bool *is_identifier)
10277 {
10278   tree identifier;
10279   tree decl;
10280   tree fns;
10281   cp_token *token = cp_lexer_peek_token (parser->lexer);
10282
10283   /* If the next token is `operator', then we have either an
10284      operator-function-id or a conversion-function-id.  */
10285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10286     {
10287       /* We don't know whether we're looking at an
10288          operator-function-id or a conversion-function-id.  */
10289       cp_parser_parse_tentatively (parser);
10290       /* Try an operator-function-id.  */
10291       identifier = cp_parser_operator_function_id (parser);
10292       /* If that didn't work, try a conversion-function-id.  */
10293       if (!cp_parser_parse_definitely (parser))
10294         {
10295           cp_parser_error (parser, "expected template-name");
10296           return error_mark_node;
10297         }
10298     }
10299   /* Look for the identifier.  */
10300   else
10301     identifier = cp_parser_identifier (parser);
10302
10303   /* If we didn't find an identifier, we don't have a template-id.  */
10304   if (identifier == error_mark_node)
10305     return error_mark_node;
10306
10307   /* If the name immediately followed the `template' keyword, then it
10308      is a template-name.  However, if the next token is not `<', then
10309      we do not treat it as a template-name, since it is not being used
10310      as part of a template-id.  This enables us to handle constructs
10311      like:
10312
10313        template <typename T> struct S { S(); };
10314        template <typename T> S<T>::S();
10315
10316      correctly.  We would treat `S' as a template -- if it were `S<T>'
10317      -- but we do not if there is no `<'.  */
10318
10319   if (processing_template_decl
10320       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10321     {
10322       /* In a declaration, in a dependent context, we pretend that the
10323          "template" keyword was present in order to improve error
10324          recovery.  For example, given:
10325
10326            template <typename T> void f(T::X<int>);
10327
10328          we want to treat "X<int>" as a template-id.  */
10329       if (is_declaration
10330           && !template_keyword_p
10331           && parser->scope && TYPE_P (parser->scope)
10332           && check_dependency_p
10333           && dependent_scope_p (parser->scope)
10334           /* Do not do this for dtors (or ctors), since they never
10335              need the template keyword before their name.  */
10336           && !constructor_name_p (identifier, parser->scope))
10337         {
10338           cp_token_position start = 0;
10339
10340           /* Explain what went wrong.  */
10341           error ("%Hnon-template %qD used as template",
10342                  &token->location, identifier);
10343           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10344                   parser->scope, identifier);
10345           /* If parsing tentatively, find the location of the "<" token.  */
10346           if (cp_parser_simulate_error (parser))
10347             start = cp_lexer_token_position (parser->lexer, true);
10348           /* Parse the template arguments so that we can issue error
10349              messages about them.  */
10350           cp_lexer_consume_token (parser->lexer);
10351           cp_parser_enclosed_template_argument_list (parser);
10352           /* Skip tokens until we find a good place from which to
10353              continue parsing.  */
10354           cp_parser_skip_to_closing_parenthesis (parser,
10355                                                  /*recovering=*/true,
10356                                                  /*or_comma=*/true,
10357                                                  /*consume_paren=*/false);
10358           /* If parsing tentatively, permanently remove the
10359              template argument list.  That will prevent duplicate
10360              error messages from being issued about the missing
10361              "template" keyword.  */
10362           if (start)
10363             cp_lexer_purge_tokens_after (parser->lexer, start);
10364           if (is_identifier)
10365             *is_identifier = true;
10366           return identifier;
10367         }
10368
10369       /* If the "template" keyword is present, then there is generally
10370          no point in doing name-lookup, so we just return IDENTIFIER.
10371          But, if the qualifying scope is non-dependent then we can
10372          (and must) do name-lookup normally.  */
10373       if (template_keyword_p
10374           && (!parser->scope
10375               || (TYPE_P (parser->scope)
10376                   && dependent_type_p (parser->scope))))
10377         return identifier;
10378     }
10379
10380   /* Look up the name.  */
10381   decl = cp_parser_lookup_name (parser, identifier,
10382                                 none_type,
10383                                 /*is_template=*/false,
10384                                 /*is_namespace=*/false,
10385                                 check_dependency_p,
10386                                 /*ambiguous_decls=*/NULL,
10387                                 token->location);
10388   decl = maybe_get_template_decl_from_type_decl (decl);
10389
10390   /* If DECL is a template, then the name was a template-name.  */
10391   if (TREE_CODE (decl) == TEMPLATE_DECL)
10392     ;
10393   else
10394     {
10395       tree fn = NULL_TREE;
10396
10397       /* The standard does not explicitly indicate whether a name that
10398          names a set of overloaded declarations, some of which are
10399          templates, is a template-name.  However, such a name should
10400          be a template-name; otherwise, there is no way to form a
10401          template-id for the overloaded templates.  */
10402       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10403       if (TREE_CODE (fns) == OVERLOAD)
10404         for (fn = fns; fn; fn = OVL_NEXT (fn))
10405           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10406             break;
10407
10408       if (!fn)
10409         {
10410           /* The name does not name a template.  */
10411           cp_parser_error (parser, "expected template-name");
10412           return error_mark_node;
10413         }
10414     }
10415
10416   /* If DECL is dependent, and refers to a function, then just return
10417      its name; we will look it up again during template instantiation.  */
10418   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10419     {
10420       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10421       if (TYPE_P (scope) && dependent_type_p (scope))
10422         return identifier;
10423     }
10424
10425   return decl;
10426 }
10427
10428 /* Parse a template-argument-list.
10429
10430    template-argument-list:
10431      template-argument ... [opt]
10432      template-argument-list , template-argument ... [opt]
10433
10434    Returns a TREE_VEC containing the arguments.  */
10435
10436 static tree
10437 cp_parser_template_argument_list (cp_parser* parser)
10438 {
10439   tree fixed_args[10];
10440   unsigned n_args = 0;
10441   unsigned alloced = 10;
10442   tree *arg_ary = fixed_args;
10443   tree vec;
10444   bool saved_in_template_argument_list_p;
10445   bool saved_ice_p;
10446   bool saved_non_ice_p;
10447
10448   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10449   parser->in_template_argument_list_p = true;
10450   /* Even if the template-id appears in an integral
10451      constant-expression, the contents of the argument list do
10452      not.  */
10453   saved_ice_p = parser->integral_constant_expression_p;
10454   parser->integral_constant_expression_p = false;
10455   saved_non_ice_p = parser->non_integral_constant_expression_p;
10456   parser->non_integral_constant_expression_p = false;
10457   /* Parse the arguments.  */
10458   do
10459     {
10460       tree argument;
10461
10462       if (n_args)
10463         /* Consume the comma.  */
10464         cp_lexer_consume_token (parser->lexer);
10465
10466       /* Parse the template-argument.  */
10467       argument = cp_parser_template_argument (parser);
10468
10469       /* If the next token is an ellipsis, we're expanding a template
10470          argument pack. */
10471       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10472         {
10473           /* Consume the `...' token. */
10474           cp_lexer_consume_token (parser->lexer);
10475
10476           /* Make the argument into a TYPE_PACK_EXPANSION or
10477              EXPR_PACK_EXPANSION. */
10478           argument = make_pack_expansion (argument);
10479         }
10480
10481       if (n_args == alloced)
10482         {
10483           alloced *= 2;
10484
10485           if (arg_ary == fixed_args)
10486             {
10487               arg_ary = XNEWVEC (tree, alloced);
10488               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10489             }
10490           else
10491             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10492         }
10493       arg_ary[n_args++] = argument;
10494     }
10495   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10496
10497   vec = make_tree_vec (n_args);
10498
10499   while (n_args--)
10500     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10501
10502   if (arg_ary != fixed_args)
10503     free (arg_ary);
10504   parser->non_integral_constant_expression_p = saved_non_ice_p;
10505   parser->integral_constant_expression_p = saved_ice_p;
10506   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10507   return vec;
10508 }
10509
10510 /* Parse a template-argument.
10511
10512    template-argument:
10513      assignment-expression
10514      type-id
10515      id-expression
10516
10517    The representation is that of an assignment-expression, type-id, or
10518    id-expression -- except that the qualified id-expression is
10519    evaluated, so that the value returned is either a DECL or an
10520    OVERLOAD.
10521
10522    Although the standard says "assignment-expression", it forbids
10523    throw-expressions or assignments in the template argument.
10524    Therefore, we use "conditional-expression" instead.  */
10525
10526 static tree
10527 cp_parser_template_argument (cp_parser* parser)
10528 {
10529   tree argument;
10530   bool template_p;
10531   bool address_p;
10532   bool maybe_type_id = false;
10533   cp_token *token = NULL, *argument_start_token = NULL;
10534   cp_id_kind idk;
10535
10536   /* There's really no way to know what we're looking at, so we just
10537      try each alternative in order.
10538
10539        [temp.arg]
10540
10541        In a template-argument, an ambiguity between a type-id and an
10542        expression is resolved to a type-id, regardless of the form of
10543        the corresponding template-parameter.
10544
10545      Therefore, we try a type-id first.  */
10546   cp_parser_parse_tentatively (parser);
10547   argument = cp_parser_template_type_arg (parser);
10548   /* If there was no error parsing the type-id but the next token is a
10549      '>>', our behavior depends on which dialect of C++ we're
10550      parsing. In C++98, we probably found a typo for '> >'. But there
10551      are type-id which are also valid expressions. For instance:
10552
10553      struct X { int operator >> (int); };
10554      template <int V> struct Foo {};
10555      Foo<X () >> 5> r;
10556
10557      Here 'X()' is a valid type-id of a function type, but the user just
10558      wanted to write the expression "X() >> 5". Thus, we remember that we
10559      found a valid type-id, but we still try to parse the argument as an
10560      expression to see what happens. 
10561
10562      In C++0x, the '>>' will be considered two separate '>'
10563      tokens.  */
10564   if (!cp_parser_error_occurred (parser)
10565       && cxx_dialect == cxx98
10566       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10567     {
10568       maybe_type_id = true;
10569       cp_parser_abort_tentative_parse (parser);
10570     }
10571   else
10572     {
10573       /* If the next token isn't a `,' or a `>', then this argument wasn't
10574       really finished. This means that the argument is not a valid
10575       type-id.  */
10576       if (!cp_parser_next_token_ends_template_argument_p (parser))
10577         cp_parser_error (parser, "expected template-argument");
10578       /* If that worked, we're done.  */
10579       if (cp_parser_parse_definitely (parser))
10580         return argument;
10581     }
10582   /* We're still not sure what the argument will be.  */
10583   cp_parser_parse_tentatively (parser);
10584   /* Try a template.  */
10585   argument_start_token = cp_lexer_peek_token (parser->lexer);
10586   argument = cp_parser_id_expression (parser,
10587                                       /*template_keyword_p=*/false,
10588                                       /*check_dependency_p=*/true,
10589                                       &template_p,
10590                                       /*declarator_p=*/false,
10591                                       /*optional_p=*/false);
10592   /* If the next token isn't a `,' or a `>', then this argument wasn't
10593      really finished.  */
10594   if (!cp_parser_next_token_ends_template_argument_p (parser))
10595     cp_parser_error (parser, "expected template-argument");
10596   if (!cp_parser_error_occurred (parser))
10597     {
10598       /* Figure out what is being referred to.  If the id-expression
10599          was for a class template specialization, then we will have a
10600          TYPE_DECL at this point.  There is no need to do name lookup
10601          at this point in that case.  */
10602       if (TREE_CODE (argument) != TYPE_DECL)
10603         argument = cp_parser_lookup_name (parser, argument,
10604                                           none_type,
10605                                           /*is_template=*/template_p,
10606                                           /*is_namespace=*/false,
10607                                           /*check_dependency=*/true,
10608                                           /*ambiguous_decls=*/NULL,
10609                                           argument_start_token->location);
10610       if (TREE_CODE (argument) != TEMPLATE_DECL
10611           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10612         cp_parser_error (parser, "expected template-name");
10613     }
10614   if (cp_parser_parse_definitely (parser))
10615     return argument;
10616   /* It must be a non-type argument.  There permitted cases are given
10617      in [temp.arg.nontype]:
10618
10619      -- an integral constant-expression of integral or enumeration
10620         type; or
10621
10622      -- the name of a non-type template-parameter; or
10623
10624      -- the name of an object or function with external linkage...
10625
10626      -- the address of an object or function with external linkage...
10627
10628      -- a pointer to member...  */
10629   /* Look for a non-type template parameter.  */
10630   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10631     {
10632       cp_parser_parse_tentatively (parser);
10633       argument = cp_parser_primary_expression (parser,
10634                                                /*address_p=*/false,
10635                                                /*cast_p=*/false,
10636                                                /*template_arg_p=*/true,
10637                                                &idk);
10638       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10639           || !cp_parser_next_token_ends_template_argument_p (parser))
10640         cp_parser_simulate_error (parser);
10641       if (cp_parser_parse_definitely (parser))
10642         return argument;
10643     }
10644
10645   /* If the next token is "&", the argument must be the address of an
10646      object or function with external linkage.  */
10647   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10648   if (address_p)
10649     cp_lexer_consume_token (parser->lexer);
10650   /* See if we might have an id-expression.  */
10651   token = cp_lexer_peek_token (parser->lexer);
10652   if (token->type == CPP_NAME
10653       || token->keyword == RID_OPERATOR
10654       || token->type == CPP_SCOPE
10655       || token->type == CPP_TEMPLATE_ID
10656       || token->type == CPP_NESTED_NAME_SPECIFIER)
10657     {
10658       cp_parser_parse_tentatively (parser);
10659       argument = cp_parser_primary_expression (parser,
10660                                                address_p,
10661                                                /*cast_p=*/false,
10662                                                /*template_arg_p=*/true,
10663                                                &idk);
10664       if (cp_parser_error_occurred (parser)
10665           || !cp_parser_next_token_ends_template_argument_p (parser))
10666         cp_parser_abort_tentative_parse (parser);
10667       else
10668         {
10669           if (TREE_CODE (argument) == INDIRECT_REF)
10670             {
10671               gcc_assert (REFERENCE_REF_P (argument));
10672               argument = TREE_OPERAND (argument, 0);
10673             }
10674
10675           if (TREE_CODE (argument) == VAR_DECL)
10676             {
10677               /* A variable without external linkage might still be a
10678                  valid constant-expression, so no error is issued here
10679                  if the external-linkage check fails.  */
10680               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10681                 cp_parser_simulate_error (parser);
10682             }
10683           else if (is_overloaded_fn (argument))
10684             /* All overloaded functions are allowed; if the external
10685                linkage test does not pass, an error will be issued
10686                later.  */
10687             ;
10688           else if (address_p
10689                    && (TREE_CODE (argument) == OFFSET_REF
10690                        || TREE_CODE (argument) == SCOPE_REF))
10691             /* A pointer-to-member.  */
10692             ;
10693           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10694             ;
10695           else
10696             cp_parser_simulate_error (parser);
10697
10698           if (cp_parser_parse_definitely (parser))
10699             {
10700               if (address_p)
10701                 argument = build_x_unary_op (ADDR_EXPR, argument,
10702                                              tf_warning_or_error);
10703               return argument;
10704             }
10705         }
10706     }
10707   /* If the argument started with "&", there are no other valid
10708      alternatives at this point.  */
10709   if (address_p)
10710     {
10711       cp_parser_error (parser, "invalid non-type template argument");
10712       return error_mark_node;
10713     }
10714
10715   /* If the argument wasn't successfully parsed as a type-id followed
10716      by '>>', the argument can only be a constant expression now.
10717      Otherwise, we try parsing the constant-expression tentatively,
10718      because the argument could really be a type-id.  */
10719   if (maybe_type_id)
10720     cp_parser_parse_tentatively (parser);
10721   argument = cp_parser_constant_expression (parser,
10722                                             /*allow_non_constant_p=*/false,
10723                                             /*non_constant_p=*/NULL);
10724   argument = fold_non_dependent_expr (argument);
10725   if (!maybe_type_id)
10726     return argument;
10727   if (!cp_parser_next_token_ends_template_argument_p (parser))
10728     cp_parser_error (parser, "expected template-argument");
10729   if (cp_parser_parse_definitely (parser))
10730     return argument;
10731   /* We did our best to parse the argument as a non type-id, but that
10732      was the only alternative that matched (albeit with a '>' after
10733      it). We can assume it's just a typo from the user, and a
10734      diagnostic will then be issued.  */
10735   return cp_parser_template_type_arg (parser);
10736 }
10737
10738 /* Parse an explicit-instantiation.
10739
10740    explicit-instantiation:
10741      template declaration
10742
10743    Although the standard says `declaration', what it really means is:
10744
10745    explicit-instantiation:
10746      template decl-specifier-seq [opt] declarator [opt] ;
10747
10748    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10749    supposed to be allowed.  A defect report has been filed about this
10750    issue.
10751
10752    GNU Extension:
10753
10754    explicit-instantiation:
10755      storage-class-specifier template
10756        decl-specifier-seq [opt] declarator [opt] ;
10757      function-specifier template
10758        decl-specifier-seq [opt] declarator [opt] ;  */
10759
10760 static void
10761 cp_parser_explicit_instantiation (cp_parser* parser)
10762 {
10763   int declares_class_or_enum;
10764   cp_decl_specifier_seq decl_specifiers;
10765   tree extension_specifier = NULL_TREE;
10766   cp_token *token;
10767
10768   /* Look for an (optional) storage-class-specifier or
10769      function-specifier.  */
10770   if (cp_parser_allow_gnu_extensions_p (parser))
10771     {
10772       extension_specifier
10773         = cp_parser_storage_class_specifier_opt (parser);
10774       if (!extension_specifier)
10775         extension_specifier
10776           = cp_parser_function_specifier_opt (parser,
10777                                               /*decl_specs=*/NULL);
10778     }
10779
10780   /* Look for the `template' keyword.  */
10781   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10782   /* Let the front end know that we are processing an explicit
10783      instantiation.  */
10784   begin_explicit_instantiation ();
10785   /* [temp.explicit] says that we are supposed to ignore access
10786      control while processing explicit instantiation directives.  */
10787   push_deferring_access_checks (dk_no_check);
10788   /* Parse a decl-specifier-seq.  */
10789   token = cp_lexer_peek_token (parser->lexer);
10790   cp_parser_decl_specifier_seq (parser,
10791                                 CP_PARSER_FLAGS_OPTIONAL,
10792                                 &decl_specifiers,
10793                                 &declares_class_or_enum);
10794   /* If there was exactly one decl-specifier, and it declared a class,
10795      and there's no declarator, then we have an explicit type
10796      instantiation.  */
10797   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10798     {
10799       tree type;
10800
10801       type = check_tag_decl (&decl_specifiers);
10802       /* Turn access control back on for names used during
10803          template instantiation.  */
10804       pop_deferring_access_checks ();
10805       if (type)
10806         do_type_instantiation (type, extension_specifier,
10807                                /*complain=*/tf_error);
10808     }
10809   else
10810     {
10811       cp_declarator *declarator;
10812       tree decl;
10813
10814       /* Parse the declarator.  */
10815       declarator
10816         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10817                                 /*ctor_dtor_or_conv_p=*/NULL,
10818                                 /*parenthesized_p=*/NULL,
10819                                 /*member_p=*/false);
10820       if (declares_class_or_enum & 2)
10821         cp_parser_check_for_definition_in_return_type (declarator,
10822                                                        decl_specifiers.type,
10823                                                        decl_specifiers.type_location);
10824       if (declarator != cp_error_declarator)
10825         {
10826           decl = grokdeclarator (declarator, &decl_specifiers,
10827                                  NORMAL, 0, &decl_specifiers.attributes);
10828           /* Turn access control back on for names used during
10829              template instantiation.  */
10830           pop_deferring_access_checks ();
10831           /* Do the explicit instantiation.  */
10832           do_decl_instantiation (decl, extension_specifier);
10833         }
10834       else
10835         {
10836           pop_deferring_access_checks ();
10837           /* Skip the body of the explicit instantiation.  */
10838           cp_parser_skip_to_end_of_statement (parser);
10839         }
10840     }
10841   /* We're done with the instantiation.  */
10842   end_explicit_instantiation ();
10843
10844   cp_parser_consume_semicolon_at_end_of_statement (parser);
10845 }
10846
10847 /* Parse an explicit-specialization.
10848
10849    explicit-specialization:
10850      template < > declaration
10851
10852    Although the standard says `declaration', what it really means is:
10853
10854    explicit-specialization:
10855      template <> decl-specifier [opt] init-declarator [opt] ;
10856      template <> function-definition
10857      template <> explicit-specialization
10858      template <> template-declaration  */
10859
10860 static void
10861 cp_parser_explicit_specialization (cp_parser* parser)
10862 {
10863   bool need_lang_pop;
10864   cp_token *token = cp_lexer_peek_token (parser->lexer);
10865
10866   /* Look for the `template' keyword.  */
10867   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10868   /* Look for the `<'.  */
10869   cp_parser_require (parser, CPP_LESS, "%<<%>");
10870   /* Look for the `>'.  */
10871   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10872   /* We have processed another parameter list.  */
10873   ++parser->num_template_parameter_lists;
10874   /* [temp]
10875
10876      A template ... explicit specialization ... shall not have C
10877      linkage.  */
10878   if (current_lang_name == lang_name_c)
10879     {
10880       error ("%Htemplate specialization with C linkage", &token->location);
10881       /* Give it C++ linkage to avoid confusing other parts of the
10882          front end.  */
10883       push_lang_context (lang_name_cplusplus);
10884       need_lang_pop = true;
10885     }
10886   else
10887     need_lang_pop = false;
10888   /* Let the front end know that we are beginning a specialization.  */
10889   if (!begin_specialization ())
10890     {
10891       end_specialization ();
10892       return;
10893     }
10894
10895   /* If the next keyword is `template', we need to figure out whether
10896      or not we're looking a template-declaration.  */
10897   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10898     {
10899       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10900           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10901         cp_parser_template_declaration_after_export (parser,
10902                                                      /*member_p=*/false);
10903       else
10904         cp_parser_explicit_specialization (parser);
10905     }
10906   else
10907     /* Parse the dependent declaration.  */
10908     cp_parser_single_declaration (parser,
10909                                   /*checks=*/NULL,
10910                                   /*member_p=*/false,
10911                                   /*explicit_specialization_p=*/true,
10912                                   /*friend_p=*/NULL);
10913   /* We're done with the specialization.  */
10914   end_specialization ();
10915   /* For the erroneous case of a template with C linkage, we pushed an
10916      implicit C++ linkage scope; exit that scope now.  */
10917   if (need_lang_pop)
10918     pop_lang_context ();
10919   /* We're done with this parameter list.  */
10920   --parser->num_template_parameter_lists;
10921 }
10922
10923 /* Parse a type-specifier.
10924
10925    type-specifier:
10926      simple-type-specifier
10927      class-specifier
10928      enum-specifier
10929      elaborated-type-specifier
10930      cv-qualifier
10931
10932    GNU Extension:
10933
10934    type-specifier:
10935      __complex__
10936
10937    Returns a representation of the type-specifier.  For a
10938    class-specifier, enum-specifier, or elaborated-type-specifier, a
10939    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10940
10941    The parser flags FLAGS is used to control type-specifier parsing.
10942
10943    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10944    in a decl-specifier-seq.
10945
10946    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10947    class-specifier, enum-specifier, or elaborated-type-specifier, then
10948    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10949    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10950    zero.
10951
10952    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10953    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10954    is set to FALSE.  */
10955
10956 static tree
10957 cp_parser_type_specifier (cp_parser* parser,
10958                           cp_parser_flags flags,
10959                           cp_decl_specifier_seq *decl_specs,
10960                           bool is_declaration,
10961                           int* declares_class_or_enum,
10962                           bool* is_cv_qualifier)
10963 {
10964   tree type_spec = NULL_TREE;
10965   cp_token *token;
10966   enum rid keyword;
10967   cp_decl_spec ds = ds_last;
10968
10969   /* Assume this type-specifier does not declare a new type.  */
10970   if (declares_class_or_enum)
10971     *declares_class_or_enum = 0;
10972   /* And that it does not specify a cv-qualifier.  */
10973   if (is_cv_qualifier)
10974     *is_cv_qualifier = false;
10975   /* Peek at the next token.  */
10976   token = cp_lexer_peek_token (parser->lexer);
10977
10978   /* If we're looking at a keyword, we can use that to guide the
10979      production we choose.  */
10980   keyword = token->keyword;
10981   switch (keyword)
10982     {
10983     case RID_ENUM:
10984       /* Look for the enum-specifier.  */
10985       type_spec = cp_parser_enum_specifier (parser);
10986       /* If that worked, we're done.  */
10987       if (type_spec)
10988         {
10989           if (declares_class_or_enum)
10990             *declares_class_or_enum = 2;
10991           if (decl_specs)
10992             cp_parser_set_decl_spec_type (decl_specs,
10993                                           type_spec,
10994                                           token->location,
10995                                           /*user_defined_p=*/true);
10996           return type_spec;
10997         }
10998       else
10999         goto elaborated_type_specifier;
11000
11001       /* Any of these indicate either a class-specifier, or an
11002          elaborated-type-specifier.  */
11003     case RID_CLASS:
11004     case RID_STRUCT:
11005     case RID_UNION:
11006       /* Parse tentatively so that we can back up if we don't find a
11007          class-specifier.  */
11008       cp_parser_parse_tentatively (parser);
11009       /* Look for the class-specifier.  */
11010       type_spec = cp_parser_class_specifier (parser);
11011       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11012       /* If that worked, we're done.  */
11013       if (cp_parser_parse_definitely (parser))
11014         {
11015           if (declares_class_or_enum)
11016             *declares_class_or_enum = 2;
11017           if (decl_specs)
11018             cp_parser_set_decl_spec_type (decl_specs,
11019                                           type_spec,
11020                                           token->location,
11021                                           /*user_defined_p=*/true);
11022           return type_spec;
11023         }
11024
11025       /* Fall through.  */
11026     elaborated_type_specifier:
11027       /* We're declaring (not defining) a class or enum.  */
11028       if (declares_class_or_enum)
11029         *declares_class_or_enum = 1;
11030
11031       /* Fall through.  */
11032     case RID_TYPENAME:
11033       /* Look for an elaborated-type-specifier.  */
11034       type_spec
11035         = (cp_parser_elaborated_type_specifier
11036            (parser,
11037             decl_specs && decl_specs->specs[(int) ds_friend],
11038             is_declaration));
11039       if (decl_specs)
11040         cp_parser_set_decl_spec_type (decl_specs,
11041                                       type_spec,
11042                                       token->location,
11043                                       /*user_defined_p=*/true);
11044       return type_spec;
11045
11046     case RID_CONST:
11047       ds = ds_const;
11048       if (is_cv_qualifier)
11049         *is_cv_qualifier = true;
11050       break;
11051
11052     case RID_VOLATILE:
11053       ds = ds_volatile;
11054       if (is_cv_qualifier)
11055         *is_cv_qualifier = true;
11056       break;
11057
11058     case RID_RESTRICT:
11059       ds = ds_restrict;
11060       if (is_cv_qualifier)
11061         *is_cv_qualifier = true;
11062       break;
11063
11064     case RID_COMPLEX:
11065       /* The `__complex__' keyword is a GNU extension.  */
11066       ds = ds_complex;
11067       break;
11068
11069     default:
11070       break;
11071     }
11072
11073   /* Handle simple keywords.  */
11074   if (ds != ds_last)
11075     {
11076       if (decl_specs)
11077         {
11078           ++decl_specs->specs[(int)ds];
11079           decl_specs->any_specifiers_p = true;
11080         }
11081       return cp_lexer_consume_token (parser->lexer)->u.value;
11082     }
11083
11084   /* If we do not already have a type-specifier, assume we are looking
11085      at a simple-type-specifier.  */
11086   type_spec = cp_parser_simple_type_specifier (parser,
11087                                                decl_specs,
11088                                                flags);
11089
11090   /* If we didn't find a type-specifier, and a type-specifier was not
11091      optional in this context, issue an error message.  */
11092   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11093     {
11094       cp_parser_error (parser, "expected type specifier");
11095       return error_mark_node;
11096     }
11097
11098   return type_spec;
11099 }
11100
11101 /* Parse a simple-type-specifier.
11102
11103    simple-type-specifier:
11104      :: [opt] nested-name-specifier [opt] type-name
11105      :: [opt] nested-name-specifier template template-id
11106      char
11107      wchar_t
11108      bool
11109      short
11110      int
11111      long
11112      signed
11113      unsigned
11114      float
11115      double
11116      void
11117
11118    C++0x Extension:
11119
11120    simple-type-specifier:
11121      auto
11122      decltype ( expression )   
11123      char16_t
11124      char32_t
11125
11126    GNU Extension:
11127
11128    simple-type-specifier:
11129      __typeof__ unary-expression
11130      __typeof__ ( type-id )
11131
11132    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11133    appropriately updated.  */
11134
11135 static tree
11136 cp_parser_simple_type_specifier (cp_parser* parser,
11137                                  cp_decl_specifier_seq *decl_specs,
11138                                  cp_parser_flags flags)
11139 {
11140   tree type = NULL_TREE;
11141   cp_token *token;
11142
11143   /* Peek at the next token.  */
11144   token = cp_lexer_peek_token (parser->lexer);
11145
11146   /* If we're looking at a keyword, things are easy.  */
11147   switch (token->keyword)
11148     {
11149     case RID_CHAR:
11150       if (decl_specs)
11151         decl_specs->explicit_char_p = true;
11152       type = char_type_node;
11153       break;
11154     case RID_CHAR16:
11155       type = char16_type_node;
11156       break;
11157     case RID_CHAR32:
11158       type = char32_type_node;
11159       break;
11160     case RID_WCHAR:
11161       type = wchar_type_node;
11162       break;
11163     case RID_BOOL:
11164       type = boolean_type_node;
11165       break;
11166     case RID_SHORT:
11167       if (decl_specs)
11168         ++decl_specs->specs[(int) ds_short];
11169       type = short_integer_type_node;
11170       break;
11171     case RID_INT:
11172       if (decl_specs)
11173         decl_specs->explicit_int_p = true;
11174       type = integer_type_node;
11175       break;
11176     case RID_LONG:
11177       if (decl_specs)
11178         ++decl_specs->specs[(int) ds_long];
11179       type = long_integer_type_node;
11180       break;
11181     case RID_SIGNED:
11182       if (decl_specs)
11183         ++decl_specs->specs[(int) ds_signed];
11184       type = integer_type_node;
11185       break;
11186     case RID_UNSIGNED:
11187       if (decl_specs)
11188         ++decl_specs->specs[(int) ds_unsigned];
11189       type = unsigned_type_node;
11190       break;
11191     case RID_FLOAT:
11192       type = float_type_node;
11193       break;
11194     case RID_DOUBLE:
11195       type = double_type_node;
11196       break;
11197     case RID_VOID:
11198       type = void_type_node;
11199       break;
11200       
11201     case RID_AUTO:
11202       maybe_warn_cpp0x ("C++0x auto");
11203       type = make_auto ();
11204       break;
11205
11206     case RID_DECLTYPE:
11207       /* Parse the `decltype' type.  */
11208       type = cp_parser_decltype (parser);
11209
11210       if (decl_specs)
11211         cp_parser_set_decl_spec_type (decl_specs, type,
11212                                       token->location,
11213                                       /*user_defined_p=*/true);
11214
11215       return type;
11216
11217     case RID_TYPEOF:
11218       /* Consume the `typeof' token.  */
11219       cp_lexer_consume_token (parser->lexer);
11220       /* Parse the operand to `typeof'.  */
11221       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11222       /* If it is not already a TYPE, take its type.  */
11223       if (!TYPE_P (type))
11224         type = finish_typeof (type);
11225
11226       if (decl_specs)
11227         cp_parser_set_decl_spec_type (decl_specs, type,
11228                                       token->location,
11229                                       /*user_defined_p=*/true);
11230
11231       return type;
11232
11233     default:
11234       break;
11235     }
11236
11237   /* If the type-specifier was for a built-in type, we're done.  */
11238   if (type)
11239     {
11240       tree id;
11241
11242       /* Record the type.  */
11243       if (decl_specs
11244           && (token->keyword != RID_SIGNED
11245               && token->keyword != RID_UNSIGNED
11246               && token->keyword != RID_SHORT
11247               && token->keyword != RID_LONG))
11248         cp_parser_set_decl_spec_type (decl_specs,
11249                                       type,
11250                                       token->location,
11251                                       /*user_defined=*/false);
11252       if (decl_specs)
11253         decl_specs->any_specifiers_p = true;
11254
11255       /* Consume the token.  */
11256       id = cp_lexer_consume_token (parser->lexer)->u.value;
11257
11258       /* There is no valid C++ program where a non-template type is
11259          followed by a "<".  That usually indicates that the user thought
11260          that the type was a template.  */
11261       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11262
11263       return TYPE_NAME (type);
11264     }
11265
11266   /* The type-specifier must be a user-defined type.  */
11267   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11268     {
11269       bool qualified_p;
11270       bool global_p;
11271
11272       /* Don't gobble tokens or issue error messages if this is an
11273          optional type-specifier.  */
11274       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11275         cp_parser_parse_tentatively (parser);
11276
11277       /* Look for the optional `::' operator.  */
11278       global_p
11279         = (cp_parser_global_scope_opt (parser,
11280                                        /*current_scope_valid_p=*/false)
11281            != NULL_TREE);
11282       /* Look for the nested-name specifier.  */
11283       qualified_p
11284         = (cp_parser_nested_name_specifier_opt (parser,
11285                                                 /*typename_keyword_p=*/false,
11286                                                 /*check_dependency_p=*/true,
11287                                                 /*type_p=*/false,
11288                                                 /*is_declaration=*/false)
11289            != NULL_TREE);
11290       token = cp_lexer_peek_token (parser->lexer);
11291       /* If we have seen a nested-name-specifier, and the next token
11292          is `template', then we are using the template-id production.  */
11293       if (parser->scope
11294           && cp_parser_optional_template_keyword (parser))
11295         {
11296           /* Look for the template-id.  */
11297           type = cp_parser_template_id (parser,
11298                                         /*template_keyword_p=*/true,
11299                                         /*check_dependency_p=*/true,
11300                                         /*is_declaration=*/false);
11301           /* If the template-id did not name a type, we are out of
11302              luck.  */
11303           if (TREE_CODE (type) != TYPE_DECL)
11304             {
11305               cp_parser_error (parser, "expected template-id for type");
11306               type = NULL_TREE;
11307             }
11308         }
11309       /* Otherwise, look for a type-name.  */
11310       else
11311         type = cp_parser_type_name (parser);
11312       /* Keep track of all name-lookups performed in class scopes.  */
11313       if (type
11314           && !global_p
11315           && !qualified_p
11316           && TREE_CODE (type) == TYPE_DECL
11317           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11318         maybe_note_name_used_in_class (DECL_NAME (type), type);
11319       /* If it didn't work out, we don't have a TYPE.  */
11320       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11321           && !cp_parser_parse_definitely (parser))
11322         type = NULL_TREE;
11323       if (type && decl_specs)
11324         cp_parser_set_decl_spec_type (decl_specs, type,
11325                                       token->location,
11326                                       /*user_defined=*/true);
11327     }
11328
11329   /* If we didn't get a type-name, issue an error message.  */
11330   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11331     {
11332       cp_parser_error (parser, "expected type-name");
11333       return error_mark_node;
11334     }
11335
11336   /* There is no valid C++ program where a non-template type is
11337      followed by a "<".  That usually indicates that the user thought
11338      that the type was a template.  */
11339   if (type && type != error_mark_node)
11340     {
11341       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11342          If it is, then the '<'...'>' enclose protocol names rather than
11343          template arguments, and so everything is fine.  */
11344       if (c_dialect_objc ()
11345           && (objc_is_id (type) || objc_is_class_name (type)))
11346         {
11347           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11348           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11349
11350           /* Clobber the "unqualified" type previously entered into
11351              DECL_SPECS with the new, improved protocol-qualified version.  */
11352           if (decl_specs)
11353             decl_specs->type = qual_type;
11354
11355           return qual_type;
11356         }
11357
11358       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11359                                                token->location);
11360     }
11361
11362   return type;
11363 }
11364
11365 /* Parse a type-name.
11366
11367    type-name:
11368      class-name
11369      enum-name
11370      typedef-name
11371
11372    enum-name:
11373      identifier
11374
11375    typedef-name:
11376      identifier
11377
11378    Returns a TYPE_DECL for the type.  */
11379
11380 static tree
11381 cp_parser_type_name (cp_parser* parser)
11382 {
11383   tree type_decl;
11384
11385   /* We can't know yet whether it is a class-name or not.  */
11386   cp_parser_parse_tentatively (parser);
11387   /* Try a class-name.  */
11388   type_decl = cp_parser_class_name (parser,
11389                                     /*typename_keyword_p=*/false,
11390                                     /*template_keyword_p=*/false,
11391                                     none_type,
11392                                     /*check_dependency_p=*/true,
11393                                     /*class_head_p=*/false,
11394                                     /*is_declaration=*/false);
11395   /* If it's not a class-name, keep looking.  */
11396   if (!cp_parser_parse_definitely (parser))
11397     {
11398       /* It must be a typedef-name or an enum-name.  */
11399       return cp_parser_nonclass_name (parser);
11400     }
11401
11402   return type_decl;
11403 }
11404
11405 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11406
11407    enum-name:
11408      identifier
11409
11410    typedef-name:
11411      identifier
11412
11413    Returns a TYPE_DECL for the type.  */
11414
11415 static tree
11416 cp_parser_nonclass_name (cp_parser* parser)
11417 {
11418   tree type_decl;
11419   tree identifier;
11420
11421   cp_token *token = cp_lexer_peek_token (parser->lexer);
11422   identifier = cp_parser_identifier (parser);
11423   if (identifier == error_mark_node)
11424     return error_mark_node;
11425
11426   /* Look up the type-name.  */
11427   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11428
11429   if (TREE_CODE (type_decl) != TYPE_DECL
11430       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11431     {
11432       /* See if this is an Objective-C type.  */
11433       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11434       tree type = objc_get_protocol_qualified_type (identifier, protos);
11435       if (type)
11436         type_decl = TYPE_NAME (type);
11437     }
11438   
11439   /* Issue an error if we did not find a type-name.  */
11440   if (TREE_CODE (type_decl) != TYPE_DECL)
11441     {
11442       if (!cp_parser_simulate_error (parser))
11443         cp_parser_name_lookup_error (parser, identifier, type_decl,
11444                                      "is not a type", token->location);
11445       return error_mark_node;
11446     }
11447   /* Remember that the name was used in the definition of the
11448      current class so that we can check later to see if the
11449      meaning would have been different after the class was
11450      entirely defined.  */
11451   else if (type_decl != error_mark_node
11452            && !parser->scope)
11453     maybe_note_name_used_in_class (identifier, type_decl);
11454   
11455   return type_decl;
11456 }
11457
11458 /* Parse an elaborated-type-specifier.  Note that the grammar given
11459    here incorporates the resolution to DR68.
11460
11461    elaborated-type-specifier:
11462      class-key :: [opt] nested-name-specifier [opt] identifier
11463      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11464      enum-key :: [opt] nested-name-specifier [opt] identifier
11465      typename :: [opt] nested-name-specifier identifier
11466      typename :: [opt] nested-name-specifier template [opt]
11467        template-id
11468
11469    GNU extension:
11470
11471    elaborated-type-specifier:
11472      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11473      class-key attributes :: [opt] nested-name-specifier [opt]
11474                template [opt] template-id
11475      enum attributes :: [opt] nested-name-specifier [opt] identifier
11476
11477    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11478    declared `friend'.  If IS_DECLARATION is TRUE, then this
11479    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11480    something is being declared.
11481
11482    Returns the TYPE specified.  */
11483
11484 static tree
11485 cp_parser_elaborated_type_specifier (cp_parser* parser,
11486                                      bool is_friend,
11487                                      bool is_declaration)
11488 {
11489   enum tag_types tag_type;
11490   tree identifier;
11491   tree type = NULL_TREE;
11492   tree attributes = NULL_TREE;
11493   cp_token *token = NULL;
11494
11495   /* See if we're looking at the `enum' keyword.  */
11496   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11497     {
11498       /* Consume the `enum' token.  */
11499       cp_lexer_consume_token (parser->lexer);
11500       /* Remember that it's an enumeration type.  */
11501       tag_type = enum_type;
11502       /* Parse the optional `struct' or `class' key (for C++0x scoped
11503          enums).  */
11504       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11505           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11506         {
11507           if (cxx_dialect == cxx98)
11508             maybe_warn_cpp0x ("scoped enums");
11509
11510           /* Consume the `struct' or `class'.  */
11511           cp_lexer_consume_token (parser->lexer);
11512         }
11513       /* Parse the attributes.  */
11514       attributes = cp_parser_attributes_opt (parser);
11515     }
11516   /* Or, it might be `typename'.  */
11517   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11518                                            RID_TYPENAME))
11519     {
11520       /* Consume the `typename' token.  */
11521       cp_lexer_consume_token (parser->lexer);
11522       /* Remember that it's a `typename' type.  */
11523       tag_type = typename_type;
11524       /* The `typename' keyword is only allowed in templates.  */
11525       if (!processing_template_decl)
11526         permerror (input_location, "using %<typename%> outside of template");
11527     }
11528   /* Otherwise it must be a class-key.  */
11529   else
11530     {
11531       tag_type = cp_parser_class_key (parser);
11532       if (tag_type == none_type)
11533         return error_mark_node;
11534       /* Parse the attributes.  */
11535       attributes = cp_parser_attributes_opt (parser);
11536     }
11537
11538   /* Look for the `::' operator.  */
11539   cp_parser_global_scope_opt (parser,
11540                               /*current_scope_valid_p=*/false);
11541   /* Look for the nested-name-specifier.  */
11542   if (tag_type == typename_type)
11543     {
11544       if (!cp_parser_nested_name_specifier (parser,
11545                                            /*typename_keyword_p=*/true,
11546                                            /*check_dependency_p=*/true,
11547                                            /*type_p=*/true,
11548                                             is_declaration))
11549         return error_mark_node;
11550     }
11551   else
11552     /* Even though `typename' is not present, the proposed resolution
11553        to Core Issue 180 says that in `class A<T>::B', `B' should be
11554        considered a type-name, even if `A<T>' is dependent.  */
11555     cp_parser_nested_name_specifier_opt (parser,
11556                                          /*typename_keyword_p=*/true,
11557                                          /*check_dependency_p=*/true,
11558                                          /*type_p=*/true,
11559                                          is_declaration);
11560  /* For everything but enumeration types, consider a template-id.
11561     For an enumeration type, consider only a plain identifier.  */
11562   if (tag_type != enum_type)
11563     {
11564       bool template_p = false;
11565       tree decl;
11566
11567       /* Allow the `template' keyword.  */
11568       template_p = cp_parser_optional_template_keyword (parser);
11569       /* If we didn't see `template', we don't know if there's a
11570          template-id or not.  */
11571       if (!template_p)
11572         cp_parser_parse_tentatively (parser);
11573       /* Parse the template-id.  */
11574       token = cp_lexer_peek_token (parser->lexer);
11575       decl = cp_parser_template_id (parser, template_p,
11576                                     /*check_dependency_p=*/true,
11577                                     is_declaration);
11578       /* If we didn't find a template-id, look for an ordinary
11579          identifier.  */
11580       if (!template_p && !cp_parser_parse_definitely (parser))
11581         ;
11582       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11583          in effect, then we must assume that, upon instantiation, the
11584          template will correspond to a class.  */
11585       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11586                && tag_type == typename_type)
11587         type = make_typename_type (parser->scope, decl,
11588                                    typename_type,
11589                                    /*complain=*/tf_error);
11590       /* If the `typename' keyword is in effect and DECL is not a type
11591          decl. Then type is non existant.   */
11592       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11593         type = NULL_TREE; 
11594       else 
11595         type = TREE_TYPE (decl);
11596     }
11597
11598   if (!type)
11599     {
11600       token = cp_lexer_peek_token (parser->lexer);
11601       identifier = cp_parser_identifier (parser);
11602
11603       if (identifier == error_mark_node)
11604         {
11605           parser->scope = NULL_TREE;
11606           return error_mark_node;
11607         }
11608
11609       /* For a `typename', we needn't call xref_tag.  */
11610       if (tag_type == typename_type
11611           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11612         return cp_parser_make_typename_type (parser, parser->scope,
11613                                              identifier,
11614                                              token->location);
11615       /* Look up a qualified name in the usual way.  */
11616       if (parser->scope)
11617         {
11618           tree decl;
11619           tree ambiguous_decls;
11620
11621           decl = cp_parser_lookup_name (parser, identifier,
11622                                         tag_type,
11623                                         /*is_template=*/false,
11624                                         /*is_namespace=*/false,
11625                                         /*check_dependency=*/true,
11626                                         &ambiguous_decls,
11627                                         token->location);
11628
11629           /* If the lookup was ambiguous, an error will already have been
11630              issued.  */
11631           if (ambiguous_decls)
11632             return error_mark_node;
11633
11634           /* If we are parsing friend declaration, DECL may be a
11635              TEMPLATE_DECL tree node here.  However, we need to check
11636              whether this TEMPLATE_DECL results in valid code.  Consider
11637              the following example:
11638
11639                namespace N {
11640                  template <class T> class C {};
11641                }
11642                class X {
11643                  template <class T> friend class N::C; // #1, valid code
11644                };
11645                template <class T> class Y {
11646                  friend class N::C;                    // #2, invalid code
11647                };
11648
11649              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11650              name lookup of `N::C'.  We see that friend declaration must
11651              be template for the code to be valid.  Note that
11652              processing_template_decl does not work here since it is
11653              always 1 for the above two cases.  */
11654
11655           decl = (cp_parser_maybe_treat_template_as_class
11656                   (decl, /*tag_name_p=*/is_friend
11657                          && parser->num_template_parameter_lists));
11658
11659           if (TREE_CODE (decl) != TYPE_DECL)
11660             {
11661               cp_parser_diagnose_invalid_type_name (parser,
11662                                                     parser->scope,
11663                                                     identifier,
11664                                                     token->location);
11665               return error_mark_node;
11666             }
11667
11668           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11669             {
11670               bool allow_template = (parser->num_template_parameter_lists
11671                                       || DECL_SELF_REFERENCE_P (decl));
11672               type = check_elaborated_type_specifier (tag_type, decl, 
11673                                                       allow_template);
11674
11675               if (type == error_mark_node)
11676                 return error_mark_node;
11677             }
11678
11679           /* Forward declarations of nested types, such as
11680
11681                class C1::C2;
11682                class C1::C2::C3;
11683
11684              are invalid unless all components preceding the final '::'
11685              are complete.  If all enclosing types are complete, these
11686              declarations become merely pointless.
11687
11688              Invalid forward declarations of nested types are errors
11689              caught elsewhere in parsing.  Those that are pointless arrive
11690              here.  */
11691
11692           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11693               && !is_friend && !processing_explicit_instantiation)
11694             warning (0, "declaration %qD does not declare anything", decl);
11695
11696           type = TREE_TYPE (decl);
11697         }
11698       else
11699         {
11700           /* An elaborated-type-specifier sometimes introduces a new type and
11701              sometimes names an existing type.  Normally, the rule is that it
11702              introduces a new type only if there is not an existing type of
11703              the same name already in scope.  For example, given:
11704
11705                struct S {};
11706                void f() { struct S s; }
11707
11708              the `struct S' in the body of `f' is the same `struct S' as in
11709              the global scope; the existing definition is used.  However, if
11710              there were no global declaration, this would introduce a new
11711              local class named `S'.
11712
11713              An exception to this rule applies to the following code:
11714
11715                namespace N { struct S; }
11716
11717              Here, the elaborated-type-specifier names a new type
11718              unconditionally; even if there is already an `S' in the
11719              containing scope this declaration names a new type.
11720              This exception only applies if the elaborated-type-specifier
11721              forms the complete declaration:
11722
11723                [class.name]
11724
11725                A declaration consisting solely of `class-key identifier ;' is
11726                either a redeclaration of the name in the current scope or a
11727                forward declaration of the identifier as a class name.  It
11728                introduces the name into the current scope.
11729
11730              We are in this situation precisely when the next token is a `;'.
11731
11732              An exception to the exception is that a `friend' declaration does
11733              *not* name a new type; i.e., given:
11734
11735                struct S { friend struct T; };
11736
11737              `T' is not a new type in the scope of `S'.
11738
11739              Also, `new struct S' or `sizeof (struct S)' never results in the
11740              definition of a new type; a new type can only be declared in a
11741              declaration context.  */
11742
11743           tag_scope ts;
11744           bool template_p;
11745
11746           if (is_friend)
11747             /* Friends have special name lookup rules.  */
11748             ts = ts_within_enclosing_non_class;
11749           else if (is_declaration
11750                    && cp_lexer_next_token_is (parser->lexer,
11751                                               CPP_SEMICOLON))
11752             /* This is a `class-key identifier ;' */
11753             ts = ts_current;
11754           else
11755             ts = ts_global;
11756
11757           template_p =
11758             (parser->num_template_parameter_lists
11759              && (cp_parser_next_token_starts_class_definition_p (parser)
11760                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11761           /* An unqualified name was used to reference this type, so
11762              there were no qualifying templates.  */
11763           if (!cp_parser_check_template_parameters (parser,
11764                                                     /*num_templates=*/0,
11765                                                     token->location,
11766                                                     /*declarator=*/NULL))
11767             return error_mark_node;
11768           type = xref_tag (tag_type, identifier, ts, template_p);
11769         }
11770     }
11771
11772   if (type == error_mark_node)
11773     return error_mark_node;
11774
11775   /* Allow attributes on forward declarations of classes.  */
11776   if (attributes)
11777     {
11778       if (TREE_CODE (type) == TYPENAME_TYPE)
11779         warning (OPT_Wattributes,
11780                  "attributes ignored on uninstantiated type");
11781       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11782                && ! processing_explicit_instantiation)
11783         warning (OPT_Wattributes,
11784                  "attributes ignored on template instantiation");
11785       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11786         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11787       else
11788         warning (OPT_Wattributes,
11789                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11790     }
11791
11792   if (tag_type != enum_type)
11793     cp_parser_check_class_key (tag_type, type);
11794
11795   /* A "<" cannot follow an elaborated type specifier.  If that
11796      happens, the user was probably trying to form a template-id.  */
11797   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11798
11799   return type;
11800 }
11801
11802 /* Parse an enum-specifier.
11803
11804    enum-specifier:
11805      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11806
11807    enum-key:
11808      enum
11809      enum class   [C++0x]
11810      enum struct  [C++0x]
11811
11812    enum-base:   [C++0x]
11813      : type-specifier-seq
11814
11815    GNU Extensions:
11816      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11817        { enumerator-list [opt] }attributes[opt]
11818
11819    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11820    if the token stream isn't an enum-specifier after all.  */
11821
11822 static tree
11823 cp_parser_enum_specifier (cp_parser* parser)
11824 {
11825   tree identifier;
11826   tree type;
11827   tree attributes;
11828   bool scoped_enum_p = false;
11829   bool has_underlying_type = false;
11830   tree underlying_type = NULL_TREE;
11831
11832   /* Parse tentatively so that we can back up if we don't find a
11833      enum-specifier.  */
11834   cp_parser_parse_tentatively (parser);
11835
11836   /* Caller guarantees that the current token is 'enum', an identifier
11837      possibly follows, and the token after that is an opening brace.
11838      If we don't have an identifier, fabricate an anonymous name for
11839      the enumeration being defined.  */
11840   cp_lexer_consume_token (parser->lexer);
11841
11842   /* Parse the "class" or "struct", which indicates a scoped
11843      enumeration type in C++0x.  */
11844   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11845       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11846     {
11847       if (cxx_dialect == cxx98)
11848         maybe_warn_cpp0x ("scoped enums");
11849
11850       /* Consume the `struct' or `class' token.  */
11851       cp_lexer_consume_token (parser->lexer);
11852
11853       scoped_enum_p = true;
11854     }
11855
11856   attributes = cp_parser_attributes_opt (parser);
11857
11858   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11859     identifier = cp_parser_identifier (parser);
11860   else
11861     identifier = make_anon_name ();
11862
11863   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11864   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11865     {
11866       cp_decl_specifier_seq type_specifiers;
11867
11868       /* At this point this is surely not elaborated type specifier.  */
11869       if (!cp_parser_parse_definitely (parser))
11870         return NULL_TREE;
11871
11872       if (cxx_dialect == cxx98)
11873         maybe_warn_cpp0x ("scoped enums");
11874
11875       /* Consume the `:'.  */
11876       cp_lexer_consume_token (parser->lexer);
11877
11878       has_underlying_type = true;
11879
11880       /* Parse the type-specifier-seq.  */
11881       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11882                                     &type_specifiers);
11883
11884       /* If that didn't work, stop.  */
11885       if (type_specifiers.type != error_mark_node)
11886         {
11887           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11888                                             /*initialized=*/0, NULL);
11889           if (underlying_type == error_mark_node)
11890             underlying_type = NULL_TREE;
11891         }
11892     }
11893
11894   /* Look for the `{' but don't consume it yet.  */
11895   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11896     {
11897       cp_parser_error (parser, "expected %<{%>");
11898       if (has_underlying_type)
11899         return NULL_TREE;
11900     }
11901
11902   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11903     return NULL_TREE;
11904
11905   /* Issue an error message if type-definitions are forbidden here.  */
11906   if (!cp_parser_check_type_definition (parser))
11907     type = error_mark_node;
11908   else
11909     /* Create the new type.  We do this before consuming the opening
11910        brace so the enum will be recorded as being on the line of its
11911        tag (or the 'enum' keyword, if there is no tag).  */
11912     type = start_enum (identifier, underlying_type, scoped_enum_p);
11913   
11914   /* Consume the opening brace.  */
11915   cp_lexer_consume_token (parser->lexer);
11916
11917   if (type == error_mark_node)
11918     {
11919       cp_parser_skip_to_end_of_block_or_statement (parser);
11920       return error_mark_node;
11921     }
11922
11923   /* If the next token is not '}', then there are some enumerators.  */
11924   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11925     cp_parser_enumerator_list (parser, type);
11926
11927   /* Consume the final '}'.  */
11928   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11929
11930   /* Look for trailing attributes to apply to this enumeration, and
11931      apply them if appropriate.  */
11932   if (cp_parser_allow_gnu_extensions_p (parser))
11933     {
11934       tree trailing_attr = cp_parser_attributes_opt (parser);
11935       trailing_attr = chainon (trailing_attr, attributes);
11936       cplus_decl_attributes (&type,
11937                              trailing_attr,
11938                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11939     }
11940
11941   /* Finish up the enumeration.  */
11942   finish_enum (type);
11943
11944   return type;
11945 }
11946
11947 /* Parse an enumerator-list.  The enumerators all have the indicated
11948    TYPE.
11949
11950    enumerator-list:
11951      enumerator-definition
11952      enumerator-list , enumerator-definition  */
11953
11954 static void
11955 cp_parser_enumerator_list (cp_parser* parser, tree type)
11956 {
11957   while (true)
11958     {
11959       /* Parse an enumerator-definition.  */
11960       cp_parser_enumerator_definition (parser, type);
11961
11962       /* If the next token is not a ',', we've reached the end of
11963          the list.  */
11964       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11965         break;
11966       /* Otherwise, consume the `,' and keep going.  */
11967       cp_lexer_consume_token (parser->lexer);
11968       /* If the next token is a `}', there is a trailing comma.  */
11969       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11970         {
11971           if (!in_system_header)
11972             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11973           break;
11974         }
11975     }
11976 }
11977
11978 /* Parse an enumerator-definition.  The enumerator has the indicated
11979    TYPE.
11980
11981    enumerator-definition:
11982      enumerator
11983      enumerator = constant-expression
11984
11985    enumerator:
11986      identifier  */
11987
11988 static void
11989 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11990 {
11991   tree identifier;
11992   tree value;
11993
11994   /* Look for the identifier.  */
11995   identifier = cp_parser_identifier (parser);
11996   if (identifier == error_mark_node)
11997     return;
11998
11999   /* If the next token is an '=', then there is an explicit value.  */
12000   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12001     {
12002       /* Consume the `=' token.  */
12003       cp_lexer_consume_token (parser->lexer);
12004       /* Parse the value.  */
12005       value = cp_parser_constant_expression (parser,
12006                                              /*allow_non_constant_p=*/false,
12007                                              NULL);
12008     }
12009   else
12010     value = NULL_TREE;
12011
12012   /* If we are processing a template, make sure the initializer of the
12013      enumerator doesn't contain any bare template parameter pack.  */
12014   if (check_for_bare_parameter_packs (value))
12015     value = error_mark_node;
12016
12017   /* Create the enumerator.  */
12018   build_enumerator (identifier, value, type);
12019 }
12020
12021 /* Parse a namespace-name.
12022
12023    namespace-name:
12024      original-namespace-name
12025      namespace-alias
12026
12027    Returns the NAMESPACE_DECL for the namespace.  */
12028
12029 static tree
12030 cp_parser_namespace_name (cp_parser* parser)
12031 {
12032   tree identifier;
12033   tree namespace_decl;
12034
12035   cp_token *token = cp_lexer_peek_token (parser->lexer);
12036
12037   /* Get the name of the namespace.  */
12038   identifier = cp_parser_identifier (parser);
12039   if (identifier == error_mark_node)
12040     return error_mark_node;
12041
12042   /* Look up the identifier in the currently active scope.  Look only
12043      for namespaces, due to:
12044
12045        [basic.lookup.udir]
12046
12047        When looking up a namespace-name in a using-directive or alias
12048        definition, only namespace names are considered.
12049
12050      And:
12051
12052        [basic.lookup.qual]
12053
12054        During the lookup of a name preceding the :: scope resolution
12055        operator, object, function, and enumerator names are ignored.
12056
12057      (Note that cp_parser_qualifying_entity only calls this
12058      function if the token after the name is the scope resolution
12059      operator.)  */
12060   namespace_decl = cp_parser_lookup_name (parser, identifier,
12061                                           none_type,
12062                                           /*is_template=*/false,
12063                                           /*is_namespace=*/true,
12064                                           /*check_dependency=*/true,
12065                                           /*ambiguous_decls=*/NULL,
12066                                           token->location);
12067   /* If it's not a namespace, issue an error.  */
12068   if (namespace_decl == error_mark_node
12069       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12070     {
12071       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12072         error ("%H%qD is not a namespace-name", &token->location, identifier);
12073       cp_parser_error (parser, "expected namespace-name");
12074       namespace_decl = error_mark_node;
12075     }
12076
12077   return namespace_decl;
12078 }
12079
12080 /* Parse a namespace-definition.
12081
12082    namespace-definition:
12083      named-namespace-definition
12084      unnamed-namespace-definition
12085
12086    named-namespace-definition:
12087      original-namespace-definition
12088      extension-namespace-definition
12089
12090    original-namespace-definition:
12091      namespace identifier { namespace-body }
12092
12093    extension-namespace-definition:
12094      namespace original-namespace-name { namespace-body }
12095
12096    unnamed-namespace-definition:
12097      namespace { namespace-body } */
12098
12099 static void
12100 cp_parser_namespace_definition (cp_parser* parser)
12101 {
12102   tree identifier, attribs;
12103   bool has_visibility;
12104   bool is_inline;
12105
12106   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12107     {
12108       is_inline = true;
12109       cp_lexer_consume_token (parser->lexer);
12110     }
12111   else
12112     is_inline = false;
12113
12114   /* Look for the `namespace' keyword.  */
12115   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12116
12117   /* Get the name of the namespace.  We do not attempt to distinguish
12118      between an original-namespace-definition and an
12119      extension-namespace-definition at this point.  The semantic
12120      analysis routines are responsible for that.  */
12121   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12122     identifier = cp_parser_identifier (parser);
12123   else
12124     identifier = NULL_TREE;
12125
12126   /* Parse any specified attributes.  */
12127   attribs = cp_parser_attributes_opt (parser);
12128
12129   /* Look for the `{' to start the namespace.  */
12130   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12131   /* Start the namespace.  */
12132   push_namespace (identifier);
12133
12134   /* "inline namespace" is equivalent to a stub namespace definition
12135      followed by a strong using directive.  */
12136   if (is_inline)
12137     {
12138       tree name_space = current_namespace;
12139       /* Set up namespace association.  */
12140       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12141         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12142                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12143       /* Import the contents of the inline namespace.  */
12144       pop_namespace ();
12145       do_using_directive (name_space);
12146       push_namespace (identifier);
12147     }
12148
12149   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12150
12151   /* Parse the body of the namespace.  */
12152   cp_parser_namespace_body (parser);
12153
12154 #ifdef HANDLE_PRAGMA_VISIBILITY
12155   if (has_visibility)
12156     pop_visibility ();
12157 #endif
12158
12159   /* Finish the namespace.  */
12160   pop_namespace ();
12161   /* Look for the final `}'.  */
12162   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12163 }
12164
12165 /* Parse a namespace-body.
12166
12167    namespace-body:
12168      declaration-seq [opt]  */
12169
12170 static void
12171 cp_parser_namespace_body (cp_parser* parser)
12172 {
12173   cp_parser_declaration_seq_opt (parser);
12174 }
12175
12176 /* Parse a namespace-alias-definition.
12177
12178    namespace-alias-definition:
12179      namespace identifier = qualified-namespace-specifier ;  */
12180
12181 static void
12182 cp_parser_namespace_alias_definition (cp_parser* parser)
12183 {
12184   tree identifier;
12185   tree namespace_specifier;
12186
12187   cp_token *token = cp_lexer_peek_token (parser->lexer);
12188
12189   /* Look for the `namespace' keyword.  */
12190   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12191   /* Look for the identifier.  */
12192   identifier = cp_parser_identifier (parser);
12193   if (identifier == error_mark_node)
12194     return;
12195   /* Look for the `=' token.  */
12196   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12197       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12198     {
12199       error ("%H%<namespace%> definition is not allowed here", &token->location);
12200       /* Skip the definition.  */
12201       cp_lexer_consume_token (parser->lexer);
12202       if (cp_parser_skip_to_closing_brace (parser))
12203         cp_lexer_consume_token (parser->lexer);
12204       return;
12205     }
12206   cp_parser_require (parser, CPP_EQ, "%<=%>");
12207   /* Look for the qualified-namespace-specifier.  */
12208   namespace_specifier
12209     = cp_parser_qualified_namespace_specifier (parser);
12210   /* Look for the `;' token.  */
12211   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12212
12213   /* Register the alias in the symbol table.  */
12214   do_namespace_alias (identifier, namespace_specifier);
12215 }
12216
12217 /* Parse a qualified-namespace-specifier.
12218
12219    qualified-namespace-specifier:
12220      :: [opt] nested-name-specifier [opt] namespace-name
12221
12222    Returns a NAMESPACE_DECL corresponding to the specified
12223    namespace.  */
12224
12225 static tree
12226 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12227 {
12228   /* Look for the optional `::'.  */
12229   cp_parser_global_scope_opt (parser,
12230                               /*current_scope_valid_p=*/false);
12231
12232   /* Look for the optional nested-name-specifier.  */
12233   cp_parser_nested_name_specifier_opt (parser,
12234                                        /*typename_keyword_p=*/false,
12235                                        /*check_dependency_p=*/true,
12236                                        /*type_p=*/false,
12237                                        /*is_declaration=*/true);
12238
12239   return cp_parser_namespace_name (parser);
12240 }
12241
12242 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12243    access declaration.
12244
12245    using-declaration:
12246      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12247      using :: unqualified-id ;  
12248
12249    access-declaration:
12250      qualified-id ;  
12251
12252    */
12253
12254 static bool
12255 cp_parser_using_declaration (cp_parser* parser, 
12256                              bool access_declaration_p)
12257 {
12258   cp_token *token;
12259   bool typename_p = false;
12260   bool global_scope_p;
12261   tree decl;
12262   tree identifier;
12263   tree qscope;
12264
12265   if (access_declaration_p)
12266     cp_parser_parse_tentatively (parser);
12267   else
12268     {
12269       /* Look for the `using' keyword.  */
12270       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12271       
12272       /* Peek at the next token.  */
12273       token = cp_lexer_peek_token (parser->lexer);
12274       /* See if it's `typename'.  */
12275       if (token->keyword == RID_TYPENAME)
12276         {
12277           /* Remember that we've seen it.  */
12278           typename_p = true;
12279           /* Consume the `typename' token.  */
12280           cp_lexer_consume_token (parser->lexer);
12281         }
12282     }
12283
12284   /* Look for the optional global scope qualification.  */
12285   global_scope_p
12286     = (cp_parser_global_scope_opt (parser,
12287                                    /*current_scope_valid_p=*/false)
12288        != NULL_TREE);
12289
12290   /* If we saw `typename', or didn't see `::', then there must be a
12291      nested-name-specifier present.  */
12292   if (typename_p || !global_scope_p)
12293     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12294                                               /*check_dependency_p=*/true,
12295                                               /*type_p=*/false,
12296                                               /*is_declaration=*/true);
12297   /* Otherwise, we could be in either of the two productions.  In that
12298      case, treat the nested-name-specifier as optional.  */
12299   else
12300     qscope = cp_parser_nested_name_specifier_opt (parser,
12301                                                   /*typename_keyword_p=*/false,
12302                                                   /*check_dependency_p=*/true,
12303                                                   /*type_p=*/false,
12304                                                   /*is_declaration=*/true);
12305   if (!qscope)
12306     qscope = global_namespace;
12307
12308   if (access_declaration_p && cp_parser_error_occurred (parser))
12309     /* Something has already gone wrong; there's no need to parse
12310        further.  Since an error has occurred, the return value of
12311        cp_parser_parse_definitely will be false, as required.  */
12312     return cp_parser_parse_definitely (parser);
12313
12314   token = cp_lexer_peek_token (parser->lexer);
12315   /* Parse the unqualified-id.  */
12316   identifier = cp_parser_unqualified_id (parser,
12317                                          /*template_keyword_p=*/false,
12318                                          /*check_dependency_p=*/true,
12319                                          /*declarator_p=*/true,
12320                                          /*optional_p=*/false);
12321
12322   if (access_declaration_p)
12323     {
12324       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12325         cp_parser_simulate_error (parser);
12326       if (!cp_parser_parse_definitely (parser))
12327         return false;
12328     }
12329
12330   /* The function we call to handle a using-declaration is different
12331      depending on what scope we are in.  */
12332   if (qscope == error_mark_node || identifier == error_mark_node)
12333     ;
12334   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12335            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12336     /* [namespace.udecl]
12337
12338        A using declaration shall not name a template-id.  */
12339     error ("%Ha template-id may not appear in a using-declaration",
12340             &token->location);
12341   else
12342     {
12343       if (at_class_scope_p ())
12344         {
12345           /* Create the USING_DECL.  */
12346           decl = do_class_using_decl (parser->scope, identifier);
12347
12348           if (check_for_bare_parameter_packs (decl))
12349             return false;
12350           else
12351             /* Add it to the list of members in this class.  */
12352             finish_member_declaration (decl);
12353         }
12354       else
12355         {
12356           decl = cp_parser_lookup_name_simple (parser,
12357                                                identifier,
12358                                                token->location);
12359           if (decl == error_mark_node)
12360             cp_parser_name_lookup_error (parser, identifier,
12361                                          decl, NULL,
12362                                          token->location);
12363           else if (check_for_bare_parameter_packs (decl))
12364             return false;
12365           else if (!at_namespace_scope_p ())
12366             do_local_using_decl (decl, qscope, identifier);
12367           else
12368             do_toplevel_using_decl (decl, qscope, identifier);
12369         }
12370     }
12371
12372   /* Look for the final `;'.  */
12373   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12374   
12375   return true;
12376 }
12377
12378 /* Parse a using-directive.
12379
12380    using-directive:
12381      using namespace :: [opt] nested-name-specifier [opt]
12382        namespace-name ;  */
12383
12384 static void
12385 cp_parser_using_directive (cp_parser* parser)
12386 {
12387   tree namespace_decl;
12388   tree attribs;
12389
12390   /* Look for the `using' keyword.  */
12391   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12392   /* And the `namespace' keyword.  */
12393   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12394   /* Look for the optional `::' operator.  */
12395   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12396   /* And the optional nested-name-specifier.  */
12397   cp_parser_nested_name_specifier_opt (parser,
12398                                        /*typename_keyword_p=*/false,
12399                                        /*check_dependency_p=*/true,
12400                                        /*type_p=*/false,
12401                                        /*is_declaration=*/true);
12402   /* Get the namespace being used.  */
12403   namespace_decl = cp_parser_namespace_name (parser);
12404   /* And any specified attributes.  */
12405   attribs = cp_parser_attributes_opt (parser);
12406   /* Update the symbol table.  */
12407   parse_using_directive (namespace_decl, attribs);
12408   /* Look for the final `;'.  */
12409   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12410 }
12411
12412 /* Parse an asm-definition.
12413
12414    asm-definition:
12415      asm ( string-literal ) ;
12416
12417    GNU Extension:
12418
12419    asm-definition:
12420      asm volatile [opt] ( string-literal ) ;
12421      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12422      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12423                           : asm-operand-list [opt] ) ;
12424      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12425                           : asm-operand-list [opt]
12426                           : asm-operand-list [opt] ) ;  */
12427
12428 static void
12429 cp_parser_asm_definition (cp_parser* parser)
12430 {
12431   tree string;
12432   tree outputs = NULL_TREE;
12433   tree inputs = NULL_TREE;
12434   tree clobbers = NULL_TREE;
12435   tree asm_stmt;
12436   bool volatile_p = false;
12437   bool extended_p = false;
12438   bool invalid_inputs_p = false;
12439   bool invalid_outputs_p = false;
12440
12441   /* Look for the `asm' keyword.  */
12442   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12443   /* See if the next token is `volatile'.  */
12444   if (cp_parser_allow_gnu_extensions_p (parser)
12445       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12446     {
12447       /* Remember that we saw the `volatile' keyword.  */
12448       volatile_p = true;
12449       /* Consume the token.  */
12450       cp_lexer_consume_token (parser->lexer);
12451     }
12452   /* Look for the opening `('.  */
12453   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12454     return;
12455   /* Look for the string.  */
12456   string = cp_parser_string_literal (parser, false, false);
12457   if (string == error_mark_node)
12458     {
12459       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12460                                              /*consume_paren=*/true);
12461       return;
12462     }
12463
12464   /* If we're allowing GNU extensions, check for the extended assembly
12465      syntax.  Unfortunately, the `:' tokens need not be separated by
12466      a space in C, and so, for compatibility, we tolerate that here
12467      too.  Doing that means that we have to treat the `::' operator as
12468      two `:' tokens.  */
12469   if (cp_parser_allow_gnu_extensions_p (parser)
12470       && parser->in_function_body
12471       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12472           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12473     {
12474       bool inputs_p = false;
12475       bool clobbers_p = false;
12476
12477       /* The extended syntax was used.  */
12478       extended_p = true;
12479
12480       /* Look for outputs.  */
12481       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12482         {
12483           /* Consume the `:'.  */
12484           cp_lexer_consume_token (parser->lexer);
12485           /* Parse the output-operands.  */
12486           if (cp_lexer_next_token_is_not (parser->lexer,
12487                                           CPP_COLON)
12488               && cp_lexer_next_token_is_not (parser->lexer,
12489                                              CPP_SCOPE)
12490               && cp_lexer_next_token_is_not (parser->lexer,
12491                                              CPP_CLOSE_PAREN))
12492             outputs = cp_parser_asm_operand_list (parser);
12493
12494             if (outputs == error_mark_node)
12495               invalid_outputs_p = true;
12496         }
12497       /* If the next token is `::', there are no outputs, and the
12498          next token is the beginning of the inputs.  */
12499       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12500         /* The inputs are coming next.  */
12501         inputs_p = true;
12502
12503       /* Look for inputs.  */
12504       if (inputs_p
12505           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12506         {
12507           /* Consume the `:' or `::'.  */
12508           cp_lexer_consume_token (parser->lexer);
12509           /* Parse the output-operands.  */
12510           if (cp_lexer_next_token_is_not (parser->lexer,
12511                                           CPP_COLON)
12512               && cp_lexer_next_token_is_not (parser->lexer,
12513                                              CPP_CLOSE_PAREN))
12514             inputs = cp_parser_asm_operand_list (parser);
12515
12516             if (inputs == error_mark_node)
12517               invalid_inputs_p = true;
12518         }
12519       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12520         /* The clobbers are coming next.  */
12521         clobbers_p = true;
12522
12523       /* Look for clobbers.  */
12524       if (clobbers_p
12525           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12526         {
12527           /* Consume the `:' or `::'.  */
12528           cp_lexer_consume_token (parser->lexer);
12529           /* Parse the clobbers.  */
12530           if (cp_lexer_next_token_is_not (parser->lexer,
12531                                           CPP_CLOSE_PAREN))
12532             clobbers = cp_parser_asm_clobber_list (parser);
12533         }
12534     }
12535   /* Look for the closing `)'.  */
12536   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12537     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12538                                            /*consume_paren=*/true);
12539   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12540
12541   if (!invalid_inputs_p && !invalid_outputs_p)
12542     {
12543       /* Create the ASM_EXPR.  */
12544       if (parser->in_function_body)
12545         {
12546           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12547                                       inputs, clobbers);
12548           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12549           if (!extended_p)
12550             {
12551               tree temp = asm_stmt;
12552               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12553                 temp = TREE_OPERAND (temp, 0);
12554
12555               ASM_INPUT_P (temp) = 1;
12556             }
12557         }
12558       else
12559         cgraph_add_asm_node (string);
12560     }
12561 }
12562
12563 /* Declarators [gram.dcl.decl] */
12564
12565 /* Parse an init-declarator.
12566
12567    init-declarator:
12568      declarator initializer [opt]
12569
12570    GNU Extension:
12571
12572    init-declarator:
12573      declarator asm-specification [opt] attributes [opt] initializer [opt]
12574
12575    function-definition:
12576      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12577        function-body
12578      decl-specifier-seq [opt] declarator function-try-block
12579
12580    GNU Extension:
12581
12582    function-definition:
12583      __extension__ function-definition
12584
12585    The DECL_SPECIFIERS apply to this declarator.  Returns a
12586    representation of the entity declared.  If MEMBER_P is TRUE, then
12587    this declarator appears in a class scope.  The new DECL created by
12588    this declarator is returned.
12589
12590    The CHECKS are access checks that should be performed once we know
12591    what entity is being declared (and, therefore, what classes have
12592    befriended it).
12593
12594    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12595    for a function-definition here as well.  If the declarator is a
12596    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12597    be TRUE upon return.  By that point, the function-definition will
12598    have been completely parsed.
12599
12600    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12601    is FALSE.  */
12602
12603 static tree
12604 cp_parser_init_declarator (cp_parser* parser,
12605                            cp_decl_specifier_seq *decl_specifiers,
12606                            VEC (deferred_access_check,gc)* checks,
12607                            bool function_definition_allowed_p,
12608                            bool member_p,
12609                            int declares_class_or_enum,
12610                            bool* function_definition_p)
12611 {
12612   cp_token *token = NULL, *asm_spec_start_token = NULL,
12613            *attributes_start_token = NULL;
12614   cp_declarator *declarator;
12615   tree prefix_attributes;
12616   tree attributes;
12617   tree asm_specification;
12618   tree initializer;
12619   tree decl = NULL_TREE;
12620   tree scope;
12621   int is_initialized;
12622   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12623      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12624      "(...)".  */
12625   enum cpp_ttype initialization_kind;
12626   bool is_direct_init = false;
12627   bool is_non_constant_init;
12628   int ctor_dtor_or_conv_p;
12629   bool friend_p;
12630   tree pushed_scope = NULL;
12631
12632   /* Gather the attributes that were provided with the
12633      decl-specifiers.  */
12634   prefix_attributes = decl_specifiers->attributes;
12635
12636   /* Assume that this is not the declarator for a function
12637      definition.  */
12638   if (function_definition_p)
12639     *function_definition_p = false;
12640
12641   /* Defer access checks while parsing the declarator; we cannot know
12642      what names are accessible until we know what is being
12643      declared.  */
12644   resume_deferring_access_checks ();
12645
12646   /* Parse the declarator.  */
12647   token = cp_lexer_peek_token (parser->lexer);
12648   declarator
12649     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12650                             &ctor_dtor_or_conv_p,
12651                             /*parenthesized_p=*/NULL,
12652                             /*member_p=*/false);
12653   /* Gather up the deferred checks.  */
12654   stop_deferring_access_checks ();
12655
12656   /* If the DECLARATOR was erroneous, there's no need to go
12657      further.  */
12658   if (declarator == cp_error_declarator)
12659     return error_mark_node;
12660
12661   /* Check that the number of template-parameter-lists is OK.  */
12662   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12663                                                        token->location))
12664     return error_mark_node;
12665
12666   if (declares_class_or_enum & 2)
12667     cp_parser_check_for_definition_in_return_type (declarator,
12668                                                    decl_specifiers->type,
12669                                                    decl_specifiers->type_location);
12670
12671   /* Figure out what scope the entity declared by the DECLARATOR is
12672      located in.  `grokdeclarator' sometimes changes the scope, so
12673      we compute it now.  */
12674   scope = get_scope_of_declarator (declarator);
12675
12676   /* If we're allowing GNU extensions, look for an asm-specification
12677      and attributes.  */
12678   if (cp_parser_allow_gnu_extensions_p (parser))
12679     {
12680       /* Look for an asm-specification.  */
12681       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12682       asm_specification = cp_parser_asm_specification_opt (parser);
12683       /* And attributes.  */
12684       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12685       attributes = cp_parser_attributes_opt (parser);
12686     }
12687   else
12688     {
12689       asm_specification = NULL_TREE;
12690       attributes = NULL_TREE;
12691     }
12692
12693   /* Peek at the next token.  */
12694   token = cp_lexer_peek_token (parser->lexer);
12695   /* Check to see if the token indicates the start of a
12696      function-definition.  */
12697   if (function_declarator_p (declarator)
12698       && cp_parser_token_starts_function_definition_p (token))
12699     {
12700       if (!function_definition_allowed_p)
12701         {
12702           /* If a function-definition should not appear here, issue an
12703              error message.  */
12704           cp_parser_error (parser,
12705                            "a function-definition is not allowed here");
12706           return error_mark_node;
12707         }
12708       else
12709         {
12710           location_t func_brace_location
12711             = cp_lexer_peek_token (parser->lexer)->location;
12712
12713           /* Neither attributes nor an asm-specification are allowed
12714              on a function-definition.  */
12715           if (asm_specification)
12716             error ("%Han asm-specification is not allowed "
12717                    "on a function-definition",
12718                    &asm_spec_start_token->location);
12719           if (attributes)
12720             error ("%Hattributes are not allowed on a function-definition",
12721                    &attributes_start_token->location);
12722           /* This is a function-definition.  */
12723           *function_definition_p = true;
12724
12725           /* Parse the function definition.  */
12726           if (member_p)
12727             decl = cp_parser_save_member_function_body (parser,
12728                                                         decl_specifiers,
12729                                                         declarator,
12730                                                         prefix_attributes);
12731           else
12732             decl
12733               = (cp_parser_function_definition_from_specifiers_and_declarator
12734                  (parser, decl_specifiers, prefix_attributes, declarator));
12735
12736           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12737             {
12738               /* This is where the prologue starts...  */
12739               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12740                 = func_brace_location;
12741             }
12742
12743           return decl;
12744         }
12745     }
12746
12747   /* [dcl.dcl]
12748
12749      Only in function declarations for constructors, destructors, and
12750      type conversions can the decl-specifier-seq be omitted.
12751
12752      We explicitly postpone this check past the point where we handle
12753      function-definitions because we tolerate function-definitions
12754      that are missing their return types in some modes.  */
12755   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12756     {
12757       cp_parser_error (parser,
12758                        "expected constructor, destructor, or type conversion");
12759       return error_mark_node;
12760     }
12761
12762   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12763   if (token->type == CPP_EQ
12764       || token->type == CPP_OPEN_PAREN
12765       || token->type == CPP_OPEN_BRACE)
12766     {
12767       is_initialized = SD_INITIALIZED;
12768       initialization_kind = token->type;
12769
12770       if (token->type == CPP_EQ
12771           && function_declarator_p (declarator))
12772         {
12773           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12774           if (t2->keyword == RID_DEFAULT)
12775             is_initialized = SD_DEFAULTED;
12776           else if (t2->keyword == RID_DELETE)
12777             is_initialized = SD_DELETED;
12778         }
12779     }
12780   else
12781     {
12782       /* If the init-declarator isn't initialized and isn't followed by a
12783          `,' or `;', it's not a valid init-declarator.  */
12784       if (token->type != CPP_COMMA
12785           && token->type != CPP_SEMICOLON)
12786         {
12787           cp_parser_error (parser, "expected initializer");
12788           return error_mark_node;
12789         }
12790       is_initialized = SD_UNINITIALIZED;
12791       initialization_kind = CPP_EOF;
12792     }
12793
12794   /* Because start_decl has side-effects, we should only call it if we
12795      know we're going ahead.  By this point, we know that we cannot
12796      possibly be looking at any other construct.  */
12797   cp_parser_commit_to_tentative_parse (parser);
12798
12799   /* If the decl specifiers were bad, issue an error now that we're
12800      sure this was intended to be a declarator.  Then continue
12801      declaring the variable(s), as int, to try to cut down on further
12802      errors.  */
12803   if (decl_specifiers->any_specifiers_p
12804       && decl_specifiers->type == error_mark_node)
12805     {
12806       cp_parser_error (parser, "invalid type in declaration");
12807       decl_specifiers->type = integer_type_node;
12808     }
12809
12810   /* Check to see whether or not this declaration is a friend.  */
12811   friend_p = cp_parser_friend_p (decl_specifiers);
12812
12813   /* Enter the newly declared entry in the symbol table.  If we're
12814      processing a declaration in a class-specifier, we wait until
12815      after processing the initializer.  */
12816   if (!member_p)
12817     {
12818       if (parser->in_unbraced_linkage_specification_p)
12819         decl_specifiers->storage_class = sc_extern;
12820       decl = start_decl (declarator, decl_specifiers,
12821                          is_initialized, attributes, prefix_attributes,
12822                          &pushed_scope);
12823     }
12824   else if (scope)
12825     /* Enter the SCOPE.  That way unqualified names appearing in the
12826        initializer will be looked up in SCOPE.  */
12827     pushed_scope = push_scope (scope);
12828
12829   /* Perform deferred access control checks, now that we know in which
12830      SCOPE the declared entity resides.  */
12831   if (!member_p && decl)
12832     {
12833       tree saved_current_function_decl = NULL_TREE;
12834
12835       /* If the entity being declared is a function, pretend that we
12836          are in its scope.  If it is a `friend', it may have access to
12837          things that would not otherwise be accessible.  */
12838       if (TREE_CODE (decl) == FUNCTION_DECL)
12839         {
12840           saved_current_function_decl = current_function_decl;
12841           current_function_decl = decl;
12842         }
12843
12844       /* Perform access checks for template parameters.  */
12845       cp_parser_perform_template_parameter_access_checks (checks);
12846
12847       /* Perform the access control checks for the declarator and the
12848          decl-specifiers.  */
12849       perform_deferred_access_checks ();
12850
12851       /* Restore the saved value.  */
12852       if (TREE_CODE (decl) == FUNCTION_DECL)
12853         current_function_decl = saved_current_function_decl;
12854     }
12855
12856   /* Parse the initializer.  */
12857   initializer = NULL_TREE;
12858   is_direct_init = false;
12859   is_non_constant_init = true;
12860   if (is_initialized)
12861     {
12862       if (function_declarator_p (declarator))
12863         {
12864           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12865            if (initialization_kind == CPP_EQ)
12866              initializer = cp_parser_pure_specifier (parser);
12867            else
12868              {
12869                /* If the declaration was erroneous, we don't really
12870                   know what the user intended, so just silently
12871                   consume the initializer.  */
12872                if (decl != error_mark_node)
12873                  error ("%Hinitializer provided for function",
12874                         &initializer_start_token->location);
12875                cp_parser_skip_to_closing_parenthesis (parser,
12876                                                       /*recovering=*/true,
12877                                                       /*or_comma=*/false,
12878                                                       /*consume_paren=*/true);
12879              }
12880         }
12881       else
12882         initializer = cp_parser_initializer (parser,
12883                                              &is_direct_init,
12884                                              &is_non_constant_init);
12885     }
12886
12887   /* The old parser allows attributes to appear after a parenthesized
12888      initializer.  Mark Mitchell proposed removing this functionality
12889      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12890      attributes -- but ignores them.  */
12891   if (cp_parser_allow_gnu_extensions_p (parser)
12892       && initialization_kind == CPP_OPEN_PAREN)
12893     if (cp_parser_attributes_opt (parser))
12894       warning (OPT_Wattributes,
12895                "attributes after parenthesized initializer ignored");
12896
12897   /* For an in-class declaration, use `grokfield' to create the
12898      declaration.  */
12899   if (member_p)
12900     {
12901       if (pushed_scope)
12902         {
12903           pop_scope (pushed_scope);
12904           pushed_scope = false;
12905         }
12906       decl = grokfield (declarator, decl_specifiers,
12907                         initializer, !is_non_constant_init,
12908                         /*asmspec=*/NULL_TREE,
12909                         prefix_attributes);
12910       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12911         cp_parser_save_default_args (parser, decl);
12912     }
12913
12914   /* Finish processing the declaration.  But, skip friend
12915      declarations.  */
12916   if (!friend_p && decl && decl != error_mark_node)
12917     {
12918       cp_finish_decl (decl,
12919                       initializer, !is_non_constant_init,
12920                       asm_specification,
12921                       /* If the initializer is in parentheses, then this is
12922                          a direct-initialization, which means that an
12923                          `explicit' constructor is OK.  Otherwise, an
12924                          `explicit' constructor cannot be used.  */
12925                       ((is_direct_init || !is_initialized)
12926                        ? 0 : LOOKUP_ONLYCONVERTING));
12927     }
12928   else if ((cxx_dialect != cxx98) && friend_p
12929            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12930     /* Core issue #226 (C++0x only): A default template-argument
12931        shall not be specified in a friend class template
12932        declaration. */
12933     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12934                              /*is_partial=*/0, /*is_friend_decl=*/1);
12935
12936   if (!friend_p && pushed_scope)
12937     pop_scope (pushed_scope);
12938
12939   return decl;
12940 }
12941
12942 /* Parse a declarator.
12943
12944    declarator:
12945      direct-declarator
12946      ptr-operator declarator
12947
12948    abstract-declarator:
12949      ptr-operator abstract-declarator [opt]
12950      direct-abstract-declarator
12951
12952    GNU Extensions:
12953
12954    declarator:
12955      attributes [opt] direct-declarator
12956      attributes [opt] ptr-operator declarator
12957
12958    abstract-declarator:
12959      attributes [opt] ptr-operator abstract-declarator [opt]
12960      attributes [opt] direct-abstract-declarator
12961
12962    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12963    detect constructor, destructor or conversion operators. It is set
12964    to -1 if the declarator is a name, and +1 if it is a
12965    function. Otherwise it is set to zero. Usually you just want to
12966    test for >0, but internally the negative value is used.
12967
12968    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12969    a decl-specifier-seq unless it declares a constructor, destructor,
12970    or conversion.  It might seem that we could check this condition in
12971    semantic analysis, rather than parsing, but that makes it difficult
12972    to handle something like `f()'.  We want to notice that there are
12973    no decl-specifiers, and therefore realize that this is an
12974    expression, not a declaration.)
12975
12976    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12977    the declarator is a direct-declarator of the form "(...)".
12978
12979    MEMBER_P is true iff this declarator is a member-declarator.  */
12980
12981 static cp_declarator *
12982 cp_parser_declarator (cp_parser* parser,
12983                       cp_parser_declarator_kind dcl_kind,
12984                       int* ctor_dtor_or_conv_p,
12985                       bool* parenthesized_p,
12986                       bool member_p)
12987 {
12988   cp_token *token;
12989   cp_declarator *declarator;
12990   enum tree_code code;
12991   cp_cv_quals cv_quals;
12992   tree class_type;
12993   tree attributes = NULL_TREE;
12994
12995   /* Assume this is not a constructor, destructor, or type-conversion
12996      operator.  */
12997   if (ctor_dtor_or_conv_p)
12998     *ctor_dtor_or_conv_p = 0;
12999
13000   if (cp_parser_allow_gnu_extensions_p (parser))
13001     attributes = cp_parser_attributes_opt (parser);
13002
13003   /* Peek at the next token.  */
13004   token = cp_lexer_peek_token (parser->lexer);
13005
13006   /* Check for the ptr-operator production.  */
13007   cp_parser_parse_tentatively (parser);
13008   /* Parse the ptr-operator.  */
13009   code = cp_parser_ptr_operator (parser,
13010                                  &class_type,
13011                                  &cv_quals);
13012   /* If that worked, then we have a ptr-operator.  */
13013   if (cp_parser_parse_definitely (parser))
13014     {
13015       /* If a ptr-operator was found, then this declarator was not
13016          parenthesized.  */
13017       if (parenthesized_p)
13018         *parenthesized_p = true;
13019       /* The dependent declarator is optional if we are parsing an
13020          abstract-declarator.  */
13021       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13022         cp_parser_parse_tentatively (parser);
13023
13024       /* Parse the dependent declarator.  */
13025       declarator = cp_parser_declarator (parser, dcl_kind,
13026                                          /*ctor_dtor_or_conv_p=*/NULL,
13027                                          /*parenthesized_p=*/NULL,
13028                                          /*member_p=*/false);
13029
13030       /* If we are parsing an abstract-declarator, we must handle the
13031          case where the dependent declarator is absent.  */
13032       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13033           && !cp_parser_parse_definitely (parser))
13034         declarator = NULL;
13035
13036       declarator = cp_parser_make_indirect_declarator
13037         (code, class_type, cv_quals, declarator);
13038     }
13039   /* Everything else is a direct-declarator.  */
13040   else
13041     {
13042       if (parenthesized_p)
13043         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13044                                                    CPP_OPEN_PAREN);
13045       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13046                                                 ctor_dtor_or_conv_p,
13047                                                 member_p);
13048     }
13049
13050   if (attributes && declarator && declarator != cp_error_declarator)
13051     declarator->attributes = attributes;
13052
13053   return declarator;
13054 }
13055
13056 /* Parse a direct-declarator or direct-abstract-declarator.
13057
13058    direct-declarator:
13059      declarator-id
13060      direct-declarator ( parameter-declaration-clause )
13061        cv-qualifier-seq [opt]
13062        exception-specification [opt]
13063      direct-declarator [ constant-expression [opt] ]
13064      ( declarator )
13065
13066    direct-abstract-declarator:
13067      direct-abstract-declarator [opt]
13068        ( parameter-declaration-clause )
13069        cv-qualifier-seq [opt]
13070        exception-specification [opt]
13071      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13072      ( abstract-declarator )
13073
13074    Returns a representation of the declarator.  DCL_KIND is
13075    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13076    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13077    we are parsing a direct-declarator.  It is
13078    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13079    of ambiguity we prefer an abstract declarator, as per
13080    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13081    cp_parser_declarator.  */
13082
13083 static cp_declarator *
13084 cp_parser_direct_declarator (cp_parser* parser,
13085                              cp_parser_declarator_kind dcl_kind,
13086                              int* ctor_dtor_or_conv_p,
13087                              bool member_p)
13088 {
13089   cp_token *token;
13090   cp_declarator *declarator = NULL;
13091   tree scope = NULL_TREE;
13092   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13093   bool saved_in_declarator_p = parser->in_declarator_p;
13094   bool first = true;
13095   tree pushed_scope = NULL_TREE;
13096
13097   while (true)
13098     {
13099       /* Peek at the next token.  */
13100       token = cp_lexer_peek_token (parser->lexer);
13101       if (token->type == CPP_OPEN_PAREN)
13102         {
13103           /* This is either a parameter-declaration-clause, or a
13104              parenthesized declarator. When we know we are parsing a
13105              named declarator, it must be a parenthesized declarator
13106              if FIRST is true. For instance, `(int)' is a
13107              parameter-declaration-clause, with an omitted
13108              direct-abstract-declarator. But `((*))', is a
13109              parenthesized abstract declarator. Finally, when T is a
13110              template parameter `(T)' is a
13111              parameter-declaration-clause, and not a parenthesized
13112              named declarator.
13113
13114              We first try and parse a parameter-declaration-clause,
13115              and then try a nested declarator (if FIRST is true).
13116
13117              It is not an error for it not to be a
13118              parameter-declaration-clause, even when FIRST is
13119              false. Consider,
13120
13121                int i (int);
13122                int i (3);
13123
13124              The first is the declaration of a function while the
13125              second is the definition of a variable, including its
13126              initializer.
13127
13128              Having seen only the parenthesis, we cannot know which of
13129              these two alternatives should be selected.  Even more
13130              complex are examples like:
13131
13132                int i (int (a));
13133                int i (int (3));
13134
13135              The former is a function-declaration; the latter is a
13136              variable initialization.
13137
13138              Thus again, we try a parameter-declaration-clause, and if
13139              that fails, we back out and return.  */
13140
13141           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13142             {
13143               tree params;
13144               unsigned saved_num_template_parameter_lists;
13145               bool is_declarator = false;
13146               tree t;
13147
13148               /* In a member-declarator, the only valid interpretation
13149                  of a parenthesis is the start of a
13150                  parameter-declaration-clause.  (It is invalid to
13151                  initialize a static data member with a parenthesized
13152                  initializer; only the "=" form of initialization is
13153                  permitted.)  */
13154               if (!member_p)
13155                 cp_parser_parse_tentatively (parser);
13156
13157               /* Consume the `('.  */
13158               cp_lexer_consume_token (parser->lexer);
13159               if (first)
13160                 {
13161                   /* If this is going to be an abstract declarator, we're
13162                      in a declarator and we can't have default args.  */
13163                   parser->default_arg_ok_p = false;
13164                   parser->in_declarator_p = true;
13165                 }
13166
13167               /* Inside the function parameter list, surrounding
13168                  template-parameter-lists do not apply.  */
13169               saved_num_template_parameter_lists
13170                 = parser->num_template_parameter_lists;
13171               parser->num_template_parameter_lists = 0;
13172
13173               begin_scope (sk_function_parms, NULL_TREE);
13174
13175               /* Parse the parameter-declaration-clause.  */
13176               params = cp_parser_parameter_declaration_clause (parser);
13177
13178               parser->num_template_parameter_lists
13179                 = saved_num_template_parameter_lists;
13180
13181               /* If all went well, parse the cv-qualifier-seq and the
13182                  exception-specification.  */
13183               if (member_p || cp_parser_parse_definitely (parser))
13184                 {
13185                   cp_cv_quals cv_quals;
13186                   tree exception_specification;
13187                   tree late_return;
13188
13189                   is_declarator = true;
13190
13191                   if (ctor_dtor_or_conv_p)
13192                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13193                   first = false;
13194                   /* Consume the `)'.  */
13195                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13196
13197                   /* Parse the cv-qualifier-seq.  */
13198                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13199                   /* And the exception-specification.  */
13200                   exception_specification
13201                     = cp_parser_exception_specification_opt (parser);
13202
13203                   late_return
13204                     = cp_parser_late_return_type_opt (parser);
13205
13206                   /* Create the function-declarator.  */
13207                   declarator = make_call_declarator (declarator,
13208                                                      params,
13209                                                      cv_quals,
13210                                                      exception_specification,
13211                                                      late_return);
13212                   /* Any subsequent parameter lists are to do with
13213                      return type, so are not those of the declared
13214                      function.  */
13215                   parser->default_arg_ok_p = false;
13216                 }
13217
13218               /* Remove the function parms from scope.  */
13219               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13220                 pop_binding (DECL_NAME (t), t);
13221               leave_scope();
13222
13223               if (is_declarator)
13224                 /* Repeat the main loop.  */
13225                 continue;
13226             }
13227
13228           /* If this is the first, we can try a parenthesized
13229              declarator.  */
13230           if (first)
13231             {
13232               bool saved_in_type_id_in_expr_p;
13233
13234               parser->default_arg_ok_p = saved_default_arg_ok_p;
13235               parser->in_declarator_p = saved_in_declarator_p;
13236
13237               /* Consume the `('.  */
13238               cp_lexer_consume_token (parser->lexer);
13239               /* Parse the nested declarator.  */
13240               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13241               parser->in_type_id_in_expr_p = true;
13242               declarator
13243                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13244                                         /*parenthesized_p=*/NULL,
13245                                         member_p);
13246               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13247               first = false;
13248               /* Expect a `)'.  */
13249               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13250                 declarator = cp_error_declarator;
13251               if (declarator == cp_error_declarator)
13252                 break;
13253
13254               goto handle_declarator;
13255             }
13256           /* Otherwise, we must be done.  */
13257           else
13258             break;
13259         }
13260       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13261                && token->type == CPP_OPEN_SQUARE)
13262         {
13263           /* Parse an array-declarator.  */
13264           tree bounds;
13265
13266           if (ctor_dtor_or_conv_p)
13267             *ctor_dtor_or_conv_p = 0;
13268
13269           first = false;
13270           parser->default_arg_ok_p = false;
13271           parser->in_declarator_p = true;
13272           /* Consume the `['.  */
13273           cp_lexer_consume_token (parser->lexer);
13274           /* Peek at the next token.  */
13275           token = cp_lexer_peek_token (parser->lexer);
13276           /* If the next token is `]', then there is no
13277              constant-expression.  */
13278           if (token->type != CPP_CLOSE_SQUARE)
13279             {
13280               bool non_constant_p;
13281
13282               bounds
13283                 = cp_parser_constant_expression (parser,
13284                                                  /*allow_non_constant=*/true,
13285                                                  &non_constant_p);
13286               if (!non_constant_p)
13287                 bounds = fold_non_dependent_expr (bounds);
13288               else if (processing_template_decl)
13289                 {
13290                   /* Remember this wasn't a constant-expression.  */
13291                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13292                   TREE_SIDE_EFFECTS (bounds) = 1;
13293                 }
13294
13295               /* Normally, the array bound must be an integral constant
13296                  expression.  However, as an extension, we allow VLAs
13297                  in function scopes.  */
13298               else if (!parser->in_function_body)
13299                 {
13300                   error ("%Harray bound is not an integer constant",
13301                          &token->location);
13302                   bounds = error_mark_node;
13303                 }
13304             }
13305           else
13306             bounds = NULL_TREE;
13307           /* Look for the closing `]'.  */
13308           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13309             {
13310               declarator = cp_error_declarator;
13311               break;
13312             }
13313
13314           declarator = make_array_declarator (declarator, bounds);
13315         }
13316       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13317         {
13318           tree qualifying_scope;
13319           tree unqualified_name;
13320           special_function_kind sfk;
13321           bool abstract_ok;
13322           bool pack_expansion_p = false;
13323           cp_token *declarator_id_start_token;
13324
13325           /* Parse a declarator-id */
13326           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13327           if (abstract_ok)
13328             {
13329               cp_parser_parse_tentatively (parser);
13330
13331               /* If we see an ellipsis, we should be looking at a
13332                  parameter pack. */
13333               if (token->type == CPP_ELLIPSIS)
13334                 {
13335                   /* Consume the `...' */
13336                   cp_lexer_consume_token (parser->lexer);
13337
13338                   pack_expansion_p = true;
13339                 }
13340             }
13341
13342           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13343           unqualified_name
13344             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13345           qualifying_scope = parser->scope;
13346           if (abstract_ok)
13347             {
13348               bool okay = false;
13349
13350               if (!unqualified_name && pack_expansion_p)
13351                 {
13352                   /* Check whether an error occurred. */
13353                   okay = !cp_parser_error_occurred (parser);
13354
13355                   /* We already consumed the ellipsis to mark a
13356                      parameter pack, but we have no way to report it,
13357                      so abort the tentative parse. We will be exiting
13358                      immediately anyway. */
13359                   cp_parser_abort_tentative_parse (parser);
13360                 }
13361               else
13362                 okay = cp_parser_parse_definitely (parser);
13363
13364               if (!okay)
13365                 unqualified_name = error_mark_node;
13366               else if (unqualified_name
13367                        && (qualifying_scope
13368                            || (TREE_CODE (unqualified_name)
13369                                != IDENTIFIER_NODE)))
13370                 {
13371                   cp_parser_error (parser, "expected unqualified-id");
13372                   unqualified_name = error_mark_node;
13373                 }
13374             }
13375
13376           if (!unqualified_name)
13377             return NULL;
13378           if (unqualified_name == error_mark_node)
13379             {
13380               declarator = cp_error_declarator;
13381               pack_expansion_p = false;
13382               declarator->parameter_pack_p = false;
13383               break;
13384             }
13385
13386           if (qualifying_scope && at_namespace_scope_p ()
13387               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13388             {
13389               /* In the declaration of a member of a template class
13390                  outside of the class itself, the SCOPE will sometimes
13391                  be a TYPENAME_TYPE.  For example, given:
13392
13393                  template <typename T>
13394                  int S<T>::R::i = 3;
13395
13396                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13397                  this context, we must resolve S<T>::R to an ordinary
13398                  type, rather than a typename type.
13399
13400                  The reason we normally avoid resolving TYPENAME_TYPEs
13401                  is that a specialization of `S' might render
13402                  `S<T>::R' not a type.  However, if `S' is
13403                  specialized, then this `i' will not be used, so there
13404                  is no harm in resolving the types here.  */
13405               tree type;
13406
13407               /* Resolve the TYPENAME_TYPE.  */
13408               type = resolve_typename_type (qualifying_scope,
13409                                             /*only_current_p=*/false);
13410               /* If that failed, the declarator is invalid.  */
13411               if (TREE_CODE (type) == TYPENAME_TYPE)
13412                 error ("%H%<%T::%E%> is not a type",
13413                        &declarator_id_start_token->location,
13414                        TYPE_CONTEXT (qualifying_scope),
13415                        TYPE_IDENTIFIER (qualifying_scope));
13416               qualifying_scope = type;
13417             }
13418
13419           sfk = sfk_none;
13420
13421           if (unqualified_name)
13422             {
13423               tree class_type;
13424
13425               if (qualifying_scope
13426                   && CLASS_TYPE_P (qualifying_scope))
13427                 class_type = qualifying_scope;
13428               else
13429                 class_type = current_class_type;
13430
13431               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13432                 {
13433                   tree name_type = TREE_TYPE (unqualified_name);
13434                   if (class_type && same_type_p (name_type, class_type))
13435                     {
13436                       if (qualifying_scope
13437                           && CLASSTYPE_USE_TEMPLATE (name_type))
13438                         {
13439                           error ("%Hinvalid use of constructor as a template",
13440                                  &declarator_id_start_token->location);
13441                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13442                                   "name the constructor in a qualified name",
13443                                   class_type,
13444                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13445                                   class_type, name_type);
13446                           declarator = cp_error_declarator;
13447                           break;
13448                         }
13449                       else
13450                         unqualified_name = constructor_name (class_type);
13451                     }
13452                   else
13453                     {
13454                       /* We do not attempt to print the declarator
13455                          here because we do not have enough
13456                          information about its original syntactic
13457                          form.  */
13458                       cp_parser_error (parser, "invalid declarator");
13459                       declarator = cp_error_declarator;
13460                       break;
13461                     }
13462                 }
13463
13464               if (class_type)
13465                 {
13466                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13467                     sfk = sfk_destructor;
13468                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13469                     sfk = sfk_conversion;
13470                   else if (/* There's no way to declare a constructor
13471                               for an anonymous type, even if the type
13472                               got a name for linkage purposes.  */
13473                            !TYPE_WAS_ANONYMOUS (class_type)
13474                            && constructor_name_p (unqualified_name,
13475                                                   class_type))
13476                     {
13477                       unqualified_name = constructor_name (class_type);
13478                       sfk = sfk_constructor;
13479                     }
13480
13481                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13482                     *ctor_dtor_or_conv_p = -1;
13483                 }
13484             }
13485           declarator = make_id_declarator (qualifying_scope,
13486                                            unqualified_name,
13487                                            sfk);
13488           declarator->id_loc = token->location;
13489           declarator->parameter_pack_p = pack_expansion_p;
13490
13491           if (pack_expansion_p)
13492             maybe_warn_variadic_templates ();
13493
13494         handle_declarator:;
13495           scope = get_scope_of_declarator (declarator);
13496           if (scope)
13497             /* Any names that appear after the declarator-id for a
13498                member are looked up in the containing scope.  */
13499             pushed_scope = push_scope (scope);
13500           parser->in_declarator_p = true;
13501           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13502               || (declarator && declarator->kind == cdk_id))
13503             /* Default args are only allowed on function
13504                declarations.  */
13505             parser->default_arg_ok_p = saved_default_arg_ok_p;
13506           else
13507             parser->default_arg_ok_p = false;
13508
13509           first = false;
13510         }
13511       /* We're done.  */
13512       else
13513         break;
13514     }
13515
13516   /* For an abstract declarator, we might wind up with nothing at this
13517      point.  That's an error; the declarator is not optional.  */
13518   if (!declarator)
13519     cp_parser_error (parser, "expected declarator");
13520
13521   /* If we entered a scope, we must exit it now.  */
13522   if (pushed_scope)
13523     pop_scope (pushed_scope);
13524
13525   parser->default_arg_ok_p = saved_default_arg_ok_p;
13526   parser->in_declarator_p = saved_in_declarator_p;
13527
13528   return declarator;
13529 }
13530
13531 /* Parse a ptr-operator.
13532
13533    ptr-operator:
13534      * cv-qualifier-seq [opt]
13535      &
13536      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13537
13538    GNU Extension:
13539
13540    ptr-operator:
13541      & cv-qualifier-seq [opt]
13542
13543    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13544    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13545    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13546    filled in with the TYPE containing the member.  *CV_QUALS is
13547    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13548    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13549    Note that the tree codes returned by this function have nothing
13550    to do with the types of trees that will be eventually be created
13551    to represent the pointer or reference type being parsed. They are
13552    just constants with suggestive names. */
13553 static enum tree_code
13554 cp_parser_ptr_operator (cp_parser* parser,
13555                         tree* type,
13556                         cp_cv_quals *cv_quals)
13557 {
13558   enum tree_code code = ERROR_MARK;
13559   cp_token *token;
13560
13561   /* Assume that it's not a pointer-to-member.  */
13562   *type = NULL_TREE;
13563   /* And that there are no cv-qualifiers.  */
13564   *cv_quals = TYPE_UNQUALIFIED;
13565
13566   /* Peek at the next token.  */
13567   token = cp_lexer_peek_token (parser->lexer);
13568
13569   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13570   if (token->type == CPP_MULT)
13571     code = INDIRECT_REF;
13572   else if (token->type == CPP_AND)
13573     code = ADDR_EXPR;
13574   else if ((cxx_dialect != cxx98) &&
13575            token->type == CPP_AND_AND) /* C++0x only */
13576     code = NON_LVALUE_EXPR;
13577
13578   if (code != ERROR_MARK)
13579     {
13580       /* Consume the `*', `&' or `&&'.  */
13581       cp_lexer_consume_token (parser->lexer);
13582
13583       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13584          `&', if we are allowing GNU extensions.  (The only qualifier
13585          that can legally appear after `&' is `restrict', but that is
13586          enforced during semantic analysis.  */
13587       if (code == INDIRECT_REF
13588           || cp_parser_allow_gnu_extensions_p (parser))
13589         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13590     }
13591   else
13592     {
13593       /* Try the pointer-to-member case.  */
13594       cp_parser_parse_tentatively (parser);
13595       /* Look for the optional `::' operator.  */
13596       cp_parser_global_scope_opt (parser,
13597                                   /*current_scope_valid_p=*/false);
13598       /* Look for the nested-name specifier.  */
13599       token = cp_lexer_peek_token (parser->lexer);
13600       cp_parser_nested_name_specifier (parser,
13601                                        /*typename_keyword_p=*/false,
13602                                        /*check_dependency_p=*/true,
13603                                        /*type_p=*/false,
13604                                        /*is_declaration=*/false);
13605       /* If we found it, and the next token is a `*', then we are
13606          indeed looking at a pointer-to-member operator.  */
13607       if (!cp_parser_error_occurred (parser)
13608           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13609         {
13610           /* Indicate that the `*' operator was used.  */
13611           code = INDIRECT_REF;
13612
13613           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13614             error ("%H%qD is a namespace", &token->location, parser->scope);
13615           else
13616             {
13617               /* The type of which the member is a member is given by the
13618                  current SCOPE.  */
13619               *type = parser->scope;
13620               /* The next name will not be qualified.  */
13621               parser->scope = NULL_TREE;
13622               parser->qualifying_scope = NULL_TREE;
13623               parser->object_scope = NULL_TREE;
13624               /* Look for the optional cv-qualifier-seq.  */
13625               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13626             }
13627         }
13628       /* If that didn't work we don't have a ptr-operator.  */
13629       if (!cp_parser_parse_definitely (parser))
13630         cp_parser_error (parser, "expected ptr-operator");
13631     }
13632
13633   return code;
13634 }
13635
13636 /* Parse an (optional) cv-qualifier-seq.
13637
13638    cv-qualifier-seq:
13639      cv-qualifier cv-qualifier-seq [opt]
13640
13641    cv-qualifier:
13642      const
13643      volatile
13644
13645    GNU Extension:
13646
13647    cv-qualifier:
13648      __restrict__
13649
13650    Returns a bitmask representing the cv-qualifiers.  */
13651
13652 static cp_cv_quals
13653 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13654 {
13655   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13656
13657   while (true)
13658     {
13659       cp_token *token;
13660       cp_cv_quals cv_qualifier;
13661
13662       /* Peek at the next token.  */
13663       token = cp_lexer_peek_token (parser->lexer);
13664       /* See if it's a cv-qualifier.  */
13665       switch (token->keyword)
13666         {
13667         case RID_CONST:
13668           cv_qualifier = TYPE_QUAL_CONST;
13669           break;
13670
13671         case RID_VOLATILE:
13672           cv_qualifier = TYPE_QUAL_VOLATILE;
13673           break;
13674
13675         case RID_RESTRICT:
13676           cv_qualifier = TYPE_QUAL_RESTRICT;
13677           break;
13678
13679         default:
13680           cv_qualifier = TYPE_UNQUALIFIED;
13681           break;
13682         }
13683
13684       if (!cv_qualifier)
13685         break;
13686
13687       if (cv_quals & cv_qualifier)
13688         {
13689           error ("%Hduplicate cv-qualifier", &token->location);
13690           cp_lexer_purge_token (parser->lexer);
13691         }
13692       else
13693         {
13694           cp_lexer_consume_token (parser->lexer);
13695           cv_quals |= cv_qualifier;
13696         }
13697     }
13698
13699   return cv_quals;
13700 }
13701
13702 /* Parse a late-specified return type, if any.  This is not a separate
13703    non-terminal, but part of a function declarator, which looks like
13704
13705    -> type-id
13706
13707    Returns the type indicated by the type-id.  */
13708
13709 static tree
13710 cp_parser_late_return_type_opt (cp_parser* parser)
13711 {
13712   cp_token *token;
13713
13714   /* Peek at the next token.  */
13715   token = cp_lexer_peek_token (parser->lexer);
13716   /* A late-specified return type is indicated by an initial '->'. */
13717   if (token->type != CPP_DEREF)
13718     return NULL_TREE;
13719
13720   /* Consume the ->.  */
13721   cp_lexer_consume_token (parser->lexer);
13722
13723   return cp_parser_type_id (parser);
13724 }
13725
13726 /* Parse a declarator-id.
13727
13728    declarator-id:
13729      id-expression
13730      :: [opt] nested-name-specifier [opt] type-name
13731
13732    In the `id-expression' case, the value returned is as for
13733    cp_parser_id_expression if the id-expression was an unqualified-id.
13734    If the id-expression was a qualified-id, then a SCOPE_REF is
13735    returned.  The first operand is the scope (either a NAMESPACE_DECL
13736    or TREE_TYPE), but the second is still just a representation of an
13737    unqualified-id.  */
13738
13739 static tree
13740 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13741 {
13742   tree id;
13743   /* The expression must be an id-expression.  Assume that qualified
13744      names are the names of types so that:
13745
13746        template <class T>
13747        int S<T>::R::i = 3;
13748
13749      will work; we must treat `S<T>::R' as the name of a type.
13750      Similarly, assume that qualified names are templates, where
13751      required, so that:
13752
13753        template <class T>
13754        int S<T>::R<T>::i = 3;
13755
13756      will work, too.  */
13757   id = cp_parser_id_expression (parser,
13758                                 /*template_keyword_p=*/false,
13759                                 /*check_dependency_p=*/false,
13760                                 /*template_p=*/NULL,
13761                                 /*declarator_p=*/true,
13762                                 optional_p);
13763   if (id && BASELINK_P (id))
13764     id = BASELINK_FUNCTIONS (id);
13765   return id;
13766 }
13767
13768 /* Parse a type-id.
13769
13770    type-id:
13771      type-specifier-seq abstract-declarator [opt]
13772
13773    Returns the TYPE specified.  */
13774
13775 static tree
13776 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13777 {
13778   cp_decl_specifier_seq type_specifier_seq;
13779   cp_declarator *abstract_declarator;
13780
13781   /* Parse the type-specifier-seq.  */
13782   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13783                                 &type_specifier_seq);
13784   if (type_specifier_seq.type == error_mark_node)
13785     return error_mark_node;
13786
13787   /* There might or might not be an abstract declarator.  */
13788   cp_parser_parse_tentatively (parser);
13789   /* Look for the declarator.  */
13790   abstract_declarator
13791     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13792                             /*parenthesized_p=*/NULL,
13793                             /*member_p=*/false);
13794   /* Check to see if there really was a declarator.  */
13795   if (!cp_parser_parse_definitely (parser))
13796     abstract_declarator = NULL;
13797
13798   if (type_specifier_seq.type
13799       && type_uses_auto (type_specifier_seq.type))
13800     {
13801       error ("invalid use of %<auto%>");
13802       return error_mark_node;
13803     }
13804   
13805   return groktypename (&type_specifier_seq, abstract_declarator,
13806                        is_template_arg);
13807 }
13808
13809 static tree cp_parser_type_id (cp_parser *parser)
13810 {
13811   return cp_parser_type_id_1 (parser, false);
13812 }
13813
13814 static tree cp_parser_template_type_arg (cp_parser *parser)
13815 {
13816   return cp_parser_type_id_1 (parser, true);
13817 }
13818
13819 /* Parse a type-specifier-seq.
13820
13821    type-specifier-seq:
13822      type-specifier type-specifier-seq [opt]
13823
13824    GNU extension:
13825
13826    type-specifier-seq:
13827      attributes type-specifier-seq [opt]
13828
13829    If IS_CONDITION is true, we are at the start of a "condition",
13830    e.g., we've just seen "if (".
13831
13832    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13833
13834 static void
13835 cp_parser_type_specifier_seq (cp_parser* parser,
13836                               bool is_condition,
13837                               cp_decl_specifier_seq *type_specifier_seq)
13838 {
13839   bool seen_type_specifier = false;
13840   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13841   cp_token *start_token = NULL;
13842
13843   /* Clear the TYPE_SPECIFIER_SEQ.  */
13844   clear_decl_specs (type_specifier_seq);
13845
13846   /* Parse the type-specifiers and attributes.  */
13847   while (true)
13848     {
13849       tree type_specifier;
13850       bool is_cv_qualifier;
13851
13852       /* Check for attributes first.  */
13853       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13854         {
13855           type_specifier_seq->attributes =
13856             chainon (type_specifier_seq->attributes,
13857                      cp_parser_attributes_opt (parser));
13858           continue;
13859         }
13860
13861       /* record the token of the beginning of the type specifier seq,
13862          for error reporting purposes*/
13863      if (!start_token)
13864        start_token = cp_lexer_peek_token (parser->lexer);
13865
13866       /* Look for the type-specifier.  */
13867       type_specifier = cp_parser_type_specifier (parser,
13868                                                  flags,
13869                                                  type_specifier_seq,
13870                                                  /*is_declaration=*/false,
13871                                                  NULL,
13872                                                  &is_cv_qualifier);
13873       if (!type_specifier)
13874         {
13875           /* If the first type-specifier could not be found, this is not a
13876              type-specifier-seq at all.  */
13877           if (!seen_type_specifier)
13878             {
13879               cp_parser_error (parser, "expected type-specifier");
13880               type_specifier_seq->type = error_mark_node;
13881               return;
13882             }
13883           /* If subsequent type-specifiers could not be found, the
13884              type-specifier-seq is complete.  */
13885           break;
13886         }
13887
13888       seen_type_specifier = true;
13889       /* The standard says that a condition can be:
13890
13891             type-specifier-seq declarator = assignment-expression
13892
13893          However, given:
13894
13895            struct S {};
13896            if (int S = ...)
13897
13898          we should treat the "S" as a declarator, not as a
13899          type-specifier.  The standard doesn't say that explicitly for
13900          type-specifier-seq, but it does say that for
13901          decl-specifier-seq in an ordinary declaration.  Perhaps it
13902          would be clearer just to allow a decl-specifier-seq here, and
13903          then add a semantic restriction that if any decl-specifiers
13904          that are not type-specifiers appear, the program is invalid.  */
13905       if (is_condition && !is_cv_qualifier)
13906         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13907     }
13908
13909   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13910 }
13911
13912 /* Parse a parameter-declaration-clause.
13913
13914    parameter-declaration-clause:
13915      parameter-declaration-list [opt] ... [opt]
13916      parameter-declaration-list , ...
13917
13918    Returns a representation for the parameter declarations.  A return
13919    value of NULL indicates a parameter-declaration-clause consisting
13920    only of an ellipsis.  */
13921
13922 static tree
13923 cp_parser_parameter_declaration_clause (cp_parser* parser)
13924 {
13925   tree parameters;
13926   cp_token *token;
13927   bool ellipsis_p;
13928   bool is_error;
13929
13930   /* Peek at the next token.  */
13931   token = cp_lexer_peek_token (parser->lexer);
13932   /* Check for trivial parameter-declaration-clauses.  */
13933   if (token->type == CPP_ELLIPSIS)
13934     {
13935       /* Consume the `...' token.  */
13936       cp_lexer_consume_token (parser->lexer);
13937       return NULL_TREE;
13938     }
13939   else if (token->type == CPP_CLOSE_PAREN)
13940     /* There are no parameters.  */
13941     {
13942 #ifndef NO_IMPLICIT_EXTERN_C
13943       if (in_system_header && current_class_type == NULL
13944           && current_lang_name == lang_name_c)
13945         return NULL_TREE;
13946       else
13947 #endif
13948         return void_list_node;
13949     }
13950   /* Check for `(void)', too, which is a special case.  */
13951   else if (token->keyword == RID_VOID
13952            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13953                == CPP_CLOSE_PAREN))
13954     {
13955       /* Consume the `void' token.  */
13956       cp_lexer_consume_token (parser->lexer);
13957       /* There are no parameters.  */
13958       return void_list_node;
13959     }
13960
13961   /* Parse the parameter-declaration-list.  */
13962   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13963   /* If a parse error occurred while parsing the
13964      parameter-declaration-list, then the entire
13965      parameter-declaration-clause is erroneous.  */
13966   if (is_error)
13967     return NULL;
13968
13969   /* Peek at the next token.  */
13970   token = cp_lexer_peek_token (parser->lexer);
13971   /* If it's a `,', the clause should terminate with an ellipsis.  */
13972   if (token->type == CPP_COMMA)
13973     {
13974       /* Consume the `,'.  */
13975       cp_lexer_consume_token (parser->lexer);
13976       /* Expect an ellipsis.  */
13977       ellipsis_p
13978         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13979     }
13980   /* It might also be `...' if the optional trailing `,' was
13981      omitted.  */
13982   else if (token->type == CPP_ELLIPSIS)
13983     {
13984       /* Consume the `...' token.  */
13985       cp_lexer_consume_token (parser->lexer);
13986       /* And remember that we saw it.  */
13987       ellipsis_p = true;
13988     }
13989   else
13990     ellipsis_p = false;
13991
13992   /* Finish the parameter list.  */
13993   if (!ellipsis_p)
13994     parameters = chainon (parameters, void_list_node);
13995
13996   return parameters;
13997 }
13998
13999 /* Parse a parameter-declaration-list.
14000
14001    parameter-declaration-list:
14002      parameter-declaration
14003      parameter-declaration-list , parameter-declaration
14004
14005    Returns a representation of the parameter-declaration-list, as for
14006    cp_parser_parameter_declaration_clause.  However, the
14007    `void_list_node' is never appended to the list.  Upon return,
14008    *IS_ERROR will be true iff an error occurred.  */
14009
14010 static tree
14011 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14012 {
14013   tree parameters = NULL_TREE;
14014   tree *tail = &parameters; 
14015   bool saved_in_unbraced_linkage_specification_p;
14016
14017   /* Assume all will go well.  */
14018   *is_error = false;
14019   /* The special considerations that apply to a function within an
14020      unbraced linkage specifications do not apply to the parameters
14021      to the function.  */
14022   saved_in_unbraced_linkage_specification_p 
14023     = parser->in_unbraced_linkage_specification_p;
14024   parser->in_unbraced_linkage_specification_p = false;
14025
14026   /* Look for more parameters.  */
14027   while (true)
14028     {
14029       cp_parameter_declarator *parameter;
14030       tree decl = error_mark_node;
14031       bool parenthesized_p;
14032       /* Parse the parameter.  */
14033       parameter
14034         = cp_parser_parameter_declaration (parser,
14035                                            /*template_parm_p=*/false,
14036                                            &parenthesized_p);
14037
14038       /* We don't know yet if the enclosing context is deprecated, so wait
14039          and warn in grokparms if appropriate.  */
14040       deprecated_state = DEPRECATED_SUPPRESS;
14041
14042       if (parameter)
14043         decl = grokdeclarator (parameter->declarator,
14044                                &parameter->decl_specifiers,
14045                                PARM,
14046                                parameter->default_argument != NULL_TREE,
14047                                &parameter->decl_specifiers.attributes);
14048
14049       deprecated_state = DEPRECATED_NORMAL;
14050
14051       /* If a parse error occurred parsing the parameter declaration,
14052          then the entire parameter-declaration-list is erroneous.  */
14053       if (decl == error_mark_node)
14054         {
14055           *is_error = true;
14056           parameters = error_mark_node;
14057           break;
14058         }
14059
14060       if (parameter->decl_specifiers.attributes)
14061         cplus_decl_attributes (&decl,
14062                                parameter->decl_specifiers.attributes,
14063                                0);
14064       if (DECL_NAME (decl))
14065         decl = pushdecl (decl);
14066
14067       /* Add the new parameter to the list.  */
14068       *tail = build_tree_list (parameter->default_argument, decl);
14069       tail = &TREE_CHAIN (*tail);
14070
14071       /* Peek at the next token.  */
14072       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14073           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14074           /* These are for Objective-C++ */
14075           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14076           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14077         /* The parameter-declaration-list is complete.  */
14078         break;
14079       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14080         {
14081           cp_token *token;
14082
14083           /* Peek at the next token.  */
14084           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14085           /* If it's an ellipsis, then the list is complete.  */
14086           if (token->type == CPP_ELLIPSIS)
14087             break;
14088           /* Otherwise, there must be more parameters.  Consume the
14089              `,'.  */
14090           cp_lexer_consume_token (parser->lexer);
14091           /* When parsing something like:
14092
14093                 int i(float f, double d)
14094
14095              we can tell after seeing the declaration for "f" that we
14096              are not looking at an initialization of a variable "i",
14097              but rather at the declaration of a function "i".
14098
14099              Due to the fact that the parsing of template arguments
14100              (as specified to a template-id) requires backtracking we
14101              cannot use this technique when inside a template argument
14102              list.  */
14103           if (!parser->in_template_argument_list_p
14104               && !parser->in_type_id_in_expr_p
14105               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14106               /* However, a parameter-declaration of the form
14107                  "foat(f)" (which is a valid declaration of a
14108                  parameter "f") can also be interpreted as an
14109                  expression (the conversion of "f" to "float").  */
14110               && !parenthesized_p)
14111             cp_parser_commit_to_tentative_parse (parser);
14112         }
14113       else
14114         {
14115           cp_parser_error (parser, "expected %<,%> or %<...%>");
14116           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14117             cp_parser_skip_to_closing_parenthesis (parser,
14118                                                    /*recovering=*/true,
14119                                                    /*or_comma=*/false,
14120                                                    /*consume_paren=*/false);
14121           break;
14122         }
14123     }
14124
14125   parser->in_unbraced_linkage_specification_p
14126     = saved_in_unbraced_linkage_specification_p;
14127
14128   return parameters;
14129 }
14130
14131 /* Parse a parameter declaration.
14132
14133    parameter-declaration:
14134      decl-specifier-seq ... [opt] declarator
14135      decl-specifier-seq declarator = assignment-expression
14136      decl-specifier-seq ... [opt] abstract-declarator [opt]
14137      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14138
14139    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14140    declares a template parameter.  (In that case, a non-nested `>'
14141    token encountered during the parsing of the assignment-expression
14142    is not interpreted as a greater-than operator.)
14143
14144    Returns a representation of the parameter, or NULL if an error
14145    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14146    true iff the declarator is of the form "(p)".  */
14147
14148 static cp_parameter_declarator *
14149 cp_parser_parameter_declaration (cp_parser *parser,
14150                                  bool template_parm_p,
14151                                  bool *parenthesized_p)
14152 {
14153   int declares_class_or_enum;
14154   bool greater_than_is_operator_p;
14155   cp_decl_specifier_seq decl_specifiers;
14156   cp_declarator *declarator;
14157   tree default_argument;
14158   cp_token *token = NULL, *declarator_token_start = NULL;
14159   const char *saved_message;
14160
14161   /* In a template parameter, `>' is not an operator.
14162
14163      [temp.param]
14164
14165      When parsing a default template-argument for a non-type
14166      template-parameter, the first non-nested `>' is taken as the end
14167      of the template parameter-list rather than a greater-than
14168      operator.  */
14169   greater_than_is_operator_p = !template_parm_p;
14170
14171   /* Type definitions may not appear in parameter types.  */
14172   saved_message = parser->type_definition_forbidden_message;
14173   parser->type_definition_forbidden_message
14174     = "types may not be defined in parameter types";
14175
14176   /* Parse the declaration-specifiers.  */
14177   cp_parser_decl_specifier_seq (parser,
14178                                 CP_PARSER_FLAGS_NONE,
14179                                 &decl_specifiers,
14180                                 &declares_class_or_enum);
14181   /* If an error occurred, there's no reason to attempt to parse the
14182      rest of the declaration.  */
14183   if (cp_parser_error_occurred (parser))
14184     {
14185       parser->type_definition_forbidden_message = saved_message;
14186       return NULL;
14187     }
14188
14189   /* Peek at the next token.  */
14190   token = cp_lexer_peek_token (parser->lexer);
14191
14192   /* If the next token is a `)', `,', `=', `>', or `...', then there
14193      is no declarator. However, when variadic templates are enabled,
14194      there may be a declarator following `...'.  */
14195   if (token->type == CPP_CLOSE_PAREN
14196       || token->type == CPP_COMMA
14197       || token->type == CPP_EQ
14198       || token->type == CPP_GREATER)
14199     {
14200       declarator = NULL;
14201       if (parenthesized_p)
14202         *parenthesized_p = false;
14203     }
14204   /* Otherwise, there should be a declarator.  */
14205   else
14206     {
14207       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14208       parser->default_arg_ok_p = false;
14209
14210       /* After seeing a decl-specifier-seq, if the next token is not a
14211          "(", there is no possibility that the code is a valid
14212          expression.  Therefore, if parsing tentatively, we commit at
14213          this point.  */
14214       if (!parser->in_template_argument_list_p
14215           /* In an expression context, having seen:
14216
14217                (int((char ...
14218
14219              we cannot be sure whether we are looking at a
14220              function-type (taking a "char" as a parameter) or a cast
14221              of some object of type "char" to "int".  */
14222           && !parser->in_type_id_in_expr_p
14223           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14224           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14225         cp_parser_commit_to_tentative_parse (parser);
14226       /* Parse the declarator.  */
14227       declarator_token_start = token;
14228       declarator = cp_parser_declarator (parser,
14229                                          CP_PARSER_DECLARATOR_EITHER,
14230                                          /*ctor_dtor_or_conv_p=*/NULL,
14231                                          parenthesized_p,
14232                                          /*member_p=*/false);
14233       parser->default_arg_ok_p = saved_default_arg_ok_p;
14234       /* After the declarator, allow more attributes.  */
14235       decl_specifiers.attributes
14236         = chainon (decl_specifiers.attributes,
14237                    cp_parser_attributes_opt (parser));
14238     }
14239
14240   /* If the next token is an ellipsis, and we have not seen a
14241      declarator name, and the type of the declarator contains parameter
14242      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14243      a parameter pack expansion expression. Otherwise, leave the
14244      ellipsis for a C-style variadic function. */
14245   token = cp_lexer_peek_token (parser->lexer);
14246   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14247     {
14248       tree type = decl_specifiers.type;
14249
14250       if (type && DECL_P (type))
14251         type = TREE_TYPE (type);
14252
14253       if (type
14254           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14255           && declarator_can_be_parameter_pack (declarator)
14256           && (!declarator || !declarator->parameter_pack_p)
14257           && uses_parameter_packs (type))
14258         {
14259           /* Consume the `...'. */
14260           cp_lexer_consume_token (parser->lexer);
14261           maybe_warn_variadic_templates ();
14262           
14263           /* Build a pack expansion type */
14264           if (declarator)
14265             declarator->parameter_pack_p = true;
14266           else
14267             decl_specifiers.type = make_pack_expansion (type);
14268         }
14269     }
14270
14271   /* The restriction on defining new types applies only to the type
14272      of the parameter, not to the default argument.  */
14273   parser->type_definition_forbidden_message = saved_message;
14274
14275   /* If the next token is `=', then process a default argument.  */
14276   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14277     {
14278       /* Consume the `='.  */
14279       cp_lexer_consume_token (parser->lexer);
14280
14281       /* If we are defining a class, then the tokens that make up the
14282          default argument must be saved and processed later.  */
14283       if (!template_parm_p && at_class_scope_p ()
14284           && TYPE_BEING_DEFINED (current_class_type))
14285         {
14286           unsigned depth = 0;
14287           int maybe_template_id = 0;
14288           cp_token *first_token;
14289           cp_token *token;
14290
14291           /* Add tokens until we have processed the entire default
14292              argument.  We add the range [first_token, token).  */
14293           first_token = cp_lexer_peek_token (parser->lexer);
14294           while (true)
14295             {
14296               bool done = false;
14297
14298               /* Peek at the next token.  */
14299               token = cp_lexer_peek_token (parser->lexer);
14300               /* What we do depends on what token we have.  */
14301               switch (token->type)
14302                 {
14303                   /* In valid code, a default argument must be
14304                      immediately followed by a `,' `)', or `...'.  */
14305                 case CPP_COMMA:
14306                   if (depth == 0 && maybe_template_id)
14307                     {
14308                       /* If we've seen a '<', we might be in a
14309                          template-argument-list.  Until Core issue 325 is
14310                          resolved, we don't know how this situation ought
14311                          to be handled, so try to DTRT.  We check whether
14312                          what comes after the comma is a valid parameter
14313                          declaration list.  If it is, then the comma ends
14314                          the default argument; otherwise the default
14315                          argument continues.  */
14316                       bool error = false;
14317
14318                       /* Set ITALP so cp_parser_parameter_declaration_list
14319                          doesn't decide to commit to this parse.  */
14320                       bool saved_italp = parser->in_template_argument_list_p;
14321                       parser->in_template_argument_list_p = true;
14322
14323                       cp_parser_parse_tentatively (parser);
14324                       cp_lexer_consume_token (parser->lexer);
14325                       cp_parser_parameter_declaration_list (parser, &error);
14326                       if (!cp_parser_error_occurred (parser) && !error)
14327                         done = true;
14328                       cp_parser_abort_tentative_parse (parser);
14329
14330                       parser->in_template_argument_list_p = saved_italp;
14331                       break;
14332                     }
14333                 case CPP_CLOSE_PAREN:
14334                 case CPP_ELLIPSIS:
14335                   /* If we run into a non-nested `;', `}', or `]',
14336                      then the code is invalid -- but the default
14337                      argument is certainly over.  */
14338                 case CPP_SEMICOLON:
14339                 case CPP_CLOSE_BRACE:
14340                 case CPP_CLOSE_SQUARE:
14341                   if (depth == 0)
14342                     done = true;
14343                   /* Update DEPTH, if necessary.  */
14344                   else if (token->type == CPP_CLOSE_PAREN
14345                            || token->type == CPP_CLOSE_BRACE
14346                            || token->type == CPP_CLOSE_SQUARE)
14347                     --depth;
14348                   break;
14349
14350                 case CPP_OPEN_PAREN:
14351                 case CPP_OPEN_SQUARE:
14352                 case CPP_OPEN_BRACE:
14353                   ++depth;
14354                   break;
14355
14356                 case CPP_LESS:
14357                   if (depth == 0)
14358                     /* This might be the comparison operator, or it might
14359                        start a template argument list.  */
14360                     ++maybe_template_id;
14361                   break;
14362
14363                 case CPP_RSHIFT:
14364                   if (cxx_dialect == cxx98)
14365                     break;
14366                   /* Fall through for C++0x, which treats the `>>'
14367                      operator like two `>' tokens in certain
14368                      cases.  */
14369
14370                 case CPP_GREATER:
14371                   if (depth == 0)
14372                     {
14373                       /* This might be an operator, or it might close a
14374                          template argument list.  But if a previous '<'
14375                          started a template argument list, this will have
14376                          closed it, so we can't be in one anymore.  */
14377                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14378                       if (maybe_template_id < 0)
14379                         maybe_template_id = 0;
14380                     }
14381                   break;
14382
14383                   /* If we run out of tokens, issue an error message.  */
14384                 case CPP_EOF:
14385                 case CPP_PRAGMA_EOL:
14386                   error ("%Hfile ends in default argument", &token->location);
14387                   done = true;
14388                   break;
14389
14390                 case CPP_NAME:
14391                 case CPP_SCOPE:
14392                   /* In these cases, we should look for template-ids.
14393                      For example, if the default argument is
14394                      `X<int, double>()', we need to do name lookup to
14395                      figure out whether or not `X' is a template; if
14396                      so, the `,' does not end the default argument.
14397
14398                      That is not yet done.  */
14399                   break;
14400
14401                 default:
14402                   break;
14403                 }
14404
14405               /* If we've reached the end, stop.  */
14406               if (done)
14407                 break;
14408
14409               /* Add the token to the token block.  */
14410               token = cp_lexer_consume_token (parser->lexer);
14411             }
14412
14413           /* Create a DEFAULT_ARG to represent the unparsed default
14414              argument.  */
14415           default_argument = make_node (DEFAULT_ARG);
14416           DEFARG_TOKENS (default_argument)
14417             = cp_token_cache_new (first_token, token);
14418           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14419         }
14420       /* Outside of a class definition, we can just parse the
14421          assignment-expression.  */
14422       else
14423         {
14424           token = cp_lexer_peek_token (parser->lexer);
14425           default_argument 
14426             = cp_parser_default_argument (parser, template_parm_p);
14427         }
14428
14429       if (!parser->default_arg_ok_p)
14430         {
14431           if (flag_permissive)
14432             warning (0, "deprecated use of default argument for parameter of non-function");
14433           else
14434             {
14435               error ("%Hdefault arguments are only "
14436                      "permitted for function parameters",
14437                      &token->location);
14438               default_argument = NULL_TREE;
14439             }
14440         }
14441       else if ((declarator && declarator->parameter_pack_p)
14442                || (decl_specifiers.type
14443                    && PACK_EXPANSION_P (decl_specifiers.type)))
14444         {
14445           const char* kind = template_parm_p? "template " : "";
14446           
14447           /* Find the name of the parameter pack.  */     
14448           cp_declarator *id_declarator = declarator;
14449           while (id_declarator && id_declarator->kind != cdk_id)
14450             id_declarator = id_declarator->declarator;
14451           
14452           if (id_declarator && id_declarator->kind == cdk_id)
14453             error ("%H%sparameter pack %qD cannot have a default argument",
14454                    &declarator_token_start->location,
14455                    kind, id_declarator->u.id.unqualified_name);
14456           else
14457             error ("%H%sparameter pack cannot have a default argument",
14458                    &declarator_token_start->location, kind);
14459           
14460           default_argument = NULL_TREE;
14461         }
14462     }
14463   else
14464     default_argument = NULL_TREE;
14465
14466   return make_parameter_declarator (&decl_specifiers,
14467                                     declarator,
14468                                     default_argument);
14469 }
14470
14471 /* Parse a default argument and return it.
14472
14473    TEMPLATE_PARM_P is true if this is a default argument for a
14474    non-type template parameter.  */
14475 static tree
14476 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14477 {
14478   tree default_argument = NULL_TREE;
14479   bool saved_greater_than_is_operator_p;
14480   bool saved_local_variables_forbidden_p;
14481
14482   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14483      set correctly.  */
14484   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14485   parser->greater_than_is_operator_p = !template_parm_p;
14486   /* Local variable names (and the `this' keyword) may not
14487      appear in a default argument.  */
14488   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14489   parser->local_variables_forbidden_p = true;
14490   /* The default argument expression may cause implicitly
14491      defined member functions to be synthesized, which will
14492      result in garbage collection.  We must treat this
14493      situation as if we were within the body of function so as
14494      to avoid collecting live data on the stack.  */
14495   ++function_depth;
14496   /* Parse the assignment-expression.  */
14497   if (template_parm_p)
14498     push_deferring_access_checks (dk_no_deferred);
14499   default_argument
14500     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14501   if (template_parm_p)
14502     pop_deferring_access_checks ();
14503   /* Restore saved state.  */
14504   --function_depth;
14505   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14506   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14507
14508   return default_argument;
14509 }
14510
14511 /* Parse a function-body.
14512
14513    function-body:
14514      compound_statement  */
14515
14516 static void
14517 cp_parser_function_body (cp_parser *parser)
14518 {
14519   cp_parser_compound_statement (parser, NULL, false);
14520 }
14521
14522 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14523    true if a ctor-initializer was present.  */
14524
14525 static bool
14526 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14527 {
14528   tree body;
14529   bool ctor_initializer_p;
14530
14531   /* Begin the function body.  */
14532   body = begin_function_body ();
14533   /* Parse the optional ctor-initializer.  */
14534   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14535   /* Parse the function-body.  */
14536   cp_parser_function_body (parser);
14537   /* Finish the function body.  */
14538   finish_function_body (body);
14539
14540   return ctor_initializer_p;
14541 }
14542
14543 /* Parse an initializer.
14544
14545    initializer:
14546      = initializer-clause
14547      ( expression-list )
14548
14549    Returns an expression representing the initializer.  If no
14550    initializer is present, NULL_TREE is returned.
14551
14552    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14553    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14554    set to TRUE if there is no initializer present.  If there is an
14555    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14556    is set to true; otherwise it is set to false.  */
14557
14558 static tree
14559 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14560                        bool* non_constant_p)
14561 {
14562   cp_token *token;
14563   tree init;
14564
14565   /* Peek at the next token.  */
14566   token = cp_lexer_peek_token (parser->lexer);
14567
14568   /* Let our caller know whether or not this initializer was
14569      parenthesized.  */
14570   *is_direct_init = (token->type != CPP_EQ);
14571   /* Assume that the initializer is constant.  */
14572   *non_constant_p = false;
14573
14574   if (token->type == CPP_EQ)
14575     {
14576       /* Consume the `='.  */
14577       cp_lexer_consume_token (parser->lexer);
14578       /* Parse the initializer-clause.  */
14579       init = cp_parser_initializer_clause (parser, non_constant_p);
14580     }
14581   else if (token->type == CPP_OPEN_PAREN)
14582     init = cp_parser_parenthesized_expression_list (parser, false,
14583                                                     /*cast_p=*/false,
14584                                                     /*allow_expansion_p=*/true,
14585                                                     non_constant_p);
14586   else if (token->type == CPP_OPEN_BRACE)
14587     {
14588       maybe_warn_cpp0x ("extended initializer lists");
14589       init = cp_parser_braced_list (parser, non_constant_p);
14590       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14591     }
14592   else
14593     {
14594       /* Anything else is an error.  */
14595       cp_parser_error (parser, "expected initializer");
14596       init = error_mark_node;
14597     }
14598
14599   return init;
14600 }
14601
14602 /* Parse an initializer-clause.
14603
14604    initializer-clause:
14605      assignment-expression
14606      braced-init-list
14607
14608    Returns an expression representing the initializer.
14609
14610    If the `assignment-expression' production is used the value
14611    returned is simply a representation for the expression.
14612
14613    Otherwise, calls cp_parser_braced_list.  */
14614
14615 static tree
14616 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14617 {
14618   tree initializer;
14619
14620   /* Assume the expression is constant.  */
14621   *non_constant_p = false;
14622
14623   /* If it is not a `{', then we are looking at an
14624      assignment-expression.  */
14625   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14626     {
14627       initializer
14628         = cp_parser_constant_expression (parser,
14629                                         /*allow_non_constant_p=*/true,
14630                                         non_constant_p);
14631       if (!*non_constant_p)
14632         initializer = fold_non_dependent_expr (initializer);
14633     }
14634   else
14635     initializer = cp_parser_braced_list (parser, non_constant_p);
14636
14637   return initializer;
14638 }
14639
14640 /* Parse a brace-enclosed initializer list.
14641
14642    braced-init-list:
14643      { initializer-list , [opt] }
14644      { }
14645
14646    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14647    the elements of the initializer-list (or NULL, if the last
14648    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14649    NULL_TREE.  There is no way to detect whether or not the optional
14650    trailing `,' was provided.  NON_CONSTANT_P is as for
14651    cp_parser_initializer.  */     
14652
14653 static tree
14654 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14655 {
14656   tree initializer;
14657
14658   /* Consume the `{' token.  */
14659   cp_lexer_consume_token (parser->lexer);
14660   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14661   initializer = make_node (CONSTRUCTOR);
14662   /* If it's not a `}', then there is a non-trivial initializer.  */
14663   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14664     {
14665       /* Parse the initializer list.  */
14666       CONSTRUCTOR_ELTS (initializer)
14667         = cp_parser_initializer_list (parser, non_constant_p);
14668       /* A trailing `,' token is allowed.  */
14669       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14670         cp_lexer_consume_token (parser->lexer);
14671     }
14672   /* Now, there should be a trailing `}'.  */
14673   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14674   TREE_TYPE (initializer) = init_list_type_node;
14675   return initializer;
14676 }
14677
14678 /* Parse an initializer-list.
14679
14680    initializer-list:
14681      initializer-clause ... [opt]
14682      initializer-list , initializer-clause ... [opt]
14683
14684    GNU Extension:
14685
14686    initializer-list:
14687      identifier : initializer-clause
14688      initializer-list, identifier : initializer-clause
14689
14690    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14691    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14692    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14693    as for cp_parser_initializer.  */
14694
14695 static VEC(constructor_elt,gc) *
14696 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14697 {
14698   VEC(constructor_elt,gc) *v = NULL;
14699
14700   /* Assume all of the expressions are constant.  */
14701   *non_constant_p = false;
14702
14703   /* Parse the rest of the list.  */
14704   while (true)
14705     {
14706       cp_token *token;
14707       tree identifier;
14708       tree initializer;
14709       bool clause_non_constant_p;
14710
14711       /* If the next token is an identifier and the following one is a
14712          colon, we are looking at the GNU designated-initializer
14713          syntax.  */
14714       if (cp_parser_allow_gnu_extensions_p (parser)
14715           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14716           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14717         {
14718           /* Warn the user that they are using an extension.  */
14719           pedwarn (input_location, OPT_pedantic, 
14720                    "ISO C++ does not allow designated initializers");
14721           /* Consume the identifier.  */
14722           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14723           /* Consume the `:'.  */
14724           cp_lexer_consume_token (parser->lexer);
14725         }
14726       else
14727         identifier = NULL_TREE;
14728
14729       /* Parse the initializer.  */
14730       initializer = cp_parser_initializer_clause (parser,
14731                                                   &clause_non_constant_p);
14732       /* If any clause is non-constant, so is the entire initializer.  */
14733       if (clause_non_constant_p)
14734         *non_constant_p = true;
14735
14736       /* If we have an ellipsis, this is an initializer pack
14737          expansion.  */
14738       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14739         {
14740           /* Consume the `...'.  */
14741           cp_lexer_consume_token (parser->lexer);
14742
14743           /* Turn the initializer into an initializer expansion.  */
14744           initializer = make_pack_expansion (initializer);
14745         }
14746
14747       /* Add it to the vector.  */
14748       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14749
14750       /* If the next token is not a comma, we have reached the end of
14751          the list.  */
14752       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14753         break;
14754
14755       /* Peek at the next token.  */
14756       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14757       /* If the next token is a `}', then we're still done.  An
14758          initializer-clause can have a trailing `,' after the
14759          initializer-list and before the closing `}'.  */
14760       if (token->type == CPP_CLOSE_BRACE)
14761         break;
14762
14763       /* Consume the `,' token.  */
14764       cp_lexer_consume_token (parser->lexer);
14765     }
14766
14767   return v;
14768 }
14769
14770 /* Classes [gram.class] */
14771
14772 /* Parse a class-name.
14773
14774    class-name:
14775      identifier
14776      template-id
14777
14778    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14779    to indicate that names looked up in dependent types should be
14780    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14781    keyword has been used to indicate that the name that appears next
14782    is a template.  TAG_TYPE indicates the explicit tag given before
14783    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14784    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14785    is the class being defined in a class-head.
14786
14787    Returns the TYPE_DECL representing the class.  */
14788
14789 static tree
14790 cp_parser_class_name (cp_parser *parser,
14791                       bool typename_keyword_p,
14792                       bool template_keyword_p,
14793                       enum tag_types tag_type,
14794                       bool check_dependency_p,
14795                       bool class_head_p,
14796                       bool is_declaration)
14797 {
14798   tree decl;
14799   tree scope;
14800   bool typename_p;
14801   cp_token *token;
14802   tree identifier = NULL_TREE;
14803
14804   /* All class-names start with an identifier.  */
14805   token = cp_lexer_peek_token (parser->lexer);
14806   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14807     {
14808       cp_parser_error (parser, "expected class-name");
14809       return error_mark_node;
14810     }
14811
14812   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14813      to a template-id, so we save it here.  */
14814   scope = parser->scope;
14815   if (scope == error_mark_node)
14816     return error_mark_node;
14817
14818   /* Any name names a type if we're following the `typename' keyword
14819      in a qualified name where the enclosing scope is type-dependent.  */
14820   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14821                 && dependent_type_p (scope));
14822   /* Handle the common case (an identifier, but not a template-id)
14823      efficiently.  */
14824   if (token->type == CPP_NAME
14825       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14826     {
14827       cp_token *identifier_token;
14828       bool ambiguous_p;
14829
14830       /* Look for the identifier.  */
14831       identifier_token = cp_lexer_peek_token (parser->lexer);
14832       ambiguous_p = identifier_token->ambiguous_p;
14833       identifier = cp_parser_identifier (parser);
14834       /* If the next token isn't an identifier, we are certainly not
14835          looking at a class-name.  */
14836       if (identifier == error_mark_node)
14837         decl = error_mark_node;
14838       /* If we know this is a type-name, there's no need to look it
14839          up.  */
14840       else if (typename_p)
14841         decl = identifier;
14842       else
14843         {
14844           tree ambiguous_decls;
14845           /* If we already know that this lookup is ambiguous, then
14846              we've already issued an error message; there's no reason
14847              to check again.  */
14848           if (ambiguous_p)
14849             {
14850               cp_parser_simulate_error (parser);
14851               return error_mark_node;
14852             }
14853           /* If the next token is a `::', then the name must be a type
14854              name.
14855
14856              [basic.lookup.qual]
14857
14858              During the lookup for a name preceding the :: scope
14859              resolution operator, object, function, and enumerator
14860              names are ignored.  */
14861           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14862             tag_type = typename_type;
14863           /* Look up the name.  */
14864           decl = cp_parser_lookup_name (parser, identifier,
14865                                         tag_type,
14866                                         /*is_template=*/false,
14867                                         /*is_namespace=*/false,
14868                                         check_dependency_p,
14869                                         &ambiguous_decls,
14870                                         identifier_token->location);
14871           if (ambiguous_decls)
14872             {
14873               error ("%Hreference to %qD is ambiguous",
14874                      &identifier_token->location, identifier);
14875               print_candidates (ambiguous_decls);
14876               if (cp_parser_parsing_tentatively (parser))
14877                 {
14878                   identifier_token->ambiguous_p = true;
14879                   cp_parser_simulate_error (parser);
14880                 }
14881               return error_mark_node;
14882             }
14883         }
14884     }
14885   else
14886     {
14887       /* Try a template-id.  */
14888       decl = cp_parser_template_id (parser, template_keyword_p,
14889                                     check_dependency_p,
14890                                     is_declaration);
14891       if (decl == error_mark_node)
14892         return error_mark_node;
14893     }
14894
14895   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14896
14897   /* If this is a typename, create a TYPENAME_TYPE.  */
14898   if (typename_p && decl != error_mark_node)
14899     {
14900       decl = make_typename_type (scope, decl, typename_type,
14901                                  /*complain=*/tf_error);
14902       if (decl != error_mark_node)
14903         decl = TYPE_NAME (decl);
14904     }
14905
14906   /* Check to see that it is really the name of a class.  */
14907   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14908       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14909       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14910     /* Situations like this:
14911
14912          template <typename T> struct A {
14913            typename T::template X<int>::I i;
14914          };
14915
14916        are problematic.  Is `T::template X<int>' a class-name?  The
14917        standard does not seem to be definitive, but there is no other
14918        valid interpretation of the following `::'.  Therefore, those
14919        names are considered class-names.  */
14920     {
14921       decl = make_typename_type (scope, decl, tag_type, tf_error);
14922       if (decl != error_mark_node)
14923         decl = TYPE_NAME (decl);
14924     }
14925   else if (TREE_CODE (decl) != TYPE_DECL
14926            || TREE_TYPE (decl) == error_mark_node
14927            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14928     decl = error_mark_node;
14929
14930   if (decl == error_mark_node)
14931     cp_parser_error (parser, "expected class-name");
14932   else if (identifier && !parser->scope)
14933     maybe_note_name_used_in_class (identifier, decl);
14934
14935   return decl;
14936 }
14937
14938 /* Parse a class-specifier.
14939
14940    class-specifier:
14941      class-head { member-specification [opt] }
14942
14943    Returns the TREE_TYPE representing the class.  */
14944
14945 static tree
14946 cp_parser_class_specifier (cp_parser* parser)
14947 {
14948   tree type;
14949   tree attributes = NULL_TREE;
14950   bool nested_name_specifier_p;
14951   unsigned saved_num_template_parameter_lists;
14952   bool saved_in_function_body;
14953   bool saved_in_unbraced_linkage_specification_p;
14954   tree old_scope = NULL_TREE;
14955   tree scope = NULL_TREE;
14956   tree bases;
14957
14958   push_deferring_access_checks (dk_no_deferred);
14959
14960   /* Parse the class-head.  */
14961   type = cp_parser_class_head (parser,
14962                                &nested_name_specifier_p,
14963                                &attributes,
14964                                &bases);
14965   /* If the class-head was a semantic disaster, skip the entire body
14966      of the class.  */
14967   if (!type)
14968     {
14969       cp_parser_skip_to_end_of_block_or_statement (parser);
14970       pop_deferring_access_checks ();
14971       return error_mark_node;
14972     }
14973
14974   /* Look for the `{'.  */
14975   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14976     {
14977       pop_deferring_access_checks ();
14978       return error_mark_node;
14979     }
14980
14981   /* Process the base classes. If they're invalid, skip the 
14982      entire class body.  */
14983   if (!xref_basetypes (type, bases))
14984     {
14985       /* Consuming the closing brace yields better error messages
14986          later on.  */
14987       if (cp_parser_skip_to_closing_brace (parser))
14988         cp_lexer_consume_token (parser->lexer);
14989       pop_deferring_access_checks ();
14990       return error_mark_node;
14991     }
14992
14993   /* Issue an error message if type-definitions are forbidden here.  */
14994   cp_parser_check_type_definition (parser);
14995   /* Remember that we are defining one more class.  */
14996   ++parser->num_classes_being_defined;
14997   /* Inside the class, surrounding template-parameter-lists do not
14998      apply.  */
14999   saved_num_template_parameter_lists
15000     = parser->num_template_parameter_lists;
15001   parser->num_template_parameter_lists = 0;
15002   /* We are not in a function body.  */
15003   saved_in_function_body = parser->in_function_body;
15004   parser->in_function_body = false;
15005   /* We are not immediately inside an extern "lang" block.  */
15006   saved_in_unbraced_linkage_specification_p
15007     = parser->in_unbraced_linkage_specification_p;
15008   parser->in_unbraced_linkage_specification_p = false;
15009
15010   /* Start the class.  */
15011   if (nested_name_specifier_p)
15012     {
15013       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15014       old_scope = push_inner_scope (scope);
15015     }
15016   type = begin_class_definition (type, attributes);
15017
15018   if (type == error_mark_node)
15019     /* If the type is erroneous, skip the entire body of the class.  */
15020     cp_parser_skip_to_closing_brace (parser);
15021   else
15022     /* Parse the member-specification.  */
15023     cp_parser_member_specification_opt (parser);
15024
15025   /* Look for the trailing `}'.  */
15026   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15027   /* Look for trailing attributes to apply to this class.  */
15028   if (cp_parser_allow_gnu_extensions_p (parser))
15029     attributes = cp_parser_attributes_opt (parser);
15030   if (type != error_mark_node)
15031     type = finish_struct (type, attributes);
15032   if (nested_name_specifier_p)
15033     pop_inner_scope (old_scope, scope);
15034   /* If this class is not itself within the scope of another class,
15035      then we need to parse the bodies of all of the queued function
15036      definitions.  Note that the queued functions defined in a class
15037      are not always processed immediately following the
15038      class-specifier for that class.  Consider:
15039
15040        struct A {
15041          struct B { void f() { sizeof (A); } };
15042        };
15043
15044      If `f' were processed before the processing of `A' were
15045      completed, there would be no way to compute the size of `A'.
15046      Note that the nesting we are interested in here is lexical --
15047      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15048      for:
15049
15050        struct A { struct B; };
15051        struct A::B { void f() { } };
15052
15053      there is no need to delay the parsing of `A::B::f'.  */
15054   if (--parser->num_classes_being_defined == 0)
15055     {
15056       tree queue_entry;
15057       tree fn;
15058       tree class_type = NULL_TREE;
15059       tree pushed_scope = NULL_TREE;
15060
15061       /* In a first pass, parse default arguments to the functions.
15062          Then, in a second pass, parse the bodies of the functions.
15063          This two-phased approach handles cases like:
15064
15065             struct S {
15066               void f() { g(); }
15067               void g(int i = 3);
15068             };
15069
15070          */
15071       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15072              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15073            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15074            TREE_PURPOSE (parser->unparsed_functions_queues)
15075              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15076         {
15077           fn = TREE_VALUE (queue_entry);
15078           /* If there are default arguments that have not yet been processed,
15079              take care of them now.  */
15080           if (class_type != TREE_PURPOSE (queue_entry))
15081             {
15082               if (pushed_scope)
15083                 pop_scope (pushed_scope);
15084               class_type = TREE_PURPOSE (queue_entry);
15085               pushed_scope = push_scope (class_type);
15086             }
15087           /* Make sure that any template parameters are in scope.  */
15088           maybe_begin_member_template_processing (fn);
15089           /* Parse the default argument expressions.  */
15090           cp_parser_late_parsing_default_args (parser, fn);
15091           /* Remove any template parameters from the symbol table.  */
15092           maybe_end_member_template_processing ();
15093         }
15094       if (pushed_scope)
15095         pop_scope (pushed_scope);
15096       /* Now parse the body of the functions.  */
15097       for (TREE_VALUE (parser->unparsed_functions_queues)
15098              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15099            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15100            TREE_VALUE (parser->unparsed_functions_queues)
15101              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15102         {
15103           /* Figure out which function we need to process.  */
15104           fn = TREE_VALUE (queue_entry);
15105           /* Parse the function.  */
15106           cp_parser_late_parsing_for_member (parser, fn);
15107         }
15108     }
15109
15110   /* Put back any saved access checks.  */
15111   pop_deferring_access_checks ();
15112
15113   /* Restore saved state.  */
15114   parser->in_function_body = saved_in_function_body;
15115   parser->num_template_parameter_lists
15116     = saved_num_template_parameter_lists;
15117   parser->in_unbraced_linkage_specification_p
15118     = saved_in_unbraced_linkage_specification_p;
15119
15120   return type;
15121 }
15122
15123 /* Parse a class-head.
15124
15125    class-head:
15126      class-key identifier [opt] base-clause [opt]
15127      class-key nested-name-specifier identifier base-clause [opt]
15128      class-key nested-name-specifier [opt] template-id
15129        base-clause [opt]
15130
15131    GNU Extensions:
15132      class-key attributes identifier [opt] base-clause [opt]
15133      class-key attributes nested-name-specifier identifier base-clause [opt]
15134      class-key attributes nested-name-specifier [opt] template-id
15135        base-clause [opt]
15136
15137    Upon return BASES is initialized to the list of base classes (or
15138    NULL, if there are none) in the same form returned by
15139    cp_parser_base_clause.
15140
15141    Returns the TYPE of the indicated class.  Sets
15142    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15143    involving a nested-name-specifier was used, and FALSE otherwise.
15144
15145    Returns error_mark_node if this is not a class-head.
15146
15147    Returns NULL_TREE if the class-head is syntactically valid, but
15148    semantically invalid in a way that means we should skip the entire
15149    body of the class.  */
15150
15151 static tree
15152 cp_parser_class_head (cp_parser* parser,
15153                       bool* nested_name_specifier_p,
15154                       tree *attributes_p,
15155                       tree *bases)
15156 {
15157   tree nested_name_specifier;
15158   enum tag_types class_key;
15159   tree id = NULL_TREE;
15160   tree type = NULL_TREE;
15161   tree attributes;
15162   bool template_id_p = false;
15163   bool qualified_p = false;
15164   bool invalid_nested_name_p = false;
15165   bool invalid_explicit_specialization_p = false;
15166   tree pushed_scope = NULL_TREE;
15167   unsigned num_templates;
15168   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15169   /* Assume no nested-name-specifier will be present.  */
15170   *nested_name_specifier_p = false;
15171   /* Assume no template parameter lists will be used in defining the
15172      type.  */
15173   num_templates = 0;
15174
15175   *bases = NULL_TREE;
15176
15177   /* Look for the class-key.  */
15178   class_key = cp_parser_class_key (parser);
15179   if (class_key == none_type)
15180     return error_mark_node;
15181
15182   /* Parse the attributes.  */
15183   attributes = cp_parser_attributes_opt (parser);
15184
15185   /* If the next token is `::', that is invalid -- but sometimes
15186      people do try to write:
15187
15188        struct ::S {};
15189
15190      Handle this gracefully by accepting the extra qualifier, and then
15191      issuing an error about it later if this really is a
15192      class-head.  If it turns out just to be an elaborated type
15193      specifier, remain silent.  */
15194   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15195     qualified_p = true;
15196
15197   push_deferring_access_checks (dk_no_check);
15198
15199   /* Determine the name of the class.  Begin by looking for an
15200      optional nested-name-specifier.  */
15201   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15202   nested_name_specifier
15203     = cp_parser_nested_name_specifier_opt (parser,
15204                                            /*typename_keyword_p=*/false,
15205                                            /*check_dependency_p=*/false,
15206                                            /*type_p=*/false,
15207                                            /*is_declaration=*/false);
15208   /* If there was a nested-name-specifier, then there *must* be an
15209      identifier.  */
15210   if (nested_name_specifier)
15211     {
15212       type_start_token = cp_lexer_peek_token (parser->lexer);
15213       /* Although the grammar says `identifier', it really means
15214          `class-name' or `template-name'.  You are only allowed to
15215          define a class that has already been declared with this
15216          syntax.
15217
15218          The proposed resolution for Core Issue 180 says that wherever
15219          you see `class T::X' you should treat `X' as a type-name.
15220
15221          It is OK to define an inaccessible class; for example:
15222
15223            class A { class B; };
15224            class A::B {};
15225
15226          We do not know if we will see a class-name, or a
15227          template-name.  We look for a class-name first, in case the
15228          class-name is a template-id; if we looked for the
15229          template-name first we would stop after the template-name.  */
15230       cp_parser_parse_tentatively (parser);
15231       type = cp_parser_class_name (parser,
15232                                    /*typename_keyword_p=*/false,
15233                                    /*template_keyword_p=*/false,
15234                                    class_type,
15235                                    /*check_dependency_p=*/false,
15236                                    /*class_head_p=*/true,
15237                                    /*is_declaration=*/false);
15238       /* If that didn't work, ignore the nested-name-specifier.  */
15239       if (!cp_parser_parse_definitely (parser))
15240         {
15241           invalid_nested_name_p = true;
15242           type_start_token = cp_lexer_peek_token (parser->lexer);
15243           id = cp_parser_identifier (parser);
15244           if (id == error_mark_node)
15245             id = NULL_TREE;
15246         }
15247       /* If we could not find a corresponding TYPE, treat this
15248          declaration like an unqualified declaration.  */
15249       if (type == error_mark_node)
15250         nested_name_specifier = NULL_TREE;
15251       /* Otherwise, count the number of templates used in TYPE and its
15252          containing scopes.  */
15253       else
15254         {
15255           tree scope;
15256
15257           for (scope = TREE_TYPE (type);
15258                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15259                scope = (TYPE_P (scope)
15260                         ? TYPE_CONTEXT (scope)
15261                         : DECL_CONTEXT (scope)))
15262             if (TYPE_P (scope)
15263                 && CLASS_TYPE_P (scope)
15264                 && CLASSTYPE_TEMPLATE_INFO (scope)
15265                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15266                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15267               ++num_templates;
15268         }
15269     }
15270   /* Otherwise, the identifier is optional.  */
15271   else
15272     {
15273       /* We don't know whether what comes next is a template-id,
15274          an identifier, or nothing at all.  */
15275       cp_parser_parse_tentatively (parser);
15276       /* Check for a template-id.  */
15277       type_start_token = cp_lexer_peek_token (parser->lexer);
15278       id = cp_parser_template_id (parser,
15279                                   /*template_keyword_p=*/false,
15280                                   /*check_dependency_p=*/true,
15281                                   /*is_declaration=*/true);
15282       /* If that didn't work, it could still be an identifier.  */
15283       if (!cp_parser_parse_definitely (parser))
15284         {
15285           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15286             {
15287               type_start_token = cp_lexer_peek_token (parser->lexer);
15288               id = cp_parser_identifier (parser);
15289             }
15290           else
15291             id = NULL_TREE;
15292         }
15293       else
15294         {
15295           template_id_p = true;
15296           ++num_templates;
15297         }
15298     }
15299
15300   pop_deferring_access_checks ();
15301
15302   if (id)
15303     cp_parser_check_for_invalid_template_id (parser, id,
15304                                              type_start_token->location);
15305
15306   /* If it's not a `:' or a `{' then we can't really be looking at a
15307      class-head, since a class-head only appears as part of a
15308      class-specifier.  We have to detect this situation before calling
15309      xref_tag, since that has irreversible side-effects.  */
15310   if (!cp_parser_next_token_starts_class_definition_p (parser))
15311     {
15312       cp_parser_error (parser, "expected %<{%> or %<:%>");
15313       return error_mark_node;
15314     }
15315
15316   /* At this point, we're going ahead with the class-specifier, even
15317      if some other problem occurs.  */
15318   cp_parser_commit_to_tentative_parse (parser);
15319   /* Issue the error about the overly-qualified name now.  */
15320   if (qualified_p)
15321     {
15322       cp_parser_error (parser,
15323                        "global qualification of class name is invalid");
15324       return error_mark_node;
15325     }
15326   else if (invalid_nested_name_p)
15327     {
15328       cp_parser_error (parser,
15329                        "qualified name does not name a class");
15330       return error_mark_node;
15331     }
15332   else if (nested_name_specifier)
15333     {
15334       tree scope;
15335
15336       /* Reject typedef-names in class heads.  */
15337       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15338         {
15339           error ("%Hinvalid class name in declaration of %qD",
15340                  &type_start_token->location, type);
15341           type = NULL_TREE;
15342           goto done;
15343         }
15344
15345       /* Figure out in what scope the declaration is being placed.  */
15346       scope = current_scope ();
15347       /* If that scope does not contain the scope in which the
15348          class was originally declared, the program is invalid.  */
15349       if (scope && !is_ancestor (scope, nested_name_specifier))
15350         {
15351           if (at_namespace_scope_p ())
15352             error ("%Hdeclaration of %qD in namespace %qD which does not "
15353                    "enclose %qD",
15354                    &type_start_token->location,
15355                    type, scope, nested_name_specifier);
15356           else
15357             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15358                    &type_start_token->location,
15359                    type, scope, nested_name_specifier);
15360           type = NULL_TREE;
15361           goto done;
15362         }
15363       /* [dcl.meaning]
15364
15365          A declarator-id shall not be qualified except for the
15366          definition of a ... nested class outside of its class
15367          ... [or] the definition or explicit instantiation of a
15368          class member of a namespace outside of its namespace.  */
15369       if (scope == nested_name_specifier)
15370         {
15371           permerror (input_location, "%Hextra qualification not allowed",
15372                      &nested_name_specifier_token_start->location);
15373           nested_name_specifier = NULL_TREE;
15374           num_templates = 0;
15375         }
15376     }
15377   /* An explicit-specialization must be preceded by "template <>".  If
15378      it is not, try to recover gracefully.  */
15379   if (at_namespace_scope_p ()
15380       && parser->num_template_parameter_lists == 0
15381       && template_id_p)
15382     {
15383       error ("%Han explicit specialization must be preceded by %<template <>%>",
15384              &type_start_token->location);
15385       invalid_explicit_specialization_p = true;
15386       /* Take the same action that would have been taken by
15387          cp_parser_explicit_specialization.  */
15388       ++parser->num_template_parameter_lists;
15389       begin_specialization ();
15390     }
15391   /* There must be no "return" statements between this point and the
15392      end of this function; set "type "to the correct return value and
15393      use "goto done;" to return.  */
15394   /* Make sure that the right number of template parameters were
15395      present.  */
15396   if (!cp_parser_check_template_parameters (parser, num_templates,
15397                                             type_start_token->location,
15398                                             /*declarator=*/NULL))
15399     {
15400       /* If something went wrong, there is no point in even trying to
15401          process the class-definition.  */
15402       type = NULL_TREE;
15403       goto done;
15404     }
15405
15406   /* Look up the type.  */
15407   if (template_id_p)
15408     {
15409       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15410           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15411               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15412         {
15413           error ("%Hfunction template %qD redeclared as a class template",
15414                  &type_start_token->location, id);
15415           type = error_mark_node;
15416         }
15417       else
15418         {
15419           type = TREE_TYPE (id);
15420           type = maybe_process_partial_specialization (type);
15421         }
15422       if (nested_name_specifier)
15423         pushed_scope = push_scope (nested_name_specifier);
15424     }
15425   else if (nested_name_specifier)
15426     {
15427       tree class_type;
15428
15429       /* Given:
15430
15431             template <typename T> struct S { struct T };
15432             template <typename T> struct S<T>::T { };
15433
15434          we will get a TYPENAME_TYPE when processing the definition of
15435          `S::T'.  We need to resolve it to the actual type before we
15436          try to define it.  */
15437       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15438         {
15439           class_type = resolve_typename_type (TREE_TYPE (type),
15440                                               /*only_current_p=*/false);
15441           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15442             type = TYPE_NAME (class_type);
15443           else
15444             {
15445               cp_parser_error (parser, "could not resolve typename type");
15446               type = error_mark_node;
15447             }
15448         }
15449
15450       if (maybe_process_partial_specialization (TREE_TYPE (type))
15451           == error_mark_node)
15452         {
15453           type = NULL_TREE;
15454           goto done;
15455         }
15456
15457       class_type = current_class_type;
15458       /* Enter the scope indicated by the nested-name-specifier.  */
15459       pushed_scope = push_scope (nested_name_specifier);
15460       /* Get the canonical version of this type.  */
15461       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15462       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15463           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15464         {
15465           type = push_template_decl (type);
15466           if (type == error_mark_node)
15467             {
15468               type = NULL_TREE;
15469               goto done;
15470             }
15471         }
15472
15473       type = TREE_TYPE (type);
15474       *nested_name_specifier_p = true;
15475     }
15476   else      /* The name is not a nested name.  */
15477     {
15478       /* If the class was unnamed, create a dummy name.  */
15479       if (!id)
15480         id = make_anon_name ();
15481       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15482                        parser->num_template_parameter_lists);
15483     }
15484
15485   /* Indicate whether this class was declared as a `class' or as a
15486      `struct'.  */
15487   if (TREE_CODE (type) == RECORD_TYPE)
15488     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15489   cp_parser_check_class_key (class_key, type);
15490
15491   /* If this type was already complete, and we see another definition,
15492      that's an error.  */
15493   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15494     {
15495       error ("%Hredefinition of %q#T",
15496              &type_start_token->location, type);
15497       error ("%Hprevious definition of %q+#T",
15498              &type_start_token->location, type);
15499       type = NULL_TREE;
15500       goto done;
15501     }
15502   else if (type == error_mark_node)
15503     type = NULL_TREE;
15504
15505   /* We will have entered the scope containing the class; the names of
15506      base classes should be looked up in that context.  For example:
15507
15508        struct A { struct B {}; struct C; };
15509        struct A::C : B {};
15510
15511      is valid.  */
15512
15513   /* Get the list of base-classes, if there is one.  */
15514   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15515     *bases = cp_parser_base_clause (parser);
15516
15517  done:
15518   /* Leave the scope given by the nested-name-specifier.  We will
15519      enter the class scope itself while processing the members.  */
15520   if (pushed_scope)
15521     pop_scope (pushed_scope);
15522
15523   if (invalid_explicit_specialization_p)
15524     {
15525       end_specialization ();
15526       --parser->num_template_parameter_lists;
15527     }
15528   *attributes_p = attributes;
15529   return type;
15530 }
15531
15532 /* Parse a class-key.
15533
15534    class-key:
15535      class
15536      struct
15537      union
15538
15539    Returns the kind of class-key specified, or none_type to indicate
15540    error.  */
15541
15542 static enum tag_types
15543 cp_parser_class_key (cp_parser* parser)
15544 {
15545   cp_token *token;
15546   enum tag_types tag_type;
15547
15548   /* Look for the class-key.  */
15549   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15550   if (!token)
15551     return none_type;
15552
15553   /* Check to see if the TOKEN is a class-key.  */
15554   tag_type = cp_parser_token_is_class_key (token);
15555   if (!tag_type)
15556     cp_parser_error (parser, "expected class-key");
15557   return tag_type;
15558 }
15559
15560 /* Parse an (optional) member-specification.
15561
15562    member-specification:
15563      member-declaration member-specification [opt]
15564      access-specifier : member-specification [opt]  */
15565
15566 static void
15567 cp_parser_member_specification_opt (cp_parser* parser)
15568 {
15569   while (true)
15570     {
15571       cp_token *token;
15572       enum rid keyword;
15573
15574       /* Peek at the next token.  */
15575       token = cp_lexer_peek_token (parser->lexer);
15576       /* If it's a `}', or EOF then we've seen all the members.  */
15577       if (token->type == CPP_CLOSE_BRACE
15578           || token->type == CPP_EOF
15579           || token->type == CPP_PRAGMA_EOL)
15580         break;
15581
15582       /* See if this token is a keyword.  */
15583       keyword = token->keyword;
15584       switch (keyword)
15585         {
15586         case RID_PUBLIC:
15587         case RID_PROTECTED:
15588         case RID_PRIVATE:
15589           /* Consume the access-specifier.  */
15590           cp_lexer_consume_token (parser->lexer);
15591           /* Remember which access-specifier is active.  */
15592           current_access_specifier = token->u.value;
15593           /* Look for the `:'.  */
15594           cp_parser_require (parser, CPP_COLON, "%<:%>");
15595           break;
15596
15597         default:
15598           /* Accept #pragmas at class scope.  */
15599           if (token->type == CPP_PRAGMA)
15600             {
15601               cp_parser_pragma (parser, pragma_external);
15602               break;
15603             }
15604
15605           /* Otherwise, the next construction must be a
15606              member-declaration.  */
15607           cp_parser_member_declaration (parser);
15608         }
15609     }
15610 }
15611
15612 /* Parse a member-declaration.
15613
15614    member-declaration:
15615      decl-specifier-seq [opt] member-declarator-list [opt] ;
15616      function-definition ; [opt]
15617      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15618      using-declaration
15619      template-declaration
15620
15621    member-declarator-list:
15622      member-declarator
15623      member-declarator-list , member-declarator
15624
15625    member-declarator:
15626      declarator pure-specifier [opt]
15627      declarator constant-initializer [opt]
15628      identifier [opt] : constant-expression
15629
15630    GNU Extensions:
15631
15632    member-declaration:
15633      __extension__ member-declaration
15634
15635    member-declarator:
15636      declarator attributes [opt] pure-specifier [opt]
15637      declarator attributes [opt] constant-initializer [opt]
15638      identifier [opt] attributes [opt] : constant-expression  
15639
15640    C++0x Extensions:
15641
15642    member-declaration:
15643      static_assert-declaration  */
15644
15645 static void
15646 cp_parser_member_declaration (cp_parser* parser)
15647 {
15648   cp_decl_specifier_seq decl_specifiers;
15649   tree prefix_attributes;
15650   tree decl;
15651   int declares_class_or_enum;
15652   bool friend_p;
15653   cp_token *token = NULL;
15654   cp_token *decl_spec_token_start = NULL;
15655   cp_token *initializer_token_start = NULL;
15656   int saved_pedantic;
15657
15658   /* Check for the `__extension__' keyword.  */
15659   if (cp_parser_extension_opt (parser, &saved_pedantic))
15660     {
15661       /* Recurse.  */
15662       cp_parser_member_declaration (parser);
15663       /* Restore the old value of the PEDANTIC flag.  */
15664       pedantic = saved_pedantic;
15665
15666       return;
15667     }
15668
15669   /* Check for a template-declaration.  */
15670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15671     {
15672       /* An explicit specialization here is an error condition, and we
15673          expect the specialization handler to detect and report this.  */
15674       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15675           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15676         cp_parser_explicit_specialization (parser);
15677       else
15678         cp_parser_template_declaration (parser, /*member_p=*/true);
15679
15680       return;
15681     }
15682
15683   /* Check for a using-declaration.  */
15684   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15685     {
15686       /* Parse the using-declaration.  */
15687       cp_parser_using_declaration (parser,
15688                                    /*access_declaration_p=*/false);
15689       return;
15690     }
15691
15692   /* Check for @defs.  */
15693   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15694     {
15695       tree ivar, member;
15696       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15697       ivar = ivar_chains;
15698       while (ivar)
15699         {
15700           member = ivar;
15701           ivar = TREE_CHAIN (member);
15702           TREE_CHAIN (member) = NULL_TREE;
15703           finish_member_declaration (member);
15704         }
15705       return;
15706     }
15707
15708   /* If the next token is `static_assert' we have a static assertion.  */
15709   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15710     {
15711       cp_parser_static_assert (parser, /*member_p=*/true);
15712       return;
15713     }
15714
15715   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15716     return;
15717
15718   /* Parse the decl-specifier-seq.  */
15719   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15720   cp_parser_decl_specifier_seq (parser,
15721                                 CP_PARSER_FLAGS_OPTIONAL,
15722                                 &decl_specifiers,
15723                                 &declares_class_or_enum);
15724   prefix_attributes = decl_specifiers.attributes;
15725   decl_specifiers.attributes = NULL_TREE;
15726   /* Check for an invalid type-name.  */
15727   if (!decl_specifiers.type
15728       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15729     return;
15730   /* If there is no declarator, then the decl-specifier-seq should
15731      specify a type.  */
15732   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15733     {
15734       /* If there was no decl-specifier-seq, and the next token is a
15735          `;', then we have something like:
15736
15737            struct S { ; };
15738
15739          [class.mem]
15740
15741          Each member-declaration shall declare at least one member
15742          name of the class.  */
15743       if (!decl_specifiers.any_specifiers_p)
15744         {
15745           cp_token *token = cp_lexer_peek_token (parser->lexer);
15746           if (!in_system_header_at (token->location))
15747             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15748         }
15749       else
15750         {
15751           tree type;
15752
15753           /* See if this declaration is a friend.  */
15754           friend_p = cp_parser_friend_p (&decl_specifiers);
15755           /* If there were decl-specifiers, check to see if there was
15756              a class-declaration.  */
15757           type = check_tag_decl (&decl_specifiers);
15758           /* Nested classes have already been added to the class, but
15759              a `friend' needs to be explicitly registered.  */
15760           if (friend_p)
15761             {
15762               /* If the `friend' keyword was present, the friend must
15763                  be introduced with a class-key.  */
15764                if (!declares_class_or_enum)
15765                  error ("%Ha class-key must be used when declaring a friend",
15766                         &decl_spec_token_start->location);
15767                /* In this case:
15768
15769                     template <typename T> struct A {
15770                       friend struct A<T>::B;
15771                     };
15772
15773                   A<T>::B will be represented by a TYPENAME_TYPE, and
15774                   therefore not recognized by check_tag_decl.  */
15775                if (!type
15776                    && decl_specifiers.type
15777                    && TYPE_P (decl_specifiers.type))
15778                  type = decl_specifiers.type;
15779                if (!type || !TYPE_P (type))
15780                  error ("%Hfriend declaration does not name a class or "
15781                         "function", &decl_spec_token_start->location);
15782                else
15783                  make_friend_class (current_class_type, type,
15784                                     /*complain=*/true);
15785             }
15786           /* If there is no TYPE, an error message will already have
15787              been issued.  */
15788           else if (!type || type == error_mark_node)
15789             ;
15790           /* An anonymous aggregate has to be handled specially; such
15791              a declaration really declares a data member (with a
15792              particular type), as opposed to a nested class.  */
15793           else if (ANON_AGGR_TYPE_P (type))
15794             {
15795               /* Remove constructors and such from TYPE, now that we
15796                  know it is an anonymous aggregate.  */
15797               fixup_anonymous_aggr (type);
15798               /* And make the corresponding data member.  */
15799               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15800               /* Add it to the class.  */
15801               finish_member_declaration (decl);
15802             }
15803           else
15804             cp_parser_check_access_in_redeclaration
15805                                               (TYPE_NAME (type),
15806                                                decl_spec_token_start->location);
15807         }
15808     }
15809   else
15810     {
15811       /* See if these declarations will be friends.  */
15812       friend_p = cp_parser_friend_p (&decl_specifiers);
15813
15814       /* Keep going until we hit the `;' at the end of the
15815          declaration.  */
15816       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15817         {
15818           tree attributes = NULL_TREE;
15819           tree first_attribute;
15820
15821           /* Peek at the next token.  */
15822           token = cp_lexer_peek_token (parser->lexer);
15823
15824           /* Check for a bitfield declaration.  */
15825           if (token->type == CPP_COLON
15826               || (token->type == CPP_NAME
15827                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15828                   == CPP_COLON))
15829             {
15830               tree identifier;
15831               tree width;
15832
15833               /* Get the name of the bitfield.  Note that we cannot just
15834                  check TOKEN here because it may have been invalidated by
15835                  the call to cp_lexer_peek_nth_token above.  */
15836               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15837                 identifier = cp_parser_identifier (parser);
15838               else
15839                 identifier = NULL_TREE;
15840
15841               /* Consume the `:' token.  */
15842               cp_lexer_consume_token (parser->lexer);
15843               /* Get the width of the bitfield.  */
15844               width
15845                 = cp_parser_constant_expression (parser,
15846                                                  /*allow_non_constant=*/false,
15847                                                  NULL);
15848
15849               /* Look for attributes that apply to the bitfield.  */
15850               attributes = cp_parser_attributes_opt (parser);
15851               /* Remember which attributes are prefix attributes and
15852                  which are not.  */
15853               first_attribute = attributes;
15854               /* Combine the attributes.  */
15855               attributes = chainon (prefix_attributes, attributes);
15856
15857               /* Create the bitfield declaration.  */
15858               decl = grokbitfield (identifier
15859                                    ? make_id_declarator (NULL_TREE,
15860                                                          identifier,
15861                                                          sfk_none)
15862                                    : NULL,
15863                                    &decl_specifiers,
15864                                    width,
15865                                    attributes);
15866             }
15867           else
15868             {
15869               cp_declarator *declarator;
15870               tree initializer;
15871               tree asm_specification;
15872               int ctor_dtor_or_conv_p;
15873
15874               /* Parse the declarator.  */
15875               declarator
15876                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15877                                         &ctor_dtor_or_conv_p,
15878                                         /*parenthesized_p=*/NULL,
15879                                         /*member_p=*/true);
15880
15881               /* If something went wrong parsing the declarator, make sure
15882                  that we at least consume some tokens.  */
15883               if (declarator == cp_error_declarator)
15884                 {
15885                   /* Skip to the end of the statement.  */
15886                   cp_parser_skip_to_end_of_statement (parser);
15887                   /* If the next token is not a semicolon, that is
15888                      probably because we just skipped over the body of
15889                      a function.  So, we consume a semicolon if
15890                      present, but do not issue an error message if it
15891                      is not present.  */
15892                   if (cp_lexer_next_token_is (parser->lexer,
15893                                               CPP_SEMICOLON))
15894                     cp_lexer_consume_token (parser->lexer);
15895                   return;
15896                 }
15897
15898               if (declares_class_or_enum & 2)
15899                 cp_parser_check_for_definition_in_return_type
15900                                             (declarator, decl_specifiers.type,
15901                                              decl_specifiers.type_location);
15902
15903               /* Look for an asm-specification.  */
15904               asm_specification = cp_parser_asm_specification_opt (parser);
15905               /* Look for attributes that apply to the declaration.  */
15906               attributes = cp_parser_attributes_opt (parser);
15907               /* Remember which attributes are prefix attributes and
15908                  which are not.  */
15909               first_attribute = attributes;
15910               /* Combine the attributes.  */
15911               attributes = chainon (prefix_attributes, attributes);
15912
15913               /* If it's an `=', then we have a constant-initializer or a
15914                  pure-specifier.  It is not correct to parse the
15915                  initializer before registering the member declaration
15916                  since the member declaration should be in scope while
15917                  its initializer is processed.  However, the rest of the
15918                  front end does not yet provide an interface that allows
15919                  us to handle this correctly.  */
15920               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15921                 {
15922                   /* In [class.mem]:
15923
15924                      A pure-specifier shall be used only in the declaration of
15925                      a virtual function.
15926
15927                      A member-declarator can contain a constant-initializer
15928                      only if it declares a static member of integral or
15929                      enumeration type.
15930
15931                      Therefore, if the DECLARATOR is for a function, we look
15932                      for a pure-specifier; otherwise, we look for a
15933                      constant-initializer.  When we call `grokfield', it will
15934                      perform more stringent semantics checks.  */
15935                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15936                   if (function_declarator_p (declarator))
15937                     initializer = cp_parser_pure_specifier (parser);
15938                   else
15939                     /* Parse the initializer.  */
15940                     initializer = cp_parser_constant_initializer (parser);
15941                 }
15942               /* Otherwise, there is no initializer.  */
15943               else
15944                 initializer = NULL_TREE;
15945
15946               /* See if we are probably looking at a function
15947                  definition.  We are certainly not looking at a
15948                  member-declarator.  Calling `grokfield' has
15949                  side-effects, so we must not do it unless we are sure
15950                  that we are looking at a member-declarator.  */
15951               if (cp_parser_token_starts_function_definition_p
15952                   (cp_lexer_peek_token (parser->lexer)))
15953                 {
15954                   /* The grammar does not allow a pure-specifier to be
15955                      used when a member function is defined.  (It is
15956                      possible that this fact is an oversight in the
15957                      standard, since a pure function may be defined
15958                      outside of the class-specifier.  */
15959                   if (initializer)
15960                     error ("%Hpure-specifier on function-definition",
15961                            &initializer_token_start->location);
15962                   decl = cp_parser_save_member_function_body (parser,
15963                                                               &decl_specifiers,
15964                                                               declarator,
15965                                                               attributes);
15966                   /* If the member was not a friend, declare it here.  */
15967                   if (!friend_p)
15968                     finish_member_declaration (decl);
15969                   /* Peek at the next token.  */
15970                   token = cp_lexer_peek_token (parser->lexer);
15971                   /* If the next token is a semicolon, consume it.  */
15972                   if (token->type == CPP_SEMICOLON)
15973                     cp_lexer_consume_token (parser->lexer);
15974                   return;
15975                 }
15976               else
15977                 if (declarator->kind == cdk_function)
15978                   declarator->id_loc = token->location;
15979                 /* Create the declaration.  */
15980                 decl = grokfield (declarator, &decl_specifiers,
15981                                   initializer, /*init_const_expr_p=*/true,
15982                                   asm_specification,
15983                                   attributes);
15984             }
15985
15986           /* Reset PREFIX_ATTRIBUTES.  */
15987           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15988             attributes = TREE_CHAIN (attributes);
15989           if (attributes)
15990             TREE_CHAIN (attributes) = NULL_TREE;
15991
15992           /* If there is any qualification still in effect, clear it
15993              now; we will be starting fresh with the next declarator.  */
15994           parser->scope = NULL_TREE;
15995           parser->qualifying_scope = NULL_TREE;
15996           parser->object_scope = NULL_TREE;
15997           /* If it's a `,', then there are more declarators.  */
15998           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15999             cp_lexer_consume_token (parser->lexer);
16000           /* If the next token isn't a `;', then we have a parse error.  */
16001           else if (cp_lexer_next_token_is_not (parser->lexer,
16002                                                CPP_SEMICOLON))
16003             {
16004               cp_parser_error (parser, "expected %<;%>");
16005               /* Skip tokens until we find a `;'.  */
16006               cp_parser_skip_to_end_of_statement (parser);
16007
16008               break;
16009             }
16010
16011           if (decl)
16012             {
16013               /* Add DECL to the list of members.  */
16014               if (!friend_p)
16015                 finish_member_declaration (decl);
16016
16017               if (TREE_CODE (decl) == FUNCTION_DECL)
16018                 cp_parser_save_default_args (parser, decl);
16019             }
16020         }
16021     }
16022
16023   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16024 }
16025
16026 /* Parse a pure-specifier.
16027
16028    pure-specifier:
16029      = 0
16030
16031    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16032    Otherwise, ERROR_MARK_NODE is returned.  */
16033
16034 static tree
16035 cp_parser_pure_specifier (cp_parser* parser)
16036 {
16037   cp_token *token;
16038
16039   /* Look for the `=' token.  */
16040   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16041     return error_mark_node;
16042   /* Look for the `0' token.  */
16043   token = cp_lexer_peek_token (parser->lexer);
16044
16045   if (token->type == CPP_EOF
16046       || token->type == CPP_PRAGMA_EOL)
16047     return error_mark_node;
16048
16049   cp_lexer_consume_token (parser->lexer);
16050
16051   /* Accept = default or = delete in c++0x mode.  */
16052   if (token->keyword == RID_DEFAULT
16053       || token->keyword == RID_DELETE)
16054     {
16055       maybe_warn_cpp0x ("defaulted and deleted functions");
16056       return token->u.value;
16057     }
16058
16059   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16060   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16061     {
16062       cp_parser_error (parser,
16063                        "invalid pure specifier (only %<= 0%> is allowed)");
16064       cp_parser_skip_to_end_of_statement (parser);
16065       return error_mark_node;
16066     }
16067   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16068     {
16069       error ("%Htemplates may not be %<virtual%>", &token->location);
16070       return error_mark_node;
16071     }
16072
16073   return integer_zero_node;
16074 }
16075
16076 /* Parse a constant-initializer.
16077
16078    constant-initializer:
16079      = constant-expression
16080
16081    Returns a representation of the constant-expression.  */
16082
16083 static tree
16084 cp_parser_constant_initializer (cp_parser* parser)
16085 {
16086   /* Look for the `=' token.  */
16087   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16088     return error_mark_node;
16089
16090   /* It is invalid to write:
16091
16092        struct S { static const int i = { 7 }; };
16093
16094      */
16095   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16096     {
16097       cp_parser_error (parser,
16098                        "a brace-enclosed initializer is not allowed here");
16099       /* Consume the opening brace.  */
16100       cp_lexer_consume_token (parser->lexer);
16101       /* Skip the initializer.  */
16102       cp_parser_skip_to_closing_brace (parser);
16103       /* Look for the trailing `}'.  */
16104       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16105
16106       return error_mark_node;
16107     }
16108
16109   return cp_parser_constant_expression (parser,
16110                                         /*allow_non_constant=*/false,
16111                                         NULL);
16112 }
16113
16114 /* Derived classes [gram.class.derived] */
16115
16116 /* Parse a base-clause.
16117
16118    base-clause:
16119      : base-specifier-list
16120
16121    base-specifier-list:
16122      base-specifier ... [opt]
16123      base-specifier-list , base-specifier ... [opt]
16124
16125    Returns a TREE_LIST representing the base-classes, in the order in
16126    which they were declared.  The representation of each node is as
16127    described by cp_parser_base_specifier.
16128
16129    In the case that no bases are specified, this function will return
16130    NULL_TREE, not ERROR_MARK_NODE.  */
16131
16132 static tree
16133 cp_parser_base_clause (cp_parser* parser)
16134 {
16135   tree bases = NULL_TREE;
16136
16137   /* Look for the `:' that begins the list.  */
16138   cp_parser_require (parser, CPP_COLON, "%<:%>");
16139
16140   /* Scan the base-specifier-list.  */
16141   while (true)
16142     {
16143       cp_token *token;
16144       tree base;
16145       bool pack_expansion_p = false;
16146
16147       /* Look for the base-specifier.  */
16148       base = cp_parser_base_specifier (parser);
16149       /* Look for the (optional) ellipsis. */
16150       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16151         {
16152           /* Consume the `...'. */
16153           cp_lexer_consume_token (parser->lexer);
16154
16155           pack_expansion_p = true;
16156         }
16157
16158       /* Add BASE to the front of the list.  */
16159       if (base != error_mark_node)
16160         {
16161           if (pack_expansion_p)
16162             /* Make this a pack expansion type. */
16163             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16164           
16165
16166           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16167             {
16168               TREE_CHAIN (base) = bases;
16169               bases = base;
16170             }
16171         }
16172       /* Peek at the next token.  */
16173       token = cp_lexer_peek_token (parser->lexer);
16174       /* If it's not a comma, then the list is complete.  */
16175       if (token->type != CPP_COMMA)
16176         break;
16177       /* Consume the `,'.  */
16178       cp_lexer_consume_token (parser->lexer);
16179     }
16180
16181   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16182      base class had a qualified name.  However, the next name that
16183      appears is certainly not qualified.  */
16184   parser->scope = NULL_TREE;
16185   parser->qualifying_scope = NULL_TREE;
16186   parser->object_scope = NULL_TREE;
16187
16188   return nreverse (bases);
16189 }
16190
16191 /* Parse a base-specifier.
16192
16193    base-specifier:
16194      :: [opt] nested-name-specifier [opt] class-name
16195      virtual access-specifier [opt] :: [opt] nested-name-specifier
16196        [opt] class-name
16197      access-specifier virtual [opt] :: [opt] nested-name-specifier
16198        [opt] class-name
16199
16200    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16201    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16202    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16203    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16204
16205 static tree
16206 cp_parser_base_specifier (cp_parser* parser)
16207 {
16208   cp_token *token;
16209   bool done = false;
16210   bool virtual_p = false;
16211   bool duplicate_virtual_error_issued_p = false;
16212   bool duplicate_access_error_issued_p = false;
16213   bool class_scope_p, template_p;
16214   tree access = access_default_node;
16215   tree type;
16216
16217   /* Process the optional `virtual' and `access-specifier'.  */
16218   while (!done)
16219     {
16220       /* Peek at the next token.  */
16221       token = cp_lexer_peek_token (parser->lexer);
16222       /* Process `virtual'.  */
16223       switch (token->keyword)
16224         {
16225         case RID_VIRTUAL:
16226           /* If `virtual' appears more than once, issue an error.  */
16227           if (virtual_p && !duplicate_virtual_error_issued_p)
16228             {
16229               cp_parser_error (parser,
16230                                "%<virtual%> specified more than once in base-specified");
16231               duplicate_virtual_error_issued_p = true;
16232             }
16233
16234           virtual_p = true;
16235
16236           /* Consume the `virtual' token.  */
16237           cp_lexer_consume_token (parser->lexer);
16238
16239           break;
16240
16241         case RID_PUBLIC:
16242         case RID_PROTECTED:
16243         case RID_PRIVATE:
16244           /* If more than one access specifier appears, issue an
16245              error.  */
16246           if (access != access_default_node
16247               && !duplicate_access_error_issued_p)
16248             {
16249               cp_parser_error (parser,
16250                                "more than one access specifier in base-specified");
16251               duplicate_access_error_issued_p = true;
16252             }
16253
16254           access = ridpointers[(int) token->keyword];
16255
16256           /* Consume the access-specifier.  */
16257           cp_lexer_consume_token (parser->lexer);
16258
16259           break;
16260
16261         default:
16262           done = true;
16263           break;
16264         }
16265     }
16266   /* It is not uncommon to see programs mechanically, erroneously, use
16267      the 'typename' keyword to denote (dependent) qualified types
16268      as base classes.  */
16269   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16270     {
16271       token = cp_lexer_peek_token (parser->lexer);
16272       if (!processing_template_decl)
16273         error ("%Hkeyword %<typename%> not allowed outside of templates",
16274                &token->location);
16275       else
16276         error ("%Hkeyword %<typename%> not allowed in this context "
16277                "(the base class is implicitly a type)",
16278                &token->location);
16279       cp_lexer_consume_token (parser->lexer);
16280     }
16281
16282   /* Look for the optional `::' operator.  */
16283   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16284   /* Look for the nested-name-specifier.  The simplest way to
16285      implement:
16286
16287        [temp.res]
16288
16289        The keyword `typename' is not permitted in a base-specifier or
16290        mem-initializer; in these contexts a qualified name that
16291        depends on a template-parameter is implicitly assumed to be a
16292        type name.
16293
16294      is to pretend that we have seen the `typename' keyword at this
16295      point.  */
16296   cp_parser_nested_name_specifier_opt (parser,
16297                                        /*typename_keyword_p=*/true,
16298                                        /*check_dependency_p=*/true,
16299                                        typename_type,
16300                                        /*is_declaration=*/true);
16301   /* If the base class is given by a qualified name, assume that names
16302      we see are type names or templates, as appropriate.  */
16303   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16304   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16305
16306   /* Finally, look for the class-name.  */
16307   type = cp_parser_class_name (parser,
16308                                class_scope_p,
16309                                template_p,
16310                                typename_type,
16311                                /*check_dependency_p=*/true,
16312                                /*class_head_p=*/false,
16313                                /*is_declaration=*/true);
16314
16315   if (type == error_mark_node)
16316     return error_mark_node;
16317
16318   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16319 }
16320
16321 /* Exception handling [gram.exception] */
16322
16323 /* Parse an (optional) exception-specification.
16324
16325    exception-specification:
16326      throw ( type-id-list [opt] )
16327
16328    Returns a TREE_LIST representing the exception-specification.  The
16329    TREE_VALUE of each node is a type.  */
16330
16331 static tree
16332 cp_parser_exception_specification_opt (cp_parser* parser)
16333 {
16334   cp_token *token;
16335   tree type_id_list;
16336
16337   /* Peek at the next token.  */
16338   token = cp_lexer_peek_token (parser->lexer);
16339   /* If it's not `throw', then there's no exception-specification.  */
16340   if (!cp_parser_is_keyword (token, RID_THROW))
16341     return NULL_TREE;
16342
16343   /* Consume the `throw'.  */
16344   cp_lexer_consume_token (parser->lexer);
16345
16346   /* Look for the `('.  */
16347   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16348
16349   /* Peek at the next token.  */
16350   token = cp_lexer_peek_token (parser->lexer);
16351   /* If it's not a `)', then there is a type-id-list.  */
16352   if (token->type != CPP_CLOSE_PAREN)
16353     {
16354       const char *saved_message;
16355
16356       /* Types may not be defined in an exception-specification.  */
16357       saved_message = parser->type_definition_forbidden_message;
16358       parser->type_definition_forbidden_message
16359         = "types may not be defined in an exception-specification";
16360       /* Parse the type-id-list.  */
16361       type_id_list = cp_parser_type_id_list (parser);
16362       /* Restore the saved message.  */
16363       parser->type_definition_forbidden_message = saved_message;
16364     }
16365   else
16366     type_id_list = empty_except_spec;
16367
16368   /* Look for the `)'.  */
16369   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16370
16371   return type_id_list;
16372 }
16373
16374 /* Parse an (optional) type-id-list.
16375
16376    type-id-list:
16377      type-id ... [opt]
16378      type-id-list , type-id ... [opt]
16379
16380    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16381    in the order that the types were presented.  */
16382
16383 static tree
16384 cp_parser_type_id_list (cp_parser* parser)
16385 {
16386   tree types = NULL_TREE;
16387
16388   while (true)
16389     {
16390       cp_token *token;
16391       tree type;
16392
16393       /* Get the next type-id.  */
16394       type = cp_parser_type_id (parser);
16395       /* Parse the optional ellipsis. */
16396       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16397         {
16398           /* Consume the `...'. */
16399           cp_lexer_consume_token (parser->lexer);
16400
16401           /* Turn the type into a pack expansion expression. */
16402           type = make_pack_expansion (type);
16403         }
16404       /* Add it to the list.  */
16405       types = add_exception_specifier (types, type, /*complain=*/1);
16406       /* Peek at the next token.  */
16407       token = cp_lexer_peek_token (parser->lexer);
16408       /* If it is not a `,', we are done.  */
16409       if (token->type != CPP_COMMA)
16410         break;
16411       /* Consume the `,'.  */
16412       cp_lexer_consume_token (parser->lexer);
16413     }
16414
16415   return nreverse (types);
16416 }
16417
16418 /* Parse a try-block.
16419
16420    try-block:
16421      try compound-statement handler-seq  */
16422
16423 static tree
16424 cp_parser_try_block (cp_parser* parser)
16425 {
16426   tree try_block;
16427
16428   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16429   try_block = begin_try_block ();
16430   cp_parser_compound_statement (parser, NULL, true);
16431   finish_try_block (try_block);
16432   cp_parser_handler_seq (parser);
16433   finish_handler_sequence (try_block);
16434
16435   return try_block;
16436 }
16437
16438 /* Parse a function-try-block.
16439
16440    function-try-block:
16441      try ctor-initializer [opt] function-body handler-seq  */
16442
16443 static bool
16444 cp_parser_function_try_block (cp_parser* parser)
16445 {
16446   tree compound_stmt;
16447   tree try_block;
16448   bool ctor_initializer_p;
16449
16450   /* Look for the `try' keyword.  */
16451   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16452     return false;
16453   /* Let the rest of the front end know where we are.  */
16454   try_block = begin_function_try_block (&compound_stmt);
16455   /* Parse the function-body.  */
16456   ctor_initializer_p
16457     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16458   /* We're done with the `try' part.  */
16459   finish_function_try_block (try_block);
16460   /* Parse the handlers.  */
16461   cp_parser_handler_seq (parser);
16462   /* We're done with the handlers.  */
16463   finish_function_handler_sequence (try_block, compound_stmt);
16464
16465   return ctor_initializer_p;
16466 }
16467
16468 /* Parse a handler-seq.
16469
16470    handler-seq:
16471      handler handler-seq [opt]  */
16472
16473 static void
16474 cp_parser_handler_seq (cp_parser* parser)
16475 {
16476   while (true)
16477     {
16478       cp_token *token;
16479
16480       /* Parse the handler.  */
16481       cp_parser_handler (parser);
16482       /* Peek at the next token.  */
16483       token = cp_lexer_peek_token (parser->lexer);
16484       /* If it's not `catch' then there are no more handlers.  */
16485       if (!cp_parser_is_keyword (token, RID_CATCH))
16486         break;
16487     }
16488 }
16489
16490 /* Parse a handler.
16491
16492    handler:
16493      catch ( exception-declaration ) compound-statement  */
16494
16495 static void
16496 cp_parser_handler (cp_parser* parser)
16497 {
16498   tree handler;
16499   tree declaration;
16500
16501   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16502   handler = begin_handler ();
16503   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16504   declaration = cp_parser_exception_declaration (parser);
16505   finish_handler_parms (declaration, handler);
16506   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16507   cp_parser_compound_statement (parser, NULL, false);
16508   finish_handler (handler);
16509 }
16510
16511 /* Parse an exception-declaration.
16512
16513    exception-declaration:
16514      type-specifier-seq declarator
16515      type-specifier-seq abstract-declarator
16516      type-specifier-seq
16517      ...
16518
16519    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16520    ellipsis variant is used.  */
16521
16522 static tree
16523 cp_parser_exception_declaration (cp_parser* parser)
16524 {
16525   cp_decl_specifier_seq type_specifiers;
16526   cp_declarator *declarator;
16527   const char *saved_message;
16528
16529   /* If it's an ellipsis, it's easy to handle.  */
16530   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16531     {
16532       /* Consume the `...' token.  */
16533       cp_lexer_consume_token (parser->lexer);
16534       return NULL_TREE;
16535     }
16536
16537   /* Types may not be defined in exception-declarations.  */
16538   saved_message = parser->type_definition_forbidden_message;
16539   parser->type_definition_forbidden_message
16540     = "types may not be defined in exception-declarations";
16541
16542   /* Parse the type-specifier-seq.  */
16543   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16544                                 &type_specifiers);
16545   /* If it's a `)', then there is no declarator.  */
16546   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16547     declarator = NULL;
16548   else
16549     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16550                                        /*ctor_dtor_or_conv_p=*/NULL,
16551                                        /*parenthesized_p=*/NULL,
16552                                        /*member_p=*/false);
16553
16554   /* Restore the saved message.  */
16555   parser->type_definition_forbidden_message = saved_message;
16556
16557   if (!type_specifiers.any_specifiers_p)
16558     return error_mark_node;
16559
16560   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16561 }
16562
16563 /* Parse a throw-expression.
16564
16565    throw-expression:
16566      throw assignment-expression [opt]
16567
16568    Returns a THROW_EXPR representing the throw-expression.  */
16569
16570 static tree
16571 cp_parser_throw_expression (cp_parser* parser)
16572 {
16573   tree expression;
16574   cp_token* token;
16575
16576   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16577   token = cp_lexer_peek_token (parser->lexer);
16578   /* Figure out whether or not there is an assignment-expression
16579      following the "throw" keyword.  */
16580   if (token->type == CPP_COMMA
16581       || token->type == CPP_SEMICOLON
16582       || token->type == CPP_CLOSE_PAREN
16583       || token->type == CPP_CLOSE_SQUARE
16584       || token->type == CPP_CLOSE_BRACE
16585       || token->type == CPP_COLON)
16586     expression = NULL_TREE;
16587   else
16588     expression = cp_parser_assignment_expression (parser,
16589                                                   /*cast_p=*/false, NULL);
16590
16591   return build_throw (expression);
16592 }
16593
16594 /* GNU Extensions */
16595
16596 /* Parse an (optional) asm-specification.
16597
16598    asm-specification:
16599      asm ( string-literal )
16600
16601    If the asm-specification is present, returns a STRING_CST
16602    corresponding to the string-literal.  Otherwise, returns
16603    NULL_TREE.  */
16604
16605 static tree
16606 cp_parser_asm_specification_opt (cp_parser* parser)
16607 {
16608   cp_token *token;
16609   tree asm_specification;
16610
16611   /* Peek at the next token.  */
16612   token = cp_lexer_peek_token (parser->lexer);
16613   /* If the next token isn't the `asm' keyword, then there's no
16614      asm-specification.  */
16615   if (!cp_parser_is_keyword (token, RID_ASM))
16616     return NULL_TREE;
16617
16618   /* Consume the `asm' token.  */
16619   cp_lexer_consume_token (parser->lexer);
16620   /* Look for the `('.  */
16621   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16622
16623   /* Look for the string-literal.  */
16624   asm_specification = cp_parser_string_literal (parser, false, false);
16625
16626   /* Look for the `)'.  */
16627   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16628
16629   return asm_specification;
16630 }
16631
16632 /* Parse an asm-operand-list.
16633
16634    asm-operand-list:
16635      asm-operand
16636      asm-operand-list , asm-operand
16637
16638    asm-operand:
16639      string-literal ( expression )
16640      [ string-literal ] string-literal ( expression )
16641
16642    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16643    each node is the expression.  The TREE_PURPOSE is itself a
16644    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16645    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16646    is a STRING_CST for the string literal before the parenthesis. Returns
16647    ERROR_MARK_NODE if any of the operands are invalid.  */
16648
16649 static tree
16650 cp_parser_asm_operand_list (cp_parser* parser)
16651 {
16652   tree asm_operands = NULL_TREE;
16653   bool invalid_operands = false;
16654
16655   while (true)
16656     {
16657       tree string_literal;
16658       tree expression;
16659       tree name;
16660
16661       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16662         {
16663           /* Consume the `[' token.  */
16664           cp_lexer_consume_token (parser->lexer);
16665           /* Read the operand name.  */
16666           name = cp_parser_identifier (parser);
16667           if (name != error_mark_node)
16668             name = build_string (IDENTIFIER_LENGTH (name),
16669                                  IDENTIFIER_POINTER (name));
16670           /* Look for the closing `]'.  */
16671           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16672         }
16673       else
16674         name = NULL_TREE;
16675       /* Look for the string-literal.  */
16676       string_literal = cp_parser_string_literal (parser, false, false);
16677
16678       /* Look for the `('.  */
16679       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16680       /* Parse the expression.  */
16681       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16682       /* Look for the `)'.  */
16683       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16684
16685       if (name == error_mark_node 
16686           || string_literal == error_mark_node 
16687           || expression == error_mark_node)
16688         invalid_operands = true;
16689
16690       /* Add this operand to the list.  */
16691       asm_operands = tree_cons (build_tree_list (name, string_literal),
16692                                 expression,
16693                                 asm_operands);
16694       /* If the next token is not a `,', there are no more
16695          operands.  */
16696       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16697         break;
16698       /* Consume the `,'.  */
16699       cp_lexer_consume_token (parser->lexer);
16700     }
16701
16702   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16703 }
16704
16705 /* Parse an asm-clobber-list.
16706
16707    asm-clobber-list:
16708      string-literal
16709      asm-clobber-list , string-literal
16710
16711    Returns a TREE_LIST, indicating the clobbers in the order that they
16712    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16713
16714 static tree
16715 cp_parser_asm_clobber_list (cp_parser* parser)
16716 {
16717   tree clobbers = NULL_TREE;
16718
16719   while (true)
16720     {
16721       tree string_literal;
16722
16723       /* Look for the string literal.  */
16724       string_literal = cp_parser_string_literal (parser, false, false);
16725       /* Add it to the list.  */
16726       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16727       /* If the next token is not a `,', then the list is
16728          complete.  */
16729       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16730         break;
16731       /* Consume the `,' token.  */
16732       cp_lexer_consume_token (parser->lexer);
16733     }
16734
16735   return clobbers;
16736 }
16737
16738 /* Parse an (optional) series of attributes.
16739
16740    attributes:
16741      attributes attribute
16742
16743    attribute:
16744      __attribute__ (( attribute-list [opt] ))
16745
16746    The return value is as for cp_parser_attribute_list.  */
16747
16748 static tree
16749 cp_parser_attributes_opt (cp_parser* parser)
16750 {
16751   tree attributes = NULL_TREE;
16752
16753   while (true)
16754     {
16755       cp_token *token;
16756       tree attribute_list;
16757
16758       /* Peek at the next token.  */
16759       token = cp_lexer_peek_token (parser->lexer);
16760       /* If it's not `__attribute__', then we're done.  */
16761       if (token->keyword != RID_ATTRIBUTE)
16762         break;
16763
16764       /* Consume the `__attribute__' keyword.  */
16765       cp_lexer_consume_token (parser->lexer);
16766       /* Look for the two `(' tokens.  */
16767       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16768       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16769
16770       /* Peek at the next token.  */
16771       token = cp_lexer_peek_token (parser->lexer);
16772       if (token->type != CPP_CLOSE_PAREN)
16773         /* Parse the attribute-list.  */
16774         attribute_list = cp_parser_attribute_list (parser);
16775       else
16776         /* If the next token is a `)', then there is no attribute
16777            list.  */
16778         attribute_list = NULL;
16779
16780       /* Look for the two `)' tokens.  */
16781       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16782       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16783
16784       /* Add these new attributes to the list.  */
16785       attributes = chainon (attributes, attribute_list);
16786     }
16787
16788   return attributes;
16789 }
16790
16791 /* Parse an attribute-list.
16792
16793    attribute-list:
16794      attribute
16795      attribute-list , attribute
16796
16797    attribute:
16798      identifier
16799      identifier ( identifier )
16800      identifier ( identifier , expression-list )
16801      identifier ( expression-list )
16802
16803    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16804    to an attribute.  The TREE_PURPOSE of each node is the identifier
16805    indicating which attribute is in use.  The TREE_VALUE represents
16806    the arguments, if any.  */
16807
16808 static tree
16809 cp_parser_attribute_list (cp_parser* parser)
16810 {
16811   tree attribute_list = NULL_TREE;
16812   bool save_translate_strings_p = parser->translate_strings_p;
16813
16814   parser->translate_strings_p = false;
16815   while (true)
16816     {
16817       cp_token *token;
16818       tree identifier;
16819       tree attribute;
16820
16821       /* Look for the identifier.  We also allow keywords here; for
16822          example `__attribute__ ((const))' is legal.  */
16823       token = cp_lexer_peek_token (parser->lexer);
16824       if (token->type == CPP_NAME
16825           || token->type == CPP_KEYWORD)
16826         {
16827           tree arguments = NULL_TREE;
16828
16829           /* Consume the token.  */
16830           token = cp_lexer_consume_token (parser->lexer);
16831
16832           /* Save away the identifier that indicates which attribute
16833              this is.  */
16834           identifier = token->u.value;
16835           attribute = build_tree_list (identifier, NULL_TREE);
16836
16837           /* Peek at the next token.  */
16838           token = cp_lexer_peek_token (parser->lexer);
16839           /* If it's an `(', then parse the attribute arguments.  */
16840           if (token->type == CPP_OPEN_PAREN)
16841             {
16842               arguments = cp_parser_parenthesized_expression_list
16843                           (parser, true, /*cast_p=*/false,
16844                            /*allow_expansion_p=*/false,
16845                            /*non_constant_p=*/NULL);
16846               /* Save the arguments away.  */
16847               TREE_VALUE (attribute) = arguments;
16848             }
16849
16850           if (arguments != error_mark_node)
16851             {
16852               /* Add this attribute to the list.  */
16853               TREE_CHAIN (attribute) = attribute_list;
16854               attribute_list = attribute;
16855             }
16856
16857           token = cp_lexer_peek_token (parser->lexer);
16858         }
16859       /* Now, look for more attributes.  If the next token isn't a
16860          `,', we're done.  */
16861       if (token->type != CPP_COMMA)
16862         break;
16863
16864       /* Consume the comma and keep going.  */
16865       cp_lexer_consume_token (parser->lexer);
16866     }
16867   parser->translate_strings_p = save_translate_strings_p;
16868
16869   /* We built up the list in reverse order.  */
16870   return nreverse (attribute_list);
16871 }
16872
16873 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16874    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16875    current value of the PEDANTIC flag, regardless of whether or not
16876    the `__extension__' keyword is present.  The caller is responsible
16877    for restoring the value of the PEDANTIC flag.  */
16878
16879 static bool
16880 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16881 {
16882   /* Save the old value of the PEDANTIC flag.  */
16883   *saved_pedantic = pedantic;
16884
16885   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16886     {
16887       /* Consume the `__extension__' token.  */
16888       cp_lexer_consume_token (parser->lexer);
16889       /* We're not being pedantic while the `__extension__' keyword is
16890          in effect.  */
16891       pedantic = 0;
16892
16893       return true;
16894     }
16895
16896   return false;
16897 }
16898
16899 /* Parse a label declaration.
16900
16901    label-declaration:
16902      __label__ label-declarator-seq ;
16903
16904    label-declarator-seq:
16905      identifier , label-declarator-seq
16906      identifier  */
16907
16908 static void
16909 cp_parser_label_declaration (cp_parser* parser)
16910 {
16911   /* Look for the `__label__' keyword.  */
16912   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16913
16914   while (true)
16915     {
16916       tree identifier;
16917
16918       /* Look for an identifier.  */
16919       identifier = cp_parser_identifier (parser);
16920       /* If we failed, stop.  */
16921       if (identifier == error_mark_node)
16922         break;
16923       /* Declare it as a label.  */
16924       finish_label_decl (identifier);
16925       /* If the next token is a `;', stop.  */
16926       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16927         break;
16928       /* Look for the `,' separating the label declarations.  */
16929       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16930     }
16931
16932   /* Look for the final `;'.  */
16933   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16934 }
16935
16936 /* Support Functions */
16937
16938 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16939    NAME should have one of the representations used for an
16940    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16941    is returned.  If PARSER->SCOPE is a dependent type, then a
16942    SCOPE_REF is returned.
16943
16944    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16945    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16946    was formed.  Abstractly, such entities should not be passed to this
16947    function, because they do not need to be looked up, but it is
16948    simpler to check for this special case here, rather than at the
16949    call-sites.
16950
16951    In cases not explicitly covered above, this function returns a
16952    DECL, OVERLOAD, or baselink representing the result of the lookup.
16953    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16954    is returned.
16955
16956    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16957    (e.g., "struct") that was used.  In that case bindings that do not
16958    refer to types are ignored.
16959
16960    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16961    ignored.
16962
16963    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16964    are ignored.
16965
16966    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16967    types.
16968
16969    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16970    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16971    NULL_TREE otherwise.  */
16972
16973 static tree
16974 cp_parser_lookup_name (cp_parser *parser, tree name,
16975                        enum tag_types tag_type,
16976                        bool is_template,
16977                        bool is_namespace,
16978                        bool check_dependency,
16979                        tree *ambiguous_decls,
16980                        location_t name_location)
16981 {
16982   int flags = 0;
16983   tree decl;
16984   tree object_type = parser->context->object_type;
16985
16986   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16987     flags |= LOOKUP_COMPLAIN;
16988
16989   /* Assume that the lookup will be unambiguous.  */
16990   if (ambiguous_decls)
16991     *ambiguous_decls = NULL_TREE;
16992
16993   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16994      no longer valid.  Note that if we are parsing tentatively, and
16995      the parse fails, OBJECT_TYPE will be automatically restored.  */
16996   parser->context->object_type = NULL_TREE;
16997
16998   if (name == error_mark_node)
16999     return error_mark_node;
17000
17001   /* A template-id has already been resolved; there is no lookup to
17002      do.  */
17003   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17004     return name;
17005   if (BASELINK_P (name))
17006     {
17007       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17008                   == TEMPLATE_ID_EXPR);
17009       return name;
17010     }
17011
17012   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17013      it should already have been checked to make sure that the name
17014      used matches the type being destroyed.  */
17015   if (TREE_CODE (name) == BIT_NOT_EXPR)
17016     {
17017       tree type;
17018
17019       /* Figure out to which type this destructor applies.  */
17020       if (parser->scope)
17021         type = parser->scope;
17022       else if (object_type)
17023         type = object_type;
17024       else
17025         type = current_class_type;
17026       /* If that's not a class type, there is no destructor.  */
17027       if (!type || !CLASS_TYPE_P (type))
17028         return error_mark_node;
17029       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17030         lazily_declare_fn (sfk_destructor, type);
17031       if (!CLASSTYPE_DESTRUCTORS (type))
17032           return error_mark_node;
17033       /* If it was a class type, return the destructor.  */
17034       return CLASSTYPE_DESTRUCTORS (type);
17035     }
17036
17037   /* By this point, the NAME should be an ordinary identifier.  If
17038      the id-expression was a qualified name, the qualifying scope is
17039      stored in PARSER->SCOPE at this point.  */
17040   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17041
17042   /* Perform the lookup.  */
17043   if (parser->scope)
17044     {
17045       bool dependent_p;
17046
17047       if (parser->scope == error_mark_node)
17048         return error_mark_node;
17049
17050       /* If the SCOPE is dependent, the lookup must be deferred until
17051          the template is instantiated -- unless we are explicitly
17052          looking up names in uninstantiated templates.  Even then, we
17053          cannot look up the name if the scope is not a class type; it
17054          might, for example, be a template type parameter.  */
17055       dependent_p = (TYPE_P (parser->scope)
17056                      && dependent_scope_p (parser->scope));
17057       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17058           && dependent_p)
17059         /* Defer lookup.  */
17060         decl = error_mark_node;
17061       else
17062         {
17063           tree pushed_scope = NULL_TREE;
17064
17065           /* If PARSER->SCOPE is a dependent type, then it must be a
17066              class type, and we must not be checking dependencies;
17067              otherwise, we would have processed this lookup above.  So
17068              that PARSER->SCOPE is not considered a dependent base by
17069              lookup_member, we must enter the scope here.  */
17070           if (dependent_p)
17071             pushed_scope = push_scope (parser->scope);
17072           /* If the PARSER->SCOPE is a template specialization, it
17073              may be instantiated during name lookup.  In that case,
17074              errors may be issued.  Even if we rollback the current
17075              tentative parse, those errors are valid.  */
17076           decl = lookup_qualified_name (parser->scope, name,
17077                                         tag_type != none_type,
17078                                         /*complain=*/true);
17079
17080           /* If we have a single function from a using decl, pull it out.  */
17081           if (TREE_CODE (decl) == OVERLOAD
17082               && !really_overloaded_fn (decl))
17083             decl = OVL_FUNCTION (decl);
17084
17085           if (pushed_scope)
17086             pop_scope (pushed_scope);
17087         }
17088
17089       /* If the scope is a dependent type and either we deferred lookup or
17090          we did lookup but didn't find the name, rememeber the name.  */
17091       if (decl == error_mark_node && TYPE_P (parser->scope)
17092           && dependent_type_p (parser->scope))
17093         {
17094           if (tag_type)
17095             {
17096               tree type;
17097
17098               /* The resolution to Core Issue 180 says that `struct
17099                  A::B' should be considered a type-name, even if `A'
17100                  is dependent.  */
17101               type = make_typename_type (parser->scope, name, tag_type,
17102                                          /*complain=*/tf_error);
17103               decl = TYPE_NAME (type);
17104             }
17105           else if (is_template
17106                    && (cp_parser_next_token_ends_template_argument_p (parser)
17107                        || cp_lexer_next_token_is (parser->lexer,
17108                                                   CPP_CLOSE_PAREN)))
17109             decl = make_unbound_class_template (parser->scope,
17110                                                 name, NULL_TREE,
17111                                                 /*complain=*/tf_error);
17112           else
17113             decl = build_qualified_name (/*type=*/NULL_TREE,
17114                                          parser->scope, name,
17115                                          is_template);
17116         }
17117       parser->qualifying_scope = parser->scope;
17118       parser->object_scope = NULL_TREE;
17119     }
17120   else if (object_type)
17121     {
17122       tree object_decl = NULL_TREE;
17123       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17124          OBJECT_TYPE is not a class.  */
17125       if (CLASS_TYPE_P (object_type))
17126         /* If the OBJECT_TYPE is a template specialization, it may
17127            be instantiated during name lookup.  In that case, errors
17128            may be issued.  Even if we rollback the current tentative
17129            parse, those errors are valid.  */
17130         object_decl = lookup_member (object_type,
17131                                      name,
17132                                      /*protect=*/0,
17133                                      tag_type != none_type);
17134       /* Look it up in the enclosing context, too.  */
17135       decl = lookup_name_real (name, tag_type != none_type,
17136                                /*nonclass=*/0,
17137                                /*block_p=*/true, is_namespace, flags);
17138       parser->object_scope = object_type;
17139       parser->qualifying_scope = NULL_TREE;
17140       if (object_decl)
17141         decl = object_decl;
17142     }
17143   else
17144     {
17145       decl = lookup_name_real (name, tag_type != none_type,
17146                                /*nonclass=*/0,
17147                                /*block_p=*/true, is_namespace, flags);
17148       parser->qualifying_scope = NULL_TREE;
17149       parser->object_scope = NULL_TREE;
17150     }
17151
17152   /* If the lookup failed, let our caller know.  */
17153   if (!decl || decl == error_mark_node)
17154     return error_mark_node;
17155
17156   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17157   if (TREE_CODE (decl) == TREE_LIST)
17158     {
17159       if (ambiguous_decls)
17160         *ambiguous_decls = decl;
17161       /* The error message we have to print is too complicated for
17162          cp_parser_error, so we incorporate its actions directly.  */
17163       if (!cp_parser_simulate_error (parser))
17164         {
17165           error ("%Hreference to %qD is ambiguous",
17166                  &name_location, name);
17167           print_candidates (decl);
17168         }
17169       return error_mark_node;
17170     }
17171
17172   gcc_assert (DECL_P (decl)
17173               || TREE_CODE (decl) == OVERLOAD
17174               || TREE_CODE (decl) == SCOPE_REF
17175               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17176               || BASELINK_P (decl));
17177
17178   /* If we have resolved the name of a member declaration, check to
17179      see if the declaration is accessible.  When the name resolves to
17180      set of overloaded functions, accessibility is checked when
17181      overload resolution is done.
17182
17183      During an explicit instantiation, access is not checked at all,
17184      as per [temp.explicit].  */
17185   if (DECL_P (decl))
17186     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17187
17188   return decl;
17189 }
17190
17191 /* Like cp_parser_lookup_name, but for use in the typical case where
17192    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17193    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17194
17195 static tree
17196 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17197 {
17198   return cp_parser_lookup_name (parser, name,
17199                                 none_type,
17200                                 /*is_template=*/false,
17201                                 /*is_namespace=*/false,
17202                                 /*check_dependency=*/true,
17203                                 /*ambiguous_decls=*/NULL,
17204                                 location);
17205 }
17206
17207 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17208    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17209    true, the DECL indicates the class being defined in a class-head,
17210    or declared in an elaborated-type-specifier.
17211
17212    Otherwise, return DECL.  */
17213
17214 static tree
17215 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17216 {
17217   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17218      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17219
17220        struct A {
17221          template <typename T> struct B;
17222        };
17223
17224        template <typename T> struct A::B {};
17225
17226      Similarly, in an elaborated-type-specifier:
17227
17228        namespace N { struct X{}; }
17229
17230        struct A {
17231          template <typename T> friend struct N::X;
17232        };
17233
17234      However, if the DECL refers to a class type, and we are in
17235      the scope of the class, then the name lookup automatically
17236      finds the TYPE_DECL created by build_self_reference rather
17237      than a TEMPLATE_DECL.  For example, in:
17238
17239        template <class T> struct S {
17240          S s;
17241        };
17242
17243      there is no need to handle such case.  */
17244
17245   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17246     return DECL_TEMPLATE_RESULT (decl);
17247
17248   return decl;
17249 }
17250
17251 /* If too many, or too few, template-parameter lists apply to the
17252    declarator, issue an error message.  Returns TRUE if all went well,
17253    and FALSE otherwise.  */
17254
17255 static bool
17256 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17257                                                 cp_declarator *declarator,
17258                                                 location_t declarator_location)
17259 {
17260   unsigned num_templates;
17261
17262   /* We haven't seen any classes that involve template parameters yet.  */
17263   num_templates = 0;
17264
17265   switch (declarator->kind)
17266     {
17267     case cdk_id:
17268       if (declarator->u.id.qualifying_scope)
17269         {
17270           tree scope;
17271           tree member;
17272
17273           scope = declarator->u.id.qualifying_scope;
17274           member = declarator->u.id.unqualified_name;
17275
17276           while (scope && CLASS_TYPE_P (scope))
17277             {
17278               /* You're supposed to have one `template <...>'
17279                  for every template class, but you don't need one
17280                  for a full specialization.  For example:
17281
17282                  template <class T> struct S{};
17283                  template <> struct S<int> { void f(); };
17284                  void S<int>::f () {}
17285
17286                  is correct; there shouldn't be a `template <>' for
17287                  the definition of `S<int>::f'.  */
17288               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17289                 /* If SCOPE does not have template information of any
17290                    kind, then it is not a template, nor is it nested
17291                    within a template.  */
17292                 break;
17293               if (explicit_class_specialization_p (scope))
17294                 break;
17295               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17296                 ++num_templates;
17297
17298               scope = TYPE_CONTEXT (scope);
17299             }
17300         }
17301       else if (TREE_CODE (declarator->u.id.unqualified_name)
17302                == TEMPLATE_ID_EXPR)
17303         /* If the DECLARATOR has the form `X<y>' then it uses one
17304            additional level of template parameters.  */
17305         ++num_templates;
17306
17307       return cp_parser_check_template_parameters 
17308         (parser, num_templates, declarator_location, declarator);
17309
17310
17311     case cdk_function:
17312     case cdk_array:
17313     case cdk_pointer:
17314     case cdk_reference:
17315     case cdk_ptrmem:
17316       return (cp_parser_check_declarator_template_parameters
17317               (parser, declarator->declarator, declarator_location));
17318
17319     case cdk_error:
17320       return true;
17321
17322     default:
17323       gcc_unreachable ();
17324     }
17325   return false;
17326 }
17327
17328 /* NUM_TEMPLATES were used in the current declaration.  If that is
17329    invalid, return FALSE and issue an error messages.  Otherwise,
17330    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17331    declarator and we can print more accurate diagnostics.  */
17332
17333 static bool
17334 cp_parser_check_template_parameters (cp_parser* parser,
17335                                      unsigned num_templates,
17336                                      location_t location,
17337                                      cp_declarator *declarator)
17338 {
17339   /* If there are the same number of template classes and parameter
17340      lists, that's OK.  */
17341   if (parser->num_template_parameter_lists == num_templates)
17342     return true;
17343   /* If there are more, but only one more, then we are referring to a
17344      member template.  That's OK too.  */
17345   if (parser->num_template_parameter_lists == num_templates + 1)
17346     return true;
17347   /* If there are more template classes than parameter lists, we have
17348      something like:
17349
17350        template <class T> void S<T>::R<T>::f ();  */
17351   if (parser->num_template_parameter_lists < num_templates)
17352     {
17353       if (declarator)
17354         error_at (location, "specializing member %<%T::%E%> "
17355                   "requires %<template<>%> syntax", 
17356                   declarator->u.id.qualifying_scope,
17357                   declarator->u.id.unqualified_name);
17358       else 
17359         error_at (location, "too few template-parameter-lists");
17360       return false;
17361     }
17362   /* Otherwise, there are too many template parameter lists.  We have
17363      something like:
17364
17365      template <class T> template <class U> void S::f();  */
17366   error ("%Htoo many template-parameter-lists", &location);
17367   return false;
17368 }
17369
17370 /* Parse an optional `::' token indicating that the following name is
17371    from the global namespace.  If so, PARSER->SCOPE is set to the
17372    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17373    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17374    Returns the new value of PARSER->SCOPE, if the `::' token is
17375    present, and NULL_TREE otherwise.  */
17376
17377 static tree
17378 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17379 {
17380   cp_token *token;
17381
17382   /* Peek at the next token.  */
17383   token = cp_lexer_peek_token (parser->lexer);
17384   /* If we're looking at a `::' token then we're starting from the
17385      global namespace, not our current location.  */
17386   if (token->type == CPP_SCOPE)
17387     {
17388       /* Consume the `::' token.  */
17389       cp_lexer_consume_token (parser->lexer);
17390       /* Set the SCOPE so that we know where to start the lookup.  */
17391       parser->scope = global_namespace;
17392       parser->qualifying_scope = global_namespace;
17393       parser->object_scope = NULL_TREE;
17394
17395       return parser->scope;
17396     }
17397   else if (!current_scope_valid_p)
17398     {
17399       parser->scope = NULL_TREE;
17400       parser->qualifying_scope = NULL_TREE;
17401       parser->object_scope = NULL_TREE;
17402     }
17403
17404   return NULL_TREE;
17405 }
17406
17407 /* Returns TRUE if the upcoming token sequence is the start of a
17408    constructor declarator.  If FRIEND_P is true, the declarator is
17409    preceded by the `friend' specifier.  */
17410
17411 static bool
17412 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17413 {
17414   bool constructor_p;
17415   tree type_decl = NULL_TREE;
17416   bool nested_name_p;
17417   cp_token *next_token;
17418
17419   /* The common case is that this is not a constructor declarator, so
17420      try to avoid doing lots of work if at all possible.  It's not
17421      valid declare a constructor at function scope.  */
17422   if (parser->in_function_body)
17423     return false;
17424   /* And only certain tokens can begin a constructor declarator.  */
17425   next_token = cp_lexer_peek_token (parser->lexer);
17426   if (next_token->type != CPP_NAME
17427       && next_token->type != CPP_SCOPE
17428       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17429       && next_token->type != CPP_TEMPLATE_ID)
17430     return false;
17431
17432   /* Parse tentatively; we are going to roll back all of the tokens
17433      consumed here.  */
17434   cp_parser_parse_tentatively (parser);
17435   /* Assume that we are looking at a constructor declarator.  */
17436   constructor_p = true;
17437
17438   /* Look for the optional `::' operator.  */
17439   cp_parser_global_scope_opt (parser,
17440                               /*current_scope_valid_p=*/false);
17441   /* Look for the nested-name-specifier.  */
17442   nested_name_p
17443     = (cp_parser_nested_name_specifier_opt (parser,
17444                                             /*typename_keyword_p=*/false,
17445                                             /*check_dependency_p=*/false,
17446                                             /*type_p=*/false,
17447                                             /*is_declaration=*/false)
17448        != NULL_TREE);
17449   /* Outside of a class-specifier, there must be a
17450      nested-name-specifier.  */
17451   if (!nested_name_p &&
17452       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17453        || friend_p))
17454     constructor_p = false;
17455   /* If we still think that this might be a constructor-declarator,
17456      look for a class-name.  */
17457   if (constructor_p)
17458     {
17459       /* If we have:
17460
17461            template <typename T> struct S { S(); };
17462            template <typename T> S<T>::S ();
17463
17464          we must recognize that the nested `S' names a class.
17465          Similarly, for:
17466
17467            template <typename T> S<T>::S<T> ();
17468
17469          we must recognize that the nested `S' names a template.  */
17470       type_decl = cp_parser_class_name (parser,
17471                                         /*typename_keyword_p=*/false,
17472                                         /*template_keyword_p=*/false,
17473                                         none_type,
17474                                         /*check_dependency_p=*/false,
17475                                         /*class_head_p=*/false,
17476                                         /*is_declaration=*/false);
17477       /* If there was no class-name, then this is not a constructor.  */
17478       constructor_p = !cp_parser_error_occurred (parser);
17479     }
17480
17481   /* If we're still considering a constructor, we have to see a `(',
17482      to begin the parameter-declaration-clause, followed by either a
17483      `)', an `...', or a decl-specifier.  We need to check for a
17484      type-specifier to avoid being fooled into thinking that:
17485
17486        S::S (f) (int);
17487
17488      is a constructor.  (It is actually a function named `f' that
17489      takes one parameter (of type `int') and returns a value of type
17490      `S::S'.  */
17491   if (constructor_p
17492       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17493     {
17494       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17495           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17496           /* A parameter declaration begins with a decl-specifier,
17497              which is either the "attribute" keyword, a storage class
17498              specifier, or (usually) a type-specifier.  */
17499           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17500         {
17501           tree type;
17502           tree pushed_scope = NULL_TREE;
17503           unsigned saved_num_template_parameter_lists;
17504
17505           /* Names appearing in the type-specifier should be looked up
17506              in the scope of the class.  */
17507           if (current_class_type)
17508             type = NULL_TREE;
17509           else
17510             {
17511               type = TREE_TYPE (type_decl);
17512               if (TREE_CODE (type) == TYPENAME_TYPE)
17513                 {
17514                   type = resolve_typename_type (type,
17515                                                 /*only_current_p=*/false);
17516                   if (TREE_CODE (type) == TYPENAME_TYPE)
17517                     {
17518                       cp_parser_abort_tentative_parse (parser);
17519                       return false;
17520                     }
17521                 }
17522               pushed_scope = push_scope (type);
17523             }
17524
17525           /* Inside the constructor parameter list, surrounding
17526              template-parameter-lists do not apply.  */
17527           saved_num_template_parameter_lists
17528             = parser->num_template_parameter_lists;
17529           parser->num_template_parameter_lists = 0;
17530
17531           /* Look for the type-specifier.  */
17532           cp_parser_type_specifier (parser,
17533                                     CP_PARSER_FLAGS_NONE,
17534                                     /*decl_specs=*/NULL,
17535                                     /*is_declarator=*/true,
17536                                     /*declares_class_or_enum=*/NULL,
17537                                     /*is_cv_qualifier=*/NULL);
17538
17539           parser->num_template_parameter_lists
17540             = saved_num_template_parameter_lists;
17541
17542           /* Leave the scope of the class.  */
17543           if (pushed_scope)
17544             pop_scope (pushed_scope);
17545
17546           constructor_p = !cp_parser_error_occurred (parser);
17547         }
17548     }
17549   else
17550     constructor_p = false;
17551   /* We did not really want to consume any tokens.  */
17552   cp_parser_abort_tentative_parse (parser);
17553
17554   return constructor_p;
17555 }
17556
17557 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17558    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17559    they must be performed once we are in the scope of the function.
17560
17561    Returns the function defined.  */
17562
17563 static tree
17564 cp_parser_function_definition_from_specifiers_and_declarator
17565   (cp_parser* parser,
17566    cp_decl_specifier_seq *decl_specifiers,
17567    tree attributes,
17568    const cp_declarator *declarator)
17569 {
17570   tree fn;
17571   bool success_p;
17572
17573   /* Begin the function-definition.  */
17574   success_p = start_function (decl_specifiers, declarator, attributes);
17575
17576   /* The things we're about to see are not directly qualified by any
17577      template headers we've seen thus far.  */
17578   reset_specialization ();
17579
17580   /* If there were names looked up in the decl-specifier-seq that we
17581      did not check, check them now.  We must wait until we are in the
17582      scope of the function to perform the checks, since the function
17583      might be a friend.  */
17584   perform_deferred_access_checks ();
17585
17586   if (!success_p)
17587     {
17588       /* Skip the entire function.  */
17589       cp_parser_skip_to_end_of_block_or_statement (parser);
17590       fn = error_mark_node;
17591     }
17592   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17593     {
17594       /* Seen already, skip it.  An error message has already been output.  */
17595       cp_parser_skip_to_end_of_block_or_statement (parser);
17596       fn = current_function_decl;
17597       current_function_decl = NULL_TREE;
17598       /* If this is a function from a class, pop the nested class.  */
17599       if (current_class_name)
17600         pop_nested_class ();
17601     }
17602   else
17603     fn = cp_parser_function_definition_after_declarator (parser,
17604                                                          /*inline_p=*/false);
17605
17606   return fn;
17607 }
17608
17609 /* Parse the part of a function-definition that follows the
17610    declarator.  INLINE_P is TRUE iff this function is an inline
17611    function defined with a class-specifier.
17612
17613    Returns the function defined.  */
17614
17615 static tree
17616 cp_parser_function_definition_after_declarator (cp_parser* parser,
17617                                                 bool inline_p)
17618 {
17619   tree fn;
17620   bool ctor_initializer_p = false;
17621   bool saved_in_unbraced_linkage_specification_p;
17622   bool saved_in_function_body;
17623   unsigned saved_num_template_parameter_lists;
17624   cp_token *token;
17625
17626   saved_in_function_body = parser->in_function_body;
17627   parser->in_function_body = true;
17628   /* If the next token is `return', then the code may be trying to
17629      make use of the "named return value" extension that G++ used to
17630      support.  */
17631   token = cp_lexer_peek_token (parser->lexer);
17632   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17633     {
17634       /* Consume the `return' keyword.  */
17635       cp_lexer_consume_token (parser->lexer);
17636       /* Look for the identifier that indicates what value is to be
17637          returned.  */
17638       cp_parser_identifier (parser);
17639       /* Issue an error message.  */
17640       error ("%Hnamed return values are no longer supported",
17641              &token->location);
17642       /* Skip tokens until we reach the start of the function body.  */
17643       while (true)
17644         {
17645           cp_token *token = cp_lexer_peek_token (parser->lexer);
17646           if (token->type == CPP_OPEN_BRACE
17647               || token->type == CPP_EOF
17648               || token->type == CPP_PRAGMA_EOL)
17649             break;
17650           cp_lexer_consume_token (parser->lexer);
17651         }
17652     }
17653   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17654      anything declared inside `f'.  */
17655   saved_in_unbraced_linkage_specification_p
17656     = parser->in_unbraced_linkage_specification_p;
17657   parser->in_unbraced_linkage_specification_p = false;
17658   /* Inside the function, surrounding template-parameter-lists do not
17659      apply.  */
17660   saved_num_template_parameter_lists
17661     = parser->num_template_parameter_lists;
17662   parser->num_template_parameter_lists = 0;
17663   /* If the next token is `try', then we are looking at a
17664      function-try-block.  */
17665   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17666     ctor_initializer_p = cp_parser_function_try_block (parser);
17667   /* A function-try-block includes the function-body, so we only do
17668      this next part if we're not processing a function-try-block.  */
17669   else
17670     ctor_initializer_p
17671       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17672
17673   /* Finish the function.  */
17674   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17675                         (inline_p ? 2 : 0));
17676   /* Generate code for it, if necessary.  */
17677   expand_or_defer_fn (fn);
17678   /* Restore the saved values.  */
17679   parser->in_unbraced_linkage_specification_p
17680     = saved_in_unbraced_linkage_specification_p;
17681   parser->num_template_parameter_lists
17682     = saved_num_template_parameter_lists;
17683   parser->in_function_body = saved_in_function_body;
17684
17685   return fn;
17686 }
17687
17688 /* Parse a template-declaration, assuming that the `export' (and
17689    `extern') keywords, if present, has already been scanned.  MEMBER_P
17690    is as for cp_parser_template_declaration.  */
17691
17692 static void
17693 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17694 {
17695   tree decl = NULL_TREE;
17696   VEC (deferred_access_check,gc) *checks;
17697   tree parameter_list;
17698   bool friend_p = false;
17699   bool need_lang_pop;
17700   cp_token *token;
17701
17702   /* Look for the `template' keyword.  */
17703   token = cp_lexer_peek_token (parser->lexer);
17704   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17705     return;
17706
17707   /* And the `<'.  */
17708   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17709     return;
17710   if (at_class_scope_p () && current_function_decl)
17711     {
17712       /* 14.5.2.2 [temp.mem]
17713
17714          A local class shall not have member templates.  */
17715       error ("%Hinvalid declaration of member template in local class",
17716              &token->location);
17717       cp_parser_skip_to_end_of_block_or_statement (parser);
17718       return;
17719     }
17720   /* [temp]
17721
17722      A template ... shall not have C linkage.  */
17723   if (current_lang_name == lang_name_c)
17724     {
17725       error ("%Htemplate with C linkage", &token->location);
17726       /* Give it C++ linkage to avoid confusing other parts of the
17727          front end.  */
17728       push_lang_context (lang_name_cplusplus);
17729       need_lang_pop = true;
17730     }
17731   else
17732     need_lang_pop = false;
17733
17734   /* We cannot perform access checks on the template parameter
17735      declarations until we know what is being declared, just as we
17736      cannot check the decl-specifier list.  */
17737   push_deferring_access_checks (dk_deferred);
17738
17739   /* If the next token is `>', then we have an invalid
17740      specialization.  Rather than complain about an invalid template
17741      parameter, issue an error message here.  */
17742   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17743     {
17744       cp_parser_error (parser, "invalid explicit specialization");
17745       begin_specialization ();
17746       parameter_list = NULL_TREE;
17747     }
17748   else
17749     /* Parse the template parameters.  */
17750     parameter_list = cp_parser_template_parameter_list (parser);
17751
17752   /* Get the deferred access checks from the parameter list.  These
17753      will be checked once we know what is being declared, as for a
17754      member template the checks must be performed in the scope of the
17755      class containing the member.  */
17756   checks = get_deferred_access_checks ();
17757
17758   /* Look for the `>'.  */
17759   cp_parser_skip_to_end_of_template_parameter_list (parser);
17760   /* We just processed one more parameter list.  */
17761   ++parser->num_template_parameter_lists;
17762   /* If the next token is `template', there are more template
17763      parameters.  */
17764   if (cp_lexer_next_token_is_keyword (parser->lexer,
17765                                       RID_TEMPLATE))
17766     cp_parser_template_declaration_after_export (parser, member_p);
17767   else
17768     {
17769       /* There are no access checks when parsing a template, as we do not
17770          know if a specialization will be a friend.  */
17771       push_deferring_access_checks (dk_no_check);
17772       token = cp_lexer_peek_token (parser->lexer);
17773       decl = cp_parser_single_declaration (parser,
17774                                            checks,
17775                                            member_p,
17776                                            /*explicit_specialization_p=*/false,
17777                                            &friend_p);
17778       pop_deferring_access_checks ();
17779
17780       /* If this is a member template declaration, let the front
17781          end know.  */
17782       if (member_p && !friend_p && decl)
17783         {
17784           if (TREE_CODE (decl) == TYPE_DECL)
17785             cp_parser_check_access_in_redeclaration (decl, token->location);
17786
17787           decl = finish_member_template_decl (decl);
17788         }
17789       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17790         make_friend_class (current_class_type, TREE_TYPE (decl),
17791                            /*complain=*/true);
17792     }
17793   /* We are done with the current parameter list.  */
17794   --parser->num_template_parameter_lists;
17795
17796   pop_deferring_access_checks ();
17797
17798   /* Finish up.  */
17799   finish_template_decl (parameter_list);
17800
17801   /* Register member declarations.  */
17802   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17803     finish_member_declaration (decl);
17804   /* For the erroneous case of a template with C linkage, we pushed an
17805      implicit C++ linkage scope; exit that scope now.  */
17806   if (need_lang_pop)
17807     pop_lang_context ();
17808   /* If DECL is a function template, we must return to parse it later.
17809      (Even though there is no definition, there might be default
17810      arguments that need handling.)  */
17811   if (member_p && decl
17812       && (TREE_CODE (decl) == FUNCTION_DECL
17813           || DECL_FUNCTION_TEMPLATE_P (decl)))
17814     TREE_VALUE (parser->unparsed_functions_queues)
17815       = tree_cons (NULL_TREE, decl,
17816                    TREE_VALUE (parser->unparsed_functions_queues));
17817 }
17818
17819 /* Perform the deferred access checks from a template-parameter-list.
17820    CHECKS is a TREE_LIST of access checks, as returned by
17821    get_deferred_access_checks.  */
17822
17823 static void
17824 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17825 {
17826   ++processing_template_parmlist;
17827   perform_access_checks (checks);
17828   --processing_template_parmlist;
17829 }
17830
17831 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17832    `function-definition' sequence.  MEMBER_P is true, this declaration
17833    appears in a class scope.
17834
17835    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17836    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17837
17838 static tree
17839 cp_parser_single_declaration (cp_parser* parser,
17840                               VEC (deferred_access_check,gc)* checks,
17841                               bool member_p,
17842                               bool explicit_specialization_p,
17843                               bool* friend_p)
17844 {
17845   int declares_class_or_enum;
17846   tree decl = NULL_TREE;
17847   cp_decl_specifier_seq decl_specifiers;
17848   bool function_definition_p = false;
17849   cp_token *decl_spec_token_start;
17850
17851   /* This function is only used when processing a template
17852      declaration.  */
17853   gcc_assert (innermost_scope_kind () == sk_template_parms
17854               || innermost_scope_kind () == sk_template_spec);
17855
17856   /* Defer access checks until we know what is being declared.  */
17857   push_deferring_access_checks (dk_deferred);
17858
17859   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17860      alternative.  */
17861   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17862   cp_parser_decl_specifier_seq (parser,
17863                                 CP_PARSER_FLAGS_OPTIONAL,
17864                                 &decl_specifiers,
17865                                 &declares_class_or_enum);
17866   if (friend_p)
17867     *friend_p = cp_parser_friend_p (&decl_specifiers);
17868
17869   /* There are no template typedefs.  */
17870   if (decl_specifiers.specs[(int) ds_typedef])
17871     {
17872       error ("%Htemplate declaration of %qs",
17873              &decl_spec_token_start->location, "typedef");
17874       decl = error_mark_node;
17875     }
17876
17877   /* Gather up the access checks that occurred the
17878      decl-specifier-seq.  */
17879   stop_deferring_access_checks ();
17880
17881   /* Check for the declaration of a template class.  */
17882   if (declares_class_or_enum)
17883     {
17884       if (cp_parser_declares_only_class_p (parser))
17885         {
17886           decl = shadow_tag (&decl_specifiers);
17887
17888           /* In this case:
17889
17890                struct C {
17891                  friend template <typename T> struct A<T>::B;
17892                };
17893
17894              A<T>::B will be represented by a TYPENAME_TYPE, and
17895              therefore not recognized by shadow_tag.  */
17896           if (friend_p && *friend_p
17897               && !decl
17898               && decl_specifiers.type
17899               && TYPE_P (decl_specifiers.type))
17900             decl = decl_specifiers.type;
17901
17902           if (decl && decl != error_mark_node)
17903             decl = TYPE_NAME (decl);
17904           else
17905             decl = error_mark_node;
17906
17907           /* Perform access checks for template parameters.  */
17908           cp_parser_perform_template_parameter_access_checks (checks);
17909         }
17910     }
17911   /* If it's not a template class, try for a template function.  If
17912      the next token is a `;', then this declaration does not declare
17913      anything.  But, if there were errors in the decl-specifiers, then
17914      the error might well have come from an attempted class-specifier.
17915      In that case, there's no need to warn about a missing declarator.  */
17916   if (!decl
17917       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17918           || decl_specifiers.type != error_mark_node))
17919     {
17920       decl = cp_parser_init_declarator (parser,
17921                                         &decl_specifiers,
17922                                         checks,
17923                                         /*function_definition_allowed_p=*/true,
17924                                         member_p,
17925                                         declares_class_or_enum,
17926                                         &function_definition_p);
17927
17928     /* 7.1.1-1 [dcl.stc]
17929
17930        A storage-class-specifier shall not be specified in an explicit
17931        specialization...  */
17932     if (decl
17933         && explicit_specialization_p
17934         && decl_specifiers.storage_class != sc_none)
17935       {
17936         error ("%Hexplicit template specialization cannot have a storage class",
17937                &decl_spec_token_start->location);
17938         decl = error_mark_node;
17939       }
17940     }
17941
17942   pop_deferring_access_checks ();
17943
17944   /* Clear any current qualification; whatever comes next is the start
17945      of something new.  */
17946   parser->scope = NULL_TREE;
17947   parser->qualifying_scope = NULL_TREE;
17948   parser->object_scope = NULL_TREE;
17949   /* Look for a trailing `;' after the declaration.  */
17950   if (!function_definition_p
17951       && (decl == error_mark_node
17952           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17953     cp_parser_skip_to_end_of_block_or_statement (parser);
17954
17955   return decl;
17956 }
17957
17958 /* Parse a cast-expression that is not the operand of a unary "&".  */
17959
17960 static tree
17961 cp_parser_simple_cast_expression (cp_parser *parser)
17962 {
17963   return cp_parser_cast_expression (parser, /*address_p=*/false,
17964                                     /*cast_p=*/false, NULL);
17965 }
17966
17967 /* Parse a functional cast to TYPE.  Returns an expression
17968    representing the cast.  */
17969
17970 static tree
17971 cp_parser_functional_cast (cp_parser* parser, tree type)
17972 {
17973   tree expression_list;
17974   tree cast;
17975   bool nonconst_p;
17976
17977   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17978     {
17979       maybe_warn_cpp0x ("extended initializer lists");
17980       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17981       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17982       if (TREE_CODE (type) == TYPE_DECL)
17983         type = TREE_TYPE (type);
17984       return finish_compound_literal (type, expression_list);
17985     }
17986
17987   expression_list
17988     = cp_parser_parenthesized_expression_list (parser, false,
17989                                                /*cast_p=*/true,
17990                                                /*allow_expansion_p=*/true,
17991                                                /*non_constant_p=*/NULL);
17992
17993   cast = build_functional_cast (type, expression_list,
17994                                 tf_warning_or_error);
17995   /* [expr.const]/1: In an integral constant expression "only type
17996      conversions to integral or enumeration type can be used".  */
17997   if (TREE_CODE (type) == TYPE_DECL)
17998     type = TREE_TYPE (type);
17999   if (cast != error_mark_node
18000       && !cast_valid_in_integral_constant_expression_p (type)
18001       && (cp_parser_non_integral_constant_expression
18002           (parser, "a call to a constructor")))
18003     return error_mark_node;
18004   return cast;
18005 }
18006
18007 /* Save the tokens that make up the body of a member function defined
18008    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18009    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18010    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18011    for the member function.  */
18012
18013 static tree
18014 cp_parser_save_member_function_body (cp_parser* parser,
18015                                      cp_decl_specifier_seq *decl_specifiers,
18016                                      cp_declarator *declarator,
18017                                      tree attributes)
18018 {
18019   cp_token *first;
18020   cp_token *last;
18021   tree fn;
18022
18023   /* Create the function-declaration.  */
18024   fn = start_method (decl_specifiers, declarator, attributes);
18025   /* If something went badly wrong, bail out now.  */
18026   if (fn == error_mark_node)
18027     {
18028       /* If there's a function-body, skip it.  */
18029       if (cp_parser_token_starts_function_definition_p
18030           (cp_lexer_peek_token (parser->lexer)))
18031         cp_parser_skip_to_end_of_block_or_statement (parser);
18032       return error_mark_node;
18033     }
18034
18035   /* Remember it, if there default args to post process.  */
18036   cp_parser_save_default_args (parser, fn);
18037
18038   /* Save away the tokens that make up the body of the
18039      function.  */
18040   first = parser->lexer->next_token;
18041   /* We can have braced-init-list mem-initializers before the fn body.  */
18042   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18043     {
18044       cp_lexer_consume_token (parser->lexer);
18045       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18046              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18047         {
18048           /* cache_group will stop after an un-nested { } pair, too.  */
18049           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18050             break;
18051
18052           /* variadic mem-inits have ... after the ')'.  */
18053           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18054             cp_lexer_consume_token (parser->lexer);
18055         }
18056     }
18057   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18058   /* Handle function try blocks.  */
18059   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18060     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18061   last = parser->lexer->next_token;
18062
18063   /* Save away the inline definition; we will process it when the
18064      class is complete.  */
18065   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18066   DECL_PENDING_INLINE_P (fn) = 1;
18067
18068   /* We need to know that this was defined in the class, so that
18069      friend templates are handled correctly.  */
18070   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18071
18072   /* We're done with the inline definition.  */
18073   finish_method (fn);
18074
18075   /* Add FN to the queue of functions to be parsed later.  */
18076   TREE_VALUE (parser->unparsed_functions_queues)
18077     = tree_cons (NULL_TREE, fn,
18078                  TREE_VALUE (parser->unparsed_functions_queues));
18079
18080   return fn;
18081 }
18082
18083 /* Parse a template-argument-list, as well as the trailing ">" (but
18084    not the opening ">").  See cp_parser_template_argument_list for the
18085    return value.  */
18086
18087 static tree
18088 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18089 {
18090   tree arguments;
18091   tree saved_scope;
18092   tree saved_qualifying_scope;
18093   tree saved_object_scope;
18094   bool saved_greater_than_is_operator_p;
18095   bool saved_skip_evaluation;
18096
18097   /* [temp.names]
18098
18099      When parsing a template-id, the first non-nested `>' is taken as
18100      the end of the template-argument-list rather than a greater-than
18101      operator.  */
18102   saved_greater_than_is_operator_p
18103     = parser->greater_than_is_operator_p;
18104   parser->greater_than_is_operator_p = false;
18105   /* Parsing the argument list may modify SCOPE, so we save it
18106      here.  */
18107   saved_scope = parser->scope;
18108   saved_qualifying_scope = parser->qualifying_scope;
18109   saved_object_scope = parser->object_scope;
18110   /* We need to evaluate the template arguments, even though this
18111      template-id may be nested within a "sizeof".  */
18112   saved_skip_evaluation = skip_evaluation;
18113   skip_evaluation = false;
18114   /* Parse the template-argument-list itself.  */
18115   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18116       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18117     arguments = NULL_TREE;
18118   else
18119     arguments = cp_parser_template_argument_list (parser);
18120   /* Look for the `>' that ends the template-argument-list. If we find
18121      a '>>' instead, it's probably just a typo.  */
18122   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18123     {
18124       if (cxx_dialect != cxx98)
18125         {
18126           /* In C++0x, a `>>' in a template argument list or cast
18127              expression is considered to be two separate `>'
18128              tokens. So, change the current token to a `>', but don't
18129              consume it: it will be consumed later when the outer
18130              template argument list (or cast expression) is parsed.
18131              Note that this replacement of `>' for `>>' is necessary
18132              even if we are parsing tentatively: in the tentative
18133              case, after calling
18134              cp_parser_enclosed_template_argument_list we will always
18135              throw away all of the template arguments and the first
18136              closing `>', either because the template argument list
18137              was erroneous or because we are replacing those tokens
18138              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18139              not have been thrown away) is needed either to close an
18140              outer template argument list or to complete a new-style
18141              cast.  */
18142           cp_token *token = cp_lexer_peek_token (parser->lexer);
18143           token->type = CPP_GREATER;
18144         }
18145       else if (!saved_greater_than_is_operator_p)
18146         {
18147           /* If we're in a nested template argument list, the '>>' has
18148             to be a typo for '> >'. We emit the error message, but we
18149             continue parsing and we push a '>' as next token, so that
18150             the argument list will be parsed correctly.  Note that the
18151             global source location is still on the token before the
18152             '>>', so we need to say explicitly where we want it.  */
18153           cp_token *token = cp_lexer_peek_token (parser->lexer);
18154           error ("%H%<>>%> should be %<> >%> "
18155                  "within a nested template argument list",
18156                  &token->location);
18157
18158           token->type = CPP_GREATER;
18159         }
18160       else
18161         {
18162           /* If this is not a nested template argument list, the '>>'
18163             is a typo for '>'. Emit an error message and continue.
18164             Same deal about the token location, but here we can get it
18165             right by consuming the '>>' before issuing the diagnostic.  */
18166           cp_token *token = cp_lexer_consume_token (parser->lexer);
18167           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18168                  "a template argument list", &token->location);
18169         }
18170     }
18171   else
18172     cp_parser_skip_to_end_of_template_parameter_list (parser);
18173   /* The `>' token might be a greater-than operator again now.  */
18174   parser->greater_than_is_operator_p
18175     = saved_greater_than_is_operator_p;
18176   /* Restore the SAVED_SCOPE.  */
18177   parser->scope = saved_scope;
18178   parser->qualifying_scope = saved_qualifying_scope;
18179   parser->object_scope = saved_object_scope;
18180   skip_evaluation = saved_skip_evaluation;
18181
18182   return arguments;
18183 }
18184
18185 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18186    arguments, or the body of the function have not yet been parsed,
18187    parse them now.  */
18188
18189 static void
18190 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18191 {
18192   /* If this member is a template, get the underlying
18193      FUNCTION_DECL.  */
18194   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18195     member_function = DECL_TEMPLATE_RESULT (member_function);
18196
18197   /* There should not be any class definitions in progress at this
18198      point; the bodies of members are only parsed outside of all class
18199      definitions.  */
18200   gcc_assert (parser->num_classes_being_defined == 0);
18201   /* While we're parsing the member functions we might encounter more
18202      classes.  We want to handle them right away, but we don't want
18203      them getting mixed up with functions that are currently in the
18204      queue.  */
18205   parser->unparsed_functions_queues
18206     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18207
18208   /* Make sure that any template parameters are in scope.  */
18209   maybe_begin_member_template_processing (member_function);
18210
18211   /* If the body of the function has not yet been parsed, parse it
18212      now.  */
18213   if (DECL_PENDING_INLINE_P (member_function))
18214     {
18215       tree function_scope;
18216       cp_token_cache *tokens;
18217
18218       /* The function is no longer pending; we are processing it.  */
18219       tokens = DECL_PENDING_INLINE_INFO (member_function);
18220       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18221       DECL_PENDING_INLINE_P (member_function) = 0;
18222
18223       /* If this is a local class, enter the scope of the containing
18224          function.  */
18225       function_scope = current_function_decl;
18226       if (function_scope)
18227         push_function_context ();
18228
18229       /* Push the body of the function onto the lexer stack.  */
18230       cp_parser_push_lexer_for_tokens (parser, tokens);
18231
18232       /* Let the front end know that we going to be defining this
18233          function.  */
18234       start_preparsed_function (member_function, NULL_TREE,
18235                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18236
18237       /* Don't do access checking if it is a templated function.  */
18238       if (processing_template_decl)
18239         push_deferring_access_checks (dk_no_check);
18240
18241       /* Now, parse the body of the function.  */
18242       cp_parser_function_definition_after_declarator (parser,
18243                                                       /*inline_p=*/true);
18244
18245       if (processing_template_decl)
18246         pop_deferring_access_checks ();
18247
18248       /* Leave the scope of the containing function.  */
18249       if (function_scope)
18250         pop_function_context ();
18251       cp_parser_pop_lexer (parser);
18252     }
18253
18254   /* Remove any template parameters from the symbol table.  */
18255   maybe_end_member_template_processing ();
18256
18257   /* Restore the queue.  */
18258   parser->unparsed_functions_queues
18259     = TREE_CHAIN (parser->unparsed_functions_queues);
18260 }
18261
18262 /* If DECL contains any default args, remember it on the unparsed
18263    functions queue.  */
18264
18265 static void
18266 cp_parser_save_default_args (cp_parser* parser, tree decl)
18267 {
18268   tree probe;
18269
18270   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18271        probe;
18272        probe = TREE_CHAIN (probe))
18273     if (TREE_PURPOSE (probe))
18274       {
18275         TREE_PURPOSE (parser->unparsed_functions_queues)
18276           = tree_cons (current_class_type, decl,
18277                        TREE_PURPOSE (parser->unparsed_functions_queues));
18278         break;
18279       }
18280 }
18281
18282 /* FN is a FUNCTION_DECL which may contains a parameter with an
18283    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18284    assumes that the current scope is the scope in which the default
18285    argument should be processed.  */
18286
18287 static void
18288 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18289 {
18290   bool saved_local_variables_forbidden_p;
18291   tree parm;
18292
18293   /* While we're parsing the default args, we might (due to the
18294      statement expression extension) encounter more classes.  We want
18295      to handle them right away, but we don't want them getting mixed
18296      up with default args that are currently in the queue.  */
18297   parser->unparsed_functions_queues
18298     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18299
18300   /* Local variable names (and the `this' keyword) may not appear
18301      in a default argument.  */
18302   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18303   parser->local_variables_forbidden_p = true;
18304
18305   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18306        parm;
18307        parm = TREE_CHAIN (parm))
18308     {
18309       cp_token_cache *tokens;
18310       tree default_arg = TREE_PURPOSE (parm);
18311       tree parsed_arg;
18312       VEC(tree,gc) *insts;
18313       tree copy;
18314       unsigned ix;
18315
18316       if (!default_arg)
18317         continue;
18318
18319       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18320         /* This can happen for a friend declaration for a function
18321            already declared with default arguments.  */
18322         continue;
18323
18324        /* Push the saved tokens for the default argument onto the parser's
18325           lexer stack.  */
18326       tokens = DEFARG_TOKENS (default_arg);
18327       cp_parser_push_lexer_for_tokens (parser, tokens);
18328
18329       /* Parse the assignment-expression.  */
18330       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18331       if (parsed_arg == error_mark_node)
18332         {
18333           cp_parser_pop_lexer (parser);
18334           continue;
18335         }
18336
18337       if (!processing_template_decl)
18338         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18339
18340       TREE_PURPOSE (parm) = parsed_arg;
18341
18342       /* Update any instantiations we've already created.  */
18343       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18344            VEC_iterate (tree, insts, ix, copy); ix++)
18345         TREE_PURPOSE (copy) = parsed_arg;
18346
18347       /* If the token stream has not been completely used up, then
18348          there was extra junk after the end of the default
18349          argument.  */
18350       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18351         cp_parser_error (parser, "expected %<,%>");
18352
18353       /* Revert to the main lexer.  */
18354       cp_parser_pop_lexer (parser);
18355     }
18356
18357   /* Make sure no default arg is missing.  */
18358   check_default_args (fn);
18359
18360   /* Restore the state of local_variables_forbidden_p.  */
18361   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18362
18363   /* Restore the queue.  */
18364   parser->unparsed_functions_queues
18365     = TREE_CHAIN (parser->unparsed_functions_queues);
18366 }
18367
18368 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18369    either a TYPE or an expression, depending on the form of the
18370    input.  The KEYWORD indicates which kind of expression we have
18371    encountered.  */
18372
18373 static tree
18374 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18375 {
18376   tree expr = NULL_TREE;
18377   const char *saved_message;
18378   char *tmp;
18379   bool saved_integral_constant_expression_p;
18380   bool saved_non_integral_constant_expression_p;
18381   bool pack_expansion_p = false;
18382
18383   /* Types cannot be defined in a `sizeof' expression.  Save away the
18384      old message.  */
18385   saved_message = parser->type_definition_forbidden_message;
18386   /* And create the new one.  */
18387   tmp = concat ("types may not be defined in %<",
18388                 IDENTIFIER_POINTER (ridpointers[keyword]),
18389                 "%> expressions", NULL);
18390   parser->type_definition_forbidden_message = tmp;
18391
18392   /* The restrictions on constant-expressions do not apply inside
18393      sizeof expressions.  */
18394   saved_integral_constant_expression_p
18395     = parser->integral_constant_expression_p;
18396   saved_non_integral_constant_expression_p
18397     = parser->non_integral_constant_expression_p;
18398   parser->integral_constant_expression_p = false;
18399
18400   /* If it's a `...', then we are computing the length of a parameter
18401      pack.  */
18402   if (keyword == RID_SIZEOF
18403       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18404     {
18405       /* Consume the `...'.  */
18406       cp_lexer_consume_token (parser->lexer);
18407       maybe_warn_variadic_templates ();
18408
18409       /* Note that this is an expansion.  */
18410       pack_expansion_p = true;
18411     }
18412
18413   /* Do not actually evaluate the expression.  */
18414   ++skip_evaluation;
18415   /* If it's a `(', then we might be looking at the type-id
18416      construction.  */
18417   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18418     {
18419       tree type;
18420       bool saved_in_type_id_in_expr_p;
18421
18422       /* We can't be sure yet whether we're looking at a type-id or an
18423          expression.  */
18424       cp_parser_parse_tentatively (parser);
18425       /* Consume the `('.  */
18426       cp_lexer_consume_token (parser->lexer);
18427       /* Parse the type-id.  */
18428       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18429       parser->in_type_id_in_expr_p = true;
18430       type = cp_parser_type_id (parser);
18431       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18432       /* Now, look for the trailing `)'.  */
18433       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18434       /* If all went well, then we're done.  */
18435       if (cp_parser_parse_definitely (parser))
18436         {
18437           cp_decl_specifier_seq decl_specs;
18438
18439           /* Build a trivial decl-specifier-seq.  */
18440           clear_decl_specs (&decl_specs);
18441           decl_specs.type = type;
18442
18443           /* Call grokdeclarator to figure out what type this is.  */
18444           expr = grokdeclarator (NULL,
18445                                  &decl_specs,
18446                                  TYPENAME,
18447                                  /*initialized=*/0,
18448                                  /*attrlist=*/NULL);
18449         }
18450     }
18451
18452   /* If the type-id production did not work out, then we must be
18453      looking at the unary-expression production.  */
18454   if (!expr)
18455     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18456                                        /*cast_p=*/false, NULL);
18457
18458   if (pack_expansion_p)
18459     /* Build a pack expansion. */
18460     expr = make_pack_expansion (expr);
18461
18462   /* Go back to evaluating expressions.  */
18463   --skip_evaluation;
18464
18465   /* Free the message we created.  */
18466   free (tmp);
18467   /* And restore the old one.  */
18468   parser->type_definition_forbidden_message = saved_message;
18469   parser->integral_constant_expression_p
18470     = saved_integral_constant_expression_p;
18471   parser->non_integral_constant_expression_p
18472     = saved_non_integral_constant_expression_p;
18473
18474   return expr;
18475 }
18476
18477 /* If the current declaration has no declarator, return true.  */
18478
18479 static bool
18480 cp_parser_declares_only_class_p (cp_parser *parser)
18481 {
18482   /* If the next token is a `;' or a `,' then there is no
18483      declarator.  */
18484   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18485           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18486 }
18487
18488 /* Update the DECL_SPECS to reflect the storage class indicated by
18489    KEYWORD.  */
18490
18491 static void
18492 cp_parser_set_storage_class (cp_parser *parser,
18493                              cp_decl_specifier_seq *decl_specs,
18494                              enum rid keyword,
18495                              location_t location)
18496 {
18497   cp_storage_class storage_class;
18498
18499   if (parser->in_unbraced_linkage_specification_p)
18500     {
18501       error ("%Hinvalid use of %qD in linkage specification",
18502              &location, ridpointers[keyword]);
18503       return;
18504     }
18505   else if (decl_specs->storage_class != sc_none)
18506     {
18507       decl_specs->conflicting_specifiers_p = true;
18508       return;
18509     }
18510
18511   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18512       && decl_specs->specs[(int) ds_thread])
18513     {
18514       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18515       decl_specs->specs[(int) ds_thread] = 0;
18516     }
18517
18518   switch (keyword)
18519     {
18520     case RID_AUTO:
18521       storage_class = sc_auto;
18522       break;
18523     case RID_REGISTER:
18524       storage_class = sc_register;
18525       break;
18526     case RID_STATIC:
18527       storage_class = sc_static;
18528       break;
18529     case RID_EXTERN:
18530       storage_class = sc_extern;
18531       break;
18532     case RID_MUTABLE:
18533       storage_class = sc_mutable;
18534       break;
18535     default:
18536       gcc_unreachable ();
18537     }
18538   decl_specs->storage_class = storage_class;
18539
18540   /* A storage class specifier cannot be applied alongside a typedef 
18541      specifier. If there is a typedef specifier present then set 
18542      conflicting_specifiers_p which will trigger an error later
18543      on in grokdeclarator. */
18544   if (decl_specs->specs[(int)ds_typedef])
18545     decl_specs->conflicting_specifiers_p = true;
18546 }
18547
18548 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18549    is true, the type is a user-defined type; otherwise it is a
18550    built-in type specified by a keyword.  */
18551
18552 static void
18553 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18554                               tree type_spec,
18555                               location_t location,
18556                               bool user_defined_p)
18557 {
18558   decl_specs->any_specifiers_p = true;
18559
18560   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18561      (with, for example, in "typedef int wchar_t;") we remember that
18562      this is what happened.  In system headers, we ignore these
18563      declarations so that G++ can work with system headers that are not
18564      C++-safe.  */
18565   if (decl_specs->specs[(int) ds_typedef]
18566       && !user_defined_p
18567       && (type_spec == boolean_type_node
18568           || type_spec == char16_type_node
18569           || type_spec == char32_type_node
18570           || type_spec == wchar_type_node)
18571       && (decl_specs->type
18572           || decl_specs->specs[(int) ds_long]
18573           || decl_specs->specs[(int) ds_short]
18574           || decl_specs->specs[(int) ds_unsigned]
18575           || decl_specs->specs[(int) ds_signed]))
18576     {
18577       decl_specs->redefined_builtin_type = type_spec;
18578       if (!decl_specs->type)
18579         {
18580           decl_specs->type = type_spec;
18581           decl_specs->user_defined_type_p = false;
18582           decl_specs->type_location = location;
18583         }
18584     }
18585   else if (decl_specs->type)
18586     decl_specs->multiple_types_p = true;
18587   else
18588     {
18589       decl_specs->type = type_spec;
18590       decl_specs->user_defined_type_p = user_defined_p;
18591       decl_specs->redefined_builtin_type = NULL_TREE;
18592       decl_specs->type_location = location;
18593     }
18594 }
18595
18596 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18597    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18598
18599 static bool
18600 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18601 {
18602   return decl_specifiers->specs[(int) ds_friend] != 0;
18603 }
18604
18605 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18606    issue an error message indicating that TOKEN_DESC was expected.
18607
18608    Returns the token consumed, if the token had the appropriate type.
18609    Otherwise, returns NULL.  */
18610
18611 static cp_token *
18612 cp_parser_require (cp_parser* parser,
18613                    enum cpp_ttype type,
18614                    const char* token_desc)
18615 {
18616   if (cp_lexer_next_token_is (parser->lexer, type))
18617     return cp_lexer_consume_token (parser->lexer);
18618   else
18619     {
18620       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18621       if (!cp_parser_simulate_error (parser))
18622         {
18623           char *message = concat ("expected ", token_desc, NULL);
18624           cp_parser_error (parser, message);
18625           free (message);
18626         }
18627       return NULL;
18628     }
18629 }
18630
18631 /* An error message is produced if the next token is not '>'.
18632    All further tokens are skipped until the desired token is
18633    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18634
18635 static void
18636 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18637 {
18638   /* Current level of '< ... >'.  */
18639   unsigned level = 0;
18640   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18641   unsigned nesting_depth = 0;
18642
18643   /* Are we ready, yet?  If not, issue error message.  */
18644   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18645     return;
18646
18647   /* Skip tokens until the desired token is found.  */
18648   while (true)
18649     {
18650       /* Peek at the next token.  */
18651       switch (cp_lexer_peek_token (parser->lexer)->type)
18652         {
18653         case CPP_LESS:
18654           if (!nesting_depth)
18655             ++level;
18656           break;
18657
18658         case CPP_RSHIFT:
18659           if (cxx_dialect == cxx98)
18660             /* C++0x views the `>>' operator as two `>' tokens, but
18661                C++98 does not. */
18662             break;
18663           else if (!nesting_depth && level-- == 0)
18664             {
18665               /* We've hit a `>>' where the first `>' closes the
18666                  template argument list, and the second `>' is
18667                  spurious.  Just consume the `>>' and stop; we've
18668                  already produced at least one error.  */
18669               cp_lexer_consume_token (parser->lexer);
18670               return;
18671             }
18672           /* Fall through for C++0x, so we handle the second `>' in
18673              the `>>'.  */
18674
18675         case CPP_GREATER:
18676           if (!nesting_depth && level-- == 0)
18677             {
18678               /* We've reached the token we want, consume it and stop.  */
18679               cp_lexer_consume_token (parser->lexer);
18680               return;
18681             }
18682           break;
18683
18684         case CPP_OPEN_PAREN:
18685         case CPP_OPEN_SQUARE:
18686           ++nesting_depth;
18687           break;
18688
18689         case CPP_CLOSE_PAREN:
18690         case CPP_CLOSE_SQUARE:
18691           if (nesting_depth-- == 0)
18692             return;
18693           break;
18694
18695         case CPP_EOF:
18696         case CPP_PRAGMA_EOL:
18697         case CPP_SEMICOLON:
18698         case CPP_OPEN_BRACE:
18699         case CPP_CLOSE_BRACE:
18700           /* The '>' was probably forgotten, don't look further.  */
18701           return;
18702
18703         default:
18704           break;
18705         }
18706
18707       /* Consume this token.  */
18708       cp_lexer_consume_token (parser->lexer);
18709     }
18710 }
18711
18712 /* If the next token is the indicated keyword, consume it.  Otherwise,
18713    issue an error message indicating that TOKEN_DESC was expected.
18714
18715    Returns the token consumed, if the token had the appropriate type.
18716    Otherwise, returns NULL.  */
18717
18718 static cp_token *
18719 cp_parser_require_keyword (cp_parser* parser,
18720                            enum rid keyword,
18721                            const char* token_desc)
18722 {
18723   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18724
18725   if (token && token->keyword != keyword)
18726     {
18727       dyn_string_t error_msg;
18728
18729       /* Format the error message.  */
18730       error_msg = dyn_string_new (0);
18731       dyn_string_append_cstr (error_msg, "expected ");
18732       dyn_string_append_cstr (error_msg, token_desc);
18733       cp_parser_error (parser, error_msg->s);
18734       dyn_string_delete (error_msg);
18735       return NULL;
18736     }
18737
18738   return token;
18739 }
18740
18741 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18742    function-definition.  */
18743
18744 static bool
18745 cp_parser_token_starts_function_definition_p (cp_token* token)
18746 {
18747   return (/* An ordinary function-body begins with an `{'.  */
18748           token->type == CPP_OPEN_BRACE
18749           /* A ctor-initializer begins with a `:'.  */
18750           || token->type == CPP_COLON
18751           /* A function-try-block begins with `try'.  */
18752           || token->keyword == RID_TRY
18753           /* The named return value extension begins with `return'.  */
18754           || token->keyword == RID_RETURN);
18755 }
18756
18757 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18758    definition.  */
18759
18760 static bool
18761 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18762 {
18763   cp_token *token;
18764
18765   token = cp_lexer_peek_token (parser->lexer);
18766   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18767 }
18768
18769 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18770    C++0x) ending a template-argument.  */
18771
18772 static bool
18773 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18774 {
18775   cp_token *token;
18776
18777   token = cp_lexer_peek_token (parser->lexer);
18778   return (token->type == CPP_COMMA 
18779           || token->type == CPP_GREATER
18780           || token->type == CPP_ELLIPSIS
18781           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18782 }
18783
18784 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18785    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18786
18787 static bool
18788 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18789                                                      size_t n)
18790 {
18791   cp_token *token;
18792
18793   token = cp_lexer_peek_nth_token (parser->lexer, n);
18794   if (token->type == CPP_LESS)
18795     return true;
18796   /* Check for the sequence `<::' in the original code. It would be lexed as
18797      `[:', where `[' is a digraph, and there is no whitespace before
18798      `:'.  */
18799   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18800     {
18801       cp_token *token2;
18802       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18803       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18804         return true;
18805     }
18806   return false;
18807 }
18808
18809 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18810    or none_type otherwise.  */
18811
18812 static enum tag_types
18813 cp_parser_token_is_class_key (cp_token* token)
18814 {
18815   switch (token->keyword)
18816     {
18817     case RID_CLASS:
18818       return class_type;
18819     case RID_STRUCT:
18820       return record_type;
18821     case RID_UNION:
18822       return union_type;
18823
18824     default:
18825       return none_type;
18826     }
18827 }
18828
18829 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18830
18831 static void
18832 cp_parser_check_class_key (enum tag_types class_key, tree type)
18833 {
18834   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18835     permerror (input_location, "%qs tag used in naming %q#T",
18836             class_key == union_type ? "union"
18837              : class_key == record_type ? "struct" : "class",
18838              type);
18839 }
18840
18841 /* Issue an error message if DECL is redeclared with different
18842    access than its original declaration [class.access.spec/3].
18843    This applies to nested classes and nested class templates.
18844    [class.mem/1].  */
18845
18846 static void
18847 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18848 {
18849   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18850     return;
18851
18852   if ((TREE_PRIVATE (decl)
18853        != (current_access_specifier == access_private_node))
18854       || (TREE_PROTECTED (decl)
18855           != (current_access_specifier == access_protected_node)))
18856     error ("%H%qD redeclared with different access", &location, decl);
18857 }
18858
18859 /* Look for the `template' keyword, as a syntactic disambiguator.
18860    Return TRUE iff it is present, in which case it will be
18861    consumed.  */
18862
18863 static bool
18864 cp_parser_optional_template_keyword (cp_parser *parser)
18865 {
18866   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18867     {
18868       /* The `template' keyword can only be used within templates;
18869          outside templates the parser can always figure out what is a
18870          template and what is not.  */
18871       if (!processing_template_decl)
18872         {
18873           cp_token *token = cp_lexer_peek_token (parser->lexer);
18874           error ("%H%<template%> (as a disambiguator) is only allowed "
18875                  "within templates", &token->location);
18876           /* If this part of the token stream is rescanned, the same
18877              error message would be generated.  So, we purge the token
18878              from the stream.  */
18879           cp_lexer_purge_token (parser->lexer);
18880           return false;
18881         }
18882       else
18883         {
18884           /* Consume the `template' keyword.  */
18885           cp_lexer_consume_token (parser->lexer);
18886           return true;
18887         }
18888     }
18889
18890   return false;
18891 }
18892
18893 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18894    set PARSER->SCOPE, and perform other related actions.  */
18895
18896 static void
18897 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18898 {
18899   int i;
18900   struct tree_check *check_value;
18901   deferred_access_check *chk;
18902   VEC (deferred_access_check,gc) *checks;
18903
18904   /* Get the stored value.  */
18905   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18906   /* Perform any access checks that were deferred.  */
18907   checks = check_value->checks;
18908   if (checks)
18909     {
18910       for (i = 0 ;
18911            VEC_iterate (deferred_access_check, checks, i, chk) ;
18912            ++i)
18913         {
18914           perform_or_defer_access_check (chk->binfo,
18915                                          chk->decl,
18916                                          chk->diag_decl);
18917         }
18918     }
18919   /* Set the scope from the stored value.  */
18920   parser->scope = check_value->value;
18921   parser->qualifying_scope = check_value->qualifying_scope;
18922   parser->object_scope = NULL_TREE;
18923 }
18924
18925 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18926    encounter the end of a block before what we were looking for.  */
18927
18928 static bool
18929 cp_parser_cache_group (cp_parser *parser,
18930                        enum cpp_ttype end,
18931                        unsigned depth)
18932 {
18933   while (true)
18934     {
18935       cp_token *token = cp_lexer_peek_token (parser->lexer);
18936
18937       /* Abort a parenthesized expression if we encounter a semicolon.  */
18938       if ((end == CPP_CLOSE_PAREN || depth == 0)
18939           && token->type == CPP_SEMICOLON)
18940         return true;
18941       /* If we've reached the end of the file, stop.  */
18942       if (token->type == CPP_EOF
18943           || (end != CPP_PRAGMA_EOL
18944               && token->type == CPP_PRAGMA_EOL))
18945         return true;
18946       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18947         /* We've hit the end of an enclosing block, so there's been some
18948            kind of syntax error.  */
18949         return true;
18950
18951       /* Consume the token.  */
18952       cp_lexer_consume_token (parser->lexer);
18953       /* See if it starts a new group.  */
18954       if (token->type == CPP_OPEN_BRACE)
18955         {
18956           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18957           /* In theory this should probably check end == '}', but
18958              cp_parser_save_member_function_body needs it to exit
18959              after either '}' or ')' when called with ')'.  */
18960           if (depth == 0)
18961             return false;
18962         }
18963       else if (token->type == CPP_OPEN_PAREN)
18964         {
18965           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18966           if (depth == 0 && end == CPP_CLOSE_PAREN)
18967             return false;
18968         }
18969       else if (token->type == CPP_PRAGMA)
18970         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18971       else if (token->type == end)
18972         return false;
18973     }
18974 }
18975
18976 /* Begin parsing tentatively.  We always save tokens while parsing
18977    tentatively so that if the tentative parsing fails we can restore the
18978    tokens.  */
18979
18980 static void
18981 cp_parser_parse_tentatively (cp_parser* parser)
18982 {
18983   /* Enter a new parsing context.  */
18984   parser->context = cp_parser_context_new (parser->context);
18985   /* Begin saving tokens.  */
18986   cp_lexer_save_tokens (parser->lexer);
18987   /* In order to avoid repetitive access control error messages,
18988      access checks are queued up until we are no longer parsing
18989      tentatively.  */
18990   push_deferring_access_checks (dk_deferred);
18991 }
18992
18993 /* Commit to the currently active tentative parse.  */
18994
18995 static void
18996 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18997 {
18998   cp_parser_context *context;
18999   cp_lexer *lexer;
19000
19001   /* Mark all of the levels as committed.  */
19002   lexer = parser->lexer;
19003   for (context = parser->context; context->next; context = context->next)
19004     {
19005       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19006         break;
19007       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19008       while (!cp_lexer_saving_tokens (lexer))
19009         lexer = lexer->next;
19010       cp_lexer_commit_tokens (lexer);
19011     }
19012 }
19013
19014 /* Abort the currently active tentative parse.  All consumed tokens
19015    will be rolled back, and no diagnostics will be issued.  */
19016
19017 static void
19018 cp_parser_abort_tentative_parse (cp_parser* parser)
19019 {
19020   cp_parser_simulate_error (parser);
19021   /* Now, pretend that we want to see if the construct was
19022      successfully parsed.  */
19023   cp_parser_parse_definitely (parser);
19024 }
19025
19026 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19027    token stream.  Otherwise, commit to the tokens we have consumed.
19028    Returns true if no error occurred; false otherwise.  */
19029
19030 static bool
19031 cp_parser_parse_definitely (cp_parser* parser)
19032 {
19033   bool error_occurred;
19034   cp_parser_context *context;
19035
19036   /* Remember whether or not an error occurred, since we are about to
19037      destroy that information.  */
19038   error_occurred = cp_parser_error_occurred (parser);
19039   /* Remove the topmost context from the stack.  */
19040   context = parser->context;
19041   parser->context = context->next;
19042   /* If no parse errors occurred, commit to the tentative parse.  */
19043   if (!error_occurred)
19044     {
19045       /* Commit to the tokens read tentatively, unless that was
19046          already done.  */
19047       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19048         cp_lexer_commit_tokens (parser->lexer);
19049
19050       pop_to_parent_deferring_access_checks ();
19051     }
19052   /* Otherwise, if errors occurred, roll back our state so that things
19053      are just as they were before we began the tentative parse.  */
19054   else
19055     {
19056       cp_lexer_rollback_tokens (parser->lexer);
19057       pop_deferring_access_checks ();
19058     }
19059   /* Add the context to the front of the free list.  */
19060   context->next = cp_parser_context_free_list;
19061   cp_parser_context_free_list = context;
19062
19063   return !error_occurred;
19064 }
19065
19066 /* Returns true if we are parsing tentatively and are not committed to
19067    this tentative parse.  */
19068
19069 static bool
19070 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19071 {
19072   return (cp_parser_parsing_tentatively (parser)
19073           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19074 }
19075
19076 /* Returns nonzero iff an error has occurred during the most recent
19077    tentative parse.  */
19078
19079 static bool
19080 cp_parser_error_occurred (cp_parser* parser)
19081 {
19082   return (cp_parser_parsing_tentatively (parser)
19083           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19084 }
19085
19086 /* Returns nonzero if GNU extensions are allowed.  */
19087
19088 static bool
19089 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19090 {
19091   return parser->allow_gnu_extensions_p;
19092 }
19093 \f
19094 /* Objective-C++ Productions */
19095
19096
19097 /* Parse an Objective-C expression, which feeds into a primary-expression
19098    above.
19099
19100    objc-expression:
19101      objc-message-expression
19102      objc-string-literal
19103      objc-encode-expression
19104      objc-protocol-expression
19105      objc-selector-expression
19106
19107   Returns a tree representation of the expression.  */
19108
19109 static tree
19110 cp_parser_objc_expression (cp_parser* parser)
19111 {
19112   /* Try to figure out what kind of declaration is present.  */
19113   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19114
19115   switch (kwd->type)
19116     {
19117     case CPP_OPEN_SQUARE:
19118       return cp_parser_objc_message_expression (parser);
19119
19120     case CPP_OBJC_STRING:
19121       kwd = cp_lexer_consume_token (parser->lexer);
19122       return objc_build_string_object (kwd->u.value);
19123
19124     case CPP_KEYWORD:
19125       switch (kwd->keyword)
19126         {
19127         case RID_AT_ENCODE:
19128           return cp_parser_objc_encode_expression (parser);
19129
19130         case RID_AT_PROTOCOL:
19131           return cp_parser_objc_protocol_expression (parser);
19132
19133         case RID_AT_SELECTOR:
19134           return cp_parser_objc_selector_expression (parser);
19135
19136         default:
19137           break;
19138         }
19139     default:
19140       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19141              &kwd->location, kwd->u.value);
19142       cp_parser_skip_to_end_of_block_or_statement (parser);
19143     }
19144
19145   return error_mark_node;
19146 }
19147
19148 /* Parse an Objective-C message expression.
19149
19150    objc-message-expression:
19151      [ objc-message-receiver objc-message-args ]
19152
19153    Returns a representation of an Objective-C message.  */
19154
19155 static tree
19156 cp_parser_objc_message_expression (cp_parser* parser)
19157 {
19158   tree receiver, messageargs;
19159
19160   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19161   receiver = cp_parser_objc_message_receiver (parser);
19162   messageargs = cp_parser_objc_message_args (parser);
19163   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19164
19165   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19166 }
19167
19168 /* Parse an objc-message-receiver.
19169
19170    objc-message-receiver:
19171      expression
19172      simple-type-specifier
19173
19174   Returns a representation of the type or expression.  */
19175
19176 static tree
19177 cp_parser_objc_message_receiver (cp_parser* parser)
19178 {
19179   tree rcv;
19180
19181   /* An Objective-C message receiver may be either (1) a type
19182      or (2) an expression.  */
19183   cp_parser_parse_tentatively (parser);
19184   rcv = cp_parser_expression (parser, false, NULL);
19185
19186   if (cp_parser_parse_definitely (parser))
19187     return rcv;
19188
19189   rcv = cp_parser_simple_type_specifier (parser,
19190                                          /*decl_specs=*/NULL,
19191                                          CP_PARSER_FLAGS_NONE);
19192
19193   return objc_get_class_reference (rcv);
19194 }
19195
19196 /* Parse the arguments and selectors comprising an Objective-C message.
19197
19198    objc-message-args:
19199      objc-selector
19200      objc-selector-args
19201      objc-selector-args , objc-comma-args
19202
19203    objc-selector-args:
19204      objc-selector [opt] : assignment-expression
19205      objc-selector-args objc-selector [opt] : assignment-expression
19206
19207    objc-comma-args:
19208      assignment-expression
19209      objc-comma-args , assignment-expression
19210
19211    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19212    selector arguments and TREE_VALUE containing a list of comma
19213    arguments.  */
19214
19215 static tree
19216 cp_parser_objc_message_args (cp_parser* parser)
19217 {
19218   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19219   bool maybe_unary_selector_p = true;
19220   cp_token *token = cp_lexer_peek_token (parser->lexer);
19221
19222   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19223     {
19224       tree selector = NULL_TREE, arg;
19225
19226       if (token->type != CPP_COLON)
19227         selector = cp_parser_objc_selector (parser);
19228
19229       /* Detect if we have a unary selector.  */
19230       if (maybe_unary_selector_p
19231           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19232         return build_tree_list (selector, NULL_TREE);
19233
19234       maybe_unary_selector_p = false;
19235       cp_parser_require (parser, CPP_COLON, "%<:%>");
19236       arg = cp_parser_assignment_expression (parser, false, NULL);
19237
19238       sel_args
19239         = chainon (sel_args,
19240                    build_tree_list (selector, arg));
19241
19242       token = cp_lexer_peek_token (parser->lexer);
19243     }
19244
19245   /* Handle non-selector arguments, if any. */
19246   while (token->type == CPP_COMMA)
19247     {
19248       tree arg;
19249
19250       cp_lexer_consume_token (parser->lexer);
19251       arg = cp_parser_assignment_expression (parser, false, NULL);
19252
19253       addl_args
19254         = chainon (addl_args,
19255                    build_tree_list (NULL_TREE, arg));
19256
19257       token = cp_lexer_peek_token (parser->lexer);
19258     }
19259
19260   return build_tree_list (sel_args, addl_args);
19261 }
19262
19263 /* Parse an Objective-C encode expression.
19264
19265    objc-encode-expression:
19266      @encode objc-typename
19267
19268    Returns an encoded representation of the type argument.  */
19269
19270 static tree
19271 cp_parser_objc_encode_expression (cp_parser* parser)
19272 {
19273   tree type;
19274   cp_token *token;
19275
19276   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19277   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19278   token = cp_lexer_peek_token (parser->lexer);
19279   type = complete_type (cp_parser_type_id (parser));
19280   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19281
19282   if (!type)
19283     {
19284       error ("%H%<@encode%> must specify a type as an argument",
19285              &token->location);
19286       return error_mark_node;
19287     }
19288
19289   return objc_build_encode_expr (type);
19290 }
19291
19292 /* Parse an Objective-C @defs expression.  */
19293
19294 static tree
19295 cp_parser_objc_defs_expression (cp_parser *parser)
19296 {
19297   tree name;
19298
19299   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19300   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19301   name = cp_parser_identifier (parser);
19302   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19303
19304   return objc_get_class_ivars (name);
19305 }
19306
19307 /* Parse an Objective-C protocol expression.
19308
19309   objc-protocol-expression:
19310     @protocol ( identifier )
19311
19312   Returns a representation of the protocol expression.  */
19313
19314 static tree
19315 cp_parser_objc_protocol_expression (cp_parser* parser)
19316 {
19317   tree proto;
19318
19319   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19320   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19321   proto = cp_parser_identifier (parser);
19322   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19323
19324   return objc_build_protocol_expr (proto);
19325 }
19326
19327 /* Parse an Objective-C selector expression.
19328
19329    objc-selector-expression:
19330      @selector ( objc-method-signature )
19331
19332    objc-method-signature:
19333      objc-selector
19334      objc-selector-seq
19335
19336    objc-selector-seq:
19337      objc-selector :
19338      objc-selector-seq objc-selector :
19339
19340   Returns a representation of the method selector.  */
19341
19342 static tree
19343 cp_parser_objc_selector_expression (cp_parser* parser)
19344 {
19345   tree sel_seq = NULL_TREE;
19346   bool maybe_unary_selector_p = true;
19347   cp_token *token;
19348
19349   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19350   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19351   token = cp_lexer_peek_token (parser->lexer);
19352
19353   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19354          || token->type == CPP_SCOPE)
19355     {
19356       tree selector = NULL_TREE;
19357
19358       if (token->type != CPP_COLON
19359           || token->type == CPP_SCOPE)
19360         selector = cp_parser_objc_selector (parser);
19361
19362       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19363           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19364         {
19365           /* Detect if we have a unary selector.  */
19366           if (maybe_unary_selector_p)
19367             {
19368               sel_seq = selector;
19369               goto finish_selector;
19370             }
19371           else
19372             {
19373               cp_parser_error (parser, "expected %<:%>");
19374             }
19375         }
19376       maybe_unary_selector_p = false;
19377       token = cp_lexer_consume_token (parser->lexer);
19378
19379       if (token->type == CPP_SCOPE)
19380         {
19381           sel_seq
19382             = chainon (sel_seq,
19383                        build_tree_list (selector, NULL_TREE));
19384           sel_seq
19385             = chainon (sel_seq,
19386                        build_tree_list (NULL_TREE, NULL_TREE));
19387         }
19388       else
19389         sel_seq
19390           = chainon (sel_seq,
19391                      build_tree_list (selector, NULL_TREE));
19392
19393       token = cp_lexer_peek_token (parser->lexer);
19394     }
19395
19396  finish_selector:
19397   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19398
19399   return objc_build_selector_expr (sel_seq);
19400 }
19401
19402 /* Parse a list of identifiers.
19403
19404    objc-identifier-list:
19405      identifier
19406      objc-identifier-list , identifier
19407
19408    Returns a TREE_LIST of identifier nodes.  */
19409
19410 static tree
19411 cp_parser_objc_identifier_list (cp_parser* parser)
19412 {
19413   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19414   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19415
19416   while (sep->type == CPP_COMMA)
19417     {
19418       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19419       list = chainon (list,
19420                       build_tree_list (NULL_TREE,
19421                                        cp_parser_identifier (parser)));
19422       sep = cp_lexer_peek_token (parser->lexer);
19423     }
19424
19425   return list;
19426 }
19427
19428 /* Parse an Objective-C alias declaration.
19429
19430    objc-alias-declaration:
19431      @compatibility_alias identifier identifier ;
19432
19433    This function registers the alias mapping with the Objective-C front end.
19434    It returns nothing.  */
19435
19436 static void
19437 cp_parser_objc_alias_declaration (cp_parser* parser)
19438 {
19439   tree alias, orig;
19440
19441   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19442   alias = cp_parser_identifier (parser);
19443   orig = cp_parser_identifier (parser);
19444   objc_declare_alias (alias, orig);
19445   cp_parser_consume_semicolon_at_end_of_statement (parser);
19446 }
19447
19448 /* Parse an Objective-C class forward-declaration.
19449
19450    objc-class-declaration:
19451      @class objc-identifier-list ;
19452
19453    The function registers the forward declarations with the Objective-C
19454    front end.  It returns nothing.  */
19455
19456 static void
19457 cp_parser_objc_class_declaration (cp_parser* parser)
19458 {
19459   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19460   objc_declare_class (cp_parser_objc_identifier_list (parser));
19461   cp_parser_consume_semicolon_at_end_of_statement (parser);
19462 }
19463
19464 /* Parse a list of Objective-C protocol references.
19465
19466    objc-protocol-refs-opt:
19467      objc-protocol-refs [opt]
19468
19469    objc-protocol-refs:
19470      < objc-identifier-list >
19471
19472    Returns a TREE_LIST of identifiers, if any.  */
19473
19474 static tree
19475 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19476 {
19477   tree protorefs = NULL_TREE;
19478
19479   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19480     {
19481       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19482       protorefs = cp_parser_objc_identifier_list (parser);
19483       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19484     }
19485
19486   return protorefs;
19487 }
19488
19489 /* Parse a Objective-C visibility specification.  */
19490
19491 static void
19492 cp_parser_objc_visibility_spec (cp_parser* parser)
19493 {
19494   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19495
19496   switch (vis->keyword)
19497     {
19498     case RID_AT_PRIVATE:
19499       objc_set_visibility (2);
19500       break;
19501     case RID_AT_PROTECTED:
19502       objc_set_visibility (0);
19503       break;
19504     case RID_AT_PUBLIC:
19505       objc_set_visibility (1);
19506       break;
19507     default:
19508       return;
19509     }
19510
19511   /* Eat '@private'/'@protected'/'@public'.  */
19512   cp_lexer_consume_token (parser->lexer);
19513 }
19514
19515 /* Parse an Objective-C method type.  */
19516
19517 static void
19518 cp_parser_objc_method_type (cp_parser* parser)
19519 {
19520   objc_set_method_type
19521    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19522     ? PLUS_EXPR
19523     : MINUS_EXPR);
19524 }
19525
19526 /* Parse an Objective-C protocol qualifier.  */
19527
19528 static tree
19529 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19530 {
19531   tree quals = NULL_TREE, node;
19532   cp_token *token = cp_lexer_peek_token (parser->lexer);
19533
19534   node = token->u.value;
19535
19536   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19537          && (node == ridpointers [(int) RID_IN]
19538              || node == ridpointers [(int) RID_OUT]
19539              || node == ridpointers [(int) RID_INOUT]
19540              || node == ridpointers [(int) RID_BYCOPY]
19541              || node == ridpointers [(int) RID_BYREF]
19542              || node == ridpointers [(int) RID_ONEWAY]))
19543     {
19544       quals = tree_cons (NULL_TREE, node, quals);
19545       cp_lexer_consume_token (parser->lexer);
19546       token = cp_lexer_peek_token (parser->lexer);
19547       node = token->u.value;
19548     }
19549
19550   return quals;
19551 }
19552
19553 /* Parse an Objective-C typename.  */
19554
19555 static tree
19556 cp_parser_objc_typename (cp_parser* parser)
19557 {
19558   tree type_name = NULL_TREE;
19559
19560   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19561     {
19562       tree proto_quals, cp_type = NULL_TREE;
19563
19564       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19565       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19566
19567       /* An ObjC type name may consist of just protocol qualifiers, in which
19568          case the type shall default to 'id'.  */
19569       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19570         cp_type = cp_parser_type_id (parser);
19571
19572       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19573       type_name = build_tree_list (proto_quals, cp_type);
19574     }
19575
19576   return type_name;
19577 }
19578
19579 /* Check to see if TYPE refers to an Objective-C selector name.  */
19580
19581 static bool
19582 cp_parser_objc_selector_p (enum cpp_ttype type)
19583 {
19584   return (type == CPP_NAME || type == CPP_KEYWORD
19585           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19586           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19587           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19588           || type == CPP_XOR || type == CPP_XOR_EQ);
19589 }
19590
19591 /* Parse an Objective-C selector.  */
19592
19593 static tree
19594 cp_parser_objc_selector (cp_parser* parser)
19595 {
19596   cp_token *token = cp_lexer_consume_token (parser->lexer);
19597
19598   if (!cp_parser_objc_selector_p (token->type))
19599     {
19600       error ("%Hinvalid Objective-C++ selector name", &token->location);
19601       return error_mark_node;
19602     }
19603
19604   /* C++ operator names are allowed to appear in ObjC selectors.  */
19605   switch (token->type)
19606     {
19607     case CPP_AND_AND: return get_identifier ("and");
19608     case CPP_AND_EQ: return get_identifier ("and_eq");
19609     case CPP_AND: return get_identifier ("bitand");
19610     case CPP_OR: return get_identifier ("bitor");
19611     case CPP_COMPL: return get_identifier ("compl");
19612     case CPP_NOT: return get_identifier ("not");
19613     case CPP_NOT_EQ: return get_identifier ("not_eq");
19614     case CPP_OR_OR: return get_identifier ("or");
19615     case CPP_OR_EQ: return get_identifier ("or_eq");
19616     case CPP_XOR: return get_identifier ("xor");
19617     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19618     default: return token->u.value;
19619     }
19620 }
19621
19622 /* Parse an Objective-C params list.  */
19623
19624 static tree
19625 cp_parser_objc_method_keyword_params (cp_parser* parser)
19626 {
19627   tree params = NULL_TREE;
19628   bool maybe_unary_selector_p = true;
19629   cp_token *token = cp_lexer_peek_token (parser->lexer);
19630
19631   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19632     {
19633       tree selector = NULL_TREE, type_name, identifier;
19634
19635       if (token->type != CPP_COLON)
19636         selector = cp_parser_objc_selector (parser);
19637
19638       /* Detect if we have a unary selector.  */
19639       if (maybe_unary_selector_p
19640           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19641         return selector;
19642
19643       maybe_unary_selector_p = false;
19644       cp_parser_require (parser, CPP_COLON, "%<:%>");
19645       type_name = cp_parser_objc_typename (parser);
19646       identifier = cp_parser_identifier (parser);
19647
19648       params
19649         = chainon (params,
19650                    objc_build_keyword_decl (selector,
19651                                             type_name,
19652                                             identifier));
19653
19654       token = cp_lexer_peek_token (parser->lexer);
19655     }
19656
19657   return params;
19658 }
19659
19660 /* Parse the non-keyword Objective-C params.  */
19661
19662 static tree
19663 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19664 {
19665   tree params = make_node (TREE_LIST);
19666   cp_token *token = cp_lexer_peek_token (parser->lexer);
19667   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19668
19669   while (token->type == CPP_COMMA)
19670     {
19671       cp_parameter_declarator *parmdecl;
19672       tree parm;
19673
19674       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19675       token = cp_lexer_peek_token (parser->lexer);
19676
19677       if (token->type == CPP_ELLIPSIS)
19678         {
19679           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19680           *ellipsisp = true;
19681           break;
19682         }
19683
19684       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19685       parm = grokdeclarator (parmdecl->declarator,
19686                              &parmdecl->decl_specifiers,
19687                              PARM, /*initialized=*/0,
19688                              /*attrlist=*/NULL);
19689
19690       chainon (params, build_tree_list (NULL_TREE, parm));
19691       token = cp_lexer_peek_token (parser->lexer);
19692     }
19693
19694   return params;
19695 }
19696
19697 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19698
19699 static void
19700 cp_parser_objc_interstitial_code (cp_parser* parser)
19701 {
19702   cp_token *token = cp_lexer_peek_token (parser->lexer);
19703
19704   /* If the next token is `extern' and the following token is a string
19705      literal, then we have a linkage specification.  */
19706   if (token->keyword == RID_EXTERN
19707       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19708     cp_parser_linkage_specification (parser);
19709   /* Handle #pragma, if any.  */
19710   else if (token->type == CPP_PRAGMA)
19711     cp_parser_pragma (parser, pragma_external);
19712   /* Allow stray semicolons.  */
19713   else if (token->type == CPP_SEMICOLON)
19714     cp_lexer_consume_token (parser->lexer);
19715   /* Finally, try to parse a block-declaration, or a function-definition.  */
19716   else
19717     cp_parser_block_declaration (parser, /*statement_p=*/false);
19718 }
19719
19720 /* Parse a method signature.  */
19721
19722 static tree
19723 cp_parser_objc_method_signature (cp_parser* parser)
19724 {
19725   tree rettype, kwdparms, optparms;
19726   bool ellipsis = false;
19727
19728   cp_parser_objc_method_type (parser);
19729   rettype = cp_parser_objc_typename (parser);
19730   kwdparms = cp_parser_objc_method_keyword_params (parser);
19731   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19732
19733   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19734 }
19735
19736 /* Pars an Objective-C method prototype list.  */
19737
19738 static void
19739 cp_parser_objc_method_prototype_list (cp_parser* parser)
19740 {
19741   cp_token *token = cp_lexer_peek_token (parser->lexer);
19742
19743   while (token->keyword != RID_AT_END)
19744     {
19745       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19746         {
19747           objc_add_method_declaration
19748            (cp_parser_objc_method_signature (parser));
19749           cp_parser_consume_semicolon_at_end_of_statement (parser);
19750         }
19751       else
19752         /* Allow for interspersed non-ObjC++ code.  */
19753         cp_parser_objc_interstitial_code (parser);
19754
19755       token = cp_lexer_peek_token (parser->lexer);
19756     }
19757
19758   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19759   objc_finish_interface ();
19760 }
19761
19762 /* Parse an Objective-C method definition list.  */
19763
19764 static void
19765 cp_parser_objc_method_definition_list (cp_parser* parser)
19766 {
19767   cp_token *token = cp_lexer_peek_token (parser->lexer);
19768
19769   while (token->keyword != RID_AT_END)
19770     {
19771       tree meth;
19772
19773       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19774         {
19775           push_deferring_access_checks (dk_deferred);
19776           objc_start_method_definition
19777            (cp_parser_objc_method_signature (parser));
19778
19779           /* For historical reasons, we accept an optional semicolon.  */
19780           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19781             cp_lexer_consume_token (parser->lexer);
19782
19783           perform_deferred_access_checks ();
19784           stop_deferring_access_checks ();
19785           meth = cp_parser_function_definition_after_declarator (parser,
19786                                                                  false);
19787           pop_deferring_access_checks ();
19788           objc_finish_method_definition (meth);
19789         }
19790       else
19791         /* Allow for interspersed non-ObjC++ code.  */
19792         cp_parser_objc_interstitial_code (parser);
19793
19794       token = cp_lexer_peek_token (parser->lexer);
19795     }
19796
19797   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19798   objc_finish_implementation ();
19799 }
19800
19801 /* Parse Objective-C ivars.  */
19802
19803 static void
19804 cp_parser_objc_class_ivars (cp_parser* parser)
19805 {
19806   cp_token *token = cp_lexer_peek_token (parser->lexer);
19807
19808   if (token->type != CPP_OPEN_BRACE)
19809     return;     /* No ivars specified.  */
19810
19811   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19812   token = cp_lexer_peek_token (parser->lexer);
19813
19814   while (token->type != CPP_CLOSE_BRACE)
19815     {
19816       cp_decl_specifier_seq declspecs;
19817       int decl_class_or_enum_p;
19818       tree prefix_attributes;
19819
19820       cp_parser_objc_visibility_spec (parser);
19821
19822       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19823         break;
19824
19825       cp_parser_decl_specifier_seq (parser,
19826                                     CP_PARSER_FLAGS_OPTIONAL,
19827                                     &declspecs,
19828                                     &decl_class_or_enum_p);
19829       prefix_attributes = declspecs.attributes;
19830       declspecs.attributes = NULL_TREE;
19831
19832       /* Keep going until we hit the `;' at the end of the
19833          declaration.  */
19834       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19835         {
19836           tree width = NULL_TREE, attributes, first_attribute, decl;
19837           cp_declarator *declarator = NULL;
19838           int ctor_dtor_or_conv_p;
19839
19840           /* Check for a (possibly unnamed) bitfield declaration.  */
19841           token = cp_lexer_peek_token (parser->lexer);
19842           if (token->type == CPP_COLON)
19843             goto eat_colon;
19844
19845           if (token->type == CPP_NAME
19846               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19847                   == CPP_COLON))
19848             {
19849               /* Get the name of the bitfield.  */
19850               declarator = make_id_declarator (NULL_TREE,
19851                                                cp_parser_identifier (parser),
19852                                                sfk_none);
19853
19854              eat_colon:
19855               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19856               /* Get the width of the bitfield.  */
19857               width
19858                 = cp_parser_constant_expression (parser,
19859                                                  /*allow_non_constant=*/false,
19860                                                  NULL);
19861             }
19862           else
19863             {
19864               /* Parse the declarator.  */
19865               declarator
19866                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19867                                         &ctor_dtor_or_conv_p,
19868                                         /*parenthesized_p=*/NULL,
19869                                         /*member_p=*/false);
19870             }
19871
19872           /* Look for attributes that apply to the ivar.  */
19873           attributes = cp_parser_attributes_opt (parser);
19874           /* Remember which attributes are prefix attributes and
19875              which are not.  */
19876           first_attribute = attributes;
19877           /* Combine the attributes.  */
19878           attributes = chainon (prefix_attributes, attributes);
19879
19880           if (width)
19881               /* Create the bitfield declaration.  */
19882               decl = grokbitfield (declarator, &declspecs,
19883                                    width,
19884                                    attributes);
19885           else
19886             decl = grokfield (declarator, &declspecs,
19887                               NULL_TREE, /*init_const_expr_p=*/false,
19888                               NULL_TREE, attributes);
19889
19890           /* Add the instance variable.  */
19891           objc_add_instance_variable (decl);
19892
19893           /* Reset PREFIX_ATTRIBUTES.  */
19894           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19895             attributes = TREE_CHAIN (attributes);
19896           if (attributes)
19897             TREE_CHAIN (attributes) = NULL_TREE;
19898
19899           token = cp_lexer_peek_token (parser->lexer);
19900
19901           if (token->type == CPP_COMMA)
19902             {
19903               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19904               continue;
19905             }
19906           break;
19907         }
19908
19909       cp_parser_consume_semicolon_at_end_of_statement (parser);
19910       token = cp_lexer_peek_token (parser->lexer);
19911     }
19912
19913   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19914   /* For historical reasons, we accept an optional semicolon.  */
19915   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19916     cp_lexer_consume_token (parser->lexer);
19917 }
19918
19919 /* Parse an Objective-C protocol declaration.  */
19920
19921 static void
19922 cp_parser_objc_protocol_declaration (cp_parser* parser)
19923 {
19924   tree proto, protorefs;
19925   cp_token *tok;
19926
19927   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19928   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19929     {
19930       tok = cp_lexer_peek_token (parser->lexer);
19931       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19932       goto finish;
19933     }
19934
19935   /* See if we have a forward declaration or a definition.  */
19936   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19937
19938   /* Try a forward declaration first.  */
19939   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19940     {
19941       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19942      finish:
19943       cp_parser_consume_semicolon_at_end_of_statement (parser);
19944     }
19945
19946   /* Ok, we got a full-fledged definition (or at least should).  */
19947   else
19948     {
19949       proto = cp_parser_identifier (parser);
19950       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19951       objc_start_protocol (proto, protorefs);
19952       cp_parser_objc_method_prototype_list (parser);
19953     }
19954 }
19955
19956 /* Parse an Objective-C superclass or category.  */
19957
19958 static void
19959 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19960                                                           tree *categ)
19961 {
19962   cp_token *next = cp_lexer_peek_token (parser->lexer);
19963
19964   *super = *categ = NULL_TREE;
19965   if (next->type == CPP_COLON)
19966     {
19967       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19968       *super = cp_parser_identifier (parser);
19969     }
19970   else if (next->type == CPP_OPEN_PAREN)
19971     {
19972       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19973       *categ = cp_parser_identifier (parser);
19974       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19975     }
19976 }
19977
19978 /* Parse an Objective-C class interface.  */
19979
19980 static void
19981 cp_parser_objc_class_interface (cp_parser* parser)
19982 {
19983   tree name, super, categ, protos;
19984
19985   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19986   name = cp_parser_identifier (parser);
19987   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19988   protos = cp_parser_objc_protocol_refs_opt (parser);
19989
19990   /* We have either a class or a category on our hands.  */
19991   if (categ)
19992     objc_start_category_interface (name, categ, protos);
19993   else
19994     {
19995       objc_start_class_interface (name, super, protos);
19996       /* Handle instance variable declarations, if any.  */
19997       cp_parser_objc_class_ivars (parser);
19998       objc_continue_interface ();
19999     }
20000
20001   cp_parser_objc_method_prototype_list (parser);
20002 }
20003
20004 /* Parse an Objective-C class implementation.  */
20005
20006 static void
20007 cp_parser_objc_class_implementation (cp_parser* parser)
20008 {
20009   tree name, super, categ;
20010
20011   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20012   name = cp_parser_identifier (parser);
20013   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20014
20015   /* We have either a class or a category on our hands.  */
20016   if (categ)
20017     objc_start_category_implementation (name, categ);
20018   else
20019     {
20020       objc_start_class_implementation (name, super);
20021       /* Handle instance variable declarations, if any.  */
20022       cp_parser_objc_class_ivars (parser);
20023       objc_continue_implementation ();
20024     }
20025
20026   cp_parser_objc_method_definition_list (parser);
20027 }
20028
20029 /* Consume the @end token and finish off the implementation.  */
20030
20031 static void
20032 cp_parser_objc_end_implementation (cp_parser* parser)
20033 {
20034   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20035   objc_finish_implementation ();
20036 }
20037
20038 /* Parse an Objective-C declaration.  */
20039
20040 static void
20041 cp_parser_objc_declaration (cp_parser* parser)
20042 {
20043   /* Try to figure out what kind of declaration is present.  */
20044   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20045
20046   switch (kwd->keyword)
20047     {
20048     case RID_AT_ALIAS:
20049       cp_parser_objc_alias_declaration (parser);
20050       break;
20051     case RID_AT_CLASS:
20052       cp_parser_objc_class_declaration (parser);
20053       break;
20054     case RID_AT_PROTOCOL:
20055       cp_parser_objc_protocol_declaration (parser);
20056       break;
20057     case RID_AT_INTERFACE:
20058       cp_parser_objc_class_interface (parser);
20059       break;
20060     case RID_AT_IMPLEMENTATION:
20061       cp_parser_objc_class_implementation (parser);
20062       break;
20063     case RID_AT_END:
20064       cp_parser_objc_end_implementation (parser);
20065       break;
20066     default:
20067       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20068              &kwd->location, kwd->u.value);
20069       cp_parser_skip_to_end_of_block_or_statement (parser);
20070     }
20071 }
20072
20073 /* Parse an Objective-C try-catch-finally statement.
20074
20075    objc-try-catch-finally-stmt:
20076      @try compound-statement objc-catch-clause-seq [opt]
20077        objc-finally-clause [opt]
20078
20079    objc-catch-clause-seq:
20080      objc-catch-clause objc-catch-clause-seq [opt]
20081
20082    objc-catch-clause:
20083      @catch ( exception-declaration ) compound-statement
20084
20085    objc-finally-clause
20086      @finally compound-statement
20087
20088    Returns NULL_TREE.  */
20089
20090 static tree
20091 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20092   location_t location;
20093   tree stmt;
20094
20095   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20096   location = cp_lexer_peek_token (parser->lexer)->location;
20097   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20098      node, lest it get absorbed into the surrounding block.  */
20099   stmt = push_stmt_list ();
20100   cp_parser_compound_statement (parser, NULL, false);
20101   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20102
20103   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20104     {
20105       cp_parameter_declarator *parmdecl;
20106       tree parm;
20107
20108       cp_lexer_consume_token (parser->lexer);
20109       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20110       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20111       parm = grokdeclarator (parmdecl->declarator,
20112                              &parmdecl->decl_specifiers,
20113                              PARM, /*initialized=*/0,
20114                              /*attrlist=*/NULL);
20115       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20116       objc_begin_catch_clause (parm);
20117       cp_parser_compound_statement (parser, NULL, false);
20118       objc_finish_catch_clause ();
20119     }
20120
20121   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20122     {
20123       cp_lexer_consume_token (parser->lexer);
20124       location = cp_lexer_peek_token (parser->lexer)->location;
20125       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20126          node, lest it get absorbed into the surrounding block.  */
20127       stmt = push_stmt_list ();
20128       cp_parser_compound_statement (parser, NULL, false);
20129       objc_build_finally_clause (location, pop_stmt_list (stmt));
20130     }
20131
20132   return objc_finish_try_stmt ();
20133 }
20134
20135 /* Parse an Objective-C synchronized statement.
20136
20137    objc-synchronized-stmt:
20138      @synchronized ( expression ) compound-statement
20139
20140    Returns NULL_TREE.  */
20141
20142 static tree
20143 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20144   location_t location;
20145   tree lock, stmt;
20146
20147   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20148
20149   location = cp_lexer_peek_token (parser->lexer)->location;
20150   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20151   lock = cp_parser_expression (parser, false, NULL);
20152   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20153
20154   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20155      node, lest it get absorbed into the surrounding block.  */
20156   stmt = push_stmt_list ();
20157   cp_parser_compound_statement (parser, NULL, false);
20158
20159   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20160 }
20161
20162 /* Parse an Objective-C throw statement.
20163
20164    objc-throw-stmt:
20165      @throw assignment-expression [opt] ;
20166
20167    Returns a constructed '@throw' statement.  */
20168
20169 static tree
20170 cp_parser_objc_throw_statement (cp_parser *parser) {
20171   tree expr = NULL_TREE;
20172
20173   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20174
20175   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20176     expr = cp_parser_assignment_expression (parser, false, NULL);
20177
20178   cp_parser_consume_semicolon_at_end_of_statement (parser);
20179
20180   return objc_build_throw_stmt (expr);
20181 }
20182
20183 /* Parse an Objective-C statement.  */
20184
20185 static tree
20186 cp_parser_objc_statement (cp_parser * parser) {
20187   /* Try to figure out what kind of declaration is present.  */
20188   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20189
20190   switch (kwd->keyword)
20191     {
20192     case RID_AT_TRY:
20193       return cp_parser_objc_try_catch_finally_statement (parser);
20194     case RID_AT_SYNCHRONIZED:
20195       return cp_parser_objc_synchronized_statement (parser);
20196     case RID_AT_THROW:
20197       return cp_parser_objc_throw_statement (parser);
20198     default:
20199       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20200              &kwd->location, kwd->u.value);
20201       cp_parser_skip_to_end_of_block_or_statement (parser);
20202     }
20203
20204   return error_mark_node;
20205 }
20206 \f
20207 /* OpenMP 2.5 parsing routines.  */
20208
20209 /* Returns name of the next clause.
20210    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20211    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20212    returned and the token is consumed.  */
20213
20214 static pragma_omp_clause
20215 cp_parser_omp_clause_name (cp_parser *parser)
20216 {
20217   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20218
20219   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20220     result = PRAGMA_OMP_CLAUSE_IF;
20221   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20222     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20223   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20224     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20225   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20226     {
20227       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20228       const char *p = IDENTIFIER_POINTER (id);
20229
20230       switch (p[0])
20231         {
20232         case 'c':
20233           if (!strcmp ("collapse", p))
20234             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20235           else if (!strcmp ("copyin", p))
20236             result = PRAGMA_OMP_CLAUSE_COPYIN;
20237           else if (!strcmp ("copyprivate", p))
20238             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20239           break;
20240         case 'f':
20241           if (!strcmp ("firstprivate", p))
20242             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20243           break;
20244         case 'l':
20245           if (!strcmp ("lastprivate", p))
20246             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20247           break;
20248         case 'n':
20249           if (!strcmp ("nowait", p))
20250             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20251           else if (!strcmp ("num_threads", p))
20252             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20253           break;
20254         case 'o':
20255           if (!strcmp ("ordered", p))
20256             result = PRAGMA_OMP_CLAUSE_ORDERED;
20257           break;
20258         case 'r':
20259           if (!strcmp ("reduction", p))
20260             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20261           break;
20262         case 's':
20263           if (!strcmp ("schedule", p))
20264             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20265           else if (!strcmp ("shared", p))
20266             result = PRAGMA_OMP_CLAUSE_SHARED;
20267           break;
20268         case 'u':
20269           if (!strcmp ("untied", p))
20270             result = PRAGMA_OMP_CLAUSE_UNTIED;
20271           break;
20272         }
20273     }
20274
20275   if (result != PRAGMA_OMP_CLAUSE_NONE)
20276     cp_lexer_consume_token (parser->lexer);
20277
20278   return result;
20279 }
20280
20281 /* Validate that a clause of the given type does not already exist.  */
20282
20283 static void
20284 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20285                            const char *name, location_t location)
20286 {
20287   tree c;
20288
20289   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20290     if (OMP_CLAUSE_CODE (c) == code)
20291       {
20292         error ("%Htoo many %qs clauses", &location, name);
20293         break;
20294       }
20295 }
20296
20297 /* OpenMP 2.5:
20298    variable-list:
20299      identifier
20300      variable-list , identifier
20301
20302    In addition, we match a closing parenthesis.  An opening parenthesis
20303    will have been consumed by the caller.
20304
20305    If KIND is nonzero, create the appropriate node and install the decl
20306    in OMP_CLAUSE_DECL and add the node to the head of the list.
20307
20308    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20309    return the list created.  */
20310
20311 static tree
20312 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20313                                 tree list)
20314 {
20315   cp_token *token;
20316   while (1)
20317     {
20318       tree name, decl;
20319
20320       token = cp_lexer_peek_token (parser->lexer);
20321       name = cp_parser_id_expression (parser, /*template_p=*/false,
20322                                       /*check_dependency_p=*/true,
20323                                       /*template_p=*/NULL,
20324                                       /*declarator_p=*/false,
20325                                       /*optional_p=*/false);
20326       if (name == error_mark_node)
20327         goto skip_comma;
20328
20329       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20330       if (decl == error_mark_node)
20331         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20332       else if (kind != 0)
20333         {
20334           tree u = build_omp_clause (kind);
20335           OMP_CLAUSE_DECL (u) = decl;
20336           OMP_CLAUSE_CHAIN (u) = list;
20337           list = u;
20338         }
20339       else
20340         list = tree_cons (decl, NULL_TREE, list);
20341
20342     get_comma:
20343       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20344         break;
20345       cp_lexer_consume_token (parser->lexer);
20346     }
20347
20348   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20349     {
20350       int ending;
20351
20352       /* Try to resync to an unnested comma.  Copied from
20353          cp_parser_parenthesized_expression_list.  */
20354     skip_comma:
20355       ending = cp_parser_skip_to_closing_parenthesis (parser,
20356                                                       /*recovering=*/true,
20357                                                       /*or_comma=*/true,
20358                                                       /*consume_paren=*/true);
20359       if (ending < 0)
20360         goto get_comma;
20361     }
20362
20363   return list;
20364 }
20365
20366 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20367    common case for omp clauses.  */
20368
20369 static tree
20370 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20371 {
20372   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20373     return cp_parser_omp_var_list_no_open (parser, kind, list);
20374   return list;
20375 }
20376
20377 /* OpenMP 3.0:
20378    collapse ( constant-expression ) */
20379
20380 static tree
20381 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20382 {
20383   tree c, num;
20384   location_t loc;
20385   HOST_WIDE_INT n;
20386
20387   loc = cp_lexer_peek_token (parser->lexer)->location;
20388   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20389     return list;
20390
20391   num = cp_parser_constant_expression (parser, false, NULL);
20392
20393   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20394     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20395                                            /*or_comma=*/false,
20396                                            /*consume_paren=*/true);
20397
20398   if (num == error_mark_node)
20399     return list;
20400   num = fold_non_dependent_expr (num);
20401   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20402       || !host_integerp (num, 0)
20403       || (n = tree_low_cst (num, 0)) <= 0
20404       || (int) n != n)
20405     {
20406       error ("%Hcollapse argument needs positive constant integer expression",
20407              &loc);
20408       return list;
20409     }
20410
20411   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20412   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20413   OMP_CLAUSE_CHAIN (c) = list;
20414   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20415
20416   return c;
20417 }
20418
20419 /* OpenMP 2.5:
20420    default ( shared | none ) */
20421
20422 static tree
20423 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20424 {
20425   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20426   tree c;
20427
20428   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20429     return list;
20430   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20431     {
20432       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20433       const char *p = IDENTIFIER_POINTER (id);
20434
20435       switch (p[0])
20436         {
20437         case 'n':
20438           if (strcmp ("none", p) != 0)
20439             goto invalid_kind;
20440           kind = OMP_CLAUSE_DEFAULT_NONE;
20441           break;
20442
20443         case 's':
20444           if (strcmp ("shared", p) != 0)
20445             goto invalid_kind;
20446           kind = OMP_CLAUSE_DEFAULT_SHARED;
20447           break;
20448
20449         default:
20450           goto invalid_kind;
20451         }
20452
20453       cp_lexer_consume_token (parser->lexer);
20454     }
20455   else
20456     {
20457     invalid_kind:
20458       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20459     }
20460
20461   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20462     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20463                                            /*or_comma=*/false,
20464                                            /*consume_paren=*/true);
20465
20466   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20467     return list;
20468
20469   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20470   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20471   OMP_CLAUSE_CHAIN (c) = list;
20472   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20473
20474   return c;
20475 }
20476
20477 /* OpenMP 2.5:
20478    if ( expression ) */
20479
20480 static tree
20481 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20482 {
20483   tree t, c;
20484
20485   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20486     return list;
20487
20488   t = cp_parser_condition (parser);
20489
20490   if (t == error_mark_node
20491       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20492     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20493                                            /*or_comma=*/false,
20494                                            /*consume_paren=*/true);
20495
20496   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20497
20498   c = build_omp_clause (OMP_CLAUSE_IF);
20499   OMP_CLAUSE_IF_EXPR (c) = t;
20500   OMP_CLAUSE_CHAIN (c) = list;
20501
20502   return c;
20503 }
20504
20505 /* OpenMP 2.5:
20506    nowait */
20507
20508 static tree
20509 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20510                              tree list, location_t location)
20511 {
20512   tree c;
20513
20514   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20515
20516   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20517   OMP_CLAUSE_CHAIN (c) = list;
20518   return c;
20519 }
20520
20521 /* OpenMP 2.5:
20522    num_threads ( expression ) */
20523
20524 static tree
20525 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20526                                   location_t location)
20527 {
20528   tree t, c;
20529
20530   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20531     return list;
20532
20533   t = cp_parser_expression (parser, false, NULL);
20534
20535   if (t == error_mark_node
20536       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20537     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20538                                            /*or_comma=*/false,
20539                                            /*consume_paren=*/true);
20540
20541   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20542                              "num_threads", location);
20543
20544   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20545   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20546   OMP_CLAUSE_CHAIN (c) = list;
20547
20548   return c;
20549 }
20550
20551 /* OpenMP 2.5:
20552    ordered */
20553
20554 static tree
20555 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20556                               tree list, location_t location)
20557 {
20558   tree c;
20559
20560   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20561                              "ordered", location);
20562
20563   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20564   OMP_CLAUSE_CHAIN (c) = list;
20565   return c;
20566 }
20567
20568 /* OpenMP 2.5:
20569    reduction ( reduction-operator : variable-list )
20570
20571    reduction-operator:
20572      One of: + * - & ^ | && || */
20573
20574 static tree
20575 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20576 {
20577   enum tree_code code;
20578   tree nlist, c;
20579
20580   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20581     return list;
20582
20583   switch (cp_lexer_peek_token (parser->lexer)->type)
20584     {
20585     case CPP_PLUS:
20586       code = PLUS_EXPR;
20587       break;
20588     case CPP_MULT:
20589       code = MULT_EXPR;
20590       break;
20591     case CPP_MINUS:
20592       code = MINUS_EXPR;
20593       break;
20594     case CPP_AND:
20595       code = BIT_AND_EXPR;
20596       break;
20597     case CPP_XOR:
20598       code = BIT_XOR_EXPR;
20599       break;
20600     case CPP_OR:
20601       code = BIT_IOR_EXPR;
20602       break;
20603     case CPP_AND_AND:
20604       code = TRUTH_ANDIF_EXPR;
20605       break;
20606     case CPP_OR_OR:
20607       code = TRUTH_ORIF_EXPR;
20608       break;
20609     default:
20610       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20611                                "%<|%>, %<&&%>, or %<||%>");
20612     resync_fail:
20613       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20614                                              /*or_comma=*/false,
20615                                              /*consume_paren=*/true);
20616       return list;
20617     }
20618   cp_lexer_consume_token (parser->lexer);
20619
20620   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20621     goto resync_fail;
20622
20623   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20624   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20625     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20626
20627   return nlist;
20628 }
20629
20630 /* OpenMP 2.5:
20631    schedule ( schedule-kind )
20632    schedule ( schedule-kind , expression )
20633
20634    schedule-kind:
20635      static | dynamic | guided | runtime | auto  */
20636
20637 static tree
20638 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20639 {
20640   tree c, t;
20641
20642   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20643     return list;
20644
20645   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20646
20647   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20648     {
20649       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20650       const char *p = IDENTIFIER_POINTER (id);
20651
20652       switch (p[0])
20653         {
20654         case 'd':
20655           if (strcmp ("dynamic", p) != 0)
20656             goto invalid_kind;
20657           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20658           break;
20659
20660         case 'g':
20661           if (strcmp ("guided", p) != 0)
20662             goto invalid_kind;
20663           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20664           break;
20665
20666         case 'r':
20667           if (strcmp ("runtime", p) != 0)
20668             goto invalid_kind;
20669           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20670           break;
20671
20672         default:
20673           goto invalid_kind;
20674         }
20675     }
20676   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20677     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20678   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20679     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20680   else
20681     goto invalid_kind;
20682   cp_lexer_consume_token (parser->lexer);
20683
20684   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20685     {
20686       cp_token *token;
20687       cp_lexer_consume_token (parser->lexer);
20688
20689       token = cp_lexer_peek_token (parser->lexer);
20690       t = cp_parser_assignment_expression (parser, false, NULL);
20691
20692       if (t == error_mark_node)
20693         goto resync_fail;
20694       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20695         error ("%Hschedule %<runtime%> does not take "
20696                "a %<chunk_size%> parameter", &token->location);
20697       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20698         error ("%Hschedule %<auto%> does not take "
20699                "a %<chunk_size%> parameter", &token->location);
20700       else
20701         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20702
20703       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20704         goto resync_fail;
20705     }
20706   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20707     goto resync_fail;
20708
20709   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20710   OMP_CLAUSE_CHAIN (c) = list;
20711   return c;
20712
20713  invalid_kind:
20714   cp_parser_error (parser, "invalid schedule kind");
20715  resync_fail:
20716   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20717                                          /*or_comma=*/false,
20718                                          /*consume_paren=*/true);
20719   return list;
20720 }
20721
20722 /* OpenMP 3.0:
20723    untied */
20724
20725 static tree
20726 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20727                              tree list, location_t location)
20728 {
20729   tree c;
20730
20731   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20732
20733   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20734   OMP_CLAUSE_CHAIN (c) = list;
20735   return c;
20736 }
20737
20738 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20739    is a bitmask in MASK.  Return the list of clauses found; the result
20740    of clause default goes in *pdefault.  */
20741
20742 static tree
20743 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20744                            const char *where, cp_token *pragma_tok)
20745 {
20746   tree clauses = NULL;
20747   bool first = true;
20748   cp_token *token = NULL;
20749
20750   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20751     {
20752       pragma_omp_clause c_kind;
20753       const char *c_name;
20754       tree prev = clauses;
20755
20756       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20757         cp_lexer_consume_token (parser->lexer);
20758
20759       token = cp_lexer_peek_token (parser->lexer);
20760       c_kind = cp_parser_omp_clause_name (parser);
20761       first = false;
20762
20763       switch (c_kind)
20764         {
20765         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20766           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20767                                                    token->location);
20768           c_name = "collapse";
20769           break;
20770         case PRAGMA_OMP_CLAUSE_COPYIN:
20771           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20772           c_name = "copyin";
20773           break;
20774         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20775           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20776                                             clauses);
20777           c_name = "copyprivate";
20778           break;
20779         case PRAGMA_OMP_CLAUSE_DEFAULT:
20780           clauses = cp_parser_omp_clause_default (parser, clauses,
20781                                                   token->location);
20782           c_name = "default";
20783           break;
20784         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20785           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20786                                             clauses);
20787           c_name = "firstprivate";
20788           break;
20789         case PRAGMA_OMP_CLAUSE_IF:
20790           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20791           c_name = "if";
20792           break;
20793         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20794           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20795                                             clauses);
20796           c_name = "lastprivate";
20797           break;
20798         case PRAGMA_OMP_CLAUSE_NOWAIT:
20799           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20800           c_name = "nowait";
20801           break;
20802         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20803           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20804                                                       token->location);
20805           c_name = "num_threads";
20806           break;
20807         case PRAGMA_OMP_CLAUSE_ORDERED:
20808           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20809                                                   token->location);
20810           c_name = "ordered";
20811           break;
20812         case PRAGMA_OMP_CLAUSE_PRIVATE:
20813           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20814                                             clauses);
20815           c_name = "private";
20816           break;
20817         case PRAGMA_OMP_CLAUSE_REDUCTION:
20818           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20819           c_name = "reduction";
20820           break;
20821         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20822           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20823                                                    token->location);
20824           c_name = "schedule";
20825           break;
20826         case PRAGMA_OMP_CLAUSE_SHARED:
20827           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20828                                             clauses);
20829           c_name = "shared";
20830           break;
20831         case PRAGMA_OMP_CLAUSE_UNTIED:
20832           clauses = cp_parser_omp_clause_untied (parser, clauses,
20833                                                  token->location);
20834           c_name = "nowait";
20835           break;
20836         default:
20837           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20838           goto saw_error;
20839         }
20840
20841       if (((mask >> c_kind) & 1) == 0)
20842         {
20843           /* Remove the invalid clause(s) from the list to avoid
20844              confusing the rest of the compiler.  */
20845           clauses = prev;
20846           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20847         }
20848     }
20849  saw_error:
20850   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20851   return finish_omp_clauses (clauses);
20852 }
20853
20854 /* OpenMP 2.5:
20855    structured-block:
20856      statement
20857
20858    In practice, we're also interested in adding the statement to an
20859    outer node.  So it is convenient if we work around the fact that
20860    cp_parser_statement calls add_stmt.  */
20861
20862 static unsigned
20863 cp_parser_begin_omp_structured_block (cp_parser *parser)
20864 {
20865   unsigned save = parser->in_statement;
20866
20867   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20868      This preserves the "not within loop or switch" style error messages
20869      for nonsense cases like
20870         void foo() {
20871         #pragma omp single
20872           break;
20873         }
20874   */
20875   if (parser->in_statement)
20876     parser->in_statement = IN_OMP_BLOCK;
20877
20878   return save;
20879 }
20880
20881 static void
20882 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20883 {
20884   parser->in_statement = save;
20885 }
20886
20887 static tree
20888 cp_parser_omp_structured_block (cp_parser *parser)
20889 {
20890   tree stmt = begin_omp_structured_block ();
20891   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20892
20893   cp_parser_statement (parser, NULL_TREE, false, NULL);
20894
20895   cp_parser_end_omp_structured_block (parser, save);
20896   return finish_omp_structured_block (stmt);
20897 }
20898
20899 /* OpenMP 2.5:
20900    # pragma omp atomic new-line
20901      expression-stmt
20902
20903    expression-stmt:
20904      x binop= expr | x++ | ++x | x-- | --x
20905    binop:
20906      +, *, -, /, &, ^, |, <<, >>
20907
20908   where x is an lvalue expression with scalar type.  */
20909
20910 static void
20911 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20912 {
20913   tree lhs, rhs;
20914   enum tree_code code;
20915
20916   cp_parser_require_pragma_eol (parser, pragma_tok);
20917
20918   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20919                                     /*cast_p=*/false, NULL);
20920   switch (TREE_CODE (lhs))
20921     {
20922     case ERROR_MARK:
20923       goto saw_error;
20924
20925     case PREINCREMENT_EXPR:
20926     case POSTINCREMENT_EXPR:
20927       lhs = TREE_OPERAND (lhs, 0);
20928       code = PLUS_EXPR;
20929       rhs = integer_one_node;
20930       break;
20931
20932     case PREDECREMENT_EXPR:
20933     case POSTDECREMENT_EXPR:
20934       lhs = TREE_OPERAND (lhs, 0);
20935       code = MINUS_EXPR;
20936       rhs = integer_one_node;
20937       break;
20938
20939     default:
20940       switch (cp_lexer_peek_token (parser->lexer)->type)
20941         {
20942         case CPP_MULT_EQ:
20943           code = MULT_EXPR;
20944           break;
20945         case CPP_DIV_EQ:
20946           code = TRUNC_DIV_EXPR;
20947           break;
20948         case CPP_PLUS_EQ:
20949           code = PLUS_EXPR;
20950           break;
20951         case CPP_MINUS_EQ:
20952           code = MINUS_EXPR;
20953           break;
20954         case CPP_LSHIFT_EQ:
20955           code = LSHIFT_EXPR;
20956           break;
20957         case CPP_RSHIFT_EQ:
20958           code = RSHIFT_EXPR;
20959           break;
20960         case CPP_AND_EQ:
20961           code = BIT_AND_EXPR;
20962           break;
20963         case CPP_OR_EQ:
20964           code = BIT_IOR_EXPR;
20965           break;
20966         case CPP_XOR_EQ:
20967           code = BIT_XOR_EXPR;
20968           break;
20969         default:
20970           cp_parser_error (parser,
20971                            "invalid operator for %<#pragma omp atomic%>");
20972           goto saw_error;
20973         }
20974       cp_lexer_consume_token (parser->lexer);
20975
20976       rhs = cp_parser_expression (parser, false, NULL);
20977       if (rhs == error_mark_node)
20978         goto saw_error;
20979       break;
20980     }
20981   finish_omp_atomic (code, lhs, rhs);
20982   cp_parser_consume_semicolon_at_end_of_statement (parser);
20983   return;
20984
20985  saw_error:
20986   cp_parser_skip_to_end_of_block_or_statement (parser);
20987 }
20988
20989
20990 /* OpenMP 2.5:
20991    # pragma omp barrier new-line  */
20992
20993 static void
20994 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20995 {
20996   cp_parser_require_pragma_eol (parser, pragma_tok);
20997   finish_omp_barrier ();
20998 }
20999
21000 /* OpenMP 2.5:
21001    # pragma omp critical [(name)] new-line
21002      structured-block  */
21003
21004 static tree
21005 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21006 {
21007   tree stmt, name = NULL;
21008
21009   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21010     {
21011       cp_lexer_consume_token (parser->lexer);
21012
21013       name = cp_parser_identifier (parser);
21014
21015       if (name == error_mark_node
21016           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21017         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21018                                                /*or_comma=*/false,
21019                                                /*consume_paren=*/true);
21020       if (name == error_mark_node)
21021         name = NULL;
21022     }
21023   cp_parser_require_pragma_eol (parser, pragma_tok);
21024
21025   stmt = cp_parser_omp_structured_block (parser);
21026   return c_finish_omp_critical (stmt, name);
21027 }
21028
21029 /* OpenMP 2.5:
21030    # pragma omp flush flush-vars[opt] new-line
21031
21032    flush-vars:
21033      ( variable-list ) */
21034
21035 static void
21036 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21037 {
21038   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21039     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21040   cp_parser_require_pragma_eol (parser, pragma_tok);
21041
21042   finish_omp_flush ();
21043 }
21044
21045 /* Helper function, to parse omp for increment expression.  */
21046
21047 static tree
21048 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21049 {
21050   tree cond = cp_parser_binary_expression (parser, false, true,
21051                                            PREC_NOT_OPERATOR, NULL);
21052   bool overloaded_p;
21053
21054   if (cond == error_mark_node
21055       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21056     {
21057       cp_parser_skip_to_end_of_statement (parser);
21058       return error_mark_node;
21059     }
21060
21061   switch (TREE_CODE (cond))
21062     {
21063     case GT_EXPR:
21064     case GE_EXPR:
21065     case LT_EXPR:
21066     case LE_EXPR:
21067       break;
21068     default:
21069       return error_mark_node;
21070     }
21071
21072   /* If decl is an iterator, preserve LHS and RHS of the relational
21073      expr until finish_omp_for.  */
21074   if (decl
21075       && (type_dependent_expression_p (decl)
21076           || CLASS_TYPE_P (TREE_TYPE (decl))))
21077     return cond;
21078
21079   return build_x_binary_op (TREE_CODE (cond),
21080                             TREE_OPERAND (cond, 0), ERROR_MARK,
21081                             TREE_OPERAND (cond, 1), ERROR_MARK,
21082                             &overloaded_p, tf_warning_or_error);
21083 }
21084
21085 /* Helper function, to parse omp for increment expression.  */
21086
21087 static tree
21088 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21089 {
21090   cp_token *token = cp_lexer_peek_token (parser->lexer);
21091   enum tree_code op;
21092   tree lhs, rhs;
21093   cp_id_kind idk;
21094   bool decl_first;
21095
21096   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21097     {
21098       op = (token->type == CPP_PLUS_PLUS
21099             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21100       cp_lexer_consume_token (parser->lexer);
21101       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21102       if (lhs != decl)
21103         return error_mark_node;
21104       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21105     }
21106
21107   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21108   if (lhs != decl)
21109     return error_mark_node;
21110
21111   token = cp_lexer_peek_token (parser->lexer);
21112   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21113     {
21114       op = (token->type == CPP_PLUS_PLUS
21115             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21116       cp_lexer_consume_token (parser->lexer);
21117       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21118     }
21119
21120   op = cp_parser_assignment_operator_opt (parser);
21121   if (op == ERROR_MARK)
21122     return error_mark_node;
21123
21124   if (op != NOP_EXPR)
21125     {
21126       rhs = cp_parser_assignment_expression (parser, false, NULL);
21127       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21128       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21129     }
21130
21131   lhs = cp_parser_binary_expression (parser, false, false,
21132                                      PREC_ADDITIVE_EXPRESSION, NULL);
21133   token = cp_lexer_peek_token (parser->lexer);
21134   decl_first = lhs == decl;
21135   if (decl_first)
21136     lhs = NULL_TREE;
21137   if (token->type != CPP_PLUS
21138       && token->type != CPP_MINUS)
21139     return error_mark_node;
21140
21141   do
21142     {
21143       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21144       cp_lexer_consume_token (parser->lexer);
21145       rhs = cp_parser_binary_expression (parser, false, false,
21146                                          PREC_ADDITIVE_EXPRESSION, NULL);
21147       token = cp_lexer_peek_token (parser->lexer);
21148       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21149         {
21150           if (lhs == NULL_TREE)
21151             {
21152               if (op == PLUS_EXPR)
21153                 lhs = rhs;
21154               else
21155                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21156             }
21157           else
21158             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21159                                      NULL, tf_warning_or_error);
21160         }
21161     }
21162   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21163
21164   if (!decl_first)
21165     {
21166       if (rhs != decl || op == MINUS_EXPR)
21167         return error_mark_node;
21168       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21169     }
21170   else
21171     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21172
21173   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21174 }
21175
21176 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21177
21178 static tree
21179 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21180 {
21181   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21182   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21183   tree this_pre_body, cl;
21184   location_t loc_first;
21185   bool collapse_err = false;
21186   int i, collapse = 1, nbraces = 0;
21187
21188   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21189     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21190       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21191
21192   gcc_assert (collapse >= 1);
21193
21194   declv = make_tree_vec (collapse);
21195   initv = make_tree_vec (collapse);
21196   condv = make_tree_vec (collapse);
21197   incrv = make_tree_vec (collapse);
21198
21199   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21200
21201   for (i = 0; i < collapse; i++)
21202     {
21203       int bracecount = 0;
21204       bool add_private_clause = false;
21205       location_t loc;
21206
21207       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21208         {
21209           cp_parser_error (parser, "for statement expected");
21210           return NULL;
21211         }
21212       loc = cp_lexer_consume_token (parser->lexer)->location;
21213
21214       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21215         return NULL;
21216
21217       init = decl = real_decl = NULL;
21218       this_pre_body = push_stmt_list ();
21219       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21220         {
21221           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21222
21223              init-expr:
21224                        var = lb
21225                        integer-type var = lb
21226                        random-access-iterator-type var = lb
21227                        pointer-type var = lb
21228           */
21229           cp_decl_specifier_seq type_specifiers;
21230
21231           /* First, try to parse as an initialized declaration.  See
21232              cp_parser_condition, from whence the bulk of this is copied.  */
21233
21234           cp_parser_parse_tentatively (parser);
21235           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21236                                         &type_specifiers);
21237           if (cp_parser_parse_definitely (parser))
21238             {
21239               /* If parsing a type specifier seq succeeded, then this
21240                  MUST be a initialized declaration.  */
21241               tree asm_specification, attributes;
21242               cp_declarator *declarator;
21243
21244               declarator = cp_parser_declarator (parser,
21245                                                  CP_PARSER_DECLARATOR_NAMED,
21246                                                  /*ctor_dtor_or_conv_p=*/NULL,
21247                                                  /*parenthesized_p=*/NULL,
21248                                                  /*member_p=*/false);
21249               attributes = cp_parser_attributes_opt (parser);
21250               asm_specification = cp_parser_asm_specification_opt (parser);
21251
21252               if (declarator == cp_error_declarator) 
21253                 cp_parser_skip_to_end_of_statement (parser);
21254
21255               else 
21256                 {
21257                   tree pushed_scope, auto_node;
21258
21259                   decl = start_decl (declarator, &type_specifiers,
21260                                      SD_INITIALIZED, attributes,
21261                                      /*prefix_attributes=*/NULL_TREE,
21262                                      &pushed_scope);
21263
21264                   auto_node = type_uses_auto (TREE_TYPE (decl));
21265                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21266                     {
21267                       if (cp_lexer_next_token_is (parser->lexer, 
21268                                                   CPP_OPEN_PAREN))
21269                         error ("parenthesized initialization is not allowed in "
21270                                "OpenMP %<for%> loop");
21271                       else
21272                         /* Trigger an error.  */
21273                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21274
21275                       init = error_mark_node;
21276                       cp_parser_skip_to_end_of_statement (parser);
21277                     }
21278                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21279                            || type_dependent_expression_p (decl)
21280                            || auto_node)
21281                     {
21282                       bool is_direct_init, is_non_constant_init;
21283
21284                       init = cp_parser_initializer (parser,
21285                                                     &is_direct_init,
21286                                                     &is_non_constant_init);
21287
21288                       if (auto_node && describable_type (init))
21289                         {
21290                           TREE_TYPE (decl)
21291                             = do_auto_deduction (TREE_TYPE (decl), init,
21292                                                  auto_node);
21293
21294                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21295                               && !type_dependent_expression_p (decl))
21296                             goto non_class;
21297                         }
21298                       
21299                       cp_finish_decl (decl, init, !is_non_constant_init,
21300                                       asm_specification,
21301                                       LOOKUP_ONLYCONVERTING);
21302                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21303                         {
21304                           for_block
21305                             = tree_cons (NULL, this_pre_body, for_block);
21306                           init = NULL_TREE;
21307                         }
21308                       else
21309                         init = pop_stmt_list (this_pre_body);
21310                       this_pre_body = NULL_TREE;
21311                     }
21312                   else
21313                     {
21314                       /* Consume '='.  */
21315                       cp_lexer_consume_token (parser->lexer);
21316                       init = cp_parser_assignment_expression (parser, false, NULL);
21317
21318                     non_class:
21319                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21320                         init = error_mark_node;
21321                       else
21322                         cp_finish_decl (decl, NULL_TREE,
21323                                         /*init_const_expr_p=*/false,
21324                                         asm_specification,
21325                                         LOOKUP_ONLYCONVERTING);
21326                     }
21327
21328                   if (pushed_scope)
21329                     pop_scope (pushed_scope);
21330                 }
21331             }
21332           else 
21333             {
21334               cp_id_kind idk;
21335               /* If parsing a type specifier sequence failed, then
21336                  this MUST be a simple expression.  */
21337               cp_parser_parse_tentatively (parser);
21338               decl = cp_parser_primary_expression (parser, false, false,
21339                                                    false, &idk);
21340               if (!cp_parser_error_occurred (parser)
21341                   && decl
21342                   && DECL_P (decl)
21343                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21344                 {
21345                   tree rhs;
21346
21347                   cp_parser_parse_definitely (parser);
21348                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21349                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21350                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21351                                                          rhs,
21352                                                          tf_warning_or_error));
21353                   add_private_clause = true;
21354                 }
21355               else
21356                 {
21357                   decl = NULL;
21358                   cp_parser_abort_tentative_parse (parser);
21359                   init = cp_parser_expression (parser, false, NULL);
21360                   if (init)
21361                     {
21362                       if (TREE_CODE (init) == MODIFY_EXPR
21363                           || TREE_CODE (init) == MODOP_EXPR)
21364                         real_decl = TREE_OPERAND (init, 0);
21365                     }
21366                 }
21367             }
21368         }
21369       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21370       if (this_pre_body)
21371         {
21372           this_pre_body = pop_stmt_list (this_pre_body);
21373           if (pre_body)
21374             {
21375               tree t = pre_body;
21376               pre_body = push_stmt_list ();
21377               add_stmt (t);
21378               add_stmt (this_pre_body);
21379               pre_body = pop_stmt_list (pre_body);
21380             }
21381           else
21382             pre_body = this_pre_body;
21383         }
21384
21385       if (decl)
21386         real_decl = decl;
21387       if (par_clauses != NULL && real_decl != NULL_TREE)
21388         {
21389           tree *c;
21390           for (c = par_clauses; *c ; )
21391             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21392                 && OMP_CLAUSE_DECL (*c) == real_decl)
21393               {
21394                 error ("%Hiteration variable %qD should not be firstprivate",
21395                        &loc, real_decl);
21396                 *c = OMP_CLAUSE_CHAIN (*c);
21397               }
21398             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21399                      && OMP_CLAUSE_DECL (*c) == real_decl)
21400               {
21401                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21402                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21403                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21404                 OMP_CLAUSE_DECL (l) = real_decl;
21405                 OMP_CLAUSE_CHAIN (l) = clauses;
21406                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21407                 clauses = l;
21408                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21409                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21410                 add_private_clause = false;
21411               }
21412             else
21413               {
21414                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21415                     && OMP_CLAUSE_DECL (*c) == real_decl)
21416                   add_private_clause = false;
21417                 c = &OMP_CLAUSE_CHAIN (*c);
21418               }
21419         }
21420
21421       if (add_private_clause)
21422         {
21423           tree c;
21424           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21425             {
21426               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21427                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21428                   && OMP_CLAUSE_DECL (c) == decl)
21429                 break;
21430               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21431                        && OMP_CLAUSE_DECL (c) == decl)
21432                 error ("%Hiteration variable %qD should not be firstprivate",
21433                        &loc, decl);
21434               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21435                        && OMP_CLAUSE_DECL (c) == decl)
21436                 error ("%Hiteration variable %qD should not be reduction",
21437                        &loc, decl);
21438             }
21439           if (c == NULL)
21440             {
21441               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21442               OMP_CLAUSE_DECL (c) = decl;
21443               c = finish_omp_clauses (c);
21444               if (c)
21445                 {
21446                   OMP_CLAUSE_CHAIN (c) = clauses;
21447                   clauses = c;
21448                 }
21449             }
21450         }
21451
21452       cond = NULL;
21453       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21454         cond = cp_parser_omp_for_cond (parser, decl);
21455       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21456
21457       incr = NULL;
21458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21459         {
21460           /* If decl is an iterator, preserve the operator on decl
21461              until finish_omp_for.  */
21462           if (decl
21463               && (type_dependent_expression_p (decl)
21464                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21465             incr = cp_parser_omp_for_incr (parser, decl);
21466           else
21467             incr = cp_parser_expression (parser, false, NULL);
21468         }
21469
21470       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21471         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21472                                                /*or_comma=*/false,
21473                                                /*consume_paren=*/true);
21474
21475       TREE_VEC_ELT (declv, i) = decl;
21476       TREE_VEC_ELT (initv, i) = init;
21477       TREE_VEC_ELT (condv, i) = cond;
21478       TREE_VEC_ELT (incrv, i) = incr;
21479
21480       if (i == collapse - 1)
21481         break;
21482
21483       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21484          in between the collapsed for loops to be still considered perfectly
21485          nested.  Hopefully the final version clarifies this.
21486          For now handle (multiple) {'s and empty statements.  */
21487       cp_parser_parse_tentatively (parser);
21488       do
21489         {
21490           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21491             break;
21492           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21493             {
21494               cp_lexer_consume_token (parser->lexer);
21495               bracecount++;
21496             }
21497           else if (bracecount
21498                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21499             cp_lexer_consume_token (parser->lexer);
21500           else
21501             {
21502               loc = cp_lexer_peek_token (parser->lexer)->location;
21503               error ("%Hnot enough collapsed for loops", &loc);
21504               collapse_err = true;
21505               cp_parser_abort_tentative_parse (parser);
21506               declv = NULL_TREE;
21507               break;
21508             }
21509         }
21510       while (1);
21511
21512       if (declv)
21513         {
21514           cp_parser_parse_definitely (parser);
21515           nbraces += bracecount;
21516         }
21517     }
21518
21519   /* Note that we saved the original contents of this flag when we entered
21520      the structured block, and so we don't need to re-save it here.  */
21521   parser->in_statement = IN_OMP_FOR;
21522
21523   /* Note that the grammar doesn't call for a structured block here,
21524      though the loop as a whole is a structured block.  */
21525   body = push_stmt_list ();
21526   cp_parser_statement (parser, NULL_TREE, false, NULL);
21527   body = pop_stmt_list (body);
21528
21529   if (declv == NULL_TREE)
21530     ret = NULL_TREE;
21531   else
21532     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21533                           pre_body, clauses);
21534
21535   while (nbraces)
21536     {
21537       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21538         {
21539           cp_lexer_consume_token (parser->lexer);
21540           nbraces--;
21541         }
21542       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21543         cp_lexer_consume_token (parser->lexer);
21544       else
21545         {
21546           if (!collapse_err)
21547             {
21548               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21549               error ("%Hcollapsed loops not perfectly nested", &loc);
21550             }
21551           collapse_err = true;
21552           cp_parser_statement_seq_opt (parser, NULL);
21553           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21554         }
21555     }
21556
21557   while (for_block)
21558     {
21559       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21560       for_block = TREE_CHAIN (for_block);
21561     }
21562
21563   return ret;
21564 }
21565
21566 /* OpenMP 2.5:
21567    #pragma omp for for-clause[optseq] new-line
21568      for-loop  */
21569
21570 #define OMP_FOR_CLAUSE_MASK                             \
21571         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21572         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21573         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21574         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21575         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21576         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21577         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21578         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21579
21580 static tree
21581 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21582 {
21583   tree clauses, sb, ret;
21584   unsigned int save;
21585
21586   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21587                                        "#pragma omp for", pragma_tok);
21588
21589   sb = begin_omp_structured_block ();
21590   save = cp_parser_begin_omp_structured_block (parser);
21591
21592   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21593
21594   cp_parser_end_omp_structured_block (parser, save);
21595   add_stmt (finish_omp_structured_block (sb));
21596
21597   return ret;
21598 }
21599
21600 /* OpenMP 2.5:
21601    # pragma omp master new-line
21602      structured-block  */
21603
21604 static tree
21605 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21606 {
21607   cp_parser_require_pragma_eol (parser, pragma_tok);
21608   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21609 }
21610
21611 /* OpenMP 2.5:
21612    # pragma omp ordered new-line
21613      structured-block  */
21614
21615 static tree
21616 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21617 {
21618   cp_parser_require_pragma_eol (parser, pragma_tok);
21619   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21620 }
21621
21622 /* OpenMP 2.5:
21623
21624    section-scope:
21625      { section-sequence }
21626
21627    section-sequence:
21628      section-directive[opt] structured-block
21629      section-sequence section-directive structured-block  */
21630
21631 static tree
21632 cp_parser_omp_sections_scope (cp_parser *parser)
21633 {
21634   tree stmt, substmt;
21635   bool error_suppress = false;
21636   cp_token *tok;
21637
21638   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21639     return NULL_TREE;
21640
21641   stmt = push_stmt_list ();
21642
21643   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21644     {
21645       unsigned save;
21646
21647       substmt = begin_omp_structured_block ();
21648       save = cp_parser_begin_omp_structured_block (parser);
21649
21650       while (1)
21651         {
21652           cp_parser_statement (parser, NULL_TREE, false, NULL);
21653
21654           tok = cp_lexer_peek_token (parser->lexer);
21655           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21656             break;
21657           if (tok->type == CPP_CLOSE_BRACE)
21658             break;
21659           if (tok->type == CPP_EOF)
21660             break;
21661         }
21662
21663       cp_parser_end_omp_structured_block (parser, save);
21664       substmt = finish_omp_structured_block (substmt);
21665       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21666       add_stmt (substmt);
21667     }
21668
21669   while (1)
21670     {
21671       tok = cp_lexer_peek_token (parser->lexer);
21672       if (tok->type == CPP_CLOSE_BRACE)
21673         break;
21674       if (tok->type == CPP_EOF)
21675         break;
21676
21677       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21678         {
21679           cp_lexer_consume_token (parser->lexer);
21680           cp_parser_require_pragma_eol (parser, tok);
21681           error_suppress = false;
21682         }
21683       else if (!error_suppress)
21684         {
21685           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21686           error_suppress = true;
21687         }
21688
21689       substmt = cp_parser_omp_structured_block (parser);
21690       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21691       add_stmt (substmt);
21692     }
21693   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21694
21695   substmt = pop_stmt_list (stmt);
21696
21697   stmt = make_node (OMP_SECTIONS);
21698   TREE_TYPE (stmt) = void_type_node;
21699   OMP_SECTIONS_BODY (stmt) = substmt;
21700
21701   add_stmt (stmt);
21702   return stmt;
21703 }
21704
21705 /* OpenMP 2.5:
21706    # pragma omp sections sections-clause[optseq] newline
21707      sections-scope  */
21708
21709 #define OMP_SECTIONS_CLAUSE_MASK                        \
21710         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21711         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21712         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21713         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21714         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21715
21716 static tree
21717 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21718 {
21719   tree clauses, ret;
21720
21721   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21722                                        "#pragma omp sections", pragma_tok);
21723
21724   ret = cp_parser_omp_sections_scope (parser);
21725   if (ret)
21726     OMP_SECTIONS_CLAUSES (ret) = clauses;
21727
21728   return ret;
21729 }
21730
21731 /* OpenMP 2.5:
21732    # pragma parallel parallel-clause new-line
21733    # pragma parallel for parallel-for-clause new-line
21734    # pragma parallel sections parallel-sections-clause new-line  */
21735
21736 #define OMP_PARALLEL_CLAUSE_MASK                        \
21737         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21738         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21739         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21740         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21741         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21742         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21743         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21744         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21745
21746 static tree
21747 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21748 {
21749   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21750   const char *p_name = "#pragma omp parallel";
21751   tree stmt, clauses, par_clause, ws_clause, block;
21752   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21753   unsigned int save;
21754
21755   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21756     {
21757       cp_lexer_consume_token (parser->lexer);
21758       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21759       p_name = "#pragma omp parallel for";
21760       mask |= OMP_FOR_CLAUSE_MASK;
21761       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21762     }
21763   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21764     {
21765       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21766       const char *p = IDENTIFIER_POINTER (id);
21767       if (strcmp (p, "sections") == 0)
21768         {
21769           cp_lexer_consume_token (parser->lexer);
21770           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21771           p_name = "#pragma omp parallel sections";
21772           mask |= OMP_SECTIONS_CLAUSE_MASK;
21773           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21774         }
21775     }
21776
21777   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21778   block = begin_omp_parallel ();
21779   save = cp_parser_begin_omp_structured_block (parser);
21780
21781   switch (p_kind)
21782     {
21783     case PRAGMA_OMP_PARALLEL:
21784       cp_parser_statement (parser, NULL_TREE, false, NULL);
21785       par_clause = clauses;
21786       break;
21787
21788     case PRAGMA_OMP_PARALLEL_FOR:
21789       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21790       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21791       break;
21792
21793     case PRAGMA_OMP_PARALLEL_SECTIONS:
21794       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21795       stmt = cp_parser_omp_sections_scope (parser);
21796       if (stmt)
21797         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21798       break;
21799
21800     default:
21801       gcc_unreachable ();
21802     }
21803
21804   cp_parser_end_omp_structured_block (parser, save);
21805   stmt = finish_omp_parallel (par_clause, block);
21806   if (p_kind != PRAGMA_OMP_PARALLEL)
21807     OMP_PARALLEL_COMBINED (stmt) = 1;
21808   return stmt;
21809 }
21810
21811 /* OpenMP 2.5:
21812    # pragma omp single single-clause[optseq] new-line
21813      structured-block  */
21814
21815 #define OMP_SINGLE_CLAUSE_MASK                          \
21816         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21817         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21818         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21819         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21820
21821 static tree
21822 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21823 {
21824   tree stmt = make_node (OMP_SINGLE);
21825   TREE_TYPE (stmt) = void_type_node;
21826
21827   OMP_SINGLE_CLAUSES (stmt)
21828     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21829                                  "#pragma omp single", pragma_tok);
21830   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21831
21832   return add_stmt (stmt);
21833 }
21834
21835 /* OpenMP 3.0:
21836    # pragma omp task task-clause[optseq] new-line
21837      structured-block  */
21838
21839 #define OMP_TASK_CLAUSE_MASK                            \
21840         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21841         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21842         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21843         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21844         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21845         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21846
21847 static tree
21848 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21849 {
21850   tree clauses, block;
21851   unsigned int save;
21852
21853   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21854                                        "#pragma omp task", pragma_tok);
21855   block = begin_omp_task ();
21856   save = cp_parser_begin_omp_structured_block (parser);
21857   cp_parser_statement (parser, NULL_TREE, false, NULL);
21858   cp_parser_end_omp_structured_block (parser, save);
21859   return finish_omp_task (clauses, block);
21860 }
21861
21862 /* OpenMP 3.0:
21863    # pragma omp taskwait new-line  */
21864
21865 static void
21866 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21867 {
21868   cp_parser_require_pragma_eol (parser, pragma_tok);
21869   finish_omp_taskwait ();
21870 }
21871
21872 /* OpenMP 2.5:
21873    # pragma omp threadprivate (variable-list) */
21874
21875 static void
21876 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21877 {
21878   tree vars;
21879
21880   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21881   cp_parser_require_pragma_eol (parser, pragma_tok);
21882
21883   finish_omp_threadprivate (vars);
21884 }
21885
21886 /* Main entry point to OpenMP statement pragmas.  */
21887
21888 static void
21889 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21890 {
21891   tree stmt;
21892
21893   switch (pragma_tok->pragma_kind)
21894     {
21895     case PRAGMA_OMP_ATOMIC:
21896       cp_parser_omp_atomic (parser, pragma_tok);
21897       return;
21898     case PRAGMA_OMP_CRITICAL:
21899       stmt = cp_parser_omp_critical (parser, pragma_tok);
21900       break;
21901     case PRAGMA_OMP_FOR:
21902       stmt = cp_parser_omp_for (parser, pragma_tok);
21903       break;
21904     case PRAGMA_OMP_MASTER:
21905       stmt = cp_parser_omp_master (parser, pragma_tok);
21906       break;
21907     case PRAGMA_OMP_ORDERED:
21908       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21909       break;
21910     case PRAGMA_OMP_PARALLEL:
21911       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21912       break;
21913     case PRAGMA_OMP_SECTIONS:
21914       stmt = cp_parser_omp_sections (parser, pragma_tok);
21915       break;
21916     case PRAGMA_OMP_SINGLE:
21917       stmt = cp_parser_omp_single (parser, pragma_tok);
21918       break;
21919     case PRAGMA_OMP_TASK:
21920       stmt = cp_parser_omp_task (parser, pragma_tok);
21921       break;
21922     default:
21923       gcc_unreachable ();
21924     }
21925
21926   if (stmt)
21927     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21928 }
21929 \f
21930 /* The parser.  */
21931
21932 static GTY (()) cp_parser *the_parser;
21933
21934 \f
21935 /* Special handling for the first token or line in the file.  The first
21936    thing in the file might be #pragma GCC pch_preprocess, which loads a
21937    PCH file, which is a GC collection point.  So we need to handle this
21938    first pragma without benefit of an existing lexer structure.
21939
21940    Always returns one token to the caller in *FIRST_TOKEN.  This is
21941    either the true first token of the file, or the first token after
21942    the initial pragma.  */
21943
21944 static void
21945 cp_parser_initial_pragma (cp_token *first_token)
21946 {
21947   tree name = NULL;
21948
21949   cp_lexer_get_preprocessor_token (NULL, first_token);
21950   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21951     return;
21952
21953   cp_lexer_get_preprocessor_token (NULL, first_token);
21954   if (first_token->type == CPP_STRING)
21955     {
21956       name = first_token->u.value;
21957
21958       cp_lexer_get_preprocessor_token (NULL, first_token);
21959       if (first_token->type != CPP_PRAGMA_EOL)
21960         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21961                &first_token->location);
21962     }
21963   else
21964     error ("%Hexpected string literal", &first_token->location);
21965
21966   /* Skip to the end of the pragma.  */
21967   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21968     cp_lexer_get_preprocessor_token (NULL, first_token);
21969
21970   /* Now actually load the PCH file.  */
21971   if (name)
21972     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21973
21974   /* Read one more token to return to our caller.  We have to do this
21975      after reading the PCH file in, since its pointers have to be
21976      live.  */
21977   cp_lexer_get_preprocessor_token (NULL, first_token);
21978 }
21979
21980 /* Normal parsing of a pragma token.  Here we can (and must) use the
21981    regular lexer.  */
21982
21983 static bool
21984 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21985 {
21986   cp_token *pragma_tok;
21987   unsigned int id;
21988
21989   pragma_tok = cp_lexer_consume_token (parser->lexer);
21990   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21991   parser->lexer->in_pragma = true;
21992
21993   id = pragma_tok->pragma_kind;
21994   switch (id)
21995     {
21996     case PRAGMA_GCC_PCH_PREPROCESS:
21997       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21998              &pragma_tok->location);
21999       break;
22000
22001     case PRAGMA_OMP_BARRIER:
22002       switch (context)
22003         {
22004         case pragma_compound:
22005           cp_parser_omp_barrier (parser, pragma_tok);
22006           return false;
22007         case pragma_stmt:
22008           error ("%H%<#pragma omp barrier%> may only be "
22009                  "used in compound statements", &pragma_tok->location);
22010           break;
22011         default:
22012           goto bad_stmt;
22013         }
22014       break;
22015
22016     case PRAGMA_OMP_FLUSH:
22017       switch (context)
22018         {
22019         case pragma_compound:
22020           cp_parser_omp_flush (parser, pragma_tok);
22021           return false;
22022         case pragma_stmt:
22023           error ("%H%<#pragma omp flush%> may only be "
22024                  "used in compound statements", &pragma_tok->location);
22025           break;
22026         default:
22027           goto bad_stmt;
22028         }
22029       break;
22030
22031     case PRAGMA_OMP_TASKWAIT:
22032       switch (context)
22033         {
22034         case pragma_compound:
22035           cp_parser_omp_taskwait (parser, pragma_tok);
22036           return false;
22037         case pragma_stmt:
22038           error ("%H%<#pragma omp taskwait%> may only be "
22039                  "used in compound statements",
22040                  &pragma_tok->location);
22041           break;
22042         default:
22043           goto bad_stmt;
22044         }
22045       break;
22046
22047     case PRAGMA_OMP_THREADPRIVATE:
22048       cp_parser_omp_threadprivate (parser, pragma_tok);
22049       return false;
22050
22051     case PRAGMA_OMP_ATOMIC:
22052     case PRAGMA_OMP_CRITICAL:
22053     case PRAGMA_OMP_FOR:
22054     case PRAGMA_OMP_MASTER:
22055     case PRAGMA_OMP_ORDERED:
22056     case PRAGMA_OMP_PARALLEL:
22057     case PRAGMA_OMP_SECTIONS:
22058     case PRAGMA_OMP_SINGLE:
22059     case PRAGMA_OMP_TASK:
22060       if (context == pragma_external)
22061         goto bad_stmt;
22062       cp_parser_omp_construct (parser, pragma_tok);
22063       return true;
22064
22065     case PRAGMA_OMP_SECTION:
22066       error ("%H%<#pragma omp section%> may only be used in "
22067              "%<#pragma omp sections%> construct", &pragma_tok->location);
22068       break;
22069
22070     default:
22071       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22072       c_invoke_pragma_handler (id);
22073       break;
22074
22075     bad_stmt:
22076       cp_parser_error (parser, "expected declaration specifiers");
22077       break;
22078     }
22079
22080   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22081   return false;
22082 }
22083
22084 /* The interface the pragma parsers have to the lexer.  */
22085
22086 enum cpp_ttype
22087 pragma_lex (tree *value)
22088 {
22089   cp_token *tok;
22090   enum cpp_ttype ret;
22091
22092   tok = cp_lexer_peek_token (the_parser->lexer);
22093
22094   ret = tok->type;
22095   *value = tok->u.value;
22096
22097   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22098     ret = CPP_EOF;
22099   else if (ret == CPP_STRING)
22100     *value = cp_parser_string_literal (the_parser, false, false);
22101   else
22102     {
22103       cp_lexer_consume_token (the_parser->lexer);
22104       if (ret == CPP_KEYWORD)
22105         ret = CPP_NAME;
22106     }
22107
22108   return ret;
22109 }
22110
22111 \f
22112 /* External interface.  */
22113
22114 /* Parse one entire translation unit.  */
22115
22116 void
22117 c_parse_file (void)
22118 {
22119   bool error_occurred;
22120   static bool already_called = false;
22121
22122   if (already_called)
22123     {
22124       sorry ("inter-module optimizations not implemented for C++");
22125       return;
22126     }
22127   already_called = true;
22128
22129   the_parser = cp_parser_new ();
22130   push_deferring_access_checks (flag_access_control
22131                                 ? dk_no_deferred : dk_no_check);
22132   error_occurred = cp_parser_translation_unit (the_parser);
22133   the_parser = NULL;
22134 }
22135
22136 #include "gt-cp-parser.h"