OSDN Git Service

* dwarf2out.c (tls_mem_loc_descriptor): Pass 1 instead of 2
[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 /* Nonzero if we are parsing an unevaluated operand: an operand to
251    sizeof, typeof, or alignof.  */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   done_lexing = true;
314
315   gcc_assert (lexer->next_token->type != CPP_PURGED);
316   return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325   cp_token *first = cache->first;
326   cp_token *last = cache->last;
327   cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329   /* We do not own the buffer.  */
330   lexer->buffer = NULL;
331   lexer->buffer_length = 0;
332   lexer->next_token = first == last ? &eof_token : first;
333   lexer->last_token = last;
334
335   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336                                    CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339   /* Initially we are not debugging.  */
340   lexer->debugging_p = false;
341 #endif
342
343   gcc_assert (lexer->next_token->type != CPP_PURGED);
344   return lexer;
345 }
346
347 /* Frees all resources associated with LEXER.  */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352   if (lexer->buffer)
353     ggc_free (lexer->buffer);
354   VEC_free (cp_token_position, heap, lexer->saved_tokens);
355   ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output.  */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365   return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373   gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375   return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381   return pos;
382 }
383
384 /* nonzero if we are presently saving tokens.  */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN.  Return true
393    if we reach EOF.  If LEXER is NULL, assume we are handling an
394    initial #pragma pch_preprocess, and thus want the lexer to return
395    processed strings.  */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400   static int is_extern_c = 0;
401
402    /* Get a new token from the preprocessor.  */
403   token->type
404     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406   token->keyword = RID_MAX;
407   token->pragma_kind = PRAGMA_NONE;
408
409   /* On some systems, some header files are surrounded by an
410      implicit extern "C" block.  Set a flag in the token if it
411      comes from such a header.  */
412   is_extern_c += pending_lang_change;
413   pending_lang_change = 0;
414   token->implicit_extern_c = is_extern_c > 0;
415
416   /* Check to see if this token is a keyword.  */
417   if (token->type == CPP_NAME)
418     {
419       if (C_IS_RESERVED_WORD (token->u.value))
420         {
421           /* Mark this token as a keyword.  */
422           token->type = CPP_KEYWORD;
423           /* Record which keyword.  */
424           token->keyword = C_RID_CODE (token->u.value);
425         }
426       else
427         {
428           if (warn_cxx0x_compat
429               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431             {
432               /* Warn about the C++0x keyword (but still treat it as
433                  an identifier).  */
434               warning (OPT_Wc__0x_compat, 
435                        "identifier %qE will become a keyword in C++0x",
436                        token->u.value);
437
438               /* Clear out the C_RID_CODE so we don't warn about this
439                  particular identifier-turned-keyword again.  */
440               C_SET_RID_CODE (token->u.value, RID_MAX);
441             }
442
443           token->ambiguous_p = false;
444           token->keyword = RID_MAX;
445         }
446     }
447   /* Handle Objective-C++ keywords.  */
448   else if (token->type == CPP_AT_NAME)
449     {
450       token->type = CPP_KEYWORD;
451       switch (C_RID_CODE (token->u.value))
452         {
453         /* Map 'class' to '@class', 'private' to '@private', etc.  */
454         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458         case RID_THROW: token->keyword = RID_AT_THROW; break;
459         case RID_TRY: token->keyword = RID_AT_TRY; break;
460         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461         default: token->keyword = C_RID_CODE (token->u.value);
462         }
463     }
464   else if (token->type == CPP_PRAGMA)
465     {
466       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
467       token->pragma_kind = ((enum pragma_kind)
468                             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 enum
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 };
1200
1201 /* This type is used for parameters and variables which hold
1202    combinations of the above flags.  */
1203 typedef int cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse.  */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209   /* We want an abstract declarator.  */
1210   CP_PARSER_DECLARATOR_ABSTRACT,
1211   /* We want a named declarator.  */
1212   CP_PARSER_DECLARATOR_NAMED,
1213   /* We don't mind, but the name must be an unqualified-id.  */
1214   CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions.  The minimum value
1218    of PREC must be 1, because zero is reserved to quickly discriminate
1219    binary operators from other tokens.  */
1220
1221 enum cp_parser_prec
1222 {
1223   PREC_NOT_OPERATOR,
1224   PREC_LOGICAL_OR_EXPRESSION,
1225   PREC_LOGICAL_AND_EXPRESSION,
1226   PREC_INCLUSIVE_OR_EXPRESSION,
1227   PREC_EXCLUSIVE_OR_EXPRESSION,
1228   PREC_AND_EXPRESSION,
1229   PREC_EQUALITY_EXPRESSION,
1230   PREC_RELATIONAL_EXPRESSION,
1231   PREC_SHIFT_EXPRESSION,
1232   PREC_ADDITIVE_EXPRESSION,
1233   PREC_MULTIPLICATIVE_EXPRESSION,
1234   PREC_PM_EXPRESSION,
1235   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239    precedence value.  */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243   /* The token type.  */
1244   enum cpp_ttype token_type;
1245   /* The corresponding tree code.  */
1246   enum tree_code tree_type;
1247   /* The precedence of this operator.  */
1248   enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse.  */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255   /* No errors have occurred.  */
1256   CP_PARSER_STATUS_KIND_NO_ERROR,
1257   /* An error has occurred.  */
1258   CP_PARSER_STATUS_KIND_ERROR,
1259   /* We are committed to this tentative parse, whether or not an error
1260      has occurred.  */
1261   CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266   /* Left hand side of the binary operation we are currently
1267      parsing.  */
1268   tree lhs;
1269   /* Original tree code for left hand side, if it was a binary
1270      expression itself (used for -Wparentheses).  */
1271   enum tree_code lhs_type;
1272   /* Tree code for the binary operation we are parsing.  */
1273   enum tree_code tree_type;
1274   /* Precedence of the binary operation we are parsing.  */
1275   enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1279    entries because precedence levels on the stack are monotonically
1280    increasing.  */
1281 typedef struct cp_parser_expression_stack_entry
1282   cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively.  */
1285 typedef struct GTY (()) cp_parser_context {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct GTY(()) cp_parser {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629
1630 /* Statements [gram.stmt.stmt]  */
1631
1632 static void cp_parser_statement
1633   (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635   (cp_parser *);
1636 static tree cp_parser_expression_statement
1637   (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639   (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641   (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643   (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645   (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647   (cp_parser *);
1648 static void cp_parser_for_init_statement
1649   (cp_parser *);
1650 static tree cp_parser_jump_statement
1651   (cp_parser *);
1652 static void cp_parser_declaration_statement
1653   (cp_parser *);
1654
1655 static tree cp_parser_implicitly_scoped_statement
1656   (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658   (cp_parser *);
1659
1660 /* Declarations [gram.dcl.dcl] */
1661
1662 static void cp_parser_declaration_seq_opt
1663   (cp_parser *);
1664 static void cp_parser_declaration
1665   (cp_parser *);
1666 static void cp_parser_block_declaration
1667   (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669   (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673   (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675   (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678    int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682   (cp_parser *);
1683 static tree cp_parser_nonclass_name 
1684   (cp_parser* parser);
1685 static tree cp_parser_elaborated_type_specifier
1686   (cp_parser *, bool, bool);
1687 static tree cp_parser_enum_specifier
1688   (cp_parser *);
1689 static void cp_parser_enumerator_list
1690   (cp_parser *, tree);
1691 static void cp_parser_enumerator_definition
1692   (cp_parser *, tree);
1693 static tree cp_parser_namespace_name
1694   (cp_parser *);
1695 static void cp_parser_namespace_definition
1696   (cp_parser *);
1697 static void cp_parser_namespace_body
1698   (cp_parser *);
1699 static tree cp_parser_qualified_namespace_specifier
1700   (cp_parser *);
1701 static void cp_parser_namespace_alias_definition
1702   (cp_parser *);
1703 static bool cp_parser_using_declaration
1704   (cp_parser *, bool);
1705 static void cp_parser_using_directive
1706   (cp_parser *);
1707 static void cp_parser_asm_definition
1708   (cp_parser *);
1709 static void cp_parser_linkage_specification
1710   (cp_parser *);
1711 static void cp_parser_static_assert
1712   (cp_parser *, bool);
1713 static tree cp_parser_decltype
1714   (cp_parser *);
1715
1716 /* Declarators [gram.dcl.decl] */
1717
1718 static tree cp_parser_init_declarator
1719   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725   (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1727   (cp_parser *);
1728 static tree cp_parser_late_return_type_opt
1729   (cp_parser *);
1730 static tree cp_parser_declarator_id
1731   (cp_parser *, bool);
1732 static tree cp_parser_type_id
1733   (cp_parser *);
1734 static tree cp_parser_template_type_arg
1735   (cp_parser *);
1736 static tree cp_parser_type_id_1
1737   (cp_parser *, bool);
1738 static void cp_parser_type_specifier_seq
1739   (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static tree cp_parser_parameter_declaration_clause
1741   (cp_parser *);
1742 static tree cp_parser_parameter_declaration_list
1743   (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745   (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument 
1747   (cp_parser *, bool);
1748 static void cp_parser_function_body
1749   (cp_parser *);
1750 static tree cp_parser_initializer
1751   (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753   (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755   (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757   (cp_parser *, bool *);
1758
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1760   (cp_parser *);
1761
1762 /* Classes [gram.class] */
1763
1764 static tree cp_parser_class_name
1765   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1767   (cp_parser *);
1768 static tree cp_parser_class_head
1769   (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1771   (cp_parser *);
1772 static void cp_parser_member_specification_opt
1773   (cp_parser *);
1774 static void cp_parser_member_declaration
1775   (cp_parser *);
1776 static tree cp_parser_pure_specifier
1777   (cp_parser *);
1778 static tree cp_parser_constant_initializer
1779   (cp_parser *);
1780
1781 /* Derived classes [gram.class.derived] */
1782
1783 static tree cp_parser_base_clause
1784   (cp_parser *);
1785 static tree cp_parser_base_specifier
1786   (cp_parser *);
1787
1788 /* Special member functions [gram.special] */
1789
1790 static tree cp_parser_conversion_function_id
1791   (cp_parser *);
1792 static tree cp_parser_conversion_type_id
1793   (cp_parser *);
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1795   (cp_parser *);
1796 static bool cp_parser_ctor_initializer_opt
1797   (cp_parser *);
1798 static void cp_parser_mem_initializer_list
1799   (cp_parser *);
1800 static tree cp_parser_mem_initializer
1801   (cp_parser *);
1802 static tree cp_parser_mem_initializer_id
1803   (cp_parser *);
1804
1805 /* Overloading [gram.over] */
1806
1807 static tree cp_parser_operator_function_id
1808   (cp_parser *);
1809 static tree cp_parser_operator
1810   (cp_parser *);
1811
1812 /* Templates [gram.temp] */
1813
1814 static void cp_parser_template_declaration
1815   (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1817   (cp_parser *);
1818 static tree cp_parser_template_parameter
1819   (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821   (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823   (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825   (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1827   (cp_parser *);
1828 static tree cp_parser_template_argument
1829   (cp_parser *);
1830 static void cp_parser_explicit_instantiation
1831   (cp_parser *);
1832 static void cp_parser_explicit_specialization
1833   (cp_parser *);
1834
1835 /* Exception handling [gram.exception] */
1836
1837 static tree cp_parser_try_block
1838   (cp_parser *);
1839 static bool cp_parser_function_try_block
1840   (cp_parser *);
1841 static void cp_parser_handler_seq
1842   (cp_parser *);
1843 static void cp_parser_handler
1844   (cp_parser *);
1845 static tree cp_parser_exception_declaration
1846   (cp_parser *);
1847 static tree cp_parser_throw_expression
1848   (cp_parser *);
1849 static tree cp_parser_exception_specification_opt
1850   (cp_parser *);
1851 static tree cp_parser_type_id_list
1852   (cp_parser *);
1853
1854 /* GNU Extensions */
1855
1856 static tree cp_parser_asm_specification_opt
1857   (cp_parser *);
1858 static tree cp_parser_asm_operand_list
1859   (cp_parser *);
1860 static tree cp_parser_asm_clobber_list
1861   (cp_parser *);
1862 static tree cp_parser_asm_label_list
1863   (cp_parser *);
1864 static tree cp_parser_attributes_opt
1865   (cp_parser *);
1866 static tree cp_parser_attribute_list
1867   (cp_parser *);
1868 static bool cp_parser_extension_opt
1869   (cp_parser *, int *);
1870 static void cp_parser_label_declaration
1871   (cp_parser *);
1872
1873 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1874 static bool cp_parser_pragma
1875   (cp_parser *, enum pragma_context);
1876
1877 /* Objective-C++ Productions */
1878
1879 static tree cp_parser_objc_message_receiver
1880   (cp_parser *);
1881 static tree cp_parser_objc_message_args
1882   (cp_parser *);
1883 static tree cp_parser_objc_message_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_encode_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_defs_expression
1888   (cp_parser *);
1889 static tree cp_parser_objc_protocol_expression
1890   (cp_parser *);
1891 static tree cp_parser_objc_selector_expression
1892   (cp_parser *);
1893 static tree cp_parser_objc_expression
1894   (cp_parser *);
1895 static bool cp_parser_objc_selector_p
1896   (enum cpp_ttype);
1897 static tree cp_parser_objc_selector
1898   (cp_parser *);
1899 static tree cp_parser_objc_protocol_refs_opt
1900   (cp_parser *);
1901 static void cp_parser_objc_declaration
1902   (cp_parser *);
1903 static tree cp_parser_objc_statement
1904   (cp_parser *);
1905
1906 /* Utility Routines */
1907
1908 static tree cp_parser_lookup_name
1909   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1910 static tree cp_parser_lookup_name_simple
1911   (cp_parser *, tree, location_t);
1912 static tree cp_parser_maybe_treat_template_as_class
1913   (tree, bool);
1914 static bool cp_parser_check_declarator_template_parameters
1915   (cp_parser *, cp_declarator *, location_t);
1916 static bool cp_parser_check_template_parameters
1917   (cp_parser *, unsigned, location_t, cp_declarator *);
1918 static tree cp_parser_simple_cast_expression
1919   (cp_parser *);
1920 static tree cp_parser_global_scope_opt
1921   (cp_parser *, bool);
1922 static bool cp_parser_constructor_declarator_p
1923   (cp_parser *, bool);
1924 static tree cp_parser_function_definition_from_specifiers_and_declarator
1925   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1926 static tree cp_parser_function_definition_after_declarator
1927   (cp_parser *, bool);
1928 static void cp_parser_template_declaration_after_export
1929   (cp_parser *, bool);
1930 static void cp_parser_perform_template_parameter_access_checks
1931   (VEC (deferred_access_check,gc)*);
1932 static tree cp_parser_single_declaration
1933   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1934 static tree cp_parser_functional_cast
1935   (cp_parser *, tree);
1936 static tree cp_parser_save_member_function_body
1937   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1938 static tree cp_parser_enclosed_template_argument_list
1939   (cp_parser *);
1940 static void cp_parser_save_default_args
1941   (cp_parser *, tree);
1942 static void cp_parser_late_parsing_for_member
1943   (cp_parser *, tree);
1944 static void cp_parser_late_parsing_default_args
1945   (cp_parser *, tree);
1946 static tree cp_parser_sizeof_operand
1947   (cp_parser *, enum rid);
1948 static tree cp_parser_trait_expr
1949   (cp_parser *, enum rid);
1950 static bool cp_parser_declares_only_class_p
1951   (cp_parser *);
1952 static void cp_parser_set_storage_class
1953   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1954 static void cp_parser_set_decl_spec_type
1955   (cp_decl_specifier_seq *, tree, location_t, bool);
1956 static bool cp_parser_friend_p
1957   (const cp_decl_specifier_seq *);
1958 static cp_token *cp_parser_require
1959   (cp_parser *, enum cpp_ttype, const char *);
1960 static cp_token *cp_parser_require_keyword
1961   (cp_parser *, enum rid, const char *);
1962 static bool cp_parser_token_starts_function_definition_p
1963   (cp_token *);
1964 static bool cp_parser_next_token_starts_class_definition_p
1965   (cp_parser *);
1966 static bool cp_parser_next_token_ends_template_argument_p
1967   (cp_parser *);
1968 static bool cp_parser_nth_token_starts_template_argument_list_p
1969   (cp_parser *, size_t);
1970 static enum tag_types cp_parser_token_is_class_key
1971   (cp_token *);
1972 static void cp_parser_check_class_key
1973   (enum tag_types, tree type);
1974 static void cp_parser_check_access_in_redeclaration
1975   (tree type, location_t location);
1976 static bool cp_parser_optional_template_keyword
1977   (cp_parser *);
1978 static void cp_parser_pre_parsed_nested_name_specifier
1979   (cp_parser *);
1980 static bool cp_parser_cache_group
1981   (cp_parser *, enum cpp_ttype, unsigned);
1982 static void cp_parser_parse_tentatively
1983   (cp_parser *);
1984 static void cp_parser_commit_to_tentative_parse
1985   (cp_parser *);
1986 static void cp_parser_abort_tentative_parse
1987   (cp_parser *);
1988 static bool cp_parser_parse_definitely
1989   (cp_parser *);
1990 static inline bool cp_parser_parsing_tentatively
1991   (cp_parser *);
1992 static bool cp_parser_uncommitted_to_tentative_parse_p
1993   (cp_parser *);
1994 static void cp_parser_error
1995   (cp_parser *, const char *);
1996 static void cp_parser_name_lookup_error
1997   (cp_parser *, tree, tree, const char *, location_t);
1998 static bool cp_parser_simulate_error
1999   (cp_parser *);
2000 static bool cp_parser_check_type_definition
2001   (cp_parser *);
2002 static void cp_parser_check_for_definition_in_return_type
2003   (cp_declarator *, tree, location_t type_location);
2004 static void cp_parser_check_for_invalid_template_id
2005   (cp_parser *, tree, location_t location);
2006 static bool cp_parser_non_integral_constant_expression
2007   (cp_parser *, const char *);
2008 static void cp_parser_diagnose_invalid_type_name
2009   (cp_parser *, tree, tree, location_t);
2010 static bool cp_parser_parse_and_diagnose_invalid_type_name
2011   (cp_parser *);
2012 static int cp_parser_skip_to_closing_parenthesis
2013   (cp_parser *, bool, bool, bool);
2014 static void cp_parser_skip_to_end_of_statement
2015   (cp_parser *);
2016 static void cp_parser_consume_semicolon_at_end_of_statement
2017   (cp_parser *);
2018 static void cp_parser_skip_to_end_of_block_or_statement
2019   (cp_parser *);
2020 static bool cp_parser_skip_to_closing_brace
2021   (cp_parser *);
2022 static void cp_parser_skip_to_end_of_template_parameter_list
2023   (cp_parser *);
2024 static void cp_parser_skip_to_pragma_eol
2025   (cp_parser*, cp_token *);
2026 static bool cp_parser_error_occurred
2027   (cp_parser *);
2028 static bool cp_parser_allow_gnu_extensions_p
2029   (cp_parser *);
2030 static bool cp_parser_is_string_literal
2031   (cp_token *);
2032 static bool cp_parser_is_keyword
2033   (cp_token *, enum rid);
2034 static tree cp_parser_make_typename_type
2035   (cp_parser *, tree, tree, location_t location);
2036 static cp_declarator * cp_parser_make_indirect_declarator
2037   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2038
2039 /* Returns nonzero if we are parsing tentatively.  */
2040
2041 static inline bool
2042 cp_parser_parsing_tentatively (cp_parser* parser)
2043 {
2044   return parser->context->next != NULL;
2045 }
2046
2047 /* Returns nonzero if TOKEN is a string literal.  */
2048
2049 static bool
2050 cp_parser_is_string_literal (cp_token* token)
2051 {
2052   return (token->type == CPP_STRING ||
2053           token->type == CPP_STRING16 ||
2054           token->type == CPP_STRING32 ||
2055           token->type == CPP_WSTRING);
2056 }
2057
2058 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2059
2060 static bool
2061 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2062 {
2063   return token->keyword == keyword;
2064 }
2065
2066 /* If not parsing tentatively, issue a diagnostic of the form
2067       FILE:LINE: MESSAGE before TOKEN
2068    where TOKEN is the next token in the input stream.  MESSAGE
2069    (specified by the caller) is usually of the form "expected
2070    OTHER-TOKEN".  */
2071
2072 static void
2073 cp_parser_error (cp_parser* parser, const char* message)
2074 {
2075   if (!cp_parser_simulate_error (parser))
2076     {
2077       cp_token *token = cp_lexer_peek_token (parser->lexer);
2078       /* This diagnostic makes more sense if it is tagged to the line
2079          of the token we just peeked at.  */
2080       cp_lexer_set_source_position_from_token (token);
2081
2082       if (token->type == CPP_PRAGMA)
2083         {
2084           error_at (token->location,
2085                     "%<#pragma%> is not allowed here");
2086           cp_parser_skip_to_pragma_eol (parser, token);
2087           return;
2088         }
2089
2090       c_parse_error (message,
2091                      /* Because c_parser_error does not understand
2092                         CPP_KEYWORD, keywords are treated like
2093                         identifiers.  */
2094                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2095                      token->u.value, token->flags);
2096     }
2097 }
2098
2099 /* Issue an error about name-lookup failing.  NAME is the
2100    IDENTIFIER_NODE DECL is the result of
2101    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2102    the thing that we hoped to find.  */
2103
2104 static void
2105 cp_parser_name_lookup_error (cp_parser* parser,
2106                              tree name,
2107                              tree decl,
2108                              const char* desired,
2109                              location_t location)
2110 {
2111   /* If name lookup completely failed, tell the user that NAME was not
2112      declared.  */
2113   if (decl == error_mark_node)
2114     {
2115       if (parser->scope && parser->scope != global_namespace)
2116         error_at (location, "%<%E::%E%> has not been declared",
2117                   parser->scope, name);
2118       else if (parser->scope == global_namespace)
2119         error_at (location, "%<::%E%> has not been declared", name);
2120       else if (parser->object_scope
2121                && !CLASS_TYPE_P (parser->object_scope))
2122         error_at (location, "request for member %qE in non-class type %qT",
2123                   name, parser->object_scope);
2124       else if (parser->object_scope)
2125         error_at (location, "%<%T::%E%> has not been declared",
2126                   parser->object_scope, name);
2127       else
2128         error_at (location, "%qE has not been declared", name);
2129     }
2130   else if (parser->scope && parser->scope != global_namespace)
2131     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2132   else if (parser->scope == global_namespace)
2133     error_at (location, "%<::%E%> %s", name, desired);
2134   else
2135     error_at (location, "%qE %s", name, desired);
2136 }
2137
2138 /* If we are parsing tentatively, remember that an error has occurred
2139    during this tentative parse.  Returns true if the error was
2140    simulated; false if a message should be issued by the caller.  */
2141
2142 static bool
2143 cp_parser_simulate_error (cp_parser* parser)
2144 {
2145   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2146     {
2147       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2148       return true;
2149     }
2150   return false;
2151 }
2152
2153 /* Check for repeated decl-specifiers.  */
2154
2155 static void
2156 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2157                            location_t location)
2158 {
2159   int ds;
2160
2161   for (ds = ds_first; ds != ds_last; ++ds)
2162     {
2163       unsigned count = decl_specs->specs[ds];
2164       if (count < 2)
2165         continue;
2166       /* The "long" specifier is a special case because of "long long".  */
2167       if (ds == ds_long)
2168         {
2169           if (count > 2)
2170             error_at (location, "%<long long long%> is too long for GCC");
2171           else 
2172             pedwarn_cxx98 (location, OPT_Wlong_long, 
2173                            "ISO C++ 1998 does not support %<long long%>");
2174         }
2175       else if (count > 1)
2176         {
2177           static const char *const decl_spec_names[] = {
2178             "signed",
2179             "unsigned",
2180             "short",
2181             "long",
2182             "const",
2183             "volatile",
2184             "restrict",
2185             "inline",
2186             "virtual",
2187             "explicit",
2188             "friend",
2189             "typedef",
2190             "__complex",
2191             "__thread"
2192           };
2193           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2194         }
2195     }
2196 }
2197
2198 /* This function is called when a type is defined.  If type
2199    definitions are forbidden at this point, an error message is
2200    issued.  */
2201
2202 static bool
2203 cp_parser_check_type_definition (cp_parser* parser)
2204 {
2205   /* If types are forbidden here, issue a message.  */
2206   if (parser->type_definition_forbidden_message)
2207     {
2208       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2209          in the message need to be interpreted.  */
2210       error (parser->type_definition_forbidden_message);
2211       return false;
2212     }
2213   return true;
2214 }
2215
2216 /* This function is called when the DECLARATOR is processed.  The TYPE
2217    was a type defined in the decl-specifiers.  If it is invalid to
2218    define a type in the decl-specifiers for DECLARATOR, an error is
2219    issued. TYPE_LOCATION is the location of TYPE and is used
2220    for error reporting.  */
2221
2222 static void
2223 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2224                                                tree type, location_t type_location)
2225 {
2226   /* [dcl.fct] forbids type definitions in return types.
2227      Unfortunately, it's not easy to know whether or not we are
2228      processing a return type until after the fact.  */
2229   while (declarator
2230          && (declarator->kind == cdk_pointer
2231              || declarator->kind == cdk_reference
2232              || declarator->kind == cdk_ptrmem))
2233     declarator = declarator->declarator;
2234   if (declarator
2235       && declarator->kind == cdk_function)
2236     {
2237       error_at (type_location,
2238                 "new types may not be defined in a return type");
2239       inform (type_location, 
2240               "(perhaps a semicolon is missing after the definition of %qT)",
2241               type);
2242     }
2243 }
2244
2245 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2246    "<" in any valid C++ program.  If the next token is indeed "<",
2247    issue a message warning the user about what appears to be an
2248    invalid attempt to form a template-id. LOCATION is the location
2249    of the type-specifier (TYPE) */
2250
2251 static void
2252 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2253                                          tree type, location_t location)
2254 {
2255   cp_token_position start = 0;
2256
2257   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2258     {
2259       if (TYPE_P (type))
2260         error_at (location, "%qT is not a template", type);
2261       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2262         error_at (location, "%qE is not a template", type);
2263       else
2264         error_at (location, "invalid template-id");
2265       /* Remember the location of the invalid "<".  */
2266       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2267         start = cp_lexer_token_position (parser->lexer, true);
2268       /* Consume the "<".  */
2269       cp_lexer_consume_token (parser->lexer);
2270       /* Parse the template arguments.  */
2271       cp_parser_enclosed_template_argument_list (parser);
2272       /* Permanently remove the invalid template arguments so that
2273          this error message is not issued again.  */
2274       if (start)
2275         cp_lexer_purge_tokens_after (parser->lexer, start);
2276     }
2277 }
2278
2279 /* If parsing an integral constant-expression, issue an error message
2280    about the fact that THING appeared and return true.  Otherwise,
2281    return false.  In either case, set
2282    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2283
2284 static bool
2285 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2286                                             const char *thing)
2287 {
2288   parser->non_integral_constant_expression_p = true;
2289   if (parser->integral_constant_expression_p)
2290     {
2291       if (!parser->allow_non_integral_constant_expression_p)
2292         {
2293           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2294              in the message need to be interpreted.  */
2295           char *message = concat (thing,
2296                                   " cannot appear in a constant-expression",
2297                                   NULL);
2298           error (message);
2299           free (message);
2300           return true;
2301         }
2302     }
2303   return false;
2304 }
2305
2306 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2307    qualifying scope (or NULL, if none) for ID.  This function commits
2308    to the current active tentative parse, if any.  (Otherwise, the
2309    problematic construct might be encountered again later, resulting
2310    in duplicate error messages.) LOCATION is the location of ID.  */
2311
2312 static void
2313 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2314                                       tree scope, tree id,
2315                                       location_t location)
2316 {
2317   tree decl, old_scope;
2318   /* Try to lookup the identifier.  */
2319   old_scope = parser->scope;
2320   parser->scope = scope;
2321   decl = cp_parser_lookup_name_simple (parser, id, location);
2322   parser->scope = old_scope;
2323   /* If the lookup found a template-name, it means that the user forgot
2324   to specify an argument list. Emit a useful error message.  */
2325   if (TREE_CODE (decl) == TEMPLATE_DECL)
2326     error_at (location,
2327               "invalid use of template-name %qE without an argument list",
2328               decl);
2329   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2330     error_at (location, "invalid use of destructor %qD as a type", id);
2331   else if (TREE_CODE (decl) == TYPE_DECL)
2332     /* Something like 'unsigned A a;'  */
2333     error_at (location, "invalid combination of multiple type-specifiers");
2334   else if (!parser->scope)
2335     {
2336       /* Issue an error message.  */
2337       error_at (location, "%qE does not name a type", id);
2338       /* If we're in a template class, it's possible that the user was
2339          referring to a type from a base class.  For example:
2340
2341            template <typename T> struct A { typedef T X; };
2342            template <typename T> struct B : public A<T> { X x; };
2343
2344          The user should have said "typename A<T>::X".  */
2345       if (processing_template_decl && current_class_type
2346           && TYPE_BINFO (current_class_type))
2347         {
2348           tree b;
2349
2350           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2351                b;
2352                b = TREE_CHAIN (b))
2353             {
2354               tree base_type = BINFO_TYPE (b);
2355               if (CLASS_TYPE_P (base_type)
2356                   && dependent_type_p (base_type))
2357                 {
2358                   tree field;
2359                   /* Go from a particular instantiation of the
2360                      template (which will have an empty TYPE_FIELDs),
2361                      to the main version.  */
2362                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2363                   for (field = TYPE_FIELDS (base_type);
2364                        field;
2365                        field = TREE_CHAIN (field))
2366                     if (TREE_CODE (field) == TYPE_DECL
2367                         && DECL_NAME (field) == id)
2368                       {
2369                         inform (location, 
2370                                 "(perhaps %<typename %T::%E%> was intended)",
2371                                 BINFO_TYPE (b), id);
2372                         break;
2373                       }
2374                   if (field)
2375                     break;
2376                 }
2377             }
2378         }
2379     }
2380   /* Here we diagnose qualified-ids where the scope is actually correct,
2381      but the identifier does not resolve to a valid type name.  */
2382   else if (parser->scope != error_mark_node)
2383     {
2384       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2385         error_at (location, "%qE in namespace %qE does not name a type",
2386                   id, parser->scope);
2387       else if (TYPE_P (parser->scope))
2388         error_at (location, "%qE in class %qT does not name a type",
2389                   id, parser->scope);
2390       else
2391         gcc_unreachable ();
2392     }
2393   cp_parser_commit_to_tentative_parse (parser);
2394 }
2395
2396 /* Check for a common situation where a type-name should be present,
2397    but is not, and issue a sensible error message.  Returns true if an
2398    invalid type-name was detected.
2399
2400    The situation handled by this function are variable declarations of the
2401    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2402    Usually, `ID' should name a type, but if we got here it means that it
2403    does not. We try to emit the best possible error message depending on
2404    how exactly the id-expression looks like.  */
2405
2406 static bool
2407 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2408 {
2409   tree id;
2410   cp_token *token = cp_lexer_peek_token (parser->lexer);
2411
2412   cp_parser_parse_tentatively (parser);
2413   id = cp_parser_id_expression (parser,
2414                                 /*template_keyword_p=*/false,
2415                                 /*check_dependency_p=*/true,
2416                                 /*template_p=*/NULL,
2417                                 /*declarator_p=*/true,
2418                                 /*optional_p=*/false);
2419   /* After the id-expression, there should be a plain identifier,
2420      otherwise this is not a simple variable declaration. Also, if
2421      the scope is dependent, we cannot do much.  */
2422   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2423       || (parser->scope && TYPE_P (parser->scope)
2424           && dependent_type_p (parser->scope))
2425       || TREE_CODE (id) == TYPE_DECL)
2426     {
2427       cp_parser_abort_tentative_parse (parser);
2428       return false;
2429     }
2430   if (!cp_parser_parse_definitely (parser))
2431     return false;
2432
2433   /* Emit a diagnostic for the invalid type.  */
2434   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2435                                         id, token->location);
2436   /* Skip to the end of the declaration; there's no point in
2437      trying to process it.  */
2438   cp_parser_skip_to_end_of_block_or_statement (parser);
2439   return true;
2440 }
2441
2442 /* Consume tokens up to, and including, the next non-nested closing `)'.
2443    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2444    are doing error recovery. Returns -1 if OR_COMMA is true and we
2445    found an unnested comma.  */
2446
2447 static int
2448 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2449                                        bool recovering,
2450                                        bool or_comma,
2451                                        bool consume_paren)
2452 {
2453   unsigned paren_depth = 0;
2454   unsigned brace_depth = 0;
2455
2456   if (recovering && !or_comma
2457       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2458     return 0;
2459
2460   while (true)
2461     {
2462       cp_token * token = cp_lexer_peek_token (parser->lexer);
2463
2464       switch (token->type)
2465         {
2466         case CPP_EOF:
2467         case CPP_PRAGMA_EOL:
2468           /* If we've run out of tokens, then there is no closing `)'.  */
2469           return 0;
2470
2471         case CPP_SEMICOLON:
2472           /* This matches the processing in skip_to_end_of_statement.  */
2473           if (!brace_depth)
2474             return 0;
2475           break;
2476
2477         case CPP_OPEN_BRACE:
2478           ++brace_depth;
2479           break;
2480         case CPP_CLOSE_BRACE:
2481           if (!brace_depth--)
2482             return 0;
2483           break;
2484
2485         case CPP_COMMA:
2486           if (recovering && or_comma && !brace_depth && !paren_depth)
2487             return -1;
2488           break;
2489
2490         case CPP_OPEN_PAREN:
2491           if (!brace_depth)
2492             ++paren_depth;
2493           break;
2494
2495         case CPP_CLOSE_PAREN:
2496           if (!brace_depth && !paren_depth--)
2497             {
2498               if (consume_paren)
2499                 cp_lexer_consume_token (parser->lexer);
2500               return 1;
2501             }
2502           break;
2503
2504         default:
2505           break;
2506         }
2507
2508       /* Consume the token.  */
2509       cp_lexer_consume_token (parser->lexer);
2510     }
2511 }
2512
2513 /* Consume tokens until we reach the end of the current statement.
2514    Normally, that will be just before consuming a `;'.  However, if a
2515    non-nested `}' comes first, then we stop before consuming that.  */
2516
2517 static void
2518 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2519 {
2520   unsigned nesting_depth = 0;
2521
2522   while (true)
2523     {
2524       cp_token *token = cp_lexer_peek_token (parser->lexer);
2525
2526       switch (token->type)
2527         {
2528         case CPP_EOF:
2529         case CPP_PRAGMA_EOL:
2530           /* If we've run out of tokens, stop.  */
2531           return;
2532
2533         case CPP_SEMICOLON:
2534           /* If the next token is a `;', we have reached the end of the
2535              statement.  */
2536           if (!nesting_depth)
2537             return;
2538           break;
2539
2540         case CPP_CLOSE_BRACE:
2541           /* If this is a non-nested '}', stop before consuming it.
2542              That way, when confronted with something like:
2543
2544                { 3 + }
2545
2546              we stop before consuming the closing '}', even though we
2547              have not yet reached a `;'.  */
2548           if (nesting_depth == 0)
2549             return;
2550
2551           /* If it is the closing '}' for a block that we have
2552              scanned, stop -- but only after consuming the token.
2553              That way given:
2554
2555                 void f g () { ... }
2556                 typedef int I;
2557
2558              we will stop after the body of the erroneously declared
2559              function, but before consuming the following `typedef'
2560              declaration.  */
2561           if (--nesting_depth == 0)
2562             {
2563               cp_lexer_consume_token (parser->lexer);
2564               return;
2565             }
2566
2567         case CPP_OPEN_BRACE:
2568           ++nesting_depth;
2569           break;
2570
2571         default:
2572           break;
2573         }
2574
2575       /* Consume the token.  */
2576       cp_lexer_consume_token (parser->lexer);
2577     }
2578 }
2579
2580 /* This function is called at the end of a statement or declaration.
2581    If the next token is a semicolon, it is consumed; otherwise, error
2582    recovery is attempted.  */
2583
2584 static void
2585 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2586 {
2587   /* Look for the trailing `;'.  */
2588   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2589     {
2590       /* If there is additional (erroneous) input, skip to the end of
2591          the statement.  */
2592       cp_parser_skip_to_end_of_statement (parser);
2593       /* If the next token is now a `;', consume it.  */
2594       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2595         cp_lexer_consume_token (parser->lexer);
2596     }
2597 }
2598
2599 /* Skip tokens until we have consumed an entire block, or until we
2600    have consumed a non-nested `;'.  */
2601
2602 static void
2603 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2604 {
2605   int nesting_depth = 0;
2606
2607   while (nesting_depth >= 0)
2608     {
2609       cp_token *token = cp_lexer_peek_token (parser->lexer);
2610
2611       switch (token->type)
2612         {
2613         case CPP_EOF:
2614         case CPP_PRAGMA_EOL:
2615           /* If we've run out of tokens, stop.  */
2616           return;
2617
2618         case CPP_SEMICOLON:
2619           /* Stop if this is an unnested ';'. */
2620           if (!nesting_depth)
2621             nesting_depth = -1;
2622           break;
2623
2624         case CPP_CLOSE_BRACE:
2625           /* Stop if this is an unnested '}', or closes the outermost
2626              nesting level.  */
2627           nesting_depth--;
2628           if (nesting_depth < 0)
2629             return;
2630           if (!nesting_depth)
2631             nesting_depth = -1;
2632           break;
2633
2634         case CPP_OPEN_BRACE:
2635           /* Nest. */
2636           nesting_depth++;
2637           break;
2638
2639         default:
2640           break;
2641         }
2642
2643       /* Consume the token.  */
2644       cp_lexer_consume_token (parser->lexer);
2645     }
2646 }
2647
2648 /* Skip tokens until a non-nested closing curly brace is the next
2649    token, or there are no more tokens. Return true in the first case,
2650    false otherwise.  */
2651
2652 static bool
2653 cp_parser_skip_to_closing_brace (cp_parser *parser)
2654 {
2655   unsigned nesting_depth = 0;
2656
2657   while (true)
2658     {
2659       cp_token *token = cp_lexer_peek_token (parser->lexer);
2660
2661       switch (token->type)
2662         {
2663         case CPP_EOF:
2664         case CPP_PRAGMA_EOL:
2665           /* If we've run out of tokens, stop.  */
2666           return false;
2667
2668         case CPP_CLOSE_BRACE:
2669           /* If the next token is a non-nested `}', then we have reached
2670              the end of the current block.  */
2671           if (nesting_depth-- == 0)
2672             return true;
2673           break;
2674
2675         case CPP_OPEN_BRACE:
2676           /* If it the next token is a `{', then we are entering a new
2677              block.  Consume the entire block.  */
2678           ++nesting_depth;
2679           break;
2680
2681         default:
2682           break;
2683         }
2684
2685       /* Consume the token.  */
2686       cp_lexer_consume_token (parser->lexer);
2687     }
2688 }
2689
2690 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2691    parameter is the PRAGMA token, allowing us to purge the entire pragma
2692    sequence.  */
2693
2694 static void
2695 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2696 {
2697   cp_token *token;
2698
2699   parser->lexer->in_pragma = false;
2700
2701   do
2702     token = cp_lexer_consume_token (parser->lexer);
2703   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2704
2705   /* Ensure that the pragma is not parsed again.  */
2706   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2707 }
2708
2709 /* Require pragma end of line, resyncing with it as necessary.  The
2710    arguments are as for cp_parser_skip_to_pragma_eol.  */
2711
2712 static void
2713 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2714 {
2715   parser->lexer->in_pragma = false;
2716   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2717     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2718 }
2719
2720 /* This is a simple wrapper around make_typename_type. When the id is
2721    an unresolved identifier node, we can provide a superior diagnostic
2722    using cp_parser_diagnose_invalid_type_name.  */
2723
2724 static tree
2725 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2726                               tree id, location_t id_location)
2727 {
2728   tree result;
2729   if (TREE_CODE (id) == IDENTIFIER_NODE)
2730     {
2731       result = make_typename_type (scope, id, typename_type,
2732                                    /*complain=*/tf_none);
2733       if (result == error_mark_node)
2734         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2735       return result;
2736     }
2737   return make_typename_type (scope, id, typename_type, tf_error);
2738 }
2739
2740 /* This is a wrapper around the
2741    make_{pointer,ptrmem,reference}_declarator functions that decides
2742    which one to call based on the CODE and CLASS_TYPE arguments. The
2743    CODE argument should be one of the values returned by
2744    cp_parser_ptr_operator. */
2745 static cp_declarator *
2746 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2747                                     cp_cv_quals cv_qualifiers,
2748                                     cp_declarator *target)
2749 {
2750   if (code == ERROR_MARK)
2751     return cp_error_declarator;
2752
2753   if (code == INDIRECT_REF)
2754     if (class_type == NULL_TREE)
2755       return make_pointer_declarator (cv_qualifiers, target);
2756     else
2757       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2758   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2759     return make_reference_declarator (cv_qualifiers, target, false);
2760   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2761     return make_reference_declarator (cv_qualifiers, target, true);
2762   gcc_unreachable ();
2763 }
2764
2765 /* Create a new C++ parser.  */
2766
2767 static cp_parser *
2768 cp_parser_new (void)
2769 {
2770   cp_parser *parser;
2771   cp_lexer *lexer;
2772   unsigned i;
2773
2774   /* cp_lexer_new_main is called before calling ggc_alloc because
2775      cp_lexer_new_main might load a PCH file.  */
2776   lexer = cp_lexer_new_main ();
2777
2778   /* Initialize the binops_by_token so that we can get the tree
2779      directly from the token.  */
2780   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2781     binops_by_token[binops[i].token_type] = binops[i];
2782
2783   parser = GGC_CNEW (cp_parser);
2784   parser->lexer = lexer;
2785   parser->context = cp_parser_context_new (NULL);
2786
2787   /* For now, we always accept GNU extensions.  */
2788   parser->allow_gnu_extensions_p = 1;
2789
2790   /* The `>' token is a greater-than operator, not the end of a
2791      template-id.  */
2792   parser->greater_than_is_operator_p = true;
2793
2794   parser->default_arg_ok_p = true;
2795
2796   /* We are not parsing a constant-expression.  */
2797   parser->integral_constant_expression_p = false;
2798   parser->allow_non_integral_constant_expression_p = false;
2799   parser->non_integral_constant_expression_p = false;
2800
2801   /* Local variable names are not forbidden.  */
2802   parser->local_variables_forbidden_p = false;
2803
2804   /* We are not processing an `extern "C"' declaration.  */
2805   parser->in_unbraced_linkage_specification_p = false;
2806
2807   /* We are not processing a declarator.  */
2808   parser->in_declarator_p = false;
2809
2810   /* We are not processing a template-argument-list.  */
2811   parser->in_template_argument_list_p = false;
2812
2813   /* We are not in an iteration statement.  */
2814   parser->in_statement = 0;
2815
2816   /* We are not in a switch statement.  */
2817   parser->in_switch_statement_p = false;
2818
2819   /* We are not parsing a type-id inside an expression.  */
2820   parser->in_type_id_in_expr_p = false;
2821
2822   /* Declarations aren't implicitly extern "C".  */
2823   parser->implicit_extern_c = false;
2824
2825   /* String literals should be translated to the execution character set.  */
2826   parser->translate_strings_p = true;
2827
2828   /* We are not parsing a function body.  */
2829   parser->in_function_body = false;
2830
2831   /* The unparsed function queue is empty.  */
2832   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2833
2834   /* There are no classes being defined.  */
2835   parser->num_classes_being_defined = 0;
2836
2837   /* No template parameters apply.  */
2838   parser->num_template_parameter_lists = 0;
2839
2840   return parser;
2841 }
2842
2843 /* Create a cp_lexer structure which will emit the tokens in CACHE
2844    and push it onto the parser's lexer stack.  This is used for delayed
2845    parsing of in-class method bodies and default arguments, and should
2846    not be confused with tentative parsing.  */
2847 static void
2848 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2849 {
2850   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2851   lexer->next = parser->lexer;
2852   parser->lexer = lexer;
2853
2854   /* Move the current source position to that of the first token in the
2855      new lexer.  */
2856   cp_lexer_set_source_position_from_token (lexer->next_token);
2857 }
2858
2859 /* Pop the top lexer off the parser stack.  This is never used for the
2860    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2861 static void
2862 cp_parser_pop_lexer (cp_parser *parser)
2863 {
2864   cp_lexer *lexer = parser->lexer;
2865   parser->lexer = lexer->next;
2866   cp_lexer_destroy (lexer);
2867
2868   /* Put the current source position back where it was before this
2869      lexer was pushed.  */
2870   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2871 }
2872
2873 /* Lexical conventions [gram.lex]  */
2874
2875 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2876    identifier.  */
2877
2878 static tree
2879 cp_parser_identifier (cp_parser* parser)
2880 {
2881   cp_token *token;
2882
2883   /* Look for the identifier.  */
2884   token = cp_parser_require (parser, CPP_NAME, "identifier");
2885   /* Return the value.  */
2886   return token ? token->u.value : error_mark_node;
2887 }
2888
2889 /* Parse a sequence of adjacent string constants.  Returns a
2890    TREE_STRING representing the combined, nul-terminated string
2891    constant.  If TRANSLATE is true, translate the string to the
2892    execution character set.  If WIDE_OK is true, a wide string is
2893    invalid here.
2894
2895    C++98 [lex.string] says that if a narrow string literal token is
2896    adjacent to a wide string literal token, the behavior is undefined.
2897    However, C99 6.4.5p4 says that this results in a wide string literal.
2898    We follow C99 here, for consistency with the C front end.
2899
2900    This code is largely lifted from lex_string() in c-lex.c.
2901
2902    FUTURE: ObjC++ will need to handle @-strings here.  */
2903 static tree
2904 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2905 {
2906   tree value;
2907   size_t count;
2908   struct obstack str_ob;
2909   cpp_string str, istr, *strs;
2910   cp_token *tok;
2911   enum cpp_ttype type;
2912
2913   tok = cp_lexer_peek_token (parser->lexer);
2914   if (!cp_parser_is_string_literal (tok))
2915     {
2916       cp_parser_error (parser, "expected string-literal");
2917       return error_mark_node;
2918     }
2919
2920   type = tok->type;
2921
2922   /* Try to avoid the overhead of creating and destroying an obstack
2923      for the common case of just one string.  */
2924   if (!cp_parser_is_string_literal
2925       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2926     {
2927       cp_lexer_consume_token (parser->lexer);
2928
2929       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2930       str.len = TREE_STRING_LENGTH (tok->u.value);
2931       count = 1;
2932
2933       strs = &str;
2934     }
2935   else
2936     {
2937       gcc_obstack_init (&str_ob);
2938       count = 0;
2939
2940       do
2941         {
2942           cp_lexer_consume_token (parser->lexer);
2943           count++;
2944           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2945           str.len = TREE_STRING_LENGTH (tok->u.value);
2946
2947           if (type != tok->type)
2948             {
2949               if (type == CPP_STRING)
2950                 type = tok->type;
2951               else if (tok->type != CPP_STRING)
2952                 error_at (tok->location,
2953                           "unsupported non-standard concatenation "
2954                           "of string literals");
2955             }
2956
2957           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2958
2959           tok = cp_lexer_peek_token (parser->lexer);
2960         }
2961       while (cp_parser_is_string_literal (tok));
2962
2963       strs = (cpp_string *) obstack_finish (&str_ob);
2964     }
2965
2966   if (type != CPP_STRING && !wide_ok)
2967     {
2968       cp_parser_error (parser, "a wide string is invalid in this context");
2969       type = CPP_STRING;
2970     }
2971
2972   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2973       (parse_in, strs, count, &istr, type))
2974     {
2975       value = build_string (istr.len, (const char *)istr.text);
2976       free (CONST_CAST (unsigned char *, istr.text));
2977
2978       switch (type)
2979         {
2980         default:
2981         case CPP_STRING:
2982           TREE_TYPE (value) = char_array_type_node;
2983           break;
2984         case CPP_STRING16:
2985           TREE_TYPE (value) = char16_array_type_node;
2986           break;
2987         case CPP_STRING32:
2988           TREE_TYPE (value) = char32_array_type_node;
2989           break;
2990         case CPP_WSTRING:
2991           TREE_TYPE (value) = wchar_array_type_node;
2992           break;
2993         }
2994
2995       value = fix_string_type (value);
2996     }
2997   else
2998     /* cpp_interpret_string has issued an error.  */
2999     value = error_mark_node;
3000
3001   if (count > 1)
3002     obstack_free (&str_ob, 0);
3003
3004   return value;
3005 }
3006
3007
3008 /* Basic concepts [gram.basic]  */
3009
3010 /* Parse a translation-unit.
3011
3012    translation-unit:
3013      declaration-seq [opt]
3014
3015    Returns TRUE if all went well.  */
3016
3017 static bool
3018 cp_parser_translation_unit (cp_parser* parser)
3019 {
3020   /* The address of the first non-permanent object on the declarator
3021      obstack.  */
3022   static void *declarator_obstack_base;
3023
3024   bool success;
3025
3026   /* Create the declarator obstack, if necessary.  */
3027   if (!cp_error_declarator)
3028     {
3029       gcc_obstack_init (&declarator_obstack);
3030       /* Create the error declarator.  */
3031       cp_error_declarator = make_declarator (cdk_error);
3032       /* Create the empty parameter list.  */
3033       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3034       /* Remember where the base of the declarator obstack lies.  */
3035       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3036     }
3037
3038   cp_parser_declaration_seq_opt (parser);
3039
3040   /* If there are no tokens left then all went well.  */
3041   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3042     {
3043       /* Get rid of the token array; we don't need it any more.  */
3044       cp_lexer_destroy (parser->lexer);
3045       parser->lexer = NULL;
3046
3047       /* This file might have been a context that's implicitly extern
3048          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3049       if (parser->implicit_extern_c)
3050         {
3051           pop_lang_context ();
3052           parser->implicit_extern_c = false;
3053         }
3054
3055       /* Finish up.  */
3056       finish_translation_unit ();
3057
3058       success = true;
3059     }
3060   else
3061     {
3062       cp_parser_error (parser, "expected declaration");
3063       success = false;
3064     }
3065
3066   /* Make sure the declarator obstack was fully cleaned up.  */
3067   gcc_assert (obstack_next_free (&declarator_obstack)
3068               == declarator_obstack_base);
3069
3070   /* All went well.  */
3071   return success;
3072 }
3073
3074 /* Expressions [gram.expr] */
3075
3076 /* Parse a primary-expression.
3077
3078    primary-expression:
3079      literal
3080      this
3081      ( expression )
3082      id-expression
3083
3084    GNU Extensions:
3085
3086    primary-expression:
3087      ( compound-statement )
3088      __builtin_va_arg ( assignment-expression , type-id )
3089      __builtin_offsetof ( type-id , offsetof-expression )
3090
3091    C++ Extensions:
3092      __has_nothrow_assign ( type-id )   
3093      __has_nothrow_constructor ( type-id )
3094      __has_nothrow_copy ( type-id )
3095      __has_trivial_assign ( type-id )   
3096      __has_trivial_constructor ( type-id )
3097      __has_trivial_copy ( type-id )
3098      __has_trivial_destructor ( type-id )
3099      __has_virtual_destructor ( type-id )     
3100      __is_abstract ( type-id )
3101      __is_base_of ( type-id , type-id )
3102      __is_class ( type-id )
3103      __is_convertible_to ( type-id , type-id )     
3104      __is_empty ( type-id )
3105      __is_enum ( type-id )
3106      __is_pod ( type-id )
3107      __is_polymorphic ( type-id )
3108      __is_union ( type-id )
3109
3110    Objective-C++ Extension:
3111
3112    primary-expression:
3113      objc-expression
3114
3115    literal:
3116      __null
3117
3118    ADDRESS_P is true iff this expression was immediately preceded by
3119    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3120    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3121    true iff this expression is a template argument.
3122
3123    Returns a representation of the expression.  Upon return, *IDK
3124    indicates what kind of id-expression (if any) was present.  */
3125
3126 static tree
3127 cp_parser_primary_expression (cp_parser *parser,
3128                               bool address_p,
3129                               bool cast_p,
3130                               bool template_arg_p,
3131                               cp_id_kind *idk)
3132 {
3133   cp_token *token = NULL;
3134
3135   /* Assume the primary expression is not an id-expression.  */
3136   *idk = CP_ID_KIND_NONE;
3137
3138   /* Peek at the next token.  */
3139   token = cp_lexer_peek_token (parser->lexer);
3140   switch (token->type)
3141     {
3142       /* literal:
3143            integer-literal
3144            character-literal
3145            floating-literal
3146            string-literal
3147            boolean-literal  */
3148     case CPP_CHAR:
3149     case CPP_CHAR16:
3150     case CPP_CHAR32:
3151     case CPP_WCHAR:
3152     case CPP_NUMBER:
3153       token = cp_lexer_consume_token (parser->lexer);
3154       if (TREE_CODE (token->u.value) == FIXED_CST)
3155         {
3156           error_at (token->location,
3157                     "fixed-point types not supported in C++");
3158           return error_mark_node;
3159         }
3160       /* Floating-point literals are only allowed in an integral
3161          constant expression if they are cast to an integral or
3162          enumeration type.  */
3163       if (TREE_CODE (token->u.value) == REAL_CST
3164           && parser->integral_constant_expression_p
3165           && pedantic)
3166         {
3167           /* CAST_P will be set even in invalid code like "int(2.7 +
3168              ...)".   Therefore, we have to check that the next token
3169              is sure to end the cast.  */
3170           if (cast_p)
3171             {
3172               cp_token *next_token;
3173
3174               next_token = cp_lexer_peek_token (parser->lexer);
3175               if (/* The comma at the end of an
3176                      enumerator-definition.  */
3177                   next_token->type != CPP_COMMA
3178                   /* The curly brace at the end of an enum-specifier.  */
3179                   && next_token->type != CPP_CLOSE_BRACE
3180                   /* The end of a statement.  */
3181                   && next_token->type != CPP_SEMICOLON
3182                   /* The end of the cast-expression.  */
3183                   && next_token->type != CPP_CLOSE_PAREN
3184                   /* The end of an array bound.  */
3185                   && next_token->type != CPP_CLOSE_SQUARE
3186                   /* The closing ">" in a template-argument-list.  */
3187                   && (next_token->type != CPP_GREATER
3188                       || parser->greater_than_is_operator_p)
3189                   /* C++0x only: A ">>" treated like two ">" tokens,
3190                      in a template-argument-list.  */
3191                   && (next_token->type != CPP_RSHIFT
3192                       || (cxx_dialect == cxx98)
3193                       || parser->greater_than_is_operator_p))
3194                 cast_p = false;
3195             }
3196
3197           /* If we are within a cast, then the constraint that the
3198              cast is to an integral or enumeration type will be
3199              checked at that point.  If we are not within a cast, then
3200              this code is invalid.  */
3201           if (!cast_p)
3202             cp_parser_non_integral_constant_expression
3203               (parser, "floating-point literal");
3204         }
3205       return token->u.value;
3206
3207     case CPP_STRING:
3208     case CPP_STRING16:
3209     case CPP_STRING32:
3210     case CPP_WSTRING:
3211       /* ??? Should wide strings be allowed when parser->translate_strings_p
3212          is false (i.e. in attributes)?  If not, we can kill the third
3213          argument to cp_parser_string_literal.  */
3214       return cp_parser_string_literal (parser,
3215                                        parser->translate_strings_p,
3216                                        true);
3217
3218     case CPP_OPEN_PAREN:
3219       {
3220         tree expr;
3221         bool saved_greater_than_is_operator_p;
3222
3223         /* Consume the `('.  */
3224         cp_lexer_consume_token (parser->lexer);
3225         /* Within a parenthesized expression, a `>' token is always
3226            the greater-than operator.  */
3227         saved_greater_than_is_operator_p
3228           = parser->greater_than_is_operator_p;
3229         parser->greater_than_is_operator_p = true;
3230         /* If we see `( { ' then we are looking at the beginning of
3231            a GNU statement-expression.  */
3232         if (cp_parser_allow_gnu_extensions_p (parser)
3233             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3234           {
3235             /* Statement-expressions are not allowed by the standard.  */
3236             pedwarn (token->location, OPT_pedantic, 
3237                      "ISO C++ forbids braced-groups within expressions");
3238
3239             /* And they're not allowed outside of a function-body; you
3240                cannot, for example, write:
3241
3242                  int i = ({ int j = 3; j + 1; });
3243
3244                at class or namespace scope.  */
3245             if (!parser->in_function_body
3246                 || parser->in_template_argument_list_p)
3247               {
3248                 error_at (token->location,
3249                           "statement-expressions are not allowed outside "
3250                           "functions nor in template-argument lists");
3251                 cp_parser_skip_to_end_of_block_or_statement (parser);
3252                 expr = error_mark_node;
3253               }
3254             else
3255               {
3256                 /* Start the statement-expression.  */
3257                 expr = begin_stmt_expr ();
3258                 /* Parse the compound-statement.  */
3259                 cp_parser_compound_statement (parser, expr, false);
3260                 /* Finish up.  */
3261                 expr = finish_stmt_expr (expr, false);
3262               }
3263           }
3264         else
3265           {
3266             /* Parse the parenthesized expression.  */
3267             expr = cp_parser_expression (parser, cast_p, idk);
3268             /* Let the front end know that this expression was
3269                enclosed in parentheses. This matters in case, for
3270                example, the expression is of the form `A::B', since
3271                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3272                not.  */
3273             finish_parenthesized_expr (expr);
3274           }
3275         /* The `>' token might be the end of a template-id or
3276            template-parameter-list now.  */
3277         parser->greater_than_is_operator_p
3278           = saved_greater_than_is_operator_p;
3279         /* Consume the `)'.  */
3280         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3281           cp_parser_skip_to_end_of_statement (parser);
3282
3283         return expr;
3284       }
3285
3286     case CPP_KEYWORD:
3287       switch (token->keyword)
3288         {
3289           /* These two are the boolean literals.  */
3290         case RID_TRUE:
3291           cp_lexer_consume_token (parser->lexer);
3292           return boolean_true_node;
3293         case RID_FALSE:
3294           cp_lexer_consume_token (parser->lexer);
3295           return boolean_false_node;
3296
3297           /* The `__null' literal.  */
3298         case RID_NULL:
3299           cp_lexer_consume_token (parser->lexer);
3300           return null_node;
3301
3302           /* Recognize the `this' keyword.  */
3303         case RID_THIS:
3304           cp_lexer_consume_token (parser->lexer);
3305           if (parser->local_variables_forbidden_p)
3306             {
3307               error_at (token->location,
3308                         "%<this%> may not be used in this context");
3309               return error_mark_node;
3310             }
3311           /* Pointers cannot appear in constant-expressions.  */
3312           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3313             return error_mark_node;
3314           return finish_this_expr ();
3315
3316           /* The `operator' keyword can be the beginning of an
3317              id-expression.  */
3318         case RID_OPERATOR:
3319           goto id_expression;
3320
3321         case RID_FUNCTION_NAME:
3322         case RID_PRETTY_FUNCTION_NAME:
3323         case RID_C99_FUNCTION_NAME:
3324           {
3325             const char *name;
3326
3327             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3328                __func__ are the names of variables -- but they are
3329                treated specially.  Therefore, they are handled here,
3330                rather than relying on the generic id-expression logic
3331                below.  Grammatically, these names are id-expressions.
3332
3333                Consume the token.  */
3334             token = cp_lexer_consume_token (parser->lexer);
3335
3336             switch (token->keyword)
3337               {
3338               case RID_FUNCTION_NAME:
3339                 name = "%<__FUNCTION__%>";
3340                 break;
3341               case RID_PRETTY_FUNCTION_NAME:
3342                 name = "%<__PRETTY_FUNCTION__%>";
3343                 break;
3344               case RID_C99_FUNCTION_NAME:
3345                 name = "%<__func__%>";
3346                 break;
3347               default:
3348                 gcc_unreachable ();
3349               }
3350
3351             if (cp_parser_non_integral_constant_expression (parser, name))
3352               return error_mark_node;
3353
3354             /* Look up the name.  */
3355             return finish_fname (token->u.value);
3356           }
3357
3358         case RID_VA_ARG:
3359           {
3360             tree expression;
3361             tree type;
3362
3363             /* The `__builtin_va_arg' construct is used to handle
3364                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3365             cp_lexer_consume_token (parser->lexer);
3366             /* Look for the opening `('.  */
3367             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3368             /* Now, parse the assignment-expression.  */
3369             expression = cp_parser_assignment_expression (parser,
3370                                                           /*cast_p=*/false, NULL);
3371             /* Look for the `,'.  */
3372             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3373             /* Parse the type-id.  */
3374             type = cp_parser_type_id (parser);
3375             /* Look for the closing `)'.  */
3376             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3377             /* Using `va_arg' in a constant-expression is not
3378                allowed.  */
3379             if (cp_parser_non_integral_constant_expression (parser,
3380                                                             "%<va_arg%>"))
3381               return error_mark_node;
3382             return build_x_va_arg (expression, type);
3383           }
3384
3385         case RID_OFFSETOF:
3386           return cp_parser_builtin_offsetof (parser);
3387
3388         case RID_HAS_NOTHROW_ASSIGN:
3389         case RID_HAS_NOTHROW_CONSTRUCTOR:
3390         case RID_HAS_NOTHROW_COPY:        
3391         case RID_HAS_TRIVIAL_ASSIGN:
3392         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3393         case RID_HAS_TRIVIAL_COPY:        
3394         case RID_HAS_TRIVIAL_DESTRUCTOR:
3395         case RID_HAS_VIRTUAL_DESTRUCTOR:
3396         case RID_IS_ABSTRACT:
3397         case RID_IS_BASE_OF:
3398         case RID_IS_CLASS:
3399         case RID_IS_CONVERTIBLE_TO:
3400         case RID_IS_EMPTY:
3401         case RID_IS_ENUM:
3402         case RID_IS_POD:
3403         case RID_IS_POLYMORPHIC:
3404         case RID_IS_STD_LAYOUT:
3405         case RID_IS_TRIVIAL:
3406         case RID_IS_UNION:
3407           return cp_parser_trait_expr (parser, token->keyword);
3408
3409         /* Objective-C++ expressions.  */
3410         case RID_AT_ENCODE:
3411         case RID_AT_PROTOCOL:
3412         case RID_AT_SELECTOR:
3413           return cp_parser_objc_expression (parser);
3414
3415         default:
3416           cp_parser_error (parser, "expected primary-expression");
3417           return error_mark_node;
3418         }
3419
3420       /* An id-expression can start with either an identifier, a
3421          `::' as the beginning of a qualified-id, or the "operator"
3422          keyword.  */
3423     case CPP_NAME:
3424     case CPP_SCOPE:
3425     case CPP_TEMPLATE_ID:
3426     case CPP_NESTED_NAME_SPECIFIER:
3427       {
3428         tree id_expression;
3429         tree decl;
3430         const char *error_msg;
3431         bool template_p;
3432         bool done;
3433         cp_token *id_expr_token;
3434
3435       id_expression:
3436         /* Parse the id-expression.  */
3437         id_expression
3438           = cp_parser_id_expression (parser,
3439                                      /*template_keyword_p=*/false,
3440                                      /*check_dependency_p=*/true,
3441                                      &template_p,
3442                                      /*declarator_p=*/false,
3443                                      /*optional_p=*/false);
3444         if (id_expression == error_mark_node)
3445           return error_mark_node;
3446         id_expr_token = token;
3447         token = cp_lexer_peek_token (parser->lexer);
3448         done = (token->type != CPP_OPEN_SQUARE
3449                 && token->type != CPP_OPEN_PAREN
3450                 && token->type != CPP_DOT
3451                 && token->type != CPP_DEREF
3452                 && token->type != CPP_PLUS_PLUS
3453                 && token->type != CPP_MINUS_MINUS);
3454         /* If we have a template-id, then no further lookup is
3455            required.  If the template-id was for a template-class, we
3456            will sometimes have a TYPE_DECL at this point.  */
3457         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3458                  || TREE_CODE (id_expression) == TYPE_DECL)
3459           decl = id_expression;
3460         /* Look up the name.  */
3461         else
3462           {
3463             tree ambiguous_decls;
3464
3465             decl = cp_parser_lookup_name (parser, id_expression,
3466                                           none_type,
3467                                           template_p,
3468                                           /*is_namespace=*/false,
3469                                           /*check_dependency=*/true,
3470                                           &ambiguous_decls,
3471                                           id_expr_token->location);
3472             /* If the lookup was ambiguous, an error will already have
3473                been issued.  */
3474             if (ambiguous_decls)
3475               return error_mark_node;
3476
3477             /* In Objective-C++, an instance variable (ivar) may be preferred
3478                to whatever cp_parser_lookup_name() found.  */
3479             decl = objc_lookup_ivar (decl, id_expression);
3480
3481             /* If name lookup gives us a SCOPE_REF, then the
3482                qualifying scope was dependent.  */
3483             if (TREE_CODE (decl) == SCOPE_REF)
3484               {
3485                 /* At this point, we do not know if DECL is a valid
3486                    integral constant expression.  We assume that it is
3487                    in fact such an expression, so that code like:
3488
3489                       template <int N> struct A {
3490                         int a[B<N>::i];
3491                       };
3492                      
3493                    is accepted.  At template-instantiation time, we
3494                    will check that B<N>::i is actually a constant.  */
3495                 return decl;
3496               }
3497             /* Check to see if DECL is a local variable in a context
3498                where that is forbidden.  */
3499             if (parser->local_variables_forbidden_p
3500                 && local_variable_p (decl))
3501               {
3502                 /* It might be that we only found DECL because we are
3503                    trying to be generous with pre-ISO scoping rules.
3504                    For example, consider:
3505
3506                      int i;
3507                      void g() {
3508                        for (int i = 0; i < 10; ++i) {}
3509                        extern void f(int j = i);
3510                      }
3511
3512                    Here, name look up will originally find the out
3513                    of scope `i'.  We need to issue a warning message,
3514                    but then use the global `i'.  */
3515                 decl = check_for_out_of_scope_variable (decl);
3516                 if (local_variable_p (decl))
3517                   {
3518                     error_at (id_expr_token->location,
3519                               "local variable %qD may not appear in this context",
3520                               decl);
3521                     return error_mark_node;
3522                   }
3523               }
3524           }
3525
3526         decl = (finish_id_expression
3527                 (id_expression, decl, parser->scope,
3528                  idk,
3529                  parser->integral_constant_expression_p,
3530                  parser->allow_non_integral_constant_expression_p,
3531                  &parser->non_integral_constant_expression_p,
3532                  template_p, done, address_p,
3533                  template_arg_p,
3534                  &error_msg,
3535                  id_expr_token->location));
3536         if (error_msg)
3537           cp_parser_error (parser, error_msg);
3538         return decl;
3539       }
3540
3541       /* Anything else is an error.  */
3542     default:
3543       /* ...unless we have an Objective-C++ message or string literal,
3544          that is.  */
3545       if (c_dialect_objc ()
3546           && (token->type == CPP_OPEN_SQUARE
3547               || token->type == CPP_OBJC_STRING))
3548         return cp_parser_objc_expression (parser);
3549
3550       cp_parser_error (parser, "expected primary-expression");
3551       return error_mark_node;
3552     }
3553 }
3554
3555 /* Parse an id-expression.
3556
3557    id-expression:
3558      unqualified-id
3559      qualified-id
3560
3561    qualified-id:
3562      :: [opt] nested-name-specifier template [opt] unqualified-id
3563      :: identifier
3564      :: operator-function-id
3565      :: template-id
3566
3567    Return a representation of the unqualified portion of the
3568    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3569    a `::' or nested-name-specifier.
3570
3571    Often, if the id-expression was a qualified-id, the caller will
3572    want to make a SCOPE_REF to represent the qualified-id.  This
3573    function does not do this in order to avoid wastefully creating
3574    SCOPE_REFs when they are not required.
3575
3576    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3577    `template' keyword.
3578
3579    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3580    uninstantiated templates.
3581
3582    If *TEMPLATE_P is non-NULL, it is set to true iff the
3583    `template' keyword is used to explicitly indicate that the entity
3584    named is a template.
3585
3586    If DECLARATOR_P is true, the id-expression is appearing as part of
3587    a declarator, rather than as part of an expression.  */
3588
3589 static tree
3590 cp_parser_id_expression (cp_parser *parser,
3591                          bool template_keyword_p,
3592                          bool check_dependency_p,
3593                          bool *template_p,
3594                          bool declarator_p,
3595                          bool optional_p)
3596 {
3597   bool global_scope_p;
3598   bool nested_name_specifier_p;
3599
3600   /* Assume the `template' keyword was not used.  */
3601   if (template_p)
3602     *template_p = template_keyword_p;
3603
3604   /* Look for the optional `::' operator.  */
3605   global_scope_p
3606     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3607        != NULL_TREE);
3608   /* Look for the optional nested-name-specifier.  */
3609   nested_name_specifier_p
3610     = (cp_parser_nested_name_specifier_opt (parser,
3611                                             /*typename_keyword_p=*/false,
3612                                             check_dependency_p,
3613                                             /*type_p=*/false,
3614                                             declarator_p)
3615        != NULL_TREE);
3616   /* If there is a nested-name-specifier, then we are looking at
3617      the first qualified-id production.  */
3618   if (nested_name_specifier_p)
3619     {
3620       tree saved_scope;
3621       tree saved_object_scope;
3622       tree saved_qualifying_scope;
3623       tree unqualified_id;
3624       bool is_template;
3625
3626       /* See if the next token is the `template' keyword.  */
3627       if (!template_p)
3628         template_p = &is_template;
3629       *template_p = cp_parser_optional_template_keyword (parser);
3630       /* Name lookup we do during the processing of the
3631          unqualified-id might obliterate SCOPE.  */
3632       saved_scope = parser->scope;
3633       saved_object_scope = parser->object_scope;
3634       saved_qualifying_scope = parser->qualifying_scope;
3635       /* Process the final unqualified-id.  */
3636       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3637                                                  check_dependency_p,
3638                                                  declarator_p,
3639                                                  /*optional_p=*/false);
3640       /* Restore the SAVED_SCOPE for our caller.  */
3641       parser->scope = saved_scope;
3642       parser->object_scope = saved_object_scope;
3643       parser->qualifying_scope = saved_qualifying_scope;
3644
3645       return unqualified_id;
3646     }
3647   /* Otherwise, if we are in global scope, then we are looking at one
3648      of the other qualified-id productions.  */
3649   else if (global_scope_p)
3650     {
3651       cp_token *token;
3652       tree id;
3653
3654       /* Peek at the next token.  */
3655       token = cp_lexer_peek_token (parser->lexer);
3656
3657       /* If it's an identifier, and the next token is not a "<", then
3658          we can avoid the template-id case.  This is an optimization
3659          for this common case.  */
3660       if (token->type == CPP_NAME
3661           && !cp_parser_nth_token_starts_template_argument_list_p
3662                (parser, 2))
3663         return cp_parser_identifier (parser);
3664
3665       cp_parser_parse_tentatively (parser);
3666       /* Try a template-id.  */
3667       id = cp_parser_template_id (parser,
3668                                   /*template_keyword_p=*/false,
3669                                   /*check_dependency_p=*/true,
3670                                   declarator_p);
3671       /* If that worked, we're done.  */
3672       if (cp_parser_parse_definitely (parser))
3673         return id;
3674
3675       /* Peek at the next token.  (Changes in the token buffer may
3676          have invalidated the pointer obtained above.)  */
3677       token = cp_lexer_peek_token (parser->lexer);
3678
3679       switch (token->type)
3680         {
3681         case CPP_NAME:
3682           return cp_parser_identifier (parser);
3683
3684         case CPP_KEYWORD:
3685           if (token->keyword == RID_OPERATOR)
3686             return cp_parser_operator_function_id (parser);
3687           /* Fall through.  */
3688
3689         default:
3690           cp_parser_error (parser, "expected id-expression");
3691           return error_mark_node;
3692         }
3693     }
3694   else
3695     return cp_parser_unqualified_id (parser, template_keyword_p,
3696                                      /*check_dependency_p=*/true,
3697                                      declarator_p,
3698                                      optional_p);
3699 }
3700
3701 /* Parse an unqualified-id.
3702
3703    unqualified-id:
3704      identifier
3705      operator-function-id
3706      conversion-function-id
3707      ~ class-name
3708      template-id
3709
3710    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3711    keyword, in a construct like `A::template ...'.
3712
3713    Returns a representation of unqualified-id.  For the `identifier'
3714    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3715    production a BIT_NOT_EXPR is returned; the operand of the
3716    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3717    other productions, see the documentation accompanying the
3718    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3719    names are looked up in uninstantiated templates.  If DECLARATOR_P
3720    is true, the unqualified-id is appearing as part of a declarator,
3721    rather than as part of an expression.  */
3722
3723 static tree
3724 cp_parser_unqualified_id (cp_parser* parser,
3725                           bool template_keyword_p,
3726                           bool check_dependency_p,
3727                           bool declarator_p,
3728                           bool optional_p)
3729 {
3730   cp_token *token;
3731
3732   /* Peek at the next token.  */
3733   token = cp_lexer_peek_token (parser->lexer);
3734
3735   switch (token->type)
3736     {
3737     case CPP_NAME:
3738       {
3739         tree id;
3740
3741         /* We don't know yet whether or not this will be a
3742            template-id.  */
3743         cp_parser_parse_tentatively (parser);
3744         /* Try a template-id.  */
3745         id = cp_parser_template_id (parser, template_keyword_p,
3746                                     check_dependency_p,
3747                                     declarator_p);
3748         /* If it worked, we're done.  */
3749         if (cp_parser_parse_definitely (parser))
3750           return id;
3751         /* Otherwise, it's an ordinary identifier.  */
3752         return cp_parser_identifier (parser);
3753       }
3754
3755     case CPP_TEMPLATE_ID:
3756       return cp_parser_template_id (parser, template_keyword_p,
3757                                     check_dependency_p,
3758                                     declarator_p);
3759
3760     case CPP_COMPL:
3761       {
3762         tree type_decl;
3763         tree qualifying_scope;
3764         tree object_scope;
3765         tree scope;
3766         bool done;
3767
3768         /* Consume the `~' token.  */
3769         cp_lexer_consume_token (parser->lexer);
3770         /* Parse the class-name.  The standard, as written, seems to
3771            say that:
3772
3773              template <typename T> struct S { ~S (); };
3774              template <typename T> S<T>::~S() {}
3775
3776            is invalid, since `~' must be followed by a class-name, but
3777            `S<T>' is dependent, and so not known to be a class.
3778            That's not right; we need to look in uninstantiated
3779            templates.  A further complication arises from:
3780
3781              template <typename T> void f(T t) {
3782                t.T::~T();
3783              }
3784
3785            Here, it is not possible to look up `T' in the scope of `T'
3786            itself.  We must look in both the current scope, and the
3787            scope of the containing complete expression.
3788
3789            Yet another issue is:
3790
3791              struct S {
3792                int S;
3793                ~S();
3794              };
3795
3796              S::~S() {}
3797
3798            The standard does not seem to say that the `S' in `~S'
3799            should refer to the type `S' and not the data member
3800            `S::S'.  */
3801
3802         /* DR 244 says that we look up the name after the "~" in the
3803            same scope as we looked up the qualifying name.  That idea
3804            isn't fully worked out; it's more complicated than that.  */
3805         scope = parser->scope;
3806         object_scope = parser->object_scope;
3807         qualifying_scope = parser->qualifying_scope;
3808
3809         /* Check for invalid scopes.  */
3810         if (scope == error_mark_node)
3811           {
3812             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813               cp_lexer_consume_token (parser->lexer);
3814             return error_mark_node;
3815           }
3816         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3817           {
3818             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3819               error_at (token->location,
3820                         "scope %qT before %<~%> is not a class-name",
3821                         scope);
3822             cp_parser_simulate_error (parser);
3823             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3824               cp_lexer_consume_token (parser->lexer);
3825             return error_mark_node;
3826           }
3827         gcc_assert (!scope || TYPE_P (scope));
3828
3829         /* If the name is of the form "X::~X" it's OK.  */
3830         token = cp_lexer_peek_token (parser->lexer);
3831         if (scope
3832             && token->type == CPP_NAME
3833             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3834                 == CPP_OPEN_PAREN)
3835             && constructor_name_p (token->u.value, scope))
3836           {
3837             cp_lexer_consume_token (parser->lexer);
3838             return build_nt (BIT_NOT_EXPR, scope);
3839           }
3840
3841         /* If there was an explicit qualification (S::~T), first look
3842            in the scope given by the qualification (i.e., S).  */
3843         done = false;
3844         type_decl = NULL_TREE;
3845         if (scope)
3846           {
3847             cp_parser_parse_tentatively (parser);
3848             type_decl = cp_parser_class_name (parser,
3849                                               /*typename_keyword_p=*/false,
3850                                               /*template_keyword_p=*/false,
3851                                               none_type,
3852                                               /*check_dependency=*/false,
3853                                               /*class_head_p=*/false,
3854                                               declarator_p);
3855             if (cp_parser_parse_definitely (parser))
3856               done = true;
3857           }
3858         /* In "N::S::~S", look in "N" as well.  */
3859         if (!done && scope && qualifying_scope)
3860           {
3861             cp_parser_parse_tentatively (parser);
3862             parser->scope = qualifying_scope;
3863             parser->object_scope = NULL_TREE;
3864             parser->qualifying_scope = NULL_TREE;
3865             type_decl
3866               = cp_parser_class_name (parser,
3867                                       /*typename_keyword_p=*/false,
3868                                       /*template_keyword_p=*/false,
3869                                       none_type,
3870                                       /*check_dependency=*/false,
3871                                       /*class_head_p=*/false,
3872                                       declarator_p);
3873             if (cp_parser_parse_definitely (parser))
3874               done = true;
3875           }
3876         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3877         else if (!done && object_scope)
3878           {
3879             cp_parser_parse_tentatively (parser);
3880             parser->scope = object_scope;
3881             parser->object_scope = NULL_TREE;
3882             parser->qualifying_scope = NULL_TREE;
3883             type_decl
3884               = cp_parser_class_name (parser,
3885                                       /*typename_keyword_p=*/false,
3886                                       /*template_keyword_p=*/false,
3887                                       none_type,
3888                                       /*check_dependency=*/false,
3889                                       /*class_head_p=*/false,
3890                                       declarator_p);
3891             if (cp_parser_parse_definitely (parser))
3892               done = true;
3893           }
3894         /* Look in the surrounding context.  */
3895         if (!done)
3896           {
3897             parser->scope = NULL_TREE;
3898             parser->object_scope = NULL_TREE;
3899             parser->qualifying_scope = NULL_TREE;
3900             if (processing_template_decl)
3901               cp_parser_parse_tentatively (parser);
3902             type_decl
3903               = cp_parser_class_name (parser,
3904                                       /*typename_keyword_p=*/false,
3905                                       /*template_keyword_p=*/false,
3906                                       none_type,
3907                                       /*check_dependency=*/false,
3908                                       /*class_head_p=*/false,
3909                                       declarator_p);
3910             if (processing_template_decl
3911                 && ! cp_parser_parse_definitely (parser))
3912               {
3913                 /* We couldn't find a type with this name, so just accept
3914                    it and check for a match at instantiation time.  */
3915                 type_decl = cp_parser_identifier (parser);
3916                 if (type_decl != error_mark_node)
3917                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3918                 return type_decl;
3919               }
3920           }
3921         /* If an error occurred, assume that the name of the
3922            destructor is the same as the name of the qualifying
3923            class.  That allows us to keep parsing after running
3924            into ill-formed destructor names.  */
3925         if (type_decl == error_mark_node && scope)
3926           return build_nt (BIT_NOT_EXPR, scope);
3927         else if (type_decl == error_mark_node)
3928           return error_mark_node;
3929
3930         /* Check that destructor name and scope match.  */
3931         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3932           {
3933             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3934               error_at (token->location,
3935                         "declaration of %<~%T%> as member of %qT",
3936                         type_decl, scope);
3937             cp_parser_simulate_error (parser);
3938             return error_mark_node;
3939           }
3940
3941         /* [class.dtor]
3942
3943            A typedef-name that names a class shall not be used as the
3944            identifier in the declarator for a destructor declaration.  */
3945         if (declarator_p
3946             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3947             && !DECL_SELF_REFERENCE_P (type_decl)
3948             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3949           error_at (token->location,
3950                     "typedef-name %qD used as destructor declarator",
3951                     type_decl);
3952
3953         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3954       }
3955
3956     case CPP_KEYWORD:
3957       if (token->keyword == RID_OPERATOR)
3958         {
3959           tree id;
3960
3961           /* This could be a template-id, so we try that first.  */
3962           cp_parser_parse_tentatively (parser);
3963           /* Try a template-id.  */
3964           id = cp_parser_template_id (parser, template_keyword_p,
3965                                       /*check_dependency_p=*/true,
3966                                       declarator_p);
3967           /* If that worked, we're done.  */
3968           if (cp_parser_parse_definitely (parser))
3969             return id;
3970           /* We still don't know whether we're looking at an
3971              operator-function-id or a conversion-function-id.  */
3972           cp_parser_parse_tentatively (parser);
3973           /* Try an operator-function-id.  */
3974           id = cp_parser_operator_function_id (parser);
3975           /* If that didn't work, try a conversion-function-id.  */
3976           if (!cp_parser_parse_definitely (parser))
3977             id = cp_parser_conversion_function_id (parser);
3978
3979           return id;
3980         }
3981       /* Fall through.  */
3982
3983     default:
3984       if (optional_p)
3985         return NULL_TREE;
3986       cp_parser_error (parser, "expected unqualified-id");
3987       return error_mark_node;
3988     }
3989 }
3990
3991 /* Parse an (optional) nested-name-specifier.
3992
3993    nested-name-specifier: [C++98]
3994      class-or-namespace-name :: nested-name-specifier [opt]
3995      class-or-namespace-name :: template nested-name-specifier [opt]
3996
3997    nested-name-specifier: [C++0x]
3998      type-name ::
3999      namespace-name ::
4000      nested-name-specifier identifier ::
4001      nested-name-specifier template [opt] simple-template-id ::
4002
4003    PARSER->SCOPE should be set appropriately before this function is
4004    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4005    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4006    in name lookups.
4007
4008    Sets PARSER->SCOPE to the class (TYPE) or namespace
4009    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4010    it unchanged if there is no nested-name-specifier.  Returns the new
4011    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4012
4013    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4014    part of a declaration and/or decl-specifier.  */
4015
4016 static tree
4017 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4018                                      bool typename_keyword_p,
4019                                      bool check_dependency_p,
4020                                      bool type_p,
4021                                      bool is_declaration)
4022 {
4023   bool success = false;
4024   cp_token_position start = 0;
4025   cp_token *token;
4026
4027   /* Remember where the nested-name-specifier starts.  */
4028   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4029     {
4030       start = cp_lexer_token_position (parser->lexer, false);
4031       push_deferring_access_checks (dk_deferred);
4032     }
4033
4034   while (true)
4035     {
4036       tree new_scope;
4037       tree old_scope;
4038       tree saved_qualifying_scope;
4039       bool template_keyword_p;
4040
4041       /* Spot cases that cannot be the beginning of a
4042          nested-name-specifier.  */
4043       token = cp_lexer_peek_token (parser->lexer);
4044
4045       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4046          the already parsed nested-name-specifier.  */
4047       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4048         {
4049           /* Grab the nested-name-specifier and continue the loop.  */
4050           cp_parser_pre_parsed_nested_name_specifier (parser);
4051           /* If we originally encountered this nested-name-specifier
4052              with IS_DECLARATION set to false, we will not have
4053              resolved TYPENAME_TYPEs, so we must do so here.  */
4054           if (is_declaration
4055               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4056             {
4057               new_scope = resolve_typename_type (parser->scope,
4058                                                  /*only_current_p=*/false);
4059               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4060                 parser->scope = new_scope;
4061             }
4062           success = true;
4063           continue;
4064         }
4065
4066       /* Spot cases that cannot be the beginning of a
4067          nested-name-specifier.  On the second and subsequent times
4068          through the loop, we look for the `template' keyword.  */
4069       if (success && token->keyword == RID_TEMPLATE)
4070         ;
4071       /* A template-id can start a nested-name-specifier.  */
4072       else if (token->type == CPP_TEMPLATE_ID)
4073         ;
4074       else
4075         {
4076           /* If the next token is not an identifier, then it is
4077              definitely not a type-name or namespace-name.  */
4078           if (token->type != CPP_NAME)
4079             break;
4080           /* If the following token is neither a `<' (to begin a
4081              template-id), nor a `::', then we are not looking at a
4082              nested-name-specifier.  */
4083           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4084           if (token->type != CPP_SCOPE
4085               && !cp_parser_nth_token_starts_template_argument_list_p
4086                   (parser, 2))
4087             break;
4088         }
4089
4090       /* The nested-name-specifier is optional, so we parse
4091          tentatively.  */
4092       cp_parser_parse_tentatively (parser);
4093
4094       /* Look for the optional `template' keyword, if this isn't the
4095          first time through the loop.  */
4096       if (success)
4097         template_keyword_p = cp_parser_optional_template_keyword (parser);
4098       else
4099         template_keyword_p = false;
4100
4101       /* Save the old scope since the name lookup we are about to do
4102          might destroy it.  */
4103       old_scope = parser->scope;
4104       saved_qualifying_scope = parser->qualifying_scope;
4105       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4106          look up names in "X<T>::I" in order to determine that "Y" is
4107          a template.  So, if we have a typename at this point, we make
4108          an effort to look through it.  */
4109       if (is_declaration
4110           && !typename_keyword_p
4111           && parser->scope
4112           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4113         parser->scope = resolve_typename_type (parser->scope,
4114                                                /*only_current_p=*/false);
4115       /* Parse the qualifying entity.  */
4116       new_scope
4117         = cp_parser_qualifying_entity (parser,
4118                                        typename_keyword_p,
4119                                        template_keyword_p,
4120                                        check_dependency_p,
4121                                        type_p,
4122                                        is_declaration);
4123       /* Look for the `::' token.  */
4124       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4125
4126       /* If we found what we wanted, we keep going; otherwise, we're
4127          done.  */
4128       if (!cp_parser_parse_definitely (parser))
4129         {
4130           bool error_p = false;
4131
4132           /* Restore the OLD_SCOPE since it was valid before the
4133              failed attempt at finding the last
4134              class-or-namespace-name.  */
4135           parser->scope = old_scope;
4136           parser->qualifying_scope = saved_qualifying_scope;
4137           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4138             break;
4139           /* If the next token is an identifier, and the one after
4140              that is a `::', then any valid interpretation would have
4141              found a class-or-namespace-name.  */
4142           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4143                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4144                      == CPP_SCOPE)
4145                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4146                      != CPP_COMPL))
4147             {
4148               token = cp_lexer_consume_token (parser->lexer);
4149               if (!error_p)
4150                 {
4151                   if (!token->ambiguous_p)
4152                     {
4153                       tree decl;
4154                       tree ambiguous_decls;
4155
4156                       decl = cp_parser_lookup_name (parser, token->u.value,
4157                                                     none_type,
4158                                                     /*is_template=*/false,
4159                                                     /*is_namespace=*/false,
4160                                                     /*check_dependency=*/true,
4161                                                     &ambiguous_decls,
4162                                                     token->location);
4163                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4164                         error_at (token->location,
4165                                   "%qD used without template parameters",
4166                                   decl);
4167                       else if (ambiguous_decls)
4168                         {
4169                           error_at (token->location,
4170                                     "reference to %qD is ambiguous",
4171                                     token->u.value);
4172                           print_candidates (ambiguous_decls);
4173                           decl = error_mark_node;
4174                         }
4175                       else
4176                         {
4177                           const char* msg = "is not a class or namespace";
4178                           if (cxx_dialect != cxx98)
4179                             msg = "is not a class, namespace, or enumeration";
4180                           cp_parser_name_lookup_error
4181                             (parser, token->u.value, decl, msg,
4182                              token->location);
4183                         }
4184                     }
4185                   parser->scope = error_mark_node;
4186                   error_p = true;
4187                   /* Treat this as a successful nested-name-specifier
4188                      due to:
4189
4190                      [basic.lookup.qual]
4191
4192                      If the name found is not a class-name (clause
4193                      _class_) or namespace-name (_namespace.def_), the
4194                      program is ill-formed.  */
4195                   success = true;
4196                 }
4197               cp_lexer_consume_token (parser->lexer);
4198             }
4199           break;
4200         }
4201       /* We've found one valid nested-name-specifier.  */
4202       success = true;
4203       /* Name lookup always gives us a DECL.  */
4204       if (TREE_CODE (new_scope) == TYPE_DECL)
4205         new_scope = TREE_TYPE (new_scope);
4206       /* Uses of "template" must be followed by actual templates.  */
4207       if (template_keyword_p
4208           && !(CLASS_TYPE_P (new_scope)
4209                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4210                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4211                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4212           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4213                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4214                    == TEMPLATE_ID_EXPR)))
4215         permerror (input_location, TYPE_P (new_scope)
4216                    ? "%qT is not a template"
4217                    : "%qD is not a template",
4218                    new_scope);
4219       /* If it is a class scope, try to complete it; we are about to
4220          be looking up names inside the class.  */
4221       if (TYPE_P (new_scope)
4222           /* Since checking types for dependency can be expensive,
4223              avoid doing it if the type is already complete.  */
4224           && !COMPLETE_TYPE_P (new_scope)
4225           /* Do not try to complete dependent types.  */
4226           && !dependent_type_p (new_scope))
4227         {
4228           new_scope = complete_type (new_scope);
4229           /* If it is a typedef to current class, use the current
4230              class instead, as the typedef won't have any names inside
4231              it yet.  */
4232           if (!COMPLETE_TYPE_P (new_scope)
4233               && currently_open_class (new_scope))
4234             new_scope = TYPE_MAIN_VARIANT (new_scope);
4235         }
4236       /* Make sure we look in the right scope the next time through
4237          the loop.  */
4238       parser->scope = new_scope;
4239     }
4240
4241   /* If parsing tentatively, replace the sequence of tokens that makes
4242      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4243      token.  That way, should we re-parse the token stream, we will
4244      not have to repeat the effort required to do the parse, nor will
4245      we issue duplicate error messages.  */
4246   if (success && start)
4247     {
4248       cp_token *token;
4249
4250       token = cp_lexer_token_at (parser->lexer, start);
4251       /* Reset the contents of the START token.  */
4252       token->type = CPP_NESTED_NAME_SPECIFIER;
4253       /* Retrieve any deferred checks.  Do not pop this access checks yet
4254          so the memory will not be reclaimed during token replacing below.  */
4255       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4256       token->u.tree_check_value->value = parser->scope;
4257       token->u.tree_check_value->checks = get_deferred_access_checks ();
4258       token->u.tree_check_value->qualifying_scope =
4259         parser->qualifying_scope;
4260       token->keyword = RID_MAX;
4261
4262       /* Purge all subsequent tokens.  */
4263       cp_lexer_purge_tokens_after (parser->lexer, start);
4264     }
4265
4266   if (start)
4267     pop_to_parent_deferring_access_checks ();
4268
4269   return success ? parser->scope : NULL_TREE;
4270 }
4271
4272 /* Parse a nested-name-specifier.  See
4273    cp_parser_nested_name_specifier_opt for details.  This function
4274    behaves identically, except that it will an issue an error if no
4275    nested-name-specifier is present.  */
4276
4277 static tree
4278 cp_parser_nested_name_specifier (cp_parser *parser,
4279                                  bool typename_keyword_p,
4280                                  bool check_dependency_p,
4281                                  bool type_p,
4282                                  bool is_declaration)
4283 {
4284   tree scope;
4285
4286   /* Look for the nested-name-specifier.  */
4287   scope = cp_parser_nested_name_specifier_opt (parser,
4288                                                typename_keyword_p,
4289                                                check_dependency_p,
4290                                                type_p,
4291                                                is_declaration);
4292   /* If it was not present, issue an error message.  */
4293   if (!scope)
4294     {
4295       cp_parser_error (parser, "expected nested-name-specifier");
4296       parser->scope = NULL_TREE;
4297     }
4298
4299   return scope;
4300 }
4301
4302 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4303    this is either a class-name or a namespace-name (which corresponds
4304    to the class-or-namespace-name production in the grammar). For
4305    C++0x, it can also be a type-name that refers to an enumeration
4306    type.
4307
4308    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4309    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4310    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4311    TYPE_P is TRUE iff the next name should be taken as a class-name,
4312    even the same name is declared to be another entity in the same
4313    scope.
4314
4315    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4316    specified by the class-or-namespace-name.  If neither is found the
4317    ERROR_MARK_NODE is returned.  */
4318
4319 static tree
4320 cp_parser_qualifying_entity (cp_parser *parser,
4321                              bool typename_keyword_p,
4322                              bool template_keyword_p,
4323                              bool check_dependency_p,
4324                              bool type_p,
4325                              bool is_declaration)
4326 {
4327   tree saved_scope;
4328   tree saved_qualifying_scope;
4329   tree saved_object_scope;
4330   tree scope;
4331   bool only_class_p;
4332   bool successful_parse_p;
4333
4334   /* Before we try to parse the class-name, we must save away the
4335      current PARSER->SCOPE since cp_parser_class_name will destroy
4336      it.  */
4337   saved_scope = parser->scope;
4338   saved_qualifying_scope = parser->qualifying_scope;
4339   saved_object_scope = parser->object_scope;
4340   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4341      there is no need to look for a namespace-name.  */
4342   only_class_p = template_keyword_p 
4343     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4344   if (!only_class_p)
4345     cp_parser_parse_tentatively (parser);
4346   scope = cp_parser_class_name (parser,
4347                                 typename_keyword_p,
4348                                 template_keyword_p,
4349                                 type_p ? class_type : none_type,
4350                                 check_dependency_p,
4351                                 /*class_head_p=*/false,
4352                                 is_declaration);
4353   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4354   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4355   if (!only_class_p 
4356       && cxx_dialect != cxx98
4357       && !successful_parse_p)
4358     {
4359       /* Restore the saved scope.  */
4360       parser->scope = saved_scope;
4361       parser->qualifying_scope = saved_qualifying_scope;
4362       parser->object_scope = saved_object_scope;
4363
4364       /* Parse tentatively.  */
4365       cp_parser_parse_tentatively (parser);
4366      
4367       /* Parse a typedef-name or enum-name.  */
4368       scope = cp_parser_nonclass_name (parser);
4369       successful_parse_p = cp_parser_parse_definitely (parser);
4370     }
4371   /* If that didn't work, try for a namespace-name.  */
4372   if (!only_class_p && !successful_parse_p)
4373     {
4374       /* Restore the saved scope.  */
4375       parser->scope = saved_scope;
4376       parser->qualifying_scope = saved_qualifying_scope;
4377       parser->object_scope = saved_object_scope;
4378       /* If we are not looking at an identifier followed by the scope
4379          resolution operator, then this is not part of a
4380          nested-name-specifier.  (Note that this function is only used
4381          to parse the components of a nested-name-specifier.)  */
4382       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4383           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4384         return error_mark_node;
4385       scope = cp_parser_namespace_name (parser);
4386     }
4387
4388   return scope;
4389 }
4390
4391 /* Parse a postfix-expression.
4392
4393    postfix-expression:
4394      primary-expression
4395      postfix-expression [ expression ]
4396      postfix-expression ( expression-list [opt] )
4397      simple-type-specifier ( expression-list [opt] )
4398      typename :: [opt] nested-name-specifier identifier
4399        ( expression-list [opt] )
4400      typename :: [opt] nested-name-specifier template [opt] template-id
4401        ( expression-list [opt] )
4402      postfix-expression . template [opt] id-expression
4403      postfix-expression -> template [opt] id-expression
4404      postfix-expression . pseudo-destructor-name
4405      postfix-expression -> pseudo-destructor-name
4406      postfix-expression ++
4407      postfix-expression --
4408      dynamic_cast < type-id > ( expression )
4409      static_cast < type-id > ( expression )
4410      reinterpret_cast < type-id > ( expression )
4411      const_cast < type-id > ( expression )
4412      typeid ( expression )
4413      typeid ( type-id )
4414
4415    GNU Extension:
4416
4417    postfix-expression:
4418      ( type-id ) { initializer-list , [opt] }
4419
4420    This extension is a GNU version of the C99 compound-literal
4421    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4422    but they are essentially the same concept.)
4423
4424    If ADDRESS_P is true, the postfix expression is the operand of the
4425    `&' operator.  CAST_P is true if this expression is the target of a
4426    cast.
4427
4428    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4429    class member access expressions [expr.ref].
4430
4431    Returns a representation of the expression.  */
4432
4433 static tree
4434 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4435                               bool member_access_only_p,
4436                               cp_id_kind * pidk_return)
4437 {
4438   cp_token *token;
4439   enum rid keyword;
4440   cp_id_kind idk = CP_ID_KIND_NONE;
4441   tree postfix_expression = NULL_TREE;
4442   bool is_member_access = false;
4443
4444   /* Peek at the next token.  */
4445   token = cp_lexer_peek_token (parser->lexer);
4446   /* Some of the productions are determined by keywords.  */
4447   keyword = token->keyword;
4448   switch (keyword)
4449     {
4450     case RID_DYNCAST:
4451     case RID_STATCAST:
4452     case RID_REINTCAST:
4453     case RID_CONSTCAST:
4454       {
4455         tree type;
4456         tree expression;
4457         const char *saved_message;
4458
4459         /* All of these can be handled in the same way from the point
4460            of view of parsing.  Begin by consuming the token
4461            identifying the cast.  */
4462         cp_lexer_consume_token (parser->lexer);
4463
4464         /* New types cannot be defined in the cast.  */
4465         saved_message = parser->type_definition_forbidden_message;
4466         parser->type_definition_forbidden_message
4467           = "types may not be defined in casts";
4468
4469         /* Look for the opening `<'.  */
4470         cp_parser_require (parser, CPP_LESS, "%<<%>");
4471         /* Parse the type to which we are casting.  */
4472         type = cp_parser_type_id (parser);
4473         /* Look for the closing `>'.  */
4474         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4475         /* Restore the old message.  */
4476         parser->type_definition_forbidden_message = saved_message;
4477
4478         /* And the expression which is being cast.  */
4479         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4480         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4481         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4482
4483         /* Only type conversions to integral or enumeration types
4484            can be used in constant-expressions.  */
4485         if (!cast_valid_in_integral_constant_expression_p (type)
4486             && (cp_parser_non_integral_constant_expression
4487                 (parser,
4488                  "a cast to a type other than an integral or "
4489                  "enumeration type")))
4490           return error_mark_node;
4491
4492         switch (keyword)
4493           {
4494           case RID_DYNCAST:
4495             postfix_expression
4496               = build_dynamic_cast (type, expression, tf_warning_or_error);
4497             break;
4498           case RID_STATCAST:
4499             postfix_expression
4500               = build_static_cast (type, expression, tf_warning_or_error);
4501             break;
4502           case RID_REINTCAST:
4503             postfix_expression
4504               = build_reinterpret_cast (type, expression, 
4505                                         tf_warning_or_error);
4506             break;
4507           case RID_CONSTCAST:
4508             postfix_expression
4509               = build_const_cast (type, expression, tf_warning_or_error);
4510             break;
4511           default:
4512             gcc_unreachable ();
4513           }
4514       }
4515       break;
4516
4517     case RID_TYPEID:
4518       {
4519         tree type;
4520         const char *saved_message;
4521         bool saved_in_type_id_in_expr_p;
4522
4523         /* Consume the `typeid' token.  */
4524         cp_lexer_consume_token (parser->lexer);
4525         /* Look for the `(' token.  */
4526         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4527         /* Types cannot be defined in a `typeid' expression.  */
4528         saved_message = parser->type_definition_forbidden_message;
4529         parser->type_definition_forbidden_message
4530           = "types may not be defined in a %<typeid%> expression";
4531         /* We can't be sure yet whether we're looking at a type-id or an
4532            expression.  */
4533         cp_parser_parse_tentatively (parser);
4534         /* Try a type-id first.  */
4535         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4536         parser->in_type_id_in_expr_p = true;
4537         type = cp_parser_type_id (parser);
4538         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4539         /* Look for the `)' token.  Otherwise, we can't be sure that
4540            we're not looking at an expression: consider `typeid (int
4541            (3))', for example.  */
4542         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4543         /* If all went well, simply lookup the type-id.  */
4544         if (cp_parser_parse_definitely (parser))
4545           postfix_expression = get_typeid (type);
4546         /* Otherwise, fall back to the expression variant.  */
4547         else
4548           {
4549             tree expression;
4550
4551             /* Look for an expression.  */
4552             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4553             /* Compute its typeid.  */
4554             postfix_expression = build_typeid (expression);
4555             /* Look for the `)' token.  */
4556             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557           }
4558         /* Restore the saved message.  */
4559         parser->type_definition_forbidden_message = saved_message;
4560         /* `typeid' may not appear in an integral constant expression.  */
4561         if (cp_parser_non_integral_constant_expression(parser,
4562                                                        "%<typeid%> operator"))
4563           return error_mark_node;
4564       }
4565       break;
4566
4567     case RID_TYPENAME:
4568       {
4569         tree type;
4570         /* The syntax permitted here is the same permitted for an
4571            elaborated-type-specifier.  */
4572         type = cp_parser_elaborated_type_specifier (parser,
4573                                                     /*is_friend=*/false,
4574                                                     /*is_declaration=*/false);
4575         postfix_expression = cp_parser_functional_cast (parser, type);
4576       }
4577       break;
4578
4579     default:
4580       {
4581         tree type;
4582
4583         /* If the next thing is a simple-type-specifier, we may be
4584            looking at a functional cast.  We could also be looking at
4585            an id-expression.  So, we try the functional cast, and if
4586            that doesn't work we fall back to the primary-expression.  */
4587         cp_parser_parse_tentatively (parser);
4588         /* Look for the simple-type-specifier.  */
4589         type = cp_parser_simple_type_specifier (parser,
4590                                                 /*decl_specs=*/NULL,
4591                                                 CP_PARSER_FLAGS_NONE);
4592         /* Parse the cast itself.  */
4593         if (!cp_parser_error_occurred (parser))
4594           postfix_expression
4595             = cp_parser_functional_cast (parser, type);
4596         /* If that worked, we're done.  */
4597         if (cp_parser_parse_definitely (parser))
4598           break;
4599
4600         /* If the functional-cast didn't work out, try a
4601            compound-literal.  */
4602         if (cp_parser_allow_gnu_extensions_p (parser)
4603             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4604           {
4605             VEC(constructor_elt,gc) *initializer_list = NULL;
4606             bool saved_in_type_id_in_expr_p;
4607
4608             cp_parser_parse_tentatively (parser);
4609             /* Consume the `('.  */
4610             cp_lexer_consume_token (parser->lexer);
4611             /* Parse the type.  */
4612             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4613             parser->in_type_id_in_expr_p = true;
4614             type = cp_parser_type_id (parser);
4615             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4616             /* Look for the `)'.  */
4617             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4618             /* Look for the `{'.  */
4619             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4620             /* If things aren't going well, there's no need to
4621                keep going.  */
4622             if (!cp_parser_error_occurred (parser))
4623               {
4624                 bool non_constant_p;
4625                 /* Parse the initializer-list.  */
4626                 initializer_list
4627                   = cp_parser_initializer_list (parser, &non_constant_p);
4628                 /* Allow a trailing `,'.  */
4629                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4630                   cp_lexer_consume_token (parser->lexer);
4631                 /* Look for the final `}'.  */
4632                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4633               }
4634             /* If that worked, we're definitely looking at a
4635                compound-literal expression.  */
4636             if (cp_parser_parse_definitely (parser))
4637               {
4638                 /* Warn the user that a compound literal is not
4639                    allowed in standard C++.  */
4640                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4641                 /* For simplicity, we disallow compound literals in
4642                    constant-expressions.  We could
4643                    allow compound literals of integer type, whose
4644                    initializer was a constant, in constant
4645                    expressions.  Permitting that usage, as a further
4646                    extension, would not change the meaning of any
4647                    currently accepted programs.  (Of course, as
4648                    compound literals are not part of ISO C++, the
4649                    standard has nothing to say.)  */
4650                 if (cp_parser_non_integral_constant_expression 
4651                     (parser, "non-constant compound literals"))
4652                   {
4653                     postfix_expression = error_mark_node;
4654                     break;
4655                   }
4656                 /* Form the representation of the compound-literal.  */
4657                 postfix_expression
4658                   = (finish_compound_literal
4659                      (type, build_constructor (init_list_type_node,
4660                                                initializer_list)));
4661                 break;
4662               }
4663           }
4664
4665         /* It must be a primary-expression.  */
4666         postfix_expression
4667           = cp_parser_primary_expression (parser, address_p, cast_p,
4668                                           /*template_arg_p=*/false,
4669                                           &idk);
4670       }
4671       break;
4672     }
4673
4674   /* Keep looping until the postfix-expression is complete.  */
4675   while (true)
4676     {
4677       if (idk == CP_ID_KIND_UNQUALIFIED
4678           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4679           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4680         /* It is not a Koenig lookup function call.  */
4681         postfix_expression
4682           = unqualified_name_lookup_error (postfix_expression);
4683
4684       /* Peek at the next token.  */
4685       token = cp_lexer_peek_token (parser->lexer);
4686
4687       switch (token->type)
4688         {
4689         case CPP_OPEN_SQUARE:
4690           postfix_expression
4691             = cp_parser_postfix_open_square_expression (parser,
4692                                                         postfix_expression,
4693                                                         false);
4694           idk = CP_ID_KIND_NONE;
4695           is_member_access = false;
4696           break;
4697
4698         case CPP_OPEN_PAREN:
4699           /* postfix-expression ( expression-list [opt] ) */
4700           {
4701             bool koenig_p;
4702             bool is_builtin_constant_p;
4703             bool saved_integral_constant_expression_p = false;
4704             bool saved_non_integral_constant_expression_p = false;
4705             VEC(tree,gc) *args;
4706
4707             is_member_access = false;
4708
4709             is_builtin_constant_p
4710               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4711             if (is_builtin_constant_p)
4712               {
4713                 /* The whole point of __builtin_constant_p is to allow
4714                    non-constant expressions to appear as arguments.  */
4715                 saved_integral_constant_expression_p
4716                   = parser->integral_constant_expression_p;
4717                 saved_non_integral_constant_expression_p
4718                   = parser->non_integral_constant_expression_p;
4719                 parser->integral_constant_expression_p = false;
4720               }
4721             args = (cp_parser_parenthesized_expression_list
4722                     (parser, /*is_attribute_list=*/false,
4723                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4724                      /*non_constant_p=*/NULL));
4725             if (is_builtin_constant_p)
4726               {
4727                 parser->integral_constant_expression_p
4728                   = saved_integral_constant_expression_p;
4729                 parser->non_integral_constant_expression_p
4730                   = saved_non_integral_constant_expression_p;
4731               }
4732
4733             if (args == NULL)
4734               {
4735                 postfix_expression = error_mark_node;
4736                 break;
4737               }
4738
4739             /* Function calls are not permitted in
4740                constant-expressions.  */
4741             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4742                 && cp_parser_non_integral_constant_expression (parser,
4743                                                                "a function call"))
4744               {
4745                 postfix_expression = error_mark_node;
4746                 release_tree_vector (args);
4747                 break;
4748               }
4749
4750             koenig_p = false;
4751             if (idk == CP_ID_KIND_UNQUALIFIED
4752                 || idk == CP_ID_KIND_TEMPLATE_ID)
4753               {
4754                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4755                   {
4756                     if (!VEC_empty (tree, args))
4757                       {
4758                         koenig_p = true;
4759                         if (!any_type_dependent_arguments_p (args))
4760                           postfix_expression
4761                             = perform_koenig_lookup (postfix_expression, args);
4762                       }
4763                     else
4764                       postfix_expression
4765                         = unqualified_fn_lookup_error (postfix_expression);
4766                   }
4767                 /* We do not perform argument-dependent lookup if
4768                    normal lookup finds a non-function, in accordance
4769                    with the expected resolution of DR 218.  */
4770                 else if (!VEC_empty (tree, args)
4771                          && is_overloaded_fn (postfix_expression))
4772                   {
4773                     tree fn = get_first_fn (postfix_expression);
4774
4775                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4776                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4777
4778                     /* Only do argument dependent lookup if regular
4779                        lookup does not find a set of member functions.
4780                        [basic.lookup.koenig]/2a  */
4781                     if (!DECL_FUNCTION_MEMBER_P (fn))
4782                       {
4783                         koenig_p = true;
4784                         if (!any_type_dependent_arguments_p (args))
4785                           postfix_expression
4786                             = perform_koenig_lookup (postfix_expression, args);
4787                       }
4788                   }
4789               }
4790
4791             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4792               {
4793                 tree instance = TREE_OPERAND (postfix_expression, 0);
4794                 tree fn = TREE_OPERAND (postfix_expression, 1);
4795
4796                 if (processing_template_decl
4797                     && (type_dependent_expression_p (instance)
4798                         || (!BASELINK_P (fn)
4799                             && TREE_CODE (fn) != FIELD_DECL)
4800                         || type_dependent_expression_p (fn)
4801                         || any_type_dependent_arguments_p (args)))
4802                   {
4803                     postfix_expression
4804                       = build_nt_call_vec (postfix_expression, args);
4805                     release_tree_vector (args);
4806                     break;
4807                   }
4808
4809                 if (BASELINK_P (fn))
4810                   {
4811                   postfix_expression
4812                     = (build_new_method_call
4813                        (instance, fn, &args, NULL_TREE,
4814                         (idk == CP_ID_KIND_QUALIFIED
4815                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4816                         /*fn_p=*/NULL,
4817                         tf_warning_or_error));
4818                   }
4819                 else
4820                   postfix_expression
4821                     = finish_call_expr (postfix_expression, &args,
4822                                         /*disallow_virtual=*/false,
4823                                         /*koenig_p=*/false,
4824                                         tf_warning_or_error);
4825               }
4826             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4827                      || TREE_CODE (postfix_expression) == MEMBER_REF
4828                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4829               postfix_expression = (build_offset_ref_call_from_tree
4830                                     (postfix_expression, &args));
4831             else if (idk == CP_ID_KIND_QUALIFIED)
4832               /* A call to a static class member, or a namespace-scope
4833                  function.  */
4834               postfix_expression
4835                 = finish_call_expr (postfix_expression, &args,
4836                                     /*disallow_virtual=*/true,
4837                                     koenig_p,
4838                                     tf_warning_or_error);
4839             else
4840               /* All other function calls.  */
4841               postfix_expression
4842                 = finish_call_expr (postfix_expression, &args,
4843                                     /*disallow_virtual=*/false,
4844                                     koenig_p,
4845                                     tf_warning_or_error);
4846
4847             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4848             idk = CP_ID_KIND_NONE;
4849
4850             release_tree_vector (args);
4851           }
4852           break;
4853
4854         case CPP_DOT:
4855         case CPP_DEREF:
4856           /* postfix-expression . template [opt] id-expression
4857              postfix-expression . pseudo-destructor-name
4858              postfix-expression -> template [opt] id-expression
4859              postfix-expression -> pseudo-destructor-name */
4860
4861           /* Consume the `.' or `->' operator.  */
4862           cp_lexer_consume_token (parser->lexer);
4863
4864           postfix_expression
4865             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4866                                                       postfix_expression,
4867                                                       false, &idk,
4868                                                       token->location);
4869
4870           is_member_access = true;
4871           break;
4872
4873         case CPP_PLUS_PLUS:
4874           /* postfix-expression ++  */
4875           /* Consume the `++' token.  */
4876           cp_lexer_consume_token (parser->lexer);
4877           /* Generate a representation for the complete expression.  */
4878           postfix_expression
4879             = finish_increment_expr (postfix_expression,
4880                                      POSTINCREMENT_EXPR);
4881           /* Increments may not appear in constant-expressions.  */
4882           if (cp_parser_non_integral_constant_expression (parser,
4883                                                           "an increment"))
4884             postfix_expression = error_mark_node;
4885           idk = CP_ID_KIND_NONE;
4886           is_member_access = false;
4887           break;
4888
4889         case CPP_MINUS_MINUS:
4890           /* postfix-expression -- */
4891           /* Consume the `--' token.  */
4892           cp_lexer_consume_token (parser->lexer);
4893           /* Generate a representation for the complete expression.  */
4894           postfix_expression
4895             = finish_increment_expr (postfix_expression,
4896                                      POSTDECREMENT_EXPR);
4897           /* Decrements may not appear in constant-expressions.  */
4898           if (cp_parser_non_integral_constant_expression (parser,
4899                                                           "a decrement"))
4900             postfix_expression = error_mark_node;
4901           idk = CP_ID_KIND_NONE;
4902           is_member_access = false;
4903           break;
4904
4905         default:
4906           if (pidk_return != NULL)
4907             * pidk_return = idk;
4908           if (member_access_only_p)
4909             return is_member_access? postfix_expression : error_mark_node;
4910           else
4911             return postfix_expression;
4912         }
4913     }
4914
4915   /* We should never get here.  */
4916   gcc_unreachable ();
4917   return error_mark_node;
4918 }
4919
4920 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4921    by cp_parser_builtin_offsetof.  We're looking for
4922
4923      postfix-expression [ expression ]
4924
4925    FOR_OFFSETOF is set if we're being called in that context, which
4926    changes how we deal with integer constant expressions.  */
4927
4928 static tree
4929 cp_parser_postfix_open_square_expression (cp_parser *parser,
4930                                           tree postfix_expression,
4931                                           bool for_offsetof)
4932 {
4933   tree index;
4934
4935   /* Consume the `[' token.  */
4936   cp_lexer_consume_token (parser->lexer);
4937
4938   /* Parse the index expression.  */
4939   /* ??? For offsetof, there is a question of what to allow here.  If
4940      offsetof is not being used in an integral constant expression context,
4941      then we *could* get the right answer by computing the value at runtime.
4942      If we are in an integral constant expression context, then we might
4943      could accept any constant expression; hard to say without analysis.
4944      Rather than open the barn door too wide right away, allow only integer
4945      constant expressions here.  */
4946   if (for_offsetof)
4947     index = cp_parser_constant_expression (parser, false, NULL);
4948   else
4949     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4950
4951   /* Look for the closing `]'.  */
4952   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4953
4954   /* Build the ARRAY_REF.  */
4955   postfix_expression = grok_array_decl (postfix_expression, index);
4956
4957   /* When not doing offsetof, array references are not permitted in
4958      constant-expressions.  */
4959   if (!for_offsetof
4960       && (cp_parser_non_integral_constant_expression
4961           (parser, "an array reference")))
4962     postfix_expression = error_mark_node;
4963
4964   return postfix_expression;
4965 }
4966
4967 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4968    by cp_parser_builtin_offsetof.  We're looking for
4969
4970      postfix-expression . template [opt] id-expression
4971      postfix-expression . pseudo-destructor-name
4972      postfix-expression -> template [opt] id-expression
4973      postfix-expression -> pseudo-destructor-name
4974
4975    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4976    limits what of the above we'll actually accept, but nevermind.
4977    TOKEN_TYPE is the "." or "->" token, which will already have been
4978    removed from the stream.  */
4979
4980 static tree
4981 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4982                                         enum cpp_ttype token_type,
4983                                         tree postfix_expression,
4984                                         bool for_offsetof, cp_id_kind *idk,
4985                                         location_t location)
4986 {
4987   tree name;
4988   bool dependent_p;
4989   bool pseudo_destructor_p;
4990   tree scope = NULL_TREE;
4991
4992   /* If this is a `->' operator, dereference the pointer.  */
4993   if (token_type == CPP_DEREF)
4994     postfix_expression = build_x_arrow (postfix_expression);
4995   /* Check to see whether or not the expression is type-dependent.  */
4996   dependent_p = type_dependent_expression_p (postfix_expression);
4997   /* The identifier following the `->' or `.' is not qualified.  */
4998   parser->scope = NULL_TREE;
4999   parser->qualifying_scope = NULL_TREE;
5000   parser->object_scope = NULL_TREE;
5001   *idk = CP_ID_KIND_NONE;
5002
5003   /* Enter the scope corresponding to the type of the object
5004      given by the POSTFIX_EXPRESSION.  */
5005   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5006     {
5007       scope = TREE_TYPE (postfix_expression);
5008       /* According to the standard, no expression should ever have
5009          reference type.  Unfortunately, we do not currently match
5010          the standard in this respect in that our internal representation
5011          of an expression may have reference type even when the standard
5012          says it does not.  Therefore, we have to manually obtain the
5013          underlying type here.  */
5014       scope = non_reference (scope);
5015       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5016       if (scope == unknown_type_node)
5017         {
5018           error_at (location, "%qE does not have class type",
5019                     postfix_expression);
5020           scope = NULL_TREE;
5021         }
5022       else
5023         scope = complete_type_or_else (scope, NULL_TREE);
5024       /* Let the name lookup machinery know that we are processing a
5025          class member access expression.  */
5026       parser->context->object_type = scope;
5027       /* If something went wrong, we want to be able to discern that case,
5028          as opposed to the case where there was no SCOPE due to the type
5029          of expression being dependent.  */
5030       if (!scope)
5031         scope = error_mark_node;
5032       /* If the SCOPE was erroneous, make the various semantic analysis
5033          functions exit quickly -- and without issuing additional error
5034          messages.  */
5035       if (scope == error_mark_node)
5036         postfix_expression = error_mark_node;
5037     }
5038
5039   /* Assume this expression is not a pseudo-destructor access.  */
5040   pseudo_destructor_p = false;
5041
5042   /* If the SCOPE is a scalar type, then, if this is a valid program,
5043      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5044      is type dependent, it can be pseudo-destructor-name or something else.
5045      Try to parse it as pseudo-destructor-name first.  */
5046   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5047     {
5048       tree s;
5049       tree type;
5050
5051       cp_parser_parse_tentatively (parser);
5052       /* Parse the pseudo-destructor-name.  */
5053       s = NULL_TREE;
5054       cp_parser_pseudo_destructor_name (parser, &s, &type);
5055       if (dependent_p
5056           && (cp_parser_error_occurred (parser)
5057               || TREE_CODE (type) != TYPE_DECL
5058               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5059         cp_parser_abort_tentative_parse (parser);
5060       else if (cp_parser_parse_definitely (parser))
5061         {
5062           pseudo_destructor_p = true;
5063           postfix_expression
5064             = finish_pseudo_destructor_expr (postfix_expression,
5065                                              s, TREE_TYPE (type));
5066         }
5067     }
5068
5069   if (!pseudo_destructor_p)
5070     {
5071       /* If the SCOPE is not a scalar type, we are looking at an
5072          ordinary class member access expression, rather than a
5073          pseudo-destructor-name.  */
5074       bool template_p;
5075       cp_token *token = cp_lexer_peek_token (parser->lexer);
5076       /* Parse the id-expression.  */
5077       name = (cp_parser_id_expression
5078               (parser,
5079                cp_parser_optional_template_keyword (parser),
5080                /*check_dependency_p=*/true,
5081                &template_p,
5082                /*declarator_p=*/false,
5083                /*optional_p=*/false));
5084       /* In general, build a SCOPE_REF if the member name is qualified.
5085          However, if the name was not dependent and has already been
5086          resolved; there is no need to build the SCOPE_REF.  For example;
5087
5088              struct X { void f(); };
5089              template <typename T> void f(T* t) { t->X::f(); }
5090
5091          Even though "t" is dependent, "X::f" is not and has been resolved
5092          to a BASELINK; there is no need to include scope information.  */
5093
5094       /* But we do need to remember that there was an explicit scope for
5095          virtual function calls.  */
5096       if (parser->scope)
5097         *idk = CP_ID_KIND_QUALIFIED;
5098
5099       /* If the name is a template-id that names a type, we will get a
5100          TYPE_DECL here.  That is invalid code.  */
5101       if (TREE_CODE (name) == TYPE_DECL)
5102         {
5103           error_at (token->location, "invalid use of %qD", name);
5104           postfix_expression = error_mark_node;
5105         }
5106       else
5107         {
5108           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5109             {
5110               name = build_qualified_name (/*type=*/NULL_TREE,
5111                                            parser->scope,
5112                                            name,
5113                                            template_p);
5114               parser->scope = NULL_TREE;
5115               parser->qualifying_scope = NULL_TREE;
5116               parser->object_scope = NULL_TREE;
5117             }
5118           if (scope && name && BASELINK_P (name))
5119             adjust_result_of_qualified_name_lookup
5120               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5121           postfix_expression
5122             = finish_class_member_access_expr (postfix_expression, name,
5123                                                template_p, 
5124                                                tf_warning_or_error);
5125         }
5126     }
5127
5128   /* We no longer need to look up names in the scope of the object on
5129      the left-hand side of the `.' or `->' operator.  */
5130   parser->context->object_type = NULL_TREE;
5131
5132   /* Outside of offsetof, these operators may not appear in
5133      constant-expressions.  */
5134   if (!for_offsetof
5135       && (cp_parser_non_integral_constant_expression
5136           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5137     postfix_expression = error_mark_node;
5138
5139   return postfix_expression;
5140 }
5141
5142 /* Parse a parenthesized expression-list.
5143
5144    expression-list:
5145      assignment-expression
5146      expression-list, assignment-expression
5147
5148    attribute-list:
5149      expression-list
5150      identifier
5151      identifier, expression-list
5152
5153    CAST_P is true if this expression is the target of a cast.
5154
5155    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5156    argument pack.
5157
5158    Returns a vector of trees.  Each element is a representation of an
5159    assignment-expression.  NULL is returned if the ( and or ) are
5160    missing.  An empty, but allocated, vector is returned on no
5161    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5162    if this is really an attribute list being parsed.  If
5163    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5164    not all of the expressions in the list were constant.  */
5165
5166 static VEC(tree,gc) *
5167 cp_parser_parenthesized_expression_list (cp_parser* parser,
5168                                          bool is_attribute_list,
5169                                          bool cast_p,
5170                                          bool allow_expansion_p,
5171                                          bool *non_constant_p)
5172 {
5173   VEC(tree,gc) *expression_list;
5174   bool fold_expr_p = is_attribute_list;
5175   tree identifier = NULL_TREE;
5176   bool saved_greater_than_is_operator_p;
5177
5178   /* Assume all the expressions will be constant.  */
5179   if (non_constant_p)
5180     *non_constant_p = false;
5181
5182   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5183     return NULL;
5184
5185   expression_list = make_tree_vector ();
5186
5187   /* Within a parenthesized expression, a `>' token is always
5188      the greater-than operator.  */
5189   saved_greater_than_is_operator_p
5190     = parser->greater_than_is_operator_p;
5191   parser->greater_than_is_operator_p = true;
5192
5193   /* Consume expressions until there are no more.  */
5194   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5195     while (true)
5196       {
5197         tree expr;
5198
5199         /* At the beginning of attribute lists, check to see if the
5200            next token is an identifier.  */
5201         if (is_attribute_list
5202             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5203           {
5204             cp_token *token;
5205
5206             /* Consume the identifier.  */
5207             token = cp_lexer_consume_token (parser->lexer);
5208             /* Save the identifier.  */
5209             identifier = token->u.value;
5210           }
5211         else
5212           {
5213             bool expr_non_constant_p;
5214
5215             /* Parse the next assignment-expression.  */
5216             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5217               {
5218                 /* A braced-init-list.  */
5219                 maybe_warn_cpp0x ("extended initializer lists");
5220                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5221                 if (non_constant_p && expr_non_constant_p)
5222                   *non_constant_p = true;
5223               }
5224             else if (non_constant_p)
5225               {
5226                 expr = (cp_parser_constant_expression
5227                         (parser, /*allow_non_constant_p=*/true,
5228                          &expr_non_constant_p));
5229                 if (expr_non_constant_p)
5230                   *non_constant_p = true;
5231               }
5232             else
5233               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5234
5235             if (fold_expr_p)
5236               expr = fold_non_dependent_expr (expr);
5237
5238             /* If we have an ellipsis, then this is an expression
5239                expansion.  */
5240             if (allow_expansion_p
5241                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5242               {
5243                 /* Consume the `...'.  */
5244                 cp_lexer_consume_token (parser->lexer);
5245
5246                 /* Build the argument pack.  */
5247                 expr = make_pack_expansion (expr);
5248               }
5249
5250              /* Add it to the list.  We add error_mark_node
5251                 expressions to the list, so that we can still tell if
5252                 the correct form for a parenthesized expression-list
5253                 is found. That gives better errors.  */
5254             VEC_safe_push (tree, gc, expression_list, expr);
5255
5256             if (expr == error_mark_node)
5257               goto skip_comma;
5258           }
5259
5260         /* After the first item, attribute lists look the same as
5261            expression lists.  */
5262         is_attribute_list = false;
5263
5264       get_comma:;
5265         /* If the next token isn't a `,', then we are done.  */
5266         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5267           break;
5268
5269         /* Otherwise, consume the `,' and keep going.  */
5270         cp_lexer_consume_token (parser->lexer);
5271       }
5272
5273   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5274     {
5275       int ending;
5276
5277     skip_comma:;
5278       /* We try and resync to an unnested comma, as that will give the
5279          user better diagnostics.  */
5280       ending = cp_parser_skip_to_closing_parenthesis (parser,
5281                                                       /*recovering=*/true,
5282                                                       /*or_comma=*/true,
5283                                                       /*consume_paren=*/true);
5284       if (ending < 0)
5285         goto get_comma;
5286       if (!ending)
5287         {
5288           parser->greater_than_is_operator_p
5289             = saved_greater_than_is_operator_p;
5290           return NULL;
5291         }
5292     }
5293
5294   parser->greater_than_is_operator_p
5295     = saved_greater_than_is_operator_p;
5296
5297   if (identifier)
5298     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5299
5300   return expression_list;
5301 }
5302
5303 /* Parse a pseudo-destructor-name.
5304
5305    pseudo-destructor-name:
5306      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5307      :: [opt] nested-name-specifier template template-id :: ~ type-name
5308      :: [opt] nested-name-specifier [opt] ~ type-name
5309
5310    If either of the first two productions is used, sets *SCOPE to the
5311    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5312    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5313    or ERROR_MARK_NODE if the parse fails.  */
5314
5315 static void
5316 cp_parser_pseudo_destructor_name (cp_parser* parser,
5317                                   tree* scope,
5318                                   tree* type)
5319 {
5320   bool nested_name_specifier_p;
5321
5322   /* Assume that things will not work out.  */
5323   *type = error_mark_node;
5324
5325   /* Look for the optional `::' operator.  */
5326   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5327   /* Look for the optional nested-name-specifier.  */
5328   nested_name_specifier_p
5329     = (cp_parser_nested_name_specifier_opt (parser,
5330                                             /*typename_keyword_p=*/false,
5331                                             /*check_dependency_p=*/true,
5332                                             /*type_p=*/false,
5333                                             /*is_declaration=*/false)
5334        != NULL_TREE);
5335   /* Now, if we saw a nested-name-specifier, we might be doing the
5336      second production.  */
5337   if (nested_name_specifier_p
5338       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5339     {
5340       /* Consume the `template' keyword.  */
5341       cp_lexer_consume_token (parser->lexer);
5342       /* Parse the template-id.  */
5343       cp_parser_template_id (parser,
5344                              /*template_keyword_p=*/true,
5345                              /*check_dependency_p=*/false,
5346                              /*is_declaration=*/true);
5347       /* Look for the `::' token.  */
5348       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5349     }
5350   /* If the next token is not a `~', then there might be some
5351      additional qualification.  */
5352   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5353     {
5354       /* At this point, we're looking for "type-name :: ~".  The type-name
5355          must not be a class-name, since this is a pseudo-destructor.  So,
5356          it must be either an enum-name, or a typedef-name -- both of which
5357          are just identifiers.  So, we peek ahead to check that the "::"
5358          and "~" tokens are present; if they are not, then we can avoid
5359          calling type_name.  */
5360       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5361           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5362           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5363         {
5364           cp_parser_error (parser, "non-scalar type");
5365           return;
5366         }
5367
5368       /* Look for the type-name.  */
5369       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5370       if (*scope == error_mark_node)
5371         return;
5372
5373       /* Look for the `::' token.  */
5374       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5375     }
5376   else
5377     *scope = NULL_TREE;
5378
5379   /* Look for the `~'.  */
5380   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5381   /* Look for the type-name again.  We are not responsible for
5382      checking that it matches the first type-name.  */
5383   *type = cp_parser_nonclass_name (parser);
5384 }
5385
5386 /* Parse a unary-expression.
5387
5388    unary-expression:
5389      postfix-expression
5390      ++ cast-expression
5391      -- cast-expression
5392      unary-operator cast-expression
5393      sizeof unary-expression
5394      sizeof ( type-id )
5395      new-expression
5396      delete-expression
5397
5398    GNU Extensions:
5399
5400    unary-expression:
5401      __extension__ cast-expression
5402      __alignof__ unary-expression
5403      __alignof__ ( type-id )
5404      __real__ cast-expression
5405      __imag__ cast-expression
5406      && identifier
5407
5408    ADDRESS_P is true iff the unary-expression is appearing as the
5409    operand of the `&' operator.   CAST_P is true if this expression is
5410    the target of a cast.
5411
5412    Returns a representation of the expression.  */
5413
5414 static tree
5415 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5416                             cp_id_kind * pidk)
5417 {
5418   cp_token *token;
5419   enum tree_code unary_operator;
5420
5421   /* Peek at the next token.  */
5422   token = cp_lexer_peek_token (parser->lexer);
5423   /* Some keywords give away the kind of expression.  */
5424   if (token->type == CPP_KEYWORD)
5425     {
5426       enum rid keyword = token->keyword;
5427
5428       switch (keyword)
5429         {
5430         case RID_ALIGNOF:
5431         case RID_SIZEOF:
5432           {
5433             tree operand;
5434             enum tree_code op;
5435
5436             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5437             /* Consume the token.  */
5438             cp_lexer_consume_token (parser->lexer);
5439             /* Parse the operand.  */
5440             operand = cp_parser_sizeof_operand (parser, keyword);
5441
5442             if (TYPE_P (operand))
5443               return cxx_sizeof_or_alignof_type (operand, op, true);
5444             else
5445               return cxx_sizeof_or_alignof_expr (operand, op, true);
5446           }
5447
5448         case RID_NEW:
5449           return cp_parser_new_expression (parser);
5450
5451         case RID_DELETE:
5452           return cp_parser_delete_expression (parser);
5453
5454         case RID_EXTENSION:
5455           {
5456             /* The saved value of the PEDANTIC flag.  */
5457             int saved_pedantic;
5458             tree expr;
5459
5460             /* Save away the PEDANTIC flag.  */
5461             cp_parser_extension_opt (parser, &saved_pedantic);
5462             /* Parse the cast-expression.  */
5463             expr = cp_parser_simple_cast_expression (parser);
5464             /* Restore the PEDANTIC flag.  */
5465             pedantic = saved_pedantic;
5466
5467             return expr;
5468           }
5469
5470         case RID_REALPART:
5471         case RID_IMAGPART:
5472           {
5473             tree expression;
5474
5475             /* Consume the `__real__' or `__imag__' token.  */
5476             cp_lexer_consume_token (parser->lexer);
5477             /* Parse the cast-expression.  */
5478             expression = cp_parser_simple_cast_expression (parser);
5479             /* Create the complete representation.  */
5480             return build_x_unary_op ((keyword == RID_REALPART
5481                                       ? REALPART_EXPR : IMAGPART_EXPR),
5482                                      expression,
5483                                      tf_warning_or_error);
5484           }
5485           break;
5486
5487         default:
5488           break;
5489         }
5490     }
5491
5492   /* Look for the `:: new' and `:: delete', which also signal the
5493      beginning of a new-expression, or delete-expression,
5494      respectively.  If the next token is `::', then it might be one of
5495      these.  */
5496   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5497     {
5498       enum rid keyword;
5499
5500       /* See if the token after the `::' is one of the keywords in
5501          which we're interested.  */
5502       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5503       /* If it's `new', we have a new-expression.  */
5504       if (keyword == RID_NEW)
5505         return cp_parser_new_expression (parser);
5506       /* Similarly, for `delete'.  */
5507       else if (keyword == RID_DELETE)
5508         return cp_parser_delete_expression (parser);
5509     }
5510
5511   /* Look for a unary operator.  */
5512   unary_operator = cp_parser_unary_operator (token);
5513   /* The `++' and `--' operators can be handled similarly, even though
5514      they are not technically unary-operators in the grammar.  */
5515   if (unary_operator == ERROR_MARK)
5516     {
5517       if (token->type == CPP_PLUS_PLUS)
5518         unary_operator = PREINCREMENT_EXPR;
5519       else if (token->type == CPP_MINUS_MINUS)
5520         unary_operator = PREDECREMENT_EXPR;
5521       /* Handle the GNU address-of-label extension.  */
5522       else if (cp_parser_allow_gnu_extensions_p (parser)
5523                && token->type == CPP_AND_AND)
5524         {
5525           tree identifier;
5526           tree expression;
5527           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5528
5529           /* Consume the '&&' token.  */
5530           cp_lexer_consume_token (parser->lexer);
5531           /* Look for the identifier.  */
5532           identifier = cp_parser_identifier (parser);
5533           /* Create an expression representing the address.  */
5534           expression = finish_label_address_expr (identifier, loc);
5535           if (cp_parser_non_integral_constant_expression (parser,
5536                                                 "the address of a label"))
5537             expression = error_mark_node;
5538           return expression;
5539         }
5540     }
5541   if (unary_operator != ERROR_MARK)
5542     {
5543       tree cast_expression;
5544       tree expression = error_mark_node;
5545       const char *non_constant_p = NULL;
5546
5547       /* Consume the operator token.  */
5548       token = cp_lexer_consume_token (parser->lexer);
5549       /* Parse the cast-expression.  */
5550       cast_expression
5551         = cp_parser_cast_expression (parser,
5552                                      unary_operator == ADDR_EXPR,
5553                                      /*cast_p=*/false, pidk);
5554       /* Now, build an appropriate representation.  */
5555       switch (unary_operator)
5556         {
5557         case INDIRECT_REF:
5558           non_constant_p = "%<*%>";
5559           expression = build_x_indirect_ref (cast_expression, "unary *",
5560                                              tf_warning_or_error);
5561           break;
5562
5563         case ADDR_EXPR:
5564           non_constant_p = "%<&%>";
5565           /* Fall through.  */
5566         case BIT_NOT_EXPR:
5567           expression = build_x_unary_op (unary_operator, cast_expression,
5568                                          tf_warning_or_error);
5569           break;
5570
5571         case PREINCREMENT_EXPR:
5572         case PREDECREMENT_EXPR:
5573           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5574                             ? "%<++%>" : "%<--%>");
5575           /* Fall through.  */
5576         case UNARY_PLUS_EXPR:
5577         case NEGATE_EXPR:
5578         case TRUTH_NOT_EXPR:
5579           expression = finish_unary_op_expr (unary_operator, cast_expression);
5580           break;
5581
5582         default:
5583           gcc_unreachable ();
5584         }
5585
5586       if (non_constant_p
5587           && cp_parser_non_integral_constant_expression (parser,
5588                                                          non_constant_p))
5589         expression = error_mark_node;
5590
5591       return expression;
5592     }
5593
5594   return cp_parser_postfix_expression (parser, address_p, cast_p,
5595                                        /*member_access_only_p=*/false,
5596                                        pidk);
5597 }
5598
5599 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5600    unary-operator, the corresponding tree code is returned.  */
5601
5602 static enum tree_code
5603 cp_parser_unary_operator (cp_token* token)
5604 {
5605   switch (token->type)
5606     {
5607     case CPP_MULT:
5608       return INDIRECT_REF;
5609
5610     case CPP_AND:
5611       return ADDR_EXPR;
5612
5613     case CPP_PLUS:
5614       return UNARY_PLUS_EXPR;
5615
5616     case CPP_MINUS:
5617       return NEGATE_EXPR;
5618
5619     case CPP_NOT:
5620       return TRUTH_NOT_EXPR;
5621
5622     case CPP_COMPL:
5623       return BIT_NOT_EXPR;
5624
5625     default:
5626       return ERROR_MARK;
5627     }
5628 }
5629
5630 /* Parse a new-expression.
5631
5632    new-expression:
5633      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5634      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5635
5636    Returns a representation of the expression.  */
5637
5638 static tree
5639 cp_parser_new_expression (cp_parser* parser)
5640 {
5641   bool global_scope_p;
5642   VEC(tree,gc) *placement;
5643   tree type;
5644   VEC(tree,gc) *initializer;
5645   tree nelts;
5646   tree ret;
5647
5648   /* Look for the optional `::' operator.  */
5649   global_scope_p
5650     = (cp_parser_global_scope_opt (parser,
5651                                    /*current_scope_valid_p=*/false)
5652        != NULL_TREE);
5653   /* Look for the `new' operator.  */
5654   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5655   /* There's no easy way to tell a new-placement from the
5656      `( type-id )' construct.  */
5657   cp_parser_parse_tentatively (parser);
5658   /* Look for a new-placement.  */
5659   placement = cp_parser_new_placement (parser);
5660   /* If that didn't work out, there's no new-placement.  */
5661   if (!cp_parser_parse_definitely (parser))
5662     {
5663       if (placement != NULL)
5664         release_tree_vector (placement);
5665       placement = NULL;
5666     }
5667
5668   /* If the next token is a `(', then we have a parenthesized
5669      type-id.  */
5670   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5671     {
5672       cp_token *token;
5673       /* Consume the `('.  */
5674       cp_lexer_consume_token (parser->lexer);
5675       /* Parse the type-id.  */
5676       type = cp_parser_type_id (parser);
5677       /* Look for the closing `)'.  */
5678       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5679       token = cp_lexer_peek_token (parser->lexer);
5680       /* There should not be a direct-new-declarator in this production,
5681          but GCC used to allowed this, so we check and emit a sensible error
5682          message for this case.  */
5683       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5684         {
5685           error_at (token->location,
5686                     "array bound forbidden after parenthesized type-id");
5687           inform (token->location, 
5688                   "try removing the parentheses around the type-id");
5689           cp_parser_direct_new_declarator (parser);
5690         }
5691       nelts = NULL_TREE;
5692     }
5693   /* Otherwise, there must be a new-type-id.  */
5694   else
5695     type = cp_parser_new_type_id (parser, &nelts);
5696
5697   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5698   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5699       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5700     initializer = cp_parser_new_initializer (parser);
5701   else
5702     initializer = NULL;
5703
5704   /* A new-expression may not appear in an integral constant
5705      expression.  */
5706   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5707     ret = error_mark_node;
5708   else
5709     {
5710       /* Create a representation of the new-expression.  */
5711       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5712                        tf_warning_or_error);
5713     }
5714
5715   if (placement != NULL)
5716     release_tree_vector (placement);
5717   if (initializer != NULL)
5718     release_tree_vector (initializer);
5719
5720   return ret;
5721 }
5722
5723 /* Parse a new-placement.
5724
5725    new-placement:
5726      ( expression-list )
5727
5728    Returns the same representation as for an expression-list.  */
5729
5730 static VEC(tree,gc) *
5731 cp_parser_new_placement (cp_parser* parser)
5732 {
5733   VEC(tree,gc) *expression_list;
5734
5735   /* Parse the expression-list.  */
5736   expression_list = (cp_parser_parenthesized_expression_list
5737                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5738                       /*non_constant_p=*/NULL));
5739
5740   return expression_list;
5741 }
5742
5743 /* Parse a new-type-id.
5744
5745    new-type-id:
5746      type-specifier-seq new-declarator [opt]
5747
5748    Returns the TYPE allocated.  If the new-type-id indicates an array
5749    type, *NELTS is set to the number of elements in the last array
5750    bound; the TYPE will not include the last array bound.  */
5751
5752 static tree
5753 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5754 {
5755   cp_decl_specifier_seq type_specifier_seq;
5756   cp_declarator *new_declarator;
5757   cp_declarator *declarator;
5758   cp_declarator *outer_declarator;
5759   const char *saved_message;
5760   tree type;
5761
5762   /* The type-specifier sequence must not contain type definitions.
5763      (It cannot contain declarations of new types either, but if they
5764      are not definitions we will catch that because they are not
5765      complete.)  */
5766   saved_message = parser->type_definition_forbidden_message;
5767   parser->type_definition_forbidden_message
5768     = "types may not be defined in a new-type-id";
5769   /* Parse the type-specifier-seq.  */
5770   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5771                                 &type_specifier_seq);
5772   /* Restore the old message.  */
5773   parser->type_definition_forbidden_message = saved_message;
5774   /* Parse the new-declarator.  */
5775   new_declarator = cp_parser_new_declarator_opt (parser);
5776
5777   /* Determine the number of elements in the last array dimension, if
5778      any.  */
5779   *nelts = NULL_TREE;
5780   /* Skip down to the last array dimension.  */
5781   declarator = new_declarator;
5782   outer_declarator = NULL;
5783   while (declarator && (declarator->kind == cdk_pointer
5784                         || declarator->kind == cdk_ptrmem))
5785     {
5786       outer_declarator = declarator;
5787       declarator = declarator->declarator;
5788     }
5789   while (declarator
5790          && declarator->kind == cdk_array
5791          && declarator->declarator
5792          && declarator->declarator->kind == cdk_array)
5793     {
5794       outer_declarator = declarator;
5795       declarator = declarator->declarator;
5796     }
5797
5798   if (declarator && declarator->kind == cdk_array)
5799     {
5800       *nelts = declarator->u.array.bounds;
5801       if (*nelts == error_mark_node)
5802         *nelts = integer_one_node;
5803
5804       if (outer_declarator)
5805         outer_declarator->declarator = declarator->declarator;
5806       else
5807         new_declarator = NULL;
5808     }
5809
5810   type = groktypename (&type_specifier_seq, new_declarator, false);
5811   return type;
5812 }
5813
5814 /* Parse an (optional) new-declarator.
5815
5816    new-declarator:
5817      ptr-operator new-declarator [opt]
5818      direct-new-declarator
5819
5820    Returns the declarator.  */
5821
5822 static cp_declarator *
5823 cp_parser_new_declarator_opt (cp_parser* parser)
5824 {
5825   enum tree_code code;
5826   tree type;
5827   cp_cv_quals cv_quals;
5828
5829   /* We don't know if there's a ptr-operator next, or not.  */
5830   cp_parser_parse_tentatively (parser);
5831   /* Look for a ptr-operator.  */
5832   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5833   /* If that worked, look for more new-declarators.  */
5834   if (cp_parser_parse_definitely (parser))
5835     {
5836       cp_declarator *declarator;
5837
5838       /* Parse another optional declarator.  */
5839       declarator = cp_parser_new_declarator_opt (parser);
5840
5841       return cp_parser_make_indirect_declarator
5842         (code, type, cv_quals, declarator);
5843     }
5844
5845   /* If the next token is a `[', there is a direct-new-declarator.  */
5846   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5847     return cp_parser_direct_new_declarator (parser);
5848
5849   return NULL;
5850 }
5851
5852 /* Parse a direct-new-declarator.
5853
5854    direct-new-declarator:
5855      [ expression ]
5856      direct-new-declarator [constant-expression]
5857
5858    */
5859
5860 static cp_declarator *
5861 cp_parser_direct_new_declarator (cp_parser* parser)
5862 {
5863   cp_declarator *declarator = NULL;
5864
5865   while (true)
5866     {
5867       tree expression;
5868
5869       /* Look for the opening `['.  */
5870       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5871       /* The first expression is not required to be constant.  */
5872       if (!declarator)
5873         {
5874           cp_token *token = cp_lexer_peek_token (parser->lexer);
5875           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5876           /* The standard requires that the expression have integral
5877              type.  DR 74 adds enumeration types.  We believe that the
5878              real intent is that these expressions be handled like the
5879              expression in a `switch' condition, which also allows
5880              classes with a single conversion to integral or
5881              enumeration type.  */
5882           if (!processing_template_decl)
5883             {
5884               expression
5885                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5886                                               expression,
5887                                               /*complain=*/true);
5888               if (!expression)
5889                 {
5890                   error_at (token->location,
5891                             "expression in new-declarator must have integral "
5892                             "or enumeration type");
5893                   expression = error_mark_node;
5894                 }
5895             }
5896         }
5897       /* But all the other expressions must be.  */
5898       else
5899         expression
5900           = cp_parser_constant_expression (parser,
5901                                            /*allow_non_constant=*/false,
5902                                            NULL);
5903       /* Look for the closing `]'.  */
5904       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5905
5906       /* Add this bound to the declarator.  */
5907       declarator = make_array_declarator (declarator, expression);
5908
5909       /* If the next token is not a `[', then there are no more
5910          bounds.  */
5911       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5912         break;
5913     }
5914
5915   return declarator;
5916 }
5917
5918 /* Parse a new-initializer.
5919
5920    new-initializer:
5921      ( expression-list [opt] )
5922      braced-init-list
5923
5924    Returns a representation of the expression-list.  */
5925
5926 static VEC(tree,gc) *
5927 cp_parser_new_initializer (cp_parser* parser)
5928 {
5929   VEC(tree,gc) *expression_list;
5930
5931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5932     {
5933       tree t;
5934       bool expr_non_constant_p;
5935       maybe_warn_cpp0x ("extended initializer lists");
5936       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5937       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5938       expression_list = make_tree_vector_single (t);
5939     }
5940   else
5941     expression_list = (cp_parser_parenthesized_expression_list
5942                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5943                         /*non_constant_p=*/NULL));
5944
5945   return expression_list;
5946 }
5947
5948 /* Parse a delete-expression.
5949
5950    delete-expression:
5951      :: [opt] delete cast-expression
5952      :: [opt] delete [ ] cast-expression
5953
5954    Returns a representation of the expression.  */
5955
5956 static tree
5957 cp_parser_delete_expression (cp_parser* parser)
5958 {
5959   bool global_scope_p;
5960   bool array_p;
5961   tree expression;
5962
5963   /* Look for the optional `::' operator.  */
5964   global_scope_p
5965     = (cp_parser_global_scope_opt (parser,
5966                                    /*current_scope_valid_p=*/false)
5967        != NULL_TREE);
5968   /* Look for the `delete' keyword.  */
5969   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5970   /* See if the array syntax is in use.  */
5971   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5972     {
5973       /* Consume the `[' token.  */
5974       cp_lexer_consume_token (parser->lexer);
5975       /* Look for the `]' token.  */
5976       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5977       /* Remember that this is the `[]' construct.  */
5978       array_p = true;
5979     }
5980   else
5981     array_p = false;
5982
5983   /* Parse the cast-expression.  */
5984   expression = cp_parser_simple_cast_expression (parser);
5985
5986   /* A delete-expression may not appear in an integral constant
5987      expression.  */
5988   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5989     return error_mark_node;
5990
5991   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5992 }
5993
5994 /* Returns true if TOKEN may start a cast-expression and false
5995    otherwise.  */
5996
5997 static bool
5998 cp_parser_token_starts_cast_expression (cp_token *token)
5999 {
6000   switch (token->type)
6001     {
6002     case CPP_COMMA:
6003     case CPP_SEMICOLON:
6004     case CPP_QUERY:
6005     case CPP_COLON:
6006     case CPP_CLOSE_SQUARE:
6007     case CPP_CLOSE_PAREN:
6008     case CPP_CLOSE_BRACE:
6009     case CPP_DOT:
6010     case CPP_DOT_STAR:
6011     case CPP_DEREF:
6012     case CPP_DEREF_STAR:
6013     case CPP_DIV:
6014     case CPP_MOD:
6015     case CPP_LSHIFT:
6016     case CPP_RSHIFT:
6017     case CPP_LESS:
6018     case CPP_GREATER:
6019     case CPP_LESS_EQ:
6020     case CPP_GREATER_EQ:
6021     case CPP_EQ_EQ:
6022     case CPP_NOT_EQ:
6023     case CPP_EQ:
6024     case CPP_MULT_EQ:
6025     case CPP_DIV_EQ:
6026     case CPP_MOD_EQ:
6027     case CPP_PLUS_EQ:
6028     case CPP_MINUS_EQ:
6029     case CPP_RSHIFT_EQ:
6030     case CPP_LSHIFT_EQ:
6031     case CPP_AND_EQ:
6032     case CPP_XOR_EQ:
6033     case CPP_OR_EQ:
6034     case CPP_XOR:
6035     case CPP_OR:
6036     case CPP_OR_OR:
6037     case CPP_EOF:
6038       return false;
6039
6040       /* '[' may start a primary-expression in obj-c++.  */
6041     case CPP_OPEN_SQUARE:
6042       return c_dialect_objc ();
6043
6044     default:
6045       return true;
6046     }
6047 }
6048
6049 /* Parse a cast-expression.
6050
6051    cast-expression:
6052      unary-expression
6053      ( type-id ) cast-expression
6054
6055    ADDRESS_P is true iff the unary-expression is appearing as the
6056    operand of the `&' operator.   CAST_P is true if this expression is
6057    the target of a cast.
6058
6059    Returns a representation of the expression.  */
6060
6061 static tree
6062 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6063                            cp_id_kind * pidk)
6064 {
6065   /* If it's a `(', then we might be looking at a cast.  */
6066   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6067     {
6068       tree type = NULL_TREE;
6069       tree expr = NULL_TREE;
6070       bool compound_literal_p;
6071       const char *saved_message;
6072
6073       /* There's no way to know yet whether or not this is a cast.
6074          For example, `(int (3))' is a unary-expression, while `(int)
6075          3' is a cast.  So, we resort to parsing tentatively.  */
6076       cp_parser_parse_tentatively (parser);
6077       /* Types may not be defined in a cast.  */
6078       saved_message = parser->type_definition_forbidden_message;
6079       parser->type_definition_forbidden_message
6080         = "types may not be defined in casts";
6081       /* Consume the `('.  */
6082       cp_lexer_consume_token (parser->lexer);
6083       /* A very tricky bit is that `(struct S) { 3 }' is a
6084          compound-literal (which we permit in C++ as an extension).
6085          But, that construct is not a cast-expression -- it is a
6086          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6087          is legal; if the compound-literal were a cast-expression,
6088          you'd need an extra set of parentheses.)  But, if we parse
6089          the type-id, and it happens to be a class-specifier, then we
6090          will commit to the parse at that point, because we cannot
6091          undo the action that is done when creating a new class.  So,
6092          then we cannot back up and do a postfix-expression.
6093
6094          Therefore, we scan ahead to the closing `)', and check to see
6095          if the token after the `)' is a `{'.  If so, we are not
6096          looking at a cast-expression.
6097
6098          Save tokens so that we can put them back.  */
6099       cp_lexer_save_tokens (parser->lexer);
6100       /* Skip tokens until the next token is a closing parenthesis.
6101          If we find the closing `)', and the next token is a `{', then
6102          we are looking at a compound-literal.  */
6103       compound_literal_p
6104         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6105                                                   /*consume_paren=*/true)
6106            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6107       /* Roll back the tokens we skipped.  */
6108       cp_lexer_rollback_tokens (parser->lexer);
6109       /* If we were looking at a compound-literal, simulate an error
6110          so that the call to cp_parser_parse_definitely below will
6111          fail.  */
6112       if (compound_literal_p)
6113         cp_parser_simulate_error (parser);
6114       else
6115         {
6116           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6117           parser->in_type_id_in_expr_p = true;
6118           /* Look for the type-id.  */
6119           type = cp_parser_type_id (parser);
6120           /* Look for the closing `)'.  */
6121           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6122           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6123         }
6124
6125       /* Restore the saved message.  */
6126       parser->type_definition_forbidden_message = saved_message;
6127
6128       /* At this point this can only be either a cast or a
6129          parenthesized ctor such as `(T ())' that looks like a cast to
6130          function returning T.  */
6131       if (!cp_parser_error_occurred (parser)
6132           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6133                                                      (parser->lexer)))
6134         {
6135           cp_parser_parse_definitely (parser);
6136           expr = cp_parser_cast_expression (parser,
6137                                             /*address_p=*/false,
6138                                             /*cast_p=*/true, pidk);
6139
6140           /* Warn about old-style casts, if so requested.  */
6141           if (warn_old_style_cast
6142               && !in_system_header
6143               && !VOID_TYPE_P (type)
6144               && current_lang_name != lang_name_c)
6145             warning (OPT_Wold_style_cast, "use of old-style cast");
6146
6147           /* Only type conversions to integral or enumeration types
6148              can be used in constant-expressions.  */
6149           if (!cast_valid_in_integral_constant_expression_p (type)
6150               && (cp_parser_non_integral_constant_expression
6151                   (parser,
6152                    "a cast to a type other than an integral or "
6153                    "enumeration type")))
6154             return error_mark_node;
6155
6156           /* Perform the cast.  */
6157           expr = build_c_cast (input_location, type, expr);
6158           return expr;
6159         }
6160       else 
6161         cp_parser_abort_tentative_parse (parser);
6162     }
6163
6164   /* If we get here, then it's not a cast, so it must be a
6165      unary-expression.  */
6166   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6167 }
6168
6169 /* Parse a binary expression of the general form:
6170
6171    pm-expression:
6172      cast-expression
6173      pm-expression .* cast-expression
6174      pm-expression ->* cast-expression
6175
6176    multiplicative-expression:
6177      pm-expression
6178      multiplicative-expression * pm-expression
6179      multiplicative-expression / pm-expression
6180      multiplicative-expression % pm-expression
6181
6182    additive-expression:
6183      multiplicative-expression
6184      additive-expression + multiplicative-expression
6185      additive-expression - multiplicative-expression
6186
6187    shift-expression:
6188      additive-expression
6189      shift-expression << additive-expression
6190      shift-expression >> additive-expression
6191
6192    relational-expression:
6193      shift-expression
6194      relational-expression < shift-expression
6195      relational-expression > shift-expression
6196      relational-expression <= shift-expression
6197      relational-expression >= shift-expression
6198
6199   GNU Extension:
6200
6201    relational-expression:
6202      relational-expression <? shift-expression
6203      relational-expression >? shift-expression
6204
6205    equality-expression:
6206      relational-expression
6207      equality-expression == relational-expression
6208      equality-expression != relational-expression
6209
6210    and-expression:
6211      equality-expression
6212      and-expression & equality-expression
6213
6214    exclusive-or-expression:
6215      and-expression
6216      exclusive-or-expression ^ and-expression
6217
6218    inclusive-or-expression:
6219      exclusive-or-expression
6220      inclusive-or-expression | exclusive-or-expression
6221
6222    logical-and-expression:
6223      inclusive-or-expression
6224      logical-and-expression && inclusive-or-expression
6225
6226    logical-or-expression:
6227      logical-and-expression
6228      logical-or-expression || logical-and-expression
6229
6230    All these are implemented with a single function like:
6231
6232    binary-expression:
6233      simple-cast-expression
6234      binary-expression <token> binary-expression
6235
6236    CAST_P is true if this expression is the target of a cast.
6237
6238    The binops_by_token map is used to get the tree codes for each <token> type.
6239    binary-expressions are associated according to a precedence table.  */
6240
6241 #define TOKEN_PRECEDENCE(token)                              \
6242 (((token->type == CPP_GREATER                                \
6243    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6244   && !parser->greater_than_is_operator_p)                    \
6245  ? PREC_NOT_OPERATOR                                         \
6246  : binops_by_token[token->type].prec)
6247
6248 static tree
6249 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6250                              bool no_toplevel_fold_p,
6251                              enum cp_parser_prec prec,
6252                              cp_id_kind * pidk)
6253 {
6254   cp_parser_expression_stack stack;
6255   cp_parser_expression_stack_entry *sp = &stack[0];
6256   tree lhs, rhs;
6257   cp_token *token;
6258   enum tree_code tree_type, lhs_type, rhs_type;
6259   enum cp_parser_prec new_prec, lookahead_prec;
6260   bool overloaded_p;
6261
6262   /* Parse the first expression.  */
6263   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6264   lhs_type = ERROR_MARK;
6265
6266   for (;;)
6267     {
6268       /* Get an operator token.  */
6269       token = cp_lexer_peek_token (parser->lexer);
6270
6271       if (warn_cxx0x_compat
6272           && token->type == CPP_RSHIFT
6273           && !parser->greater_than_is_operator_p)
6274         {
6275           if (warning_at (token->location, OPT_Wc__0x_compat, 
6276                           "%<>>%> operator will be treated as"
6277                           " two right angle brackets in C++0x"))
6278             inform (token->location,
6279                     "suggest parentheses around %<>>%> expression");
6280         }
6281
6282       new_prec = TOKEN_PRECEDENCE (token);
6283
6284       /* Popping an entry off the stack means we completed a subexpression:
6285          - either we found a token which is not an operator (`>' where it is not
6286            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6287            will happen repeatedly;
6288          - or, we found an operator which has lower priority.  This is the case
6289            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6290            parsing `3 * 4'.  */
6291       if (new_prec <= prec)
6292         {
6293           if (sp == stack)
6294             break;
6295           else
6296             goto pop;
6297         }
6298
6299      get_rhs:
6300       tree_type = binops_by_token[token->type].tree_type;
6301
6302       /* We used the operator token.  */
6303       cp_lexer_consume_token (parser->lexer);
6304
6305       /* For "false && x" or "true || x", x will never be executed;
6306          disable warnings while evaluating it.  */
6307       if (tree_type == TRUTH_ANDIF_EXPR)
6308         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6309       else if (tree_type == TRUTH_ORIF_EXPR)
6310         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6311
6312       /* Extract another operand.  It may be the RHS of this expression
6313          or the LHS of a new, higher priority expression.  */
6314       rhs = cp_parser_simple_cast_expression (parser);
6315       rhs_type = ERROR_MARK;
6316
6317       /* Get another operator token.  Look up its precedence to avoid
6318          building a useless (immediately popped) stack entry for common
6319          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6320       token = cp_lexer_peek_token (parser->lexer);
6321       lookahead_prec = TOKEN_PRECEDENCE (token);
6322       if (lookahead_prec > new_prec)
6323         {
6324           /* ... and prepare to parse the RHS of the new, higher priority
6325              expression.  Since precedence levels on the stack are
6326              monotonically increasing, we do not have to care about
6327              stack overflows.  */
6328           sp->prec = prec;
6329           sp->tree_type = tree_type;
6330           sp->lhs = lhs;
6331           sp->lhs_type = lhs_type;
6332           sp++;
6333           lhs = rhs;
6334           lhs_type = rhs_type;
6335           prec = new_prec;
6336           new_prec = lookahead_prec;
6337           goto get_rhs;
6338
6339          pop:
6340           lookahead_prec = new_prec;
6341           /* If the stack is not empty, we have parsed into LHS the right side
6342              (`4' in the example above) of an expression we had suspended.
6343              We can use the information on the stack to recover the LHS (`3')
6344              from the stack together with the tree code (`MULT_EXPR'), and
6345              the precedence of the higher level subexpression
6346              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6347              which will be used to actually build the additive expression.  */
6348           --sp;
6349           prec = sp->prec;
6350           tree_type = sp->tree_type;
6351           rhs = lhs;
6352           rhs_type = lhs_type;
6353           lhs = sp->lhs;
6354           lhs_type = sp->lhs_type;
6355         }
6356
6357       /* Undo the disabling of warnings done above.  */
6358       if (tree_type == TRUTH_ANDIF_EXPR)
6359         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6360       else if (tree_type == TRUTH_ORIF_EXPR)
6361         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6362
6363       overloaded_p = false;
6364       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6365          ERROR_MARK for everything that is not a binary expression.
6366          This makes warn_about_parentheses miss some warnings that
6367          involve unary operators.  For unary expressions we should
6368          pass the correct tree_code unless the unary expression was
6369          surrounded by parentheses.
6370       */
6371       if (no_toplevel_fold_p
6372           && lookahead_prec <= prec
6373           && sp == stack
6374           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6375         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6376       else
6377         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6378                                  &overloaded_p, tf_warning_or_error);
6379       lhs_type = tree_type;
6380
6381       /* If the binary operator required the use of an overloaded operator,
6382          then this expression cannot be an integral constant-expression.
6383          An overloaded operator can be used even if both operands are
6384          otherwise permissible in an integral constant-expression if at
6385          least one of the operands is of enumeration type.  */
6386
6387       if (overloaded_p
6388           && (cp_parser_non_integral_constant_expression
6389               (parser, "calls to overloaded operators")))
6390         return error_mark_node;
6391     }
6392
6393   return lhs;
6394 }
6395
6396
6397 /* Parse the `? expression : assignment-expression' part of a
6398    conditional-expression.  The LOGICAL_OR_EXPR is the
6399    logical-or-expression that started the conditional-expression.
6400    Returns a representation of the entire conditional-expression.
6401
6402    This routine is used by cp_parser_assignment_expression.
6403
6404      ? expression : assignment-expression
6405
6406    GNU Extensions:
6407
6408      ? : assignment-expression */
6409
6410 static tree
6411 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6412 {
6413   tree expr;
6414   tree assignment_expr;
6415
6416   /* Consume the `?' token.  */
6417   cp_lexer_consume_token (parser->lexer);
6418   if (cp_parser_allow_gnu_extensions_p (parser)
6419       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6420     {
6421       /* Implicit true clause.  */
6422       expr = NULL_TREE;
6423       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6424     }
6425   else
6426     {
6427       /* Parse the expression.  */
6428       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6429       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6430       c_inhibit_evaluation_warnings +=
6431         ((logical_or_expr == truthvalue_true_node)
6432          - (logical_or_expr == truthvalue_false_node));
6433     }
6434
6435   /* The next token should be a `:'.  */
6436   cp_parser_require (parser, CPP_COLON, "%<:%>");
6437   /* Parse the assignment-expression.  */
6438   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6439   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6440
6441   /* Build the conditional-expression.  */
6442   return build_x_conditional_expr (logical_or_expr,
6443                                    expr,
6444                                    assignment_expr,
6445                                    tf_warning_or_error);
6446 }
6447
6448 /* Parse an assignment-expression.
6449
6450    assignment-expression:
6451      conditional-expression
6452      logical-or-expression assignment-operator assignment_expression
6453      throw-expression
6454
6455    CAST_P is true if this expression is the target of a cast.
6456
6457    Returns a representation for the expression.  */
6458
6459 static tree
6460 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6461                                  cp_id_kind * pidk)
6462 {
6463   tree expr;
6464
6465   /* If the next token is the `throw' keyword, then we're looking at
6466      a throw-expression.  */
6467   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6468     expr = cp_parser_throw_expression (parser);
6469   /* Otherwise, it must be that we are looking at a
6470      logical-or-expression.  */
6471   else
6472     {
6473       /* Parse the binary expressions (logical-or-expression).  */
6474       expr = cp_parser_binary_expression (parser, cast_p, false,
6475                                           PREC_NOT_OPERATOR, pidk);
6476       /* If the next token is a `?' then we're actually looking at a
6477          conditional-expression.  */
6478       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6479         return cp_parser_question_colon_clause (parser, expr);
6480       else
6481         {
6482           enum tree_code assignment_operator;
6483
6484           /* If it's an assignment-operator, we're using the second
6485              production.  */
6486           assignment_operator
6487             = cp_parser_assignment_operator_opt (parser);
6488           if (assignment_operator != ERROR_MARK)
6489             {
6490               bool non_constant_p;
6491
6492               /* Parse the right-hand side of the assignment.  */
6493               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6494
6495               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6496                 maybe_warn_cpp0x ("extended initializer lists");
6497
6498               /* An assignment may not appear in a
6499                  constant-expression.  */
6500               if (cp_parser_non_integral_constant_expression (parser,
6501                                                               "an assignment"))
6502                 return error_mark_node;
6503               /* Build the assignment expression.  */
6504               expr = build_x_modify_expr (expr,
6505                                           assignment_operator,
6506                                           rhs,
6507                                           tf_warning_or_error);
6508             }
6509         }
6510     }
6511
6512   return expr;
6513 }
6514
6515 /* Parse an (optional) assignment-operator.
6516
6517    assignment-operator: one of
6518      = *= /= %= += -= >>= <<= &= ^= |=
6519
6520    GNU Extension:
6521
6522    assignment-operator: one of
6523      <?= >?=
6524
6525    If the next token is an assignment operator, the corresponding tree
6526    code is returned, and the token is consumed.  For example, for
6527    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6528    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6529    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6530    operator, ERROR_MARK is returned.  */
6531
6532 static enum tree_code
6533 cp_parser_assignment_operator_opt (cp_parser* parser)
6534 {
6535   enum tree_code op;
6536   cp_token *token;
6537
6538   /* Peek at the next token.  */
6539   token = cp_lexer_peek_token (parser->lexer);
6540
6541   switch (token->type)
6542     {
6543     case CPP_EQ:
6544       op = NOP_EXPR;
6545       break;
6546
6547     case CPP_MULT_EQ:
6548       op = MULT_EXPR;
6549       break;
6550
6551     case CPP_DIV_EQ:
6552       op = TRUNC_DIV_EXPR;
6553       break;
6554
6555     case CPP_MOD_EQ:
6556       op = TRUNC_MOD_EXPR;
6557       break;
6558
6559     case CPP_PLUS_EQ:
6560       op = PLUS_EXPR;
6561       break;
6562
6563     case CPP_MINUS_EQ:
6564       op = MINUS_EXPR;
6565       break;
6566
6567     case CPP_RSHIFT_EQ:
6568       op = RSHIFT_EXPR;
6569       break;
6570
6571     case CPP_LSHIFT_EQ:
6572       op = LSHIFT_EXPR;
6573       break;
6574
6575     case CPP_AND_EQ:
6576       op = BIT_AND_EXPR;
6577       break;
6578
6579     case CPP_XOR_EQ:
6580       op = BIT_XOR_EXPR;
6581       break;
6582
6583     case CPP_OR_EQ:
6584       op = BIT_IOR_EXPR;
6585       break;
6586
6587     default:
6588       /* Nothing else is an assignment operator.  */
6589       op = ERROR_MARK;
6590     }
6591
6592   /* If it was an assignment operator, consume it.  */
6593   if (op != ERROR_MARK)
6594     cp_lexer_consume_token (parser->lexer);
6595
6596   return op;
6597 }
6598
6599 /* Parse an expression.
6600
6601    expression:
6602      assignment-expression
6603      expression , assignment-expression
6604
6605    CAST_P is true if this expression is the target of a cast.
6606
6607    Returns a representation of the expression.  */
6608
6609 static tree
6610 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6611 {
6612   tree expression = NULL_TREE;
6613
6614   while (true)
6615     {
6616       tree assignment_expression;
6617
6618       /* Parse the next assignment-expression.  */
6619       assignment_expression
6620         = cp_parser_assignment_expression (parser, cast_p, pidk);
6621       /* If this is the first assignment-expression, we can just
6622          save it away.  */
6623       if (!expression)
6624         expression = assignment_expression;
6625       else
6626         expression = build_x_compound_expr (expression,
6627                                             assignment_expression,
6628                                             tf_warning_or_error);
6629       /* If the next token is not a comma, then we are done with the
6630          expression.  */
6631       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6632         break;
6633       /* Consume the `,'.  */
6634       cp_lexer_consume_token (parser->lexer);
6635       /* A comma operator cannot appear in a constant-expression.  */
6636       if (cp_parser_non_integral_constant_expression (parser,
6637                                                       "a comma operator"))
6638         expression = error_mark_node;
6639     }
6640
6641   return expression;
6642 }
6643
6644 /* Parse a constant-expression.
6645
6646    constant-expression:
6647      conditional-expression
6648
6649   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6650   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6651   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6652   is false, NON_CONSTANT_P should be NULL.  */
6653
6654 static tree
6655 cp_parser_constant_expression (cp_parser* parser,
6656                                bool allow_non_constant_p,
6657                                bool *non_constant_p)
6658 {
6659   bool saved_integral_constant_expression_p;
6660   bool saved_allow_non_integral_constant_expression_p;
6661   bool saved_non_integral_constant_expression_p;
6662   tree expression;
6663
6664   /* It might seem that we could simply parse the
6665      conditional-expression, and then check to see if it were
6666      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6667      one that the compiler can figure out is constant, possibly after
6668      doing some simplifications or optimizations.  The standard has a
6669      precise definition of constant-expression, and we must honor
6670      that, even though it is somewhat more restrictive.
6671
6672      For example:
6673
6674        int i[(2, 3)];
6675
6676      is not a legal declaration, because `(2, 3)' is not a
6677      constant-expression.  The `,' operator is forbidden in a
6678      constant-expression.  However, GCC's constant-folding machinery
6679      will fold this operation to an INTEGER_CST for `3'.  */
6680
6681   /* Save the old settings.  */
6682   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6683   saved_allow_non_integral_constant_expression_p
6684     = parser->allow_non_integral_constant_expression_p;
6685   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6686   /* We are now parsing a constant-expression.  */
6687   parser->integral_constant_expression_p = true;
6688   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6689   parser->non_integral_constant_expression_p = false;
6690   /* Although the grammar says "conditional-expression", we parse an
6691      "assignment-expression", which also permits "throw-expression"
6692      and the use of assignment operators.  In the case that
6693      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6694      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6695      actually essential that we look for an assignment-expression.
6696      For example, cp_parser_initializer_clauses uses this function to
6697      determine whether a particular assignment-expression is in fact
6698      constant.  */
6699   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6700   /* Restore the old settings.  */
6701   parser->integral_constant_expression_p
6702     = saved_integral_constant_expression_p;
6703   parser->allow_non_integral_constant_expression_p
6704     = saved_allow_non_integral_constant_expression_p;
6705   if (allow_non_constant_p)
6706     *non_constant_p = parser->non_integral_constant_expression_p;
6707   else if (parser->non_integral_constant_expression_p)
6708     expression = error_mark_node;
6709   parser->non_integral_constant_expression_p
6710     = saved_non_integral_constant_expression_p;
6711
6712   return expression;
6713 }
6714
6715 /* Parse __builtin_offsetof.
6716
6717    offsetof-expression:
6718      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6719
6720    offsetof-member-designator:
6721      id-expression
6722      | offsetof-member-designator "." id-expression
6723      | offsetof-member-designator "[" expression "]"
6724      | offsetof-member-designator "->" id-expression  */
6725
6726 static tree
6727 cp_parser_builtin_offsetof (cp_parser *parser)
6728 {
6729   int save_ice_p, save_non_ice_p;
6730   tree type, expr;
6731   cp_id_kind dummy;
6732   cp_token *token;
6733
6734   /* We're about to accept non-integral-constant things, but will
6735      definitely yield an integral constant expression.  Save and
6736      restore these values around our local parsing.  */
6737   save_ice_p = parser->integral_constant_expression_p;
6738   save_non_ice_p = parser->non_integral_constant_expression_p;
6739
6740   /* Consume the "__builtin_offsetof" token.  */
6741   cp_lexer_consume_token (parser->lexer);
6742   /* Consume the opening `('.  */
6743   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6744   /* Parse the type-id.  */
6745   type = cp_parser_type_id (parser);
6746   /* Look for the `,'.  */
6747   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6748   token = cp_lexer_peek_token (parser->lexer);
6749
6750   /* Build the (type *)null that begins the traditional offsetof macro.  */
6751   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6752                             tf_warning_or_error);
6753
6754   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6755   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6756                                                  true, &dummy, token->location);
6757   while (true)
6758     {
6759       token = cp_lexer_peek_token (parser->lexer);
6760       switch (token->type)
6761         {
6762         case CPP_OPEN_SQUARE:
6763           /* offsetof-member-designator "[" expression "]" */
6764           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6765           break;
6766
6767         case CPP_DEREF:
6768           /* offsetof-member-designator "->" identifier */
6769           expr = grok_array_decl (expr, integer_zero_node);
6770           /* FALLTHRU */
6771
6772         case CPP_DOT:
6773           /* offsetof-member-designator "." identifier */
6774           cp_lexer_consume_token (parser->lexer);
6775           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6776                                                          expr, true, &dummy,
6777                                                          token->location);
6778           break;
6779
6780         case CPP_CLOSE_PAREN:
6781           /* Consume the ")" token.  */
6782           cp_lexer_consume_token (parser->lexer);
6783           goto success;
6784
6785         default:
6786           /* Error.  We know the following require will fail, but
6787              that gives the proper error message.  */
6788           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6789           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6790           expr = error_mark_node;
6791           goto failure;
6792         }
6793     }
6794
6795  success:
6796   /* If we're processing a template, we can't finish the semantics yet.
6797      Otherwise we can fold the entire expression now.  */
6798   if (processing_template_decl)
6799     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6800   else
6801     expr = finish_offsetof (expr);
6802
6803  failure:
6804   parser->integral_constant_expression_p = save_ice_p;
6805   parser->non_integral_constant_expression_p = save_non_ice_p;
6806
6807   return expr;
6808 }
6809
6810 /* Parse a trait expression.  */
6811
6812 static tree
6813 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6814 {
6815   cp_trait_kind kind;
6816   tree type1, type2 = NULL_TREE;
6817   bool binary = false;
6818   cp_decl_specifier_seq decl_specs;
6819
6820   switch (keyword)
6821     {
6822     case RID_HAS_NOTHROW_ASSIGN:
6823       kind = CPTK_HAS_NOTHROW_ASSIGN;
6824       break;
6825     case RID_HAS_NOTHROW_CONSTRUCTOR:
6826       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6827       break;
6828     case RID_HAS_NOTHROW_COPY:
6829       kind = CPTK_HAS_NOTHROW_COPY;
6830       break;
6831     case RID_HAS_TRIVIAL_ASSIGN:
6832       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6833       break;
6834     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6835       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6836       break;
6837     case RID_HAS_TRIVIAL_COPY:
6838       kind = CPTK_HAS_TRIVIAL_COPY;
6839       break;
6840     case RID_HAS_TRIVIAL_DESTRUCTOR:
6841       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6842       break;
6843     case RID_HAS_VIRTUAL_DESTRUCTOR:
6844       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6845       break;
6846     case RID_IS_ABSTRACT:
6847       kind = CPTK_IS_ABSTRACT;
6848       break;
6849     case RID_IS_BASE_OF:
6850       kind = CPTK_IS_BASE_OF;
6851       binary = true;
6852       break;
6853     case RID_IS_CLASS:
6854       kind = CPTK_IS_CLASS;
6855       break;
6856     case RID_IS_CONVERTIBLE_TO:
6857       kind = CPTK_IS_CONVERTIBLE_TO;
6858       binary = true;
6859       break;
6860     case RID_IS_EMPTY:
6861       kind = CPTK_IS_EMPTY;
6862       break;
6863     case RID_IS_ENUM:
6864       kind = CPTK_IS_ENUM;
6865       break;
6866     case RID_IS_POD:
6867       kind = CPTK_IS_POD;
6868       break;
6869     case RID_IS_POLYMORPHIC:
6870       kind = CPTK_IS_POLYMORPHIC;
6871       break;
6872     case RID_IS_STD_LAYOUT:
6873       kind = CPTK_IS_STD_LAYOUT;
6874       break;
6875     case RID_IS_TRIVIAL:
6876       kind = CPTK_IS_TRIVIAL;
6877       break;
6878     case RID_IS_UNION:
6879       kind = CPTK_IS_UNION;
6880       break;
6881     default:
6882       gcc_unreachable ();
6883     }
6884
6885   /* Consume the token.  */
6886   cp_lexer_consume_token (parser->lexer);
6887
6888   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6889
6890   type1 = cp_parser_type_id (parser);
6891
6892   if (type1 == error_mark_node)
6893     return error_mark_node;
6894
6895   /* Build a trivial decl-specifier-seq.  */
6896   clear_decl_specs (&decl_specs);
6897   decl_specs.type = type1;
6898
6899   /* Call grokdeclarator to figure out what type this is.  */
6900   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6901                           /*initialized=*/0, /*attrlist=*/NULL);
6902
6903   if (binary)
6904     {
6905       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6906  
6907       type2 = cp_parser_type_id (parser);
6908
6909       if (type2 == error_mark_node)
6910         return error_mark_node;
6911
6912       /* Build a trivial decl-specifier-seq.  */
6913       clear_decl_specs (&decl_specs);
6914       decl_specs.type = type2;
6915
6916       /* Call grokdeclarator to figure out what type this is.  */
6917       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6918                               /*initialized=*/0, /*attrlist=*/NULL);
6919     }
6920
6921   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6922
6923   /* Complete the trait expression, which may mean either processing
6924      the trait expr now or saving it for template instantiation.  */
6925   return finish_trait_expr (kind, type1, type2);
6926 }
6927
6928 /* Statements [gram.stmt.stmt]  */
6929
6930 /* Parse a statement.
6931
6932    statement:
6933      labeled-statement
6934      expression-statement
6935      compound-statement
6936      selection-statement
6937      iteration-statement
6938      jump-statement
6939      declaration-statement
6940      try-block
6941
6942   IN_COMPOUND is true when the statement is nested inside a
6943   cp_parser_compound_statement; this matters for certain pragmas.
6944
6945   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6946   is a (possibly labeled) if statement which is not enclosed in braces
6947   and has an else clause.  This is used to implement -Wparentheses.  */
6948
6949 static void
6950 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6951                      bool in_compound, bool *if_p)
6952 {
6953   tree statement;
6954   cp_token *token;
6955   location_t statement_location;
6956
6957  restart:
6958   if (if_p != NULL)
6959     *if_p = false;
6960   /* There is no statement yet.  */
6961   statement = NULL_TREE;
6962   /* Peek at the next token.  */
6963   token = cp_lexer_peek_token (parser->lexer);
6964   /* Remember the location of the first token in the statement.  */
6965   statement_location = token->location;
6966   /* If this is a keyword, then that will often determine what kind of
6967      statement we have.  */
6968   if (token->type == CPP_KEYWORD)
6969     {
6970       enum rid keyword = token->keyword;
6971
6972       switch (keyword)
6973         {
6974         case RID_CASE:
6975         case RID_DEFAULT:
6976           /* Looks like a labeled-statement with a case label.
6977              Parse the label, and then use tail recursion to parse
6978              the statement.  */
6979           cp_parser_label_for_labeled_statement (parser);
6980           goto restart;
6981
6982         case RID_IF:
6983         case RID_SWITCH:
6984           statement = cp_parser_selection_statement (parser, if_p);
6985           break;
6986
6987         case RID_WHILE:
6988         case RID_DO:
6989         case RID_FOR:
6990           statement = cp_parser_iteration_statement (parser);
6991           break;
6992
6993         case RID_BREAK:
6994         case RID_CONTINUE:
6995         case RID_RETURN:
6996         case RID_GOTO:
6997           statement = cp_parser_jump_statement (parser);
6998           break;
6999
7000           /* Objective-C++ exception-handling constructs.  */
7001         case RID_AT_TRY:
7002         case RID_AT_CATCH:
7003         case RID_AT_FINALLY:
7004         case RID_AT_SYNCHRONIZED:
7005         case RID_AT_THROW:
7006           statement = cp_parser_objc_statement (parser);
7007           break;
7008
7009         case RID_TRY:
7010           statement = cp_parser_try_block (parser);
7011           break;
7012
7013         case RID_NAMESPACE:
7014           /* This must be a namespace alias definition.  */
7015           cp_parser_declaration_statement (parser);
7016           return;
7017           
7018         default:
7019           /* It might be a keyword like `int' that can start a
7020              declaration-statement.  */
7021           break;
7022         }
7023     }
7024   else if (token->type == CPP_NAME)
7025     {
7026       /* If the next token is a `:', then we are looking at a
7027          labeled-statement.  */
7028       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7029       if (token->type == CPP_COLON)
7030         {
7031           /* Looks like a labeled-statement with an ordinary label.
7032              Parse the label, and then use tail recursion to parse
7033              the statement.  */
7034           cp_parser_label_for_labeled_statement (parser);
7035           goto restart;
7036         }
7037     }
7038   /* Anything that starts with a `{' must be a compound-statement.  */
7039   else if (token->type == CPP_OPEN_BRACE)
7040     statement = cp_parser_compound_statement (parser, NULL, false);
7041   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7042      a statement all its own.  */
7043   else if (token->type == CPP_PRAGMA)
7044     {
7045       /* Only certain OpenMP pragmas are attached to statements, and thus
7046          are considered statements themselves.  All others are not.  In
7047          the context of a compound, accept the pragma as a "statement" and
7048          return so that we can check for a close brace.  Otherwise we
7049          require a real statement and must go back and read one.  */
7050       if (in_compound)
7051         cp_parser_pragma (parser, pragma_compound);
7052       else if (!cp_parser_pragma (parser, pragma_stmt))
7053         goto restart;
7054       return;
7055     }
7056   else if (token->type == CPP_EOF)
7057     {
7058       cp_parser_error (parser, "expected statement");
7059       return;
7060     }
7061
7062   /* Everything else must be a declaration-statement or an
7063      expression-statement.  Try for the declaration-statement
7064      first, unless we are looking at a `;', in which case we know that
7065      we have an expression-statement.  */
7066   if (!statement)
7067     {
7068       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7069         {
7070           cp_parser_parse_tentatively (parser);
7071           /* Try to parse the declaration-statement.  */
7072           cp_parser_declaration_statement (parser);
7073           /* If that worked, we're done.  */
7074           if (cp_parser_parse_definitely (parser))
7075             return;
7076         }
7077       /* Look for an expression-statement instead.  */
7078       statement = cp_parser_expression_statement (parser, in_statement_expr);
7079     }
7080
7081   /* Set the line number for the statement.  */
7082   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7083     SET_EXPR_LOCATION (statement, statement_location);
7084 }
7085
7086 /* Parse the label for a labeled-statement, i.e.
7087
7088    identifier :
7089    case constant-expression :
7090    default :
7091
7092    GNU Extension:
7093    case constant-expression ... constant-expression : statement
7094
7095    When a label is parsed without errors, the label is added to the
7096    parse tree by the finish_* functions, so this function doesn't
7097    have to return the label.  */
7098
7099 static void
7100 cp_parser_label_for_labeled_statement (cp_parser* parser)
7101 {
7102   cp_token *token;
7103   tree label = NULL_TREE;
7104
7105   /* The next token should be an identifier.  */
7106   token = cp_lexer_peek_token (parser->lexer);
7107   if (token->type != CPP_NAME
7108       && token->type != CPP_KEYWORD)
7109     {
7110       cp_parser_error (parser, "expected labeled-statement");
7111       return;
7112     }
7113
7114   switch (token->keyword)
7115     {
7116     case RID_CASE:
7117       {
7118         tree expr, expr_hi;
7119         cp_token *ellipsis;
7120
7121         /* Consume the `case' token.  */
7122         cp_lexer_consume_token (parser->lexer);
7123         /* Parse the constant-expression.  */
7124         expr = cp_parser_constant_expression (parser,
7125                                               /*allow_non_constant_p=*/false,
7126                                               NULL);
7127
7128         ellipsis = cp_lexer_peek_token (parser->lexer);
7129         if (ellipsis->type == CPP_ELLIPSIS)
7130           {
7131             /* Consume the `...' token.  */
7132             cp_lexer_consume_token (parser->lexer);
7133             expr_hi =
7134               cp_parser_constant_expression (parser,
7135                                              /*allow_non_constant_p=*/false,
7136                                              NULL);
7137             /* We don't need to emit warnings here, as the common code
7138                will do this for us.  */
7139           }
7140         else
7141           expr_hi = NULL_TREE;
7142
7143         if (parser->in_switch_statement_p)
7144           finish_case_label (token->location, expr, expr_hi);
7145         else
7146           error_at (token->location,
7147                     "case label %qE not within a switch statement",
7148                     expr);
7149       }
7150       break;
7151
7152     case RID_DEFAULT:
7153       /* Consume the `default' token.  */
7154       cp_lexer_consume_token (parser->lexer);
7155
7156       if (parser->in_switch_statement_p)
7157         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7158       else
7159         error_at (token->location, "case label not within a switch statement");
7160       break;
7161
7162     default:
7163       /* Anything else must be an ordinary label.  */
7164       label = finish_label_stmt (cp_parser_identifier (parser));
7165       break;
7166     }
7167
7168   /* Require the `:' token.  */
7169   cp_parser_require (parser, CPP_COLON, "%<:%>");
7170
7171   /* An ordinary label may optionally be followed by attributes.
7172      However, this is only permitted if the attributes are then
7173      followed by a semicolon.  This is because, for backward
7174      compatibility, when parsing
7175        lab: __attribute__ ((unused)) int i;
7176      we want the attribute to attach to "i", not "lab".  */
7177   if (label != NULL_TREE
7178       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7179     {
7180       tree attrs;
7181
7182       cp_parser_parse_tentatively (parser);
7183       attrs = cp_parser_attributes_opt (parser);
7184       if (attrs == NULL_TREE
7185           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7186         cp_parser_abort_tentative_parse (parser);
7187       else if (!cp_parser_parse_definitely (parser))
7188         ;
7189       else
7190         cplus_decl_attributes (&label, attrs, 0);
7191     }
7192 }
7193
7194 /* Parse an expression-statement.
7195
7196    expression-statement:
7197      expression [opt] ;
7198
7199    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7200    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7201    indicates whether this expression-statement is part of an
7202    expression statement.  */
7203
7204 static tree
7205 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7206 {
7207   tree statement = NULL_TREE;
7208
7209   /* If the next token is a ';', then there is no expression
7210      statement.  */
7211   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7212     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7213
7214   /* Consume the final `;'.  */
7215   cp_parser_consume_semicolon_at_end_of_statement (parser);
7216
7217   if (in_statement_expr
7218       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7219     /* This is the final expression statement of a statement
7220        expression.  */
7221     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7222   else if (statement)
7223     statement = finish_expr_stmt (statement);
7224   else
7225     finish_stmt ();
7226
7227   return statement;
7228 }
7229
7230 /* Parse a compound-statement.
7231
7232    compound-statement:
7233      { statement-seq [opt] }
7234
7235    GNU extension:
7236
7237    compound-statement:
7238      { label-declaration-seq [opt] statement-seq [opt] }
7239
7240    label-declaration-seq:
7241      label-declaration
7242      label-declaration-seq label-declaration
7243
7244    Returns a tree representing the statement.  */
7245
7246 static tree
7247 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7248                               bool in_try)
7249 {
7250   tree compound_stmt;
7251
7252   /* Consume the `{'.  */
7253   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7254     return error_mark_node;
7255   /* Begin the compound-statement.  */
7256   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7257   /* If the next keyword is `__label__' we have a label declaration.  */
7258   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7259     cp_parser_label_declaration (parser);
7260   /* Parse an (optional) statement-seq.  */
7261   cp_parser_statement_seq_opt (parser, in_statement_expr);
7262   /* Finish the compound-statement.  */
7263   finish_compound_stmt (compound_stmt);
7264   /* Consume the `}'.  */
7265   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7266
7267   return compound_stmt;
7268 }
7269
7270 /* Parse an (optional) statement-seq.
7271
7272    statement-seq:
7273      statement
7274      statement-seq [opt] statement  */
7275
7276 static void
7277 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7278 {
7279   /* Scan statements until there aren't any more.  */
7280   while (true)
7281     {
7282       cp_token *token = cp_lexer_peek_token (parser->lexer);
7283
7284       /* If we're looking at a `}', then we've run out of statements.  */
7285       if (token->type == CPP_CLOSE_BRACE
7286           || token->type == CPP_EOF
7287           || token->type == CPP_PRAGMA_EOL)
7288         break;
7289       
7290       /* If we are in a compound statement and find 'else' then
7291          something went wrong.  */
7292       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7293         {
7294           if (parser->in_statement & IN_IF_STMT) 
7295             break;
7296           else
7297             {
7298               token = cp_lexer_consume_token (parser->lexer);
7299               error_at (token->location, "%<else%> without a previous %<if%>");
7300             }
7301         }
7302
7303       /* Parse the statement.  */
7304       cp_parser_statement (parser, in_statement_expr, true, NULL);
7305     }
7306 }
7307
7308 /* Parse a selection-statement.
7309
7310    selection-statement:
7311      if ( condition ) statement
7312      if ( condition ) statement else statement
7313      switch ( condition ) statement
7314
7315    Returns the new IF_STMT or SWITCH_STMT.
7316
7317    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7318    is a (possibly labeled) if statement which is not enclosed in
7319    braces and has an else clause.  This is used to implement
7320    -Wparentheses.  */
7321
7322 static tree
7323 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7324 {
7325   cp_token *token;
7326   enum rid keyword;
7327
7328   if (if_p != NULL)
7329     *if_p = false;
7330
7331   /* Peek at the next token.  */
7332   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7333
7334   /* See what kind of keyword it is.  */
7335   keyword = token->keyword;
7336   switch (keyword)
7337     {
7338     case RID_IF:
7339     case RID_SWITCH:
7340       {
7341         tree statement;
7342         tree condition;
7343
7344         /* Look for the `('.  */
7345         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7346           {
7347             cp_parser_skip_to_end_of_statement (parser);
7348             return error_mark_node;
7349           }
7350
7351         /* Begin the selection-statement.  */
7352         if (keyword == RID_IF)
7353           statement = begin_if_stmt ();
7354         else
7355           statement = begin_switch_stmt ();
7356
7357         /* Parse the condition.  */
7358         condition = cp_parser_condition (parser);
7359         /* Look for the `)'.  */
7360         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7361           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7362                                                  /*consume_paren=*/true);
7363
7364         if (keyword == RID_IF)
7365           {
7366             bool nested_if;
7367             unsigned char in_statement;
7368
7369             /* Add the condition.  */
7370             finish_if_stmt_cond (condition, statement);
7371
7372             /* Parse the then-clause.  */
7373             in_statement = parser->in_statement;
7374             parser->in_statement |= IN_IF_STMT;
7375             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7376               {
7377                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7378                 add_stmt (build_empty_stmt (loc));
7379                 cp_lexer_consume_token (parser->lexer);
7380                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7381                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7382                               "empty body in an %<if%> statement");
7383                 nested_if = false;
7384               }
7385             else
7386               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7387             parser->in_statement = in_statement;
7388
7389             finish_then_clause (statement);
7390
7391             /* If the next token is `else', parse the else-clause.  */
7392             if (cp_lexer_next_token_is_keyword (parser->lexer,
7393                                                 RID_ELSE))
7394               {
7395                 /* Consume the `else' keyword.  */
7396                 cp_lexer_consume_token (parser->lexer);
7397                 begin_else_clause (statement);
7398                 /* Parse the else-clause.  */
7399                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7400                   {
7401                     location_t loc;
7402                     loc = cp_lexer_peek_token (parser->lexer)->location;
7403                     warning_at (loc,
7404                                 OPT_Wempty_body, "suggest braces around "
7405                                 "empty body in an %<else%> statement");
7406                     add_stmt (build_empty_stmt (loc));
7407                     cp_lexer_consume_token (parser->lexer);
7408                   }
7409                 else
7410                   cp_parser_implicitly_scoped_statement (parser, NULL);
7411
7412                 finish_else_clause (statement);
7413
7414                 /* If we are currently parsing a then-clause, then
7415                    IF_P will not be NULL.  We set it to true to
7416                    indicate that this if statement has an else clause.
7417                    This may trigger the Wparentheses warning below
7418                    when we get back up to the parent if statement.  */
7419                 if (if_p != NULL)
7420                   *if_p = true;
7421               }
7422             else
7423               {
7424                 /* This if statement does not have an else clause.  If
7425                    NESTED_IF is true, then the then-clause is an if
7426                    statement which does have an else clause.  We warn
7427                    about the potential ambiguity.  */
7428                 if (nested_if)
7429                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7430                               "suggest explicit braces to avoid ambiguous"
7431                               " %<else%>");
7432               }
7433
7434             /* Now we're all done with the if-statement.  */
7435             finish_if_stmt (statement);
7436           }
7437         else
7438           {
7439             bool in_switch_statement_p;
7440             unsigned char in_statement;
7441
7442             /* Add the condition.  */
7443             finish_switch_cond (condition, statement);
7444
7445             /* Parse the body of the switch-statement.  */
7446             in_switch_statement_p = parser->in_switch_statement_p;
7447             in_statement = parser->in_statement;
7448             parser->in_switch_statement_p = true;
7449             parser->in_statement |= IN_SWITCH_STMT;
7450             cp_parser_implicitly_scoped_statement (parser, NULL);
7451             parser->in_switch_statement_p = in_switch_statement_p;
7452             parser->in_statement = in_statement;
7453
7454             /* Now we're all done with the switch-statement.  */
7455             finish_switch_stmt (statement);
7456           }
7457
7458         return statement;
7459       }
7460       break;
7461
7462     default:
7463       cp_parser_error (parser, "expected selection-statement");
7464       return error_mark_node;
7465     }
7466 }
7467
7468 /* Parse a condition.
7469
7470    condition:
7471      expression
7472      type-specifier-seq declarator = initializer-clause
7473      type-specifier-seq declarator braced-init-list
7474
7475    GNU Extension:
7476
7477    condition:
7478      type-specifier-seq declarator asm-specification [opt]
7479        attributes [opt] = assignment-expression
7480
7481    Returns the expression that should be tested.  */
7482
7483 static tree
7484 cp_parser_condition (cp_parser* parser)
7485 {
7486   cp_decl_specifier_seq type_specifiers;
7487   const char *saved_message;
7488
7489   /* Try the declaration first.  */
7490   cp_parser_parse_tentatively (parser);
7491   /* New types are not allowed in the type-specifier-seq for a
7492      condition.  */
7493   saved_message = parser->type_definition_forbidden_message;
7494   parser->type_definition_forbidden_message
7495     = "types may not be defined in conditions";
7496   /* Parse the type-specifier-seq.  */
7497   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7498                                 &type_specifiers);
7499   /* Restore the saved message.  */
7500   parser->type_definition_forbidden_message = saved_message;
7501   /* If all is well, we might be looking at a declaration.  */
7502   if (!cp_parser_error_occurred (parser))
7503     {
7504       tree decl;
7505       tree asm_specification;
7506       tree attributes;
7507       cp_declarator *declarator;
7508       tree initializer = NULL_TREE;
7509
7510       /* Parse the declarator.  */
7511       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7512                                          /*ctor_dtor_or_conv_p=*/NULL,
7513                                          /*parenthesized_p=*/NULL,
7514                                          /*member_p=*/false);
7515       /* Parse the attributes.  */
7516       attributes = cp_parser_attributes_opt (parser);
7517       /* Parse the asm-specification.  */
7518       asm_specification = cp_parser_asm_specification_opt (parser);
7519       /* If the next token is not an `=' or '{', then we might still be
7520          looking at an expression.  For example:
7521
7522            if (A(a).x)
7523
7524          looks like a decl-specifier-seq and a declarator -- but then
7525          there is no `=', so this is an expression.  */
7526       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7527           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7528         cp_parser_simulate_error (parser);
7529         
7530       /* If we did see an `=' or '{', then we are looking at a declaration
7531          for sure.  */
7532       if (cp_parser_parse_definitely (parser))
7533         {
7534           tree pushed_scope;
7535           bool non_constant_p;
7536           bool flags = LOOKUP_ONLYCONVERTING;
7537
7538           /* Create the declaration.  */
7539           decl = start_decl (declarator, &type_specifiers,
7540                              /*initialized_p=*/true,
7541                              attributes, /*prefix_attributes=*/NULL_TREE,
7542                              &pushed_scope);
7543
7544           /* Parse the initializer.  */
7545           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7546             {
7547               initializer = cp_parser_braced_list (parser, &non_constant_p);
7548               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7549               flags = 0;
7550             }
7551           else
7552             {
7553               /* Consume the `='.  */
7554               cp_parser_require (parser, CPP_EQ, "%<=%>");
7555               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7556             }
7557           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7558             maybe_warn_cpp0x ("extended initializer lists");
7559
7560           if (!non_constant_p)
7561             initializer = fold_non_dependent_expr (initializer);
7562
7563           /* Process the initializer.  */
7564           cp_finish_decl (decl,
7565                           initializer, !non_constant_p,
7566                           asm_specification,
7567                           flags);
7568
7569           if (pushed_scope)
7570             pop_scope (pushed_scope);
7571
7572           return convert_from_reference (decl);
7573         }
7574     }
7575   /* If we didn't even get past the declarator successfully, we are
7576      definitely not looking at a declaration.  */
7577   else
7578     cp_parser_abort_tentative_parse (parser);
7579
7580   /* Otherwise, we are looking at an expression.  */
7581   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7582 }
7583
7584 /* Parse an iteration-statement.
7585
7586    iteration-statement:
7587      while ( condition ) statement
7588      do statement while ( expression ) ;
7589      for ( for-init-statement condition [opt] ; expression [opt] )
7590        statement
7591
7592    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7593
7594 static tree
7595 cp_parser_iteration_statement (cp_parser* parser)
7596 {
7597   cp_token *token;
7598   enum rid keyword;
7599   tree statement;
7600   unsigned char in_statement;
7601
7602   /* Peek at the next token.  */
7603   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7604   if (!token)
7605     return error_mark_node;
7606
7607   /* Remember whether or not we are already within an iteration
7608      statement.  */
7609   in_statement = parser->in_statement;
7610
7611   /* See what kind of keyword it is.  */
7612   keyword = token->keyword;
7613   switch (keyword)
7614     {
7615     case RID_WHILE:
7616       {
7617         tree condition;
7618
7619         /* Begin the while-statement.  */
7620         statement = begin_while_stmt ();
7621         /* Look for the `('.  */
7622         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7623         /* Parse the condition.  */
7624         condition = cp_parser_condition (parser);
7625         finish_while_stmt_cond (condition, statement);
7626         /* Look for the `)'.  */
7627         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7628         /* Parse the dependent statement.  */
7629         parser->in_statement = IN_ITERATION_STMT;
7630         cp_parser_already_scoped_statement (parser);
7631         parser->in_statement = in_statement;
7632         /* We're done with the while-statement.  */
7633         finish_while_stmt (statement);
7634       }
7635       break;
7636
7637     case RID_DO:
7638       {
7639         tree expression;
7640
7641         /* Begin the do-statement.  */
7642         statement = begin_do_stmt ();
7643         /* Parse the body of the do-statement.  */
7644         parser->in_statement = IN_ITERATION_STMT;
7645         cp_parser_implicitly_scoped_statement (parser, NULL);
7646         parser->in_statement = in_statement;
7647         finish_do_body (statement);
7648         /* Look for the `while' keyword.  */
7649         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7650         /* Look for the `('.  */
7651         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7652         /* Parse the expression.  */
7653         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7654         /* We're done with the do-statement.  */
7655         finish_do_stmt (expression, statement);
7656         /* Look for the `)'.  */
7657         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7658         /* Look for the `;'.  */
7659         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7660       }
7661       break;
7662
7663     case RID_FOR:
7664       {
7665         tree condition = NULL_TREE;
7666         tree expression = NULL_TREE;
7667
7668         /* Begin the for-statement.  */
7669         statement = begin_for_stmt ();
7670         /* Look for the `('.  */
7671         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7672         /* Parse the initialization.  */
7673         cp_parser_for_init_statement (parser);
7674         finish_for_init_stmt (statement);
7675
7676         /* If there's a condition, process it.  */
7677         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7678           condition = cp_parser_condition (parser);
7679         finish_for_cond (condition, statement);
7680         /* Look for the `;'.  */
7681         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7682
7683         /* If there's an expression, process it.  */
7684         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7685           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7686         finish_for_expr (expression, statement);
7687         /* Look for the `)'.  */
7688         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7689
7690         /* Parse the body of the for-statement.  */
7691         parser->in_statement = IN_ITERATION_STMT;
7692         cp_parser_already_scoped_statement (parser);
7693         parser->in_statement = in_statement;
7694
7695         /* We're done with the for-statement.  */
7696         finish_for_stmt (statement);
7697       }
7698       break;
7699
7700     default:
7701       cp_parser_error (parser, "expected iteration-statement");
7702       statement = error_mark_node;
7703       break;
7704     }
7705
7706   return statement;
7707 }
7708
7709 /* Parse a for-init-statement.
7710
7711    for-init-statement:
7712      expression-statement
7713      simple-declaration  */
7714
7715 static void
7716 cp_parser_for_init_statement (cp_parser* parser)
7717 {
7718   /* If the next token is a `;', then we have an empty
7719      expression-statement.  Grammatically, this is also a
7720      simple-declaration, but an invalid one, because it does not
7721      declare anything.  Therefore, if we did not handle this case
7722      specially, we would issue an error message about an invalid
7723      declaration.  */
7724   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7725     {
7726       /* We're going to speculatively look for a declaration, falling back
7727          to an expression, if necessary.  */
7728       cp_parser_parse_tentatively (parser);
7729       /* Parse the declaration.  */
7730       cp_parser_simple_declaration (parser,
7731                                     /*function_definition_allowed_p=*/false);
7732       /* If the tentative parse failed, then we shall need to look for an
7733          expression-statement.  */
7734       if (cp_parser_parse_definitely (parser))
7735         return;
7736     }
7737
7738   cp_parser_expression_statement (parser, false);
7739 }
7740
7741 /* Parse a jump-statement.
7742
7743    jump-statement:
7744      break ;
7745      continue ;
7746      return expression [opt] ;
7747      return braced-init-list ;
7748      goto identifier ;
7749
7750    GNU extension:
7751
7752    jump-statement:
7753      goto * expression ;
7754
7755    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7756
7757 static tree
7758 cp_parser_jump_statement (cp_parser* parser)
7759 {
7760   tree statement = error_mark_node;
7761   cp_token *token;
7762   enum rid keyword;
7763   unsigned char in_statement;
7764
7765   /* Peek at the next token.  */
7766   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7767   if (!token)
7768     return error_mark_node;
7769
7770   /* See what kind of keyword it is.  */
7771   keyword = token->keyword;
7772   switch (keyword)
7773     {
7774     case RID_BREAK:
7775       in_statement = parser->in_statement & ~IN_IF_STMT;      
7776       switch (in_statement)
7777         {
7778         case 0:
7779           error_at (token->location, "break statement not within loop or switch");
7780           break;
7781         default:
7782           gcc_assert ((in_statement & IN_SWITCH_STMT)
7783                       || in_statement == IN_ITERATION_STMT);
7784           statement = finish_break_stmt ();
7785           break;
7786         case IN_OMP_BLOCK:
7787           error_at (token->location, "invalid exit from OpenMP structured block");
7788           break;
7789         case IN_OMP_FOR:
7790           error_at (token->location, "break statement used with OpenMP for loop");
7791           break;
7792         }
7793       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7794       break;
7795
7796     case RID_CONTINUE:
7797       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7798         {
7799         case 0:
7800           error_at (token->location, "continue statement not within a loop");
7801           break;
7802         case IN_ITERATION_STMT:
7803         case IN_OMP_FOR:
7804           statement = finish_continue_stmt ();
7805           break;
7806         case IN_OMP_BLOCK:
7807           error_at (token->location, "invalid exit from OpenMP structured block");
7808           break;
7809         default:
7810           gcc_unreachable ();
7811         }
7812       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7813       break;
7814
7815     case RID_RETURN:
7816       {
7817         tree expr;
7818         bool expr_non_constant_p;
7819
7820         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7821           {
7822             maybe_warn_cpp0x ("extended initializer lists");
7823             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7824           }
7825         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7826           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7827         else
7828           /* If the next token is a `;', then there is no
7829              expression.  */
7830           expr = NULL_TREE;
7831         /* Build the return-statement.  */
7832         statement = finish_return_stmt (expr);
7833         /* Look for the final `;'.  */
7834         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7835       }
7836       break;
7837
7838     case RID_GOTO:
7839       /* Create the goto-statement.  */
7840       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7841         {
7842           /* Issue a warning about this use of a GNU extension.  */
7843           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7844           /* Consume the '*' token.  */
7845           cp_lexer_consume_token (parser->lexer);
7846           /* Parse the dependent expression.  */
7847           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7848         }
7849       else
7850         finish_goto_stmt (cp_parser_identifier (parser));
7851       /* Look for the final `;'.  */
7852       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7853       break;
7854
7855     default:
7856       cp_parser_error (parser, "expected jump-statement");
7857       break;
7858     }
7859
7860   return statement;
7861 }
7862
7863 /* Parse a declaration-statement.
7864
7865    declaration-statement:
7866      block-declaration  */
7867
7868 static void
7869 cp_parser_declaration_statement (cp_parser* parser)
7870 {
7871   void *p;
7872
7873   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7874   p = obstack_alloc (&declarator_obstack, 0);
7875
7876  /* Parse the block-declaration.  */
7877   cp_parser_block_declaration (parser, /*statement_p=*/true);
7878
7879   /* Free any declarators allocated.  */
7880   obstack_free (&declarator_obstack, p);
7881
7882   /* Finish off the statement.  */
7883   finish_stmt ();
7884 }
7885
7886 /* Some dependent statements (like `if (cond) statement'), are
7887    implicitly in their own scope.  In other words, if the statement is
7888    a single statement (as opposed to a compound-statement), it is
7889    none-the-less treated as if it were enclosed in braces.  Any
7890    declarations appearing in the dependent statement are out of scope
7891    after control passes that point.  This function parses a statement,
7892    but ensures that is in its own scope, even if it is not a
7893    compound-statement.
7894
7895    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7896    is a (possibly labeled) if statement which is not enclosed in
7897    braces and has an else clause.  This is used to implement
7898    -Wparentheses.
7899
7900    Returns the new statement.  */
7901
7902 static tree
7903 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7904 {
7905   tree statement;
7906
7907   if (if_p != NULL)
7908     *if_p = false;
7909
7910   /* Mark if () ; with a special NOP_EXPR.  */
7911   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7912     {
7913       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7914       cp_lexer_consume_token (parser->lexer);
7915       statement = add_stmt (build_empty_stmt (loc));
7916     }
7917   /* if a compound is opened, we simply parse the statement directly.  */
7918   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7919     statement = cp_parser_compound_statement (parser, NULL, false);
7920   /* If the token is not a `{', then we must take special action.  */
7921   else
7922     {
7923       /* Create a compound-statement.  */
7924       statement = begin_compound_stmt (0);
7925       /* Parse the dependent-statement.  */
7926       cp_parser_statement (parser, NULL_TREE, false, if_p);
7927       /* Finish the dummy compound-statement.  */
7928       finish_compound_stmt (statement);
7929     }
7930
7931   /* Return the statement.  */
7932   return statement;
7933 }
7934
7935 /* For some dependent statements (like `while (cond) statement'), we
7936    have already created a scope.  Therefore, even if the dependent
7937    statement is a compound-statement, we do not want to create another
7938    scope.  */
7939
7940 static void
7941 cp_parser_already_scoped_statement (cp_parser* parser)
7942 {
7943   /* If the token is a `{', then we must take special action.  */
7944   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7945     cp_parser_statement (parser, NULL_TREE, false, NULL);
7946   else
7947     {
7948       /* Avoid calling cp_parser_compound_statement, so that we
7949          don't create a new scope.  Do everything else by hand.  */
7950       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7951       /* If the next keyword is `__label__' we have a label declaration.  */
7952       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7953         cp_parser_label_declaration (parser);
7954       /* Parse an (optional) statement-seq.  */
7955       cp_parser_statement_seq_opt (parser, NULL_TREE);
7956       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7957     }
7958 }
7959
7960 /* Declarations [gram.dcl.dcl] */
7961
7962 /* Parse an optional declaration-sequence.
7963
7964    declaration-seq:
7965      declaration
7966      declaration-seq declaration  */
7967
7968 static void
7969 cp_parser_declaration_seq_opt (cp_parser* parser)
7970 {
7971   while (true)
7972     {
7973       cp_token *token;
7974
7975       token = cp_lexer_peek_token (parser->lexer);
7976
7977       if (token->type == CPP_CLOSE_BRACE
7978           || token->type == CPP_EOF
7979           || token->type == CPP_PRAGMA_EOL)
7980         break;
7981
7982       if (token->type == CPP_SEMICOLON)
7983         {
7984           /* A declaration consisting of a single semicolon is
7985              invalid.  Allow it unless we're being pedantic.  */
7986           cp_lexer_consume_token (parser->lexer);
7987           if (!in_system_header)
7988             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7989           continue;
7990         }
7991
7992       /* If we're entering or exiting a region that's implicitly
7993          extern "C", modify the lang context appropriately.  */
7994       if (!parser->implicit_extern_c && token->implicit_extern_c)
7995         {
7996           push_lang_context (lang_name_c);
7997           parser->implicit_extern_c = true;
7998         }
7999       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8000         {
8001           pop_lang_context ();
8002           parser->implicit_extern_c = false;
8003         }
8004
8005       if (token->type == CPP_PRAGMA)
8006         {
8007           /* A top-level declaration can consist solely of a #pragma.
8008              A nested declaration cannot, so this is done here and not
8009              in cp_parser_declaration.  (A #pragma at block scope is
8010              handled in cp_parser_statement.)  */
8011           cp_parser_pragma (parser, pragma_external);
8012           continue;
8013         }
8014
8015       /* Parse the declaration itself.  */
8016       cp_parser_declaration (parser);
8017     }
8018 }
8019
8020 /* Parse a declaration.
8021
8022    declaration:
8023      block-declaration
8024      function-definition
8025      template-declaration
8026      explicit-instantiation
8027      explicit-specialization
8028      linkage-specification
8029      namespace-definition
8030
8031    GNU extension:
8032
8033    declaration:
8034       __extension__ declaration */
8035
8036 static void
8037 cp_parser_declaration (cp_parser* parser)
8038 {
8039   cp_token token1;
8040   cp_token token2;
8041   int saved_pedantic;
8042   void *p;
8043
8044   /* Check for the `__extension__' keyword.  */
8045   if (cp_parser_extension_opt (parser, &saved_pedantic))
8046     {
8047       /* Parse the qualified declaration.  */
8048       cp_parser_declaration (parser);
8049       /* Restore the PEDANTIC flag.  */
8050       pedantic = saved_pedantic;
8051
8052       return;
8053     }
8054
8055   /* Try to figure out what kind of declaration is present.  */
8056   token1 = *cp_lexer_peek_token (parser->lexer);
8057
8058   if (token1.type != CPP_EOF)
8059     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8060   else
8061     {
8062       token2.type = CPP_EOF;
8063       token2.keyword = RID_MAX;
8064     }
8065
8066   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8067   p = obstack_alloc (&declarator_obstack, 0);
8068
8069   /* If the next token is `extern' and the following token is a string
8070      literal, then we have a linkage specification.  */
8071   if (token1.keyword == RID_EXTERN
8072       && cp_parser_is_string_literal (&token2))
8073     cp_parser_linkage_specification (parser);
8074   /* If the next token is `template', then we have either a template
8075      declaration, an explicit instantiation, or an explicit
8076      specialization.  */
8077   else if (token1.keyword == RID_TEMPLATE)
8078     {
8079       /* `template <>' indicates a template specialization.  */
8080       if (token2.type == CPP_LESS
8081           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8082         cp_parser_explicit_specialization (parser);
8083       /* `template <' indicates a template declaration.  */
8084       else if (token2.type == CPP_LESS)
8085         cp_parser_template_declaration (parser, /*member_p=*/false);
8086       /* Anything else must be an explicit instantiation.  */
8087       else
8088         cp_parser_explicit_instantiation (parser);
8089     }
8090   /* If the next token is `export', then we have a template
8091      declaration.  */
8092   else if (token1.keyword == RID_EXPORT)
8093     cp_parser_template_declaration (parser, /*member_p=*/false);
8094   /* If the next token is `extern', 'static' or 'inline' and the one
8095      after that is `template', we have a GNU extended explicit
8096      instantiation directive.  */
8097   else if (cp_parser_allow_gnu_extensions_p (parser)
8098            && (token1.keyword == RID_EXTERN
8099                || token1.keyword == RID_STATIC
8100                || token1.keyword == RID_INLINE)
8101            && token2.keyword == RID_TEMPLATE)
8102     cp_parser_explicit_instantiation (parser);
8103   /* If the next token is `namespace', check for a named or unnamed
8104      namespace definition.  */
8105   else if (token1.keyword == RID_NAMESPACE
8106            && (/* A named namespace definition.  */
8107                (token2.type == CPP_NAME
8108                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8109                     != CPP_EQ))
8110                /* An unnamed namespace definition.  */
8111                || token2.type == CPP_OPEN_BRACE
8112                || token2.keyword == RID_ATTRIBUTE))
8113     cp_parser_namespace_definition (parser);
8114   /* An inline (associated) namespace definition.  */
8115   else if (token1.keyword == RID_INLINE
8116            && token2.keyword == RID_NAMESPACE)
8117     cp_parser_namespace_definition (parser);
8118   /* Objective-C++ declaration/definition.  */
8119   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8120     cp_parser_objc_declaration (parser);
8121   /* We must have either a block declaration or a function
8122      definition.  */
8123   else
8124     /* Try to parse a block-declaration, or a function-definition.  */
8125     cp_parser_block_declaration (parser, /*statement_p=*/false);
8126
8127   /* Free any declarators allocated.  */
8128   obstack_free (&declarator_obstack, p);
8129 }
8130
8131 /* Parse a block-declaration.
8132
8133    block-declaration:
8134      simple-declaration
8135      asm-definition
8136      namespace-alias-definition
8137      using-declaration
8138      using-directive
8139
8140    GNU Extension:
8141
8142    block-declaration:
8143      __extension__ block-declaration
8144
8145    C++0x Extension:
8146
8147    block-declaration:
8148      static_assert-declaration
8149
8150    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8151    part of a declaration-statement.  */
8152
8153 static void
8154 cp_parser_block_declaration (cp_parser *parser,
8155                              bool      statement_p)
8156 {
8157   cp_token *token1;
8158   int saved_pedantic;
8159
8160   /* Check for the `__extension__' keyword.  */
8161   if (cp_parser_extension_opt (parser, &saved_pedantic))
8162     {
8163       /* Parse the qualified declaration.  */
8164       cp_parser_block_declaration (parser, statement_p);
8165       /* Restore the PEDANTIC flag.  */
8166       pedantic = saved_pedantic;
8167
8168       return;
8169     }
8170
8171   /* Peek at the next token to figure out which kind of declaration is
8172      present.  */
8173   token1 = cp_lexer_peek_token (parser->lexer);
8174
8175   /* If the next keyword is `asm', we have an asm-definition.  */
8176   if (token1->keyword == RID_ASM)
8177     {
8178       if (statement_p)
8179         cp_parser_commit_to_tentative_parse (parser);
8180       cp_parser_asm_definition (parser);
8181     }
8182   /* If the next keyword is `namespace', we have a
8183      namespace-alias-definition.  */
8184   else if (token1->keyword == RID_NAMESPACE)
8185     cp_parser_namespace_alias_definition (parser);
8186   /* If the next keyword is `using', we have either a
8187      using-declaration or a using-directive.  */
8188   else if (token1->keyword == RID_USING)
8189     {
8190       cp_token *token2;
8191
8192       if (statement_p)
8193         cp_parser_commit_to_tentative_parse (parser);
8194       /* If the token after `using' is `namespace', then we have a
8195          using-directive.  */
8196       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8197       if (token2->keyword == RID_NAMESPACE)
8198         cp_parser_using_directive (parser);
8199       /* Otherwise, it's a using-declaration.  */
8200       else
8201         cp_parser_using_declaration (parser,
8202                                      /*access_declaration_p=*/false);
8203     }
8204   /* If the next keyword is `__label__' we have a misplaced label
8205      declaration.  */
8206   else if (token1->keyword == RID_LABEL)
8207     {
8208       cp_lexer_consume_token (parser->lexer);
8209       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8210       cp_parser_skip_to_end_of_statement (parser);
8211       /* If the next token is now a `;', consume it.  */
8212       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8213         cp_lexer_consume_token (parser->lexer);
8214     }
8215   /* If the next token is `static_assert' we have a static assertion.  */
8216   else if (token1->keyword == RID_STATIC_ASSERT)
8217     cp_parser_static_assert (parser, /*member_p=*/false);
8218   /* Anything else must be a simple-declaration.  */
8219   else
8220     cp_parser_simple_declaration (parser, !statement_p);
8221 }
8222
8223 /* Parse a simple-declaration.
8224
8225    simple-declaration:
8226      decl-specifier-seq [opt] init-declarator-list [opt] ;
8227
8228    init-declarator-list:
8229      init-declarator
8230      init-declarator-list , init-declarator
8231
8232    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8233    function-definition as a simple-declaration.  */
8234
8235 static void
8236 cp_parser_simple_declaration (cp_parser* parser,
8237                               bool function_definition_allowed_p)
8238 {
8239   cp_decl_specifier_seq decl_specifiers;
8240   int declares_class_or_enum;
8241   bool saw_declarator;
8242
8243   /* Defer access checks until we know what is being declared; the
8244      checks for names appearing in the decl-specifier-seq should be
8245      done as if we were in the scope of the thing being declared.  */
8246   push_deferring_access_checks (dk_deferred);
8247
8248   /* Parse the decl-specifier-seq.  We have to keep track of whether
8249      or not the decl-specifier-seq declares a named class or
8250      enumeration type, since that is the only case in which the
8251      init-declarator-list is allowed to be empty.
8252
8253      [dcl.dcl]
8254
8255      In a simple-declaration, the optional init-declarator-list can be
8256      omitted only when declaring a class or enumeration, that is when
8257      the decl-specifier-seq contains either a class-specifier, an
8258      elaborated-type-specifier, or an enum-specifier.  */
8259   cp_parser_decl_specifier_seq (parser,
8260                                 CP_PARSER_FLAGS_OPTIONAL,
8261                                 &decl_specifiers,
8262                                 &declares_class_or_enum);
8263   /* We no longer need to defer access checks.  */
8264   stop_deferring_access_checks ();
8265
8266   /* In a block scope, a valid declaration must always have a
8267      decl-specifier-seq.  By not trying to parse declarators, we can
8268      resolve the declaration/expression ambiguity more quickly.  */
8269   if (!function_definition_allowed_p
8270       && !decl_specifiers.any_specifiers_p)
8271     {
8272       cp_parser_error (parser, "expected declaration");
8273       goto done;
8274     }
8275
8276   /* If the next two tokens are both identifiers, the code is
8277      erroneous. The usual cause of this situation is code like:
8278
8279        T t;
8280
8281      where "T" should name a type -- but does not.  */
8282   if (!decl_specifiers.type
8283       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8284     {
8285       /* If parsing tentatively, we should commit; we really are
8286          looking at a declaration.  */
8287       cp_parser_commit_to_tentative_parse (parser);
8288       /* Give up.  */
8289       goto done;
8290     }
8291
8292   /* If we have seen at least one decl-specifier, and the next token
8293      is not a parenthesis, then we must be looking at a declaration.
8294      (After "int (" we might be looking at a functional cast.)  */
8295   if (decl_specifiers.any_specifiers_p
8296       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8297       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8298       && !cp_parser_error_occurred (parser))
8299     cp_parser_commit_to_tentative_parse (parser);
8300
8301   /* Keep going until we hit the `;' at the end of the simple
8302      declaration.  */
8303   saw_declarator = false;
8304   while (cp_lexer_next_token_is_not (parser->lexer,
8305                                      CPP_SEMICOLON))
8306     {
8307       cp_token *token;
8308       bool function_definition_p;
8309       tree decl;
8310
8311       if (saw_declarator)
8312         {
8313           /* If we are processing next declarator, coma is expected */
8314           token = cp_lexer_peek_token (parser->lexer);
8315           gcc_assert (token->type == CPP_COMMA);
8316           cp_lexer_consume_token (parser->lexer);
8317         }
8318       else
8319         saw_declarator = true;
8320
8321       /* Parse the init-declarator.  */
8322       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8323                                         /*checks=*/NULL,
8324                                         function_definition_allowed_p,
8325                                         /*member_p=*/false,
8326                                         declares_class_or_enum,
8327                                         &function_definition_p);
8328       /* If an error occurred while parsing tentatively, exit quickly.
8329          (That usually happens when in the body of a function; each
8330          statement is treated as a declaration-statement until proven
8331          otherwise.)  */
8332       if (cp_parser_error_occurred (parser))
8333         goto done;
8334       /* Handle function definitions specially.  */
8335       if (function_definition_p)
8336         {
8337           /* If the next token is a `,', then we are probably
8338              processing something like:
8339
8340                void f() {}, *p;
8341
8342              which is erroneous.  */
8343           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8344             {
8345               cp_token *token = cp_lexer_peek_token (parser->lexer);
8346               error_at (token->location,
8347                         "mixing"
8348                         " declarations and function-definitions is forbidden");
8349             }
8350           /* Otherwise, we're done with the list of declarators.  */
8351           else
8352             {
8353               pop_deferring_access_checks ();
8354               return;
8355             }
8356         }
8357       /* The next token should be either a `,' or a `;'.  */
8358       token = cp_lexer_peek_token (parser->lexer);
8359       /* If it's a `,', there are more declarators to come.  */
8360       if (token->type == CPP_COMMA)
8361         /* will be consumed next time around */;
8362       /* If it's a `;', we are done.  */
8363       else if (token->type == CPP_SEMICOLON)
8364         break;
8365       /* Anything else is an error.  */
8366       else
8367         {
8368           /* If we have already issued an error message we don't need
8369              to issue another one.  */
8370           if (decl != error_mark_node
8371               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8372             cp_parser_error (parser, "expected %<,%> or %<;%>");
8373           /* Skip tokens until we reach the end of the statement.  */
8374           cp_parser_skip_to_end_of_statement (parser);
8375           /* If the next token is now a `;', consume it.  */
8376           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8377             cp_lexer_consume_token (parser->lexer);
8378           goto done;
8379         }
8380       /* After the first time around, a function-definition is not
8381          allowed -- even if it was OK at first.  For example:
8382
8383            int i, f() {}
8384
8385          is not valid.  */
8386       function_definition_allowed_p = false;
8387     }
8388
8389   /* Issue an error message if no declarators are present, and the
8390      decl-specifier-seq does not itself declare a class or
8391      enumeration.  */
8392   if (!saw_declarator)
8393     {
8394       if (cp_parser_declares_only_class_p (parser))
8395         shadow_tag (&decl_specifiers);
8396       /* Perform any deferred access checks.  */
8397       perform_deferred_access_checks ();
8398     }
8399
8400   /* Consume the `;'.  */
8401   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8402
8403  done:
8404   pop_deferring_access_checks ();
8405 }
8406
8407 /* Parse a decl-specifier-seq.
8408
8409    decl-specifier-seq:
8410      decl-specifier-seq [opt] decl-specifier
8411
8412    decl-specifier:
8413      storage-class-specifier
8414      type-specifier
8415      function-specifier
8416      friend
8417      typedef
8418
8419    GNU Extension:
8420
8421    decl-specifier:
8422      attributes
8423
8424    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8425
8426    The parser flags FLAGS is used to control type-specifier parsing.
8427
8428    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8429    flags:
8430
8431      1: one of the decl-specifiers is an elaborated-type-specifier
8432         (i.e., a type declaration)
8433      2: one of the decl-specifiers is an enum-specifier or a
8434         class-specifier (i.e., a type definition)
8435
8436    */
8437
8438 static void
8439 cp_parser_decl_specifier_seq (cp_parser* parser,
8440                               cp_parser_flags flags,
8441                               cp_decl_specifier_seq *decl_specs,
8442                               int* declares_class_or_enum)
8443 {
8444   bool constructor_possible_p = !parser->in_declarator_p;
8445   cp_token *start_token = NULL;
8446
8447   /* Clear DECL_SPECS.  */
8448   clear_decl_specs (decl_specs);
8449
8450   /* Assume no class or enumeration type is declared.  */
8451   *declares_class_or_enum = 0;
8452
8453   /* Keep reading specifiers until there are no more to read.  */
8454   while (true)
8455     {
8456       bool constructor_p;
8457       bool found_decl_spec;
8458       cp_token *token;
8459
8460       /* Peek at the next token.  */
8461       token = cp_lexer_peek_token (parser->lexer);
8462
8463       /* Save the first token of the decl spec list for error
8464          reporting.  */
8465       if (!start_token)
8466         start_token = token;
8467       /* Handle attributes.  */
8468       if (token->keyword == RID_ATTRIBUTE)
8469         {
8470           /* Parse the attributes.  */
8471           decl_specs->attributes
8472             = chainon (decl_specs->attributes,
8473                        cp_parser_attributes_opt (parser));
8474           continue;
8475         }
8476       /* Assume we will find a decl-specifier keyword.  */
8477       found_decl_spec = true;
8478       /* If the next token is an appropriate keyword, we can simply
8479          add it to the list.  */
8480       switch (token->keyword)
8481         {
8482           /* decl-specifier:
8483                friend  */
8484         case RID_FRIEND:
8485           if (!at_class_scope_p ())
8486             {
8487               error_at (token->location, "%<friend%> used outside of class");
8488               cp_lexer_purge_token (parser->lexer);
8489             }
8490           else
8491             {
8492               ++decl_specs->specs[(int) ds_friend];
8493               /* Consume the token.  */
8494               cp_lexer_consume_token (parser->lexer);
8495             }
8496           break;
8497
8498           /* function-specifier:
8499                inline
8500                virtual
8501                explicit  */
8502         case RID_INLINE:
8503         case RID_VIRTUAL:
8504         case RID_EXPLICIT:
8505           cp_parser_function_specifier_opt (parser, decl_specs);
8506           break;
8507
8508           /* decl-specifier:
8509                typedef  */
8510         case RID_TYPEDEF:
8511           ++decl_specs->specs[(int) ds_typedef];
8512           /* Consume the token.  */
8513           cp_lexer_consume_token (parser->lexer);
8514           /* A constructor declarator cannot appear in a typedef.  */
8515           constructor_possible_p = false;
8516           /* The "typedef" keyword can only occur in a declaration; we
8517              may as well commit at this point.  */
8518           cp_parser_commit_to_tentative_parse (parser);
8519
8520           if (decl_specs->storage_class != sc_none)
8521             decl_specs->conflicting_specifiers_p = true;
8522           break;
8523
8524           /* storage-class-specifier:
8525                auto
8526                register
8527                static
8528                extern
8529                mutable
8530
8531              GNU Extension:
8532                thread  */
8533         case RID_AUTO:
8534           if (cxx_dialect == cxx98) 
8535             {
8536               /* Consume the token.  */
8537               cp_lexer_consume_token (parser->lexer);
8538
8539               /* Complain about `auto' as a storage specifier, if
8540                  we're complaining about C++0x compatibility.  */
8541               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
8542                           " will change meaning in C++0x; please remove it");
8543
8544               /* Set the storage class anyway.  */
8545               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8546                                            token->location);
8547             }
8548           else
8549             /* C++0x auto type-specifier.  */
8550             found_decl_spec = false;
8551           break;
8552
8553         case RID_REGISTER:
8554         case RID_STATIC:
8555         case RID_EXTERN:
8556         case RID_MUTABLE:
8557           /* Consume the token.  */
8558           cp_lexer_consume_token (parser->lexer);
8559           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8560                                        token->location);
8561           break;
8562         case RID_THREAD:
8563           /* Consume the token.  */
8564           cp_lexer_consume_token (parser->lexer);
8565           ++decl_specs->specs[(int) ds_thread];
8566           break;
8567
8568         default:
8569           /* We did not yet find a decl-specifier yet.  */
8570           found_decl_spec = false;
8571           break;
8572         }
8573
8574       /* Constructors are a special case.  The `S' in `S()' is not a
8575          decl-specifier; it is the beginning of the declarator.  */
8576       constructor_p
8577         = (!found_decl_spec
8578            && constructor_possible_p
8579            && (cp_parser_constructor_declarator_p
8580                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8581
8582       /* If we don't have a DECL_SPEC yet, then we must be looking at
8583          a type-specifier.  */
8584       if (!found_decl_spec && !constructor_p)
8585         {
8586           int decl_spec_declares_class_or_enum;
8587           bool is_cv_qualifier;
8588           tree type_spec;
8589
8590           type_spec
8591             = cp_parser_type_specifier (parser, flags,
8592                                         decl_specs,
8593                                         /*is_declaration=*/true,
8594                                         &decl_spec_declares_class_or_enum,
8595                                         &is_cv_qualifier);
8596           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8597
8598           /* If this type-specifier referenced a user-defined type
8599              (a typedef, class-name, etc.), then we can't allow any
8600              more such type-specifiers henceforth.
8601
8602              [dcl.spec]
8603
8604              The longest sequence of decl-specifiers that could
8605              possibly be a type name is taken as the
8606              decl-specifier-seq of a declaration.  The sequence shall
8607              be self-consistent as described below.
8608
8609              [dcl.type]
8610
8611              As a general rule, at most one type-specifier is allowed
8612              in the complete decl-specifier-seq of a declaration.  The
8613              only exceptions are the following:
8614
8615              -- const or volatile can be combined with any other
8616                 type-specifier.
8617
8618              -- signed or unsigned can be combined with char, long,
8619                 short, or int.
8620
8621              -- ..
8622
8623              Example:
8624
8625                typedef char* Pc;
8626                void g (const int Pc);
8627
8628              Here, Pc is *not* part of the decl-specifier seq; it's
8629              the declarator.  Therefore, once we see a type-specifier
8630              (other than a cv-qualifier), we forbid any additional
8631              user-defined types.  We *do* still allow things like `int
8632              int' to be considered a decl-specifier-seq, and issue the
8633              error message later.  */
8634           if (type_spec && !is_cv_qualifier)
8635             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8636           /* A constructor declarator cannot follow a type-specifier.  */
8637           if (type_spec)
8638             {
8639               constructor_possible_p = false;
8640               found_decl_spec = true;
8641             }
8642         }
8643
8644       /* If we still do not have a DECL_SPEC, then there are no more
8645          decl-specifiers.  */
8646       if (!found_decl_spec)
8647         break;
8648
8649       decl_specs->any_specifiers_p = true;
8650       /* After we see one decl-specifier, further decl-specifiers are
8651          always optional.  */
8652       flags |= CP_PARSER_FLAGS_OPTIONAL;
8653     }
8654
8655   cp_parser_check_decl_spec (decl_specs, start_token->location);
8656
8657   /* Don't allow a friend specifier with a class definition.  */
8658   if (decl_specs->specs[(int) ds_friend] != 0
8659       && (*declares_class_or_enum & 2))
8660     error_at (start_token->location,
8661               "class definition may not be declared a friend");
8662 }
8663
8664 /* Parse an (optional) storage-class-specifier.
8665
8666    storage-class-specifier:
8667      auto
8668      register
8669      static
8670      extern
8671      mutable
8672
8673    GNU Extension:
8674
8675    storage-class-specifier:
8676      thread
8677
8678    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8679
8680 static tree
8681 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8682 {
8683   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8684     {
8685     case RID_AUTO:
8686       if (cxx_dialect != cxx98)
8687         return NULL_TREE;
8688       /* Fall through for C++98.  */
8689
8690     case RID_REGISTER:
8691     case RID_STATIC:
8692     case RID_EXTERN:
8693     case RID_MUTABLE:
8694     case RID_THREAD:
8695       /* Consume the token.  */
8696       return cp_lexer_consume_token (parser->lexer)->u.value;
8697
8698     default:
8699       return NULL_TREE;
8700     }
8701 }
8702
8703 /* Parse an (optional) function-specifier.
8704
8705    function-specifier:
8706      inline
8707      virtual
8708      explicit
8709
8710    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8711    Updates DECL_SPECS, if it is non-NULL.  */
8712
8713 static tree
8714 cp_parser_function_specifier_opt (cp_parser* parser,
8715                                   cp_decl_specifier_seq *decl_specs)
8716 {
8717   cp_token *token = cp_lexer_peek_token (parser->lexer);
8718   switch (token->keyword)
8719     {
8720     case RID_INLINE:
8721       if (decl_specs)
8722         ++decl_specs->specs[(int) ds_inline];
8723       break;
8724
8725     case RID_VIRTUAL:
8726       /* 14.5.2.3 [temp.mem]
8727
8728          A member function template shall not be virtual.  */
8729       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8730         error_at (token->location, "templates may not be %<virtual%>");
8731       else if (decl_specs)
8732         ++decl_specs->specs[(int) ds_virtual];
8733       break;
8734
8735     case RID_EXPLICIT:
8736       if (decl_specs)
8737         ++decl_specs->specs[(int) ds_explicit];
8738       break;
8739
8740     default:
8741       return NULL_TREE;
8742     }
8743
8744   /* Consume the token.  */
8745   return cp_lexer_consume_token (parser->lexer)->u.value;
8746 }
8747
8748 /* Parse a linkage-specification.
8749
8750    linkage-specification:
8751      extern string-literal { declaration-seq [opt] }
8752      extern string-literal declaration  */
8753
8754 static void
8755 cp_parser_linkage_specification (cp_parser* parser)
8756 {
8757   tree linkage;
8758
8759   /* Look for the `extern' keyword.  */
8760   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8761
8762   /* Look for the string-literal.  */
8763   linkage = cp_parser_string_literal (parser, false, false);
8764
8765   /* Transform the literal into an identifier.  If the literal is a
8766      wide-character string, or contains embedded NULs, then we can't
8767      handle it as the user wants.  */
8768   if (strlen (TREE_STRING_POINTER (linkage))
8769       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8770     {
8771       cp_parser_error (parser, "invalid linkage-specification");
8772       /* Assume C++ linkage.  */
8773       linkage = lang_name_cplusplus;
8774     }
8775   else
8776     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8777
8778   /* We're now using the new linkage.  */
8779   push_lang_context (linkage);
8780
8781   /* If the next token is a `{', then we're using the first
8782      production.  */
8783   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8784     {
8785       /* Consume the `{' token.  */
8786       cp_lexer_consume_token (parser->lexer);
8787       /* Parse the declarations.  */
8788       cp_parser_declaration_seq_opt (parser);
8789       /* Look for the closing `}'.  */
8790       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8791     }
8792   /* Otherwise, there's just one declaration.  */
8793   else
8794     {
8795       bool saved_in_unbraced_linkage_specification_p;
8796
8797       saved_in_unbraced_linkage_specification_p
8798         = parser->in_unbraced_linkage_specification_p;
8799       parser->in_unbraced_linkage_specification_p = true;
8800       cp_parser_declaration (parser);
8801       parser->in_unbraced_linkage_specification_p
8802         = saved_in_unbraced_linkage_specification_p;
8803     }
8804
8805   /* We're done with the linkage-specification.  */
8806   pop_lang_context ();
8807 }
8808
8809 /* Parse a static_assert-declaration.
8810
8811    static_assert-declaration:
8812      static_assert ( constant-expression , string-literal ) ; 
8813
8814    If MEMBER_P, this static_assert is a class member.  */
8815
8816 static void 
8817 cp_parser_static_assert(cp_parser *parser, bool member_p)
8818 {
8819   tree condition;
8820   tree message;
8821   cp_token *token;
8822   location_t saved_loc;
8823
8824   /* Peek at the `static_assert' token so we can keep track of exactly
8825      where the static assertion started.  */
8826   token = cp_lexer_peek_token (parser->lexer);
8827   saved_loc = token->location;
8828
8829   /* Look for the `static_assert' keyword.  */
8830   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8831                                   "%<static_assert%>"))
8832     return;
8833
8834   /*  We know we are in a static assertion; commit to any tentative
8835       parse.  */
8836   if (cp_parser_parsing_tentatively (parser))
8837     cp_parser_commit_to_tentative_parse (parser);
8838
8839   /* Parse the `(' starting the static assertion condition.  */
8840   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8841
8842   /* Parse the constant-expression.  */
8843   condition = 
8844     cp_parser_constant_expression (parser,
8845                                    /*allow_non_constant_p=*/false,
8846                                    /*non_constant_p=*/NULL);
8847
8848   /* Parse the separating `,'.  */
8849   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8850
8851   /* Parse the string-literal message.  */
8852   message = cp_parser_string_literal (parser, 
8853                                       /*translate=*/false,
8854                                       /*wide_ok=*/true);
8855
8856   /* A `)' completes the static assertion.  */
8857   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8858     cp_parser_skip_to_closing_parenthesis (parser, 
8859                                            /*recovering=*/true, 
8860                                            /*or_comma=*/false,
8861                                            /*consume_paren=*/true);
8862
8863   /* A semicolon terminates the declaration.  */
8864   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8865
8866   /* Complete the static assertion, which may mean either processing 
8867      the static assert now or saving it for template instantiation.  */
8868   finish_static_assert (condition, message, saved_loc, member_p);
8869 }
8870
8871 /* Parse a `decltype' type. Returns the type. 
8872
8873    simple-type-specifier:
8874      decltype ( expression )  */
8875
8876 static tree
8877 cp_parser_decltype (cp_parser *parser)
8878 {
8879   tree expr;
8880   bool id_expression_or_member_access_p = false;
8881   const char *saved_message;
8882   bool saved_integral_constant_expression_p;
8883   bool saved_non_integral_constant_expression_p;
8884   cp_token *id_expr_start_token;
8885
8886   /* Look for the `decltype' token.  */
8887   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8888     return error_mark_node;
8889
8890   /* Types cannot be defined in a `decltype' expression.  Save away the
8891      old message.  */
8892   saved_message = parser->type_definition_forbidden_message;
8893
8894   /* And create the new one.  */
8895   parser->type_definition_forbidden_message
8896     = "types may not be defined in %<decltype%> expressions";
8897
8898   /* The restrictions on constant-expressions do not apply inside
8899      decltype expressions.  */
8900   saved_integral_constant_expression_p
8901     = parser->integral_constant_expression_p;
8902   saved_non_integral_constant_expression_p
8903     = parser->non_integral_constant_expression_p;
8904   parser->integral_constant_expression_p = false;
8905
8906   /* Do not actually evaluate the expression.  */
8907   ++cp_unevaluated_operand;
8908
8909   /* Do not warn about problems with the expression.  */
8910   ++c_inhibit_evaluation_warnings;
8911
8912   /* Parse the opening `('.  */
8913   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8914     return error_mark_node;
8915   
8916   /* First, try parsing an id-expression.  */
8917   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8918   cp_parser_parse_tentatively (parser);
8919   expr = cp_parser_id_expression (parser,
8920                                   /*template_keyword_p=*/false,
8921                                   /*check_dependency_p=*/true,
8922                                   /*template_p=*/NULL,
8923                                   /*declarator_p=*/false,
8924                                   /*optional_p=*/false);
8925
8926   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8927     {
8928       bool non_integral_constant_expression_p = false;
8929       tree id_expression = expr;
8930       cp_id_kind idk;
8931       const char *error_msg;
8932
8933       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8934         /* Lookup the name we got back from the id-expression.  */
8935         expr = cp_parser_lookup_name (parser, expr,
8936                                       none_type,
8937                                       /*is_template=*/false,
8938                                       /*is_namespace=*/false,
8939                                       /*check_dependency=*/true,
8940                                       /*ambiguous_decls=*/NULL,
8941                                       id_expr_start_token->location);
8942
8943       if (expr
8944           && expr != error_mark_node
8945           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8946           && TREE_CODE (expr) != TYPE_DECL
8947           && (TREE_CODE (expr) != BIT_NOT_EXPR
8948               || !TYPE_P (TREE_OPERAND (expr, 0)))
8949           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8950         {
8951           /* Complete lookup of the id-expression.  */
8952           expr = (finish_id_expression
8953                   (id_expression, expr, parser->scope, &idk,
8954                    /*integral_constant_expression_p=*/false,
8955                    /*allow_non_integral_constant_expression_p=*/true,
8956                    &non_integral_constant_expression_p,
8957                    /*template_p=*/false,
8958                    /*done=*/true,
8959                    /*address_p=*/false,
8960                    /*template_arg_p=*/false,
8961                    &error_msg,
8962                    id_expr_start_token->location));
8963
8964           if (expr == error_mark_node)
8965             /* We found an id-expression, but it was something that we
8966                should not have found. This is an error, not something
8967                we can recover from, so note that we found an
8968                id-expression and we'll recover as gracefully as
8969                possible.  */
8970             id_expression_or_member_access_p = true;
8971         }
8972
8973       if (expr 
8974           && expr != error_mark_node
8975           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8976         /* We have an id-expression.  */
8977         id_expression_or_member_access_p = true;
8978     }
8979
8980   if (!id_expression_or_member_access_p)
8981     {
8982       /* Abort the id-expression parse.  */
8983       cp_parser_abort_tentative_parse (parser);
8984
8985       /* Parsing tentatively, again.  */
8986       cp_parser_parse_tentatively (parser);
8987
8988       /* Parse a class member access.  */
8989       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8990                                            /*cast_p=*/false,
8991                                            /*member_access_only_p=*/true, NULL);
8992
8993       if (expr 
8994           && expr != error_mark_node
8995           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8996         /* We have an id-expression.  */
8997         id_expression_or_member_access_p = true;
8998     }
8999
9000   if (id_expression_or_member_access_p)
9001     /* We have parsed the complete id-expression or member access.  */
9002     cp_parser_parse_definitely (parser);
9003   else
9004     {
9005       /* Abort our attempt to parse an id-expression or member access
9006          expression.  */
9007       cp_parser_abort_tentative_parse (parser);
9008
9009       /* Parse a full expression.  */
9010       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9011     }
9012
9013   /* Go back to evaluating expressions.  */
9014   --cp_unevaluated_operand;
9015   --c_inhibit_evaluation_warnings;
9016
9017   /* Restore the old message and the integral constant expression
9018      flags.  */
9019   parser->type_definition_forbidden_message = saved_message;
9020   parser->integral_constant_expression_p
9021     = saved_integral_constant_expression_p;
9022   parser->non_integral_constant_expression_p
9023     = saved_non_integral_constant_expression_p;
9024
9025   if (expr == error_mark_node)
9026     {
9027       /* Skip everything up to the closing `)'.  */
9028       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9029                                              /*consume_paren=*/true);
9030       return error_mark_node;
9031     }
9032   
9033   /* Parse to the closing `)'.  */
9034   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9035     {
9036       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9037                                              /*consume_paren=*/true);
9038       return error_mark_node;
9039     }
9040
9041   return finish_decltype_type (expr, id_expression_or_member_access_p);
9042 }
9043
9044 /* Special member functions [gram.special] */
9045
9046 /* Parse a conversion-function-id.
9047
9048    conversion-function-id:
9049      operator conversion-type-id
9050
9051    Returns an IDENTIFIER_NODE representing the operator.  */
9052
9053 static tree
9054 cp_parser_conversion_function_id (cp_parser* parser)
9055 {
9056   tree type;
9057   tree saved_scope;
9058   tree saved_qualifying_scope;
9059   tree saved_object_scope;
9060   tree pushed_scope = NULL_TREE;
9061
9062   /* Look for the `operator' token.  */
9063   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9064     return error_mark_node;
9065   /* When we parse the conversion-type-id, the current scope will be
9066      reset.  However, we need that information in able to look up the
9067      conversion function later, so we save it here.  */
9068   saved_scope = parser->scope;
9069   saved_qualifying_scope = parser->qualifying_scope;
9070   saved_object_scope = parser->object_scope;
9071   /* We must enter the scope of the class so that the names of
9072      entities declared within the class are available in the
9073      conversion-type-id.  For example, consider:
9074
9075        struct S {
9076          typedef int I;
9077          operator I();
9078        };
9079
9080        S::operator I() { ... }
9081
9082      In order to see that `I' is a type-name in the definition, we
9083      must be in the scope of `S'.  */
9084   if (saved_scope)
9085     pushed_scope = push_scope (saved_scope);
9086   /* Parse the conversion-type-id.  */
9087   type = cp_parser_conversion_type_id (parser);
9088   /* Leave the scope of the class, if any.  */
9089   if (pushed_scope)
9090     pop_scope (pushed_scope);
9091   /* Restore the saved scope.  */
9092   parser->scope = saved_scope;
9093   parser->qualifying_scope = saved_qualifying_scope;
9094   parser->object_scope = saved_object_scope;
9095   /* If the TYPE is invalid, indicate failure.  */
9096   if (type == error_mark_node)
9097     return error_mark_node;
9098   return mangle_conv_op_name_for_type (type);
9099 }
9100
9101 /* Parse a conversion-type-id:
9102
9103    conversion-type-id:
9104      type-specifier-seq conversion-declarator [opt]
9105
9106    Returns the TYPE specified.  */
9107
9108 static tree
9109 cp_parser_conversion_type_id (cp_parser* parser)
9110 {
9111   tree attributes;
9112   cp_decl_specifier_seq type_specifiers;
9113   cp_declarator *declarator;
9114   tree type_specified;
9115
9116   /* Parse the attributes.  */
9117   attributes = cp_parser_attributes_opt (parser);
9118   /* Parse the type-specifiers.  */
9119   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9120                                 &type_specifiers);
9121   /* If that didn't work, stop.  */
9122   if (type_specifiers.type == error_mark_node)
9123     return error_mark_node;
9124   /* Parse the conversion-declarator.  */
9125   declarator = cp_parser_conversion_declarator_opt (parser);
9126
9127   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9128                                     /*initialized=*/0, &attributes);
9129   if (attributes)
9130     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9131
9132   /* Don't give this error when parsing tentatively.  This happens to
9133      work because we always parse this definitively once.  */
9134   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9135       && type_uses_auto (type_specified))
9136     {
9137       error ("invalid use of %<auto%> in conversion operator");
9138       return error_mark_node;
9139     }
9140
9141   return type_specified;
9142 }
9143
9144 /* Parse an (optional) conversion-declarator.
9145
9146    conversion-declarator:
9147      ptr-operator conversion-declarator [opt]
9148
9149    */
9150
9151 static cp_declarator *
9152 cp_parser_conversion_declarator_opt (cp_parser* parser)
9153 {
9154   enum tree_code code;
9155   tree class_type;
9156   cp_cv_quals cv_quals;
9157
9158   /* We don't know if there's a ptr-operator next, or not.  */
9159   cp_parser_parse_tentatively (parser);
9160   /* Try the ptr-operator.  */
9161   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9162   /* If it worked, look for more conversion-declarators.  */
9163   if (cp_parser_parse_definitely (parser))
9164     {
9165       cp_declarator *declarator;
9166
9167       /* Parse another optional declarator.  */
9168       declarator = cp_parser_conversion_declarator_opt (parser);
9169
9170       return cp_parser_make_indirect_declarator
9171         (code, class_type, cv_quals, declarator);
9172    }
9173
9174   return NULL;
9175 }
9176
9177 /* Parse an (optional) ctor-initializer.
9178
9179    ctor-initializer:
9180      : mem-initializer-list
9181
9182    Returns TRUE iff the ctor-initializer was actually present.  */
9183
9184 static bool
9185 cp_parser_ctor_initializer_opt (cp_parser* parser)
9186 {
9187   /* If the next token is not a `:', then there is no
9188      ctor-initializer.  */
9189   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9190     {
9191       /* Do default initialization of any bases and members.  */
9192       if (DECL_CONSTRUCTOR_P (current_function_decl))
9193         finish_mem_initializers (NULL_TREE);
9194
9195       return false;
9196     }
9197
9198   /* Consume the `:' token.  */
9199   cp_lexer_consume_token (parser->lexer);
9200   /* And the mem-initializer-list.  */
9201   cp_parser_mem_initializer_list (parser);
9202
9203   return true;
9204 }
9205
9206 /* Parse a mem-initializer-list.
9207
9208    mem-initializer-list:
9209      mem-initializer ... [opt]
9210      mem-initializer ... [opt] , mem-initializer-list  */
9211
9212 static void
9213 cp_parser_mem_initializer_list (cp_parser* parser)
9214 {
9215   tree mem_initializer_list = NULL_TREE;
9216   cp_token *token = cp_lexer_peek_token (parser->lexer);
9217
9218   /* Let the semantic analysis code know that we are starting the
9219      mem-initializer-list.  */
9220   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9221     error_at (token->location,
9222               "only constructors take base initializers");
9223
9224   /* Loop through the list.  */
9225   while (true)
9226     {
9227       tree mem_initializer;
9228
9229       token = cp_lexer_peek_token (parser->lexer);
9230       /* Parse the mem-initializer.  */
9231       mem_initializer = cp_parser_mem_initializer (parser);
9232       /* If the next token is a `...', we're expanding member initializers. */
9233       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9234         {
9235           /* Consume the `...'. */
9236           cp_lexer_consume_token (parser->lexer);
9237
9238           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9239              can be expanded but members cannot. */
9240           if (mem_initializer != error_mark_node
9241               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9242             {
9243               error_at (token->location,
9244                         "cannot expand initializer for member %<%D%>",
9245                         TREE_PURPOSE (mem_initializer));
9246               mem_initializer = error_mark_node;
9247             }
9248
9249           /* Construct the pack expansion type. */
9250           if (mem_initializer != error_mark_node)
9251             mem_initializer = make_pack_expansion (mem_initializer);
9252         }
9253       /* Add it to the list, unless it was erroneous.  */
9254       if (mem_initializer != error_mark_node)
9255         {
9256           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9257           mem_initializer_list = mem_initializer;
9258         }
9259       /* If the next token is not a `,', we're done.  */
9260       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9261         break;
9262       /* Consume the `,' token.  */
9263       cp_lexer_consume_token (parser->lexer);
9264     }
9265
9266   /* Perform semantic analysis.  */
9267   if (DECL_CONSTRUCTOR_P (current_function_decl))
9268     finish_mem_initializers (mem_initializer_list);
9269 }
9270
9271 /* Parse a mem-initializer.
9272
9273    mem-initializer:
9274      mem-initializer-id ( expression-list [opt] )
9275      mem-initializer-id braced-init-list
9276
9277    GNU extension:
9278
9279    mem-initializer:
9280      ( expression-list [opt] )
9281
9282    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9283    class) or FIELD_DECL (for a non-static data member) to initialize;
9284    the TREE_VALUE is the expression-list.  An empty initialization
9285    list is represented by void_list_node.  */
9286
9287 static tree
9288 cp_parser_mem_initializer (cp_parser* parser)
9289 {
9290   tree mem_initializer_id;
9291   tree expression_list;
9292   tree member;
9293   cp_token *token = cp_lexer_peek_token (parser->lexer);
9294
9295   /* Find out what is being initialized.  */
9296   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9297     {
9298       permerror (token->location,
9299                  "anachronistic old-style base class initializer");
9300       mem_initializer_id = NULL_TREE;
9301     }
9302   else
9303     {
9304       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9305       if (mem_initializer_id == error_mark_node)
9306         return mem_initializer_id;
9307     }
9308   member = expand_member_init (mem_initializer_id);
9309   if (member && !DECL_P (member))
9310     in_base_initializer = 1;
9311
9312   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9313     {
9314       bool expr_non_constant_p;
9315       maybe_warn_cpp0x ("extended initializer lists");
9316       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9317       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9318       expression_list = build_tree_list (NULL_TREE, expression_list);
9319     }
9320   else
9321     {
9322       VEC(tree,gc)* vec;
9323       vec = cp_parser_parenthesized_expression_list (parser, false,
9324                                                      /*cast_p=*/false,
9325                                                      /*allow_expansion_p=*/true,
9326                                                      /*non_constant_p=*/NULL);
9327       if (vec == NULL)
9328         return error_mark_node;
9329       expression_list = build_tree_list_vec (vec);
9330       release_tree_vector (vec);
9331     }
9332
9333   if (expression_list == error_mark_node)
9334     return error_mark_node;
9335   if (!expression_list)
9336     expression_list = void_type_node;
9337
9338   in_base_initializer = 0;
9339
9340   return member ? build_tree_list (member, expression_list) : error_mark_node;
9341 }
9342
9343 /* Parse a mem-initializer-id.
9344
9345    mem-initializer-id:
9346      :: [opt] nested-name-specifier [opt] class-name
9347      identifier
9348
9349    Returns a TYPE indicating the class to be initializer for the first
9350    production.  Returns an IDENTIFIER_NODE indicating the data member
9351    to be initialized for the second production.  */
9352
9353 static tree
9354 cp_parser_mem_initializer_id (cp_parser* parser)
9355 {
9356   bool global_scope_p;
9357   bool nested_name_specifier_p;
9358   bool template_p = false;
9359   tree id;
9360
9361   cp_token *token = cp_lexer_peek_token (parser->lexer);
9362
9363   /* `typename' is not allowed in this context ([temp.res]).  */
9364   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9365     {
9366       error_at (token->location, 
9367                 "keyword %<typename%> not allowed in this context (a qualified "
9368                 "member initializer is implicitly a type)");
9369       cp_lexer_consume_token (parser->lexer);
9370     }
9371   /* Look for the optional `::' operator.  */
9372   global_scope_p
9373     = (cp_parser_global_scope_opt (parser,
9374                                    /*current_scope_valid_p=*/false)
9375        != NULL_TREE);
9376   /* Look for the optional nested-name-specifier.  The simplest way to
9377      implement:
9378
9379        [temp.res]
9380
9381        The keyword `typename' is not permitted in a base-specifier or
9382        mem-initializer; in these contexts a qualified name that
9383        depends on a template-parameter is implicitly assumed to be a
9384        type name.
9385
9386      is to assume that we have seen the `typename' keyword at this
9387      point.  */
9388   nested_name_specifier_p
9389     = (cp_parser_nested_name_specifier_opt (parser,
9390                                             /*typename_keyword_p=*/true,
9391                                             /*check_dependency_p=*/true,
9392                                             /*type_p=*/true,
9393                                             /*is_declaration=*/true)
9394        != NULL_TREE);
9395   if (nested_name_specifier_p)
9396     template_p = cp_parser_optional_template_keyword (parser);
9397   /* If there is a `::' operator or a nested-name-specifier, then we
9398      are definitely looking for a class-name.  */
9399   if (global_scope_p || nested_name_specifier_p)
9400     return cp_parser_class_name (parser,
9401                                  /*typename_keyword_p=*/true,
9402                                  /*template_keyword_p=*/template_p,
9403                                  none_type,
9404                                  /*check_dependency_p=*/true,
9405                                  /*class_head_p=*/false,
9406                                  /*is_declaration=*/true);
9407   /* Otherwise, we could also be looking for an ordinary identifier.  */
9408   cp_parser_parse_tentatively (parser);
9409   /* Try a class-name.  */
9410   id = cp_parser_class_name (parser,
9411                              /*typename_keyword_p=*/true,
9412                              /*template_keyword_p=*/false,
9413                              none_type,
9414                              /*check_dependency_p=*/true,
9415                              /*class_head_p=*/false,
9416                              /*is_declaration=*/true);
9417   /* If we found one, we're done.  */
9418   if (cp_parser_parse_definitely (parser))
9419     return id;
9420   /* Otherwise, look for an ordinary identifier.  */
9421   return cp_parser_identifier (parser);
9422 }
9423
9424 /* Overloading [gram.over] */
9425
9426 /* Parse an operator-function-id.
9427
9428    operator-function-id:
9429      operator operator
9430
9431    Returns an IDENTIFIER_NODE for the operator which is a
9432    human-readable spelling of the identifier, e.g., `operator +'.  */
9433
9434 static tree
9435 cp_parser_operator_function_id (cp_parser* parser)
9436 {
9437   /* Look for the `operator' keyword.  */
9438   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9439     return error_mark_node;
9440   /* And then the name of the operator itself.  */
9441   return cp_parser_operator (parser);
9442 }
9443
9444 /* Parse an operator.
9445
9446    operator:
9447      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9448      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9449      || ++ -- , ->* -> () []
9450
9451    GNU Extensions:
9452
9453    operator:
9454      <? >? <?= >?=
9455
9456    Returns an IDENTIFIER_NODE for the operator which is a
9457    human-readable spelling of the identifier, e.g., `operator +'.  */
9458
9459 static tree
9460 cp_parser_operator (cp_parser* parser)
9461 {
9462   tree id = NULL_TREE;
9463   cp_token *token;
9464
9465   /* Peek at the next token.  */
9466   token = cp_lexer_peek_token (parser->lexer);
9467   /* Figure out which operator we have.  */
9468   switch (token->type)
9469     {
9470     case CPP_KEYWORD:
9471       {
9472         enum tree_code op;
9473
9474         /* The keyword should be either `new' or `delete'.  */
9475         if (token->keyword == RID_NEW)
9476           op = NEW_EXPR;
9477         else if (token->keyword == RID_DELETE)
9478           op = DELETE_EXPR;
9479         else
9480           break;
9481
9482         /* Consume the `new' or `delete' token.  */
9483         cp_lexer_consume_token (parser->lexer);
9484
9485         /* Peek at the next token.  */
9486         token = cp_lexer_peek_token (parser->lexer);
9487         /* If it's a `[' token then this is the array variant of the
9488            operator.  */
9489         if (token->type == CPP_OPEN_SQUARE)
9490           {
9491             /* Consume the `[' token.  */
9492             cp_lexer_consume_token (parser->lexer);
9493             /* Look for the `]' token.  */
9494             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9495             id = ansi_opname (op == NEW_EXPR
9496                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9497           }
9498         /* Otherwise, we have the non-array variant.  */
9499         else
9500           id = ansi_opname (op);
9501
9502         return id;
9503       }
9504
9505     case CPP_PLUS:
9506       id = ansi_opname (PLUS_EXPR);
9507       break;
9508
9509     case CPP_MINUS:
9510       id = ansi_opname (MINUS_EXPR);
9511       break;
9512
9513     case CPP_MULT:
9514       id = ansi_opname (MULT_EXPR);
9515       break;
9516
9517     case CPP_DIV:
9518       id = ansi_opname (TRUNC_DIV_EXPR);
9519       break;
9520
9521     case CPP_MOD:
9522       id = ansi_opname (TRUNC_MOD_EXPR);
9523       break;
9524
9525     case CPP_XOR:
9526       id = ansi_opname (BIT_XOR_EXPR);
9527       break;
9528
9529     case CPP_AND:
9530       id = ansi_opname (BIT_AND_EXPR);
9531       break;
9532
9533     case CPP_OR:
9534       id = ansi_opname (BIT_IOR_EXPR);
9535       break;
9536
9537     case CPP_COMPL:
9538       id = ansi_opname (BIT_NOT_EXPR);
9539       break;
9540
9541     case CPP_NOT:
9542       id = ansi_opname (TRUTH_NOT_EXPR);
9543       break;
9544
9545     case CPP_EQ:
9546       id = ansi_assopname (NOP_EXPR);
9547       break;
9548
9549     case CPP_LESS:
9550       id = ansi_opname (LT_EXPR);
9551       break;
9552
9553     case CPP_GREATER:
9554       id = ansi_opname (GT_EXPR);
9555       break;
9556
9557     case CPP_PLUS_EQ:
9558       id = ansi_assopname (PLUS_EXPR);
9559       break;
9560
9561     case CPP_MINUS_EQ:
9562       id = ansi_assopname (MINUS_EXPR);
9563       break;
9564
9565     case CPP_MULT_EQ:
9566       id = ansi_assopname (MULT_EXPR);
9567       break;
9568
9569     case CPP_DIV_EQ:
9570       id = ansi_assopname (TRUNC_DIV_EXPR);
9571       break;
9572
9573     case CPP_MOD_EQ:
9574       id = ansi_assopname (TRUNC_MOD_EXPR);
9575       break;
9576
9577     case CPP_XOR_EQ:
9578       id = ansi_assopname (BIT_XOR_EXPR);
9579       break;
9580
9581     case CPP_AND_EQ:
9582       id = ansi_assopname (BIT_AND_EXPR);
9583       break;
9584
9585     case CPP_OR_EQ:
9586       id = ansi_assopname (BIT_IOR_EXPR);
9587       break;
9588
9589     case CPP_LSHIFT:
9590       id = ansi_opname (LSHIFT_EXPR);
9591       break;
9592
9593     case CPP_RSHIFT:
9594       id = ansi_opname (RSHIFT_EXPR);
9595       break;
9596
9597     case CPP_LSHIFT_EQ:
9598       id = ansi_assopname (LSHIFT_EXPR);
9599       break;
9600
9601     case CPP_RSHIFT_EQ:
9602       id = ansi_assopname (RSHIFT_EXPR);
9603       break;
9604
9605     case CPP_EQ_EQ:
9606       id = ansi_opname (EQ_EXPR);
9607       break;
9608
9609     case CPP_NOT_EQ:
9610       id = ansi_opname (NE_EXPR);
9611       break;
9612
9613     case CPP_LESS_EQ:
9614       id = ansi_opname (LE_EXPR);
9615       break;
9616
9617     case CPP_GREATER_EQ:
9618       id = ansi_opname (GE_EXPR);
9619       break;
9620
9621     case CPP_AND_AND:
9622       id = ansi_opname (TRUTH_ANDIF_EXPR);
9623       break;
9624
9625     case CPP_OR_OR:
9626       id = ansi_opname (TRUTH_ORIF_EXPR);
9627       break;
9628
9629     case CPP_PLUS_PLUS:
9630       id = ansi_opname (POSTINCREMENT_EXPR);
9631       break;
9632
9633     case CPP_MINUS_MINUS:
9634       id = ansi_opname (PREDECREMENT_EXPR);
9635       break;
9636
9637     case CPP_COMMA:
9638       id = ansi_opname (COMPOUND_EXPR);
9639       break;
9640
9641     case CPP_DEREF_STAR:
9642       id = ansi_opname (MEMBER_REF);
9643       break;
9644
9645     case CPP_DEREF:
9646       id = ansi_opname (COMPONENT_REF);
9647       break;
9648
9649     case CPP_OPEN_PAREN:
9650       /* Consume the `('.  */
9651       cp_lexer_consume_token (parser->lexer);
9652       /* Look for the matching `)'.  */
9653       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9654       return ansi_opname (CALL_EXPR);
9655
9656     case CPP_OPEN_SQUARE:
9657       /* Consume the `['.  */
9658       cp_lexer_consume_token (parser->lexer);
9659       /* Look for the matching `]'.  */
9660       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9661       return ansi_opname (ARRAY_REF);
9662
9663     default:
9664       /* Anything else is an error.  */
9665       break;
9666     }
9667
9668   /* If we have selected an identifier, we need to consume the
9669      operator token.  */
9670   if (id)
9671     cp_lexer_consume_token (parser->lexer);
9672   /* Otherwise, no valid operator name was present.  */
9673   else
9674     {
9675       cp_parser_error (parser, "expected operator");
9676       id = error_mark_node;
9677     }
9678
9679   return id;
9680 }
9681
9682 /* Parse a template-declaration.
9683
9684    template-declaration:
9685      export [opt] template < template-parameter-list > declaration
9686
9687    If MEMBER_P is TRUE, this template-declaration occurs within a
9688    class-specifier.
9689
9690    The grammar rule given by the standard isn't correct.  What
9691    is really meant is:
9692
9693    template-declaration:
9694      export [opt] template-parameter-list-seq
9695        decl-specifier-seq [opt] init-declarator [opt] ;
9696      export [opt] template-parameter-list-seq
9697        function-definition
9698
9699    template-parameter-list-seq:
9700      template-parameter-list-seq [opt]
9701      template < template-parameter-list >  */
9702
9703 static void
9704 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9705 {
9706   /* Check for `export'.  */
9707   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9708     {
9709       /* Consume the `export' token.  */
9710       cp_lexer_consume_token (parser->lexer);
9711       /* Warn that we do not support `export'.  */
9712       warning (0, "keyword %<export%> not implemented, and will be ignored");
9713     }
9714
9715   cp_parser_template_declaration_after_export (parser, member_p);
9716 }
9717
9718 /* Parse a template-parameter-list.
9719
9720    template-parameter-list:
9721      template-parameter
9722      template-parameter-list , template-parameter
9723
9724    Returns a TREE_LIST.  Each node represents a template parameter.
9725    The nodes are connected via their TREE_CHAINs.  */
9726
9727 static tree
9728 cp_parser_template_parameter_list (cp_parser* parser)
9729 {
9730   tree parameter_list = NULL_TREE;
9731
9732   begin_template_parm_list ();
9733   while (true)
9734     {
9735       tree parameter;
9736       bool is_non_type;
9737       bool is_parameter_pack;
9738       location_t parm_loc;
9739
9740       /* Parse the template-parameter.  */
9741       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9742       parameter = cp_parser_template_parameter (parser, 
9743                                                 &is_non_type,
9744                                                 &is_parameter_pack);
9745       /* Add it to the list.  */
9746       if (parameter != error_mark_node)
9747         parameter_list = process_template_parm (parameter_list,
9748                                                 parm_loc,
9749                                                 parameter,
9750                                                 is_non_type,
9751                                                 is_parameter_pack);
9752       else
9753        {
9754          tree err_parm = build_tree_list (parameter, parameter);
9755          TREE_VALUE (err_parm) = error_mark_node;
9756          parameter_list = chainon (parameter_list, err_parm);
9757        }
9758
9759       /* If the next token is not a `,', we're done.  */
9760       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9761         break;
9762       /* Otherwise, consume the `,' token.  */
9763       cp_lexer_consume_token (parser->lexer);
9764     }
9765
9766   return end_template_parm_list (parameter_list);
9767 }
9768
9769 /* Parse a template-parameter.
9770
9771    template-parameter:
9772      type-parameter
9773      parameter-declaration
9774
9775    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9776    the parameter.  The TREE_PURPOSE is the default value, if any.
9777    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9778    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9779    set to true iff this parameter is a parameter pack. */
9780
9781 static tree
9782 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9783                               bool *is_parameter_pack)
9784 {
9785   cp_token *token;
9786   cp_parameter_declarator *parameter_declarator;
9787   cp_declarator *id_declarator;
9788   tree parm;
9789
9790   /* Assume it is a type parameter or a template parameter.  */
9791   *is_non_type = false;
9792   /* Assume it not a parameter pack. */
9793   *is_parameter_pack = false;
9794   /* Peek at the next token.  */
9795   token = cp_lexer_peek_token (parser->lexer);
9796   /* If it is `class' or `template', we have a type-parameter.  */
9797   if (token->keyword == RID_TEMPLATE)
9798     return cp_parser_type_parameter (parser, is_parameter_pack);
9799   /* If it is `class' or `typename' we do not know yet whether it is a
9800      type parameter or a non-type parameter.  Consider:
9801
9802        template <typename T, typename T::X X> ...
9803
9804      or:
9805
9806        template <class C, class D*> ...
9807
9808      Here, the first parameter is a type parameter, and the second is
9809      a non-type parameter.  We can tell by looking at the token after
9810      the identifier -- if it is a `,', `=', or `>' then we have a type
9811      parameter.  */
9812   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9813     {
9814       /* Peek at the token after `class' or `typename'.  */
9815       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9816       /* If it's an ellipsis, we have a template type parameter
9817          pack. */
9818       if (token->type == CPP_ELLIPSIS)
9819         return cp_parser_type_parameter (parser, is_parameter_pack);
9820       /* If it's an identifier, skip it.  */
9821       if (token->type == CPP_NAME)
9822         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9823       /* Now, see if the token looks like the end of a template
9824          parameter.  */
9825       if (token->type == CPP_COMMA
9826           || token->type == CPP_EQ
9827           || token->type == CPP_GREATER)
9828         return cp_parser_type_parameter (parser, is_parameter_pack);
9829     }
9830
9831   /* Otherwise, it is a non-type parameter.
9832
9833      [temp.param]
9834
9835      When parsing a default template-argument for a non-type
9836      template-parameter, the first non-nested `>' is taken as the end
9837      of the template parameter-list rather than a greater-than
9838      operator.  */
9839   *is_non_type = true;
9840   parameter_declarator
9841      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9842                                         /*parenthesized_p=*/NULL);
9843
9844   /* If the parameter declaration is marked as a parameter pack, set
9845      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9846      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9847      grokdeclarator. */
9848   if (parameter_declarator
9849       && parameter_declarator->declarator
9850       && parameter_declarator->declarator->parameter_pack_p)
9851     {
9852       *is_parameter_pack = true;
9853       parameter_declarator->declarator->parameter_pack_p = false;
9854     }
9855
9856   /* If the next token is an ellipsis, and we don't already have it
9857      marked as a parameter pack, then we have a parameter pack (that
9858      has no declarator).  */
9859   if (!*is_parameter_pack
9860       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9861       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9862     {
9863       /* Consume the `...'.  */
9864       cp_lexer_consume_token (parser->lexer);
9865       maybe_warn_variadic_templates ();
9866       
9867       *is_parameter_pack = true;
9868     }
9869   /* We might end up with a pack expansion as the type of the non-type
9870      template parameter, in which case this is a non-type template
9871      parameter pack.  */
9872   else if (parameter_declarator
9873            && parameter_declarator->decl_specifiers.type
9874            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9875     {
9876       *is_parameter_pack = true;
9877       parameter_declarator->decl_specifiers.type = 
9878         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9879     }
9880
9881   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9882     {
9883       /* Parameter packs cannot have default arguments.  However, a
9884          user may try to do so, so we'll parse them and give an
9885          appropriate diagnostic here.  */
9886
9887       /* Consume the `='.  */
9888       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9889       cp_lexer_consume_token (parser->lexer);
9890       
9891       /* Find the name of the parameter pack.  */     
9892       id_declarator = parameter_declarator->declarator;
9893       while (id_declarator && id_declarator->kind != cdk_id)
9894         id_declarator = id_declarator->declarator;
9895       
9896       if (id_declarator && id_declarator->kind == cdk_id)
9897         error_at (start_token->location,
9898                   "template parameter pack %qD cannot have a default argument",
9899                   id_declarator->u.id.unqualified_name);
9900       else
9901         error_at (start_token->location,
9902                   "template parameter pack cannot have a default argument");
9903       
9904       /* Parse the default argument, but throw away the result.  */
9905       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9906     }
9907
9908   parm = grokdeclarator (parameter_declarator->declarator,
9909                          &parameter_declarator->decl_specifiers,
9910                          PARM, /*initialized=*/0,
9911                          /*attrlist=*/NULL);
9912   if (parm == error_mark_node)
9913     return error_mark_node;
9914
9915   return build_tree_list (parameter_declarator->default_argument, parm);
9916 }
9917
9918 /* Parse a type-parameter.
9919
9920    type-parameter:
9921      class identifier [opt]
9922      class identifier [opt] = type-id
9923      typename identifier [opt]
9924      typename identifier [opt] = type-id
9925      template < template-parameter-list > class identifier [opt]
9926      template < template-parameter-list > class identifier [opt]
9927        = id-expression
9928
9929    GNU Extension (variadic templates):
9930
9931    type-parameter:
9932      class ... identifier [opt]
9933      typename ... identifier [opt]
9934
9935    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9936    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9937    the declaration of the parameter.
9938
9939    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9940
9941 static tree
9942 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9943 {
9944   cp_token *token;
9945   tree parameter;
9946
9947   /* Look for a keyword to tell us what kind of parameter this is.  */
9948   token = cp_parser_require (parser, CPP_KEYWORD,
9949                              "%<class%>, %<typename%>, or %<template%>");
9950   if (!token)
9951     return error_mark_node;
9952
9953   switch (token->keyword)
9954     {
9955     case RID_CLASS:
9956     case RID_TYPENAME:
9957       {
9958         tree identifier;
9959         tree default_argument;
9960
9961         /* If the next token is an ellipsis, we have a template
9962            argument pack. */
9963         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9964           {
9965             /* Consume the `...' token. */
9966             cp_lexer_consume_token (parser->lexer);
9967             maybe_warn_variadic_templates ();
9968
9969             *is_parameter_pack = true;
9970           }
9971
9972         /* If the next token is an identifier, then it names the
9973            parameter.  */
9974         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9975           identifier = cp_parser_identifier (parser);
9976         else
9977           identifier = NULL_TREE;
9978
9979         /* Create the parameter.  */
9980         parameter = finish_template_type_parm (class_type_node, identifier);
9981
9982         /* If the next token is an `=', we have a default argument.  */
9983         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9984           {
9985             /* Consume the `=' token.  */
9986             cp_lexer_consume_token (parser->lexer);
9987             /* Parse the default-argument.  */
9988             push_deferring_access_checks (dk_no_deferred);
9989             default_argument = cp_parser_type_id (parser);
9990
9991             /* Template parameter packs cannot have default
9992                arguments. */
9993             if (*is_parameter_pack)
9994               {
9995                 if (identifier)
9996                   error_at (token->location,
9997                             "template parameter pack %qD cannot have a "
9998                             "default argument", identifier);
9999                 else
10000                   error_at (token->location,
10001                             "template parameter packs cannot have "
10002                             "default arguments");
10003                 default_argument = NULL_TREE;
10004               }
10005             pop_deferring_access_checks ();
10006           }
10007         else
10008           default_argument = NULL_TREE;
10009
10010         /* Create the combined representation of the parameter and the
10011            default argument.  */
10012         parameter = build_tree_list (default_argument, parameter);
10013       }
10014       break;
10015
10016     case RID_TEMPLATE:
10017       {
10018         tree parameter_list;
10019         tree identifier;
10020         tree default_argument;
10021
10022         /* Look for the `<'.  */
10023         cp_parser_require (parser, CPP_LESS, "%<<%>");
10024         /* Parse the template-parameter-list.  */
10025         parameter_list = cp_parser_template_parameter_list (parser);
10026         /* Look for the `>'.  */
10027         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10028         /* Look for the `class' keyword.  */
10029         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10030         /* If the next token is an ellipsis, we have a template
10031            argument pack. */
10032         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10033           {
10034             /* Consume the `...' token. */
10035             cp_lexer_consume_token (parser->lexer);
10036             maybe_warn_variadic_templates ();
10037
10038             *is_parameter_pack = true;
10039           }
10040         /* If the next token is an `=', then there is a
10041            default-argument.  If the next token is a `>', we are at
10042            the end of the parameter-list.  If the next token is a `,',
10043            then we are at the end of this parameter.  */
10044         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10045             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10046             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10047           {
10048             identifier = cp_parser_identifier (parser);
10049             /* Treat invalid names as if the parameter were nameless.  */
10050             if (identifier == error_mark_node)
10051               identifier = NULL_TREE;
10052           }
10053         else
10054           identifier = NULL_TREE;
10055
10056         /* Create the template parameter.  */
10057         parameter = finish_template_template_parm (class_type_node,
10058                                                    identifier);
10059
10060         /* If the next token is an `=', then there is a
10061            default-argument.  */
10062         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10063           {
10064             bool is_template;
10065
10066             /* Consume the `='.  */
10067             cp_lexer_consume_token (parser->lexer);
10068             /* Parse the id-expression.  */
10069             push_deferring_access_checks (dk_no_deferred);
10070             /* save token before parsing the id-expression, for error
10071                reporting */
10072             token = cp_lexer_peek_token (parser->lexer);
10073             default_argument
10074               = cp_parser_id_expression (parser,
10075                                          /*template_keyword_p=*/false,
10076                                          /*check_dependency_p=*/true,
10077                                          /*template_p=*/&is_template,
10078                                          /*declarator_p=*/false,
10079                                          /*optional_p=*/false);
10080             if (TREE_CODE (default_argument) == TYPE_DECL)
10081               /* If the id-expression was a template-id that refers to
10082                  a template-class, we already have the declaration here,
10083                  so no further lookup is needed.  */
10084                  ;
10085             else
10086               /* Look up the name.  */
10087               default_argument
10088                 = cp_parser_lookup_name (parser, default_argument,
10089                                          none_type,
10090                                          /*is_template=*/is_template,
10091                                          /*is_namespace=*/false,
10092                                          /*check_dependency=*/true,
10093                                          /*ambiguous_decls=*/NULL,
10094                                          token->location);
10095             /* See if the default argument is valid.  */
10096             default_argument
10097               = check_template_template_default_arg (default_argument);
10098
10099             /* Template parameter packs cannot have default
10100                arguments. */
10101             if (*is_parameter_pack)
10102               {
10103                 if (identifier)
10104                   error_at (token->location,
10105                             "template parameter pack %qD cannot "
10106                             "have a default argument",
10107                             identifier);
10108                 else
10109                   error_at (token->location, "template parameter packs cannot "
10110                             "have default arguments");
10111                 default_argument = NULL_TREE;
10112               }
10113             pop_deferring_access_checks ();
10114           }
10115         else
10116           default_argument = NULL_TREE;
10117
10118         /* Create the combined representation of the parameter and the
10119            default argument.  */
10120         parameter = build_tree_list (default_argument, parameter);
10121       }
10122       break;
10123
10124     default:
10125       gcc_unreachable ();
10126       break;
10127     }
10128
10129   return parameter;
10130 }
10131
10132 /* Parse a template-id.
10133
10134    template-id:
10135      template-name < template-argument-list [opt] >
10136
10137    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10138    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10139    returned.  Otherwise, if the template-name names a function, or set
10140    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10141    names a class, returns a TYPE_DECL for the specialization.
10142
10143    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10144    uninstantiated templates.  */
10145
10146 static tree
10147 cp_parser_template_id (cp_parser *parser,
10148                        bool template_keyword_p,
10149                        bool check_dependency_p,
10150                        bool is_declaration)
10151 {
10152   int i;
10153   tree templ;
10154   tree arguments;
10155   tree template_id;
10156   cp_token_position start_of_id = 0;
10157   deferred_access_check *chk;
10158   VEC (deferred_access_check,gc) *access_check;
10159   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10160   bool is_identifier;
10161
10162   /* If the next token corresponds to a template-id, there is no need
10163      to reparse it.  */
10164   next_token = cp_lexer_peek_token (parser->lexer);
10165   if (next_token->type == CPP_TEMPLATE_ID)
10166     {
10167       struct tree_check *check_value;
10168
10169       /* Get the stored value.  */
10170       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10171       /* Perform any access checks that were deferred.  */
10172       access_check = check_value->checks;
10173       if (access_check)
10174         {
10175           for (i = 0 ;
10176                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10177                ++i)
10178             {
10179               perform_or_defer_access_check (chk->binfo,
10180                                              chk->decl,
10181                                              chk->diag_decl);
10182             }
10183         }
10184       /* Return the stored value.  */
10185       return check_value->value;
10186     }
10187
10188   /* Avoid performing name lookup if there is no possibility of
10189      finding a template-id.  */
10190   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10191       || (next_token->type == CPP_NAME
10192           && !cp_parser_nth_token_starts_template_argument_list_p
10193                (parser, 2)))
10194     {
10195       cp_parser_error (parser, "expected template-id");
10196       return error_mark_node;
10197     }
10198
10199   /* Remember where the template-id starts.  */
10200   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10201     start_of_id = cp_lexer_token_position (parser->lexer, false);
10202
10203   push_deferring_access_checks (dk_deferred);
10204
10205   /* Parse the template-name.  */
10206   is_identifier = false;
10207   token = cp_lexer_peek_token (parser->lexer);
10208   templ = cp_parser_template_name (parser, template_keyword_p,
10209                                    check_dependency_p,
10210                                    is_declaration,
10211                                    &is_identifier);
10212   if (templ == error_mark_node || is_identifier)
10213     {
10214       pop_deferring_access_checks ();
10215       return templ;
10216     }
10217
10218   /* If we find the sequence `[:' after a template-name, it's probably
10219      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10220      parse correctly the argument list.  */
10221   next_token = cp_lexer_peek_token (parser->lexer);
10222   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10223   if (next_token->type == CPP_OPEN_SQUARE
10224       && next_token->flags & DIGRAPH
10225       && next_token_2->type == CPP_COLON
10226       && !(next_token_2->flags & PREV_WHITE))
10227     {
10228       cp_parser_parse_tentatively (parser);
10229       /* Change `:' into `::'.  */
10230       next_token_2->type = CPP_SCOPE;
10231       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10232          CPP_LESS.  */
10233       cp_lexer_consume_token (parser->lexer);
10234
10235       /* Parse the arguments.  */
10236       arguments = cp_parser_enclosed_template_argument_list (parser);
10237       if (!cp_parser_parse_definitely (parser))
10238         {
10239           /* If we couldn't parse an argument list, then we revert our changes
10240              and return simply an error. Maybe this is not a template-id
10241              after all.  */
10242           next_token_2->type = CPP_COLON;
10243           cp_parser_error (parser, "expected %<<%>");
10244           pop_deferring_access_checks ();
10245           return error_mark_node;
10246         }
10247       /* Otherwise, emit an error about the invalid digraph, but continue
10248          parsing because we got our argument list.  */
10249       if (permerror (next_token->location,
10250                      "%<<::%> cannot begin a template-argument list"))
10251         {
10252           static bool hint = false;
10253           inform (next_token->location,
10254                   "%<<:%> is an alternate spelling for %<[%>."
10255                   " Insert whitespace between %<<%> and %<::%>");
10256           if (!hint && !flag_permissive)
10257             {
10258               inform (next_token->location, "(if you use %<-fpermissive%>"
10259                       " G++ will accept your code)");
10260               hint = true;
10261             }
10262         }
10263     }
10264   else
10265     {
10266       /* Look for the `<' that starts the template-argument-list.  */
10267       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10268         {
10269           pop_deferring_access_checks ();
10270           return error_mark_node;
10271         }
10272       /* Parse the arguments.  */
10273       arguments = cp_parser_enclosed_template_argument_list (parser);
10274     }
10275
10276   /* Build a representation of the specialization.  */
10277   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10278     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10279   else if (DECL_CLASS_TEMPLATE_P (templ)
10280            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10281     {
10282       bool entering_scope;
10283       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10284          template (rather than some instantiation thereof) only if
10285          is not nested within some other construct.  For example, in
10286          "template <typename T> void f(T) { A<T>::", A<T> is just an
10287          instantiation of A.  */
10288       entering_scope = (template_parm_scope_p ()
10289                         && cp_lexer_next_token_is (parser->lexer,
10290                                                    CPP_SCOPE));
10291       template_id
10292         = finish_template_type (templ, arguments, entering_scope);
10293     }
10294   else
10295     {
10296       /* If it's not a class-template or a template-template, it should be
10297          a function-template.  */
10298       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10299                    || TREE_CODE (templ) == OVERLOAD
10300                    || BASELINK_P (templ)));
10301
10302       template_id = lookup_template_function (templ, arguments);
10303     }
10304
10305   /* If parsing tentatively, replace the sequence of tokens that makes
10306      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10307      should we re-parse the token stream, we will not have to repeat
10308      the effort required to do the parse, nor will we issue duplicate
10309      error messages about problems during instantiation of the
10310      template.  */
10311   if (start_of_id)
10312     {
10313       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10314
10315       /* Reset the contents of the START_OF_ID token.  */
10316       token->type = CPP_TEMPLATE_ID;
10317       /* Retrieve any deferred checks.  Do not pop this access checks yet
10318          so the memory will not be reclaimed during token replacing below.  */
10319       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10320       token->u.tree_check_value->value = template_id;
10321       token->u.tree_check_value->checks = get_deferred_access_checks ();
10322       token->keyword = RID_MAX;
10323
10324       /* Purge all subsequent tokens.  */
10325       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10326
10327       /* ??? Can we actually assume that, if template_id ==
10328          error_mark_node, we will have issued a diagnostic to the
10329          user, as opposed to simply marking the tentative parse as
10330          failed?  */
10331       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10332         error_at (token->location, "parse error in template argument list");
10333     }
10334
10335   pop_deferring_access_checks ();
10336   return template_id;
10337 }
10338
10339 /* Parse a template-name.
10340
10341    template-name:
10342      identifier
10343
10344    The standard should actually say:
10345
10346    template-name:
10347      identifier
10348      operator-function-id
10349
10350    A defect report has been filed about this issue.
10351
10352    A conversion-function-id cannot be a template name because they cannot
10353    be part of a template-id. In fact, looking at this code:
10354
10355    a.operator K<int>()
10356
10357    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10358    It is impossible to call a templated conversion-function-id with an
10359    explicit argument list, since the only allowed template parameter is
10360    the type to which it is converting.
10361
10362    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10363    `template' keyword, in a construction like:
10364
10365      T::template f<3>()
10366
10367    In that case `f' is taken to be a template-name, even though there
10368    is no way of knowing for sure.
10369
10370    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10371    name refers to a set of overloaded functions, at least one of which
10372    is a template, or an IDENTIFIER_NODE with the name of the template,
10373    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10374    names are looked up inside uninstantiated templates.  */
10375
10376 static tree
10377 cp_parser_template_name (cp_parser* parser,
10378                          bool template_keyword_p,
10379                          bool check_dependency_p,
10380                          bool is_declaration,
10381                          bool *is_identifier)
10382 {
10383   tree identifier;
10384   tree decl;
10385   tree fns;
10386   cp_token *token = cp_lexer_peek_token (parser->lexer);
10387
10388   /* If the next token is `operator', then we have either an
10389      operator-function-id or a conversion-function-id.  */
10390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10391     {
10392       /* We don't know whether we're looking at an
10393          operator-function-id or a conversion-function-id.  */
10394       cp_parser_parse_tentatively (parser);
10395       /* Try an operator-function-id.  */
10396       identifier = cp_parser_operator_function_id (parser);
10397       /* If that didn't work, try a conversion-function-id.  */
10398       if (!cp_parser_parse_definitely (parser))
10399         {
10400           cp_parser_error (parser, "expected template-name");
10401           return error_mark_node;
10402         }
10403     }
10404   /* Look for the identifier.  */
10405   else
10406     identifier = cp_parser_identifier (parser);
10407
10408   /* If we didn't find an identifier, we don't have a template-id.  */
10409   if (identifier == error_mark_node)
10410     return error_mark_node;
10411
10412   /* If the name immediately followed the `template' keyword, then it
10413      is a template-name.  However, if the next token is not `<', then
10414      we do not treat it as a template-name, since it is not being used
10415      as part of a template-id.  This enables us to handle constructs
10416      like:
10417
10418        template <typename T> struct S { S(); };
10419        template <typename T> S<T>::S();
10420
10421      correctly.  We would treat `S' as a template -- if it were `S<T>'
10422      -- but we do not if there is no `<'.  */
10423
10424   if (processing_template_decl
10425       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10426     {
10427       /* In a declaration, in a dependent context, we pretend that the
10428          "template" keyword was present in order to improve error
10429          recovery.  For example, given:
10430
10431            template <typename T> void f(T::X<int>);
10432
10433          we want to treat "X<int>" as a template-id.  */
10434       if (is_declaration
10435           && !template_keyword_p
10436           && parser->scope && TYPE_P (parser->scope)
10437           && check_dependency_p
10438           && dependent_scope_p (parser->scope)
10439           /* Do not do this for dtors (or ctors), since they never
10440              need the template keyword before their name.  */
10441           && !constructor_name_p (identifier, parser->scope))
10442         {
10443           cp_token_position start = 0;
10444
10445           /* Explain what went wrong.  */
10446           error_at (token->location, "non-template %qD used as template",
10447                     identifier);
10448           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10449                   parser->scope, identifier);
10450           /* If parsing tentatively, find the location of the "<" token.  */
10451           if (cp_parser_simulate_error (parser))
10452             start = cp_lexer_token_position (parser->lexer, true);
10453           /* Parse the template arguments so that we can issue error
10454              messages about them.  */
10455           cp_lexer_consume_token (parser->lexer);
10456           cp_parser_enclosed_template_argument_list (parser);
10457           /* Skip tokens until we find a good place from which to
10458              continue parsing.  */
10459           cp_parser_skip_to_closing_parenthesis (parser,
10460                                                  /*recovering=*/true,
10461                                                  /*or_comma=*/true,
10462                                                  /*consume_paren=*/false);
10463           /* If parsing tentatively, permanently remove the
10464              template argument list.  That will prevent duplicate
10465              error messages from being issued about the missing
10466              "template" keyword.  */
10467           if (start)
10468             cp_lexer_purge_tokens_after (parser->lexer, start);
10469           if (is_identifier)
10470             *is_identifier = true;
10471           return identifier;
10472         }
10473
10474       /* If the "template" keyword is present, then there is generally
10475          no point in doing name-lookup, so we just return IDENTIFIER.
10476          But, if the qualifying scope is non-dependent then we can
10477          (and must) do name-lookup normally.  */
10478       if (template_keyword_p
10479           && (!parser->scope
10480               || (TYPE_P (parser->scope)
10481                   && dependent_type_p (parser->scope))))
10482         return identifier;
10483     }
10484
10485   /* Look up the name.  */
10486   decl = cp_parser_lookup_name (parser, identifier,
10487                                 none_type,
10488                                 /*is_template=*/false,
10489                                 /*is_namespace=*/false,
10490                                 check_dependency_p,
10491                                 /*ambiguous_decls=*/NULL,
10492                                 token->location);
10493   decl = maybe_get_template_decl_from_type_decl (decl);
10494
10495   /* If DECL is a template, then the name was a template-name.  */
10496   if (TREE_CODE (decl) == TEMPLATE_DECL)
10497     ;
10498   else
10499     {
10500       tree fn = NULL_TREE;
10501
10502       /* The standard does not explicitly indicate whether a name that
10503          names a set of overloaded declarations, some of which are
10504          templates, is a template-name.  However, such a name should
10505          be a template-name; otherwise, there is no way to form a
10506          template-id for the overloaded templates.  */
10507       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10508       if (TREE_CODE (fns) == OVERLOAD)
10509         for (fn = fns; fn; fn = OVL_NEXT (fn))
10510           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10511             break;
10512
10513       if (!fn)
10514         {
10515           /* The name does not name a template.  */
10516           cp_parser_error (parser, "expected template-name");
10517           return error_mark_node;
10518         }
10519     }
10520
10521   /* If DECL is dependent, and refers to a function, then just return
10522      its name; we will look it up again during template instantiation.  */
10523   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10524     {
10525       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10526       if (TYPE_P (scope) && dependent_type_p (scope))
10527         return identifier;
10528     }
10529
10530   return decl;
10531 }
10532
10533 /* Parse a template-argument-list.
10534
10535    template-argument-list:
10536      template-argument ... [opt]
10537      template-argument-list , template-argument ... [opt]
10538
10539    Returns a TREE_VEC containing the arguments.  */
10540
10541 static tree
10542 cp_parser_template_argument_list (cp_parser* parser)
10543 {
10544   tree fixed_args[10];
10545   unsigned n_args = 0;
10546   unsigned alloced = 10;
10547   tree *arg_ary = fixed_args;
10548   tree vec;
10549   bool saved_in_template_argument_list_p;
10550   bool saved_ice_p;
10551   bool saved_non_ice_p;
10552
10553   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10554   parser->in_template_argument_list_p = true;
10555   /* Even if the template-id appears in an integral
10556      constant-expression, the contents of the argument list do
10557      not.  */
10558   saved_ice_p = parser->integral_constant_expression_p;
10559   parser->integral_constant_expression_p = false;
10560   saved_non_ice_p = parser->non_integral_constant_expression_p;
10561   parser->non_integral_constant_expression_p = false;
10562   /* Parse the arguments.  */
10563   do
10564     {
10565       tree argument;
10566
10567       if (n_args)
10568         /* Consume the comma.  */
10569         cp_lexer_consume_token (parser->lexer);
10570
10571       /* Parse the template-argument.  */
10572       argument = cp_parser_template_argument (parser);
10573
10574       /* If the next token is an ellipsis, we're expanding a template
10575          argument pack. */
10576       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10577         {
10578           if (argument == error_mark_node)
10579             {
10580               cp_token *token = cp_lexer_peek_token (parser->lexer);
10581               error_at (token->location,
10582                         "expected parameter pack before %<...%>");
10583             }
10584           /* Consume the `...' token. */
10585           cp_lexer_consume_token (parser->lexer);
10586
10587           /* Make the argument into a TYPE_PACK_EXPANSION or
10588              EXPR_PACK_EXPANSION. */
10589           argument = make_pack_expansion (argument);
10590         }
10591
10592       if (n_args == alloced)
10593         {
10594           alloced *= 2;
10595
10596           if (arg_ary == fixed_args)
10597             {
10598               arg_ary = XNEWVEC (tree, alloced);
10599               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10600             }
10601           else
10602             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10603         }
10604       arg_ary[n_args++] = argument;
10605     }
10606   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10607
10608   vec = make_tree_vec (n_args);
10609
10610   while (n_args--)
10611     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10612
10613   if (arg_ary != fixed_args)
10614     free (arg_ary);
10615   parser->non_integral_constant_expression_p = saved_non_ice_p;
10616   parser->integral_constant_expression_p = saved_ice_p;
10617   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10618   return vec;
10619 }
10620
10621 /* Parse a template-argument.
10622
10623    template-argument:
10624      assignment-expression
10625      type-id
10626      id-expression
10627
10628    The representation is that of an assignment-expression, type-id, or
10629    id-expression -- except that the qualified id-expression is
10630    evaluated, so that the value returned is either a DECL or an
10631    OVERLOAD.
10632
10633    Although the standard says "assignment-expression", it forbids
10634    throw-expressions or assignments in the template argument.
10635    Therefore, we use "conditional-expression" instead.  */
10636
10637 static tree
10638 cp_parser_template_argument (cp_parser* parser)
10639 {
10640   tree argument;
10641   bool template_p;
10642   bool address_p;
10643   bool maybe_type_id = false;
10644   cp_token *token = NULL, *argument_start_token = NULL;
10645   cp_id_kind idk;
10646
10647   /* There's really no way to know what we're looking at, so we just
10648      try each alternative in order.
10649
10650        [temp.arg]
10651
10652        In a template-argument, an ambiguity between a type-id and an
10653        expression is resolved to a type-id, regardless of the form of
10654        the corresponding template-parameter.
10655
10656      Therefore, we try a type-id first.  */
10657   cp_parser_parse_tentatively (parser);
10658   argument = cp_parser_template_type_arg (parser);
10659   /* If there was no error parsing the type-id but the next token is a
10660      '>>', our behavior depends on which dialect of C++ we're
10661      parsing. In C++98, we probably found a typo for '> >'. But there
10662      are type-id which are also valid expressions. For instance:
10663
10664      struct X { int operator >> (int); };
10665      template <int V> struct Foo {};
10666      Foo<X () >> 5> r;
10667
10668      Here 'X()' is a valid type-id of a function type, but the user just
10669      wanted to write the expression "X() >> 5". Thus, we remember that we
10670      found a valid type-id, but we still try to parse the argument as an
10671      expression to see what happens. 
10672
10673      In C++0x, the '>>' will be considered two separate '>'
10674      tokens.  */
10675   if (!cp_parser_error_occurred (parser)
10676       && cxx_dialect == cxx98
10677       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10678     {
10679       maybe_type_id = true;
10680       cp_parser_abort_tentative_parse (parser);
10681     }
10682   else
10683     {
10684       /* If the next token isn't a `,' or a `>', then this argument wasn't
10685       really finished. This means that the argument is not a valid
10686       type-id.  */
10687       if (!cp_parser_next_token_ends_template_argument_p (parser))
10688         cp_parser_error (parser, "expected template-argument");
10689       /* If that worked, we're done.  */
10690       if (cp_parser_parse_definitely (parser))
10691         return argument;
10692     }
10693   /* We're still not sure what the argument will be.  */
10694   cp_parser_parse_tentatively (parser);
10695   /* Try a template.  */
10696   argument_start_token = cp_lexer_peek_token (parser->lexer);
10697   argument = cp_parser_id_expression (parser,
10698                                       /*template_keyword_p=*/false,
10699                                       /*check_dependency_p=*/true,
10700                                       &template_p,
10701                                       /*declarator_p=*/false,
10702                                       /*optional_p=*/false);
10703   /* If the next token isn't a `,' or a `>', then this argument wasn't
10704      really finished.  */
10705   if (!cp_parser_next_token_ends_template_argument_p (parser))
10706     cp_parser_error (parser, "expected template-argument");
10707   if (!cp_parser_error_occurred (parser))
10708     {
10709       /* Figure out what is being referred to.  If the id-expression
10710          was for a class template specialization, then we will have a
10711          TYPE_DECL at this point.  There is no need to do name lookup
10712          at this point in that case.  */
10713       if (TREE_CODE (argument) != TYPE_DECL)
10714         argument = cp_parser_lookup_name (parser, argument,
10715                                           none_type,
10716                                           /*is_template=*/template_p,
10717                                           /*is_namespace=*/false,
10718                                           /*check_dependency=*/true,
10719                                           /*ambiguous_decls=*/NULL,
10720                                           argument_start_token->location);
10721       if (TREE_CODE (argument) != TEMPLATE_DECL
10722           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10723         cp_parser_error (parser, "expected template-name");
10724     }
10725   if (cp_parser_parse_definitely (parser))
10726     return argument;
10727   /* It must be a non-type argument.  There permitted cases are given
10728      in [temp.arg.nontype]:
10729
10730      -- an integral constant-expression of integral or enumeration
10731         type; or
10732
10733      -- the name of a non-type template-parameter; or
10734
10735      -- the name of an object or function with external linkage...
10736
10737      -- the address of an object or function with external linkage...
10738
10739      -- a pointer to member...  */
10740   /* Look for a non-type template parameter.  */
10741   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10742     {
10743       cp_parser_parse_tentatively (parser);
10744       argument = cp_parser_primary_expression (parser,
10745                                                /*address_p=*/false,
10746                                                /*cast_p=*/false,
10747                                                /*template_arg_p=*/true,
10748                                                &idk);
10749       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10750           || !cp_parser_next_token_ends_template_argument_p (parser))
10751         cp_parser_simulate_error (parser);
10752       if (cp_parser_parse_definitely (parser))
10753         return argument;
10754     }
10755
10756   /* If the next token is "&", the argument must be the address of an
10757      object or function with external linkage.  */
10758   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10759   if (address_p)
10760     cp_lexer_consume_token (parser->lexer);
10761   /* See if we might have an id-expression.  */
10762   token = cp_lexer_peek_token (parser->lexer);
10763   if (token->type == CPP_NAME
10764       || token->keyword == RID_OPERATOR
10765       || token->type == CPP_SCOPE
10766       || token->type == CPP_TEMPLATE_ID
10767       || token->type == CPP_NESTED_NAME_SPECIFIER)
10768     {
10769       cp_parser_parse_tentatively (parser);
10770       argument = cp_parser_primary_expression (parser,
10771                                                address_p,
10772                                                /*cast_p=*/false,
10773                                                /*template_arg_p=*/true,
10774                                                &idk);
10775       if (cp_parser_error_occurred (parser)
10776           || !cp_parser_next_token_ends_template_argument_p (parser))
10777         cp_parser_abort_tentative_parse (parser);
10778       else
10779         {
10780           if (TREE_CODE (argument) == INDIRECT_REF)
10781             {
10782               gcc_assert (REFERENCE_REF_P (argument));
10783               argument = TREE_OPERAND (argument, 0);
10784             }
10785
10786           if (TREE_CODE (argument) == VAR_DECL)
10787             {
10788               /* A variable without external linkage might still be a
10789                  valid constant-expression, so no error is issued here
10790                  if the external-linkage check fails.  */
10791               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10792                 cp_parser_simulate_error (parser);
10793             }
10794           else if (is_overloaded_fn (argument))
10795             /* All overloaded functions are allowed; if the external
10796                linkage test does not pass, an error will be issued
10797                later.  */
10798             ;
10799           else if (address_p
10800                    && (TREE_CODE (argument) == OFFSET_REF
10801                        || TREE_CODE (argument) == SCOPE_REF))
10802             /* A pointer-to-member.  */
10803             ;
10804           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10805             ;
10806           else
10807             cp_parser_simulate_error (parser);
10808
10809           if (cp_parser_parse_definitely (parser))
10810             {
10811               if (address_p)
10812                 argument = build_x_unary_op (ADDR_EXPR, argument,
10813                                              tf_warning_or_error);
10814               return argument;
10815             }
10816         }
10817     }
10818   /* If the argument started with "&", there are no other valid
10819      alternatives at this point.  */
10820   if (address_p)
10821     {
10822       cp_parser_error (parser, "invalid non-type template argument");
10823       return error_mark_node;
10824     }
10825
10826   /* If the argument wasn't successfully parsed as a type-id followed
10827      by '>>', the argument can only be a constant expression now.
10828      Otherwise, we try parsing the constant-expression tentatively,
10829      because the argument could really be a type-id.  */
10830   if (maybe_type_id)
10831     cp_parser_parse_tentatively (parser);
10832   argument = cp_parser_constant_expression (parser,
10833                                             /*allow_non_constant_p=*/false,
10834                                             /*non_constant_p=*/NULL);
10835   argument = fold_non_dependent_expr (argument);
10836   if (!maybe_type_id)
10837     return argument;
10838   if (!cp_parser_next_token_ends_template_argument_p (parser))
10839     cp_parser_error (parser, "expected template-argument");
10840   if (cp_parser_parse_definitely (parser))
10841     return argument;
10842   /* We did our best to parse the argument as a non type-id, but that
10843      was the only alternative that matched (albeit with a '>' after
10844      it). We can assume it's just a typo from the user, and a
10845      diagnostic will then be issued.  */
10846   return cp_parser_template_type_arg (parser);
10847 }
10848
10849 /* Parse an explicit-instantiation.
10850
10851    explicit-instantiation:
10852      template declaration
10853
10854    Although the standard says `declaration', what it really means is:
10855
10856    explicit-instantiation:
10857      template decl-specifier-seq [opt] declarator [opt] ;
10858
10859    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10860    supposed to be allowed.  A defect report has been filed about this
10861    issue.
10862
10863    GNU Extension:
10864
10865    explicit-instantiation:
10866      storage-class-specifier template
10867        decl-specifier-seq [opt] declarator [opt] ;
10868      function-specifier template
10869        decl-specifier-seq [opt] declarator [opt] ;  */
10870
10871 static void
10872 cp_parser_explicit_instantiation (cp_parser* parser)
10873 {
10874   int declares_class_or_enum;
10875   cp_decl_specifier_seq decl_specifiers;
10876   tree extension_specifier = NULL_TREE;
10877   cp_token *token;
10878
10879   /* Look for an (optional) storage-class-specifier or
10880      function-specifier.  */
10881   if (cp_parser_allow_gnu_extensions_p (parser))
10882     {
10883       extension_specifier
10884         = cp_parser_storage_class_specifier_opt (parser);
10885       if (!extension_specifier)
10886         extension_specifier
10887           = cp_parser_function_specifier_opt (parser,
10888                                               /*decl_specs=*/NULL);
10889     }
10890
10891   /* Look for the `template' keyword.  */
10892   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10893   /* Let the front end know that we are processing an explicit
10894      instantiation.  */
10895   begin_explicit_instantiation ();
10896   /* [temp.explicit] says that we are supposed to ignore access
10897      control while processing explicit instantiation directives.  */
10898   push_deferring_access_checks (dk_no_check);
10899   /* Parse a decl-specifier-seq.  */
10900   token = cp_lexer_peek_token (parser->lexer);
10901   cp_parser_decl_specifier_seq (parser,
10902                                 CP_PARSER_FLAGS_OPTIONAL,
10903                                 &decl_specifiers,
10904                                 &declares_class_or_enum);
10905   /* If there was exactly one decl-specifier, and it declared a class,
10906      and there's no declarator, then we have an explicit type
10907      instantiation.  */
10908   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10909     {
10910       tree type;
10911
10912       type = check_tag_decl (&decl_specifiers);
10913       /* Turn access control back on for names used during
10914          template instantiation.  */
10915       pop_deferring_access_checks ();
10916       if (type)
10917         do_type_instantiation (type, extension_specifier,
10918                                /*complain=*/tf_error);
10919     }
10920   else
10921     {
10922       cp_declarator *declarator;
10923       tree decl;
10924
10925       /* Parse the declarator.  */
10926       declarator
10927         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10928                                 /*ctor_dtor_or_conv_p=*/NULL,
10929                                 /*parenthesized_p=*/NULL,
10930                                 /*member_p=*/false);
10931       if (declares_class_or_enum & 2)
10932         cp_parser_check_for_definition_in_return_type (declarator,
10933                                                        decl_specifiers.type,
10934                                                        decl_specifiers.type_location);
10935       if (declarator != cp_error_declarator)
10936         {
10937           decl = grokdeclarator (declarator, &decl_specifiers,
10938                                  NORMAL, 0, &decl_specifiers.attributes);
10939           /* Turn access control back on for names used during
10940              template instantiation.  */
10941           pop_deferring_access_checks ();
10942           /* Do the explicit instantiation.  */
10943           do_decl_instantiation (decl, extension_specifier);
10944         }
10945       else
10946         {
10947           pop_deferring_access_checks ();
10948           /* Skip the body of the explicit instantiation.  */
10949           cp_parser_skip_to_end_of_statement (parser);
10950         }
10951     }
10952   /* We're done with the instantiation.  */
10953   end_explicit_instantiation ();
10954
10955   cp_parser_consume_semicolon_at_end_of_statement (parser);
10956 }
10957
10958 /* Parse an explicit-specialization.
10959
10960    explicit-specialization:
10961      template < > declaration
10962
10963    Although the standard says `declaration', what it really means is:
10964
10965    explicit-specialization:
10966      template <> decl-specifier [opt] init-declarator [opt] ;
10967      template <> function-definition
10968      template <> explicit-specialization
10969      template <> template-declaration  */
10970
10971 static void
10972 cp_parser_explicit_specialization (cp_parser* parser)
10973 {
10974   bool need_lang_pop;
10975   cp_token *token = cp_lexer_peek_token (parser->lexer);
10976
10977   /* Look for the `template' keyword.  */
10978   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10979   /* Look for the `<'.  */
10980   cp_parser_require (parser, CPP_LESS, "%<<%>");
10981   /* Look for the `>'.  */
10982   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10983   /* We have processed another parameter list.  */
10984   ++parser->num_template_parameter_lists;
10985   /* [temp]
10986
10987      A template ... explicit specialization ... shall not have C
10988      linkage.  */
10989   if (current_lang_name == lang_name_c)
10990     {
10991       error_at (token->location, "template specialization with C linkage");
10992       /* Give it C++ linkage to avoid confusing other parts of the
10993          front end.  */
10994       push_lang_context (lang_name_cplusplus);
10995       need_lang_pop = true;
10996     }
10997   else
10998     need_lang_pop = false;
10999   /* Let the front end know that we are beginning a specialization.  */
11000   if (!begin_specialization ())
11001     {
11002       end_specialization ();
11003       return;
11004     }
11005
11006   /* If the next keyword is `template', we need to figure out whether
11007      or not we're looking a template-declaration.  */
11008   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11009     {
11010       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11011           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11012         cp_parser_template_declaration_after_export (parser,
11013                                                      /*member_p=*/false);
11014       else
11015         cp_parser_explicit_specialization (parser);
11016     }
11017   else
11018     /* Parse the dependent declaration.  */
11019     cp_parser_single_declaration (parser,
11020                                   /*checks=*/NULL,
11021                                   /*member_p=*/false,
11022                                   /*explicit_specialization_p=*/true,
11023                                   /*friend_p=*/NULL);
11024   /* We're done with the specialization.  */
11025   end_specialization ();
11026   /* For the erroneous case of a template with C linkage, we pushed an
11027      implicit C++ linkage scope; exit that scope now.  */
11028   if (need_lang_pop)
11029     pop_lang_context ();
11030   /* We're done with this parameter list.  */
11031   --parser->num_template_parameter_lists;
11032 }
11033
11034 /* Parse a type-specifier.
11035
11036    type-specifier:
11037      simple-type-specifier
11038      class-specifier
11039      enum-specifier
11040      elaborated-type-specifier
11041      cv-qualifier
11042
11043    GNU Extension:
11044
11045    type-specifier:
11046      __complex__
11047
11048    Returns a representation of the type-specifier.  For a
11049    class-specifier, enum-specifier, or elaborated-type-specifier, a
11050    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11051
11052    The parser flags FLAGS is used to control type-specifier parsing.
11053
11054    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11055    in a decl-specifier-seq.
11056
11057    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11058    class-specifier, enum-specifier, or elaborated-type-specifier, then
11059    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11060    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11061    zero.
11062
11063    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11064    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11065    is set to FALSE.  */
11066
11067 static tree
11068 cp_parser_type_specifier (cp_parser* parser,
11069                           cp_parser_flags flags,
11070                           cp_decl_specifier_seq *decl_specs,
11071                           bool is_declaration,
11072                           int* declares_class_or_enum,
11073                           bool* is_cv_qualifier)
11074 {
11075   tree type_spec = NULL_TREE;
11076   cp_token *token;
11077   enum rid keyword;
11078   cp_decl_spec ds = ds_last;
11079
11080   /* Assume this type-specifier does not declare a new type.  */
11081   if (declares_class_or_enum)
11082     *declares_class_or_enum = 0;
11083   /* And that it does not specify a cv-qualifier.  */
11084   if (is_cv_qualifier)
11085     *is_cv_qualifier = false;
11086   /* Peek at the next token.  */
11087   token = cp_lexer_peek_token (parser->lexer);
11088
11089   /* If we're looking at a keyword, we can use that to guide the
11090      production we choose.  */
11091   keyword = token->keyword;
11092   switch (keyword)
11093     {
11094     case RID_ENUM:
11095       /* Look for the enum-specifier.  */
11096       type_spec = cp_parser_enum_specifier (parser);
11097       /* If that worked, we're done.  */
11098       if (type_spec)
11099         {
11100           if (declares_class_or_enum)
11101             *declares_class_or_enum = 2;
11102           if (decl_specs)
11103             cp_parser_set_decl_spec_type (decl_specs,
11104                                           type_spec,
11105                                           token->location,
11106                                           /*user_defined_p=*/true);
11107           return type_spec;
11108         }
11109       else
11110         goto elaborated_type_specifier;
11111
11112       /* Any of these indicate either a class-specifier, or an
11113          elaborated-type-specifier.  */
11114     case RID_CLASS:
11115     case RID_STRUCT:
11116     case RID_UNION:
11117       /* Parse tentatively so that we can back up if we don't find a
11118          class-specifier.  */
11119       cp_parser_parse_tentatively (parser);
11120       /* Look for the class-specifier.  */
11121       type_spec = cp_parser_class_specifier (parser);
11122       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11123       /* If that worked, we're done.  */
11124       if (cp_parser_parse_definitely (parser))
11125         {
11126           if (declares_class_or_enum)
11127             *declares_class_or_enum = 2;
11128           if (decl_specs)
11129             cp_parser_set_decl_spec_type (decl_specs,
11130                                           type_spec,
11131                                           token->location,
11132                                           /*user_defined_p=*/true);
11133           return type_spec;
11134         }
11135
11136       /* Fall through.  */
11137     elaborated_type_specifier:
11138       /* We're declaring (not defining) a class or enum.  */
11139       if (declares_class_or_enum)
11140         *declares_class_or_enum = 1;
11141
11142       /* Fall through.  */
11143     case RID_TYPENAME:
11144       /* Look for an elaborated-type-specifier.  */
11145       type_spec
11146         = (cp_parser_elaborated_type_specifier
11147            (parser,
11148             decl_specs && decl_specs->specs[(int) ds_friend],
11149             is_declaration));
11150       if (decl_specs)
11151         cp_parser_set_decl_spec_type (decl_specs,
11152                                       type_spec,
11153                                       token->location,
11154                                       /*user_defined_p=*/true);
11155       return type_spec;
11156
11157     case RID_CONST:
11158       ds = ds_const;
11159       if (is_cv_qualifier)
11160         *is_cv_qualifier = true;
11161       break;
11162
11163     case RID_VOLATILE:
11164       ds = ds_volatile;
11165       if (is_cv_qualifier)
11166         *is_cv_qualifier = true;
11167       break;
11168
11169     case RID_RESTRICT:
11170       ds = ds_restrict;
11171       if (is_cv_qualifier)
11172         *is_cv_qualifier = true;
11173       break;
11174
11175     case RID_COMPLEX:
11176       /* The `__complex__' keyword is a GNU extension.  */
11177       ds = ds_complex;
11178       break;
11179
11180     default:
11181       break;
11182     }
11183
11184   /* Handle simple keywords.  */
11185   if (ds != ds_last)
11186     {
11187       if (decl_specs)
11188         {
11189           ++decl_specs->specs[(int)ds];
11190           decl_specs->any_specifiers_p = true;
11191         }
11192       return cp_lexer_consume_token (parser->lexer)->u.value;
11193     }
11194
11195   /* If we do not already have a type-specifier, assume we are looking
11196      at a simple-type-specifier.  */
11197   type_spec = cp_parser_simple_type_specifier (parser,
11198                                                decl_specs,
11199                                                flags);
11200
11201   /* If we didn't find a type-specifier, and a type-specifier was not
11202      optional in this context, issue an error message.  */
11203   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11204     {
11205       cp_parser_error (parser, "expected type specifier");
11206       return error_mark_node;
11207     }
11208
11209   return type_spec;
11210 }
11211
11212 /* Parse a simple-type-specifier.
11213
11214    simple-type-specifier:
11215      :: [opt] nested-name-specifier [opt] type-name
11216      :: [opt] nested-name-specifier template template-id
11217      char
11218      wchar_t
11219      bool
11220      short
11221      int
11222      long
11223      signed
11224      unsigned
11225      float
11226      double
11227      void
11228
11229    C++0x Extension:
11230
11231    simple-type-specifier:
11232      auto
11233      decltype ( expression )   
11234      char16_t
11235      char32_t
11236
11237    GNU Extension:
11238
11239    simple-type-specifier:
11240      __typeof__ unary-expression
11241      __typeof__ ( type-id )
11242
11243    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11244    appropriately updated.  */
11245
11246 static tree
11247 cp_parser_simple_type_specifier (cp_parser* parser,
11248                                  cp_decl_specifier_seq *decl_specs,
11249                                  cp_parser_flags flags)
11250 {
11251   tree type = NULL_TREE;
11252   cp_token *token;
11253
11254   /* Peek at the next token.  */
11255   token = cp_lexer_peek_token (parser->lexer);
11256
11257   /* If we're looking at a keyword, things are easy.  */
11258   switch (token->keyword)
11259     {
11260     case RID_CHAR:
11261       if (decl_specs)
11262         decl_specs->explicit_char_p = true;
11263       type = char_type_node;
11264       break;
11265     case RID_CHAR16:
11266       type = char16_type_node;
11267       break;
11268     case RID_CHAR32:
11269       type = char32_type_node;
11270       break;
11271     case RID_WCHAR:
11272       type = wchar_type_node;
11273       break;
11274     case RID_BOOL:
11275       type = boolean_type_node;
11276       break;
11277     case RID_SHORT:
11278       if (decl_specs)
11279         ++decl_specs->specs[(int) ds_short];
11280       type = short_integer_type_node;
11281       break;
11282     case RID_INT:
11283       if (decl_specs)
11284         decl_specs->explicit_int_p = true;
11285       type = integer_type_node;
11286       break;
11287     case RID_LONG:
11288       if (decl_specs)
11289         ++decl_specs->specs[(int) ds_long];
11290       type = long_integer_type_node;
11291       break;
11292     case RID_SIGNED:
11293       if (decl_specs)
11294         ++decl_specs->specs[(int) ds_signed];
11295       type = integer_type_node;
11296       break;
11297     case RID_UNSIGNED:
11298       if (decl_specs)
11299         ++decl_specs->specs[(int) ds_unsigned];
11300       type = unsigned_type_node;
11301       break;
11302     case RID_FLOAT:
11303       type = float_type_node;
11304       break;
11305     case RID_DOUBLE:
11306       type = double_type_node;
11307       break;
11308     case RID_VOID:
11309       type = void_type_node;
11310       break;
11311       
11312     case RID_AUTO:
11313       maybe_warn_cpp0x ("C++0x auto");
11314       type = make_auto ();
11315       break;
11316
11317     case RID_DECLTYPE:
11318       /* Parse the `decltype' type.  */
11319       type = cp_parser_decltype (parser);
11320
11321       if (decl_specs)
11322         cp_parser_set_decl_spec_type (decl_specs, type,
11323                                       token->location,
11324                                       /*user_defined_p=*/true);
11325
11326       return type;
11327
11328     case RID_TYPEOF:
11329       /* Consume the `typeof' token.  */
11330       cp_lexer_consume_token (parser->lexer);
11331       /* Parse the operand to `typeof'.  */
11332       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11333       /* If it is not already a TYPE, take its type.  */
11334       if (!TYPE_P (type))
11335         type = finish_typeof (type);
11336
11337       if (decl_specs)
11338         cp_parser_set_decl_spec_type (decl_specs, type,
11339                                       token->location,
11340                                       /*user_defined_p=*/true);
11341
11342       return type;
11343
11344     default:
11345       break;
11346     }
11347
11348   /* If the type-specifier was for a built-in type, we're done.  */
11349   if (type)
11350     {
11351       tree id;
11352
11353       /* Record the type.  */
11354       if (decl_specs
11355           && (token->keyword != RID_SIGNED
11356               && token->keyword != RID_UNSIGNED
11357               && token->keyword != RID_SHORT
11358               && token->keyword != RID_LONG))
11359         cp_parser_set_decl_spec_type (decl_specs,
11360                                       type,
11361                                       token->location,
11362                                       /*user_defined=*/false);
11363       if (decl_specs)
11364         decl_specs->any_specifiers_p = true;
11365
11366       /* Consume the token.  */
11367       id = cp_lexer_consume_token (parser->lexer)->u.value;
11368
11369       /* There is no valid C++ program where a non-template type is
11370          followed by a "<".  That usually indicates that the user thought
11371          that the type was a template.  */
11372       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11373
11374       return TYPE_NAME (type);
11375     }
11376
11377   /* The type-specifier must be a user-defined type.  */
11378   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11379     {
11380       bool qualified_p;
11381       bool global_p;
11382
11383       /* Don't gobble tokens or issue error messages if this is an
11384          optional type-specifier.  */
11385       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11386         cp_parser_parse_tentatively (parser);
11387
11388       /* Look for the optional `::' operator.  */
11389       global_p
11390         = (cp_parser_global_scope_opt (parser,
11391                                        /*current_scope_valid_p=*/false)
11392            != NULL_TREE);
11393       /* Look for the nested-name specifier.  */
11394       qualified_p
11395         = (cp_parser_nested_name_specifier_opt (parser,
11396                                                 /*typename_keyword_p=*/false,
11397                                                 /*check_dependency_p=*/true,
11398                                                 /*type_p=*/false,
11399                                                 /*is_declaration=*/false)
11400            != NULL_TREE);
11401       token = cp_lexer_peek_token (parser->lexer);
11402       /* If we have seen a nested-name-specifier, and the next token
11403          is `template', then we are using the template-id production.  */
11404       if (parser->scope
11405           && cp_parser_optional_template_keyword (parser))
11406         {
11407           /* Look for the template-id.  */
11408           type = cp_parser_template_id (parser,
11409                                         /*template_keyword_p=*/true,
11410                                         /*check_dependency_p=*/true,
11411                                         /*is_declaration=*/false);
11412           /* If the template-id did not name a type, we are out of
11413              luck.  */
11414           if (TREE_CODE (type) != TYPE_DECL)
11415             {
11416               cp_parser_error (parser, "expected template-id for type");
11417               type = NULL_TREE;
11418             }
11419         }
11420       /* Otherwise, look for a type-name.  */
11421       else
11422         type = cp_parser_type_name (parser);
11423       /* Keep track of all name-lookups performed in class scopes.  */
11424       if (type
11425           && !global_p
11426           && !qualified_p
11427           && TREE_CODE (type) == TYPE_DECL
11428           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11429         maybe_note_name_used_in_class (DECL_NAME (type), type);
11430       /* If it didn't work out, we don't have a TYPE.  */
11431       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11432           && !cp_parser_parse_definitely (parser))
11433         type = NULL_TREE;
11434       if (type && decl_specs)
11435         cp_parser_set_decl_spec_type (decl_specs, type,
11436                                       token->location,
11437                                       /*user_defined=*/true);
11438     }
11439
11440   /* If we didn't get a type-name, issue an error message.  */
11441   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11442     {
11443       cp_parser_error (parser, "expected type-name");
11444       return error_mark_node;
11445     }
11446
11447   /* There is no valid C++ program where a non-template type is
11448      followed by a "<".  That usually indicates that the user thought
11449      that the type was a template.  */
11450   if (type && type != error_mark_node)
11451     {
11452       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11453          If it is, then the '<'...'>' enclose protocol names rather than
11454          template arguments, and so everything is fine.  */
11455       if (c_dialect_objc ()
11456           && (objc_is_id (type) || objc_is_class_name (type)))
11457         {
11458           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11459           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11460
11461           /* Clobber the "unqualified" type previously entered into
11462              DECL_SPECS with the new, improved protocol-qualified version.  */
11463           if (decl_specs)
11464             decl_specs->type = qual_type;
11465
11466           return qual_type;
11467         }
11468
11469       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11470                                                token->location);
11471     }
11472
11473   return type;
11474 }
11475
11476 /* Parse a type-name.
11477
11478    type-name:
11479      class-name
11480      enum-name
11481      typedef-name
11482
11483    enum-name:
11484      identifier
11485
11486    typedef-name:
11487      identifier
11488
11489    Returns a TYPE_DECL for the type.  */
11490
11491 static tree
11492 cp_parser_type_name (cp_parser* parser)
11493 {
11494   tree type_decl;
11495
11496   /* We can't know yet whether it is a class-name or not.  */
11497   cp_parser_parse_tentatively (parser);
11498   /* Try a class-name.  */
11499   type_decl = cp_parser_class_name (parser,
11500                                     /*typename_keyword_p=*/false,
11501                                     /*template_keyword_p=*/false,
11502                                     none_type,
11503                                     /*check_dependency_p=*/true,
11504                                     /*class_head_p=*/false,
11505                                     /*is_declaration=*/false);
11506   /* If it's not a class-name, keep looking.  */
11507   if (!cp_parser_parse_definitely (parser))
11508     {
11509       /* It must be a typedef-name or an enum-name.  */
11510       return cp_parser_nonclass_name (parser);
11511     }
11512
11513   return type_decl;
11514 }
11515
11516 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11517
11518    enum-name:
11519      identifier
11520
11521    typedef-name:
11522      identifier
11523
11524    Returns a TYPE_DECL for the type.  */
11525
11526 static tree
11527 cp_parser_nonclass_name (cp_parser* parser)
11528 {
11529   tree type_decl;
11530   tree identifier;
11531
11532   cp_token *token = cp_lexer_peek_token (parser->lexer);
11533   identifier = cp_parser_identifier (parser);
11534   if (identifier == error_mark_node)
11535     return error_mark_node;
11536
11537   /* Look up the type-name.  */
11538   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11539
11540   if (TREE_CODE (type_decl) != TYPE_DECL
11541       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11542     {
11543       /* See if this is an Objective-C type.  */
11544       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11545       tree type = objc_get_protocol_qualified_type (identifier, protos);
11546       if (type)
11547         type_decl = TYPE_NAME (type);
11548     }
11549   
11550   /* Issue an error if we did not find a type-name.  */
11551   if (TREE_CODE (type_decl) != TYPE_DECL)
11552     {
11553       if (!cp_parser_simulate_error (parser))
11554         cp_parser_name_lookup_error (parser, identifier, type_decl,
11555                                      "is not a type", token->location);
11556       return error_mark_node;
11557     }
11558   /* Remember that the name was used in the definition of the
11559      current class so that we can check later to see if the
11560      meaning would have been different after the class was
11561      entirely defined.  */
11562   else if (type_decl != error_mark_node
11563            && !parser->scope)
11564     maybe_note_name_used_in_class (identifier, type_decl);
11565   
11566   return type_decl;
11567 }
11568
11569 /* Parse an elaborated-type-specifier.  Note that the grammar given
11570    here incorporates the resolution to DR68.
11571
11572    elaborated-type-specifier:
11573      class-key :: [opt] nested-name-specifier [opt] identifier
11574      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11575      enum-key :: [opt] nested-name-specifier [opt] identifier
11576      typename :: [opt] nested-name-specifier identifier
11577      typename :: [opt] nested-name-specifier template [opt]
11578        template-id
11579
11580    GNU extension:
11581
11582    elaborated-type-specifier:
11583      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11584      class-key attributes :: [opt] nested-name-specifier [opt]
11585                template [opt] template-id
11586      enum attributes :: [opt] nested-name-specifier [opt] identifier
11587
11588    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11589    declared `friend'.  If IS_DECLARATION is TRUE, then this
11590    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11591    something is being declared.
11592
11593    Returns the TYPE specified.  */
11594
11595 static tree
11596 cp_parser_elaborated_type_specifier (cp_parser* parser,
11597                                      bool is_friend,
11598                                      bool is_declaration)
11599 {
11600   enum tag_types tag_type;
11601   tree identifier;
11602   tree type = NULL_TREE;
11603   tree attributes = NULL_TREE;
11604   tree globalscope;
11605   cp_token *token = NULL;
11606
11607   /* See if we're looking at the `enum' keyword.  */
11608   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11609     {
11610       /* Consume the `enum' token.  */
11611       cp_lexer_consume_token (parser->lexer);
11612       /* Remember that it's an enumeration type.  */
11613       tag_type = enum_type;
11614       /* Parse the optional `struct' or `class' key (for C++0x scoped
11615          enums).  */
11616       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11617           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11618         {
11619           if (cxx_dialect == cxx98)
11620             maybe_warn_cpp0x ("scoped enums");
11621
11622           /* Consume the `struct' or `class'.  */
11623           cp_lexer_consume_token (parser->lexer);
11624         }
11625       /* Parse the attributes.  */
11626       attributes = cp_parser_attributes_opt (parser);
11627     }
11628   /* Or, it might be `typename'.  */
11629   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11630                                            RID_TYPENAME))
11631     {
11632       /* Consume the `typename' token.  */
11633       cp_lexer_consume_token (parser->lexer);
11634       /* Remember that it's a `typename' type.  */
11635       tag_type = typename_type;
11636     }
11637   /* Otherwise it must be a class-key.  */
11638   else
11639     {
11640       tag_type = cp_parser_class_key (parser);
11641       if (tag_type == none_type)
11642         return error_mark_node;
11643       /* Parse the attributes.  */
11644       attributes = cp_parser_attributes_opt (parser);
11645     }
11646
11647   /* Look for the `::' operator.  */
11648   globalscope =  cp_parser_global_scope_opt (parser,
11649                                              /*current_scope_valid_p=*/false);
11650   /* Look for the nested-name-specifier.  */
11651   if (tag_type == typename_type && !globalscope)
11652     {
11653       if (!cp_parser_nested_name_specifier (parser,
11654                                            /*typename_keyword_p=*/true,
11655                                            /*check_dependency_p=*/true,
11656                                            /*type_p=*/true,
11657                                             is_declaration))
11658         return error_mark_node;
11659     }
11660   else
11661     /* Even though `typename' is not present, the proposed resolution
11662        to Core Issue 180 says that in `class A<T>::B', `B' should be
11663        considered a type-name, even if `A<T>' is dependent.  */
11664     cp_parser_nested_name_specifier_opt (parser,
11665                                          /*typename_keyword_p=*/true,
11666                                          /*check_dependency_p=*/true,
11667                                          /*type_p=*/true,
11668                                          is_declaration);
11669  /* For everything but enumeration types, consider a template-id.
11670     For an enumeration type, consider only a plain identifier.  */
11671   if (tag_type != enum_type)
11672     {
11673       bool template_p = false;
11674       tree decl;
11675
11676       /* Allow the `template' keyword.  */
11677       template_p = cp_parser_optional_template_keyword (parser);
11678       /* If we didn't see `template', we don't know if there's a
11679          template-id or not.  */
11680       if (!template_p)
11681         cp_parser_parse_tentatively (parser);
11682       /* Parse the template-id.  */
11683       token = cp_lexer_peek_token (parser->lexer);
11684       decl = cp_parser_template_id (parser, template_p,
11685                                     /*check_dependency_p=*/true,
11686                                     is_declaration);
11687       /* If we didn't find a template-id, look for an ordinary
11688          identifier.  */
11689       if (!template_p && !cp_parser_parse_definitely (parser))
11690         ;
11691       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11692          in effect, then we must assume that, upon instantiation, the
11693          template will correspond to a class.  */
11694       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11695                && tag_type == typename_type)
11696         type = make_typename_type (parser->scope, decl,
11697                                    typename_type,
11698                                    /*complain=*/tf_error);
11699       /* If the `typename' keyword is in effect and DECL is not a type
11700          decl. Then type is non existant.   */
11701       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11702         type = NULL_TREE; 
11703       else 
11704         type = TREE_TYPE (decl);
11705     }
11706
11707   if (!type)
11708     {
11709       token = cp_lexer_peek_token (parser->lexer);
11710       identifier = cp_parser_identifier (parser);
11711
11712       if (identifier == error_mark_node)
11713         {
11714           parser->scope = NULL_TREE;
11715           return error_mark_node;
11716         }
11717
11718       /* For a `typename', we needn't call xref_tag.  */
11719       if (tag_type == typename_type
11720           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11721         return cp_parser_make_typename_type (parser, parser->scope,
11722                                              identifier,
11723                                              token->location);
11724       /* Look up a qualified name in the usual way.  */
11725       if (parser->scope)
11726         {
11727           tree decl;
11728           tree ambiguous_decls;
11729
11730           decl = cp_parser_lookup_name (parser, identifier,
11731                                         tag_type,
11732                                         /*is_template=*/false,
11733                                         /*is_namespace=*/false,
11734                                         /*check_dependency=*/true,
11735                                         &ambiguous_decls,
11736                                         token->location);
11737
11738           /* If the lookup was ambiguous, an error will already have been
11739              issued.  */
11740           if (ambiguous_decls)
11741             return error_mark_node;
11742
11743           /* If we are parsing friend declaration, DECL may be a
11744              TEMPLATE_DECL tree node here.  However, we need to check
11745              whether this TEMPLATE_DECL results in valid code.  Consider
11746              the following example:
11747
11748                namespace N {
11749                  template <class T> class C {};
11750                }
11751                class X {
11752                  template <class T> friend class N::C; // #1, valid code
11753                };
11754                template <class T> class Y {
11755                  friend class N::C;                    // #2, invalid code
11756                };
11757
11758              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11759              name lookup of `N::C'.  We see that friend declaration must
11760              be template for the code to be valid.  Note that
11761              processing_template_decl does not work here since it is
11762              always 1 for the above two cases.  */
11763
11764           decl = (cp_parser_maybe_treat_template_as_class
11765                   (decl, /*tag_name_p=*/is_friend
11766                          && parser->num_template_parameter_lists));
11767
11768           if (TREE_CODE (decl) != TYPE_DECL)
11769             {
11770               cp_parser_diagnose_invalid_type_name (parser,
11771                                                     parser->scope,
11772                                                     identifier,
11773                                                     token->location);
11774               return error_mark_node;
11775             }
11776
11777           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11778             {
11779               bool allow_template = (parser->num_template_parameter_lists
11780                                       || DECL_SELF_REFERENCE_P (decl));
11781               type = check_elaborated_type_specifier (tag_type, decl, 
11782                                                       allow_template);
11783
11784               if (type == error_mark_node)
11785                 return error_mark_node;
11786             }
11787
11788           /* Forward declarations of nested types, such as
11789
11790                class C1::C2;
11791                class C1::C2::C3;
11792
11793              are invalid unless all components preceding the final '::'
11794              are complete.  If all enclosing types are complete, these
11795              declarations become merely pointless.
11796
11797              Invalid forward declarations of nested types are errors
11798              caught elsewhere in parsing.  Those that are pointless arrive
11799              here.  */
11800
11801           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11802               && !is_friend && !processing_explicit_instantiation)
11803             warning (0, "declaration %qD does not declare anything", decl);
11804
11805           type = TREE_TYPE (decl);
11806         }
11807       else
11808         {
11809           /* An elaborated-type-specifier sometimes introduces a new type and
11810              sometimes names an existing type.  Normally, the rule is that it
11811              introduces a new type only if there is not an existing type of
11812              the same name already in scope.  For example, given:
11813
11814                struct S {};
11815                void f() { struct S s; }
11816
11817              the `struct S' in the body of `f' is the same `struct S' as in
11818              the global scope; the existing definition is used.  However, if
11819              there were no global declaration, this would introduce a new
11820              local class named `S'.
11821
11822              An exception to this rule applies to the following code:
11823
11824                namespace N { struct S; }
11825
11826              Here, the elaborated-type-specifier names a new type
11827              unconditionally; even if there is already an `S' in the
11828              containing scope this declaration names a new type.
11829              This exception only applies if the elaborated-type-specifier
11830              forms the complete declaration:
11831
11832                [class.name]
11833
11834                A declaration consisting solely of `class-key identifier ;' is
11835                either a redeclaration of the name in the current scope or a
11836                forward declaration of the identifier as a class name.  It
11837                introduces the name into the current scope.
11838
11839              We are in this situation precisely when the next token is a `;'.
11840
11841              An exception to the exception is that a `friend' declaration does
11842              *not* name a new type; i.e., given:
11843
11844                struct S { friend struct T; };
11845
11846              `T' is not a new type in the scope of `S'.
11847
11848              Also, `new struct S' or `sizeof (struct S)' never results in the
11849              definition of a new type; a new type can only be declared in a
11850              declaration context.  */
11851
11852           tag_scope ts;
11853           bool template_p;
11854
11855           if (is_friend)
11856             /* Friends have special name lookup rules.  */
11857             ts = ts_within_enclosing_non_class;
11858           else if (is_declaration
11859                    && cp_lexer_next_token_is (parser->lexer,
11860                                               CPP_SEMICOLON))
11861             /* This is a `class-key identifier ;' */
11862             ts = ts_current;
11863           else
11864             ts = ts_global;
11865
11866           template_p =
11867             (parser->num_template_parameter_lists
11868              && (cp_parser_next_token_starts_class_definition_p (parser)
11869                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11870           /* An unqualified name was used to reference this type, so
11871              there were no qualifying templates.  */
11872           if (!cp_parser_check_template_parameters (parser,
11873                                                     /*num_templates=*/0,
11874                                                     token->location,
11875                                                     /*declarator=*/NULL))
11876             return error_mark_node;
11877           type = xref_tag (tag_type, identifier, ts, template_p);
11878         }
11879     }
11880
11881   if (type == error_mark_node)
11882     return error_mark_node;
11883
11884   /* Allow attributes on forward declarations of classes.  */
11885   if (attributes)
11886     {
11887       if (TREE_CODE (type) == TYPENAME_TYPE)
11888         warning (OPT_Wattributes,
11889                  "attributes ignored on uninstantiated type");
11890       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11891                && ! processing_explicit_instantiation)
11892         warning (OPT_Wattributes,
11893                  "attributes ignored on template instantiation");
11894       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11895         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11896       else
11897         warning (OPT_Wattributes,
11898                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11899     }
11900
11901   if (tag_type != enum_type)
11902     cp_parser_check_class_key (tag_type, type);
11903
11904   /* A "<" cannot follow an elaborated type specifier.  If that
11905      happens, the user was probably trying to form a template-id.  */
11906   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11907
11908   return type;
11909 }
11910
11911 /* Parse an enum-specifier.
11912
11913    enum-specifier:
11914      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11915
11916    enum-key:
11917      enum
11918      enum class   [C++0x]
11919      enum struct  [C++0x]
11920
11921    enum-base:   [C++0x]
11922      : type-specifier-seq
11923
11924    GNU Extensions:
11925      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11926        { enumerator-list [opt] }attributes[opt]
11927
11928    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11929    if the token stream isn't an enum-specifier after all.  */
11930
11931 static tree
11932 cp_parser_enum_specifier (cp_parser* parser)
11933 {
11934   tree identifier;
11935   tree type;
11936   tree attributes;
11937   bool scoped_enum_p = false;
11938   bool has_underlying_type = false;
11939   tree underlying_type = NULL_TREE;
11940
11941   /* Parse tentatively so that we can back up if we don't find a
11942      enum-specifier.  */
11943   cp_parser_parse_tentatively (parser);
11944
11945   /* Caller guarantees that the current token is 'enum', an identifier
11946      possibly follows, and the token after that is an opening brace.
11947      If we don't have an identifier, fabricate an anonymous name for
11948      the enumeration being defined.  */
11949   cp_lexer_consume_token (parser->lexer);
11950
11951   /* Parse the "class" or "struct", which indicates a scoped
11952      enumeration type in C++0x.  */
11953   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11954       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11955     {
11956       if (cxx_dialect == cxx98)
11957         maybe_warn_cpp0x ("scoped enums");
11958
11959       /* Consume the `struct' or `class' token.  */
11960       cp_lexer_consume_token (parser->lexer);
11961
11962       scoped_enum_p = true;
11963     }
11964
11965   attributes = cp_parser_attributes_opt (parser);
11966
11967   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11968     identifier = cp_parser_identifier (parser);
11969   else
11970     identifier = make_anon_name ();
11971
11972   /* Check for the `:' that denotes a specified underlying type in C++0x.
11973      Note that a ':' could also indicate a bitfield width, however.  */
11974   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11975     {
11976       cp_decl_specifier_seq type_specifiers;
11977
11978       /* Consume the `:'.  */
11979       cp_lexer_consume_token (parser->lexer);
11980
11981       /* Parse the type-specifier-seq.  */
11982       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11983                                     &type_specifiers);
11984
11985       /* At this point this is surely not elaborated type specifier.  */
11986       if (!cp_parser_parse_definitely (parser))
11987         return NULL_TREE;
11988
11989       if (cxx_dialect == cxx98)
11990         maybe_warn_cpp0x ("scoped enums");
11991
11992       has_underlying_type = true;
11993
11994       /* If that didn't work, stop.  */
11995       if (type_specifiers.type != error_mark_node)
11996         {
11997           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11998                                             /*initialized=*/0, NULL);
11999           if (underlying_type == error_mark_node)
12000             underlying_type = NULL_TREE;
12001         }
12002     }
12003
12004   /* Look for the `{' but don't consume it yet.  */
12005   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12006     {
12007       cp_parser_error (parser, "expected %<{%>");
12008       if (has_underlying_type)
12009         return NULL_TREE;
12010     }
12011
12012   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12013     return NULL_TREE;
12014
12015   /* Issue an error message if type-definitions are forbidden here.  */
12016   if (!cp_parser_check_type_definition (parser))
12017     type = error_mark_node;
12018   else
12019     /* Create the new type.  We do this before consuming the opening
12020        brace so the enum will be recorded as being on the line of its
12021        tag (or the 'enum' keyword, if there is no tag).  */
12022     type = start_enum (identifier, underlying_type, scoped_enum_p);
12023   
12024   /* Consume the opening brace.  */
12025   cp_lexer_consume_token (parser->lexer);
12026
12027   if (type == error_mark_node)
12028     {
12029       cp_parser_skip_to_end_of_block_or_statement (parser);
12030       return error_mark_node;
12031     }
12032
12033   /* If the next token is not '}', then there are some enumerators.  */
12034   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12035     cp_parser_enumerator_list (parser, type);
12036
12037   /* Consume the final '}'.  */
12038   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12039
12040   /* Look for trailing attributes to apply to this enumeration, and
12041      apply them if appropriate.  */
12042   if (cp_parser_allow_gnu_extensions_p (parser))
12043     {
12044       tree trailing_attr = cp_parser_attributes_opt (parser);
12045       trailing_attr = chainon (trailing_attr, attributes);
12046       cplus_decl_attributes (&type,
12047                              trailing_attr,
12048                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12049     }
12050
12051   /* Finish up the enumeration.  */
12052   finish_enum (type);
12053
12054   return type;
12055 }
12056
12057 /* Parse an enumerator-list.  The enumerators all have the indicated
12058    TYPE.
12059
12060    enumerator-list:
12061      enumerator-definition
12062      enumerator-list , enumerator-definition  */
12063
12064 static void
12065 cp_parser_enumerator_list (cp_parser* parser, tree type)
12066 {
12067   while (true)
12068     {
12069       /* Parse an enumerator-definition.  */
12070       cp_parser_enumerator_definition (parser, type);
12071
12072       /* If the next token is not a ',', we've reached the end of
12073          the list.  */
12074       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12075         break;
12076       /* Otherwise, consume the `,' and keep going.  */
12077       cp_lexer_consume_token (parser->lexer);
12078       /* If the next token is a `}', there is a trailing comma.  */
12079       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12080         {
12081           if (!in_system_header)
12082             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12083           break;
12084         }
12085     }
12086 }
12087
12088 /* Parse an enumerator-definition.  The enumerator has the indicated
12089    TYPE.
12090
12091    enumerator-definition:
12092      enumerator
12093      enumerator = constant-expression
12094
12095    enumerator:
12096      identifier  */
12097
12098 static void
12099 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12100 {
12101   tree identifier;
12102   tree value;
12103
12104   /* Look for the identifier.  */
12105   identifier = cp_parser_identifier (parser);
12106   if (identifier == error_mark_node)
12107     return;
12108
12109   /* If the next token is an '=', then there is an explicit value.  */
12110   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12111     {
12112       /* Consume the `=' token.  */
12113       cp_lexer_consume_token (parser->lexer);
12114       /* Parse the value.  */
12115       value = cp_parser_constant_expression (parser,
12116                                              /*allow_non_constant_p=*/false,
12117                                              NULL);
12118     }
12119   else
12120     value = NULL_TREE;
12121
12122   /* If we are processing a template, make sure the initializer of the
12123      enumerator doesn't contain any bare template parameter pack.  */
12124   if (check_for_bare_parameter_packs (value))
12125     value = error_mark_node;
12126
12127   /* Create the enumerator.  */
12128   build_enumerator (identifier, value, type);
12129 }
12130
12131 /* Parse a namespace-name.
12132
12133    namespace-name:
12134      original-namespace-name
12135      namespace-alias
12136
12137    Returns the NAMESPACE_DECL for the namespace.  */
12138
12139 static tree
12140 cp_parser_namespace_name (cp_parser* parser)
12141 {
12142   tree identifier;
12143   tree namespace_decl;
12144
12145   cp_token *token = cp_lexer_peek_token (parser->lexer);
12146
12147   /* Get the name of the namespace.  */
12148   identifier = cp_parser_identifier (parser);
12149   if (identifier == error_mark_node)
12150     return error_mark_node;
12151
12152   /* Look up the identifier in the currently active scope.  Look only
12153      for namespaces, due to:
12154
12155        [basic.lookup.udir]
12156
12157        When looking up a namespace-name in a using-directive or alias
12158        definition, only namespace names are considered.
12159
12160      And:
12161
12162        [basic.lookup.qual]
12163
12164        During the lookup of a name preceding the :: scope resolution
12165        operator, object, function, and enumerator names are ignored.
12166
12167      (Note that cp_parser_qualifying_entity only calls this
12168      function if the token after the name is the scope resolution
12169      operator.)  */
12170   namespace_decl = cp_parser_lookup_name (parser, identifier,
12171                                           none_type,
12172                                           /*is_template=*/false,
12173                                           /*is_namespace=*/true,
12174                                           /*check_dependency=*/true,
12175                                           /*ambiguous_decls=*/NULL,
12176                                           token->location);
12177   /* If it's not a namespace, issue an error.  */
12178   if (namespace_decl == error_mark_node
12179       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12180     {
12181       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12182         error_at (token->location, "%qD is not a namespace-name", identifier);
12183       cp_parser_error (parser, "expected namespace-name");
12184       namespace_decl = error_mark_node;
12185     }
12186
12187   return namespace_decl;
12188 }
12189
12190 /* Parse a namespace-definition.
12191
12192    namespace-definition:
12193      named-namespace-definition
12194      unnamed-namespace-definition
12195
12196    named-namespace-definition:
12197      original-namespace-definition
12198      extension-namespace-definition
12199
12200    original-namespace-definition:
12201      namespace identifier { namespace-body }
12202
12203    extension-namespace-definition:
12204      namespace original-namespace-name { namespace-body }
12205
12206    unnamed-namespace-definition:
12207      namespace { namespace-body } */
12208
12209 static void
12210 cp_parser_namespace_definition (cp_parser* parser)
12211 {
12212   tree identifier, attribs;
12213   bool has_visibility;
12214   bool is_inline;
12215
12216   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12217     {
12218       is_inline = true;
12219       cp_lexer_consume_token (parser->lexer);
12220     }
12221   else
12222     is_inline = false;
12223
12224   /* Look for the `namespace' keyword.  */
12225   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12226
12227   /* Get the name of the namespace.  We do not attempt to distinguish
12228      between an original-namespace-definition and an
12229      extension-namespace-definition at this point.  The semantic
12230      analysis routines are responsible for that.  */
12231   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12232     identifier = cp_parser_identifier (parser);
12233   else
12234     identifier = NULL_TREE;
12235
12236   /* Parse any specified attributes.  */
12237   attribs = cp_parser_attributes_opt (parser);
12238
12239   /* Look for the `{' to start the namespace.  */
12240   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12241   /* Start the namespace.  */
12242   push_namespace (identifier);
12243
12244   /* "inline namespace" is equivalent to a stub namespace definition
12245      followed by a strong using directive.  */
12246   if (is_inline)
12247     {
12248       tree name_space = current_namespace;
12249       /* Set up namespace association.  */
12250       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12251         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12252                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12253       /* Import the contents of the inline namespace.  */
12254       pop_namespace ();
12255       do_using_directive (name_space);
12256       push_namespace (identifier);
12257     }
12258
12259   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12260
12261   /* Parse the body of the namespace.  */
12262   cp_parser_namespace_body (parser);
12263
12264 #ifdef HANDLE_PRAGMA_VISIBILITY
12265   if (has_visibility)
12266     pop_visibility ();
12267 #endif
12268
12269   /* Finish the namespace.  */
12270   pop_namespace ();
12271   /* Look for the final `}'.  */
12272   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12273 }
12274
12275 /* Parse a namespace-body.
12276
12277    namespace-body:
12278      declaration-seq [opt]  */
12279
12280 static void
12281 cp_parser_namespace_body (cp_parser* parser)
12282 {
12283   cp_parser_declaration_seq_opt (parser);
12284 }
12285
12286 /* Parse a namespace-alias-definition.
12287
12288    namespace-alias-definition:
12289      namespace identifier = qualified-namespace-specifier ;  */
12290
12291 static void
12292 cp_parser_namespace_alias_definition (cp_parser* parser)
12293 {
12294   tree identifier;
12295   tree namespace_specifier;
12296
12297   cp_token *token = cp_lexer_peek_token (parser->lexer);
12298
12299   /* Look for the `namespace' keyword.  */
12300   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12301   /* Look for the identifier.  */
12302   identifier = cp_parser_identifier (parser);
12303   if (identifier == error_mark_node)
12304     return;
12305   /* Look for the `=' token.  */
12306   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12307       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12308     {
12309       error_at (token->location, "%<namespace%> definition is not allowed here");
12310       /* Skip the definition.  */
12311       cp_lexer_consume_token (parser->lexer);
12312       if (cp_parser_skip_to_closing_brace (parser))
12313         cp_lexer_consume_token (parser->lexer);
12314       return;
12315     }
12316   cp_parser_require (parser, CPP_EQ, "%<=%>");
12317   /* Look for the qualified-namespace-specifier.  */
12318   namespace_specifier
12319     = cp_parser_qualified_namespace_specifier (parser);
12320   /* Look for the `;' token.  */
12321   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12322
12323   /* Register the alias in the symbol table.  */
12324   do_namespace_alias (identifier, namespace_specifier);
12325 }
12326
12327 /* Parse a qualified-namespace-specifier.
12328
12329    qualified-namespace-specifier:
12330      :: [opt] nested-name-specifier [opt] namespace-name
12331
12332    Returns a NAMESPACE_DECL corresponding to the specified
12333    namespace.  */
12334
12335 static tree
12336 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12337 {
12338   /* Look for the optional `::'.  */
12339   cp_parser_global_scope_opt (parser,
12340                               /*current_scope_valid_p=*/false);
12341
12342   /* Look for the optional nested-name-specifier.  */
12343   cp_parser_nested_name_specifier_opt (parser,
12344                                        /*typename_keyword_p=*/false,
12345                                        /*check_dependency_p=*/true,
12346                                        /*type_p=*/false,
12347                                        /*is_declaration=*/true);
12348
12349   return cp_parser_namespace_name (parser);
12350 }
12351
12352 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12353    access declaration.
12354
12355    using-declaration:
12356      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12357      using :: unqualified-id ;  
12358
12359    access-declaration:
12360      qualified-id ;  
12361
12362    */
12363
12364 static bool
12365 cp_parser_using_declaration (cp_parser* parser, 
12366                              bool access_declaration_p)
12367 {
12368   cp_token *token;
12369   bool typename_p = false;
12370   bool global_scope_p;
12371   tree decl;
12372   tree identifier;
12373   tree qscope;
12374
12375   if (access_declaration_p)
12376     cp_parser_parse_tentatively (parser);
12377   else
12378     {
12379       /* Look for the `using' keyword.  */
12380       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12381       
12382       /* Peek at the next token.  */
12383       token = cp_lexer_peek_token (parser->lexer);
12384       /* See if it's `typename'.  */
12385       if (token->keyword == RID_TYPENAME)
12386         {
12387           /* Remember that we've seen it.  */
12388           typename_p = true;
12389           /* Consume the `typename' token.  */
12390           cp_lexer_consume_token (parser->lexer);
12391         }
12392     }
12393
12394   /* Look for the optional global scope qualification.  */
12395   global_scope_p
12396     = (cp_parser_global_scope_opt (parser,
12397                                    /*current_scope_valid_p=*/false)
12398        != NULL_TREE);
12399
12400   /* If we saw `typename', or didn't see `::', then there must be a
12401      nested-name-specifier present.  */
12402   if (typename_p || !global_scope_p)
12403     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12404                                               /*check_dependency_p=*/true,
12405                                               /*type_p=*/false,
12406                                               /*is_declaration=*/true);
12407   /* Otherwise, we could be in either of the two productions.  In that
12408      case, treat the nested-name-specifier as optional.  */
12409   else
12410     qscope = cp_parser_nested_name_specifier_opt (parser,
12411                                                   /*typename_keyword_p=*/false,
12412                                                   /*check_dependency_p=*/true,
12413                                                   /*type_p=*/false,
12414                                                   /*is_declaration=*/true);
12415   if (!qscope)
12416     qscope = global_namespace;
12417
12418   if (access_declaration_p && cp_parser_error_occurred (parser))
12419     /* Something has already gone wrong; there's no need to parse
12420        further.  Since an error has occurred, the return value of
12421        cp_parser_parse_definitely will be false, as required.  */
12422     return cp_parser_parse_definitely (parser);
12423
12424   token = cp_lexer_peek_token (parser->lexer);
12425   /* Parse the unqualified-id.  */
12426   identifier = cp_parser_unqualified_id (parser,
12427                                          /*template_keyword_p=*/false,
12428                                          /*check_dependency_p=*/true,
12429                                          /*declarator_p=*/true,
12430                                          /*optional_p=*/false);
12431
12432   if (access_declaration_p)
12433     {
12434       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12435         cp_parser_simulate_error (parser);
12436       if (!cp_parser_parse_definitely (parser))
12437         return false;
12438     }
12439
12440   /* The function we call to handle a using-declaration is different
12441      depending on what scope we are in.  */
12442   if (qscope == error_mark_node || identifier == error_mark_node)
12443     ;
12444   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12445            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12446     /* [namespace.udecl]
12447
12448        A using declaration shall not name a template-id.  */
12449     error_at (token->location,
12450               "a template-id may not appear in a using-declaration");
12451   else
12452     {
12453       if (at_class_scope_p ())
12454         {
12455           /* Create the USING_DECL.  */
12456           decl = do_class_using_decl (parser->scope, identifier);
12457
12458           if (check_for_bare_parameter_packs (decl))
12459             return false;
12460           else
12461             /* Add it to the list of members in this class.  */
12462             finish_member_declaration (decl);
12463         }
12464       else
12465         {
12466           decl = cp_parser_lookup_name_simple (parser,
12467                                                identifier,
12468                                                token->location);
12469           if (decl == error_mark_node)
12470             cp_parser_name_lookup_error (parser, identifier,
12471                                          decl, NULL,
12472                                          token->location);
12473           else if (check_for_bare_parameter_packs (decl))
12474             return false;
12475           else if (!at_namespace_scope_p ())
12476             do_local_using_decl (decl, qscope, identifier);
12477           else
12478             do_toplevel_using_decl (decl, qscope, identifier);
12479         }
12480     }
12481
12482   /* Look for the final `;'.  */
12483   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12484   
12485   return true;
12486 }
12487
12488 /* Parse a using-directive.
12489
12490    using-directive:
12491      using namespace :: [opt] nested-name-specifier [opt]
12492        namespace-name ;  */
12493
12494 static void
12495 cp_parser_using_directive (cp_parser* parser)
12496 {
12497   tree namespace_decl;
12498   tree attribs;
12499
12500   /* Look for the `using' keyword.  */
12501   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12502   /* And the `namespace' keyword.  */
12503   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12504   /* Look for the optional `::' operator.  */
12505   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12506   /* And the optional nested-name-specifier.  */
12507   cp_parser_nested_name_specifier_opt (parser,
12508                                        /*typename_keyword_p=*/false,
12509                                        /*check_dependency_p=*/true,
12510                                        /*type_p=*/false,
12511                                        /*is_declaration=*/true);
12512   /* Get the namespace being used.  */
12513   namespace_decl = cp_parser_namespace_name (parser);
12514   /* And any specified attributes.  */
12515   attribs = cp_parser_attributes_opt (parser);
12516   /* Update the symbol table.  */
12517   parse_using_directive (namespace_decl, attribs);
12518   /* Look for the final `;'.  */
12519   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12520 }
12521
12522 /* Parse an asm-definition.
12523
12524    asm-definition:
12525      asm ( string-literal ) ;
12526
12527    GNU Extension:
12528
12529    asm-definition:
12530      asm volatile [opt] ( string-literal ) ;
12531      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12532      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12533                           : asm-operand-list [opt] ) ;
12534      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12535                           : asm-operand-list [opt]
12536                           : asm-clobber-list [opt] ) ;
12537      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
12538                                : asm-clobber-list [opt]
12539                                : asm-goto-list ) ;  */
12540
12541 static void
12542 cp_parser_asm_definition (cp_parser* parser)
12543 {
12544   tree string;
12545   tree outputs = NULL_TREE;
12546   tree inputs = NULL_TREE;
12547   tree clobbers = NULL_TREE;
12548   tree labels = NULL_TREE;
12549   tree asm_stmt;
12550   bool volatile_p = false;
12551   bool extended_p = false;
12552   bool invalid_inputs_p = false;
12553   bool invalid_outputs_p = false;
12554   bool goto_p = false;
12555   const char *missing = NULL;
12556
12557   /* Look for the `asm' keyword.  */
12558   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12559   /* See if the next token is `volatile'.  */
12560   if (cp_parser_allow_gnu_extensions_p (parser)
12561       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12562     {
12563       /* Remember that we saw the `volatile' keyword.  */
12564       volatile_p = true;
12565       /* Consume the token.  */
12566       cp_lexer_consume_token (parser->lexer);
12567     }
12568   if (cp_parser_allow_gnu_extensions_p (parser)
12569       && parser->in_function_body
12570       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
12571     {
12572       /* Remember that we saw the `goto' keyword.  */
12573       goto_p = true;
12574       /* Consume the token.  */
12575       cp_lexer_consume_token (parser->lexer);
12576     }
12577   /* Look for the opening `('.  */
12578   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12579     return;
12580   /* Look for the string.  */
12581   string = cp_parser_string_literal (parser, false, false);
12582   if (string == error_mark_node)
12583     {
12584       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12585                                              /*consume_paren=*/true);
12586       return;
12587     }
12588
12589   /* If we're allowing GNU extensions, check for the extended assembly
12590      syntax.  Unfortunately, the `:' tokens need not be separated by
12591      a space in C, and so, for compatibility, we tolerate that here
12592      too.  Doing that means that we have to treat the `::' operator as
12593      two `:' tokens.  */
12594   if (cp_parser_allow_gnu_extensions_p (parser)
12595       && parser->in_function_body
12596       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12597           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12598     {
12599       bool inputs_p = false;
12600       bool clobbers_p = false;
12601       bool labels_p = false;
12602
12603       /* The extended syntax was used.  */
12604       extended_p = true;
12605
12606       /* Look for outputs.  */
12607       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12608         {
12609           /* Consume the `:'.  */
12610           cp_lexer_consume_token (parser->lexer);
12611           /* Parse the output-operands.  */
12612           if (cp_lexer_next_token_is_not (parser->lexer,
12613                                           CPP_COLON)
12614               && cp_lexer_next_token_is_not (parser->lexer,
12615                                              CPP_SCOPE)
12616               && cp_lexer_next_token_is_not (parser->lexer,
12617                                              CPP_CLOSE_PAREN)
12618               && !goto_p)
12619             outputs = cp_parser_asm_operand_list (parser);
12620
12621             if (outputs == error_mark_node)
12622               invalid_outputs_p = true;
12623         }
12624       /* If the next token is `::', there are no outputs, and the
12625          next token is the beginning of the inputs.  */
12626       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12627         /* The inputs are coming next.  */
12628         inputs_p = true;
12629
12630       /* Look for inputs.  */
12631       if (inputs_p
12632           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12633         {
12634           /* Consume the `:' or `::'.  */
12635           cp_lexer_consume_token (parser->lexer);
12636           /* Parse the output-operands.  */
12637           if (cp_lexer_next_token_is_not (parser->lexer,
12638                                           CPP_COLON)
12639               && cp_lexer_next_token_is_not (parser->lexer,
12640                                              CPP_SCOPE)
12641               && cp_lexer_next_token_is_not (parser->lexer,
12642                                              CPP_CLOSE_PAREN))
12643             inputs = cp_parser_asm_operand_list (parser);
12644
12645             if (inputs == error_mark_node)
12646               invalid_inputs_p = true;
12647         }
12648       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12649         /* The clobbers are coming next.  */
12650         clobbers_p = true;
12651
12652       /* Look for clobbers.  */
12653       if (clobbers_p
12654           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12655         {
12656           clobbers_p = true;
12657           /* Consume the `:' or `::'.  */
12658           cp_lexer_consume_token (parser->lexer);
12659           /* Parse the clobbers.  */
12660           if (cp_lexer_next_token_is_not (parser->lexer,
12661                                           CPP_COLON)
12662               && cp_lexer_next_token_is_not (parser->lexer,
12663                                              CPP_CLOSE_PAREN))
12664             clobbers = cp_parser_asm_clobber_list (parser);
12665         }
12666       else if (goto_p
12667                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12668         /* The labels are coming next.  */
12669         labels_p = true;
12670
12671       /* Look for labels.  */
12672       if (labels_p
12673           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
12674         {
12675           labels_p = true;
12676           /* Consume the `:' or `::'.  */
12677           cp_lexer_consume_token (parser->lexer);
12678           /* Parse the labels.  */
12679           labels = cp_parser_asm_label_list (parser);
12680         }
12681
12682       if (goto_p && !labels_p)
12683         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
12684     }
12685   else if (goto_p)
12686     missing = "%<:%> or %<::%>";
12687
12688   /* Look for the closing `)'.  */
12689   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
12690                           missing ? missing : "%<)%>"))
12691     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12692                                            /*consume_paren=*/true);
12693   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12694
12695   if (!invalid_inputs_p && !invalid_outputs_p)
12696     {
12697       /* Create the ASM_EXPR.  */
12698       if (parser->in_function_body)
12699         {
12700           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12701                                       inputs, clobbers, labels);
12702           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12703           if (!extended_p)
12704             {
12705               tree temp = asm_stmt;
12706               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12707                 temp = TREE_OPERAND (temp, 0);
12708
12709               ASM_INPUT_P (temp) = 1;
12710             }
12711         }
12712       else
12713         cgraph_add_asm_node (string);
12714     }
12715 }
12716
12717 /* Declarators [gram.dcl.decl] */
12718
12719 /* Parse an init-declarator.
12720
12721    init-declarator:
12722      declarator initializer [opt]
12723
12724    GNU Extension:
12725
12726    init-declarator:
12727      declarator asm-specification [opt] attributes [opt] initializer [opt]
12728
12729    function-definition:
12730      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12731        function-body
12732      decl-specifier-seq [opt] declarator function-try-block
12733
12734    GNU Extension:
12735
12736    function-definition:
12737      __extension__ function-definition
12738
12739    The DECL_SPECIFIERS apply to this declarator.  Returns a
12740    representation of the entity declared.  If MEMBER_P is TRUE, then
12741    this declarator appears in a class scope.  The new DECL created by
12742    this declarator is returned.
12743
12744    The CHECKS are access checks that should be performed once we know
12745    what entity is being declared (and, therefore, what classes have
12746    befriended it).
12747
12748    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12749    for a function-definition here as well.  If the declarator is a
12750    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12751    be TRUE upon return.  By that point, the function-definition will
12752    have been completely parsed.
12753
12754    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12755    is FALSE.  */
12756
12757 static tree
12758 cp_parser_init_declarator (cp_parser* parser,
12759                            cp_decl_specifier_seq *decl_specifiers,
12760                            VEC (deferred_access_check,gc)* checks,
12761                            bool function_definition_allowed_p,
12762                            bool member_p,
12763                            int declares_class_or_enum,
12764                            bool* function_definition_p)
12765 {
12766   cp_token *token = NULL, *asm_spec_start_token = NULL,
12767            *attributes_start_token = NULL;
12768   cp_declarator *declarator;
12769   tree prefix_attributes;
12770   tree attributes;
12771   tree asm_specification;
12772   tree initializer;
12773   tree decl = NULL_TREE;
12774   tree scope;
12775   int is_initialized;
12776   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12777      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12778      "(...)".  */
12779   enum cpp_ttype initialization_kind;
12780   bool is_direct_init = false;
12781   bool is_non_constant_init;
12782   int ctor_dtor_or_conv_p;
12783   bool friend_p;
12784   tree pushed_scope = NULL;
12785
12786   /* Gather the attributes that were provided with the
12787      decl-specifiers.  */
12788   prefix_attributes = decl_specifiers->attributes;
12789
12790   /* Assume that this is not the declarator for a function
12791      definition.  */
12792   if (function_definition_p)
12793     *function_definition_p = false;
12794
12795   /* Defer access checks while parsing the declarator; we cannot know
12796      what names are accessible until we know what is being
12797      declared.  */
12798   resume_deferring_access_checks ();
12799
12800   /* Parse the declarator.  */
12801   token = cp_lexer_peek_token (parser->lexer);
12802   declarator
12803     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12804                             &ctor_dtor_or_conv_p,
12805                             /*parenthesized_p=*/NULL,
12806                             /*member_p=*/false);
12807   /* Gather up the deferred checks.  */
12808   stop_deferring_access_checks ();
12809
12810   /* If the DECLARATOR was erroneous, there's no need to go
12811      further.  */
12812   if (declarator == cp_error_declarator)
12813     return error_mark_node;
12814
12815   /* Check that the number of template-parameter-lists is OK.  */
12816   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12817                                                        token->location))
12818     return error_mark_node;
12819
12820   if (declares_class_or_enum & 2)
12821     cp_parser_check_for_definition_in_return_type (declarator,
12822                                                    decl_specifiers->type,
12823                                                    decl_specifiers->type_location);
12824
12825   /* Figure out what scope the entity declared by the DECLARATOR is
12826      located in.  `grokdeclarator' sometimes changes the scope, so
12827      we compute it now.  */
12828   scope = get_scope_of_declarator (declarator);
12829
12830   /* If we're allowing GNU extensions, look for an asm-specification
12831      and attributes.  */
12832   if (cp_parser_allow_gnu_extensions_p (parser))
12833     {
12834       /* Look for an asm-specification.  */
12835       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12836       asm_specification = cp_parser_asm_specification_opt (parser);
12837       /* And attributes.  */
12838       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12839       attributes = cp_parser_attributes_opt (parser);
12840     }
12841   else
12842     {
12843       asm_specification = NULL_TREE;
12844       attributes = NULL_TREE;
12845     }
12846
12847   /* Peek at the next token.  */
12848   token = cp_lexer_peek_token (parser->lexer);
12849   /* Check to see if the token indicates the start of a
12850      function-definition.  */
12851   if (function_declarator_p (declarator)
12852       && cp_parser_token_starts_function_definition_p (token))
12853     {
12854       if (!function_definition_allowed_p)
12855         {
12856           /* If a function-definition should not appear here, issue an
12857              error message.  */
12858           cp_parser_error (parser,
12859                            "a function-definition is not allowed here");
12860           return error_mark_node;
12861         }
12862       else
12863         {
12864           location_t func_brace_location
12865             = cp_lexer_peek_token (parser->lexer)->location;
12866
12867           /* Neither attributes nor an asm-specification are allowed
12868              on a function-definition.  */
12869           if (asm_specification)
12870             error_at (asm_spec_start_token->location,
12871                       "an asm-specification is not allowed "
12872                       "on a function-definition");
12873           if (attributes)
12874             error_at (attributes_start_token->location,
12875                       "attributes are not allowed on a function-definition");
12876           /* This is a function-definition.  */
12877           *function_definition_p = true;
12878
12879           /* Parse the function definition.  */
12880           if (member_p)
12881             decl = cp_parser_save_member_function_body (parser,
12882                                                         decl_specifiers,
12883                                                         declarator,
12884                                                         prefix_attributes);
12885           else
12886             decl
12887               = (cp_parser_function_definition_from_specifiers_and_declarator
12888                  (parser, decl_specifiers, prefix_attributes, declarator));
12889
12890           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12891             {
12892               /* This is where the prologue starts...  */
12893               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12894                 = func_brace_location;
12895             }
12896
12897           return decl;
12898         }
12899     }
12900
12901   /* [dcl.dcl]
12902
12903      Only in function declarations for constructors, destructors, and
12904      type conversions can the decl-specifier-seq be omitted.
12905
12906      We explicitly postpone this check past the point where we handle
12907      function-definitions because we tolerate function-definitions
12908      that are missing their return types in some modes.  */
12909   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12910     {
12911       cp_parser_error (parser,
12912                        "expected constructor, destructor, or type conversion");
12913       return error_mark_node;
12914     }
12915
12916   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12917   if (token->type == CPP_EQ
12918       || token->type == CPP_OPEN_PAREN
12919       || token->type == CPP_OPEN_BRACE)
12920     {
12921       is_initialized = SD_INITIALIZED;
12922       initialization_kind = token->type;
12923
12924       if (token->type == CPP_EQ
12925           && function_declarator_p (declarator))
12926         {
12927           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12928           if (t2->keyword == RID_DEFAULT)
12929             is_initialized = SD_DEFAULTED;
12930           else if (t2->keyword == RID_DELETE)
12931             is_initialized = SD_DELETED;
12932         }
12933     }
12934   else
12935     {
12936       /* If the init-declarator isn't initialized and isn't followed by a
12937          `,' or `;', it's not a valid init-declarator.  */
12938       if (token->type != CPP_COMMA
12939           && token->type != CPP_SEMICOLON)
12940         {
12941           cp_parser_error (parser, "expected initializer");
12942           return error_mark_node;
12943         }
12944       is_initialized = SD_UNINITIALIZED;
12945       initialization_kind = CPP_EOF;
12946     }
12947
12948   /* Because start_decl has side-effects, we should only call it if we
12949      know we're going ahead.  By this point, we know that we cannot
12950      possibly be looking at any other construct.  */
12951   cp_parser_commit_to_tentative_parse (parser);
12952
12953   /* If the decl specifiers were bad, issue an error now that we're
12954      sure this was intended to be a declarator.  Then continue
12955      declaring the variable(s), as int, to try to cut down on further
12956      errors.  */
12957   if (decl_specifiers->any_specifiers_p
12958       && decl_specifiers->type == error_mark_node)
12959     {
12960       cp_parser_error (parser, "invalid type in declaration");
12961       decl_specifiers->type = integer_type_node;
12962     }
12963
12964   /* Check to see whether or not this declaration is a friend.  */
12965   friend_p = cp_parser_friend_p (decl_specifiers);
12966
12967   /* Enter the newly declared entry in the symbol table.  If we're
12968      processing a declaration in a class-specifier, we wait until
12969      after processing the initializer.  */
12970   if (!member_p)
12971     {
12972       if (parser->in_unbraced_linkage_specification_p)
12973         decl_specifiers->storage_class = sc_extern;
12974       decl = start_decl (declarator, decl_specifiers,
12975                          is_initialized, attributes, prefix_attributes,
12976                          &pushed_scope);
12977     }
12978   else if (scope)
12979     /* Enter the SCOPE.  That way unqualified names appearing in the
12980        initializer will be looked up in SCOPE.  */
12981     pushed_scope = push_scope (scope);
12982
12983   /* Perform deferred access control checks, now that we know in which
12984      SCOPE the declared entity resides.  */
12985   if (!member_p && decl)
12986     {
12987       tree saved_current_function_decl = NULL_TREE;
12988
12989       /* If the entity being declared is a function, pretend that we
12990          are in its scope.  If it is a `friend', it may have access to
12991          things that would not otherwise be accessible.  */
12992       if (TREE_CODE (decl) == FUNCTION_DECL)
12993         {
12994           saved_current_function_decl = current_function_decl;
12995           current_function_decl = decl;
12996         }
12997
12998       /* Perform access checks for template parameters.  */
12999       cp_parser_perform_template_parameter_access_checks (checks);
13000
13001       /* Perform the access control checks for the declarator and the
13002          decl-specifiers.  */
13003       perform_deferred_access_checks ();
13004
13005       /* Restore the saved value.  */
13006       if (TREE_CODE (decl) == FUNCTION_DECL)
13007         current_function_decl = saved_current_function_decl;
13008     }
13009
13010   /* Parse the initializer.  */
13011   initializer = NULL_TREE;
13012   is_direct_init = false;
13013   is_non_constant_init = true;
13014   if (is_initialized)
13015     {
13016       if (function_declarator_p (declarator))
13017         {
13018           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13019            if (initialization_kind == CPP_EQ)
13020              initializer = cp_parser_pure_specifier (parser);
13021            else
13022              {
13023                /* If the declaration was erroneous, we don't really
13024                   know what the user intended, so just silently
13025                   consume the initializer.  */
13026                if (decl != error_mark_node)
13027                  error_at (initializer_start_token->location,
13028                            "initializer provided for function");
13029                cp_parser_skip_to_closing_parenthesis (parser,
13030                                                       /*recovering=*/true,
13031                                                       /*or_comma=*/false,
13032                                                       /*consume_paren=*/true);
13033              }
13034         }
13035       else
13036         initializer = cp_parser_initializer (parser,
13037                                              &is_direct_init,
13038                                              &is_non_constant_init);
13039     }
13040
13041   /* The old parser allows attributes to appear after a parenthesized
13042      initializer.  Mark Mitchell proposed removing this functionality
13043      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13044      attributes -- but ignores them.  */
13045   if (cp_parser_allow_gnu_extensions_p (parser)
13046       && initialization_kind == CPP_OPEN_PAREN)
13047     if (cp_parser_attributes_opt (parser))
13048       warning (OPT_Wattributes,
13049                "attributes after parenthesized initializer ignored");
13050
13051   /* For an in-class declaration, use `grokfield' to create the
13052      declaration.  */
13053   if (member_p)
13054     {
13055       if (pushed_scope)
13056         {
13057           pop_scope (pushed_scope);
13058           pushed_scope = false;
13059         }
13060       decl = grokfield (declarator, decl_specifiers,
13061                         initializer, !is_non_constant_init,
13062                         /*asmspec=*/NULL_TREE,
13063                         prefix_attributes);
13064       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13065         cp_parser_save_default_args (parser, decl);
13066     }
13067
13068   /* Finish processing the declaration.  But, skip friend
13069      declarations.  */
13070   if (!friend_p && decl && decl != error_mark_node)
13071     {
13072       cp_finish_decl (decl,
13073                       initializer, !is_non_constant_init,
13074                       asm_specification,
13075                       /* If the initializer is in parentheses, then this is
13076                          a direct-initialization, which means that an
13077                          `explicit' constructor is OK.  Otherwise, an
13078                          `explicit' constructor cannot be used.  */
13079                       ((is_direct_init || !is_initialized)
13080                        ? 0 : LOOKUP_ONLYCONVERTING));
13081     }
13082   else if ((cxx_dialect != cxx98) && friend_p
13083            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13084     /* Core issue #226 (C++0x only): A default template-argument
13085        shall not be specified in a friend class template
13086        declaration. */
13087     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13088                              /*is_partial=*/0, /*is_friend_decl=*/1);
13089
13090   if (!friend_p && pushed_scope)
13091     pop_scope (pushed_scope);
13092
13093   return decl;
13094 }
13095
13096 /* Parse a declarator.
13097
13098    declarator:
13099      direct-declarator
13100      ptr-operator declarator
13101
13102    abstract-declarator:
13103      ptr-operator abstract-declarator [opt]
13104      direct-abstract-declarator
13105
13106    GNU Extensions:
13107
13108    declarator:
13109      attributes [opt] direct-declarator
13110      attributes [opt] ptr-operator declarator
13111
13112    abstract-declarator:
13113      attributes [opt] ptr-operator abstract-declarator [opt]
13114      attributes [opt] direct-abstract-declarator
13115
13116    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13117    detect constructor, destructor or conversion operators. It is set
13118    to -1 if the declarator is a name, and +1 if it is a
13119    function. Otherwise it is set to zero. Usually you just want to
13120    test for >0, but internally the negative value is used.
13121
13122    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13123    a decl-specifier-seq unless it declares a constructor, destructor,
13124    or conversion.  It might seem that we could check this condition in
13125    semantic analysis, rather than parsing, but that makes it difficult
13126    to handle something like `f()'.  We want to notice that there are
13127    no decl-specifiers, and therefore realize that this is an
13128    expression, not a declaration.)
13129
13130    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13131    the declarator is a direct-declarator of the form "(...)".
13132
13133    MEMBER_P is true iff this declarator is a member-declarator.  */
13134
13135 static cp_declarator *
13136 cp_parser_declarator (cp_parser* parser,
13137                       cp_parser_declarator_kind dcl_kind,
13138                       int* ctor_dtor_or_conv_p,
13139                       bool* parenthesized_p,
13140                       bool member_p)
13141 {
13142   cp_token *token;
13143   cp_declarator *declarator;
13144   enum tree_code code;
13145   cp_cv_quals cv_quals;
13146   tree class_type;
13147   tree attributes = NULL_TREE;
13148
13149   /* Assume this is not a constructor, destructor, or type-conversion
13150      operator.  */
13151   if (ctor_dtor_or_conv_p)
13152     *ctor_dtor_or_conv_p = 0;
13153
13154   if (cp_parser_allow_gnu_extensions_p (parser))
13155     attributes = cp_parser_attributes_opt (parser);
13156
13157   /* Peek at the next token.  */
13158   token = cp_lexer_peek_token (parser->lexer);
13159
13160   /* Check for the ptr-operator production.  */
13161   cp_parser_parse_tentatively (parser);
13162   /* Parse the ptr-operator.  */
13163   code = cp_parser_ptr_operator (parser,
13164                                  &class_type,
13165                                  &cv_quals);
13166   /* If that worked, then we have a ptr-operator.  */
13167   if (cp_parser_parse_definitely (parser))
13168     {
13169       /* If a ptr-operator was found, then this declarator was not
13170          parenthesized.  */
13171       if (parenthesized_p)
13172         *parenthesized_p = true;
13173       /* The dependent declarator is optional if we are parsing an
13174          abstract-declarator.  */
13175       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13176         cp_parser_parse_tentatively (parser);
13177
13178       /* Parse the dependent declarator.  */
13179       declarator = cp_parser_declarator (parser, dcl_kind,
13180                                          /*ctor_dtor_or_conv_p=*/NULL,
13181                                          /*parenthesized_p=*/NULL,
13182                                          /*member_p=*/false);
13183
13184       /* If we are parsing an abstract-declarator, we must handle the
13185          case where the dependent declarator is absent.  */
13186       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13187           && !cp_parser_parse_definitely (parser))
13188         declarator = NULL;
13189
13190       declarator = cp_parser_make_indirect_declarator
13191         (code, class_type, cv_quals, declarator);
13192     }
13193   /* Everything else is a direct-declarator.  */
13194   else
13195     {
13196       if (parenthesized_p)
13197         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13198                                                    CPP_OPEN_PAREN);
13199       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13200                                                 ctor_dtor_or_conv_p,
13201                                                 member_p);
13202     }
13203
13204   if (attributes && declarator && declarator != cp_error_declarator)
13205     declarator->attributes = attributes;
13206
13207   return declarator;
13208 }
13209
13210 /* Parse a direct-declarator or direct-abstract-declarator.
13211
13212    direct-declarator:
13213      declarator-id
13214      direct-declarator ( parameter-declaration-clause )
13215        cv-qualifier-seq [opt]
13216        exception-specification [opt]
13217      direct-declarator [ constant-expression [opt] ]
13218      ( declarator )
13219
13220    direct-abstract-declarator:
13221      direct-abstract-declarator [opt]
13222        ( parameter-declaration-clause )
13223        cv-qualifier-seq [opt]
13224        exception-specification [opt]
13225      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13226      ( abstract-declarator )
13227
13228    Returns a representation of the declarator.  DCL_KIND is
13229    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13230    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13231    we are parsing a direct-declarator.  It is
13232    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13233    of ambiguity we prefer an abstract declarator, as per
13234    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13235    cp_parser_declarator.  */
13236
13237 static cp_declarator *
13238 cp_parser_direct_declarator (cp_parser* parser,
13239                              cp_parser_declarator_kind dcl_kind,
13240                              int* ctor_dtor_or_conv_p,
13241                              bool member_p)
13242 {
13243   cp_token *token;
13244   cp_declarator *declarator = NULL;
13245   tree scope = NULL_TREE;
13246   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13247   bool saved_in_declarator_p = parser->in_declarator_p;
13248   bool first = true;
13249   tree pushed_scope = NULL_TREE;
13250
13251   while (true)
13252     {
13253       /* Peek at the next token.  */
13254       token = cp_lexer_peek_token (parser->lexer);
13255       if (token->type == CPP_OPEN_PAREN)
13256         {
13257           /* This is either a parameter-declaration-clause, or a
13258              parenthesized declarator. When we know we are parsing a
13259              named declarator, it must be a parenthesized declarator
13260              if FIRST is true. For instance, `(int)' is a
13261              parameter-declaration-clause, with an omitted
13262              direct-abstract-declarator. But `((*))', is a
13263              parenthesized abstract declarator. Finally, when T is a
13264              template parameter `(T)' is a
13265              parameter-declaration-clause, and not a parenthesized
13266              named declarator.
13267
13268              We first try and parse a parameter-declaration-clause,
13269              and then try a nested declarator (if FIRST is true).
13270
13271              It is not an error for it not to be a
13272              parameter-declaration-clause, even when FIRST is
13273              false. Consider,
13274
13275                int i (int);
13276                int i (3);
13277
13278              The first is the declaration of a function while the
13279              second is the definition of a variable, including its
13280              initializer.
13281
13282              Having seen only the parenthesis, we cannot know which of
13283              these two alternatives should be selected.  Even more
13284              complex are examples like:
13285
13286                int i (int (a));
13287                int i (int (3));
13288
13289              The former is a function-declaration; the latter is a
13290              variable initialization.
13291
13292              Thus again, we try a parameter-declaration-clause, and if
13293              that fails, we back out and return.  */
13294
13295           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13296             {
13297               tree params;
13298               unsigned saved_num_template_parameter_lists;
13299               bool is_declarator = false;
13300               tree t;
13301
13302               /* In a member-declarator, the only valid interpretation
13303                  of a parenthesis is the start of a
13304                  parameter-declaration-clause.  (It is invalid to
13305                  initialize a static data member with a parenthesized
13306                  initializer; only the "=" form of initialization is
13307                  permitted.)  */
13308               if (!member_p)
13309                 cp_parser_parse_tentatively (parser);
13310
13311               /* Consume the `('.  */
13312               cp_lexer_consume_token (parser->lexer);
13313               if (first)
13314                 {
13315                   /* If this is going to be an abstract declarator, we're
13316                      in a declarator and we can't have default args.  */
13317                   parser->default_arg_ok_p = false;
13318                   parser->in_declarator_p = true;
13319                 }
13320
13321               /* Inside the function parameter list, surrounding
13322                  template-parameter-lists do not apply.  */
13323               saved_num_template_parameter_lists
13324                 = parser->num_template_parameter_lists;
13325               parser->num_template_parameter_lists = 0;
13326
13327               begin_scope (sk_function_parms, NULL_TREE);
13328
13329               /* Parse the parameter-declaration-clause.  */
13330               params = cp_parser_parameter_declaration_clause (parser);
13331
13332               parser->num_template_parameter_lists
13333                 = saved_num_template_parameter_lists;
13334
13335               /* If all went well, parse the cv-qualifier-seq and the
13336                  exception-specification.  */
13337               if (member_p || cp_parser_parse_definitely (parser))
13338                 {
13339                   cp_cv_quals cv_quals;
13340                   tree exception_specification;
13341                   tree late_return;
13342
13343                   is_declarator = true;
13344
13345                   if (ctor_dtor_or_conv_p)
13346                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13347                   first = false;
13348                   /* Consume the `)'.  */
13349                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13350
13351                   /* Parse the cv-qualifier-seq.  */
13352                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13353                   /* And the exception-specification.  */
13354                   exception_specification
13355                     = cp_parser_exception_specification_opt (parser);
13356
13357                   late_return
13358                     = cp_parser_late_return_type_opt (parser);
13359
13360                   /* Create the function-declarator.  */
13361                   declarator = make_call_declarator (declarator,
13362                                                      params,
13363                                                      cv_quals,
13364                                                      exception_specification,
13365                                                      late_return);
13366                   /* Any subsequent parameter lists are to do with
13367                      return type, so are not those of the declared
13368                      function.  */
13369                   parser->default_arg_ok_p = false;
13370                 }
13371
13372               /* Remove the function parms from scope.  */
13373               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13374                 pop_binding (DECL_NAME (t), t);
13375               leave_scope();
13376
13377               if (is_declarator)
13378                 /* Repeat the main loop.  */
13379                 continue;
13380             }
13381
13382           /* If this is the first, we can try a parenthesized
13383              declarator.  */
13384           if (first)
13385             {
13386               bool saved_in_type_id_in_expr_p;
13387
13388               parser->default_arg_ok_p = saved_default_arg_ok_p;
13389               parser->in_declarator_p = saved_in_declarator_p;
13390
13391               /* Consume the `('.  */
13392               cp_lexer_consume_token (parser->lexer);
13393               /* Parse the nested declarator.  */
13394               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13395               parser->in_type_id_in_expr_p = true;
13396               declarator
13397                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13398                                         /*parenthesized_p=*/NULL,
13399                                         member_p);
13400               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13401               first = false;
13402               /* Expect a `)'.  */
13403               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13404                 declarator = cp_error_declarator;
13405               if (declarator == cp_error_declarator)
13406                 break;
13407
13408               goto handle_declarator;
13409             }
13410           /* Otherwise, we must be done.  */
13411           else
13412             break;
13413         }
13414       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13415                && token->type == CPP_OPEN_SQUARE)
13416         {
13417           /* Parse an array-declarator.  */
13418           tree bounds;
13419
13420           if (ctor_dtor_or_conv_p)
13421             *ctor_dtor_or_conv_p = 0;
13422
13423           first = false;
13424           parser->default_arg_ok_p = false;
13425           parser->in_declarator_p = true;
13426           /* Consume the `['.  */
13427           cp_lexer_consume_token (parser->lexer);
13428           /* Peek at the next token.  */
13429           token = cp_lexer_peek_token (parser->lexer);
13430           /* If the next token is `]', then there is no
13431              constant-expression.  */
13432           if (token->type != CPP_CLOSE_SQUARE)
13433             {
13434               bool non_constant_p;
13435
13436               bounds
13437                 = cp_parser_constant_expression (parser,
13438                                                  /*allow_non_constant=*/true,
13439                                                  &non_constant_p);
13440               if (!non_constant_p)
13441                 bounds = fold_non_dependent_expr (bounds);
13442               /* Normally, the array bound must be an integral constant
13443                  expression.  However, as an extension, we allow VLAs
13444                  in function scopes.  */
13445               else if (!parser->in_function_body)
13446                 {
13447                   error_at (token->location,
13448                             "array bound is not an integer constant");
13449                   bounds = error_mark_node;
13450                 }
13451               else if (processing_template_decl && !error_operand_p (bounds))
13452                 {
13453                   /* Remember this wasn't a constant-expression.  */
13454                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13455                   TREE_SIDE_EFFECTS (bounds) = 1;
13456                 }
13457             }
13458           else
13459             bounds = NULL_TREE;
13460           /* Look for the closing `]'.  */
13461           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13462             {
13463               declarator = cp_error_declarator;
13464               break;
13465             }
13466
13467           declarator = make_array_declarator (declarator, bounds);
13468         }
13469       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13470         {
13471           {
13472             tree qualifying_scope;
13473             tree unqualified_name;
13474             special_function_kind sfk;
13475             bool abstract_ok;
13476             bool pack_expansion_p = false;
13477             cp_token *declarator_id_start_token;
13478
13479             /* Parse a declarator-id */
13480             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13481             if (abstract_ok)
13482               {
13483                 cp_parser_parse_tentatively (parser);
13484
13485                 /* If we see an ellipsis, we should be looking at a
13486                    parameter pack. */
13487                 if (token->type == CPP_ELLIPSIS)
13488                   {
13489                     /* Consume the `...' */
13490                     cp_lexer_consume_token (parser->lexer);
13491
13492                     pack_expansion_p = true;
13493                   }
13494               }
13495
13496             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13497             unqualified_name
13498               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13499             qualifying_scope = parser->scope;
13500             if (abstract_ok)
13501               {
13502                 bool okay = false;
13503
13504                 if (!unqualified_name && pack_expansion_p)
13505                   {
13506                     /* Check whether an error occurred. */
13507                     okay = !cp_parser_error_occurred (parser);
13508
13509                     /* We already consumed the ellipsis to mark a
13510                        parameter pack, but we have no way to report it,
13511                        so abort the tentative parse. We will be exiting
13512                        immediately anyway. */
13513                     cp_parser_abort_tentative_parse (parser);
13514                   }
13515                 else
13516                   okay = cp_parser_parse_definitely (parser);
13517
13518                 if (!okay)
13519                   unqualified_name = error_mark_node;
13520                 else if (unqualified_name
13521                          && (qualifying_scope
13522                              || (TREE_CODE (unqualified_name)
13523                                  != IDENTIFIER_NODE)))
13524                   {
13525                     cp_parser_error (parser, "expected unqualified-id");
13526                     unqualified_name = error_mark_node;
13527                   }
13528               }
13529
13530             if (!unqualified_name)
13531               return NULL;
13532             if (unqualified_name == error_mark_node)
13533               {
13534                 declarator = cp_error_declarator;
13535                 pack_expansion_p = false;
13536                 declarator->parameter_pack_p = false;
13537                 break;
13538               }
13539
13540             if (qualifying_scope && at_namespace_scope_p ()
13541                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13542               {
13543                 /* In the declaration of a member of a template class
13544                    outside of the class itself, the SCOPE will sometimes
13545                    be a TYPENAME_TYPE.  For example, given:
13546
13547                    template <typename T>
13548                    int S<T>::R::i = 3;
13549
13550                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13551                    this context, we must resolve S<T>::R to an ordinary
13552                    type, rather than a typename type.
13553
13554                    The reason we normally avoid resolving TYPENAME_TYPEs
13555                    is that a specialization of `S' might render
13556                    `S<T>::R' not a type.  However, if `S' is
13557                    specialized, then this `i' will not be used, so there
13558                    is no harm in resolving the types here.  */
13559                 tree type;
13560
13561                 /* Resolve the TYPENAME_TYPE.  */
13562                 type = resolve_typename_type (qualifying_scope,
13563                                               /*only_current_p=*/false);
13564                 /* If that failed, the declarator is invalid.  */
13565                 if (TREE_CODE (type) == TYPENAME_TYPE)
13566                   error_at (declarator_id_start_token->location,
13567                             "%<%T::%E%> is not a type",
13568                             TYPE_CONTEXT (qualifying_scope),
13569                             TYPE_IDENTIFIER (qualifying_scope));
13570                 qualifying_scope = type;
13571               }
13572
13573             sfk = sfk_none;
13574
13575             if (unqualified_name)
13576               {
13577                 tree class_type;
13578
13579                 if (qualifying_scope
13580                     && CLASS_TYPE_P (qualifying_scope))
13581                   class_type = qualifying_scope;
13582                 else
13583                   class_type = current_class_type;
13584
13585                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13586                   {
13587                     tree name_type = TREE_TYPE (unqualified_name);
13588                     if (class_type && same_type_p (name_type, class_type))
13589                       {
13590                         if (qualifying_scope
13591                             && CLASSTYPE_USE_TEMPLATE (name_type))
13592                           {
13593                             error_at (declarator_id_start_token->location,
13594                                       "invalid use of constructor as a template");
13595                             inform (declarator_id_start_token->location,
13596                                     "use %<%T::%D%> instead of %<%T::%D%> to "
13597                                     "name the constructor in a qualified name",
13598                                     class_type,
13599                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13600                                     class_type, name_type);
13601                             declarator = cp_error_declarator;
13602                             break;
13603                           }
13604                         else
13605                           unqualified_name = constructor_name (class_type);
13606                       }
13607                     else
13608                       {
13609                         /* We do not attempt to print the declarator
13610                            here because we do not have enough
13611                            information about its original syntactic
13612                            form.  */
13613                         cp_parser_error (parser, "invalid declarator");
13614                         declarator = cp_error_declarator;
13615                         break;
13616                       }
13617                   }
13618
13619                 if (class_type)
13620                   {
13621                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13622                       sfk = sfk_destructor;
13623                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13624                       sfk = sfk_conversion;
13625                     else if (/* There's no way to declare a constructor
13626                                 for an anonymous type, even if the type
13627                                 got a name for linkage purposes.  */
13628                              !TYPE_WAS_ANONYMOUS (class_type)
13629                              && constructor_name_p (unqualified_name,
13630                                                     class_type))
13631                       {
13632                         unqualified_name = constructor_name (class_type);
13633                         sfk = sfk_constructor;
13634                       }
13635
13636                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
13637                       *ctor_dtor_or_conv_p = -1;
13638                   }
13639               }
13640             declarator = make_id_declarator (qualifying_scope,
13641                                              unqualified_name,
13642                                              sfk);
13643             declarator->id_loc = token->location;
13644             declarator->parameter_pack_p = pack_expansion_p;
13645
13646             if (pack_expansion_p)
13647               maybe_warn_variadic_templates ();
13648           }
13649
13650         handle_declarator:;
13651           scope = get_scope_of_declarator (declarator);
13652           if (scope)
13653             /* Any names that appear after the declarator-id for a
13654                member are looked up in the containing scope.  */
13655             pushed_scope = push_scope (scope);
13656           parser->in_declarator_p = true;
13657           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13658               || (declarator && declarator->kind == cdk_id))
13659             /* Default args are only allowed on function
13660                declarations.  */
13661             parser->default_arg_ok_p = saved_default_arg_ok_p;
13662           else
13663             parser->default_arg_ok_p = false;
13664
13665           first = false;
13666         }
13667       /* We're done.  */
13668       else
13669         break;
13670     }
13671
13672   /* For an abstract declarator, we might wind up with nothing at this
13673      point.  That's an error; the declarator is not optional.  */
13674   if (!declarator)
13675     cp_parser_error (parser, "expected declarator");
13676
13677   /* If we entered a scope, we must exit it now.  */
13678   if (pushed_scope)
13679     pop_scope (pushed_scope);
13680
13681   parser->default_arg_ok_p = saved_default_arg_ok_p;
13682   parser->in_declarator_p = saved_in_declarator_p;
13683
13684   return declarator;
13685 }
13686
13687 /* Parse a ptr-operator.
13688
13689    ptr-operator:
13690      * cv-qualifier-seq [opt]
13691      &
13692      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13693
13694    GNU Extension:
13695
13696    ptr-operator:
13697      & cv-qualifier-seq [opt]
13698
13699    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13700    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13701    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13702    filled in with the TYPE containing the member.  *CV_QUALS is
13703    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13704    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13705    Note that the tree codes returned by this function have nothing
13706    to do with the types of trees that will be eventually be created
13707    to represent the pointer or reference type being parsed. They are
13708    just constants with suggestive names. */
13709 static enum tree_code
13710 cp_parser_ptr_operator (cp_parser* parser,
13711                         tree* type,
13712                         cp_cv_quals *cv_quals)
13713 {
13714   enum tree_code code = ERROR_MARK;
13715   cp_token *token;
13716
13717   /* Assume that it's not a pointer-to-member.  */
13718   *type = NULL_TREE;
13719   /* And that there are no cv-qualifiers.  */
13720   *cv_quals = TYPE_UNQUALIFIED;
13721
13722   /* Peek at the next token.  */
13723   token = cp_lexer_peek_token (parser->lexer);
13724
13725   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13726   if (token->type == CPP_MULT)
13727     code = INDIRECT_REF;
13728   else if (token->type == CPP_AND)
13729     code = ADDR_EXPR;
13730   else if ((cxx_dialect != cxx98) &&
13731            token->type == CPP_AND_AND) /* C++0x only */
13732     code = NON_LVALUE_EXPR;
13733
13734   if (code != ERROR_MARK)
13735     {
13736       /* Consume the `*', `&' or `&&'.  */
13737       cp_lexer_consume_token (parser->lexer);
13738
13739       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13740          `&', if we are allowing GNU extensions.  (The only qualifier
13741          that can legally appear after `&' is `restrict', but that is
13742          enforced during semantic analysis.  */
13743       if (code == INDIRECT_REF
13744           || cp_parser_allow_gnu_extensions_p (parser))
13745         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13746     }
13747   else
13748     {
13749       /* Try the pointer-to-member case.  */
13750       cp_parser_parse_tentatively (parser);
13751       /* Look for the optional `::' operator.  */
13752       cp_parser_global_scope_opt (parser,
13753                                   /*current_scope_valid_p=*/false);
13754       /* Look for the nested-name specifier.  */
13755       token = cp_lexer_peek_token (parser->lexer);
13756       cp_parser_nested_name_specifier (parser,
13757                                        /*typename_keyword_p=*/false,
13758                                        /*check_dependency_p=*/true,
13759                                        /*type_p=*/false,
13760                                        /*is_declaration=*/false);
13761       /* If we found it, and the next token is a `*', then we are
13762          indeed looking at a pointer-to-member operator.  */
13763       if (!cp_parser_error_occurred (parser)
13764           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13765         {
13766           /* Indicate that the `*' operator was used.  */
13767           code = INDIRECT_REF;
13768
13769           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13770             error_at (token->location, "%qD is a namespace", parser->scope);
13771           else
13772             {
13773               /* The type of which the member is a member is given by the
13774                  current SCOPE.  */
13775               *type = parser->scope;
13776               /* The next name will not be qualified.  */
13777               parser->scope = NULL_TREE;
13778               parser->qualifying_scope = NULL_TREE;
13779               parser->object_scope = NULL_TREE;
13780               /* Look for the optional cv-qualifier-seq.  */
13781               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13782             }
13783         }
13784       /* If that didn't work we don't have a ptr-operator.  */
13785       if (!cp_parser_parse_definitely (parser))
13786         cp_parser_error (parser, "expected ptr-operator");
13787     }
13788
13789   return code;
13790 }
13791
13792 /* Parse an (optional) cv-qualifier-seq.
13793
13794    cv-qualifier-seq:
13795      cv-qualifier cv-qualifier-seq [opt]
13796
13797    cv-qualifier:
13798      const
13799      volatile
13800
13801    GNU Extension:
13802
13803    cv-qualifier:
13804      __restrict__
13805
13806    Returns a bitmask representing the cv-qualifiers.  */
13807
13808 static cp_cv_quals
13809 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13810 {
13811   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13812
13813   while (true)
13814     {
13815       cp_token *token;
13816       cp_cv_quals cv_qualifier;
13817
13818       /* Peek at the next token.  */
13819       token = cp_lexer_peek_token (parser->lexer);
13820       /* See if it's a cv-qualifier.  */
13821       switch (token->keyword)
13822         {
13823         case RID_CONST:
13824           cv_qualifier = TYPE_QUAL_CONST;
13825           break;
13826
13827         case RID_VOLATILE:
13828           cv_qualifier = TYPE_QUAL_VOLATILE;
13829           break;
13830
13831         case RID_RESTRICT:
13832           cv_qualifier = TYPE_QUAL_RESTRICT;
13833           break;
13834
13835         default:
13836           cv_qualifier = TYPE_UNQUALIFIED;
13837           break;
13838         }
13839
13840       if (!cv_qualifier)
13841         break;
13842
13843       if (cv_quals & cv_qualifier)
13844         {
13845           error_at (token->location, "duplicate cv-qualifier");
13846           cp_lexer_purge_token (parser->lexer);
13847         }
13848       else
13849         {
13850           cp_lexer_consume_token (parser->lexer);
13851           cv_quals |= cv_qualifier;
13852         }
13853     }
13854
13855   return cv_quals;
13856 }
13857
13858 /* Parse a late-specified return type, if any.  This is not a separate
13859    non-terminal, but part of a function declarator, which looks like
13860
13861    -> type-id
13862
13863    Returns the type indicated by the type-id.  */
13864
13865 static tree
13866 cp_parser_late_return_type_opt (cp_parser* parser)
13867 {
13868   cp_token *token;
13869
13870   /* Peek at the next token.  */
13871   token = cp_lexer_peek_token (parser->lexer);
13872   /* A late-specified return type is indicated by an initial '->'. */
13873   if (token->type != CPP_DEREF)
13874     return NULL_TREE;
13875
13876   /* Consume the ->.  */
13877   cp_lexer_consume_token (parser->lexer);
13878
13879   return cp_parser_type_id (parser);
13880 }
13881
13882 /* Parse a declarator-id.
13883
13884    declarator-id:
13885      id-expression
13886      :: [opt] nested-name-specifier [opt] type-name
13887
13888    In the `id-expression' case, the value returned is as for
13889    cp_parser_id_expression if the id-expression was an unqualified-id.
13890    If the id-expression was a qualified-id, then a SCOPE_REF is
13891    returned.  The first operand is the scope (either a NAMESPACE_DECL
13892    or TREE_TYPE), but the second is still just a representation of an
13893    unqualified-id.  */
13894
13895 static tree
13896 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13897 {
13898   tree id;
13899   /* The expression must be an id-expression.  Assume that qualified
13900      names are the names of types so that:
13901
13902        template <class T>
13903        int S<T>::R::i = 3;
13904
13905      will work; we must treat `S<T>::R' as the name of a type.
13906      Similarly, assume that qualified names are templates, where
13907      required, so that:
13908
13909        template <class T>
13910        int S<T>::R<T>::i = 3;
13911
13912      will work, too.  */
13913   id = cp_parser_id_expression (parser,
13914                                 /*template_keyword_p=*/false,
13915                                 /*check_dependency_p=*/false,
13916                                 /*template_p=*/NULL,
13917                                 /*declarator_p=*/true,
13918                                 optional_p);
13919   if (id && BASELINK_P (id))
13920     id = BASELINK_FUNCTIONS (id);
13921   return id;
13922 }
13923
13924 /* Parse a type-id.
13925
13926    type-id:
13927      type-specifier-seq abstract-declarator [opt]
13928
13929    Returns the TYPE specified.  */
13930
13931 static tree
13932 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13933 {
13934   cp_decl_specifier_seq type_specifier_seq;
13935   cp_declarator *abstract_declarator;
13936
13937   /* Parse the type-specifier-seq.  */
13938   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13939                                 &type_specifier_seq);
13940   if (type_specifier_seq.type == error_mark_node)
13941     return error_mark_node;
13942
13943   /* There might or might not be an abstract declarator.  */
13944   cp_parser_parse_tentatively (parser);
13945   /* Look for the declarator.  */
13946   abstract_declarator
13947     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13948                             /*parenthesized_p=*/NULL,
13949                             /*member_p=*/false);
13950   /* Check to see if there really was a declarator.  */
13951   if (!cp_parser_parse_definitely (parser))
13952     abstract_declarator = NULL;
13953
13954   if (type_specifier_seq.type
13955       && type_uses_auto (type_specifier_seq.type))
13956     {
13957       /* A type-id with type 'auto' is only ok if the abstract declarator
13958          is a function declarator with a late-specified return type.  */
13959       if (abstract_declarator
13960           && abstract_declarator->kind == cdk_function
13961           && abstract_declarator->u.function.late_return_type)
13962         /* OK */;
13963       else
13964         {
13965           error ("invalid use of %<auto%>");
13966           return error_mark_node;
13967         }
13968     }
13969   
13970   return groktypename (&type_specifier_seq, abstract_declarator,
13971                        is_template_arg);
13972 }
13973
13974 static tree cp_parser_type_id (cp_parser *parser)
13975 {
13976   return cp_parser_type_id_1 (parser, false);
13977 }
13978
13979 static tree cp_parser_template_type_arg (cp_parser *parser)
13980 {
13981   return cp_parser_type_id_1 (parser, true);
13982 }
13983
13984 /* Parse a type-specifier-seq.
13985
13986    type-specifier-seq:
13987      type-specifier type-specifier-seq [opt]
13988
13989    GNU extension:
13990
13991    type-specifier-seq:
13992      attributes type-specifier-seq [opt]
13993
13994    If IS_CONDITION is true, we are at the start of a "condition",
13995    e.g., we've just seen "if (".
13996
13997    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13998
13999 static void
14000 cp_parser_type_specifier_seq (cp_parser* parser,
14001                               bool is_condition,
14002                               cp_decl_specifier_seq *type_specifier_seq)
14003 {
14004   bool seen_type_specifier = false;
14005   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14006   cp_token *start_token = NULL;
14007
14008   /* Clear the TYPE_SPECIFIER_SEQ.  */
14009   clear_decl_specs (type_specifier_seq);
14010
14011   /* Parse the type-specifiers and attributes.  */
14012   while (true)
14013     {
14014       tree type_specifier;
14015       bool is_cv_qualifier;
14016
14017       /* Check for attributes first.  */
14018       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14019         {
14020           type_specifier_seq->attributes =
14021             chainon (type_specifier_seq->attributes,
14022                      cp_parser_attributes_opt (parser));
14023           continue;
14024         }
14025
14026       /* record the token of the beginning of the type specifier seq,
14027          for error reporting purposes*/
14028      if (!start_token)
14029        start_token = cp_lexer_peek_token (parser->lexer);
14030
14031       /* Look for the type-specifier.  */
14032       type_specifier = cp_parser_type_specifier (parser,
14033                                                  flags,
14034                                                  type_specifier_seq,
14035                                                  /*is_declaration=*/false,
14036                                                  NULL,
14037                                                  &is_cv_qualifier);
14038       if (!type_specifier)
14039         {
14040           /* If the first type-specifier could not be found, this is not a
14041              type-specifier-seq at all.  */
14042           if (!seen_type_specifier)
14043             {
14044               cp_parser_error (parser, "expected type-specifier");
14045               type_specifier_seq->type = error_mark_node;
14046               return;
14047             }
14048           /* If subsequent type-specifiers could not be found, the
14049              type-specifier-seq is complete.  */
14050           break;
14051         }
14052
14053       seen_type_specifier = true;
14054       /* The standard says that a condition can be:
14055
14056             type-specifier-seq declarator = assignment-expression
14057
14058          However, given:
14059
14060            struct S {};
14061            if (int S = ...)
14062
14063          we should treat the "S" as a declarator, not as a
14064          type-specifier.  The standard doesn't say that explicitly for
14065          type-specifier-seq, but it does say that for
14066          decl-specifier-seq in an ordinary declaration.  Perhaps it
14067          would be clearer just to allow a decl-specifier-seq here, and
14068          then add a semantic restriction that if any decl-specifiers
14069          that are not type-specifiers appear, the program is invalid.  */
14070       if (is_condition && !is_cv_qualifier)
14071         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14072     }
14073
14074   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14075 }
14076
14077 /* Parse a parameter-declaration-clause.
14078
14079    parameter-declaration-clause:
14080      parameter-declaration-list [opt] ... [opt]
14081      parameter-declaration-list , ...
14082
14083    Returns a representation for the parameter declarations.  A return
14084    value of NULL indicates a parameter-declaration-clause consisting
14085    only of an ellipsis.  */
14086
14087 static tree
14088 cp_parser_parameter_declaration_clause (cp_parser* parser)
14089 {
14090   tree parameters;
14091   cp_token *token;
14092   bool ellipsis_p;
14093   bool is_error;
14094
14095   /* Peek at the next token.  */
14096   token = cp_lexer_peek_token (parser->lexer);
14097   /* Check for trivial parameter-declaration-clauses.  */
14098   if (token->type == CPP_ELLIPSIS)
14099     {
14100       /* Consume the `...' token.  */
14101       cp_lexer_consume_token (parser->lexer);
14102       return NULL_TREE;
14103     }
14104   else if (token->type == CPP_CLOSE_PAREN)
14105     /* There are no parameters.  */
14106     {
14107 #ifndef NO_IMPLICIT_EXTERN_C
14108       if (in_system_header && current_class_type == NULL
14109           && current_lang_name == lang_name_c)
14110         return NULL_TREE;
14111       else
14112 #endif
14113         return void_list_node;
14114     }
14115   /* Check for `(void)', too, which is a special case.  */
14116   else if (token->keyword == RID_VOID
14117            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14118                == CPP_CLOSE_PAREN))
14119     {
14120       /* Consume the `void' token.  */
14121       cp_lexer_consume_token (parser->lexer);
14122       /* There are no parameters.  */
14123       return void_list_node;
14124     }
14125
14126   /* Parse the parameter-declaration-list.  */
14127   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14128   /* If a parse error occurred while parsing the
14129      parameter-declaration-list, then the entire
14130      parameter-declaration-clause is erroneous.  */
14131   if (is_error)
14132     return NULL;
14133
14134   /* Peek at the next token.  */
14135   token = cp_lexer_peek_token (parser->lexer);
14136   /* If it's a `,', the clause should terminate with an ellipsis.  */
14137   if (token->type == CPP_COMMA)
14138     {
14139       /* Consume the `,'.  */
14140       cp_lexer_consume_token (parser->lexer);
14141       /* Expect an ellipsis.  */
14142       ellipsis_p
14143         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14144     }
14145   /* It might also be `...' if the optional trailing `,' was
14146      omitted.  */
14147   else if (token->type == CPP_ELLIPSIS)
14148     {
14149       /* Consume the `...' token.  */
14150       cp_lexer_consume_token (parser->lexer);
14151       /* And remember that we saw it.  */
14152       ellipsis_p = true;
14153     }
14154   else
14155     ellipsis_p = false;
14156
14157   /* Finish the parameter list.  */
14158   if (!ellipsis_p)
14159     parameters = chainon (parameters, void_list_node);
14160
14161   return parameters;
14162 }
14163
14164 /* Parse a parameter-declaration-list.
14165
14166    parameter-declaration-list:
14167      parameter-declaration
14168      parameter-declaration-list , parameter-declaration
14169
14170    Returns a representation of the parameter-declaration-list, as for
14171    cp_parser_parameter_declaration_clause.  However, the
14172    `void_list_node' is never appended to the list.  Upon return,
14173    *IS_ERROR will be true iff an error occurred.  */
14174
14175 static tree
14176 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14177 {
14178   tree parameters = NULL_TREE;
14179   tree *tail = &parameters; 
14180   bool saved_in_unbraced_linkage_specification_p;
14181   int index = 0;
14182
14183   /* Assume all will go well.  */
14184   *is_error = false;
14185   /* The special considerations that apply to a function within an
14186      unbraced linkage specifications do not apply to the parameters
14187      to the function.  */
14188   saved_in_unbraced_linkage_specification_p 
14189     = parser->in_unbraced_linkage_specification_p;
14190   parser->in_unbraced_linkage_specification_p = false;
14191
14192   /* Look for more parameters.  */
14193   while (true)
14194     {
14195       cp_parameter_declarator *parameter;
14196       tree decl = error_mark_node;
14197       bool parenthesized_p;
14198       /* Parse the parameter.  */
14199       parameter
14200         = cp_parser_parameter_declaration (parser,
14201                                            /*template_parm_p=*/false,
14202                                            &parenthesized_p);
14203
14204       /* We don't know yet if the enclosing context is deprecated, so wait
14205          and warn in grokparms if appropriate.  */
14206       deprecated_state = DEPRECATED_SUPPRESS;
14207
14208       if (parameter)
14209         decl = grokdeclarator (parameter->declarator,
14210                                &parameter->decl_specifiers,
14211                                PARM,
14212                                parameter->default_argument != NULL_TREE,
14213                                &parameter->decl_specifiers.attributes);
14214
14215       deprecated_state = DEPRECATED_NORMAL;
14216
14217       /* If a parse error occurred parsing the parameter declaration,
14218          then the entire parameter-declaration-list is erroneous.  */
14219       if (decl == error_mark_node)
14220         {
14221           *is_error = true;
14222           parameters = error_mark_node;
14223           break;
14224         }
14225
14226       if (parameter->decl_specifiers.attributes)
14227         cplus_decl_attributes (&decl,
14228                                parameter->decl_specifiers.attributes,
14229                                0);
14230       if (DECL_NAME (decl))
14231         decl = pushdecl (decl);
14232
14233       if (decl != error_mark_node)
14234         {
14235           retrofit_lang_decl (decl);
14236           DECL_PARM_INDEX (decl) = ++index;
14237         }
14238
14239       /* Add the new parameter to the list.  */
14240       *tail = build_tree_list (parameter->default_argument, decl);
14241       tail = &TREE_CHAIN (*tail);
14242
14243       /* Peek at the next token.  */
14244       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14245           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14246           /* These are for Objective-C++ */
14247           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14248           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14249         /* The parameter-declaration-list is complete.  */
14250         break;
14251       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14252         {
14253           cp_token *token;
14254
14255           /* Peek at the next token.  */
14256           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14257           /* If it's an ellipsis, then the list is complete.  */
14258           if (token->type == CPP_ELLIPSIS)
14259             break;
14260           /* Otherwise, there must be more parameters.  Consume the
14261              `,'.  */
14262           cp_lexer_consume_token (parser->lexer);
14263           /* When parsing something like:
14264
14265                 int i(float f, double d)
14266
14267              we can tell after seeing the declaration for "f" that we
14268              are not looking at an initialization of a variable "i",
14269              but rather at the declaration of a function "i".
14270
14271              Due to the fact that the parsing of template arguments
14272              (as specified to a template-id) requires backtracking we
14273              cannot use this technique when inside a template argument
14274              list.  */
14275           if (!parser->in_template_argument_list_p
14276               && !parser->in_type_id_in_expr_p
14277               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14278               /* However, a parameter-declaration of the form
14279                  "foat(f)" (which is a valid declaration of a
14280                  parameter "f") can also be interpreted as an
14281                  expression (the conversion of "f" to "float").  */
14282               && !parenthesized_p)
14283             cp_parser_commit_to_tentative_parse (parser);
14284         }
14285       else
14286         {
14287           cp_parser_error (parser, "expected %<,%> or %<...%>");
14288           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14289             cp_parser_skip_to_closing_parenthesis (parser,
14290                                                    /*recovering=*/true,
14291                                                    /*or_comma=*/false,
14292                                                    /*consume_paren=*/false);
14293           break;
14294         }
14295     }
14296
14297   parser->in_unbraced_linkage_specification_p
14298     = saved_in_unbraced_linkage_specification_p;
14299
14300   return parameters;
14301 }
14302
14303 /* Parse a parameter declaration.
14304
14305    parameter-declaration:
14306      decl-specifier-seq ... [opt] declarator
14307      decl-specifier-seq declarator = assignment-expression
14308      decl-specifier-seq ... [opt] abstract-declarator [opt]
14309      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14310
14311    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14312    declares a template parameter.  (In that case, a non-nested `>'
14313    token encountered during the parsing of the assignment-expression
14314    is not interpreted as a greater-than operator.)
14315
14316    Returns a representation of the parameter, or NULL if an error
14317    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14318    true iff the declarator is of the form "(p)".  */
14319
14320 static cp_parameter_declarator *
14321 cp_parser_parameter_declaration (cp_parser *parser,
14322                                  bool template_parm_p,
14323                                  bool *parenthesized_p)
14324 {
14325   int declares_class_or_enum;
14326   bool greater_than_is_operator_p;
14327   cp_decl_specifier_seq decl_specifiers;
14328   cp_declarator *declarator;
14329   tree default_argument;
14330   cp_token *token = NULL, *declarator_token_start = NULL;
14331   const char *saved_message;
14332
14333   /* In a template parameter, `>' is not an operator.
14334
14335      [temp.param]
14336
14337      When parsing a default template-argument for a non-type
14338      template-parameter, the first non-nested `>' is taken as the end
14339      of the template parameter-list rather than a greater-than
14340      operator.  */
14341   greater_than_is_operator_p = !template_parm_p;
14342
14343   /* Type definitions may not appear in parameter types.  */
14344   saved_message = parser->type_definition_forbidden_message;
14345   parser->type_definition_forbidden_message
14346     = "types may not be defined in parameter types";
14347
14348   /* Parse the declaration-specifiers.  */
14349   cp_parser_decl_specifier_seq (parser,
14350                                 CP_PARSER_FLAGS_NONE,
14351                                 &decl_specifiers,
14352                                 &declares_class_or_enum);
14353   /* If an error occurred, there's no reason to attempt to parse the
14354      rest of the declaration.  */
14355   if (cp_parser_error_occurred (parser))
14356     {
14357       parser->type_definition_forbidden_message = saved_message;
14358       return NULL;
14359     }
14360
14361   /* Peek at the next token.  */
14362   token = cp_lexer_peek_token (parser->lexer);
14363
14364   /* If the next token is a `)', `,', `=', `>', or `...', then there
14365      is no declarator. However, when variadic templates are enabled,
14366      there may be a declarator following `...'.  */
14367   if (token->type == CPP_CLOSE_PAREN
14368       || token->type == CPP_COMMA
14369       || token->type == CPP_EQ
14370       || token->type == CPP_GREATER)
14371     {
14372       declarator = NULL;
14373       if (parenthesized_p)
14374         *parenthesized_p = false;
14375     }
14376   /* Otherwise, there should be a declarator.  */
14377   else
14378     {
14379       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14380       parser->default_arg_ok_p = false;
14381
14382       /* After seeing a decl-specifier-seq, if the next token is not a
14383          "(", there is no possibility that the code is a valid
14384          expression.  Therefore, if parsing tentatively, we commit at
14385          this point.  */
14386       if (!parser->in_template_argument_list_p
14387           /* In an expression context, having seen:
14388
14389                (int((char ...
14390
14391              we cannot be sure whether we are looking at a
14392              function-type (taking a "char" as a parameter) or a cast
14393              of some object of type "char" to "int".  */
14394           && !parser->in_type_id_in_expr_p
14395           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14396           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14397         cp_parser_commit_to_tentative_parse (parser);
14398       /* Parse the declarator.  */
14399       declarator_token_start = token;
14400       declarator = cp_parser_declarator (parser,
14401                                          CP_PARSER_DECLARATOR_EITHER,
14402                                          /*ctor_dtor_or_conv_p=*/NULL,
14403                                          parenthesized_p,
14404                                          /*member_p=*/false);
14405       parser->default_arg_ok_p = saved_default_arg_ok_p;
14406       /* After the declarator, allow more attributes.  */
14407       decl_specifiers.attributes
14408         = chainon (decl_specifiers.attributes,
14409                    cp_parser_attributes_opt (parser));
14410     }
14411
14412   /* If the next token is an ellipsis, and we have not seen a
14413      declarator name, and the type of the declarator contains parameter
14414      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14415      a parameter pack expansion expression. Otherwise, leave the
14416      ellipsis for a C-style variadic function. */
14417   token = cp_lexer_peek_token (parser->lexer);
14418   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14419     {
14420       tree type = decl_specifiers.type;
14421
14422       if (type && DECL_P (type))
14423         type = TREE_TYPE (type);
14424
14425       if (type
14426           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14427           && declarator_can_be_parameter_pack (declarator)
14428           && (!declarator || !declarator->parameter_pack_p)
14429           && uses_parameter_packs (type))
14430         {
14431           /* Consume the `...'. */
14432           cp_lexer_consume_token (parser->lexer);
14433           maybe_warn_variadic_templates ();
14434           
14435           /* Build a pack expansion type */
14436           if (declarator)
14437             declarator->parameter_pack_p = true;
14438           else
14439             decl_specifiers.type = make_pack_expansion (type);
14440         }
14441     }
14442
14443   /* The restriction on defining new types applies only to the type
14444      of the parameter, not to the default argument.  */
14445   parser->type_definition_forbidden_message = saved_message;
14446
14447   /* If the next token is `=', then process a default argument.  */
14448   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14449     {
14450       /* Consume the `='.  */
14451       cp_lexer_consume_token (parser->lexer);
14452
14453       /* If we are defining a class, then the tokens that make up the
14454          default argument must be saved and processed later.  */
14455       if (!template_parm_p && at_class_scope_p ()
14456           && TYPE_BEING_DEFINED (current_class_type))
14457         {
14458           unsigned depth = 0;
14459           int maybe_template_id = 0;
14460           cp_token *first_token;
14461           cp_token *token;
14462
14463           /* Add tokens until we have processed the entire default
14464              argument.  We add the range [first_token, token).  */
14465           first_token = cp_lexer_peek_token (parser->lexer);
14466           while (true)
14467             {
14468               bool done = false;
14469
14470               /* Peek at the next token.  */
14471               token = cp_lexer_peek_token (parser->lexer);
14472               /* What we do depends on what token we have.  */
14473               switch (token->type)
14474                 {
14475                   /* In valid code, a default argument must be
14476                      immediately followed by a `,' `)', or `...'.  */
14477                 case CPP_COMMA:
14478                   if (depth == 0 && maybe_template_id)
14479                     {
14480                       /* If we've seen a '<', we might be in a
14481                          template-argument-list.  Until Core issue 325 is
14482                          resolved, we don't know how this situation ought
14483                          to be handled, so try to DTRT.  We check whether
14484                          what comes after the comma is a valid parameter
14485                          declaration list.  If it is, then the comma ends
14486                          the default argument; otherwise the default
14487                          argument continues.  */
14488                       bool error = false;
14489
14490                       /* Set ITALP so cp_parser_parameter_declaration_list
14491                          doesn't decide to commit to this parse.  */
14492                       bool saved_italp = parser->in_template_argument_list_p;
14493                       parser->in_template_argument_list_p = true;
14494
14495                       cp_parser_parse_tentatively (parser);
14496                       cp_lexer_consume_token (parser->lexer);
14497                       cp_parser_parameter_declaration_list (parser, &error);
14498                       if (!cp_parser_error_occurred (parser) && !error)
14499                         done = true;
14500                       cp_parser_abort_tentative_parse (parser);
14501
14502                       parser->in_template_argument_list_p = saved_italp;
14503                       break;
14504                     }
14505                 case CPP_CLOSE_PAREN:
14506                 case CPP_ELLIPSIS:
14507                   /* If we run into a non-nested `;', `}', or `]',
14508                      then the code is invalid -- but the default
14509                      argument is certainly over.  */
14510                 case CPP_SEMICOLON:
14511                 case CPP_CLOSE_BRACE:
14512                 case CPP_CLOSE_SQUARE:
14513                   if (depth == 0)
14514                     done = true;
14515                   /* Update DEPTH, if necessary.  */
14516                   else if (token->type == CPP_CLOSE_PAREN
14517                            || token->type == CPP_CLOSE_BRACE
14518                            || token->type == CPP_CLOSE_SQUARE)
14519                     --depth;
14520                   break;
14521
14522                 case CPP_OPEN_PAREN:
14523                 case CPP_OPEN_SQUARE:
14524                 case CPP_OPEN_BRACE:
14525                   ++depth;
14526                   break;
14527
14528                 case CPP_LESS:
14529                   if (depth == 0)
14530                     /* This might be the comparison operator, or it might
14531                        start a template argument list.  */
14532                     ++maybe_template_id;
14533                   break;
14534
14535                 case CPP_RSHIFT:
14536                   if (cxx_dialect == cxx98)
14537                     break;
14538                   /* Fall through for C++0x, which treats the `>>'
14539                      operator like two `>' tokens in certain
14540                      cases.  */
14541
14542                 case CPP_GREATER:
14543                   if (depth == 0)
14544                     {
14545                       /* This might be an operator, or it might close a
14546                          template argument list.  But if a previous '<'
14547                          started a template argument list, this will have
14548                          closed it, so we can't be in one anymore.  */
14549                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14550                       if (maybe_template_id < 0)
14551                         maybe_template_id = 0;
14552                     }
14553                   break;
14554
14555                   /* If we run out of tokens, issue an error message.  */
14556                 case CPP_EOF:
14557                 case CPP_PRAGMA_EOL:
14558                   error_at (token->location, "file ends in default argument");
14559                   done = true;
14560                   break;
14561
14562                 case CPP_NAME:
14563                 case CPP_SCOPE:
14564                   /* In these cases, we should look for template-ids.
14565                      For example, if the default argument is
14566                      `X<int, double>()', we need to do name lookup to
14567                      figure out whether or not `X' is a template; if
14568                      so, the `,' does not end the default argument.
14569
14570                      That is not yet done.  */
14571                   break;
14572
14573                 default:
14574                   break;
14575                 }
14576
14577               /* If we've reached the end, stop.  */
14578               if (done)
14579                 break;
14580
14581               /* Add the token to the token block.  */
14582               token = cp_lexer_consume_token (parser->lexer);
14583             }
14584
14585           /* Create a DEFAULT_ARG to represent the unparsed default
14586              argument.  */
14587           default_argument = make_node (DEFAULT_ARG);
14588           DEFARG_TOKENS (default_argument)
14589             = cp_token_cache_new (first_token, token);
14590           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14591         }
14592       /* Outside of a class definition, we can just parse the
14593          assignment-expression.  */
14594       else
14595         {
14596           token = cp_lexer_peek_token (parser->lexer);
14597           default_argument 
14598             = cp_parser_default_argument (parser, template_parm_p);
14599         }
14600
14601       if (!parser->default_arg_ok_p)
14602         {
14603           if (flag_permissive)
14604             warning (0, "deprecated use of default argument for parameter of non-function");
14605           else
14606             {
14607               error_at (token->location,
14608                         "default arguments are only "
14609                         "permitted for function parameters");
14610               default_argument = NULL_TREE;
14611             }
14612         }
14613       else if ((declarator && declarator->parameter_pack_p)
14614                || (decl_specifiers.type
14615                    && PACK_EXPANSION_P (decl_specifiers.type)))
14616         {
14617           /* Find the name of the parameter pack.  */     
14618           cp_declarator *id_declarator = declarator;
14619           while (id_declarator && id_declarator->kind != cdk_id)
14620             id_declarator = id_declarator->declarator;
14621           
14622           if (id_declarator && id_declarator->kind == cdk_id)
14623             error_at (declarator_token_start->location,
14624                       template_parm_p 
14625                       ? "template parameter pack %qD"
14626                       " cannot have a default argument"
14627                       : "parameter pack %qD cannot have a default argument",
14628                       id_declarator->u.id.unqualified_name);
14629           else
14630             error_at (declarator_token_start->location,
14631                       template_parm_p 
14632                       ? "template parameter pack cannot have a default argument"
14633                       : "parameter pack cannot have a default argument");
14634           
14635           default_argument = NULL_TREE;
14636         }
14637     }
14638   else
14639     default_argument = NULL_TREE;
14640
14641   return make_parameter_declarator (&decl_specifiers,
14642                                     declarator,
14643                                     default_argument);
14644 }
14645
14646 /* Parse a default argument and return it.
14647
14648    TEMPLATE_PARM_P is true if this is a default argument for a
14649    non-type template parameter.  */
14650 static tree
14651 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14652 {
14653   tree default_argument = NULL_TREE;
14654   bool saved_greater_than_is_operator_p;
14655   bool saved_local_variables_forbidden_p;
14656
14657   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14658      set correctly.  */
14659   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14660   parser->greater_than_is_operator_p = !template_parm_p;
14661   /* Local variable names (and the `this' keyword) may not
14662      appear in a default argument.  */
14663   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14664   parser->local_variables_forbidden_p = true;
14665   /* Parse the assignment-expression.  */
14666   if (template_parm_p)
14667     push_deferring_access_checks (dk_no_deferred);
14668   default_argument
14669     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14670   if (template_parm_p)
14671     pop_deferring_access_checks ();
14672   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14673   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14674
14675   return default_argument;
14676 }
14677
14678 /* Parse a function-body.
14679
14680    function-body:
14681      compound_statement  */
14682
14683 static void
14684 cp_parser_function_body (cp_parser *parser)
14685 {
14686   cp_parser_compound_statement (parser, NULL, false);
14687 }
14688
14689 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14690    true if a ctor-initializer was present.  */
14691
14692 static bool
14693 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14694 {
14695   tree body;
14696   bool ctor_initializer_p;
14697
14698   /* Begin the function body.  */
14699   body = begin_function_body ();
14700   /* Parse the optional ctor-initializer.  */
14701   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14702   /* Parse the function-body.  */
14703   cp_parser_function_body (parser);
14704   /* Finish the function body.  */
14705   finish_function_body (body);
14706
14707   return ctor_initializer_p;
14708 }
14709
14710 /* Parse an initializer.
14711
14712    initializer:
14713      = initializer-clause
14714      ( expression-list )
14715
14716    Returns an expression representing the initializer.  If no
14717    initializer is present, NULL_TREE is returned.
14718
14719    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14720    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14721    set to TRUE if there is no initializer present.  If there is an
14722    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14723    is set to true; otherwise it is set to false.  */
14724
14725 static tree
14726 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14727                        bool* non_constant_p)
14728 {
14729   cp_token *token;
14730   tree init;
14731
14732   /* Peek at the next token.  */
14733   token = cp_lexer_peek_token (parser->lexer);
14734
14735   /* Let our caller know whether or not this initializer was
14736      parenthesized.  */
14737   *is_direct_init = (token->type != CPP_EQ);
14738   /* Assume that the initializer is constant.  */
14739   *non_constant_p = false;
14740
14741   if (token->type == CPP_EQ)
14742     {
14743       /* Consume the `='.  */
14744       cp_lexer_consume_token (parser->lexer);
14745       /* Parse the initializer-clause.  */
14746       init = cp_parser_initializer_clause (parser, non_constant_p);
14747     }
14748   else if (token->type == CPP_OPEN_PAREN)
14749     {
14750       VEC(tree,gc) *vec;
14751       vec = cp_parser_parenthesized_expression_list (parser, false,
14752                                                      /*cast_p=*/false,
14753                                                      /*allow_expansion_p=*/true,
14754                                                      non_constant_p);
14755       if (vec == NULL)
14756         return error_mark_node;
14757       init = build_tree_list_vec (vec);
14758       release_tree_vector (vec);
14759     }
14760   else if (token->type == CPP_OPEN_BRACE)
14761     {
14762       maybe_warn_cpp0x ("extended initializer lists");
14763       init = cp_parser_braced_list (parser, non_constant_p);
14764       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14765     }
14766   else
14767     {
14768       /* Anything else is an error.  */
14769       cp_parser_error (parser, "expected initializer");
14770       init = error_mark_node;
14771     }
14772
14773   return init;
14774 }
14775
14776 /* Parse an initializer-clause.
14777
14778    initializer-clause:
14779      assignment-expression
14780      braced-init-list
14781
14782    Returns an expression representing the initializer.
14783
14784    If the `assignment-expression' production is used the value
14785    returned is simply a representation for the expression.
14786
14787    Otherwise, calls cp_parser_braced_list.  */
14788
14789 static tree
14790 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14791 {
14792   tree initializer;
14793
14794   /* Assume the expression is constant.  */
14795   *non_constant_p = false;
14796
14797   /* If it is not a `{', then we are looking at an
14798      assignment-expression.  */
14799   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14800     {
14801       initializer
14802         = cp_parser_constant_expression (parser,
14803                                         /*allow_non_constant_p=*/true,
14804                                         non_constant_p);
14805       if (!*non_constant_p)
14806         initializer = fold_non_dependent_expr (initializer);
14807     }
14808   else
14809     initializer = cp_parser_braced_list (parser, non_constant_p);
14810
14811   return initializer;
14812 }
14813
14814 /* Parse a brace-enclosed initializer list.
14815
14816    braced-init-list:
14817      { initializer-list , [opt] }
14818      { }
14819
14820    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14821    the elements of the initializer-list (or NULL, if the last
14822    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14823    NULL_TREE.  There is no way to detect whether or not the optional
14824    trailing `,' was provided.  NON_CONSTANT_P is as for
14825    cp_parser_initializer.  */     
14826
14827 static tree
14828 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14829 {
14830   tree initializer;
14831
14832   /* Consume the `{' token.  */
14833   cp_lexer_consume_token (parser->lexer);
14834   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14835   initializer = make_node (CONSTRUCTOR);
14836   /* If it's not a `}', then there is a non-trivial initializer.  */
14837   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14838     {
14839       /* Parse the initializer list.  */
14840       CONSTRUCTOR_ELTS (initializer)
14841         = cp_parser_initializer_list (parser, non_constant_p);
14842       /* A trailing `,' token is allowed.  */
14843       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14844         cp_lexer_consume_token (parser->lexer);
14845     }
14846   /* Now, there should be a trailing `}'.  */
14847   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14848   TREE_TYPE (initializer) = init_list_type_node;
14849   return initializer;
14850 }
14851
14852 /* Parse an initializer-list.
14853
14854    initializer-list:
14855      initializer-clause ... [opt]
14856      initializer-list , initializer-clause ... [opt]
14857
14858    GNU Extension:
14859
14860    initializer-list:
14861      identifier : initializer-clause
14862      initializer-list, identifier : initializer-clause
14863
14864    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14865    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14866    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14867    as for cp_parser_initializer.  */
14868
14869 static VEC(constructor_elt,gc) *
14870 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14871 {
14872   VEC(constructor_elt,gc) *v = NULL;
14873
14874   /* Assume all of the expressions are constant.  */
14875   *non_constant_p = false;
14876
14877   /* Parse the rest of the list.  */
14878   while (true)
14879     {
14880       cp_token *token;
14881       tree identifier;
14882       tree initializer;
14883       bool clause_non_constant_p;
14884
14885       /* If the next token is an identifier and the following one is a
14886          colon, we are looking at the GNU designated-initializer
14887          syntax.  */
14888       if (cp_parser_allow_gnu_extensions_p (parser)
14889           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14890           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14891         {
14892           /* Warn the user that they are using an extension.  */
14893           pedwarn (input_location, OPT_pedantic, 
14894                    "ISO C++ does not allow designated initializers");
14895           /* Consume the identifier.  */
14896           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14897           /* Consume the `:'.  */
14898           cp_lexer_consume_token (parser->lexer);
14899         }
14900       else
14901         identifier = NULL_TREE;
14902
14903       /* Parse the initializer.  */
14904       initializer = cp_parser_initializer_clause (parser,
14905                                                   &clause_non_constant_p);
14906       /* If any clause is non-constant, so is the entire initializer.  */
14907       if (clause_non_constant_p)
14908         *non_constant_p = true;
14909
14910       /* If we have an ellipsis, this is an initializer pack
14911          expansion.  */
14912       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14913         {
14914           /* Consume the `...'.  */
14915           cp_lexer_consume_token (parser->lexer);
14916
14917           /* Turn the initializer into an initializer expansion.  */
14918           initializer = make_pack_expansion (initializer);
14919         }
14920
14921       /* Add it to the vector.  */
14922       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14923
14924       /* If the next token is not a comma, we have reached the end of
14925          the list.  */
14926       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14927         break;
14928
14929       /* Peek at the next token.  */
14930       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14931       /* If the next token is a `}', then we're still done.  An
14932          initializer-clause can have a trailing `,' after the
14933          initializer-list and before the closing `}'.  */
14934       if (token->type == CPP_CLOSE_BRACE)
14935         break;
14936
14937       /* Consume the `,' token.  */
14938       cp_lexer_consume_token (parser->lexer);
14939     }
14940
14941   return v;
14942 }
14943
14944 /* Classes [gram.class] */
14945
14946 /* Parse a class-name.
14947
14948    class-name:
14949      identifier
14950      template-id
14951
14952    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14953    to indicate that names looked up in dependent types should be
14954    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14955    keyword has been used to indicate that the name that appears next
14956    is a template.  TAG_TYPE indicates the explicit tag given before
14957    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14958    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14959    is the class being defined in a class-head.
14960
14961    Returns the TYPE_DECL representing the class.  */
14962
14963 static tree
14964 cp_parser_class_name (cp_parser *parser,
14965                       bool typename_keyword_p,
14966                       bool template_keyword_p,
14967                       enum tag_types tag_type,
14968                       bool check_dependency_p,
14969                       bool class_head_p,
14970                       bool is_declaration)
14971 {
14972   tree decl;
14973   tree scope;
14974   bool typename_p;
14975   cp_token *token;
14976   tree identifier = NULL_TREE;
14977
14978   /* All class-names start with an identifier.  */
14979   token = cp_lexer_peek_token (parser->lexer);
14980   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14981     {
14982       cp_parser_error (parser, "expected class-name");
14983       return error_mark_node;
14984     }
14985
14986   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14987      to a template-id, so we save it here.  */
14988   scope = parser->scope;
14989   if (scope == error_mark_node)
14990     return error_mark_node;
14991
14992   /* Any name names a type if we're following the `typename' keyword
14993      in a qualified name where the enclosing scope is type-dependent.  */
14994   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14995                 && dependent_type_p (scope));
14996   /* Handle the common case (an identifier, but not a template-id)
14997      efficiently.  */
14998   if (token->type == CPP_NAME
14999       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15000     {
15001       cp_token *identifier_token;
15002       bool ambiguous_p;
15003
15004       /* Look for the identifier.  */
15005       identifier_token = cp_lexer_peek_token (parser->lexer);
15006       ambiguous_p = identifier_token->ambiguous_p;
15007       identifier = cp_parser_identifier (parser);
15008       /* If the next token isn't an identifier, we are certainly not
15009          looking at a class-name.  */
15010       if (identifier == error_mark_node)
15011         decl = error_mark_node;
15012       /* If we know this is a type-name, there's no need to look it
15013          up.  */
15014       else if (typename_p)
15015         decl = identifier;
15016       else
15017         {
15018           tree ambiguous_decls;
15019           /* If we already know that this lookup is ambiguous, then
15020              we've already issued an error message; there's no reason
15021              to check again.  */
15022           if (ambiguous_p)
15023             {
15024               cp_parser_simulate_error (parser);
15025               return error_mark_node;
15026             }
15027           /* If the next token is a `::', then the name must be a type
15028              name.
15029
15030              [basic.lookup.qual]
15031
15032              During the lookup for a name preceding the :: scope
15033              resolution operator, object, function, and enumerator
15034              names are ignored.  */
15035           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15036             tag_type = typename_type;
15037           /* Look up the name.  */
15038           decl = cp_parser_lookup_name (parser, identifier,
15039                                         tag_type,
15040                                         /*is_template=*/false,
15041                                         /*is_namespace=*/false,
15042                                         check_dependency_p,
15043                                         &ambiguous_decls,
15044                                         identifier_token->location);
15045           if (ambiguous_decls)
15046             {
15047               error_at (identifier_token->location,
15048                         "reference to %qD is ambiguous", identifier);
15049               print_candidates (ambiguous_decls);
15050               if (cp_parser_parsing_tentatively (parser))
15051                 {
15052                   identifier_token->ambiguous_p = true;
15053                   cp_parser_simulate_error (parser);
15054                 }
15055               return error_mark_node;
15056             }
15057         }
15058     }
15059   else
15060     {
15061       /* Try a template-id.  */
15062       decl = cp_parser_template_id (parser, template_keyword_p,
15063                                     check_dependency_p,
15064                                     is_declaration);
15065       if (decl == error_mark_node)
15066         return error_mark_node;
15067     }
15068
15069   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15070
15071   /* If this is a typename, create a TYPENAME_TYPE.  */
15072   if (typename_p && decl != error_mark_node)
15073     {
15074       decl = make_typename_type (scope, decl, typename_type,
15075                                  /*complain=*/tf_error);
15076       if (decl != error_mark_node)
15077         decl = TYPE_NAME (decl);
15078     }
15079
15080   /* Check to see that it is really the name of a class.  */
15081   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15082       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15083       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15084     /* Situations like this:
15085
15086          template <typename T> struct A {
15087            typename T::template X<int>::I i;
15088          };
15089
15090        are problematic.  Is `T::template X<int>' a class-name?  The
15091        standard does not seem to be definitive, but there is no other
15092        valid interpretation of the following `::'.  Therefore, those
15093        names are considered class-names.  */
15094     {
15095       decl = make_typename_type (scope, decl, tag_type, tf_error);
15096       if (decl != error_mark_node)
15097         decl = TYPE_NAME (decl);
15098     }
15099   else if (TREE_CODE (decl) != TYPE_DECL
15100            || TREE_TYPE (decl) == error_mark_node
15101            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15102     decl = error_mark_node;
15103
15104   if (decl == error_mark_node)
15105     cp_parser_error (parser, "expected class-name");
15106   else if (identifier && !parser->scope)
15107     maybe_note_name_used_in_class (identifier, decl);
15108
15109   return decl;
15110 }
15111
15112 /* Parse a class-specifier.
15113
15114    class-specifier:
15115      class-head { member-specification [opt] }
15116
15117    Returns the TREE_TYPE representing the class.  */
15118
15119 static tree
15120 cp_parser_class_specifier (cp_parser* parser)
15121 {
15122   tree type;
15123   tree attributes = NULL_TREE;
15124   bool nested_name_specifier_p;
15125   unsigned saved_num_template_parameter_lists;
15126   bool saved_in_function_body;
15127   bool saved_in_unbraced_linkage_specification_p;
15128   tree old_scope = NULL_TREE;
15129   tree scope = NULL_TREE;
15130   tree bases;
15131
15132   push_deferring_access_checks (dk_no_deferred);
15133
15134   /* Parse the class-head.  */
15135   type = cp_parser_class_head (parser,
15136                                &nested_name_specifier_p,
15137                                &attributes,
15138                                &bases);
15139   /* If the class-head was a semantic disaster, skip the entire body
15140      of the class.  */
15141   if (!type)
15142     {
15143       cp_parser_skip_to_end_of_block_or_statement (parser);
15144       pop_deferring_access_checks ();
15145       return error_mark_node;
15146     }
15147
15148   /* Look for the `{'.  */
15149   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15150     {
15151       pop_deferring_access_checks ();
15152       return error_mark_node;
15153     }
15154
15155   /* Process the base classes. If they're invalid, skip the 
15156      entire class body.  */
15157   if (!xref_basetypes (type, bases))
15158     {
15159       /* Consuming the closing brace yields better error messages
15160          later on.  */
15161       if (cp_parser_skip_to_closing_brace (parser))
15162         cp_lexer_consume_token (parser->lexer);
15163       pop_deferring_access_checks ();
15164       return error_mark_node;
15165     }
15166
15167   /* Issue an error message if type-definitions are forbidden here.  */
15168   cp_parser_check_type_definition (parser);
15169   /* Remember that we are defining one more class.  */
15170   ++parser->num_classes_being_defined;
15171   /* Inside the class, surrounding template-parameter-lists do not
15172      apply.  */
15173   saved_num_template_parameter_lists
15174     = parser->num_template_parameter_lists;
15175   parser->num_template_parameter_lists = 0;
15176   /* We are not in a function body.  */
15177   saved_in_function_body = parser->in_function_body;
15178   parser->in_function_body = false;
15179   /* We are not immediately inside an extern "lang" block.  */
15180   saved_in_unbraced_linkage_specification_p
15181     = parser->in_unbraced_linkage_specification_p;
15182   parser->in_unbraced_linkage_specification_p = false;
15183
15184   /* Start the class.  */
15185   if (nested_name_specifier_p)
15186     {
15187       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15188       old_scope = push_inner_scope (scope);
15189     }
15190   type = begin_class_definition (type, attributes);
15191
15192   if (type == error_mark_node)
15193     /* If the type is erroneous, skip the entire body of the class.  */
15194     cp_parser_skip_to_closing_brace (parser);
15195   else
15196     /* Parse the member-specification.  */
15197     cp_parser_member_specification_opt (parser);
15198
15199   /* Look for the trailing `}'.  */
15200   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15201   /* Look for trailing attributes to apply to this class.  */
15202   if (cp_parser_allow_gnu_extensions_p (parser))
15203     attributes = cp_parser_attributes_opt (parser);
15204   if (type != error_mark_node)
15205     type = finish_struct (type, attributes);
15206   if (nested_name_specifier_p)
15207     pop_inner_scope (old_scope, scope);
15208   /* If this class is not itself within the scope of another class,
15209      then we need to parse the bodies of all of the queued function
15210      definitions.  Note that the queued functions defined in a class
15211      are not always processed immediately following the
15212      class-specifier for that class.  Consider:
15213
15214        struct A {
15215          struct B { void f() { sizeof (A); } };
15216        };
15217
15218      If `f' were processed before the processing of `A' were
15219      completed, there would be no way to compute the size of `A'.
15220      Note that the nesting we are interested in here is lexical --
15221      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15222      for:
15223
15224        struct A { struct B; };
15225        struct A::B { void f() { } };
15226
15227      there is no need to delay the parsing of `A::B::f'.  */
15228   if (--parser->num_classes_being_defined == 0)
15229     {
15230       tree queue_entry;
15231       tree fn;
15232       tree class_type = NULL_TREE;
15233       tree pushed_scope = NULL_TREE;
15234
15235       /* In a first pass, parse default arguments to the functions.
15236          Then, in a second pass, parse the bodies of the functions.
15237          This two-phased approach handles cases like:
15238
15239             struct S {
15240               void f() { g(); }
15241               void g(int i = 3);
15242             };
15243
15244          */
15245       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15246              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15247            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15248            TREE_PURPOSE (parser->unparsed_functions_queues)
15249              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15250         {
15251           fn = TREE_VALUE (queue_entry);
15252           /* If there are default arguments that have not yet been processed,
15253              take care of them now.  */
15254           if (class_type != TREE_PURPOSE (queue_entry))
15255             {
15256               if (pushed_scope)
15257                 pop_scope (pushed_scope);
15258               class_type = TREE_PURPOSE (queue_entry);
15259               pushed_scope = push_scope (class_type);
15260             }
15261           /* Make sure that any template parameters are in scope.  */
15262           maybe_begin_member_template_processing (fn);
15263           /* Parse the default argument expressions.  */
15264           cp_parser_late_parsing_default_args (parser, fn);
15265           /* Remove any template parameters from the symbol table.  */
15266           maybe_end_member_template_processing ();
15267         }
15268       if (pushed_scope)
15269         pop_scope (pushed_scope);
15270       /* Now parse the body of the functions.  */
15271       for (TREE_VALUE (parser->unparsed_functions_queues)
15272              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15273            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15274            TREE_VALUE (parser->unparsed_functions_queues)
15275              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15276         {
15277           /* Figure out which function we need to process.  */
15278           fn = TREE_VALUE (queue_entry);
15279           /* Parse the function.  */
15280           cp_parser_late_parsing_for_member (parser, fn);
15281         }
15282     }
15283
15284   /* Put back any saved access checks.  */
15285   pop_deferring_access_checks ();
15286
15287   /* Restore saved state.  */
15288   parser->in_function_body = saved_in_function_body;
15289   parser->num_template_parameter_lists
15290     = saved_num_template_parameter_lists;
15291   parser->in_unbraced_linkage_specification_p
15292     = saved_in_unbraced_linkage_specification_p;
15293
15294   return type;
15295 }
15296
15297 /* Parse a class-head.
15298
15299    class-head:
15300      class-key identifier [opt] base-clause [opt]
15301      class-key nested-name-specifier identifier base-clause [opt]
15302      class-key nested-name-specifier [opt] template-id
15303        base-clause [opt]
15304
15305    GNU Extensions:
15306      class-key attributes identifier [opt] base-clause [opt]
15307      class-key attributes nested-name-specifier identifier base-clause [opt]
15308      class-key attributes nested-name-specifier [opt] template-id
15309        base-clause [opt]
15310
15311    Upon return BASES is initialized to the list of base classes (or
15312    NULL, if there are none) in the same form returned by
15313    cp_parser_base_clause.
15314
15315    Returns the TYPE of the indicated class.  Sets
15316    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15317    involving a nested-name-specifier was used, and FALSE otherwise.
15318
15319    Returns error_mark_node if this is not a class-head.
15320
15321    Returns NULL_TREE if the class-head is syntactically valid, but
15322    semantically invalid in a way that means we should skip the entire
15323    body of the class.  */
15324
15325 static tree
15326 cp_parser_class_head (cp_parser* parser,
15327                       bool* nested_name_specifier_p,
15328                       tree *attributes_p,
15329                       tree *bases)
15330 {
15331   tree nested_name_specifier;
15332   enum tag_types class_key;
15333   tree id = NULL_TREE;
15334   tree type = NULL_TREE;
15335   tree attributes;
15336   bool template_id_p = false;
15337   bool qualified_p = false;
15338   bool invalid_nested_name_p = false;
15339   bool invalid_explicit_specialization_p = false;
15340   tree pushed_scope = NULL_TREE;
15341   unsigned num_templates;
15342   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15343   /* Assume no nested-name-specifier will be present.  */
15344   *nested_name_specifier_p = false;
15345   /* Assume no template parameter lists will be used in defining the
15346      type.  */
15347   num_templates = 0;
15348
15349   *bases = NULL_TREE;
15350
15351   /* Look for the class-key.  */
15352   class_key = cp_parser_class_key (parser);
15353   if (class_key == none_type)
15354     return error_mark_node;
15355
15356   /* Parse the attributes.  */
15357   attributes = cp_parser_attributes_opt (parser);
15358
15359   /* If the next token is `::', that is invalid -- but sometimes
15360      people do try to write:
15361
15362        struct ::S {};
15363
15364      Handle this gracefully by accepting the extra qualifier, and then
15365      issuing an error about it later if this really is a
15366      class-head.  If it turns out just to be an elaborated type
15367      specifier, remain silent.  */
15368   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15369     qualified_p = true;
15370
15371   push_deferring_access_checks (dk_no_check);
15372
15373   /* Determine the name of the class.  Begin by looking for an
15374      optional nested-name-specifier.  */
15375   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15376   nested_name_specifier
15377     = cp_parser_nested_name_specifier_opt (parser,
15378                                            /*typename_keyword_p=*/false,
15379                                            /*check_dependency_p=*/false,
15380                                            /*type_p=*/false,
15381                                            /*is_declaration=*/false);
15382   /* If there was a nested-name-specifier, then there *must* be an
15383      identifier.  */
15384   if (nested_name_specifier)
15385     {
15386       type_start_token = cp_lexer_peek_token (parser->lexer);
15387       /* Although the grammar says `identifier', it really means
15388          `class-name' or `template-name'.  You are only allowed to
15389          define a class that has already been declared with this
15390          syntax.
15391
15392          The proposed resolution for Core Issue 180 says that wherever
15393          you see `class T::X' you should treat `X' as a type-name.
15394
15395          It is OK to define an inaccessible class; for example:
15396
15397            class A { class B; };
15398            class A::B {};
15399
15400          We do not know if we will see a class-name, or a
15401          template-name.  We look for a class-name first, in case the
15402          class-name is a template-id; if we looked for the
15403          template-name first we would stop after the template-name.  */
15404       cp_parser_parse_tentatively (parser);
15405       type = cp_parser_class_name (parser,
15406                                    /*typename_keyword_p=*/false,
15407                                    /*template_keyword_p=*/false,
15408                                    class_type,
15409                                    /*check_dependency_p=*/false,
15410                                    /*class_head_p=*/true,
15411                                    /*is_declaration=*/false);
15412       /* If that didn't work, ignore the nested-name-specifier.  */
15413       if (!cp_parser_parse_definitely (parser))
15414         {
15415           invalid_nested_name_p = true;
15416           type_start_token = cp_lexer_peek_token (parser->lexer);
15417           id = cp_parser_identifier (parser);
15418           if (id == error_mark_node)
15419             id = NULL_TREE;
15420         }
15421       /* If we could not find a corresponding TYPE, treat this
15422          declaration like an unqualified declaration.  */
15423       if (type == error_mark_node)
15424         nested_name_specifier = NULL_TREE;
15425       /* Otherwise, count the number of templates used in TYPE and its
15426          containing scopes.  */
15427       else
15428         {
15429           tree scope;
15430
15431           for (scope = TREE_TYPE (type);
15432                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15433                scope = (TYPE_P (scope)
15434                         ? TYPE_CONTEXT (scope)
15435                         : DECL_CONTEXT (scope)))
15436             if (TYPE_P (scope)
15437                 && CLASS_TYPE_P (scope)
15438                 && CLASSTYPE_TEMPLATE_INFO (scope)
15439                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15440                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15441               ++num_templates;
15442         }
15443     }
15444   /* Otherwise, the identifier is optional.  */
15445   else
15446     {
15447       /* We don't know whether what comes next is a template-id,
15448          an identifier, or nothing at all.  */
15449       cp_parser_parse_tentatively (parser);
15450       /* Check for a template-id.  */
15451       type_start_token = cp_lexer_peek_token (parser->lexer);
15452       id = cp_parser_template_id (parser,
15453                                   /*template_keyword_p=*/false,
15454                                   /*check_dependency_p=*/true,
15455                                   /*is_declaration=*/true);
15456       /* If that didn't work, it could still be an identifier.  */
15457       if (!cp_parser_parse_definitely (parser))
15458         {
15459           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15460             {
15461               type_start_token = cp_lexer_peek_token (parser->lexer);
15462               id = cp_parser_identifier (parser);
15463             }
15464           else
15465             id = NULL_TREE;
15466         }
15467       else
15468         {
15469           template_id_p = true;
15470           ++num_templates;
15471         }
15472     }
15473
15474   pop_deferring_access_checks ();
15475
15476   if (id)
15477     cp_parser_check_for_invalid_template_id (parser, id,
15478                                              type_start_token->location);
15479
15480   /* If it's not a `:' or a `{' then we can't really be looking at a
15481      class-head, since a class-head only appears as part of a
15482      class-specifier.  We have to detect this situation before calling
15483      xref_tag, since that has irreversible side-effects.  */
15484   if (!cp_parser_next_token_starts_class_definition_p (parser))
15485     {
15486       cp_parser_error (parser, "expected %<{%> or %<:%>");
15487       return error_mark_node;
15488     }
15489
15490   /* At this point, we're going ahead with the class-specifier, even
15491      if some other problem occurs.  */
15492   cp_parser_commit_to_tentative_parse (parser);
15493   /* Issue the error about the overly-qualified name now.  */
15494   if (qualified_p)
15495     {
15496       cp_parser_error (parser,
15497                        "global qualification of class name is invalid");
15498       return error_mark_node;
15499     }
15500   else if (invalid_nested_name_p)
15501     {
15502       cp_parser_error (parser,
15503                        "qualified name does not name a class");
15504       return error_mark_node;
15505     }
15506   else if (nested_name_specifier)
15507     {
15508       tree scope;
15509
15510       /* Reject typedef-names in class heads.  */
15511       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15512         {
15513           error_at (type_start_token->location,
15514                     "invalid class name in declaration of %qD",
15515                     type);
15516           type = NULL_TREE;
15517           goto done;
15518         }
15519
15520       /* Figure out in what scope the declaration is being placed.  */
15521       scope = current_scope ();
15522       /* If that scope does not contain the scope in which the
15523          class was originally declared, the program is invalid.  */
15524       if (scope && !is_ancestor (scope, nested_name_specifier))
15525         {
15526           if (at_namespace_scope_p ())
15527             error_at (type_start_token->location,
15528                       "declaration of %qD in namespace %qD which does not "
15529                       "enclose %qD",
15530                       type, scope, nested_name_specifier);
15531           else
15532             error_at (type_start_token->location,
15533                       "declaration of %qD in %qD which does not enclose %qD",
15534                       type, scope, nested_name_specifier);
15535           type = NULL_TREE;
15536           goto done;
15537         }
15538       /* [dcl.meaning]
15539
15540          A declarator-id shall not be qualified except for the
15541          definition of a ... nested class outside of its class
15542          ... [or] the definition or explicit instantiation of a
15543          class member of a namespace outside of its namespace.  */
15544       if (scope == nested_name_specifier)
15545         {
15546           permerror (nested_name_specifier_token_start->location,
15547                      "extra qualification not allowed");
15548           nested_name_specifier = NULL_TREE;
15549           num_templates = 0;
15550         }
15551     }
15552   /* An explicit-specialization must be preceded by "template <>".  If
15553      it is not, try to recover gracefully.  */
15554   if (at_namespace_scope_p ()
15555       && parser->num_template_parameter_lists == 0
15556       && template_id_p)
15557     {
15558       error_at (type_start_token->location,
15559                 "an explicit specialization must be preceded by %<template <>%>");
15560       invalid_explicit_specialization_p = true;
15561       /* Take the same action that would have been taken by
15562          cp_parser_explicit_specialization.  */
15563       ++parser->num_template_parameter_lists;
15564       begin_specialization ();
15565     }
15566   /* There must be no "return" statements between this point and the
15567      end of this function; set "type "to the correct return value and
15568      use "goto done;" to return.  */
15569   /* Make sure that the right number of template parameters were
15570      present.  */
15571   if (!cp_parser_check_template_parameters (parser, num_templates,
15572                                             type_start_token->location,
15573                                             /*declarator=*/NULL))
15574     {
15575       /* If something went wrong, there is no point in even trying to
15576          process the class-definition.  */
15577       type = NULL_TREE;
15578       goto done;
15579     }
15580
15581   /* Look up the type.  */
15582   if (template_id_p)
15583     {
15584       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15585           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15586               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15587         {
15588           error_at (type_start_token->location,
15589                     "function template %qD redeclared as a class template", id);
15590           type = error_mark_node;
15591         }
15592       else
15593         {
15594           type = TREE_TYPE (id);
15595           type = maybe_process_partial_specialization (type);
15596         }
15597       if (nested_name_specifier)
15598         pushed_scope = push_scope (nested_name_specifier);
15599     }
15600   else if (nested_name_specifier)
15601     {
15602       tree class_type;
15603
15604       /* Given:
15605
15606             template <typename T> struct S { struct T };
15607             template <typename T> struct S<T>::T { };
15608
15609          we will get a TYPENAME_TYPE when processing the definition of
15610          `S::T'.  We need to resolve it to the actual type before we
15611          try to define it.  */
15612       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15613         {
15614           class_type = resolve_typename_type (TREE_TYPE (type),
15615                                               /*only_current_p=*/false);
15616           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15617             type = TYPE_NAME (class_type);
15618           else
15619             {
15620               cp_parser_error (parser, "could not resolve typename type");
15621               type = error_mark_node;
15622             }
15623         }
15624
15625       if (maybe_process_partial_specialization (TREE_TYPE (type))
15626           == error_mark_node)
15627         {
15628           type = NULL_TREE;
15629           goto done;
15630         }
15631
15632       class_type = current_class_type;
15633       /* Enter the scope indicated by the nested-name-specifier.  */
15634       pushed_scope = push_scope (nested_name_specifier);
15635       /* Get the canonical version of this type.  */
15636       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15637       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15638           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15639         {
15640           type = push_template_decl (type);
15641           if (type == error_mark_node)
15642             {
15643               type = NULL_TREE;
15644               goto done;
15645             }
15646         }
15647
15648       type = TREE_TYPE (type);
15649       *nested_name_specifier_p = true;
15650     }
15651   else      /* The name is not a nested name.  */
15652     {
15653       /* If the class was unnamed, create a dummy name.  */
15654       if (!id)
15655         id = make_anon_name ();
15656       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15657                        parser->num_template_parameter_lists);
15658     }
15659
15660   /* Indicate whether this class was declared as a `class' or as a
15661      `struct'.  */
15662   if (TREE_CODE (type) == RECORD_TYPE)
15663     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15664   cp_parser_check_class_key (class_key, type);
15665
15666   /* If this type was already complete, and we see another definition,
15667      that's an error.  */
15668   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15669     {
15670       error_at (type_start_token->location, "redefinition of %q#T",
15671                 type);
15672       error_at (type_start_token->location, "previous definition of %q+#T",
15673                 type);
15674       type = NULL_TREE;
15675       goto done;
15676     }
15677   else if (type == error_mark_node)
15678     type = NULL_TREE;
15679
15680   /* We will have entered the scope containing the class; the names of
15681      base classes should be looked up in that context.  For example:
15682
15683        struct A { struct B {}; struct C; };
15684        struct A::C : B {};
15685
15686      is valid.  */
15687
15688   /* Get the list of base-classes, if there is one.  */
15689   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15690     *bases = cp_parser_base_clause (parser);
15691
15692  done:
15693   /* Leave the scope given by the nested-name-specifier.  We will
15694      enter the class scope itself while processing the members.  */
15695   if (pushed_scope)
15696     pop_scope (pushed_scope);
15697
15698   if (invalid_explicit_specialization_p)
15699     {
15700       end_specialization ();
15701       --parser->num_template_parameter_lists;
15702     }
15703   *attributes_p = attributes;
15704   return type;
15705 }
15706
15707 /* Parse a class-key.
15708
15709    class-key:
15710      class
15711      struct
15712      union
15713
15714    Returns the kind of class-key specified, or none_type to indicate
15715    error.  */
15716
15717 static enum tag_types
15718 cp_parser_class_key (cp_parser* parser)
15719 {
15720   cp_token *token;
15721   enum tag_types tag_type;
15722
15723   /* Look for the class-key.  */
15724   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15725   if (!token)
15726     return none_type;
15727
15728   /* Check to see if the TOKEN is a class-key.  */
15729   tag_type = cp_parser_token_is_class_key (token);
15730   if (!tag_type)
15731     cp_parser_error (parser, "expected class-key");
15732   return tag_type;
15733 }
15734
15735 /* Parse an (optional) member-specification.
15736
15737    member-specification:
15738      member-declaration member-specification [opt]
15739      access-specifier : member-specification [opt]  */
15740
15741 static void
15742 cp_parser_member_specification_opt (cp_parser* parser)
15743 {
15744   while (true)
15745     {
15746       cp_token *token;
15747       enum rid keyword;
15748
15749       /* Peek at the next token.  */
15750       token = cp_lexer_peek_token (parser->lexer);
15751       /* If it's a `}', or EOF then we've seen all the members.  */
15752       if (token->type == CPP_CLOSE_BRACE
15753           || token->type == CPP_EOF
15754           || token->type == CPP_PRAGMA_EOL)
15755         break;
15756
15757       /* See if this token is a keyword.  */
15758       keyword = token->keyword;
15759       switch (keyword)
15760         {
15761         case RID_PUBLIC:
15762         case RID_PROTECTED:
15763         case RID_PRIVATE:
15764           /* Consume the access-specifier.  */
15765           cp_lexer_consume_token (parser->lexer);
15766           /* Remember which access-specifier is active.  */
15767           current_access_specifier = token->u.value;
15768           /* Look for the `:'.  */
15769           cp_parser_require (parser, CPP_COLON, "%<:%>");
15770           break;
15771
15772         default:
15773           /* Accept #pragmas at class scope.  */
15774           if (token->type == CPP_PRAGMA)
15775             {
15776               cp_parser_pragma (parser, pragma_external);
15777               break;
15778             }
15779
15780           /* Otherwise, the next construction must be a
15781              member-declaration.  */
15782           cp_parser_member_declaration (parser);
15783         }
15784     }
15785 }
15786
15787 /* Parse a member-declaration.
15788
15789    member-declaration:
15790      decl-specifier-seq [opt] member-declarator-list [opt] ;
15791      function-definition ; [opt]
15792      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15793      using-declaration
15794      template-declaration
15795
15796    member-declarator-list:
15797      member-declarator
15798      member-declarator-list , member-declarator
15799
15800    member-declarator:
15801      declarator pure-specifier [opt]
15802      declarator constant-initializer [opt]
15803      identifier [opt] : constant-expression
15804
15805    GNU Extensions:
15806
15807    member-declaration:
15808      __extension__ member-declaration
15809
15810    member-declarator:
15811      declarator attributes [opt] pure-specifier [opt]
15812      declarator attributes [opt] constant-initializer [opt]
15813      identifier [opt] attributes [opt] : constant-expression  
15814
15815    C++0x Extensions:
15816
15817    member-declaration:
15818      static_assert-declaration  */
15819
15820 static void
15821 cp_parser_member_declaration (cp_parser* parser)
15822 {
15823   cp_decl_specifier_seq decl_specifiers;
15824   tree prefix_attributes;
15825   tree decl;
15826   int declares_class_or_enum;
15827   bool friend_p;
15828   cp_token *token = NULL;
15829   cp_token *decl_spec_token_start = NULL;
15830   cp_token *initializer_token_start = NULL;
15831   int saved_pedantic;
15832
15833   /* Check for the `__extension__' keyword.  */
15834   if (cp_parser_extension_opt (parser, &saved_pedantic))
15835     {
15836       /* Recurse.  */
15837       cp_parser_member_declaration (parser);
15838       /* Restore the old value of the PEDANTIC flag.  */
15839       pedantic = saved_pedantic;
15840
15841       return;
15842     }
15843
15844   /* Check for a template-declaration.  */
15845   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15846     {
15847       /* An explicit specialization here is an error condition, and we
15848          expect the specialization handler to detect and report this.  */
15849       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15850           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15851         cp_parser_explicit_specialization (parser);
15852       else
15853         cp_parser_template_declaration (parser, /*member_p=*/true);
15854
15855       return;
15856     }
15857
15858   /* Check for a using-declaration.  */
15859   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15860     {
15861       /* Parse the using-declaration.  */
15862       cp_parser_using_declaration (parser,
15863                                    /*access_declaration_p=*/false);
15864       return;
15865     }
15866
15867   /* Check for @defs.  */
15868   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15869     {
15870       tree ivar, member;
15871       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15872       ivar = ivar_chains;
15873       while (ivar)
15874         {
15875           member = ivar;
15876           ivar = TREE_CHAIN (member);
15877           TREE_CHAIN (member) = NULL_TREE;
15878           finish_member_declaration (member);
15879         }
15880       return;
15881     }
15882
15883   /* If the next token is `static_assert' we have a static assertion.  */
15884   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15885     {
15886       cp_parser_static_assert (parser, /*member_p=*/true);
15887       return;
15888     }
15889
15890   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15891     return;
15892
15893   /* Parse the decl-specifier-seq.  */
15894   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15895   cp_parser_decl_specifier_seq (parser,
15896                                 CP_PARSER_FLAGS_OPTIONAL,
15897                                 &decl_specifiers,
15898                                 &declares_class_or_enum);
15899   prefix_attributes = decl_specifiers.attributes;
15900   decl_specifiers.attributes = NULL_TREE;
15901   /* Check for an invalid type-name.  */
15902   if (!decl_specifiers.type
15903       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15904     return;
15905   /* If there is no declarator, then the decl-specifier-seq should
15906      specify a type.  */
15907   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15908     {
15909       /* If there was no decl-specifier-seq, and the next token is a
15910          `;', then we have something like:
15911
15912            struct S { ; };
15913
15914          [class.mem]
15915
15916          Each member-declaration shall declare at least one member
15917          name of the class.  */
15918       if (!decl_specifiers.any_specifiers_p)
15919         {
15920           cp_token *token = cp_lexer_peek_token (parser->lexer);
15921           if (!in_system_header_at (token->location))
15922             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15923         }
15924       else
15925         {
15926           tree type;
15927
15928           /* See if this declaration is a friend.  */
15929           friend_p = cp_parser_friend_p (&decl_specifiers);
15930           /* If there were decl-specifiers, check to see if there was
15931              a class-declaration.  */
15932           type = check_tag_decl (&decl_specifiers);
15933           /* Nested classes have already been added to the class, but
15934              a `friend' needs to be explicitly registered.  */
15935           if (friend_p)
15936             {
15937               /* If the `friend' keyword was present, the friend must
15938                  be introduced with a class-key.  */
15939                if (!declares_class_or_enum)
15940                  error_at (decl_spec_token_start->location,
15941                            "a class-key must be used when declaring a friend");
15942                /* In this case:
15943
15944                     template <typename T> struct A {
15945                       friend struct A<T>::B;
15946                     };
15947
15948                   A<T>::B will be represented by a TYPENAME_TYPE, and
15949                   therefore not recognized by check_tag_decl.  */
15950                if (!type
15951                    && decl_specifiers.type
15952                    && TYPE_P (decl_specifiers.type))
15953                  type = decl_specifiers.type;
15954                if (!type || !TYPE_P (type))
15955                  error_at (decl_spec_token_start->location,
15956                            "friend declaration does not name a class or "
15957                            "function");
15958                else
15959                  make_friend_class (current_class_type, type,
15960                                     /*complain=*/true);
15961             }
15962           /* If there is no TYPE, an error message will already have
15963              been issued.  */
15964           else if (!type || type == error_mark_node)
15965             ;
15966           /* An anonymous aggregate has to be handled specially; such
15967              a declaration really declares a data member (with a
15968              particular type), as opposed to a nested class.  */
15969           else if (ANON_AGGR_TYPE_P (type))
15970             {
15971               /* Remove constructors and such from TYPE, now that we
15972                  know it is an anonymous aggregate.  */
15973               fixup_anonymous_aggr (type);
15974               /* And make the corresponding data member.  */
15975               decl = build_decl (decl_spec_token_start->location,
15976                                  FIELD_DECL, NULL_TREE, type);
15977               /* Add it to the class.  */
15978               finish_member_declaration (decl);
15979             }
15980           else
15981             cp_parser_check_access_in_redeclaration
15982                                               (TYPE_NAME (type),
15983                                                decl_spec_token_start->location);
15984         }
15985     }
15986   else
15987     {
15988       /* See if these declarations will be friends.  */
15989       friend_p = cp_parser_friend_p (&decl_specifiers);
15990
15991       /* Keep going until we hit the `;' at the end of the
15992          declaration.  */
15993       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15994         {
15995           tree attributes = NULL_TREE;
15996           tree first_attribute;
15997
15998           /* Peek at the next token.  */
15999           token = cp_lexer_peek_token (parser->lexer);
16000
16001           /* Check for a bitfield declaration.  */
16002           if (token->type == CPP_COLON
16003               || (token->type == CPP_NAME
16004                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16005                   == CPP_COLON))
16006             {
16007               tree identifier;
16008               tree width;
16009
16010               /* Get the name of the bitfield.  Note that we cannot just
16011                  check TOKEN here because it may have been invalidated by
16012                  the call to cp_lexer_peek_nth_token above.  */
16013               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16014                 identifier = cp_parser_identifier (parser);
16015               else
16016                 identifier = NULL_TREE;
16017
16018               /* Consume the `:' token.  */
16019               cp_lexer_consume_token (parser->lexer);
16020               /* Get the width of the bitfield.  */
16021               width
16022                 = cp_parser_constant_expression (parser,
16023                                                  /*allow_non_constant=*/false,
16024                                                  NULL);
16025
16026               /* Look for attributes that apply to the bitfield.  */
16027               attributes = cp_parser_attributes_opt (parser);
16028               /* Remember which attributes are prefix attributes and
16029                  which are not.  */
16030               first_attribute = attributes;
16031               /* Combine the attributes.  */
16032               attributes = chainon (prefix_attributes, attributes);
16033
16034               /* Create the bitfield declaration.  */
16035               decl = grokbitfield (identifier
16036                                    ? make_id_declarator (NULL_TREE,
16037                                                          identifier,
16038                                                          sfk_none)
16039                                    : NULL,
16040                                    &decl_specifiers,
16041                                    width,
16042                                    attributes);
16043             }
16044           else
16045             {
16046               cp_declarator *declarator;
16047               tree initializer;
16048               tree asm_specification;
16049               int ctor_dtor_or_conv_p;
16050
16051               /* Parse the declarator.  */
16052               declarator
16053                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16054                                         &ctor_dtor_or_conv_p,
16055                                         /*parenthesized_p=*/NULL,
16056                                         /*member_p=*/true);
16057
16058               /* If something went wrong parsing the declarator, make sure
16059                  that we at least consume some tokens.  */
16060               if (declarator == cp_error_declarator)
16061                 {
16062                   /* Skip to the end of the statement.  */
16063                   cp_parser_skip_to_end_of_statement (parser);
16064                   /* If the next token is not a semicolon, that is
16065                      probably because we just skipped over the body of
16066                      a function.  So, we consume a semicolon if
16067                      present, but do not issue an error message if it
16068                      is not present.  */
16069                   if (cp_lexer_next_token_is (parser->lexer,
16070                                               CPP_SEMICOLON))
16071                     cp_lexer_consume_token (parser->lexer);
16072                   return;
16073                 }
16074
16075               if (declares_class_or_enum & 2)
16076                 cp_parser_check_for_definition_in_return_type
16077                                             (declarator, decl_specifiers.type,
16078                                              decl_specifiers.type_location);
16079
16080               /* Look for an asm-specification.  */
16081               asm_specification = cp_parser_asm_specification_opt (parser);
16082               /* Look for attributes that apply to the declaration.  */
16083               attributes = cp_parser_attributes_opt (parser);
16084               /* Remember which attributes are prefix attributes and
16085                  which are not.  */
16086               first_attribute = attributes;
16087               /* Combine the attributes.  */
16088               attributes = chainon (prefix_attributes, attributes);
16089
16090               /* If it's an `=', then we have a constant-initializer or a
16091                  pure-specifier.  It is not correct to parse the
16092                  initializer before registering the member declaration
16093                  since the member declaration should be in scope while
16094                  its initializer is processed.  However, the rest of the
16095                  front end does not yet provide an interface that allows
16096                  us to handle this correctly.  */
16097               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16098                 {
16099                   /* In [class.mem]:
16100
16101                      A pure-specifier shall be used only in the declaration of
16102                      a virtual function.
16103
16104                      A member-declarator can contain a constant-initializer
16105                      only if it declares a static member of integral or
16106                      enumeration type.
16107
16108                      Therefore, if the DECLARATOR is for a function, we look
16109                      for a pure-specifier; otherwise, we look for a
16110                      constant-initializer.  When we call `grokfield', it will
16111                      perform more stringent semantics checks.  */
16112                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16113                   if (function_declarator_p (declarator))
16114                     initializer = cp_parser_pure_specifier (parser);
16115                   else
16116                     /* Parse the initializer.  */
16117                     initializer = cp_parser_constant_initializer (parser);
16118                 }
16119               /* Otherwise, there is no initializer.  */
16120               else
16121                 initializer = NULL_TREE;
16122
16123               /* See if we are probably looking at a function
16124                  definition.  We are certainly not looking at a
16125                  member-declarator.  Calling `grokfield' has
16126                  side-effects, so we must not do it unless we are sure
16127                  that we are looking at a member-declarator.  */
16128               if (cp_parser_token_starts_function_definition_p
16129                   (cp_lexer_peek_token (parser->lexer)))
16130                 {
16131                   /* The grammar does not allow a pure-specifier to be
16132                      used when a member function is defined.  (It is
16133                      possible that this fact is an oversight in the
16134                      standard, since a pure function may be defined
16135                      outside of the class-specifier.  */
16136                   if (initializer)
16137                     error_at (initializer_token_start->location,
16138                               "pure-specifier on function-definition");
16139                   decl = cp_parser_save_member_function_body (parser,
16140                                                               &decl_specifiers,
16141                                                               declarator,
16142                                                               attributes);
16143                   /* If the member was not a friend, declare it here.  */
16144                   if (!friend_p)
16145                     finish_member_declaration (decl);
16146                   /* Peek at the next token.  */
16147                   token = cp_lexer_peek_token (parser->lexer);
16148                   /* If the next token is a semicolon, consume it.  */
16149                   if (token->type == CPP_SEMICOLON)
16150                     cp_lexer_consume_token (parser->lexer);
16151                   return;
16152                 }
16153               else
16154                 if (declarator->kind == cdk_function)
16155                   declarator->id_loc = token->location;
16156                 /* Create the declaration.  */
16157                 decl = grokfield (declarator, &decl_specifiers,
16158                                   initializer, /*init_const_expr_p=*/true,
16159                                   asm_specification,
16160                                   attributes);
16161             }
16162
16163           /* Reset PREFIX_ATTRIBUTES.  */
16164           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16165             attributes = TREE_CHAIN (attributes);
16166           if (attributes)
16167             TREE_CHAIN (attributes) = NULL_TREE;
16168
16169           /* If there is any qualification still in effect, clear it
16170              now; we will be starting fresh with the next declarator.  */
16171           parser->scope = NULL_TREE;
16172           parser->qualifying_scope = NULL_TREE;
16173           parser->object_scope = NULL_TREE;
16174           /* If it's a `,', then there are more declarators.  */
16175           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16176             cp_lexer_consume_token (parser->lexer);
16177           /* If the next token isn't a `;', then we have a parse error.  */
16178           else if (cp_lexer_next_token_is_not (parser->lexer,
16179                                                CPP_SEMICOLON))
16180             {
16181               cp_parser_error (parser, "expected %<;%>");
16182               /* Skip tokens until we find a `;'.  */
16183               cp_parser_skip_to_end_of_statement (parser);
16184
16185               break;
16186             }
16187
16188           if (decl)
16189             {
16190               /* Add DECL to the list of members.  */
16191               if (!friend_p)
16192                 finish_member_declaration (decl);
16193
16194               if (TREE_CODE (decl) == FUNCTION_DECL)
16195                 cp_parser_save_default_args (parser, decl);
16196             }
16197         }
16198     }
16199
16200   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16201 }
16202
16203 /* Parse a pure-specifier.
16204
16205    pure-specifier:
16206      = 0
16207
16208    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16209    Otherwise, ERROR_MARK_NODE is returned.  */
16210
16211 static tree
16212 cp_parser_pure_specifier (cp_parser* parser)
16213 {
16214   cp_token *token;
16215
16216   /* Look for the `=' token.  */
16217   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16218     return error_mark_node;
16219   /* Look for the `0' token.  */
16220   token = cp_lexer_peek_token (parser->lexer);
16221
16222   if (token->type == CPP_EOF
16223       || token->type == CPP_PRAGMA_EOL)
16224     return error_mark_node;
16225
16226   cp_lexer_consume_token (parser->lexer);
16227
16228   /* Accept = default or = delete in c++0x mode.  */
16229   if (token->keyword == RID_DEFAULT
16230       || token->keyword == RID_DELETE)
16231     {
16232       maybe_warn_cpp0x ("defaulted and deleted functions");
16233       return token->u.value;
16234     }
16235
16236   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16237   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16238     {
16239       cp_parser_error (parser,
16240                        "invalid pure specifier (only %<= 0%> is allowed)");
16241       cp_parser_skip_to_end_of_statement (parser);
16242       return error_mark_node;
16243     }
16244   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16245     {
16246       error_at (token->location, "templates may not be %<virtual%>");
16247       return error_mark_node;
16248     }
16249
16250   return integer_zero_node;
16251 }
16252
16253 /* Parse a constant-initializer.
16254
16255    constant-initializer:
16256      = constant-expression
16257
16258    Returns a representation of the constant-expression.  */
16259
16260 static tree
16261 cp_parser_constant_initializer (cp_parser* parser)
16262 {
16263   /* Look for the `=' token.  */
16264   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16265     return error_mark_node;
16266
16267   /* It is invalid to write:
16268
16269        struct S { static const int i = { 7 }; };
16270
16271      */
16272   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16273     {
16274       cp_parser_error (parser,
16275                        "a brace-enclosed initializer is not allowed here");
16276       /* Consume the opening brace.  */
16277       cp_lexer_consume_token (parser->lexer);
16278       /* Skip the initializer.  */
16279       cp_parser_skip_to_closing_brace (parser);
16280       /* Look for the trailing `}'.  */
16281       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16282
16283       return error_mark_node;
16284     }
16285
16286   return cp_parser_constant_expression (parser,
16287                                         /*allow_non_constant=*/false,
16288                                         NULL);
16289 }
16290
16291 /* Derived classes [gram.class.derived] */
16292
16293 /* Parse a base-clause.
16294
16295    base-clause:
16296      : base-specifier-list
16297
16298    base-specifier-list:
16299      base-specifier ... [opt]
16300      base-specifier-list , base-specifier ... [opt]
16301
16302    Returns a TREE_LIST representing the base-classes, in the order in
16303    which they were declared.  The representation of each node is as
16304    described by cp_parser_base_specifier.
16305
16306    In the case that no bases are specified, this function will return
16307    NULL_TREE, not ERROR_MARK_NODE.  */
16308
16309 static tree
16310 cp_parser_base_clause (cp_parser* parser)
16311 {
16312   tree bases = NULL_TREE;
16313
16314   /* Look for the `:' that begins the list.  */
16315   cp_parser_require (parser, CPP_COLON, "%<:%>");
16316
16317   /* Scan the base-specifier-list.  */
16318   while (true)
16319     {
16320       cp_token *token;
16321       tree base;
16322       bool pack_expansion_p = false;
16323
16324       /* Look for the base-specifier.  */
16325       base = cp_parser_base_specifier (parser);
16326       /* Look for the (optional) ellipsis. */
16327       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16328         {
16329           /* Consume the `...'. */
16330           cp_lexer_consume_token (parser->lexer);
16331
16332           pack_expansion_p = true;
16333         }
16334
16335       /* Add BASE to the front of the list.  */
16336       if (base != error_mark_node)
16337         {
16338           if (pack_expansion_p)
16339             /* Make this a pack expansion type. */
16340             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16341           
16342
16343           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16344             {
16345               TREE_CHAIN (base) = bases;
16346               bases = base;
16347             }
16348         }
16349       /* Peek at the next token.  */
16350       token = cp_lexer_peek_token (parser->lexer);
16351       /* If it's not a comma, then the list is complete.  */
16352       if (token->type != CPP_COMMA)
16353         break;
16354       /* Consume the `,'.  */
16355       cp_lexer_consume_token (parser->lexer);
16356     }
16357
16358   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16359      base class had a qualified name.  However, the next name that
16360      appears is certainly not qualified.  */
16361   parser->scope = NULL_TREE;
16362   parser->qualifying_scope = NULL_TREE;
16363   parser->object_scope = NULL_TREE;
16364
16365   return nreverse (bases);
16366 }
16367
16368 /* Parse a base-specifier.
16369
16370    base-specifier:
16371      :: [opt] nested-name-specifier [opt] class-name
16372      virtual access-specifier [opt] :: [opt] nested-name-specifier
16373        [opt] class-name
16374      access-specifier virtual [opt] :: [opt] nested-name-specifier
16375        [opt] class-name
16376
16377    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16378    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16379    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16380    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16381
16382 static tree
16383 cp_parser_base_specifier (cp_parser* parser)
16384 {
16385   cp_token *token;
16386   bool done = false;
16387   bool virtual_p = false;
16388   bool duplicate_virtual_error_issued_p = false;
16389   bool duplicate_access_error_issued_p = false;
16390   bool class_scope_p, template_p;
16391   tree access = access_default_node;
16392   tree type;
16393
16394   /* Process the optional `virtual' and `access-specifier'.  */
16395   while (!done)
16396     {
16397       /* Peek at the next token.  */
16398       token = cp_lexer_peek_token (parser->lexer);
16399       /* Process `virtual'.  */
16400       switch (token->keyword)
16401         {
16402         case RID_VIRTUAL:
16403           /* If `virtual' appears more than once, issue an error.  */
16404           if (virtual_p && !duplicate_virtual_error_issued_p)
16405             {
16406               cp_parser_error (parser,
16407                                "%<virtual%> specified more than once in base-specified");
16408               duplicate_virtual_error_issued_p = true;
16409             }
16410
16411           virtual_p = true;
16412
16413           /* Consume the `virtual' token.  */
16414           cp_lexer_consume_token (parser->lexer);
16415
16416           break;
16417
16418         case RID_PUBLIC:
16419         case RID_PROTECTED:
16420         case RID_PRIVATE:
16421           /* If more than one access specifier appears, issue an
16422              error.  */
16423           if (access != access_default_node
16424               && !duplicate_access_error_issued_p)
16425             {
16426               cp_parser_error (parser,
16427                                "more than one access specifier in base-specified");
16428               duplicate_access_error_issued_p = true;
16429             }
16430
16431           access = ridpointers[(int) token->keyword];
16432
16433           /* Consume the access-specifier.  */
16434           cp_lexer_consume_token (parser->lexer);
16435
16436           break;
16437
16438         default:
16439           done = true;
16440           break;
16441         }
16442     }
16443   /* It is not uncommon to see programs mechanically, erroneously, use
16444      the 'typename' keyword to denote (dependent) qualified types
16445      as base classes.  */
16446   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16447     {
16448       token = cp_lexer_peek_token (parser->lexer);
16449       if (!processing_template_decl)
16450         error_at (token->location,
16451                   "keyword %<typename%> not allowed outside of templates");
16452       else
16453         error_at (token->location,
16454                   "keyword %<typename%> not allowed in this context "
16455                   "(the base class is implicitly a type)");
16456       cp_lexer_consume_token (parser->lexer);
16457     }
16458
16459   /* Look for the optional `::' operator.  */
16460   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16461   /* Look for the nested-name-specifier.  The simplest way to
16462      implement:
16463
16464        [temp.res]
16465
16466        The keyword `typename' is not permitted in a base-specifier or
16467        mem-initializer; in these contexts a qualified name that
16468        depends on a template-parameter is implicitly assumed to be a
16469        type name.
16470
16471      is to pretend that we have seen the `typename' keyword at this
16472      point.  */
16473   cp_parser_nested_name_specifier_opt (parser,
16474                                        /*typename_keyword_p=*/true,
16475                                        /*check_dependency_p=*/true,
16476                                        typename_type,
16477                                        /*is_declaration=*/true);
16478   /* If the base class is given by a qualified name, assume that names
16479      we see are type names or templates, as appropriate.  */
16480   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16481   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16482
16483   /* Finally, look for the class-name.  */
16484   type = cp_parser_class_name (parser,
16485                                class_scope_p,
16486                                template_p,
16487                                typename_type,
16488                                /*check_dependency_p=*/true,
16489                                /*class_head_p=*/false,
16490                                /*is_declaration=*/true);
16491
16492   if (type == error_mark_node)
16493     return error_mark_node;
16494
16495   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16496 }
16497
16498 /* Exception handling [gram.exception] */
16499
16500 /* Parse an (optional) exception-specification.
16501
16502    exception-specification:
16503      throw ( type-id-list [opt] )
16504
16505    Returns a TREE_LIST representing the exception-specification.  The
16506    TREE_VALUE of each node is a type.  */
16507
16508 static tree
16509 cp_parser_exception_specification_opt (cp_parser* parser)
16510 {
16511   cp_token *token;
16512   tree type_id_list;
16513
16514   /* Peek at the next token.  */
16515   token = cp_lexer_peek_token (parser->lexer);
16516   /* If it's not `throw', then there's no exception-specification.  */
16517   if (!cp_parser_is_keyword (token, RID_THROW))
16518     return NULL_TREE;
16519
16520   /* Consume the `throw'.  */
16521   cp_lexer_consume_token (parser->lexer);
16522
16523   /* Look for the `('.  */
16524   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16525
16526   /* Peek at the next token.  */
16527   token = cp_lexer_peek_token (parser->lexer);
16528   /* If it's not a `)', then there is a type-id-list.  */
16529   if (token->type != CPP_CLOSE_PAREN)
16530     {
16531       const char *saved_message;
16532
16533       /* Types may not be defined in an exception-specification.  */
16534       saved_message = parser->type_definition_forbidden_message;
16535       parser->type_definition_forbidden_message
16536         = "types may not be defined in an exception-specification";
16537       /* Parse the type-id-list.  */
16538       type_id_list = cp_parser_type_id_list (parser);
16539       /* Restore the saved message.  */
16540       parser->type_definition_forbidden_message = saved_message;
16541     }
16542   else
16543     type_id_list = empty_except_spec;
16544
16545   /* Look for the `)'.  */
16546   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16547
16548   return type_id_list;
16549 }
16550
16551 /* Parse an (optional) type-id-list.
16552
16553    type-id-list:
16554      type-id ... [opt]
16555      type-id-list , type-id ... [opt]
16556
16557    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16558    in the order that the types were presented.  */
16559
16560 static tree
16561 cp_parser_type_id_list (cp_parser* parser)
16562 {
16563   tree types = NULL_TREE;
16564
16565   while (true)
16566     {
16567       cp_token *token;
16568       tree type;
16569
16570       /* Get the next type-id.  */
16571       type = cp_parser_type_id (parser);
16572       /* Parse the optional ellipsis. */
16573       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16574         {
16575           /* Consume the `...'. */
16576           cp_lexer_consume_token (parser->lexer);
16577
16578           /* Turn the type into a pack expansion expression. */
16579           type = make_pack_expansion (type);
16580         }
16581       /* Add it to the list.  */
16582       types = add_exception_specifier (types, type, /*complain=*/1);
16583       /* Peek at the next token.  */
16584       token = cp_lexer_peek_token (parser->lexer);
16585       /* If it is not a `,', we are done.  */
16586       if (token->type != CPP_COMMA)
16587         break;
16588       /* Consume the `,'.  */
16589       cp_lexer_consume_token (parser->lexer);
16590     }
16591
16592   return nreverse (types);
16593 }
16594
16595 /* Parse a try-block.
16596
16597    try-block:
16598      try compound-statement handler-seq  */
16599
16600 static tree
16601 cp_parser_try_block (cp_parser* parser)
16602 {
16603   tree try_block;
16604
16605   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16606   try_block = begin_try_block ();
16607   cp_parser_compound_statement (parser, NULL, true);
16608   finish_try_block (try_block);
16609   cp_parser_handler_seq (parser);
16610   finish_handler_sequence (try_block);
16611
16612   return try_block;
16613 }
16614
16615 /* Parse a function-try-block.
16616
16617    function-try-block:
16618      try ctor-initializer [opt] function-body handler-seq  */
16619
16620 static bool
16621 cp_parser_function_try_block (cp_parser* parser)
16622 {
16623   tree compound_stmt;
16624   tree try_block;
16625   bool ctor_initializer_p;
16626
16627   /* Look for the `try' keyword.  */
16628   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16629     return false;
16630   /* Let the rest of the front end know where we are.  */
16631   try_block = begin_function_try_block (&compound_stmt);
16632   /* Parse the function-body.  */
16633   ctor_initializer_p
16634     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16635   /* We're done with the `try' part.  */
16636   finish_function_try_block (try_block);
16637   /* Parse the handlers.  */
16638   cp_parser_handler_seq (parser);
16639   /* We're done with the handlers.  */
16640   finish_function_handler_sequence (try_block, compound_stmt);
16641
16642   return ctor_initializer_p;
16643 }
16644
16645 /* Parse a handler-seq.
16646
16647    handler-seq:
16648      handler handler-seq [opt]  */
16649
16650 static void
16651 cp_parser_handler_seq (cp_parser* parser)
16652 {
16653   while (true)
16654     {
16655       cp_token *token;
16656
16657       /* Parse the handler.  */
16658       cp_parser_handler (parser);
16659       /* Peek at the next token.  */
16660       token = cp_lexer_peek_token (parser->lexer);
16661       /* If it's not `catch' then there are no more handlers.  */
16662       if (!cp_parser_is_keyword (token, RID_CATCH))
16663         break;
16664     }
16665 }
16666
16667 /* Parse a handler.
16668
16669    handler:
16670      catch ( exception-declaration ) compound-statement  */
16671
16672 static void
16673 cp_parser_handler (cp_parser* parser)
16674 {
16675   tree handler;
16676   tree declaration;
16677
16678   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16679   handler = begin_handler ();
16680   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16681   declaration = cp_parser_exception_declaration (parser);
16682   finish_handler_parms (declaration, handler);
16683   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16684   cp_parser_compound_statement (parser, NULL, false);
16685   finish_handler (handler);
16686 }
16687
16688 /* Parse an exception-declaration.
16689
16690    exception-declaration:
16691      type-specifier-seq declarator
16692      type-specifier-seq abstract-declarator
16693      type-specifier-seq
16694      ...
16695
16696    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16697    ellipsis variant is used.  */
16698
16699 static tree
16700 cp_parser_exception_declaration (cp_parser* parser)
16701 {
16702   cp_decl_specifier_seq type_specifiers;
16703   cp_declarator *declarator;
16704   const char *saved_message;
16705
16706   /* If it's an ellipsis, it's easy to handle.  */
16707   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16708     {
16709       /* Consume the `...' token.  */
16710       cp_lexer_consume_token (parser->lexer);
16711       return NULL_TREE;
16712     }
16713
16714   /* Types may not be defined in exception-declarations.  */
16715   saved_message = parser->type_definition_forbidden_message;
16716   parser->type_definition_forbidden_message
16717     = "types may not be defined in exception-declarations";
16718
16719   /* Parse the type-specifier-seq.  */
16720   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16721                                 &type_specifiers);
16722   /* If it's a `)', then there is no declarator.  */
16723   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16724     declarator = NULL;
16725   else
16726     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16727                                        /*ctor_dtor_or_conv_p=*/NULL,
16728                                        /*parenthesized_p=*/NULL,
16729                                        /*member_p=*/false);
16730
16731   /* Restore the saved message.  */
16732   parser->type_definition_forbidden_message = saved_message;
16733
16734   if (!type_specifiers.any_specifiers_p)
16735     return error_mark_node;
16736
16737   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16738 }
16739
16740 /* Parse a throw-expression.
16741
16742    throw-expression:
16743      throw assignment-expression [opt]
16744
16745    Returns a THROW_EXPR representing the throw-expression.  */
16746
16747 static tree
16748 cp_parser_throw_expression (cp_parser* parser)
16749 {
16750   tree expression;
16751   cp_token* token;
16752
16753   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16754   token = cp_lexer_peek_token (parser->lexer);
16755   /* Figure out whether or not there is an assignment-expression
16756      following the "throw" keyword.  */
16757   if (token->type == CPP_COMMA
16758       || token->type == CPP_SEMICOLON
16759       || token->type == CPP_CLOSE_PAREN
16760       || token->type == CPP_CLOSE_SQUARE
16761       || token->type == CPP_CLOSE_BRACE
16762       || token->type == CPP_COLON)
16763     expression = NULL_TREE;
16764   else
16765     expression = cp_parser_assignment_expression (parser,
16766                                                   /*cast_p=*/false, NULL);
16767
16768   return build_throw (expression);
16769 }
16770
16771 /* GNU Extensions */
16772
16773 /* Parse an (optional) asm-specification.
16774
16775    asm-specification:
16776      asm ( string-literal )
16777
16778    If the asm-specification is present, returns a STRING_CST
16779    corresponding to the string-literal.  Otherwise, returns
16780    NULL_TREE.  */
16781
16782 static tree
16783 cp_parser_asm_specification_opt (cp_parser* parser)
16784 {
16785   cp_token *token;
16786   tree asm_specification;
16787
16788   /* Peek at the next token.  */
16789   token = cp_lexer_peek_token (parser->lexer);
16790   /* If the next token isn't the `asm' keyword, then there's no
16791      asm-specification.  */
16792   if (!cp_parser_is_keyword (token, RID_ASM))
16793     return NULL_TREE;
16794
16795   /* Consume the `asm' token.  */
16796   cp_lexer_consume_token (parser->lexer);
16797   /* Look for the `('.  */
16798   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16799
16800   /* Look for the string-literal.  */
16801   asm_specification = cp_parser_string_literal (parser, false, false);
16802
16803   /* Look for the `)'.  */
16804   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16805
16806   return asm_specification;
16807 }
16808
16809 /* Parse an asm-operand-list.
16810
16811    asm-operand-list:
16812      asm-operand
16813      asm-operand-list , asm-operand
16814
16815    asm-operand:
16816      string-literal ( expression )
16817      [ string-literal ] string-literal ( expression )
16818
16819    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16820    each node is the expression.  The TREE_PURPOSE is itself a
16821    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16822    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16823    is a STRING_CST for the string literal before the parenthesis. Returns
16824    ERROR_MARK_NODE if any of the operands are invalid.  */
16825
16826 static tree
16827 cp_parser_asm_operand_list (cp_parser* parser)
16828 {
16829   tree asm_operands = NULL_TREE;
16830   bool invalid_operands = false;
16831
16832   while (true)
16833     {
16834       tree string_literal;
16835       tree expression;
16836       tree name;
16837
16838       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16839         {
16840           /* Consume the `[' token.  */
16841           cp_lexer_consume_token (parser->lexer);
16842           /* Read the operand name.  */
16843           name = cp_parser_identifier (parser);
16844           if (name != error_mark_node)
16845             name = build_string (IDENTIFIER_LENGTH (name),
16846                                  IDENTIFIER_POINTER (name));
16847           /* Look for the closing `]'.  */
16848           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16849         }
16850       else
16851         name = NULL_TREE;
16852       /* Look for the string-literal.  */
16853       string_literal = cp_parser_string_literal (parser, false, false);
16854
16855       /* Look for the `('.  */
16856       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16857       /* Parse the expression.  */
16858       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16859       /* Look for the `)'.  */
16860       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16861
16862       if (name == error_mark_node 
16863           || string_literal == error_mark_node 
16864           || expression == error_mark_node)
16865         invalid_operands = true;
16866
16867       /* Add this operand to the list.  */
16868       asm_operands = tree_cons (build_tree_list (name, string_literal),
16869                                 expression,
16870                                 asm_operands);
16871       /* If the next token is not a `,', there are no more
16872          operands.  */
16873       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16874         break;
16875       /* Consume the `,'.  */
16876       cp_lexer_consume_token (parser->lexer);
16877     }
16878
16879   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16880 }
16881
16882 /* Parse an asm-clobber-list.
16883
16884    asm-clobber-list:
16885      string-literal
16886      asm-clobber-list , string-literal
16887
16888    Returns a TREE_LIST, indicating the clobbers in the order that they
16889    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16890
16891 static tree
16892 cp_parser_asm_clobber_list (cp_parser* parser)
16893 {
16894   tree clobbers = NULL_TREE;
16895
16896   while (true)
16897     {
16898       tree string_literal;
16899
16900       /* Look for the string literal.  */
16901       string_literal = cp_parser_string_literal (parser, false, false);
16902       /* Add it to the list.  */
16903       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16904       /* If the next token is not a `,', then the list is
16905          complete.  */
16906       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16907         break;
16908       /* Consume the `,' token.  */
16909       cp_lexer_consume_token (parser->lexer);
16910     }
16911
16912   return clobbers;
16913 }
16914
16915 /* Parse an asm-label-list.
16916
16917    asm-label-list:
16918      identifier
16919      asm-label-list , identifier
16920
16921    Returns a TREE_LIST, indicating the labels in the order that they
16922    appeared.  The TREE_VALUE of each node is a label.  */
16923
16924 static tree
16925 cp_parser_asm_label_list (cp_parser* parser)
16926 {
16927   tree labels = NULL_TREE;
16928
16929   while (true)
16930     {
16931       tree identifier, label, name;
16932
16933       /* Look for the identifier.  */
16934       identifier = cp_parser_identifier (parser);
16935       if (!error_operand_p (identifier))
16936         {
16937           label = lookup_label (identifier);
16938           if (TREE_CODE (label) == LABEL_DECL)
16939             {
16940               TREE_USED (label) = 1;
16941               check_goto (label);
16942               name = build_string (IDENTIFIER_LENGTH (identifier),
16943                                    IDENTIFIER_POINTER (identifier));
16944               labels = tree_cons (name, label, labels);
16945             }
16946         }
16947       /* If the next token is not a `,', then the list is
16948          complete.  */
16949       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16950         break;
16951       /* Consume the `,' token.  */
16952       cp_lexer_consume_token (parser->lexer);
16953     }
16954
16955   return nreverse (labels);
16956 }
16957
16958 /* Parse an (optional) series of attributes.
16959
16960    attributes:
16961      attributes attribute
16962
16963    attribute:
16964      __attribute__ (( attribute-list [opt] ))
16965
16966    The return value is as for cp_parser_attribute_list.  */
16967
16968 static tree
16969 cp_parser_attributes_opt (cp_parser* parser)
16970 {
16971   tree attributes = NULL_TREE;
16972
16973   while (true)
16974     {
16975       cp_token *token;
16976       tree attribute_list;
16977
16978       /* Peek at the next token.  */
16979       token = cp_lexer_peek_token (parser->lexer);
16980       /* If it's not `__attribute__', then we're done.  */
16981       if (token->keyword != RID_ATTRIBUTE)
16982         break;
16983
16984       /* Consume the `__attribute__' keyword.  */
16985       cp_lexer_consume_token (parser->lexer);
16986       /* Look for the two `(' tokens.  */
16987       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16988       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16989
16990       /* Peek at the next token.  */
16991       token = cp_lexer_peek_token (parser->lexer);
16992       if (token->type != CPP_CLOSE_PAREN)
16993         /* Parse the attribute-list.  */
16994         attribute_list = cp_parser_attribute_list (parser);
16995       else
16996         /* If the next token is a `)', then there is no attribute
16997            list.  */
16998         attribute_list = NULL;
16999
17000       /* Look for the two `)' tokens.  */
17001       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17002       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17003
17004       /* Add these new attributes to the list.  */
17005       attributes = chainon (attributes, attribute_list);
17006     }
17007
17008   return attributes;
17009 }
17010
17011 /* Parse an attribute-list.
17012
17013    attribute-list:
17014      attribute
17015      attribute-list , attribute
17016
17017    attribute:
17018      identifier
17019      identifier ( identifier )
17020      identifier ( identifier , expression-list )
17021      identifier ( expression-list )
17022
17023    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17024    to an attribute.  The TREE_PURPOSE of each node is the identifier
17025    indicating which attribute is in use.  The TREE_VALUE represents
17026    the arguments, if any.  */
17027
17028 static tree
17029 cp_parser_attribute_list (cp_parser* parser)
17030 {
17031   tree attribute_list = NULL_TREE;
17032   bool save_translate_strings_p = parser->translate_strings_p;
17033
17034   parser->translate_strings_p = false;
17035   while (true)
17036     {
17037       cp_token *token;
17038       tree identifier;
17039       tree attribute;
17040
17041       /* Look for the identifier.  We also allow keywords here; for
17042          example `__attribute__ ((const))' is legal.  */
17043       token = cp_lexer_peek_token (parser->lexer);
17044       if (token->type == CPP_NAME
17045           || token->type == CPP_KEYWORD)
17046         {
17047           tree arguments = NULL_TREE;
17048
17049           /* Consume the token.  */
17050           token = cp_lexer_consume_token (parser->lexer);
17051
17052           /* Save away the identifier that indicates which attribute
17053              this is.  */
17054           identifier = (token->type == CPP_KEYWORD) 
17055             /* For keywords, use the canonical spelling, not the
17056                parsed identifier.  */
17057             ? ridpointers[(int) token->keyword]
17058             : token->u.value;
17059           
17060           attribute = build_tree_list (identifier, NULL_TREE);
17061
17062           /* Peek at the next token.  */
17063           token = cp_lexer_peek_token (parser->lexer);
17064           /* If it's an `(', then parse the attribute arguments.  */
17065           if (token->type == CPP_OPEN_PAREN)
17066             {
17067               VEC(tree,gc) *vec;
17068               vec = cp_parser_parenthesized_expression_list
17069                     (parser, true, /*cast_p=*/false,
17070                      /*allow_expansion_p=*/false,
17071                      /*non_constant_p=*/NULL);
17072               if (vec == NULL)
17073                 arguments = error_mark_node;
17074               else
17075                 {
17076                   arguments = build_tree_list_vec (vec);
17077                   release_tree_vector (vec);
17078                 }
17079               /* Save the arguments away.  */
17080               TREE_VALUE (attribute) = arguments;
17081             }
17082
17083           if (arguments != error_mark_node)
17084             {
17085               /* Add this attribute to the list.  */
17086               TREE_CHAIN (attribute) = attribute_list;
17087               attribute_list = attribute;
17088             }
17089
17090           token = cp_lexer_peek_token (parser->lexer);
17091         }
17092       /* Now, look for more attributes.  If the next token isn't a
17093          `,', we're done.  */
17094       if (token->type != CPP_COMMA)
17095         break;
17096
17097       /* Consume the comma and keep going.  */
17098       cp_lexer_consume_token (parser->lexer);
17099     }
17100   parser->translate_strings_p = save_translate_strings_p;
17101
17102   /* We built up the list in reverse order.  */
17103   return nreverse (attribute_list);
17104 }
17105
17106 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17107    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17108    current value of the PEDANTIC flag, regardless of whether or not
17109    the `__extension__' keyword is present.  The caller is responsible
17110    for restoring the value of the PEDANTIC flag.  */
17111
17112 static bool
17113 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17114 {
17115   /* Save the old value of the PEDANTIC flag.  */
17116   *saved_pedantic = pedantic;
17117
17118   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17119     {
17120       /* Consume the `__extension__' token.  */
17121       cp_lexer_consume_token (parser->lexer);
17122       /* We're not being pedantic while the `__extension__' keyword is
17123          in effect.  */
17124       pedantic = 0;
17125
17126       return true;
17127     }
17128
17129   return false;
17130 }
17131
17132 /* Parse a label declaration.
17133
17134    label-declaration:
17135      __label__ label-declarator-seq ;
17136
17137    label-declarator-seq:
17138      identifier , label-declarator-seq
17139      identifier  */
17140
17141 static void
17142 cp_parser_label_declaration (cp_parser* parser)
17143 {
17144   /* Look for the `__label__' keyword.  */
17145   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17146
17147   while (true)
17148     {
17149       tree identifier;
17150
17151       /* Look for an identifier.  */
17152       identifier = cp_parser_identifier (parser);
17153       /* If we failed, stop.  */
17154       if (identifier == error_mark_node)
17155         break;
17156       /* Declare it as a label.  */
17157       finish_label_decl (identifier);
17158       /* If the next token is a `;', stop.  */
17159       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17160         break;
17161       /* Look for the `,' separating the label declarations.  */
17162       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17163     }
17164
17165   /* Look for the final `;'.  */
17166   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17167 }
17168
17169 /* Support Functions */
17170
17171 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17172    NAME should have one of the representations used for an
17173    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17174    is returned.  If PARSER->SCOPE is a dependent type, then a
17175    SCOPE_REF is returned.
17176
17177    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17178    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17179    was formed.  Abstractly, such entities should not be passed to this
17180    function, because they do not need to be looked up, but it is
17181    simpler to check for this special case here, rather than at the
17182    call-sites.
17183
17184    In cases not explicitly covered above, this function returns a
17185    DECL, OVERLOAD, or baselink representing the result of the lookup.
17186    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17187    is returned.
17188
17189    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17190    (e.g., "struct") that was used.  In that case bindings that do not
17191    refer to types are ignored.
17192
17193    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17194    ignored.
17195
17196    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17197    are ignored.
17198
17199    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17200    types.
17201
17202    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17203    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17204    NULL_TREE otherwise.  */
17205
17206 static tree
17207 cp_parser_lookup_name (cp_parser *parser, tree name,
17208                        enum tag_types tag_type,
17209                        bool is_template,
17210                        bool is_namespace,
17211                        bool check_dependency,
17212                        tree *ambiguous_decls,
17213                        location_t name_location)
17214 {
17215   int flags = 0;
17216   tree decl;
17217   tree object_type = parser->context->object_type;
17218
17219   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17220     flags |= LOOKUP_COMPLAIN;
17221
17222   /* Assume that the lookup will be unambiguous.  */
17223   if (ambiguous_decls)
17224     *ambiguous_decls = NULL_TREE;
17225
17226   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17227      no longer valid.  Note that if we are parsing tentatively, and
17228      the parse fails, OBJECT_TYPE will be automatically restored.  */
17229   parser->context->object_type = NULL_TREE;
17230
17231   if (name == error_mark_node)
17232     return error_mark_node;
17233
17234   /* A template-id has already been resolved; there is no lookup to
17235      do.  */
17236   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17237     return name;
17238   if (BASELINK_P (name))
17239     {
17240       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17241                   == TEMPLATE_ID_EXPR);
17242       return name;
17243     }
17244
17245   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17246      it should already have been checked to make sure that the name
17247      used matches the type being destroyed.  */
17248   if (TREE_CODE (name) == BIT_NOT_EXPR)
17249     {
17250       tree type;
17251
17252       /* Figure out to which type this destructor applies.  */
17253       if (parser->scope)
17254         type = parser->scope;
17255       else if (object_type)
17256         type = object_type;
17257       else
17258         type = current_class_type;
17259       /* If that's not a class type, there is no destructor.  */
17260       if (!type || !CLASS_TYPE_P (type))
17261         return error_mark_node;
17262       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17263         lazily_declare_fn (sfk_destructor, type);
17264       if (!CLASSTYPE_DESTRUCTORS (type))
17265           return error_mark_node;
17266       /* If it was a class type, return the destructor.  */
17267       return CLASSTYPE_DESTRUCTORS (type);
17268     }
17269
17270   /* By this point, the NAME should be an ordinary identifier.  If
17271      the id-expression was a qualified name, the qualifying scope is
17272      stored in PARSER->SCOPE at this point.  */
17273   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17274
17275   /* Perform the lookup.  */
17276   if (parser->scope)
17277     {
17278       bool dependent_p;
17279
17280       if (parser->scope == error_mark_node)
17281         return error_mark_node;
17282
17283       /* If the SCOPE is dependent, the lookup must be deferred until
17284          the template is instantiated -- unless we are explicitly
17285          looking up names in uninstantiated templates.  Even then, we
17286          cannot look up the name if the scope is not a class type; it
17287          might, for example, be a template type parameter.  */
17288       dependent_p = (TYPE_P (parser->scope)
17289                      && dependent_scope_p (parser->scope));
17290       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17291           && dependent_p)
17292         /* Defer lookup.  */
17293         decl = error_mark_node;
17294       else
17295         {
17296           tree pushed_scope = NULL_TREE;
17297
17298           /* If PARSER->SCOPE is a dependent type, then it must be a
17299              class type, and we must not be checking dependencies;
17300              otherwise, we would have processed this lookup above.  So
17301              that PARSER->SCOPE is not considered a dependent base by
17302              lookup_member, we must enter the scope here.  */
17303           if (dependent_p)
17304             pushed_scope = push_scope (parser->scope);
17305           /* If the PARSER->SCOPE is a template specialization, it
17306              may be instantiated during name lookup.  In that case,
17307              errors may be issued.  Even if we rollback the current
17308              tentative parse, those errors are valid.  */
17309           decl = lookup_qualified_name (parser->scope, name,
17310                                         tag_type != none_type,
17311                                         /*complain=*/true);
17312
17313           /* If we have a single function from a using decl, pull it out.  */
17314           if (TREE_CODE (decl) == OVERLOAD
17315               && !really_overloaded_fn (decl))
17316             decl = OVL_FUNCTION (decl);
17317
17318           if (pushed_scope)
17319             pop_scope (pushed_scope);
17320         }
17321
17322       /* If the scope is a dependent type and either we deferred lookup or
17323          we did lookup but didn't find the name, rememeber the name.  */
17324       if (decl == error_mark_node && TYPE_P (parser->scope)
17325           && dependent_type_p (parser->scope))
17326         {
17327           if (tag_type)
17328             {
17329               tree type;
17330
17331               /* The resolution to Core Issue 180 says that `struct
17332                  A::B' should be considered a type-name, even if `A'
17333                  is dependent.  */
17334               type = make_typename_type (parser->scope, name, tag_type,
17335                                          /*complain=*/tf_error);
17336               decl = TYPE_NAME (type);
17337             }
17338           else if (is_template
17339                    && (cp_parser_next_token_ends_template_argument_p (parser)
17340                        || cp_lexer_next_token_is (parser->lexer,
17341                                                   CPP_CLOSE_PAREN)))
17342             decl = make_unbound_class_template (parser->scope,
17343                                                 name, NULL_TREE,
17344                                                 /*complain=*/tf_error);
17345           else
17346             decl = build_qualified_name (/*type=*/NULL_TREE,
17347                                          parser->scope, name,
17348                                          is_template);
17349         }
17350       parser->qualifying_scope = parser->scope;
17351       parser->object_scope = NULL_TREE;
17352     }
17353   else if (object_type)
17354     {
17355       tree object_decl = NULL_TREE;
17356       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17357          OBJECT_TYPE is not a class.  */
17358       if (CLASS_TYPE_P (object_type))
17359         /* If the OBJECT_TYPE is a template specialization, it may
17360            be instantiated during name lookup.  In that case, errors
17361            may be issued.  Even if we rollback the current tentative
17362            parse, those errors are valid.  */
17363         object_decl = lookup_member (object_type,
17364                                      name,
17365                                      /*protect=*/0,
17366                                      tag_type != none_type);
17367       /* Look it up in the enclosing context, too.  */
17368       decl = lookup_name_real (name, tag_type != none_type,
17369                                /*nonclass=*/0,
17370                                /*block_p=*/true, is_namespace, flags);
17371       parser->object_scope = object_type;
17372       parser->qualifying_scope = NULL_TREE;
17373       if (object_decl)
17374         decl = object_decl;
17375     }
17376   else
17377     {
17378       decl = lookup_name_real (name, tag_type != none_type,
17379                                /*nonclass=*/0,
17380                                /*block_p=*/true, is_namespace, flags);
17381       parser->qualifying_scope = NULL_TREE;
17382       parser->object_scope = NULL_TREE;
17383     }
17384
17385   /* If the lookup failed, let our caller know.  */
17386   if (!decl || decl == error_mark_node)
17387     return error_mark_node;
17388
17389   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17390   if (TREE_CODE (decl) == TREE_LIST)
17391     {
17392       if (ambiguous_decls)
17393         *ambiguous_decls = decl;
17394       /* The error message we have to print is too complicated for
17395          cp_parser_error, so we incorporate its actions directly.  */
17396       if (!cp_parser_simulate_error (parser))
17397         {
17398           error_at (name_location, "reference to %qD is ambiguous",
17399                     name);
17400           print_candidates (decl);
17401         }
17402       return error_mark_node;
17403     }
17404
17405   gcc_assert (DECL_P (decl)
17406               || TREE_CODE (decl) == OVERLOAD
17407               || TREE_CODE (decl) == SCOPE_REF
17408               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17409               || BASELINK_P (decl));
17410
17411   /* If we have resolved the name of a member declaration, check to
17412      see if the declaration is accessible.  When the name resolves to
17413      set of overloaded functions, accessibility is checked when
17414      overload resolution is done.
17415
17416      During an explicit instantiation, access is not checked at all,
17417      as per [temp.explicit].  */
17418   if (DECL_P (decl))
17419     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17420
17421   return decl;
17422 }
17423
17424 /* Like cp_parser_lookup_name, but for use in the typical case where
17425    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17426    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17427
17428 static tree
17429 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17430 {
17431   return cp_parser_lookup_name (parser, name,
17432                                 none_type,
17433                                 /*is_template=*/false,
17434                                 /*is_namespace=*/false,
17435                                 /*check_dependency=*/true,
17436                                 /*ambiguous_decls=*/NULL,
17437                                 location);
17438 }
17439
17440 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17441    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17442    true, the DECL indicates the class being defined in a class-head,
17443    or declared in an elaborated-type-specifier.
17444
17445    Otherwise, return DECL.  */
17446
17447 static tree
17448 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17449 {
17450   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17451      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17452
17453        struct A {
17454          template <typename T> struct B;
17455        };
17456
17457        template <typename T> struct A::B {};
17458
17459      Similarly, in an elaborated-type-specifier:
17460
17461        namespace N { struct X{}; }
17462
17463        struct A {
17464          template <typename T> friend struct N::X;
17465        };
17466
17467      However, if the DECL refers to a class type, and we are in
17468      the scope of the class, then the name lookup automatically
17469      finds the TYPE_DECL created by build_self_reference rather
17470      than a TEMPLATE_DECL.  For example, in:
17471
17472        template <class T> struct S {
17473          S s;
17474        };
17475
17476      there is no need to handle such case.  */
17477
17478   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17479     return DECL_TEMPLATE_RESULT (decl);
17480
17481   return decl;
17482 }
17483
17484 /* If too many, or too few, template-parameter lists apply to the
17485    declarator, issue an error message.  Returns TRUE if all went well,
17486    and FALSE otherwise.  */
17487
17488 static bool
17489 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17490                                                 cp_declarator *declarator,
17491                                                 location_t declarator_location)
17492 {
17493   unsigned num_templates;
17494
17495   /* We haven't seen any classes that involve template parameters yet.  */
17496   num_templates = 0;
17497
17498   switch (declarator->kind)
17499     {
17500     case cdk_id:
17501       if (declarator->u.id.qualifying_scope)
17502         {
17503           tree scope;
17504           tree member;
17505
17506           scope = declarator->u.id.qualifying_scope;
17507           member = declarator->u.id.unqualified_name;
17508
17509           while (scope && CLASS_TYPE_P (scope))
17510             {
17511               /* You're supposed to have one `template <...>'
17512                  for every template class, but you don't need one
17513                  for a full specialization.  For example:
17514
17515                  template <class T> struct S{};
17516                  template <> struct S<int> { void f(); };
17517                  void S<int>::f () {}
17518
17519                  is correct; there shouldn't be a `template <>' for
17520                  the definition of `S<int>::f'.  */
17521               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17522                 /* If SCOPE does not have template information of any
17523                    kind, then it is not a template, nor is it nested
17524                    within a template.  */
17525                 break;
17526               if (explicit_class_specialization_p (scope))
17527                 break;
17528               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17529                 ++num_templates;
17530
17531               scope = TYPE_CONTEXT (scope);
17532             }
17533         }
17534       else if (TREE_CODE (declarator->u.id.unqualified_name)
17535                == TEMPLATE_ID_EXPR)
17536         /* If the DECLARATOR has the form `X<y>' then it uses one
17537            additional level of template parameters.  */
17538         ++num_templates;
17539
17540       return cp_parser_check_template_parameters 
17541         (parser, num_templates, declarator_location, declarator);
17542
17543
17544     case cdk_function:
17545     case cdk_array:
17546     case cdk_pointer:
17547     case cdk_reference:
17548     case cdk_ptrmem:
17549       return (cp_parser_check_declarator_template_parameters
17550               (parser, declarator->declarator, declarator_location));
17551
17552     case cdk_error:
17553       return true;
17554
17555     default:
17556       gcc_unreachable ();
17557     }
17558   return false;
17559 }
17560
17561 /* NUM_TEMPLATES were used in the current declaration.  If that is
17562    invalid, return FALSE and issue an error messages.  Otherwise,
17563    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17564    declarator and we can print more accurate diagnostics.  */
17565
17566 static bool
17567 cp_parser_check_template_parameters (cp_parser* parser,
17568                                      unsigned num_templates,
17569                                      location_t location,
17570                                      cp_declarator *declarator)
17571 {
17572   /* If there are the same number of template classes and parameter
17573      lists, that's OK.  */
17574   if (parser->num_template_parameter_lists == num_templates)
17575     return true;
17576   /* If there are more, but only one more, then we are referring to a
17577      member template.  That's OK too.  */
17578   if (parser->num_template_parameter_lists == num_templates + 1)
17579     return true;
17580   /* If there are more template classes than parameter lists, we have
17581      something like:
17582
17583        template <class T> void S<T>::R<T>::f ();  */
17584   if (parser->num_template_parameter_lists < num_templates)
17585     {
17586       if (declarator)
17587         error_at (location, "specializing member %<%T::%E%> "
17588                   "requires %<template<>%> syntax", 
17589                   declarator->u.id.qualifying_scope,
17590                   declarator->u.id.unqualified_name);
17591       else 
17592         error_at (location, "too few template-parameter-lists");
17593       return false;
17594     }
17595   /* Otherwise, there are too many template parameter lists.  We have
17596      something like:
17597
17598      template <class T> template <class U> void S::f();  */
17599   error_at (location, "too many template-parameter-lists");
17600   return false;
17601 }
17602
17603 /* Parse an optional `::' token indicating that the following name is
17604    from the global namespace.  If so, PARSER->SCOPE is set to the
17605    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17606    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17607    Returns the new value of PARSER->SCOPE, if the `::' token is
17608    present, and NULL_TREE otherwise.  */
17609
17610 static tree
17611 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17612 {
17613   cp_token *token;
17614
17615   /* Peek at the next token.  */
17616   token = cp_lexer_peek_token (parser->lexer);
17617   /* If we're looking at a `::' token then we're starting from the
17618      global namespace, not our current location.  */
17619   if (token->type == CPP_SCOPE)
17620     {
17621       /* Consume the `::' token.  */
17622       cp_lexer_consume_token (parser->lexer);
17623       /* Set the SCOPE so that we know where to start the lookup.  */
17624       parser->scope = global_namespace;
17625       parser->qualifying_scope = global_namespace;
17626       parser->object_scope = NULL_TREE;
17627
17628       return parser->scope;
17629     }
17630   else if (!current_scope_valid_p)
17631     {
17632       parser->scope = NULL_TREE;
17633       parser->qualifying_scope = NULL_TREE;
17634       parser->object_scope = NULL_TREE;
17635     }
17636
17637   return NULL_TREE;
17638 }
17639
17640 /* Returns TRUE if the upcoming token sequence is the start of a
17641    constructor declarator.  If FRIEND_P is true, the declarator is
17642    preceded by the `friend' specifier.  */
17643
17644 static bool
17645 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17646 {
17647   bool constructor_p;
17648   tree type_decl = NULL_TREE;
17649   bool nested_name_p;
17650   cp_token *next_token;
17651
17652   /* The common case is that this is not a constructor declarator, so
17653      try to avoid doing lots of work if at all possible.  It's not
17654      valid declare a constructor at function scope.  */
17655   if (parser->in_function_body)
17656     return false;
17657   /* And only certain tokens can begin a constructor declarator.  */
17658   next_token = cp_lexer_peek_token (parser->lexer);
17659   if (next_token->type != CPP_NAME
17660       && next_token->type != CPP_SCOPE
17661       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17662       && next_token->type != CPP_TEMPLATE_ID)
17663     return false;
17664
17665   /* Parse tentatively; we are going to roll back all of the tokens
17666      consumed here.  */
17667   cp_parser_parse_tentatively (parser);
17668   /* Assume that we are looking at a constructor declarator.  */
17669   constructor_p = true;
17670
17671   /* Look for the optional `::' operator.  */
17672   cp_parser_global_scope_opt (parser,
17673                               /*current_scope_valid_p=*/false);
17674   /* Look for the nested-name-specifier.  */
17675   nested_name_p
17676     = (cp_parser_nested_name_specifier_opt (parser,
17677                                             /*typename_keyword_p=*/false,
17678                                             /*check_dependency_p=*/false,
17679                                             /*type_p=*/false,
17680                                             /*is_declaration=*/false)
17681        != NULL_TREE);
17682   /* Outside of a class-specifier, there must be a
17683      nested-name-specifier.  */
17684   if (!nested_name_p &&
17685       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17686        || friend_p))
17687     constructor_p = false;
17688   /* If we still think that this might be a constructor-declarator,
17689      look for a class-name.  */
17690   if (constructor_p)
17691     {
17692       /* If we have:
17693
17694            template <typename T> struct S { S(); };
17695            template <typename T> S<T>::S ();
17696
17697          we must recognize that the nested `S' names a class.
17698          Similarly, for:
17699
17700            template <typename T> S<T>::S<T> ();
17701
17702          we must recognize that the nested `S' names a template.  */
17703       type_decl = cp_parser_class_name (parser,
17704                                         /*typename_keyword_p=*/false,
17705                                         /*template_keyword_p=*/false,
17706                                         none_type,
17707                                         /*check_dependency_p=*/false,
17708                                         /*class_head_p=*/false,
17709                                         /*is_declaration=*/false);
17710       /* If there was no class-name, then this is not a constructor.  */
17711       constructor_p = !cp_parser_error_occurred (parser);
17712     }
17713
17714   /* If we're still considering a constructor, we have to see a `(',
17715      to begin the parameter-declaration-clause, followed by either a
17716      `)', an `...', or a decl-specifier.  We need to check for a
17717      type-specifier to avoid being fooled into thinking that:
17718
17719        S::S (f) (int);
17720
17721      is a constructor.  (It is actually a function named `f' that
17722      takes one parameter (of type `int') and returns a value of type
17723      `S::S'.  */
17724   if (constructor_p
17725       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17726     {
17727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17728           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17729           /* A parameter declaration begins with a decl-specifier,
17730              which is either the "attribute" keyword, a storage class
17731              specifier, or (usually) a type-specifier.  */
17732           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17733         {
17734           tree type;
17735           tree pushed_scope = NULL_TREE;
17736           unsigned saved_num_template_parameter_lists;
17737
17738           /* Names appearing in the type-specifier should be looked up
17739              in the scope of the class.  */
17740           if (current_class_type)
17741             type = NULL_TREE;
17742           else
17743             {
17744               type = TREE_TYPE (type_decl);
17745               if (TREE_CODE (type) == TYPENAME_TYPE)
17746                 {
17747                   type = resolve_typename_type (type,
17748                                                 /*only_current_p=*/false);
17749                   if (TREE_CODE (type) == TYPENAME_TYPE)
17750                     {
17751                       cp_parser_abort_tentative_parse (parser);
17752                       return false;
17753                     }
17754                 }
17755               pushed_scope = push_scope (type);
17756             }
17757
17758           /* Inside the constructor parameter list, surrounding
17759              template-parameter-lists do not apply.  */
17760           saved_num_template_parameter_lists
17761             = parser->num_template_parameter_lists;
17762           parser->num_template_parameter_lists = 0;
17763
17764           /* Look for the type-specifier.  */
17765           cp_parser_type_specifier (parser,
17766                                     CP_PARSER_FLAGS_NONE,
17767                                     /*decl_specs=*/NULL,
17768                                     /*is_declarator=*/true,
17769                                     /*declares_class_or_enum=*/NULL,
17770                                     /*is_cv_qualifier=*/NULL);
17771
17772           parser->num_template_parameter_lists
17773             = saved_num_template_parameter_lists;
17774
17775           /* Leave the scope of the class.  */
17776           if (pushed_scope)
17777             pop_scope (pushed_scope);
17778
17779           constructor_p = !cp_parser_error_occurred (parser);
17780         }
17781     }
17782   else
17783     constructor_p = false;
17784   /* We did not really want to consume any tokens.  */
17785   cp_parser_abort_tentative_parse (parser);
17786
17787   return constructor_p;
17788 }
17789
17790 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17791    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17792    they must be performed once we are in the scope of the function.
17793
17794    Returns the function defined.  */
17795
17796 static tree
17797 cp_parser_function_definition_from_specifiers_and_declarator
17798   (cp_parser* parser,
17799    cp_decl_specifier_seq *decl_specifiers,
17800    tree attributes,
17801    const cp_declarator *declarator)
17802 {
17803   tree fn;
17804   bool success_p;
17805
17806   /* Begin the function-definition.  */
17807   success_p = start_function (decl_specifiers, declarator, attributes);
17808
17809   /* The things we're about to see are not directly qualified by any
17810      template headers we've seen thus far.  */
17811   reset_specialization ();
17812
17813   /* If there were names looked up in the decl-specifier-seq that we
17814      did not check, check them now.  We must wait until we are in the
17815      scope of the function to perform the checks, since the function
17816      might be a friend.  */
17817   perform_deferred_access_checks ();
17818
17819   if (!success_p)
17820     {
17821       /* Skip the entire function.  */
17822       cp_parser_skip_to_end_of_block_or_statement (parser);
17823       fn = error_mark_node;
17824     }
17825   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17826     {
17827       /* Seen already, skip it.  An error message has already been output.  */
17828       cp_parser_skip_to_end_of_block_or_statement (parser);
17829       fn = current_function_decl;
17830       current_function_decl = NULL_TREE;
17831       /* If this is a function from a class, pop the nested class.  */
17832       if (current_class_name)
17833         pop_nested_class ();
17834     }
17835   else
17836     fn = cp_parser_function_definition_after_declarator (parser,
17837                                                          /*inline_p=*/false);
17838
17839   return fn;
17840 }
17841
17842 /* Parse the part of a function-definition that follows the
17843    declarator.  INLINE_P is TRUE iff this function is an inline
17844    function defined with a class-specifier.
17845
17846    Returns the function defined.  */
17847
17848 static tree
17849 cp_parser_function_definition_after_declarator (cp_parser* parser,
17850                                                 bool inline_p)
17851 {
17852   tree fn;
17853   bool ctor_initializer_p = false;
17854   bool saved_in_unbraced_linkage_specification_p;
17855   bool saved_in_function_body;
17856   unsigned saved_num_template_parameter_lists;
17857   cp_token *token;
17858
17859   saved_in_function_body = parser->in_function_body;
17860   parser->in_function_body = true;
17861   /* If the next token is `return', then the code may be trying to
17862      make use of the "named return value" extension that G++ used to
17863      support.  */
17864   token = cp_lexer_peek_token (parser->lexer);
17865   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17866     {
17867       /* Consume the `return' keyword.  */
17868       cp_lexer_consume_token (parser->lexer);
17869       /* Look for the identifier that indicates what value is to be
17870          returned.  */
17871       cp_parser_identifier (parser);
17872       /* Issue an error message.  */
17873       error_at (token->location,
17874                 "named return values are no longer supported");
17875       /* Skip tokens until we reach the start of the function body.  */
17876       while (true)
17877         {
17878           cp_token *token = cp_lexer_peek_token (parser->lexer);
17879           if (token->type == CPP_OPEN_BRACE
17880               || token->type == CPP_EOF
17881               || token->type == CPP_PRAGMA_EOL)
17882             break;
17883           cp_lexer_consume_token (parser->lexer);
17884         }
17885     }
17886   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17887      anything declared inside `f'.  */
17888   saved_in_unbraced_linkage_specification_p
17889     = parser->in_unbraced_linkage_specification_p;
17890   parser->in_unbraced_linkage_specification_p = false;
17891   /* Inside the function, surrounding template-parameter-lists do not
17892      apply.  */
17893   saved_num_template_parameter_lists
17894     = parser->num_template_parameter_lists;
17895   parser->num_template_parameter_lists = 0;
17896   /* If the next token is `try', then we are looking at a
17897      function-try-block.  */
17898   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17899     ctor_initializer_p = cp_parser_function_try_block (parser);
17900   /* A function-try-block includes the function-body, so we only do
17901      this next part if we're not processing a function-try-block.  */
17902   else
17903     ctor_initializer_p
17904       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17905
17906   /* Finish the function.  */
17907   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17908                         (inline_p ? 2 : 0));
17909   /* Generate code for it, if necessary.  */
17910   expand_or_defer_fn (fn);
17911   /* Restore the saved values.  */
17912   parser->in_unbraced_linkage_specification_p
17913     = saved_in_unbraced_linkage_specification_p;
17914   parser->num_template_parameter_lists
17915     = saved_num_template_parameter_lists;
17916   parser->in_function_body = saved_in_function_body;
17917
17918   return fn;
17919 }
17920
17921 /* Parse a template-declaration, assuming that the `export' (and
17922    `extern') keywords, if present, has already been scanned.  MEMBER_P
17923    is as for cp_parser_template_declaration.  */
17924
17925 static void
17926 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17927 {
17928   tree decl = NULL_TREE;
17929   VEC (deferred_access_check,gc) *checks;
17930   tree parameter_list;
17931   bool friend_p = false;
17932   bool need_lang_pop;
17933   cp_token *token;
17934
17935   /* Look for the `template' keyword.  */
17936   token = cp_lexer_peek_token (parser->lexer);
17937   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17938     return;
17939
17940   /* And the `<'.  */
17941   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17942     return;
17943   if (at_class_scope_p () && current_function_decl)
17944     {
17945       /* 14.5.2.2 [temp.mem]
17946
17947          A local class shall not have member templates.  */
17948       error_at (token->location,
17949                 "invalid declaration of member template in local class");
17950       cp_parser_skip_to_end_of_block_or_statement (parser);
17951       return;
17952     }
17953   /* [temp]
17954
17955      A template ... shall not have C linkage.  */
17956   if (current_lang_name == lang_name_c)
17957     {
17958       error_at (token->location, "template with C linkage");
17959       /* Give it C++ linkage to avoid confusing other parts of the
17960          front end.  */
17961       push_lang_context (lang_name_cplusplus);
17962       need_lang_pop = true;
17963     }
17964   else
17965     need_lang_pop = false;
17966
17967   /* We cannot perform access checks on the template parameter
17968      declarations until we know what is being declared, just as we
17969      cannot check the decl-specifier list.  */
17970   push_deferring_access_checks (dk_deferred);
17971
17972   /* If the next token is `>', then we have an invalid
17973      specialization.  Rather than complain about an invalid template
17974      parameter, issue an error message here.  */
17975   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17976     {
17977       cp_parser_error (parser, "invalid explicit specialization");
17978       begin_specialization ();
17979       parameter_list = NULL_TREE;
17980     }
17981   else
17982     /* Parse the template parameters.  */
17983     parameter_list = cp_parser_template_parameter_list (parser);
17984
17985   /* Get the deferred access checks from the parameter list.  These
17986      will be checked once we know what is being declared, as for a
17987      member template the checks must be performed in the scope of the
17988      class containing the member.  */
17989   checks = get_deferred_access_checks ();
17990
17991   /* Look for the `>'.  */
17992   cp_parser_skip_to_end_of_template_parameter_list (parser);
17993   /* We just processed one more parameter list.  */
17994   ++parser->num_template_parameter_lists;
17995   /* If the next token is `template', there are more template
17996      parameters.  */
17997   if (cp_lexer_next_token_is_keyword (parser->lexer,
17998                                       RID_TEMPLATE))
17999     cp_parser_template_declaration_after_export (parser, member_p);
18000   else
18001     {
18002       /* There are no access checks when parsing a template, as we do not
18003          know if a specialization will be a friend.  */
18004       push_deferring_access_checks (dk_no_check);
18005       token = cp_lexer_peek_token (parser->lexer);
18006       decl = cp_parser_single_declaration (parser,
18007                                            checks,
18008                                            member_p,
18009                                            /*explicit_specialization_p=*/false,
18010                                            &friend_p);
18011       pop_deferring_access_checks ();
18012
18013       /* If this is a member template declaration, let the front
18014          end know.  */
18015       if (member_p && !friend_p && decl)
18016         {
18017           if (TREE_CODE (decl) == TYPE_DECL)
18018             cp_parser_check_access_in_redeclaration (decl, token->location);
18019
18020           decl = finish_member_template_decl (decl);
18021         }
18022       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18023         make_friend_class (current_class_type, TREE_TYPE (decl),
18024                            /*complain=*/true);
18025     }
18026   /* We are done with the current parameter list.  */
18027   --parser->num_template_parameter_lists;
18028
18029   pop_deferring_access_checks ();
18030
18031   /* Finish up.  */
18032   finish_template_decl (parameter_list);
18033
18034   /* Register member declarations.  */
18035   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18036     finish_member_declaration (decl);
18037   /* For the erroneous case of a template with C linkage, we pushed an
18038      implicit C++ linkage scope; exit that scope now.  */
18039   if (need_lang_pop)
18040     pop_lang_context ();
18041   /* If DECL is a function template, we must return to parse it later.
18042      (Even though there is no definition, there might be default
18043      arguments that need handling.)  */
18044   if (member_p && decl
18045       && (TREE_CODE (decl) == FUNCTION_DECL
18046           || DECL_FUNCTION_TEMPLATE_P (decl)))
18047     TREE_VALUE (parser->unparsed_functions_queues)
18048       = tree_cons (NULL_TREE, decl,
18049                    TREE_VALUE (parser->unparsed_functions_queues));
18050 }
18051
18052 /* Perform the deferred access checks from a template-parameter-list.
18053    CHECKS is a TREE_LIST of access checks, as returned by
18054    get_deferred_access_checks.  */
18055
18056 static void
18057 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18058 {
18059   ++processing_template_parmlist;
18060   perform_access_checks (checks);
18061   --processing_template_parmlist;
18062 }
18063
18064 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18065    `function-definition' sequence.  MEMBER_P is true, this declaration
18066    appears in a class scope.
18067
18068    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18069    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18070
18071 static tree
18072 cp_parser_single_declaration (cp_parser* parser,
18073                               VEC (deferred_access_check,gc)* checks,
18074                               bool member_p,
18075                               bool explicit_specialization_p,
18076                               bool* friend_p)
18077 {
18078   int declares_class_or_enum;
18079   tree decl = NULL_TREE;
18080   cp_decl_specifier_seq decl_specifiers;
18081   bool function_definition_p = false;
18082   cp_token *decl_spec_token_start;
18083
18084   /* This function is only used when processing a template
18085      declaration.  */
18086   gcc_assert (innermost_scope_kind () == sk_template_parms
18087               || innermost_scope_kind () == sk_template_spec);
18088
18089   /* Defer access checks until we know what is being declared.  */
18090   push_deferring_access_checks (dk_deferred);
18091
18092   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18093      alternative.  */
18094   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18095   cp_parser_decl_specifier_seq (parser,
18096                                 CP_PARSER_FLAGS_OPTIONAL,
18097                                 &decl_specifiers,
18098                                 &declares_class_or_enum);
18099   if (friend_p)
18100     *friend_p = cp_parser_friend_p (&decl_specifiers);
18101
18102   /* There are no template typedefs.  */
18103   if (decl_specifiers.specs[(int) ds_typedef])
18104     {
18105       error_at (decl_spec_token_start->location,
18106                 "template declaration of %<typedef%>");
18107       decl = error_mark_node;
18108     }
18109
18110   /* Gather up the access checks that occurred the
18111      decl-specifier-seq.  */
18112   stop_deferring_access_checks ();
18113
18114   /* Check for the declaration of a template class.  */
18115   if (declares_class_or_enum)
18116     {
18117       if (cp_parser_declares_only_class_p (parser))
18118         {
18119           decl = shadow_tag (&decl_specifiers);
18120
18121           /* In this case:
18122
18123                struct C {
18124                  friend template <typename T> struct A<T>::B;
18125                };
18126
18127              A<T>::B will be represented by a TYPENAME_TYPE, and
18128              therefore not recognized by shadow_tag.  */
18129           if (friend_p && *friend_p
18130               && !decl
18131               && decl_specifiers.type
18132               && TYPE_P (decl_specifiers.type))
18133             decl = decl_specifiers.type;
18134
18135           if (decl && decl != error_mark_node)
18136             decl = TYPE_NAME (decl);
18137           else
18138             decl = error_mark_node;
18139
18140           /* Perform access checks for template parameters.  */
18141           cp_parser_perform_template_parameter_access_checks (checks);
18142         }
18143     }
18144   /* If it's not a template class, try for a template function.  If
18145      the next token is a `;', then this declaration does not declare
18146      anything.  But, if there were errors in the decl-specifiers, then
18147      the error might well have come from an attempted class-specifier.
18148      In that case, there's no need to warn about a missing declarator.  */
18149   if (!decl
18150       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18151           || decl_specifiers.type != error_mark_node))
18152     {
18153       decl = cp_parser_init_declarator (parser,
18154                                         &decl_specifiers,
18155                                         checks,
18156                                         /*function_definition_allowed_p=*/true,
18157                                         member_p,
18158                                         declares_class_or_enum,
18159                                         &function_definition_p);
18160
18161     /* 7.1.1-1 [dcl.stc]
18162
18163        A storage-class-specifier shall not be specified in an explicit
18164        specialization...  */
18165     if (decl
18166         && explicit_specialization_p
18167         && decl_specifiers.storage_class != sc_none)
18168       {
18169         error_at (decl_spec_token_start->location,
18170                   "explicit template specialization cannot have a storage class");
18171         decl = error_mark_node;
18172       }
18173     }
18174
18175   pop_deferring_access_checks ();
18176
18177   /* Clear any current qualification; whatever comes next is the start
18178      of something new.  */
18179   parser->scope = NULL_TREE;
18180   parser->qualifying_scope = NULL_TREE;
18181   parser->object_scope = NULL_TREE;
18182   /* Look for a trailing `;' after the declaration.  */
18183   if (!function_definition_p
18184       && (decl == error_mark_node
18185           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18186     cp_parser_skip_to_end_of_block_or_statement (parser);
18187
18188   return decl;
18189 }
18190
18191 /* Parse a cast-expression that is not the operand of a unary "&".  */
18192
18193 static tree
18194 cp_parser_simple_cast_expression (cp_parser *parser)
18195 {
18196   return cp_parser_cast_expression (parser, /*address_p=*/false,
18197                                     /*cast_p=*/false, NULL);
18198 }
18199
18200 /* Parse a functional cast to TYPE.  Returns an expression
18201    representing the cast.  */
18202
18203 static tree
18204 cp_parser_functional_cast (cp_parser* parser, tree type)
18205 {
18206   VEC(tree,gc) *vec;
18207   tree expression_list;
18208   tree cast;
18209   bool nonconst_p;
18210
18211   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18212     {
18213       maybe_warn_cpp0x ("extended initializer lists");
18214       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18215       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18216       if (TREE_CODE (type) == TYPE_DECL)
18217         type = TREE_TYPE (type);
18218       return finish_compound_literal (type, expression_list);
18219     }
18220
18221
18222   vec = cp_parser_parenthesized_expression_list (parser, false,
18223                                                  /*cast_p=*/true,
18224                                                  /*allow_expansion_p=*/true,
18225                                                  /*non_constant_p=*/NULL);
18226   if (vec == NULL)
18227     expression_list = error_mark_node;
18228   else
18229     {
18230       expression_list = build_tree_list_vec (vec);
18231       release_tree_vector (vec);
18232     }
18233
18234   cast = build_functional_cast (type, expression_list,
18235                                 tf_warning_or_error);
18236   /* [expr.const]/1: In an integral constant expression "only type
18237      conversions to integral or enumeration type can be used".  */
18238   if (TREE_CODE (type) == TYPE_DECL)
18239     type = TREE_TYPE (type);
18240   if (cast != error_mark_node
18241       && !cast_valid_in_integral_constant_expression_p (type)
18242       && (cp_parser_non_integral_constant_expression
18243           (parser, "a call to a constructor")))
18244     return error_mark_node;
18245   return cast;
18246 }
18247
18248 /* Save the tokens that make up the body of a member function defined
18249    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18250    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18251    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18252    for the member function.  */
18253
18254 static tree
18255 cp_parser_save_member_function_body (cp_parser* parser,
18256                                      cp_decl_specifier_seq *decl_specifiers,
18257                                      cp_declarator *declarator,
18258                                      tree attributes)
18259 {
18260   cp_token *first;
18261   cp_token *last;
18262   tree fn;
18263
18264   /* Create the FUNCTION_DECL.  */
18265   fn = grokmethod (decl_specifiers, declarator, attributes);
18266   /* If something went badly wrong, bail out now.  */
18267   if (fn == error_mark_node)
18268     {
18269       /* If there's a function-body, skip it.  */
18270       if (cp_parser_token_starts_function_definition_p
18271           (cp_lexer_peek_token (parser->lexer)))
18272         cp_parser_skip_to_end_of_block_or_statement (parser);
18273       return error_mark_node;
18274     }
18275
18276   /* Remember it, if there default args to post process.  */
18277   cp_parser_save_default_args (parser, fn);
18278
18279   /* Save away the tokens that make up the body of the
18280      function.  */
18281   first = parser->lexer->next_token;
18282   /* We can have braced-init-list mem-initializers before the fn body.  */
18283   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18284     {
18285       cp_lexer_consume_token (parser->lexer);
18286       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18287              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18288         {
18289           /* cache_group will stop after an un-nested { } pair, too.  */
18290           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18291             break;
18292
18293           /* variadic mem-inits have ... after the ')'.  */
18294           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18295             cp_lexer_consume_token (parser->lexer);
18296         }
18297     }
18298   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18299   /* Handle function try blocks.  */
18300   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18301     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18302   last = parser->lexer->next_token;
18303
18304   /* Save away the inline definition; we will process it when the
18305      class is complete.  */
18306   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18307   DECL_PENDING_INLINE_P (fn) = 1;
18308
18309   /* We need to know that this was defined in the class, so that
18310      friend templates are handled correctly.  */
18311   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18312
18313   /* Add FN to the queue of functions to be parsed later.  */
18314   TREE_VALUE (parser->unparsed_functions_queues)
18315     = tree_cons (NULL_TREE, fn,
18316                  TREE_VALUE (parser->unparsed_functions_queues));
18317
18318   return fn;
18319 }
18320
18321 /* Parse a template-argument-list, as well as the trailing ">" (but
18322    not the opening ">").  See cp_parser_template_argument_list for the
18323    return value.  */
18324
18325 static tree
18326 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18327 {
18328   tree arguments;
18329   tree saved_scope;
18330   tree saved_qualifying_scope;
18331   tree saved_object_scope;
18332   bool saved_greater_than_is_operator_p;
18333   int saved_unevaluated_operand;
18334   int saved_inhibit_evaluation_warnings;
18335
18336   /* [temp.names]
18337
18338      When parsing a template-id, the first non-nested `>' is taken as
18339      the end of the template-argument-list rather than a greater-than
18340      operator.  */
18341   saved_greater_than_is_operator_p
18342     = parser->greater_than_is_operator_p;
18343   parser->greater_than_is_operator_p = false;
18344   /* Parsing the argument list may modify SCOPE, so we save it
18345      here.  */
18346   saved_scope = parser->scope;
18347   saved_qualifying_scope = parser->qualifying_scope;
18348   saved_object_scope = parser->object_scope;
18349   /* We need to evaluate the template arguments, even though this
18350      template-id may be nested within a "sizeof".  */
18351   saved_unevaluated_operand = cp_unevaluated_operand;
18352   cp_unevaluated_operand = 0;
18353   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18354   c_inhibit_evaluation_warnings = 0;
18355   /* Parse the template-argument-list itself.  */
18356   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18357       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18358     arguments = NULL_TREE;
18359   else
18360     arguments = cp_parser_template_argument_list (parser);
18361   /* Look for the `>' that ends the template-argument-list. If we find
18362      a '>>' instead, it's probably just a typo.  */
18363   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18364     {
18365       if (cxx_dialect != cxx98)
18366         {
18367           /* In C++0x, a `>>' in a template argument list or cast
18368              expression is considered to be two separate `>'
18369              tokens. So, change the current token to a `>', but don't
18370              consume it: it will be consumed later when the outer
18371              template argument list (or cast expression) is parsed.
18372              Note that this replacement of `>' for `>>' is necessary
18373              even if we are parsing tentatively: in the tentative
18374              case, after calling
18375              cp_parser_enclosed_template_argument_list we will always
18376              throw away all of the template arguments and the first
18377              closing `>', either because the template argument list
18378              was erroneous or because we are replacing those tokens
18379              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18380              not have been thrown away) is needed either to close an
18381              outer template argument list or to complete a new-style
18382              cast.  */
18383           cp_token *token = cp_lexer_peek_token (parser->lexer);
18384           token->type = CPP_GREATER;
18385         }
18386       else if (!saved_greater_than_is_operator_p)
18387         {
18388           /* If we're in a nested template argument list, the '>>' has
18389             to be a typo for '> >'. We emit the error message, but we
18390             continue parsing and we push a '>' as next token, so that
18391             the argument list will be parsed correctly.  Note that the
18392             global source location is still on the token before the
18393             '>>', so we need to say explicitly where we want it.  */
18394           cp_token *token = cp_lexer_peek_token (parser->lexer);
18395           error_at (token->location, "%<>>%> should be %<> >%> "
18396                     "within a nested template argument list");
18397
18398           token->type = CPP_GREATER;
18399         }
18400       else
18401         {
18402           /* If this is not a nested template argument list, the '>>'
18403             is a typo for '>'. Emit an error message and continue.
18404             Same deal about the token location, but here we can get it
18405             right by consuming the '>>' before issuing the diagnostic.  */
18406           cp_token *token = cp_lexer_consume_token (parser->lexer);
18407           error_at (token->location,
18408                     "spurious %<>>%>, use %<>%> to terminate "
18409                     "a template argument list");
18410         }
18411     }
18412   else
18413     cp_parser_skip_to_end_of_template_parameter_list (parser);
18414   /* The `>' token might be a greater-than operator again now.  */
18415   parser->greater_than_is_operator_p
18416     = saved_greater_than_is_operator_p;
18417   /* Restore the SAVED_SCOPE.  */
18418   parser->scope = saved_scope;
18419   parser->qualifying_scope = saved_qualifying_scope;
18420   parser->object_scope = saved_object_scope;
18421   cp_unevaluated_operand = saved_unevaluated_operand;
18422   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18423
18424   return arguments;
18425 }
18426
18427 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18428    arguments, or the body of the function have not yet been parsed,
18429    parse them now.  */
18430
18431 static void
18432 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18433 {
18434   /* If this member is a template, get the underlying
18435      FUNCTION_DECL.  */
18436   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18437     member_function = DECL_TEMPLATE_RESULT (member_function);
18438
18439   /* There should not be any class definitions in progress at this
18440      point; the bodies of members are only parsed outside of all class
18441      definitions.  */
18442   gcc_assert (parser->num_classes_being_defined == 0);
18443   /* While we're parsing the member functions we might encounter more
18444      classes.  We want to handle them right away, but we don't want
18445      them getting mixed up with functions that are currently in the
18446      queue.  */
18447   parser->unparsed_functions_queues
18448     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18449
18450   /* Make sure that any template parameters are in scope.  */
18451   maybe_begin_member_template_processing (member_function);
18452
18453   /* If the body of the function has not yet been parsed, parse it
18454      now.  */
18455   if (DECL_PENDING_INLINE_P (member_function))
18456     {
18457       tree function_scope;
18458       cp_token_cache *tokens;
18459
18460       /* The function is no longer pending; we are processing it.  */
18461       tokens = DECL_PENDING_INLINE_INFO (member_function);
18462       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18463       DECL_PENDING_INLINE_P (member_function) = 0;
18464
18465       /* If this is a local class, enter the scope of the containing
18466          function.  */
18467       function_scope = current_function_decl;
18468       if (function_scope)
18469         push_function_context ();
18470
18471       /* Push the body of the function onto the lexer stack.  */
18472       cp_parser_push_lexer_for_tokens (parser, tokens);
18473
18474       /* Let the front end know that we going to be defining this
18475          function.  */
18476       start_preparsed_function (member_function, NULL_TREE,
18477                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18478
18479       /* Don't do access checking if it is a templated function.  */
18480       if (processing_template_decl)
18481         push_deferring_access_checks (dk_no_check);
18482
18483       /* Now, parse the body of the function.  */
18484       cp_parser_function_definition_after_declarator (parser,
18485                                                       /*inline_p=*/true);
18486
18487       if (processing_template_decl)
18488         pop_deferring_access_checks ();
18489
18490       /* Leave the scope of the containing function.  */
18491       if (function_scope)
18492         pop_function_context ();
18493       cp_parser_pop_lexer (parser);
18494     }
18495
18496   /* Remove any template parameters from the symbol table.  */
18497   maybe_end_member_template_processing ();
18498
18499   /* Restore the queue.  */
18500   parser->unparsed_functions_queues
18501     = TREE_CHAIN (parser->unparsed_functions_queues);
18502 }
18503
18504 /* If DECL contains any default args, remember it on the unparsed
18505    functions queue.  */
18506
18507 static void
18508 cp_parser_save_default_args (cp_parser* parser, tree decl)
18509 {
18510   tree probe;
18511
18512   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18513        probe;
18514        probe = TREE_CHAIN (probe))
18515     if (TREE_PURPOSE (probe))
18516       {
18517         TREE_PURPOSE (parser->unparsed_functions_queues)
18518           = tree_cons (current_class_type, decl,
18519                        TREE_PURPOSE (parser->unparsed_functions_queues));
18520         break;
18521       }
18522 }
18523
18524 /* FN is a FUNCTION_DECL which may contains a parameter with an
18525    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18526    assumes that the current scope is the scope in which the default
18527    argument should be processed.  */
18528
18529 static void
18530 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18531 {
18532   bool saved_local_variables_forbidden_p;
18533   tree parm;
18534
18535   /* While we're parsing the default args, we might (due to the
18536      statement expression extension) encounter more classes.  We want
18537      to handle them right away, but we don't want them getting mixed
18538      up with default args that are currently in the queue.  */
18539   parser->unparsed_functions_queues
18540     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18541
18542   /* Local variable names (and the `this' keyword) may not appear
18543      in a default argument.  */
18544   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18545   parser->local_variables_forbidden_p = true;
18546
18547   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18548        parm;
18549        parm = TREE_CHAIN (parm))
18550     {
18551       cp_token_cache *tokens;
18552       tree default_arg = TREE_PURPOSE (parm);
18553       tree parsed_arg;
18554       VEC(tree,gc) *insts;
18555       tree copy;
18556       unsigned ix;
18557
18558       if (!default_arg)
18559         continue;
18560
18561       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18562         /* This can happen for a friend declaration for a function
18563            already declared with default arguments.  */
18564         continue;
18565
18566        /* Push the saved tokens for the default argument onto the parser's
18567           lexer stack.  */
18568       tokens = DEFARG_TOKENS (default_arg);
18569       cp_parser_push_lexer_for_tokens (parser, tokens);
18570
18571       /* Parse the assignment-expression.  */
18572       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18573       if (parsed_arg == error_mark_node)
18574         {
18575           cp_parser_pop_lexer (parser);
18576           continue;
18577         }
18578
18579       if (!processing_template_decl)
18580         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18581
18582       TREE_PURPOSE (parm) = parsed_arg;
18583
18584       /* Update any instantiations we've already created.  */
18585       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18586            VEC_iterate (tree, insts, ix, copy); ix++)
18587         TREE_PURPOSE (copy) = parsed_arg;
18588
18589       /* If the token stream has not been completely used up, then
18590          there was extra junk after the end of the default
18591          argument.  */
18592       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18593         cp_parser_error (parser, "expected %<,%>");
18594
18595       /* Revert to the main lexer.  */
18596       cp_parser_pop_lexer (parser);
18597     }
18598
18599   /* Make sure no default arg is missing.  */
18600   check_default_args (fn);
18601
18602   /* Restore the state of local_variables_forbidden_p.  */
18603   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18604
18605   /* Restore the queue.  */
18606   parser->unparsed_functions_queues
18607     = TREE_CHAIN (parser->unparsed_functions_queues);
18608 }
18609
18610 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18611    either a TYPE or an expression, depending on the form of the
18612    input.  The KEYWORD indicates which kind of expression we have
18613    encountered.  */
18614
18615 static tree
18616 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18617 {
18618   tree expr = NULL_TREE;
18619   const char *saved_message;
18620   char *tmp;
18621   bool saved_integral_constant_expression_p;
18622   bool saved_non_integral_constant_expression_p;
18623   bool pack_expansion_p = false;
18624
18625   /* Types cannot be defined in a `sizeof' expression.  Save away the
18626      old message.  */
18627   saved_message = parser->type_definition_forbidden_message;
18628   /* And create the new one.  */
18629   tmp = concat ("types may not be defined in %<",
18630                 IDENTIFIER_POINTER (ridpointers[keyword]),
18631                 "%> expressions", NULL);
18632   parser->type_definition_forbidden_message = tmp;
18633
18634   /* The restrictions on constant-expressions do not apply inside
18635      sizeof expressions.  */
18636   saved_integral_constant_expression_p
18637     = parser->integral_constant_expression_p;
18638   saved_non_integral_constant_expression_p
18639     = parser->non_integral_constant_expression_p;
18640   parser->integral_constant_expression_p = false;
18641
18642   /* If it's a `...', then we are computing the length of a parameter
18643      pack.  */
18644   if (keyword == RID_SIZEOF
18645       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18646     {
18647       /* Consume the `...'.  */
18648       cp_lexer_consume_token (parser->lexer);
18649       maybe_warn_variadic_templates ();
18650
18651       /* Note that this is an expansion.  */
18652       pack_expansion_p = true;
18653     }
18654
18655   /* Do not actually evaluate the expression.  */
18656   ++cp_unevaluated_operand;
18657   ++c_inhibit_evaluation_warnings;
18658   /* If it's a `(', then we might be looking at the type-id
18659      construction.  */
18660   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18661     {
18662       tree type;
18663       bool saved_in_type_id_in_expr_p;
18664
18665       /* We can't be sure yet whether we're looking at a type-id or an
18666          expression.  */
18667       cp_parser_parse_tentatively (parser);
18668       /* Consume the `('.  */
18669       cp_lexer_consume_token (parser->lexer);
18670       /* Parse the type-id.  */
18671       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18672       parser->in_type_id_in_expr_p = true;
18673       type = cp_parser_type_id (parser);
18674       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18675       /* Now, look for the trailing `)'.  */
18676       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18677       /* If all went well, then we're done.  */
18678       if (cp_parser_parse_definitely (parser))
18679         {
18680           cp_decl_specifier_seq decl_specs;
18681
18682           /* Build a trivial decl-specifier-seq.  */
18683           clear_decl_specs (&decl_specs);
18684           decl_specs.type = type;
18685
18686           /* Call grokdeclarator to figure out what type this is.  */
18687           expr = grokdeclarator (NULL,
18688                                  &decl_specs,
18689                                  TYPENAME,
18690                                  /*initialized=*/0,
18691                                  /*attrlist=*/NULL);
18692         }
18693     }
18694
18695   /* If the type-id production did not work out, then we must be
18696      looking at the unary-expression production.  */
18697   if (!expr)
18698     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18699                                        /*cast_p=*/false, NULL);
18700
18701   if (pack_expansion_p)
18702     /* Build a pack expansion. */
18703     expr = make_pack_expansion (expr);
18704
18705   /* Go back to evaluating expressions.  */
18706   --cp_unevaluated_operand;
18707   --c_inhibit_evaluation_warnings;
18708
18709   /* Free the message we created.  */
18710   free (tmp);
18711   /* And restore the old one.  */
18712   parser->type_definition_forbidden_message = saved_message;
18713   parser->integral_constant_expression_p
18714     = saved_integral_constant_expression_p;
18715   parser->non_integral_constant_expression_p
18716     = saved_non_integral_constant_expression_p;
18717
18718   return expr;
18719 }
18720
18721 /* If the current declaration has no declarator, return true.  */
18722
18723 static bool
18724 cp_parser_declares_only_class_p (cp_parser *parser)
18725 {
18726   /* If the next token is a `;' or a `,' then there is no
18727      declarator.  */
18728   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18729           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18730 }
18731
18732 /* Update the DECL_SPECS to reflect the storage class indicated by
18733    KEYWORD.  */
18734
18735 static void
18736 cp_parser_set_storage_class (cp_parser *parser,
18737                              cp_decl_specifier_seq *decl_specs,
18738                              enum rid keyword,
18739                              location_t location)
18740 {
18741   cp_storage_class storage_class;
18742
18743   if (parser->in_unbraced_linkage_specification_p)
18744     {
18745       error_at (location, "invalid use of %qD in linkage specification",
18746                 ridpointers[keyword]);
18747       return;
18748     }
18749   else if (decl_specs->storage_class != sc_none)
18750     {
18751       decl_specs->conflicting_specifiers_p = true;
18752       return;
18753     }
18754
18755   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18756       && decl_specs->specs[(int) ds_thread])
18757     {
18758       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
18759       decl_specs->specs[(int) ds_thread] = 0;
18760     }
18761
18762   switch (keyword)
18763     {
18764     case RID_AUTO:
18765       storage_class = sc_auto;
18766       break;
18767     case RID_REGISTER:
18768       storage_class = sc_register;
18769       break;
18770     case RID_STATIC:
18771       storage_class = sc_static;
18772       break;
18773     case RID_EXTERN:
18774       storage_class = sc_extern;
18775       break;
18776     case RID_MUTABLE:
18777       storage_class = sc_mutable;
18778       break;
18779     default:
18780       gcc_unreachable ();
18781     }
18782   decl_specs->storage_class = storage_class;
18783
18784   /* A storage class specifier cannot be applied alongside a typedef 
18785      specifier. If there is a typedef specifier present then set 
18786      conflicting_specifiers_p which will trigger an error later
18787      on in grokdeclarator. */
18788   if (decl_specs->specs[(int)ds_typedef])
18789     decl_specs->conflicting_specifiers_p = true;
18790 }
18791
18792 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18793    is true, the type is a user-defined type; otherwise it is a
18794    built-in type specified by a keyword.  */
18795
18796 static void
18797 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18798                               tree type_spec,
18799                               location_t location,
18800                               bool user_defined_p)
18801 {
18802   decl_specs->any_specifiers_p = true;
18803
18804   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18805      (with, for example, in "typedef int wchar_t;") we remember that
18806      this is what happened.  In system headers, we ignore these
18807      declarations so that G++ can work with system headers that are not
18808      C++-safe.  */
18809   if (decl_specs->specs[(int) ds_typedef]
18810       && !user_defined_p
18811       && (type_spec == boolean_type_node
18812           || type_spec == char16_type_node
18813           || type_spec == char32_type_node
18814           || type_spec == wchar_type_node)
18815       && (decl_specs->type
18816           || decl_specs->specs[(int) ds_long]
18817           || decl_specs->specs[(int) ds_short]
18818           || decl_specs->specs[(int) ds_unsigned]
18819           || decl_specs->specs[(int) ds_signed]))
18820     {
18821       decl_specs->redefined_builtin_type = type_spec;
18822       if (!decl_specs->type)
18823         {
18824           decl_specs->type = type_spec;
18825           decl_specs->user_defined_type_p = false;
18826           decl_specs->type_location = location;
18827         }
18828     }
18829   else if (decl_specs->type)
18830     decl_specs->multiple_types_p = true;
18831   else
18832     {
18833       decl_specs->type = type_spec;
18834       decl_specs->user_defined_type_p = user_defined_p;
18835       decl_specs->redefined_builtin_type = NULL_TREE;
18836       decl_specs->type_location = location;
18837     }
18838 }
18839
18840 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18841    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18842
18843 static bool
18844 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18845 {
18846   return decl_specifiers->specs[(int) ds_friend] != 0;
18847 }
18848
18849 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18850    issue an error message indicating that TOKEN_DESC was expected.
18851
18852    Returns the token consumed, if the token had the appropriate type.
18853    Otherwise, returns NULL.  */
18854
18855 static cp_token *
18856 cp_parser_require (cp_parser* parser,
18857                    enum cpp_ttype type,
18858                    const char* token_desc)
18859 {
18860   if (cp_lexer_next_token_is (parser->lexer, type))
18861     return cp_lexer_consume_token (parser->lexer);
18862   else
18863     {
18864       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18865       if (!cp_parser_simulate_error (parser))
18866         {
18867           char *message = concat ("expected ", token_desc, NULL);
18868           cp_parser_error (parser, message);
18869           free (message);
18870         }
18871       return NULL;
18872     }
18873 }
18874
18875 /* An error message is produced if the next token is not '>'.
18876    All further tokens are skipped until the desired token is
18877    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18878
18879 static void
18880 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18881 {
18882   /* Current level of '< ... >'.  */
18883   unsigned level = 0;
18884   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18885   unsigned nesting_depth = 0;
18886
18887   /* Are we ready, yet?  If not, issue error message.  */
18888   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18889     return;
18890
18891   /* Skip tokens until the desired token is found.  */
18892   while (true)
18893     {
18894       /* Peek at the next token.  */
18895       switch (cp_lexer_peek_token (parser->lexer)->type)
18896         {
18897         case CPP_LESS:
18898           if (!nesting_depth)
18899             ++level;
18900           break;
18901
18902         case CPP_RSHIFT:
18903           if (cxx_dialect == cxx98)
18904             /* C++0x views the `>>' operator as two `>' tokens, but
18905                C++98 does not. */
18906             break;
18907           else if (!nesting_depth && level-- == 0)
18908             {
18909               /* We've hit a `>>' where the first `>' closes the
18910                  template argument list, and the second `>' is
18911                  spurious.  Just consume the `>>' and stop; we've
18912                  already produced at least one error.  */
18913               cp_lexer_consume_token (parser->lexer);
18914               return;
18915             }
18916           /* Fall through for C++0x, so we handle the second `>' in
18917              the `>>'.  */
18918
18919         case CPP_GREATER:
18920           if (!nesting_depth && level-- == 0)
18921             {
18922               /* We've reached the token we want, consume it and stop.  */
18923               cp_lexer_consume_token (parser->lexer);
18924               return;
18925             }
18926           break;
18927
18928         case CPP_OPEN_PAREN:
18929         case CPP_OPEN_SQUARE:
18930           ++nesting_depth;
18931           break;
18932
18933         case CPP_CLOSE_PAREN:
18934         case CPP_CLOSE_SQUARE:
18935           if (nesting_depth-- == 0)
18936             return;
18937           break;
18938
18939         case CPP_EOF:
18940         case CPP_PRAGMA_EOL:
18941         case CPP_SEMICOLON:
18942         case CPP_OPEN_BRACE:
18943         case CPP_CLOSE_BRACE:
18944           /* The '>' was probably forgotten, don't look further.  */
18945           return;
18946
18947         default:
18948           break;
18949         }
18950
18951       /* Consume this token.  */
18952       cp_lexer_consume_token (parser->lexer);
18953     }
18954 }
18955
18956 /* If the next token is the indicated keyword, consume it.  Otherwise,
18957    issue an error message indicating that TOKEN_DESC was expected.
18958
18959    Returns the token consumed, if the token had the appropriate type.
18960    Otherwise, returns NULL.  */
18961
18962 static cp_token *
18963 cp_parser_require_keyword (cp_parser* parser,
18964                            enum rid keyword,
18965                            const char* token_desc)
18966 {
18967   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18968
18969   if (token && token->keyword != keyword)
18970     {
18971       dyn_string_t error_msg;
18972
18973       /* Format the error message.  */
18974       error_msg = dyn_string_new (0);
18975       dyn_string_append_cstr (error_msg, "expected ");
18976       dyn_string_append_cstr (error_msg, token_desc);
18977       cp_parser_error (parser, error_msg->s);
18978       dyn_string_delete (error_msg);
18979       return NULL;
18980     }
18981
18982   return token;
18983 }
18984
18985 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18986    function-definition.  */
18987
18988 static bool
18989 cp_parser_token_starts_function_definition_p (cp_token* token)
18990 {
18991   return (/* An ordinary function-body begins with an `{'.  */
18992           token->type == CPP_OPEN_BRACE
18993           /* A ctor-initializer begins with a `:'.  */
18994           || token->type == CPP_COLON
18995           /* A function-try-block begins with `try'.  */
18996           || token->keyword == RID_TRY
18997           /* The named return value extension begins with `return'.  */
18998           || token->keyword == RID_RETURN);
18999 }
19000
19001 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19002    definition.  */
19003
19004 static bool
19005 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19006 {
19007   cp_token *token;
19008
19009   token = cp_lexer_peek_token (parser->lexer);
19010   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19011 }
19012
19013 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19014    C++0x) ending a template-argument.  */
19015
19016 static bool
19017 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19018 {
19019   cp_token *token;
19020
19021   token = cp_lexer_peek_token (parser->lexer);
19022   return (token->type == CPP_COMMA 
19023           || token->type == CPP_GREATER
19024           || token->type == CPP_ELLIPSIS
19025           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19026 }
19027
19028 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19029    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19030
19031 static bool
19032 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19033                                                      size_t n)
19034 {
19035   cp_token *token;
19036
19037   token = cp_lexer_peek_nth_token (parser->lexer, n);
19038   if (token->type == CPP_LESS)
19039     return true;
19040   /* Check for the sequence `<::' in the original code. It would be lexed as
19041      `[:', where `[' is a digraph, and there is no whitespace before
19042      `:'.  */
19043   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19044     {
19045       cp_token *token2;
19046       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19047       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19048         return true;
19049     }
19050   return false;
19051 }
19052
19053 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19054    or none_type otherwise.  */
19055
19056 static enum tag_types
19057 cp_parser_token_is_class_key (cp_token* token)
19058 {
19059   switch (token->keyword)
19060     {
19061     case RID_CLASS:
19062       return class_type;
19063     case RID_STRUCT:
19064       return record_type;
19065     case RID_UNION:
19066       return union_type;
19067
19068     default:
19069       return none_type;
19070     }
19071 }
19072
19073 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19074
19075 static void
19076 cp_parser_check_class_key (enum tag_types class_key, tree type)
19077 {
19078   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19079     permerror (input_location, "%qs tag used in naming %q#T",
19080             class_key == union_type ? "union"
19081              : class_key == record_type ? "struct" : "class",
19082              type);
19083 }
19084
19085 /* Issue an error message if DECL is redeclared with different
19086    access than its original declaration [class.access.spec/3].
19087    This applies to nested classes and nested class templates.
19088    [class.mem/1].  */
19089
19090 static void
19091 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19092 {
19093   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19094     return;
19095
19096   if ((TREE_PRIVATE (decl)
19097        != (current_access_specifier == access_private_node))
19098       || (TREE_PROTECTED (decl)
19099           != (current_access_specifier == access_protected_node)))
19100     error_at (location, "%qD redeclared with different access", decl);
19101 }
19102
19103 /* Look for the `template' keyword, as a syntactic disambiguator.
19104    Return TRUE iff it is present, in which case it will be
19105    consumed.  */
19106
19107 static bool
19108 cp_parser_optional_template_keyword (cp_parser *parser)
19109 {
19110   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19111     {
19112       /* The `template' keyword can only be used within templates;
19113          outside templates the parser can always figure out what is a
19114          template and what is not.  */
19115       if (!processing_template_decl)
19116         {
19117           cp_token *token = cp_lexer_peek_token (parser->lexer);
19118           error_at (token->location,
19119                     "%<template%> (as a disambiguator) is only allowed "
19120                     "within templates");
19121           /* If this part of the token stream is rescanned, the same
19122              error message would be generated.  So, we purge the token
19123              from the stream.  */
19124           cp_lexer_purge_token (parser->lexer);
19125           return false;
19126         }
19127       else
19128         {
19129           /* Consume the `template' keyword.  */
19130           cp_lexer_consume_token (parser->lexer);
19131           return true;
19132         }
19133     }
19134
19135   return false;
19136 }
19137
19138 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19139    set PARSER->SCOPE, and perform other related actions.  */
19140
19141 static void
19142 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19143 {
19144   int i;
19145   struct tree_check *check_value;
19146   deferred_access_check *chk;
19147   VEC (deferred_access_check,gc) *checks;
19148
19149   /* Get the stored value.  */
19150   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19151   /* Perform any access checks that were deferred.  */
19152   checks = check_value->checks;
19153   if (checks)
19154     {
19155       for (i = 0 ;
19156            VEC_iterate (deferred_access_check, checks, i, chk) ;
19157            ++i)
19158         {
19159           perform_or_defer_access_check (chk->binfo,
19160                                          chk->decl,
19161                                          chk->diag_decl);
19162         }
19163     }
19164   /* Set the scope from the stored value.  */
19165   parser->scope = check_value->value;
19166   parser->qualifying_scope = check_value->qualifying_scope;
19167   parser->object_scope = NULL_TREE;
19168 }
19169
19170 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19171    encounter the end of a block before what we were looking for.  */
19172
19173 static bool
19174 cp_parser_cache_group (cp_parser *parser,
19175                        enum cpp_ttype end,
19176                        unsigned depth)
19177 {
19178   while (true)
19179     {
19180       cp_token *token = cp_lexer_peek_token (parser->lexer);
19181
19182       /* Abort a parenthesized expression if we encounter a semicolon.  */
19183       if ((end == CPP_CLOSE_PAREN || depth == 0)
19184           && token->type == CPP_SEMICOLON)
19185         return true;
19186       /* If we've reached the end of the file, stop.  */
19187       if (token->type == CPP_EOF
19188           || (end != CPP_PRAGMA_EOL
19189               && token->type == CPP_PRAGMA_EOL))
19190         return true;
19191       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19192         /* We've hit the end of an enclosing block, so there's been some
19193            kind of syntax error.  */
19194         return true;
19195
19196       /* Consume the token.  */
19197       cp_lexer_consume_token (parser->lexer);
19198       /* See if it starts a new group.  */
19199       if (token->type == CPP_OPEN_BRACE)
19200         {
19201           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19202           /* In theory this should probably check end == '}', but
19203              cp_parser_save_member_function_body needs it to exit
19204              after either '}' or ')' when called with ')'.  */
19205           if (depth == 0)
19206             return false;
19207         }
19208       else if (token->type == CPP_OPEN_PAREN)
19209         {
19210           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19211           if (depth == 0 && end == CPP_CLOSE_PAREN)
19212             return false;
19213         }
19214       else if (token->type == CPP_PRAGMA)
19215         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19216       else if (token->type == end)
19217         return false;
19218     }
19219 }
19220
19221 /* Begin parsing tentatively.  We always save tokens while parsing
19222    tentatively so that if the tentative parsing fails we can restore the
19223    tokens.  */
19224
19225 static void
19226 cp_parser_parse_tentatively (cp_parser* parser)
19227 {
19228   /* Enter a new parsing context.  */
19229   parser->context = cp_parser_context_new (parser->context);
19230   /* Begin saving tokens.  */
19231   cp_lexer_save_tokens (parser->lexer);
19232   /* In order to avoid repetitive access control error messages,
19233      access checks are queued up until we are no longer parsing
19234      tentatively.  */
19235   push_deferring_access_checks (dk_deferred);
19236 }
19237
19238 /* Commit to the currently active tentative parse.  */
19239
19240 static void
19241 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19242 {
19243   cp_parser_context *context;
19244   cp_lexer *lexer;
19245
19246   /* Mark all of the levels as committed.  */
19247   lexer = parser->lexer;
19248   for (context = parser->context; context->next; context = context->next)
19249     {
19250       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19251         break;
19252       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19253       while (!cp_lexer_saving_tokens (lexer))
19254         lexer = lexer->next;
19255       cp_lexer_commit_tokens (lexer);
19256     }
19257 }
19258
19259 /* Abort the currently active tentative parse.  All consumed tokens
19260    will be rolled back, and no diagnostics will be issued.  */
19261
19262 static void
19263 cp_parser_abort_tentative_parse (cp_parser* parser)
19264 {
19265   cp_parser_simulate_error (parser);
19266   /* Now, pretend that we want to see if the construct was
19267      successfully parsed.  */
19268   cp_parser_parse_definitely (parser);
19269 }
19270
19271 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19272    token stream.  Otherwise, commit to the tokens we have consumed.
19273    Returns true if no error occurred; false otherwise.  */
19274
19275 static bool
19276 cp_parser_parse_definitely (cp_parser* parser)
19277 {
19278   bool error_occurred;
19279   cp_parser_context *context;
19280
19281   /* Remember whether or not an error occurred, since we are about to
19282      destroy that information.  */
19283   error_occurred = cp_parser_error_occurred (parser);
19284   /* Remove the topmost context from the stack.  */
19285   context = parser->context;
19286   parser->context = context->next;
19287   /* If no parse errors occurred, commit to the tentative parse.  */
19288   if (!error_occurred)
19289     {
19290       /* Commit to the tokens read tentatively, unless that was
19291          already done.  */
19292       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19293         cp_lexer_commit_tokens (parser->lexer);
19294
19295       pop_to_parent_deferring_access_checks ();
19296     }
19297   /* Otherwise, if errors occurred, roll back our state so that things
19298      are just as they were before we began the tentative parse.  */
19299   else
19300     {
19301       cp_lexer_rollback_tokens (parser->lexer);
19302       pop_deferring_access_checks ();
19303     }
19304   /* Add the context to the front of the free list.  */
19305   context->next = cp_parser_context_free_list;
19306   cp_parser_context_free_list = context;
19307
19308   return !error_occurred;
19309 }
19310
19311 /* Returns true if we are parsing tentatively and are not committed to
19312    this tentative parse.  */
19313
19314 static bool
19315 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19316 {
19317   return (cp_parser_parsing_tentatively (parser)
19318           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19319 }
19320
19321 /* Returns nonzero iff an error has occurred during the most recent
19322    tentative parse.  */
19323
19324 static bool
19325 cp_parser_error_occurred (cp_parser* parser)
19326 {
19327   return (cp_parser_parsing_tentatively (parser)
19328           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19329 }
19330
19331 /* Returns nonzero if GNU extensions are allowed.  */
19332
19333 static bool
19334 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19335 {
19336   return parser->allow_gnu_extensions_p;
19337 }
19338 \f
19339 /* Objective-C++ Productions */
19340
19341
19342 /* Parse an Objective-C expression, which feeds into a primary-expression
19343    above.
19344
19345    objc-expression:
19346      objc-message-expression
19347      objc-string-literal
19348      objc-encode-expression
19349      objc-protocol-expression
19350      objc-selector-expression
19351
19352   Returns a tree representation of the expression.  */
19353
19354 static tree
19355 cp_parser_objc_expression (cp_parser* parser)
19356 {
19357   /* Try to figure out what kind of declaration is present.  */
19358   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19359
19360   switch (kwd->type)
19361     {
19362     case CPP_OPEN_SQUARE:
19363       return cp_parser_objc_message_expression (parser);
19364
19365     case CPP_OBJC_STRING:
19366       kwd = cp_lexer_consume_token (parser->lexer);
19367       return objc_build_string_object (kwd->u.value);
19368
19369     case CPP_KEYWORD:
19370       switch (kwd->keyword)
19371         {
19372         case RID_AT_ENCODE:
19373           return cp_parser_objc_encode_expression (parser);
19374
19375         case RID_AT_PROTOCOL:
19376           return cp_parser_objc_protocol_expression (parser);
19377
19378         case RID_AT_SELECTOR:
19379           return cp_parser_objc_selector_expression (parser);
19380
19381         default:
19382           break;
19383         }
19384     default:
19385       error_at (kwd->location,
19386                 "misplaced %<@%D%> Objective-C++ construct",
19387                 kwd->u.value);
19388       cp_parser_skip_to_end_of_block_or_statement (parser);
19389     }
19390
19391   return error_mark_node;
19392 }
19393
19394 /* Parse an Objective-C message expression.
19395
19396    objc-message-expression:
19397      [ objc-message-receiver objc-message-args ]
19398
19399    Returns a representation of an Objective-C message.  */
19400
19401 static tree
19402 cp_parser_objc_message_expression (cp_parser* parser)
19403 {
19404   tree receiver, messageargs;
19405
19406   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19407   receiver = cp_parser_objc_message_receiver (parser);
19408   messageargs = cp_parser_objc_message_args (parser);
19409   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19410
19411   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19412 }
19413
19414 /* Parse an objc-message-receiver.
19415
19416    objc-message-receiver:
19417      expression
19418      simple-type-specifier
19419
19420   Returns a representation of the type or expression.  */
19421
19422 static tree
19423 cp_parser_objc_message_receiver (cp_parser* parser)
19424 {
19425   tree rcv;
19426
19427   /* An Objective-C message receiver may be either (1) a type
19428      or (2) an expression.  */
19429   cp_parser_parse_tentatively (parser);
19430   rcv = cp_parser_expression (parser, false, NULL);
19431
19432   if (cp_parser_parse_definitely (parser))
19433     return rcv;
19434
19435   rcv = cp_parser_simple_type_specifier (parser,
19436                                          /*decl_specs=*/NULL,
19437                                          CP_PARSER_FLAGS_NONE);
19438
19439   return objc_get_class_reference (rcv);
19440 }
19441
19442 /* Parse the arguments and selectors comprising an Objective-C message.
19443
19444    objc-message-args:
19445      objc-selector
19446      objc-selector-args
19447      objc-selector-args , objc-comma-args
19448
19449    objc-selector-args:
19450      objc-selector [opt] : assignment-expression
19451      objc-selector-args objc-selector [opt] : assignment-expression
19452
19453    objc-comma-args:
19454      assignment-expression
19455      objc-comma-args , assignment-expression
19456
19457    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19458    selector arguments and TREE_VALUE containing a list of comma
19459    arguments.  */
19460
19461 static tree
19462 cp_parser_objc_message_args (cp_parser* parser)
19463 {
19464   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19465   bool maybe_unary_selector_p = true;
19466   cp_token *token = cp_lexer_peek_token (parser->lexer);
19467
19468   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19469     {
19470       tree selector = NULL_TREE, arg;
19471
19472       if (token->type != CPP_COLON)
19473         selector = cp_parser_objc_selector (parser);
19474
19475       /* Detect if we have a unary selector.  */
19476       if (maybe_unary_selector_p
19477           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19478         return build_tree_list (selector, NULL_TREE);
19479
19480       maybe_unary_selector_p = false;
19481       cp_parser_require (parser, CPP_COLON, "%<:%>");
19482       arg = cp_parser_assignment_expression (parser, false, NULL);
19483
19484       sel_args
19485         = chainon (sel_args,
19486                    build_tree_list (selector, arg));
19487
19488       token = cp_lexer_peek_token (parser->lexer);
19489     }
19490
19491   /* Handle non-selector arguments, if any. */
19492   while (token->type == CPP_COMMA)
19493     {
19494       tree arg;
19495
19496       cp_lexer_consume_token (parser->lexer);
19497       arg = cp_parser_assignment_expression (parser, false, NULL);
19498
19499       addl_args
19500         = chainon (addl_args,
19501                    build_tree_list (NULL_TREE, arg));
19502
19503       token = cp_lexer_peek_token (parser->lexer);
19504     }
19505
19506   return build_tree_list (sel_args, addl_args);
19507 }
19508
19509 /* Parse an Objective-C encode expression.
19510
19511    objc-encode-expression:
19512      @encode objc-typename
19513
19514    Returns an encoded representation of the type argument.  */
19515
19516 static tree
19517 cp_parser_objc_encode_expression (cp_parser* parser)
19518 {
19519   tree type;
19520   cp_token *token;
19521
19522   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19523   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19524   token = cp_lexer_peek_token (parser->lexer);
19525   type = complete_type (cp_parser_type_id (parser));
19526   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19527
19528   if (!type)
19529     {
19530       error_at (token->location, 
19531                 "%<@encode%> must specify a type as an argument");
19532       return error_mark_node;
19533     }
19534
19535   return objc_build_encode_expr (type);
19536 }
19537
19538 /* Parse an Objective-C @defs expression.  */
19539
19540 static tree
19541 cp_parser_objc_defs_expression (cp_parser *parser)
19542 {
19543   tree name;
19544
19545   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19546   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19547   name = cp_parser_identifier (parser);
19548   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19549
19550   return objc_get_class_ivars (name);
19551 }
19552
19553 /* Parse an Objective-C protocol expression.
19554
19555   objc-protocol-expression:
19556     @protocol ( identifier )
19557
19558   Returns a representation of the protocol expression.  */
19559
19560 static tree
19561 cp_parser_objc_protocol_expression (cp_parser* parser)
19562 {
19563   tree proto;
19564
19565   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19566   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19567   proto = cp_parser_identifier (parser);
19568   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19569
19570   return objc_build_protocol_expr (proto);
19571 }
19572
19573 /* Parse an Objective-C selector expression.
19574
19575    objc-selector-expression:
19576      @selector ( objc-method-signature )
19577
19578    objc-method-signature:
19579      objc-selector
19580      objc-selector-seq
19581
19582    objc-selector-seq:
19583      objc-selector :
19584      objc-selector-seq objc-selector :
19585
19586   Returns a representation of the method selector.  */
19587
19588 static tree
19589 cp_parser_objc_selector_expression (cp_parser* parser)
19590 {
19591   tree sel_seq = NULL_TREE;
19592   bool maybe_unary_selector_p = true;
19593   cp_token *token;
19594   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19595
19596   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19597   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19598   token = cp_lexer_peek_token (parser->lexer);
19599
19600   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19601          || token->type == CPP_SCOPE)
19602     {
19603       tree selector = NULL_TREE;
19604
19605       if (token->type != CPP_COLON
19606           || token->type == CPP_SCOPE)
19607         selector = cp_parser_objc_selector (parser);
19608
19609       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19610           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19611         {
19612           /* Detect if we have a unary selector.  */
19613           if (maybe_unary_selector_p)
19614             {
19615               sel_seq = selector;
19616               goto finish_selector;
19617             }
19618           else
19619             {
19620               cp_parser_error (parser, "expected %<:%>");
19621             }
19622         }
19623       maybe_unary_selector_p = false;
19624       token = cp_lexer_consume_token (parser->lexer);
19625
19626       if (token->type == CPP_SCOPE)
19627         {
19628           sel_seq
19629             = chainon (sel_seq,
19630                        build_tree_list (selector, NULL_TREE));
19631           sel_seq
19632             = chainon (sel_seq,
19633                        build_tree_list (NULL_TREE, NULL_TREE));
19634         }
19635       else
19636         sel_seq
19637           = chainon (sel_seq,
19638                      build_tree_list (selector, NULL_TREE));
19639
19640       token = cp_lexer_peek_token (parser->lexer);
19641     }
19642
19643  finish_selector:
19644   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19645
19646   return objc_build_selector_expr (loc, sel_seq);
19647 }
19648
19649 /* Parse a list of identifiers.
19650
19651    objc-identifier-list:
19652      identifier
19653      objc-identifier-list , identifier
19654
19655    Returns a TREE_LIST of identifier nodes.  */
19656
19657 static tree
19658 cp_parser_objc_identifier_list (cp_parser* parser)
19659 {
19660   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19661   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19662
19663   while (sep->type == CPP_COMMA)
19664     {
19665       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19666       list = chainon (list,
19667                       build_tree_list (NULL_TREE,
19668                                        cp_parser_identifier (parser)));
19669       sep = cp_lexer_peek_token (parser->lexer);
19670     }
19671
19672   return list;
19673 }
19674
19675 /* Parse an Objective-C alias declaration.
19676
19677    objc-alias-declaration:
19678      @compatibility_alias identifier identifier ;
19679
19680    This function registers the alias mapping with the Objective-C front end.
19681    It returns nothing.  */
19682
19683 static void
19684 cp_parser_objc_alias_declaration (cp_parser* parser)
19685 {
19686   tree alias, orig;
19687
19688   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19689   alias = cp_parser_identifier (parser);
19690   orig = cp_parser_identifier (parser);
19691   objc_declare_alias (alias, orig);
19692   cp_parser_consume_semicolon_at_end_of_statement (parser);
19693 }
19694
19695 /* Parse an Objective-C class forward-declaration.
19696
19697    objc-class-declaration:
19698      @class objc-identifier-list ;
19699
19700    The function registers the forward declarations with the Objective-C
19701    front end.  It returns nothing.  */
19702
19703 static void
19704 cp_parser_objc_class_declaration (cp_parser* parser)
19705 {
19706   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19707   objc_declare_class (cp_parser_objc_identifier_list (parser));
19708   cp_parser_consume_semicolon_at_end_of_statement (parser);
19709 }
19710
19711 /* Parse a list of Objective-C protocol references.
19712
19713    objc-protocol-refs-opt:
19714      objc-protocol-refs [opt]
19715
19716    objc-protocol-refs:
19717      < objc-identifier-list >
19718
19719    Returns a TREE_LIST of identifiers, if any.  */
19720
19721 static tree
19722 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19723 {
19724   tree protorefs = NULL_TREE;
19725
19726   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19727     {
19728       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19729       protorefs = cp_parser_objc_identifier_list (parser);
19730       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19731     }
19732
19733   return protorefs;
19734 }
19735
19736 /* Parse a Objective-C visibility specification.  */
19737
19738 static void
19739 cp_parser_objc_visibility_spec (cp_parser* parser)
19740 {
19741   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19742
19743   switch (vis->keyword)
19744     {
19745     case RID_AT_PRIVATE:
19746       objc_set_visibility (2);
19747       break;
19748     case RID_AT_PROTECTED:
19749       objc_set_visibility (0);
19750       break;
19751     case RID_AT_PUBLIC:
19752       objc_set_visibility (1);
19753       break;
19754     default:
19755       return;
19756     }
19757
19758   /* Eat '@private'/'@protected'/'@public'.  */
19759   cp_lexer_consume_token (parser->lexer);
19760 }
19761
19762 /* Parse an Objective-C method type.  */
19763
19764 static void
19765 cp_parser_objc_method_type (cp_parser* parser)
19766 {
19767   objc_set_method_type
19768    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19769     ? PLUS_EXPR
19770     : MINUS_EXPR);
19771 }
19772
19773 /* Parse an Objective-C protocol qualifier.  */
19774
19775 static tree
19776 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19777 {
19778   tree quals = NULL_TREE, node;
19779   cp_token *token = cp_lexer_peek_token (parser->lexer);
19780
19781   node = token->u.value;
19782
19783   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19784          && (node == ridpointers [(int) RID_IN]
19785              || node == ridpointers [(int) RID_OUT]
19786              || node == ridpointers [(int) RID_INOUT]
19787              || node == ridpointers [(int) RID_BYCOPY]
19788              || node == ridpointers [(int) RID_BYREF]
19789              || node == ridpointers [(int) RID_ONEWAY]))
19790     {
19791       quals = tree_cons (NULL_TREE, node, quals);
19792       cp_lexer_consume_token (parser->lexer);
19793       token = cp_lexer_peek_token (parser->lexer);
19794       node = token->u.value;
19795     }
19796
19797   return quals;
19798 }
19799
19800 /* Parse an Objective-C typename.  */
19801
19802 static tree
19803 cp_parser_objc_typename (cp_parser* parser)
19804 {
19805   tree type_name = NULL_TREE;
19806
19807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19808     {
19809       tree proto_quals, cp_type = NULL_TREE;
19810
19811       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19812       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19813
19814       /* An ObjC type name may consist of just protocol qualifiers, in which
19815          case the type shall default to 'id'.  */
19816       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19817         cp_type = cp_parser_type_id (parser);
19818
19819       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19820       type_name = build_tree_list (proto_quals, cp_type);
19821     }
19822
19823   return type_name;
19824 }
19825
19826 /* Check to see if TYPE refers to an Objective-C selector name.  */
19827
19828 static bool
19829 cp_parser_objc_selector_p (enum cpp_ttype type)
19830 {
19831   return (type == CPP_NAME || type == CPP_KEYWORD
19832           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19833           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19834           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19835           || type == CPP_XOR || type == CPP_XOR_EQ);
19836 }
19837
19838 /* Parse an Objective-C selector.  */
19839
19840 static tree
19841 cp_parser_objc_selector (cp_parser* parser)
19842 {
19843   cp_token *token = cp_lexer_consume_token (parser->lexer);
19844
19845   if (!cp_parser_objc_selector_p (token->type))
19846     {
19847       error_at (token->location, "invalid Objective-C++ selector name");
19848       return error_mark_node;
19849     }
19850
19851   /* C++ operator names are allowed to appear in ObjC selectors.  */
19852   switch (token->type)
19853     {
19854     case CPP_AND_AND: return get_identifier ("and");
19855     case CPP_AND_EQ: return get_identifier ("and_eq");
19856     case CPP_AND: return get_identifier ("bitand");
19857     case CPP_OR: return get_identifier ("bitor");
19858     case CPP_COMPL: return get_identifier ("compl");
19859     case CPP_NOT: return get_identifier ("not");
19860     case CPP_NOT_EQ: return get_identifier ("not_eq");
19861     case CPP_OR_OR: return get_identifier ("or");
19862     case CPP_OR_EQ: return get_identifier ("or_eq");
19863     case CPP_XOR: return get_identifier ("xor");
19864     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19865     default: return token->u.value;
19866     }
19867 }
19868
19869 /* Parse an Objective-C params list.  */
19870
19871 static tree
19872 cp_parser_objc_method_keyword_params (cp_parser* parser)
19873 {
19874   tree params = NULL_TREE;
19875   bool maybe_unary_selector_p = true;
19876   cp_token *token = cp_lexer_peek_token (parser->lexer);
19877
19878   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19879     {
19880       tree selector = NULL_TREE, type_name, identifier;
19881
19882       if (token->type != CPP_COLON)
19883         selector = cp_parser_objc_selector (parser);
19884
19885       /* Detect if we have a unary selector.  */
19886       if (maybe_unary_selector_p
19887           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19888         return selector;
19889
19890       maybe_unary_selector_p = false;
19891       cp_parser_require (parser, CPP_COLON, "%<:%>");
19892       type_name = cp_parser_objc_typename (parser);
19893       identifier = cp_parser_identifier (parser);
19894
19895       params
19896         = chainon (params,
19897                    objc_build_keyword_decl (selector,
19898                                             type_name,
19899                                             identifier));
19900
19901       token = cp_lexer_peek_token (parser->lexer);
19902     }
19903
19904   return params;
19905 }
19906
19907 /* Parse the non-keyword Objective-C params.  */
19908
19909 static tree
19910 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19911 {
19912   tree params = make_node (TREE_LIST);
19913   cp_token *token = cp_lexer_peek_token (parser->lexer);
19914   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19915
19916   while (token->type == CPP_COMMA)
19917     {
19918       cp_parameter_declarator *parmdecl;
19919       tree parm;
19920
19921       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19922       token = cp_lexer_peek_token (parser->lexer);
19923
19924       if (token->type == CPP_ELLIPSIS)
19925         {
19926           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19927           *ellipsisp = true;
19928           break;
19929         }
19930
19931       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19932       parm = grokdeclarator (parmdecl->declarator,
19933                              &parmdecl->decl_specifiers,
19934                              PARM, /*initialized=*/0,
19935                              /*attrlist=*/NULL);
19936
19937       chainon (params, build_tree_list (NULL_TREE, parm));
19938       token = cp_lexer_peek_token (parser->lexer);
19939     }
19940
19941   return params;
19942 }
19943
19944 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19945
19946 static void
19947 cp_parser_objc_interstitial_code (cp_parser* parser)
19948 {
19949   cp_token *token = cp_lexer_peek_token (parser->lexer);
19950
19951   /* If the next token is `extern' and the following token is a string
19952      literal, then we have a linkage specification.  */
19953   if (token->keyword == RID_EXTERN
19954       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19955     cp_parser_linkage_specification (parser);
19956   /* Handle #pragma, if any.  */
19957   else if (token->type == CPP_PRAGMA)
19958     cp_parser_pragma (parser, pragma_external);
19959   /* Allow stray semicolons.  */
19960   else if (token->type == CPP_SEMICOLON)
19961     cp_lexer_consume_token (parser->lexer);
19962   /* Finally, try to parse a block-declaration, or a function-definition.  */
19963   else
19964     cp_parser_block_declaration (parser, /*statement_p=*/false);
19965 }
19966
19967 /* Parse a method signature.  */
19968
19969 static tree
19970 cp_parser_objc_method_signature (cp_parser* parser)
19971 {
19972   tree rettype, kwdparms, optparms;
19973   bool ellipsis = false;
19974
19975   cp_parser_objc_method_type (parser);
19976   rettype = cp_parser_objc_typename (parser);
19977   kwdparms = cp_parser_objc_method_keyword_params (parser);
19978   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19979
19980   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19981 }
19982
19983 /* Pars an Objective-C method prototype list.  */
19984
19985 static void
19986 cp_parser_objc_method_prototype_list (cp_parser* parser)
19987 {
19988   cp_token *token = cp_lexer_peek_token (parser->lexer);
19989
19990   while (token->keyword != RID_AT_END)
19991     {
19992       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19993         {
19994           objc_add_method_declaration
19995            (cp_parser_objc_method_signature (parser));
19996           cp_parser_consume_semicolon_at_end_of_statement (parser);
19997         }
19998       else
19999         /* Allow for interspersed non-ObjC++ code.  */
20000         cp_parser_objc_interstitial_code (parser);
20001
20002       token = cp_lexer_peek_token (parser->lexer);
20003     }
20004
20005   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20006   objc_finish_interface ();
20007 }
20008
20009 /* Parse an Objective-C method definition list.  */
20010
20011 static void
20012 cp_parser_objc_method_definition_list (cp_parser* parser)
20013 {
20014   cp_token *token = cp_lexer_peek_token (parser->lexer);
20015
20016   while (token->keyword != RID_AT_END)
20017     {
20018       tree meth;
20019
20020       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20021         {
20022           push_deferring_access_checks (dk_deferred);
20023           objc_start_method_definition
20024            (cp_parser_objc_method_signature (parser));
20025
20026           /* For historical reasons, we accept an optional semicolon.  */
20027           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20028             cp_lexer_consume_token (parser->lexer);
20029
20030           perform_deferred_access_checks ();
20031           stop_deferring_access_checks ();
20032           meth = cp_parser_function_definition_after_declarator (parser,
20033                                                                  false);
20034           pop_deferring_access_checks ();
20035           objc_finish_method_definition (meth);
20036         }
20037       else
20038         /* Allow for interspersed non-ObjC++ code.  */
20039         cp_parser_objc_interstitial_code (parser);
20040
20041       token = cp_lexer_peek_token (parser->lexer);
20042     }
20043
20044   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20045   objc_finish_implementation ();
20046 }
20047
20048 /* Parse Objective-C ivars.  */
20049
20050 static void
20051 cp_parser_objc_class_ivars (cp_parser* parser)
20052 {
20053   cp_token *token = cp_lexer_peek_token (parser->lexer);
20054
20055   if (token->type != CPP_OPEN_BRACE)
20056     return;     /* No ivars specified.  */
20057
20058   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20059   token = cp_lexer_peek_token (parser->lexer);
20060
20061   while (token->type != CPP_CLOSE_BRACE)
20062     {
20063       cp_decl_specifier_seq declspecs;
20064       int decl_class_or_enum_p;
20065       tree prefix_attributes;
20066
20067       cp_parser_objc_visibility_spec (parser);
20068
20069       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20070         break;
20071
20072       cp_parser_decl_specifier_seq (parser,
20073                                     CP_PARSER_FLAGS_OPTIONAL,
20074                                     &declspecs,
20075                                     &decl_class_or_enum_p);
20076       prefix_attributes = declspecs.attributes;
20077       declspecs.attributes = NULL_TREE;
20078
20079       /* Keep going until we hit the `;' at the end of the
20080          declaration.  */
20081       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20082         {
20083           tree width = NULL_TREE, attributes, first_attribute, decl;
20084           cp_declarator *declarator = NULL;
20085           int ctor_dtor_or_conv_p;
20086
20087           /* Check for a (possibly unnamed) bitfield declaration.  */
20088           token = cp_lexer_peek_token (parser->lexer);
20089           if (token->type == CPP_COLON)
20090             goto eat_colon;
20091
20092           if (token->type == CPP_NAME
20093               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20094                   == CPP_COLON))
20095             {
20096               /* Get the name of the bitfield.  */
20097               declarator = make_id_declarator (NULL_TREE,
20098                                                cp_parser_identifier (parser),
20099                                                sfk_none);
20100
20101              eat_colon:
20102               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20103               /* Get the width of the bitfield.  */
20104               width
20105                 = cp_parser_constant_expression (parser,
20106                                                  /*allow_non_constant=*/false,
20107                                                  NULL);
20108             }
20109           else
20110             {
20111               /* Parse the declarator.  */
20112               declarator
20113                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20114                                         &ctor_dtor_or_conv_p,
20115                                         /*parenthesized_p=*/NULL,
20116                                         /*member_p=*/false);
20117             }
20118
20119           /* Look for attributes that apply to the ivar.  */
20120           attributes = cp_parser_attributes_opt (parser);
20121           /* Remember which attributes are prefix attributes and
20122              which are not.  */
20123           first_attribute = attributes;
20124           /* Combine the attributes.  */
20125           attributes = chainon (prefix_attributes, attributes);
20126
20127           if (width)
20128               /* Create the bitfield declaration.  */
20129               decl = grokbitfield (declarator, &declspecs,
20130                                    width,
20131                                    attributes);
20132           else
20133             decl = grokfield (declarator, &declspecs,
20134                               NULL_TREE, /*init_const_expr_p=*/false,
20135                               NULL_TREE, attributes);
20136
20137           /* Add the instance variable.  */
20138           objc_add_instance_variable (decl);
20139
20140           /* Reset PREFIX_ATTRIBUTES.  */
20141           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20142             attributes = TREE_CHAIN (attributes);
20143           if (attributes)
20144             TREE_CHAIN (attributes) = NULL_TREE;
20145
20146           token = cp_lexer_peek_token (parser->lexer);
20147
20148           if (token->type == CPP_COMMA)
20149             {
20150               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20151               continue;
20152             }
20153           break;
20154         }
20155
20156       cp_parser_consume_semicolon_at_end_of_statement (parser);
20157       token = cp_lexer_peek_token (parser->lexer);
20158     }
20159
20160   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20161   /* For historical reasons, we accept an optional semicolon.  */
20162   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20163     cp_lexer_consume_token (parser->lexer);
20164 }
20165
20166 /* Parse an Objective-C protocol declaration.  */
20167
20168 static void
20169 cp_parser_objc_protocol_declaration (cp_parser* parser)
20170 {
20171   tree proto, protorefs;
20172   cp_token *tok;
20173
20174   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20175   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20176     {
20177       tok = cp_lexer_peek_token (parser->lexer);
20178       error_at (tok->location, "identifier expected after %<@protocol%>");
20179       goto finish;
20180     }
20181
20182   /* See if we have a forward declaration or a definition.  */
20183   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20184
20185   /* Try a forward declaration first.  */
20186   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20187     {
20188       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20189      finish:
20190       cp_parser_consume_semicolon_at_end_of_statement (parser);
20191     }
20192
20193   /* Ok, we got a full-fledged definition (or at least should).  */
20194   else
20195     {
20196       proto = cp_parser_identifier (parser);
20197       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20198       objc_start_protocol (proto, protorefs);
20199       cp_parser_objc_method_prototype_list (parser);
20200     }
20201 }
20202
20203 /* Parse an Objective-C superclass or category.  */
20204
20205 static void
20206 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20207                                                           tree *categ)
20208 {
20209   cp_token *next = cp_lexer_peek_token (parser->lexer);
20210
20211   *super = *categ = NULL_TREE;
20212   if (next->type == CPP_COLON)
20213     {
20214       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20215       *super = cp_parser_identifier (parser);
20216     }
20217   else if (next->type == CPP_OPEN_PAREN)
20218     {
20219       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20220       *categ = cp_parser_identifier (parser);
20221       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20222     }
20223 }
20224
20225 /* Parse an Objective-C class interface.  */
20226
20227 static void
20228 cp_parser_objc_class_interface (cp_parser* parser)
20229 {
20230   tree name, super, categ, protos;
20231
20232   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20233   name = cp_parser_identifier (parser);
20234   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20235   protos = cp_parser_objc_protocol_refs_opt (parser);
20236
20237   /* We have either a class or a category on our hands.  */
20238   if (categ)
20239     objc_start_category_interface (name, categ, protos);
20240   else
20241     {
20242       objc_start_class_interface (name, super, protos);
20243       /* Handle instance variable declarations, if any.  */
20244       cp_parser_objc_class_ivars (parser);
20245       objc_continue_interface ();
20246     }
20247
20248   cp_parser_objc_method_prototype_list (parser);
20249 }
20250
20251 /* Parse an Objective-C class implementation.  */
20252
20253 static void
20254 cp_parser_objc_class_implementation (cp_parser* parser)
20255 {
20256   tree name, super, categ;
20257
20258   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20259   name = cp_parser_identifier (parser);
20260   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20261
20262   /* We have either a class or a category on our hands.  */
20263   if (categ)
20264     objc_start_category_implementation (name, categ);
20265   else
20266     {
20267       objc_start_class_implementation (name, super);
20268       /* Handle instance variable declarations, if any.  */
20269       cp_parser_objc_class_ivars (parser);
20270       objc_continue_implementation ();
20271     }
20272
20273   cp_parser_objc_method_definition_list (parser);
20274 }
20275
20276 /* Consume the @end token and finish off the implementation.  */
20277
20278 static void
20279 cp_parser_objc_end_implementation (cp_parser* parser)
20280 {
20281   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20282   objc_finish_implementation ();
20283 }
20284
20285 /* Parse an Objective-C declaration.  */
20286
20287 static void
20288 cp_parser_objc_declaration (cp_parser* parser)
20289 {
20290   /* Try to figure out what kind of declaration is present.  */
20291   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20292
20293   switch (kwd->keyword)
20294     {
20295     case RID_AT_ALIAS:
20296       cp_parser_objc_alias_declaration (parser);
20297       break;
20298     case RID_AT_CLASS:
20299       cp_parser_objc_class_declaration (parser);
20300       break;
20301     case RID_AT_PROTOCOL:
20302       cp_parser_objc_protocol_declaration (parser);
20303       break;
20304     case RID_AT_INTERFACE:
20305       cp_parser_objc_class_interface (parser);
20306       break;
20307     case RID_AT_IMPLEMENTATION:
20308       cp_parser_objc_class_implementation (parser);
20309       break;
20310     case RID_AT_END:
20311       cp_parser_objc_end_implementation (parser);
20312       break;
20313     default:
20314       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20315                 kwd->u.value);
20316       cp_parser_skip_to_end_of_block_or_statement (parser);
20317     }
20318 }
20319
20320 /* Parse an Objective-C try-catch-finally statement.
20321
20322    objc-try-catch-finally-stmt:
20323      @try compound-statement objc-catch-clause-seq [opt]
20324        objc-finally-clause [opt]
20325
20326    objc-catch-clause-seq:
20327      objc-catch-clause objc-catch-clause-seq [opt]
20328
20329    objc-catch-clause:
20330      @catch ( exception-declaration ) compound-statement
20331
20332    objc-finally-clause
20333      @finally compound-statement
20334
20335    Returns NULL_TREE.  */
20336
20337 static tree
20338 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20339   location_t location;
20340   tree stmt;
20341
20342   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20343   location = cp_lexer_peek_token (parser->lexer)->location;
20344   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20345      node, lest it get absorbed into the surrounding block.  */
20346   stmt = push_stmt_list ();
20347   cp_parser_compound_statement (parser, NULL, false);
20348   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20349
20350   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20351     {
20352       cp_parameter_declarator *parmdecl;
20353       tree parm;
20354
20355       cp_lexer_consume_token (parser->lexer);
20356       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20357       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20358       parm = grokdeclarator (parmdecl->declarator,
20359                              &parmdecl->decl_specifiers,
20360                              PARM, /*initialized=*/0,
20361                              /*attrlist=*/NULL);
20362       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20363       objc_begin_catch_clause (parm);
20364       cp_parser_compound_statement (parser, NULL, false);
20365       objc_finish_catch_clause ();
20366     }
20367
20368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20369     {
20370       cp_lexer_consume_token (parser->lexer);
20371       location = cp_lexer_peek_token (parser->lexer)->location;
20372       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20373          node, lest it get absorbed into the surrounding block.  */
20374       stmt = push_stmt_list ();
20375       cp_parser_compound_statement (parser, NULL, false);
20376       objc_build_finally_clause (location, pop_stmt_list (stmt));
20377     }
20378
20379   return objc_finish_try_stmt ();
20380 }
20381
20382 /* Parse an Objective-C synchronized statement.
20383
20384    objc-synchronized-stmt:
20385      @synchronized ( expression ) compound-statement
20386
20387    Returns NULL_TREE.  */
20388
20389 static tree
20390 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20391   location_t location;
20392   tree lock, stmt;
20393
20394   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20395
20396   location = cp_lexer_peek_token (parser->lexer)->location;
20397   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20398   lock = cp_parser_expression (parser, false, NULL);
20399   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20400
20401   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20402      node, lest it get absorbed into the surrounding block.  */
20403   stmt = push_stmt_list ();
20404   cp_parser_compound_statement (parser, NULL, false);
20405
20406   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20407 }
20408
20409 /* Parse an Objective-C throw statement.
20410
20411    objc-throw-stmt:
20412      @throw assignment-expression [opt] ;
20413
20414    Returns a constructed '@throw' statement.  */
20415
20416 static tree
20417 cp_parser_objc_throw_statement (cp_parser *parser) {
20418   tree expr = NULL_TREE;
20419   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20420
20421   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20422
20423   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20424     expr = cp_parser_assignment_expression (parser, false, NULL);
20425
20426   cp_parser_consume_semicolon_at_end_of_statement (parser);
20427
20428   return objc_build_throw_stmt (loc, expr);
20429 }
20430
20431 /* Parse an Objective-C statement.  */
20432
20433 static tree
20434 cp_parser_objc_statement (cp_parser * parser) {
20435   /* Try to figure out what kind of declaration is present.  */
20436   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20437
20438   switch (kwd->keyword)
20439     {
20440     case RID_AT_TRY:
20441       return cp_parser_objc_try_catch_finally_statement (parser);
20442     case RID_AT_SYNCHRONIZED:
20443       return cp_parser_objc_synchronized_statement (parser);
20444     case RID_AT_THROW:
20445       return cp_parser_objc_throw_statement (parser);
20446     default:
20447       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20448                kwd->u.value);
20449       cp_parser_skip_to_end_of_block_or_statement (parser);
20450     }
20451
20452   return error_mark_node;
20453 }
20454 \f
20455 /* OpenMP 2.5 parsing routines.  */
20456
20457 /* Returns name of the next clause.
20458    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20459    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20460    returned and the token is consumed.  */
20461
20462 static pragma_omp_clause
20463 cp_parser_omp_clause_name (cp_parser *parser)
20464 {
20465   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20466
20467   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20468     result = PRAGMA_OMP_CLAUSE_IF;
20469   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20470     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20471   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20472     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20473   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20474     {
20475       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20476       const char *p = IDENTIFIER_POINTER (id);
20477
20478       switch (p[0])
20479         {
20480         case 'c':
20481           if (!strcmp ("collapse", p))
20482             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20483           else if (!strcmp ("copyin", p))
20484             result = PRAGMA_OMP_CLAUSE_COPYIN;
20485           else if (!strcmp ("copyprivate", p))
20486             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20487           break;
20488         case 'f':
20489           if (!strcmp ("firstprivate", p))
20490             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20491           break;
20492         case 'l':
20493           if (!strcmp ("lastprivate", p))
20494             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20495           break;
20496         case 'n':
20497           if (!strcmp ("nowait", p))
20498             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20499           else if (!strcmp ("num_threads", p))
20500             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20501           break;
20502         case 'o':
20503           if (!strcmp ("ordered", p))
20504             result = PRAGMA_OMP_CLAUSE_ORDERED;
20505           break;
20506         case 'r':
20507           if (!strcmp ("reduction", p))
20508             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20509           break;
20510         case 's':
20511           if (!strcmp ("schedule", p))
20512             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20513           else if (!strcmp ("shared", p))
20514             result = PRAGMA_OMP_CLAUSE_SHARED;
20515           break;
20516         case 'u':
20517           if (!strcmp ("untied", p))
20518             result = PRAGMA_OMP_CLAUSE_UNTIED;
20519           break;
20520         }
20521     }
20522
20523   if (result != PRAGMA_OMP_CLAUSE_NONE)
20524     cp_lexer_consume_token (parser->lexer);
20525
20526   return result;
20527 }
20528
20529 /* Validate that a clause of the given type does not already exist.  */
20530
20531 static void
20532 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20533                            const char *name, location_t location)
20534 {
20535   tree c;
20536
20537   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20538     if (OMP_CLAUSE_CODE (c) == code)
20539       {
20540         error_at (location, "too many %qs clauses", name);
20541         break;
20542       }
20543 }
20544
20545 /* OpenMP 2.5:
20546    variable-list:
20547      identifier
20548      variable-list , identifier
20549
20550    In addition, we match a closing parenthesis.  An opening parenthesis
20551    will have been consumed by the caller.
20552
20553    If KIND is nonzero, create the appropriate node and install the decl
20554    in OMP_CLAUSE_DECL and add the node to the head of the list.
20555
20556    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20557    return the list created.  */
20558
20559 static tree
20560 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20561                                 tree list)
20562 {
20563   cp_token *token;
20564   while (1)
20565     {
20566       tree name, decl;
20567
20568       token = cp_lexer_peek_token (parser->lexer);
20569       name = cp_parser_id_expression (parser, /*template_p=*/false,
20570                                       /*check_dependency_p=*/true,
20571                                       /*template_p=*/NULL,
20572                                       /*declarator_p=*/false,
20573                                       /*optional_p=*/false);
20574       if (name == error_mark_node)
20575         goto skip_comma;
20576
20577       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20578       if (decl == error_mark_node)
20579         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20580       else if (kind != 0)
20581         {
20582           tree u = build_omp_clause (token->location, kind);
20583           OMP_CLAUSE_DECL (u) = decl;
20584           OMP_CLAUSE_CHAIN (u) = list;
20585           list = u;
20586         }
20587       else
20588         list = tree_cons (decl, NULL_TREE, list);
20589
20590     get_comma:
20591       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20592         break;
20593       cp_lexer_consume_token (parser->lexer);
20594     }
20595
20596   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20597     {
20598       int ending;
20599
20600       /* Try to resync to an unnested comma.  Copied from
20601          cp_parser_parenthesized_expression_list.  */
20602     skip_comma:
20603       ending = cp_parser_skip_to_closing_parenthesis (parser,
20604                                                       /*recovering=*/true,
20605                                                       /*or_comma=*/true,
20606                                                       /*consume_paren=*/true);
20607       if (ending < 0)
20608         goto get_comma;
20609     }
20610
20611   return list;
20612 }
20613
20614 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20615    common case for omp clauses.  */
20616
20617 static tree
20618 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20619 {
20620   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20621     return cp_parser_omp_var_list_no_open (parser, kind, list);
20622   return list;
20623 }
20624
20625 /* OpenMP 3.0:
20626    collapse ( constant-expression ) */
20627
20628 static tree
20629 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20630 {
20631   tree c, num;
20632   location_t loc;
20633   HOST_WIDE_INT n;
20634
20635   loc = cp_lexer_peek_token (parser->lexer)->location;
20636   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20637     return list;
20638
20639   num = cp_parser_constant_expression (parser, false, NULL);
20640
20641   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20642     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20643                                            /*or_comma=*/false,
20644                                            /*consume_paren=*/true);
20645
20646   if (num == error_mark_node)
20647     return list;
20648   num = fold_non_dependent_expr (num);
20649   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20650       || !host_integerp (num, 0)
20651       || (n = tree_low_cst (num, 0)) <= 0
20652       || (int) n != n)
20653     {
20654       error_at (loc, "collapse argument needs positive constant integer expression");
20655       return list;
20656     }
20657
20658   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20659   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20660   OMP_CLAUSE_CHAIN (c) = list;
20661   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20662
20663   return c;
20664 }
20665
20666 /* OpenMP 2.5:
20667    default ( shared | none ) */
20668
20669 static tree
20670 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20671 {
20672   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20673   tree c;
20674
20675   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20676     return list;
20677   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20678     {
20679       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20680       const char *p = IDENTIFIER_POINTER (id);
20681
20682       switch (p[0])
20683         {
20684         case 'n':
20685           if (strcmp ("none", p) != 0)
20686             goto invalid_kind;
20687           kind = OMP_CLAUSE_DEFAULT_NONE;
20688           break;
20689
20690         case 's':
20691           if (strcmp ("shared", p) != 0)
20692             goto invalid_kind;
20693           kind = OMP_CLAUSE_DEFAULT_SHARED;
20694           break;
20695
20696         default:
20697           goto invalid_kind;
20698         }
20699
20700       cp_lexer_consume_token (parser->lexer);
20701     }
20702   else
20703     {
20704     invalid_kind:
20705       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20706     }
20707
20708   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20709     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20710                                            /*or_comma=*/false,
20711                                            /*consume_paren=*/true);
20712
20713   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20714     return list;
20715
20716   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20717   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20718   OMP_CLAUSE_CHAIN (c) = list;
20719   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20720
20721   return c;
20722 }
20723
20724 /* OpenMP 2.5:
20725    if ( expression ) */
20726
20727 static tree
20728 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20729 {
20730   tree t, c;
20731
20732   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20733     return list;
20734
20735   t = cp_parser_condition (parser);
20736
20737   if (t == error_mark_node
20738       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20739     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20740                                            /*or_comma=*/false,
20741                                            /*consume_paren=*/true);
20742
20743   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20744
20745   c = build_omp_clause (location, OMP_CLAUSE_IF);
20746   OMP_CLAUSE_IF_EXPR (c) = t;
20747   OMP_CLAUSE_CHAIN (c) = list;
20748
20749   return c;
20750 }
20751
20752 /* OpenMP 2.5:
20753    nowait */
20754
20755 static tree
20756 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20757                              tree list, location_t location)
20758 {
20759   tree c;
20760
20761   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20762
20763   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20764   OMP_CLAUSE_CHAIN (c) = list;
20765   return c;
20766 }
20767
20768 /* OpenMP 2.5:
20769    num_threads ( expression ) */
20770
20771 static tree
20772 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20773                                   location_t location)
20774 {
20775   tree t, c;
20776
20777   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20778     return list;
20779
20780   t = cp_parser_expression (parser, false, NULL);
20781
20782   if (t == error_mark_node
20783       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20784     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20785                                            /*or_comma=*/false,
20786                                            /*consume_paren=*/true);
20787
20788   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20789                              "num_threads", location);
20790
20791   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20792   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20793   OMP_CLAUSE_CHAIN (c) = list;
20794
20795   return c;
20796 }
20797
20798 /* OpenMP 2.5:
20799    ordered */
20800
20801 static tree
20802 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20803                               tree list, location_t location)
20804 {
20805   tree c;
20806
20807   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20808                              "ordered", location);
20809
20810   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20811   OMP_CLAUSE_CHAIN (c) = list;
20812   return c;
20813 }
20814
20815 /* OpenMP 2.5:
20816    reduction ( reduction-operator : variable-list )
20817
20818    reduction-operator:
20819      One of: + * - & ^ | && || */
20820
20821 static tree
20822 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20823 {
20824   enum tree_code code;
20825   tree nlist, c;
20826
20827   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20828     return list;
20829
20830   switch (cp_lexer_peek_token (parser->lexer)->type)
20831     {
20832     case CPP_PLUS:
20833       code = PLUS_EXPR;
20834       break;
20835     case CPP_MULT:
20836       code = MULT_EXPR;
20837       break;
20838     case CPP_MINUS:
20839       code = MINUS_EXPR;
20840       break;
20841     case CPP_AND:
20842       code = BIT_AND_EXPR;
20843       break;
20844     case CPP_XOR:
20845       code = BIT_XOR_EXPR;
20846       break;
20847     case CPP_OR:
20848       code = BIT_IOR_EXPR;
20849       break;
20850     case CPP_AND_AND:
20851       code = TRUTH_ANDIF_EXPR;
20852       break;
20853     case CPP_OR_OR:
20854       code = TRUTH_ORIF_EXPR;
20855       break;
20856     default:
20857       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20858                                "%<|%>, %<&&%>, or %<||%>");
20859     resync_fail:
20860       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20861                                              /*or_comma=*/false,
20862                                              /*consume_paren=*/true);
20863       return list;
20864     }
20865   cp_lexer_consume_token (parser->lexer);
20866
20867   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20868     goto resync_fail;
20869
20870   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20871   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20872     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20873
20874   return nlist;
20875 }
20876
20877 /* OpenMP 2.5:
20878    schedule ( schedule-kind )
20879    schedule ( schedule-kind , expression )
20880
20881    schedule-kind:
20882      static | dynamic | guided | runtime | auto  */
20883
20884 static tree
20885 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20886 {
20887   tree c, t;
20888
20889   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20890     return list;
20891
20892   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20893
20894   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20895     {
20896       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20897       const char *p = IDENTIFIER_POINTER (id);
20898
20899       switch (p[0])
20900         {
20901         case 'd':
20902           if (strcmp ("dynamic", p) != 0)
20903             goto invalid_kind;
20904           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20905           break;
20906
20907         case 'g':
20908           if (strcmp ("guided", p) != 0)
20909             goto invalid_kind;
20910           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20911           break;
20912
20913         case 'r':
20914           if (strcmp ("runtime", p) != 0)
20915             goto invalid_kind;
20916           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20917           break;
20918
20919         default:
20920           goto invalid_kind;
20921         }
20922     }
20923   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20924     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20925   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20926     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20927   else
20928     goto invalid_kind;
20929   cp_lexer_consume_token (parser->lexer);
20930
20931   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20932     {
20933       cp_token *token;
20934       cp_lexer_consume_token (parser->lexer);
20935
20936       token = cp_lexer_peek_token (parser->lexer);
20937       t = cp_parser_assignment_expression (parser, false, NULL);
20938
20939       if (t == error_mark_node)
20940         goto resync_fail;
20941       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20942         error_at (token->location, "schedule %<runtime%> does not take "
20943                   "a %<chunk_size%> parameter");
20944       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20945         error_at (token->location, "schedule %<auto%> does not take "
20946                   "a %<chunk_size%> parameter");
20947       else
20948         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20949
20950       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20951         goto resync_fail;
20952     }
20953   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20954     goto resync_fail;
20955
20956   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20957   OMP_CLAUSE_CHAIN (c) = list;
20958   return c;
20959
20960  invalid_kind:
20961   cp_parser_error (parser, "invalid schedule kind");
20962  resync_fail:
20963   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20964                                          /*or_comma=*/false,
20965                                          /*consume_paren=*/true);
20966   return list;
20967 }
20968
20969 /* OpenMP 3.0:
20970    untied */
20971
20972 static tree
20973 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20974                              tree list, location_t location)
20975 {
20976   tree c;
20977
20978   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20979
20980   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20981   OMP_CLAUSE_CHAIN (c) = list;
20982   return c;
20983 }
20984
20985 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20986    is a bitmask in MASK.  Return the list of clauses found; the result
20987    of clause default goes in *pdefault.  */
20988
20989 static tree
20990 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20991                            const char *where, cp_token *pragma_tok)
20992 {
20993   tree clauses = NULL;
20994   bool first = true;
20995   cp_token *token = NULL;
20996
20997   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20998     {
20999       pragma_omp_clause c_kind;
21000       const char *c_name;
21001       tree prev = clauses;
21002
21003       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21004         cp_lexer_consume_token (parser->lexer);
21005
21006       token = cp_lexer_peek_token (parser->lexer);
21007       c_kind = cp_parser_omp_clause_name (parser);
21008       first = false;
21009
21010       switch (c_kind)
21011         {
21012         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21013           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21014                                                    token->location);
21015           c_name = "collapse";
21016           break;
21017         case PRAGMA_OMP_CLAUSE_COPYIN:
21018           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21019           c_name = "copyin";
21020           break;
21021         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21022           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21023                                             clauses);
21024           c_name = "copyprivate";
21025           break;
21026         case PRAGMA_OMP_CLAUSE_DEFAULT:
21027           clauses = cp_parser_omp_clause_default (parser, clauses,
21028                                                   token->location);
21029           c_name = "default";
21030           break;
21031         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21032           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21033                                             clauses);
21034           c_name = "firstprivate";
21035           break;
21036         case PRAGMA_OMP_CLAUSE_IF:
21037           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21038           c_name = "if";
21039           break;
21040         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21041           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21042                                             clauses);
21043           c_name = "lastprivate";
21044           break;
21045         case PRAGMA_OMP_CLAUSE_NOWAIT:
21046           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21047           c_name = "nowait";
21048           break;
21049         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21050           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21051                                                       token->location);
21052           c_name = "num_threads";
21053           break;
21054         case PRAGMA_OMP_CLAUSE_ORDERED:
21055           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21056                                                   token->location);
21057           c_name = "ordered";
21058           break;
21059         case PRAGMA_OMP_CLAUSE_PRIVATE:
21060           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21061                                             clauses);
21062           c_name = "private";
21063           break;
21064         case PRAGMA_OMP_CLAUSE_REDUCTION:
21065           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21066           c_name = "reduction";
21067           break;
21068         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21069           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21070                                                    token->location);
21071           c_name = "schedule";
21072           break;
21073         case PRAGMA_OMP_CLAUSE_SHARED:
21074           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21075                                             clauses);
21076           c_name = "shared";
21077           break;
21078         case PRAGMA_OMP_CLAUSE_UNTIED:
21079           clauses = cp_parser_omp_clause_untied (parser, clauses,
21080                                                  token->location);
21081           c_name = "nowait";
21082           break;
21083         default:
21084           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21085           goto saw_error;
21086         }
21087
21088       if (((mask >> c_kind) & 1) == 0)
21089         {
21090           /* Remove the invalid clause(s) from the list to avoid
21091              confusing the rest of the compiler.  */
21092           clauses = prev;
21093           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21094         }
21095     }
21096  saw_error:
21097   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21098   return finish_omp_clauses (clauses);
21099 }
21100
21101 /* OpenMP 2.5:
21102    structured-block:
21103      statement
21104
21105    In practice, we're also interested in adding the statement to an
21106    outer node.  So it is convenient if we work around the fact that
21107    cp_parser_statement calls add_stmt.  */
21108
21109 static unsigned
21110 cp_parser_begin_omp_structured_block (cp_parser *parser)
21111 {
21112   unsigned save = parser->in_statement;
21113
21114   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21115      This preserves the "not within loop or switch" style error messages
21116      for nonsense cases like
21117         void foo() {
21118         #pragma omp single
21119           break;
21120         }
21121   */
21122   if (parser->in_statement)
21123     parser->in_statement = IN_OMP_BLOCK;
21124
21125   return save;
21126 }
21127
21128 static void
21129 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21130 {
21131   parser->in_statement = save;
21132 }
21133
21134 static tree
21135 cp_parser_omp_structured_block (cp_parser *parser)
21136 {
21137   tree stmt = begin_omp_structured_block ();
21138   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21139
21140   cp_parser_statement (parser, NULL_TREE, false, NULL);
21141
21142   cp_parser_end_omp_structured_block (parser, save);
21143   return finish_omp_structured_block (stmt);
21144 }
21145
21146 /* OpenMP 2.5:
21147    # pragma omp atomic new-line
21148      expression-stmt
21149
21150    expression-stmt:
21151      x binop= expr | x++ | ++x | x-- | --x
21152    binop:
21153      +, *, -, /, &, ^, |, <<, >>
21154
21155   where x is an lvalue expression with scalar type.  */
21156
21157 static void
21158 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21159 {
21160   tree lhs, rhs;
21161   enum tree_code code;
21162
21163   cp_parser_require_pragma_eol (parser, pragma_tok);
21164
21165   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21166                                     /*cast_p=*/false, NULL);
21167   switch (TREE_CODE (lhs))
21168     {
21169     case ERROR_MARK:
21170       goto saw_error;
21171
21172     case PREINCREMENT_EXPR:
21173     case POSTINCREMENT_EXPR:
21174       lhs = TREE_OPERAND (lhs, 0);
21175       code = PLUS_EXPR;
21176       rhs = integer_one_node;
21177       break;
21178
21179     case PREDECREMENT_EXPR:
21180     case POSTDECREMENT_EXPR:
21181       lhs = TREE_OPERAND (lhs, 0);
21182       code = MINUS_EXPR;
21183       rhs = integer_one_node;
21184       break;
21185
21186     default:
21187       switch (cp_lexer_peek_token (parser->lexer)->type)
21188         {
21189         case CPP_MULT_EQ:
21190           code = MULT_EXPR;
21191           break;
21192         case CPP_DIV_EQ:
21193           code = TRUNC_DIV_EXPR;
21194           break;
21195         case CPP_PLUS_EQ:
21196           code = PLUS_EXPR;
21197           break;
21198         case CPP_MINUS_EQ:
21199           code = MINUS_EXPR;
21200           break;
21201         case CPP_LSHIFT_EQ:
21202           code = LSHIFT_EXPR;
21203           break;
21204         case CPP_RSHIFT_EQ:
21205           code = RSHIFT_EXPR;
21206           break;
21207         case CPP_AND_EQ:
21208           code = BIT_AND_EXPR;
21209           break;
21210         case CPP_OR_EQ:
21211           code = BIT_IOR_EXPR;
21212           break;
21213         case CPP_XOR_EQ:
21214           code = BIT_XOR_EXPR;
21215           break;
21216         default:
21217           cp_parser_error (parser,
21218                            "invalid operator for %<#pragma omp atomic%>");
21219           goto saw_error;
21220         }
21221       cp_lexer_consume_token (parser->lexer);
21222
21223       rhs = cp_parser_expression (parser, false, NULL);
21224       if (rhs == error_mark_node)
21225         goto saw_error;
21226       break;
21227     }
21228   finish_omp_atomic (code, lhs, rhs);
21229   cp_parser_consume_semicolon_at_end_of_statement (parser);
21230   return;
21231
21232  saw_error:
21233   cp_parser_skip_to_end_of_block_or_statement (parser);
21234 }
21235
21236
21237 /* OpenMP 2.5:
21238    # pragma omp barrier new-line  */
21239
21240 static void
21241 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21242 {
21243   cp_parser_require_pragma_eol (parser, pragma_tok);
21244   finish_omp_barrier ();
21245 }
21246
21247 /* OpenMP 2.5:
21248    # pragma omp critical [(name)] new-line
21249      structured-block  */
21250
21251 static tree
21252 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21253 {
21254   tree stmt, name = NULL;
21255
21256   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21257     {
21258       cp_lexer_consume_token (parser->lexer);
21259
21260       name = cp_parser_identifier (parser);
21261
21262       if (name == error_mark_node
21263           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21264         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21265                                                /*or_comma=*/false,
21266                                                /*consume_paren=*/true);
21267       if (name == error_mark_node)
21268         name = NULL;
21269     }
21270   cp_parser_require_pragma_eol (parser, pragma_tok);
21271
21272   stmt = cp_parser_omp_structured_block (parser);
21273   return c_finish_omp_critical (input_location, stmt, name);
21274 }
21275
21276 /* OpenMP 2.5:
21277    # pragma omp flush flush-vars[opt] new-line
21278
21279    flush-vars:
21280      ( variable-list ) */
21281
21282 static void
21283 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21284 {
21285   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21286     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21287   cp_parser_require_pragma_eol (parser, pragma_tok);
21288
21289   finish_omp_flush ();
21290 }
21291
21292 /* Helper function, to parse omp for increment expression.  */
21293
21294 static tree
21295 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21296 {
21297   tree cond = cp_parser_binary_expression (parser, false, true,
21298                                            PREC_NOT_OPERATOR, NULL);
21299   bool overloaded_p;
21300
21301   if (cond == error_mark_node
21302       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21303     {
21304       cp_parser_skip_to_end_of_statement (parser);
21305       return error_mark_node;
21306     }
21307
21308   switch (TREE_CODE (cond))
21309     {
21310     case GT_EXPR:
21311     case GE_EXPR:
21312     case LT_EXPR:
21313     case LE_EXPR:
21314       break;
21315     default:
21316       return error_mark_node;
21317     }
21318
21319   /* If decl is an iterator, preserve LHS and RHS of the relational
21320      expr until finish_omp_for.  */
21321   if (decl
21322       && (type_dependent_expression_p (decl)
21323           || CLASS_TYPE_P (TREE_TYPE (decl))))
21324     return cond;
21325
21326   return build_x_binary_op (TREE_CODE (cond),
21327                             TREE_OPERAND (cond, 0), ERROR_MARK,
21328                             TREE_OPERAND (cond, 1), ERROR_MARK,
21329                             &overloaded_p, tf_warning_or_error);
21330 }
21331
21332 /* Helper function, to parse omp for increment expression.  */
21333
21334 static tree
21335 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21336 {
21337   cp_token *token = cp_lexer_peek_token (parser->lexer);
21338   enum tree_code op;
21339   tree lhs, rhs;
21340   cp_id_kind idk;
21341   bool decl_first;
21342
21343   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21344     {
21345       op = (token->type == CPP_PLUS_PLUS
21346             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21347       cp_lexer_consume_token (parser->lexer);
21348       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21349       if (lhs != decl)
21350         return error_mark_node;
21351       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21352     }
21353
21354   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21355   if (lhs != decl)
21356     return error_mark_node;
21357
21358   token = cp_lexer_peek_token (parser->lexer);
21359   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21360     {
21361       op = (token->type == CPP_PLUS_PLUS
21362             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21363       cp_lexer_consume_token (parser->lexer);
21364       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21365     }
21366
21367   op = cp_parser_assignment_operator_opt (parser);
21368   if (op == ERROR_MARK)
21369     return error_mark_node;
21370
21371   if (op != NOP_EXPR)
21372     {
21373       rhs = cp_parser_assignment_expression (parser, false, NULL);
21374       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21375       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21376     }
21377
21378   lhs = cp_parser_binary_expression (parser, false, false,
21379                                      PREC_ADDITIVE_EXPRESSION, NULL);
21380   token = cp_lexer_peek_token (parser->lexer);
21381   decl_first = lhs == decl;
21382   if (decl_first)
21383     lhs = NULL_TREE;
21384   if (token->type != CPP_PLUS
21385       && token->type != CPP_MINUS)
21386     return error_mark_node;
21387
21388   do
21389     {
21390       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21391       cp_lexer_consume_token (parser->lexer);
21392       rhs = cp_parser_binary_expression (parser, false, false,
21393                                          PREC_ADDITIVE_EXPRESSION, NULL);
21394       token = cp_lexer_peek_token (parser->lexer);
21395       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21396         {
21397           if (lhs == NULL_TREE)
21398             {
21399               if (op == PLUS_EXPR)
21400                 lhs = rhs;
21401               else
21402                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21403             }
21404           else
21405             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21406                                      NULL, tf_warning_or_error);
21407         }
21408     }
21409   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21410
21411   if (!decl_first)
21412     {
21413       if (rhs != decl || op == MINUS_EXPR)
21414         return error_mark_node;
21415       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21416     }
21417   else
21418     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21419
21420   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21421 }
21422
21423 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21424
21425 static tree
21426 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21427 {
21428   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21429   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21430   tree this_pre_body, cl;
21431   location_t loc_first;
21432   bool collapse_err = false;
21433   int i, collapse = 1, nbraces = 0;
21434
21435   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21436     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21437       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21438
21439   gcc_assert (collapse >= 1);
21440
21441   declv = make_tree_vec (collapse);
21442   initv = make_tree_vec (collapse);
21443   condv = make_tree_vec (collapse);
21444   incrv = make_tree_vec (collapse);
21445
21446   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21447
21448   for (i = 0; i < collapse; i++)
21449     {
21450       int bracecount = 0;
21451       bool add_private_clause = false;
21452       location_t loc;
21453
21454       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21455         {
21456           cp_parser_error (parser, "for statement expected");
21457           return NULL;
21458         }
21459       loc = cp_lexer_consume_token (parser->lexer)->location;
21460
21461       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21462         return NULL;
21463
21464       init = decl = real_decl = NULL;
21465       this_pre_body = push_stmt_list ();
21466       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21467         {
21468           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21469
21470              init-expr:
21471                        var = lb
21472                        integer-type var = lb
21473                        random-access-iterator-type var = lb
21474                        pointer-type var = lb
21475           */
21476           cp_decl_specifier_seq type_specifiers;
21477
21478           /* First, try to parse as an initialized declaration.  See
21479              cp_parser_condition, from whence the bulk of this is copied.  */
21480
21481           cp_parser_parse_tentatively (parser);
21482           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21483                                         &type_specifiers);
21484           if (cp_parser_parse_definitely (parser))
21485             {
21486               /* If parsing a type specifier seq succeeded, then this
21487                  MUST be a initialized declaration.  */
21488               tree asm_specification, attributes;
21489               cp_declarator *declarator;
21490
21491               declarator = cp_parser_declarator (parser,
21492                                                  CP_PARSER_DECLARATOR_NAMED,
21493                                                  /*ctor_dtor_or_conv_p=*/NULL,
21494                                                  /*parenthesized_p=*/NULL,
21495                                                  /*member_p=*/false);
21496               attributes = cp_parser_attributes_opt (parser);
21497               asm_specification = cp_parser_asm_specification_opt (parser);
21498
21499               if (declarator == cp_error_declarator) 
21500                 cp_parser_skip_to_end_of_statement (parser);
21501
21502               else 
21503                 {
21504                   tree pushed_scope, auto_node;
21505
21506                   decl = start_decl (declarator, &type_specifiers,
21507                                      SD_INITIALIZED, attributes,
21508                                      /*prefix_attributes=*/NULL_TREE,
21509                                      &pushed_scope);
21510
21511                   auto_node = type_uses_auto (TREE_TYPE (decl));
21512                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21513                     {
21514                       if (cp_lexer_next_token_is (parser->lexer, 
21515                                                   CPP_OPEN_PAREN))
21516                         error ("parenthesized initialization is not allowed in "
21517                                "OpenMP %<for%> loop");
21518                       else
21519                         /* Trigger an error.  */
21520                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21521
21522                       init = error_mark_node;
21523                       cp_parser_skip_to_end_of_statement (parser);
21524                     }
21525                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21526                            || type_dependent_expression_p (decl)
21527                            || auto_node)
21528                     {
21529                       bool is_direct_init, is_non_constant_init;
21530
21531                       init = cp_parser_initializer (parser,
21532                                                     &is_direct_init,
21533                                                     &is_non_constant_init);
21534
21535                       if (auto_node && describable_type (init))
21536                         {
21537                           TREE_TYPE (decl)
21538                             = do_auto_deduction (TREE_TYPE (decl), init,
21539                                                  auto_node);
21540
21541                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21542                               && !type_dependent_expression_p (decl))
21543                             goto non_class;
21544                         }
21545                       
21546                       cp_finish_decl (decl, init, !is_non_constant_init,
21547                                       asm_specification,
21548                                       LOOKUP_ONLYCONVERTING);
21549                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21550                         {
21551                           for_block
21552                             = tree_cons (NULL, this_pre_body, for_block);
21553                           init = NULL_TREE;
21554                         }
21555                       else
21556                         init = pop_stmt_list (this_pre_body);
21557                       this_pre_body = NULL_TREE;
21558                     }
21559                   else
21560                     {
21561                       /* Consume '='.  */
21562                       cp_lexer_consume_token (parser->lexer);
21563                       init = cp_parser_assignment_expression (parser, false, NULL);
21564
21565                     non_class:
21566                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21567                         init = error_mark_node;
21568                       else
21569                         cp_finish_decl (decl, NULL_TREE,
21570                                         /*init_const_expr_p=*/false,
21571                                         asm_specification,
21572                                         LOOKUP_ONLYCONVERTING);
21573                     }
21574
21575                   if (pushed_scope)
21576                     pop_scope (pushed_scope);
21577                 }
21578             }
21579           else 
21580             {
21581               cp_id_kind idk;
21582               /* If parsing a type specifier sequence failed, then
21583                  this MUST be a simple expression.  */
21584               cp_parser_parse_tentatively (parser);
21585               decl = cp_parser_primary_expression (parser, false, false,
21586                                                    false, &idk);
21587               if (!cp_parser_error_occurred (parser)
21588                   && decl
21589                   && DECL_P (decl)
21590                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21591                 {
21592                   tree rhs;
21593
21594                   cp_parser_parse_definitely (parser);
21595                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21596                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21597                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21598                                                          rhs,
21599                                                          tf_warning_or_error));
21600                   add_private_clause = true;
21601                 }
21602               else
21603                 {
21604                   decl = NULL;
21605                   cp_parser_abort_tentative_parse (parser);
21606                   init = cp_parser_expression (parser, false, NULL);
21607                   if (init)
21608                     {
21609                       if (TREE_CODE (init) == MODIFY_EXPR
21610                           || TREE_CODE (init) == MODOP_EXPR)
21611                         real_decl = TREE_OPERAND (init, 0);
21612                     }
21613                 }
21614             }
21615         }
21616       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21617       if (this_pre_body)
21618         {
21619           this_pre_body = pop_stmt_list (this_pre_body);
21620           if (pre_body)
21621             {
21622               tree t = pre_body;
21623               pre_body = push_stmt_list ();
21624               add_stmt (t);
21625               add_stmt (this_pre_body);
21626               pre_body = pop_stmt_list (pre_body);
21627             }
21628           else
21629             pre_body = this_pre_body;
21630         }
21631
21632       if (decl)
21633         real_decl = decl;
21634       if (par_clauses != NULL && real_decl != NULL_TREE)
21635         {
21636           tree *c;
21637           for (c = par_clauses; *c ; )
21638             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21639                 && OMP_CLAUSE_DECL (*c) == real_decl)
21640               {
21641                 error_at (loc, "iteration variable %qD"
21642                           " should not be firstprivate", real_decl);
21643                 *c = OMP_CLAUSE_CHAIN (*c);
21644               }
21645             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21646                      && OMP_CLAUSE_DECL (*c) == real_decl)
21647               {
21648                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21649                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21650                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21651                 OMP_CLAUSE_DECL (l) = real_decl;
21652                 OMP_CLAUSE_CHAIN (l) = clauses;
21653                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21654                 clauses = l;
21655                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21656                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21657                 add_private_clause = false;
21658               }
21659             else
21660               {
21661                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21662                     && OMP_CLAUSE_DECL (*c) == real_decl)
21663                   add_private_clause = false;
21664                 c = &OMP_CLAUSE_CHAIN (*c);
21665               }
21666         }
21667
21668       if (add_private_clause)
21669         {
21670           tree c;
21671           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21672             {
21673               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21674                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21675                   && OMP_CLAUSE_DECL (c) == decl)
21676                 break;
21677               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21678                        && OMP_CLAUSE_DECL (c) == decl)
21679                 error_at (loc, "iteration variable %qD "
21680                           "should not be firstprivate",
21681                           decl);
21682               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21683                        && OMP_CLAUSE_DECL (c) == decl)
21684                 error_at (loc, "iteration variable %qD should not be reduction",
21685                           decl);
21686             }
21687           if (c == NULL)
21688             {
21689               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21690               OMP_CLAUSE_DECL (c) = decl;
21691               c = finish_omp_clauses (c);
21692               if (c)
21693                 {
21694                   OMP_CLAUSE_CHAIN (c) = clauses;
21695                   clauses = c;
21696                 }
21697             }
21698         }
21699
21700       cond = NULL;
21701       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21702         cond = cp_parser_omp_for_cond (parser, decl);
21703       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21704
21705       incr = NULL;
21706       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21707         {
21708           /* If decl is an iterator, preserve the operator on decl
21709              until finish_omp_for.  */
21710           if (decl
21711               && (type_dependent_expression_p (decl)
21712                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21713             incr = cp_parser_omp_for_incr (parser, decl);
21714           else
21715             incr = cp_parser_expression (parser, false, NULL);
21716         }
21717
21718       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21719         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21720                                                /*or_comma=*/false,
21721                                                /*consume_paren=*/true);
21722
21723       TREE_VEC_ELT (declv, i) = decl;
21724       TREE_VEC_ELT (initv, i) = init;
21725       TREE_VEC_ELT (condv, i) = cond;
21726       TREE_VEC_ELT (incrv, i) = incr;
21727
21728       if (i == collapse - 1)
21729         break;
21730
21731       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21732          in between the collapsed for loops to be still considered perfectly
21733          nested.  Hopefully the final version clarifies this.
21734          For now handle (multiple) {'s and empty statements.  */
21735       cp_parser_parse_tentatively (parser);
21736       do
21737         {
21738           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21739             break;
21740           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21741             {
21742               cp_lexer_consume_token (parser->lexer);
21743               bracecount++;
21744             }
21745           else if (bracecount
21746                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21747             cp_lexer_consume_token (parser->lexer);
21748           else
21749             {
21750               loc = cp_lexer_peek_token (parser->lexer)->location;
21751               error_at (loc, "not enough collapsed for loops");
21752               collapse_err = true;
21753               cp_parser_abort_tentative_parse (parser);
21754               declv = NULL_TREE;
21755               break;
21756             }
21757         }
21758       while (1);
21759
21760       if (declv)
21761         {
21762           cp_parser_parse_definitely (parser);
21763           nbraces += bracecount;
21764         }
21765     }
21766
21767   /* Note that we saved the original contents of this flag when we entered
21768      the structured block, and so we don't need to re-save it here.  */
21769   parser->in_statement = IN_OMP_FOR;
21770
21771   /* Note that the grammar doesn't call for a structured block here,
21772      though the loop as a whole is a structured block.  */
21773   body = push_stmt_list ();
21774   cp_parser_statement (parser, NULL_TREE, false, NULL);
21775   body = pop_stmt_list (body);
21776
21777   if (declv == NULL_TREE)
21778     ret = NULL_TREE;
21779   else
21780     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21781                           pre_body, clauses);
21782
21783   while (nbraces)
21784     {
21785       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21786         {
21787           cp_lexer_consume_token (parser->lexer);
21788           nbraces--;
21789         }
21790       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21791         cp_lexer_consume_token (parser->lexer);
21792       else
21793         {
21794           if (!collapse_err)
21795             {
21796               error_at (cp_lexer_peek_token (parser->lexer)->location,
21797                         "collapsed loops not perfectly nested");
21798             }
21799           collapse_err = true;
21800           cp_parser_statement_seq_opt (parser, NULL);
21801           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21802         }
21803     }
21804
21805   while (for_block)
21806     {
21807       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21808       for_block = TREE_CHAIN (for_block);
21809     }
21810
21811   return ret;
21812 }
21813
21814 /* OpenMP 2.5:
21815    #pragma omp for for-clause[optseq] new-line
21816      for-loop  */
21817
21818 #define OMP_FOR_CLAUSE_MASK                             \
21819         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21820         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21821         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21822         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21823         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21824         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21825         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21826         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21827
21828 static tree
21829 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21830 {
21831   tree clauses, sb, ret;
21832   unsigned int save;
21833
21834   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21835                                        "#pragma omp for", pragma_tok);
21836
21837   sb = begin_omp_structured_block ();
21838   save = cp_parser_begin_omp_structured_block (parser);
21839
21840   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21841
21842   cp_parser_end_omp_structured_block (parser, save);
21843   add_stmt (finish_omp_structured_block (sb));
21844
21845   return ret;
21846 }
21847
21848 /* OpenMP 2.5:
21849    # pragma omp master new-line
21850      structured-block  */
21851
21852 static tree
21853 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21854 {
21855   cp_parser_require_pragma_eol (parser, pragma_tok);
21856   return c_finish_omp_master (input_location,
21857                               cp_parser_omp_structured_block (parser));
21858 }
21859
21860 /* OpenMP 2.5:
21861    # pragma omp ordered new-line
21862      structured-block  */
21863
21864 static tree
21865 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21866 {
21867   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21868   cp_parser_require_pragma_eol (parser, pragma_tok);
21869   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21870 }
21871
21872 /* OpenMP 2.5:
21873
21874    section-scope:
21875      { section-sequence }
21876
21877    section-sequence:
21878      section-directive[opt] structured-block
21879      section-sequence section-directive structured-block  */
21880
21881 static tree
21882 cp_parser_omp_sections_scope (cp_parser *parser)
21883 {
21884   tree stmt, substmt;
21885   bool error_suppress = false;
21886   cp_token *tok;
21887
21888   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21889     return NULL_TREE;
21890
21891   stmt = push_stmt_list ();
21892
21893   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21894     {
21895       unsigned save;
21896
21897       substmt = begin_omp_structured_block ();
21898       save = cp_parser_begin_omp_structured_block (parser);
21899
21900       while (1)
21901         {
21902           cp_parser_statement (parser, NULL_TREE, false, NULL);
21903
21904           tok = cp_lexer_peek_token (parser->lexer);
21905           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21906             break;
21907           if (tok->type == CPP_CLOSE_BRACE)
21908             break;
21909           if (tok->type == CPP_EOF)
21910             break;
21911         }
21912
21913       cp_parser_end_omp_structured_block (parser, save);
21914       substmt = finish_omp_structured_block (substmt);
21915       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21916       add_stmt (substmt);
21917     }
21918
21919   while (1)
21920     {
21921       tok = cp_lexer_peek_token (parser->lexer);
21922       if (tok->type == CPP_CLOSE_BRACE)
21923         break;
21924       if (tok->type == CPP_EOF)
21925         break;
21926
21927       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21928         {
21929           cp_lexer_consume_token (parser->lexer);
21930           cp_parser_require_pragma_eol (parser, tok);
21931           error_suppress = false;
21932         }
21933       else if (!error_suppress)
21934         {
21935           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21936           error_suppress = true;
21937         }
21938
21939       substmt = cp_parser_omp_structured_block (parser);
21940       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21941       add_stmt (substmt);
21942     }
21943   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21944
21945   substmt = pop_stmt_list (stmt);
21946
21947   stmt = make_node (OMP_SECTIONS);
21948   TREE_TYPE (stmt) = void_type_node;
21949   OMP_SECTIONS_BODY (stmt) = substmt;
21950
21951   add_stmt (stmt);
21952   return stmt;
21953 }
21954
21955 /* OpenMP 2.5:
21956    # pragma omp sections sections-clause[optseq] newline
21957      sections-scope  */
21958
21959 #define OMP_SECTIONS_CLAUSE_MASK                        \
21960         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21961         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21962         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21963         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21964         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21965
21966 static tree
21967 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21968 {
21969   tree clauses, ret;
21970
21971   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21972                                        "#pragma omp sections", pragma_tok);
21973
21974   ret = cp_parser_omp_sections_scope (parser);
21975   if (ret)
21976     OMP_SECTIONS_CLAUSES (ret) = clauses;
21977
21978   return ret;
21979 }
21980
21981 /* OpenMP 2.5:
21982    # pragma parallel parallel-clause new-line
21983    # pragma parallel for parallel-for-clause new-line
21984    # pragma parallel sections parallel-sections-clause new-line  */
21985
21986 #define OMP_PARALLEL_CLAUSE_MASK                        \
21987         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21988         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21989         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21990         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21991         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21992         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21993         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21994         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21995
21996 static tree
21997 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21998 {
21999   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22000   const char *p_name = "#pragma omp parallel";
22001   tree stmt, clauses, par_clause, ws_clause, block;
22002   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22003   unsigned int save;
22004   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22005
22006   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22007     {
22008       cp_lexer_consume_token (parser->lexer);
22009       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22010       p_name = "#pragma omp parallel for";
22011       mask |= OMP_FOR_CLAUSE_MASK;
22012       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22013     }
22014   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22015     {
22016       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22017       const char *p = IDENTIFIER_POINTER (id);
22018       if (strcmp (p, "sections") == 0)
22019         {
22020           cp_lexer_consume_token (parser->lexer);
22021           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22022           p_name = "#pragma omp parallel sections";
22023           mask |= OMP_SECTIONS_CLAUSE_MASK;
22024           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22025         }
22026     }
22027
22028   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22029   block = begin_omp_parallel ();
22030   save = cp_parser_begin_omp_structured_block (parser);
22031
22032   switch (p_kind)
22033     {
22034     case PRAGMA_OMP_PARALLEL:
22035       cp_parser_statement (parser, NULL_TREE, false, NULL);
22036       par_clause = clauses;
22037       break;
22038
22039     case PRAGMA_OMP_PARALLEL_FOR:
22040       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22041       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22042       break;
22043
22044     case PRAGMA_OMP_PARALLEL_SECTIONS:
22045       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22046       stmt = cp_parser_omp_sections_scope (parser);
22047       if (stmt)
22048         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22049       break;
22050
22051     default:
22052       gcc_unreachable ();
22053     }
22054
22055   cp_parser_end_omp_structured_block (parser, save);
22056   stmt = finish_omp_parallel (par_clause, block);
22057   if (p_kind != PRAGMA_OMP_PARALLEL)
22058     OMP_PARALLEL_COMBINED (stmt) = 1;
22059   return stmt;
22060 }
22061
22062 /* OpenMP 2.5:
22063    # pragma omp single single-clause[optseq] new-line
22064      structured-block  */
22065
22066 #define OMP_SINGLE_CLAUSE_MASK                          \
22067         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22068         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22069         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22070         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22071
22072 static tree
22073 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22074 {
22075   tree stmt = make_node (OMP_SINGLE);
22076   TREE_TYPE (stmt) = void_type_node;
22077
22078   OMP_SINGLE_CLAUSES (stmt)
22079     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22080                                  "#pragma omp single", pragma_tok);
22081   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22082
22083   return add_stmt (stmt);
22084 }
22085
22086 /* OpenMP 3.0:
22087    # pragma omp task task-clause[optseq] new-line
22088      structured-block  */
22089
22090 #define OMP_TASK_CLAUSE_MASK                            \
22091         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22092         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22093         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22094         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22095         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22096         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22097
22098 static tree
22099 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22100 {
22101   tree clauses, block;
22102   unsigned int save;
22103
22104   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22105                                        "#pragma omp task", pragma_tok);
22106   block = begin_omp_task ();
22107   save = cp_parser_begin_omp_structured_block (parser);
22108   cp_parser_statement (parser, NULL_TREE, false, NULL);
22109   cp_parser_end_omp_structured_block (parser, save);
22110   return finish_omp_task (clauses, block);
22111 }
22112
22113 /* OpenMP 3.0:
22114    # pragma omp taskwait new-line  */
22115
22116 static void
22117 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22118 {
22119   cp_parser_require_pragma_eol (parser, pragma_tok);
22120   finish_omp_taskwait ();
22121 }
22122
22123 /* OpenMP 2.5:
22124    # pragma omp threadprivate (variable-list) */
22125
22126 static void
22127 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22128 {
22129   tree vars;
22130
22131   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22132   cp_parser_require_pragma_eol (parser, pragma_tok);
22133
22134   finish_omp_threadprivate (vars);
22135 }
22136
22137 /* Main entry point to OpenMP statement pragmas.  */
22138
22139 static void
22140 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22141 {
22142   tree stmt;
22143
22144   switch (pragma_tok->pragma_kind)
22145     {
22146     case PRAGMA_OMP_ATOMIC:
22147       cp_parser_omp_atomic (parser, pragma_tok);
22148       return;
22149     case PRAGMA_OMP_CRITICAL:
22150       stmt = cp_parser_omp_critical (parser, pragma_tok);
22151       break;
22152     case PRAGMA_OMP_FOR:
22153       stmt = cp_parser_omp_for (parser, pragma_tok);
22154       break;
22155     case PRAGMA_OMP_MASTER:
22156       stmt = cp_parser_omp_master (parser, pragma_tok);
22157       break;
22158     case PRAGMA_OMP_ORDERED:
22159       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22160       break;
22161     case PRAGMA_OMP_PARALLEL:
22162       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22163       break;
22164     case PRAGMA_OMP_SECTIONS:
22165       stmt = cp_parser_omp_sections (parser, pragma_tok);
22166       break;
22167     case PRAGMA_OMP_SINGLE:
22168       stmt = cp_parser_omp_single (parser, pragma_tok);
22169       break;
22170     case PRAGMA_OMP_TASK:
22171       stmt = cp_parser_omp_task (parser, pragma_tok);
22172       break;
22173     default:
22174       gcc_unreachable ();
22175     }
22176
22177   if (stmt)
22178     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22179 }
22180 \f
22181 /* The parser.  */
22182
22183 static GTY (()) cp_parser *the_parser;
22184
22185 \f
22186 /* Special handling for the first token or line in the file.  The first
22187    thing in the file might be #pragma GCC pch_preprocess, which loads a
22188    PCH file, which is a GC collection point.  So we need to handle this
22189    first pragma without benefit of an existing lexer structure.
22190
22191    Always returns one token to the caller in *FIRST_TOKEN.  This is
22192    either the true first token of the file, or the first token after
22193    the initial pragma.  */
22194
22195 static void
22196 cp_parser_initial_pragma (cp_token *first_token)
22197 {
22198   tree name = NULL;
22199
22200   cp_lexer_get_preprocessor_token (NULL, first_token);
22201   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22202     return;
22203
22204   cp_lexer_get_preprocessor_token (NULL, first_token);
22205   if (first_token->type == CPP_STRING)
22206     {
22207       name = first_token->u.value;
22208
22209       cp_lexer_get_preprocessor_token (NULL, first_token);
22210       if (first_token->type != CPP_PRAGMA_EOL)
22211         error_at (first_token->location,
22212                   "junk at end of %<#pragma GCC pch_preprocess%>");
22213     }
22214   else
22215     error_at (first_token->location, "expected string literal");
22216
22217   /* Skip to the end of the pragma.  */
22218   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22219     cp_lexer_get_preprocessor_token (NULL, first_token);
22220
22221   /* Now actually load the PCH file.  */
22222   if (name)
22223     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22224
22225   /* Read one more token to return to our caller.  We have to do this
22226      after reading the PCH file in, since its pointers have to be
22227      live.  */
22228   cp_lexer_get_preprocessor_token (NULL, first_token);
22229 }
22230
22231 /* Normal parsing of a pragma token.  Here we can (and must) use the
22232    regular lexer.  */
22233
22234 static bool
22235 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22236 {
22237   cp_token *pragma_tok;
22238   unsigned int id;
22239
22240   pragma_tok = cp_lexer_consume_token (parser->lexer);
22241   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22242   parser->lexer->in_pragma = true;
22243
22244   id = pragma_tok->pragma_kind;
22245   switch (id)
22246     {
22247     case PRAGMA_GCC_PCH_PREPROCESS:
22248       error_at (pragma_tok->location,
22249                 "%<#pragma GCC pch_preprocess%> must be first");
22250       break;
22251
22252     case PRAGMA_OMP_BARRIER:
22253       switch (context)
22254         {
22255         case pragma_compound:
22256           cp_parser_omp_barrier (parser, pragma_tok);
22257           return false;
22258         case pragma_stmt:
22259           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22260                     "used in compound statements");
22261           break;
22262         default:
22263           goto bad_stmt;
22264         }
22265       break;
22266
22267     case PRAGMA_OMP_FLUSH:
22268       switch (context)
22269         {
22270         case pragma_compound:
22271           cp_parser_omp_flush (parser, pragma_tok);
22272           return false;
22273         case pragma_stmt:
22274           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22275                     "used in compound statements");
22276           break;
22277         default:
22278           goto bad_stmt;
22279         }
22280       break;
22281
22282     case PRAGMA_OMP_TASKWAIT:
22283       switch (context)
22284         {
22285         case pragma_compound:
22286           cp_parser_omp_taskwait (parser, pragma_tok);
22287           return false;
22288         case pragma_stmt:
22289           error_at (pragma_tok->location,
22290                     "%<#pragma omp taskwait%> may only be "
22291                     "used in compound statements");
22292           break;
22293         default:
22294           goto bad_stmt;
22295         }
22296       break;
22297
22298     case PRAGMA_OMP_THREADPRIVATE:
22299       cp_parser_omp_threadprivate (parser, pragma_tok);
22300       return false;
22301
22302     case PRAGMA_OMP_ATOMIC:
22303     case PRAGMA_OMP_CRITICAL:
22304     case PRAGMA_OMP_FOR:
22305     case PRAGMA_OMP_MASTER:
22306     case PRAGMA_OMP_ORDERED:
22307     case PRAGMA_OMP_PARALLEL:
22308     case PRAGMA_OMP_SECTIONS:
22309     case PRAGMA_OMP_SINGLE:
22310     case PRAGMA_OMP_TASK:
22311       if (context == pragma_external)
22312         goto bad_stmt;
22313       cp_parser_omp_construct (parser, pragma_tok);
22314       return true;
22315
22316     case PRAGMA_OMP_SECTION:
22317       error_at (pragma_tok->location, 
22318                 "%<#pragma omp section%> may only be used in "
22319                 "%<#pragma omp sections%> construct");
22320       break;
22321
22322     default:
22323       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22324       c_invoke_pragma_handler (id);
22325       break;
22326
22327     bad_stmt:
22328       cp_parser_error (parser, "expected declaration specifiers");
22329       break;
22330     }
22331
22332   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22333   return false;
22334 }
22335
22336 /* The interface the pragma parsers have to the lexer.  */
22337
22338 enum cpp_ttype
22339 pragma_lex (tree *value)
22340 {
22341   cp_token *tok;
22342   enum cpp_ttype ret;
22343
22344   tok = cp_lexer_peek_token (the_parser->lexer);
22345
22346   ret = tok->type;
22347   *value = tok->u.value;
22348
22349   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22350     ret = CPP_EOF;
22351   else if (ret == CPP_STRING)
22352     *value = cp_parser_string_literal (the_parser, false, false);
22353   else
22354     {
22355       cp_lexer_consume_token (the_parser->lexer);
22356       if (ret == CPP_KEYWORD)
22357         ret = CPP_NAME;
22358     }
22359
22360   return ret;
22361 }
22362
22363 \f
22364 /* External interface.  */
22365
22366 /* Parse one entire translation unit.  */
22367
22368 void
22369 c_parse_file (void)
22370 {
22371   bool error_occurred;
22372   static bool already_called = false;
22373
22374   if (already_called)
22375     {
22376       sorry ("inter-module optimizations not implemented for C++");
22377       return;
22378     }
22379   already_called = true;
22380
22381   the_parser = cp_parser_new ();
22382   push_deferring_access_checks (flag_access_control
22383                                 ? dk_no_deferred : dk_no_check);
22384   error_occurred = cp_parser_translation_unit (the_parser);
22385   the_parser = NULL;
22386 }
22387
22388 #include "gt-cp-parser.h"