gcc50/csu: Skip depends step to avoid possible race
[dragonfly.git] / contrib / gcc-4.4 / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct cp_lexer GTY (())
107 {
108   /* The memory allocated for the buffer.  NULL if this lexer does not
109      own the token buffer.  */
110   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111   /* If the lexer owns the buffer, this is the number of tokens in the
112      buffer.  */
113   size_t buffer_length;
114
115   /* A pointer just past the last available token.  The tokens
116      in this lexer are [buffer, last_token).  */
117   cp_token_position GTY ((skip)) last_token;
118
119   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
120      no more available tokens.  */
121   cp_token_position GTY ((skip)) next_token;
122
123   /* A stack indicating positions at which cp_lexer_save_tokens was
124      called.  The top entry is the most recent position at which we
125      began saving tokens.  If the stack is non-empty, we are saving
126      tokens.  */
127   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128
129   /* The next lexer in a linked list of lexers.  */
130   struct cp_lexer *next;
131
132   /* True if we should output debugging information.  */
133   bool debugging_p;
134
135   /* True if we're in the context of parsing a pragma, and should not
136      increment past the end-of-line marker.  */
137   bool in_pragma;
138 } cp_lexer;
139
140 /* cp_token_cache is a range of tokens.  There is no need to represent
141    allocate heap memory for it, since tokens are never removed from the
142    lexer's array.  There is also no need for the GC to walk through
143    a cp_token_cache, since everything in here is referenced through
144    a lexer.  */
145
146 typedef struct cp_token_cache GTY(())
147 {
148   /* The beginning of the token range.  */
149   cp_token * GTY((skip)) first;
150
151   /* Points immediately after the last token in the range.  */
152   cp_token * GTY ((skip)) last;
153 } cp_token_cache;
154
155 /* Prototypes.  */
156
157 static cp_lexer *cp_lexer_new_main
158   (void);
159 static cp_lexer *cp_lexer_new_from_tokens
160   (cp_token_cache *tokens);
161 static void cp_lexer_destroy
162   (cp_lexer *);
163 static int cp_lexer_saving_tokens
164   (const cp_lexer *);
165 static cp_token_position cp_lexer_token_position
166   (cp_lexer *, bool);
167 static cp_token *cp_lexer_token_at
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170   (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
172   (cp_lexer *);
173 static cp_token *cp_lexer_peek_nth_token
174   (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178   (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180   (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
182   (cp_lexer *);
183 static void cp_lexer_purge_token
184   (cp_lexer *);
185 static void cp_lexer_purge_tokens_after
186   (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
188   (cp_lexer *);
189 static void cp_lexer_commit_tokens
190   (cp_lexer *);
191 static void cp_lexer_rollback_tokens
192   (cp_lexer *);
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195   (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
197   (cp_lexer *);
198 static void cp_lexer_start_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201   (cp_lexer *) ATTRIBUTE_UNUSED;
202 #else
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204    about passing NULL to functions that require non-NULL arguments
205    (fputs, fprintf).  It will never be used, so all we need is a value
206    of the right type that's guaranteed not to be NULL.  */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
211
212 static cp_token_cache *cp_token_cache_new
213   (cp_token *, cp_token *);
214
215 static void cp_parser_initial_pragma
216   (cp_token *);
217
218 /* Manifest constants.  */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
221
222 /* A token type for keywords, as opposed to ordinary identifiers.  */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
224
225 /* A token type for template-ids.  If a template-id is processed while
226    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227    the value of the CPP_TEMPLATE_ID is whatever was returned by
228    cp_parser_template_id.  */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
230
231 /* A token type for nested-name-specifiers.  If a
232    nested-name-specifier is processed while parsing tentatively, it is
233    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235    cp_parser_nested_name_specifier_opt.  */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
237
238 /* A token type for tokens that are not tokens at all; these are used
239    to represent slots in the array where there used to be a token
240    that has now been deleted.  */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
242
243 /* The number of token types, including C++-specific ones.  */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245
246 /* Variables.  */
247
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written.  */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
252
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
254    preprocessor.  */
255
256 static cp_lexer *
257 cp_lexer_new_main (void)
258 {
259   cp_token first_token;
260   cp_lexer *lexer;
261   cp_token *pos;
262   size_t alloc;
263   size_t space;
264   cp_token *buffer;
265
266   /* It's possible that parsing the first pragma will load a PCH file,
267      which is a GC collection point.  So we have to do that before
268      allocating any memory.  */
269   cp_parser_initial_pragma (&first_token);
270
271   c_common_no_more_pch ();
272
273   /* Allocate the memory.  */
274   lexer = GGC_CNEW (cp_lexer);
275
276 #ifdef ENABLE_CHECKING
277   /* Initially we are not debugging.  */
278   lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281                                    CP_SAVED_TOKEN_STACK);
282
283   /* Create the buffer.  */
284   alloc = CP_LEXER_BUFFER_SIZE;
285   buffer = GGC_NEWVEC (cp_token, alloc);
286
287   /* Put the first token in the buffer.  */
288   space = alloc;
289   pos = buffer;
290   *pos = first_token;
291
292   /* Get the remaining tokens from the preprocessor.  */
293   while (pos->type != CPP_EOF)
294     {
295       pos++;
296       if (!--space)
297         {
298           space = alloc;
299           alloc *= 2;
300           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301           pos = buffer + space;
302         }
303       cp_lexer_get_preprocessor_token (lexer, pos);
304     }
305   lexer->buffer = buffer;
306   lexer->buffer_length = alloc - space;
307   lexer->last_token = pos;
308   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
309
310   /* Subsequent preprocessor diagnostics should use compiler
311      diagnostic functions to get the compiler source location.  */
312   cpp_get_options (parse_in)->client_diagnostic = true;
313   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
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           /* Update the value.  Some keywords are mapped to particular
426              entities, rather than simply having the value of the
427              corresponding IDENTIFIER_NODE.  For example, `__const' is
428              mapped to `const'.  */
429           token->u.value = ridpointers[token->keyword];
430         }
431       else
432         {
433           if (warn_cxx0x_compat
434               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
435               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
436             {
437               /* Warn about the C++0x keyword (but still treat it as
438                  an identifier).  */
439               warning (OPT_Wc__0x_compat, 
440                        "identifier %<%s%> will become a keyword in C++0x",
441                        IDENTIFIER_POINTER (token->u.value));
442
443               /* Clear out the C_RID_CODE so we don't warn about this
444                  particular identifier-turned-keyword again.  */
445               C_SET_RID_CODE (token->u.value, RID_MAX);
446             }
447
448           token->ambiguous_p = false;
449           token->keyword = RID_MAX;
450         }
451     }
452   /* Handle Objective-C++ keywords.  */
453   else if (token->type == CPP_AT_NAME)
454     {
455       token->type = CPP_KEYWORD;
456       switch (C_RID_CODE (token->u.value))
457         {
458         /* Map 'class' to '@class', 'private' to '@private', etc.  */
459         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463         case RID_THROW: token->keyword = RID_AT_THROW; break;
464         case RID_TRY: token->keyword = RID_AT_TRY; break;
465         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466         default: token->keyword = C_RID_CODE (token->u.value);
467         }
468     }
469   else if (token->type == CPP_PRAGMA)
470     {
471       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
472       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473       token->u.value = NULL_TREE;
474     }
475 }
476
477 /* Update the globals input_location and the input file stack from TOKEN.  */
478 static inline void
479 cp_lexer_set_source_position_from_token (cp_token *token)
480 {
481   if (token->type != CPP_EOF)
482     {
483       input_location = token->location;
484     }
485 }
486
487 /* Return a pointer to the next token in the token stream, but do not
488    consume it.  */
489
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
492 {
493   if (cp_lexer_debugging_p (lexer))
494     {
495       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
496       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
497       putc ('\n', cp_lexer_debug_stream);
498     }
499   return lexer->next_token;
500 }
501
502 /* Return true if the next token has the indicated TYPE.  */
503
504 static inline bool
505 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
506 {
507   return cp_lexer_peek_token (lexer)->type == type;
508 }
509
510 /* Return true if the next token does not have the indicated TYPE.  */
511
512 static inline bool
513 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
514 {
515   return !cp_lexer_next_token_is (lexer, type);
516 }
517
518 /* Return true if the next token is the indicated KEYWORD.  */
519
520 static inline bool
521 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
522 {
523   return cp_lexer_peek_token (lexer)->keyword == keyword;
524 }
525
526 /* Return true if the next token is not the indicated KEYWORD.  */
527
528 static inline bool
529 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
530 {
531   return cp_lexer_peek_token (lexer)->keyword != keyword;
532 }
533
534 /* Return true if the next token is a keyword for a decl-specifier.  */
535
536 static bool
537 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
538 {
539   cp_token *token;
540
541   token = cp_lexer_peek_token (lexer);
542   switch (token->keyword) 
543     {
544       /* auto specifier: storage-class-specifier in C++,
545          simple-type-specifier in C++0x.  */
546     case RID_AUTO:
547       /* Storage classes.  */
548     case RID_REGISTER:
549     case RID_STATIC:
550     case RID_EXTERN:
551     case RID_MUTABLE:
552     case RID_THREAD:
553       /* Elaborated type specifiers.  */
554     case RID_ENUM:
555     case RID_CLASS:
556     case RID_STRUCT:
557     case RID_UNION:
558     case RID_TYPENAME:
559       /* Simple type specifiers.  */
560     case RID_CHAR:
561     case RID_CHAR16:
562     case RID_CHAR32:
563     case RID_WCHAR:
564     case RID_BOOL:
565     case RID_SHORT:
566     case RID_INT:
567     case RID_LONG:
568     case RID_SIGNED:
569     case RID_UNSIGNED:
570     case RID_FLOAT:
571     case RID_DOUBLE:
572     case RID_VOID:
573       /* GNU extensions.  */ 
574     case RID_ATTRIBUTE:
575     case RID_TYPEOF:
576       /* C++0x extensions.  */
577     case RID_DECLTYPE:
578       return true;
579
580     default:
581       return false;
582     }
583 }
584
585 /* Return a pointer to the Nth token in the token stream.  If N is 1,
586    then this is precisely equivalent to cp_lexer_peek_token (except
587    that it is not inline).  One would like to disallow that case, but
588    there is one case (cp_parser_nth_token_starts_template_id) where
589    the caller passes a variable for N and it might be 1.  */
590
591 static cp_token *
592 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
593 {
594   cp_token *token;
595
596   /* N is 1-based, not zero-based.  */
597   gcc_assert (n > 0);
598
599   if (cp_lexer_debugging_p (lexer))
600     fprintf (cp_lexer_debug_stream,
601              "cp_lexer: peeking ahead %ld at token: ", (long)n);
602
603   --n;
604   token = lexer->next_token;
605   gcc_assert (!n || token != &eof_token);
606   while (n != 0)
607     {
608       ++token;
609       if (token == lexer->last_token)
610         {
611           token = &eof_token;
612           break;
613         }
614
615       if (token->type != CPP_PURGED)
616         --n;
617     }
618
619   if (cp_lexer_debugging_p (lexer))
620     {
621       cp_lexer_print_token (cp_lexer_debug_stream, token);
622       putc ('\n', cp_lexer_debug_stream);
623     }
624
625   return token;
626 }
627
628 /* Return the next token, and advance the lexer's next_token pointer
629    to point to the next non-purged token.  */
630
631 static cp_token *
632 cp_lexer_consume_token (cp_lexer* lexer)
633 {
634   cp_token *token = lexer->next_token;
635
636   gcc_assert (token != &eof_token);
637   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638
639   do
640     {
641       lexer->next_token++;
642       if (lexer->next_token == lexer->last_token)
643         {
644           lexer->next_token = &eof_token;
645           break;
646         }
647
648     }
649   while (lexer->next_token->type == CPP_PURGED);
650
651   cp_lexer_set_source_position_from_token (token);
652
653   /* Provide debugging output.  */
654   if (cp_lexer_debugging_p (lexer))
655     {
656       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
657       cp_lexer_print_token (cp_lexer_debug_stream, token);
658       putc ('\n', cp_lexer_debug_stream);
659     }
660
661   return token;
662 }
663
664 /* Permanently remove the next token from the token stream, and
665    advance the next_token pointer to refer to the next non-purged
666    token.  */
667
668 static void
669 cp_lexer_purge_token (cp_lexer *lexer)
670 {
671   cp_token *tok = lexer->next_token;
672
673   gcc_assert (tok != &eof_token);
674   tok->type = CPP_PURGED;
675   tok->location = UNKNOWN_LOCATION;
676   tok->u.value = NULL_TREE;
677   tok->keyword = RID_MAX;
678
679   do
680     {
681       tok++;
682       if (tok == lexer->last_token)
683         {
684           tok = &eof_token;
685           break;
686         }
687     }
688   while (tok->type == CPP_PURGED);
689   lexer->next_token = tok;
690 }
691
692 /* Permanently remove all tokens after TOK, up to, but not
693    including, the token that will be returned next by
694    cp_lexer_peek_token.  */
695
696 static void
697 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
698 {
699   cp_token *peek = lexer->next_token;
700
701   if (peek == &eof_token)
702     peek = lexer->last_token;
703
704   gcc_assert (tok < peek);
705
706   for ( tok += 1; tok != peek; tok += 1)
707     {
708       tok->type = CPP_PURGED;
709       tok->location = UNKNOWN_LOCATION;
710       tok->u.value = NULL_TREE;
711       tok->keyword = RID_MAX;
712     }
713 }
714
715 /* Begin saving tokens.  All tokens consumed after this point will be
716    preserved.  */
717
718 static void
719 cp_lexer_save_tokens (cp_lexer* lexer)
720 {
721   /* Provide debugging output.  */
722   if (cp_lexer_debugging_p (lexer))
723     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
724
725   VEC_safe_push (cp_token_position, heap,
726                  lexer->saved_tokens, lexer->next_token);
727 }
728
729 /* Commit to the portion of the token stream most recently saved.  */
730
731 static void
732 cp_lexer_commit_tokens (cp_lexer* lexer)
733 {
734   /* Provide debugging output.  */
735   if (cp_lexer_debugging_p (lexer))
736     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
737
738   VEC_pop (cp_token_position, lexer->saved_tokens);
739 }
740
741 /* Return all tokens saved since the last call to cp_lexer_save_tokens
742    to the token stream.  Stop saving tokens.  */
743
744 static void
745 cp_lexer_rollback_tokens (cp_lexer* lexer)
746 {
747   /* Provide debugging output.  */
748   if (cp_lexer_debugging_p (lexer))
749     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
750
751   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
752 }
753
754 /* Print a representation of the TOKEN on the STREAM.  */
755
756 #ifdef ENABLE_CHECKING
757
758 static void
759 cp_lexer_print_token (FILE * stream, cp_token *token)
760 {
761   /* We don't use cpp_type2name here because the parser defines
762      a few tokens of its own.  */
763   static const char *const token_names[] = {
764     /* cpplib-defined token types */
765 #define OP(e, s) #e,
766 #define TK(e, s) #e,
767     TTYPE_TABLE
768 #undef OP
769 #undef TK
770     /* C++ parser token types - see "Manifest constants", above.  */
771     "KEYWORD",
772     "TEMPLATE_ID",
773     "NESTED_NAME_SPECIFIER",
774     "PURGED"
775   };
776
777   /* If we have a name for the token, print it out.  Otherwise, we
778      simply give the numeric code.  */
779   gcc_assert (token->type < ARRAY_SIZE(token_names));
780   fputs (token_names[token->type], stream);
781
782   /* For some tokens, print the associated data.  */
783   switch (token->type)
784     {
785     case CPP_KEYWORD:
786       /* Some keywords have a value that is not an IDENTIFIER_NODE.
787          For example, `struct' is mapped to an INTEGER_CST.  */
788       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
789         break;
790       /* else fall through */
791     case CPP_NAME:
792       fputs (IDENTIFIER_POINTER (token->u.value), stream);
793       break;
794
795     case CPP_STRING:
796     case CPP_STRING16:
797     case CPP_STRING32:
798     case CPP_WSTRING:
799       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800       break;
801
802     default:
803       break;
804     }
805 }
806
807 /* Start emitting debugging information.  */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812   lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information.  */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820   lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens.  */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830   cp_token_cache *cache = GGC_NEW (cp_token_cache);
831   cache->first = first;
832   cache->last = last;
833   return cache;
834 }
835
836 \f
837 /* Decl-specifiers.  */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators.  */
848
849 /* Nothing other than the parser should be creating declarators;
850    declarators are a semi-syntactic representation of C++ entities.
851    Other parts of the front end that need to create entities (like
852    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
853
854 static cp_declarator *make_call_declarator
855   (cp_declarator *, tree, cp_cv_quals, tree, tree);
856 static cp_declarator *make_array_declarator
857   (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859   (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861   (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863   (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865   (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator.  */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871    allocated.  */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool.  */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879   return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND.  Clear fields that are
883    common to all declarators.  */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888   cp_declarator *declarator;
889
890   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891   declarator->kind = kind;
892   declarator->attributes = NULL_TREE;
893   declarator->declarator = NULL;
894   declarator->parameter_pack_p = false;
895
896   return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier.  If
900    QUALIFYING_SCOPE is non-NULL, the identifier is
901    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
903    is, if any.   */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907                     special_function_kind sfk)
908 {
909   cp_declarator *declarator;
910
911   /* It is valid to write:
912
913        class C { void f(); };
914        typedef C D;
915        void D::f();
916
917      The standard is not clear about whether `typedef const C D' is
918      legal; as of 2002-09-15 the committee is considering that
919      question.  EDG 3.0 allows that syntax.  Therefore, we do as
920      well.  */
921   if (qualifying_scope && TYPE_P (qualifying_scope))
922     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928   declarator = make_declarator (cdk_id);
929   declarator->u.id.qualifying_scope = qualifying_scope;
930   declarator->u.id.unqualified_name = unqualified_name;
931   declarator->u.id.sfk = sfk;
932   
933   return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
937    of modifiers such as const or volatile to apply to the pointer
938    type, represented as identifiers.  */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943   cp_declarator *declarator;
944
945   declarator = make_declarator (cdk_pointer);
946   declarator->declarator = target;
947   declarator->u.pointer.qualifiers = cv_qualifiers;
948   declarator->u.pointer.class_type = NULL_TREE;
949   if (target)
950     {
951       declarator->parameter_pack_p = target->parameter_pack_p;
952       target->parameter_pack_p = false;
953     }
954   else
955     declarator->parameter_pack_p = false;
956
957   return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references.  */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
964                            bool rvalue_ref)
965 {
966   cp_declarator *declarator;
967
968   declarator = make_declarator (cdk_reference);
969   declarator->declarator = target;
970   declarator->u.reference.qualifiers = cv_qualifiers;
971   declarator->u.reference.rvalue_ref = rvalue_ref;
972   if (target)
973     {
974       declarator->parameter_pack_p = target->parameter_pack_p;
975       target->parameter_pack_p = false;
976     }
977   else
978     declarator->parameter_pack_p = false;
979
980   return declarator;
981 }
982
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984    member of CLASS_TYPE.  */
985
986 cp_declarator *
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988                         cp_declarator *pointee)
989 {
990   cp_declarator *declarator;
991
992   declarator = make_declarator (cdk_ptrmem);
993   declarator->declarator = pointee;
994   declarator->u.pointer.qualifiers = cv_qualifiers;
995   declarator->u.pointer.class_type = class_type;
996
997   if (pointee)
998     {
999       declarator->parameter_pack_p = pointee->parameter_pack_p;
1000       pointee->parameter_pack_p = false;
1001     }
1002   else
1003     declarator->parameter_pack_p = false;
1004
1005   return declarator;
1006 }
1007
1008 /* Make a declarator for the function given by TARGET, with the
1009    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1010    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1011    indicates what exceptions can be thrown.  */
1012
1013 cp_declarator *
1014 make_call_declarator (cp_declarator *target,
1015                       tree parms,
1016                       cp_cv_quals cv_qualifiers,
1017                       tree exception_specification,
1018                       tree late_return_type)
1019 {
1020   cp_declarator *declarator;
1021
1022   declarator = make_declarator (cdk_function);
1023   declarator->declarator = target;
1024   declarator->u.function.parameters = parms;
1025   declarator->u.function.qualifiers = cv_qualifiers;
1026   declarator->u.function.exception_specification = exception_specification;
1027   declarator->u.function.late_return_type = late_return_type;
1028   if (target)
1029     {
1030       declarator->parameter_pack_p = target->parameter_pack_p;
1031       target->parameter_pack_p = false;
1032     }
1033   else
1034     declarator->parameter_pack_p = false;
1035
1036   return declarator;
1037 }
1038
1039 /* Make a declarator for an array of BOUNDS elements, each of which is
1040    defined by ELEMENT.  */
1041
1042 cp_declarator *
1043 make_array_declarator (cp_declarator *element, tree bounds)
1044 {
1045   cp_declarator *declarator;
1046
1047   declarator = make_declarator (cdk_array);
1048   declarator->declarator = element;
1049   declarator->u.array.bounds = bounds;
1050   if (element)
1051     {
1052       declarator->parameter_pack_p = element->parameter_pack_p;
1053       element->parameter_pack_p = false;
1054     }
1055   else
1056     declarator->parameter_pack_p = false;
1057
1058   return declarator;
1059 }
1060
1061 /* Determine whether the declarator we've seen so far can be a
1062    parameter pack, when followed by an ellipsis.  */
1063 static bool 
1064 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 {
1066   /* Search for a declarator name, or any other declarator that goes
1067      after the point where the ellipsis could appear in a parameter
1068      pack. If we find any of these, then this declarator can not be
1069      made into a parameter pack.  */
1070   bool found = false;
1071   while (declarator && !found)
1072     {
1073       switch ((int)declarator->kind)
1074         {
1075         case cdk_id:
1076         case cdk_array:
1077           found = true;
1078           break;
1079
1080         case cdk_error:
1081           return true;
1082
1083         default:
1084           declarator = declarator->declarator;
1085           break;
1086         }
1087     }
1088
1089   return !found;
1090 }
1091
1092 cp_parameter_declarator *no_parameters;
1093
1094 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1095    DECLARATOR and DEFAULT_ARGUMENT.  */
1096
1097 cp_parameter_declarator *
1098 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1099                            cp_declarator *declarator,
1100                            tree default_argument)
1101 {
1102   cp_parameter_declarator *parameter;
1103
1104   parameter = ((cp_parameter_declarator *)
1105                alloc_declarator (sizeof (cp_parameter_declarator)));
1106   parameter->next = NULL;
1107   if (decl_specifiers)
1108     parameter->decl_specifiers = *decl_specifiers;
1109   else
1110     clear_decl_specs (&parameter->decl_specifiers);
1111   parameter->declarator = declarator;
1112   parameter->default_argument = default_argument;
1113   parameter->ellipsis_p = false;
1114
1115   return parameter;
1116 }
1117
1118 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1119
1120 static bool
1121 function_declarator_p (const cp_declarator *declarator)
1122 {
1123   while (declarator)
1124     {
1125       if (declarator->kind == cdk_function
1126           && declarator->declarator->kind == cdk_id)
1127         return true;
1128       if (declarator->kind == cdk_id
1129           || declarator->kind == cdk_error)
1130         return false;
1131       declarator = declarator->declarator;
1132     }
1133   return false;
1134 }
1135  
1136 /* The parser.  */
1137
1138 /* Overview
1139    --------
1140
1141    A cp_parser parses the token stream as specified by the C++
1142    grammar.  Its job is purely parsing, not semantic analysis.  For
1143    example, the parser breaks the token stream into declarators,
1144    expressions, statements, and other similar syntactic constructs.
1145    It does not check that the types of the expressions on either side
1146    of an assignment-statement are compatible, or that a function is
1147    not declared with a parameter of type `void'.
1148
1149    The parser invokes routines elsewhere in the compiler to perform
1150    semantic analysis and to build up the abstract syntax tree for the
1151    code processed.
1152
1153    The parser (and the template instantiation code, which is, in a
1154    way, a close relative of parsing) are the only parts of the
1155    compiler that should be calling push_scope and pop_scope, or
1156    related functions.  The parser (and template instantiation code)
1157    keeps track of what scope is presently active; everything else
1158    should simply honor that.  (The code that generates static
1159    initializers may also need to set the scope, in order to check
1160    access control correctly when emitting the initializers.)
1161
1162    Methodology
1163    -----------
1164
1165    The parser is of the standard recursive-descent variety.  Upcoming
1166    tokens in the token stream are examined in order to determine which
1167    production to use when parsing a non-terminal.  Some C++ constructs
1168    require arbitrary look ahead to disambiguate.  For example, it is
1169    impossible, in the general case, to tell whether a statement is an
1170    expression or declaration without scanning the entire statement.
1171    Therefore, the parser is capable of "parsing tentatively."  When the
1172    parser is not sure what construct comes next, it enters this mode.
1173    Then, while we attempt to parse the construct, the parser queues up
1174    error messages, rather than issuing them immediately, and saves the
1175    tokens it consumes.  If the construct is parsed successfully, the
1176    parser "commits", i.e., it issues any queued error messages and
1177    the tokens that were being preserved are permanently discarded.
1178    If, however, the construct is not parsed successfully, the parser
1179    rolls back its state completely so that it can resume parsing using
1180    a different alternative.
1181
1182    Future Improvements
1183    -------------------
1184
1185    The performance of the parser could probably be improved substantially.
1186    We could often eliminate the need to parse tentatively by looking ahead
1187    a little bit.  In some places, this approach might not entirely eliminate
1188    the need to parse tentatively, but it might still speed up the average
1189    case.  */
1190
1191 /* Flags that are passed to some parsing functions.  These values can
1192    be bitwise-ored together.  */
1193
1194 typedef enum cp_parser_flags
1195 {
1196   /* No flags.  */
1197   CP_PARSER_FLAGS_NONE = 0x0,
1198   /* The construct is optional.  If it is not present, then no error
1199      should be issued.  */
1200   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1201   /* When parsing a type-specifier, treat user-defined type-names
1202      as non-type identifiers.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1204   /* When parsing a type-specifier, do not try to parse a class-specifier
1205      or enum-specifier.  */
1206   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1207 } cp_parser_flags;
1208
1209 /* The different kinds of declarators we want to parse.  */
1210
1211 typedef enum cp_parser_declarator_kind
1212 {
1213   /* We want an abstract declarator.  */
1214   CP_PARSER_DECLARATOR_ABSTRACT,
1215   /* We want a named declarator.  */
1216   CP_PARSER_DECLARATOR_NAMED,
1217   /* We don't mind, but the name must be an unqualified-id.  */
1218   CP_PARSER_DECLARATOR_EITHER
1219 } cp_parser_declarator_kind;
1220
1221 /* The precedence values used to parse binary expressions.  The minimum value
1222    of PREC must be 1, because zero is reserved to quickly discriminate
1223    binary operators from other tokens.  */
1224
1225 enum cp_parser_prec
1226 {
1227   PREC_NOT_OPERATOR,
1228   PREC_LOGICAL_OR_EXPRESSION,
1229   PREC_LOGICAL_AND_EXPRESSION,
1230   PREC_INCLUSIVE_OR_EXPRESSION,
1231   PREC_EXCLUSIVE_OR_EXPRESSION,
1232   PREC_AND_EXPRESSION,
1233   PREC_EQUALITY_EXPRESSION,
1234   PREC_RELATIONAL_EXPRESSION,
1235   PREC_SHIFT_EXPRESSION,
1236   PREC_ADDITIVE_EXPRESSION,
1237   PREC_MULTIPLICATIVE_EXPRESSION,
1238   PREC_PM_EXPRESSION,
1239   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1240 };
1241
1242 /* A mapping from a token type to a corresponding tree node type, with a
1243    precedence value.  */
1244
1245 typedef struct cp_parser_binary_operations_map_node
1246 {
1247   /* The token type.  */
1248   enum cpp_ttype token_type;
1249   /* The corresponding tree code.  */
1250   enum tree_code tree_type;
1251   /* The precedence of this operator.  */
1252   enum cp_parser_prec prec;
1253 } cp_parser_binary_operations_map_node;
1254
1255 /* The status of a tentative parse.  */
1256
1257 typedef enum cp_parser_status_kind
1258 {
1259   /* No errors have occurred.  */
1260   CP_PARSER_STATUS_KIND_NO_ERROR,
1261   /* An error has occurred.  */
1262   CP_PARSER_STATUS_KIND_ERROR,
1263   /* We are committed to this tentative parse, whether or not an error
1264      has occurred.  */
1265   CP_PARSER_STATUS_KIND_COMMITTED
1266 } cp_parser_status_kind;
1267
1268 typedef struct cp_parser_expression_stack_entry
1269 {
1270   /* Left hand side of the binary operation we are currently
1271      parsing.  */
1272   tree lhs;
1273   /* Original tree code for left hand side, if it was a binary
1274      expression itself (used for -Wparentheses).  */
1275   enum tree_code lhs_type;
1276   /* Tree code for the binary operation we are parsing.  */
1277   enum tree_code tree_type;
1278   /* Precedence of the binary operation we are parsing.  */
1279   int prec;
1280 } cp_parser_expression_stack_entry;
1281
1282 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1283    entries because precedence levels on the stack are monotonically
1284    increasing.  */
1285 typedef struct cp_parser_expression_stack_entry
1286   cp_parser_expression_stack[NUM_PREC_VALUES];
1287
1288 /* Context that is saved and restored when parsing tentatively.  */
1289 typedef struct cp_parser_context GTY (())
1290 {
1291   /* If this is a tentative parsing context, the status of the
1292      tentative parse.  */
1293   enum cp_parser_status_kind status;
1294   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1295      that are looked up in this context must be looked up both in the
1296      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297      the context of the containing expression.  */
1298   tree object_type;
1299
1300   /* The next parsing context in the stack.  */
1301   struct cp_parser_context *next;
1302 } cp_parser_context;
1303
1304 /* Prototypes.  */
1305
1306 /* Constructors and destructors.  */
1307
1308 static cp_parser_context *cp_parser_context_new
1309   (cp_parser_context *);
1310
1311 /* Class variables.  */
1312
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1314
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316    Transformed into an associative array (binops_by_token) by
1317    cp_parser_new.  */
1318
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1322
1323   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
1327   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329
1330   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332
1333   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
1338   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1340
1341   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1342
1343   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1344
1345   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1348
1349   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1350 };
1351
1352 /* The same as binops, but initialized by cp_parser_new so that
1353    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1354    for speed.  */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1356
1357 /* Constructors and destructors.  */
1358
1359 /* Construct a new context.  The context below this one on the stack
1360    is given by NEXT.  */
1361
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1364 {
1365   cp_parser_context *context;
1366
1367   /* Allocate the storage.  */
1368   if (cp_parser_context_free_list != NULL)
1369     {
1370       /* Pull the first entry from the free list.  */
1371       context = cp_parser_context_free_list;
1372       cp_parser_context_free_list = context->next;
1373       memset (context, 0, sizeof (*context));
1374     }
1375   else
1376     context = GGC_CNEW (cp_parser_context);
1377
1378   /* No errors have occurred yet in this context.  */
1379   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380   /* If this is not the bottommost context, copy information that we
1381      need from the previous context.  */
1382   if (next)
1383     {
1384       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385          expression, then we are parsing one in this context, too.  */
1386       context->object_type = next->object_type;
1387       /* Thread the stack.  */
1388       context->next = next;
1389     }
1390
1391   return context;
1392 }
1393
1394 /* The cp_parser structure represents the C++ parser.  */
1395
1396 typedef struct cp_parser GTY(())
1397 {
1398   /* The lexer from which we are obtaining tokens.  */
1399   cp_lexer *lexer;
1400
1401   /* The scope in which names should be looked up.  If NULL_TREE, then
1402      we look up names in the scope that is currently open in the
1403      source program.  If non-NULL, this is either a TYPE or
1404      NAMESPACE_DECL for the scope in which we should look.  It can
1405      also be ERROR_MARK, when we've parsed a bogus scope.
1406
1407      This value is not cleared automatically after a name is looked
1408      up, so we must be careful to clear it before starting a new look
1409      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1410      will look up `Z' in the scope of `X', rather than the current
1411      scope.)  Unfortunately, it is difficult to tell when name lookup
1412      is complete, because we sometimes peek at a token, look it up,
1413      and then decide not to consume it.   */
1414   tree scope;
1415
1416   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417      last lookup took place.  OBJECT_SCOPE is used if an expression
1418      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419      respectively.  QUALIFYING_SCOPE is used for an expression of the
1420      form "X::Y"; it refers to X.  */
1421   tree object_scope;
1422   tree qualifying_scope;
1423
1424   /* A stack of parsing contexts.  All but the bottom entry on the
1425      stack will be tentative contexts.
1426
1427      We parse tentatively in order to determine which construct is in
1428      use in some situations.  For example, in order to determine
1429      whether a statement is an expression-statement or a
1430      declaration-statement we parse it tentatively as a
1431      declaration-statement.  If that fails, we then reparse the same
1432      token stream as an expression-statement.  */
1433   cp_parser_context *context;
1434
1435   /* True if we are parsing GNU C++.  If this flag is not set, then
1436      GNU extensions are not recognized.  */
1437   bool allow_gnu_extensions_p;
1438
1439   /* TRUE if the `>' token should be interpreted as the greater-than
1440      operator.  FALSE if it is the end of a template-id or
1441      template-parameter-list. In C++0x mode, this flag also applies to
1442      `>>' tokens, which are viewed as two consecutive `>' tokens when
1443      this flag is FALSE.  */
1444   bool greater_than_is_operator_p;
1445
1446   /* TRUE if default arguments are allowed within a parameter list
1447      that starts at this point. FALSE if only a gnu extension makes
1448      them permissible.  */
1449   bool default_arg_ok_p;
1450
1451   /* TRUE if we are parsing an integral constant-expression.  See
1452      [expr.const] for a precise definition.  */
1453   bool integral_constant_expression_p;
1454
1455   /* TRUE if we are parsing an integral constant-expression -- but a
1456      non-constant expression should be permitted as well.  This flag
1457      is used when parsing an array bound so that GNU variable-length
1458      arrays are tolerated.  */
1459   bool allow_non_integral_constant_expression_p;
1460
1461   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462      been seen that makes the expression non-constant.  */
1463   bool non_integral_constant_expression_p;
1464
1465   /* TRUE if local variable names and `this' are forbidden in the
1466      current context.  */
1467   bool local_variables_forbidden_p;
1468
1469   /* TRUE if the declaration we are parsing is part of a
1470      linkage-specification of the form `extern string-literal
1471      declaration'.  */
1472   bool in_unbraced_linkage_specification_p;
1473
1474   /* TRUE if we are presently parsing a declarator, after the
1475      direct-declarator.  */
1476   bool in_declarator_p;
1477
1478   /* TRUE if we are presently parsing a template-argument-list.  */
1479   bool in_template_argument_list_p;
1480
1481   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482      to IN_OMP_BLOCK if parsing OpenMP structured block and
1483      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1484      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485      iteration-statement, OpenMP block or loop within that switch.  */
1486 #define IN_SWITCH_STMT          1
1487 #define IN_ITERATION_STMT       2
1488 #define IN_OMP_BLOCK            4
1489 #define IN_OMP_FOR              8
1490 #define IN_IF_STMT             16
1491   unsigned char in_statement;
1492
1493   /* TRUE if we are presently parsing the body of a switch statement.
1494      Note that this doesn't quite overlap with in_statement above.
1495      The difference relates to giving the right sets of error messages:
1496      "case not in switch" vs "break statement used with OpenMP...".  */
1497   bool in_switch_statement_p;
1498
1499   /* TRUE if we are parsing a type-id in an expression context.  In
1500      such a situation, both "type (expr)" and "type (type)" are valid
1501      alternatives.  */
1502   bool in_type_id_in_expr_p;
1503
1504   /* TRUE if we are currently in a header file where declarations are
1505      implicitly extern "C".  */
1506   bool implicit_extern_c;
1507
1508   /* TRUE if strings in expressions should be translated to the execution
1509      character set.  */
1510   bool translate_strings_p;
1511
1512   /* TRUE if we are presently parsing the body of a function, but not
1513      a local class.  */
1514   bool in_function_body;
1515
1516   /* If non-NULL, then we are parsing a construct where new type
1517      definitions are not permitted.  The string stored here will be
1518      issued as an error message if a type is defined.  */
1519   const char *type_definition_forbidden_message;
1520
1521   /* A list of lists. The outer list is a stack, used for member
1522      functions of local classes. At each level there are two sub-list,
1523      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525      TREE_VALUE's. The functions are chained in reverse declaration
1526      order.
1527
1528      The TREE_PURPOSE sublist contains those functions with default
1529      arguments that need post processing, and the TREE_VALUE sublist
1530      contains those functions with definitions that need post
1531      processing.
1532
1533      These lists can only be processed once the outermost class being
1534      defined is complete.  */
1535   tree unparsed_functions_queues;
1536
1537   /* The number of classes whose definitions are currently in
1538      progress.  */
1539   unsigned num_classes_being_defined;
1540
1541   /* The number of template parameter lists that apply directly to the
1542      current declaration.  */
1543   unsigned num_template_parameter_lists;
1544 } cp_parser;
1545
1546 /* Prototypes.  */
1547
1548 /* Constructors and destructors.  */
1549
1550 static cp_parser *cp_parser_new
1551   (void);
1552
1553 /* Routines to parse various constructs.
1554
1555    Those that return `tree' will return the error_mark_node (rather
1556    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557    Sometimes, they will return an ordinary node if error-recovery was
1558    attempted, even though a parse error occurred.  So, to check
1559    whether or not a parse error occurred, you should always use
1560    cp_parser_error_occurred.  If the construct is optional (indicated
1561    either by an `_opt' in the name of the function that does the
1562    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563    the construct is not present.  */
1564
1565 /* Lexical conventions [gram.lex]  */
1566
1567 static tree cp_parser_identifier
1568   (cp_parser *);
1569 static tree cp_parser_string_literal
1570   (cp_parser *, bool, bool);
1571
1572 /* Basic concepts [gram.basic]  */
1573
1574 static bool cp_parser_translation_unit
1575   (cp_parser *);
1576
1577 /* Expressions [gram.expr]  */
1578
1579 static tree cp_parser_primary_expression
1580   (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582   (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584   (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586   (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588   (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590   (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592   (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594   (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static tree cp_parser_parenthesized_expression_list
1598   (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600   (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602   (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1604   (cp_token *);
1605 static tree cp_parser_new_expression
1606   (cp_parser *);
1607 static tree cp_parser_new_placement
1608   (cp_parser *);
1609 static tree cp_parser_new_type_id
1610   (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612   (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614   (cp_parser *);
1615 static tree cp_parser_new_initializer
1616   (cp_parser *);
1617 static tree cp_parser_delete_expression
1618   (cp_parser *);
1619 static tree cp_parser_cast_expression
1620   (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624   (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626   (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628   (cp_parser *);
1629 static tree cp_parser_expression
1630   (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632   (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634   (cp_parser *);
1635
1636 /* Statements [gram.stmt.stmt]  */
1637
1638 static void cp_parser_statement
1639   (cp_parser *, tree, bool, bool *);
1640 static void cp_parser_label_for_labeled_statement
1641   (cp_parser *);
1642 static tree cp_parser_expression_statement
1643   (cp_parser *, tree);
1644 static tree cp_parser_compound_statement
1645   (cp_parser *, tree, bool);
1646 static void cp_parser_statement_seq_opt
1647   (cp_parser *, tree);
1648 static tree cp_parser_selection_statement
1649   (cp_parser *, bool *);
1650 static tree cp_parser_condition
1651   (cp_parser *);
1652 static tree cp_parser_iteration_statement
1653   (cp_parser *);
1654 static void cp_parser_for_init_statement
1655   (cp_parser *);
1656 static tree cp_parser_jump_statement
1657   (cp_parser *);
1658 static void cp_parser_declaration_statement
1659   (cp_parser *);
1660
1661 static tree cp_parser_implicitly_scoped_statement
1662   (cp_parser *, bool *);
1663 static void cp_parser_already_scoped_statement
1664   (cp_parser *);
1665
1666 /* Declarations [gram.dcl.dcl] */
1667
1668 static void cp_parser_declaration_seq_opt
1669   (cp_parser *);
1670 static void cp_parser_declaration
1671   (cp_parser *);
1672 static void cp_parser_block_declaration
1673   (cp_parser *, bool);
1674 static void cp_parser_simple_declaration
1675   (cp_parser *, bool);
1676 static void cp_parser_decl_specifier_seq
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1678 static tree cp_parser_storage_class_specifier_opt
1679   (cp_parser *);
1680 static tree cp_parser_function_specifier_opt
1681   (cp_parser *, cp_decl_specifier_seq *);
1682 static tree cp_parser_type_specifier
1683   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1684    int *, bool *);
1685 static tree cp_parser_simple_type_specifier
1686   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1687 static tree cp_parser_type_name
1688   (cp_parser *);
1689 static tree cp_parser_nonclass_name 
1690   (cp_parser* parser);
1691 static tree cp_parser_elaborated_type_specifier
1692   (cp_parser *, bool, bool);
1693 static tree cp_parser_enum_specifier
1694   (cp_parser *);
1695 static void cp_parser_enumerator_list
1696   (cp_parser *, tree);
1697 static void cp_parser_enumerator_definition
1698   (cp_parser *, tree);
1699 static tree cp_parser_namespace_name
1700   (cp_parser *);
1701 static void cp_parser_namespace_definition
1702   (cp_parser *);
1703 static void cp_parser_namespace_body
1704   (cp_parser *);
1705 static tree cp_parser_qualified_namespace_specifier
1706   (cp_parser *);
1707 static void cp_parser_namespace_alias_definition
1708   (cp_parser *);
1709 static bool cp_parser_using_declaration
1710   (cp_parser *, bool);
1711 static void cp_parser_using_directive
1712   (cp_parser *);
1713 static void cp_parser_asm_definition
1714   (cp_parser *);
1715 static void cp_parser_linkage_specification
1716   (cp_parser *);
1717 static void cp_parser_static_assert
1718   (cp_parser *, bool);
1719 static tree cp_parser_decltype
1720   (cp_parser *);
1721
1722 /* Declarators [gram.dcl.decl] */
1723
1724 static tree cp_parser_init_declarator
1725   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1726 static cp_declarator *cp_parser_declarator
1727   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1728 static cp_declarator *cp_parser_direct_declarator
1729   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1730 static enum tree_code cp_parser_ptr_operator
1731   (cp_parser *, tree *, cp_cv_quals *);
1732 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1733   (cp_parser *);
1734 static tree cp_parser_late_return_type_opt
1735   (cp_parser *);
1736 static tree cp_parser_declarator_id
1737   (cp_parser *, bool);
1738 static tree cp_parser_type_id
1739   (cp_parser *);
1740 static tree cp_parser_template_type_arg
1741   (cp_parser *);
1742 static tree cp_parser_trailing_type_id (cp_parser *);
1743 static tree cp_parser_type_id_1
1744   (cp_parser *, bool, bool);
1745 static void cp_parser_type_specifier_seq
1746   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1747 static tree cp_parser_parameter_declaration_clause
1748   (cp_parser *);
1749 static tree cp_parser_parameter_declaration_list
1750   (cp_parser *, bool *);
1751 static cp_parameter_declarator *cp_parser_parameter_declaration
1752   (cp_parser *, bool, bool *);
1753 static tree cp_parser_default_argument 
1754   (cp_parser *, bool);
1755 static void cp_parser_function_body
1756   (cp_parser *);
1757 static tree cp_parser_initializer
1758   (cp_parser *, bool *, bool *);
1759 static tree cp_parser_initializer_clause
1760   (cp_parser *, bool *);
1761 static tree cp_parser_braced_list
1762   (cp_parser*, bool*);
1763 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1764   (cp_parser *, bool *);
1765
1766 static bool cp_parser_ctor_initializer_opt_and_function_body
1767   (cp_parser *);
1768
1769 /* Classes [gram.class] */
1770
1771 static tree cp_parser_class_name
1772   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1773 static tree cp_parser_class_specifier
1774   (cp_parser *);
1775 static tree cp_parser_class_head
1776   (cp_parser *, bool *, tree *, tree *);
1777 static enum tag_types cp_parser_class_key
1778   (cp_parser *);
1779 static void cp_parser_member_specification_opt
1780   (cp_parser *);
1781 static void cp_parser_member_declaration
1782   (cp_parser *);
1783 static tree cp_parser_pure_specifier
1784   (cp_parser *);
1785 static tree cp_parser_constant_initializer
1786   (cp_parser *);
1787
1788 /* Derived classes [gram.class.derived] */
1789
1790 static tree cp_parser_base_clause
1791   (cp_parser *);
1792 static tree cp_parser_base_specifier
1793   (cp_parser *);
1794
1795 /* Special member functions [gram.special] */
1796
1797 static tree cp_parser_conversion_function_id
1798   (cp_parser *);
1799 static tree cp_parser_conversion_type_id
1800   (cp_parser *);
1801 static cp_declarator *cp_parser_conversion_declarator_opt
1802   (cp_parser *);
1803 static bool cp_parser_ctor_initializer_opt
1804   (cp_parser *);
1805 static void cp_parser_mem_initializer_list
1806   (cp_parser *);
1807 static tree cp_parser_mem_initializer
1808   (cp_parser *);
1809 static tree cp_parser_mem_initializer_id
1810   (cp_parser *);
1811
1812 /* Overloading [gram.over] */
1813
1814 static tree cp_parser_operator_function_id
1815   (cp_parser *);
1816 static tree cp_parser_operator
1817   (cp_parser *);
1818
1819 /* Templates [gram.temp] */
1820
1821 static void cp_parser_template_declaration
1822   (cp_parser *, bool);
1823 static tree cp_parser_template_parameter_list
1824   (cp_parser *);
1825 static tree cp_parser_template_parameter
1826   (cp_parser *, bool *, bool *);
1827 static tree cp_parser_type_parameter
1828   (cp_parser *, bool *);
1829 static tree cp_parser_template_id
1830   (cp_parser *, bool, bool, bool);
1831 static tree cp_parser_template_name
1832   (cp_parser *, bool, bool, bool, bool *);
1833 static tree cp_parser_template_argument_list
1834   (cp_parser *);
1835 static tree cp_parser_template_argument
1836   (cp_parser *);
1837 static void cp_parser_explicit_instantiation
1838   (cp_parser *);
1839 static void cp_parser_explicit_specialization
1840   (cp_parser *);
1841
1842 /* Exception handling [gram.exception] */
1843
1844 static tree cp_parser_try_block
1845   (cp_parser *);
1846 static bool cp_parser_function_try_block
1847   (cp_parser *);
1848 static void cp_parser_handler_seq
1849   (cp_parser *);
1850 static void cp_parser_handler
1851   (cp_parser *);
1852 static tree cp_parser_exception_declaration
1853   (cp_parser *);
1854 static tree cp_parser_throw_expression
1855   (cp_parser *);
1856 static tree cp_parser_exception_specification_opt
1857   (cp_parser *);
1858 static tree cp_parser_type_id_list
1859   (cp_parser *);
1860
1861 /* GNU Extensions */
1862
1863 static tree cp_parser_asm_specification_opt
1864   (cp_parser *);
1865 static tree cp_parser_asm_operand_list
1866   (cp_parser *);
1867 static tree cp_parser_asm_clobber_list
1868   (cp_parser *);
1869 static tree cp_parser_attributes_opt
1870   (cp_parser *);
1871 static tree cp_parser_attribute_list
1872   (cp_parser *);
1873 static bool cp_parser_extension_opt
1874   (cp_parser *, int *);
1875 static void cp_parser_label_declaration
1876   (cp_parser *);
1877
1878 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1879 static bool cp_parser_pragma
1880   (cp_parser *, enum pragma_context);
1881
1882 /* Objective-C++ Productions */
1883
1884 static tree cp_parser_objc_message_receiver
1885   (cp_parser *);
1886 static tree cp_parser_objc_message_args
1887   (cp_parser *);
1888 static tree cp_parser_objc_message_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_encode_expression
1891   (cp_parser *);
1892 static tree cp_parser_objc_defs_expression
1893   (cp_parser *);
1894 static tree cp_parser_objc_protocol_expression
1895   (cp_parser *);
1896 static tree cp_parser_objc_selector_expression
1897   (cp_parser *);
1898 static tree cp_parser_objc_expression
1899   (cp_parser *);
1900 static bool cp_parser_objc_selector_p
1901   (enum cpp_ttype);
1902 static tree cp_parser_objc_selector
1903   (cp_parser *);
1904 static tree cp_parser_objc_protocol_refs_opt
1905   (cp_parser *);
1906 static void cp_parser_objc_declaration
1907   (cp_parser *);
1908 static tree cp_parser_objc_statement
1909   (cp_parser *);
1910
1911 /* Utility Routines */
1912
1913 static tree cp_parser_lookup_name
1914   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1915 static tree cp_parser_lookup_name_simple
1916   (cp_parser *, tree, location_t);
1917 static tree cp_parser_maybe_treat_template_as_class
1918   (tree, bool);
1919 static bool cp_parser_check_declarator_template_parameters
1920   (cp_parser *, cp_declarator *, location_t);
1921 static bool cp_parser_check_template_parameters
1922   (cp_parser *, unsigned, location_t);
1923 static tree cp_parser_simple_cast_expression
1924   (cp_parser *);
1925 static tree cp_parser_global_scope_opt
1926   (cp_parser *, bool);
1927 static bool cp_parser_constructor_declarator_p
1928   (cp_parser *, bool);
1929 static tree cp_parser_function_definition_from_specifiers_and_declarator
1930   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1931 static tree cp_parser_function_definition_after_declarator
1932   (cp_parser *, bool);
1933 static void cp_parser_template_declaration_after_export
1934   (cp_parser *, bool);
1935 static void cp_parser_perform_template_parameter_access_checks
1936   (VEC (deferred_access_check,gc)*);
1937 static tree cp_parser_single_declaration
1938   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1939 static tree cp_parser_functional_cast
1940   (cp_parser *, tree);
1941 static tree cp_parser_save_member_function_body
1942   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1943 static tree cp_parser_enclosed_template_argument_list
1944   (cp_parser *);
1945 static void cp_parser_save_default_args
1946   (cp_parser *, tree);
1947 static void cp_parser_late_parsing_for_member
1948   (cp_parser *, tree);
1949 static void cp_parser_late_parsing_default_args
1950   (cp_parser *, tree);
1951 static tree cp_parser_sizeof_operand
1952   (cp_parser *, enum rid);
1953 static tree cp_parser_trait_expr
1954   (cp_parser *, enum rid);
1955 static bool cp_parser_declares_only_class_p
1956   (cp_parser *);
1957 static void cp_parser_set_storage_class
1958   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1959 static void cp_parser_set_decl_spec_type
1960   (cp_decl_specifier_seq *, tree, location_t, bool);
1961 static bool cp_parser_friend_p
1962   (const cp_decl_specifier_seq *);
1963 static cp_token *cp_parser_require
1964   (cp_parser *, enum cpp_ttype, const char *);
1965 static cp_token *cp_parser_require_keyword
1966   (cp_parser *, enum rid, const char *);
1967 static bool cp_parser_token_starts_function_definition_p
1968   (cp_token *);
1969 static bool cp_parser_next_token_starts_class_definition_p
1970   (cp_parser *);
1971 static bool cp_parser_next_token_ends_template_argument_p
1972   (cp_parser *);
1973 static bool cp_parser_nth_token_starts_template_argument_list_p
1974   (cp_parser *, size_t);
1975 static enum tag_types cp_parser_token_is_class_key
1976   (cp_token *);
1977 static void cp_parser_check_class_key
1978   (enum tag_types, tree type);
1979 static void cp_parser_check_access_in_redeclaration
1980   (tree type, location_t location);
1981 static bool cp_parser_optional_template_keyword
1982   (cp_parser *);
1983 static void cp_parser_pre_parsed_nested_name_specifier
1984   (cp_parser *);
1985 static bool cp_parser_cache_group
1986   (cp_parser *, enum cpp_ttype, unsigned);
1987 static void cp_parser_parse_tentatively
1988   (cp_parser *);
1989 static void cp_parser_commit_to_tentative_parse
1990   (cp_parser *);
1991 static void cp_parser_abort_tentative_parse
1992   (cp_parser *);
1993 static bool cp_parser_parse_definitely
1994   (cp_parser *);
1995 static inline bool cp_parser_parsing_tentatively
1996   (cp_parser *);
1997 static bool cp_parser_uncommitted_to_tentative_parse_p
1998   (cp_parser *);
1999 static void cp_parser_error
2000   (cp_parser *, const char *);
2001 static void cp_parser_name_lookup_error
2002   (cp_parser *, tree, tree, const char *, location_t);
2003 static bool cp_parser_simulate_error
2004   (cp_parser *);
2005 static bool cp_parser_check_type_definition
2006   (cp_parser *);
2007 static void cp_parser_check_for_definition_in_return_type
2008   (cp_declarator *, tree, location_t type_location);
2009 static void cp_parser_check_for_invalid_template_id
2010   (cp_parser *, tree, location_t location);
2011 static bool cp_parser_non_integral_constant_expression
2012   (cp_parser *, const char *);
2013 static void cp_parser_diagnose_invalid_type_name
2014   (cp_parser *, tree, tree, location_t);
2015 static bool cp_parser_parse_and_diagnose_invalid_type_name
2016   (cp_parser *);
2017 static int cp_parser_skip_to_closing_parenthesis
2018   (cp_parser *, bool, bool, bool);
2019 static void cp_parser_skip_to_end_of_statement
2020   (cp_parser *);
2021 static void cp_parser_consume_semicolon_at_end_of_statement
2022   (cp_parser *);
2023 static void cp_parser_skip_to_end_of_block_or_statement
2024   (cp_parser *);
2025 static bool cp_parser_skip_to_closing_brace
2026   (cp_parser *);
2027 static void cp_parser_skip_to_end_of_template_parameter_list
2028   (cp_parser *);
2029 static void cp_parser_skip_to_pragma_eol
2030   (cp_parser*, cp_token *);
2031 static bool cp_parser_error_occurred
2032   (cp_parser *);
2033 static bool cp_parser_allow_gnu_extensions_p
2034   (cp_parser *);
2035 static bool cp_parser_is_string_literal
2036   (cp_token *);
2037 static bool cp_parser_is_keyword
2038   (cp_token *, enum rid);
2039 static tree cp_parser_make_typename_type
2040   (cp_parser *, tree, tree, location_t location);
2041 static cp_declarator * cp_parser_make_indirect_declarator
2042   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2043
2044 /* Returns nonzero if we are parsing tentatively.  */
2045
2046 static inline bool
2047 cp_parser_parsing_tentatively (cp_parser* parser)
2048 {
2049   return parser->context->next != NULL;
2050 }
2051
2052 /* Returns nonzero if TOKEN is a string literal.  */
2053
2054 static bool
2055 cp_parser_is_string_literal (cp_token* token)
2056 {
2057   return (token->type == CPP_STRING ||
2058           token->type == CPP_STRING16 ||
2059           token->type == CPP_STRING32 ||
2060           token->type == CPP_WSTRING);
2061 }
2062
2063 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2064
2065 static bool
2066 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2067 {
2068   return token->keyword == keyword;
2069 }
2070
2071 /* If not parsing tentatively, issue a diagnostic of the form
2072       FILE:LINE: MESSAGE before TOKEN
2073    where TOKEN is the next token in the input stream.  MESSAGE
2074    (specified by the caller) is usually of the form "expected
2075    OTHER-TOKEN".  */
2076
2077 static void
2078 cp_parser_error (cp_parser* parser, const char* message)
2079 {
2080   if (!cp_parser_simulate_error (parser))
2081     {
2082       cp_token *token = cp_lexer_peek_token (parser->lexer);
2083       /* This diagnostic makes more sense if it is tagged to the line
2084          of the token we just peeked at.  */
2085       cp_lexer_set_source_position_from_token (token);
2086
2087       if (token->type == CPP_PRAGMA)
2088         {
2089           error ("%H%<#pragma%> is not allowed here", &token->location);
2090           cp_parser_skip_to_pragma_eol (parser, token);
2091           return;
2092         }
2093
2094       c_parse_error (message,
2095                      /* Because c_parser_error does not understand
2096                         CPP_KEYWORD, keywords are treated like
2097                         identifiers.  */
2098                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2099                      token->u.value);
2100     }
2101 }
2102
2103 /* Issue an error about name-lookup failing.  NAME is the
2104    IDENTIFIER_NODE DECL is the result of
2105    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2106    the thing that we hoped to find.  */
2107
2108 static void
2109 cp_parser_name_lookup_error (cp_parser* parser,
2110                              tree name,
2111                              tree decl,
2112                              const char* desired,
2113                              location_t location)
2114 {
2115   /* If name lookup completely failed, tell the user that NAME was not
2116      declared.  */
2117   if (decl == error_mark_node)
2118     {
2119       if (parser->scope && parser->scope != global_namespace)
2120         error ("%H%<%E::%E%> has not been declared",
2121                &location, parser->scope, name);
2122       else if (parser->scope == global_namespace)
2123         error ("%H%<::%E%> has not been declared", &location, name);
2124       else if (parser->object_scope
2125                && !CLASS_TYPE_P (parser->object_scope))
2126         error ("%Hrequest for member %qE in non-class type %qT",
2127                &location, name, parser->object_scope);
2128       else if (parser->object_scope)
2129         error ("%H%<%T::%E%> has not been declared",
2130                &location, parser->object_scope, name);
2131       else
2132         error ("%H%qE has not been declared", &location, name);
2133     }
2134   else if (parser->scope && parser->scope != global_namespace)
2135     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2136   else if (parser->scope == global_namespace)
2137     error ("%H%<::%E%> %s", &location, name, desired);
2138   else
2139     error ("%H%qE %s", &location, name, desired);
2140 }
2141
2142 /* If we are parsing tentatively, remember that an error has occurred
2143    during this tentative parse.  Returns true if the error was
2144    simulated; false if a message should be issued by the caller.  */
2145
2146 static bool
2147 cp_parser_simulate_error (cp_parser* parser)
2148 {
2149   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2150     {
2151       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2152       return true;
2153     }
2154   return false;
2155 }
2156
2157 /* Check for repeated decl-specifiers.  */
2158
2159 static void
2160 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2161                            location_t location)
2162 {
2163   cp_decl_spec ds;
2164
2165   for (ds = ds_first; ds != ds_last; ++ds)
2166     {
2167       unsigned count = decl_specs->specs[(int)ds];
2168       if (count < 2)
2169         continue;
2170       /* The "long" specifier is a special case because of "long long".  */
2171       if (ds == ds_long)
2172         {
2173           if (count > 2)
2174             error ("%H%<long long long%> is too long for GCC", &location);
2175           else if (pedantic && !in_system_header && warn_long_long
2176                    && cxx_dialect == cxx98)
2177             pedwarn (location, OPT_Wlong_long, 
2178                      "ISO C++ 1998 does not support %<long long%>");
2179         }
2180       else if (count > 1)
2181         {
2182           static const char *const decl_spec_names[] = {
2183             "signed",
2184             "unsigned",
2185             "short",
2186             "long",
2187             "const",
2188             "volatile",
2189             "restrict",
2190             "inline",
2191             "virtual",
2192             "explicit",
2193             "friend",
2194             "typedef",
2195             "__complex",
2196             "__thread"
2197           };
2198           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2199         }
2200     }
2201 }
2202
2203 /* This function is called when a type is defined.  If type
2204    definitions are forbidden at this point, an error message is
2205    issued.  */
2206
2207 static bool
2208 cp_parser_check_type_definition (cp_parser* parser)
2209 {
2210   /* If types are forbidden here, issue a message.  */
2211   if (parser->type_definition_forbidden_message)
2212     {
2213       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2214          in the message need to be interpreted.  */
2215       error (parser->type_definition_forbidden_message);
2216       return false;
2217     }
2218   return true;
2219 }
2220
2221 /* This function is called when the DECLARATOR is processed.  The TYPE
2222    was a type defined in the decl-specifiers.  If it is invalid to
2223    define a type in the decl-specifiers for DECLARATOR, an error is
2224    issued. TYPE_LOCATION is the location of TYPE and is used
2225    for error reporting.  */
2226
2227 static void
2228 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2229                                                tree type, location_t type_location)
2230 {
2231   /* [dcl.fct] forbids type definitions in return types.
2232      Unfortunately, it's not easy to know whether or not we are
2233      processing a return type until after the fact.  */
2234   while (declarator
2235          && (declarator->kind == cdk_pointer
2236              || declarator->kind == cdk_reference
2237              || declarator->kind == cdk_ptrmem))
2238     declarator = declarator->declarator;
2239   if (declarator
2240       && declarator->kind == cdk_function)
2241     {
2242       error ("%Hnew types may not be defined in a return type", &type_location);
2243       inform (type_location, 
2244               "(perhaps a semicolon is missing after the definition of %qT)",
2245               type);
2246     }
2247 }
2248
2249 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2250    "<" in any valid C++ program.  If the next token is indeed "<",
2251    issue a message warning the user about what appears to be an
2252    invalid attempt to form a template-id. LOCATION is the location
2253    of the type-specifier (TYPE) */
2254
2255 static void
2256 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2257                                          tree type, location_t location)
2258 {
2259   cp_token_position start = 0;
2260
2261   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2262     {
2263       if (TYPE_P (type))
2264         error ("%H%qT is not a template", &location, type);
2265       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2266         error ("%H%qE is not a template", &location, type);
2267       else
2268         error ("%Hinvalid template-id", &location);
2269       /* Remember the location of the invalid "<".  */
2270       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2271         start = cp_lexer_token_position (parser->lexer, true);
2272       /* Consume the "<".  */
2273       cp_lexer_consume_token (parser->lexer);
2274       /* Parse the template arguments.  */
2275       cp_parser_enclosed_template_argument_list (parser);
2276       /* Permanently remove the invalid template arguments so that
2277          this error message is not issued again.  */
2278       if (start)
2279         cp_lexer_purge_tokens_after (parser->lexer, start);
2280     }
2281 }
2282
2283 /* If parsing an integral constant-expression, issue an error message
2284    about the fact that THING appeared and return true.  Otherwise,
2285    return false.  In either case, set
2286    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2287
2288 static bool
2289 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2290                                             const char *thing)
2291 {
2292   parser->non_integral_constant_expression_p = true;
2293   if (parser->integral_constant_expression_p)
2294     {
2295       if (!parser->allow_non_integral_constant_expression_p)
2296         {
2297           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2298              in the message need to be interpreted.  */
2299           char *message = concat (thing,
2300                                   " cannot appear in a constant-expression",
2301                                   NULL);
2302           error (message);
2303           free (message);
2304           return true;
2305         }
2306     }
2307   return false;
2308 }
2309
2310 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2311    qualifying scope (or NULL, if none) for ID.  This function commits
2312    to the current active tentative parse, if any.  (Otherwise, the
2313    problematic construct might be encountered again later, resulting
2314    in duplicate error messages.) LOCATION is the location of ID.  */
2315
2316 static void
2317 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2318                                       tree scope, tree id,
2319                                       location_t location)
2320 {
2321   tree decl, old_scope;
2322   /* Try to lookup the identifier.  */
2323   old_scope = parser->scope;
2324   parser->scope = scope;
2325   decl = cp_parser_lookup_name_simple (parser, id, location);
2326   parser->scope = old_scope;
2327   /* If the lookup found a template-name, it means that the user forgot
2328   to specify an argument list. Emit a useful error message.  */
2329   if (TREE_CODE (decl) == TEMPLATE_DECL)
2330     error ("%Hinvalid use of template-name %qE without an argument list",
2331            &location, decl);
2332   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2333     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2334   else if (TREE_CODE (decl) == TYPE_DECL)
2335     /* Something like 'unsigned A a;'  */
2336     error ("%Hinvalid combination of multiple type-specifiers",
2337            &location);
2338   else if (!parser->scope)
2339     {
2340       /* Issue an error message.  */
2341       error ("%H%qE does not name a type", &location, id);
2342       /* If we're in a template class, it's possible that the user was
2343          referring to a type from a base class.  For example:
2344
2345            template <typename T> struct A { typedef T X; };
2346            template <typename T> struct B : public A<T> { X x; };
2347
2348          The user should have said "typename A<T>::X".  */
2349       if (processing_template_decl && current_class_type
2350           && TYPE_BINFO (current_class_type))
2351         {
2352           tree b;
2353
2354           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2355                b;
2356                b = TREE_CHAIN (b))
2357             {
2358               tree base_type = BINFO_TYPE (b);
2359               if (CLASS_TYPE_P (base_type)
2360                   && dependent_type_p (base_type))
2361                 {
2362                   tree field;
2363                   /* Go from a particular instantiation of the
2364                      template (which will have an empty TYPE_FIELDs),
2365                      to the main version.  */
2366                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2367                   for (field = TYPE_FIELDS (base_type);
2368                        field;
2369                        field = TREE_CHAIN (field))
2370                     if (TREE_CODE (field) == TYPE_DECL
2371                         && DECL_NAME (field) == id)
2372                       {
2373                         inform (location, 
2374                                 "(perhaps %<typename %T::%E%> was intended)",
2375                                 BINFO_TYPE (b), id);
2376                         break;
2377                       }
2378                   if (field)
2379                     break;
2380                 }
2381             }
2382         }
2383     }
2384   /* Here we diagnose qualified-ids where the scope is actually correct,
2385      but the identifier does not resolve to a valid type name.  */
2386   else if (parser->scope != error_mark_node)
2387     {
2388       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2389         error ("%H%qE in namespace %qE does not name a type",
2390                &location, id, parser->scope);
2391       else if (TYPE_P (parser->scope))
2392         error ("%H%qE in class %qT does not name a type",
2393                &location, id, parser->scope);
2394       else
2395         gcc_unreachable ();
2396     }
2397   cp_parser_commit_to_tentative_parse (parser);
2398 }
2399
2400 /* Check for a common situation where a type-name should be present,
2401    but is not, and issue a sensible error message.  Returns true if an
2402    invalid type-name was detected.
2403
2404    The situation handled by this function are variable declarations of the
2405    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2406    Usually, `ID' should name a type, but if we got here it means that it
2407    does not. We try to emit the best possible error message depending on
2408    how exactly the id-expression looks like.  */
2409
2410 static bool
2411 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2412 {
2413   tree id;
2414   cp_token *token = cp_lexer_peek_token (parser->lexer);
2415
2416   cp_parser_parse_tentatively (parser);
2417   id = cp_parser_id_expression (parser,
2418                                 /*template_keyword_p=*/false,
2419                                 /*check_dependency_p=*/true,
2420                                 /*template_p=*/NULL,
2421                                 /*declarator_p=*/true,
2422                                 /*optional_p=*/false);
2423   /* After the id-expression, there should be a plain identifier,
2424      otherwise this is not a simple variable declaration. Also, if
2425      the scope is dependent, we cannot do much.  */
2426   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2427       || (parser->scope && TYPE_P (parser->scope)
2428           && dependent_type_p (parser->scope))
2429       || TREE_CODE (id) == TYPE_DECL)
2430     {
2431       cp_parser_abort_tentative_parse (parser);
2432       return false;
2433     }
2434   if (!cp_parser_parse_definitely (parser))
2435     return false;
2436
2437   /* Emit a diagnostic for the invalid type.  */
2438   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2439                                         id, token->location);
2440   /* Skip to the end of the declaration; there's no point in
2441      trying to process it.  */
2442   cp_parser_skip_to_end_of_block_or_statement (parser);
2443   return true;
2444 }
2445
2446 /* Consume tokens up to, and including, the next non-nested closing `)'.
2447    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2448    are doing error recovery. Returns -1 if OR_COMMA is true and we
2449    found an unnested comma.  */
2450
2451 static int
2452 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2453                                        bool recovering,
2454                                        bool or_comma,
2455                                        bool consume_paren)
2456 {
2457   unsigned paren_depth = 0;
2458   unsigned brace_depth = 0;
2459
2460   if (recovering && !or_comma
2461       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2462     return 0;
2463
2464   while (true)
2465     {
2466       cp_token * token = cp_lexer_peek_token (parser->lexer);
2467
2468       switch (token->type)
2469         {
2470         case CPP_EOF:
2471         case CPP_PRAGMA_EOL:
2472           /* If we've run out of tokens, then there is no closing `)'.  */
2473           return 0;
2474
2475         case CPP_SEMICOLON:
2476           /* This matches the processing in skip_to_end_of_statement.  */
2477           if (!brace_depth)
2478             return 0;
2479           break;
2480
2481         case CPP_OPEN_BRACE:
2482           ++brace_depth;
2483           break;
2484         case CPP_CLOSE_BRACE:
2485           if (!brace_depth--)
2486             return 0;
2487           break;
2488
2489         case CPP_COMMA:
2490           if (recovering && or_comma && !brace_depth && !paren_depth)
2491             return -1;
2492           break;
2493
2494         case CPP_OPEN_PAREN:
2495           if (!brace_depth)
2496             ++paren_depth;
2497           break;
2498
2499         case CPP_CLOSE_PAREN:
2500           if (!brace_depth && !paren_depth--)
2501             {
2502               if (consume_paren)
2503                 cp_lexer_consume_token (parser->lexer);
2504               return 1;
2505             }
2506           break;
2507
2508         default:
2509           break;
2510         }
2511
2512       /* Consume the token.  */
2513       cp_lexer_consume_token (parser->lexer);
2514     }
2515 }
2516
2517 /* Consume tokens until we reach the end of the current statement.
2518    Normally, that will be just before consuming a `;'.  However, if a
2519    non-nested `}' comes first, then we stop before consuming that.  */
2520
2521 static void
2522 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2523 {
2524   unsigned nesting_depth = 0;
2525
2526   while (true)
2527     {
2528       cp_token *token = cp_lexer_peek_token (parser->lexer);
2529
2530       switch (token->type)
2531         {
2532         case CPP_EOF:
2533         case CPP_PRAGMA_EOL:
2534           /* If we've run out of tokens, stop.  */
2535           return;
2536
2537         case CPP_SEMICOLON:
2538           /* If the next token is a `;', we have reached the end of the
2539              statement.  */
2540           if (!nesting_depth)
2541             return;
2542           break;
2543
2544         case CPP_CLOSE_BRACE:
2545           /* If this is a non-nested '}', stop before consuming it.
2546              That way, when confronted with something like:
2547
2548                { 3 + }
2549
2550              we stop before consuming the closing '}', even though we
2551              have not yet reached a `;'.  */
2552           if (nesting_depth == 0)
2553             return;
2554
2555           /* If it is the closing '}' for a block that we have
2556              scanned, stop -- but only after consuming the token.
2557              That way given:
2558
2559                 void f g () { ... }
2560                 typedef int I;
2561
2562              we will stop after the body of the erroneously declared
2563              function, but before consuming the following `typedef'
2564              declaration.  */
2565           if (--nesting_depth == 0)
2566             {
2567               cp_lexer_consume_token (parser->lexer);
2568               return;
2569             }
2570
2571         case CPP_OPEN_BRACE:
2572           ++nesting_depth;
2573           break;
2574
2575         default:
2576           break;
2577         }
2578
2579       /* Consume the token.  */
2580       cp_lexer_consume_token (parser->lexer);
2581     }
2582 }
2583
2584 /* This function is called at the end of a statement or declaration.
2585    If the next token is a semicolon, it is consumed; otherwise, error
2586    recovery is attempted.  */
2587
2588 static void
2589 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2590 {
2591   /* Look for the trailing `;'.  */
2592   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2593     {
2594       /* If there is additional (erroneous) input, skip to the end of
2595          the statement.  */
2596       cp_parser_skip_to_end_of_statement (parser);
2597       /* If the next token is now a `;', consume it.  */
2598       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2599         cp_lexer_consume_token (parser->lexer);
2600     }
2601 }
2602
2603 /* Skip tokens until we have consumed an entire block, or until we
2604    have consumed a non-nested `;'.  */
2605
2606 static void
2607 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2608 {
2609   int nesting_depth = 0;
2610
2611   while (nesting_depth >= 0)
2612     {
2613       cp_token *token = cp_lexer_peek_token (parser->lexer);
2614
2615       switch (token->type)
2616         {
2617         case CPP_EOF:
2618         case CPP_PRAGMA_EOL:
2619           /* If we've run out of tokens, stop.  */
2620           return;
2621
2622         case CPP_SEMICOLON:
2623           /* Stop if this is an unnested ';'. */
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_CLOSE_BRACE:
2629           /* Stop if this is an unnested '}', or closes the outermost
2630              nesting level.  */
2631           nesting_depth--;
2632           if (nesting_depth < 0)
2633             return;
2634           if (!nesting_depth)
2635             nesting_depth = -1;
2636           break;
2637
2638         case CPP_OPEN_BRACE:
2639           /* Nest. */
2640           nesting_depth++;
2641           break;
2642
2643         default:
2644           break;
2645         }
2646
2647       /* Consume the token.  */
2648       cp_lexer_consume_token (parser->lexer);
2649     }
2650 }
2651
2652 /* Skip tokens until a non-nested closing curly brace is the next
2653    token, or there are no more tokens. Return true in the first case,
2654    false otherwise.  */
2655
2656 static bool
2657 cp_parser_skip_to_closing_brace (cp_parser *parser)
2658 {
2659   unsigned nesting_depth = 0;
2660
2661   while (true)
2662     {
2663       cp_token *token = cp_lexer_peek_token (parser->lexer);
2664
2665       switch (token->type)
2666         {
2667         case CPP_EOF:
2668         case CPP_PRAGMA_EOL:
2669           /* If we've run out of tokens, stop.  */
2670           return false;
2671
2672         case CPP_CLOSE_BRACE:
2673           /* If the next token is a non-nested `}', then we have reached
2674              the end of the current block.  */
2675           if (nesting_depth-- == 0)
2676             return true;
2677           break;
2678
2679         case CPP_OPEN_BRACE:
2680           /* If it the next token is a `{', then we are entering a new
2681              block.  Consume the entire block.  */
2682           ++nesting_depth;
2683           break;
2684
2685         default:
2686           break;
2687         }
2688
2689       /* Consume the token.  */
2690       cp_lexer_consume_token (parser->lexer);
2691     }
2692 }
2693
2694 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2695    parameter is the PRAGMA token, allowing us to purge the entire pragma
2696    sequence.  */
2697
2698 static void
2699 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2700 {
2701   cp_token *token;
2702
2703   parser->lexer->in_pragma = false;
2704
2705   do
2706     token = cp_lexer_consume_token (parser->lexer);
2707   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2708
2709   /* Ensure that the pragma is not parsed again.  */
2710   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2711 }
2712
2713 /* Require pragma end of line, resyncing with it as necessary.  The
2714    arguments are as for cp_parser_skip_to_pragma_eol.  */
2715
2716 static void
2717 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2718 {
2719   parser->lexer->in_pragma = false;
2720   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2721     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2722 }
2723
2724 /* This is a simple wrapper around make_typename_type. When the id is
2725    an unresolved identifier node, we can provide a superior diagnostic
2726    using cp_parser_diagnose_invalid_type_name.  */
2727
2728 static tree
2729 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2730                               tree id, location_t id_location)
2731 {
2732   tree result;
2733   if (TREE_CODE (id) == IDENTIFIER_NODE)
2734     {
2735       result = make_typename_type (scope, id, typename_type,
2736                                    /*complain=*/tf_none);
2737       if (result == error_mark_node)
2738         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2739       return result;
2740     }
2741   return make_typename_type (scope, id, typename_type, tf_error);
2742 }
2743
2744 /* This is a wrapper around the
2745    make_{pointer,ptrmem,reference}_declarator functions that decides
2746    which one to call based on the CODE and CLASS_TYPE arguments. The
2747    CODE argument should be one of the values returned by
2748    cp_parser_ptr_operator. */
2749 static cp_declarator *
2750 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2751                                     cp_cv_quals cv_qualifiers,
2752                                     cp_declarator *target)
2753 {
2754   if (code == ERROR_MARK)
2755     return cp_error_declarator;
2756
2757   if (code == INDIRECT_REF)
2758     if (class_type == NULL_TREE)
2759       return make_pointer_declarator (cv_qualifiers, target);
2760     else
2761       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2762   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2763     return make_reference_declarator (cv_qualifiers, target, false);
2764   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2765     return make_reference_declarator (cv_qualifiers, target, true);
2766   gcc_unreachable ();
2767 }
2768
2769 /* Create a new C++ parser.  */
2770
2771 static cp_parser *
2772 cp_parser_new (void)
2773 {
2774   cp_parser *parser;
2775   cp_lexer *lexer;
2776   unsigned i;
2777
2778   /* cp_lexer_new_main is called before calling ggc_alloc because
2779      cp_lexer_new_main might load a PCH file.  */
2780   lexer = cp_lexer_new_main ();
2781
2782   /* Initialize the binops_by_token so that we can get the tree
2783      directly from the token.  */
2784   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2785     binops_by_token[binops[i].token_type] = binops[i];
2786
2787   parser = GGC_CNEW (cp_parser);
2788   parser->lexer = lexer;
2789   parser->context = cp_parser_context_new (NULL);
2790
2791   /* For now, we always accept GNU extensions.  */
2792   parser->allow_gnu_extensions_p = 1;
2793
2794   /* The `>' token is a greater-than operator, not the end of a
2795      template-id.  */
2796   parser->greater_than_is_operator_p = true;
2797
2798   parser->default_arg_ok_p = true;
2799
2800   /* We are not parsing a constant-expression.  */
2801   parser->integral_constant_expression_p = false;
2802   parser->allow_non_integral_constant_expression_p = false;
2803   parser->non_integral_constant_expression_p = false;
2804
2805   /* Local variable names are not forbidden.  */
2806   parser->local_variables_forbidden_p = false;
2807
2808   /* We are not processing an `extern "C"' declaration.  */
2809   parser->in_unbraced_linkage_specification_p = false;
2810
2811   /* We are not processing a declarator.  */
2812   parser->in_declarator_p = false;
2813
2814   /* We are not processing a template-argument-list.  */
2815   parser->in_template_argument_list_p = false;
2816
2817   /* We are not in an iteration statement.  */
2818   parser->in_statement = 0;
2819
2820   /* We are not in a switch statement.  */
2821   parser->in_switch_statement_p = false;
2822
2823   /* We are not parsing a type-id inside an expression.  */
2824   parser->in_type_id_in_expr_p = false;
2825
2826   /* Declarations aren't implicitly extern "C".  */
2827   parser->implicit_extern_c = false;
2828
2829   /* String literals should be translated to the execution character set.  */
2830   parser->translate_strings_p = true;
2831
2832   /* We are not parsing a function body.  */
2833   parser->in_function_body = false;
2834
2835   /* The unparsed function queue is empty.  */
2836   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2837
2838   /* There are no classes being defined.  */
2839   parser->num_classes_being_defined = 0;
2840
2841   /* No template parameters apply.  */
2842   parser->num_template_parameter_lists = 0;
2843
2844   return parser;
2845 }
2846
2847 /* Create a cp_lexer structure which will emit the tokens in CACHE
2848    and push it onto the parser's lexer stack.  This is used for delayed
2849    parsing of in-class method bodies and default arguments, and should
2850    not be confused with tentative parsing.  */
2851 static void
2852 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2853 {
2854   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2855   lexer->next = parser->lexer;
2856   parser->lexer = lexer;
2857
2858   /* Move the current source position to that of the first token in the
2859      new lexer.  */
2860   cp_lexer_set_source_position_from_token (lexer->next_token);
2861 }
2862
2863 /* Pop the top lexer off the parser stack.  This is never used for the
2864    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2865 static void
2866 cp_parser_pop_lexer (cp_parser *parser)
2867 {
2868   cp_lexer *lexer = parser->lexer;
2869   parser->lexer = lexer->next;
2870   cp_lexer_destroy (lexer);
2871
2872   /* Put the current source position back where it was before this
2873      lexer was pushed.  */
2874   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2875 }
2876
2877 /* Lexical conventions [gram.lex]  */
2878
2879 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2880    identifier.  */
2881
2882 static tree
2883 cp_parser_identifier (cp_parser* parser)
2884 {
2885   cp_token *token;
2886
2887   /* Look for the identifier.  */
2888   token = cp_parser_require (parser, CPP_NAME, "identifier");
2889   /* Return the value.  */
2890   return token ? token->u.value : error_mark_node;
2891 }
2892
2893 /* Parse a sequence of adjacent string constants.  Returns a
2894    TREE_STRING representing the combined, nul-terminated string
2895    constant.  If TRANSLATE is true, translate the string to the
2896    execution character set.  If WIDE_OK is true, a wide string is
2897    invalid here.
2898
2899    C++98 [lex.string] says that if a narrow string literal token is
2900    adjacent to a wide string literal token, the behavior is undefined.
2901    However, C99 6.4.5p4 says that this results in a wide string literal.
2902    We follow C99 here, for consistency with the C front end.
2903
2904    This code is largely lifted from lex_string() in c-lex.c.
2905
2906    FUTURE: ObjC++ will need to handle @-strings here.  */
2907 static tree
2908 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2909 {
2910   tree value;
2911   size_t count;
2912   struct obstack str_ob;
2913   cpp_string str, istr, *strs;
2914   cp_token *tok;
2915   enum cpp_ttype type;
2916
2917   tok = cp_lexer_peek_token (parser->lexer);
2918   if (!cp_parser_is_string_literal (tok))
2919     {
2920       cp_parser_error (parser, "expected string-literal");
2921       return error_mark_node;
2922     }
2923
2924   type = tok->type;
2925
2926   /* Try to avoid the overhead of creating and destroying an obstack
2927      for the common case of just one string.  */
2928   if (!cp_parser_is_string_literal
2929       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2930     {
2931       cp_lexer_consume_token (parser->lexer);
2932
2933       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2934       str.len = TREE_STRING_LENGTH (tok->u.value);
2935       count = 1;
2936
2937       strs = &str;
2938     }
2939   else
2940     {
2941       gcc_obstack_init (&str_ob);
2942       count = 0;
2943
2944       do
2945         {
2946           cp_lexer_consume_token (parser->lexer);
2947           count++;
2948           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2949           str.len = TREE_STRING_LENGTH (tok->u.value);
2950
2951           if (type != tok->type)
2952             {
2953               if (type == CPP_STRING)
2954                 type = tok->type;
2955               else if (tok->type != CPP_STRING)
2956                 error ("%Hunsupported non-standard concatenation "
2957                        "of string literals", &tok->location);
2958             }
2959
2960           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2961
2962           tok = cp_lexer_peek_token (parser->lexer);
2963         }
2964       while (cp_parser_is_string_literal (tok));
2965
2966       strs = (cpp_string *) obstack_finish (&str_ob);
2967     }
2968
2969   if (type != CPP_STRING && !wide_ok)
2970     {
2971       cp_parser_error (parser, "a wide string is invalid in this context");
2972       type = CPP_STRING;
2973     }
2974
2975   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2976       (parse_in, strs, count, &istr, type))
2977     {
2978       value = build_string (istr.len, (const char *)istr.text);
2979       free (CONST_CAST (unsigned char *, istr.text));
2980
2981       switch (type)
2982         {
2983         default:
2984         case CPP_STRING:
2985           TREE_TYPE (value) = char_array_type_node;
2986           break;
2987         case CPP_STRING16:
2988           TREE_TYPE (value) = char16_array_type_node;
2989           break;
2990         case CPP_STRING32:
2991           TREE_TYPE (value) = char32_array_type_node;
2992           break;
2993         case CPP_WSTRING:
2994           TREE_TYPE (value) = wchar_array_type_node;
2995           break;
2996         }
2997
2998       value = fix_string_type (value);
2999     }
3000   else
3001     /* cpp_interpret_string has issued an error.  */
3002     value = error_mark_node;
3003
3004   if (count > 1)
3005     obstack_free (&str_ob, 0);
3006
3007   return value;
3008 }
3009
3010
3011 /* Basic concepts [gram.basic]  */
3012
3013 /* Parse a translation-unit.
3014
3015    translation-unit:
3016      declaration-seq [opt]
3017
3018    Returns TRUE if all went well.  */
3019
3020 static bool
3021 cp_parser_translation_unit (cp_parser* parser)
3022 {
3023   /* The address of the first non-permanent object on the declarator
3024      obstack.  */
3025   static void *declarator_obstack_base;
3026
3027   bool success;
3028
3029   /* Create the declarator obstack, if necessary.  */
3030   if (!cp_error_declarator)
3031     {
3032       gcc_obstack_init (&declarator_obstack);
3033       /* Create the error declarator.  */
3034       cp_error_declarator = make_declarator (cdk_error);
3035       /* Create the empty parameter list.  */
3036       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3037       /* Remember where the base of the declarator obstack lies.  */
3038       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3039     }
3040
3041   cp_parser_declaration_seq_opt (parser);
3042
3043   /* If there are no tokens left then all went well.  */
3044   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3045     {
3046       /* Get rid of the token array; we don't need it any more.  */
3047       cp_lexer_destroy (parser->lexer);
3048       parser->lexer = NULL;
3049
3050       /* This file might have been a context that's implicitly extern
3051          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3052       if (parser->implicit_extern_c)
3053         {
3054           pop_lang_context ();
3055           parser->implicit_extern_c = false;
3056         }
3057
3058       /* Finish up.  */
3059       finish_translation_unit ();
3060
3061       success = true;
3062     }
3063   else
3064     {
3065       cp_parser_error (parser, "expected declaration");
3066       success = false;
3067     }
3068
3069   /* Make sure the declarator obstack was fully cleaned up.  */
3070   gcc_assert (obstack_next_free (&declarator_obstack)
3071               == declarator_obstack_base);
3072
3073   /* All went well.  */
3074   return success;
3075 }
3076
3077 /* Expressions [gram.expr] */
3078
3079 /* Parse a primary-expression.
3080
3081    primary-expression:
3082      literal
3083      this
3084      ( expression )
3085      id-expression
3086
3087    GNU Extensions:
3088
3089    primary-expression:
3090      ( compound-statement )
3091      __builtin_va_arg ( assignment-expression , type-id )
3092      __builtin_offsetof ( type-id , offsetof-expression )
3093
3094    C++ Extensions:
3095      __has_nothrow_assign ( type-id )   
3096      __has_nothrow_constructor ( type-id )
3097      __has_nothrow_copy ( type-id )
3098      __has_trivial_assign ( type-id )   
3099      __has_trivial_constructor ( type-id )
3100      __has_trivial_copy ( type-id )
3101      __has_trivial_destructor ( type-id )
3102      __has_virtual_destructor ( type-id )     
3103      __is_abstract ( type-id )
3104      __is_base_of ( type-id , type-id )
3105      __is_class ( type-id )
3106      __is_convertible_to ( type-id , type-id )     
3107      __is_empty ( type-id )
3108      __is_enum ( type-id )
3109      __is_pod ( type-id )
3110      __is_polymorphic ( type-id )
3111      __is_union ( type-id )
3112
3113    Objective-C++ Extension:
3114
3115    primary-expression:
3116      objc-expression
3117
3118    literal:
3119      __null
3120
3121    ADDRESS_P is true iff this expression was immediately preceded by
3122    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3123    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3124    true iff this expression is a template argument.
3125
3126    Returns a representation of the expression.  Upon return, *IDK
3127    indicates what kind of id-expression (if any) was present.  */
3128
3129 static tree
3130 cp_parser_primary_expression (cp_parser *parser,
3131                               bool address_p,
3132                               bool cast_p,
3133                               bool template_arg_p,
3134                               cp_id_kind *idk)
3135 {
3136   cp_token *token = NULL;
3137
3138   /* Assume the primary expression is not an id-expression.  */
3139   *idk = CP_ID_KIND_NONE;
3140
3141   /* Peek at the next token.  */
3142   token = cp_lexer_peek_token (parser->lexer);
3143   switch (token->type)
3144     {
3145       /* literal:
3146            integer-literal
3147            character-literal
3148            floating-literal
3149            string-literal
3150            boolean-literal  */
3151     case CPP_CHAR:
3152     case CPP_CHAR16:
3153     case CPP_CHAR32:
3154     case CPP_WCHAR:
3155     case CPP_NUMBER:
3156       token = cp_lexer_consume_token (parser->lexer);
3157       if (TREE_CODE (token->u.value) == FIXED_CST)
3158         {
3159           error ("%Hfixed-point types not supported in C++",
3160                  &token->location);
3161           return error_mark_node;
3162         }
3163       /* Floating-point literals are only allowed in an integral
3164          constant expression if they are cast to an integral or
3165          enumeration type.  */
3166       if (TREE_CODE (token->u.value) == REAL_CST
3167           && parser->integral_constant_expression_p
3168           && pedantic)
3169         {
3170           /* CAST_P will be set even in invalid code like "int(2.7 +
3171              ...)".   Therefore, we have to check that the next token
3172              is sure to end the cast.  */
3173           if (cast_p)
3174             {
3175               cp_token *next_token;
3176
3177               next_token = cp_lexer_peek_token (parser->lexer);
3178               if (/* The comma at the end of an
3179                      enumerator-definition.  */
3180                   next_token->type != CPP_COMMA
3181                   /* The curly brace at the end of an enum-specifier.  */
3182                   && next_token->type != CPP_CLOSE_BRACE
3183                   /* The end of a statement.  */
3184                   && next_token->type != CPP_SEMICOLON
3185                   /* The end of the cast-expression.  */
3186                   && next_token->type != CPP_CLOSE_PAREN
3187                   /* The end of an array bound.  */
3188                   && next_token->type != CPP_CLOSE_SQUARE
3189                   /* The closing ">" in a template-argument-list.  */
3190                   && (next_token->type != CPP_GREATER
3191                       || parser->greater_than_is_operator_p)
3192                   /* C++0x only: A ">>" treated like two ">" tokens,
3193                      in a template-argument-list.  */
3194                   && (next_token->type != CPP_RSHIFT
3195                       || (cxx_dialect == cxx98)
3196                       || parser->greater_than_is_operator_p))
3197                 cast_p = false;
3198             }
3199
3200           /* If we are within a cast, then the constraint that the
3201              cast is to an integral or enumeration type will be
3202              checked at that point.  If we are not within a cast, then
3203              this code is invalid.  */
3204           if (!cast_p)
3205             cp_parser_non_integral_constant_expression
3206               (parser, "floating-point literal");
3207         }
3208       return token->u.value;
3209
3210     case CPP_STRING:
3211     case CPP_STRING16:
3212     case CPP_STRING32:
3213     case CPP_WSTRING:
3214       /* ??? Should wide strings be allowed when parser->translate_strings_p
3215          is false (i.e. in attributes)?  If not, we can kill the third
3216          argument to cp_parser_string_literal.  */
3217       return cp_parser_string_literal (parser,
3218                                        parser->translate_strings_p,
3219                                        true);
3220
3221     case CPP_OPEN_PAREN:
3222       {
3223         tree expr;
3224         bool saved_greater_than_is_operator_p;
3225
3226         /* Consume the `('.  */
3227         cp_lexer_consume_token (parser->lexer);
3228         /* Within a parenthesized expression, a `>' token is always
3229            the greater-than operator.  */
3230         saved_greater_than_is_operator_p
3231           = parser->greater_than_is_operator_p;
3232         parser->greater_than_is_operator_p = true;
3233         /* If we see `( { ' then we are looking at the beginning of
3234            a GNU statement-expression.  */
3235         if (cp_parser_allow_gnu_extensions_p (parser)
3236             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3237           {
3238             /* Statement-expressions are not allowed by the standard.  */
3239             pedwarn (token->location, OPT_pedantic, 
3240                      "ISO C++ forbids braced-groups within expressions");
3241
3242             /* And they're not allowed outside of a function-body; you
3243                cannot, for example, write:
3244
3245                  int i = ({ int j = 3; j + 1; });
3246
3247                at class or namespace scope.  */
3248             if (!parser->in_function_body
3249                 || parser->in_template_argument_list_p)
3250               {
3251                 error ("%Hstatement-expressions are not allowed outside "
3252                        "functions nor in template-argument lists",
3253                        &token->location);
3254                 cp_parser_skip_to_end_of_block_or_statement (parser);
3255                 expr = error_mark_node;
3256               }
3257             else
3258               {
3259                 /* Start the statement-expression.  */
3260                 expr = begin_stmt_expr ();
3261                 /* Parse the compound-statement.  */
3262                 cp_parser_compound_statement (parser, expr, false);
3263                 /* Finish up.  */
3264                 expr = finish_stmt_expr (expr, false);
3265               }
3266           }
3267         else
3268           {
3269             /* Parse the parenthesized expression.  */
3270             expr = cp_parser_expression (parser, cast_p, idk);
3271             /* Let the front end know that this expression was
3272                enclosed in parentheses. This matters in case, for
3273                example, the expression is of the form `A::B', since
3274                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3275                not.  */
3276             finish_parenthesized_expr (expr);
3277             /* DR 705: Wrapping an unqualified name in parentheses
3278                suppresses arg-dependent lookup.  We want to pass back
3279                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3280                (c++/37862), but none of the others.  */
3281             if (*idk != CP_ID_KIND_QUALIFIED)
3282               *idk = CP_ID_KIND_NONE;
3283           }
3284         /* The `>' token might be the end of a template-id or
3285            template-parameter-list now.  */
3286         parser->greater_than_is_operator_p
3287           = saved_greater_than_is_operator_p;
3288         /* Consume the `)'.  */
3289         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3290           cp_parser_skip_to_end_of_statement (parser);
3291
3292         return expr;
3293       }
3294
3295     case CPP_KEYWORD:
3296       switch (token->keyword)
3297         {
3298           /* These two are the boolean literals.  */
3299         case RID_TRUE:
3300           cp_lexer_consume_token (parser->lexer);
3301           return boolean_true_node;
3302         case RID_FALSE:
3303           cp_lexer_consume_token (parser->lexer);
3304           return boolean_false_node;
3305
3306           /* The `__null' literal.  */
3307         case RID_NULL:
3308           cp_lexer_consume_token (parser->lexer);
3309           return null_node;
3310
3311           /* Recognize the `this' keyword.  */
3312         case RID_THIS:
3313           cp_lexer_consume_token (parser->lexer);
3314           if (parser->local_variables_forbidden_p)
3315             {
3316               error ("%H%<this%> may not be used in this context",
3317                      &token->location);
3318               return error_mark_node;
3319             }
3320           /* Pointers cannot appear in constant-expressions.  */
3321           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3322             return error_mark_node;
3323           return finish_this_expr ();
3324
3325           /* The `operator' keyword can be the beginning of an
3326              id-expression.  */
3327         case RID_OPERATOR:
3328           goto id_expression;
3329
3330         case RID_FUNCTION_NAME:
3331         case RID_PRETTY_FUNCTION_NAME:
3332         case RID_C99_FUNCTION_NAME:
3333           {
3334             const char *name;
3335
3336             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3337                __func__ are the names of variables -- but they are
3338                treated specially.  Therefore, they are handled here,
3339                rather than relying on the generic id-expression logic
3340                below.  Grammatically, these names are id-expressions.
3341
3342                Consume the token.  */
3343             token = cp_lexer_consume_token (parser->lexer);
3344
3345             switch (token->keyword)
3346               {
3347               case RID_FUNCTION_NAME:
3348                 name = "%<__FUNCTION__%>";
3349                 break;
3350               case RID_PRETTY_FUNCTION_NAME:
3351                 name = "%<__PRETTY_FUNCTION__%>";
3352                 break;
3353               case RID_C99_FUNCTION_NAME:
3354                 name = "%<__func__%>";
3355                 break;
3356               default:
3357                 gcc_unreachable ();
3358               }
3359
3360             if (cp_parser_non_integral_constant_expression (parser, name))
3361               return error_mark_node;
3362
3363             /* Look up the name.  */
3364             return finish_fname (token->u.value);
3365           }
3366
3367         case RID_VA_ARG:
3368           {
3369             tree expression;
3370             tree type;
3371
3372             /* The `__builtin_va_arg' construct is used to handle
3373                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3374             cp_lexer_consume_token (parser->lexer);
3375             /* Look for the opening `('.  */
3376             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3377             /* Now, parse the assignment-expression.  */
3378             expression = cp_parser_assignment_expression (parser,
3379                                                           /*cast_p=*/false, NULL);
3380             /* Look for the `,'.  */
3381             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3382             /* Parse the type-id.  */
3383             type = cp_parser_type_id (parser);
3384             /* Look for the closing `)'.  */
3385             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3386             /* Using `va_arg' in a constant-expression is not
3387                allowed.  */
3388             if (cp_parser_non_integral_constant_expression (parser,
3389                                                             "%<va_arg%>"))
3390               return error_mark_node;
3391             return build_x_va_arg (expression, type);
3392           }
3393
3394         case RID_OFFSETOF:
3395           return cp_parser_builtin_offsetof (parser);
3396
3397         case RID_HAS_NOTHROW_ASSIGN:
3398         case RID_HAS_NOTHROW_CONSTRUCTOR:
3399         case RID_HAS_NOTHROW_COPY:        
3400         case RID_HAS_TRIVIAL_ASSIGN:
3401         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3402         case RID_HAS_TRIVIAL_COPY:        
3403         case RID_HAS_TRIVIAL_DESTRUCTOR:
3404         case RID_HAS_VIRTUAL_DESTRUCTOR:
3405         case RID_IS_ABSTRACT:
3406         case RID_IS_BASE_OF:
3407         case RID_IS_CLASS:
3408         case RID_IS_CONVERTIBLE_TO:
3409         case RID_IS_EMPTY:
3410         case RID_IS_ENUM:
3411         case RID_IS_POD:
3412         case RID_IS_POLYMORPHIC:
3413         case RID_IS_UNION:
3414           return cp_parser_trait_expr (parser, token->keyword);
3415
3416         /* Objective-C++ expressions.  */
3417         case RID_AT_ENCODE:
3418         case RID_AT_PROTOCOL:
3419         case RID_AT_SELECTOR:
3420           return cp_parser_objc_expression (parser);
3421
3422         default:
3423           cp_parser_error (parser, "expected primary-expression");
3424           return error_mark_node;
3425         }
3426
3427       /* An id-expression can start with either an identifier, a
3428          `::' as the beginning of a qualified-id, or the "operator"
3429          keyword.  */
3430     case CPP_NAME:
3431     case CPP_SCOPE:
3432     case CPP_TEMPLATE_ID:
3433     case CPP_NESTED_NAME_SPECIFIER:
3434       {
3435         tree id_expression;
3436         tree decl;
3437         const char *error_msg;
3438         bool template_p;
3439         bool done;
3440         cp_token *id_expr_token;
3441
3442       id_expression:
3443         /* Parse the id-expression.  */
3444         id_expression
3445           = cp_parser_id_expression (parser,
3446                                      /*template_keyword_p=*/false,
3447                                      /*check_dependency_p=*/true,
3448                                      &template_p,
3449                                      /*declarator_p=*/false,
3450                                      /*optional_p=*/false);
3451         if (id_expression == error_mark_node)
3452           return error_mark_node;
3453         id_expr_token = token;
3454         token = cp_lexer_peek_token (parser->lexer);
3455         done = (token->type != CPP_OPEN_SQUARE
3456                 && token->type != CPP_OPEN_PAREN
3457                 && token->type != CPP_DOT
3458                 && token->type != CPP_DEREF
3459                 && token->type != CPP_PLUS_PLUS
3460                 && token->type != CPP_MINUS_MINUS);
3461         /* If we have a template-id, then no further lookup is
3462            required.  If the template-id was for a template-class, we
3463            will sometimes have a TYPE_DECL at this point.  */
3464         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3465                  || TREE_CODE (id_expression) == TYPE_DECL)
3466           decl = id_expression;
3467         /* Look up the name.  */
3468         else
3469           {
3470             tree ambiguous_decls;
3471
3472             decl = cp_parser_lookup_name (parser, id_expression,
3473                                           none_type,
3474                                           template_p,
3475                                           /*is_namespace=*/false,
3476                                           /*check_dependency=*/true,
3477                                           &ambiguous_decls,
3478                                           id_expr_token->location);
3479             /* If the lookup was ambiguous, an error will already have
3480                been issued.  */
3481             if (ambiguous_decls)
3482               return error_mark_node;
3483
3484             /* In Objective-C++, an instance variable (ivar) may be preferred
3485                to whatever cp_parser_lookup_name() found.  */
3486             decl = objc_lookup_ivar (decl, id_expression);
3487
3488             /* If name lookup gives us a SCOPE_REF, then the
3489                qualifying scope was dependent.  */
3490             if (TREE_CODE (decl) == SCOPE_REF)
3491               {
3492                 /* At this point, we do not know if DECL is a valid
3493                    integral constant expression.  We assume that it is
3494                    in fact such an expression, so that code like:
3495
3496                       template <int N> struct A {
3497                         int a[B<N>::i];
3498                       };
3499                      
3500                    is accepted.  At template-instantiation time, we
3501                    will check that B<N>::i is actually a constant.  */
3502                 return decl;
3503               }
3504             /* Check to see if DECL is a local variable in a context
3505                where that is forbidden.  */
3506             if (parser->local_variables_forbidden_p
3507                 && local_variable_p (decl))
3508               {
3509                 /* It might be that we only found DECL because we are
3510                    trying to be generous with pre-ISO scoping rules.
3511                    For example, consider:
3512
3513                      int i;
3514                      void g() {
3515                        for (int i = 0; i < 10; ++i) {}
3516                        extern void f(int j = i);
3517                      }
3518
3519                    Here, name look up will originally find the out
3520                    of scope `i'.  We need to issue a warning message,
3521                    but then use the global `i'.  */
3522                 decl = check_for_out_of_scope_variable (decl);
3523                 if (local_variable_p (decl))
3524                   {
3525                     error ("%Hlocal variable %qD may not appear in this context",
3526                            &id_expr_token->location, decl);
3527                     return error_mark_node;
3528                   }
3529               }
3530           }
3531
3532         decl = (finish_id_expression
3533                 (id_expression, decl, parser->scope,
3534                  idk,
3535                  parser->integral_constant_expression_p,
3536                  parser->allow_non_integral_constant_expression_p,
3537                  &parser->non_integral_constant_expression_p,
3538                  template_p, done, address_p,
3539                  template_arg_p,
3540                  &error_msg,
3541                  id_expr_token->location));
3542         if (error_msg)
3543           cp_parser_error (parser, error_msg);
3544         return decl;
3545       }
3546
3547       /* Anything else is an error.  */
3548     default:
3549       /* ...unless we have an Objective-C++ message or string literal,
3550          that is.  */
3551       if (c_dialect_objc ()
3552           && (token->type == CPP_OPEN_SQUARE
3553               || token->type == CPP_OBJC_STRING))
3554         return cp_parser_objc_expression (parser);
3555
3556       cp_parser_error (parser, "expected primary-expression");
3557       return error_mark_node;
3558     }
3559 }
3560
3561 /* Parse an id-expression.
3562
3563    id-expression:
3564      unqualified-id
3565      qualified-id
3566
3567    qualified-id:
3568      :: [opt] nested-name-specifier template [opt] unqualified-id
3569      :: identifier
3570      :: operator-function-id
3571      :: template-id
3572
3573    Return a representation of the unqualified portion of the
3574    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3575    a `::' or nested-name-specifier.
3576
3577    Often, if the id-expression was a qualified-id, the caller will
3578    want to make a SCOPE_REF to represent the qualified-id.  This
3579    function does not do this in order to avoid wastefully creating
3580    SCOPE_REFs when they are not required.
3581
3582    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3583    `template' keyword.
3584
3585    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3586    uninstantiated templates.
3587
3588    If *TEMPLATE_P is non-NULL, it is set to true iff the
3589    `template' keyword is used to explicitly indicate that the entity
3590    named is a template.
3591
3592    If DECLARATOR_P is true, the id-expression is appearing as part of
3593    a declarator, rather than as part of an expression.  */
3594
3595 static tree
3596 cp_parser_id_expression (cp_parser *parser,
3597                          bool template_keyword_p,
3598                          bool check_dependency_p,
3599                          bool *template_p,
3600                          bool declarator_p,
3601                          bool optional_p)
3602 {
3603   bool global_scope_p;
3604   bool nested_name_specifier_p;
3605
3606   /* Assume the `template' keyword was not used.  */
3607   if (template_p)
3608     *template_p = template_keyword_p;
3609
3610   /* Look for the optional `::' operator.  */
3611   global_scope_p
3612     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3613        != NULL_TREE);
3614   /* Look for the optional nested-name-specifier.  */
3615   nested_name_specifier_p
3616     = (cp_parser_nested_name_specifier_opt (parser,
3617                                             /*typename_keyword_p=*/false,
3618                                             check_dependency_p,
3619                                             /*type_p=*/false,
3620                                             declarator_p)
3621        != NULL_TREE);
3622   /* If there is a nested-name-specifier, then we are looking at
3623      the first qualified-id production.  */
3624   if (nested_name_specifier_p)
3625     {
3626       tree saved_scope;
3627       tree saved_object_scope;
3628       tree saved_qualifying_scope;
3629       tree unqualified_id;
3630       bool is_template;
3631
3632       /* See if the next token is the `template' keyword.  */
3633       if (!template_p)
3634         template_p = &is_template;
3635       *template_p = cp_parser_optional_template_keyword (parser);
3636       /* Name lookup we do during the processing of the
3637          unqualified-id might obliterate SCOPE.  */
3638       saved_scope = parser->scope;
3639       saved_object_scope = parser->object_scope;
3640       saved_qualifying_scope = parser->qualifying_scope;
3641       /* Process the final unqualified-id.  */
3642       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3643                                                  check_dependency_p,
3644                                                  declarator_p,
3645                                                  /*optional_p=*/false);
3646       /* Restore the SAVED_SCOPE for our caller.  */
3647       parser->scope = saved_scope;
3648       parser->object_scope = saved_object_scope;
3649       parser->qualifying_scope = saved_qualifying_scope;
3650
3651       return unqualified_id;
3652     }
3653   /* Otherwise, if we are in global scope, then we are looking at one
3654      of the other qualified-id productions.  */
3655   else if (global_scope_p)
3656     {
3657       cp_token *token;
3658       tree id;
3659
3660       /* Peek at the next token.  */
3661       token = cp_lexer_peek_token (parser->lexer);
3662
3663       /* If it's an identifier, and the next token is not a "<", then
3664          we can avoid the template-id case.  This is an optimization
3665          for this common case.  */
3666       if (token->type == CPP_NAME
3667           && !cp_parser_nth_token_starts_template_argument_list_p
3668                (parser, 2))
3669         return cp_parser_identifier (parser);
3670
3671       cp_parser_parse_tentatively (parser);
3672       /* Try a template-id.  */
3673       id = cp_parser_template_id (parser,
3674                                   /*template_keyword_p=*/false,
3675                                   /*check_dependency_p=*/true,
3676                                   declarator_p);
3677       /* If that worked, we're done.  */
3678       if (cp_parser_parse_definitely (parser))
3679         return id;
3680
3681       /* Peek at the next token.  (Changes in the token buffer may
3682          have invalidated the pointer obtained above.)  */
3683       token = cp_lexer_peek_token (parser->lexer);
3684
3685       switch (token->type)
3686         {
3687         case CPP_NAME:
3688           return cp_parser_identifier (parser);
3689
3690         case CPP_KEYWORD:
3691           if (token->keyword == RID_OPERATOR)
3692             return cp_parser_operator_function_id (parser);
3693           /* Fall through.  */
3694
3695         default:
3696           cp_parser_error (parser, "expected id-expression");
3697           return error_mark_node;
3698         }
3699     }
3700   else
3701     return cp_parser_unqualified_id (parser, template_keyword_p,
3702                                      /*check_dependency_p=*/true,
3703                                      declarator_p,
3704                                      optional_p);
3705 }
3706
3707 /* Parse an unqualified-id.
3708
3709    unqualified-id:
3710      identifier
3711      operator-function-id
3712      conversion-function-id
3713      ~ class-name
3714      template-id
3715
3716    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3717    keyword, in a construct like `A::template ...'.
3718
3719    Returns a representation of unqualified-id.  For the `identifier'
3720    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3721    production a BIT_NOT_EXPR is returned; the operand of the
3722    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3723    other productions, see the documentation accompanying the
3724    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3725    names are looked up in uninstantiated templates.  If DECLARATOR_P
3726    is true, the unqualified-id is appearing as part of a declarator,
3727    rather than as part of an expression.  */
3728
3729 static tree
3730 cp_parser_unqualified_id (cp_parser* parser,
3731                           bool template_keyword_p,
3732                           bool check_dependency_p,
3733                           bool declarator_p,
3734                           bool optional_p)
3735 {
3736   cp_token *token;
3737
3738   /* Peek at the next token.  */
3739   token = cp_lexer_peek_token (parser->lexer);
3740
3741   switch (token->type)
3742     {
3743     case CPP_NAME:
3744       {
3745         tree id;
3746
3747         /* We don't know yet whether or not this will be a
3748            template-id.  */
3749         cp_parser_parse_tentatively (parser);
3750         /* Try a template-id.  */
3751         id = cp_parser_template_id (parser, template_keyword_p,
3752                                     check_dependency_p,
3753                                     declarator_p);
3754         /* If it worked, we're done.  */
3755         if (cp_parser_parse_definitely (parser))
3756           return id;
3757         /* Otherwise, it's an ordinary identifier.  */
3758         return cp_parser_identifier (parser);
3759       }
3760
3761     case CPP_TEMPLATE_ID:
3762       return cp_parser_template_id (parser, template_keyword_p,
3763                                     check_dependency_p,
3764                                     declarator_p);
3765
3766     case CPP_COMPL:
3767       {
3768         tree type_decl;
3769         tree qualifying_scope;
3770         tree object_scope;
3771         tree scope;
3772         bool done;
3773
3774         /* Consume the `~' token.  */
3775         cp_lexer_consume_token (parser->lexer);
3776         /* Parse the class-name.  The standard, as written, seems to
3777            say that:
3778
3779              template <typename T> struct S { ~S (); };
3780              template <typename T> S<T>::~S() {}
3781
3782            is invalid, since `~' must be followed by a class-name, but
3783            `S<T>' is dependent, and so not known to be a class.
3784            That's not right; we need to look in uninstantiated
3785            templates.  A further complication arises from:
3786
3787              template <typename T> void f(T t) {
3788                t.T::~T();
3789              }
3790
3791            Here, it is not possible to look up `T' in the scope of `T'
3792            itself.  We must look in both the current scope, and the
3793            scope of the containing complete expression.
3794
3795            Yet another issue is:
3796
3797              struct S {
3798                int S;
3799                ~S();
3800              };
3801
3802              S::~S() {}
3803
3804            The standard does not seem to say that the `S' in `~S'
3805            should refer to the type `S' and not the data member
3806            `S::S'.  */
3807
3808         /* DR 244 says that we look up the name after the "~" in the
3809            same scope as we looked up the qualifying name.  That idea
3810            isn't fully worked out; it's more complicated than that.  */
3811         scope = parser->scope;
3812         object_scope = parser->object_scope;
3813         qualifying_scope = parser->qualifying_scope;
3814
3815         /* Check for invalid scopes.  */
3816         if (scope == error_mark_node)
3817           {
3818             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3819               cp_lexer_consume_token (parser->lexer);
3820             return error_mark_node;
3821           }
3822         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3823           {
3824             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3825               error ("%Hscope %qT before %<~%> is not a class-name",
3826                      &token->location, scope);
3827             cp_parser_simulate_error (parser);
3828             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3829               cp_lexer_consume_token (parser->lexer);
3830             return error_mark_node;
3831           }
3832         gcc_assert (!scope || TYPE_P (scope));
3833
3834         /* If the name is of the form "X::~X" it's OK.  */
3835         token = cp_lexer_peek_token (parser->lexer);
3836         if (scope
3837             && token->type == CPP_NAME
3838             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3839                 == CPP_OPEN_PAREN)
3840             && constructor_name_p (token->u.value, scope))
3841           {
3842             cp_lexer_consume_token (parser->lexer);
3843             return build_nt (BIT_NOT_EXPR, scope);
3844           }
3845
3846         /* If there was an explicit qualification (S::~T), first look
3847            in the scope given by the qualification (i.e., S).  */
3848         done = false;
3849         type_decl = NULL_TREE;
3850         if (scope)
3851           {
3852             cp_parser_parse_tentatively (parser);
3853             type_decl = cp_parser_class_name (parser,
3854                                               /*typename_keyword_p=*/false,
3855                                               /*template_keyword_p=*/false,
3856                                               none_type,
3857                                               /*check_dependency=*/false,
3858                                               /*class_head_p=*/false,
3859                                               declarator_p);
3860             if (cp_parser_parse_definitely (parser))
3861               done = true;
3862           }
3863         /* In "N::S::~S", look in "N" as well.  */
3864         if (!done && scope && qualifying_scope)
3865           {
3866             cp_parser_parse_tentatively (parser);
3867             parser->scope = qualifying_scope;
3868             parser->object_scope = NULL_TREE;
3869             parser->qualifying_scope = NULL_TREE;
3870             type_decl
3871               = cp_parser_class_name (parser,
3872                                       /*typename_keyword_p=*/false,
3873                                       /*template_keyword_p=*/false,
3874                                       none_type,
3875                                       /*check_dependency=*/false,
3876                                       /*class_head_p=*/false,
3877                                       declarator_p);
3878             if (cp_parser_parse_definitely (parser))
3879               done = true;
3880           }
3881         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3882         else if (!done && object_scope)
3883           {
3884             cp_parser_parse_tentatively (parser);
3885             parser->scope = object_scope;
3886             parser->object_scope = NULL_TREE;
3887             parser->qualifying_scope = NULL_TREE;
3888             type_decl
3889               = cp_parser_class_name (parser,
3890                                       /*typename_keyword_p=*/false,
3891                                       /*template_keyword_p=*/false,
3892                                       none_type,
3893                                       /*check_dependency=*/false,
3894                                       /*class_head_p=*/false,
3895                                       declarator_p);
3896             if (cp_parser_parse_definitely (parser))
3897               done = true;
3898           }
3899         /* Look in the surrounding context.  */
3900         if (!done)
3901           {
3902             parser->scope = NULL_TREE;
3903             parser->object_scope = NULL_TREE;
3904             parser->qualifying_scope = NULL_TREE;
3905             if (processing_template_decl)
3906               cp_parser_parse_tentatively (parser);
3907             type_decl
3908               = cp_parser_class_name (parser,
3909                                       /*typename_keyword_p=*/false,
3910                                       /*template_keyword_p=*/false,
3911                                       none_type,
3912                                       /*check_dependency=*/false,
3913                                       /*class_head_p=*/false,
3914                                       declarator_p);
3915             if (processing_template_decl
3916                 && ! cp_parser_parse_definitely (parser))
3917               {
3918                 /* We couldn't find a type with this name, so just accept
3919                    it and check for a match at instantiation time.  */
3920                 type_decl = cp_parser_identifier (parser);
3921                 if (type_decl != error_mark_node)
3922                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3923                 return type_decl;
3924               }
3925           }
3926         /* If an error occurred, assume that the name of the
3927            destructor is the same as the name of the qualifying
3928            class.  That allows us to keep parsing after running
3929            into ill-formed destructor names.  */
3930         if (type_decl == error_mark_node && scope)
3931           return build_nt (BIT_NOT_EXPR, scope);
3932         else if (type_decl == error_mark_node)
3933           return error_mark_node;
3934
3935         /* Check that destructor name and scope match.  */
3936         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3937           {
3938             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3939               error ("%Hdeclaration of %<~%T%> as member of %qT",
3940                      &token->location, type_decl, scope);
3941             cp_parser_simulate_error (parser);
3942             return error_mark_node;
3943           }
3944
3945         /* [class.dtor]
3946
3947            A typedef-name that names a class shall not be used as the
3948            identifier in the declarator for a destructor declaration.  */
3949         if (declarator_p
3950             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3951             && !DECL_SELF_REFERENCE_P (type_decl)
3952             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3953           error ("%Htypedef-name %qD used as destructor declarator",
3954                  &token->location, type_decl);
3955
3956         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3957       }
3958
3959     case CPP_KEYWORD:
3960       if (token->keyword == RID_OPERATOR)
3961         {
3962           tree id;
3963
3964           /* This could be a template-id, so we try that first.  */
3965           cp_parser_parse_tentatively (parser);
3966           /* Try a template-id.  */
3967           id = cp_parser_template_id (parser, template_keyword_p,
3968                                       /*check_dependency_p=*/true,
3969                                       declarator_p);
3970           /* If that worked, we're done.  */
3971           if (cp_parser_parse_definitely (parser))
3972             return id;
3973           /* We still don't know whether we're looking at an
3974              operator-function-id or a conversion-function-id.  */
3975           cp_parser_parse_tentatively (parser);
3976           /* Try an operator-function-id.  */
3977           id = cp_parser_operator_function_id (parser);
3978           /* If that didn't work, try a conversion-function-id.  */
3979           if (!cp_parser_parse_definitely (parser))
3980             id = cp_parser_conversion_function_id (parser);
3981
3982           return id;
3983         }
3984       /* Fall through.  */
3985
3986     default:
3987       if (optional_p)
3988         return NULL_TREE;
3989       cp_parser_error (parser, "expected unqualified-id");
3990       return error_mark_node;
3991     }
3992 }
3993
3994 /* Parse an (optional) nested-name-specifier.
3995
3996    nested-name-specifier: [C++98]
3997      class-or-namespace-name :: nested-name-specifier [opt]
3998      class-or-namespace-name :: template nested-name-specifier [opt]
3999
4000    nested-name-specifier: [C++0x]
4001      type-name ::
4002      namespace-name ::
4003      nested-name-specifier identifier ::
4004      nested-name-specifier template [opt] simple-template-id ::
4005
4006    PARSER->SCOPE should be set appropriately before this function is
4007    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4008    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4009    in name lookups.
4010
4011    Sets PARSER->SCOPE to the class (TYPE) or namespace
4012    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4013    it unchanged if there is no nested-name-specifier.  Returns the new
4014    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4015
4016    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4017    part of a declaration and/or decl-specifier.  */
4018
4019 static tree
4020 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4021                                      bool typename_keyword_p,
4022                                      bool check_dependency_p,
4023                                      bool type_p,
4024                                      bool is_declaration)
4025 {
4026   bool success = false;
4027   cp_token_position start = 0;
4028   cp_token *token;
4029
4030   /* Remember where the nested-name-specifier starts.  */
4031   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4032     {
4033       start = cp_lexer_token_position (parser->lexer, false);
4034       push_deferring_access_checks (dk_deferred);
4035     }
4036
4037   while (true)
4038     {
4039       tree new_scope;
4040       tree old_scope;
4041       tree saved_qualifying_scope;
4042       bool template_keyword_p;
4043
4044       /* Spot cases that cannot be the beginning of a
4045          nested-name-specifier.  */
4046       token = cp_lexer_peek_token (parser->lexer);
4047
4048       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4049          the already parsed nested-name-specifier.  */
4050       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4051         {
4052           /* Grab the nested-name-specifier and continue the loop.  */
4053           cp_parser_pre_parsed_nested_name_specifier (parser);
4054           /* If we originally encountered this nested-name-specifier
4055              with IS_DECLARATION set to false, we will not have
4056              resolved TYPENAME_TYPEs, so we must do so here.  */
4057           if (is_declaration
4058               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059             {
4060               new_scope = resolve_typename_type (parser->scope,
4061                                                  /*only_current_p=*/false);
4062               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4063                 parser->scope = new_scope;
4064             }
4065           success = true;
4066           continue;
4067         }
4068
4069       /* Spot cases that cannot be the beginning of a
4070          nested-name-specifier.  On the second and subsequent times
4071          through the loop, we look for the `template' keyword.  */
4072       if (success && token->keyword == RID_TEMPLATE)
4073         ;
4074       /* A template-id can start a nested-name-specifier.  */
4075       else if (token->type == CPP_TEMPLATE_ID)
4076         ;
4077       else
4078         {
4079           /* If the next token is not an identifier, then it is
4080              definitely not a type-name or namespace-name.  */
4081           if (token->type != CPP_NAME)
4082             break;
4083           /* If the following token is neither a `<' (to begin a
4084              template-id), nor a `::', then we are not looking at a
4085              nested-name-specifier.  */
4086           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4087           if (token->type != CPP_SCOPE
4088               && !cp_parser_nth_token_starts_template_argument_list_p
4089                   (parser, 2))
4090             break;
4091         }
4092
4093       /* The nested-name-specifier is optional, so we parse
4094          tentatively.  */
4095       cp_parser_parse_tentatively (parser);
4096
4097       /* Look for the optional `template' keyword, if this isn't the
4098          first time through the loop.  */
4099       if (success)
4100         template_keyword_p = cp_parser_optional_template_keyword (parser);
4101       else
4102         template_keyword_p = false;
4103
4104       /* Save the old scope since the name lookup we are about to do
4105          might destroy it.  */
4106       old_scope = parser->scope;
4107       saved_qualifying_scope = parser->qualifying_scope;
4108       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4109          look up names in "X<T>::I" in order to determine that "Y" is
4110          a template.  So, if we have a typename at this point, we make
4111          an effort to look through it.  */
4112       if (is_declaration
4113           && !typename_keyword_p
4114           && parser->scope
4115           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4116         parser->scope = resolve_typename_type (parser->scope,
4117                                                /*only_current_p=*/false);
4118       /* Parse the qualifying entity.  */
4119       new_scope
4120         = cp_parser_qualifying_entity (parser,
4121                                        typename_keyword_p,
4122                                        template_keyword_p,
4123                                        check_dependency_p,
4124                                        type_p,
4125                                        is_declaration);
4126       /* Look for the `::' token.  */
4127       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4128
4129       /* If we found what we wanted, we keep going; otherwise, we're
4130          done.  */
4131       if (!cp_parser_parse_definitely (parser))
4132         {
4133           bool error_p = false;
4134
4135           /* Restore the OLD_SCOPE since it was valid before the
4136              failed attempt at finding the last
4137              class-or-namespace-name.  */
4138           parser->scope = old_scope;
4139           parser->qualifying_scope = saved_qualifying_scope;
4140           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4141             break;
4142           /* If the next token is an identifier, and the one after
4143              that is a `::', then any valid interpretation would have
4144              found a class-or-namespace-name.  */
4145           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4146                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4147                      == CPP_SCOPE)
4148                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4149                      != CPP_COMPL))
4150             {
4151               token = cp_lexer_consume_token (parser->lexer);
4152               if (!error_p)
4153                 {
4154                   if (!token->ambiguous_p)
4155                     {
4156                       tree decl;
4157                       tree ambiguous_decls;
4158
4159                       decl = cp_parser_lookup_name (parser, token->u.value,
4160                                                     none_type,
4161                                                     /*is_template=*/false,
4162                                                     /*is_namespace=*/false,
4163                                                     /*check_dependency=*/true,
4164                                                     &ambiguous_decls,
4165                                                     token->location);
4166                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4167                         error ("%H%qD used without template parameters",
4168                                &token->location, decl);
4169                       else if (ambiguous_decls)
4170                         {
4171                           error ("%Hreference to %qD is ambiguous",
4172                                  &token->location, token->u.value);
4173                           print_candidates (ambiguous_decls);
4174                           decl = error_mark_node;
4175                         }
4176                       else
4177                         {
4178                           const char* msg = "is not a class or namespace";
4179                           if (cxx_dialect != cxx98)
4180                             msg = "is not a class, namespace, or enumeration";
4181                           cp_parser_name_lookup_error
4182                             (parser, token->u.value, decl, msg,
4183                              token->location);
4184                         }
4185                     }
4186                   parser->scope = error_mark_node;
4187                   error_p = true;
4188                   /* Treat this as a successful nested-name-specifier
4189                      due to:
4190
4191                      [basic.lookup.qual]
4192
4193                      If the name found is not a class-name (clause
4194                      _class_) or namespace-name (_namespace.def_), the
4195                      program is ill-formed.  */
4196                   success = true;
4197                 }
4198               cp_lexer_consume_token (parser->lexer);
4199             }
4200           break;
4201         }
4202       /* We've found one valid nested-name-specifier.  */
4203       success = true;
4204       /* Name lookup always gives us a DECL.  */
4205       if (TREE_CODE (new_scope) == TYPE_DECL)
4206         new_scope = TREE_TYPE (new_scope);
4207       /* Uses of "template" must be followed by actual templates.  */
4208       if (template_keyword_p
4209           && !(CLASS_TYPE_P (new_scope)
4210                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4211                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4212                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4213           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4214                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4215                    == TEMPLATE_ID_EXPR)))
4216         permerror (input_location, TYPE_P (new_scope)
4217                    ? "%qT is not a template"
4218                    : "%qD is not a template",
4219                    new_scope);
4220       /* If it is a class scope, try to complete it; we are about to
4221          be looking up names inside the class.  */
4222       if (TYPE_P (new_scope)
4223           /* Since checking types for dependency can be expensive,
4224              avoid doing it if the type is already complete.  */
4225           && !COMPLETE_TYPE_P (new_scope)
4226           /* Do not try to complete dependent types.  */
4227           && !dependent_type_p (new_scope))
4228         {
4229           new_scope = complete_type (new_scope);
4230           /* If it is a typedef to current class, use the current
4231              class instead, as the typedef won't have any names inside
4232              it yet.  */
4233           if (!COMPLETE_TYPE_P (new_scope)
4234               && currently_open_class (new_scope))
4235             new_scope = TYPE_MAIN_VARIANT (new_scope);
4236         }
4237       /* Make sure we look in the right scope the next time through
4238          the loop.  */
4239       parser->scope = new_scope;
4240     }
4241
4242   /* If parsing tentatively, replace the sequence of tokens that makes
4243      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4244      token.  That way, should we re-parse the token stream, we will
4245      not have to repeat the effort required to do the parse, nor will
4246      we issue duplicate error messages.  */
4247   if (success && start)
4248     {
4249       cp_token *token;
4250
4251       token = cp_lexer_token_at (parser->lexer, start);
4252       /* Reset the contents of the START token.  */
4253       token->type = CPP_NESTED_NAME_SPECIFIER;
4254       /* Retrieve any deferred checks.  Do not pop this access checks yet
4255          so the memory will not be reclaimed during token replacing below.  */
4256       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4257       token->u.tree_check_value->value = parser->scope;
4258       token->u.tree_check_value->checks = get_deferred_access_checks ();
4259       token->u.tree_check_value->qualifying_scope =
4260         parser->qualifying_scope;
4261       token->keyword = RID_MAX;
4262
4263       /* Purge all subsequent tokens.  */
4264       cp_lexer_purge_tokens_after (parser->lexer, start);
4265     }
4266
4267   if (start)
4268     pop_to_parent_deferring_access_checks ();
4269
4270   return success ? parser->scope : NULL_TREE;
4271 }
4272
4273 /* Parse a nested-name-specifier.  See
4274    cp_parser_nested_name_specifier_opt for details.  This function
4275    behaves identically, except that it will an issue an error if no
4276    nested-name-specifier is present.  */
4277
4278 static tree
4279 cp_parser_nested_name_specifier (cp_parser *parser,
4280                                  bool typename_keyword_p,
4281                                  bool check_dependency_p,
4282                                  bool type_p,
4283                                  bool is_declaration)
4284 {
4285   tree scope;
4286
4287   /* Look for the nested-name-specifier.  */
4288   scope = cp_parser_nested_name_specifier_opt (parser,
4289                                                typename_keyword_p,
4290                                                check_dependency_p,
4291                                                type_p,
4292                                                is_declaration);
4293   /* If it was not present, issue an error message.  */
4294   if (!scope)
4295     {
4296       cp_parser_error (parser, "expected nested-name-specifier");
4297       parser->scope = NULL_TREE;
4298     }
4299
4300   return scope;
4301 }
4302
4303 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4304    this is either a class-name or a namespace-name (which corresponds
4305    to the class-or-namespace-name production in the grammar). For
4306    C++0x, it can also be a type-name that refers to an enumeration
4307    type.
4308
4309    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4310    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4311    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4312    TYPE_P is TRUE iff the next name should be taken as a class-name,
4313    even the same name is declared to be another entity in the same
4314    scope.
4315
4316    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4317    specified by the class-or-namespace-name.  If neither is found the
4318    ERROR_MARK_NODE is returned.  */
4319
4320 static tree
4321 cp_parser_qualifying_entity (cp_parser *parser,
4322                              bool typename_keyword_p,
4323                              bool template_keyword_p,
4324                              bool check_dependency_p,
4325                              bool type_p,
4326                              bool is_declaration)
4327 {
4328   tree saved_scope;
4329   tree saved_qualifying_scope;
4330   tree saved_object_scope;
4331   tree scope;
4332   bool only_class_p;
4333   bool successful_parse_p;
4334
4335   /* Before we try to parse the class-name, we must save away the
4336      current PARSER->SCOPE since cp_parser_class_name will destroy
4337      it.  */
4338   saved_scope = parser->scope;
4339   saved_qualifying_scope = parser->qualifying_scope;
4340   saved_object_scope = parser->object_scope;
4341   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4342      there is no need to look for a namespace-name.  */
4343   only_class_p = template_keyword_p 
4344     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4345   if (!only_class_p)
4346     cp_parser_parse_tentatively (parser);
4347   scope = cp_parser_class_name (parser,
4348                                 typename_keyword_p,
4349                                 template_keyword_p,
4350                                 type_p ? class_type : none_type,
4351                                 check_dependency_p,
4352                                 /*class_head_p=*/false,
4353                                 is_declaration);
4354   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4355   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4356   if (!only_class_p 
4357       && cxx_dialect != cxx98
4358       && !successful_parse_p)
4359     {
4360       /* Restore the saved scope.  */
4361       parser->scope = saved_scope;
4362       parser->qualifying_scope = saved_qualifying_scope;
4363       parser->object_scope = saved_object_scope;
4364
4365       /* Parse tentatively.  */
4366       cp_parser_parse_tentatively (parser);
4367      
4368       /* Parse a typedef-name or enum-name.  */
4369       scope = cp_parser_nonclass_name (parser);
4370       successful_parse_p = cp_parser_parse_definitely (parser);
4371     }
4372   /* If that didn't work, try for a namespace-name.  */
4373   if (!only_class_p && !successful_parse_p)
4374     {
4375       /* Restore the saved scope.  */
4376       parser->scope = saved_scope;
4377       parser->qualifying_scope = saved_qualifying_scope;
4378       parser->object_scope = saved_object_scope;
4379       /* If we are not looking at an identifier followed by the scope
4380          resolution operator, then this is not part of a
4381          nested-name-specifier.  (Note that this function is only used
4382          to parse the components of a nested-name-specifier.)  */
4383       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4384           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4385         return error_mark_node;
4386       scope = cp_parser_namespace_name (parser);
4387     }
4388
4389   return scope;
4390 }
4391
4392 /* Parse a postfix-expression.
4393
4394    postfix-expression:
4395      primary-expression
4396      postfix-expression [ expression ]
4397      postfix-expression ( expression-list [opt] )
4398      simple-type-specifier ( expression-list [opt] )
4399      typename :: [opt] nested-name-specifier identifier
4400        ( expression-list [opt] )
4401      typename :: [opt] nested-name-specifier template [opt] template-id
4402        ( expression-list [opt] )
4403      postfix-expression . template [opt] id-expression
4404      postfix-expression -> template [opt] id-expression
4405      postfix-expression . pseudo-destructor-name
4406      postfix-expression -> pseudo-destructor-name
4407      postfix-expression ++
4408      postfix-expression --
4409      dynamic_cast < type-id > ( expression )
4410      static_cast < type-id > ( expression )
4411      reinterpret_cast < type-id > ( expression )
4412      const_cast < type-id > ( expression )
4413      typeid ( expression )
4414      typeid ( type-id )
4415
4416    GNU Extension:
4417
4418    postfix-expression:
4419      ( type-id ) { initializer-list , [opt] }
4420
4421    This extension is a GNU version of the C99 compound-literal
4422    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4423    but they are essentially the same concept.)
4424
4425    If ADDRESS_P is true, the postfix expression is the operand of the
4426    `&' operator.  CAST_P is true if this expression is the target of a
4427    cast.
4428
4429    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4430    class member access expressions [expr.ref].
4431
4432    Returns a representation of the expression.  */
4433
4434 static tree
4435 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4436                               bool member_access_only_p,
4437                               cp_id_kind * pidk_return)
4438 {
4439   cp_token *token;
4440   enum rid keyword;
4441   cp_id_kind idk = CP_ID_KIND_NONE;
4442   tree postfix_expression = NULL_TREE;
4443   bool is_member_access = false;
4444
4445   /* Peek at the next token.  */
4446   token = cp_lexer_peek_token (parser->lexer);
4447   /* Some of the productions are determined by keywords.  */
4448   keyword = token->keyword;
4449   switch (keyword)
4450     {
4451     case RID_DYNCAST:
4452     case RID_STATCAST:
4453     case RID_REINTCAST:
4454     case RID_CONSTCAST:
4455       {
4456         tree type;
4457         tree expression;
4458         const char *saved_message;
4459
4460         /* All of these can be handled in the same way from the point
4461            of view of parsing.  Begin by consuming the token
4462            identifying the cast.  */
4463         cp_lexer_consume_token (parser->lexer);
4464
4465         /* New types cannot be defined in the cast.  */
4466         saved_message = parser->type_definition_forbidden_message;
4467         parser->type_definition_forbidden_message
4468           = "types may not be defined in casts";
4469
4470         /* Look for the opening `<'.  */
4471         cp_parser_require (parser, CPP_LESS, "%<<%>");
4472         /* Parse the type to which we are casting.  */
4473         type = cp_parser_type_id (parser);
4474         /* Look for the closing `>'.  */
4475         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4476         /* Restore the old message.  */
4477         parser->type_definition_forbidden_message = saved_message;
4478
4479         /* And the expression which is being cast.  */
4480         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4481         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4482         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4483
4484         /* Only type conversions to integral or enumeration types
4485            can be used in constant-expressions.  */
4486         if (!cast_valid_in_integral_constant_expression_p (type)
4487             && (cp_parser_non_integral_constant_expression
4488                 (parser,
4489                  "a cast to a type other than an integral or "
4490                  "enumeration type")))
4491           return error_mark_node;
4492
4493         switch (keyword)
4494           {
4495           case RID_DYNCAST:
4496             postfix_expression
4497               = build_dynamic_cast (type, expression, tf_warning_or_error);
4498             break;
4499           case RID_STATCAST:
4500             postfix_expression
4501               = build_static_cast (type, expression, tf_warning_or_error);
4502             break;
4503           case RID_REINTCAST:
4504             postfix_expression
4505               = build_reinterpret_cast (type, expression, 
4506                                         tf_warning_or_error);
4507             break;
4508           case RID_CONSTCAST:
4509             postfix_expression
4510               = build_const_cast (type, expression, tf_warning_or_error);
4511             break;
4512           default:
4513             gcc_unreachable ();
4514           }
4515       }
4516       break;
4517
4518     case RID_TYPEID:
4519       {
4520         tree type;
4521         const char *saved_message;
4522         bool saved_in_type_id_in_expr_p;
4523
4524         /* Consume the `typeid' token.  */
4525         cp_lexer_consume_token (parser->lexer);
4526         /* Look for the `(' token.  */
4527         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4528         /* Types cannot be defined in a `typeid' expression.  */
4529         saved_message = parser->type_definition_forbidden_message;
4530         parser->type_definition_forbidden_message
4531           = "types may not be defined in a %<typeid%> expression";
4532         /* We can't be sure yet whether we're looking at a type-id or an
4533            expression.  */
4534         cp_parser_parse_tentatively (parser);
4535         /* Try a type-id first.  */
4536         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4537         parser->in_type_id_in_expr_p = true;
4538         type = cp_parser_type_id (parser);
4539         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4540         /* Look for the `)' token.  Otherwise, we can't be sure that
4541            we're not looking at an expression: consider `typeid (int
4542            (3))', for example.  */
4543         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4544         /* If all went well, simply lookup the type-id.  */
4545         if (cp_parser_parse_definitely (parser))
4546           postfix_expression = get_typeid (type);
4547         /* Otherwise, fall back to the expression variant.  */
4548         else
4549           {
4550             tree expression;
4551
4552             /* Look for an expression.  */
4553             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4554             /* Compute its typeid.  */
4555             postfix_expression = build_typeid (expression);
4556             /* Look for the `)' token.  */
4557             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4558           }
4559         /* Restore the saved message.  */
4560         parser->type_definition_forbidden_message = saved_message;
4561         /* `typeid' may not appear in an integral constant expression.  */
4562         if (cp_parser_non_integral_constant_expression(parser,
4563                                                        "%<typeid%> operator"))
4564           return error_mark_node;
4565       }
4566       break;
4567
4568     case RID_TYPENAME:
4569       {
4570         tree type;
4571         /* The syntax permitted here is the same permitted for an
4572            elaborated-type-specifier.  */
4573         type = cp_parser_elaborated_type_specifier (parser,
4574                                                     /*is_friend=*/false,
4575                                                     /*is_declaration=*/false);
4576         postfix_expression = cp_parser_functional_cast (parser, type);
4577       }
4578       break;
4579
4580     default:
4581       {
4582         tree type;
4583
4584         /* If the next thing is a simple-type-specifier, we may be
4585            looking at a functional cast.  We could also be looking at
4586            an id-expression.  So, we try the functional cast, and if
4587            that doesn't work we fall back to the primary-expression.  */
4588         cp_parser_parse_tentatively (parser);
4589         /* Look for the simple-type-specifier.  */
4590         type = cp_parser_simple_type_specifier (parser,
4591                                                 /*decl_specs=*/NULL,
4592                                                 CP_PARSER_FLAGS_NONE);
4593         /* Parse the cast itself.  */
4594         if (!cp_parser_error_occurred (parser))
4595           postfix_expression
4596             = cp_parser_functional_cast (parser, type);
4597         /* If that worked, we're done.  */
4598         if (cp_parser_parse_definitely (parser))
4599           break;
4600
4601         /* If the functional-cast didn't work out, try a
4602            compound-literal.  */
4603         if (cp_parser_allow_gnu_extensions_p (parser)
4604             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4605           {
4606             VEC(constructor_elt,gc) *initializer_list = NULL;
4607             bool saved_in_type_id_in_expr_p;
4608
4609             cp_parser_parse_tentatively (parser);
4610             /* Consume the `('.  */
4611             cp_lexer_consume_token (parser->lexer);
4612             /* Parse the type.  */
4613             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4614             parser->in_type_id_in_expr_p = true;
4615             type = cp_parser_type_id (parser);
4616             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4617             /* Look for the `)'.  */
4618             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4619             /* Look for the `{'.  */
4620             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4621             /* If things aren't going well, there's no need to
4622                keep going.  */
4623             if (!cp_parser_error_occurred (parser))
4624               {
4625                 bool non_constant_p;
4626                 /* Parse the initializer-list.  */
4627                 initializer_list
4628                   = cp_parser_initializer_list (parser, &non_constant_p);
4629                 /* Allow a trailing `,'.  */
4630                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4631                   cp_lexer_consume_token (parser->lexer);
4632                 /* Look for the final `}'.  */
4633                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4634               }
4635             /* If that worked, we're definitely looking at a
4636                compound-literal expression.  */
4637             if (cp_parser_parse_definitely (parser))
4638               {
4639                 /* Warn the user that a compound literal is not
4640                    allowed in standard C++.  */
4641                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4642                 /* For simplicity, we disallow compound literals in
4643                    constant-expressions.  We could
4644                    allow compound literals of integer type, whose
4645                    initializer was a constant, in constant
4646                    expressions.  Permitting that usage, as a further
4647                    extension, would not change the meaning of any
4648                    currently accepted programs.  (Of course, as
4649                    compound literals are not part of ISO C++, the
4650                    standard has nothing to say.)  */
4651                 if (cp_parser_non_integral_constant_expression 
4652                     (parser, "non-constant compound literals"))
4653                   {
4654                     postfix_expression = error_mark_node;
4655                     break;
4656                   }
4657                 /* Form the representation of the compound-literal.  */
4658                 postfix_expression
4659                   = (finish_compound_literal
4660                      (type, build_constructor (init_list_type_node,
4661                                                initializer_list)));
4662                 break;
4663               }
4664           }
4665
4666         /* It must be a primary-expression.  */
4667         postfix_expression
4668           = cp_parser_primary_expression (parser, address_p, cast_p,
4669                                           /*template_arg_p=*/false,
4670                                           &idk);
4671       }
4672       break;
4673     }
4674
4675   /* Keep looping until the postfix-expression is complete.  */
4676   while (true)
4677     {
4678       if (idk == CP_ID_KIND_UNQUALIFIED
4679           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4680           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4681         /* It is not a Koenig lookup function call.  */
4682         postfix_expression
4683           = unqualified_name_lookup_error (postfix_expression);
4684
4685       /* Peek at the next token.  */
4686       token = cp_lexer_peek_token (parser->lexer);
4687
4688       switch (token->type)
4689         {
4690         case CPP_OPEN_SQUARE:
4691           postfix_expression
4692             = cp_parser_postfix_open_square_expression (parser,
4693                                                         postfix_expression,
4694                                                         false);
4695           idk = CP_ID_KIND_NONE;
4696           is_member_access = false;
4697           break;
4698
4699         case CPP_OPEN_PAREN:
4700           /* postfix-expression ( expression-list [opt] ) */
4701           {
4702             bool koenig_p;
4703             bool is_builtin_constant_p;
4704             bool saved_integral_constant_expression_p = false;
4705             bool saved_non_integral_constant_expression_p = false;
4706             tree args;
4707
4708             is_member_access = false;
4709
4710             is_builtin_constant_p
4711               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4712             if (is_builtin_constant_p)
4713               {
4714                 /* The whole point of __builtin_constant_p is to allow
4715                    non-constant expressions to appear as arguments.  */
4716                 saved_integral_constant_expression_p
4717                   = parser->integral_constant_expression_p;
4718                 saved_non_integral_constant_expression_p
4719                   = parser->non_integral_constant_expression_p;
4720                 parser->integral_constant_expression_p = false;
4721               }
4722             args = (cp_parser_parenthesized_expression_list
4723                     (parser, /*is_attribute_list=*/false,
4724                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4725                      /*non_constant_p=*/NULL));
4726             if (is_builtin_constant_p)
4727               {
4728                 parser->integral_constant_expression_p
4729                   = saved_integral_constant_expression_p;
4730                 parser->non_integral_constant_expression_p
4731                   = saved_non_integral_constant_expression_p;
4732               }
4733
4734             if (args == error_mark_node)
4735               {
4736                 postfix_expression = error_mark_node;
4737                 break;
4738               }
4739
4740             /* Function calls are not permitted in
4741                constant-expressions.  */
4742             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4743                 && cp_parser_non_integral_constant_expression (parser,
4744                                                                "a function call"))
4745               {
4746                 postfix_expression = error_mark_node;
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 (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 (args && is_overloaded_fn (postfix_expression))
4771                   {
4772                     tree fn = get_first_fn (postfix_expression);
4773
4774                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4775                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4776
4777                     /* Only do argument dependent lookup if regular
4778                        lookup does not find a set of member functions.
4779                        [basic.lookup.koenig]/2a  */
4780                     if (!DECL_FUNCTION_MEMBER_P (fn))
4781                       {
4782                         koenig_p = true;
4783                         if (!any_type_dependent_arguments_p (args))
4784                           postfix_expression
4785                             = perform_koenig_lookup (postfix_expression, args);
4786                       }
4787                   }
4788               }
4789
4790             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4791               {
4792                 tree instance = TREE_OPERAND (postfix_expression, 0);
4793                 tree fn = TREE_OPERAND (postfix_expression, 1);
4794
4795                 if (processing_template_decl
4796                     && (type_dependent_expression_p (instance)
4797                         || (!BASELINK_P (fn)
4798                             && TREE_CODE (fn) != FIELD_DECL)
4799                         || type_dependent_expression_p (fn)
4800                         || any_type_dependent_arguments_p (args)))
4801                   {
4802                     postfix_expression
4803                       = build_nt_call_list (postfix_expression, args);
4804                     break;
4805                   }
4806
4807                 if (BASELINK_P (fn))
4808                   {
4809                   postfix_expression
4810                     = (build_new_method_call
4811                        (instance, fn, args, NULL_TREE,
4812                         (idk == CP_ID_KIND_QUALIFIED
4813                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4814                         /*fn_p=*/NULL,
4815                         tf_warning_or_error));
4816                   }
4817                 else
4818                   postfix_expression
4819                     = finish_call_expr (postfix_expression, args,
4820                                         /*disallow_virtual=*/false,
4821                                         /*koenig_p=*/false,
4822                                         tf_warning_or_error);
4823               }
4824             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4825                      || TREE_CODE (postfix_expression) == MEMBER_REF
4826                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4827               postfix_expression = (build_offset_ref_call_from_tree
4828                                     (postfix_expression, args));
4829             else if (idk == CP_ID_KIND_QUALIFIED)
4830               /* A call to a static class member, or a namespace-scope
4831                  function.  */
4832               postfix_expression
4833                 = finish_call_expr (postfix_expression, args,
4834                                     /*disallow_virtual=*/true,
4835                                     koenig_p,
4836                                     tf_warning_or_error);
4837             else
4838               /* All other function calls.  */
4839               postfix_expression
4840                 = finish_call_expr (postfix_expression, args,
4841                                     /*disallow_virtual=*/false,
4842                                     koenig_p,
4843                                     tf_warning_or_error);
4844
4845             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4846             idk = CP_ID_KIND_NONE;
4847           }
4848           break;
4849
4850         case CPP_DOT:
4851         case CPP_DEREF:
4852           /* postfix-expression . template [opt] id-expression
4853              postfix-expression . pseudo-destructor-name
4854              postfix-expression -> template [opt] id-expression
4855              postfix-expression -> pseudo-destructor-name */
4856
4857           /* Consume the `.' or `->' operator.  */
4858           cp_lexer_consume_token (parser->lexer);
4859
4860           postfix_expression
4861             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4862                                                       postfix_expression,
4863                                                       false, &idk,
4864                                                       token->location);
4865
4866           is_member_access = true;
4867           break;
4868
4869         case CPP_PLUS_PLUS:
4870           /* postfix-expression ++  */
4871           /* Consume the `++' token.  */
4872           cp_lexer_consume_token (parser->lexer);
4873           /* Generate a representation for the complete expression.  */
4874           postfix_expression
4875             = finish_increment_expr (postfix_expression,
4876                                      POSTINCREMENT_EXPR);
4877           /* Increments may not appear in constant-expressions.  */
4878           if (cp_parser_non_integral_constant_expression (parser,
4879                                                           "an increment"))
4880             postfix_expression = error_mark_node;
4881           idk = CP_ID_KIND_NONE;
4882           is_member_access = false;
4883           break;
4884
4885         case CPP_MINUS_MINUS:
4886           /* postfix-expression -- */
4887           /* Consume the `--' token.  */
4888           cp_lexer_consume_token (parser->lexer);
4889           /* Generate a representation for the complete expression.  */
4890           postfix_expression
4891             = finish_increment_expr (postfix_expression,
4892                                      POSTDECREMENT_EXPR);
4893           /* Decrements may not appear in constant-expressions.  */
4894           if (cp_parser_non_integral_constant_expression (parser,
4895                                                           "a decrement"))
4896             postfix_expression = error_mark_node;
4897           idk = CP_ID_KIND_NONE;
4898           is_member_access = false;
4899           break;
4900
4901         default:
4902           if (pidk_return != NULL)
4903             * pidk_return = idk;
4904           if (member_access_only_p)
4905             return is_member_access? postfix_expression : error_mark_node;
4906           else
4907             return postfix_expression;
4908         }
4909     }
4910
4911   /* We should never get here.  */
4912   gcc_unreachable ();
4913   return error_mark_node;
4914 }
4915
4916 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4917    by cp_parser_builtin_offsetof.  We're looking for
4918
4919      postfix-expression [ expression ]
4920
4921    FOR_OFFSETOF is set if we're being called in that context, which
4922    changes how we deal with integer constant expressions.  */
4923
4924 static tree
4925 cp_parser_postfix_open_square_expression (cp_parser *parser,
4926                                           tree postfix_expression,
4927                                           bool for_offsetof)
4928 {
4929   tree index;
4930
4931   /* Consume the `[' token.  */
4932   cp_lexer_consume_token (parser->lexer);
4933
4934   /* Parse the index expression.  */
4935   /* ??? For offsetof, there is a question of what to allow here.  If
4936      offsetof is not being used in an integral constant expression context,
4937      then we *could* get the right answer by computing the value at runtime.
4938      If we are in an integral constant expression context, then we might
4939      could accept any constant expression; hard to say without analysis.
4940      Rather than open the barn door too wide right away, allow only integer
4941      constant expressions here.  */
4942   if (for_offsetof)
4943     index = cp_parser_constant_expression (parser, false, NULL);
4944   else
4945     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4946
4947   /* Look for the closing `]'.  */
4948   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4949
4950   /* Build the ARRAY_REF.  */
4951   postfix_expression = grok_array_decl (postfix_expression, index);
4952
4953   /* When not doing offsetof, array references are not permitted in
4954      constant-expressions.  */
4955   if (!for_offsetof
4956       && (cp_parser_non_integral_constant_expression
4957           (parser, "an array reference")))
4958     postfix_expression = error_mark_node;
4959
4960   return postfix_expression;
4961 }
4962
4963 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4964    by cp_parser_builtin_offsetof.  We're looking for
4965
4966      postfix-expression . template [opt] id-expression
4967      postfix-expression . pseudo-destructor-name
4968      postfix-expression -> template [opt] id-expression
4969      postfix-expression -> pseudo-destructor-name
4970
4971    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4972    limits what of the above we'll actually accept, but nevermind.
4973    TOKEN_TYPE is the "." or "->" token, which will already have been
4974    removed from the stream.  */
4975
4976 static tree
4977 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4978                                         enum cpp_ttype token_type,
4979                                         tree postfix_expression,
4980                                         bool for_offsetof, cp_id_kind *idk,
4981                                         location_t location)
4982 {
4983   tree name;
4984   bool dependent_p;
4985   bool pseudo_destructor_p;
4986   tree scope = NULL_TREE;
4987
4988   /* If this is a `->' operator, dereference the pointer.  */
4989   if (token_type == CPP_DEREF)
4990     postfix_expression = build_x_arrow (postfix_expression);
4991   /* Check to see whether or not the expression is type-dependent.  */
4992   dependent_p = type_dependent_expression_p (postfix_expression);
4993   /* The identifier following the `->' or `.' is not qualified.  */
4994   parser->scope = NULL_TREE;
4995   parser->qualifying_scope = NULL_TREE;
4996   parser->object_scope = NULL_TREE;
4997   *idk = CP_ID_KIND_NONE;
4998
4999   /* Enter the scope corresponding to the type of the object
5000      given by the POSTFIX_EXPRESSION.  */
5001   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5002     {
5003       scope = TREE_TYPE (postfix_expression);
5004       /* According to the standard, no expression should ever have
5005          reference type.  Unfortunately, we do not currently match
5006          the standard in this respect in that our internal representation
5007          of an expression may have reference type even when the standard
5008          says it does not.  Therefore, we have to manually obtain the
5009          underlying type here.  */
5010       scope = non_reference (scope);
5011       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5012       if (scope == unknown_type_node)
5013         {
5014           error ("%H%qE does not have class type", &location, postfix_expression);
5015           scope = NULL_TREE;
5016         }
5017       else
5018         scope = complete_type_or_else (scope, NULL_TREE);
5019       /* Let the name lookup machinery know that we are processing a
5020          class member access expression.  */
5021       parser->context->object_type = scope;
5022       /* If something went wrong, we want to be able to discern that case,
5023          as opposed to the case where there was no SCOPE due to the type
5024          of expression being dependent.  */
5025       if (!scope)
5026         scope = error_mark_node;
5027       /* If the SCOPE was erroneous, make the various semantic analysis
5028          functions exit quickly -- and without issuing additional error
5029          messages.  */
5030       if (scope == error_mark_node)
5031         postfix_expression = error_mark_node;
5032     }
5033
5034   /* Assume this expression is not a pseudo-destructor access.  */
5035   pseudo_destructor_p = false;
5036
5037   /* If the SCOPE is a scalar type, then, if this is a valid program,
5038      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5039      is type dependent, it can be pseudo-destructor-name or something else.
5040      Try to parse it as pseudo-destructor-name first.  */
5041   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5042     {
5043       tree s;
5044       tree type;
5045
5046       cp_parser_parse_tentatively (parser);
5047       /* Parse the pseudo-destructor-name.  */
5048       s = NULL_TREE;
5049       cp_parser_pseudo_destructor_name (parser, &s, &type);
5050       if (dependent_p
5051           && (cp_parser_error_occurred (parser)
5052               || TREE_CODE (type) != TYPE_DECL
5053               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5054         cp_parser_abort_tentative_parse (parser);
5055       else if (cp_parser_parse_definitely (parser))
5056         {
5057           pseudo_destructor_p = true;
5058           postfix_expression
5059             = finish_pseudo_destructor_expr (postfix_expression,
5060                                              s, TREE_TYPE (type));
5061         }
5062     }
5063
5064   if (!pseudo_destructor_p)
5065     {
5066       /* If the SCOPE is not a scalar type, we are looking at an
5067          ordinary class member access expression, rather than a
5068          pseudo-destructor-name.  */
5069       bool template_p;
5070       cp_token *token = cp_lexer_peek_token (parser->lexer);
5071       /* Parse the id-expression.  */
5072       name = (cp_parser_id_expression
5073               (parser,
5074                cp_parser_optional_template_keyword (parser),
5075                /*check_dependency_p=*/true,
5076                &template_p,
5077                /*declarator_p=*/false,
5078                /*optional_p=*/false));
5079       /* In general, build a SCOPE_REF if the member name is qualified.
5080          However, if the name was not dependent and has already been
5081          resolved; there is no need to build the SCOPE_REF.  For example;
5082
5083              struct X { void f(); };
5084              template <typename T> void f(T* t) { t->X::f(); }
5085
5086          Even though "t" is dependent, "X::f" is not and has been resolved
5087          to a BASELINK; there is no need to include scope information.  */
5088
5089       /* But we do need to remember that there was an explicit scope for
5090          virtual function calls.  */
5091       if (parser->scope)
5092         *idk = CP_ID_KIND_QUALIFIED;
5093
5094       /* If the name is a template-id that names a type, we will get a
5095          TYPE_DECL here.  That is invalid code.  */
5096       if (TREE_CODE (name) == TYPE_DECL)
5097         {
5098           error ("%Hinvalid use of %qD", &token->location, name);
5099           postfix_expression = error_mark_node;
5100         }
5101       else
5102         {
5103           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5104             {
5105               name = build_qualified_name (/*type=*/NULL_TREE,
5106                                            parser->scope,
5107                                            name,
5108                                            template_p);
5109               parser->scope = NULL_TREE;
5110               parser->qualifying_scope = NULL_TREE;
5111               parser->object_scope = NULL_TREE;
5112             }
5113           if (scope && name && BASELINK_P (name))
5114             adjust_result_of_qualified_name_lookup
5115               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5116           postfix_expression
5117             = finish_class_member_access_expr (postfix_expression, name,
5118                                                template_p, 
5119                                                tf_warning_or_error);
5120         }
5121     }
5122
5123   /* We no longer need to look up names in the scope of the object on
5124      the left-hand side of the `.' or `->' operator.  */
5125   parser->context->object_type = NULL_TREE;
5126
5127   /* Outside of offsetof, these operators may not appear in
5128      constant-expressions.  */
5129   if (!for_offsetof
5130       && (cp_parser_non_integral_constant_expression
5131           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5132     postfix_expression = error_mark_node;
5133
5134   return postfix_expression;
5135 }
5136
5137 /* Parse a parenthesized expression-list.
5138
5139    expression-list:
5140      assignment-expression
5141      expression-list, assignment-expression
5142
5143    attribute-list:
5144      expression-list
5145      identifier
5146      identifier, expression-list
5147
5148    CAST_P is true if this expression is the target of a cast.
5149
5150    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5151    argument pack.
5152
5153    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5154    representation of an assignment-expression.  Note that a TREE_LIST
5155    is returned even if there is only a single expression in the list.
5156    error_mark_node is returned if the ( and or ) are
5157    missing. NULL_TREE is returned on no expressions. The parentheses
5158    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5159    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5160    indicates whether or not all of the expressions in the list were
5161    constant.  */
5162
5163 static tree
5164 cp_parser_parenthesized_expression_list (cp_parser* parser,
5165                                          bool is_attribute_list,
5166                                          bool cast_p,
5167                                          bool allow_expansion_p,
5168                                          bool *non_constant_p)
5169 {
5170   tree expression_list = NULL_TREE;
5171   bool fold_expr_p = is_attribute_list;
5172   tree identifier = NULL_TREE;
5173   bool saved_greater_than_is_operator_p;
5174
5175   /* Assume all the expressions will be constant.  */
5176   if (non_constant_p)
5177     *non_constant_p = false;
5178
5179   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5180     return error_mark_node;
5181
5182   /* Within a parenthesized expression, a `>' token is always
5183      the greater-than operator.  */
5184   saved_greater_than_is_operator_p
5185     = parser->greater_than_is_operator_p;
5186   parser->greater_than_is_operator_p = true;
5187
5188   /* Consume expressions until there are no more.  */
5189   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5190     while (true)
5191       {
5192         tree expr;
5193
5194         /* At the beginning of attribute lists, check to see if the
5195            next token is an identifier.  */
5196         if (is_attribute_list
5197             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5198           {
5199             cp_token *token;
5200
5201             /* Consume the identifier.  */
5202             token = cp_lexer_consume_token (parser->lexer);
5203             /* Save the identifier.  */
5204             identifier = token->u.value;
5205           }
5206         else
5207           {
5208             bool expr_non_constant_p;
5209
5210             /* Parse the next assignment-expression.  */
5211             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5212               {
5213                 /* A braced-init-list.  */
5214                 maybe_warn_cpp0x ("extended initializer lists");
5215                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5216                 if (non_constant_p && expr_non_constant_p)
5217                   *non_constant_p = true;
5218               }
5219             else if (non_constant_p)
5220               {
5221                 expr = (cp_parser_constant_expression
5222                         (parser, /*allow_non_constant_p=*/true,
5223                          &expr_non_constant_p));
5224                 if (expr_non_constant_p)
5225                   *non_constant_p = true;
5226               }
5227             else
5228               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5229
5230             if (fold_expr_p)
5231               expr = fold_non_dependent_expr (expr);
5232
5233             /* If we have an ellipsis, then this is an expression
5234                expansion.  */
5235             if (allow_expansion_p
5236                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5237               {
5238                 /* Consume the `...'.  */
5239                 cp_lexer_consume_token (parser->lexer);
5240
5241                 /* Build the argument pack.  */
5242                 expr = make_pack_expansion (expr);
5243               }
5244
5245              /* Add it to the list.  We add error_mark_node
5246                 expressions to the list, so that we can still tell if
5247                 the correct form for a parenthesized expression-list
5248                 is found. That gives better errors.  */
5249             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5250
5251             if (expr == error_mark_node)
5252               goto skip_comma;
5253           }
5254
5255         /* After the first item, attribute lists look the same as
5256            expression lists.  */
5257         is_attribute_list = false;
5258
5259       get_comma:;
5260         /* If the next token isn't a `,', then we are done.  */
5261         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5262           break;
5263
5264         /* Otherwise, consume the `,' and keep going.  */
5265         cp_lexer_consume_token (parser->lexer);
5266       }
5267
5268   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5269     {
5270       int ending;
5271
5272     skip_comma:;
5273       /* We try and resync to an unnested comma, as that will give the
5274          user better diagnostics.  */
5275       ending = cp_parser_skip_to_closing_parenthesis (parser,
5276                                                       /*recovering=*/true,
5277                                                       /*or_comma=*/true,
5278                                                       /*consume_paren=*/true);
5279       if (ending < 0)
5280         goto get_comma;
5281       if (!ending)
5282         {
5283           parser->greater_than_is_operator_p
5284             = saved_greater_than_is_operator_p;
5285           return error_mark_node;
5286         }
5287     }
5288
5289   parser->greater_than_is_operator_p
5290     = saved_greater_than_is_operator_p;
5291
5292   /* We built up the list in reverse order so we must reverse it now.  */
5293   expression_list = nreverse (expression_list);
5294   if (identifier)
5295     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5296
5297   return expression_list;
5298 }
5299
5300 /* Parse a pseudo-destructor-name.
5301
5302    pseudo-destructor-name:
5303      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5304      :: [opt] nested-name-specifier template template-id :: ~ type-name
5305      :: [opt] nested-name-specifier [opt] ~ type-name
5306
5307    If either of the first two productions is used, sets *SCOPE to the
5308    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5309    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5310    or ERROR_MARK_NODE if the parse fails.  */
5311
5312 static void
5313 cp_parser_pseudo_destructor_name (cp_parser* parser,
5314                                   tree* scope,
5315                                   tree* type)
5316 {
5317   bool nested_name_specifier_p;
5318
5319   /* Assume that things will not work out.  */
5320   *type = error_mark_node;
5321
5322   /* Look for the optional `::' operator.  */
5323   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5324   /* Look for the optional nested-name-specifier.  */
5325   nested_name_specifier_p
5326     = (cp_parser_nested_name_specifier_opt (parser,
5327                                             /*typename_keyword_p=*/false,
5328                                             /*check_dependency_p=*/true,
5329                                             /*type_p=*/false,
5330                                             /*is_declaration=*/false)
5331        != NULL_TREE);
5332   /* Now, if we saw a nested-name-specifier, we might be doing the
5333      second production.  */
5334   if (nested_name_specifier_p
5335       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5336     {
5337       /* Consume the `template' keyword.  */
5338       cp_lexer_consume_token (parser->lexer);
5339       /* Parse the template-id.  */
5340       cp_parser_template_id (parser,
5341                              /*template_keyword_p=*/true,
5342                              /*check_dependency_p=*/false,
5343                              /*is_declaration=*/true);
5344       /* Look for the `::' token.  */
5345       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5346     }
5347   /* If the next token is not a `~', then there might be some
5348      additional qualification.  */
5349   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5350     {
5351       /* At this point, we're looking for "type-name :: ~".  The type-name
5352          must not be a class-name, since this is a pseudo-destructor.  So,
5353          it must be either an enum-name, or a typedef-name -- both of which
5354          are just identifiers.  So, we peek ahead to check that the "::"
5355          and "~" tokens are present; if they are not, then we can avoid
5356          calling type_name.  */
5357       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5358           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5359           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5360         {
5361           cp_parser_error (parser, "non-scalar type");
5362           return;
5363         }
5364
5365       /* Look for the type-name.  */
5366       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5367       if (*scope == error_mark_node)
5368         return;
5369
5370       /* Look for the `::' token.  */
5371       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5372     }
5373   else
5374     *scope = NULL_TREE;
5375
5376   /* Look for the `~'.  */
5377   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5378   /* Look for the type-name again.  We are not responsible for
5379      checking that it matches the first type-name.  */
5380   *type = cp_parser_nonclass_name (parser);
5381 }
5382
5383 /* Parse a unary-expression.
5384
5385    unary-expression:
5386      postfix-expression
5387      ++ cast-expression
5388      -- cast-expression
5389      unary-operator cast-expression
5390      sizeof unary-expression
5391      sizeof ( type-id )
5392      new-expression
5393      delete-expression
5394
5395    GNU Extensions:
5396
5397    unary-expression:
5398      __extension__ cast-expression
5399      __alignof__ unary-expression
5400      __alignof__ ( type-id )
5401      __real__ cast-expression
5402      __imag__ cast-expression
5403      && identifier
5404
5405    ADDRESS_P is true iff the unary-expression is appearing as the
5406    operand of the `&' operator.   CAST_P is true if this expression is
5407    the target of a cast.
5408
5409    Returns a representation of the expression.  */
5410
5411 static tree
5412 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5413                             cp_id_kind * pidk)
5414 {
5415   cp_token *token;
5416   enum tree_code unary_operator;
5417
5418   /* Peek at the next token.  */
5419   token = cp_lexer_peek_token (parser->lexer);
5420   /* Some keywords give away the kind of expression.  */
5421   if (token->type == CPP_KEYWORD)
5422     {
5423       enum rid keyword = token->keyword;
5424
5425       switch (keyword)
5426         {
5427         case RID_ALIGNOF:
5428         case RID_SIZEOF:
5429           {
5430             tree operand;
5431             enum tree_code op;
5432
5433             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5434             /* Consume the token.  */
5435             cp_lexer_consume_token (parser->lexer);
5436             /* Parse the operand.  */
5437             operand = cp_parser_sizeof_operand (parser, keyword);
5438
5439             if (TYPE_P (operand))
5440               return cxx_sizeof_or_alignof_type (operand, op, true);
5441             else
5442               return cxx_sizeof_or_alignof_expr (operand, op, true);
5443           }
5444
5445         case RID_NEW:
5446           return cp_parser_new_expression (parser);
5447
5448         case RID_DELETE:
5449           return cp_parser_delete_expression (parser);
5450
5451         case RID_EXTENSION:
5452           {
5453             /* The saved value of the PEDANTIC flag.  */
5454             int saved_pedantic;
5455             tree expr;
5456
5457             /* Save away the PEDANTIC flag.  */
5458             cp_parser_extension_opt (parser, &saved_pedantic);
5459             /* Parse the cast-expression.  */
5460             expr = cp_parser_simple_cast_expression (parser);
5461             /* Restore the PEDANTIC flag.  */
5462             pedantic = saved_pedantic;
5463
5464             return expr;
5465           }
5466
5467         case RID_REALPART:
5468         case RID_IMAGPART:
5469           {
5470             tree expression;
5471
5472             /* Consume the `__real__' or `__imag__' token.  */
5473             cp_lexer_consume_token (parser->lexer);
5474             /* Parse the cast-expression.  */
5475             expression = cp_parser_simple_cast_expression (parser);
5476             /* Create the complete representation.  */
5477             return build_x_unary_op ((keyword == RID_REALPART
5478                                       ? REALPART_EXPR : IMAGPART_EXPR),
5479                                      expression,
5480                                      tf_warning_or_error);
5481           }
5482           break;
5483
5484         default:
5485           break;
5486         }
5487     }
5488
5489   /* Look for the `:: new' and `:: delete', which also signal the
5490      beginning of a new-expression, or delete-expression,
5491      respectively.  If the next token is `::', then it might be one of
5492      these.  */
5493   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5494     {
5495       enum rid keyword;
5496
5497       /* See if the token after the `::' is one of the keywords in
5498          which we're interested.  */
5499       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5500       /* If it's `new', we have a new-expression.  */
5501       if (keyword == RID_NEW)
5502         return cp_parser_new_expression (parser);
5503       /* Similarly, for `delete'.  */
5504       else if (keyword == RID_DELETE)
5505         return cp_parser_delete_expression (parser);
5506     }
5507
5508   /* Look for a unary operator.  */
5509   unary_operator = cp_parser_unary_operator (token);
5510   /* The `++' and `--' operators can be handled similarly, even though
5511      they are not technically unary-operators in the grammar.  */
5512   if (unary_operator == ERROR_MARK)
5513     {
5514       if (token->type == CPP_PLUS_PLUS)
5515         unary_operator = PREINCREMENT_EXPR;
5516       else if (token->type == CPP_MINUS_MINUS)
5517         unary_operator = PREDECREMENT_EXPR;
5518       /* Handle the GNU address-of-label extension.  */
5519       else if (cp_parser_allow_gnu_extensions_p (parser)
5520                && token->type == CPP_AND_AND)
5521         {
5522           tree identifier;
5523           tree expression;
5524           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5525
5526           /* Consume the '&&' token.  */
5527           cp_lexer_consume_token (parser->lexer);
5528           /* Look for the identifier.  */
5529           identifier = cp_parser_identifier (parser);
5530           /* Create an expression representing the address.  */
5531           expression = finish_label_address_expr (identifier, loc);
5532           if (cp_parser_non_integral_constant_expression (parser,
5533                                                 "the address of a label"))
5534             expression = error_mark_node;
5535           return expression;
5536         }
5537     }
5538   if (unary_operator != ERROR_MARK)
5539     {
5540       tree cast_expression;
5541       tree expression = error_mark_node;
5542       const char *non_constant_p = NULL;
5543
5544       /* Consume the operator token.  */
5545       token = cp_lexer_consume_token (parser->lexer);
5546       /* Parse the cast-expression.  */
5547       cast_expression
5548         = cp_parser_cast_expression (parser,
5549                                      unary_operator == ADDR_EXPR,
5550                                      /*cast_p=*/false, pidk);
5551       /* Now, build an appropriate representation.  */
5552       switch (unary_operator)
5553         {
5554         case INDIRECT_REF:
5555           non_constant_p = "%<*%>";
5556           expression = build_x_indirect_ref (cast_expression, "unary *",
5557                                              tf_warning_or_error);
5558           break;
5559
5560         case ADDR_EXPR:
5561           non_constant_p = "%<&%>";
5562           /* Fall through.  */
5563         case BIT_NOT_EXPR:
5564           expression = build_x_unary_op (unary_operator, cast_expression,
5565                                          tf_warning_or_error);
5566           break;
5567
5568         case PREINCREMENT_EXPR:
5569         case PREDECREMENT_EXPR:
5570           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5571                             ? "%<++%>" : "%<--%>");
5572           /* Fall through.  */
5573         case UNARY_PLUS_EXPR:
5574         case NEGATE_EXPR:
5575         case TRUTH_NOT_EXPR:
5576           expression = finish_unary_op_expr (unary_operator, cast_expression);
5577           break;
5578
5579         default:
5580           gcc_unreachable ();
5581         }
5582
5583       if (non_constant_p
5584           && cp_parser_non_integral_constant_expression (parser,
5585                                                          non_constant_p))
5586         expression = error_mark_node;
5587
5588       return expression;
5589     }
5590
5591   return cp_parser_postfix_expression (parser, address_p, cast_p,
5592                                        /*member_access_only_p=*/false,
5593                                        pidk);
5594 }
5595
5596 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5597    unary-operator, the corresponding tree code is returned.  */
5598
5599 static enum tree_code
5600 cp_parser_unary_operator (cp_token* token)
5601 {
5602   switch (token->type)
5603     {
5604     case CPP_MULT:
5605       return INDIRECT_REF;
5606
5607     case CPP_AND:
5608       return ADDR_EXPR;
5609
5610     case CPP_PLUS:
5611       return UNARY_PLUS_EXPR;
5612
5613     case CPP_MINUS:
5614       return NEGATE_EXPR;
5615
5616     case CPP_NOT:
5617       return TRUTH_NOT_EXPR;
5618
5619     case CPP_COMPL:
5620       return BIT_NOT_EXPR;
5621
5622     default:
5623       return ERROR_MARK;
5624     }
5625 }
5626
5627 /* Parse a new-expression.
5628
5629    new-expression:
5630      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5631      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5632
5633    Returns a representation of the expression.  */
5634
5635 static tree
5636 cp_parser_new_expression (cp_parser* parser)
5637 {
5638   bool global_scope_p;
5639   tree placement;
5640   tree type;
5641   tree initializer;
5642   tree nelts;
5643
5644   /* Look for the optional `::' operator.  */
5645   global_scope_p
5646     = (cp_parser_global_scope_opt (parser,
5647                                    /*current_scope_valid_p=*/false)
5648        != NULL_TREE);
5649   /* Look for the `new' operator.  */
5650   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5651   /* There's no easy way to tell a new-placement from the
5652      `( type-id )' construct.  */
5653   cp_parser_parse_tentatively (parser);
5654   /* Look for a new-placement.  */
5655   placement = cp_parser_new_placement (parser);
5656   /* If that didn't work out, there's no new-placement.  */
5657   if (!cp_parser_parse_definitely (parser))
5658     placement = NULL_TREE;
5659
5660   /* If the next token is a `(', then we have a parenthesized
5661      type-id.  */
5662   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5663     {
5664       cp_token *token;
5665       /* Consume the `('.  */
5666       cp_lexer_consume_token (parser->lexer);
5667       /* Parse the type-id.  */
5668       type = cp_parser_type_id (parser);
5669       /* Look for the closing `)'.  */
5670       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5671       token = cp_lexer_peek_token (parser->lexer);
5672       /* There should not be a direct-new-declarator in this production,
5673          but GCC used to allowed this, so we check and emit a sensible error
5674          message for this case.  */
5675       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5676         {
5677           error ("%Harray bound forbidden after parenthesized type-id",
5678                  &token->location);
5679           inform (token->location, 
5680                   "try removing the parentheses around the type-id");
5681           cp_parser_direct_new_declarator (parser);
5682         }
5683       nelts = NULL_TREE;
5684     }
5685   /* Otherwise, there must be a new-type-id.  */
5686   else
5687     type = cp_parser_new_type_id (parser, &nelts);
5688
5689   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5690   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5691       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5692     initializer = cp_parser_new_initializer (parser);
5693   else
5694     initializer = NULL_TREE;
5695
5696   /* A new-expression may not appear in an integral constant
5697      expression.  */
5698   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5699     return error_mark_node;
5700
5701   /* Create a representation of the new-expression.  */
5702   return build_new (placement, type, nelts, initializer, global_scope_p,
5703                     tf_warning_or_error);
5704 }
5705
5706 /* Parse a new-placement.
5707
5708    new-placement:
5709      ( expression-list )
5710
5711    Returns the same representation as for an expression-list.  */
5712
5713 static tree
5714 cp_parser_new_placement (cp_parser* parser)
5715 {
5716   tree expression_list;
5717
5718   /* Parse the expression-list.  */
5719   expression_list = (cp_parser_parenthesized_expression_list
5720                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5721                       /*non_constant_p=*/NULL));
5722
5723   return expression_list;
5724 }
5725
5726 /* Parse a new-type-id.
5727
5728    new-type-id:
5729      type-specifier-seq new-declarator [opt]
5730
5731    Returns the TYPE allocated.  If the new-type-id indicates an array
5732    type, *NELTS is set to the number of elements in the last array
5733    bound; the TYPE will not include the last array bound.  */
5734
5735 static tree
5736 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5737 {
5738   cp_decl_specifier_seq type_specifier_seq;
5739   cp_declarator *new_declarator;
5740   cp_declarator *declarator;
5741   cp_declarator *outer_declarator;
5742   const char *saved_message;
5743   tree type;
5744
5745   /* The type-specifier sequence must not contain type definitions.
5746      (It cannot contain declarations of new types either, but if they
5747      are not definitions we will catch that because they are not
5748      complete.)  */
5749   saved_message = parser->type_definition_forbidden_message;
5750   parser->type_definition_forbidden_message
5751     = "types may not be defined in a new-type-id";
5752   /* Parse the type-specifier-seq.  */
5753   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5754                                 /*is_trailing_return=*/false,
5755                                 &type_specifier_seq);
5756   /* Restore the old message.  */
5757   parser->type_definition_forbidden_message = saved_message;
5758   /* Parse the new-declarator.  */
5759   new_declarator = cp_parser_new_declarator_opt (parser);
5760
5761   /* Determine the number of elements in the last array dimension, if
5762      any.  */
5763   *nelts = NULL_TREE;
5764   /* Skip down to the last array dimension.  */
5765   declarator = new_declarator;
5766   outer_declarator = NULL;
5767   while (declarator && (declarator->kind == cdk_pointer
5768                         || declarator->kind == cdk_ptrmem))
5769     {
5770       outer_declarator = declarator;
5771       declarator = declarator->declarator;
5772     }
5773   while (declarator
5774          && declarator->kind == cdk_array
5775          && declarator->declarator
5776          && declarator->declarator->kind == cdk_array)
5777     {
5778       outer_declarator = declarator;
5779       declarator = declarator->declarator;
5780     }
5781
5782   if (declarator && declarator->kind == cdk_array)
5783     {
5784       *nelts = declarator->u.array.bounds;
5785       if (*nelts == error_mark_node)
5786         *nelts = integer_one_node;
5787
5788       if (outer_declarator)
5789         outer_declarator->declarator = declarator->declarator;
5790       else
5791         new_declarator = NULL;
5792     }
5793
5794   type = groktypename (&type_specifier_seq, new_declarator, false);
5795   return type;
5796 }
5797
5798 /* Parse an (optional) new-declarator.
5799
5800    new-declarator:
5801      ptr-operator new-declarator [opt]
5802      direct-new-declarator
5803
5804    Returns the declarator.  */
5805
5806 static cp_declarator *
5807 cp_parser_new_declarator_opt (cp_parser* parser)
5808 {
5809   enum tree_code code;
5810   tree type;
5811   cp_cv_quals cv_quals;
5812
5813   /* We don't know if there's a ptr-operator next, or not.  */
5814   cp_parser_parse_tentatively (parser);
5815   /* Look for a ptr-operator.  */
5816   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5817   /* If that worked, look for more new-declarators.  */
5818   if (cp_parser_parse_definitely (parser))
5819     {
5820       cp_declarator *declarator;
5821
5822       /* Parse another optional declarator.  */
5823       declarator = cp_parser_new_declarator_opt (parser);
5824
5825       return cp_parser_make_indirect_declarator
5826         (code, type, cv_quals, declarator);
5827     }
5828
5829   /* If the next token is a `[', there is a direct-new-declarator.  */
5830   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5831     return cp_parser_direct_new_declarator (parser);
5832
5833   return NULL;
5834 }
5835
5836 /* Parse a direct-new-declarator.
5837
5838    direct-new-declarator:
5839      [ expression ]
5840      direct-new-declarator [constant-expression]
5841
5842    */
5843
5844 static cp_declarator *
5845 cp_parser_direct_new_declarator (cp_parser* parser)
5846 {
5847   cp_declarator *declarator = NULL;
5848
5849   while (true)
5850     {
5851       tree expression;
5852
5853       /* Look for the opening `['.  */
5854       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5855       /* The first expression is not required to be constant.  */
5856       if (!declarator)
5857         {
5858           cp_token *token = cp_lexer_peek_token (parser->lexer);
5859           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5860           /* The standard requires that the expression have integral
5861              type.  DR 74 adds enumeration types.  We believe that the
5862              real intent is that these expressions be handled like the
5863              expression in a `switch' condition, which also allows
5864              classes with a single conversion to integral or
5865              enumeration type.  */
5866           if (!processing_template_decl)
5867             {
5868               expression
5869                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5870                                               expression,
5871                                               /*complain=*/true);
5872               if (!expression)
5873                 {
5874                   error ("%Hexpression in new-declarator must have integral "
5875                          "or enumeration type", &token->location);
5876                   expression = error_mark_node;
5877                 }
5878             }
5879         }
5880       /* But all the other expressions must be.  */
5881       else
5882         expression
5883           = cp_parser_constant_expression (parser,
5884                                            /*allow_non_constant=*/false,
5885                                            NULL);
5886       /* Look for the closing `]'.  */
5887       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5888
5889       /* Add this bound to the declarator.  */
5890       declarator = make_array_declarator (declarator, expression);
5891
5892       /* If the next token is not a `[', then there are no more
5893          bounds.  */
5894       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5895         break;
5896     }
5897
5898   return declarator;
5899 }
5900
5901 /* Parse a new-initializer.
5902
5903    new-initializer:
5904      ( expression-list [opt] )
5905      braced-init-list
5906
5907    Returns a representation of the expression-list.  If there is no
5908    expression-list, VOID_ZERO_NODE is returned.  */
5909
5910 static tree
5911 cp_parser_new_initializer (cp_parser* parser)
5912 {
5913   tree expression_list;
5914
5915   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5916     {
5917       bool expr_non_constant_p;
5918       maybe_warn_cpp0x ("extended initializer lists");
5919       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5920       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5921       expression_list = build_tree_list (NULL_TREE, expression_list);
5922     }
5923   else
5924     expression_list = (cp_parser_parenthesized_expression_list
5925                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5926                         /*non_constant_p=*/NULL));
5927   if (!expression_list)
5928     expression_list = void_zero_node;
5929
5930   return expression_list;
5931 }
5932
5933 /* Parse a delete-expression.
5934
5935    delete-expression:
5936      :: [opt] delete cast-expression
5937      :: [opt] delete [ ] cast-expression
5938
5939    Returns a representation of the expression.  */
5940
5941 static tree
5942 cp_parser_delete_expression (cp_parser* parser)
5943 {
5944   bool global_scope_p;
5945   bool array_p;
5946   tree expression;
5947
5948   /* Look for the optional `::' operator.  */
5949   global_scope_p
5950     = (cp_parser_global_scope_opt (parser,
5951                                    /*current_scope_valid_p=*/false)
5952        != NULL_TREE);
5953   /* Look for the `delete' keyword.  */
5954   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5955   /* See if the array syntax is in use.  */
5956   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5957     {
5958       /* Consume the `[' token.  */
5959       cp_lexer_consume_token (parser->lexer);
5960       /* Look for the `]' token.  */
5961       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5962       /* Remember that this is the `[]' construct.  */
5963       array_p = true;
5964     }
5965   else
5966     array_p = false;
5967
5968   /* Parse the cast-expression.  */
5969   expression = cp_parser_simple_cast_expression (parser);
5970
5971   /* A delete-expression may not appear in an integral constant
5972      expression.  */
5973   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5974     return error_mark_node;
5975
5976   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5977 }
5978
5979 /* Returns true if TOKEN may start a cast-expression and false
5980    otherwise.  */
5981
5982 static bool
5983 cp_parser_token_starts_cast_expression (cp_token *token)
5984 {
5985   switch (token->type)
5986     {
5987     case CPP_COMMA:
5988     case CPP_SEMICOLON:
5989     case CPP_QUERY:
5990     case CPP_COLON:
5991     case CPP_CLOSE_SQUARE:
5992     case CPP_CLOSE_PAREN:
5993     case CPP_CLOSE_BRACE:
5994     case CPP_DOT:
5995     case CPP_DOT_STAR:
5996     case CPP_DEREF:
5997     case CPP_DEREF_STAR:
5998     case CPP_DIV:
5999     case CPP_MOD:
6000     case CPP_LSHIFT:
6001     case CPP_RSHIFT:
6002     case CPP_LESS:
6003     case CPP_GREATER:
6004     case CPP_LESS_EQ:
6005     case CPP_GREATER_EQ:
6006     case CPP_EQ_EQ:
6007     case CPP_NOT_EQ:
6008     case CPP_EQ:
6009     case CPP_MULT_EQ:
6010     case CPP_DIV_EQ:
6011     case CPP_MOD_EQ:
6012     case CPP_PLUS_EQ:
6013     case CPP_MINUS_EQ:
6014     case CPP_RSHIFT_EQ:
6015     case CPP_LSHIFT_EQ:
6016     case CPP_AND_EQ:
6017     case CPP_XOR_EQ:
6018     case CPP_OR_EQ:
6019     case CPP_XOR:
6020     case CPP_OR:
6021     case CPP_OR_OR:
6022     case CPP_EOF:
6023       return false;
6024
6025       /* '[' may start a primary-expression in obj-c++.  */
6026     case CPP_OPEN_SQUARE:
6027       return c_dialect_objc ();
6028
6029     default:
6030       return true;
6031     }
6032 }
6033
6034 /* Parse a cast-expression.
6035
6036    cast-expression:
6037      unary-expression
6038      ( type-id ) cast-expression
6039
6040    ADDRESS_P is true iff the unary-expression is appearing as the
6041    operand of the `&' operator.   CAST_P is true if this expression is
6042    the target of a cast.
6043
6044    Returns a representation of the expression.  */
6045
6046 static tree
6047 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6048                            cp_id_kind * pidk)
6049 {
6050   /* If it's a `(', then we might be looking at a cast.  */
6051   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6052     {
6053       tree type = NULL_TREE;
6054       tree expr = NULL_TREE;
6055       bool compound_literal_p;
6056       const char *saved_message;
6057
6058       /* There's no way to know yet whether or not this is a cast.
6059          For example, `(int (3))' is a unary-expression, while `(int)
6060          3' is a cast.  So, we resort to parsing tentatively.  */
6061       cp_parser_parse_tentatively (parser);
6062       /* Types may not be defined in a cast.  */
6063       saved_message = parser->type_definition_forbidden_message;
6064       parser->type_definition_forbidden_message
6065         = "types may not be defined in casts";
6066       /* Consume the `('.  */
6067       cp_lexer_consume_token (parser->lexer);
6068       /* A very tricky bit is that `(struct S) { 3 }' is a
6069          compound-literal (which we permit in C++ as an extension).
6070          But, that construct is not a cast-expression -- it is a
6071          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6072          is legal; if the compound-literal were a cast-expression,
6073          you'd need an extra set of parentheses.)  But, if we parse
6074          the type-id, and it happens to be a class-specifier, then we
6075          will commit to the parse at that point, because we cannot
6076          undo the action that is done when creating a new class.  So,
6077          then we cannot back up and do a postfix-expression.
6078
6079          Therefore, we scan ahead to the closing `)', and check to see
6080          if the token after the `)' is a `{'.  If so, we are not
6081          looking at a cast-expression.
6082
6083          Save tokens so that we can put them back.  */
6084       cp_lexer_save_tokens (parser->lexer);
6085       /* Skip tokens until the next token is a closing parenthesis.
6086          If we find the closing `)', and the next token is a `{', then
6087          we are looking at a compound-literal.  */
6088       compound_literal_p
6089         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6090                                                   /*consume_paren=*/true)
6091            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6092       /* Roll back the tokens we skipped.  */
6093       cp_lexer_rollback_tokens (parser->lexer);
6094       /* If we were looking at a compound-literal, simulate an error
6095          so that the call to cp_parser_parse_definitely below will
6096          fail.  */
6097       if (compound_literal_p)
6098         cp_parser_simulate_error (parser);
6099       else
6100         {
6101           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6102           parser->in_type_id_in_expr_p = true;
6103           /* Look for the type-id.  */
6104           type = cp_parser_type_id (parser);
6105           /* Look for the closing `)'.  */
6106           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6107           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6108         }
6109
6110       /* Restore the saved message.  */
6111       parser->type_definition_forbidden_message = saved_message;
6112
6113       /* At this point this can only be either a cast or a
6114          parenthesized ctor such as `(T ())' that looks like a cast to
6115          function returning T.  */
6116       if (!cp_parser_error_occurred (parser)
6117           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6118                                                      (parser->lexer)))
6119         {
6120           cp_parser_parse_definitely (parser);
6121           expr = cp_parser_cast_expression (parser,
6122                                             /*address_p=*/false,
6123                                             /*cast_p=*/true, pidk);
6124
6125           /* Warn about old-style casts, if so requested.  */
6126           if (warn_old_style_cast
6127               && !in_system_header
6128               && !VOID_TYPE_P (type)
6129               && current_lang_name != lang_name_c)
6130             warning (OPT_Wold_style_cast, "use of old-style cast");
6131
6132           /* Only type conversions to integral or enumeration types
6133              can be used in constant-expressions.  */
6134           if (!cast_valid_in_integral_constant_expression_p (type)
6135               && (cp_parser_non_integral_constant_expression
6136                   (parser,
6137                    "a cast to a type other than an integral or "
6138                    "enumeration type")))
6139             return error_mark_node;
6140
6141           /* Perform the cast.  */
6142           expr = build_c_cast (type, expr);
6143           return expr;
6144         }
6145       else 
6146         cp_parser_abort_tentative_parse (parser);
6147     }
6148
6149   /* If we get here, then it's not a cast, so it must be a
6150      unary-expression.  */
6151   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6152 }
6153
6154 /* Parse a binary expression of the general form:
6155
6156    pm-expression:
6157      cast-expression
6158      pm-expression .* cast-expression
6159      pm-expression ->* cast-expression
6160
6161    multiplicative-expression:
6162      pm-expression
6163      multiplicative-expression * pm-expression
6164      multiplicative-expression / pm-expression
6165      multiplicative-expression % pm-expression
6166
6167    additive-expression:
6168      multiplicative-expression
6169      additive-expression + multiplicative-expression
6170      additive-expression - multiplicative-expression
6171
6172    shift-expression:
6173      additive-expression
6174      shift-expression << additive-expression
6175      shift-expression >> additive-expression
6176
6177    relational-expression:
6178      shift-expression
6179      relational-expression < shift-expression
6180      relational-expression > shift-expression
6181      relational-expression <= shift-expression
6182      relational-expression >= shift-expression
6183
6184   GNU Extension:
6185
6186    relational-expression:
6187      relational-expression <? shift-expression
6188      relational-expression >? shift-expression
6189
6190    equality-expression:
6191      relational-expression
6192      equality-expression == relational-expression
6193      equality-expression != relational-expression
6194
6195    and-expression:
6196      equality-expression
6197      and-expression & equality-expression
6198
6199    exclusive-or-expression:
6200      and-expression
6201      exclusive-or-expression ^ and-expression
6202
6203    inclusive-or-expression:
6204      exclusive-or-expression
6205      inclusive-or-expression | exclusive-or-expression
6206
6207    logical-and-expression:
6208      inclusive-or-expression
6209      logical-and-expression && inclusive-or-expression
6210
6211    logical-or-expression:
6212      logical-and-expression
6213      logical-or-expression || logical-and-expression
6214
6215    All these are implemented with a single function like:
6216
6217    binary-expression:
6218      simple-cast-expression
6219      binary-expression <token> binary-expression
6220
6221    CAST_P is true if this expression is the target of a cast.
6222
6223    The binops_by_token map is used to get the tree codes for each <token> type.
6224    binary-expressions are associated according to a precedence table.  */
6225
6226 #define TOKEN_PRECEDENCE(token)                              \
6227 (((token->type == CPP_GREATER                                \
6228    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6229   && !parser->greater_than_is_operator_p)                    \
6230  ? PREC_NOT_OPERATOR                                         \
6231  : binops_by_token[token->type].prec)
6232
6233 static tree
6234 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6235                              bool no_toplevel_fold_p,
6236                              enum cp_parser_prec prec,
6237                              cp_id_kind * pidk)
6238 {
6239   cp_parser_expression_stack stack;
6240   cp_parser_expression_stack_entry *sp = &stack[0];
6241   tree lhs, rhs;
6242   cp_token *token;
6243   enum tree_code tree_type, lhs_type, rhs_type;
6244   enum cp_parser_prec new_prec, lookahead_prec;
6245   bool overloaded_p;
6246
6247   /* Parse the first expression.  */
6248   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6249   lhs_type = ERROR_MARK;
6250
6251   for (;;)
6252     {
6253       /* Get an operator token.  */
6254       token = cp_lexer_peek_token (parser->lexer);
6255
6256       if (warn_cxx0x_compat
6257           && token->type == CPP_RSHIFT
6258           && !parser->greater_than_is_operator_p)
6259         {
6260           warning (OPT_Wc__0x_compat, 
6261                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6262                    &token->location);
6263           warning (OPT_Wc__0x_compat, 
6264                    "suggest parentheses around %<>>%> expression");
6265         }
6266
6267       new_prec = TOKEN_PRECEDENCE (token);
6268
6269       /* Popping an entry off the stack means we completed a subexpression:
6270          - either we found a token which is not an operator (`>' where it is not
6271            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6272            will happen repeatedly;
6273          - or, we found an operator which has lower priority.  This is the case
6274            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6275            parsing `3 * 4'.  */
6276       if (new_prec <= prec)
6277         {
6278           if (sp == stack)
6279             break;
6280           else
6281             goto pop;
6282         }
6283
6284      get_rhs:
6285       tree_type = binops_by_token[token->type].tree_type;
6286
6287       /* We used the operator token.  */
6288       cp_lexer_consume_token (parser->lexer);
6289
6290       /* Extract another operand.  It may be the RHS of this expression
6291          or the LHS of a new, higher priority expression.  */
6292       rhs = cp_parser_simple_cast_expression (parser);
6293       rhs_type = ERROR_MARK;
6294
6295       /* Get another operator token.  Look up its precedence to avoid
6296          building a useless (immediately popped) stack entry for common
6297          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6298       token = cp_lexer_peek_token (parser->lexer);
6299       lookahead_prec = TOKEN_PRECEDENCE (token);
6300       if (lookahead_prec > new_prec)
6301         {
6302           /* ... and prepare to parse the RHS of the new, higher priority
6303              expression.  Since precedence levels on the stack are
6304              monotonically increasing, we do not have to care about
6305              stack overflows.  */
6306           sp->prec = prec;
6307           sp->tree_type = tree_type;
6308           sp->lhs = lhs;
6309           sp->lhs_type = lhs_type;
6310           sp++;
6311           lhs = rhs;
6312           lhs_type = rhs_type;
6313           prec = new_prec;
6314           new_prec = lookahead_prec;
6315           goto get_rhs;
6316
6317          pop:
6318           lookahead_prec = new_prec;
6319           /* If the stack is not empty, we have parsed into LHS the right side
6320              (`4' in the example above) of an expression we had suspended.
6321              We can use the information on the stack to recover the LHS (`3')
6322              from the stack together with the tree code (`MULT_EXPR'), and
6323              the precedence of the higher level subexpression
6324              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6325              which will be used to actually build the additive expression.  */
6326           --sp;
6327           prec = sp->prec;
6328           tree_type = sp->tree_type;
6329           rhs = lhs;
6330           rhs_type = lhs_type;
6331           lhs = sp->lhs;
6332           lhs_type = sp->lhs_type;
6333         }
6334
6335       overloaded_p = false;
6336       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6337          ERROR_MARK for everything that is not a binary expression.
6338          This makes warn_about_parentheses miss some warnings that
6339          involve unary operators.  For unary expressions we should
6340          pass the correct tree_code unless the unary expression was
6341          surrounded by parentheses.
6342       */
6343       if (no_toplevel_fold_p
6344           && lookahead_prec <= prec
6345           && sp == stack
6346           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6347         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6348       else
6349         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6350                                  &overloaded_p, tf_warning_or_error);
6351       lhs_type = tree_type;
6352
6353       /* If the binary operator required the use of an overloaded operator,
6354          then this expression cannot be an integral constant-expression.
6355          An overloaded operator can be used even if both operands are
6356          otherwise permissible in an integral constant-expression if at
6357          least one of the operands is of enumeration type.  */
6358
6359       if (overloaded_p
6360           && (cp_parser_non_integral_constant_expression
6361               (parser, "calls to overloaded operators")))
6362         return error_mark_node;
6363     }
6364
6365   return lhs;
6366 }
6367
6368
6369 /* Parse the `? expression : assignment-expression' part of a
6370    conditional-expression.  The LOGICAL_OR_EXPR is the
6371    logical-or-expression that started the conditional-expression.
6372    Returns a representation of the entire conditional-expression.
6373
6374    This routine is used by cp_parser_assignment_expression.
6375
6376      ? expression : assignment-expression
6377
6378    GNU Extensions:
6379
6380      ? : assignment-expression */
6381
6382 static tree
6383 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6384 {
6385   tree expr;
6386   tree assignment_expr;
6387
6388   /* Consume the `?' token.  */
6389   cp_lexer_consume_token (parser->lexer);
6390   if (cp_parser_allow_gnu_extensions_p (parser)
6391       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6392     /* Implicit true clause.  */
6393     expr = NULL_TREE;
6394   else
6395     /* Parse the expression.  */
6396     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6397
6398   /* The next token should be a `:'.  */
6399   cp_parser_require (parser, CPP_COLON, "%<:%>");
6400   /* Parse the assignment-expression.  */
6401   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6402
6403   /* Build the conditional-expression.  */
6404   return build_x_conditional_expr (logical_or_expr,
6405                                    expr,
6406                                    assignment_expr,
6407                                    tf_warning_or_error);
6408 }
6409
6410 /* Parse an assignment-expression.
6411
6412    assignment-expression:
6413      conditional-expression
6414      logical-or-expression assignment-operator assignment_expression
6415      throw-expression
6416
6417    CAST_P is true if this expression is the target of a cast.
6418
6419    Returns a representation for the expression.  */
6420
6421 static tree
6422 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6423                                  cp_id_kind * pidk)
6424 {
6425   tree expr;
6426
6427   /* If the next token is the `throw' keyword, then we're looking at
6428      a throw-expression.  */
6429   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6430     expr = cp_parser_throw_expression (parser);
6431   /* Otherwise, it must be that we are looking at a
6432      logical-or-expression.  */
6433   else
6434     {
6435       /* Parse the binary expressions (logical-or-expression).  */
6436       expr = cp_parser_binary_expression (parser, cast_p, false,
6437                                           PREC_NOT_OPERATOR, pidk);
6438       /* If the next token is a `?' then we're actually looking at a
6439          conditional-expression.  */
6440       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6441         return cp_parser_question_colon_clause (parser, expr);
6442       else
6443         {
6444           enum tree_code assignment_operator;
6445
6446           /* If it's an assignment-operator, we're using the second
6447              production.  */
6448           assignment_operator
6449             = cp_parser_assignment_operator_opt (parser);
6450           if (assignment_operator != ERROR_MARK)
6451             {
6452               bool non_constant_p;
6453
6454               /* Parse the right-hand side of the assignment.  */
6455               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6456
6457               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6458                 maybe_warn_cpp0x ("extended initializer lists");
6459
6460               /* An assignment may not appear in a
6461                  constant-expression.  */
6462               if (cp_parser_non_integral_constant_expression (parser,
6463                                                               "an assignment"))
6464                 return error_mark_node;
6465               /* Build the assignment expression.  */
6466               expr = build_x_modify_expr (expr,
6467                                           assignment_operator,
6468                                           rhs,
6469                                           tf_warning_or_error);
6470             }
6471         }
6472     }
6473
6474   return expr;
6475 }
6476
6477 /* Parse an (optional) assignment-operator.
6478
6479    assignment-operator: one of
6480      = *= /= %= += -= >>= <<= &= ^= |=
6481
6482    GNU Extension:
6483
6484    assignment-operator: one of
6485      <?= >?=
6486
6487    If the next token is an assignment operator, the corresponding tree
6488    code is returned, and the token is consumed.  For example, for
6489    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6490    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6491    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6492    operator, ERROR_MARK is returned.  */
6493
6494 static enum tree_code
6495 cp_parser_assignment_operator_opt (cp_parser* parser)
6496 {
6497   enum tree_code op;
6498   cp_token *token;
6499
6500   /* Peek at the next token.  */
6501   token = cp_lexer_peek_token (parser->lexer);
6502
6503   switch (token->type)
6504     {
6505     case CPP_EQ:
6506       op = NOP_EXPR;
6507       break;
6508
6509     case CPP_MULT_EQ:
6510       op = MULT_EXPR;
6511       break;
6512
6513     case CPP_DIV_EQ:
6514       op = TRUNC_DIV_EXPR;
6515       break;
6516
6517     case CPP_MOD_EQ:
6518       op = TRUNC_MOD_EXPR;
6519       break;
6520
6521     case CPP_PLUS_EQ:
6522       op = PLUS_EXPR;
6523       break;
6524
6525     case CPP_MINUS_EQ:
6526       op = MINUS_EXPR;
6527       break;
6528
6529     case CPP_RSHIFT_EQ:
6530       op = RSHIFT_EXPR;
6531       break;
6532
6533     case CPP_LSHIFT_EQ:
6534       op = LSHIFT_EXPR;
6535       break;
6536
6537     case CPP_AND_EQ:
6538       op = BIT_AND_EXPR;
6539       break;
6540
6541     case CPP_XOR_EQ:
6542       op = BIT_XOR_EXPR;
6543       break;
6544
6545     case CPP_OR_EQ:
6546       op = BIT_IOR_EXPR;
6547       break;
6548
6549     default:
6550       /* Nothing else is an assignment operator.  */
6551       op = ERROR_MARK;
6552     }
6553
6554   /* If it was an assignment operator, consume it.  */
6555   if (op != ERROR_MARK)
6556     cp_lexer_consume_token (parser->lexer);
6557
6558   return op;
6559 }
6560
6561 /* Parse an expression.
6562
6563    expression:
6564      assignment-expression
6565      expression , assignment-expression
6566
6567    CAST_P is true if this expression is the target of a cast.
6568
6569    Returns a representation of the expression.  */
6570
6571 static tree
6572 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6573 {
6574   tree expression = NULL_TREE;
6575
6576   while (true)
6577     {
6578       tree assignment_expression;
6579
6580       /* Parse the next assignment-expression.  */
6581       assignment_expression
6582         = cp_parser_assignment_expression (parser, cast_p, pidk);
6583       /* If this is the first assignment-expression, we can just
6584          save it away.  */
6585       if (!expression)
6586         expression = assignment_expression;
6587       else
6588         expression = build_x_compound_expr (expression,
6589                                             assignment_expression,
6590                                             tf_warning_or_error);
6591       /* If the next token is not a comma, then we are done with the
6592          expression.  */
6593       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6594         break;
6595       /* Consume the `,'.  */
6596       cp_lexer_consume_token (parser->lexer);
6597       /* A comma operator cannot appear in a constant-expression.  */
6598       if (cp_parser_non_integral_constant_expression (parser,
6599                                                       "a comma operator"))
6600         expression = error_mark_node;
6601     }
6602
6603   return expression;
6604 }
6605
6606 /* Parse a constant-expression.
6607
6608    constant-expression:
6609      conditional-expression
6610
6611   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6612   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6613   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6614   is false, NON_CONSTANT_P should be NULL.  */
6615
6616 static tree
6617 cp_parser_constant_expression (cp_parser* parser,
6618                                bool allow_non_constant_p,
6619                                bool *non_constant_p)
6620 {
6621   bool saved_integral_constant_expression_p;
6622   bool saved_allow_non_integral_constant_expression_p;
6623   bool saved_non_integral_constant_expression_p;
6624   tree expression;
6625
6626   /* It might seem that we could simply parse the
6627      conditional-expression, and then check to see if it were
6628      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6629      one that the compiler can figure out is constant, possibly after
6630      doing some simplifications or optimizations.  The standard has a
6631      precise definition of constant-expression, and we must honor
6632      that, even though it is somewhat more restrictive.
6633
6634      For example:
6635
6636        int i[(2, 3)];
6637
6638      is not a legal declaration, because `(2, 3)' is not a
6639      constant-expression.  The `,' operator is forbidden in a
6640      constant-expression.  However, GCC's constant-folding machinery
6641      will fold this operation to an INTEGER_CST for `3'.  */
6642
6643   /* Save the old settings.  */
6644   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6645   saved_allow_non_integral_constant_expression_p
6646     = parser->allow_non_integral_constant_expression_p;
6647   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6648   /* We are now parsing a constant-expression.  */
6649   parser->integral_constant_expression_p = true;
6650   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6651   parser->non_integral_constant_expression_p = false;
6652   /* Although the grammar says "conditional-expression", we parse an
6653      "assignment-expression", which also permits "throw-expression"
6654      and the use of assignment operators.  In the case that
6655      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6656      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6657      actually essential that we look for an assignment-expression.
6658      For example, cp_parser_initializer_clauses uses this function to
6659      determine whether a particular assignment-expression is in fact
6660      constant.  */
6661   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6662   /* Restore the old settings.  */
6663   parser->integral_constant_expression_p
6664     = saved_integral_constant_expression_p;
6665   parser->allow_non_integral_constant_expression_p
6666     = saved_allow_non_integral_constant_expression_p;
6667   if (allow_non_constant_p)
6668     *non_constant_p = parser->non_integral_constant_expression_p;
6669   else if (parser->non_integral_constant_expression_p)
6670     expression = error_mark_node;
6671   parser->non_integral_constant_expression_p
6672     = saved_non_integral_constant_expression_p;
6673
6674   return expression;
6675 }
6676
6677 /* Parse __builtin_offsetof.
6678
6679    offsetof-expression:
6680      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6681
6682    offsetof-member-designator:
6683      id-expression
6684      | offsetof-member-designator "." id-expression
6685      | offsetof-member-designator "[" expression "]"
6686      | offsetof-member-designator "->" id-expression  */
6687
6688 static tree
6689 cp_parser_builtin_offsetof (cp_parser *parser)
6690 {
6691   int save_ice_p, save_non_ice_p;
6692   tree type, expr;
6693   cp_id_kind dummy;
6694   cp_token *token;
6695
6696   /* We're about to accept non-integral-constant things, but will
6697      definitely yield an integral constant expression.  Save and
6698      restore these values around our local parsing.  */
6699   save_ice_p = parser->integral_constant_expression_p;
6700   save_non_ice_p = parser->non_integral_constant_expression_p;
6701
6702   /* Consume the "__builtin_offsetof" token.  */
6703   cp_lexer_consume_token (parser->lexer);
6704   /* Consume the opening `('.  */
6705   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6706   /* Parse the type-id.  */
6707   type = cp_parser_type_id (parser);
6708   /* Look for the `,'.  */
6709   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6710   token = cp_lexer_peek_token (parser->lexer);
6711
6712   /* Build the (type *)null that begins the traditional offsetof macro.  */
6713   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6714                             tf_warning_or_error);
6715
6716   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6717   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6718                                                  true, &dummy, token->location);
6719   while (true)
6720     {
6721       token = cp_lexer_peek_token (parser->lexer);
6722       switch (token->type)
6723         {
6724         case CPP_OPEN_SQUARE:
6725           /* offsetof-member-designator "[" expression "]" */
6726           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6727           break;
6728
6729         case CPP_DEREF:
6730           /* offsetof-member-designator "->" identifier */
6731           expr = grok_array_decl (expr, integer_zero_node);
6732           /* FALLTHRU */
6733
6734         case CPP_DOT:
6735           /* offsetof-member-designator "." identifier */
6736           cp_lexer_consume_token (parser->lexer);
6737           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6738                                                          expr, true, &dummy,
6739                                                          token->location);
6740           break;
6741
6742         case CPP_CLOSE_PAREN:
6743           /* Consume the ")" token.  */
6744           cp_lexer_consume_token (parser->lexer);
6745           goto success;
6746
6747         default:
6748           /* Error.  We know the following require will fail, but
6749              that gives the proper error message.  */
6750           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6751           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6752           expr = error_mark_node;
6753           goto failure;
6754         }
6755     }
6756
6757  success:
6758   /* If we're processing a template, we can't finish the semantics yet.
6759      Otherwise we can fold the entire expression now.  */
6760   if (processing_template_decl)
6761     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6762   else
6763     expr = finish_offsetof (expr);
6764
6765  failure:
6766   parser->integral_constant_expression_p = save_ice_p;
6767   parser->non_integral_constant_expression_p = save_non_ice_p;
6768
6769   return expr;
6770 }
6771
6772 /* Parse a trait expression.  */
6773
6774 static tree
6775 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6776 {
6777   cp_trait_kind kind;
6778   tree type1, type2 = NULL_TREE;
6779   bool binary = false;
6780   cp_decl_specifier_seq decl_specs;
6781
6782   switch (keyword)
6783     {
6784     case RID_HAS_NOTHROW_ASSIGN:
6785       kind = CPTK_HAS_NOTHROW_ASSIGN;
6786       break;
6787     case RID_HAS_NOTHROW_CONSTRUCTOR:
6788       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6789       break;
6790     case RID_HAS_NOTHROW_COPY:
6791       kind = CPTK_HAS_NOTHROW_COPY;
6792       break;
6793     case RID_HAS_TRIVIAL_ASSIGN:
6794       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6795       break;
6796     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6797       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6798       break;
6799     case RID_HAS_TRIVIAL_COPY:
6800       kind = CPTK_HAS_TRIVIAL_COPY;
6801       break;
6802     case RID_HAS_TRIVIAL_DESTRUCTOR:
6803       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6804       break;
6805     case RID_HAS_VIRTUAL_DESTRUCTOR:
6806       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6807       break;
6808     case RID_IS_ABSTRACT:
6809       kind = CPTK_IS_ABSTRACT;
6810       break;
6811     case RID_IS_BASE_OF:
6812       kind = CPTK_IS_BASE_OF;
6813       binary = true;
6814       break;
6815     case RID_IS_CLASS:
6816       kind = CPTK_IS_CLASS;
6817       break;
6818     case RID_IS_CONVERTIBLE_TO:
6819       kind = CPTK_IS_CONVERTIBLE_TO;
6820       binary = true;
6821       break;
6822     case RID_IS_EMPTY:
6823       kind = CPTK_IS_EMPTY;
6824       break;
6825     case RID_IS_ENUM:
6826       kind = CPTK_IS_ENUM;
6827       break;
6828     case RID_IS_POD:
6829       kind = CPTK_IS_POD;
6830       break;
6831     case RID_IS_POLYMORPHIC:
6832       kind = CPTK_IS_POLYMORPHIC;
6833       break;
6834     case RID_IS_UNION:
6835       kind = CPTK_IS_UNION;
6836       break;
6837     default:
6838       gcc_unreachable ();
6839     }
6840
6841   /* Consume the token.  */
6842   cp_lexer_consume_token (parser->lexer);
6843
6844   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6845
6846   type1 = cp_parser_type_id (parser);
6847
6848   if (type1 == error_mark_node)
6849     return error_mark_node;
6850
6851   /* Build a trivial decl-specifier-seq.  */
6852   clear_decl_specs (&decl_specs);
6853   decl_specs.type = type1;
6854
6855   /* Call grokdeclarator to figure out what type this is.  */
6856   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6857                           /*initialized=*/0, /*attrlist=*/NULL);
6858
6859   if (binary)
6860     {
6861       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6862  
6863       type2 = cp_parser_type_id (parser);
6864
6865       if (type2 == error_mark_node)
6866         return error_mark_node;
6867
6868       /* Build a trivial decl-specifier-seq.  */
6869       clear_decl_specs (&decl_specs);
6870       decl_specs.type = type2;
6871
6872       /* Call grokdeclarator to figure out what type this is.  */
6873       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6874                               /*initialized=*/0, /*attrlist=*/NULL);
6875     }
6876
6877   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6878
6879   /* Complete the trait expression, which may mean either processing
6880      the trait expr now or saving it for template instantiation.  */
6881   return finish_trait_expr (kind, type1, type2);
6882 }
6883
6884 /* Statements [gram.stmt.stmt]  */
6885
6886 /* Parse a statement.
6887
6888    statement:
6889      labeled-statement
6890      expression-statement
6891      compound-statement
6892      selection-statement
6893      iteration-statement
6894      jump-statement
6895      declaration-statement
6896      try-block
6897
6898   IN_COMPOUND is true when the statement is nested inside a
6899   cp_parser_compound_statement; this matters for certain pragmas.
6900
6901   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6902   is a (possibly labeled) if statement which is not enclosed in braces
6903   and has an else clause.  This is used to implement -Wparentheses.  */
6904
6905 static void
6906 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6907                      bool in_compound, bool *if_p)
6908 {
6909   tree statement;
6910   cp_token *token;
6911   location_t statement_location;
6912
6913  restart:
6914   if (if_p != NULL)
6915     *if_p = false;
6916   /* There is no statement yet.  */
6917   statement = NULL_TREE;
6918   /* Peek at the next token.  */
6919   token = cp_lexer_peek_token (parser->lexer);
6920   /* Remember the location of the first token in the statement.  */
6921   statement_location = token->location;
6922   /* If this is a keyword, then that will often determine what kind of
6923      statement we have.  */
6924   if (token->type == CPP_KEYWORD)
6925     {
6926       enum rid keyword = token->keyword;
6927
6928       switch (keyword)
6929         {
6930         case RID_CASE:
6931         case RID_DEFAULT:
6932           /* Looks like a labeled-statement with a case label.
6933              Parse the label, and then use tail recursion to parse
6934              the statement.  */
6935           cp_parser_label_for_labeled_statement (parser);
6936           goto restart;
6937
6938         case RID_IF:
6939         case RID_SWITCH:
6940           statement = cp_parser_selection_statement (parser, if_p);
6941           break;
6942
6943         case RID_WHILE:
6944         case RID_DO:
6945         case RID_FOR:
6946           statement = cp_parser_iteration_statement (parser);
6947           break;
6948
6949         case RID_BREAK:
6950         case RID_CONTINUE:
6951         case RID_RETURN:
6952         case RID_GOTO:
6953           statement = cp_parser_jump_statement (parser);
6954           break;
6955
6956           /* Objective-C++ exception-handling constructs.  */
6957         case RID_AT_TRY:
6958         case RID_AT_CATCH:
6959         case RID_AT_FINALLY:
6960         case RID_AT_SYNCHRONIZED:
6961         case RID_AT_THROW:
6962           statement = cp_parser_objc_statement (parser);
6963           break;
6964
6965         case RID_TRY:
6966           statement = cp_parser_try_block (parser);
6967           break;
6968
6969         case RID_NAMESPACE:
6970           /* This must be a namespace alias definition.  */
6971           cp_parser_declaration_statement (parser);
6972           return;
6973           
6974         default:
6975           /* It might be a keyword like `int' that can start a
6976              declaration-statement.  */
6977           break;
6978         }
6979     }
6980   else if (token->type == CPP_NAME)
6981     {
6982       /* If the next token is a `:', then we are looking at a
6983          labeled-statement.  */
6984       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6985       if (token->type == CPP_COLON)
6986         {
6987           /* Looks like a labeled-statement with an ordinary label.
6988              Parse the label, and then use tail recursion to parse
6989              the statement.  */
6990           cp_parser_label_for_labeled_statement (parser);
6991           goto restart;
6992         }
6993     }
6994   /* Anything that starts with a `{' must be a compound-statement.  */
6995   else if (token->type == CPP_OPEN_BRACE)
6996     statement = cp_parser_compound_statement (parser, NULL, false);
6997   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6998      a statement all its own.  */
6999   else if (token->type == CPP_PRAGMA)
7000     {
7001       /* Only certain OpenMP pragmas are attached to statements, and thus
7002          are considered statements themselves.  All others are not.  In
7003          the context of a compound, accept the pragma as a "statement" and
7004          return so that we can check for a close brace.  Otherwise we
7005          require a real statement and must go back and read one.  */
7006       if (in_compound)
7007         cp_parser_pragma (parser, pragma_compound);
7008       else if (!cp_parser_pragma (parser, pragma_stmt))
7009         goto restart;
7010       return;
7011     }
7012   else if (token->type == CPP_EOF)
7013     {
7014       cp_parser_error (parser, "expected statement");
7015       return;
7016     }
7017
7018   /* Everything else must be a declaration-statement or an
7019      expression-statement.  Try for the declaration-statement
7020      first, unless we are looking at a `;', in which case we know that
7021      we have an expression-statement.  */
7022   if (!statement)
7023     {
7024       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7025         {
7026           cp_parser_parse_tentatively (parser);
7027           /* Try to parse the declaration-statement.  */
7028           cp_parser_declaration_statement (parser);
7029           /* If that worked, we're done.  */
7030           if (cp_parser_parse_definitely (parser))
7031             return;
7032         }
7033       /* Look for an expression-statement instead.  */
7034       statement = cp_parser_expression_statement (parser, in_statement_expr);
7035     }
7036
7037   /* Set the line number for the statement.  */
7038   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7039     SET_EXPR_LOCATION (statement, statement_location);
7040 }
7041
7042 /* Parse the label for a labeled-statement, i.e.
7043
7044    identifier :
7045    case constant-expression :
7046    default :
7047
7048    GNU Extension:
7049    case constant-expression ... constant-expression : statement
7050
7051    When a label is parsed without errors, the label is added to the
7052    parse tree by the finish_* functions, so this function doesn't
7053    have to return the label.  */
7054
7055 static void
7056 cp_parser_label_for_labeled_statement (cp_parser* parser)
7057 {
7058   cp_token *token;
7059
7060   /* The next token should be an identifier.  */
7061   token = cp_lexer_peek_token (parser->lexer);
7062   if (token->type != CPP_NAME
7063       && token->type != CPP_KEYWORD)
7064     {
7065       cp_parser_error (parser, "expected labeled-statement");
7066       return;
7067     }
7068
7069   switch (token->keyword)
7070     {
7071     case RID_CASE:
7072       {
7073         tree expr, expr_hi;
7074         cp_token *ellipsis;
7075
7076         /* Consume the `case' token.  */
7077         cp_lexer_consume_token (parser->lexer);
7078         /* Parse the constant-expression.  */
7079         expr = cp_parser_constant_expression (parser,
7080                                               /*allow_non_constant_p=*/false,
7081                                               NULL);
7082
7083         ellipsis = cp_lexer_peek_token (parser->lexer);
7084         if (ellipsis->type == CPP_ELLIPSIS)
7085           {
7086             /* Consume the `...' token.  */
7087             cp_lexer_consume_token (parser->lexer);
7088             expr_hi =
7089               cp_parser_constant_expression (parser,
7090                                              /*allow_non_constant_p=*/false,
7091                                              NULL);
7092             /* We don't need to emit warnings here, as the common code
7093                will do this for us.  */
7094           }
7095         else
7096           expr_hi = NULL_TREE;
7097
7098         if (parser->in_switch_statement_p)
7099           finish_case_label (expr, expr_hi);
7100         else
7101           error ("%Hcase label %qE not within a switch statement",
7102                  &token->location, expr);
7103       }
7104       break;
7105
7106     case RID_DEFAULT:
7107       /* Consume the `default' token.  */
7108       cp_lexer_consume_token (parser->lexer);
7109
7110       if (parser->in_switch_statement_p)
7111         finish_case_label (NULL_TREE, NULL_TREE);
7112       else
7113         error ("%Hcase label not within a switch statement", &token->location);
7114       break;
7115
7116     default:
7117       /* Anything else must be an ordinary label.  */
7118       finish_label_stmt (cp_parser_identifier (parser));
7119       break;
7120     }
7121
7122   /* Require the `:' token.  */
7123   cp_parser_require (parser, CPP_COLON, "%<:%>");
7124 }
7125
7126 /* Parse an expression-statement.
7127
7128    expression-statement:
7129      expression [opt] ;
7130
7131    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7132    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7133    indicates whether this expression-statement is part of an
7134    expression statement.  */
7135
7136 static tree
7137 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7138 {
7139   tree statement = NULL_TREE;
7140
7141   /* If the next token is a ';', then there is no expression
7142      statement.  */
7143   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7144     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7145
7146   /* Consume the final `;'.  */
7147   cp_parser_consume_semicolon_at_end_of_statement (parser);
7148
7149   if (in_statement_expr
7150       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7151     /* This is the final expression statement of a statement
7152        expression.  */
7153     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7154   else if (statement)
7155     statement = finish_expr_stmt (statement);
7156   else
7157     finish_stmt ();
7158
7159   return statement;
7160 }
7161
7162 /* Parse a compound-statement.
7163
7164    compound-statement:
7165      { statement-seq [opt] }
7166
7167    GNU extension:
7168
7169    compound-statement:
7170      { label-declaration-seq [opt] statement-seq [opt] }
7171
7172    label-declaration-seq:
7173      label-declaration
7174      label-declaration-seq label-declaration
7175
7176    Returns a tree representing the statement.  */
7177
7178 static tree
7179 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7180                               bool in_try)
7181 {
7182   tree compound_stmt;
7183
7184   /* Consume the `{'.  */
7185   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7186     return error_mark_node;
7187   /* Begin the compound-statement.  */
7188   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7189   /* If the next keyword is `__label__' we have a label declaration.  */
7190   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7191     cp_parser_label_declaration (parser);
7192   /* Parse an (optional) statement-seq.  */
7193   cp_parser_statement_seq_opt (parser, in_statement_expr);
7194   /* Finish the compound-statement.  */
7195   finish_compound_stmt (compound_stmt);
7196   /* Consume the `}'.  */
7197   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7198
7199   return compound_stmt;
7200 }
7201
7202 /* Parse an (optional) statement-seq.
7203
7204    statement-seq:
7205      statement
7206      statement-seq [opt] statement  */
7207
7208 static void
7209 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7210 {
7211   /* Scan statements until there aren't any more.  */
7212   while (true)
7213     {
7214       cp_token *token = cp_lexer_peek_token (parser->lexer);
7215
7216       /* If we're looking at a `}', then we've run out of statements.  */
7217       if (token->type == CPP_CLOSE_BRACE
7218           || token->type == CPP_EOF
7219           || token->type == CPP_PRAGMA_EOL)
7220         break;
7221       
7222       /* If we are in a compound statement and find 'else' then
7223          something went wrong.  */
7224       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7225         {
7226           if (parser->in_statement & IN_IF_STMT) 
7227             break;
7228           else
7229             {
7230               token = cp_lexer_consume_token (parser->lexer);
7231               error ("%H%<else%> without a previous %<if%>", &token->location);
7232             }
7233         }
7234
7235       /* Parse the statement.  */
7236       cp_parser_statement (parser, in_statement_expr, true, NULL);
7237     }
7238 }
7239
7240 /* Parse a selection-statement.
7241
7242    selection-statement:
7243      if ( condition ) statement
7244      if ( condition ) statement else statement
7245      switch ( condition ) statement
7246
7247    Returns the new IF_STMT or SWITCH_STMT.
7248
7249    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7250    is a (possibly labeled) if statement which is not enclosed in
7251    braces and has an else clause.  This is used to implement
7252    -Wparentheses.  */
7253
7254 static tree
7255 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7256 {
7257   cp_token *token;
7258   enum rid keyword;
7259
7260   if (if_p != NULL)
7261     *if_p = false;
7262
7263   /* Peek at the next token.  */
7264   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7265
7266   /* See what kind of keyword it is.  */
7267   keyword = token->keyword;
7268   switch (keyword)
7269     {
7270     case RID_IF:
7271     case RID_SWITCH:
7272       {
7273         tree statement;
7274         tree condition;
7275
7276         /* Look for the `('.  */
7277         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7278           {
7279             cp_parser_skip_to_end_of_statement (parser);
7280             return error_mark_node;
7281           }
7282
7283         /* Begin the selection-statement.  */
7284         if (keyword == RID_IF)
7285           statement = begin_if_stmt ();
7286         else
7287           statement = begin_switch_stmt ();
7288
7289         /* Parse the condition.  */
7290         condition = cp_parser_condition (parser);
7291         /* Look for the `)'.  */
7292         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7293           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7294                                                  /*consume_paren=*/true);
7295
7296         if (keyword == RID_IF)
7297           {
7298             bool nested_if;
7299             unsigned char in_statement;
7300
7301             /* Add the condition.  */
7302             finish_if_stmt_cond (condition, statement);
7303
7304             /* Parse the then-clause.  */
7305             in_statement = parser->in_statement;
7306             parser->in_statement |= IN_IF_STMT;
7307             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7308               {
7309                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7310                 add_stmt (build_empty_stmt ());
7311                 cp_lexer_consume_token (parser->lexer);
7312                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7313                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7314                               "empty body in an %<if%> statement");
7315                 nested_if = false;
7316               }
7317             else
7318               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7319             parser->in_statement = in_statement;
7320
7321             finish_then_clause (statement);
7322
7323             /* If the next token is `else', parse the else-clause.  */
7324             if (cp_lexer_next_token_is_keyword (parser->lexer,
7325                                                 RID_ELSE))
7326               {
7327                 /* Consume the `else' keyword.  */
7328                 cp_lexer_consume_token (parser->lexer);
7329                 begin_else_clause (statement);
7330                 /* Parse the else-clause.  */
7331                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7332                   {
7333                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7334                                 OPT_Wempty_body, "suggest braces around "
7335                                 "empty body in an %<else%> statement");
7336                     add_stmt (build_empty_stmt ());
7337                     cp_lexer_consume_token (parser->lexer);
7338                   }
7339                 else
7340                   cp_parser_implicitly_scoped_statement (parser, NULL);
7341
7342                 finish_else_clause (statement);
7343
7344                 /* If we are currently parsing a then-clause, then
7345                    IF_P will not be NULL.  We set it to true to
7346                    indicate that this if statement has an else clause.
7347                    This may trigger the Wparentheses warning below
7348                    when we get back up to the parent if statement.  */
7349                 if (if_p != NULL)
7350                   *if_p = true;
7351               }
7352             else
7353               {
7354                 /* This if statement does not have an else clause.  If
7355                    NESTED_IF is true, then the then-clause is an if
7356                    statement which does have an else clause.  We warn
7357                    about the potential ambiguity.  */
7358                 if (nested_if)
7359                   warning (OPT_Wparentheses,
7360                            ("%Hsuggest explicit braces "
7361                             "to avoid ambiguous %<else%>"),
7362                            EXPR_LOCUS (statement));
7363               }
7364
7365             /* Now we're all done with the if-statement.  */
7366             finish_if_stmt (statement);
7367           }
7368         else
7369           {
7370             bool in_switch_statement_p;
7371             unsigned char in_statement;
7372
7373             /* Add the condition.  */
7374             finish_switch_cond (condition, statement);
7375
7376             /* Parse the body of the switch-statement.  */
7377             in_switch_statement_p = parser->in_switch_statement_p;
7378             in_statement = parser->in_statement;
7379             parser->in_switch_statement_p = true;
7380             parser->in_statement |= IN_SWITCH_STMT;
7381             cp_parser_implicitly_scoped_statement (parser, NULL);
7382             parser->in_switch_statement_p = in_switch_statement_p;
7383             parser->in_statement = in_statement;
7384
7385             /* Now we're all done with the switch-statement.  */
7386             finish_switch_stmt (statement);
7387           }
7388
7389         return statement;
7390       }
7391       break;
7392
7393     default:
7394       cp_parser_error (parser, "expected selection-statement");
7395       return error_mark_node;
7396     }
7397 }
7398
7399 /* Parse a condition.
7400
7401    condition:
7402      expression
7403      type-specifier-seq declarator = initializer-clause
7404      type-specifier-seq declarator braced-init-list
7405
7406    GNU Extension:
7407
7408    condition:
7409      type-specifier-seq declarator asm-specification [opt]
7410        attributes [opt] = assignment-expression
7411
7412    Returns the expression that should be tested.  */
7413
7414 static tree
7415 cp_parser_condition (cp_parser* parser)
7416 {
7417   cp_decl_specifier_seq type_specifiers;
7418   const char *saved_message;
7419
7420   /* Try the declaration first.  */
7421   cp_parser_parse_tentatively (parser);
7422   /* New types are not allowed in the type-specifier-seq for a
7423      condition.  */
7424   saved_message = parser->type_definition_forbidden_message;
7425   parser->type_definition_forbidden_message
7426     = "types may not be defined in conditions";
7427   /* Parse the type-specifier-seq.  */
7428   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7429                                 /*is_trailing_return=*/false,
7430                                 &type_specifiers);
7431   /* Restore the saved message.  */
7432   parser->type_definition_forbidden_message = saved_message;
7433   /* If all is well, we might be looking at a declaration.  */
7434   if (!cp_parser_error_occurred (parser))
7435     {
7436       tree decl;
7437       tree asm_specification;
7438       tree attributes;
7439       cp_declarator *declarator;
7440       tree initializer = NULL_TREE;
7441
7442       /* Parse the declarator.  */
7443       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7444                                          /*ctor_dtor_or_conv_p=*/NULL,
7445                                          /*parenthesized_p=*/NULL,
7446                                          /*member_p=*/false);
7447       /* Parse the attributes.  */
7448       attributes = cp_parser_attributes_opt (parser);
7449       /* Parse the asm-specification.  */
7450       asm_specification = cp_parser_asm_specification_opt (parser);
7451       /* If the next token is not an `=' or '{', then we might still be
7452          looking at an expression.  For example:
7453
7454            if (A(a).x)
7455
7456          looks like a decl-specifier-seq and a declarator -- but then
7457          there is no `=', so this is an expression.  */
7458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7459           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7460         cp_parser_simulate_error (parser);
7461         
7462       /* If we did see an `=' or '{', then we are looking at a declaration
7463          for sure.  */
7464       if (cp_parser_parse_definitely (parser))
7465         {
7466           tree pushed_scope;
7467           bool non_constant_p;
7468           bool flags = LOOKUP_ONLYCONVERTING;
7469
7470           /* Create the declaration.  */
7471           decl = start_decl (declarator, &type_specifiers,
7472                              /*initialized_p=*/true,
7473                              attributes, /*prefix_attributes=*/NULL_TREE,
7474                              &pushed_scope);
7475
7476           /* Parse the initializer.  */
7477           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7478             {
7479               initializer = cp_parser_braced_list (parser, &non_constant_p);
7480               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7481               flags = 0;
7482             }
7483           else
7484             {
7485               /* Consume the `='.  */
7486               cp_parser_require (parser, CPP_EQ, "%<=%>");
7487               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7488             }
7489           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7490             maybe_warn_cpp0x ("extended initializer lists");
7491
7492           if (!non_constant_p)
7493             initializer = fold_non_dependent_expr (initializer);
7494
7495           /* Process the initializer.  */
7496           cp_finish_decl (decl,
7497                           initializer, !non_constant_p,
7498                           asm_specification,
7499                           flags);
7500
7501           if (pushed_scope)
7502             pop_scope (pushed_scope);
7503
7504           return convert_from_reference (decl);
7505         }
7506     }
7507   /* If we didn't even get past the declarator successfully, we are
7508      definitely not looking at a declaration.  */
7509   else
7510     cp_parser_abort_tentative_parse (parser);
7511
7512   /* Otherwise, we are looking at an expression.  */
7513   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7514 }
7515
7516 /* Parse an iteration-statement.
7517
7518    iteration-statement:
7519      while ( condition ) statement
7520      do statement while ( expression ) ;
7521      for ( for-init-statement condition [opt] ; expression [opt] )
7522        statement
7523
7524    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7525
7526 static tree
7527 cp_parser_iteration_statement (cp_parser* parser)
7528 {
7529   cp_token *token;
7530   enum rid keyword;
7531   tree statement;
7532   unsigned char in_statement;
7533
7534   /* Peek at the next token.  */
7535   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7536   if (!token)
7537     return error_mark_node;
7538
7539   /* Remember whether or not we are already within an iteration
7540      statement.  */
7541   in_statement = parser->in_statement;
7542
7543   /* See what kind of keyword it is.  */
7544   keyword = token->keyword;
7545   switch (keyword)
7546     {
7547     case RID_WHILE:
7548       {
7549         tree condition;
7550
7551         /* Begin the while-statement.  */
7552         statement = begin_while_stmt ();
7553         /* Look for the `('.  */
7554         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7555         /* Parse the condition.  */
7556         condition = cp_parser_condition (parser);
7557         finish_while_stmt_cond (condition, statement);
7558         /* Look for the `)'.  */
7559         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7560         /* Parse the dependent statement.  */
7561         parser->in_statement = IN_ITERATION_STMT;
7562         cp_parser_already_scoped_statement (parser);
7563         parser->in_statement = in_statement;
7564         /* We're done with the while-statement.  */
7565         finish_while_stmt (statement);
7566       }
7567       break;
7568
7569     case RID_DO:
7570       {
7571         tree expression;
7572
7573         /* Begin the do-statement.  */
7574         statement = begin_do_stmt ();
7575         /* Parse the body of the do-statement.  */
7576         parser->in_statement = IN_ITERATION_STMT;
7577         cp_parser_implicitly_scoped_statement (parser, NULL);
7578         parser->in_statement = in_statement;
7579         finish_do_body (statement);
7580         /* Look for the `while' keyword.  */
7581         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7582         /* Look for the `('.  */
7583         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7584         /* Parse the expression.  */
7585         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7586         /* We're done with the do-statement.  */
7587         finish_do_stmt (expression, statement);
7588         /* Look for the `)'.  */
7589         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7590         /* Look for the `;'.  */
7591         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7592       }
7593       break;
7594
7595     case RID_FOR:
7596       {
7597         tree condition = NULL_TREE;
7598         tree expression = NULL_TREE;
7599
7600         /* Begin the for-statement.  */
7601         statement = begin_for_stmt ();
7602         /* Look for the `('.  */
7603         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7604         /* Parse the initialization.  */
7605         cp_parser_for_init_statement (parser);
7606         finish_for_init_stmt (statement);
7607
7608         /* If there's a condition, process it.  */
7609         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7610           condition = cp_parser_condition (parser);
7611         finish_for_cond (condition, statement);
7612         /* Look for the `;'.  */
7613         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7614
7615         /* If there's an expression, process it.  */
7616         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7617           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7618         finish_for_expr (expression, statement);
7619         /* Look for the `)'.  */
7620         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7621
7622         /* Parse the body of the for-statement.  */
7623         parser->in_statement = IN_ITERATION_STMT;
7624         cp_parser_already_scoped_statement (parser);
7625         parser->in_statement = in_statement;
7626
7627         /* We're done with the for-statement.  */
7628         finish_for_stmt (statement);
7629       }
7630       break;
7631
7632     default:
7633       cp_parser_error (parser, "expected iteration-statement");
7634       statement = error_mark_node;
7635       break;
7636     }
7637
7638   return statement;
7639 }
7640
7641 /* Parse a for-init-statement.
7642
7643    for-init-statement:
7644      expression-statement
7645      simple-declaration  */
7646
7647 static void
7648 cp_parser_for_init_statement (cp_parser* parser)
7649 {
7650   /* If the next token is a `;', then we have an empty
7651      expression-statement.  Grammatically, this is also a
7652      simple-declaration, but an invalid one, because it does not
7653      declare anything.  Therefore, if we did not handle this case
7654      specially, we would issue an error message about an invalid
7655      declaration.  */
7656   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7657     {
7658       /* We're going to speculatively look for a declaration, falling back
7659          to an expression, if necessary.  */
7660       cp_parser_parse_tentatively (parser);
7661       /* Parse the declaration.  */
7662       cp_parser_simple_declaration (parser,
7663                                     /*function_definition_allowed_p=*/false);
7664       /* If the tentative parse failed, then we shall need to look for an
7665          expression-statement.  */
7666       if (cp_parser_parse_definitely (parser))
7667         return;
7668     }
7669
7670   cp_parser_expression_statement (parser, false);
7671 }
7672
7673 /* Parse a jump-statement.
7674
7675    jump-statement:
7676      break ;
7677      continue ;
7678      return expression [opt] ;
7679      return braced-init-list ;
7680      goto identifier ;
7681
7682    GNU extension:
7683
7684    jump-statement:
7685      goto * expression ;
7686
7687    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7688
7689 static tree
7690 cp_parser_jump_statement (cp_parser* parser)
7691 {
7692   tree statement = error_mark_node;
7693   cp_token *token;
7694   enum rid keyword;
7695   unsigned char in_statement;
7696
7697   /* Peek at the next token.  */
7698   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7699   if (!token)
7700     return error_mark_node;
7701
7702   /* See what kind of keyword it is.  */
7703   keyword = token->keyword;
7704   switch (keyword)
7705     {
7706     case RID_BREAK:
7707       in_statement = parser->in_statement & ~IN_IF_STMT;      
7708       switch (in_statement)
7709         {
7710         case 0:
7711           error ("%Hbreak statement not within loop or switch", &token->location);
7712           break;
7713         default:
7714           gcc_assert ((in_statement & IN_SWITCH_STMT)
7715                       || in_statement == IN_ITERATION_STMT);
7716           statement = finish_break_stmt ();
7717           break;
7718         case IN_OMP_BLOCK:
7719           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7720           break;
7721         case IN_OMP_FOR:
7722           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7723           break;
7724         }
7725       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7726       break;
7727
7728     case RID_CONTINUE:
7729       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7730         {
7731         case 0:
7732           error ("%Hcontinue statement not within a loop", &token->location);
7733           break;
7734         case IN_ITERATION_STMT:
7735         case IN_OMP_FOR:
7736           statement = finish_continue_stmt ();
7737           break;
7738         case IN_OMP_BLOCK:
7739           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7740           break;
7741         default:
7742           gcc_unreachable ();
7743         }
7744       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7745       break;
7746
7747     case RID_RETURN:
7748       {
7749         tree expr;
7750         bool expr_non_constant_p;
7751
7752         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7753           {
7754             maybe_warn_cpp0x ("extended initializer lists");
7755             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7756           }
7757         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7758           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7759         else
7760           /* If the next token is a `;', then there is no
7761              expression.  */
7762           expr = NULL_TREE;
7763         /* Build the return-statement.  */
7764         statement = finish_return_stmt (expr);
7765         /* Look for the final `;'.  */
7766         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7767       }
7768       break;
7769
7770     case RID_GOTO:
7771       /* Create the goto-statement.  */
7772       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7773         {
7774           /* Issue a warning about this use of a GNU extension.  */
7775           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7776           /* Consume the '*' token.  */
7777           cp_lexer_consume_token (parser->lexer);
7778           /* Parse the dependent expression.  */
7779           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7780         }
7781       else
7782         finish_goto_stmt (cp_parser_identifier (parser));
7783       /* Look for the final `;'.  */
7784       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7785       break;
7786
7787     default:
7788       cp_parser_error (parser, "expected jump-statement");
7789       break;
7790     }
7791
7792   return statement;
7793 }
7794
7795 /* Parse a declaration-statement.
7796
7797    declaration-statement:
7798      block-declaration  */
7799
7800 static void
7801 cp_parser_declaration_statement (cp_parser* parser)
7802 {
7803   void *p;
7804
7805   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7806   p = obstack_alloc (&declarator_obstack, 0);
7807
7808  /* Parse the block-declaration.  */
7809   cp_parser_block_declaration (parser, /*statement_p=*/true);
7810
7811   /* Free any declarators allocated.  */
7812   obstack_free (&declarator_obstack, p);
7813
7814   /* Finish off the statement.  */
7815   finish_stmt ();
7816 }
7817
7818 /* Some dependent statements (like `if (cond) statement'), are
7819    implicitly in their own scope.  In other words, if the statement is
7820    a single statement (as opposed to a compound-statement), it is
7821    none-the-less treated as if it were enclosed in braces.  Any
7822    declarations appearing in the dependent statement are out of scope
7823    after control passes that point.  This function parses a statement,
7824    but ensures that is in its own scope, even if it is not a
7825    compound-statement.
7826
7827    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7828    is a (possibly labeled) if statement which is not enclosed in
7829    braces and has an else clause.  This is used to implement
7830    -Wparentheses.
7831
7832    Returns the new statement.  */
7833
7834 static tree
7835 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7836 {
7837   tree statement;
7838
7839   if (if_p != NULL)
7840     *if_p = false;
7841
7842   /* Mark if () ; with a special NOP_EXPR.  */
7843   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7844     {
7845       cp_lexer_consume_token (parser->lexer);
7846       statement = add_stmt (build_empty_stmt ());
7847     }
7848   /* if a compound is opened, we simply parse the statement directly.  */
7849   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7850     statement = cp_parser_compound_statement (parser, NULL, false);
7851   /* If the token is not a `{', then we must take special action.  */
7852   else
7853     {
7854       /* Create a compound-statement.  */
7855       statement = begin_compound_stmt (0);
7856       /* Parse the dependent-statement.  */
7857       cp_parser_statement (parser, NULL_TREE, false, if_p);
7858       /* Finish the dummy compound-statement.  */
7859       finish_compound_stmt (statement);
7860     }
7861
7862   /* Return the statement.  */
7863   return statement;
7864 }
7865
7866 /* For some dependent statements (like `while (cond) statement'), we
7867    have already created a scope.  Therefore, even if the dependent
7868    statement is a compound-statement, we do not want to create another
7869    scope.  */
7870
7871 static void
7872 cp_parser_already_scoped_statement (cp_parser* parser)
7873 {
7874   /* If the token is a `{', then we must take special action.  */
7875   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7876     cp_parser_statement (parser, NULL_TREE, false, NULL);
7877   else
7878     {
7879       /* Avoid calling cp_parser_compound_statement, so that we
7880          don't create a new scope.  Do everything else by hand.  */
7881       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7882       /* If the next keyword is `__label__' we have a label declaration.  */
7883       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7884         cp_parser_label_declaration (parser);
7885       /* Parse an (optional) statement-seq.  */
7886       cp_parser_statement_seq_opt (parser, NULL_TREE);
7887       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7888     }
7889 }
7890
7891 /* Declarations [gram.dcl.dcl] */
7892
7893 /* Parse an optional declaration-sequence.
7894
7895    declaration-seq:
7896      declaration
7897      declaration-seq declaration  */
7898
7899 static void
7900 cp_parser_declaration_seq_opt (cp_parser* parser)
7901 {
7902   while (true)
7903     {
7904       cp_token *token;
7905
7906       token = cp_lexer_peek_token (parser->lexer);
7907
7908       if (token->type == CPP_CLOSE_BRACE
7909           || token->type == CPP_EOF
7910           || token->type == CPP_PRAGMA_EOL)
7911         break;
7912
7913       if (token->type == CPP_SEMICOLON)
7914         {
7915           /* A declaration consisting of a single semicolon is
7916              invalid.  Allow it unless we're being pedantic.  */
7917           cp_lexer_consume_token (parser->lexer);
7918           if (!in_system_header)
7919             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7920           continue;
7921         }
7922
7923       /* If we're entering or exiting a region that's implicitly
7924          extern "C", modify the lang context appropriately.  */
7925       if (!parser->implicit_extern_c && token->implicit_extern_c)
7926         {
7927           push_lang_context (lang_name_c);
7928           parser->implicit_extern_c = true;
7929         }
7930       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7931         {
7932           pop_lang_context ();
7933           parser->implicit_extern_c = false;
7934         }
7935
7936       if (token->type == CPP_PRAGMA)
7937         {
7938           /* A top-level declaration can consist solely of a #pragma.
7939              A nested declaration cannot, so this is done here and not
7940              in cp_parser_declaration.  (A #pragma at block scope is
7941              handled in cp_parser_statement.)  */
7942           cp_parser_pragma (parser, pragma_external);
7943           continue;
7944         }
7945
7946       /* Parse the declaration itself.  */
7947       cp_parser_declaration (parser);
7948     }
7949 }
7950
7951 /* Parse a declaration.
7952
7953    declaration:
7954      block-declaration
7955      function-definition
7956      template-declaration
7957      explicit-instantiation
7958      explicit-specialization
7959      linkage-specification
7960      namespace-definition
7961
7962    GNU extension:
7963
7964    declaration:
7965       __extension__ declaration */
7966
7967 static void
7968 cp_parser_declaration (cp_parser* parser)
7969 {
7970   cp_token token1;
7971   cp_token token2;
7972   int saved_pedantic;
7973   void *p;
7974
7975   /* Check for the `__extension__' keyword.  */
7976   if (cp_parser_extension_opt (parser, &saved_pedantic))
7977     {
7978       /* Parse the qualified declaration.  */
7979       cp_parser_declaration (parser);
7980       /* Restore the PEDANTIC flag.  */
7981       pedantic = saved_pedantic;
7982
7983       return;
7984     }
7985
7986   /* Try to figure out what kind of declaration is present.  */
7987   token1 = *cp_lexer_peek_token (parser->lexer);
7988
7989   if (token1.type != CPP_EOF)
7990     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7991   else
7992     {
7993       token2.type = CPP_EOF;
7994       token2.keyword = RID_MAX;
7995     }
7996
7997   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7998   p = obstack_alloc (&declarator_obstack, 0);
7999
8000   /* If the next token is `extern' and the following token is a string
8001      literal, then we have a linkage specification.  */
8002   if (token1.keyword == RID_EXTERN
8003       && cp_parser_is_string_literal (&token2))
8004     cp_parser_linkage_specification (parser);
8005   /* If the next token is `template', then we have either a template
8006      declaration, an explicit instantiation, or an explicit
8007      specialization.  */
8008   else if (token1.keyword == RID_TEMPLATE)
8009     {
8010       /* `template <>' indicates a template specialization.  */
8011       if (token2.type == CPP_LESS
8012           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8013         cp_parser_explicit_specialization (parser);
8014       /* `template <' indicates a template declaration.  */
8015       else if (token2.type == CPP_LESS)
8016         cp_parser_template_declaration (parser, /*member_p=*/false);
8017       /* Anything else must be an explicit instantiation.  */
8018       else
8019         cp_parser_explicit_instantiation (parser);
8020     }
8021   /* If the next token is `export', then we have a template
8022      declaration.  */
8023   else if (token1.keyword == RID_EXPORT)
8024     cp_parser_template_declaration (parser, /*member_p=*/false);
8025   /* If the next token is `extern', 'static' or 'inline' and the one
8026      after that is `template', we have a GNU extended explicit
8027      instantiation directive.  */
8028   else if (cp_parser_allow_gnu_extensions_p (parser)
8029            && (token1.keyword == RID_EXTERN
8030                || token1.keyword == RID_STATIC
8031                || token1.keyword == RID_INLINE)
8032            && token2.keyword == RID_TEMPLATE)
8033     cp_parser_explicit_instantiation (parser);
8034   /* If the next token is `namespace', check for a named or unnamed
8035      namespace definition.  */
8036   else if (token1.keyword == RID_NAMESPACE
8037            && (/* A named namespace definition.  */
8038                (token2.type == CPP_NAME
8039                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8040                     != CPP_EQ))
8041                /* An unnamed namespace definition.  */
8042                || token2.type == CPP_OPEN_BRACE
8043                || token2.keyword == RID_ATTRIBUTE))
8044     cp_parser_namespace_definition (parser);
8045   /* An inline (associated) namespace definition.  */
8046   else if (token1.keyword == RID_INLINE
8047            && token2.keyword == RID_NAMESPACE)
8048     cp_parser_namespace_definition (parser);
8049   /* Objective-C++ declaration/definition.  */
8050   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8051     cp_parser_objc_declaration (parser);
8052   /* We must have either a block declaration or a function
8053      definition.  */
8054   else
8055     /* Try to parse a block-declaration, or a function-definition.  */
8056     cp_parser_block_declaration (parser, /*statement_p=*/false);
8057
8058   /* Free any declarators allocated.  */
8059   obstack_free (&declarator_obstack, p);
8060 }
8061
8062 /* Parse a block-declaration.
8063
8064    block-declaration:
8065      simple-declaration
8066      asm-definition
8067      namespace-alias-definition
8068      using-declaration
8069      using-directive
8070
8071    GNU Extension:
8072
8073    block-declaration:
8074      __extension__ block-declaration
8075
8076    C++0x Extension:
8077
8078    block-declaration:
8079      static_assert-declaration
8080
8081    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8082    part of a declaration-statement.  */
8083
8084 static void
8085 cp_parser_block_declaration (cp_parser *parser,
8086                              bool      statement_p)
8087 {
8088   cp_token *token1;
8089   int saved_pedantic;
8090
8091   /* Check for the `__extension__' keyword.  */
8092   if (cp_parser_extension_opt (parser, &saved_pedantic))
8093     {
8094       /* Parse the qualified declaration.  */
8095       cp_parser_block_declaration (parser, statement_p);
8096       /* Restore the PEDANTIC flag.  */
8097       pedantic = saved_pedantic;
8098
8099       return;
8100     }
8101
8102   /* Peek at the next token to figure out which kind of declaration is
8103      present.  */
8104   token1 = cp_lexer_peek_token (parser->lexer);
8105
8106   /* If the next keyword is `asm', we have an asm-definition.  */
8107   if (token1->keyword == RID_ASM)
8108     {
8109       if (statement_p)
8110         cp_parser_commit_to_tentative_parse (parser);
8111       cp_parser_asm_definition (parser);
8112     }
8113   /* If the next keyword is `namespace', we have a
8114      namespace-alias-definition.  */
8115   else if (token1->keyword == RID_NAMESPACE)
8116     cp_parser_namespace_alias_definition (parser);
8117   /* If the next keyword is `using', we have either a
8118      using-declaration or a using-directive.  */
8119   else if (token1->keyword == RID_USING)
8120     {
8121       cp_token *token2;
8122
8123       if (statement_p)
8124         cp_parser_commit_to_tentative_parse (parser);
8125       /* If the token after `using' is `namespace', then we have a
8126          using-directive.  */
8127       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8128       if (token2->keyword == RID_NAMESPACE)
8129         cp_parser_using_directive (parser);
8130       /* Otherwise, it's a using-declaration.  */
8131       else
8132         cp_parser_using_declaration (parser,
8133                                      /*access_declaration_p=*/false);
8134     }
8135   /* If the next keyword is `__label__' we have a misplaced label
8136      declaration.  */
8137   else if (token1->keyword == RID_LABEL)
8138     {
8139       cp_lexer_consume_token (parser->lexer);
8140       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8141       cp_parser_skip_to_end_of_statement (parser);
8142       /* If the next token is now a `;', consume it.  */
8143       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8144         cp_lexer_consume_token (parser->lexer);
8145     }
8146   /* If the next token is `static_assert' we have a static assertion.  */
8147   else if (token1->keyword == RID_STATIC_ASSERT)
8148     cp_parser_static_assert (parser, /*member_p=*/false);
8149   /* Anything else must be a simple-declaration.  */
8150   else
8151     cp_parser_simple_declaration (parser, !statement_p);
8152 }
8153
8154 /* Parse a simple-declaration.
8155
8156    simple-declaration:
8157      decl-specifier-seq [opt] init-declarator-list [opt] ;
8158
8159    init-declarator-list:
8160      init-declarator
8161      init-declarator-list , init-declarator
8162
8163    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8164    function-definition as a simple-declaration.  */
8165
8166 static void
8167 cp_parser_simple_declaration (cp_parser* parser,
8168                               bool function_definition_allowed_p)
8169 {
8170   cp_decl_specifier_seq decl_specifiers;
8171   int declares_class_or_enum;
8172   bool saw_declarator;
8173
8174   /* Defer access checks until we know what is being declared; the
8175      checks for names appearing in the decl-specifier-seq should be
8176      done as if we were in the scope of the thing being declared.  */
8177   push_deferring_access_checks (dk_deferred);
8178
8179   /* Parse the decl-specifier-seq.  We have to keep track of whether
8180      or not the decl-specifier-seq declares a named class or
8181      enumeration type, since that is the only case in which the
8182      init-declarator-list is allowed to be empty.
8183
8184      [dcl.dcl]
8185
8186      In a simple-declaration, the optional init-declarator-list can be
8187      omitted only when declaring a class or enumeration, that is when
8188      the decl-specifier-seq contains either a class-specifier, an
8189      elaborated-type-specifier, or an enum-specifier.  */
8190   cp_parser_decl_specifier_seq (parser,
8191                                 CP_PARSER_FLAGS_OPTIONAL,
8192                                 &decl_specifiers,
8193                                 &declares_class_or_enum);
8194   /* We no longer need to defer access checks.  */
8195   stop_deferring_access_checks ();
8196
8197   /* In a block scope, a valid declaration must always have a
8198      decl-specifier-seq.  By not trying to parse declarators, we can
8199      resolve the declaration/expression ambiguity more quickly.  */
8200   if (!function_definition_allowed_p
8201       && !decl_specifiers.any_specifiers_p)
8202     {
8203       cp_parser_error (parser, "expected declaration");
8204       goto done;
8205     }
8206
8207   /* If the next two tokens are both identifiers, the code is
8208      erroneous. The usual cause of this situation is code like:
8209
8210        T t;
8211
8212      where "T" should name a type -- but does not.  */
8213   if (!decl_specifiers.type
8214       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8215     {
8216       /* If parsing tentatively, we should commit; we really are
8217          looking at a declaration.  */
8218       cp_parser_commit_to_tentative_parse (parser);
8219       /* Give up.  */
8220       goto done;
8221     }
8222
8223   /* If we have seen at least one decl-specifier, and the next token
8224      is not a parenthesis, then we must be looking at a declaration.
8225      (After "int (" we might be looking at a functional cast.)  */
8226   if (decl_specifiers.any_specifiers_p
8227       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8228       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8229       && !cp_parser_error_occurred (parser))
8230     cp_parser_commit_to_tentative_parse (parser);
8231
8232   /* Keep going until we hit the `;' at the end of the simple
8233      declaration.  */
8234   saw_declarator = false;
8235   while (cp_lexer_next_token_is_not (parser->lexer,
8236                                      CPP_SEMICOLON))
8237     {
8238       cp_token *token;
8239       bool function_definition_p;
8240       tree decl;
8241
8242       if (saw_declarator)
8243         {
8244           /* If we are processing next declarator, coma is expected */
8245           token = cp_lexer_peek_token (parser->lexer);
8246           gcc_assert (token->type == CPP_COMMA);
8247           cp_lexer_consume_token (parser->lexer);
8248         }
8249       else
8250         saw_declarator = true;
8251
8252       /* Parse the init-declarator.  */
8253       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8254                                         /*checks=*/NULL,
8255                                         function_definition_allowed_p,
8256                                         /*member_p=*/false,
8257                                         declares_class_or_enum,
8258                                         &function_definition_p);
8259       /* If an error occurred while parsing tentatively, exit quickly.
8260          (That usually happens when in the body of a function; each
8261          statement is treated as a declaration-statement until proven
8262          otherwise.)  */
8263       if (cp_parser_error_occurred (parser))
8264         goto done;
8265       /* Handle function definitions specially.  */
8266       if (function_definition_p)
8267         {
8268           /* If the next token is a `,', then we are probably
8269              processing something like:
8270
8271                void f() {}, *p;
8272
8273              which is erroneous.  */
8274           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8275             {
8276               cp_token *token = cp_lexer_peek_token (parser->lexer);
8277               error ("%Hmixing declarations and function-definitions is forbidden",
8278                      &token->location);
8279             }
8280           /* Otherwise, we're done with the list of declarators.  */
8281           else
8282             {
8283               pop_deferring_access_checks ();
8284               return;
8285             }
8286         }
8287       /* The next token should be either a `,' or a `;'.  */
8288       token = cp_lexer_peek_token (parser->lexer);
8289       /* If it's a `,', there are more declarators to come.  */
8290       if (token->type == CPP_COMMA)
8291         /* will be consumed next time around */;
8292       /* If it's a `;', we are done.  */
8293       else if (token->type == CPP_SEMICOLON)
8294         break;
8295       /* Anything else is an error.  */
8296       else
8297         {
8298           /* If we have already issued an error message we don't need
8299              to issue another one.  */
8300           if (decl != error_mark_node
8301               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8302             cp_parser_error (parser, "expected %<,%> or %<;%>");
8303           /* Skip tokens until we reach the end of the statement.  */
8304           cp_parser_skip_to_end_of_statement (parser);
8305           /* If the next token is now a `;', consume it.  */
8306           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8307             cp_lexer_consume_token (parser->lexer);
8308           goto done;
8309         }
8310       /* After the first time around, a function-definition is not
8311          allowed -- even if it was OK at first.  For example:
8312
8313            int i, f() {}
8314
8315          is not valid.  */
8316       function_definition_allowed_p = false;
8317     }
8318
8319   /* Issue an error message if no declarators are present, and the
8320      decl-specifier-seq does not itself declare a class or
8321      enumeration.  */
8322   if (!saw_declarator)
8323     {
8324       if (cp_parser_declares_only_class_p (parser))
8325         shadow_tag (&decl_specifiers);
8326       /* Perform any deferred access checks.  */
8327       perform_deferred_access_checks ();
8328     }
8329
8330   /* Consume the `;'.  */
8331   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8332
8333  done:
8334   pop_deferring_access_checks ();
8335 }
8336
8337 /* Parse a decl-specifier-seq.
8338
8339    decl-specifier-seq:
8340      decl-specifier-seq [opt] decl-specifier
8341
8342    decl-specifier:
8343      storage-class-specifier
8344      type-specifier
8345      function-specifier
8346      friend
8347      typedef
8348
8349    GNU Extension:
8350
8351    decl-specifier:
8352      attributes
8353
8354    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8355
8356    The parser flags FLAGS is used to control type-specifier parsing.
8357
8358    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8359    flags:
8360
8361      1: one of the decl-specifiers is an elaborated-type-specifier
8362         (i.e., a type declaration)
8363      2: one of the decl-specifiers is an enum-specifier or a
8364         class-specifier (i.e., a type definition)
8365
8366    */
8367
8368 static void
8369 cp_parser_decl_specifier_seq (cp_parser* parser,
8370                               cp_parser_flags flags,
8371                               cp_decl_specifier_seq *decl_specs,
8372                               int* declares_class_or_enum)
8373 {
8374   bool constructor_possible_p = !parser->in_declarator_p;
8375   cp_token *start_token = NULL;
8376
8377   /* Clear DECL_SPECS.  */
8378   clear_decl_specs (decl_specs);
8379
8380   /* Assume no class or enumeration type is declared.  */
8381   *declares_class_or_enum = 0;
8382
8383   /* Keep reading specifiers until there are no more to read.  */
8384   while (true)
8385     {
8386       bool constructor_p;
8387       bool found_decl_spec;
8388       cp_token *token;
8389
8390       /* Peek at the next token.  */
8391       token = cp_lexer_peek_token (parser->lexer);
8392
8393       /* Save the first token of the decl spec list for error
8394          reporting.  */
8395       if (!start_token)
8396         start_token = token;
8397       /* Handle attributes.  */
8398       if (token->keyword == RID_ATTRIBUTE)
8399         {
8400           /* Parse the attributes.  */
8401           decl_specs->attributes
8402             = chainon (decl_specs->attributes,
8403                        cp_parser_attributes_opt (parser));
8404           continue;
8405         }
8406       /* Assume we will find a decl-specifier keyword.  */
8407       found_decl_spec = true;
8408       /* If the next token is an appropriate keyword, we can simply
8409          add it to the list.  */
8410       switch (token->keyword)
8411         {
8412           /* decl-specifier:
8413                friend  */
8414         case RID_FRIEND:
8415           if (!at_class_scope_p ())
8416             {
8417               error ("%H%<friend%> used outside of class", &token->location);
8418               cp_lexer_purge_token (parser->lexer);
8419             }
8420           else
8421             {
8422               ++decl_specs->specs[(int) ds_friend];
8423               /* Consume the token.  */
8424               cp_lexer_consume_token (parser->lexer);
8425             }
8426           break;
8427
8428           /* function-specifier:
8429                inline
8430                virtual
8431                explicit  */
8432         case RID_INLINE:
8433         case RID_VIRTUAL:
8434         case RID_EXPLICIT:
8435           cp_parser_function_specifier_opt (parser, decl_specs);
8436           break;
8437
8438           /* decl-specifier:
8439                typedef  */
8440         case RID_TYPEDEF:
8441           ++decl_specs->specs[(int) ds_typedef];
8442           /* Consume the token.  */
8443           cp_lexer_consume_token (parser->lexer);
8444           /* A constructor declarator cannot appear in a typedef.  */
8445           constructor_possible_p = false;
8446           /* The "typedef" keyword can only occur in a declaration; we
8447              may as well commit at this point.  */
8448           cp_parser_commit_to_tentative_parse (parser);
8449
8450           if (decl_specs->storage_class != sc_none)
8451             decl_specs->conflicting_specifiers_p = true;
8452           break;
8453
8454           /* storage-class-specifier:
8455                auto
8456                register
8457                static
8458                extern
8459                mutable
8460
8461              GNU Extension:
8462                thread  */
8463         case RID_AUTO:
8464           if (cxx_dialect == cxx98) 
8465             {
8466               /* Consume the token.  */
8467               cp_lexer_consume_token (parser->lexer);
8468
8469               /* Complain about `auto' as a storage specifier, if
8470                  we're complaining about C++0x compatibility.  */
8471               warning 
8472                 (OPT_Wc__0x_compat, 
8473                  "%H%<auto%> will change meaning in C++0x; please remove it",
8474                  &token->location);
8475
8476               /* Set the storage class anyway.  */
8477               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8478                                            token->location);
8479             }
8480           else
8481             /* C++0x auto type-specifier.  */
8482             found_decl_spec = false;
8483           break;
8484
8485         case RID_REGISTER:
8486         case RID_STATIC:
8487         case RID_EXTERN:
8488         case RID_MUTABLE:
8489           /* Consume the token.  */
8490           cp_lexer_consume_token (parser->lexer);
8491           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8492                                        token->location);
8493           break;
8494         case RID_THREAD:
8495           /* Consume the token.  */
8496           cp_lexer_consume_token (parser->lexer);
8497           ++decl_specs->specs[(int) ds_thread];
8498           break;
8499
8500         default:
8501           /* We did not yet find a decl-specifier yet.  */
8502           found_decl_spec = false;
8503           break;
8504         }
8505
8506       /* Constructors are a special case.  The `S' in `S()' is not a
8507          decl-specifier; it is the beginning of the declarator.  */
8508       constructor_p
8509         = (!found_decl_spec
8510            && constructor_possible_p
8511            && (cp_parser_constructor_declarator_p
8512                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8513
8514       /* If we don't have a DECL_SPEC yet, then we must be looking at
8515          a type-specifier.  */
8516       if (!found_decl_spec && !constructor_p)
8517         {
8518           int decl_spec_declares_class_or_enum;
8519           bool is_cv_qualifier;
8520           tree type_spec;
8521
8522           type_spec
8523             = cp_parser_type_specifier (parser, flags,
8524                                         decl_specs,
8525                                         /*is_declaration=*/true,
8526                                         &decl_spec_declares_class_or_enum,
8527                                         &is_cv_qualifier);
8528           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8529
8530           /* If this type-specifier referenced a user-defined type
8531              (a typedef, class-name, etc.), then we can't allow any
8532              more such type-specifiers henceforth.
8533
8534              [dcl.spec]
8535
8536              The longest sequence of decl-specifiers that could
8537              possibly be a type name is taken as the
8538              decl-specifier-seq of a declaration.  The sequence shall
8539              be self-consistent as described below.
8540
8541              [dcl.type]
8542
8543              As a general rule, at most one type-specifier is allowed
8544              in the complete decl-specifier-seq of a declaration.  The
8545              only exceptions are the following:
8546
8547              -- const or volatile can be combined with any other
8548                 type-specifier.
8549
8550              -- signed or unsigned can be combined with char, long,
8551                 short, or int.
8552
8553              -- ..
8554
8555              Example:
8556
8557                typedef char* Pc;
8558                void g (const int Pc);
8559
8560              Here, Pc is *not* part of the decl-specifier seq; it's
8561              the declarator.  Therefore, once we see a type-specifier
8562              (other than a cv-qualifier), we forbid any additional
8563              user-defined types.  We *do* still allow things like `int
8564              int' to be considered a decl-specifier-seq, and issue the
8565              error message later.  */
8566           if (type_spec && !is_cv_qualifier)
8567             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8568           /* A constructor declarator cannot follow a type-specifier.  */
8569           if (type_spec)
8570             {
8571               constructor_possible_p = false;
8572               found_decl_spec = true;
8573             }
8574         }
8575
8576       /* If we still do not have a DECL_SPEC, then there are no more
8577          decl-specifiers.  */
8578       if (!found_decl_spec)
8579         break;
8580
8581       decl_specs->any_specifiers_p = true;
8582       /* After we see one decl-specifier, further decl-specifiers are
8583          always optional.  */
8584       flags |= CP_PARSER_FLAGS_OPTIONAL;
8585     }
8586
8587   cp_parser_check_decl_spec (decl_specs, start_token->location);
8588
8589   /* Don't allow a friend specifier with a class definition.  */
8590   if (decl_specs->specs[(int) ds_friend] != 0
8591       && (*declares_class_or_enum & 2))
8592     error ("%Hclass definition may not be declared a friend",
8593             &start_token->location);
8594 }
8595
8596 /* Parse an (optional) storage-class-specifier.
8597
8598    storage-class-specifier:
8599      auto
8600      register
8601      static
8602      extern
8603      mutable
8604
8605    GNU Extension:
8606
8607    storage-class-specifier:
8608      thread
8609
8610    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8611
8612 static tree
8613 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8614 {
8615   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8616     {
8617     case RID_AUTO:
8618       if (cxx_dialect != cxx98)
8619         return NULL_TREE;
8620       /* Fall through for C++98.  */
8621
8622     case RID_REGISTER:
8623     case RID_STATIC:
8624     case RID_EXTERN:
8625     case RID_MUTABLE:
8626     case RID_THREAD:
8627       /* Consume the token.  */
8628       return cp_lexer_consume_token (parser->lexer)->u.value;
8629
8630     default:
8631       return NULL_TREE;
8632     }
8633 }
8634
8635 /* Parse an (optional) function-specifier.
8636
8637    function-specifier:
8638      inline
8639      virtual
8640      explicit
8641
8642    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8643    Updates DECL_SPECS, if it is non-NULL.  */
8644
8645 static tree
8646 cp_parser_function_specifier_opt (cp_parser* parser,
8647                                   cp_decl_specifier_seq *decl_specs)
8648 {
8649   cp_token *token = cp_lexer_peek_token (parser->lexer);
8650   switch (token->keyword)
8651     {
8652     case RID_INLINE:
8653       if (decl_specs)
8654         ++decl_specs->specs[(int) ds_inline];
8655       break;
8656
8657     case RID_VIRTUAL:
8658       /* 14.5.2.3 [temp.mem]
8659
8660          A member function template shall not be virtual.  */
8661       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8662         error ("%Htemplates may not be %<virtual%>", &token->location);
8663       else if (decl_specs)
8664         ++decl_specs->specs[(int) ds_virtual];
8665       break;
8666
8667     case RID_EXPLICIT:
8668       if (decl_specs)
8669         ++decl_specs->specs[(int) ds_explicit];
8670       break;
8671
8672     default:
8673       return NULL_TREE;
8674     }
8675
8676   /* Consume the token.  */
8677   return cp_lexer_consume_token (parser->lexer)->u.value;
8678 }
8679
8680 /* Parse a linkage-specification.
8681
8682    linkage-specification:
8683      extern string-literal { declaration-seq [opt] }
8684      extern string-literal declaration  */
8685
8686 static void
8687 cp_parser_linkage_specification (cp_parser* parser)
8688 {
8689   tree linkage;
8690
8691   /* Look for the `extern' keyword.  */
8692   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8693
8694   /* Look for the string-literal.  */
8695   linkage = cp_parser_string_literal (parser, false, false);
8696
8697   /* Transform the literal into an identifier.  If the literal is a
8698      wide-character string, or contains embedded NULs, then we can't
8699      handle it as the user wants.  */
8700   if (strlen (TREE_STRING_POINTER (linkage))
8701       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8702     {
8703       cp_parser_error (parser, "invalid linkage-specification");
8704       /* Assume C++ linkage.  */
8705       linkage = lang_name_cplusplus;
8706     }
8707   else
8708     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8709
8710   /* We're now using the new linkage.  */
8711   push_lang_context (linkage);
8712
8713   /* If the next token is a `{', then we're using the first
8714      production.  */
8715   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8716     {
8717       /* Consume the `{' token.  */
8718       cp_lexer_consume_token (parser->lexer);
8719       /* Parse the declarations.  */
8720       cp_parser_declaration_seq_opt (parser);
8721       /* Look for the closing `}'.  */
8722       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8723     }
8724   /* Otherwise, there's just one declaration.  */
8725   else
8726     {
8727       bool saved_in_unbraced_linkage_specification_p;
8728
8729       saved_in_unbraced_linkage_specification_p
8730         = parser->in_unbraced_linkage_specification_p;
8731       parser->in_unbraced_linkage_specification_p = true;
8732       cp_parser_declaration (parser);
8733       parser->in_unbraced_linkage_specification_p
8734         = saved_in_unbraced_linkage_specification_p;
8735     }
8736
8737   /* We're done with the linkage-specification.  */
8738   pop_lang_context ();
8739 }
8740
8741 /* Parse a static_assert-declaration.
8742
8743    static_assert-declaration:
8744      static_assert ( constant-expression , string-literal ) ; 
8745
8746    If MEMBER_P, this static_assert is a class member.  */
8747
8748 static void 
8749 cp_parser_static_assert(cp_parser *parser, bool member_p)
8750 {
8751   tree condition;
8752   tree message;
8753   cp_token *token;
8754   location_t saved_loc;
8755
8756   /* Peek at the `static_assert' token so we can keep track of exactly
8757      where the static assertion started.  */
8758   token = cp_lexer_peek_token (parser->lexer);
8759   saved_loc = token->location;
8760
8761   /* Look for the `static_assert' keyword.  */
8762   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8763                                   "%<static_assert%>"))
8764     return;
8765
8766   /*  We know we are in a static assertion; commit to any tentative
8767       parse.  */
8768   if (cp_parser_parsing_tentatively (parser))
8769     cp_parser_commit_to_tentative_parse (parser);
8770
8771   /* Parse the `(' starting the static assertion condition.  */
8772   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8773
8774   /* Parse the constant-expression.  */
8775   condition = 
8776     cp_parser_constant_expression (parser,
8777                                    /*allow_non_constant_p=*/false,
8778                                    /*non_constant_p=*/NULL);
8779
8780   /* Parse the separating `,'.  */
8781   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8782
8783   /* Parse the string-literal message.  */
8784   message = cp_parser_string_literal (parser, 
8785                                       /*translate=*/false,
8786                                       /*wide_ok=*/true);
8787
8788   /* A `)' completes the static assertion.  */
8789   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8790     cp_parser_skip_to_closing_parenthesis (parser, 
8791                                            /*recovering=*/true, 
8792                                            /*or_comma=*/false,
8793                                            /*consume_paren=*/true);
8794
8795   /* A semicolon terminates the declaration.  */
8796   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8797
8798   /* Complete the static assertion, which may mean either processing 
8799      the static assert now or saving it for template instantiation.  */
8800   finish_static_assert (condition, message, saved_loc, member_p);
8801 }
8802
8803 /* Parse a `decltype' type. Returns the type. 
8804
8805    simple-type-specifier:
8806      decltype ( expression )  */
8807
8808 static tree
8809 cp_parser_decltype (cp_parser *parser)
8810 {
8811   tree expr;
8812   bool id_expression_or_member_access_p = false;
8813   const char *saved_message;
8814   bool saved_integral_constant_expression_p;
8815   bool saved_non_integral_constant_expression_p;
8816   cp_token *id_expr_start_token;
8817
8818   /* Look for the `decltype' token.  */
8819   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8820     return error_mark_node;
8821
8822   /* Types cannot be defined in a `decltype' expression.  Save away the
8823      old message.  */
8824   saved_message = parser->type_definition_forbidden_message;
8825
8826   /* And create the new one.  */
8827   parser->type_definition_forbidden_message
8828     = "types may not be defined in %<decltype%> expressions";
8829
8830   /* The restrictions on constant-expressions do not apply inside
8831      decltype expressions.  */
8832   saved_integral_constant_expression_p
8833     = parser->integral_constant_expression_p;
8834   saved_non_integral_constant_expression_p
8835     = parser->non_integral_constant_expression_p;
8836   parser->integral_constant_expression_p = false;
8837
8838   /* Do not actually evaluate the expression.  */
8839   ++skip_evaluation;
8840
8841   /* Parse the opening `('.  */
8842   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8843     return error_mark_node;
8844   
8845   /* First, try parsing an id-expression.  */
8846   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8847   cp_parser_parse_tentatively (parser);
8848   expr = cp_parser_id_expression (parser,
8849                                   /*template_keyword_p=*/false,
8850                                   /*check_dependency_p=*/true,
8851                                   /*template_p=*/NULL,
8852                                   /*declarator_p=*/false,
8853                                   /*optional_p=*/false);
8854
8855   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8856     {
8857       bool non_integral_constant_expression_p = false;
8858       tree id_expression = expr;
8859       cp_id_kind idk;
8860       const char *error_msg;
8861
8862       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8863         /* Lookup the name we got back from the id-expression.  */
8864         expr = cp_parser_lookup_name (parser, expr,
8865                                       none_type,
8866                                       /*is_template=*/false,
8867                                       /*is_namespace=*/false,
8868                                       /*check_dependency=*/true,
8869                                       /*ambiguous_decls=*/NULL,
8870                                       id_expr_start_token->location);
8871
8872       if (expr
8873           && expr != error_mark_node
8874           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8875           && TREE_CODE (expr) != TYPE_DECL
8876           && (TREE_CODE (expr) != BIT_NOT_EXPR
8877               || !TYPE_P (TREE_OPERAND (expr, 0)))
8878           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8879         {
8880           /* Complete lookup of the id-expression.  */
8881           expr = (finish_id_expression
8882                   (id_expression, expr, parser->scope, &idk,
8883                    /*integral_constant_expression_p=*/false,
8884                    /*allow_non_integral_constant_expression_p=*/true,
8885                    &non_integral_constant_expression_p,
8886                    /*template_p=*/false,
8887                    /*done=*/true,
8888                    /*address_p=*/false,
8889                    /*template_arg_p=*/false,
8890                    &error_msg,
8891                    id_expr_start_token->location));
8892
8893           if (expr == error_mark_node)
8894             /* We found an id-expression, but it was something that we
8895                should not have found. This is an error, not something
8896                we can recover from, so note that we found an
8897                id-expression and we'll recover as gracefully as
8898                possible.  */
8899             id_expression_or_member_access_p = true;
8900         }
8901
8902       if (expr 
8903           && expr != error_mark_node
8904           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8905         /* We have an id-expression.  */
8906         id_expression_or_member_access_p = true;
8907     }
8908
8909   if (!id_expression_or_member_access_p)
8910     {
8911       /* Abort the id-expression parse.  */
8912       cp_parser_abort_tentative_parse (parser);
8913
8914       /* Parsing tentatively, again.  */
8915       cp_parser_parse_tentatively (parser);
8916
8917       /* Parse a class member access.  */
8918       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8919                                            /*cast_p=*/false,
8920                                            /*member_access_only_p=*/true, NULL);
8921
8922       if (expr 
8923           && expr != error_mark_node
8924           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8925         /* We have an id-expression.  */
8926         id_expression_or_member_access_p = true;
8927     }
8928
8929   if (id_expression_or_member_access_p)
8930     /* We have parsed the complete id-expression or member access.  */
8931     cp_parser_parse_definitely (parser);
8932   else
8933     {
8934       bool saved_greater_than_is_operator_p;
8935
8936       /* Abort our attempt to parse an id-expression or member access
8937          expression.  */
8938       cp_parser_abort_tentative_parse (parser);
8939
8940       /* Within a parenthesized expression, a `>' token is always
8941          the greater-than operator.  */
8942       saved_greater_than_is_operator_p
8943         = parser->greater_than_is_operator_p;
8944       parser->greater_than_is_operator_p = true;
8945
8946       /* Parse a full expression.  */
8947       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8948
8949       /* The `>' token might be the end of a template-id or
8950          template-parameter-list now.  */
8951       parser->greater_than_is_operator_p
8952         = saved_greater_than_is_operator_p;
8953     }
8954
8955   /* Go back to evaluating expressions.  */
8956   --skip_evaluation;
8957
8958   /* Restore the old message and the integral constant expression
8959      flags.  */
8960   parser->type_definition_forbidden_message = saved_message;
8961   parser->integral_constant_expression_p
8962     = saved_integral_constant_expression_p;
8963   parser->non_integral_constant_expression_p
8964     = saved_non_integral_constant_expression_p;
8965
8966   if (expr == error_mark_node)
8967     {
8968       /* Skip everything up to the closing `)'.  */
8969       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8970                                              /*consume_paren=*/true);
8971       return error_mark_node;
8972     }
8973   
8974   /* Parse to the closing `)'.  */
8975   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8976     {
8977       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8978                                              /*consume_paren=*/true);
8979       return error_mark_node;
8980     }
8981
8982   return finish_decltype_type (expr, id_expression_or_member_access_p);
8983 }
8984
8985 /* Special member functions [gram.special] */
8986
8987 /* Parse a conversion-function-id.
8988
8989    conversion-function-id:
8990      operator conversion-type-id
8991
8992    Returns an IDENTIFIER_NODE representing the operator.  */
8993
8994 static tree
8995 cp_parser_conversion_function_id (cp_parser* parser)
8996 {
8997   tree type;
8998   tree saved_scope;
8999   tree saved_qualifying_scope;
9000   tree saved_object_scope;
9001   tree pushed_scope = NULL_TREE;
9002
9003   /* Look for the `operator' token.  */
9004   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9005     return error_mark_node;
9006   /* When we parse the conversion-type-id, the current scope will be
9007      reset.  However, we need that information in able to look up the
9008      conversion function later, so we save it here.  */
9009   saved_scope = parser->scope;
9010   saved_qualifying_scope = parser->qualifying_scope;
9011   saved_object_scope = parser->object_scope;
9012   /* We must enter the scope of the class so that the names of
9013      entities declared within the class are available in the
9014      conversion-type-id.  For example, consider:
9015
9016        struct S {
9017          typedef int I;
9018          operator I();
9019        };
9020
9021        S::operator I() { ... }
9022
9023      In order to see that `I' is a type-name in the definition, we
9024      must be in the scope of `S'.  */
9025   if (saved_scope)
9026     pushed_scope = push_scope (saved_scope);
9027   /* Parse the conversion-type-id.  */
9028   type = cp_parser_conversion_type_id (parser);
9029   /* Leave the scope of the class, if any.  */
9030   if (pushed_scope)
9031     pop_scope (pushed_scope);
9032   /* Restore the saved scope.  */
9033   parser->scope = saved_scope;
9034   parser->qualifying_scope = saved_qualifying_scope;
9035   parser->object_scope = saved_object_scope;
9036   /* If the TYPE is invalid, indicate failure.  */
9037   if (type == error_mark_node)
9038     return error_mark_node;
9039   return mangle_conv_op_name_for_type (type);
9040 }
9041
9042 /* Parse a conversion-type-id:
9043
9044    conversion-type-id:
9045      type-specifier-seq conversion-declarator [opt]
9046
9047    Returns the TYPE specified.  */
9048
9049 static tree
9050 cp_parser_conversion_type_id (cp_parser* parser)
9051 {
9052   tree attributes;
9053   cp_decl_specifier_seq type_specifiers;
9054   cp_declarator *declarator;
9055   tree type_specified;
9056
9057   /* Parse the attributes.  */
9058   attributes = cp_parser_attributes_opt (parser);
9059   /* Parse the type-specifiers.  */
9060   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9061                                 /*is_trailing_return=*/false,
9062                                 &type_specifiers);
9063   /* If that didn't work, stop.  */
9064   if (type_specifiers.type == error_mark_node)
9065     return error_mark_node;
9066   /* Parse the conversion-declarator.  */
9067   declarator = cp_parser_conversion_declarator_opt (parser);
9068
9069   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9070                                     /*initialized=*/0, &attributes);
9071   if (attributes)
9072     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9073
9074   /* Don't give this error when parsing tentatively.  This happens to
9075      work because we always parse this definitively once.  */
9076   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9077       && type_uses_auto (type_specified))
9078     {
9079       error ("invalid use of %<auto%> in conversion operator");
9080       return error_mark_node;
9081     }
9082
9083   return type_specified;
9084 }
9085
9086 /* Parse an (optional) conversion-declarator.
9087
9088    conversion-declarator:
9089      ptr-operator conversion-declarator [opt]
9090
9091    */
9092
9093 static cp_declarator *
9094 cp_parser_conversion_declarator_opt (cp_parser* parser)
9095 {
9096   enum tree_code code;
9097   tree class_type;
9098   cp_cv_quals cv_quals;
9099
9100   /* We don't know if there's a ptr-operator next, or not.  */
9101   cp_parser_parse_tentatively (parser);
9102   /* Try the ptr-operator.  */
9103   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9104   /* If it worked, look for more conversion-declarators.  */
9105   if (cp_parser_parse_definitely (parser))
9106     {
9107       cp_declarator *declarator;
9108
9109       /* Parse another optional declarator.  */
9110       declarator = cp_parser_conversion_declarator_opt (parser);
9111
9112       return cp_parser_make_indirect_declarator
9113         (code, class_type, cv_quals, declarator);
9114    }
9115
9116   return NULL;
9117 }
9118
9119 /* Parse an (optional) ctor-initializer.
9120
9121    ctor-initializer:
9122      : mem-initializer-list
9123
9124    Returns TRUE iff the ctor-initializer was actually present.  */
9125
9126 static bool
9127 cp_parser_ctor_initializer_opt (cp_parser* parser)
9128 {
9129   /* If the next token is not a `:', then there is no
9130      ctor-initializer.  */
9131   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9132     {
9133       /* Do default initialization of any bases and members.  */
9134       if (DECL_CONSTRUCTOR_P (current_function_decl))
9135         finish_mem_initializers (NULL_TREE);
9136
9137       return false;
9138     }
9139
9140   /* Consume the `:' token.  */
9141   cp_lexer_consume_token (parser->lexer);
9142   /* And the mem-initializer-list.  */
9143   cp_parser_mem_initializer_list (parser);
9144
9145   return true;
9146 }
9147
9148 /* Parse a mem-initializer-list.
9149
9150    mem-initializer-list:
9151      mem-initializer ... [opt]
9152      mem-initializer ... [opt] , mem-initializer-list  */
9153
9154 static void
9155 cp_parser_mem_initializer_list (cp_parser* parser)
9156 {
9157   tree mem_initializer_list = NULL_TREE;
9158   cp_token *token = cp_lexer_peek_token (parser->lexer);
9159
9160   /* Let the semantic analysis code know that we are starting the
9161      mem-initializer-list.  */
9162   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9163     error ("%Honly constructors take base initializers",
9164            &token->location);
9165
9166   /* Loop through the list.  */
9167   while (true)
9168     {
9169       tree mem_initializer;
9170
9171       token = cp_lexer_peek_token (parser->lexer);
9172       /* Parse the mem-initializer.  */
9173       mem_initializer = cp_parser_mem_initializer (parser);
9174       /* If the next token is a `...', we're expanding member initializers. */
9175       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9176         {
9177           /* Consume the `...'. */
9178           cp_lexer_consume_token (parser->lexer);
9179
9180           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9181              can be expanded but members cannot. */
9182           if (mem_initializer != error_mark_node
9183               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9184             {
9185               error ("%Hcannot expand initializer for member %<%D%>",
9186                      &token->location, TREE_PURPOSE (mem_initializer));
9187               mem_initializer = error_mark_node;
9188             }
9189
9190           /* Construct the pack expansion type. */
9191           if (mem_initializer != error_mark_node)
9192             mem_initializer = make_pack_expansion (mem_initializer);
9193         }
9194       /* Add it to the list, unless it was erroneous.  */
9195       if (mem_initializer != error_mark_node)
9196         {
9197           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9198           mem_initializer_list = mem_initializer;
9199         }
9200       /* If the next token is not a `,', we're done.  */
9201       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9202         break;
9203       /* Consume the `,' token.  */
9204       cp_lexer_consume_token (parser->lexer);
9205     }
9206
9207   /* Perform semantic analysis.  */
9208   if (DECL_CONSTRUCTOR_P (current_function_decl))
9209     finish_mem_initializers (mem_initializer_list);
9210 }
9211
9212 /* Parse a mem-initializer.
9213
9214    mem-initializer:
9215      mem-initializer-id ( expression-list [opt] )
9216      mem-initializer-id braced-init-list
9217
9218    GNU extension:
9219
9220    mem-initializer:
9221      ( expression-list [opt] )
9222
9223    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9224    class) or FIELD_DECL (for a non-static data member) to initialize;
9225    the TREE_VALUE is the expression-list.  An empty initialization
9226    list is represented by void_list_node.  */
9227
9228 static tree
9229 cp_parser_mem_initializer (cp_parser* parser)
9230 {
9231   tree mem_initializer_id;
9232   tree expression_list;
9233   tree member;
9234   cp_token *token = cp_lexer_peek_token (parser->lexer);
9235
9236   /* Find out what is being initialized.  */
9237   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9238     {
9239       permerror (token->location,
9240                  "anachronistic old-style base class initializer");
9241       mem_initializer_id = NULL_TREE;
9242     }
9243   else
9244     {
9245       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9246       if (mem_initializer_id == error_mark_node)
9247         return mem_initializer_id;
9248     }
9249   member = expand_member_init (mem_initializer_id);
9250   if (member && !DECL_P (member))
9251     in_base_initializer = 1;
9252
9253   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9254     {
9255       bool expr_non_constant_p;
9256       maybe_warn_cpp0x ("extended initializer lists");
9257       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9258       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9259       expression_list = build_tree_list (NULL_TREE, expression_list);
9260     }
9261   else
9262     expression_list
9263       = cp_parser_parenthesized_expression_list (parser, false,
9264                                                  /*cast_p=*/false,
9265                                                  /*allow_expansion_p=*/true,
9266                                                  /*non_constant_p=*/NULL);
9267   if (expression_list == error_mark_node)
9268     return error_mark_node;
9269   if (!expression_list)
9270     expression_list = void_type_node;
9271
9272   in_base_initializer = 0;
9273
9274   return member ? build_tree_list (member, expression_list) : error_mark_node;
9275 }
9276
9277 /* Parse a mem-initializer-id.
9278
9279    mem-initializer-id:
9280      :: [opt] nested-name-specifier [opt] class-name
9281      identifier
9282
9283    Returns a TYPE indicating the class to be initializer for the first
9284    production.  Returns an IDENTIFIER_NODE indicating the data member
9285    to be initialized for the second production.  */
9286
9287 static tree
9288 cp_parser_mem_initializer_id (cp_parser* parser)
9289 {
9290   bool global_scope_p;
9291   bool nested_name_specifier_p;
9292   bool template_p = false;
9293   tree id;
9294
9295   cp_token *token = cp_lexer_peek_token (parser->lexer);
9296
9297   /* `typename' is not allowed in this context ([temp.res]).  */
9298   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9299     {
9300       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9301              "member initializer is implicitly a type)",
9302              &token->location);
9303       cp_lexer_consume_token (parser->lexer);
9304     }
9305   /* Look for the optional `::' operator.  */
9306   global_scope_p
9307     = (cp_parser_global_scope_opt (parser,
9308                                    /*current_scope_valid_p=*/false)
9309        != NULL_TREE);
9310   /* Look for the optional nested-name-specifier.  The simplest way to
9311      implement:
9312
9313        [temp.res]
9314
9315        The keyword `typename' is not permitted in a base-specifier or
9316        mem-initializer; in these contexts a qualified name that
9317        depends on a template-parameter is implicitly assumed to be a
9318        type name.
9319
9320      is to assume that we have seen the `typename' keyword at this
9321      point.  */
9322   nested_name_specifier_p
9323     = (cp_parser_nested_name_specifier_opt (parser,
9324                                             /*typename_keyword_p=*/true,
9325                                             /*check_dependency_p=*/true,
9326                                             /*type_p=*/true,
9327                                             /*is_declaration=*/true)
9328        != NULL_TREE);
9329   if (nested_name_specifier_p)
9330     template_p = cp_parser_optional_template_keyword (parser);
9331   /* If there is a `::' operator or a nested-name-specifier, then we
9332      are definitely looking for a class-name.  */
9333   if (global_scope_p || nested_name_specifier_p)
9334     return cp_parser_class_name (parser,
9335                                  /*typename_keyword_p=*/true,
9336                                  /*template_keyword_p=*/template_p,
9337                                  none_type,
9338                                  /*check_dependency_p=*/true,
9339                                  /*class_head_p=*/false,
9340                                  /*is_declaration=*/true);
9341   /* Otherwise, we could also be looking for an ordinary identifier.  */
9342   cp_parser_parse_tentatively (parser);
9343   /* Try a class-name.  */
9344   id = cp_parser_class_name (parser,
9345                              /*typename_keyword_p=*/true,
9346                              /*template_keyword_p=*/false,
9347                              none_type,
9348                              /*check_dependency_p=*/true,
9349                              /*class_head_p=*/false,
9350                              /*is_declaration=*/true);
9351   /* If we found one, we're done.  */
9352   if (cp_parser_parse_definitely (parser))
9353     return id;
9354   /* Otherwise, look for an ordinary identifier.  */
9355   return cp_parser_identifier (parser);
9356 }
9357
9358 /* Overloading [gram.over] */
9359
9360 /* Parse an operator-function-id.
9361
9362    operator-function-id:
9363      operator operator
9364
9365    Returns an IDENTIFIER_NODE for the operator which is a
9366    human-readable spelling of the identifier, e.g., `operator +'.  */
9367
9368 static tree
9369 cp_parser_operator_function_id (cp_parser* parser)
9370 {
9371   /* Look for the `operator' keyword.  */
9372   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9373     return error_mark_node;
9374   /* And then the name of the operator itself.  */
9375   return cp_parser_operator (parser);
9376 }
9377
9378 /* Parse an operator.
9379
9380    operator:
9381      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9382      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9383      || ++ -- , ->* -> () []
9384
9385    GNU Extensions:
9386
9387    operator:
9388      <? >? <?= >?=
9389
9390    Returns an IDENTIFIER_NODE for the operator which is a
9391    human-readable spelling of the identifier, e.g., `operator +'.  */
9392
9393 static tree
9394 cp_parser_operator (cp_parser* parser)
9395 {
9396   tree id = NULL_TREE;
9397   cp_token *token;
9398
9399   /* Peek at the next token.  */
9400   token = cp_lexer_peek_token (parser->lexer);
9401   /* Figure out which operator we have.  */
9402   switch (token->type)
9403     {
9404     case CPP_KEYWORD:
9405       {
9406         enum tree_code op;
9407
9408         /* The keyword should be either `new' or `delete'.  */
9409         if (token->keyword == RID_NEW)
9410           op = NEW_EXPR;
9411         else if (token->keyword == RID_DELETE)
9412           op = DELETE_EXPR;
9413         else
9414           break;
9415
9416         /* Consume the `new' or `delete' token.  */
9417         cp_lexer_consume_token (parser->lexer);
9418
9419         /* Peek at the next token.  */
9420         token = cp_lexer_peek_token (parser->lexer);
9421         /* If it's a `[' token then this is the array variant of the
9422            operator.  */
9423         if (token->type == CPP_OPEN_SQUARE)
9424           {
9425             /* Consume the `[' token.  */
9426             cp_lexer_consume_token (parser->lexer);
9427             /* Look for the `]' token.  */
9428             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9429             id = ansi_opname (op == NEW_EXPR
9430                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9431           }
9432         /* Otherwise, we have the non-array variant.  */
9433         else
9434           id = ansi_opname (op);
9435
9436         return id;
9437       }
9438
9439     case CPP_PLUS:
9440       id = ansi_opname (PLUS_EXPR);
9441       break;
9442
9443     case CPP_MINUS:
9444       id = ansi_opname (MINUS_EXPR);
9445       break;
9446
9447     case CPP_MULT:
9448       id = ansi_opname (MULT_EXPR);
9449       break;
9450
9451     case CPP_DIV:
9452       id = ansi_opname (TRUNC_DIV_EXPR);
9453       break;
9454
9455     case CPP_MOD:
9456       id = ansi_opname (TRUNC_MOD_EXPR);
9457       break;
9458
9459     case CPP_XOR:
9460       id = ansi_opname (BIT_XOR_EXPR);
9461       break;
9462
9463     case CPP_AND:
9464       id = ansi_opname (BIT_AND_EXPR);
9465       break;
9466
9467     case CPP_OR:
9468       id = ansi_opname (BIT_IOR_EXPR);
9469       break;
9470
9471     case CPP_COMPL:
9472       id = ansi_opname (BIT_NOT_EXPR);
9473       break;
9474
9475     case CPP_NOT:
9476       id = ansi_opname (TRUTH_NOT_EXPR);
9477       break;
9478
9479     case CPP_EQ:
9480       id = ansi_assopname (NOP_EXPR);
9481       break;
9482
9483     case CPP_LESS:
9484       id = ansi_opname (LT_EXPR);
9485       break;
9486
9487     case CPP_GREATER:
9488       id = ansi_opname (GT_EXPR);
9489       break;
9490
9491     case CPP_PLUS_EQ:
9492       id = ansi_assopname (PLUS_EXPR);
9493       break;
9494
9495     case CPP_MINUS_EQ:
9496       id = ansi_assopname (MINUS_EXPR);
9497       break;
9498
9499     case CPP_MULT_EQ:
9500       id = ansi_assopname (MULT_EXPR);
9501       break;
9502
9503     case CPP_DIV_EQ:
9504       id = ansi_assopname (TRUNC_DIV_EXPR);
9505       break;
9506
9507     case CPP_MOD_EQ:
9508       id = ansi_assopname (TRUNC_MOD_EXPR);
9509       break;
9510
9511     case CPP_XOR_EQ:
9512       id = ansi_assopname (BIT_XOR_EXPR);
9513       break;
9514
9515     case CPP_AND_EQ:
9516       id = ansi_assopname (BIT_AND_EXPR);
9517       break;
9518
9519     case CPP_OR_EQ:
9520       id = ansi_assopname (BIT_IOR_EXPR);
9521       break;
9522
9523     case CPP_LSHIFT:
9524       id = ansi_opname (LSHIFT_EXPR);
9525       break;
9526
9527     case CPP_RSHIFT:
9528       id = ansi_opname (RSHIFT_EXPR);
9529       break;
9530
9531     case CPP_LSHIFT_EQ:
9532       id = ansi_assopname (LSHIFT_EXPR);
9533       break;
9534
9535     case CPP_RSHIFT_EQ:
9536       id = ansi_assopname (RSHIFT_EXPR);
9537       break;
9538
9539     case CPP_EQ_EQ:
9540       id = ansi_opname (EQ_EXPR);
9541       break;
9542
9543     case CPP_NOT_EQ:
9544       id = ansi_opname (NE_EXPR);
9545       break;
9546
9547     case CPP_LESS_EQ:
9548       id = ansi_opname (LE_EXPR);
9549       break;
9550
9551     case CPP_GREATER_EQ:
9552       id = ansi_opname (GE_EXPR);
9553       break;
9554
9555     case CPP_AND_AND:
9556       id = ansi_opname (TRUTH_ANDIF_EXPR);
9557       break;
9558
9559     case CPP_OR_OR:
9560       id = ansi_opname (TRUTH_ORIF_EXPR);
9561       break;
9562
9563     case CPP_PLUS_PLUS:
9564       id = ansi_opname (POSTINCREMENT_EXPR);
9565       break;
9566
9567     case CPP_MINUS_MINUS:
9568       id = ansi_opname (PREDECREMENT_EXPR);
9569       break;
9570
9571     case CPP_COMMA:
9572       id = ansi_opname (COMPOUND_EXPR);
9573       break;
9574
9575     case CPP_DEREF_STAR:
9576       id = ansi_opname (MEMBER_REF);
9577       break;
9578
9579     case CPP_DEREF:
9580       id = ansi_opname (COMPONENT_REF);
9581       break;
9582
9583     case CPP_OPEN_PAREN:
9584       /* Consume the `('.  */
9585       cp_lexer_consume_token (parser->lexer);
9586       /* Look for the matching `)'.  */
9587       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9588       return ansi_opname (CALL_EXPR);
9589
9590     case CPP_OPEN_SQUARE:
9591       /* Consume the `['.  */
9592       cp_lexer_consume_token (parser->lexer);
9593       /* Look for the matching `]'.  */
9594       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9595       return ansi_opname (ARRAY_REF);
9596
9597     default:
9598       /* Anything else is an error.  */
9599       break;
9600     }
9601
9602   /* If we have selected an identifier, we need to consume the
9603      operator token.  */
9604   if (id)
9605     cp_lexer_consume_token (parser->lexer);
9606   /* Otherwise, no valid operator name was present.  */
9607   else
9608     {
9609       cp_parser_error (parser, "expected operator");
9610       id = error_mark_node;
9611     }
9612
9613   return id;
9614 }
9615
9616 /* Parse a template-declaration.
9617
9618    template-declaration:
9619      export [opt] template < template-parameter-list > declaration
9620
9621    If MEMBER_P is TRUE, this template-declaration occurs within a
9622    class-specifier.
9623
9624    The grammar rule given by the standard isn't correct.  What
9625    is really meant is:
9626
9627    template-declaration:
9628      export [opt] template-parameter-list-seq
9629        decl-specifier-seq [opt] init-declarator [opt] ;
9630      export [opt] template-parameter-list-seq
9631        function-definition
9632
9633    template-parameter-list-seq:
9634      template-parameter-list-seq [opt]
9635      template < template-parameter-list >  */
9636
9637 static void
9638 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9639 {
9640   /* Check for `export'.  */
9641   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9642     {
9643       /* Consume the `export' token.  */
9644       cp_lexer_consume_token (parser->lexer);
9645       /* Warn that we do not support `export'.  */
9646       warning (0, "keyword %<export%> not implemented, and will be ignored");
9647     }
9648
9649   cp_parser_template_declaration_after_export (parser, member_p);
9650 }
9651
9652 /* Parse a template-parameter-list.
9653
9654    template-parameter-list:
9655      template-parameter
9656      template-parameter-list , template-parameter
9657
9658    Returns a TREE_LIST.  Each node represents a template parameter.
9659    The nodes are connected via their TREE_CHAINs.  */
9660
9661 static tree
9662 cp_parser_template_parameter_list (cp_parser* parser)
9663 {
9664   tree parameter_list = NULL_TREE;
9665
9666   begin_template_parm_list ();
9667   while (true)
9668     {
9669       tree parameter;
9670       bool is_non_type;
9671       bool is_parameter_pack;
9672
9673       /* Parse the template-parameter.  */
9674       parameter = cp_parser_template_parameter (parser, 
9675                                                 &is_non_type,
9676                                                 &is_parameter_pack);
9677       /* Add it to the list.  */
9678       if (parameter != error_mark_node)
9679         parameter_list = process_template_parm (parameter_list,
9680                                                 parameter,
9681                                                 is_non_type,
9682                                                 is_parameter_pack);
9683       else
9684        {
9685          tree err_parm = build_tree_list (parameter, parameter);
9686          TREE_VALUE (err_parm) = error_mark_node;
9687          parameter_list = chainon (parameter_list, err_parm);
9688        }
9689
9690       /* If the next token is not a `,', we're done.  */
9691       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9692         break;
9693       /* Otherwise, consume the `,' token.  */
9694       cp_lexer_consume_token (parser->lexer);
9695     }
9696
9697   return end_template_parm_list (parameter_list);
9698 }
9699
9700 /* Parse a template-parameter.
9701
9702    template-parameter:
9703      type-parameter
9704      parameter-declaration
9705
9706    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9707    the parameter.  The TREE_PURPOSE is the default value, if any.
9708    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9709    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9710    set to true iff this parameter is a parameter pack. */
9711
9712 static tree
9713 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9714                               bool *is_parameter_pack)
9715 {
9716   cp_token *token;
9717   cp_parameter_declarator *parameter_declarator;
9718   cp_declarator *id_declarator;
9719   tree parm;
9720
9721   /* Assume it is a type parameter or a template parameter.  */
9722   *is_non_type = false;
9723   /* Assume it not a parameter pack. */
9724   *is_parameter_pack = false;
9725   /* Peek at the next token.  */
9726   token = cp_lexer_peek_token (parser->lexer);
9727   /* If it is `class' or `template', we have a type-parameter.  */
9728   if (token->keyword == RID_TEMPLATE)
9729     return cp_parser_type_parameter (parser, is_parameter_pack);
9730   /* If it is `class' or `typename' we do not know yet whether it is a
9731      type parameter or a non-type parameter.  Consider:
9732
9733        template <typename T, typename T::X X> ...
9734
9735      or:
9736
9737        template <class C, class D*> ...
9738
9739      Here, the first parameter is a type parameter, and the second is
9740      a non-type parameter.  We can tell by looking at the token after
9741      the identifier -- if it is a `,', `=', or `>' then we have a type
9742      parameter.  */
9743   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9744     {
9745       /* Peek at the token after `class' or `typename'.  */
9746       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9747       /* If it's an ellipsis, we have a template type parameter
9748          pack. */
9749       if (token->type == CPP_ELLIPSIS)
9750         return cp_parser_type_parameter (parser, is_parameter_pack);
9751       /* If it's an identifier, skip it.  */
9752       if (token->type == CPP_NAME)
9753         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9754       /* Now, see if the token looks like the end of a template
9755          parameter.  */
9756       if (token->type == CPP_COMMA
9757           || token->type == CPP_EQ
9758           || token->type == CPP_GREATER)
9759         return cp_parser_type_parameter (parser, is_parameter_pack);
9760     }
9761
9762   /* Otherwise, it is a non-type parameter.
9763
9764      [temp.param]
9765
9766      When parsing a default template-argument for a non-type
9767      template-parameter, the first non-nested `>' is taken as the end
9768      of the template parameter-list rather than a greater-than
9769      operator.  */
9770   *is_non_type = true;
9771   parameter_declarator
9772      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9773                                         /*parenthesized_p=*/NULL);
9774
9775   /* If the parameter declaration is marked as a parameter pack, set
9776      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9777      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9778      grokdeclarator. */
9779   if (parameter_declarator
9780       && parameter_declarator->declarator
9781       && parameter_declarator->declarator->parameter_pack_p)
9782     {
9783       *is_parameter_pack = true;
9784       parameter_declarator->declarator->parameter_pack_p = false;
9785     }
9786
9787   /* If the next token is an ellipsis, and we don't already have it
9788      marked as a parameter pack, then we have a parameter pack (that
9789      has no declarator).  */
9790   if (!*is_parameter_pack
9791       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9792       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9793     {
9794       /* Consume the `...'.  */
9795       cp_lexer_consume_token (parser->lexer);
9796       maybe_warn_variadic_templates ();
9797       
9798       *is_parameter_pack = true;
9799     }
9800   /* We might end up with a pack expansion as the type of the non-type
9801      template parameter, in which case this is a non-type template
9802      parameter pack.  */
9803   else if (parameter_declarator
9804            && parameter_declarator->decl_specifiers.type
9805            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9806     {
9807       *is_parameter_pack = true;
9808       parameter_declarator->decl_specifiers.type = 
9809         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9810     }
9811
9812   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9813     {
9814       /* Parameter packs cannot have default arguments.  However, a
9815          user may try to do so, so we'll parse them and give an
9816          appropriate diagnostic here.  */
9817
9818       /* Consume the `='.  */
9819       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9820       cp_lexer_consume_token (parser->lexer);
9821       
9822       /* Find the name of the parameter pack.  */     
9823       id_declarator = parameter_declarator->declarator;
9824       while (id_declarator && id_declarator->kind != cdk_id)
9825         id_declarator = id_declarator->declarator;
9826       
9827       if (id_declarator && id_declarator->kind == cdk_id)
9828         error ("%Htemplate parameter pack %qD cannot have a default argument",
9829                &start_token->location, id_declarator->u.id.unqualified_name);
9830       else
9831         error ("%Htemplate parameter pack cannot have a default argument",
9832                &start_token->location);
9833       
9834       /* Parse the default argument, but throw away the result.  */
9835       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9836     }
9837
9838   parm = grokdeclarator (parameter_declarator->declarator,
9839                          &parameter_declarator->decl_specifiers,
9840                          PARM, /*initialized=*/0,
9841                          /*attrlist=*/NULL);
9842   if (parm == error_mark_node)
9843     return error_mark_node;
9844
9845   return build_tree_list (parameter_declarator->default_argument, parm);
9846 }
9847
9848 /* Parse a type-parameter.
9849
9850    type-parameter:
9851      class identifier [opt]
9852      class identifier [opt] = type-id
9853      typename identifier [opt]
9854      typename identifier [opt] = type-id
9855      template < template-parameter-list > class identifier [opt]
9856      template < template-parameter-list > class identifier [opt]
9857        = id-expression
9858
9859    GNU Extension (variadic templates):
9860
9861    type-parameter:
9862      class ... identifier [opt]
9863      typename ... identifier [opt]
9864
9865    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9866    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9867    the declaration of the parameter.
9868
9869    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9870
9871 static tree
9872 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9873 {
9874   cp_token *token;
9875   tree parameter;
9876
9877   /* Look for a keyword to tell us what kind of parameter this is.  */
9878   token = cp_parser_require (parser, CPP_KEYWORD,
9879                              "%<class%>, %<typename%>, or %<template%>");
9880   if (!token)
9881     return error_mark_node;
9882
9883   switch (token->keyword)
9884     {
9885     case RID_CLASS:
9886     case RID_TYPENAME:
9887       {
9888         tree identifier;
9889         tree default_argument;
9890
9891         /* If the next token is an ellipsis, we have a template
9892            argument pack. */
9893         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9894           {
9895             /* Consume the `...' token. */
9896             cp_lexer_consume_token (parser->lexer);
9897             maybe_warn_variadic_templates ();
9898
9899             *is_parameter_pack = true;
9900           }
9901
9902         /* If the next token is an identifier, then it names the
9903            parameter.  */
9904         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9905           identifier = cp_parser_identifier (parser);
9906         else
9907           identifier = NULL_TREE;
9908
9909         /* Create the parameter.  */
9910         parameter = finish_template_type_parm (class_type_node, identifier);
9911
9912         /* If the next token is an `=', we have a default argument.  */
9913         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9914           {
9915             /* Consume the `=' token.  */
9916             cp_lexer_consume_token (parser->lexer);
9917             /* Parse the default-argument.  */
9918             push_deferring_access_checks (dk_no_deferred);
9919             default_argument = cp_parser_type_id (parser);
9920
9921             /* Template parameter packs cannot have default
9922                arguments. */
9923             if (*is_parameter_pack)
9924               {
9925                 if (identifier)
9926                   error ("%Htemplate parameter pack %qD cannot have a "
9927                          "default argument", &token->location, identifier);
9928                 else
9929                   error ("%Htemplate parameter packs cannot have "
9930                          "default arguments", &token->location);
9931                 default_argument = NULL_TREE;
9932               }
9933             pop_deferring_access_checks ();
9934           }
9935         else
9936           default_argument = NULL_TREE;
9937
9938         /* Create the combined representation of the parameter and the
9939            default argument.  */
9940         parameter = build_tree_list (default_argument, parameter);
9941       }
9942       break;
9943
9944     case RID_TEMPLATE:
9945       {
9946         tree parameter_list;
9947         tree identifier;
9948         tree default_argument;
9949
9950         /* Look for the `<'.  */
9951         cp_parser_require (parser, CPP_LESS, "%<<%>");
9952         /* Parse the template-parameter-list.  */
9953         parameter_list = cp_parser_template_parameter_list (parser);
9954         /* Look for the `>'.  */
9955         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9956         /* Look for the `class' keyword.  */
9957         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9958         /* If the next token is an ellipsis, we have a template
9959            argument pack. */
9960         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9961           {
9962             /* Consume the `...' token. */
9963             cp_lexer_consume_token (parser->lexer);
9964             maybe_warn_variadic_templates ();
9965
9966             *is_parameter_pack = true;
9967           }
9968         /* If the next token is an `=', then there is a
9969            default-argument.  If the next token is a `>', we are at
9970            the end of the parameter-list.  If the next token is a `,',
9971            then we are at the end of this parameter.  */
9972         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9973             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9974             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9975           {
9976             identifier = cp_parser_identifier (parser);
9977             /* Treat invalid names as if the parameter were nameless.  */
9978             if (identifier == error_mark_node)
9979               identifier = NULL_TREE;
9980           }
9981         else
9982           identifier = NULL_TREE;
9983
9984         /* Create the template parameter.  */
9985         parameter = finish_template_template_parm (class_type_node,
9986                                                    identifier);
9987
9988         /* If the next token is an `=', then there is a
9989            default-argument.  */
9990         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9991           {
9992             bool is_template;
9993
9994             /* Consume the `='.  */
9995             cp_lexer_consume_token (parser->lexer);
9996             /* Parse the id-expression.  */
9997             push_deferring_access_checks (dk_no_deferred);
9998             /* save token before parsing the id-expression, for error
9999                reporting */
10000             token = cp_lexer_peek_token (parser->lexer);
10001             default_argument
10002               = cp_parser_id_expression (parser,
10003                                          /*template_keyword_p=*/false,
10004                                          /*check_dependency_p=*/true,
10005                                          /*template_p=*/&is_template,
10006                                          /*declarator_p=*/false,
10007                                          /*optional_p=*/false);
10008             if (TREE_CODE (default_argument) == TYPE_DECL)
10009               /* If the id-expression was a template-id that refers to
10010                  a template-class, we already have the declaration here,
10011                  so no further lookup is needed.  */
10012                  ;
10013             else
10014               /* Look up the name.  */
10015               default_argument
10016                 = cp_parser_lookup_name (parser, default_argument,
10017                                          none_type,
10018                                          /*is_template=*/is_template,
10019                                          /*is_namespace=*/false,
10020                                          /*check_dependency=*/true,
10021                                          /*ambiguous_decls=*/NULL,
10022                                          token->location);
10023             /* See if the default argument is valid.  */
10024             default_argument
10025               = check_template_template_default_arg (default_argument);
10026
10027             /* Template parameter packs cannot have default
10028                arguments. */
10029             if (*is_parameter_pack)
10030               {
10031                 if (identifier)
10032                   error ("%Htemplate parameter pack %qD cannot "
10033                          "have a default argument",
10034                          &token->location, identifier);
10035                 else
10036                   error ("%Htemplate parameter packs cannot "
10037                          "have default arguments",
10038                          &token->location);
10039                 default_argument = NULL_TREE;
10040               }
10041             pop_deferring_access_checks ();
10042           }
10043         else
10044           default_argument = NULL_TREE;
10045
10046         /* Create the combined representation of the parameter and the
10047            default argument.  */
10048         parameter = build_tree_list (default_argument, parameter);
10049       }
10050       break;
10051
10052     default:
10053       gcc_unreachable ();
10054       break;
10055     }
10056
10057   return parameter;
10058 }
10059
10060 /* Parse a template-id.
10061
10062    template-id:
10063      template-name < template-argument-list [opt] >
10064
10065    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10066    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10067    returned.  Otherwise, if the template-name names a function, or set
10068    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10069    names a class, returns a TYPE_DECL for the specialization.
10070
10071    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10072    uninstantiated templates.  */
10073
10074 static tree
10075 cp_parser_template_id (cp_parser *parser,
10076                        bool template_keyword_p,
10077                        bool check_dependency_p,
10078                        bool is_declaration)
10079 {
10080   int i;
10081   tree templ;
10082   tree arguments;
10083   tree template_id;
10084   cp_token_position start_of_id = 0;
10085   deferred_access_check *chk;
10086   VEC (deferred_access_check,gc) *access_check;
10087   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10088   bool is_identifier;
10089
10090   /* If the next token corresponds to a template-id, there is no need
10091      to reparse it.  */
10092   next_token = cp_lexer_peek_token (parser->lexer);
10093   if (next_token->type == CPP_TEMPLATE_ID)
10094     {
10095       struct tree_check *check_value;
10096
10097       /* Get the stored value.  */
10098       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10099       /* Perform any access checks that were deferred.  */
10100       access_check = check_value->checks;
10101       if (access_check)
10102         {
10103           for (i = 0 ;
10104                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10105                ++i)
10106             {
10107               perform_or_defer_access_check (chk->binfo,
10108                                              chk->decl,
10109                                              chk->diag_decl);
10110             }
10111         }
10112       /* Return the stored value.  */
10113       return check_value->value;
10114     }
10115
10116   /* Avoid performing name lookup if there is no possibility of
10117      finding a template-id.  */
10118   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10119       || (next_token->type == CPP_NAME
10120           && !cp_parser_nth_token_starts_template_argument_list_p
10121                (parser, 2)))
10122     {
10123       cp_parser_error (parser, "expected template-id");
10124       return error_mark_node;
10125     }
10126
10127   /* Remember where the template-id starts.  */
10128   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10129     start_of_id = cp_lexer_token_position (parser->lexer, false);
10130
10131   push_deferring_access_checks (dk_deferred);
10132
10133   /* Parse the template-name.  */
10134   is_identifier = false;
10135   token = cp_lexer_peek_token (parser->lexer);
10136   templ = cp_parser_template_name (parser, template_keyword_p,
10137                                    check_dependency_p,
10138                                    is_declaration,
10139                                    &is_identifier);
10140   if (templ == error_mark_node || is_identifier)
10141     {
10142       pop_deferring_access_checks ();
10143       return templ;
10144     }
10145
10146   /* If we find the sequence `[:' after a template-name, it's probably
10147      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10148      parse correctly the argument list.  */
10149   next_token = cp_lexer_peek_token (parser->lexer);
10150   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10151   if (next_token->type == CPP_OPEN_SQUARE
10152       && next_token->flags & DIGRAPH
10153       && next_token_2->type == CPP_COLON
10154       && !(next_token_2->flags & PREV_WHITE))
10155     {
10156       cp_parser_parse_tentatively (parser);
10157       /* Change `:' into `::'.  */
10158       next_token_2->type = CPP_SCOPE;
10159       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10160          CPP_LESS.  */
10161       cp_lexer_consume_token (parser->lexer);
10162
10163       /* Parse the arguments.  */
10164       arguments = cp_parser_enclosed_template_argument_list (parser);
10165       if (!cp_parser_parse_definitely (parser))
10166         {
10167           /* If we couldn't parse an argument list, then we revert our changes
10168              and return simply an error. Maybe this is not a template-id
10169              after all.  */
10170           next_token_2->type = CPP_COLON;
10171           cp_parser_error (parser, "expected %<<%>");
10172           pop_deferring_access_checks ();
10173           return error_mark_node;
10174         }
10175       /* Otherwise, emit an error about the invalid digraph, but continue
10176          parsing because we got our argument list.  */
10177       if (permerror (next_token->location,
10178                      "%<<::%> cannot begin a template-argument list"))
10179         {
10180           static bool hint = false;
10181           inform (next_token->location,
10182                   "%<<:%> is an alternate spelling for %<[%>."
10183                   " Insert whitespace between %<<%> and %<::%>");
10184           if (!hint && !flag_permissive)
10185             {
10186               inform (next_token->location, "(if you use %<-fpermissive%>"
10187                       " G++ will accept your code)");
10188               hint = true;
10189             }
10190         }
10191     }
10192   else
10193     {
10194       /* Look for the `<' that starts the template-argument-list.  */
10195       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10196         {
10197           pop_deferring_access_checks ();
10198           return error_mark_node;
10199         }
10200       /* Parse the arguments.  */
10201       arguments = cp_parser_enclosed_template_argument_list (parser);
10202     }
10203
10204   /* Build a representation of the specialization.  */
10205   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10206     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10207   else if (DECL_CLASS_TEMPLATE_P (templ)
10208            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10209     {
10210       bool entering_scope;
10211       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10212          template (rather than some instantiation thereof) only if
10213          is not nested within some other construct.  For example, in
10214          "template <typename T> void f(T) { A<T>::", A<T> is just an
10215          instantiation of A.  */
10216       entering_scope = (template_parm_scope_p ()
10217                         && cp_lexer_next_token_is (parser->lexer,
10218                                                    CPP_SCOPE));
10219       template_id
10220         = finish_template_type (templ, arguments, entering_scope);
10221     }
10222   else
10223     {
10224       /* If it's not a class-template or a template-template, it should be
10225          a function-template.  */
10226       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10227                    || TREE_CODE (templ) == OVERLOAD
10228                    || BASELINK_P (templ)));
10229
10230       template_id = lookup_template_function (templ, arguments);
10231     }
10232
10233   /* If parsing tentatively, replace the sequence of tokens that makes
10234      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10235      should we re-parse the token stream, we will not have to repeat
10236      the effort required to do the parse, nor will we issue duplicate
10237      error messages about problems during instantiation of the
10238      template.  */
10239   if (start_of_id)
10240     {
10241       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10242
10243       /* Reset the contents of the START_OF_ID token.  */
10244       token->type = CPP_TEMPLATE_ID;
10245       /* Retrieve any deferred checks.  Do not pop this access checks yet
10246          so the memory will not be reclaimed during token replacing below.  */
10247       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10248       token->u.tree_check_value->value = template_id;
10249       token->u.tree_check_value->checks = get_deferred_access_checks ();
10250       token->keyword = RID_MAX;
10251
10252       /* Purge all subsequent tokens.  */
10253       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10254
10255       /* ??? Can we actually assume that, if template_id ==
10256          error_mark_node, we will have issued a diagnostic to the
10257          user, as opposed to simply marking the tentative parse as
10258          failed?  */
10259       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10260         error ("%Hparse error in template argument list",
10261                &token->location);
10262     }
10263
10264   pop_deferring_access_checks ();
10265   return template_id;
10266 }
10267
10268 /* Parse a template-name.
10269
10270    template-name:
10271      identifier
10272
10273    The standard should actually say:
10274
10275    template-name:
10276      identifier
10277      operator-function-id
10278
10279    A defect report has been filed about this issue.
10280
10281    A conversion-function-id cannot be a template name because they cannot
10282    be part of a template-id. In fact, looking at this code:
10283
10284    a.operator K<int>()
10285
10286    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10287    It is impossible to call a templated conversion-function-id with an
10288    explicit argument list, since the only allowed template parameter is
10289    the type to which it is converting.
10290
10291    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10292    `template' keyword, in a construction like:
10293
10294      T::template f<3>()
10295
10296    In that case `f' is taken to be a template-name, even though there
10297    is no way of knowing for sure.
10298
10299    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10300    name refers to a set of overloaded functions, at least one of which
10301    is a template, or an IDENTIFIER_NODE with the name of the template,
10302    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10303    names are looked up inside uninstantiated templates.  */
10304
10305 static tree
10306 cp_parser_template_name (cp_parser* parser,
10307                          bool template_keyword_p,
10308                          bool check_dependency_p,
10309                          bool is_declaration,
10310                          bool *is_identifier)
10311 {
10312   tree identifier;
10313   tree decl;
10314   tree fns;
10315   cp_token *token = cp_lexer_peek_token (parser->lexer);
10316
10317   /* If the next token is `operator', then we have either an
10318      operator-function-id or a conversion-function-id.  */
10319   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10320     {
10321       /* We don't know whether we're looking at an
10322          operator-function-id or a conversion-function-id.  */
10323       cp_parser_parse_tentatively (parser);
10324       /* Try an operator-function-id.  */
10325       identifier = cp_parser_operator_function_id (parser);
10326       /* If that didn't work, try a conversion-function-id.  */
10327       if (!cp_parser_parse_definitely (parser))
10328         {
10329           cp_parser_error (parser, "expected template-name");
10330           return error_mark_node;
10331         }
10332     }
10333   /* Look for the identifier.  */
10334   else
10335     identifier = cp_parser_identifier (parser);
10336
10337   /* If we didn't find an identifier, we don't have a template-id.  */
10338   if (identifier == error_mark_node)
10339     return error_mark_node;
10340
10341   /* If the name immediately followed the `template' keyword, then it
10342      is a template-name.  However, if the next token is not `<', then
10343      we do not treat it as a template-name, since it is not being used
10344      as part of a template-id.  This enables us to handle constructs
10345      like:
10346
10347        template <typename T> struct S { S(); };
10348        template <typename T> S<T>::S();
10349
10350      correctly.  We would treat `S' as a template -- if it were `S<T>'
10351      -- but we do not if there is no `<'.  */
10352
10353   if (processing_template_decl
10354       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10355     {
10356       /* In a declaration, in a dependent context, we pretend that the
10357          "template" keyword was present in order to improve error
10358          recovery.  For example, given:
10359
10360            template <typename T> void f(T::X<int>);
10361
10362          we want to treat "X<int>" as a template-id.  */
10363       if (is_declaration
10364           && !template_keyword_p
10365           && parser->scope && TYPE_P (parser->scope)
10366           && check_dependency_p
10367           && dependent_scope_p (parser->scope)
10368           /* Do not do this for dtors (or ctors), since they never
10369              need the template keyword before their name.  */
10370           && !constructor_name_p (identifier, parser->scope))
10371         {
10372           cp_token_position start = 0;
10373
10374           /* Explain what went wrong.  */
10375           error ("%Hnon-template %qD used as template",
10376                  &token->location, identifier);
10377           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10378                   parser->scope, identifier);
10379           /* If parsing tentatively, find the location of the "<" token.  */
10380           if (cp_parser_simulate_error (parser))
10381             start = cp_lexer_token_position (parser->lexer, true);
10382           /* Parse the template arguments so that we can issue error
10383              messages about them.  */
10384           cp_lexer_consume_token (parser->lexer);
10385           cp_parser_enclosed_template_argument_list (parser);
10386           /* Skip tokens until we find a good place from which to
10387              continue parsing.  */
10388           cp_parser_skip_to_closing_parenthesis (parser,
10389                                                  /*recovering=*/true,
10390                                                  /*or_comma=*/true,
10391                                                  /*consume_paren=*/false);
10392           /* If parsing tentatively, permanently remove the
10393              template argument list.  That will prevent duplicate
10394              error messages from being issued about the missing
10395              "template" keyword.  */
10396           if (start)
10397             cp_lexer_purge_tokens_after (parser->lexer, start);
10398           if (is_identifier)
10399             *is_identifier = true;
10400           return identifier;
10401         }
10402
10403       /* If the "template" keyword is present, then there is generally
10404          no point in doing name-lookup, so we just return IDENTIFIER.
10405          But, if the qualifying scope is non-dependent then we can
10406          (and must) do name-lookup normally.  */
10407       if (template_keyword_p
10408           && (!parser->scope
10409               || (TYPE_P (parser->scope)
10410                   && dependent_type_p (parser->scope))))
10411         return identifier;
10412     }
10413
10414   /* Look up the name.  */
10415   decl = cp_parser_lookup_name (parser, identifier,
10416                                 none_type,
10417                                 /*is_template=*/false,
10418                                 /*is_namespace=*/false,
10419                                 check_dependency_p,
10420                                 /*ambiguous_decls=*/NULL,
10421                                 token->location);
10422   decl = maybe_get_template_decl_from_type_decl (decl);
10423
10424   /* If DECL is a template, then the name was a template-name.  */
10425   if (TREE_CODE (decl) == TEMPLATE_DECL)
10426     ;
10427   else
10428     {
10429       tree fn = NULL_TREE;
10430
10431       /* The standard does not explicitly indicate whether a name that
10432          names a set of overloaded declarations, some of which are
10433          templates, is a template-name.  However, such a name should
10434          be a template-name; otherwise, there is no way to form a
10435          template-id for the overloaded templates.  */
10436       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10437       if (TREE_CODE (fns) == OVERLOAD)
10438         for (fn = fns; fn; fn = OVL_NEXT (fn))
10439           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10440             break;
10441
10442       if (!fn)
10443         {
10444           /* The name does not name a template.  */
10445           cp_parser_error (parser, "expected template-name");
10446           return error_mark_node;
10447         }
10448     }
10449
10450   /* If DECL is dependent, and refers to a function, then just return
10451      its name; we will look it up again during template instantiation.  */
10452   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10453     {
10454       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10455       if (TYPE_P (scope) && dependent_type_p (scope))
10456         return identifier;
10457     }
10458
10459   return decl;
10460 }
10461
10462 /* Parse a template-argument-list.
10463
10464    template-argument-list:
10465      template-argument ... [opt]
10466      template-argument-list , template-argument ... [opt]
10467
10468    Returns a TREE_VEC containing the arguments.  */
10469
10470 static tree
10471 cp_parser_template_argument_list (cp_parser* parser)
10472 {
10473   tree fixed_args[10];
10474   unsigned n_args = 0;
10475   unsigned alloced = 10;
10476   tree *arg_ary = fixed_args;
10477   tree vec;
10478   bool saved_in_template_argument_list_p;
10479   bool saved_ice_p;
10480   bool saved_non_ice_p;
10481
10482   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10483   parser->in_template_argument_list_p = true;
10484   /* Even if the template-id appears in an integral
10485      constant-expression, the contents of the argument list do
10486      not.  */
10487   saved_ice_p = parser->integral_constant_expression_p;
10488   parser->integral_constant_expression_p = false;
10489   saved_non_ice_p = parser->non_integral_constant_expression_p;
10490   parser->non_integral_constant_expression_p = false;
10491   /* Parse the arguments.  */
10492   do
10493     {
10494       tree argument;
10495
10496       if (n_args)
10497         /* Consume the comma.  */
10498         cp_lexer_consume_token (parser->lexer);
10499
10500       /* Parse the template-argument.  */
10501       argument = cp_parser_template_argument (parser);
10502
10503       /* If the next token is an ellipsis, we're expanding a template
10504          argument pack. */
10505       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10506         {
10507           if (argument == error_mark_node)
10508             {
10509               cp_token *token = cp_lexer_peek_token (parser->lexer);
10510               error ("%Hexpected parameter pack before %<...%>",
10511                      &token->location);
10512             }
10513           /* Consume the `...' token. */
10514           cp_lexer_consume_token (parser->lexer);
10515
10516           /* Make the argument into a TYPE_PACK_EXPANSION or
10517              EXPR_PACK_EXPANSION. */
10518           argument = make_pack_expansion (argument);
10519         }
10520
10521       if (n_args == alloced)
10522         {
10523           alloced *= 2;
10524
10525           if (arg_ary == fixed_args)
10526             {
10527               arg_ary = XNEWVEC (tree, alloced);
10528               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10529             }
10530           else
10531             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10532         }
10533       arg_ary[n_args++] = argument;
10534     }
10535   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10536
10537   vec = make_tree_vec (n_args);
10538
10539   while (n_args--)
10540     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10541
10542   if (arg_ary != fixed_args)
10543     free (arg_ary);
10544   parser->non_integral_constant_expression_p = saved_non_ice_p;
10545   parser->integral_constant_expression_p = saved_ice_p;
10546   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10547   return vec;
10548 }
10549
10550 /* Parse a template-argument.
10551
10552    template-argument:
10553      assignment-expression
10554      type-id
10555      id-expression
10556
10557    The representation is that of an assignment-expression, type-id, or
10558    id-expression -- except that the qualified id-expression is
10559    evaluated, so that the value returned is either a DECL or an
10560    OVERLOAD.
10561
10562    Although the standard says "assignment-expression", it forbids
10563    throw-expressions or assignments in the template argument.
10564    Therefore, we use "conditional-expression" instead.  */
10565
10566 static tree
10567 cp_parser_template_argument (cp_parser* parser)
10568 {
10569   tree argument;
10570   bool template_p;
10571   bool address_p;
10572   bool maybe_type_id = false;
10573   cp_token *token = NULL, *argument_start_token = NULL;
10574   cp_id_kind idk;
10575
10576   /* There's really no way to know what we're looking at, so we just
10577      try each alternative in order.
10578
10579        [temp.arg]
10580
10581        In a template-argument, an ambiguity between a type-id and an
10582        expression is resolved to a type-id, regardless of the form of
10583        the corresponding template-parameter.
10584
10585      Therefore, we try a type-id first.  */
10586   cp_parser_parse_tentatively (parser);
10587   argument = cp_parser_template_type_arg (parser);
10588   /* If there was no error parsing the type-id but the next token is a
10589      '>>', our behavior depends on which dialect of C++ we're
10590      parsing. In C++98, we probably found a typo for '> >'. But there
10591      are type-id which are also valid expressions. For instance:
10592
10593      struct X { int operator >> (int); };
10594      template <int V> struct Foo {};
10595      Foo<X () >> 5> r;
10596
10597      Here 'X()' is a valid type-id of a function type, but the user just
10598      wanted to write the expression "X() >> 5". Thus, we remember that we
10599      found a valid type-id, but we still try to parse the argument as an
10600      expression to see what happens. 
10601
10602      In C++0x, the '>>' will be considered two separate '>'
10603      tokens.  */
10604   if (!cp_parser_error_occurred (parser)
10605       && cxx_dialect == cxx98
10606       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10607     {
10608       maybe_type_id = true;
10609       cp_parser_abort_tentative_parse (parser);
10610     }
10611   else
10612     {
10613       /* If the next token isn't a `,' or a `>', then this argument wasn't
10614       really finished. This means that the argument is not a valid
10615       type-id.  */
10616       if (!cp_parser_next_token_ends_template_argument_p (parser))
10617         cp_parser_error (parser, "expected template-argument");
10618       /* If that worked, we're done.  */
10619       if (cp_parser_parse_definitely (parser))
10620         return argument;
10621     }
10622   /* We're still not sure what the argument will be.  */
10623   cp_parser_parse_tentatively (parser);
10624   /* Try a template.  */
10625   argument_start_token = cp_lexer_peek_token (parser->lexer);
10626   argument = cp_parser_id_expression (parser,
10627                                       /*template_keyword_p=*/false,
10628                                       /*check_dependency_p=*/true,
10629                                       &template_p,
10630                                       /*declarator_p=*/false,
10631                                       /*optional_p=*/false);
10632   /* If the next token isn't a `,' or a `>', then this argument wasn't
10633      really finished.  */
10634   if (!cp_parser_next_token_ends_template_argument_p (parser))
10635     cp_parser_error (parser, "expected template-argument");
10636   if (!cp_parser_error_occurred (parser))
10637     {
10638       /* Figure out what is being referred to.  If the id-expression
10639          was for a class template specialization, then we will have a
10640          TYPE_DECL at this point.  There is no need to do name lookup
10641          at this point in that case.  */
10642       if (TREE_CODE (argument) != TYPE_DECL)
10643         argument = cp_parser_lookup_name (parser, argument,
10644                                           none_type,
10645                                           /*is_template=*/template_p,
10646                                           /*is_namespace=*/false,
10647                                           /*check_dependency=*/true,
10648                                           /*ambiguous_decls=*/NULL,
10649                                           argument_start_token->location);
10650       if (TREE_CODE (argument) != TEMPLATE_DECL
10651           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10652         cp_parser_error (parser, "expected template-name");
10653     }
10654   if (cp_parser_parse_definitely (parser))
10655     return argument;
10656   /* It must be a non-type argument.  There permitted cases are given
10657      in [temp.arg.nontype]:
10658
10659      -- an integral constant-expression of integral or enumeration
10660         type; or
10661
10662      -- the name of a non-type template-parameter; or
10663
10664      -- the name of an object or function with external linkage...
10665
10666      -- the address of an object or function with external linkage...
10667
10668      -- a pointer to member...  */
10669   /* Look for a non-type template parameter.  */
10670   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10671     {
10672       cp_parser_parse_tentatively (parser);
10673       argument = cp_parser_primary_expression (parser,
10674                                                /*address_p=*/false,
10675                                                /*cast_p=*/false,
10676                                                /*template_arg_p=*/true,
10677                                                &idk);
10678       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10679           || !cp_parser_next_token_ends_template_argument_p (parser))
10680         cp_parser_simulate_error (parser);
10681       if (cp_parser_parse_definitely (parser))
10682         return argument;
10683     }
10684
10685   /* If the next token is "&", the argument must be the address of an
10686      object or function with external linkage.  */
10687   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10688   if (address_p)
10689     cp_lexer_consume_token (parser->lexer);
10690   /* See if we might have an id-expression.  */
10691   token = cp_lexer_peek_token (parser->lexer);
10692   if (token->type == CPP_NAME
10693       || token->keyword == RID_OPERATOR
10694       || token->type == CPP_SCOPE
10695       || token->type == CPP_TEMPLATE_ID
10696       || token->type == CPP_NESTED_NAME_SPECIFIER)
10697     {
10698       cp_parser_parse_tentatively (parser);
10699       argument = cp_parser_primary_expression (parser,
10700                                                address_p,
10701                                                /*cast_p=*/false,
10702                                                /*template_arg_p=*/true,
10703                                                &idk);
10704       if (cp_parser_error_occurred (parser)
10705           || !cp_parser_next_token_ends_template_argument_p (parser))
10706         cp_parser_abort_tentative_parse (parser);
10707       else
10708         {
10709           tree probe;
10710
10711           if (TREE_CODE (argument) == INDIRECT_REF)
10712             {
10713               gcc_assert (REFERENCE_REF_P (argument));
10714               argument = TREE_OPERAND (argument, 0);
10715             }
10716
10717           /* If we're in a template, we represent a qualified-id referring
10718              to a static data member as a SCOPE_REF even if the scope isn't
10719              dependent so that we can check access control later.  */
10720           probe = argument;
10721           if (TREE_CODE (probe) == SCOPE_REF)
10722             probe = TREE_OPERAND (probe, 1);
10723           if (TREE_CODE (probe) == VAR_DECL)
10724             {
10725               /* A variable without external linkage might still be a
10726                  valid constant-expression, so no error is issued here
10727                  if the external-linkage check fails.  */
10728               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
10729                 cp_parser_simulate_error (parser);
10730             }
10731           else if (is_overloaded_fn (argument))
10732             /* All overloaded functions are allowed; if the external
10733                linkage test does not pass, an error will be issued
10734                later.  */
10735             ;
10736           else if (address_p
10737                    && (TREE_CODE (argument) == OFFSET_REF
10738                        || TREE_CODE (argument) == SCOPE_REF))
10739             /* A pointer-to-member.  */
10740             ;
10741           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10742             ;
10743           else
10744             cp_parser_simulate_error (parser);
10745
10746           if (cp_parser_parse_definitely (parser))
10747             {
10748               if (address_p)
10749                 argument = build_x_unary_op (ADDR_EXPR, argument,
10750                                              tf_warning_or_error);
10751               return argument;
10752             }
10753         }
10754     }
10755   /* If the argument started with "&", there are no other valid
10756      alternatives at this point.  */
10757   if (address_p)
10758     {
10759       cp_parser_error (parser, "invalid non-type template argument");
10760       return error_mark_node;
10761     }
10762
10763   /* If the argument wasn't successfully parsed as a type-id followed
10764      by '>>', the argument can only be a constant expression now.
10765      Otherwise, we try parsing the constant-expression tentatively,
10766      because the argument could really be a type-id.  */
10767   if (maybe_type_id)
10768     cp_parser_parse_tentatively (parser);
10769   argument = cp_parser_constant_expression (parser,
10770                                             /*allow_non_constant_p=*/false,
10771                                             /*non_constant_p=*/NULL);
10772   argument = fold_non_dependent_expr (argument);
10773   if (!maybe_type_id)
10774     return argument;
10775   if (!cp_parser_next_token_ends_template_argument_p (parser))
10776     cp_parser_error (parser, "expected template-argument");
10777   if (cp_parser_parse_definitely (parser))
10778     return argument;
10779   /* We did our best to parse the argument as a non type-id, but that
10780      was the only alternative that matched (albeit with a '>' after
10781      it). We can assume it's just a typo from the user, and a
10782      diagnostic will then be issued.  */
10783   return cp_parser_template_type_arg (parser);
10784 }
10785
10786 /* Parse an explicit-instantiation.
10787
10788    explicit-instantiation:
10789      template declaration
10790
10791    Although the standard says `declaration', what it really means is:
10792
10793    explicit-instantiation:
10794      template decl-specifier-seq [opt] declarator [opt] ;
10795
10796    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10797    supposed to be allowed.  A defect report has been filed about this
10798    issue.
10799
10800    GNU Extension:
10801
10802    explicit-instantiation:
10803      storage-class-specifier template
10804        decl-specifier-seq [opt] declarator [opt] ;
10805      function-specifier template
10806        decl-specifier-seq [opt] declarator [opt] ;  */
10807
10808 static void
10809 cp_parser_explicit_instantiation (cp_parser* parser)
10810 {
10811   int declares_class_or_enum;
10812   cp_decl_specifier_seq decl_specifiers;
10813   tree extension_specifier = NULL_TREE;
10814   cp_token *token;
10815
10816   /* Look for an (optional) storage-class-specifier or
10817      function-specifier.  */
10818   if (cp_parser_allow_gnu_extensions_p (parser))
10819     {
10820       extension_specifier
10821         = cp_parser_storage_class_specifier_opt (parser);
10822       if (!extension_specifier)
10823         extension_specifier
10824           = cp_parser_function_specifier_opt (parser,
10825                                               /*decl_specs=*/NULL);
10826     }
10827
10828   /* Look for the `template' keyword.  */
10829   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10830   /* Let the front end know that we are processing an explicit
10831      instantiation.  */
10832   begin_explicit_instantiation ();
10833   /* [temp.explicit] says that we are supposed to ignore access
10834      control while processing explicit instantiation directives.  */
10835   push_deferring_access_checks (dk_no_check);
10836   /* Parse a decl-specifier-seq.  */
10837   token = cp_lexer_peek_token (parser->lexer);
10838   cp_parser_decl_specifier_seq (parser,
10839                                 CP_PARSER_FLAGS_OPTIONAL,
10840                                 &decl_specifiers,
10841                                 &declares_class_or_enum);
10842   /* If there was exactly one decl-specifier, and it declared a class,
10843      and there's no declarator, then we have an explicit type
10844      instantiation.  */
10845   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10846     {
10847       tree type;
10848
10849       type = check_tag_decl (&decl_specifiers);
10850       /* Turn access control back on for names used during
10851          template instantiation.  */
10852       pop_deferring_access_checks ();
10853       if (type)
10854         do_type_instantiation (type, extension_specifier,
10855                                /*complain=*/tf_error);
10856     }
10857   else
10858     {
10859       cp_declarator *declarator;
10860       tree decl;
10861
10862       /* Parse the declarator.  */
10863       declarator
10864         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10865                                 /*ctor_dtor_or_conv_p=*/NULL,
10866                                 /*parenthesized_p=*/NULL,
10867                                 /*member_p=*/false);
10868       if (declares_class_or_enum & 2)
10869         cp_parser_check_for_definition_in_return_type (declarator,
10870                                                        decl_specifiers.type,
10871                                                        decl_specifiers.type_location);
10872       if (declarator != cp_error_declarator)
10873         {
10874           decl = grokdeclarator (declarator, &decl_specifiers,
10875                                  NORMAL, 0, &decl_specifiers.attributes);
10876           /* Turn access control back on for names used during
10877              template instantiation.  */
10878           pop_deferring_access_checks ();
10879           /* Do the explicit instantiation.  */
10880           do_decl_instantiation (decl, extension_specifier);
10881         }
10882       else
10883         {
10884           pop_deferring_access_checks ();
10885           /* Skip the body of the explicit instantiation.  */
10886           cp_parser_skip_to_end_of_statement (parser);
10887         }
10888     }
10889   /* We're done with the instantiation.  */
10890   end_explicit_instantiation ();
10891
10892   cp_parser_consume_semicolon_at_end_of_statement (parser);
10893 }
10894
10895 /* Parse an explicit-specialization.
10896
10897    explicit-specialization:
10898      template < > declaration
10899
10900    Although the standard says `declaration', what it really means is:
10901
10902    explicit-specialization:
10903      template <> decl-specifier [opt] init-declarator [opt] ;
10904      template <> function-definition
10905      template <> explicit-specialization
10906      template <> template-declaration  */
10907
10908 static void
10909 cp_parser_explicit_specialization (cp_parser* parser)
10910 {
10911   bool need_lang_pop;
10912   cp_token *token = cp_lexer_peek_token (parser->lexer);
10913
10914   /* Look for the `template' keyword.  */
10915   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10916   /* Look for the `<'.  */
10917   cp_parser_require (parser, CPP_LESS, "%<<%>");
10918   /* Look for the `>'.  */
10919   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10920   /* We have processed another parameter list.  */
10921   ++parser->num_template_parameter_lists;
10922   /* [temp]
10923
10924      A template ... explicit specialization ... shall not have C
10925      linkage.  */
10926   if (current_lang_name == lang_name_c)
10927     {
10928       error ("%Htemplate specialization with C linkage", &token->location);
10929       /* Give it C++ linkage to avoid confusing other parts of the
10930          front end.  */
10931       push_lang_context (lang_name_cplusplus);
10932       need_lang_pop = true;
10933     }
10934   else
10935     need_lang_pop = false;
10936   /* Let the front end know that we are beginning a specialization.  */
10937   if (!begin_specialization ())
10938     {
10939       end_specialization ();
10940       return;
10941     }
10942
10943   /* If the next keyword is `template', we need to figure out whether
10944      or not we're looking a template-declaration.  */
10945   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10946     {
10947       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10948           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10949         cp_parser_template_declaration_after_export (parser,
10950                                                      /*member_p=*/false);
10951       else
10952         cp_parser_explicit_specialization (parser);
10953     }
10954   else
10955     /* Parse the dependent declaration.  */
10956     cp_parser_single_declaration (parser,
10957                                   /*checks=*/NULL,
10958                                   /*member_p=*/false,
10959                                   /*explicit_specialization_p=*/true,
10960                                   /*friend_p=*/NULL);
10961   /* We're done with the specialization.  */
10962   end_specialization ();
10963   /* For the erroneous case of a template with C linkage, we pushed an
10964      implicit C++ linkage scope; exit that scope now.  */
10965   if (need_lang_pop)
10966     pop_lang_context ();
10967   /* We're done with this parameter list.  */
10968   --parser->num_template_parameter_lists;
10969 }
10970
10971 /* Parse a type-specifier.
10972
10973    type-specifier:
10974      simple-type-specifier
10975      class-specifier
10976      enum-specifier
10977      elaborated-type-specifier
10978      cv-qualifier
10979
10980    GNU Extension:
10981
10982    type-specifier:
10983      __complex__
10984
10985    Returns a representation of the type-specifier.  For a
10986    class-specifier, enum-specifier, or elaborated-type-specifier, a
10987    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10988
10989    The parser flags FLAGS is used to control type-specifier parsing.
10990
10991    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10992    in a decl-specifier-seq.
10993
10994    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10995    class-specifier, enum-specifier, or elaborated-type-specifier, then
10996    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10997    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10998    zero.
10999
11000    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11001    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11002    is set to FALSE.  */
11003
11004 static tree
11005 cp_parser_type_specifier (cp_parser* parser,
11006                           cp_parser_flags flags,
11007                           cp_decl_specifier_seq *decl_specs,
11008                           bool is_declaration,
11009                           int* declares_class_or_enum,
11010                           bool* is_cv_qualifier)
11011 {
11012   tree type_spec = NULL_TREE;
11013   cp_token *token;
11014   enum rid keyword;
11015   cp_decl_spec ds = ds_last;
11016
11017   /* Assume this type-specifier does not declare a new type.  */
11018   if (declares_class_or_enum)
11019     *declares_class_or_enum = 0;
11020   /* And that it does not specify a cv-qualifier.  */
11021   if (is_cv_qualifier)
11022     *is_cv_qualifier = false;
11023   /* Peek at the next token.  */
11024   token = cp_lexer_peek_token (parser->lexer);
11025
11026   /* If we're looking at a keyword, we can use that to guide the
11027      production we choose.  */
11028   keyword = token->keyword;
11029   switch (keyword)
11030     {
11031     case RID_ENUM:
11032       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11033         goto elaborated_type_specifier;
11034
11035       /* Look for the enum-specifier.  */
11036       type_spec = cp_parser_enum_specifier (parser);
11037       /* If that worked, we're done.  */
11038       if (type_spec)
11039         {
11040           if (declares_class_or_enum)
11041             *declares_class_or_enum = 2;
11042           if (decl_specs)
11043             cp_parser_set_decl_spec_type (decl_specs,
11044                                           type_spec,
11045                                           token->location,
11046                                           /*user_defined_p=*/true);
11047           return type_spec;
11048         }
11049       else
11050         goto elaborated_type_specifier;
11051
11052       /* Any of these indicate either a class-specifier, or an
11053          elaborated-type-specifier.  */
11054     case RID_CLASS:
11055     case RID_STRUCT:
11056     case RID_UNION:
11057       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11058         goto elaborated_type_specifier;
11059
11060       /* Parse tentatively so that we can back up if we don't find a
11061          class-specifier.  */
11062       cp_parser_parse_tentatively (parser);
11063       /* Look for the class-specifier.  */
11064       type_spec = cp_parser_class_specifier (parser);
11065       /* If that worked, we're done.  */
11066       if (cp_parser_parse_definitely (parser))
11067         {
11068           if (declares_class_or_enum)
11069             *declares_class_or_enum = 2;
11070           if (decl_specs)
11071             cp_parser_set_decl_spec_type (decl_specs,
11072                                           type_spec,
11073                                           token->location,
11074                                           /*user_defined_p=*/true);
11075           return type_spec;
11076         }
11077
11078       /* Fall through.  */
11079     elaborated_type_specifier:
11080       /* We're declaring (not defining) a class or enum.  */
11081       if (declares_class_or_enum)
11082         *declares_class_or_enum = 1;
11083
11084       /* Fall through.  */
11085     case RID_TYPENAME:
11086       /* Look for an elaborated-type-specifier.  */
11087       type_spec
11088         = (cp_parser_elaborated_type_specifier
11089            (parser,
11090             decl_specs && decl_specs->specs[(int) ds_friend],
11091             is_declaration));
11092       if (decl_specs)
11093         cp_parser_set_decl_spec_type (decl_specs,
11094                                       type_spec,
11095                                       token->location,
11096                                       /*user_defined_p=*/true);
11097       return type_spec;
11098
11099     case RID_CONST:
11100       ds = ds_const;
11101       if (is_cv_qualifier)
11102         *is_cv_qualifier = true;
11103       break;
11104
11105     case RID_VOLATILE:
11106       ds = ds_volatile;
11107       if (is_cv_qualifier)
11108         *is_cv_qualifier = true;
11109       break;
11110
11111     case RID_RESTRICT:
11112       ds = ds_restrict;
11113       if (is_cv_qualifier)
11114         *is_cv_qualifier = true;
11115       break;
11116
11117     case RID_COMPLEX:
11118       /* The `__complex__' keyword is a GNU extension.  */
11119       ds = ds_complex;
11120       break;
11121
11122     default:
11123       break;
11124     }
11125
11126   /* Handle simple keywords.  */
11127   if (ds != ds_last)
11128     {
11129       if (decl_specs)
11130         {
11131           ++decl_specs->specs[(int)ds];
11132           decl_specs->any_specifiers_p = true;
11133         }
11134       return cp_lexer_consume_token (parser->lexer)->u.value;
11135     }
11136
11137   /* If we do not already have a type-specifier, assume we are looking
11138      at a simple-type-specifier.  */
11139   type_spec = cp_parser_simple_type_specifier (parser,
11140                                                decl_specs,
11141                                                flags);
11142
11143   /* If we didn't find a type-specifier, and a type-specifier was not
11144      optional in this context, issue an error message.  */
11145   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11146     {
11147       cp_parser_error (parser, "expected type specifier");
11148       return error_mark_node;
11149     }
11150
11151   return type_spec;
11152 }
11153
11154 /* Parse a simple-type-specifier.
11155
11156    simple-type-specifier:
11157      :: [opt] nested-name-specifier [opt] type-name
11158      :: [opt] nested-name-specifier template template-id
11159      char
11160      wchar_t
11161      bool
11162      short
11163      int
11164      long
11165      signed
11166      unsigned
11167      float
11168      double
11169      void
11170
11171    C++0x Extension:
11172
11173    simple-type-specifier:
11174      auto
11175      decltype ( expression )   
11176      char16_t
11177      char32_t
11178
11179    GNU Extension:
11180
11181    simple-type-specifier:
11182      __typeof__ unary-expression
11183      __typeof__ ( type-id )
11184
11185    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11186    appropriately updated.  */
11187
11188 static tree
11189 cp_parser_simple_type_specifier (cp_parser* parser,
11190                                  cp_decl_specifier_seq *decl_specs,
11191                                  cp_parser_flags flags)
11192 {
11193   tree type = NULL_TREE;
11194   cp_token *token;
11195
11196   /* Peek at the next token.  */
11197   token = cp_lexer_peek_token (parser->lexer);
11198
11199   /* If we're looking at a keyword, things are easy.  */
11200   switch (token->keyword)
11201     {
11202     case RID_CHAR:
11203       if (decl_specs)
11204         decl_specs->explicit_char_p = true;
11205       type = char_type_node;
11206       break;
11207     case RID_CHAR16:
11208       type = char16_type_node;
11209       break;
11210     case RID_CHAR32:
11211       type = char32_type_node;
11212       break;
11213     case RID_WCHAR:
11214       type = wchar_type_node;
11215       break;
11216     case RID_BOOL:
11217       type = boolean_type_node;
11218       break;
11219     case RID_SHORT:
11220       if (decl_specs)
11221         ++decl_specs->specs[(int) ds_short];
11222       type = short_integer_type_node;
11223       break;
11224     case RID_INT:
11225       if (decl_specs)
11226         decl_specs->explicit_int_p = true;
11227       type = integer_type_node;
11228       break;
11229     case RID_LONG:
11230       if (decl_specs)
11231         ++decl_specs->specs[(int) ds_long];
11232       type = long_integer_type_node;
11233       break;
11234     case RID_SIGNED:
11235       if (decl_specs)
11236         ++decl_specs->specs[(int) ds_signed];
11237       type = integer_type_node;
11238       break;
11239     case RID_UNSIGNED:
11240       if (decl_specs)
11241         ++decl_specs->specs[(int) ds_unsigned];
11242       type = unsigned_type_node;
11243       break;
11244     case RID_FLOAT:
11245       type = float_type_node;
11246       break;
11247     case RID_DOUBLE:
11248       type = double_type_node;
11249       break;
11250     case RID_VOID:
11251       type = void_type_node;
11252       break;
11253       
11254     case RID_AUTO:
11255       maybe_warn_cpp0x ("C++0x auto");
11256       type = make_auto ();
11257       break;
11258
11259     case RID_DECLTYPE:
11260       /* Parse the `decltype' type.  */
11261       type = cp_parser_decltype (parser);
11262
11263       if (decl_specs)
11264         cp_parser_set_decl_spec_type (decl_specs, type,
11265                                       token->location,
11266                                       /*user_defined_p=*/true);
11267
11268       return type;
11269
11270     case RID_TYPEOF:
11271       /* Consume the `typeof' token.  */
11272       cp_lexer_consume_token (parser->lexer);
11273       /* Parse the operand to `typeof'.  */
11274       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11275       /* If it is not already a TYPE, take its type.  */
11276       if (!TYPE_P (type))
11277         type = finish_typeof (type);
11278
11279       if (decl_specs)
11280         cp_parser_set_decl_spec_type (decl_specs, type,
11281                                       token->location,
11282                                       /*user_defined_p=*/true);
11283
11284       return type;
11285
11286     default:
11287       break;
11288     }
11289
11290   /* If the type-specifier was for a built-in type, we're done.  */
11291   if (type)
11292     {
11293       tree id;
11294
11295       /* Record the type.  */
11296       if (decl_specs
11297           && (token->keyword != RID_SIGNED
11298               && token->keyword != RID_UNSIGNED
11299               && token->keyword != RID_SHORT
11300               && token->keyword != RID_LONG))
11301         cp_parser_set_decl_spec_type (decl_specs,
11302                                       type,
11303                                       token->location,
11304                                       /*user_defined=*/false);
11305       if (decl_specs)
11306         decl_specs->any_specifiers_p = true;
11307
11308       /* Consume the token.  */
11309       id = cp_lexer_consume_token (parser->lexer)->u.value;
11310
11311       /* There is no valid C++ program where a non-template type is
11312          followed by a "<".  That usually indicates that the user thought
11313          that the type was a template.  */
11314       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11315
11316       return TYPE_NAME (type);
11317     }
11318
11319   /* The type-specifier must be a user-defined type.  */
11320   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11321     {
11322       bool qualified_p;
11323       bool global_p;
11324
11325       /* Don't gobble tokens or issue error messages if this is an
11326          optional type-specifier.  */
11327       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11328         cp_parser_parse_tentatively (parser);
11329
11330       /* Look for the optional `::' operator.  */
11331       global_p
11332         = (cp_parser_global_scope_opt (parser,
11333                                        /*current_scope_valid_p=*/false)
11334            != NULL_TREE);
11335       /* Look for the nested-name specifier.  */
11336       qualified_p
11337         = (cp_parser_nested_name_specifier_opt (parser,
11338                                                 /*typename_keyword_p=*/false,
11339                                                 /*check_dependency_p=*/true,
11340                                                 /*type_p=*/false,
11341                                                 /*is_declaration=*/false)
11342            != NULL_TREE);
11343       token = cp_lexer_peek_token (parser->lexer);
11344       /* If we have seen a nested-name-specifier, and the next token
11345          is `template', then we are using the template-id production.  */
11346       if (parser->scope
11347           && cp_parser_optional_template_keyword (parser))
11348         {
11349           /* Look for the template-id.  */
11350           type = cp_parser_template_id (parser,
11351                                         /*template_keyword_p=*/true,
11352                                         /*check_dependency_p=*/true,
11353                                         /*is_declaration=*/false);
11354           /* If the template-id did not name a type, we are out of
11355              luck.  */
11356           if (TREE_CODE (type) != TYPE_DECL)
11357             {
11358               cp_parser_error (parser, "expected template-id for type");
11359               type = NULL_TREE;
11360             }
11361         }
11362       /* Otherwise, look for a type-name.  */
11363       else
11364         type = cp_parser_type_name (parser);
11365       /* Keep track of all name-lookups performed in class scopes.  */
11366       if (type
11367           && !global_p
11368           && !qualified_p
11369           && TREE_CODE (type) == TYPE_DECL
11370           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11371         maybe_note_name_used_in_class (DECL_NAME (type), type);
11372       /* If it didn't work out, we don't have a TYPE.  */
11373       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11374           && !cp_parser_parse_definitely (parser))
11375         type = NULL_TREE;
11376       if (type && decl_specs)
11377         cp_parser_set_decl_spec_type (decl_specs, type,
11378                                       token->location,
11379                                       /*user_defined=*/true);
11380     }
11381
11382   /* If we didn't get a type-name, issue an error message.  */
11383   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11384     {
11385       cp_parser_error (parser, "expected type-name");
11386       return error_mark_node;
11387     }
11388
11389   /* There is no valid C++ program where a non-template type is
11390      followed by a "<".  That usually indicates that the user thought
11391      that the type was a template.  */
11392   if (type && type != error_mark_node)
11393     {
11394       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11395          If it is, then the '<'...'>' enclose protocol names rather than
11396          template arguments, and so everything is fine.  */
11397       if (c_dialect_objc ()
11398           && (objc_is_id (type) || objc_is_class_name (type)))
11399         {
11400           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11401           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11402
11403           /* Clobber the "unqualified" type previously entered into
11404              DECL_SPECS with the new, improved protocol-qualified version.  */
11405           if (decl_specs)
11406             decl_specs->type = qual_type;
11407
11408           return qual_type;
11409         }
11410
11411       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11412                                                token->location);
11413     }
11414
11415   return type;
11416 }
11417
11418 /* Parse a type-name.
11419
11420    type-name:
11421      class-name
11422      enum-name
11423      typedef-name
11424
11425    enum-name:
11426      identifier
11427
11428    typedef-name:
11429      identifier
11430
11431    Returns a TYPE_DECL for the type.  */
11432
11433 static tree
11434 cp_parser_type_name (cp_parser* parser)
11435 {
11436   tree type_decl;
11437
11438   /* We can't know yet whether it is a class-name or not.  */
11439   cp_parser_parse_tentatively (parser);
11440   /* Try a class-name.  */
11441   type_decl = cp_parser_class_name (parser,
11442                                     /*typename_keyword_p=*/false,
11443                                     /*template_keyword_p=*/false,
11444                                     none_type,
11445                                     /*check_dependency_p=*/true,
11446                                     /*class_head_p=*/false,
11447                                     /*is_declaration=*/false);
11448   /* If it's not a class-name, keep looking.  */
11449   if (!cp_parser_parse_definitely (parser))
11450     {
11451       /* It must be a typedef-name or an enum-name.  */
11452       return cp_parser_nonclass_name (parser);
11453     }
11454
11455   return type_decl;
11456 }
11457
11458 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11459
11460    enum-name:
11461      identifier
11462
11463    typedef-name:
11464      identifier
11465
11466    Returns a TYPE_DECL for the type.  */
11467
11468 static tree
11469 cp_parser_nonclass_name (cp_parser* parser)
11470 {
11471   tree type_decl;
11472   tree identifier;
11473
11474   cp_token *token = cp_lexer_peek_token (parser->lexer);
11475   identifier = cp_parser_identifier (parser);
11476   if (identifier == error_mark_node)
11477     return error_mark_node;
11478
11479   /* Look up the type-name.  */
11480   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11481
11482   if (TREE_CODE (type_decl) != TYPE_DECL
11483       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11484     {
11485       /* See if this is an Objective-C type.  */
11486       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11487       tree type = objc_get_protocol_qualified_type (identifier, protos);
11488       if (type)
11489         type_decl = TYPE_NAME (type);
11490     }
11491   
11492   /* Issue an error if we did not find a type-name.  */
11493   if (TREE_CODE (type_decl) != TYPE_DECL)
11494     {
11495       if (!cp_parser_simulate_error (parser))
11496         cp_parser_name_lookup_error (parser, identifier, type_decl,
11497                                      "is not a type", token->location);
11498       return error_mark_node;
11499     }
11500   /* Remember that the name was used in the definition of the
11501      current class so that we can check later to see if the
11502      meaning would have been different after the class was
11503      entirely defined.  */
11504   else if (type_decl != error_mark_node
11505            && !parser->scope)
11506     maybe_note_name_used_in_class (identifier, type_decl);
11507   
11508   return type_decl;
11509 }
11510
11511 /* Parse an elaborated-type-specifier.  Note that the grammar given
11512    here incorporates the resolution to DR68.
11513
11514    elaborated-type-specifier:
11515      class-key :: [opt] nested-name-specifier [opt] identifier
11516      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11517      enum-key :: [opt] nested-name-specifier [opt] identifier
11518      typename :: [opt] nested-name-specifier identifier
11519      typename :: [opt] nested-name-specifier template [opt]
11520        template-id
11521
11522    GNU extension:
11523
11524    elaborated-type-specifier:
11525      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11526      class-key attributes :: [opt] nested-name-specifier [opt]
11527                template [opt] template-id
11528      enum attributes :: [opt] nested-name-specifier [opt] identifier
11529
11530    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11531    declared `friend'.  If IS_DECLARATION is TRUE, then this
11532    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11533    something is being declared.
11534
11535    Returns the TYPE specified.  */
11536
11537 static tree
11538 cp_parser_elaborated_type_specifier (cp_parser* parser,
11539                                      bool is_friend,
11540                                      bool is_declaration)
11541 {
11542   enum tag_types tag_type;
11543   tree identifier;
11544   tree type = NULL_TREE;
11545   tree attributes = NULL_TREE;
11546   cp_token *token = NULL;
11547
11548   /* See if we're looking at the `enum' keyword.  */
11549   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11550     {
11551       /* Consume the `enum' token.  */
11552       cp_lexer_consume_token (parser->lexer);
11553       /* Remember that it's an enumeration type.  */
11554       tag_type = enum_type;
11555       /* Parse the optional `struct' or `class' key (for C++0x scoped
11556          enums).  */
11557       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11558           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11559         {
11560           if (cxx_dialect == cxx98)
11561             maybe_warn_cpp0x ("scoped enums");
11562
11563           /* Consume the `struct' or `class'.  */
11564           cp_lexer_consume_token (parser->lexer);
11565         }
11566       /* Parse the attributes.  */
11567       attributes = cp_parser_attributes_opt (parser);
11568     }
11569   /* Or, it might be `typename'.  */
11570   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11571                                            RID_TYPENAME))
11572     {
11573       /* Consume the `typename' token.  */
11574       cp_lexer_consume_token (parser->lexer);
11575       /* Remember that it's a `typename' type.  */
11576       tag_type = typename_type;
11577       /* The `typename' keyword is only allowed in templates.  */
11578       if (!processing_template_decl)
11579         permerror (input_location, "using %<typename%> outside of template");
11580     }
11581   /* Otherwise it must be a class-key.  */
11582   else
11583     {
11584       tag_type = cp_parser_class_key (parser);
11585       if (tag_type == none_type)
11586         return error_mark_node;
11587       /* Parse the attributes.  */
11588       attributes = cp_parser_attributes_opt (parser);
11589     }
11590
11591   /* Look for the `::' operator.  */
11592   cp_parser_global_scope_opt (parser,
11593                               /*current_scope_valid_p=*/false);
11594   /* Look for the nested-name-specifier.  */
11595   if (tag_type == typename_type)
11596     {
11597       if (!cp_parser_nested_name_specifier (parser,
11598                                            /*typename_keyword_p=*/true,
11599                                            /*check_dependency_p=*/true,
11600                                            /*type_p=*/true,
11601                                             is_declaration))
11602         return error_mark_node;
11603     }
11604   else
11605     /* Even though `typename' is not present, the proposed resolution
11606        to Core Issue 180 says that in `class A<T>::B', `B' should be
11607        considered a type-name, even if `A<T>' is dependent.  */
11608     cp_parser_nested_name_specifier_opt (parser,
11609                                          /*typename_keyword_p=*/true,
11610                                          /*check_dependency_p=*/true,
11611                                          /*type_p=*/true,
11612                                          is_declaration);
11613  /* For everything but enumeration types, consider a template-id.
11614     For an enumeration type, consider only a plain identifier.  */
11615   if (tag_type != enum_type)
11616     {
11617       bool template_p = false;
11618       tree decl;
11619
11620       /* Allow the `template' keyword.  */
11621       template_p = cp_parser_optional_template_keyword (parser);
11622       /* If we didn't see `template', we don't know if there's a
11623          template-id or not.  */
11624       if (!template_p)
11625         cp_parser_parse_tentatively (parser);
11626       /* Parse the template-id.  */
11627       token = cp_lexer_peek_token (parser->lexer);
11628       decl = cp_parser_template_id (parser, template_p,
11629                                     /*check_dependency_p=*/true,
11630                                     is_declaration);
11631       /* If we didn't find a template-id, look for an ordinary
11632          identifier.  */
11633       if (!template_p && !cp_parser_parse_definitely (parser))
11634         ;
11635       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11636          in effect, then we must assume that, upon instantiation, the
11637          template will correspond to a class.  */
11638       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11639                && tag_type == typename_type)
11640         type = make_typename_type (parser->scope, decl,
11641                                    typename_type,
11642                                    /*complain=*/tf_error);
11643       /* If the `typename' keyword is in effect and DECL is not a type
11644          decl. Then type is non existant.   */
11645       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11646         type = NULL_TREE; 
11647       else 
11648         type = TREE_TYPE (decl);
11649     }
11650
11651   if (!type)
11652     {
11653       token = cp_lexer_peek_token (parser->lexer);
11654       identifier = cp_parser_identifier (parser);
11655
11656       if (identifier == error_mark_node)
11657         {
11658           parser->scope = NULL_TREE;
11659           return error_mark_node;
11660         }
11661
11662       /* For a `typename', we needn't call xref_tag.  */
11663       if (tag_type == typename_type
11664           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11665         return cp_parser_make_typename_type (parser, parser->scope,
11666                                              identifier,
11667                                              token->location);
11668       /* Look up a qualified name in the usual way.  */
11669       if (parser->scope)
11670         {
11671           tree decl;
11672           tree ambiguous_decls;
11673
11674           decl = cp_parser_lookup_name (parser, identifier,
11675                                         tag_type,
11676                                         /*is_template=*/false,
11677                                         /*is_namespace=*/false,
11678                                         /*check_dependency=*/true,
11679                                         &ambiguous_decls,
11680                                         token->location);
11681
11682           /* If the lookup was ambiguous, an error will already have been
11683              issued.  */
11684           if (ambiguous_decls)
11685             return error_mark_node;
11686
11687           /* If we are parsing friend declaration, DECL may be a
11688              TEMPLATE_DECL tree node here.  However, we need to check
11689              whether this TEMPLATE_DECL results in valid code.  Consider
11690              the following example:
11691
11692                namespace N {
11693                  template <class T> class C {};
11694                }
11695                class X {
11696                  template <class T> friend class N::C; // #1, valid code
11697                };
11698                template <class T> class Y {
11699                  friend class N::C;                    // #2, invalid code
11700                };
11701
11702              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11703              name lookup of `N::C'.  We see that friend declaration must
11704              be template for the code to be valid.  Note that
11705              processing_template_decl does not work here since it is
11706              always 1 for the above two cases.  */
11707
11708           decl = (cp_parser_maybe_treat_template_as_class
11709                   (decl, /*tag_name_p=*/is_friend
11710                          && parser->num_template_parameter_lists));
11711
11712           if (TREE_CODE (decl) != TYPE_DECL)
11713             {
11714               cp_parser_diagnose_invalid_type_name (parser,
11715                                                     parser->scope,
11716                                                     identifier,
11717                                                     token->location);
11718               return error_mark_node;
11719             }
11720
11721           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11722             {
11723               bool allow_template = (parser->num_template_parameter_lists
11724                                       || DECL_SELF_REFERENCE_P (decl));
11725               type = check_elaborated_type_specifier (tag_type, decl, 
11726                                                       allow_template);
11727
11728               if (type == error_mark_node)
11729                 return error_mark_node;
11730             }
11731
11732           /* Forward declarations of nested types, such as
11733
11734                class C1::C2;
11735                class C1::C2::C3;
11736
11737              are invalid unless all components preceding the final '::'
11738              are complete.  If all enclosing types are complete, these
11739              declarations become merely pointless.
11740
11741              Invalid forward declarations of nested types are errors
11742              caught elsewhere in parsing.  Those that are pointless arrive
11743              here.  */
11744
11745           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11746               && !is_friend && !processing_explicit_instantiation)
11747             warning (0, "declaration %qD does not declare anything", decl);
11748
11749           type = TREE_TYPE (decl);
11750         }
11751       else
11752         {
11753           /* An elaborated-type-specifier sometimes introduces a new type and
11754              sometimes names an existing type.  Normally, the rule is that it
11755              introduces a new type only if there is not an existing type of
11756              the same name already in scope.  For example, given:
11757
11758                struct S {};
11759                void f() { struct S s; }
11760
11761              the `struct S' in the body of `f' is the same `struct S' as in
11762              the global scope; the existing definition is used.  However, if
11763              there were no global declaration, this would introduce a new
11764              local class named `S'.
11765
11766              An exception to this rule applies to the following code:
11767
11768                namespace N { struct S; }
11769
11770              Here, the elaborated-type-specifier names a new type
11771              unconditionally; even if there is already an `S' in the
11772              containing scope this declaration names a new type.
11773              This exception only applies if the elaborated-type-specifier
11774              forms the complete declaration:
11775
11776                [class.name]
11777
11778                A declaration consisting solely of `class-key identifier ;' is
11779                either a redeclaration of the name in the current scope or a
11780                forward declaration of the identifier as a class name.  It
11781                introduces the name into the current scope.
11782
11783              We are in this situation precisely when the next token is a `;'.
11784
11785              An exception to the exception is that a `friend' declaration does
11786              *not* name a new type; i.e., given:
11787
11788                struct S { friend struct T; };
11789
11790              `T' is not a new type in the scope of `S'.
11791
11792              Also, `new struct S' or `sizeof (struct S)' never results in the
11793              definition of a new type; a new type can only be declared in a
11794              declaration context.  */
11795
11796           tag_scope ts;
11797           bool template_p;
11798
11799           if (is_friend)
11800             /* Friends have special name lookup rules.  */
11801             ts = ts_within_enclosing_non_class;
11802           else if (is_declaration
11803                    && cp_lexer_next_token_is (parser->lexer,
11804                                               CPP_SEMICOLON))
11805             /* This is a `class-key identifier ;' */
11806             ts = ts_current;
11807           else
11808             ts = ts_global;
11809
11810           template_p =
11811             (parser->num_template_parameter_lists
11812              && (cp_parser_next_token_starts_class_definition_p (parser)
11813                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11814           /* An unqualified name was used to reference this type, so
11815              there were no qualifying templates.  */
11816           if (!cp_parser_check_template_parameters (parser,
11817                                                     /*num_templates=*/0,
11818                                                     token->location))
11819             return error_mark_node;
11820           type = xref_tag (tag_type, identifier, ts, template_p);
11821         }
11822     }
11823
11824   if (type == error_mark_node)
11825     return error_mark_node;
11826
11827   /* Allow attributes on forward declarations of classes.  */
11828   if (attributes)
11829     {
11830       if (TREE_CODE (type) == TYPENAME_TYPE)
11831         warning (OPT_Wattributes,
11832                  "attributes ignored on uninstantiated type");
11833       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11834                && ! processing_explicit_instantiation)
11835         warning (OPT_Wattributes,
11836                  "attributes ignored on template instantiation");
11837       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11838         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11839       else
11840         warning (OPT_Wattributes,
11841                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11842     }
11843
11844   if (tag_type != enum_type)
11845     cp_parser_check_class_key (tag_type, type);
11846
11847   /* A "<" cannot follow an elaborated type specifier.  If that
11848      happens, the user was probably trying to form a template-id.  */
11849   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11850
11851   return type;
11852 }
11853
11854 /* Parse an enum-specifier.
11855
11856    enum-specifier:
11857      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11858
11859    enum-key:
11860      enum
11861      enum class   [C++0x]
11862      enum struct  [C++0x]
11863
11864    enum-base:   [C++0x]
11865      : type-specifier-seq
11866
11867    GNU Extensions:
11868      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11869        { enumerator-list [opt] }attributes[opt]
11870
11871    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11872    if the token stream isn't an enum-specifier after all.  */
11873
11874 static tree
11875 cp_parser_enum_specifier (cp_parser* parser)
11876 {
11877   tree identifier;
11878   tree type;
11879   tree attributes;
11880   bool scoped_enum_p = false;
11881   bool has_underlying_type = false;
11882   tree underlying_type = NULL_TREE;
11883
11884   /* Parse tentatively so that we can back up if we don't find a
11885      enum-specifier.  */
11886   cp_parser_parse_tentatively (parser);
11887
11888   /* Caller guarantees that the current token is 'enum', an identifier
11889      possibly follows, and the token after that is an opening brace.
11890      If we don't have an identifier, fabricate an anonymous name for
11891      the enumeration being defined.  */
11892   cp_lexer_consume_token (parser->lexer);
11893
11894   /* Parse the "class" or "struct", which indicates a scoped
11895      enumeration type in C++0x.  */
11896   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11897       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11898     {
11899       if (cxx_dialect == cxx98)
11900         maybe_warn_cpp0x ("scoped enums");
11901
11902       /* Consume the `struct' or `class' token.  */
11903       cp_lexer_consume_token (parser->lexer);
11904
11905       scoped_enum_p = true;
11906     }
11907
11908   attributes = cp_parser_attributes_opt (parser);
11909
11910   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11911     identifier = cp_parser_identifier (parser);
11912   else
11913     identifier = make_anon_name ();
11914
11915   /* Check for the `:' that denotes a specified underlying type in C++0x.
11916      Note that a ':' could also indicate a bitfield width, however.  */
11917   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11918     {
11919       cp_decl_specifier_seq type_specifiers;
11920
11921       /* Consume the `:'.  */
11922       cp_lexer_consume_token (parser->lexer);
11923
11924       /* Parse the type-specifier-seq.  */
11925       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11926                                     /*is_trailing_return=*/false,
11927                                     &type_specifiers);
11928
11929       /* At this point this is surely not elaborated type specifier.  */
11930       if (!cp_parser_parse_definitely (parser))
11931         return NULL_TREE;
11932
11933       if (cxx_dialect == cxx98)
11934         maybe_warn_cpp0x ("scoped enums");
11935
11936       has_underlying_type = true;
11937
11938       /* If that didn't work, stop.  */
11939       if (type_specifiers.type != error_mark_node)
11940         {
11941           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11942                                             /*initialized=*/0, NULL);
11943           if (underlying_type == error_mark_node)
11944             underlying_type = NULL_TREE;
11945         }
11946     }
11947
11948   /* Look for the `{' but don't consume it yet.  */
11949   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11950     {
11951       cp_parser_error (parser, "expected %<{%>");
11952       if (has_underlying_type)
11953         return NULL_TREE;
11954     }
11955
11956   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11957     return NULL_TREE;
11958
11959   /* Issue an error message if type-definitions are forbidden here.  */
11960   if (!cp_parser_check_type_definition (parser))
11961     type = error_mark_node;
11962   else
11963     /* Create the new type.  We do this before consuming the opening
11964        brace so the enum will be recorded as being on the line of its
11965        tag (or the 'enum' keyword, if there is no tag).  */
11966     type = start_enum (identifier, underlying_type, scoped_enum_p);
11967   
11968   /* Consume the opening brace.  */
11969   cp_lexer_consume_token (parser->lexer);
11970
11971   if (type == error_mark_node)
11972     {
11973       cp_parser_skip_to_end_of_block_or_statement (parser);
11974       return error_mark_node;
11975     }
11976
11977   /* If the next token is not '}', then there are some enumerators.  */
11978   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11979     cp_parser_enumerator_list (parser, type);
11980
11981   /* Consume the final '}'.  */
11982   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11983
11984   /* Look for trailing attributes to apply to this enumeration, and
11985      apply them if appropriate.  */
11986   if (cp_parser_allow_gnu_extensions_p (parser))
11987     {
11988       tree trailing_attr = cp_parser_attributes_opt (parser);
11989       trailing_attr = chainon (trailing_attr, attributes);
11990       cplus_decl_attributes (&type,
11991                              trailing_attr,
11992                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11993     }
11994
11995   /* Finish up the enumeration.  */
11996   finish_enum (type);
11997
11998   return type;
11999 }
12000
12001 /* Parse an enumerator-list.  The enumerators all have the indicated
12002    TYPE.
12003
12004    enumerator-list:
12005      enumerator-definition
12006      enumerator-list , enumerator-definition  */
12007
12008 static void
12009 cp_parser_enumerator_list (cp_parser* parser, tree type)
12010 {
12011   while (true)
12012     {
12013       /* Parse an enumerator-definition.  */
12014       cp_parser_enumerator_definition (parser, type);
12015
12016       /* If the next token is not a ',', we've reached the end of
12017          the list.  */
12018       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12019         break;
12020       /* Otherwise, consume the `,' and keep going.  */
12021       cp_lexer_consume_token (parser->lexer);
12022       /* If the next token is a `}', there is a trailing comma.  */
12023       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12024         {
12025           if (!in_system_header)
12026             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12027           break;
12028         }
12029     }
12030 }
12031
12032 /* Parse an enumerator-definition.  The enumerator has the indicated
12033    TYPE.
12034
12035    enumerator-definition:
12036      enumerator
12037      enumerator = constant-expression
12038
12039    enumerator:
12040      identifier  */
12041
12042 static void
12043 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12044 {
12045   tree identifier;
12046   tree value;
12047
12048   /* Look for the identifier.  */
12049   identifier = cp_parser_identifier (parser);
12050   if (identifier == error_mark_node)
12051     return;
12052
12053   /* If the next token is an '=', then there is an explicit value.  */
12054   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12055     {
12056       /* Consume the `=' token.  */
12057       cp_lexer_consume_token (parser->lexer);
12058       /* Parse the value.  */
12059       value = cp_parser_constant_expression (parser,
12060                                              /*allow_non_constant_p=*/false,
12061                                              NULL);
12062     }
12063   else
12064     value = NULL_TREE;
12065
12066   /* If we are processing a template, make sure the initializer of the
12067      enumerator doesn't contain any bare template parameter pack.  */
12068   if (check_for_bare_parameter_packs (value))
12069     value = error_mark_node;
12070
12071   /* Create the enumerator.  */
12072   build_enumerator (identifier, value, type);
12073 }
12074
12075 /* Parse a namespace-name.
12076
12077    namespace-name:
12078      original-namespace-name
12079      namespace-alias
12080
12081    Returns the NAMESPACE_DECL for the namespace.  */
12082
12083 static tree
12084 cp_parser_namespace_name (cp_parser* parser)
12085 {
12086   tree identifier;
12087   tree namespace_decl;
12088
12089   cp_token *token = cp_lexer_peek_token (parser->lexer);
12090
12091   /* Get the name of the namespace.  */
12092   identifier = cp_parser_identifier (parser);
12093   if (identifier == error_mark_node)
12094     return error_mark_node;
12095
12096   /* Look up the identifier in the currently active scope.  Look only
12097      for namespaces, due to:
12098
12099        [basic.lookup.udir]
12100
12101        When looking up a namespace-name in a using-directive or alias
12102        definition, only namespace names are considered.
12103
12104      And:
12105
12106        [basic.lookup.qual]
12107
12108        During the lookup of a name preceding the :: scope resolution
12109        operator, object, function, and enumerator names are ignored.
12110
12111      (Note that cp_parser_qualifying_entity only calls this
12112      function if the token after the name is the scope resolution
12113      operator.)  */
12114   namespace_decl = cp_parser_lookup_name (parser, identifier,
12115                                           none_type,
12116                                           /*is_template=*/false,
12117                                           /*is_namespace=*/true,
12118                                           /*check_dependency=*/true,
12119                                           /*ambiguous_decls=*/NULL,
12120                                           token->location);
12121   /* If it's not a namespace, issue an error.  */
12122   if (namespace_decl == error_mark_node
12123       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12124     {
12125       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12126         error ("%H%qD is not a namespace-name", &token->location, identifier);
12127       cp_parser_error (parser, "expected namespace-name");
12128       namespace_decl = error_mark_node;
12129     }
12130
12131   return namespace_decl;
12132 }
12133
12134 /* Parse a namespace-definition.
12135
12136    namespace-definition:
12137      named-namespace-definition
12138      unnamed-namespace-definition
12139
12140    named-namespace-definition:
12141      original-namespace-definition
12142      extension-namespace-definition
12143
12144    original-namespace-definition:
12145      namespace identifier { namespace-body }
12146
12147    extension-namespace-definition:
12148      namespace original-namespace-name { namespace-body }
12149
12150    unnamed-namespace-definition:
12151      namespace { namespace-body } */
12152
12153 static void
12154 cp_parser_namespace_definition (cp_parser* parser)
12155 {
12156   tree identifier, attribs;
12157   bool has_visibility;
12158   bool is_inline;
12159
12160   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12161     {
12162       is_inline = true;
12163       cp_lexer_consume_token (parser->lexer);
12164     }
12165   else
12166     is_inline = false;
12167
12168   /* Look for the `namespace' keyword.  */
12169   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12170
12171   /* Get the name of the namespace.  We do not attempt to distinguish
12172      between an original-namespace-definition and an
12173      extension-namespace-definition at this point.  The semantic
12174      analysis routines are responsible for that.  */
12175   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12176     identifier = cp_parser_identifier (parser);
12177   else
12178     identifier = NULL_TREE;
12179
12180   /* Parse any specified attributes.  */
12181   attribs = cp_parser_attributes_opt (parser);
12182
12183   /* Look for the `{' to start the namespace.  */
12184   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12185   /* Start the namespace.  */
12186   push_namespace (identifier);
12187
12188   /* "inline namespace" is equivalent to a stub namespace definition
12189      followed by a strong using directive.  */
12190   if (is_inline)
12191     {
12192       tree name_space = current_namespace;
12193       /* Set up namespace association.  */
12194       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12195         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12196                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12197       /* Import the contents of the inline namespace.  */
12198       pop_namespace ();
12199       do_using_directive (name_space);
12200       push_namespace (identifier);
12201     }
12202
12203   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12204
12205   /* Parse the body of the namespace.  */
12206   cp_parser_namespace_body (parser);
12207
12208 #ifdef HANDLE_PRAGMA_VISIBILITY
12209   if (has_visibility)
12210     pop_visibility ();
12211 #endif
12212
12213   /* Finish the namespace.  */
12214   pop_namespace ();
12215   /* Look for the final `}'.  */
12216   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12217 }
12218
12219 /* Parse a namespace-body.
12220
12221    namespace-body:
12222      declaration-seq [opt]  */
12223
12224 static void
12225 cp_parser_namespace_body (cp_parser* parser)
12226 {
12227   cp_parser_declaration_seq_opt (parser);
12228 }
12229
12230 /* Parse a namespace-alias-definition.
12231
12232    namespace-alias-definition:
12233      namespace identifier = qualified-namespace-specifier ;  */
12234
12235 static void
12236 cp_parser_namespace_alias_definition (cp_parser* parser)
12237 {
12238   tree identifier;
12239   tree namespace_specifier;
12240
12241   cp_token *token = cp_lexer_peek_token (parser->lexer);
12242
12243   /* Look for the `namespace' keyword.  */
12244   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12245   /* Look for the identifier.  */
12246   identifier = cp_parser_identifier (parser);
12247   if (identifier == error_mark_node)
12248     return;
12249   /* Look for the `=' token.  */
12250   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12251       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12252     {
12253       error ("%H%<namespace%> definition is not allowed here", &token->location);
12254       /* Skip the definition.  */
12255       cp_lexer_consume_token (parser->lexer);
12256       if (cp_parser_skip_to_closing_brace (parser))
12257         cp_lexer_consume_token (parser->lexer);
12258       return;
12259     }
12260   cp_parser_require (parser, CPP_EQ, "%<=%>");
12261   /* Look for the qualified-namespace-specifier.  */
12262   namespace_specifier
12263     = cp_parser_qualified_namespace_specifier (parser);
12264   /* Look for the `;' token.  */
12265   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12266
12267   /* Register the alias in the symbol table.  */
12268   do_namespace_alias (identifier, namespace_specifier);
12269 }
12270
12271 /* Parse a qualified-namespace-specifier.
12272
12273    qualified-namespace-specifier:
12274      :: [opt] nested-name-specifier [opt] namespace-name
12275
12276    Returns a NAMESPACE_DECL corresponding to the specified
12277    namespace.  */
12278
12279 static tree
12280 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12281 {
12282   /* Look for the optional `::'.  */
12283   cp_parser_global_scope_opt (parser,
12284                               /*current_scope_valid_p=*/false);
12285
12286   /* Look for the optional nested-name-specifier.  */
12287   cp_parser_nested_name_specifier_opt (parser,
12288                                        /*typename_keyword_p=*/false,
12289                                        /*check_dependency_p=*/true,
12290                                        /*type_p=*/false,
12291                                        /*is_declaration=*/true);
12292
12293   return cp_parser_namespace_name (parser);
12294 }
12295
12296 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12297    access declaration.
12298
12299    using-declaration:
12300      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12301      using :: unqualified-id ;  
12302
12303    access-declaration:
12304      qualified-id ;  
12305
12306    */
12307
12308 static bool
12309 cp_parser_using_declaration (cp_parser* parser, 
12310                              bool access_declaration_p)
12311 {
12312   cp_token *token;
12313   bool typename_p = false;
12314   bool global_scope_p;
12315   tree decl;
12316   tree identifier;
12317   tree qscope;
12318
12319   if (access_declaration_p)
12320     cp_parser_parse_tentatively (parser);
12321   else
12322     {
12323       /* Look for the `using' keyword.  */
12324       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12325       
12326       /* Peek at the next token.  */
12327       token = cp_lexer_peek_token (parser->lexer);
12328       /* See if it's `typename'.  */
12329       if (token->keyword == RID_TYPENAME)
12330         {
12331           /* Remember that we've seen it.  */
12332           typename_p = true;
12333           /* Consume the `typename' token.  */
12334           cp_lexer_consume_token (parser->lexer);
12335         }
12336     }
12337
12338   /* Look for the optional global scope qualification.  */
12339   global_scope_p
12340     = (cp_parser_global_scope_opt (parser,
12341                                    /*current_scope_valid_p=*/false)
12342        != NULL_TREE);
12343
12344   /* If we saw `typename', or didn't see `::', then there must be a
12345      nested-name-specifier present.  */
12346   if (typename_p || !global_scope_p)
12347     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12348                                               /*check_dependency_p=*/true,
12349                                               /*type_p=*/false,
12350                                               /*is_declaration=*/true);
12351   /* Otherwise, we could be in either of the two productions.  In that
12352      case, treat the nested-name-specifier as optional.  */
12353   else
12354     qscope = cp_parser_nested_name_specifier_opt (parser,
12355                                                   /*typename_keyword_p=*/false,
12356                                                   /*check_dependency_p=*/true,
12357                                                   /*type_p=*/false,
12358                                                   /*is_declaration=*/true);
12359   if (!qscope)
12360     qscope = global_namespace;
12361
12362   if (access_declaration_p && cp_parser_error_occurred (parser))
12363     /* Something has already gone wrong; there's no need to parse
12364        further.  Since an error has occurred, the return value of
12365        cp_parser_parse_definitely will be false, as required.  */
12366     return cp_parser_parse_definitely (parser);
12367
12368   token = cp_lexer_peek_token (parser->lexer);
12369   /* Parse the unqualified-id.  */
12370   identifier = cp_parser_unqualified_id (parser,
12371                                          /*template_keyword_p=*/false,
12372                                          /*check_dependency_p=*/true,
12373                                          /*declarator_p=*/true,
12374                                          /*optional_p=*/false);
12375
12376   if (access_declaration_p)
12377     {
12378       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12379         cp_parser_simulate_error (parser);
12380       if (!cp_parser_parse_definitely (parser))
12381         return false;
12382     }
12383
12384   /* The function we call to handle a using-declaration is different
12385      depending on what scope we are in.  */
12386   if (qscope == error_mark_node || identifier == error_mark_node)
12387     ;
12388   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12389            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12390     /* [namespace.udecl]
12391
12392        A using declaration shall not name a template-id.  */
12393     error ("%Ha template-id may not appear in a using-declaration",
12394             &token->location);
12395   else
12396     {
12397       if (at_class_scope_p ())
12398         {
12399           /* Create the USING_DECL.  */
12400           decl = do_class_using_decl (parser->scope, identifier);
12401
12402           if (check_for_bare_parameter_packs (decl))
12403             return false;
12404           else
12405             /* Add it to the list of members in this class.  */
12406             finish_member_declaration (decl);
12407         }
12408       else
12409         {
12410           decl = cp_parser_lookup_name_simple (parser,
12411                                                identifier,
12412                                                token->location);
12413           if (decl == error_mark_node)
12414             cp_parser_name_lookup_error (parser, identifier,
12415                                          decl, NULL,
12416                                          token->location);
12417           else if (check_for_bare_parameter_packs (decl))
12418             return false;
12419           else if (!at_namespace_scope_p ())
12420             do_local_using_decl (decl, qscope, identifier);
12421           else
12422             do_toplevel_using_decl (decl, qscope, identifier);
12423         }
12424     }
12425
12426   /* Look for the final `;'.  */
12427   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12428   
12429   return true;
12430 }
12431
12432 /* Parse a using-directive.
12433
12434    using-directive:
12435      using namespace :: [opt] nested-name-specifier [opt]
12436        namespace-name ;  */
12437
12438 static void
12439 cp_parser_using_directive (cp_parser* parser)
12440 {
12441   tree namespace_decl;
12442   tree attribs;
12443
12444   /* Look for the `using' keyword.  */
12445   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12446   /* And the `namespace' keyword.  */
12447   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12448   /* Look for the optional `::' operator.  */
12449   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12450   /* And the optional nested-name-specifier.  */
12451   cp_parser_nested_name_specifier_opt (parser,
12452                                        /*typename_keyword_p=*/false,
12453                                        /*check_dependency_p=*/true,
12454                                        /*type_p=*/false,
12455                                        /*is_declaration=*/true);
12456   /* Get the namespace being used.  */
12457   namespace_decl = cp_parser_namespace_name (parser);
12458   /* And any specified attributes.  */
12459   attribs = cp_parser_attributes_opt (parser);
12460   /* Update the symbol table.  */
12461   parse_using_directive (namespace_decl, attribs);
12462   /* Look for the final `;'.  */
12463   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12464 }
12465
12466 /* Parse an asm-definition.
12467
12468    asm-definition:
12469      asm ( string-literal ) ;
12470
12471    GNU Extension:
12472
12473    asm-definition:
12474      asm volatile [opt] ( string-literal ) ;
12475      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12476      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12477                           : asm-operand-list [opt] ) ;
12478      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12479                           : asm-operand-list [opt]
12480                           : asm-operand-list [opt] ) ;  */
12481
12482 static void
12483 cp_parser_asm_definition (cp_parser* parser)
12484 {
12485   tree string;
12486   tree outputs = NULL_TREE;
12487   tree inputs = NULL_TREE;
12488   tree clobbers = NULL_TREE;
12489   tree asm_stmt;
12490   bool volatile_p = false;
12491   bool extended_p = false;
12492   bool invalid_inputs_p = false;
12493   bool invalid_outputs_p = false;
12494
12495   /* Look for the `asm' keyword.  */
12496   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12497   /* See if the next token is `volatile'.  */
12498   if (cp_parser_allow_gnu_extensions_p (parser)
12499       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12500     {
12501       /* Remember that we saw the `volatile' keyword.  */
12502       volatile_p = true;
12503       /* Consume the token.  */
12504       cp_lexer_consume_token (parser->lexer);
12505     }
12506   /* Look for the opening `('.  */
12507   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12508     return;
12509   /* Look for the string.  */
12510   string = cp_parser_string_literal (parser, false, false);
12511   if (string == error_mark_node)
12512     {
12513       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12514                                              /*consume_paren=*/true);
12515       return;
12516     }
12517
12518   /* If we're allowing GNU extensions, check for the extended assembly
12519      syntax.  Unfortunately, the `:' tokens need not be separated by
12520      a space in C, and so, for compatibility, we tolerate that here
12521      too.  Doing that means that we have to treat the `::' operator as
12522      two `:' tokens.  */
12523   if (cp_parser_allow_gnu_extensions_p (parser)
12524       && parser->in_function_body
12525       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12526           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12527     {
12528       bool inputs_p = false;
12529       bool clobbers_p = false;
12530
12531       /* The extended syntax was used.  */
12532       extended_p = true;
12533
12534       /* Look for outputs.  */
12535       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12536         {
12537           /* Consume the `:'.  */
12538           cp_lexer_consume_token (parser->lexer);
12539           /* Parse the output-operands.  */
12540           if (cp_lexer_next_token_is_not (parser->lexer,
12541                                           CPP_COLON)
12542               && cp_lexer_next_token_is_not (parser->lexer,
12543                                              CPP_SCOPE)
12544               && cp_lexer_next_token_is_not (parser->lexer,
12545                                              CPP_CLOSE_PAREN))
12546             outputs = cp_parser_asm_operand_list (parser);
12547
12548             if (outputs == error_mark_node)
12549               invalid_outputs_p = true;
12550         }
12551       /* If the next token is `::', there are no outputs, and the
12552          next token is the beginning of the inputs.  */
12553       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12554         /* The inputs are coming next.  */
12555         inputs_p = true;
12556
12557       /* Look for inputs.  */
12558       if (inputs_p
12559           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12560         {
12561           /* Consume the `:' or `::'.  */
12562           cp_lexer_consume_token (parser->lexer);
12563           /* Parse the output-operands.  */
12564           if (cp_lexer_next_token_is_not (parser->lexer,
12565                                           CPP_COLON)
12566               && cp_lexer_next_token_is_not (parser->lexer,
12567                                              CPP_CLOSE_PAREN))
12568             inputs = cp_parser_asm_operand_list (parser);
12569
12570             if (inputs == error_mark_node)
12571               invalid_inputs_p = true;
12572         }
12573       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12574         /* The clobbers are coming next.  */
12575         clobbers_p = true;
12576
12577       /* Look for clobbers.  */
12578       if (clobbers_p
12579           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12580         {
12581           /* Consume the `:' or `::'.  */
12582           cp_lexer_consume_token (parser->lexer);
12583           /* Parse the clobbers.  */
12584           if (cp_lexer_next_token_is_not (parser->lexer,
12585                                           CPP_CLOSE_PAREN))
12586             clobbers = cp_parser_asm_clobber_list (parser);
12587         }
12588     }
12589   /* Look for the closing `)'.  */
12590   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12591     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12592                                            /*consume_paren=*/true);
12593   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12594
12595   if (!invalid_inputs_p && !invalid_outputs_p)
12596     {
12597       /* Create the ASM_EXPR.  */
12598       if (parser->in_function_body)
12599         {
12600           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12601                                       inputs, clobbers);
12602           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12603           if (!extended_p)
12604             {
12605               tree temp = asm_stmt;
12606               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12607                 temp = TREE_OPERAND (temp, 0);
12608
12609               ASM_INPUT_P (temp) = 1;
12610             }
12611         }
12612       else
12613         cgraph_add_asm_node (string);
12614     }
12615 }
12616
12617 /* Declarators [gram.dcl.decl] */
12618
12619 /* Parse an init-declarator.
12620
12621    init-declarator:
12622      declarator initializer [opt]
12623
12624    GNU Extension:
12625
12626    init-declarator:
12627      declarator asm-specification [opt] attributes [opt] initializer [opt]
12628
12629    function-definition:
12630      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12631        function-body
12632      decl-specifier-seq [opt] declarator function-try-block
12633
12634    GNU Extension:
12635
12636    function-definition:
12637      __extension__ function-definition
12638
12639    The DECL_SPECIFIERS apply to this declarator.  Returns a
12640    representation of the entity declared.  If MEMBER_P is TRUE, then
12641    this declarator appears in a class scope.  The new DECL created by
12642    this declarator is returned.
12643
12644    The CHECKS are access checks that should be performed once we know
12645    what entity is being declared (and, therefore, what classes have
12646    befriended it).
12647
12648    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12649    for a function-definition here as well.  If the declarator is a
12650    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12651    be TRUE upon return.  By that point, the function-definition will
12652    have been completely parsed.
12653
12654    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12655    is FALSE.  */
12656
12657 static tree
12658 cp_parser_init_declarator (cp_parser* parser,
12659                            cp_decl_specifier_seq *decl_specifiers,
12660                            VEC (deferred_access_check,gc)* checks,
12661                            bool function_definition_allowed_p,
12662                            bool member_p,
12663                            int declares_class_or_enum,
12664                            bool* function_definition_p)
12665 {
12666   cp_token *token = NULL, *asm_spec_start_token = NULL,
12667            *attributes_start_token = NULL;
12668   cp_declarator *declarator;
12669   tree prefix_attributes;
12670   tree attributes;
12671   tree asm_specification;
12672   tree initializer;
12673   tree decl = NULL_TREE;
12674   tree scope;
12675   int is_initialized;
12676   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12677      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12678      "(...)".  */
12679   enum cpp_ttype initialization_kind;
12680   bool is_direct_init = false;
12681   bool is_non_constant_init;
12682   int ctor_dtor_or_conv_p;
12683   bool friend_p;
12684   tree pushed_scope = NULL;
12685
12686   /* Gather the attributes that were provided with the
12687      decl-specifiers.  */
12688   prefix_attributes = decl_specifiers->attributes;
12689
12690   /* Assume that this is not the declarator for a function
12691      definition.  */
12692   if (function_definition_p)
12693     *function_definition_p = false;
12694
12695   /* Defer access checks while parsing the declarator; we cannot know
12696      what names are accessible until we know what is being
12697      declared.  */
12698   resume_deferring_access_checks ();
12699
12700   /* Parse the declarator.  */
12701   token = cp_lexer_peek_token (parser->lexer);
12702   declarator
12703     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12704                             &ctor_dtor_or_conv_p,
12705                             /*parenthesized_p=*/NULL,
12706                             /*member_p=*/false);
12707   /* Gather up the deferred checks.  */
12708   stop_deferring_access_checks ();
12709
12710   /* If the DECLARATOR was erroneous, there's no need to go
12711      further.  */
12712   if (declarator == cp_error_declarator)
12713     return error_mark_node;
12714
12715   /* Check that the number of template-parameter-lists is OK.  */
12716   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12717                                                        token->location))
12718     return error_mark_node;
12719
12720   if (declares_class_or_enum & 2)
12721     cp_parser_check_for_definition_in_return_type (declarator,
12722                                                    decl_specifiers->type,
12723                                                    decl_specifiers->type_location);
12724
12725   /* Figure out what scope the entity declared by the DECLARATOR is
12726      located in.  `grokdeclarator' sometimes changes the scope, so
12727      we compute it now.  */
12728   scope = get_scope_of_declarator (declarator);
12729
12730   /* If we're allowing GNU extensions, look for an asm-specification
12731      and attributes.  */
12732   if (cp_parser_allow_gnu_extensions_p (parser))
12733     {
12734       /* Look for an asm-specification.  */
12735       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12736       asm_specification = cp_parser_asm_specification_opt (parser);
12737       /* And attributes.  */
12738       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12739       attributes = cp_parser_attributes_opt (parser);
12740     }
12741   else
12742     {
12743       asm_specification = NULL_TREE;
12744       attributes = NULL_TREE;
12745     }
12746
12747   /* Peek at the next token.  */
12748   token = cp_lexer_peek_token (parser->lexer);
12749   /* Check to see if the token indicates the start of a
12750      function-definition.  */
12751   if (function_declarator_p (declarator)
12752       && cp_parser_token_starts_function_definition_p (token))
12753     {
12754       if (!function_definition_allowed_p)
12755         {
12756           /* If a function-definition should not appear here, issue an
12757              error message.  */
12758           cp_parser_error (parser,
12759                            "a function-definition is not allowed here");
12760           return error_mark_node;
12761         }
12762       else
12763         {
12764           location_t func_brace_location
12765             = cp_lexer_peek_token (parser->lexer)->location;
12766
12767           /* Neither attributes nor an asm-specification are allowed
12768              on a function-definition.  */
12769           if (asm_specification)
12770             error ("%Han asm-specification is not allowed "
12771                    "on a function-definition",
12772                    &asm_spec_start_token->location);
12773           if (attributes)
12774             error ("%Hattributes are not allowed on a function-definition",
12775                    &attributes_start_token->location);
12776           /* This is a function-definition.  */
12777           *function_definition_p = true;
12778
12779           /* Parse the function definition.  */
12780           if (member_p)
12781             decl = cp_parser_save_member_function_body (parser,
12782                                                         decl_specifiers,
12783                                                         declarator,
12784                                                         prefix_attributes);
12785           else
12786             decl
12787               = (cp_parser_function_definition_from_specifiers_and_declarator
12788                  (parser, decl_specifiers, prefix_attributes, declarator));
12789
12790           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12791             {
12792               /* This is where the prologue starts...  */
12793               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12794                 = func_brace_location;
12795             }
12796
12797           return decl;
12798         }
12799     }
12800
12801   /* [dcl.dcl]
12802
12803      Only in function declarations for constructors, destructors, and
12804      type conversions can the decl-specifier-seq be omitted.
12805
12806      We explicitly postpone this check past the point where we handle
12807      function-definitions because we tolerate function-definitions
12808      that are missing their return types in some modes.  */
12809   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12810     {
12811       cp_parser_error (parser,
12812                        "expected constructor, destructor, or type conversion");
12813       return error_mark_node;
12814     }
12815
12816   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12817   if (token->type == CPP_EQ
12818       || token->type == CPP_OPEN_PAREN
12819       || token->type == CPP_OPEN_BRACE)
12820     {
12821       is_initialized = SD_INITIALIZED;
12822       initialization_kind = token->type;
12823
12824       if (token->type == CPP_EQ
12825           && function_declarator_p (declarator))
12826         {
12827           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12828           if (t2->keyword == RID_DEFAULT)
12829             is_initialized = SD_DEFAULTED;
12830           else if (t2->keyword == RID_DELETE)
12831             is_initialized = SD_DELETED;
12832         }
12833     }
12834   else
12835     {
12836       /* If the init-declarator isn't initialized and isn't followed by a
12837          `,' or `;', it's not a valid init-declarator.  */
12838       if (token->type != CPP_COMMA
12839           && token->type != CPP_SEMICOLON)
12840         {
12841           cp_parser_error (parser, "expected initializer");
12842           return error_mark_node;
12843         }
12844       is_initialized = SD_UNINITIALIZED;
12845       initialization_kind = CPP_EOF;
12846     }
12847
12848   /* Because start_decl has side-effects, we should only call it if we
12849      know we're going ahead.  By this point, we know that we cannot
12850      possibly be looking at any other construct.  */
12851   cp_parser_commit_to_tentative_parse (parser);
12852
12853   /* If the decl specifiers were bad, issue an error now that we're
12854      sure this was intended to be a declarator.  Then continue
12855      declaring the variable(s), as int, to try to cut down on further
12856      errors.  */
12857   if (decl_specifiers->any_specifiers_p
12858       && decl_specifiers->type == error_mark_node)
12859     {
12860       cp_parser_error (parser, "invalid type in declaration");
12861       decl_specifiers->type = integer_type_node;
12862     }
12863
12864   /* Check to see whether or not this declaration is a friend.  */
12865   friend_p = cp_parser_friend_p (decl_specifiers);
12866
12867   /* Enter the newly declared entry in the symbol table.  If we're
12868      processing a declaration in a class-specifier, we wait until
12869      after processing the initializer.  */
12870   if (!member_p)
12871     {
12872       if (parser->in_unbraced_linkage_specification_p)
12873         decl_specifiers->storage_class = sc_extern;
12874       decl = start_decl (declarator, decl_specifiers,
12875                          is_initialized, attributes, prefix_attributes,
12876                          &pushed_scope);
12877     }
12878   else if (scope)
12879     /* Enter the SCOPE.  That way unqualified names appearing in the
12880        initializer will be looked up in SCOPE.  */
12881     pushed_scope = push_scope (scope);
12882
12883   /* Perform deferred access control checks, now that we know in which
12884      SCOPE the declared entity resides.  */
12885   if (!member_p && decl)
12886     {
12887       tree saved_current_function_decl = NULL_TREE;
12888
12889       /* If the entity being declared is a function, pretend that we
12890          are in its scope.  If it is a `friend', it may have access to
12891          things that would not otherwise be accessible.  */
12892       if (TREE_CODE (decl) == FUNCTION_DECL)
12893         {
12894           saved_current_function_decl = current_function_decl;
12895           current_function_decl = decl;
12896         }
12897
12898       /* Perform access checks for template parameters.  */
12899       cp_parser_perform_template_parameter_access_checks (checks);
12900
12901       /* Perform the access control checks for the declarator and the
12902          decl-specifiers.  */
12903       perform_deferred_access_checks ();
12904
12905       /* Restore the saved value.  */
12906       if (TREE_CODE (decl) == FUNCTION_DECL)
12907         current_function_decl = saved_current_function_decl;
12908     }
12909
12910   /* Parse the initializer.  */
12911   initializer = NULL_TREE;
12912   is_direct_init = false;
12913   is_non_constant_init = true;
12914   if (is_initialized)
12915     {
12916       if (function_declarator_p (declarator))
12917         {
12918           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12919            if (initialization_kind == CPP_EQ)
12920              initializer = cp_parser_pure_specifier (parser);
12921            else
12922              {
12923                /* If the declaration was erroneous, we don't really
12924                   know what the user intended, so just silently
12925                   consume the initializer.  */
12926                if (decl != error_mark_node)
12927                  error ("%Hinitializer provided for function",
12928                         &initializer_start_token->location);
12929                cp_parser_skip_to_closing_parenthesis (parser,
12930                                                       /*recovering=*/true,
12931                                                       /*or_comma=*/false,
12932                                                       /*consume_paren=*/true);
12933              }
12934         }
12935       else
12936         initializer = cp_parser_initializer (parser,
12937                                              &is_direct_init,
12938                                              &is_non_constant_init);
12939     }
12940
12941   /* The old parser allows attributes to appear after a parenthesized
12942      initializer.  Mark Mitchell proposed removing this functionality
12943      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12944      attributes -- but ignores them.  */
12945   if (cp_parser_allow_gnu_extensions_p (parser)
12946       && initialization_kind == CPP_OPEN_PAREN)
12947     if (cp_parser_attributes_opt (parser))
12948       warning (OPT_Wattributes,
12949                "attributes after parenthesized initializer ignored");
12950
12951   /* For an in-class declaration, use `grokfield' to create the
12952      declaration.  */
12953   if (member_p)
12954     {
12955       if (pushed_scope)
12956         {
12957           pop_scope (pushed_scope);
12958           pushed_scope = false;
12959         }
12960       decl = grokfield (declarator, decl_specifiers,
12961                         initializer, !is_non_constant_init,
12962                         /*asmspec=*/NULL_TREE,
12963                         prefix_attributes);
12964       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12965         cp_parser_save_default_args (parser, decl);
12966     }
12967
12968   /* Finish processing the declaration.  But, skip friend
12969      declarations.  */
12970   if (!friend_p && decl && decl != error_mark_node)
12971     {
12972       cp_finish_decl (decl,
12973                       initializer, !is_non_constant_init,
12974                       asm_specification,
12975                       /* If the initializer is in parentheses, then this is
12976                          a direct-initialization, which means that an
12977                          `explicit' constructor is OK.  Otherwise, an
12978                          `explicit' constructor cannot be used.  */
12979                       ((is_direct_init || !is_initialized)
12980                        ? 0 : LOOKUP_ONLYCONVERTING));
12981     }
12982   else if ((cxx_dialect != cxx98) && friend_p
12983            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12984     /* Core issue #226 (C++0x only): A default template-argument
12985        shall not be specified in a friend class template
12986        declaration. */
12987     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12988                              /*is_partial=*/0, /*is_friend_decl=*/1);
12989
12990   if (!friend_p && pushed_scope)
12991     pop_scope (pushed_scope);
12992
12993   return decl;
12994 }
12995
12996 /* Parse a declarator.
12997
12998    declarator:
12999      direct-declarator
13000      ptr-operator declarator
13001
13002    abstract-declarator:
13003      ptr-operator abstract-declarator [opt]
13004      direct-abstract-declarator
13005
13006    GNU Extensions:
13007
13008    declarator:
13009      attributes [opt] direct-declarator
13010      attributes [opt] ptr-operator declarator
13011
13012    abstract-declarator:
13013      attributes [opt] ptr-operator abstract-declarator [opt]
13014      attributes [opt] direct-abstract-declarator
13015
13016    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13017    detect constructor, destructor or conversion operators. It is set
13018    to -1 if the declarator is a name, and +1 if it is a
13019    function. Otherwise it is set to zero. Usually you just want to
13020    test for >0, but internally the negative value is used.
13021
13022    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13023    a decl-specifier-seq unless it declares a constructor, destructor,
13024    or conversion.  It might seem that we could check this condition in
13025    semantic analysis, rather than parsing, but that makes it difficult
13026    to handle something like `f()'.  We want to notice that there are
13027    no decl-specifiers, and therefore realize that this is an
13028    expression, not a declaration.)
13029
13030    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13031    the declarator is a direct-declarator of the form "(...)".
13032
13033    MEMBER_P is true iff this declarator is a member-declarator.  */
13034
13035 static cp_declarator *
13036 cp_parser_declarator (cp_parser* parser,
13037                       cp_parser_declarator_kind dcl_kind,
13038                       int* ctor_dtor_or_conv_p,
13039                       bool* parenthesized_p,
13040                       bool member_p)
13041 {
13042   cp_token *token;
13043   cp_declarator *declarator;
13044   enum tree_code code;
13045   cp_cv_quals cv_quals;
13046   tree class_type;
13047   tree attributes = NULL_TREE;
13048
13049   /* Assume this is not a constructor, destructor, or type-conversion
13050      operator.  */
13051   if (ctor_dtor_or_conv_p)
13052     *ctor_dtor_or_conv_p = 0;
13053
13054   if (cp_parser_allow_gnu_extensions_p (parser))
13055     attributes = cp_parser_attributes_opt (parser);
13056
13057   /* Peek at the next token.  */
13058   token = cp_lexer_peek_token (parser->lexer);
13059
13060   /* Check for the ptr-operator production.  */
13061   cp_parser_parse_tentatively (parser);
13062   /* Parse the ptr-operator.  */
13063   code = cp_parser_ptr_operator (parser,
13064                                  &class_type,
13065                                  &cv_quals);
13066   /* If that worked, then we have a ptr-operator.  */
13067   if (cp_parser_parse_definitely (parser))
13068     {
13069       /* If a ptr-operator was found, then this declarator was not
13070          parenthesized.  */
13071       if (parenthesized_p)
13072         *parenthesized_p = true;
13073       /* The dependent declarator is optional if we are parsing an
13074          abstract-declarator.  */
13075       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13076         cp_parser_parse_tentatively (parser);
13077
13078       /* Parse the dependent declarator.  */
13079       declarator = cp_parser_declarator (parser, dcl_kind,
13080                                          /*ctor_dtor_or_conv_p=*/NULL,
13081                                          /*parenthesized_p=*/NULL,
13082                                          /*member_p=*/false);
13083
13084       /* If we are parsing an abstract-declarator, we must handle the
13085          case where the dependent declarator is absent.  */
13086       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13087           && !cp_parser_parse_definitely (parser))
13088         declarator = NULL;
13089
13090       declarator = cp_parser_make_indirect_declarator
13091         (code, class_type, cv_quals, declarator);
13092     }
13093   /* Everything else is a direct-declarator.  */
13094   else
13095     {
13096       if (parenthesized_p)
13097         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13098                                                    CPP_OPEN_PAREN);
13099       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13100                                                 ctor_dtor_or_conv_p,
13101                                                 member_p);
13102     }
13103
13104   if (attributes && declarator && declarator != cp_error_declarator)
13105     declarator->attributes = attributes;
13106
13107   return declarator;
13108 }
13109
13110 /* Parse a direct-declarator or direct-abstract-declarator.
13111
13112    direct-declarator:
13113      declarator-id
13114      direct-declarator ( parameter-declaration-clause )
13115        cv-qualifier-seq [opt]
13116        exception-specification [opt]
13117      direct-declarator [ constant-expression [opt] ]
13118      ( declarator )
13119
13120    direct-abstract-declarator:
13121      direct-abstract-declarator [opt]
13122        ( parameter-declaration-clause )
13123        cv-qualifier-seq [opt]
13124        exception-specification [opt]
13125      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13126      ( abstract-declarator )
13127
13128    Returns a representation of the declarator.  DCL_KIND is
13129    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13130    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13131    we are parsing a direct-declarator.  It is
13132    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13133    of ambiguity we prefer an abstract declarator, as per
13134    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13135    cp_parser_declarator.  */
13136
13137 static cp_declarator *
13138 cp_parser_direct_declarator (cp_parser* parser,
13139                              cp_parser_declarator_kind dcl_kind,
13140                              int* ctor_dtor_or_conv_p,
13141                              bool member_p)
13142 {
13143   cp_token *token;
13144   cp_declarator *declarator = NULL;
13145   tree scope = NULL_TREE;
13146   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13147   bool saved_in_declarator_p = parser->in_declarator_p;
13148   bool first = true;
13149   tree pushed_scope = NULL_TREE;
13150
13151   while (true)
13152     {
13153       /* Peek at the next token.  */
13154       token = cp_lexer_peek_token (parser->lexer);
13155       if (token->type == CPP_OPEN_PAREN)
13156         {
13157           /* This is either a parameter-declaration-clause, or a
13158              parenthesized declarator. When we know we are parsing a
13159              named declarator, it must be a parenthesized declarator
13160              if FIRST is true. For instance, `(int)' is a
13161              parameter-declaration-clause, with an omitted
13162              direct-abstract-declarator. But `((*))', is a
13163              parenthesized abstract declarator. Finally, when T is a
13164              template parameter `(T)' is a
13165              parameter-declaration-clause, and not a parenthesized
13166              named declarator.
13167
13168              We first try and parse a parameter-declaration-clause,
13169              and then try a nested declarator (if FIRST is true).
13170
13171              It is not an error for it not to be a
13172              parameter-declaration-clause, even when FIRST is
13173              false. Consider,
13174
13175                int i (int);
13176                int i (3);
13177
13178              The first is the declaration of a function while the
13179              second is the definition of a variable, including its
13180              initializer.
13181
13182              Having seen only the parenthesis, we cannot know which of
13183              these two alternatives should be selected.  Even more
13184              complex are examples like:
13185
13186                int i (int (a));
13187                int i (int (3));
13188
13189              The former is a function-declaration; the latter is a
13190              variable initialization.
13191
13192              Thus again, we try a parameter-declaration-clause, and if
13193              that fails, we back out and return.  */
13194
13195           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13196             {
13197               tree params;
13198               unsigned saved_num_template_parameter_lists;
13199               bool is_declarator = false;
13200               tree t;
13201
13202               /* In a member-declarator, the only valid interpretation
13203                  of a parenthesis is the start of a
13204                  parameter-declaration-clause.  (It is invalid to
13205                  initialize a static data member with a parenthesized
13206                  initializer; only the "=" form of initialization is
13207                  permitted.)  */
13208               if (!member_p)
13209                 cp_parser_parse_tentatively (parser);
13210
13211               /* Consume the `('.  */
13212               cp_lexer_consume_token (parser->lexer);
13213               if (first)
13214                 {
13215                   /* If this is going to be an abstract declarator, we're
13216                      in a declarator and we can't have default args.  */
13217                   parser->default_arg_ok_p = false;
13218                   parser->in_declarator_p = true;
13219                 }
13220
13221               /* Inside the function parameter list, surrounding
13222                  template-parameter-lists do not apply.  */
13223               saved_num_template_parameter_lists
13224                 = parser->num_template_parameter_lists;
13225               parser->num_template_parameter_lists = 0;
13226
13227               begin_scope (sk_function_parms, NULL_TREE);
13228
13229               /* Parse the parameter-declaration-clause.  */
13230               params = cp_parser_parameter_declaration_clause (parser);
13231
13232               parser->num_template_parameter_lists
13233                 = saved_num_template_parameter_lists;
13234
13235               /* If all went well, parse the cv-qualifier-seq and the
13236                  exception-specification.  */
13237               if (member_p || cp_parser_parse_definitely (parser))
13238                 {
13239                   cp_cv_quals cv_quals;
13240                   tree exception_specification;
13241                   tree late_return;
13242
13243                   is_declarator = true;
13244
13245                   if (ctor_dtor_or_conv_p)
13246                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13247                   first = false;
13248                   /* Consume the `)'.  */
13249                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13250
13251                   /* Parse the cv-qualifier-seq.  */
13252                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13253                   /* And the exception-specification.  */
13254                   exception_specification
13255                     = cp_parser_exception_specification_opt (parser);
13256
13257                   late_return
13258                     = cp_parser_late_return_type_opt (parser);
13259
13260                   /* Create the function-declarator.  */
13261                   declarator = make_call_declarator (declarator,
13262                                                      params,
13263                                                      cv_quals,
13264                                                      exception_specification,
13265                                                      late_return);
13266                   /* Any subsequent parameter lists are to do with
13267                      return type, so are not those of the declared
13268                      function.  */
13269                   parser->default_arg_ok_p = false;
13270                 }
13271
13272               /* Remove the function parms from scope.  */
13273               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13274                 pop_binding (DECL_NAME (t), t);
13275               leave_scope();
13276
13277               if (is_declarator)
13278                 /* Repeat the main loop.  */
13279                 continue;
13280             }
13281
13282           /* If this is the first, we can try a parenthesized
13283              declarator.  */
13284           if (first)
13285             {
13286               bool saved_in_type_id_in_expr_p;
13287
13288               parser->default_arg_ok_p = saved_default_arg_ok_p;
13289               parser->in_declarator_p = saved_in_declarator_p;
13290
13291               /* Consume the `('.  */
13292               cp_lexer_consume_token (parser->lexer);
13293               /* Parse the nested declarator.  */
13294               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13295               parser->in_type_id_in_expr_p = true;
13296               declarator
13297                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13298                                         /*parenthesized_p=*/NULL,
13299                                         member_p);
13300               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13301               first = false;
13302               /* Expect a `)'.  */
13303               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13304                 declarator = cp_error_declarator;
13305               if (declarator == cp_error_declarator)
13306                 break;
13307
13308               goto handle_declarator;
13309             }
13310           /* Otherwise, we must be done.  */
13311           else
13312             break;
13313         }
13314       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13315                && token->type == CPP_OPEN_SQUARE)
13316         {
13317           /* Parse an array-declarator.  */
13318           tree bounds;
13319
13320           if (ctor_dtor_or_conv_p)
13321             *ctor_dtor_or_conv_p = 0;
13322
13323           first = false;
13324           parser->default_arg_ok_p = false;
13325           parser->in_declarator_p = true;
13326           /* Consume the `['.  */
13327           cp_lexer_consume_token (parser->lexer);
13328           /* Peek at the next token.  */
13329           token = cp_lexer_peek_token (parser->lexer);
13330           /* If the next token is `]', then there is no
13331              constant-expression.  */
13332           if (token->type != CPP_CLOSE_SQUARE)
13333             {
13334               bool non_constant_p;
13335
13336               bounds
13337                 = cp_parser_constant_expression (parser,
13338                                                  /*allow_non_constant=*/true,
13339                                                  &non_constant_p);
13340               if (!non_constant_p)
13341                 bounds = fold_non_dependent_expr (bounds);
13342               /* Normally, the array bound must be an integral constant
13343                  expression.  However, as an extension, we allow VLAs
13344                  in function scopes as long as they aren't part of a
13345                  parameter declaration.  */
13346               else if (!parser->in_function_body
13347                        || current_binding_level->kind == sk_function_parms)
13348                 {
13349                   cp_parser_error (parser,
13350                                    "array bound is not an integer constant");
13351                   bounds = error_mark_node;
13352                 }
13353               else if (processing_template_decl && !error_operand_p (bounds))
13354                 {
13355                   /* Remember this wasn't a constant-expression.  */
13356                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13357                   TREE_SIDE_EFFECTS (bounds) = 1;
13358                 }
13359             }
13360           else
13361             bounds = NULL_TREE;
13362           /* Look for the closing `]'.  */
13363           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13364             {
13365               declarator = cp_error_declarator;
13366               break;
13367             }
13368
13369           declarator = make_array_declarator (declarator, bounds);
13370         }
13371       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13372         {
13373           tree qualifying_scope;
13374           tree unqualified_name;
13375           special_function_kind sfk;
13376           bool abstract_ok;
13377           bool pack_expansion_p = false;
13378           cp_token *declarator_id_start_token;
13379
13380           /* Parse a declarator-id */
13381           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13382           if (abstract_ok)
13383             {
13384               cp_parser_parse_tentatively (parser);
13385
13386               /* If we see an ellipsis, we should be looking at a
13387                  parameter pack. */
13388               if (token->type == CPP_ELLIPSIS)
13389                 {
13390                   /* Consume the `...' */
13391                   cp_lexer_consume_token (parser->lexer);
13392
13393                   pack_expansion_p = true;
13394                 }
13395             }
13396
13397           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13398           unqualified_name
13399             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13400           qualifying_scope = parser->scope;
13401           if (abstract_ok)
13402             {
13403               bool okay = false;
13404
13405               if (!unqualified_name && pack_expansion_p)
13406                 {
13407                   /* Check whether an error occurred. */
13408                   okay = !cp_parser_error_occurred (parser);
13409
13410                   /* We already consumed the ellipsis to mark a
13411                      parameter pack, but we have no way to report it,
13412                      so abort the tentative parse. We will be exiting
13413                      immediately anyway. */
13414                   cp_parser_abort_tentative_parse (parser);
13415                 }
13416               else
13417                 okay = cp_parser_parse_definitely (parser);
13418
13419               if (!okay)
13420                 unqualified_name = error_mark_node;
13421               else if (unqualified_name
13422                        && (qualifying_scope
13423                            || (TREE_CODE (unqualified_name)
13424                                != IDENTIFIER_NODE)))
13425                 {
13426                   cp_parser_error (parser, "expected unqualified-id");
13427                   unqualified_name = error_mark_node;
13428                 }
13429             }
13430
13431           if (!unqualified_name)
13432             return NULL;
13433           if (unqualified_name == error_mark_node)
13434             {
13435               declarator = cp_error_declarator;
13436               pack_expansion_p = false;
13437               declarator->parameter_pack_p = false;
13438               break;
13439             }
13440
13441           if (qualifying_scope && at_namespace_scope_p ()
13442               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13443             {
13444               /* In the declaration of a member of a template class
13445                  outside of the class itself, the SCOPE will sometimes
13446                  be a TYPENAME_TYPE.  For example, given:
13447
13448                  template <typename T>
13449                  int S<T>::R::i = 3;
13450
13451                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13452                  this context, we must resolve S<T>::R to an ordinary
13453                  type, rather than a typename type.
13454
13455                  The reason we normally avoid resolving TYPENAME_TYPEs
13456                  is that a specialization of `S' might render
13457                  `S<T>::R' not a type.  However, if `S' is
13458                  specialized, then this `i' will not be used, so there
13459                  is no harm in resolving the types here.  */
13460               tree type;
13461
13462               /* Resolve the TYPENAME_TYPE.  */
13463               type = resolve_typename_type (qualifying_scope,
13464                                             /*only_current_p=*/false);
13465               /* If that failed, the declarator is invalid.  */
13466               if (TREE_CODE (type) == TYPENAME_TYPE)
13467                 error ("%H%<%T::%E%> is not a type",
13468                        &declarator_id_start_token->location,
13469                        TYPE_CONTEXT (qualifying_scope),
13470                        TYPE_IDENTIFIER (qualifying_scope));
13471               qualifying_scope = type;
13472             }
13473
13474           sfk = sfk_none;
13475
13476           if (unqualified_name)
13477             {
13478               tree class_type;
13479
13480               if (qualifying_scope
13481                   && CLASS_TYPE_P (qualifying_scope))
13482                 class_type = qualifying_scope;
13483               else
13484                 class_type = current_class_type;
13485
13486               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13487                 {
13488                   tree name_type = TREE_TYPE (unqualified_name);
13489                   if (class_type && same_type_p (name_type, class_type))
13490                     {
13491                       if (qualifying_scope
13492                           && CLASSTYPE_USE_TEMPLATE (name_type))
13493                         {
13494                           error ("%Hinvalid use of constructor as a template",
13495                                  &declarator_id_start_token->location);
13496                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13497                                   "name the constructor in a qualified name",
13498                                   class_type,
13499                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13500                                   class_type, name_type);
13501                           declarator = cp_error_declarator;
13502                           break;
13503                         }
13504                       else
13505                         unqualified_name = constructor_name (class_type);
13506                     }
13507                   else
13508                     {
13509                       /* We do not attempt to print the declarator
13510                          here because we do not have enough
13511                          information about its original syntactic
13512                          form.  */
13513                       cp_parser_error (parser, "invalid declarator");
13514                       declarator = cp_error_declarator;
13515                       break;
13516                     }
13517                 }
13518
13519               if (class_type)
13520                 {
13521                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13522                     sfk = sfk_destructor;
13523                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13524                     sfk = sfk_conversion;
13525                   else if (/* There's no way to declare a constructor
13526                               for an anonymous type, even if the type
13527                               got a name for linkage purposes.  */
13528                            !TYPE_WAS_ANONYMOUS (class_type)
13529                            && constructor_name_p (unqualified_name,
13530                                                   class_type))
13531                     {
13532                       unqualified_name = constructor_name (class_type);
13533                       sfk = sfk_constructor;
13534                     }
13535
13536                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13537                     *ctor_dtor_or_conv_p = -1;
13538                 }
13539             }
13540           declarator = make_id_declarator (qualifying_scope,
13541                                            unqualified_name,
13542                                            sfk);
13543           declarator->id_loc = token->location;
13544           declarator->parameter_pack_p = pack_expansion_p;
13545
13546           if (pack_expansion_p)
13547             maybe_warn_variadic_templates ();
13548
13549         handle_declarator:;
13550           scope = get_scope_of_declarator (declarator);
13551           if (scope)
13552             /* Any names that appear after the declarator-id for a
13553                member are looked up in the containing scope.  */
13554             pushed_scope = push_scope (scope);
13555           parser->in_declarator_p = true;
13556           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13557               || (declarator && declarator->kind == cdk_id))
13558             /* Default args are only allowed on function
13559                declarations.  */
13560             parser->default_arg_ok_p = saved_default_arg_ok_p;
13561           else
13562             parser->default_arg_ok_p = false;
13563
13564           first = false;
13565         }
13566       /* We're done.  */
13567       else
13568         break;
13569     }
13570
13571   /* For an abstract declarator, we might wind up with nothing at this
13572      point.  That's an error; the declarator is not optional.  */
13573   if (!declarator)
13574     cp_parser_error (parser, "expected declarator");
13575
13576   /* If we entered a scope, we must exit it now.  */
13577   if (pushed_scope)
13578     pop_scope (pushed_scope);
13579
13580   parser->default_arg_ok_p = saved_default_arg_ok_p;
13581   parser->in_declarator_p = saved_in_declarator_p;
13582
13583   return declarator;
13584 }
13585
13586 /* Parse a ptr-operator.
13587
13588    ptr-operator:
13589      * cv-qualifier-seq [opt]
13590      &
13591      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13592
13593    GNU Extension:
13594
13595    ptr-operator:
13596      & cv-qualifier-seq [opt]
13597
13598    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13599    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13600    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13601    filled in with the TYPE containing the member.  *CV_QUALS is
13602    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13603    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13604    Note that the tree codes returned by this function have nothing
13605    to do with the types of trees that will be eventually be created
13606    to represent the pointer or reference type being parsed. They are
13607    just constants with suggestive names. */
13608 static enum tree_code
13609 cp_parser_ptr_operator (cp_parser* parser,
13610                         tree* type,
13611                         cp_cv_quals *cv_quals)
13612 {
13613   enum tree_code code = ERROR_MARK;
13614   cp_token *token;
13615
13616   /* Assume that it's not a pointer-to-member.  */
13617   *type = NULL_TREE;
13618   /* And that there are no cv-qualifiers.  */
13619   *cv_quals = TYPE_UNQUALIFIED;
13620
13621   /* Peek at the next token.  */
13622   token = cp_lexer_peek_token (parser->lexer);
13623
13624   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13625   if (token->type == CPP_MULT)
13626     code = INDIRECT_REF;
13627   else if (token->type == CPP_AND)
13628     code = ADDR_EXPR;
13629   else if ((cxx_dialect != cxx98) &&
13630            token->type == CPP_AND_AND) /* C++0x only */
13631     code = NON_LVALUE_EXPR;
13632
13633   if (code != ERROR_MARK)
13634     {
13635       /* Consume the `*', `&' or `&&'.  */
13636       cp_lexer_consume_token (parser->lexer);
13637
13638       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13639          `&', if we are allowing GNU extensions.  (The only qualifier
13640          that can legally appear after `&' is `restrict', but that is
13641          enforced during semantic analysis.  */
13642       if (code == INDIRECT_REF
13643           || cp_parser_allow_gnu_extensions_p (parser))
13644         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13645     }
13646   else
13647     {
13648       /* Try the pointer-to-member case.  */
13649       cp_parser_parse_tentatively (parser);
13650       /* Look for the optional `::' operator.  */
13651       cp_parser_global_scope_opt (parser,
13652                                   /*current_scope_valid_p=*/false);
13653       /* Look for the nested-name specifier.  */
13654       token = cp_lexer_peek_token (parser->lexer);
13655       cp_parser_nested_name_specifier (parser,
13656                                        /*typename_keyword_p=*/false,
13657                                        /*check_dependency_p=*/true,
13658                                        /*type_p=*/false,
13659                                        /*is_declaration=*/false);
13660       /* If we found it, and the next token is a `*', then we are
13661          indeed looking at a pointer-to-member operator.  */
13662       if (!cp_parser_error_occurred (parser)
13663           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13664         {
13665           /* Indicate that the `*' operator was used.  */
13666           code = INDIRECT_REF;
13667
13668           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13669             error ("%H%qD is a namespace", &token->location, parser->scope);
13670           else
13671             {
13672               /* The type of which the member is a member is given by the
13673                  current SCOPE.  */
13674               *type = parser->scope;
13675               /* The next name will not be qualified.  */
13676               parser->scope = NULL_TREE;
13677               parser->qualifying_scope = NULL_TREE;
13678               parser->object_scope = NULL_TREE;
13679               /* Look for the optional cv-qualifier-seq.  */
13680               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13681             }
13682         }
13683       /* If that didn't work we don't have a ptr-operator.  */
13684       if (!cp_parser_parse_definitely (parser))
13685         cp_parser_error (parser, "expected ptr-operator");
13686     }
13687
13688   return code;
13689 }
13690
13691 /* Parse an (optional) cv-qualifier-seq.
13692
13693    cv-qualifier-seq:
13694      cv-qualifier cv-qualifier-seq [opt]
13695
13696    cv-qualifier:
13697      const
13698      volatile
13699
13700    GNU Extension:
13701
13702    cv-qualifier:
13703      __restrict__
13704
13705    Returns a bitmask representing the cv-qualifiers.  */
13706
13707 static cp_cv_quals
13708 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13709 {
13710   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13711
13712   while (true)
13713     {
13714       cp_token *token;
13715       cp_cv_quals cv_qualifier;
13716
13717       /* Peek at the next token.  */
13718       token = cp_lexer_peek_token (parser->lexer);
13719       /* See if it's a cv-qualifier.  */
13720       switch (token->keyword)
13721         {
13722         case RID_CONST:
13723           cv_qualifier = TYPE_QUAL_CONST;
13724           break;
13725
13726         case RID_VOLATILE:
13727           cv_qualifier = TYPE_QUAL_VOLATILE;
13728           break;
13729
13730         case RID_RESTRICT:
13731           cv_qualifier = TYPE_QUAL_RESTRICT;
13732           break;
13733
13734         default:
13735           cv_qualifier = TYPE_UNQUALIFIED;
13736           break;
13737         }
13738
13739       if (!cv_qualifier)
13740         break;
13741
13742       if (cv_quals & cv_qualifier)
13743         {
13744           error ("%Hduplicate cv-qualifier", &token->location);
13745           cp_lexer_purge_token (parser->lexer);
13746         }
13747       else
13748         {
13749           cp_lexer_consume_token (parser->lexer);
13750           cv_quals |= cv_qualifier;
13751         }
13752     }
13753
13754   return cv_quals;
13755 }
13756
13757 /* Parse a late-specified return type, if any.  This is not a separate
13758    non-terminal, but part of a function declarator, which looks like
13759
13760    -> trailing-type-specifier-seq abstract-declarator(opt)
13761
13762    Returns the type indicated by the type-id.  */
13763
13764 static tree
13765 cp_parser_late_return_type_opt (cp_parser* parser)
13766 {
13767   cp_token *token;
13768
13769   /* Peek at the next token.  */
13770   token = cp_lexer_peek_token (parser->lexer);
13771   /* A late-specified return type is indicated by an initial '->'. */
13772   if (token->type != CPP_DEREF)
13773     return NULL_TREE;
13774
13775   /* Consume the ->.  */
13776   cp_lexer_consume_token (parser->lexer);
13777
13778   return cp_parser_trailing_type_id (parser);
13779 }
13780
13781 /* Parse a declarator-id.
13782
13783    declarator-id:
13784      id-expression
13785      :: [opt] nested-name-specifier [opt] type-name
13786
13787    In the `id-expression' case, the value returned is as for
13788    cp_parser_id_expression if the id-expression was an unqualified-id.
13789    If the id-expression was a qualified-id, then a SCOPE_REF is
13790    returned.  The first operand is the scope (either a NAMESPACE_DECL
13791    or TREE_TYPE), but the second is still just a representation of an
13792    unqualified-id.  */
13793
13794 static tree
13795 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13796 {
13797   tree id;
13798   /* The expression must be an id-expression.  Assume that qualified
13799      names are the names of types so that:
13800
13801        template <class T>
13802        int S<T>::R::i = 3;
13803
13804      will work; we must treat `S<T>::R' as the name of a type.
13805      Similarly, assume that qualified names are templates, where
13806      required, so that:
13807
13808        template <class T>
13809        int S<T>::R<T>::i = 3;
13810
13811      will work, too.  */
13812   id = cp_parser_id_expression (parser,
13813                                 /*template_keyword_p=*/false,
13814                                 /*check_dependency_p=*/false,
13815                                 /*template_p=*/NULL,
13816                                 /*declarator_p=*/true,
13817                                 optional_p);
13818   if (id && BASELINK_P (id))
13819     id = BASELINK_FUNCTIONS (id);
13820   return id;
13821 }
13822
13823 /* Parse a type-id.
13824
13825    type-id:
13826      type-specifier-seq abstract-declarator [opt]
13827
13828    Returns the TYPE specified.  */
13829
13830 static tree
13831 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
13832                      bool is_trailing_return)
13833 {
13834   cp_decl_specifier_seq type_specifier_seq;
13835   cp_declarator *abstract_declarator;
13836
13837   /* Parse the type-specifier-seq.  */
13838   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13839                                 is_trailing_return,
13840                                 &type_specifier_seq);
13841   if (type_specifier_seq.type == error_mark_node)
13842     return error_mark_node;
13843
13844   /* There might or might not be an abstract declarator.  */
13845   cp_parser_parse_tentatively (parser);
13846   /* Look for the declarator.  */
13847   abstract_declarator
13848     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13849                             /*parenthesized_p=*/NULL,
13850                             /*member_p=*/false);
13851   /* Check to see if there really was a declarator.  */
13852   if (!cp_parser_parse_definitely (parser))
13853     abstract_declarator = NULL;
13854
13855   if (type_specifier_seq.type
13856       && type_uses_auto (type_specifier_seq.type))
13857     {
13858       /* A type-id with type 'auto' is only ok if the abstract declarator
13859          is a function declarator with a late-specified return type.  */
13860       if (abstract_declarator
13861           && abstract_declarator->kind == cdk_function
13862           && abstract_declarator->u.function.late_return_type)
13863         /* OK */;
13864       else
13865         {
13866           error ("invalid use of %<auto%>");
13867           return error_mark_node;
13868         }
13869     }
13870   
13871   return groktypename (&type_specifier_seq, abstract_declarator,
13872                        is_template_arg);
13873 }
13874
13875 static tree cp_parser_type_id (cp_parser *parser)
13876 {
13877   return cp_parser_type_id_1 (parser, false, false);
13878 }
13879
13880 static tree cp_parser_template_type_arg (cp_parser *parser)
13881 {
13882   return cp_parser_type_id_1 (parser, true, false);
13883 }
13884
13885 static tree cp_parser_trailing_type_id (cp_parser *parser)
13886 {
13887   return cp_parser_type_id_1 (parser, false, true);
13888 }
13889
13890 /* Parse a type-specifier-seq.
13891
13892    type-specifier-seq:
13893      type-specifier type-specifier-seq [opt]
13894
13895    GNU extension:
13896
13897    type-specifier-seq:
13898      attributes type-specifier-seq [opt]
13899
13900    If IS_CONDITION is true, we are at the start of a "condition",
13901    e.g., we've just seen "if (".
13902
13903    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
13904    i.e. we've just seen "->".
13905
13906    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13907
13908 static void
13909 cp_parser_type_specifier_seq (cp_parser* parser,
13910                               bool is_condition,
13911                               bool is_trailing_return,
13912                               cp_decl_specifier_seq *type_specifier_seq)
13913 {
13914   bool seen_type_specifier = false;
13915   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13916   cp_token *start_token = NULL;
13917
13918   /* Clear the TYPE_SPECIFIER_SEQ.  */
13919   clear_decl_specs (type_specifier_seq);
13920
13921   /* In the context of a trailing return type, enum E { } is an
13922      elaborated-type-specifier followed by a function-body, not an
13923      enum-specifier.  */
13924   if (is_trailing_return)
13925     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
13926
13927   /* Parse the type-specifiers and attributes.  */
13928   while (true)
13929     {
13930       tree type_specifier;
13931       bool is_cv_qualifier;
13932
13933       /* Check for attributes first.  */
13934       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13935         {
13936           type_specifier_seq->attributes =
13937             chainon (type_specifier_seq->attributes,
13938                      cp_parser_attributes_opt (parser));
13939           continue;
13940         }
13941
13942       /* record the token of the beginning of the type specifier seq,
13943          for error reporting purposes*/
13944      if (!start_token)
13945        start_token = cp_lexer_peek_token (parser->lexer);
13946
13947       /* Look for the type-specifier.  */
13948       type_specifier = cp_parser_type_specifier (parser,
13949                                                  flags,
13950                                                  type_specifier_seq,
13951                                                  /*is_declaration=*/false,
13952                                                  NULL,
13953                                                  &is_cv_qualifier);
13954       if (!type_specifier)
13955         {
13956           /* If the first type-specifier could not be found, this is not a
13957              type-specifier-seq at all.  */
13958           if (!seen_type_specifier)
13959             {
13960               cp_parser_error (parser, "expected type-specifier");
13961               type_specifier_seq->type = error_mark_node;
13962               return;
13963             }
13964           /* If subsequent type-specifiers could not be found, the
13965              type-specifier-seq is complete.  */
13966           break;
13967         }
13968
13969       seen_type_specifier = true;
13970       /* The standard says that a condition can be:
13971
13972             type-specifier-seq declarator = assignment-expression
13973
13974          However, given:
13975
13976            struct S {};
13977            if (int S = ...)
13978
13979          we should treat the "S" as a declarator, not as a
13980          type-specifier.  The standard doesn't say that explicitly for
13981          type-specifier-seq, but it does say that for
13982          decl-specifier-seq in an ordinary declaration.  Perhaps it
13983          would be clearer just to allow a decl-specifier-seq here, and
13984          then add a semantic restriction that if any decl-specifiers
13985          that are not type-specifiers appear, the program is invalid.  */
13986       if (is_condition && !is_cv_qualifier)
13987         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13988     }
13989
13990   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13991 }
13992
13993 /* Parse a parameter-declaration-clause.
13994
13995    parameter-declaration-clause:
13996      parameter-declaration-list [opt] ... [opt]
13997      parameter-declaration-list , ...
13998
13999    Returns a representation for the parameter declarations.  A return
14000    value of NULL indicates a parameter-declaration-clause consisting
14001    only of an ellipsis.  */
14002
14003 static tree
14004 cp_parser_parameter_declaration_clause (cp_parser* parser)
14005 {
14006   tree parameters;
14007   cp_token *token;
14008   bool ellipsis_p;
14009   bool is_error;
14010
14011   /* Peek at the next token.  */
14012   token = cp_lexer_peek_token (parser->lexer);
14013   /* Check for trivial parameter-declaration-clauses.  */
14014   if (token->type == CPP_ELLIPSIS)
14015     {
14016       /* Consume the `...' token.  */
14017       cp_lexer_consume_token (parser->lexer);
14018       return NULL_TREE;
14019     }
14020   else if (token->type == CPP_CLOSE_PAREN)
14021     /* There are no parameters.  */
14022     {
14023 #ifndef NO_IMPLICIT_EXTERN_C
14024       if (in_system_header && current_class_type == NULL
14025           && current_lang_name == lang_name_c)
14026         return NULL_TREE;
14027       else
14028 #endif
14029         return void_list_node;
14030     }
14031   /* Check for `(void)', too, which is a special case.  */
14032   else if (token->keyword == RID_VOID
14033            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14034                == CPP_CLOSE_PAREN))
14035     {
14036       /* Consume the `void' token.  */
14037       cp_lexer_consume_token (parser->lexer);
14038       /* There are no parameters.  */
14039       return void_list_node;
14040     }
14041
14042   /* Parse the parameter-declaration-list.  */
14043   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14044   /* If a parse error occurred while parsing the
14045      parameter-declaration-list, then the entire
14046      parameter-declaration-clause is erroneous.  */
14047   if (is_error)
14048     return NULL;
14049
14050   /* Peek at the next token.  */
14051   token = cp_lexer_peek_token (parser->lexer);
14052   /* If it's a `,', the clause should terminate with an ellipsis.  */
14053   if (token->type == CPP_COMMA)
14054     {
14055       /* Consume the `,'.  */
14056       cp_lexer_consume_token (parser->lexer);
14057       /* Expect an ellipsis.  */
14058       ellipsis_p
14059         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14060     }
14061   /* It might also be `...' if the optional trailing `,' was
14062      omitted.  */
14063   else if (token->type == CPP_ELLIPSIS)
14064     {
14065       /* Consume the `...' token.  */
14066       cp_lexer_consume_token (parser->lexer);
14067       /* And remember that we saw it.  */
14068       ellipsis_p = true;
14069     }
14070   else
14071     ellipsis_p = false;
14072
14073   /* Finish the parameter list.  */
14074   if (!ellipsis_p)
14075     parameters = chainon (parameters, void_list_node);
14076
14077   return parameters;
14078 }
14079
14080 /* Parse a parameter-declaration-list.
14081
14082    parameter-declaration-list:
14083      parameter-declaration
14084      parameter-declaration-list , parameter-declaration
14085
14086    Returns a representation of the parameter-declaration-list, as for
14087    cp_parser_parameter_declaration_clause.  However, the
14088    `void_list_node' is never appended to the list.  Upon return,
14089    *IS_ERROR will be true iff an error occurred.  */
14090
14091 static tree
14092 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14093 {
14094   tree parameters = NULL_TREE;
14095   tree *tail = &parameters; 
14096   bool saved_in_unbraced_linkage_specification_p;
14097
14098   /* Assume all will go well.  */
14099   *is_error = false;
14100   /* The special considerations that apply to a function within an
14101      unbraced linkage specifications do not apply to the parameters
14102      to the function.  */
14103   saved_in_unbraced_linkage_specification_p 
14104     = parser->in_unbraced_linkage_specification_p;
14105   parser->in_unbraced_linkage_specification_p = false;
14106
14107   /* Look for more parameters.  */
14108   while (true)
14109     {
14110       cp_parameter_declarator *parameter;
14111       tree decl = error_mark_node;
14112       bool parenthesized_p;
14113       /* Parse the parameter.  */
14114       parameter
14115         = cp_parser_parameter_declaration (parser,
14116                                            /*template_parm_p=*/false,
14117                                            &parenthesized_p);
14118
14119       /* We don't know yet if the enclosing context is deprecated, so wait
14120          and warn in grokparms if appropriate.  */
14121       deprecated_state = DEPRECATED_SUPPRESS;
14122
14123       if (parameter)
14124         decl = grokdeclarator (parameter->declarator,
14125                                &parameter->decl_specifiers,
14126                                PARM,
14127                                parameter->default_argument != NULL_TREE,
14128                                &parameter->decl_specifiers.attributes);
14129
14130       deprecated_state = DEPRECATED_NORMAL;
14131
14132       /* If a parse error occurred parsing the parameter declaration,
14133          then the entire parameter-declaration-list is erroneous.  */
14134       if (decl == error_mark_node)
14135         {
14136           *is_error = true;
14137           parameters = error_mark_node;
14138           break;
14139         }
14140
14141       if (parameter->decl_specifiers.attributes)
14142         cplus_decl_attributes (&decl,
14143                                parameter->decl_specifiers.attributes,
14144                                0);
14145       if (DECL_NAME (decl))
14146         decl = pushdecl (decl);
14147
14148       /* Add the new parameter to the list.  */
14149       *tail = build_tree_list (parameter->default_argument, decl);
14150       tail = &TREE_CHAIN (*tail);
14151
14152       /* Peek at the next token.  */
14153       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14154           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14155           /* These are for Objective-C++ */
14156           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14157           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14158         /* The parameter-declaration-list is complete.  */
14159         break;
14160       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14161         {
14162           cp_token *token;
14163
14164           /* Peek at the next token.  */
14165           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14166           /* If it's an ellipsis, then the list is complete.  */
14167           if (token->type == CPP_ELLIPSIS)
14168             break;
14169           /* Otherwise, there must be more parameters.  Consume the
14170              `,'.  */
14171           cp_lexer_consume_token (parser->lexer);
14172           /* When parsing something like:
14173
14174                 int i(float f, double d)
14175
14176              we can tell after seeing the declaration for "f" that we
14177              are not looking at an initialization of a variable "i",
14178              but rather at the declaration of a function "i".
14179
14180              Due to the fact that the parsing of template arguments
14181              (as specified to a template-id) requires backtracking we
14182              cannot use this technique when inside a template argument
14183              list.  */
14184           if (!parser->in_template_argument_list_p
14185               && !parser->in_type_id_in_expr_p
14186               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14187               /* However, a parameter-declaration of the form
14188                  "foat(f)" (which is a valid declaration of a
14189                  parameter "f") can also be interpreted as an
14190                  expression (the conversion of "f" to "float").  */
14191               && !parenthesized_p)
14192             cp_parser_commit_to_tentative_parse (parser);
14193         }
14194       else
14195         {
14196           cp_parser_error (parser, "expected %<,%> or %<...%>");
14197           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14198             cp_parser_skip_to_closing_parenthesis (parser,
14199                                                    /*recovering=*/true,
14200                                                    /*or_comma=*/false,
14201                                                    /*consume_paren=*/false);
14202           break;
14203         }
14204     }
14205
14206   parser->in_unbraced_linkage_specification_p
14207     = saved_in_unbraced_linkage_specification_p;
14208
14209   return parameters;
14210 }
14211
14212 /* Parse a parameter declaration.
14213
14214    parameter-declaration:
14215      decl-specifier-seq ... [opt] declarator
14216      decl-specifier-seq declarator = assignment-expression
14217      decl-specifier-seq ... [opt] abstract-declarator [opt]
14218      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14219
14220    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14221    declares a template parameter.  (In that case, a non-nested `>'
14222    token encountered during the parsing of the assignment-expression
14223    is not interpreted as a greater-than operator.)
14224
14225    Returns a representation of the parameter, or NULL if an error
14226    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14227    true iff the declarator is of the form "(p)".  */
14228
14229 static cp_parameter_declarator *
14230 cp_parser_parameter_declaration (cp_parser *parser,
14231                                  bool template_parm_p,
14232                                  bool *parenthesized_p)
14233 {
14234   int declares_class_or_enum;
14235   bool greater_than_is_operator_p;
14236   cp_decl_specifier_seq decl_specifiers;
14237   cp_declarator *declarator;
14238   tree default_argument;
14239   cp_token *token = NULL, *declarator_token_start = NULL;
14240   const char *saved_message;
14241
14242   /* In a template parameter, `>' is not an operator.
14243
14244      [temp.param]
14245
14246      When parsing a default template-argument for a non-type
14247      template-parameter, the first non-nested `>' is taken as the end
14248      of the template parameter-list rather than a greater-than
14249      operator.  */
14250   greater_than_is_operator_p = !template_parm_p;
14251
14252   /* Type definitions may not appear in parameter types.  */
14253   saved_message = parser->type_definition_forbidden_message;
14254   parser->type_definition_forbidden_message
14255     = "types may not be defined in parameter types";
14256
14257   /* Parse the declaration-specifiers.  */
14258   cp_parser_decl_specifier_seq (parser,
14259                                 CP_PARSER_FLAGS_NONE,
14260                                 &decl_specifiers,
14261                                 &declares_class_or_enum);
14262   /* If an error occurred, there's no reason to attempt to parse the
14263      rest of the declaration.  */
14264   if (cp_parser_error_occurred (parser))
14265     {
14266       parser->type_definition_forbidden_message = saved_message;
14267       return NULL;
14268     }
14269
14270   /* Peek at the next token.  */
14271   token = cp_lexer_peek_token (parser->lexer);
14272
14273   /* If the next token is a `)', `,', `=', `>', or `...', then there
14274      is no declarator. However, when variadic templates are enabled,
14275      there may be a declarator following `...'.  */
14276   if (token->type == CPP_CLOSE_PAREN
14277       || token->type == CPP_COMMA
14278       || token->type == CPP_EQ
14279       || token->type == CPP_GREATER)
14280     {
14281       declarator = NULL;
14282       if (parenthesized_p)
14283         *parenthesized_p = false;
14284     }
14285   /* Otherwise, there should be a declarator.  */
14286   else
14287     {
14288       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14289       parser->default_arg_ok_p = false;
14290
14291       /* After seeing a decl-specifier-seq, if the next token is not a
14292          "(", there is no possibility that the code is a valid
14293          expression.  Therefore, if parsing tentatively, we commit at
14294          this point.  */
14295       if (!parser->in_template_argument_list_p
14296           /* In an expression context, having seen:
14297
14298                (int((char ...
14299
14300              we cannot be sure whether we are looking at a
14301              function-type (taking a "char" as a parameter) or a cast
14302              of some object of type "char" to "int".  */
14303           && !parser->in_type_id_in_expr_p
14304           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14305           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14306         cp_parser_commit_to_tentative_parse (parser);
14307       /* Parse the declarator.  */
14308       declarator_token_start = token;
14309       declarator = cp_parser_declarator (parser,
14310                                          CP_PARSER_DECLARATOR_EITHER,
14311                                          /*ctor_dtor_or_conv_p=*/NULL,
14312                                          parenthesized_p,
14313                                          /*member_p=*/false);
14314       parser->default_arg_ok_p = saved_default_arg_ok_p;
14315       /* After the declarator, allow more attributes.  */
14316       decl_specifiers.attributes
14317         = chainon (decl_specifiers.attributes,
14318                    cp_parser_attributes_opt (parser));
14319     }
14320
14321   /* If the next token is an ellipsis, and we have not seen a
14322      declarator name, and the type of the declarator contains parameter
14323      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14324      a parameter pack expansion expression. Otherwise, leave the
14325      ellipsis for a C-style variadic function. */
14326   token = cp_lexer_peek_token (parser->lexer);
14327   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14328     {
14329       tree type = decl_specifiers.type;
14330
14331       if (type && DECL_P (type))
14332         type = TREE_TYPE (type);
14333
14334       if (type
14335           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14336           && declarator_can_be_parameter_pack (declarator)
14337           && (!declarator || !declarator->parameter_pack_p)
14338           && uses_parameter_packs (type))
14339         {
14340           /* Consume the `...'. */
14341           cp_lexer_consume_token (parser->lexer);
14342           maybe_warn_variadic_templates ();
14343           
14344           /* Build a pack expansion type */
14345           if (declarator)
14346             declarator->parameter_pack_p = true;
14347           else
14348             decl_specifiers.type = make_pack_expansion (type);
14349         }
14350     }
14351
14352   /* The restriction on defining new types applies only to the type
14353      of the parameter, not to the default argument.  */
14354   parser->type_definition_forbidden_message = saved_message;
14355
14356   /* If the next token is `=', then process a default argument.  */
14357   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14358     {
14359       /* Consume the `='.  */
14360       cp_lexer_consume_token (parser->lexer);
14361
14362       /* If we are defining a class, then the tokens that make up the
14363          default argument must be saved and processed later.  */
14364       if (!template_parm_p && at_class_scope_p ()
14365           && TYPE_BEING_DEFINED (current_class_type))
14366         {
14367           unsigned depth = 0;
14368           int maybe_template_id = 0;
14369           cp_token *first_token;
14370           cp_token *token;
14371
14372           /* Add tokens until we have processed the entire default
14373              argument.  We add the range [first_token, token).  */
14374           first_token = cp_lexer_peek_token (parser->lexer);
14375           while (true)
14376             {
14377               bool done = false;
14378
14379               /* Peek at the next token.  */
14380               token = cp_lexer_peek_token (parser->lexer);
14381               /* What we do depends on what token we have.  */
14382               switch (token->type)
14383                 {
14384                   /* In valid code, a default argument must be
14385                      immediately followed by a `,' `)', or `...'.  */
14386                 case CPP_COMMA:
14387                   if (depth == 0 && maybe_template_id)
14388                     {
14389                       /* If we've seen a '<', we might be in a
14390                          template-argument-list.  Until Core issue 325 is
14391                          resolved, we don't know how this situation ought
14392                          to be handled, so try to DTRT.  We check whether
14393                          what comes after the comma is a valid parameter
14394                          declaration list.  If it is, then the comma ends
14395                          the default argument; otherwise the default
14396                          argument continues.  */
14397                       bool error = false;
14398                       tree t;
14399
14400                       /* Set ITALP so cp_parser_parameter_declaration_list
14401                          doesn't decide to commit to this parse.  */
14402                       bool saved_italp = parser->in_template_argument_list_p;
14403                       parser->in_template_argument_list_p = true;
14404
14405                       cp_parser_parse_tentatively (parser);
14406                       cp_lexer_consume_token (parser->lexer);
14407                       begin_scope (sk_function_parms, NULL_TREE);
14408                       cp_parser_parameter_declaration_list (parser, &error);
14409                       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14410                         pop_binding (DECL_NAME (t), t);
14411                       leave_scope ();
14412                       if (!cp_parser_error_occurred (parser) && !error)
14413                         done = true;
14414                       cp_parser_abort_tentative_parse (parser);
14415
14416                       parser->in_template_argument_list_p = saved_italp;
14417                       break;
14418                     }
14419                 case CPP_CLOSE_PAREN:
14420                 case CPP_ELLIPSIS:
14421                   /* If we run into a non-nested `;', `}', or `]',
14422                      then the code is invalid -- but the default
14423                      argument is certainly over.  */
14424                 case CPP_SEMICOLON:
14425                 case CPP_CLOSE_BRACE:
14426                 case CPP_CLOSE_SQUARE:
14427                   if (depth == 0)
14428                     done = true;
14429                   /* Update DEPTH, if necessary.  */
14430                   else if (token->type == CPP_CLOSE_PAREN
14431                            || token->type == CPP_CLOSE_BRACE
14432                            || token->type == CPP_CLOSE_SQUARE)
14433                     --depth;
14434                   break;
14435
14436                 case CPP_OPEN_PAREN:
14437                 case CPP_OPEN_SQUARE:
14438                 case CPP_OPEN_BRACE:
14439                   ++depth;
14440                   break;
14441
14442                 case CPP_LESS:
14443                   if (depth == 0)
14444                     /* This might be the comparison operator, or it might
14445                        start a template argument list.  */
14446                     ++maybe_template_id;
14447                   break;
14448
14449                 case CPP_RSHIFT:
14450                   if (cxx_dialect == cxx98)
14451                     break;
14452                   /* Fall through for C++0x, which treats the `>>'
14453                      operator like two `>' tokens in certain
14454                      cases.  */
14455
14456                 case CPP_GREATER:
14457                   if (depth == 0)
14458                     {
14459                       /* This might be an operator, or it might close a
14460                          template argument list.  But if a previous '<'
14461                          started a template argument list, this will have
14462                          closed it, so we can't be in one anymore.  */
14463                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14464                       if (maybe_template_id < 0)
14465                         maybe_template_id = 0;
14466                     }
14467                   break;
14468
14469                   /* If we run out of tokens, issue an error message.  */
14470                 case CPP_EOF:
14471                 case CPP_PRAGMA_EOL:
14472                   error ("%Hfile ends in default argument", &token->location);
14473                   done = true;
14474                   break;
14475
14476                 case CPP_NAME:
14477                 case CPP_SCOPE:
14478                   /* In these cases, we should look for template-ids.
14479                      For example, if the default argument is
14480                      `X<int, double>()', we need to do name lookup to
14481                      figure out whether or not `X' is a template; if
14482                      so, the `,' does not end the default argument.
14483
14484                      That is not yet done.  */
14485                   break;
14486
14487                 default:
14488                   break;
14489                 }
14490
14491               /* If we've reached the end, stop.  */
14492               if (done)
14493                 break;
14494
14495               /* Add the token to the token block.  */
14496               token = cp_lexer_consume_token (parser->lexer);
14497             }
14498
14499           /* Create a DEFAULT_ARG to represent the unparsed default
14500              argument.  */
14501           default_argument = make_node (DEFAULT_ARG);
14502           DEFARG_TOKENS (default_argument)
14503             = cp_token_cache_new (first_token, token);
14504           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14505         }
14506       /* Outside of a class definition, we can just parse the
14507          assignment-expression.  */
14508       else
14509         {
14510           token = cp_lexer_peek_token (parser->lexer);
14511           default_argument 
14512             = cp_parser_default_argument (parser, template_parm_p);
14513         }
14514
14515       if (!parser->default_arg_ok_p)
14516         {
14517           if (flag_permissive)
14518             warning (0, "deprecated use of default argument for parameter of non-function");
14519           else
14520             {
14521               error ("%Hdefault arguments are only "
14522                      "permitted for function parameters",
14523                      &token->location);
14524               default_argument = NULL_TREE;
14525             }
14526         }
14527       else if ((declarator && declarator->parameter_pack_p)
14528                || (decl_specifiers.type
14529                    && PACK_EXPANSION_P (decl_specifiers.type)))
14530         {
14531           const char* kind = template_parm_p? "template " : "";
14532           
14533           /* Find the name of the parameter pack.  */     
14534           cp_declarator *id_declarator = declarator;
14535           while (id_declarator && id_declarator->kind != cdk_id)
14536             id_declarator = id_declarator->declarator;
14537           
14538           if (id_declarator && id_declarator->kind == cdk_id)
14539             error ("%H%sparameter pack %qD cannot have a default argument",
14540                    &declarator_token_start->location,
14541                    kind, id_declarator->u.id.unqualified_name);
14542           else
14543             error ("%H%sparameter pack cannot have a default argument",
14544                    &declarator_token_start->location, kind);
14545           
14546           default_argument = NULL_TREE;
14547         }
14548     }
14549   else
14550     default_argument = NULL_TREE;
14551
14552   return make_parameter_declarator (&decl_specifiers,
14553                                     declarator,
14554                                     default_argument);
14555 }
14556
14557 /* Parse a default argument and return it.
14558
14559    TEMPLATE_PARM_P is true if this is a default argument for a
14560    non-type template parameter.  */
14561 static tree
14562 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14563 {
14564   tree default_argument = NULL_TREE;
14565   bool saved_greater_than_is_operator_p;
14566   bool saved_local_variables_forbidden_p;
14567
14568   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14569      set correctly.  */
14570   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14571   parser->greater_than_is_operator_p = !template_parm_p;
14572   /* Local variable names (and the `this' keyword) may not
14573      appear in a default argument.  */
14574   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14575   parser->local_variables_forbidden_p = true;
14576   /* The default argument expression may cause implicitly
14577      defined member functions to be synthesized, which will
14578      result in garbage collection.  We must treat this
14579      situation as if we were within the body of function so as
14580      to avoid collecting live data on the stack.  */
14581   ++function_depth;
14582   /* Parse the assignment-expression.  */
14583   if (template_parm_p)
14584     push_deferring_access_checks (dk_no_deferred);
14585   default_argument
14586     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14587   if (template_parm_p)
14588     pop_deferring_access_checks ();
14589   /* Restore saved state.  */
14590   --function_depth;
14591   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14592   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14593
14594   return default_argument;
14595 }
14596
14597 /* Parse a function-body.
14598
14599    function-body:
14600      compound_statement  */
14601
14602 static void
14603 cp_parser_function_body (cp_parser *parser)
14604 {
14605   cp_parser_compound_statement (parser, NULL, false);
14606 }
14607
14608 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14609    true if a ctor-initializer was present.  */
14610
14611 static bool
14612 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14613 {
14614   tree body;
14615   bool ctor_initializer_p;
14616
14617   /* Begin the function body.  */
14618   body = begin_function_body ();
14619   /* Parse the optional ctor-initializer.  */
14620   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14621   /* Parse the function-body.  */
14622   cp_parser_function_body (parser);
14623   /* Finish the function body.  */
14624   finish_function_body (body);
14625
14626   return ctor_initializer_p;
14627 }
14628
14629 /* Parse an initializer.
14630
14631    initializer:
14632      = initializer-clause
14633      ( expression-list )
14634
14635    Returns an expression representing the initializer.  If no
14636    initializer is present, NULL_TREE is returned.
14637
14638    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14639    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14640    set to TRUE if there is no initializer present.  If there is an
14641    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14642    is set to true; otherwise it is set to false.  */
14643
14644 static tree
14645 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14646                        bool* non_constant_p)
14647 {
14648   cp_token *token;
14649   tree init;
14650
14651   /* Peek at the next token.  */
14652   token = cp_lexer_peek_token (parser->lexer);
14653
14654   /* Let our caller know whether or not this initializer was
14655      parenthesized.  */
14656   *is_direct_init = (token->type != CPP_EQ);
14657   /* Assume that the initializer is constant.  */
14658   *non_constant_p = false;
14659
14660   if (token->type == CPP_EQ)
14661     {
14662       /* Consume the `='.  */
14663       cp_lexer_consume_token (parser->lexer);
14664       /* Parse the initializer-clause.  */
14665       init = cp_parser_initializer_clause (parser, non_constant_p);
14666     }
14667   else if (token->type == CPP_OPEN_PAREN)
14668     init = cp_parser_parenthesized_expression_list (parser, false,
14669                                                     /*cast_p=*/false,
14670                                                     /*allow_expansion_p=*/true,
14671                                                     non_constant_p);
14672   else if (token->type == CPP_OPEN_BRACE)
14673     {
14674       maybe_warn_cpp0x ("extended initializer lists");
14675       init = cp_parser_braced_list (parser, non_constant_p);
14676       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14677     }
14678   else
14679     {
14680       /* Anything else is an error.  */
14681       cp_parser_error (parser, "expected initializer");
14682       init = error_mark_node;
14683     }
14684
14685   return init;
14686 }
14687
14688 /* Parse an initializer-clause.
14689
14690    initializer-clause:
14691      assignment-expression
14692      braced-init-list
14693
14694    Returns an expression representing the initializer.
14695
14696    If the `assignment-expression' production is used the value
14697    returned is simply a representation for the expression.
14698
14699    Otherwise, calls cp_parser_braced_list.  */
14700
14701 static tree
14702 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14703 {
14704   tree initializer;
14705
14706   /* Assume the expression is constant.  */
14707   *non_constant_p = false;
14708
14709   /* If it is not a `{', then we are looking at an
14710      assignment-expression.  */
14711   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14712     {
14713       initializer
14714         = cp_parser_constant_expression (parser,
14715                                         /*allow_non_constant_p=*/true,
14716                                         non_constant_p);
14717       if (!*non_constant_p)
14718         initializer = fold_non_dependent_expr (initializer);
14719     }
14720   else
14721     initializer = cp_parser_braced_list (parser, non_constant_p);
14722
14723   return initializer;
14724 }
14725
14726 /* Parse a brace-enclosed initializer list.
14727
14728    braced-init-list:
14729      { initializer-list , [opt] }
14730      { }
14731
14732    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14733    the elements of the initializer-list (or NULL, if the last
14734    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14735    NULL_TREE.  There is no way to detect whether or not the optional
14736    trailing `,' was provided.  NON_CONSTANT_P is as for
14737    cp_parser_initializer.  */     
14738
14739 static tree
14740 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14741 {
14742   tree initializer;
14743
14744   /* Consume the `{' token.  */
14745   cp_lexer_consume_token (parser->lexer);
14746   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14747   initializer = make_node (CONSTRUCTOR);
14748   /* If it's not a `}', then there is a non-trivial initializer.  */
14749   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14750     {
14751       /* Parse the initializer list.  */
14752       CONSTRUCTOR_ELTS (initializer)
14753         = cp_parser_initializer_list (parser, non_constant_p);
14754       /* A trailing `,' token is allowed.  */
14755       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14756         cp_lexer_consume_token (parser->lexer);
14757     }
14758   /* Now, there should be a trailing `}'.  */
14759   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14760   TREE_TYPE (initializer) = init_list_type_node;
14761   return initializer;
14762 }
14763
14764 /* Parse an initializer-list.
14765
14766    initializer-list:
14767      initializer-clause ... [opt]
14768      initializer-list , initializer-clause ... [opt]
14769
14770    GNU Extension:
14771
14772    initializer-list:
14773      identifier : initializer-clause
14774      initializer-list, identifier : initializer-clause
14775
14776    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14777    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14778    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14779    as for cp_parser_initializer.  */
14780
14781 static VEC(constructor_elt,gc) *
14782 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14783 {
14784   VEC(constructor_elt,gc) *v = NULL;
14785
14786   /* Assume all of the expressions are constant.  */
14787   *non_constant_p = false;
14788
14789   /* Parse the rest of the list.  */
14790   while (true)
14791     {
14792       cp_token *token;
14793       tree identifier;
14794       tree initializer;
14795       bool clause_non_constant_p;
14796
14797       /* If the next token is an identifier and the following one is a
14798          colon, we are looking at the GNU designated-initializer
14799          syntax.  */
14800       if (cp_parser_allow_gnu_extensions_p (parser)
14801           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14802           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14803         {
14804           /* Warn the user that they are using an extension.  */
14805           pedwarn (input_location, OPT_pedantic, 
14806                    "ISO C++ does not allow designated initializers");
14807           /* Consume the identifier.  */
14808           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14809           /* Consume the `:'.  */
14810           cp_lexer_consume_token (parser->lexer);
14811         }
14812       else
14813         identifier = NULL_TREE;
14814
14815       /* Parse the initializer.  */
14816       initializer = cp_parser_initializer_clause (parser,
14817                                                   &clause_non_constant_p);
14818       /* If any clause is non-constant, so is the entire initializer.  */
14819       if (clause_non_constant_p)
14820         *non_constant_p = true;
14821
14822       /* If we have an ellipsis, this is an initializer pack
14823          expansion.  */
14824       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14825         {
14826           /* Consume the `...'.  */
14827           cp_lexer_consume_token (parser->lexer);
14828
14829           /* Turn the initializer into an initializer expansion.  */
14830           initializer = make_pack_expansion (initializer);
14831         }
14832
14833       /* Add it to the vector.  */
14834       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14835
14836       /* If the next token is not a comma, we have reached the end of
14837          the list.  */
14838       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14839         break;
14840
14841       /* Peek at the next token.  */
14842       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14843       /* If the next token is a `}', then we're still done.  An
14844          initializer-clause can have a trailing `,' after the
14845          initializer-list and before the closing `}'.  */
14846       if (token->type == CPP_CLOSE_BRACE)
14847         break;
14848
14849       /* Consume the `,' token.  */
14850       cp_lexer_consume_token (parser->lexer);
14851     }
14852
14853   return v;
14854 }
14855
14856 /* Classes [gram.class] */
14857
14858 /* Parse a class-name.
14859
14860    class-name:
14861      identifier
14862      template-id
14863
14864    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14865    to indicate that names looked up in dependent types should be
14866    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14867    keyword has been used to indicate that the name that appears next
14868    is a template.  TAG_TYPE indicates the explicit tag given before
14869    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14870    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14871    is the class being defined in a class-head.
14872
14873    Returns the TYPE_DECL representing the class.  */
14874
14875 static tree
14876 cp_parser_class_name (cp_parser *parser,
14877                       bool typename_keyword_p,
14878                       bool template_keyword_p,
14879                       enum tag_types tag_type,
14880                       bool check_dependency_p,
14881                       bool class_head_p,
14882                       bool is_declaration)
14883 {
14884   tree decl;
14885   tree scope;
14886   bool typename_p;
14887   cp_token *token;
14888   tree identifier = NULL_TREE;
14889
14890   /* All class-names start with an identifier.  */
14891   token = cp_lexer_peek_token (parser->lexer);
14892   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14893     {
14894       cp_parser_error (parser, "expected class-name");
14895       return error_mark_node;
14896     }
14897
14898   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14899      to a template-id, so we save it here.  */
14900   scope = parser->scope;
14901   if (scope == error_mark_node)
14902     return error_mark_node;
14903
14904   /* Any name names a type if we're following the `typename' keyword
14905      in a qualified name where the enclosing scope is type-dependent.  */
14906   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14907                 && dependent_type_p (scope));
14908   /* Handle the common case (an identifier, but not a template-id)
14909      efficiently.  */
14910   if (token->type == CPP_NAME
14911       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14912     {
14913       cp_token *identifier_token;
14914       bool ambiguous_p;
14915
14916       /* Look for the identifier.  */
14917       identifier_token = cp_lexer_peek_token (parser->lexer);
14918       ambiguous_p = identifier_token->ambiguous_p;
14919       identifier = cp_parser_identifier (parser);
14920       /* If the next token isn't an identifier, we are certainly not
14921          looking at a class-name.  */
14922       if (identifier == error_mark_node)
14923         decl = error_mark_node;
14924       /* If we know this is a type-name, there's no need to look it
14925          up.  */
14926       else if (typename_p)
14927         decl = identifier;
14928       else
14929         {
14930           tree ambiguous_decls;
14931           /* If we already know that this lookup is ambiguous, then
14932              we've already issued an error message; there's no reason
14933              to check again.  */
14934           if (ambiguous_p)
14935             {
14936               cp_parser_simulate_error (parser);
14937               return error_mark_node;
14938             }
14939           /* If the next token is a `::', then the name must be a type
14940              name.
14941
14942              [basic.lookup.qual]
14943
14944              During the lookup for a name preceding the :: scope
14945              resolution operator, object, function, and enumerator
14946              names are ignored.  */
14947           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14948             tag_type = typename_type;
14949           /* Look up the name.  */
14950           decl = cp_parser_lookup_name (parser, identifier,
14951                                         tag_type,
14952                                         /*is_template=*/false,
14953                                         /*is_namespace=*/false,
14954                                         check_dependency_p,
14955                                         &ambiguous_decls,
14956                                         identifier_token->location);
14957           if (ambiguous_decls)
14958             {
14959               error ("%Hreference to %qD is ambiguous",
14960                      &identifier_token->location, identifier);
14961               print_candidates (ambiguous_decls);
14962               if (cp_parser_parsing_tentatively (parser))
14963                 {
14964                   identifier_token->ambiguous_p = true;
14965                   cp_parser_simulate_error (parser);
14966                 }
14967               return error_mark_node;
14968             }
14969         }
14970     }
14971   else
14972     {
14973       /* Try a template-id.  */
14974       decl = cp_parser_template_id (parser, template_keyword_p,
14975                                     check_dependency_p,
14976                                     is_declaration);
14977       if (decl == error_mark_node)
14978         return error_mark_node;
14979     }
14980
14981   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14982
14983   /* If this is a typename, create a TYPENAME_TYPE.  */
14984   if (typename_p && decl != error_mark_node)
14985     {
14986       decl = make_typename_type (scope, decl, typename_type,
14987                                  /*complain=*/tf_error);
14988       if (decl != error_mark_node)
14989         decl = TYPE_NAME (decl);
14990     }
14991
14992   /* Check to see that it is really the name of a class.  */
14993   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14994       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14995       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14996     /* Situations like this:
14997
14998          template <typename T> struct A {
14999            typename T::template X<int>::I i;
15000          };
15001
15002        are problematic.  Is `T::template X<int>' a class-name?  The
15003        standard does not seem to be definitive, but there is no other
15004        valid interpretation of the following `::'.  Therefore, those
15005        names are considered class-names.  */
15006     {
15007       decl = make_typename_type (scope, decl, tag_type, tf_error);
15008       if (decl != error_mark_node)
15009         decl = TYPE_NAME (decl);
15010     }
15011   else if (TREE_CODE (decl) != TYPE_DECL
15012            || TREE_TYPE (decl) == error_mark_node
15013            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15014     decl = error_mark_node;
15015
15016   if (decl == error_mark_node)
15017     cp_parser_error (parser, "expected class-name");
15018   else if (identifier && !parser->scope)
15019     maybe_note_name_used_in_class (identifier, decl);
15020
15021   return decl;
15022 }
15023
15024 /* Parse a class-specifier.
15025
15026    class-specifier:
15027      class-head { member-specification [opt] }
15028
15029    Returns the TREE_TYPE representing the class.  */
15030
15031 static tree
15032 cp_parser_class_specifier (cp_parser* parser)
15033 {
15034   cp_token *token;
15035   tree type;
15036   tree attributes = NULL_TREE;
15037   int has_trailing_semicolon;
15038   bool nested_name_specifier_p;
15039   unsigned saved_num_template_parameter_lists;
15040   bool saved_in_function_body;
15041   bool saved_in_unbraced_linkage_specification_p;
15042   tree old_scope = NULL_TREE;
15043   tree scope = NULL_TREE;
15044   tree bases;
15045
15046   push_deferring_access_checks (dk_no_deferred);
15047
15048   /* Parse the class-head.  */
15049   type = cp_parser_class_head (parser,
15050                                &nested_name_specifier_p,
15051                                &attributes,
15052                                &bases);
15053   /* If the class-head was a semantic disaster, skip the entire body
15054      of the class.  */
15055   if (!type)
15056     {
15057       cp_parser_skip_to_end_of_block_or_statement (parser);
15058       pop_deferring_access_checks ();
15059       return error_mark_node;
15060     }
15061
15062   /* Look for the `{'.  */
15063   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15064     {
15065       pop_deferring_access_checks ();
15066       return error_mark_node;
15067     }
15068
15069   /* Process the base classes. If they're invalid, skip the 
15070      entire class body.  */
15071   if (!xref_basetypes (type, bases))
15072     {
15073       /* Consuming the closing brace yields better error messages
15074          later on.  */
15075       if (cp_parser_skip_to_closing_brace (parser))
15076         cp_lexer_consume_token (parser->lexer);
15077       pop_deferring_access_checks ();
15078       return error_mark_node;
15079     }
15080
15081   /* Issue an error message if type-definitions are forbidden here.  */
15082   cp_parser_check_type_definition (parser);
15083   /* Remember that we are defining one more class.  */
15084   ++parser->num_classes_being_defined;
15085   /* Inside the class, surrounding template-parameter-lists do not
15086      apply.  */
15087   saved_num_template_parameter_lists
15088     = parser->num_template_parameter_lists;
15089   parser->num_template_parameter_lists = 0;
15090   /* We are not in a function body.  */
15091   saved_in_function_body = parser->in_function_body;
15092   parser->in_function_body = false;
15093   /* We are not immediately inside an extern "lang" block.  */
15094   saved_in_unbraced_linkage_specification_p
15095     = parser->in_unbraced_linkage_specification_p;
15096   parser->in_unbraced_linkage_specification_p = false;
15097
15098   /* Start the class.  */
15099   if (nested_name_specifier_p)
15100     {
15101       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15102       old_scope = push_inner_scope (scope);
15103     }
15104   type = begin_class_definition (type, attributes);
15105
15106   if (type == error_mark_node)
15107     /* If the type is erroneous, skip the entire body of the class.  */
15108     cp_parser_skip_to_closing_brace (parser);
15109   else
15110     /* Parse the member-specification.  */
15111     cp_parser_member_specification_opt (parser);
15112
15113   /* Look for the trailing `}'.  */
15114   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15115   /* We get better error messages by noticing a common problem: a
15116      missing trailing `;'.  */
15117   token = cp_lexer_peek_token (parser->lexer);
15118   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15119   /* Look for trailing attributes to apply to this class.  */
15120   if (cp_parser_allow_gnu_extensions_p (parser))
15121     attributes = cp_parser_attributes_opt (parser);
15122   if (type != error_mark_node)
15123     type = finish_struct (type, attributes);
15124   if (nested_name_specifier_p)
15125     pop_inner_scope (old_scope, scope);
15126   /* If this class is not itself within the scope of another class,
15127      then we need to parse the bodies of all of the queued function
15128      definitions.  Note that the queued functions defined in a class
15129      are not always processed immediately following the
15130      class-specifier for that class.  Consider:
15131
15132        struct A {
15133          struct B { void f() { sizeof (A); } };
15134        };
15135
15136      If `f' were processed before the processing of `A' were
15137      completed, there would be no way to compute the size of `A'.
15138      Note that the nesting we are interested in here is lexical --
15139      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15140      for:
15141
15142        struct A { struct B; };
15143        struct A::B { void f() { } };
15144
15145      there is no need to delay the parsing of `A::B::f'.  */
15146   if (--parser->num_classes_being_defined == 0)
15147     {
15148       tree queue_entry;
15149       tree fn;
15150       tree class_type = NULL_TREE;
15151       tree pushed_scope = NULL_TREE;
15152
15153       /* In a first pass, parse default arguments to the functions.
15154          Then, in a second pass, parse the bodies of the functions.
15155          This two-phased approach handles cases like:
15156
15157             struct S {
15158               void f() { g(); }
15159               void g(int i = 3);
15160             };
15161
15162          */
15163       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15164              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15165            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15166            TREE_PURPOSE (parser->unparsed_functions_queues)
15167              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15168         {
15169           fn = TREE_VALUE (queue_entry);
15170           /* If there are default arguments that have not yet been processed,
15171              take care of them now.  */
15172           if (class_type != TREE_PURPOSE (queue_entry))
15173             {
15174               if (pushed_scope)
15175                 pop_scope (pushed_scope);
15176               class_type = TREE_PURPOSE (queue_entry);
15177               pushed_scope = push_scope (class_type);
15178             }
15179           /* Make sure that any template parameters are in scope.  */
15180           maybe_begin_member_template_processing (fn);
15181           /* Parse the default argument expressions.  */
15182           cp_parser_late_parsing_default_args (parser, fn);
15183           /* Remove any template parameters from the symbol table.  */
15184           maybe_end_member_template_processing ();
15185         }
15186       if (pushed_scope)
15187         pop_scope (pushed_scope);
15188       /* Now parse the body of the functions.  */
15189       for (TREE_VALUE (parser->unparsed_functions_queues)
15190              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15191            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15192            TREE_VALUE (parser->unparsed_functions_queues)
15193              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15194         {
15195           /* Figure out which function we need to process.  */
15196           fn = TREE_VALUE (queue_entry);
15197           /* Parse the function.  */
15198           cp_parser_late_parsing_for_member (parser, fn);
15199         }
15200     }
15201
15202   /* Put back any saved access checks.  */
15203   pop_deferring_access_checks ();
15204
15205   /* Restore saved state.  */
15206   parser->in_function_body = saved_in_function_body;
15207   parser->num_template_parameter_lists
15208     = saved_num_template_parameter_lists;
15209   parser->in_unbraced_linkage_specification_p
15210     = saved_in_unbraced_linkage_specification_p;
15211
15212   return type;
15213 }
15214
15215 /* Parse a class-head.
15216
15217    class-head:
15218      class-key identifier [opt] base-clause [opt]
15219      class-key nested-name-specifier identifier base-clause [opt]
15220      class-key nested-name-specifier [opt] template-id
15221        base-clause [opt]
15222
15223    GNU Extensions:
15224      class-key attributes identifier [opt] base-clause [opt]
15225      class-key attributes nested-name-specifier identifier base-clause [opt]
15226      class-key attributes nested-name-specifier [opt] template-id
15227        base-clause [opt]
15228
15229    Upon return BASES is initialized to the list of base classes (or
15230    NULL, if there are none) in the same form returned by
15231    cp_parser_base_clause.
15232
15233    Returns the TYPE of the indicated class.  Sets
15234    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15235    involving a nested-name-specifier was used, and FALSE otherwise.
15236
15237    Returns error_mark_node if this is not a class-head.
15238
15239    Returns NULL_TREE if the class-head is syntactically valid, but
15240    semantically invalid in a way that means we should skip the entire
15241    body of the class.  */
15242
15243 static tree
15244 cp_parser_class_head (cp_parser* parser,
15245                       bool* nested_name_specifier_p,
15246                       tree *attributes_p,
15247                       tree *bases)
15248 {
15249   tree nested_name_specifier;
15250   enum tag_types class_key;
15251   tree id = NULL_TREE;
15252   tree type = NULL_TREE;
15253   tree attributes;
15254   bool template_id_p = false;
15255   bool qualified_p = false;
15256   bool invalid_nested_name_p = false;
15257   bool invalid_explicit_specialization_p = false;
15258   tree pushed_scope = NULL_TREE;
15259   unsigned num_templates;
15260   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15261   /* Assume no nested-name-specifier will be present.  */
15262   *nested_name_specifier_p = false;
15263   /* Assume no template parameter lists will be used in defining the
15264      type.  */
15265   num_templates = 0;
15266
15267   *bases = NULL_TREE;
15268
15269   /* Look for the class-key.  */
15270   class_key = cp_parser_class_key (parser);
15271   if (class_key == none_type)
15272     return error_mark_node;
15273
15274   /* Parse the attributes.  */
15275   attributes = cp_parser_attributes_opt (parser);
15276
15277   /* If the next token is `::', that is invalid -- but sometimes
15278      people do try to write:
15279
15280        struct ::S {};
15281
15282      Handle this gracefully by accepting the extra qualifier, and then
15283      issuing an error about it later if this really is a
15284      class-head.  If it turns out just to be an elaborated type
15285      specifier, remain silent.  */
15286   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15287     qualified_p = true;
15288
15289   push_deferring_access_checks (dk_no_check);
15290
15291   /* Determine the name of the class.  Begin by looking for an
15292      optional nested-name-specifier.  */
15293   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15294   nested_name_specifier
15295     = cp_parser_nested_name_specifier_opt (parser,
15296                                            /*typename_keyword_p=*/false,
15297                                            /*check_dependency_p=*/false,
15298                                            /*type_p=*/false,
15299                                            /*is_declaration=*/false);
15300   /* If there was a nested-name-specifier, then there *must* be an
15301      identifier.  */
15302   if (nested_name_specifier)
15303     {
15304       type_start_token = cp_lexer_peek_token (parser->lexer);
15305       /* Although the grammar says `identifier', it really means
15306          `class-name' or `template-name'.  You are only allowed to
15307          define a class that has already been declared with this
15308          syntax.
15309
15310          The proposed resolution for Core Issue 180 says that wherever
15311          you see `class T::X' you should treat `X' as a type-name.
15312
15313          It is OK to define an inaccessible class; for example:
15314
15315            class A { class B; };
15316            class A::B {};
15317
15318          We do not know if we will see a class-name, or a
15319          template-name.  We look for a class-name first, in case the
15320          class-name is a template-id; if we looked for the
15321          template-name first we would stop after the template-name.  */
15322       cp_parser_parse_tentatively (parser);
15323       type = cp_parser_class_name (parser,
15324                                    /*typename_keyword_p=*/false,
15325                                    /*template_keyword_p=*/false,
15326                                    class_type,
15327                                    /*check_dependency_p=*/false,
15328                                    /*class_head_p=*/true,
15329                                    /*is_declaration=*/false);
15330       /* If that didn't work, ignore the nested-name-specifier.  */
15331       if (!cp_parser_parse_definitely (parser))
15332         {
15333           invalid_nested_name_p = true;
15334           type_start_token = cp_lexer_peek_token (parser->lexer);
15335           id = cp_parser_identifier (parser);
15336           if (id == error_mark_node)
15337             id = NULL_TREE;
15338         }
15339       /* If we could not find a corresponding TYPE, treat this
15340          declaration like an unqualified declaration.  */
15341       if (type == error_mark_node)
15342         nested_name_specifier = NULL_TREE;
15343       /* Otherwise, count the number of templates used in TYPE and its
15344          containing scopes.  */
15345       else
15346         {
15347           tree scope;
15348
15349           for (scope = TREE_TYPE (type);
15350                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15351                scope = (TYPE_P (scope)
15352                         ? TYPE_CONTEXT (scope)
15353                         : DECL_CONTEXT (scope)))
15354             if (TYPE_P (scope)
15355                 && CLASS_TYPE_P (scope)
15356                 && CLASSTYPE_TEMPLATE_INFO (scope)
15357                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15358                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15359               ++num_templates;
15360         }
15361     }
15362   /* Otherwise, the identifier is optional.  */
15363   else
15364     {
15365       /* We don't know whether what comes next is a template-id,
15366          an identifier, or nothing at all.  */
15367       cp_parser_parse_tentatively (parser);
15368       /* Check for a template-id.  */
15369       type_start_token = cp_lexer_peek_token (parser->lexer);
15370       id = cp_parser_template_id (parser,
15371                                   /*template_keyword_p=*/false,
15372                                   /*check_dependency_p=*/true,
15373                                   /*is_declaration=*/true);
15374       /* If that didn't work, it could still be an identifier.  */
15375       if (!cp_parser_parse_definitely (parser))
15376         {
15377           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15378             {
15379               type_start_token = cp_lexer_peek_token (parser->lexer);
15380               id = cp_parser_identifier (parser);
15381             }
15382           else
15383             id = NULL_TREE;
15384         }
15385       else
15386         {
15387           template_id_p = true;
15388           ++num_templates;
15389         }
15390     }
15391
15392   pop_deferring_access_checks ();
15393
15394   if (id)
15395     cp_parser_check_for_invalid_template_id (parser, id,
15396                                              type_start_token->location);
15397
15398   /* If it's not a `:' or a `{' then we can't really be looking at a
15399      class-head, since a class-head only appears as part of a
15400      class-specifier.  We have to detect this situation before calling
15401      xref_tag, since that has irreversible side-effects.  */
15402   if (!cp_parser_next_token_starts_class_definition_p (parser))
15403     {
15404       cp_parser_error (parser, "expected %<{%> or %<:%>");
15405       return error_mark_node;
15406     }
15407
15408   /* At this point, we're going ahead with the class-specifier, even
15409      if some other problem occurs.  */
15410   cp_parser_commit_to_tentative_parse (parser);
15411   /* Issue the error about the overly-qualified name now.  */
15412   if (qualified_p)
15413     {
15414       cp_parser_error (parser,
15415                        "global qualification of class name is invalid");
15416       return error_mark_node;
15417     }
15418   else if (invalid_nested_name_p)
15419     {
15420       cp_parser_error (parser,
15421                        "qualified name does not name a class");
15422       return error_mark_node;
15423     }
15424   else if (nested_name_specifier)
15425     {
15426       tree scope;
15427
15428       /* Reject typedef-names in class heads.  */
15429       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15430         {
15431           error ("%Hinvalid class name in declaration of %qD",
15432                  &type_start_token->location, type);
15433           type = NULL_TREE;
15434           goto done;
15435         }
15436
15437       /* Figure out in what scope the declaration is being placed.  */
15438       scope = current_scope ();
15439       /* If that scope does not contain the scope in which the
15440          class was originally declared, the program is invalid.  */
15441       if (scope && !is_ancestor (scope, nested_name_specifier))
15442         {
15443           if (at_namespace_scope_p ())
15444             error ("%Hdeclaration of %qD in namespace %qD which does not "
15445                    "enclose %qD",
15446                    &type_start_token->location,
15447                    type, scope, nested_name_specifier);
15448           else
15449             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15450                    &type_start_token->location,
15451                    type, scope, nested_name_specifier);
15452           type = NULL_TREE;
15453           goto done;
15454         }
15455       /* [dcl.meaning]
15456
15457          A declarator-id shall not be qualified except for the
15458          definition of a ... nested class outside of its class
15459          ... [or] the definition or explicit instantiation of a
15460          class member of a namespace outside of its namespace.  */
15461       if (scope == nested_name_specifier)
15462         {
15463           permerror (input_location, "%Hextra qualification not allowed",
15464                      &nested_name_specifier_token_start->location);
15465           nested_name_specifier = NULL_TREE;
15466           num_templates = 0;
15467         }
15468     }
15469   /* An explicit-specialization must be preceded by "template <>".  If
15470      it is not, try to recover gracefully.  */
15471   if (at_namespace_scope_p ()
15472       && parser->num_template_parameter_lists == 0
15473       && template_id_p)
15474     {
15475       error ("%Han explicit specialization must be preceded by %<template <>%>",
15476              &type_start_token->location);
15477       invalid_explicit_specialization_p = true;
15478       /* Take the same action that would have been taken by
15479          cp_parser_explicit_specialization.  */
15480       ++parser->num_template_parameter_lists;
15481       begin_specialization ();
15482     }
15483   /* There must be no "return" statements between this point and the
15484      end of this function; set "type "to the correct return value and
15485      use "goto done;" to return.  */
15486   /* Make sure that the right number of template parameters were
15487      present.  */
15488   if (!cp_parser_check_template_parameters (parser, num_templates,
15489                                             type_start_token->location))
15490     {
15491       /* If something went wrong, there is no point in even trying to
15492          process the class-definition.  */
15493       type = NULL_TREE;
15494       goto done;
15495     }
15496
15497   /* Look up the type.  */
15498   if (template_id_p)
15499     {
15500       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15501           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15502               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15503         {
15504           error ("%Hfunction template %qD redeclared as a class template",
15505                  &type_start_token->location, id);
15506           type = error_mark_node;
15507         }
15508       else
15509         {
15510           type = TREE_TYPE (id);
15511           type = maybe_process_partial_specialization (type);
15512         }
15513       if (nested_name_specifier)
15514         pushed_scope = push_scope (nested_name_specifier);
15515     }
15516   else if (nested_name_specifier)
15517     {
15518       tree class_type;
15519
15520       /* Given:
15521
15522             template <typename T> struct S { struct T };
15523             template <typename T> struct S<T>::T { };
15524
15525          we will get a TYPENAME_TYPE when processing the definition of
15526          `S::T'.  We need to resolve it to the actual type before we
15527          try to define it.  */
15528       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15529         {
15530           class_type = resolve_typename_type (TREE_TYPE (type),
15531                                               /*only_current_p=*/false);
15532           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15533             type = TYPE_NAME (class_type);
15534           else
15535             {
15536               cp_parser_error (parser, "could not resolve typename type");
15537               type = error_mark_node;
15538             }
15539         }
15540
15541       if (maybe_process_partial_specialization (TREE_TYPE (type))
15542           == error_mark_node)
15543         {
15544           type = NULL_TREE;
15545           goto done;
15546         }
15547
15548       class_type = current_class_type;
15549       /* Enter the scope indicated by the nested-name-specifier.  */
15550       pushed_scope = push_scope (nested_name_specifier);
15551       /* Get the canonical version of this type.  */
15552       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15553       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15554           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15555         {
15556           type = push_template_decl (type);
15557           if (type == error_mark_node)
15558             {
15559               type = NULL_TREE;
15560               goto done;
15561             }
15562         }
15563
15564       type = TREE_TYPE (type);
15565       *nested_name_specifier_p = true;
15566     }
15567   else      /* The name is not a nested name.  */
15568     {
15569       /* If the class was unnamed, create a dummy name.  */
15570       if (!id)
15571         id = make_anon_name ();
15572       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15573                        parser->num_template_parameter_lists);
15574     }
15575
15576   /* Indicate whether this class was declared as a `class' or as a
15577      `struct'.  */
15578   if (TREE_CODE (type) == RECORD_TYPE)
15579     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15580   cp_parser_check_class_key (class_key, type);
15581
15582   /* If this type was already complete, and we see another definition,
15583      that's an error.  */
15584   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15585     {
15586       error ("%Hredefinition of %q#T",
15587              &type_start_token->location, type);
15588       error ("%Hprevious definition of %q+#T",
15589              &type_start_token->location, type);
15590       type = NULL_TREE;
15591       goto done;
15592     }
15593   else if (type == error_mark_node)
15594     type = NULL_TREE;
15595
15596   /* We will have entered the scope containing the class; the names of
15597      base classes should be looked up in that context.  For example:
15598
15599        struct A { struct B {}; struct C; };
15600        struct A::C : B {};
15601
15602      is valid.  */
15603
15604   /* Get the list of base-classes, if there is one.  */
15605   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15606     *bases = cp_parser_base_clause (parser);
15607
15608  done:
15609   /* Leave the scope given by the nested-name-specifier.  We will
15610      enter the class scope itself while processing the members.  */
15611   if (pushed_scope)
15612     pop_scope (pushed_scope);
15613
15614   if (invalid_explicit_specialization_p)
15615     {
15616       end_specialization ();
15617       --parser->num_template_parameter_lists;
15618     }
15619   *attributes_p = attributes;
15620   return type;
15621 }
15622
15623 /* Parse a class-key.
15624
15625    class-key:
15626      class
15627      struct
15628      union
15629
15630    Returns the kind of class-key specified, or none_type to indicate
15631    error.  */
15632
15633 static enum tag_types
15634 cp_parser_class_key (cp_parser* parser)
15635 {
15636   cp_token *token;
15637   enum tag_types tag_type;
15638
15639   /* Look for the class-key.  */
15640   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15641   if (!token)
15642     return none_type;
15643
15644   /* Check to see if the TOKEN is a class-key.  */
15645   tag_type = cp_parser_token_is_class_key (token);
15646   if (!tag_type)
15647     cp_parser_error (parser, "expected class-key");
15648   return tag_type;
15649 }
15650
15651 /* Parse an (optional) member-specification.
15652
15653    member-specification:
15654      member-declaration member-specification [opt]
15655      access-specifier : member-specification [opt]  */
15656
15657 static void
15658 cp_parser_member_specification_opt (cp_parser* parser)
15659 {
15660   while (true)
15661     {
15662       cp_token *token;
15663       enum rid keyword;
15664
15665       /* Peek at the next token.  */
15666       token = cp_lexer_peek_token (parser->lexer);
15667       /* If it's a `}', or EOF then we've seen all the members.  */
15668       if (token->type == CPP_CLOSE_BRACE
15669           || token->type == CPP_EOF
15670           || token->type == CPP_PRAGMA_EOL)
15671         break;
15672
15673       /* See if this token is a keyword.  */
15674       keyword = token->keyword;
15675       switch (keyword)
15676         {
15677         case RID_PUBLIC:
15678         case RID_PROTECTED:
15679         case RID_PRIVATE:
15680           /* Consume the access-specifier.  */
15681           cp_lexer_consume_token (parser->lexer);
15682           /* Remember which access-specifier is active.  */
15683           current_access_specifier = token->u.value;
15684           /* Look for the `:'.  */
15685           cp_parser_require (parser, CPP_COLON, "%<:%>");
15686           break;
15687
15688         default:
15689           /* Accept #pragmas at class scope.  */
15690           if (token->type == CPP_PRAGMA)
15691             {
15692               cp_parser_pragma (parser, pragma_external);
15693               break;
15694             }
15695
15696           /* Otherwise, the next construction must be a
15697              member-declaration.  */
15698           cp_parser_member_declaration (parser);
15699         }
15700     }
15701 }
15702
15703 /* Parse a member-declaration.
15704
15705    member-declaration:
15706      decl-specifier-seq [opt] member-declarator-list [opt] ;
15707      function-definition ; [opt]
15708      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15709      using-declaration
15710      template-declaration
15711
15712    member-declarator-list:
15713      member-declarator
15714      member-declarator-list , member-declarator
15715
15716    member-declarator:
15717      declarator pure-specifier [opt]
15718      declarator constant-initializer [opt]
15719      identifier [opt] : constant-expression
15720
15721    GNU Extensions:
15722
15723    member-declaration:
15724      __extension__ member-declaration
15725
15726    member-declarator:
15727      declarator attributes [opt] pure-specifier [opt]
15728      declarator attributes [opt] constant-initializer [opt]
15729      identifier [opt] attributes [opt] : constant-expression  
15730
15731    C++0x Extensions:
15732
15733    member-declaration:
15734      static_assert-declaration  */
15735
15736 static void
15737 cp_parser_member_declaration (cp_parser* parser)
15738 {
15739   cp_decl_specifier_seq decl_specifiers;
15740   tree prefix_attributes;
15741   tree decl;
15742   int declares_class_or_enum;
15743   bool friend_p;
15744   cp_token *token = NULL;
15745   cp_token *decl_spec_token_start = NULL;
15746   cp_token *initializer_token_start = NULL;
15747   int saved_pedantic;
15748
15749   /* Check for the `__extension__' keyword.  */
15750   if (cp_parser_extension_opt (parser, &saved_pedantic))
15751     {
15752       /* Recurse.  */
15753       cp_parser_member_declaration (parser);
15754       /* Restore the old value of the PEDANTIC flag.  */
15755       pedantic = saved_pedantic;
15756
15757       return;
15758     }
15759
15760   /* Check for a template-declaration.  */
15761   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15762     {
15763       /* An explicit specialization here is an error condition, and we
15764          expect the specialization handler to detect and report this.  */
15765       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15766           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15767         cp_parser_explicit_specialization (parser);
15768       else
15769         cp_parser_template_declaration (parser, /*member_p=*/true);
15770
15771       return;
15772     }
15773
15774   /* Check for a using-declaration.  */
15775   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15776     {
15777       /* Parse the using-declaration.  */
15778       cp_parser_using_declaration (parser,
15779                                    /*access_declaration_p=*/false);
15780       return;
15781     }
15782
15783   /* Check for @defs.  */
15784   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15785     {
15786       tree ivar, member;
15787       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15788       ivar = ivar_chains;
15789       while (ivar)
15790         {
15791           member = ivar;
15792           ivar = TREE_CHAIN (member);
15793           TREE_CHAIN (member) = NULL_TREE;
15794           finish_member_declaration (member);
15795         }
15796       return;
15797     }
15798
15799   /* If the next token is `static_assert' we have a static assertion.  */
15800   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15801     {
15802       cp_parser_static_assert (parser, /*member_p=*/true);
15803       return;
15804     }
15805
15806   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15807     return;
15808
15809   /* Parse the decl-specifier-seq.  */
15810   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15811   cp_parser_decl_specifier_seq (parser,
15812                                 CP_PARSER_FLAGS_OPTIONAL,
15813                                 &decl_specifiers,
15814                                 &declares_class_or_enum);
15815   prefix_attributes = decl_specifiers.attributes;
15816   decl_specifiers.attributes = NULL_TREE;
15817   /* Check for an invalid type-name.  */
15818   if (!decl_specifiers.type
15819       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15820     return;
15821   /* If there is no declarator, then the decl-specifier-seq should
15822      specify a type.  */
15823   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15824     {
15825       /* If there was no decl-specifier-seq, and the next token is a
15826          `;', then we have something like:
15827
15828            struct S { ; };
15829
15830          [class.mem]
15831
15832          Each member-declaration shall declare at least one member
15833          name of the class.  */
15834       if (!decl_specifiers.any_specifiers_p)
15835         {
15836           cp_token *token = cp_lexer_peek_token (parser->lexer);
15837           if (!in_system_header_at (token->location))
15838             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15839         }
15840       else
15841         {
15842           tree type;
15843
15844           /* See if this declaration is a friend.  */
15845           friend_p = cp_parser_friend_p (&decl_specifiers);
15846           /* If there were decl-specifiers, check to see if there was
15847              a class-declaration.  */
15848           type = check_tag_decl (&decl_specifiers);
15849           /* Nested classes have already been added to the class, but
15850              a `friend' needs to be explicitly registered.  */
15851           if (friend_p)
15852             {
15853               /* If the `friend' keyword was present, the friend must
15854                  be introduced with a class-key.  */
15855                if (!declares_class_or_enum)
15856                  error ("%Ha class-key must be used when declaring a friend",
15857                         &decl_spec_token_start->location);
15858                /* In this case:
15859
15860                     template <typename T> struct A {
15861                       friend struct A<T>::B;
15862                     };
15863
15864                   A<T>::B will be represented by a TYPENAME_TYPE, and
15865                   therefore not recognized by check_tag_decl.  */
15866                if (!type
15867                    && decl_specifiers.type
15868                    && TYPE_P (decl_specifiers.type))
15869                  type = decl_specifiers.type;
15870                if (!type || !TYPE_P (type))
15871                  error ("%Hfriend declaration does not name a class or "
15872                         "function", &decl_spec_token_start->location);
15873                else
15874                  make_friend_class (current_class_type, type,
15875                                     /*complain=*/true);
15876             }
15877           /* If there is no TYPE, an error message will already have
15878              been issued.  */
15879           else if (!type || type == error_mark_node)
15880             ;
15881           /* An anonymous aggregate has to be handled specially; such
15882              a declaration really declares a data member (with a
15883              particular type), as opposed to a nested class.  */
15884           else if (ANON_AGGR_TYPE_P (type))
15885             {
15886               /* Remove constructors and such from TYPE, now that we
15887                  know it is an anonymous aggregate.  */
15888               fixup_anonymous_aggr (type);
15889               /* And make the corresponding data member.  */
15890               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15891               /* Add it to the class.  */
15892               finish_member_declaration (decl);
15893             }
15894           else
15895             cp_parser_check_access_in_redeclaration
15896                                               (TYPE_NAME (type),
15897                                                decl_spec_token_start->location);
15898         }
15899     }
15900   else
15901     {
15902       /* See if these declarations will be friends.  */
15903       friend_p = cp_parser_friend_p (&decl_specifiers);
15904
15905       /* Keep going until we hit the `;' at the end of the
15906          declaration.  */
15907       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15908         {
15909           tree attributes = NULL_TREE;
15910           tree first_attribute;
15911
15912           /* Peek at the next token.  */
15913           token = cp_lexer_peek_token (parser->lexer);
15914
15915           /* Check for a bitfield declaration.  */
15916           if (token->type == CPP_COLON
15917               || (token->type == CPP_NAME
15918                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15919                   == CPP_COLON))
15920             {
15921               tree identifier;
15922               tree width;
15923
15924               /* Get the name of the bitfield.  Note that we cannot just
15925                  check TOKEN here because it may have been invalidated by
15926                  the call to cp_lexer_peek_nth_token above.  */
15927               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15928                 identifier = cp_parser_identifier (parser);
15929               else
15930                 identifier = NULL_TREE;
15931
15932               /* Consume the `:' token.  */
15933               cp_lexer_consume_token (parser->lexer);
15934               /* Get the width of the bitfield.  */
15935               width
15936                 = cp_parser_constant_expression (parser,
15937                                                  /*allow_non_constant=*/false,
15938                                                  NULL);
15939
15940               /* Look for attributes that apply to the bitfield.  */
15941               attributes = cp_parser_attributes_opt (parser);
15942               /* Remember which attributes are prefix attributes and
15943                  which are not.  */
15944               first_attribute = attributes;
15945               /* Combine the attributes.  */
15946               attributes = chainon (prefix_attributes, attributes);
15947
15948               /* Create the bitfield declaration.  */
15949               decl = grokbitfield (identifier
15950                                    ? make_id_declarator (NULL_TREE,
15951                                                          identifier,
15952                                                          sfk_none)
15953                                    : NULL,
15954                                    &decl_specifiers,
15955                                    width,
15956                                    attributes);
15957             }
15958           else
15959             {
15960               cp_declarator *declarator;
15961               tree initializer;
15962               tree asm_specification;
15963               int ctor_dtor_or_conv_p;
15964
15965               /* Parse the declarator.  */
15966               declarator
15967                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15968                                         &ctor_dtor_or_conv_p,
15969                                         /*parenthesized_p=*/NULL,
15970                                         /*member_p=*/true);
15971
15972               /* If something went wrong parsing the declarator, make sure
15973                  that we at least consume some tokens.  */
15974               if (declarator == cp_error_declarator)
15975                 {
15976                   /* Skip to the end of the statement.  */
15977                   cp_parser_skip_to_end_of_statement (parser);
15978                   /* If the next token is not a semicolon, that is
15979                      probably because we just skipped over the body of
15980                      a function.  So, we consume a semicolon if
15981                      present, but do not issue an error message if it
15982                      is not present.  */
15983                   if (cp_lexer_next_token_is (parser->lexer,
15984                                               CPP_SEMICOLON))
15985                     cp_lexer_consume_token (parser->lexer);
15986                   return;
15987                 }
15988
15989               if (declares_class_or_enum & 2)
15990                 cp_parser_check_for_definition_in_return_type
15991                                             (declarator, decl_specifiers.type,
15992                                              decl_specifiers.type_location);
15993
15994               /* Look for an asm-specification.  */
15995               asm_specification = cp_parser_asm_specification_opt (parser);
15996               /* Look for attributes that apply to the declaration.  */
15997               attributes = cp_parser_attributes_opt (parser);
15998               /* Remember which attributes are prefix attributes and
15999                  which are not.  */
16000               first_attribute = attributes;
16001               /* Combine the attributes.  */
16002               attributes = chainon (prefix_attributes, attributes);
16003
16004               /* If it's an `=', then we have a constant-initializer or a
16005                  pure-specifier.  It is not correct to parse the
16006                  initializer before registering the member declaration
16007                  since the member declaration should be in scope while
16008                  its initializer is processed.  However, the rest of the
16009                  front end does not yet provide an interface that allows
16010                  us to handle this correctly.  */
16011               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16012                 {
16013                   /* In [class.mem]:
16014
16015                      A pure-specifier shall be used only in the declaration of
16016                      a virtual function.
16017
16018                      A member-declarator can contain a constant-initializer
16019                      only if it declares a static member of integral or
16020                      enumeration type.
16021
16022                      Therefore, if the DECLARATOR is for a function, we look
16023                      for a pure-specifier; otherwise, we look for a
16024                      constant-initializer.  When we call `grokfield', it will
16025                      perform more stringent semantics checks.  */
16026                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16027                   if (function_declarator_p (declarator))
16028                     initializer = cp_parser_pure_specifier (parser);
16029                   else
16030                     /* Parse the initializer.  */
16031                     initializer = cp_parser_constant_initializer (parser);
16032                 }
16033               /* Otherwise, there is no initializer.  */
16034               else
16035                 initializer = NULL_TREE;
16036
16037               /* See if we are probably looking at a function
16038                  definition.  We are certainly not looking at a
16039                  member-declarator.  Calling `grokfield' has
16040                  side-effects, so we must not do it unless we are sure
16041                  that we are looking at a member-declarator.  */
16042               if (cp_parser_token_starts_function_definition_p
16043                   (cp_lexer_peek_token (parser->lexer)))
16044                 {
16045                   /* The grammar does not allow a pure-specifier to be
16046                      used when a member function is defined.  (It is
16047                      possible that this fact is an oversight in the
16048                      standard, since a pure function may be defined
16049                      outside of the class-specifier.  */
16050                   if (initializer)
16051                     error ("%Hpure-specifier on function-definition",
16052                            &initializer_token_start->location);
16053                   decl = cp_parser_save_member_function_body (parser,
16054                                                               &decl_specifiers,
16055                                                               declarator,
16056                                                               attributes);
16057                   /* If the member was not a friend, declare it here.  */
16058                   if (!friend_p)
16059                     finish_member_declaration (decl);
16060                   /* Peek at the next token.  */
16061                   token = cp_lexer_peek_token (parser->lexer);
16062                   /* If the next token is a semicolon, consume it.  */
16063                   if (token->type == CPP_SEMICOLON)
16064                     cp_lexer_consume_token (parser->lexer);
16065                   return;
16066                 }
16067               else
16068                 if (declarator->kind == cdk_function)
16069                   declarator->id_loc = token->location;
16070                 /* Create the declaration.  */
16071                 decl = grokfield (declarator, &decl_specifiers,
16072                                   initializer, /*init_const_expr_p=*/true,
16073                                   asm_specification,
16074                                   attributes);
16075             }
16076
16077           /* Reset PREFIX_ATTRIBUTES.  */
16078           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16079             attributes = TREE_CHAIN (attributes);
16080           if (attributes)
16081             TREE_CHAIN (attributes) = NULL_TREE;
16082
16083           /* If there is any qualification still in effect, clear it
16084              now; we will be starting fresh with the next declarator.  */
16085           parser->scope = NULL_TREE;
16086           parser->qualifying_scope = NULL_TREE;
16087           parser->object_scope = NULL_TREE;
16088           /* If it's a `,', then there are more declarators.  */
16089           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16090             cp_lexer_consume_token (parser->lexer);
16091           /* If the next token isn't a `;', then we have a parse error.  */
16092           else if (cp_lexer_next_token_is_not (parser->lexer,
16093                                                CPP_SEMICOLON))
16094             {
16095               cp_parser_error (parser, "expected %<;%>");
16096               /* Skip tokens until we find a `;'.  */
16097               cp_parser_skip_to_end_of_statement (parser);
16098
16099               break;
16100             }
16101
16102           if (decl)
16103             {
16104               /* Add DECL to the list of members.  */
16105               if (!friend_p)
16106                 finish_member_declaration (decl);
16107
16108               if (TREE_CODE (decl) == FUNCTION_DECL)
16109                 cp_parser_save_default_args (parser, decl);
16110             }
16111         }
16112     }
16113
16114   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16115 }
16116
16117 /* Parse a pure-specifier.
16118
16119    pure-specifier:
16120      = 0
16121
16122    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16123    Otherwise, ERROR_MARK_NODE is returned.  */
16124
16125 static tree
16126 cp_parser_pure_specifier (cp_parser* parser)
16127 {
16128   cp_token *token;
16129
16130   /* Look for the `=' token.  */
16131   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16132     return error_mark_node;
16133   /* Look for the `0' token.  */
16134   token = cp_lexer_peek_token (parser->lexer);
16135
16136   if (token->type == CPP_EOF
16137       || token->type == CPP_PRAGMA_EOL)
16138     return error_mark_node;
16139
16140   cp_lexer_consume_token (parser->lexer);
16141
16142   /* Accept = default or = delete in c++0x mode.  */
16143   if (token->keyword == RID_DEFAULT
16144       || token->keyword == RID_DELETE)
16145     {
16146       maybe_warn_cpp0x ("defaulted and deleted functions");
16147       return token->u.value;
16148     }
16149
16150   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16151   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16152     {
16153       cp_parser_error (parser,
16154                        "invalid pure specifier (only %<= 0%> is allowed)");
16155       cp_parser_skip_to_end_of_statement (parser);
16156       return error_mark_node;
16157     }
16158   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16159     {
16160       error ("%Htemplates may not be %<virtual%>", &token->location);
16161       return error_mark_node;
16162     }
16163
16164   return integer_zero_node;
16165 }
16166
16167 /* Parse a constant-initializer.
16168
16169    constant-initializer:
16170      = constant-expression
16171
16172    Returns a representation of the constant-expression.  */
16173
16174 static tree
16175 cp_parser_constant_initializer (cp_parser* parser)
16176 {
16177   /* Look for the `=' token.  */
16178   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16179     return error_mark_node;
16180
16181   /* It is invalid to write:
16182
16183        struct S { static const int i = { 7 }; };
16184
16185      */
16186   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16187     {
16188       cp_parser_error (parser,
16189                        "a brace-enclosed initializer is not allowed here");
16190       /* Consume the opening brace.  */
16191       cp_lexer_consume_token (parser->lexer);
16192       /* Skip the initializer.  */
16193       cp_parser_skip_to_closing_brace (parser);
16194       /* Look for the trailing `}'.  */
16195       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16196
16197       return error_mark_node;
16198     }
16199
16200   return cp_parser_constant_expression (parser,
16201                                         /*allow_non_constant=*/false,
16202                                         NULL);
16203 }
16204
16205 /* Derived classes [gram.class.derived] */
16206
16207 /* Parse a base-clause.
16208
16209    base-clause:
16210      : base-specifier-list
16211
16212    base-specifier-list:
16213      base-specifier ... [opt]
16214      base-specifier-list , base-specifier ... [opt]
16215
16216    Returns a TREE_LIST representing the base-classes, in the order in
16217    which they were declared.  The representation of each node is as
16218    described by cp_parser_base_specifier.
16219
16220    In the case that no bases are specified, this function will return
16221    NULL_TREE, not ERROR_MARK_NODE.  */
16222
16223 static tree
16224 cp_parser_base_clause (cp_parser* parser)
16225 {
16226   tree bases = NULL_TREE;
16227
16228   /* Look for the `:' that begins the list.  */
16229   cp_parser_require (parser, CPP_COLON, "%<:%>");
16230
16231   /* Scan the base-specifier-list.  */
16232   while (true)
16233     {
16234       cp_token *token;
16235       tree base;
16236       bool pack_expansion_p = false;
16237
16238       /* Look for the base-specifier.  */
16239       base = cp_parser_base_specifier (parser);
16240       /* Look for the (optional) ellipsis. */
16241       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16242         {
16243           /* Consume the `...'. */
16244           cp_lexer_consume_token (parser->lexer);
16245
16246           pack_expansion_p = true;
16247         }
16248
16249       /* Add BASE to the front of the list.  */
16250       if (base != error_mark_node)
16251         {
16252           if (pack_expansion_p)
16253             /* Make this a pack expansion type. */
16254             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16255           
16256
16257           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16258             {
16259               TREE_CHAIN (base) = bases;
16260               bases = base;
16261             }
16262         }
16263       /* Peek at the next token.  */
16264       token = cp_lexer_peek_token (parser->lexer);
16265       /* If it's not a comma, then the list is complete.  */
16266       if (token->type != CPP_COMMA)
16267         break;
16268       /* Consume the `,'.  */
16269       cp_lexer_consume_token (parser->lexer);
16270     }
16271
16272   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16273      base class had a qualified name.  However, the next name that
16274      appears is certainly not qualified.  */
16275   parser->scope = NULL_TREE;
16276   parser->qualifying_scope = NULL_TREE;
16277   parser->object_scope = NULL_TREE;
16278
16279   return nreverse (bases);
16280 }
16281
16282 /* Parse a base-specifier.
16283
16284    base-specifier:
16285      :: [opt] nested-name-specifier [opt] class-name
16286      virtual access-specifier [opt] :: [opt] nested-name-specifier
16287        [opt] class-name
16288      access-specifier virtual [opt] :: [opt] nested-name-specifier
16289        [opt] class-name
16290
16291    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16292    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16293    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16294    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16295
16296 static tree
16297 cp_parser_base_specifier (cp_parser* parser)
16298 {
16299   cp_token *token;
16300   bool done = false;
16301   bool virtual_p = false;
16302   bool duplicate_virtual_error_issued_p = false;
16303   bool duplicate_access_error_issued_p = false;
16304   bool class_scope_p, template_p;
16305   tree access = access_default_node;
16306   tree type;
16307
16308   /* Process the optional `virtual' and `access-specifier'.  */
16309   while (!done)
16310     {
16311       /* Peek at the next token.  */
16312       token = cp_lexer_peek_token (parser->lexer);
16313       /* Process `virtual'.  */
16314       switch (token->keyword)
16315         {
16316         case RID_VIRTUAL:
16317           /* If `virtual' appears more than once, issue an error.  */
16318           if (virtual_p && !duplicate_virtual_error_issued_p)
16319             {
16320               cp_parser_error (parser,
16321                                "%<virtual%> specified more than once in base-specified");
16322               duplicate_virtual_error_issued_p = true;
16323             }
16324
16325           virtual_p = true;
16326
16327           /* Consume the `virtual' token.  */
16328           cp_lexer_consume_token (parser->lexer);
16329
16330           break;
16331
16332         case RID_PUBLIC:
16333         case RID_PROTECTED:
16334         case RID_PRIVATE:
16335           /* If more than one access specifier appears, issue an
16336              error.  */
16337           if (access != access_default_node
16338               && !duplicate_access_error_issued_p)
16339             {
16340               cp_parser_error (parser,
16341                                "more than one access specifier in base-specified");
16342               duplicate_access_error_issued_p = true;
16343             }
16344
16345           access = ridpointers[(int) token->keyword];
16346
16347           /* Consume the access-specifier.  */
16348           cp_lexer_consume_token (parser->lexer);
16349
16350           break;
16351
16352         default:
16353           done = true;
16354           break;
16355         }
16356     }
16357   /* It is not uncommon to see programs mechanically, erroneously, use
16358      the 'typename' keyword to denote (dependent) qualified types
16359      as base classes.  */
16360   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16361     {
16362       token = cp_lexer_peek_token (parser->lexer);
16363       if (!processing_template_decl)
16364         error ("%Hkeyword %<typename%> not allowed outside of templates",
16365                &token->location);
16366       else
16367         error ("%Hkeyword %<typename%> not allowed in this context "
16368                "(the base class is implicitly a type)",
16369                &token->location);
16370       cp_lexer_consume_token (parser->lexer);
16371     }
16372
16373   /* Look for the optional `::' operator.  */
16374   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16375   /* Look for the nested-name-specifier.  The simplest way to
16376      implement:
16377
16378        [temp.res]
16379
16380        The keyword `typename' is not permitted in a base-specifier or
16381        mem-initializer; in these contexts a qualified name that
16382        depends on a template-parameter is implicitly assumed to be a
16383        type name.
16384
16385      is to pretend that we have seen the `typename' keyword at this
16386      point.  */
16387   cp_parser_nested_name_specifier_opt (parser,
16388                                        /*typename_keyword_p=*/true,
16389                                        /*check_dependency_p=*/true,
16390                                        typename_type,
16391                                        /*is_declaration=*/true);
16392   /* If the base class is given by a qualified name, assume that names
16393      we see are type names or templates, as appropriate.  */
16394   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16395   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16396
16397   /* Finally, look for the class-name.  */
16398   type = cp_parser_class_name (parser,
16399                                class_scope_p,
16400                                template_p,
16401                                typename_type,
16402                                /*check_dependency_p=*/true,
16403                                /*class_head_p=*/false,
16404                                /*is_declaration=*/true);
16405
16406   if (type == error_mark_node)
16407     return error_mark_node;
16408
16409   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16410 }
16411
16412 /* Exception handling [gram.exception] */
16413
16414 /* Parse an (optional) exception-specification.
16415
16416    exception-specification:
16417      throw ( type-id-list [opt] )
16418
16419    Returns a TREE_LIST representing the exception-specification.  The
16420    TREE_VALUE of each node is a type.  */
16421
16422 static tree
16423 cp_parser_exception_specification_opt (cp_parser* parser)
16424 {
16425   cp_token *token;
16426   tree type_id_list;
16427
16428   /* Peek at the next token.  */
16429   token = cp_lexer_peek_token (parser->lexer);
16430   /* If it's not `throw', then there's no exception-specification.  */
16431   if (!cp_parser_is_keyword (token, RID_THROW))
16432     return NULL_TREE;
16433
16434   /* Consume the `throw'.  */
16435   cp_lexer_consume_token (parser->lexer);
16436
16437   /* Look for the `('.  */
16438   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16439
16440   /* Peek at the next token.  */
16441   token = cp_lexer_peek_token (parser->lexer);
16442   /* If it's not a `)', then there is a type-id-list.  */
16443   if (token->type != CPP_CLOSE_PAREN)
16444     {
16445       const char *saved_message;
16446
16447       /* Types may not be defined in an exception-specification.  */
16448       saved_message = parser->type_definition_forbidden_message;
16449       parser->type_definition_forbidden_message
16450         = "types may not be defined in an exception-specification";
16451       /* Parse the type-id-list.  */
16452       type_id_list = cp_parser_type_id_list (parser);
16453       /* Restore the saved message.  */
16454       parser->type_definition_forbidden_message = saved_message;
16455     }
16456   else
16457     type_id_list = empty_except_spec;
16458
16459   /* Look for the `)'.  */
16460   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16461
16462   return type_id_list;
16463 }
16464
16465 /* Parse an (optional) type-id-list.
16466
16467    type-id-list:
16468      type-id ... [opt]
16469      type-id-list , type-id ... [opt]
16470
16471    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16472    in the order that the types were presented.  */
16473
16474 static tree
16475 cp_parser_type_id_list (cp_parser* parser)
16476 {
16477   tree types = NULL_TREE;
16478
16479   while (true)
16480     {
16481       cp_token *token;
16482       tree type;
16483
16484       /* Get the next type-id.  */
16485       type = cp_parser_type_id (parser);
16486       /* Parse the optional ellipsis. */
16487       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16488         {
16489           /* Consume the `...'. */
16490           cp_lexer_consume_token (parser->lexer);
16491
16492           /* Turn the type into a pack expansion expression. */
16493           type = make_pack_expansion (type);
16494         }
16495       /* Add it to the list.  */
16496       types = add_exception_specifier (types, type, /*complain=*/1);
16497       /* Peek at the next token.  */
16498       token = cp_lexer_peek_token (parser->lexer);
16499       /* If it is not a `,', we are done.  */
16500       if (token->type != CPP_COMMA)
16501         break;
16502       /* Consume the `,'.  */
16503       cp_lexer_consume_token (parser->lexer);
16504     }
16505
16506   return nreverse (types);
16507 }
16508
16509 /* Parse a try-block.
16510
16511    try-block:
16512      try compound-statement handler-seq  */
16513
16514 static tree
16515 cp_parser_try_block (cp_parser* parser)
16516 {
16517   tree try_block;
16518
16519   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16520   try_block = begin_try_block ();
16521   cp_parser_compound_statement (parser, NULL, true);
16522   finish_try_block (try_block);
16523   cp_parser_handler_seq (parser);
16524   finish_handler_sequence (try_block);
16525
16526   return try_block;
16527 }
16528
16529 /* Parse a function-try-block.
16530
16531    function-try-block:
16532      try ctor-initializer [opt] function-body handler-seq  */
16533
16534 static bool
16535 cp_parser_function_try_block (cp_parser* parser)
16536 {
16537   tree compound_stmt;
16538   tree try_block;
16539   bool ctor_initializer_p;
16540
16541   /* Look for the `try' keyword.  */
16542   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16543     return false;
16544   /* Let the rest of the front end know where we are.  */
16545   try_block = begin_function_try_block (&compound_stmt);
16546   /* Parse the function-body.  */
16547   ctor_initializer_p
16548     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16549   /* We're done with the `try' part.  */
16550   finish_function_try_block (try_block);
16551   /* Parse the handlers.  */
16552   cp_parser_handler_seq (parser);
16553   /* We're done with the handlers.  */
16554   finish_function_handler_sequence (try_block, compound_stmt);
16555
16556   return ctor_initializer_p;
16557 }
16558
16559 /* Parse a handler-seq.
16560
16561    handler-seq:
16562      handler handler-seq [opt]  */
16563
16564 static void
16565 cp_parser_handler_seq (cp_parser* parser)
16566 {
16567   while (true)
16568     {
16569       cp_token *token;
16570
16571       /* Parse the handler.  */
16572       cp_parser_handler (parser);
16573       /* Peek at the next token.  */
16574       token = cp_lexer_peek_token (parser->lexer);
16575       /* If it's not `catch' then there are no more handlers.  */
16576       if (!cp_parser_is_keyword (token, RID_CATCH))
16577         break;
16578     }
16579 }
16580
16581 /* Parse a handler.
16582
16583    handler:
16584      catch ( exception-declaration ) compound-statement  */
16585
16586 static void
16587 cp_parser_handler (cp_parser* parser)
16588 {
16589   tree handler;
16590   tree declaration;
16591
16592   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16593   handler = begin_handler ();
16594   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16595   declaration = cp_parser_exception_declaration (parser);
16596   finish_handler_parms (declaration, handler);
16597   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16598   cp_parser_compound_statement (parser, NULL, false);
16599   finish_handler (handler);
16600 }
16601
16602 /* Parse an exception-declaration.
16603
16604    exception-declaration:
16605      type-specifier-seq declarator
16606      type-specifier-seq abstract-declarator
16607      type-specifier-seq
16608      ...
16609
16610    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16611    ellipsis variant is used.  */
16612
16613 static tree
16614 cp_parser_exception_declaration (cp_parser* parser)
16615 {
16616   cp_decl_specifier_seq type_specifiers;
16617   cp_declarator *declarator;
16618   const char *saved_message;
16619
16620   /* If it's an ellipsis, it's easy to handle.  */
16621   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16622     {
16623       /* Consume the `...' token.  */
16624       cp_lexer_consume_token (parser->lexer);
16625       return NULL_TREE;
16626     }
16627
16628   /* Types may not be defined in exception-declarations.  */
16629   saved_message = parser->type_definition_forbidden_message;
16630   parser->type_definition_forbidden_message
16631     = "types may not be defined in exception-declarations";
16632
16633   /* Parse the type-specifier-seq.  */
16634   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
16635                                 /*is_trailing_return=*/false,
16636                                 &type_specifiers);
16637   /* If it's a `)', then there is no declarator.  */
16638   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16639     declarator = NULL;
16640   else
16641     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16642                                        /*ctor_dtor_or_conv_p=*/NULL,
16643                                        /*parenthesized_p=*/NULL,
16644                                        /*member_p=*/false);
16645
16646   /* Restore the saved message.  */
16647   parser->type_definition_forbidden_message = saved_message;
16648
16649   if (!type_specifiers.any_specifiers_p)
16650     return error_mark_node;
16651
16652   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16653 }
16654
16655 /* Parse a throw-expression.
16656
16657    throw-expression:
16658      throw assignment-expression [opt]
16659
16660    Returns a THROW_EXPR representing the throw-expression.  */
16661
16662 static tree
16663 cp_parser_throw_expression (cp_parser* parser)
16664 {
16665   tree expression;
16666   cp_token* token;
16667
16668   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16669   token = cp_lexer_peek_token (parser->lexer);
16670   /* Figure out whether or not there is an assignment-expression
16671      following the "throw" keyword.  */
16672   if (token->type == CPP_COMMA
16673       || token->type == CPP_SEMICOLON
16674       || token->type == CPP_CLOSE_PAREN
16675       || token->type == CPP_CLOSE_SQUARE
16676       || token->type == CPP_CLOSE_BRACE
16677       || token->type == CPP_COLON)
16678     expression = NULL_TREE;
16679   else
16680     expression = cp_parser_assignment_expression (parser,
16681                                                   /*cast_p=*/false, NULL);
16682
16683   return build_throw (expression);
16684 }
16685
16686 /* GNU Extensions */
16687
16688 /* Parse an (optional) asm-specification.
16689
16690    asm-specification:
16691      asm ( string-literal )
16692
16693    If the asm-specification is present, returns a STRING_CST
16694    corresponding to the string-literal.  Otherwise, returns
16695    NULL_TREE.  */
16696
16697 static tree
16698 cp_parser_asm_specification_opt (cp_parser* parser)
16699 {
16700   cp_token *token;
16701   tree asm_specification;
16702
16703   /* Peek at the next token.  */
16704   token = cp_lexer_peek_token (parser->lexer);
16705   /* If the next token isn't the `asm' keyword, then there's no
16706      asm-specification.  */
16707   if (!cp_parser_is_keyword (token, RID_ASM))
16708     return NULL_TREE;
16709
16710   /* Consume the `asm' token.  */
16711   cp_lexer_consume_token (parser->lexer);
16712   /* Look for the `('.  */
16713   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16714
16715   /* Look for the string-literal.  */
16716   asm_specification = cp_parser_string_literal (parser, false, false);
16717
16718   /* Look for the `)'.  */
16719   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16720
16721   return asm_specification;
16722 }
16723
16724 /* Parse an asm-operand-list.
16725
16726    asm-operand-list:
16727      asm-operand
16728      asm-operand-list , asm-operand
16729
16730    asm-operand:
16731      string-literal ( expression )
16732      [ string-literal ] string-literal ( expression )
16733
16734    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16735    each node is the expression.  The TREE_PURPOSE is itself a
16736    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16737    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16738    is a STRING_CST for the string literal before the parenthesis. Returns
16739    ERROR_MARK_NODE if any of the operands are invalid.  */
16740
16741 static tree
16742 cp_parser_asm_operand_list (cp_parser* parser)
16743 {
16744   tree asm_operands = NULL_TREE;
16745   bool invalid_operands = false;
16746
16747   while (true)
16748     {
16749       tree string_literal;
16750       tree expression;
16751       tree name;
16752
16753       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16754         {
16755           /* Consume the `[' token.  */
16756           cp_lexer_consume_token (parser->lexer);
16757           /* Read the operand name.  */
16758           name = cp_parser_identifier (parser);
16759           if (name != error_mark_node)
16760             name = build_string (IDENTIFIER_LENGTH (name),
16761                                  IDENTIFIER_POINTER (name));
16762           /* Look for the closing `]'.  */
16763           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16764         }
16765       else
16766         name = NULL_TREE;
16767       /* Look for the string-literal.  */
16768       string_literal = cp_parser_string_literal (parser, false, false);
16769
16770       /* Look for the `('.  */
16771       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16772       /* Parse the expression.  */
16773       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16774       /* Look for the `)'.  */
16775       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16776
16777       if (name == error_mark_node 
16778           || string_literal == error_mark_node 
16779           || expression == error_mark_node)
16780         invalid_operands = true;
16781
16782       /* Add this operand to the list.  */
16783       asm_operands = tree_cons (build_tree_list (name, string_literal),
16784                                 expression,
16785                                 asm_operands);
16786       /* If the next token is not a `,', there are no more
16787          operands.  */
16788       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16789         break;
16790       /* Consume the `,'.  */
16791       cp_lexer_consume_token (parser->lexer);
16792     }
16793
16794   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16795 }
16796
16797 /* Parse an asm-clobber-list.
16798
16799    asm-clobber-list:
16800      string-literal
16801      asm-clobber-list , string-literal
16802
16803    Returns a TREE_LIST, indicating the clobbers in the order that they
16804    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16805
16806 static tree
16807 cp_parser_asm_clobber_list (cp_parser* parser)
16808 {
16809   tree clobbers = NULL_TREE;
16810
16811   while (true)
16812     {
16813       tree string_literal;
16814
16815       /* Look for the string literal.  */
16816       string_literal = cp_parser_string_literal (parser, false, false);
16817       /* Add it to the list.  */
16818       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16819       /* If the next token is not a `,', then the list is
16820          complete.  */
16821       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16822         break;
16823       /* Consume the `,' token.  */
16824       cp_lexer_consume_token (parser->lexer);
16825     }
16826
16827   return clobbers;
16828 }
16829
16830 /* Parse an (optional) series of attributes.
16831
16832    attributes:
16833      attributes attribute
16834
16835    attribute:
16836      __attribute__ (( attribute-list [opt] ))
16837
16838    The return value is as for cp_parser_attribute_list.  */
16839
16840 static tree
16841 cp_parser_attributes_opt (cp_parser* parser)
16842 {
16843   tree attributes = NULL_TREE;
16844
16845   while (true)
16846     {
16847       cp_token *token;
16848       tree attribute_list;
16849
16850       /* Peek at the next token.  */
16851       token = cp_lexer_peek_token (parser->lexer);
16852       /* If it's not `__attribute__', then we're done.  */
16853       if (token->keyword != RID_ATTRIBUTE)
16854         break;
16855
16856       /* Consume the `__attribute__' keyword.  */
16857       cp_lexer_consume_token (parser->lexer);
16858       /* Look for the two `(' tokens.  */
16859       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16860       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16861
16862       /* Peek at the next token.  */
16863       token = cp_lexer_peek_token (parser->lexer);
16864       if (token->type != CPP_CLOSE_PAREN)
16865         /* Parse the attribute-list.  */
16866         attribute_list = cp_parser_attribute_list (parser);
16867       else
16868         /* If the next token is a `)', then there is no attribute
16869            list.  */
16870         attribute_list = NULL;
16871
16872       /* Look for the two `)' tokens.  */
16873       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16874       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16875
16876       /* Add these new attributes to the list.  */
16877       attributes = chainon (attributes, attribute_list);
16878     }
16879
16880   return attributes;
16881 }
16882
16883 /* Parse an attribute-list.
16884
16885    attribute-list:
16886      attribute
16887      attribute-list , attribute
16888
16889    attribute:
16890      identifier
16891      identifier ( identifier )
16892      identifier ( identifier , expression-list )
16893      identifier ( expression-list )
16894
16895    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16896    to an attribute.  The TREE_PURPOSE of each node is the identifier
16897    indicating which attribute is in use.  The TREE_VALUE represents
16898    the arguments, if any.  */
16899
16900 static tree
16901 cp_parser_attribute_list (cp_parser* parser)
16902 {
16903   tree attribute_list = NULL_TREE;
16904   bool save_translate_strings_p = parser->translate_strings_p;
16905
16906   parser->translate_strings_p = false;
16907   while (true)
16908     {
16909       cp_token *token;
16910       tree identifier;
16911       tree attribute;
16912
16913       /* Look for the identifier.  We also allow keywords here; for
16914          example `__attribute__ ((const))' is legal.  */
16915       token = cp_lexer_peek_token (parser->lexer);
16916       if (token->type == CPP_NAME
16917           || token->type == CPP_KEYWORD)
16918         {
16919           tree arguments = NULL_TREE;
16920
16921           /* Consume the token.  */
16922           token = cp_lexer_consume_token (parser->lexer);
16923
16924           /* Save away the identifier that indicates which attribute
16925              this is.  */
16926           identifier = token->u.value;
16927           attribute = build_tree_list (identifier, NULL_TREE);
16928
16929           /* Peek at the next token.  */
16930           token = cp_lexer_peek_token (parser->lexer);
16931           /* If it's an `(', then parse the attribute arguments.  */
16932           if (token->type == CPP_OPEN_PAREN)
16933             {
16934               arguments = cp_parser_parenthesized_expression_list
16935                           (parser, true, /*cast_p=*/false,
16936                            /*allow_expansion_p=*/false,
16937                            /*non_constant_p=*/NULL);
16938               /* Save the arguments away.  */
16939               TREE_VALUE (attribute) = arguments;
16940             }
16941
16942           if (arguments != error_mark_node)
16943             {
16944               /* Add this attribute to the list.  */
16945               TREE_CHAIN (attribute) = attribute_list;
16946               attribute_list = attribute;
16947             }
16948
16949           token = cp_lexer_peek_token (parser->lexer);
16950         }
16951       /* Now, look for more attributes.  If the next token isn't a
16952          `,', we're done.  */
16953       if (token->type != CPP_COMMA)
16954         break;
16955
16956       /* Consume the comma and keep going.  */
16957       cp_lexer_consume_token (parser->lexer);
16958     }
16959   parser->translate_strings_p = save_translate_strings_p;
16960
16961   /* We built up the list in reverse order.  */
16962   return nreverse (attribute_list);
16963 }
16964
16965 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16966    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16967    current value of the PEDANTIC flag, regardless of whether or not
16968    the `__extension__' keyword is present.  The caller is responsible
16969    for restoring the value of the PEDANTIC flag.  */
16970
16971 static bool
16972 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16973 {
16974   /* Save the old value of the PEDANTIC flag.  */
16975   *saved_pedantic = pedantic;
16976
16977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16978     {
16979       /* Consume the `__extension__' token.  */
16980       cp_lexer_consume_token (parser->lexer);
16981       /* We're not being pedantic while the `__extension__' keyword is
16982          in effect.  */
16983       pedantic = 0;
16984
16985       return true;
16986     }
16987
16988   return false;
16989 }
16990
16991 /* Parse a label declaration.
16992
16993    label-declaration:
16994      __label__ label-declarator-seq ;
16995
16996    label-declarator-seq:
16997      identifier , label-declarator-seq
16998      identifier  */
16999
17000 static void
17001 cp_parser_label_declaration (cp_parser* parser)
17002 {
17003   /* Look for the `__label__' keyword.  */
17004   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17005
17006   while (true)
17007     {
17008       tree identifier;
17009
17010       /* Look for an identifier.  */
17011       identifier = cp_parser_identifier (parser);
17012       /* If we failed, stop.  */
17013       if (identifier == error_mark_node)
17014         break;
17015       /* Declare it as a label.  */
17016       finish_label_decl (identifier);
17017       /* If the next token is a `;', stop.  */
17018       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17019         break;
17020       /* Look for the `,' separating the label declarations.  */
17021       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17022     }
17023
17024   /* Look for the final `;'.  */
17025   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17026 }
17027
17028 /* Support Functions */
17029
17030 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17031    NAME should have one of the representations used for an
17032    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17033    is returned.  If PARSER->SCOPE is a dependent type, then a
17034    SCOPE_REF is returned.
17035
17036    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17037    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17038    was formed.  Abstractly, such entities should not be passed to this
17039    function, because they do not need to be looked up, but it is
17040    simpler to check for this special case here, rather than at the
17041    call-sites.
17042
17043    In cases not explicitly covered above, this function returns a
17044    DECL, OVERLOAD, or baselink representing the result of the lookup.
17045    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17046    is returned.
17047
17048    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17049    (e.g., "struct") that was used.  In that case bindings that do not
17050    refer to types are ignored.
17051
17052    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17053    ignored.
17054
17055    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17056    are ignored.
17057
17058    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17059    types.
17060
17061    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17062    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17063    NULL_TREE otherwise.  */
17064
17065 static tree
17066 cp_parser_lookup_name (cp_parser *parser, tree name,
17067                        enum tag_types tag_type,
17068                        bool is_template,
17069                        bool is_namespace,
17070                        bool check_dependency,
17071                        tree *ambiguous_decls,
17072                        location_t name_location)
17073 {
17074   int flags = 0;
17075   tree decl;
17076   tree object_type = parser->context->object_type;
17077
17078   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17079     flags |= LOOKUP_COMPLAIN;
17080
17081   /* Assume that the lookup will be unambiguous.  */
17082   if (ambiguous_decls)
17083     *ambiguous_decls = NULL_TREE;
17084
17085   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17086      no longer valid.  Note that if we are parsing tentatively, and
17087      the parse fails, OBJECT_TYPE will be automatically restored.  */
17088   parser->context->object_type = NULL_TREE;
17089
17090   if (name == error_mark_node)
17091     return error_mark_node;
17092
17093   /* A template-id has already been resolved; there is no lookup to
17094      do.  */
17095   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17096     return name;
17097   if (BASELINK_P (name))
17098     {
17099       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17100                   == TEMPLATE_ID_EXPR);
17101       return name;
17102     }
17103
17104   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17105      it should already have been checked to make sure that the name
17106      used matches the type being destroyed.  */
17107   if (TREE_CODE (name) == BIT_NOT_EXPR)
17108     {
17109       tree type;
17110
17111       /* Figure out to which type this destructor applies.  */
17112       if (parser->scope)
17113         type = parser->scope;
17114       else if (object_type)
17115         type = object_type;
17116       else
17117         type = current_class_type;
17118       /* If that's not a class type, there is no destructor.  */
17119       if (!type || !CLASS_TYPE_P (type))
17120         return error_mark_node;
17121       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17122         lazily_declare_fn (sfk_destructor, type);
17123       if (!CLASSTYPE_DESTRUCTORS (type))
17124           return error_mark_node;
17125       /* If it was a class type, return the destructor.  */
17126       return CLASSTYPE_DESTRUCTORS (type);
17127     }
17128
17129   /* By this point, the NAME should be an ordinary identifier.  If
17130      the id-expression was a qualified name, the qualifying scope is
17131      stored in PARSER->SCOPE at this point.  */
17132   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17133
17134   /* Perform the lookup.  */
17135   if (parser->scope)
17136     {
17137       bool dependent_p;
17138
17139       if (parser->scope == error_mark_node)
17140         return error_mark_node;
17141
17142       /* If the SCOPE is dependent, the lookup must be deferred until
17143          the template is instantiated -- unless we are explicitly
17144          looking up names in uninstantiated templates.  Even then, we
17145          cannot look up the name if the scope is not a class type; it
17146          might, for example, be a template type parameter.  */
17147       dependent_p = (TYPE_P (parser->scope)
17148                      && dependent_scope_p (parser->scope));
17149       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17150           && dependent_p)
17151         /* Defer lookup.  */
17152         decl = error_mark_node;
17153       else
17154         {
17155           tree pushed_scope = NULL_TREE;
17156
17157           /* If PARSER->SCOPE is a dependent type, then it must be a
17158              class type, and we must not be checking dependencies;
17159              otherwise, we would have processed this lookup above.  So
17160              that PARSER->SCOPE is not considered a dependent base by
17161              lookup_member, we must enter the scope here.  */
17162           if (dependent_p)
17163             pushed_scope = push_scope (parser->scope);
17164           /* If the PARSER->SCOPE is a template specialization, it
17165              may be instantiated during name lookup.  In that case,
17166              errors may be issued.  Even if we rollback the current
17167              tentative parse, those errors are valid.  */
17168           decl = lookup_qualified_name (parser->scope, name,
17169                                         tag_type != none_type,
17170                                         /*complain=*/true);
17171
17172           /* If we have a single function from a using decl, pull it out.  */
17173           if (TREE_CODE (decl) == OVERLOAD
17174               && !really_overloaded_fn (decl))
17175             decl = OVL_FUNCTION (decl);
17176
17177           if (pushed_scope)
17178             pop_scope (pushed_scope);
17179         }
17180
17181       /* If the scope is a dependent type and either we deferred lookup or
17182          we did lookup but didn't find the name, rememeber the name.  */
17183       if (decl == error_mark_node && TYPE_P (parser->scope)
17184           && dependent_type_p (parser->scope))
17185         {
17186           if (tag_type)
17187             {
17188               tree type;
17189
17190               /* The resolution to Core Issue 180 says that `struct
17191                  A::B' should be considered a type-name, even if `A'
17192                  is dependent.  */
17193               type = make_typename_type (parser->scope, name, tag_type,
17194                                          /*complain=*/tf_error);
17195               decl = TYPE_NAME (type);
17196             }
17197           else if (is_template
17198                    && (cp_parser_next_token_ends_template_argument_p (parser)
17199                        || cp_lexer_next_token_is (parser->lexer,
17200                                                   CPP_CLOSE_PAREN)))
17201             decl = make_unbound_class_template (parser->scope,
17202                                                 name, NULL_TREE,
17203                                                 /*complain=*/tf_error);
17204           else
17205             decl = build_qualified_name (/*type=*/NULL_TREE,
17206                                          parser->scope, name,
17207                                          is_template);
17208         }
17209       parser->qualifying_scope = parser->scope;
17210       parser->object_scope = NULL_TREE;
17211     }
17212   else if (object_type)
17213     {
17214       tree object_decl = NULL_TREE;
17215       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17216          OBJECT_TYPE is not a class.  */
17217       if (CLASS_TYPE_P (object_type))
17218         /* If the OBJECT_TYPE is a template specialization, it may
17219            be instantiated during name lookup.  In that case, errors
17220            may be issued.  Even if we rollback the current tentative
17221            parse, those errors are valid.  */
17222         object_decl = lookup_member (object_type,
17223                                      name,
17224                                      /*protect=*/0,
17225                                      tag_type != none_type);
17226       /* Look it up in the enclosing context, too.  */
17227       decl = lookup_name_real (name, tag_type != none_type,
17228                                /*nonclass=*/0,
17229                                /*block_p=*/true, is_namespace, flags);
17230       parser->object_scope = object_type;
17231       parser->qualifying_scope = NULL_TREE;
17232       if (object_decl)
17233         decl = object_decl;
17234     }
17235   else
17236     {
17237       decl = lookup_name_real (name, tag_type != none_type,
17238                                /*nonclass=*/0,
17239                                /*block_p=*/true, is_namespace, flags);
17240       parser->qualifying_scope = NULL_TREE;
17241       parser->object_scope = NULL_TREE;
17242     }
17243
17244   /* If the lookup failed, let our caller know.  */
17245   if (!decl || decl == error_mark_node)
17246     return error_mark_node;
17247
17248   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17249   if (TREE_CODE (decl) == TREE_LIST)
17250     {
17251       if (ambiguous_decls)
17252         *ambiguous_decls = decl;
17253       /* The error message we have to print is too complicated for
17254          cp_parser_error, so we incorporate its actions directly.  */
17255       if (!cp_parser_simulate_error (parser))
17256         {
17257           error ("%Hreference to %qD is ambiguous",
17258                  &name_location, name);
17259           print_candidates (decl);
17260         }
17261       return error_mark_node;
17262     }
17263
17264   gcc_assert (DECL_P (decl)
17265               || TREE_CODE (decl) == OVERLOAD
17266               || TREE_CODE (decl) == SCOPE_REF
17267               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17268               || BASELINK_P (decl));
17269
17270   /* If we have resolved the name of a member declaration, check to
17271      see if the declaration is accessible.  When the name resolves to
17272      set of overloaded functions, accessibility is checked when
17273      overload resolution is done.
17274
17275      During an explicit instantiation, access is not checked at all,
17276      as per [temp.explicit].  */
17277   if (DECL_P (decl))
17278     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17279
17280   return decl;
17281 }
17282
17283 /* Like cp_parser_lookup_name, but for use in the typical case where
17284    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17285    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17286
17287 static tree
17288 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17289 {
17290   return cp_parser_lookup_name (parser, name,
17291                                 none_type,
17292                                 /*is_template=*/false,
17293                                 /*is_namespace=*/false,
17294                                 /*check_dependency=*/true,
17295                                 /*ambiguous_decls=*/NULL,
17296                                 location);
17297 }
17298
17299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17300    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17301    true, the DECL indicates the class being defined in a class-head,
17302    or declared in an elaborated-type-specifier.
17303
17304    Otherwise, return DECL.  */
17305
17306 static tree
17307 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17308 {
17309   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17310      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17311
17312        struct A {
17313          template <typename T> struct B;
17314        };
17315
17316        template <typename T> struct A::B {};
17317
17318      Similarly, in an elaborated-type-specifier:
17319
17320        namespace N { struct X{}; }
17321
17322        struct A {
17323          template <typename T> friend struct N::X;
17324        };
17325
17326      However, if the DECL refers to a class type, and we are in
17327      the scope of the class, then the name lookup automatically
17328      finds the TYPE_DECL created by build_self_reference rather
17329      than a TEMPLATE_DECL.  For example, in:
17330
17331        template <class T> struct S {
17332          S s;
17333        };
17334
17335      there is no need to handle such case.  */
17336
17337   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17338     return DECL_TEMPLATE_RESULT (decl);
17339
17340   return decl;
17341 }
17342
17343 /* If too many, or too few, template-parameter lists apply to the
17344    declarator, issue an error message.  Returns TRUE if all went well,
17345    and FALSE otherwise.  */
17346
17347 static bool
17348 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17349                                                 cp_declarator *declarator,
17350                                                 location_t declarator_location)
17351 {
17352   unsigned num_templates;
17353
17354   /* We haven't seen any classes that involve template parameters yet.  */
17355   num_templates = 0;
17356
17357   switch (declarator->kind)
17358     {
17359     case cdk_id:
17360       if (declarator->u.id.qualifying_scope)
17361         {
17362           tree scope;
17363           tree member;
17364
17365           scope = declarator->u.id.qualifying_scope;
17366           member = declarator->u.id.unqualified_name;
17367
17368           while (scope && CLASS_TYPE_P (scope))
17369             {
17370               /* You're supposed to have one `template <...>'
17371                  for every template class, but you don't need one
17372                  for a full specialization.  For example:
17373
17374                  template <class T> struct S{};
17375                  template <> struct S<int> { void f(); };
17376                  void S<int>::f () {}
17377
17378                  is correct; there shouldn't be a `template <>' for
17379                  the definition of `S<int>::f'.  */
17380               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17381                 /* If SCOPE does not have template information of any
17382                    kind, then it is not a template, nor is it nested
17383                    within a template.  */
17384                 break;
17385               if (explicit_class_specialization_p (scope))
17386                 break;
17387               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17388                 ++num_templates;
17389
17390               scope = TYPE_CONTEXT (scope);
17391             }
17392         }
17393       else if (TREE_CODE (declarator->u.id.unqualified_name)
17394                == TEMPLATE_ID_EXPR)
17395         /* If the DECLARATOR has the form `X<y>' then it uses one
17396            additional level of template parameters.  */
17397         ++num_templates;
17398
17399       return cp_parser_check_template_parameters (parser,
17400                                                   num_templates,
17401                                                   declarator_location);
17402
17403     case cdk_function:
17404     case cdk_array:
17405     case cdk_pointer:
17406     case cdk_reference:
17407     case cdk_ptrmem:
17408       return (cp_parser_check_declarator_template_parameters
17409               (parser, declarator->declarator, declarator_location));
17410
17411     case cdk_error:
17412       return true;
17413
17414     default:
17415       gcc_unreachable ();
17416     }
17417   return false;
17418 }
17419
17420 /* NUM_TEMPLATES were used in the current declaration.  If that is
17421    invalid, return FALSE and issue an error messages.  Otherwise,
17422    return TRUE.  */
17423
17424 static bool
17425 cp_parser_check_template_parameters (cp_parser* parser,
17426                                      unsigned num_templates,
17427                                      location_t location)
17428 {
17429   /* If there are more template classes than parameter lists, we have
17430      something like:
17431
17432        template <class T> void S<T>::R<T>::f ();  */
17433   if (parser->num_template_parameter_lists < num_templates)
17434     {
17435       error ("%Htoo few template-parameter-lists", &location);
17436       return false;
17437     }
17438   /* If there are the same number of template classes and parameter
17439      lists, that's OK.  */
17440   if (parser->num_template_parameter_lists == num_templates)
17441     return true;
17442   /* If there are more, but only one more, then we are referring to a
17443      member template.  That's OK too.  */
17444   if (parser->num_template_parameter_lists == num_templates + 1)
17445       return true;
17446   /* Otherwise, there are too many template parameter lists.  We have
17447      something like:
17448
17449      template <class T> template <class U> void S::f();  */
17450   error ("%Htoo many template-parameter-lists", &location);
17451   return false;
17452 }
17453
17454 /* Parse an optional `::' token indicating that the following name is
17455    from the global namespace.  If so, PARSER->SCOPE is set to the
17456    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17457    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17458    Returns the new value of PARSER->SCOPE, if the `::' token is
17459    present, and NULL_TREE otherwise.  */
17460
17461 static tree
17462 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17463 {
17464   cp_token *token;
17465
17466   /* Peek at the next token.  */
17467   token = cp_lexer_peek_token (parser->lexer);
17468   /* If we're looking at a `::' token then we're starting from the
17469      global namespace, not our current location.  */
17470   if (token->type == CPP_SCOPE)
17471     {
17472       /* Consume the `::' token.  */
17473       cp_lexer_consume_token (parser->lexer);
17474       /* Set the SCOPE so that we know where to start the lookup.  */
17475       parser->scope = global_namespace;
17476       parser->qualifying_scope = global_namespace;
17477       parser->object_scope = NULL_TREE;
17478
17479       return parser->scope;
17480     }
17481   else if (!current_scope_valid_p)
17482     {
17483       parser->scope = NULL_TREE;
17484       parser->qualifying_scope = NULL_TREE;
17485       parser->object_scope = NULL_TREE;
17486     }
17487
17488   return NULL_TREE;
17489 }
17490
17491 /* Returns TRUE if the upcoming token sequence is the start of a
17492    constructor declarator.  If FRIEND_P is true, the declarator is
17493    preceded by the `friend' specifier.  */
17494
17495 static bool
17496 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17497 {
17498   bool constructor_p;
17499   tree type_decl = NULL_TREE;
17500   bool nested_name_p;
17501   cp_token *next_token;
17502
17503   /* The common case is that this is not a constructor declarator, so
17504      try to avoid doing lots of work if at all possible.  It's not
17505      valid declare a constructor at function scope.  */
17506   if (parser->in_function_body)
17507     return false;
17508   /* And only certain tokens can begin a constructor declarator.  */
17509   next_token = cp_lexer_peek_token (parser->lexer);
17510   if (next_token->type != CPP_NAME
17511       && next_token->type != CPP_SCOPE
17512       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17513       && next_token->type != CPP_TEMPLATE_ID)
17514     return false;
17515
17516   /* Parse tentatively; we are going to roll back all of the tokens
17517      consumed here.  */
17518   cp_parser_parse_tentatively (parser);
17519   /* Assume that we are looking at a constructor declarator.  */
17520   constructor_p = true;
17521
17522   /* Look for the optional `::' operator.  */
17523   cp_parser_global_scope_opt (parser,
17524                               /*current_scope_valid_p=*/false);
17525   /* Look for the nested-name-specifier.  */
17526   nested_name_p
17527     = (cp_parser_nested_name_specifier_opt (parser,
17528                                             /*typename_keyword_p=*/false,
17529                                             /*check_dependency_p=*/false,
17530                                             /*type_p=*/false,
17531                                             /*is_declaration=*/false)
17532        != NULL_TREE);
17533   /* Outside of a class-specifier, there must be a
17534      nested-name-specifier.  */
17535   if (!nested_name_p &&
17536       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17537        || friend_p))
17538     constructor_p = false;
17539   /* If we still think that this might be a constructor-declarator,
17540      look for a class-name.  */
17541   if (constructor_p)
17542     {
17543       /* If we have:
17544
17545            template <typename T> struct S { S(); };
17546            template <typename T> S<T>::S ();
17547
17548          we must recognize that the nested `S' names a class.
17549          Similarly, for:
17550
17551            template <typename T> S<T>::S<T> ();
17552
17553          we must recognize that the nested `S' names a template.  */
17554       type_decl = cp_parser_class_name (parser,
17555                                         /*typename_keyword_p=*/false,
17556                                         /*template_keyword_p=*/false,
17557                                         none_type,
17558                                         /*check_dependency_p=*/false,
17559                                         /*class_head_p=*/false,
17560                                         /*is_declaration=*/false);
17561       /* If there was no class-name, then this is not a constructor.  */
17562       constructor_p = !cp_parser_error_occurred (parser);
17563     }
17564
17565   /* If we're still considering a constructor, we have to see a `(',
17566      to begin the parameter-declaration-clause, followed by either a
17567      `)', an `...', or a decl-specifier.  We need to check for a
17568      type-specifier to avoid being fooled into thinking that:
17569
17570        S::S (f) (int);
17571
17572      is a constructor.  (It is actually a function named `f' that
17573      takes one parameter (of type `int') and returns a value of type
17574      `S::S'.  */
17575   if (constructor_p
17576       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17577     {
17578       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17579           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17580           /* A parameter declaration begins with a decl-specifier,
17581              which is either the "attribute" keyword, a storage class
17582              specifier, or (usually) a type-specifier.  */
17583           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17584         {
17585           tree type;
17586           tree pushed_scope = NULL_TREE;
17587           unsigned saved_num_template_parameter_lists;
17588
17589           /* Names appearing in the type-specifier should be looked up
17590              in the scope of the class.  */
17591           if (current_class_type)
17592             type = NULL_TREE;
17593           else
17594             {
17595               type = TREE_TYPE (type_decl);
17596               if (TREE_CODE (type) == TYPENAME_TYPE)
17597                 {
17598                   type = resolve_typename_type (type,
17599                                                 /*only_current_p=*/false);
17600                   if (TREE_CODE (type) == TYPENAME_TYPE)
17601                     {
17602                       cp_parser_abort_tentative_parse (parser);
17603                       return false;
17604                     }
17605                 }
17606               pushed_scope = push_scope (type);
17607             }
17608
17609           /* Inside the constructor parameter list, surrounding
17610              template-parameter-lists do not apply.  */
17611           saved_num_template_parameter_lists
17612             = parser->num_template_parameter_lists;
17613           parser->num_template_parameter_lists = 0;
17614
17615           /* Look for the type-specifier.  */
17616           cp_parser_type_specifier (parser,
17617                                     CP_PARSER_FLAGS_NONE,
17618                                     /*decl_specs=*/NULL,
17619                                     /*is_declarator=*/true,
17620                                     /*declares_class_or_enum=*/NULL,
17621                                     /*is_cv_qualifier=*/NULL);
17622
17623           parser->num_template_parameter_lists
17624             = saved_num_template_parameter_lists;
17625
17626           /* Leave the scope of the class.  */
17627           if (pushed_scope)
17628             pop_scope (pushed_scope);
17629
17630           constructor_p = !cp_parser_error_occurred (parser);
17631         }
17632     }
17633   else
17634     constructor_p = false;
17635   /* We did not really want to consume any tokens.  */
17636   cp_parser_abort_tentative_parse (parser);
17637
17638   return constructor_p;
17639 }
17640
17641 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17642    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17643    they must be performed once we are in the scope of the function.
17644
17645    Returns the function defined.  */
17646
17647 static tree
17648 cp_parser_function_definition_from_specifiers_and_declarator
17649   (cp_parser* parser,
17650    cp_decl_specifier_seq *decl_specifiers,
17651    tree attributes,
17652    const cp_declarator *declarator)
17653 {
17654   tree fn;
17655   bool success_p;
17656
17657   /* Begin the function-definition.  */
17658   success_p = start_function (decl_specifiers, declarator, attributes);
17659
17660   /* The things we're about to see are not directly qualified by any
17661      template headers we've seen thus far.  */
17662   reset_specialization ();
17663
17664   /* If there were names looked up in the decl-specifier-seq that we
17665      did not check, check them now.  We must wait until we are in the
17666      scope of the function to perform the checks, since the function
17667      might be a friend.  */
17668   perform_deferred_access_checks ();
17669
17670   if (!success_p)
17671     {
17672       /* Skip the entire function.  */
17673       cp_parser_skip_to_end_of_block_or_statement (parser);
17674       fn = error_mark_node;
17675     }
17676   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17677     {
17678       /* Seen already, skip it.  An error message has already been output.  */
17679       cp_parser_skip_to_end_of_block_or_statement (parser);
17680       fn = current_function_decl;
17681       current_function_decl = NULL_TREE;
17682       /* If this is a function from a class, pop the nested class.  */
17683       if (current_class_name)
17684         pop_nested_class ();
17685     }
17686   else
17687     fn = cp_parser_function_definition_after_declarator (parser,
17688                                                          /*inline_p=*/false);
17689
17690   return fn;
17691 }
17692
17693 /* Parse the part of a function-definition that follows the
17694    declarator.  INLINE_P is TRUE iff this function is an inline
17695    function defined with a class-specifier.
17696
17697    Returns the function defined.  */
17698
17699 static tree
17700 cp_parser_function_definition_after_declarator (cp_parser* parser,
17701                                                 bool inline_p)
17702 {
17703   tree fn;
17704   bool ctor_initializer_p = false;
17705   bool saved_in_unbraced_linkage_specification_p;
17706   bool saved_in_function_body;
17707   unsigned saved_num_template_parameter_lists;
17708   cp_token *token;
17709
17710   saved_in_function_body = parser->in_function_body;
17711   parser->in_function_body = true;
17712   /* If the next token is `return', then the code may be trying to
17713      make use of the "named return value" extension that G++ used to
17714      support.  */
17715   token = cp_lexer_peek_token (parser->lexer);
17716   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17717     {
17718       /* Consume the `return' keyword.  */
17719       cp_lexer_consume_token (parser->lexer);
17720       /* Look for the identifier that indicates what value is to be
17721          returned.  */
17722       cp_parser_identifier (parser);
17723       /* Issue an error message.  */
17724       error ("%Hnamed return values are no longer supported",
17725              &token->location);
17726       /* Skip tokens until we reach the start of the function body.  */
17727       while (true)
17728         {
17729           cp_token *token = cp_lexer_peek_token (parser->lexer);
17730           if (token->type == CPP_OPEN_BRACE
17731               || token->type == CPP_EOF
17732               || token->type == CPP_PRAGMA_EOL)
17733             break;
17734           cp_lexer_consume_token (parser->lexer);
17735         }
17736     }
17737   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17738      anything declared inside `f'.  */
17739   saved_in_unbraced_linkage_specification_p
17740     = parser->in_unbraced_linkage_specification_p;
17741   parser->in_unbraced_linkage_specification_p = false;
17742   /* Inside the function, surrounding template-parameter-lists do not
17743      apply.  */
17744   saved_num_template_parameter_lists
17745     = parser->num_template_parameter_lists;
17746   parser->num_template_parameter_lists = 0;
17747   /* If the next token is `try', then we are looking at a
17748      function-try-block.  */
17749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17750     ctor_initializer_p = cp_parser_function_try_block (parser);
17751   /* A function-try-block includes the function-body, so we only do
17752      this next part if we're not processing a function-try-block.  */
17753   else
17754     ctor_initializer_p
17755       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17756
17757   /* Finish the function.  */
17758   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17759                         (inline_p ? 2 : 0));
17760   /* Generate code for it, if necessary.  */
17761   expand_or_defer_fn (fn);
17762   /* Restore the saved values.  */
17763   parser->in_unbraced_linkage_specification_p
17764     = saved_in_unbraced_linkage_specification_p;
17765   parser->num_template_parameter_lists
17766     = saved_num_template_parameter_lists;
17767   parser->in_function_body = saved_in_function_body;
17768
17769   return fn;
17770 }
17771
17772 /* Parse a template-declaration, assuming that the `export' (and
17773    `extern') keywords, if present, has already been scanned.  MEMBER_P
17774    is as for cp_parser_template_declaration.  */
17775
17776 static void
17777 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17778 {
17779   tree decl = NULL_TREE;
17780   VEC (deferred_access_check,gc) *checks;
17781   tree parameter_list;
17782   bool friend_p = false;
17783   bool need_lang_pop;
17784   cp_token *token;
17785
17786   /* Look for the `template' keyword.  */
17787   token = cp_lexer_peek_token (parser->lexer);
17788   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17789     return;
17790
17791   /* And the `<'.  */
17792   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17793     return;
17794   if (at_class_scope_p () && current_function_decl)
17795     {
17796       /* 14.5.2.2 [temp.mem]
17797
17798          A local class shall not have member templates.  */
17799       error ("%Hinvalid declaration of member template in local class",
17800              &token->location);
17801       cp_parser_skip_to_end_of_block_or_statement (parser);
17802       return;
17803     }
17804   /* [temp]
17805
17806      A template ... shall not have C linkage.  */
17807   if (current_lang_name == lang_name_c)
17808     {
17809       error ("%Htemplate with C linkage", &token->location);
17810       /* Give it C++ linkage to avoid confusing other parts of the
17811          front end.  */
17812       push_lang_context (lang_name_cplusplus);
17813       need_lang_pop = true;
17814     }
17815   else
17816     need_lang_pop = false;
17817
17818   /* We cannot perform access checks on the template parameter
17819      declarations until we know what is being declared, just as we
17820      cannot check the decl-specifier list.  */
17821   push_deferring_access_checks (dk_deferred);
17822
17823   /* If the next token is `>', then we have an invalid
17824      specialization.  Rather than complain about an invalid template
17825      parameter, issue an error message here.  */
17826   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17827     {
17828       cp_parser_error (parser, "invalid explicit specialization");
17829       begin_specialization ();
17830       parameter_list = NULL_TREE;
17831     }
17832   else
17833     /* Parse the template parameters.  */
17834     parameter_list = cp_parser_template_parameter_list (parser);
17835
17836   /* Get the deferred access checks from the parameter list.  These
17837      will be checked once we know what is being declared, as for a
17838      member template the checks must be performed in the scope of the
17839      class containing the member.  */
17840   checks = get_deferred_access_checks ();
17841
17842   /* Look for the `>'.  */
17843   cp_parser_skip_to_end_of_template_parameter_list (parser);
17844   /* We just processed one more parameter list.  */
17845   ++parser->num_template_parameter_lists;
17846   /* If the next token is `template', there are more template
17847      parameters.  */
17848   if (cp_lexer_next_token_is_keyword (parser->lexer,
17849                                       RID_TEMPLATE))
17850     cp_parser_template_declaration_after_export (parser, member_p);
17851   else
17852     {
17853       /* There are no access checks when parsing a template, as we do not
17854          know if a specialization will be a friend.  */
17855       push_deferring_access_checks (dk_no_check);
17856       token = cp_lexer_peek_token (parser->lexer);
17857       decl = cp_parser_single_declaration (parser,
17858                                            checks,
17859                                            member_p,
17860                                            /*explicit_specialization_p=*/false,
17861                                            &friend_p);
17862       pop_deferring_access_checks ();
17863
17864       /* If this is a member template declaration, let the front
17865          end know.  */
17866       if (member_p && !friend_p && decl)
17867         {
17868           if (TREE_CODE (decl) == TYPE_DECL)
17869             cp_parser_check_access_in_redeclaration (decl, token->location);
17870
17871           decl = finish_member_template_decl (decl);
17872         }
17873       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17874         make_friend_class (current_class_type, TREE_TYPE (decl),
17875                            /*complain=*/true);
17876     }
17877   /* We are done with the current parameter list.  */
17878   --parser->num_template_parameter_lists;
17879
17880   pop_deferring_access_checks ();
17881
17882   /* Finish up.  */
17883   finish_template_decl (parameter_list);
17884
17885   /* Register member declarations.  */
17886   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17887     finish_member_declaration (decl);
17888   /* For the erroneous case of a template with C linkage, we pushed an
17889      implicit C++ linkage scope; exit that scope now.  */
17890   if (need_lang_pop)
17891     pop_lang_context ();
17892   /* If DECL is a function template, we must return to parse it later.
17893      (Even though there is no definition, there might be default
17894      arguments that need handling.)  */
17895   if (member_p && decl
17896       && (TREE_CODE (decl) == FUNCTION_DECL
17897           || DECL_FUNCTION_TEMPLATE_P (decl)))
17898     TREE_VALUE (parser->unparsed_functions_queues)
17899       = tree_cons (NULL_TREE, decl,
17900                    TREE_VALUE (parser->unparsed_functions_queues));
17901 }
17902
17903 /* Perform the deferred access checks from a template-parameter-list.
17904    CHECKS is a TREE_LIST of access checks, as returned by
17905    get_deferred_access_checks.  */
17906
17907 static void
17908 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17909 {
17910   ++processing_template_parmlist;
17911   perform_access_checks (checks);
17912   --processing_template_parmlist;
17913 }
17914
17915 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17916    `function-definition' sequence.  MEMBER_P is true, this declaration
17917    appears in a class scope.
17918
17919    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17920    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17921
17922 static tree
17923 cp_parser_single_declaration (cp_parser* parser,
17924                               VEC (deferred_access_check,gc)* checks,
17925                               bool member_p,
17926                               bool explicit_specialization_p,
17927                               bool* friend_p)
17928 {
17929   int declares_class_or_enum;
17930   tree decl = NULL_TREE;
17931   cp_decl_specifier_seq decl_specifiers;
17932   bool function_definition_p = false;
17933   cp_token *decl_spec_token_start;
17934
17935   /* This function is only used when processing a template
17936      declaration.  */
17937   gcc_assert (innermost_scope_kind () == sk_template_parms
17938               || innermost_scope_kind () == sk_template_spec);
17939
17940   /* Defer access checks until we know what is being declared.  */
17941   push_deferring_access_checks (dk_deferred);
17942
17943   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17944      alternative.  */
17945   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17946   cp_parser_decl_specifier_seq (parser,
17947                                 CP_PARSER_FLAGS_OPTIONAL,
17948                                 &decl_specifiers,
17949                                 &declares_class_or_enum);
17950   if (friend_p)
17951     *friend_p = cp_parser_friend_p (&decl_specifiers);
17952
17953   /* There are no template typedefs.  */
17954   if (decl_specifiers.specs[(int) ds_typedef])
17955     {
17956       error ("%Htemplate declaration of %qs",
17957              &decl_spec_token_start->location, "typedef");
17958       decl = error_mark_node;
17959     }
17960
17961   /* Gather up the access checks that occurred the
17962      decl-specifier-seq.  */
17963   stop_deferring_access_checks ();
17964
17965   /* Check for the declaration of a template class.  */
17966   if (declares_class_or_enum)
17967     {
17968       if (cp_parser_declares_only_class_p (parser))
17969         {
17970           decl = shadow_tag (&decl_specifiers);
17971
17972           /* In this case:
17973
17974                struct C {
17975                  friend template <typename T> struct A<T>::B;
17976                };
17977
17978              A<T>::B will be represented by a TYPENAME_TYPE, and
17979              therefore not recognized by shadow_tag.  */
17980           if (friend_p && *friend_p
17981               && !decl
17982               && decl_specifiers.type
17983               && TYPE_P (decl_specifiers.type))
17984             decl = decl_specifiers.type;
17985
17986           if (decl && decl != error_mark_node)
17987             decl = TYPE_NAME (decl);
17988           else
17989             decl = error_mark_node;
17990
17991           /* Perform access checks for template parameters.  */
17992           cp_parser_perform_template_parameter_access_checks (checks);
17993         }
17994     }
17995   /* If it's not a template class, try for a template function.  If
17996      the next token is a `;', then this declaration does not declare
17997      anything.  But, if there were errors in the decl-specifiers, then
17998      the error might well have come from an attempted class-specifier.
17999      In that case, there's no need to warn about a missing declarator.  */
18000   if (!decl
18001       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18002           || decl_specifiers.type != error_mark_node))
18003     {
18004       decl = cp_parser_init_declarator (parser,
18005                                         &decl_specifiers,
18006                                         checks,
18007                                         /*function_definition_allowed_p=*/true,
18008                                         member_p,
18009                                         declares_class_or_enum,
18010                                         &function_definition_p);
18011
18012     /* 7.1.1-1 [dcl.stc]
18013
18014        A storage-class-specifier shall not be specified in an explicit
18015        specialization...  */
18016     if (decl
18017         && explicit_specialization_p
18018         && decl_specifiers.storage_class != sc_none)
18019       {
18020         error ("%Hexplicit template specialization cannot have a storage class",
18021                &decl_spec_token_start->location);
18022         decl = error_mark_node;
18023       }
18024     }
18025
18026   pop_deferring_access_checks ();
18027
18028   /* Clear any current qualification; whatever comes next is the start
18029      of something new.  */
18030   parser->scope = NULL_TREE;
18031   parser->qualifying_scope = NULL_TREE;
18032   parser->object_scope = NULL_TREE;
18033   /* Look for a trailing `;' after the declaration.  */
18034   if (!function_definition_p
18035       && (decl == error_mark_node
18036           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18037     cp_parser_skip_to_end_of_block_or_statement (parser);
18038
18039   return decl;
18040 }
18041
18042 /* Parse a cast-expression that is not the operand of a unary "&".  */
18043
18044 static tree
18045 cp_parser_simple_cast_expression (cp_parser *parser)
18046 {
18047   return cp_parser_cast_expression (parser, /*address_p=*/false,
18048                                     /*cast_p=*/false, NULL);
18049 }
18050
18051 /* Parse a functional cast to TYPE.  Returns an expression
18052    representing the cast.  */
18053
18054 static tree
18055 cp_parser_functional_cast (cp_parser* parser, tree type)
18056 {
18057   tree expression_list;
18058   tree cast;
18059   bool nonconst_p;
18060
18061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18062     {
18063       maybe_warn_cpp0x ("extended initializer lists");
18064       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18065       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18066       if (TREE_CODE (type) == TYPE_DECL)
18067         type = TREE_TYPE (type);
18068       return finish_compound_literal (type, expression_list);
18069     }
18070
18071   expression_list
18072     = cp_parser_parenthesized_expression_list (parser, false,
18073                                                /*cast_p=*/true,
18074                                                /*allow_expansion_p=*/true,
18075                                                /*non_constant_p=*/NULL);
18076
18077   cast = build_functional_cast (type, expression_list,
18078                                 tf_warning_or_error);
18079   /* [expr.const]/1: In an integral constant expression "only type
18080      conversions to integral or enumeration type can be used".  */
18081   if (TREE_CODE (type) == TYPE_DECL)
18082     type = TREE_TYPE (type);
18083   if (cast != error_mark_node
18084       && !cast_valid_in_integral_constant_expression_p (type)
18085       && (cp_parser_non_integral_constant_expression
18086           (parser, "a call to a constructor")))
18087     return error_mark_node;
18088   return cast;
18089 }
18090
18091 /* Save the tokens that make up the body of a member function defined
18092    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18093    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18094    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18095    for the member function.  */
18096
18097 static tree
18098 cp_parser_save_member_function_body (cp_parser* parser,
18099                                      cp_decl_specifier_seq *decl_specifiers,
18100                                      cp_declarator *declarator,
18101                                      tree attributes)
18102 {
18103   cp_token *first;
18104   cp_token *last;
18105   tree fn;
18106
18107   /* Create the function-declaration.  */
18108   fn = start_method (decl_specifiers, declarator, attributes);
18109   /* If something went badly wrong, bail out now.  */
18110   if (fn == error_mark_node)
18111     {
18112       /* If there's a function-body, skip it.  */
18113       if (cp_parser_token_starts_function_definition_p
18114           (cp_lexer_peek_token (parser->lexer)))
18115         cp_parser_skip_to_end_of_block_or_statement (parser);
18116       return error_mark_node;
18117     }
18118
18119   /* Remember it, if there default args to post process.  */
18120   cp_parser_save_default_args (parser, fn);
18121
18122   /* Save away the tokens that make up the body of the
18123      function.  */
18124   first = parser->lexer->next_token;
18125   /* We can have braced-init-list mem-initializers before the fn body.  */
18126   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18127     {
18128       cp_lexer_consume_token (parser->lexer);
18129       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18130              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18131         {
18132           /* cache_group will stop after an un-nested { } pair, too.  */
18133           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18134             break;
18135
18136           /* variadic mem-inits have ... after the ')'.  */
18137           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18138             cp_lexer_consume_token (parser->lexer);
18139         }
18140     }
18141   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18142   /* Handle function try blocks.  */
18143   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18144     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18145   last = parser->lexer->next_token;
18146
18147   /* Save away the inline definition; we will process it when the
18148      class is complete.  */
18149   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18150   DECL_PENDING_INLINE_P (fn) = 1;
18151
18152   /* We need to know that this was defined in the class, so that
18153      friend templates are handled correctly.  */
18154   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18155
18156   /* We're done with the inline definition.  */
18157   finish_method (fn);
18158
18159   /* Add FN to the queue of functions to be parsed later.  */
18160   TREE_VALUE (parser->unparsed_functions_queues)
18161     = tree_cons (NULL_TREE, fn,
18162                  TREE_VALUE (parser->unparsed_functions_queues));
18163
18164   return fn;
18165 }
18166
18167 /* Parse a template-argument-list, as well as the trailing ">" (but
18168    not the opening ">").  See cp_parser_template_argument_list for the
18169    return value.  */
18170
18171 static tree
18172 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18173 {
18174   tree arguments;
18175   tree saved_scope;
18176   tree saved_qualifying_scope;
18177   tree saved_object_scope;
18178   bool saved_greater_than_is_operator_p;
18179   bool saved_skip_evaluation;
18180
18181   /* [temp.names]
18182
18183      When parsing a template-id, the first non-nested `>' is taken as
18184      the end of the template-argument-list rather than a greater-than
18185      operator.  */
18186   saved_greater_than_is_operator_p
18187     = parser->greater_than_is_operator_p;
18188   parser->greater_than_is_operator_p = false;
18189   /* Parsing the argument list may modify SCOPE, so we save it
18190      here.  */
18191   saved_scope = parser->scope;
18192   saved_qualifying_scope = parser->qualifying_scope;
18193   saved_object_scope = parser->object_scope;
18194   /* We need to evaluate the template arguments, even though this
18195      template-id may be nested within a "sizeof".  */
18196   saved_skip_evaluation = skip_evaluation;
18197   skip_evaluation = false;
18198   /* Parse the template-argument-list itself.  */
18199   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18200       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18201     arguments = NULL_TREE;
18202   else
18203     arguments = cp_parser_template_argument_list (parser);
18204   /* Look for the `>' that ends the template-argument-list. If we find
18205      a '>>' instead, it's probably just a typo.  */
18206   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18207     {
18208       if (cxx_dialect != cxx98)
18209         {
18210           /* In C++0x, a `>>' in a template argument list or cast
18211              expression is considered to be two separate `>'
18212              tokens. So, change the current token to a `>', but don't
18213              consume it: it will be consumed later when the outer
18214              template argument list (or cast expression) is parsed.
18215              Note that this replacement of `>' for `>>' is necessary
18216              even if we are parsing tentatively: in the tentative
18217              case, after calling
18218              cp_parser_enclosed_template_argument_list we will always
18219              throw away all of the template arguments and the first
18220              closing `>', either because the template argument list
18221              was erroneous or because we are replacing those tokens
18222              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18223              not have been thrown away) is needed either to close an
18224              outer template argument list or to complete a new-style
18225              cast.  */
18226           cp_token *token = cp_lexer_peek_token (parser->lexer);
18227           token->type = CPP_GREATER;
18228         }
18229       else if (!saved_greater_than_is_operator_p)
18230         {
18231           /* If we're in a nested template argument list, the '>>' has
18232             to be a typo for '> >'. We emit the error message, but we
18233             continue parsing and we push a '>' as next token, so that
18234             the argument list will be parsed correctly.  Note that the
18235             global source location is still on the token before the
18236             '>>', so we need to say explicitly where we want it.  */
18237           cp_token *token = cp_lexer_peek_token (parser->lexer);
18238           error ("%H%<>>%> should be %<> >%> "
18239                  "within a nested template argument list",
18240                  &token->location);
18241
18242           token->type = CPP_GREATER;
18243         }
18244       else
18245         {
18246           /* If this is not a nested template argument list, the '>>'
18247             is a typo for '>'. Emit an error message and continue.
18248             Same deal about the token location, but here we can get it
18249             right by consuming the '>>' before issuing the diagnostic.  */
18250           cp_token *token = cp_lexer_consume_token (parser->lexer);
18251           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18252                  "a template argument list", &token->location);
18253         }
18254     }
18255   else
18256     cp_parser_skip_to_end_of_template_parameter_list (parser);
18257   /* The `>' token might be a greater-than operator again now.  */
18258   parser->greater_than_is_operator_p
18259     = saved_greater_than_is_operator_p;
18260   /* Restore the SAVED_SCOPE.  */
18261   parser->scope = saved_scope;
18262   parser->qualifying_scope = saved_qualifying_scope;
18263   parser->object_scope = saved_object_scope;
18264   skip_evaluation = saved_skip_evaluation;
18265
18266   return arguments;
18267 }
18268
18269 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18270    arguments, or the body of the function have not yet been parsed,
18271    parse them now.  */
18272
18273 static void
18274 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18275 {
18276   /* If this member is a template, get the underlying
18277      FUNCTION_DECL.  */
18278   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18279     member_function = DECL_TEMPLATE_RESULT (member_function);
18280
18281   /* There should not be any class definitions in progress at this
18282      point; the bodies of members are only parsed outside of all class
18283      definitions.  */
18284   gcc_assert (parser->num_classes_being_defined == 0);
18285   /* While we're parsing the member functions we might encounter more
18286      classes.  We want to handle them right away, but we don't want
18287      them getting mixed up with functions that are currently in the
18288      queue.  */
18289   parser->unparsed_functions_queues
18290     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18291
18292   /* Make sure that any template parameters are in scope.  */
18293   maybe_begin_member_template_processing (member_function);
18294
18295   /* If the body of the function has not yet been parsed, parse it
18296      now.  */
18297   if (DECL_PENDING_INLINE_P (member_function))
18298     {
18299       tree function_scope;
18300       cp_token_cache *tokens;
18301
18302       /* The function is no longer pending; we are processing it.  */
18303       tokens = DECL_PENDING_INLINE_INFO (member_function);
18304       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18305       DECL_PENDING_INLINE_P (member_function) = 0;
18306
18307       /* If this is a local class, enter the scope of the containing
18308          function.  */
18309       function_scope = current_function_decl;
18310       if (function_scope)
18311         push_function_context ();
18312
18313       /* Push the body of the function onto the lexer stack.  */
18314       cp_parser_push_lexer_for_tokens (parser, tokens);
18315
18316       /* Let the front end know that we going to be defining this
18317          function.  */
18318       start_preparsed_function (member_function, NULL_TREE,
18319                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18320
18321       /* Don't do access checking if it is a templated function.  */
18322       if (processing_template_decl)
18323         push_deferring_access_checks (dk_no_check);
18324
18325       /* Now, parse the body of the function.  */
18326       cp_parser_function_definition_after_declarator (parser,
18327                                                       /*inline_p=*/true);
18328
18329       if (processing_template_decl)
18330         pop_deferring_access_checks ();
18331
18332       /* Leave the scope of the containing function.  */
18333       if (function_scope)
18334         pop_function_context ();
18335       cp_parser_pop_lexer (parser);
18336     }
18337
18338   /* Remove any template parameters from the symbol table.  */
18339   maybe_end_member_template_processing ();
18340
18341   /* Restore the queue.  */
18342   parser->unparsed_functions_queues
18343     = TREE_CHAIN (parser->unparsed_functions_queues);
18344 }
18345
18346 /* If DECL contains any default args, remember it on the unparsed
18347    functions queue.  */
18348
18349 static void
18350 cp_parser_save_default_args (cp_parser* parser, tree decl)
18351 {
18352   tree probe;
18353
18354   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18355        probe;
18356        probe = TREE_CHAIN (probe))
18357     if (TREE_PURPOSE (probe))
18358       {
18359         TREE_PURPOSE (parser->unparsed_functions_queues)
18360           = tree_cons (current_class_type, decl,
18361                        TREE_PURPOSE (parser->unparsed_functions_queues));
18362         break;
18363       }
18364 }
18365
18366 /* FN is a FUNCTION_DECL which may contains a parameter with an
18367    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18368    assumes that the current scope is the scope in which the default
18369    argument should be processed.  */
18370
18371 static void
18372 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18373 {
18374   bool saved_local_variables_forbidden_p;
18375   tree parm;
18376
18377   /* While we're parsing the default args, we might (due to the
18378      statement expression extension) encounter more classes.  We want
18379      to handle them right away, but we don't want them getting mixed
18380      up with default args that are currently in the queue.  */
18381   parser->unparsed_functions_queues
18382     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18383
18384   /* Local variable names (and the `this' keyword) may not appear
18385      in a default argument.  */
18386   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18387   parser->local_variables_forbidden_p = true;
18388
18389   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18390        parm;
18391        parm = TREE_CHAIN (parm))
18392     {
18393       cp_token_cache *tokens;
18394       tree default_arg = TREE_PURPOSE (parm);
18395       tree parsed_arg;
18396       VEC(tree,gc) *insts;
18397       tree copy;
18398       unsigned ix;
18399
18400       if (!default_arg)
18401         continue;
18402
18403       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18404         /* This can happen for a friend declaration for a function
18405            already declared with default arguments.  */
18406         continue;
18407
18408        /* Push the saved tokens for the default argument onto the parser's
18409           lexer stack.  */
18410       tokens = DEFARG_TOKENS (default_arg);
18411       cp_parser_push_lexer_for_tokens (parser, tokens);
18412
18413       /* Parse the assignment-expression.  */
18414       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18415       if (parsed_arg == error_mark_node)
18416         {
18417           cp_parser_pop_lexer (parser);
18418           continue;
18419         }
18420
18421       if (!processing_template_decl)
18422         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18423
18424       TREE_PURPOSE (parm) = parsed_arg;
18425
18426       /* Update any instantiations we've already created.  */
18427       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18428            VEC_iterate (tree, insts, ix, copy); ix++)
18429         TREE_PURPOSE (copy) = parsed_arg;
18430
18431       /* If the token stream has not been completely used up, then
18432          there was extra junk after the end of the default
18433          argument.  */
18434       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18435         cp_parser_error (parser, "expected %<,%>");
18436
18437       /* Revert to the main lexer.  */
18438       cp_parser_pop_lexer (parser);
18439     }
18440
18441   /* Make sure no default arg is missing.  */
18442   check_default_args (fn);
18443
18444   /* Restore the state of local_variables_forbidden_p.  */
18445   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18446
18447   /* Restore the queue.  */
18448   parser->unparsed_functions_queues
18449     = TREE_CHAIN (parser->unparsed_functions_queues);
18450 }
18451
18452 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18453    either a TYPE or an expression, depending on the form of the
18454    input.  The KEYWORD indicates which kind of expression we have
18455    encountered.  */
18456
18457 static tree
18458 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18459 {
18460   tree expr = NULL_TREE;
18461   const char *saved_message;
18462   char *tmp;
18463   bool saved_integral_constant_expression_p;
18464   bool saved_non_integral_constant_expression_p;
18465   bool pack_expansion_p = false;
18466
18467   /* Types cannot be defined in a `sizeof' expression.  Save away the
18468      old message.  */
18469   saved_message = parser->type_definition_forbidden_message;
18470   /* And create the new one.  */
18471   tmp = concat ("types may not be defined in %<",
18472                 IDENTIFIER_POINTER (ridpointers[keyword]),
18473                 "%> expressions", NULL);
18474   parser->type_definition_forbidden_message = tmp;
18475
18476   /* The restrictions on constant-expressions do not apply inside
18477      sizeof expressions.  */
18478   saved_integral_constant_expression_p
18479     = parser->integral_constant_expression_p;
18480   saved_non_integral_constant_expression_p
18481     = parser->non_integral_constant_expression_p;
18482   parser->integral_constant_expression_p = false;
18483
18484   /* If it's a `...', then we are computing the length of a parameter
18485      pack.  */
18486   if (keyword == RID_SIZEOF
18487       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18488     {
18489       /* Consume the `...'.  */
18490       cp_lexer_consume_token (parser->lexer);
18491       maybe_warn_variadic_templates ();
18492
18493       /* Note that this is an expansion.  */
18494       pack_expansion_p = true;
18495     }
18496
18497   /* Do not actually evaluate the expression.  */
18498   ++skip_evaluation;
18499   /* If it's a `(', then we might be looking at the type-id
18500      construction.  */
18501   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18502     {
18503       tree type;
18504       bool saved_in_type_id_in_expr_p;
18505
18506       /* We can't be sure yet whether we're looking at a type-id or an
18507          expression.  */
18508       cp_parser_parse_tentatively (parser);
18509       /* Consume the `('.  */
18510       cp_lexer_consume_token (parser->lexer);
18511       /* Parse the type-id.  */
18512       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18513       parser->in_type_id_in_expr_p = true;
18514       type = cp_parser_type_id (parser);
18515       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18516       /* Now, look for the trailing `)'.  */
18517       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18518       /* If all went well, then we're done.  */
18519       if (cp_parser_parse_definitely (parser))
18520         {
18521           cp_decl_specifier_seq decl_specs;
18522
18523           /* Build a trivial decl-specifier-seq.  */
18524           clear_decl_specs (&decl_specs);
18525           decl_specs.type = type;
18526
18527           /* Call grokdeclarator to figure out what type this is.  */
18528           expr = grokdeclarator (NULL,
18529                                  &decl_specs,
18530                                  TYPENAME,
18531                                  /*initialized=*/0,
18532                                  /*attrlist=*/NULL);
18533         }
18534     }
18535
18536   /* If the type-id production did not work out, then we must be
18537      looking at the unary-expression production.  */
18538   if (!expr)
18539     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18540                                        /*cast_p=*/false, NULL);
18541
18542   if (pack_expansion_p)
18543     /* Build a pack expansion. */
18544     expr = make_pack_expansion (expr);
18545
18546   /* Go back to evaluating expressions.  */
18547   --skip_evaluation;
18548
18549   /* Free the message we created.  */
18550   free (tmp);
18551   /* And restore the old one.  */
18552   parser->type_definition_forbidden_message = saved_message;
18553   parser->integral_constant_expression_p
18554     = saved_integral_constant_expression_p;
18555   parser->non_integral_constant_expression_p
18556     = saved_non_integral_constant_expression_p;
18557
18558   return expr;
18559 }
18560
18561 /* If the current declaration has no declarator, return true.  */
18562
18563 static bool
18564 cp_parser_declares_only_class_p (cp_parser *parser)
18565 {
18566   /* If the next token is a `;' or a `,' then there is no
18567      declarator.  */
18568   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18569           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18570 }
18571
18572 /* Update the DECL_SPECS to reflect the storage class indicated by
18573    KEYWORD.  */
18574
18575 static void
18576 cp_parser_set_storage_class (cp_parser *parser,
18577                              cp_decl_specifier_seq *decl_specs,
18578                              enum rid keyword,
18579                              location_t location)
18580 {
18581   cp_storage_class storage_class;
18582
18583   if (parser->in_unbraced_linkage_specification_p)
18584     {
18585       error ("%Hinvalid use of %qD in linkage specification",
18586              &location, ridpointers[keyword]);
18587       return;
18588     }
18589   else if (decl_specs->storage_class != sc_none)
18590     {
18591       decl_specs->conflicting_specifiers_p = true;
18592       return;
18593     }
18594
18595   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18596       && decl_specs->specs[(int) ds_thread])
18597     {
18598       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18599       decl_specs->specs[(int) ds_thread] = 0;
18600     }
18601
18602   switch (keyword)
18603     {
18604     case RID_AUTO:
18605       storage_class = sc_auto;
18606       break;
18607     case RID_REGISTER:
18608       storage_class = sc_register;
18609       break;
18610     case RID_STATIC:
18611       storage_class = sc_static;
18612       break;
18613     case RID_EXTERN:
18614       storage_class = sc_extern;
18615       break;
18616     case RID_MUTABLE:
18617       storage_class = sc_mutable;
18618       break;
18619     default:
18620       gcc_unreachable ();
18621     }
18622   decl_specs->storage_class = storage_class;
18623
18624   /* A storage class specifier cannot be applied alongside a typedef 
18625      specifier. If there is a typedef specifier present then set 
18626      conflicting_specifiers_p which will trigger an error later
18627      on in grokdeclarator. */
18628   if (decl_specs->specs[(int)ds_typedef])
18629     decl_specs->conflicting_specifiers_p = true;
18630 }
18631
18632 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18633    is true, the type is a user-defined type; otherwise it is a
18634    built-in type specified by a keyword.  */
18635
18636 static void
18637 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18638                               tree type_spec,
18639                               location_t location,
18640                               bool user_defined_p)
18641 {
18642   decl_specs->any_specifiers_p = true;
18643
18644   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18645      (with, for example, in "typedef int wchar_t;") we remember that
18646      this is what happened.  In system headers, we ignore these
18647      declarations so that G++ can work with system headers that are not
18648      C++-safe.  */
18649   if (decl_specs->specs[(int) ds_typedef]
18650       && !user_defined_p
18651       && (type_spec == boolean_type_node
18652           || type_spec == char16_type_node
18653           || type_spec == char32_type_node
18654           || type_spec == wchar_type_node)
18655       && (decl_specs->type
18656           || decl_specs->specs[(int) ds_long]
18657           || decl_specs->specs[(int) ds_short]
18658           || decl_specs->specs[(int) ds_unsigned]
18659           || decl_specs->specs[(int) ds_signed]))
18660     {
18661       decl_specs->redefined_builtin_type = type_spec;
18662       if (!decl_specs->type)
18663         {
18664           decl_specs->type = type_spec;
18665           decl_specs->user_defined_type_p = false;
18666           decl_specs->type_location = location;
18667         }
18668     }
18669   else if (decl_specs->type)
18670     decl_specs->multiple_types_p = true;
18671   else
18672     {
18673       decl_specs->type = type_spec;
18674       decl_specs->user_defined_type_p = user_defined_p;
18675       decl_specs->redefined_builtin_type = NULL_TREE;
18676       decl_specs->type_location = location;
18677     }
18678 }
18679
18680 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18681    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18682
18683 static bool
18684 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18685 {
18686   return decl_specifiers->specs[(int) ds_friend] != 0;
18687 }
18688
18689 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18690    issue an error message indicating that TOKEN_DESC was expected.
18691
18692    Returns the token consumed, if the token had the appropriate type.
18693    Otherwise, returns NULL.  */
18694
18695 static cp_token *
18696 cp_parser_require (cp_parser* parser,
18697                    enum cpp_ttype type,
18698                    const char* token_desc)
18699 {
18700   if (cp_lexer_next_token_is (parser->lexer, type))
18701     return cp_lexer_consume_token (parser->lexer);
18702   else
18703     {
18704       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18705       if (!cp_parser_simulate_error (parser))
18706         {
18707           char *message = concat ("expected ", token_desc, NULL);
18708           cp_parser_error (parser, message);
18709           free (message);
18710         }
18711       return NULL;
18712     }
18713 }
18714
18715 /* An error message is produced if the next token is not '>'.
18716    All further tokens are skipped until the desired token is
18717    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18718
18719 static void
18720 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18721 {
18722   /* Current level of '< ... >'.  */
18723   unsigned level = 0;
18724   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18725   unsigned nesting_depth = 0;
18726
18727   /* Are we ready, yet?  If not, issue error message.  */
18728   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18729     return;
18730
18731   /* Skip tokens until the desired token is found.  */
18732   while (true)
18733     {
18734       /* Peek at the next token.  */
18735       switch (cp_lexer_peek_token (parser->lexer)->type)
18736         {
18737         case CPP_LESS:
18738           if (!nesting_depth)
18739             ++level;
18740           break;
18741
18742         case CPP_RSHIFT:
18743           if (cxx_dialect == cxx98)
18744             /* C++0x views the `>>' operator as two `>' tokens, but
18745                C++98 does not. */
18746             break;
18747           else if (!nesting_depth && level-- == 0)
18748             {
18749               /* We've hit a `>>' where the first `>' closes the
18750                  template argument list, and the second `>' is
18751                  spurious.  Just consume the `>>' and stop; we've
18752                  already produced at least one error.  */
18753               cp_lexer_consume_token (parser->lexer);
18754               return;
18755             }
18756           /* Fall through for C++0x, so we handle the second `>' in
18757              the `>>'.  */
18758
18759         case CPP_GREATER:
18760           if (!nesting_depth && level-- == 0)
18761             {
18762               /* We've reached the token we want, consume it and stop.  */
18763               cp_lexer_consume_token (parser->lexer);
18764               return;
18765             }
18766           break;
18767
18768         case CPP_OPEN_PAREN:
18769         case CPP_OPEN_SQUARE:
18770           ++nesting_depth;
18771           break;
18772
18773         case CPP_CLOSE_PAREN:
18774         case CPP_CLOSE_SQUARE:
18775           if (nesting_depth-- == 0)
18776             return;
18777           break;
18778
18779         case CPP_EOF:
18780         case CPP_PRAGMA_EOL:
18781         case CPP_SEMICOLON:
18782         case CPP_OPEN_BRACE:
18783         case CPP_CLOSE_BRACE:
18784           /* The '>' was probably forgotten, don't look further.  */
18785           return;
18786
18787         default:
18788           break;
18789         }
18790
18791       /* Consume this token.  */
18792       cp_lexer_consume_token (parser->lexer);
18793     }
18794 }
18795
18796 /* If the next token is the indicated keyword, consume it.  Otherwise,
18797    issue an error message indicating that TOKEN_DESC was expected.
18798
18799    Returns the token consumed, if the token had the appropriate type.
18800    Otherwise, returns NULL.  */
18801
18802 static cp_token *
18803 cp_parser_require_keyword (cp_parser* parser,
18804                            enum rid keyword,
18805                            const char* token_desc)
18806 {
18807   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18808
18809   if (token && token->keyword != keyword)
18810     {
18811       dyn_string_t error_msg;
18812
18813       /* Format the error message.  */
18814       error_msg = dyn_string_new (0);
18815       dyn_string_append_cstr (error_msg, "expected ");
18816       dyn_string_append_cstr (error_msg, token_desc);
18817       cp_parser_error (parser, error_msg->s);
18818       dyn_string_delete (error_msg);
18819       return NULL;
18820     }
18821
18822   return token;
18823 }
18824
18825 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18826    function-definition.  */
18827
18828 static bool
18829 cp_parser_token_starts_function_definition_p (cp_token* token)
18830 {
18831   return (/* An ordinary function-body begins with an `{'.  */
18832           token->type == CPP_OPEN_BRACE
18833           /* A ctor-initializer begins with a `:'.  */
18834           || token->type == CPP_COLON
18835           /* A function-try-block begins with `try'.  */
18836           || token->keyword == RID_TRY
18837           /* The named return value extension begins with `return'.  */
18838           || token->keyword == RID_RETURN);
18839 }
18840
18841 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18842    definition.  */
18843
18844 static bool
18845 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18846 {
18847   cp_token *token;
18848
18849   token = cp_lexer_peek_token (parser->lexer);
18850   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18851 }
18852
18853 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18854    C++0x) ending a template-argument.  */
18855
18856 static bool
18857 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18858 {
18859   cp_token *token;
18860
18861   token = cp_lexer_peek_token (parser->lexer);
18862   return (token->type == CPP_COMMA 
18863           || token->type == CPP_GREATER
18864           || token->type == CPP_ELLIPSIS
18865           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18866 }
18867
18868 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18869    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18870
18871 static bool
18872 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18873                                                      size_t n)
18874 {
18875   cp_token *token;
18876
18877   token = cp_lexer_peek_nth_token (parser->lexer, n);
18878   if (token->type == CPP_LESS)
18879     return true;
18880   /* Check for the sequence `<::' in the original code. It would be lexed as
18881      `[:', where `[' is a digraph, and there is no whitespace before
18882      `:'.  */
18883   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18884     {
18885       cp_token *token2;
18886       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18887       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18888         return true;
18889     }
18890   return false;
18891 }
18892
18893 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18894    or none_type otherwise.  */
18895
18896 static enum tag_types
18897 cp_parser_token_is_class_key (cp_token* token)
18898 {
18899   switch (token->keyword)
18900     {
18901     case RID_CLASS:
18902       return class_type;
18903     case RID_STRUCT:
18904       return record_type;
18905     case RID_UNION:
18906       return union_type;
18907
18908     default:
18909       return none_type;
18910     }
18911 }
18912
18913 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18914
18915 static void
18916 cp_parser_check_class_key (enum tag_types class_key, tree type)
18917 {
18918   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18919     permerror (input_location, "%qs tag used in naming %q#T",
18920             class_key == union_type ? "union"
18921              : class_key == record_type ? "struct" : "class",
18922              type);
18923 }
18924
18925 /* Issue an error message if DECL is redeclared with different
18926    access than its original declaration [class.access.spec/3].
18927    This applies to nested classes and nested class templates.
18928    [class.mem/1].  */
18929
18930 static void
18931 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18932 {
18933   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18934     return;
18935
18936   if ((TREE_PRIVATE (decl)
18937        != (current_access_specifier == access_private_node))
18938       || (TREE_PROTECTED (decl)
18939           != (current_access_specifier == access_protected_node)))
18940     error ("%H%qD redeclared with different access", &location, decl);
18941 }
18942
18943 /* Look for the `template' keyword, as a syntactic disambiguator.
18944    Return TRUE iff it is present, in which case it will be
18945    consumed.  */
18946
18947 static bool
18948 cp_parser_optional_template_keyword (cp_parser *parser)
18949 {
18950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18951     {
18952       /* The `template' keyword can only be used within templates;
18953          outside templates the parser can always figure out what is a
18954          template and what is not.  */
18955       if (!processing_template_decl)
18956         {
18957           cp_token *token = cp_lexer_peek_token (parser->lexer);
18958           error ("%H%<template%> (as a disambiguator) is only allowed "
18959                  "within templates", &token->location);
18960           /* If this part of the token stream is rescanned, the same
18961              error message would be generated.  So, we purge the token
18962              from the stream.  */
18963           cp_lexer_purge_token (parser->lexer);
18964           return false;
18965         }
18966       else
18967         {
18968           /* Consume the `template' keyword.  */
18969           cp_lexer_consume_token (parser->lexer);
18970           return true;
18971         }
18972     }
18973
18974   return false;
18975 }
18976
18977 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18978    set PARSER->SCOPE, and perform other related actions.  */
18979
18980 static void
18981 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18982 {
18983   int i;
18984   struct tree_check *check_value;
18985   deferred_access_check *chk;
18986   VEC (deferred_access_check,gc) *checks;
18987
18988   /* Get the stored value.  */
18989   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18990   /* Perform any access checks that were deferred.  */
18991   checks = check_value->checks;
18992   if (checks)
18993     {
18994       for (i = 0 ;
18995            VEC_iterate (deferred_access_check, checks, i, chk) ;
18996            ++i)
18997         {
18998           perform_or_defer_access_check (chk->binfo,
18999                                          chk->decl,
19000                                          chk->diag_decl);
19001         }
19002     }
19003   /* Set the scope from the stored value.  */
19004   parser->scope = check_value->value;
19005   parser->qualifying_scope = check_value->qualifying_scope;
19006   parser->object_scope = NULL_TREE;
19007 }
19008
19009 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19010    encounter the end of a block before what we were looking for.  */
19011
19012 static bool
19013 cp_parser_cache_group (cp_parser *parser,
19014                        enum cpp_ttype end,
19015                        unsigned depth)
19016 {
19017   while (true)
19018     {
19019       cp_token *token = cp_lexer_peek_token (parser->lexer);
19020
19021       /* Abort a parenthesized expression if we encounter a semicolon.  */
19022       if ((end == CPP_CLOSE_PAREN || depth == 0)
19023           && token->type == CPP_SEMICOLON)
19024         return true;
19025       /* If we've reached the end of the file, stop.  */
19026       if (token->type == CPP_EOF
19027           || (end != CPP_PRAGMA_EOL
19028               && token->type == CPP_PRAGMA_EOL))
19029         return true;
19030       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19031         /* We've hit the end of an enclosing block, so there's been some
19032            kind of syntax error.  */
19033         return true;
19034
19035       /* Consume the token.  */
19036       cp_lexer_consume_token (parser->lexer);
19037       /* See if it starts a new group.  */
19038       if (token->type == CPP_OPEN_BRACE)
19039         {
19040           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19041           /* In theory this should probably check end == '}', but
19042              cp_parser_save_member_function_body needs it to exit
19043              after either '}' or ')' when called with ')'.  */
19044           if (depth == 0)
19045             return false;
19046         }
19047       else if (token->type == CPP_OPEN_PAREN)
19048         {
19049           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19050           if (depth == 0 && end == CPP_CLOSE_PAREN)
19051             return false;
19052         }
19053       else if (token->type == CPP_PRAGMA)
19054         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19055       else if (token->type == end)
19056         return false;
19057     }
19058 }
19059
19060 /* Begin parsing tentatively.  We always save tokens while parsing
19061    tentatively so that if the tentative parsing fails we can restore the
19062    tokens.  */
19063
19064 static void
19065 cp_parser_parse_tentatively (cp_parser* parser)
19066 {
19067   /* Enter a new parsing context.  */
19068   parser->context = cp_parser_context_new (parser->context);
19069   /* Begin saving tokens.  */
19070   cp_lexer_save_tokens (parser->lexer);
19071   /* In order to avoid repetitive access control error messages,
19072      access checks are queued up until we are no longer parsing
19073      tentatively.  */
19074   push_deferring_access_checks (dk_deferred);
19075 }
19076
19077 /* Commit to the currently active tentative parse.  */
19078
19079 static void
19080 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19081 {
19082   cp_parser_context *context;
19083   cp_lexer *lexer;
19084
19085   /* Mark all of the levels as committed.  */
19086   lexer = parser->lexer;
19087   for (context = parser->context; context->next; context = context->next)
19088     {
19089       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19090         break;
19091       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19092       while (!cp_lexer_saving_tokens (lexer))
19093         lexer = lexer->next;
19094       cp_lexer_commit_tokens (lexer);
19095     }
19096 }
19097
19098 /* Abort the currently active tentative parse.  All consumed tokens
19099    will be rolled back, and no diagnostics will be issued.  */
19100
19101 static void
19102 cp_parser_abort_tentative_parse (cp_parser* parser)
19103 {
19104   cp_parser_simulate_error (parser);
19105   /* Now, pretend that we want to see if the construct was
19106      successfully parsed.  */
19107   cp_parser_parse_definitely (parser);
19108 }
19109
19110 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19111    token stream.  Otherwise, commit to the tokens we have consumed.
19112    Returns true if no error occurred; false otherwise.  */
19113
19114 static bool
19115 cp_parser_parse_definitely (cp_parser* parser)
19116 {
19117   bool error_occurred;
19118   cp_parser_context *context;
19119
19120   /* Remember whether or not an error occurred, since we are about to
19121      destroy that information.  */
19122   error_occurred = cp_parser_error_occurred (parser);
19123   /* Remove the topmost context from the stack.  */
19124   context = parser->context;
19125   parser->context = context->next;
19126   /* If no parse errors occurred, commit to the tentative parse.  */
19127   if (!error_occurred)
19128     {
19129       /* Commit to the tokens read tentatively, unless that was
19130          already done.  */
19131       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19132         cp_lexer_commit_tokens (parser->lexer);
19133
19134       pop_to_parent_deferring_access_checks ();
19135     }
19136   /* Otherwise, if errors occurred, roll back our state so that things
19137      are just as they were before we began the tentative parse.  */
19138   else
19139     {
19140       cp_lexer_rollback_tokens (parser->lexer);
19141       pop_deferring_access_checks ();
19142     }
19143   /* Add the context to the front of the free list.  */
19144   context->next = cp_parser_context_free_list;
19145   cp_parser_context_free_list = context;
19146
19147   return !error_occurred;
19148 }
19149
19150 /* Returns true if we are parsing tentatively and are not committed to
19151    this tentative parse.  */
19152
19153 static bool
19154 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19155 {
19156   return (cp_parser_parsing_tentatively (parser)
19157           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19158 }
19159
19160 /* Returns nonzero iff an error has occurred during the most recent
19161    tentative parse.  */
19162
19163 static bool
19164 cp_parser_error_occurred (cp_parser* parser)
19165 {
19166   return (cp_parser_parsing_tentatively (parser)
19167           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19168 }
19169
19170 /* Returns nonzero if GNU extensions are allowed.  */
19171
19172 static bool
19173 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19174 {
19175   return parser->allow_gnu_extensions_p;
19176 }
19177 \f
19178 /* Objective-C++ Productions */
19179
19180
19181 /* Parse an Objective-C expression, which feeds into a primary-expression
19182    above.
19183
19184    objc-expression:
19185      objc-message-expression
19186      objc-string-literal
19187      objc-encode-expression
19188      objc-protocol-expression
19189      objc-selector-expression
19190
19191   Returns a tree representation of the expression.  */
19192
19193 static tree
19194 cp_parser_objc_expression (cp_parser* parser)
19195 {
19196   /* Try to figure out what kind of declaration is present.  */
19197   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19198
19199   switch (kwd->type)
19200     {
19201     case CPP_OPEN_SQUARE:
19202       return cp_parser_objc_message_expression (parser);
19203
19204     case CPP_OBJC_STRING:
19205       kwd = cp_lexer_consume_token (parser->lexer);
19206       return objc_build_string_object (kwd->u.value);
19207
19208     case CPP_KEYWORD:
19209       switch (kwd->keyword)
19210         {
19211         case RID_AT_ENCODE:
19212           return cp_parser_objc_encode_expression (parser);
19213
19214         case RID_AT_PROTOCOL:
19215           return cp_parser_objc_protocol_expression (parser);
19216
19217         case RID_AT_SELECTOR:
19218           return cp_parser_objc_selector_expression (parser);
19219
19220         default:
19221           break;
19222         }
19223     default:
19224       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19225              &kwd->location, kwd->u.value);
19226       cp_parser_skip_to_end_of_block_or_statement (parser);
19227     }
19228
19229   return error_mark_node;
19230 }
19231
19232 /* Parse an Objective-C message expression.
19233
19234    objc-message-expression:
19235      [ objc-message-receiver objc-message-args ]
19236
19237    Returns a representation of an Objective-C message.  */
19238
19239 static tree
19240 cp_parser_objc_message_expression (cp_parser* parser)
19241 {
19242   tree receiver, messageargs;
19243
19244   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19245   receiver = cp_parser_objc_message_receiver (parser);
19246   messageargs = cp_parser_objc_message_args (parser);
19247   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19248
19249   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19250 }
19251
19252 /* Parse an objc-message-receiver.
19253
19254    objc-message-receiver:
19255      expression
19256      simple-type-specifier
19257
19258   Returns a representation of the type or expression.  */
19259
19260 static tree
19261 cp_parser_objc_message_receiver (cp_parser* parser)
19262 {
19263   tree rcv;
19264
19265   /* An Objective-C message receiver may be either (1) a type
19266      or (2) an expression.  */
19267   cp_parser_parse_tentatively (parser);
19268   rcv = cp_parser_expression (parser, false, NULL);
19269
19270   if (cp_parser_parse_definitely (parser))
19271     return rcv;
19272
19273   rcv = cp_parser_simple_type_specifier (parser,
19274                                          /*decl_specs=*/NULL,
19275                                          CP_PARSER_FLAGS_NONE);
19276
19277   return objc_get_class_reference (rcv);
19278 }
19279
19280 /* Parse the arguments and selectors comprising an Objective-C message.
19281
19282    objc-message-args:
19283      objc-selector
19284      objc-selector-args
19285      objc-selector-args , objc-comma-args
19286
19287    objc-selector-args:
19288      objc-selector [opt] : assignment-expression
19289      objc-selector-args objc-selector [opt] : assignment-expression
19290
19291    objc-comma-args:
19292      assignment-expression
19293      objc-comma-args , assignment-expression
19294
19295    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19296    selector arguments and TREE_VALUE containing a list of comma
19297    arguments.  */
19298
19299 static tree
19300 cp_parser_objc_message_args (cp_parser* parser)
19301 {
19302   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19303   bool maybe_unary_selector_p = true;
19304   cp_token *token = cp_lexer_peek_token (parser->lexer);
19305
19306   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19307     {
19308       tree selector = NULL_TREE, arg;
19309
19310       if (token->type != CPP_COLON)
19311         selector = cp_parser_objc_selector (parser);
19312
19313       /* Detect if we have a unary selector.  */
19314       if (maybe_unary_selector_p
19315           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19316         return build_tree_list (selector, NULL_TREE);
19317
19318       maybe_unary_selector_p = false;
19319       cp_parser_require (parser, CPP_COLON, "%<:%>");
19320       arg = cp_parser_assignment_expression (parser, false, NULL);
19321
19322       sel_args
19323         = chainon (sel_args,
19324                    build_tree_list (selector, arg));
19325
19326       token = cp_lexer_peek_token (parser->lexer);
19327     }
19328
19329   /* Handle non-selector arguments, if any. */
19330   while (token->type == CPP_COMMA)
19331     {
19332       tree arg;
19333
19334       cp_lexer_consume_token (parser->lexer);
19335       arg = cp_parser_assignment_expression (parser, false, NULL);
19336
19337       addl_args
19338         = chainon (addl_args,
19339                    build_tree_list (NULL_TREE, arg));
19340
19341       token = cp_lexer_peek_token (parser->lexer);
19342     }
19343
19344   return build_tree_list (sel_args, addl_args);
19345 }
19346
19347 /* Parse an Objective-C encode expression.
19348
19349    objc-encode-expression:
19350      @encode objc-typename
19351
19352    Returns an encoded representation of the type argument.  */
19353
19354 static tree
19355 cp_parser_objc_encode_expression (cp_parser* parser)
19356 {
19357   tree type;
19358   cp_token *token;
19359
19360   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19361   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19362   token = cp_lexer_peek_token (parser->lexer);
19363   type = complete_type (cp_parser_type_id (parser));
19364   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19365
19366   if (!type)
19367     {
19368       error ("%H%<@encode%> must specify a type as an argument",
19369              &token->location);
19370       return error_mark_node;
19371     }
19372
19373   return objc_build_encode_expr (type);
19374 }
19375
19376 /* Parse an Objective-C @defs expression.  */
19377
19378 static tree
19379 cp_parser_objc_defs_expression (cp_parser *parser)
19380 {
19381   tree name;
19382
19383   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19384   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19385   name = cp_parser_identifier (parser);
19386   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19387
19388   return objc_get_class_ivars (name);
19389 }
19390
19391 /* Parse an Objective-C protocol expression.
19392
19393   objc-protocol-expression:
19394     @protocol ( identifier )
19395
19396   Returns a representation of the protocol expression.  */
19397
19398 static tree
19399 cp_parser_objc_protocol_expression (cp_parser* parser)
19400 {
19401   tree proto;
19402
19403   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19404   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19405   proto = cp_parser_identifier (parser);
19406   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19407
19408   return objc_build_protocol_expr (proto);
19409 }
19410
19411 /* Parse an Objective-C selector expression.
19412
19413    objc-selector-expression:
19414      @selector ( objc-method-signature )
19415
19416    objc-method-signature:
19417      objc-selector
19418      objc-selector-seq
19419
19420    objc-selector-seq:
19421      objc-selector :
19422      objc-selector-seq objc-selector :
19423
19424   Returns a representation of the method selector.  */
19425
19426 static tree
19427 cp_parser_objc_selector_expression (cp_parser* parser)
19428 {
19429   tree sel_seq = NULL_TREE;
19430   bool maybe_unary_selector_p = true;
19431   cp_token *token;
19432
19433   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19434   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19435   token = cp_lexer_peek_token (parser->lexer);
19436
19437   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19438          || token->type == CPP_SCOPE)
19439     {
19440       tree selector = NULL_TREE;
19441
19442       if (token->type != CPP_COLON
19443           || token->type == CPP_SCOPE)
19444         selector = cp_parser_objc_selector (parser);
19445
19446       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19447           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19448         {
19449           /* Detect if we have a unary selector.  */
19450           if (maybe_unary_selector_p)
19451             {
19452               sel_seq = selector;
19453               goto finish_selector;
19454             }
19455           else
19456             {
19457               cp_parser_error (parser, "expected %<:%>");
19458             }
19459         }
19460       maybe_unary_selector_p = false;
19461       token = cp_lexer_consume_token (parser->lexer);
19462
19463       if (token->type == CPP_SCOPE)
19464         {
19465           sel_seq
19466             = chainon (sel_seq,
19467                        build_tree_list (selector, NULL_TREE));
19468           sel_seq
19469             = chainon (sel_seq,
19470                        build_tree_list (NULL_TREE, NULL_TREE));
19471         }
19472       else
19473         sel_seq
19474           = chainon (sel_seq,
19475                      build_tree_list (selector, NULL_TREE));
19476
19477       token = cp_lexer_peek_token (parser->lexer);
19478     }
19479
19480  finish_selector:
19481   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19482
19483   return objc_build_selector_expr (sel_seq);
19484 }
19485
19486 /* Parse a list of identifiers.
19487
19488    objc-identifier-list:
19489      identifier
19490      objc-identifier-list , identifier
19491
19492    Returns a TREE_LIST of identifier nodes.  */
19493
19494 static tree
19495 cp_parser_objc_identifier_list (cp_parser* parser)
19496 {
19497   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19498   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19499
19500   while (sep->type == CPP_COMMA)
19501     {
19502       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19503       list = chainon (list,
19504                       build_tree_list (NULL_TREE,
19505                                        cp_parser_identifier (parser)));
19506       sep = cp_lexer_peek_token (parser->lexer);
19507     }
19508
19509   return list;
19510 }
19511
19512 /* Parse an Objective-C alias declaration.
19513
19514    objc-alias-declaration:
19515      @compatibility_alias identifier identifier ;
19516
19517    This function registers the alias mapping with the Objective-C front end.
19518    It returns nothing.  */
19519
19520 static void
19521 cp_parser_objc_alias_declaration (cp_parser* parser)
19522 {
19523   tree alias, orig;
19524
19525   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19526   alias = cp_parser_identifier (parser);
19527   orig = cp_parser_identifier (parser);
19528   objc_declare_alias (alias, orig);
19529   cp_parser_consume_semicolon_at_end_of_statement (parser);
19530 }
19531
19532 /* Parse an Objective-C class forward-declaration.
19533
19534    objc-class-declaration:
19535      @class objc-identifier-list ;
19536
19537    The function registers the forward declarations with the Objective-C
19538    front end.  It returns nothing.  */
19539
19540 static void
19541 cp_parser_objc_class_declaration (cp_parser* parser)
19542 {
19543   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19544   objc_declare_class (cp_parser_objc_identifier_list (parser));
19545   cp_parser_consume_semicolon_at_end_of_statement (parser);
19546 }
19547
19548 /* Parse a list of Objective-C protocol references.
19549
19550    objc-protocol-refs-opt:
19551      objc-protocol-refs [opt]
19552
19553    objc-protocol-refs:
19554      < objc-identifier-list >
19555
19556    Returns a TREE_LIST of identifiers, if any.  */
19557
19558 static tree
19559 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19560 {
19561   tree protorefs = NULL_TREE;
19562
19563   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19564     {
19565       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19566       protorefs = cp_parser_objc_identifier_list (parser);
19567       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19568     }
19569
19570   return protorefs;
19571 }
19572
19573 /* Parse a Objective-C visibility specification.  */
19574
19575 static void
19576 cp_parser_objc_visibility_spec (cp_parser* parser)
19577 {
19578   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19579
19580   switch (vis->keyword)
19581     {
19582     case RID_AT_PRIVATE:
19583       objc_set_visibility (2);
19584       break;
19585     case RID_AT_PROTECTED:
19586       objc_set_visibility (0);
19587       break;
19588     case RID_AT_PUBLIC:
19589       objc_set_visibility (1);
19590       break;
19591     default:
19592       return;
19593     }
19594
19595   /* Eat '@private'/'@protected'/'@public'.  */
19596   cp_lexer_consume_token (parser->lexer);
19597 }
19598
19599 /* Parse an Objective-C method type.  */
19600
19601 static void
19602 cp_parser_objc_method_type (cp_parser* parser)
19603 {
19604   objc_set_method_type
19605    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19606     ? PLUS_EXPR
19607     : MINUS_EXPR);
19608 }
19609
19610 /* Parse an Objective-C protocol qualifier.  */
19611
19612 static tree
19613 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19614 {
19615   tree quals = NULL_TREE, node;
19616   cp_token *token = cp_lexer_peek_token (parser->lexer);
19617
19618   node = token->u.value;
19619
19620   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19621          && (node == ridpointers [(int) RID_IN]
19622              || node == ridpointers [(int) RID_OUT]
19623              || node == ridpointers [(int) RID_INOUT]
19624              || node == ridpointers [(int) RID_BYCOPY]
19625              || node == ridpointers [(int) RID_BYREF]
19626              || node == ridpointers [(int) RID_ONEWAY]))
19627     {
19628       quals = tree_cons (NULL_TREE, node, quals);
19629       cp_lexer_consume_token (parser->lexer);
19630       token = cp_lexer_peek_token (parser->lexer);
19631       node = token->u.value;
19632     }
19633
19634   return quals;
19635 }
19636
19637 /* Parse an Objective-C typename.  */
19638
19639 static tree
19640 cp_parser_objc_typename (cp_parser* parser)
19641 {
19642   tree type_name = NULL_TREE;
19643
19644   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19645     {
19646       tree proto_quals, cp_type = NULL_TREE;
19647
19648       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19649       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19650
19651       /* An ObjC type name may consist of just protocol qualifiers, in which
19652          case the type shall default to 'id'.  */
19653       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19654         cp_type = cp_parser_type_id (parser);
19655
19656       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19657       type_name = build_tree_list (proto_quals, cp_type);
19658     }
19659
19660   return type_name;
19661 }
19662
19663 /* Check to see if TYPE refers to an Objective-C selector name.  */
19664
19665 static bool
19666 cp_parser_objc_selector_p (enum cpp_ttype type)
19667 {
19668   return (type == CPP_NAME || type == CPP_KEYWORD
19669           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19670           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19671           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19672           || type == CPP_XOR || type == CPP_XOR_EQ);
19673 }
19674
19675 /* Parse an Objective-C selector.  */
19676
19677 static tree
19678 cp_parser_objc_selector (cp_parser* parser)
19679 {
19680   cp_token *token = cp_lexer_consume_token (parser->lexer);
19681
19682   if (!cp_parser_objc_selector_p (token->type))
19683     {
19684       error ("%Hinvalid Objective-C++ selector name", &token->location);
19685       return error_mark_node;
19686     }
19687
19688   /* C++ operator names are allowed to appear in ObjC selectors.  */
19689   switch (token->type)
19690     {
19691     case CPP_AND_AND: return get_identifier ("and");
19692     case CPP_AND_EQ: return get_identifier ("and_eq");
19693     case CPP_AND: return get_identifier ("bitand");
19694     case CPP_OR: return get_identifier ("bitor");
19695     case CPP_COMPL: return get_identifier ("compl");
19696     case CPP_NOT: return get_identifier ("not");
19697     case CPP_NOT_EQ: return get_identifier ("not_eq");
19698     case CPP_OR_OR: return get_identifier ("or");
19699     case CPP_OR_EQ: return get_identifier ("or_eq");
19700     case CPP_XOR: return get_identifier ("xor");
19701     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19702     default: return token->u.value;
19703     }
19704 }
19705
19706 /* Parse an Objective-C params list.  */
19707
19708 static tree
19709 cp_parser_objc_method_keyword_params (cp_parser* parser)
19710 {
19711   tree params = NULL_TREE;
19712   bool maybe_unary_selector_p = true;
19713   cp_token *token = cp_lexer_peek_token (parser->lexer);
19714
19715   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19716     {
19717       tree selector = NULL_TREE, type_name, identifier;
19718
19719       if (token->type != CPP_COLON)
19720         selector = cp_parser_objc_selector (parser);
19721
19722       /* Detect if we have a unary selector.  */
19723       if (maybe_unary_selector_p
19724           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19725         return selector;
19726
19727       maybe_unary_selector_p = false;
19728       cp_parser_require (parser, CPP_COLON, "%<:%>");
19729       type_name = cp_parser_objc_typename (parser);
19730       identifier = cp_parser_identifier (parser);
19731
19732       params
19733         = chainon (params,
19734                    objc_build_keyword_decl (selector,
19735                                             type_name,
19736                                             identifier));
19737
19738       token = cp_lexer_peek_token (parser->lexer);
19739     }
19740
19741   return params;
19742 }
19743
19744 /* Parse the non-keyword Objective-C params.  */
19745
19746 static tree
19747 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19748 {
19749   tree params = make_node (TREE_LIST);
19750   cp_token *token = cp_lexer_peek_token (parser->lexer);
19751   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19752
19753   while (token->type == CPP_COMMA)
19754     {
19755       cp_parameter_declarator *parmdecl;
19756       tree parm;
19757
19758       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19759       token = cp_lexer_peek_token (parser->lexer);
19760
19761       if (token->type == CPP_ELLIPSIS)
19762         {
19763           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19764           *ellipsisp = true;
19765           break;
19766         }
19767
19768       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19769       parm = grokdeclarator (parmdecl->declarator,
19770                              &parmdecl->decl_specifiers,
19771                              PARM, /*initialized=*/0,
19772                              /*attrlist=*/NULL);
19773
19774       chainon (params, build_tree_list (NULL_TREE, parm));
19775       token = cp_lexer_peek_token (parser->lexer);
19776     }
19777
19778   return params;
19779 }
19780
19781 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19782
19783 static void
19784 cp_parser_objc_interstitial_code (cp_parser* parser)
19785 {
19786   cp_token *token = cp_lexer_peek_token (parser->lexer);
19787
19788   /* If the next token is `extern' and the following token is a string
19789      literal, then we have a linkage specification.  */
19790   if (token->keyword == RID_EXTERN
19791       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19792     cp_parser_linkage_specification (parser);
19793   /* Handle #pragma, if any.  */
19794   else if (token->type == CPP_PRAGMA)
19795     cp_parser_pragma (parser, pragma_external);
19796   /* Allow stray semicolons.  */
19797   else if (token->type == CPP_SEMICOLON)
19798     cp_lexer_consume_token (parser->lexer);
19799   /* Finally, try to parse a block-declaration, or a function-definition.  */
19800   else
19801     cp_parser_block_declaration (parser, /*statement_p=*/false);
19802 }
19803
19804 /* Parse a method signature.  */
19805
19806 static tree
19807 cp_parser_objc_method_signature (cp_parser* parser)
19808 {
19809   tree rettype, kwdparms, optparms;
19810   bool ellipsis = false;
19811
19812   cp_parser_objc_method_type (parser);
19813   rettype = cp_parser_objc_typename (parser);
19814   kwdparms = cp_parser_objc_method_keyword_params (parser);
19815   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19816
19817   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19818 }
19819
19820 /* Pars an Objective-C method prototype list.  */
19821
19822 static void
19823 cp_parser_objc_method_prototype_list (cp_parser* parser)
19824 {
19825   cp_token *token = cp_lexer_peek_token (parser->lexer);
19826
19827   while (token->keyword != RID_AT_END)
19828     {
19829       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19830         {
19831           objc_add_method_declaration
19832            (cp_parser_objc_method_signature (parser));
19833           cp_parser_consume_semicolon_at_end_of_statement (parser);
19834         }
19835       else
19836         /* Allow for interspersed non-ObjC++ code.  */
19837         cp_parser_objc_interstitial_code (parser);
19838
19839       token = cp_lexer_peek_token (parser->lexer);
19840     }
19841
19842   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19843   objc_finish_interface ();
19844 }
19845
19846 /* Parse an Objective-C method definition list.  */
19847
19848 static void
19849 cp_parser_objc_method_definition_list (cp_parser* parser)
19850 {
19851   cp_token *token = cp_lexer_peek_token (parser->lexer);
19852
19853   while (token->keyword != RID_AT_END)
19854     {
19855       tree meth;
19856
19857       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19858         {
19859           push_deferring_access_checks (dk_deferred);
19860           objc_start_method_definition
19861            (cp_parser_objc_method_signature (parser));
19862
19863           /* For historical reasons, we accept an optional semicolon.  */
19864           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19865             cp_lexer_consume_token (parser->lexer);
19866
19867           perform_deferred_access_checks ();
19868           stop_deferring_access_checks ();
19869           meth = cp_parser_function_definition_after_declarator (parser,
19870                                                                  false);
19871           pop_deferring_access_checks ();
19872           objc_finish_method_definition (meth);
19873         }
19874       else
19875         /* Allow for interspersed non-ObjC++ code.  */
19876         cp_parser_objc_interstitial_code (parser);
19877
19878       token = cp_lexer_peek_token (parser->lexer);
19879     }
19880
19881   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19882   objc_finish_implementation ();
19883 }
19884
19885 /* Parse Objective-C ivars.  */
19886
19887 static void
19888 cp_parser_objc_class_ivars (cp_parser* parser)
19889 {
19890   cp_token *token = cp_lexer_peek_token (parser->lexer);
19891
19892   if (token->type != CPP_OPEN_BRACE)
19893     return;     /* No ivars specified.  */
19894
19895   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19896   token = cp_lexer_peek_token (parser->lexer);
19897
19898   while (token->type != CPP_CLOSE_BRACE)
19899     {
19900       cp_decl_specifier_seq declspecs;
19901       int decl_class_or_enum_p;
19902       tree prefix_attributes;
19903
19904       cp_parser_objc_visibility_spec (parser);
19905
19906       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19907         break;
19908
19909       cp_parser_decl_specifier_seq (parser,
19910                                     CP_PARSER_FLAGS_OPTIONAL,
19911                                     &declspecs,
19912                                     &decl_class_or_enum_p);
19913       prefix_attributes = declspecs.attributes;
19914       declspecs.attributes = NULL_TREE;
19915
19916       /* Keep going until we hit the `;' at the end of the
19917          declaration.  */
19918       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19919         {
19920           tree width = NULL_TREE, attributes, first_attribute, decl;
19921           cp_declarator *declarator = NULL;
19922           int ctor_dtor_or_conv_p;
19923
19924           /* Check for a (possibly unnamed) bitfield declaration.  */
19925           token = cp_lexer_peek_token (parser->lexer);
19926           if (token->type == CPP_COLON)
19927             goto eat_colon;
19928
19929           if (token->type == CPP_NAME
19930               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19931                   == CPP_COLON))
19932             {
19933               /* Get the name of the bitfield.  */
19934               declarator = make_id_declarator (NULL_TREE,
19935                                                cp_parser_identifier (parser),
19936                                                sfk_none);
19937
19938              eat_colon:
19939               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19940               /* Get the width of the bitfield.  */
19941               width
19942                 = cp_parser_constant_expression (parser,
19943                                                  /*allow_non_constant=*/false,
19944                                                  NULL);
19945             }
19946           else
19947             {
19948               /* Parse the declarator.  */
19949               declarator
19950                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19951                                         &ctor_dtor_or_conv_p,
19952                                         /*parenthesized_p=*/NULL,
19953                                         /*member_p=*/false);
19954             }
19955
19956           /* Look for attributes that apply to the ivar.  */
19957           attributes = cp_parser_attributes_opt (parser);
19958           /* Remember which attributes are prefix attributes and
19959              which are not.  */
19960           first_attribute = attributes;
19961           /* Combine the attributes.  */
19962           attributes = chainon (prefix_attributes, attributes);
19963
19964           if (width)
19965               /* Create the bitfield declaration.  */
19966               decl = grokbitfield (declarator, &declspecs,
19967                                    width,
19968                                    attributes);
19969           else
19970             decl = grokfield (declarator, &declspecs,
19971                               NULL_TREE, /*init_const_expr_p=*/false,
19972                               NULL_TREE, attributes);
19973
19974           /* Add the instance variable.  */
19975           objc_add_instance_variable (decl);
19976
19977           /* Reset PREFIX_ATTRIBUTES.  */
19978           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19979             attributes = TREE_CHAIN (attributes);
19980           if (attributes)
19981             TREE_CHAIN (attributes) = NULL_TREE;
19982
19983           token = cp_lexer_peek_token (parser->lexer);
19984
19985           if (token->type == CPP_COMMA)
19986             {
19987               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19988               continue;
19989             }
19990           break;
19991         }
19992
19993       cp_parser_consume_semicolon_at_end_of_statement (parser);
19994       token = cp_lexer_peek_token (parser->lexer);
19995     }
19996
19997   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19998   /* For historical reasons, we accept an optional semicolon.  */
19999   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20000     cp_lexer_consume_token (parser->lexer);
20001 }
20002
20003 /* Parse an Objective-C protocol declaration.  */
20004
20005 static void
20006 cp_parser_objc_protocol_declaration (cp_parser* parser)
20007 {
20008   tree proto, protorefs;
20009   cp_token *tok;
20010
20011   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20012   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20013     {
20014       tok = cp_lexer_peek_token (parser->lexer);
20015       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20016       goto finish;
20017     }
20018
20019   /* See if we have a forward declaration or a definition.  */
20020   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20021
20022   /* Try a forward declaration first.  */
20023   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20024     {
20025       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20026      finish:
20027       cp_parser_consume_semicolon_at_end_of_statement (parser);
20028     }
20029
20030   /* Ok, we got a full-fledged definition (or at least should).  */
20031   else
20032     {
20033       proto = cp_parser_identifier (parser);
20034       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20035       objc_start_protocol (proto, protorefs);
20036       cp_parser_objc_method_prototype_list (parser);
20037     }
20038 }
20039
20040 /* Parse an Objective-C superclass or category.  */
20041
20042 static void
20043 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20044                                                           tree *categ)
20045 {
20046   cp_token *next = cp_lexer_peek_token (parser->lexer);
20047
20048   *super = *categ = NULL_TREE;
20049   if (next->type == CPP_COLON)
20050     {
20051       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20052       *super = cp_parser_identifier (parser);
20053     }
20054   else if (next->type == CPP_OPEN_PAREN)
20055     {
20056       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20057       *categ = cp_parser_identifier (parser);
20058       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20059     }
20060 }
20061
20062 /* Parse an Objective-C class interface.  */
20063
20064 static void
20065 cp_parser_objc_class_interface (cp_parser* parser)
20066 {
20067   tree name, super, categ, protos;
20068
20069   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20070   name = cp_parser_identifier (parser);
20071   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20072   protos = cp_parser_objc_protocol_refs_opt (parser);
20073
20074   /* We have either a class or a category on our hands.  */
20075   if (categ)
20076     objc_start_category_interface (name, categ, protos);
20077   else
20078     {
20079       objc_start_class_interface (name, super, protos);
20080       /* Handle instance variable declarations, if any.  */
20081       cp_parser_objc_class_ivars (parser);
20082       objc_continue_interface ();
20083     }
20084
20085   cp_parser_objc_method_prototype_list (parser);
20086 }
20087
20088 /* Parse an Objective-C class implementation.  */
20089
20090 static void
20091 cp_parser_objc_class_implementation (cp_parser* parser)
20092 {
20093   tree name, super, categ;
20094
20095   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20096   name = cp_parser_identifier (parser);
20097   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20098
20099   /* We have either a class or a category on our hands.  */
20100   if (categ)
20101     objc_start_category_implementation (name, categ);
20102   else
20103     {
20104       objc_start_class_implementation (name, super);
20105       /* Handle instance variable declarations, if any.  */
20106       cp_parser_objc_class_ivars (parser);
20107       objc_continue_implementation ();
20108     }
20109
20110   cp_parser_objc_method_definition_list (parser);
20111 }
20112
20113 /* Consume the @end token and finish off the implementation.  */
20114
20115 static void
20116 cp_parser_objc_end_implementation (cp_parser* parser)
20117 {
20118   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20119   objc_finish_implementation ();
20120 }
20121
20122 /* Parse an Objective-C declaration.  */
20123
20124 static void
20125 cp_parser_objc_declaration (cp_parser* parser)
20126 {
20127   /* Try to figure out what kind of declaration is present.  */
20128   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20129
20130   switch (kwd->keyword)
20131     {
20132     case RID_AT_ALIAS:
20133       cp_parser_objc_alias_declaration (parser);
20134       break;
20135     case RID_AT_CLASS:
20136       cp_parser_objc_class_declaration (parser);
20137       break;
20138     case RID_AT_PROTOCOL:
20139       cp_parser_objc_protocol_declaration (parser);
20140       break;
20141     case RID_AT_INTERFACE:
20142       cp_parser_objc_class_interface (parser);
20143       break;
20144     case RID_AT_IMPLEMENTATION:
20145       cp_parser_objc_class_implementation (parser);
20146       break;
20147     case RID_AT_END:
20148       cp_parser_objc_end_implementation (parser);
20149       break;
20150     default:
20151       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20152              &kwd->location, kwd->u.value);
20153       cp_parser_skip_to_end_of_block_or_statement (parser);
20154     }
20155 }
20156
20157 /* Parse an Objective-C try-catch-finally statement.
20158
20159    objc-try-catch-finally-stmt:
20160      @try compound-statement objc-catch-clause-seq [opt]
20161        objc-finally-clause [opt]
20162
20163    objc-catch-clause-seq:
20164      objc-catch-clause objc-catch-clause-seq [opt]
20165
20166    objc-catch-clause:
20167      @catch ( exception-declaration ) compound-statement
20168
20169    objc-finally-clause
20170      @finally compound-statement
20171
20172    Returns NULL_TREE.  */
20173
20174 static tree
20175 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20176   location_t location;
20177   tree stmt;
20178
20179   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20180   location = cp_lexer_peek_token (parser->lexer)->location;
20181   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20182      node, lest it get absorbed into the surrounding block.  */
20183   stmt = push_stmt_list ();
20184   cp_parser_compound_statement (parser, NULL, false);
20185   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20186
20187   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20188     {
20189       cp_parameter_declarator *parmdecl;
20190       tree parm;
20191
20192       cp_lexer_consume_token (parser->lexer);
20193       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20194       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20195       parm = grokdeclarator (parmdecl->declarator,
20196                              &parmdecl->decl_specifiers,
20197                              PARM, /*initialized=*/0,
20198                              /*attrlist=*/NULL);
20199       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20200       objc_begin_catch_clause (parm);
20201       cp_parser_compound_statement (parser, NULL, false);
20202       objc_finish_catch_clause ();
20203     }
20204
20205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20206     {
20207       cp_lexer_consume_token (parser->lexer);
20208       location = cp_lexer_peek_token (parser->lexer)->location;
20209       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20210          node, lest it get absorbed into the surrounding block.  */
20211       stmt = push_stmt_list ();
20212       cp_parser_compound_statement (parser, NULL, false);
20213       objc_build_finally_clause (location, pop_stmt_list (stmt));
20214     }
20215
20216   return objc_finish_try_stmt ();
20217 }
20218
20219 /* Parse an Objective-C synchronized statement.
20220
20221    objc-synchronized-stmt:
20222      @synchronized ( expression ) compound-statement
20223
20224    Returns NULL_TREE.  */
20225
20226 static tree
20227 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20228   location_t location;
20229   tree lock, stmt;
20230
20231   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20232
20233   location = cp_lexer_peek_token (parser->lexer)->location;
20234   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20235   lock = cp_parser_expression (parser, false, NULL);
20236   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20237
20238   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20239      node, lest it get absorbed into the surrounding block.  */
20240   stmt = push_stmt_list ();
20241   cp_parser_compound_statement (parser, NULL, false);
20242
20243   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20244 }
20245
20246 /* Parse an Objective-C throw statement.
20247
20248    objc-throw-stmt:
20249      @throw assignment-expression [opt] ;
20250
20251    Returns a constructed '@throw' statement.  */
20252
20253 static tree
20254 cp_parser_objc_throw_statement (cp_parser *parser) {
20255   tree expr = NULL_TREE;
20256
20257   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20258
20259   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20260     expr = cp_parser_assignment_expression (parser, false, NULL);
20261
20262   cp_parser_consume_semicolon_at_end_of_statement (parser);
20263
20264   return objc_build_throw_stmt (expr);
20265 }
20266
20267 /* Parse an Objective-C statement.  */
20268
20269 static tree
20270 cp_parser_objc_statement (cp_parser * parser) {
20271   /* Try to figure out what kind of declaration is present.  */
20272   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20273
20274   switch (kwd->keyword)
20275     {
20276     case RID_AT_TRY:
20277       return cp_parser_objc_try_catch_finally_statement (parser);
20278     case RID_AT_SYNCHRONIZED:
20279       return cp_parser_objc_synchronized_statement (parser);
20280     case RID_AT_THROW:
20281       return cp_parser_objc_throw_statement (parser);
20282     default:
20283       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20284              &kwd->location, kwd->u.value);
20285       cp_parser_skip_to_end_of_block_or_statement (parser);
20286     }
20287
20288   return error_mark_node;
20289 }
20290 \f
20291 /* OpenMP 2.5 parsing routines.  */
20292
20293 /* Returns name of the next clause.
20294    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20295    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20296    returned and the token is consumed.  */
20297
20298 static pragma_omp_clause
20299 cp_parser_omp_clause_name (cp_parser *parser)
20300 {
20301   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20302
20303   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20304     result = PRAGMA_OMP_CLAUSE_IF;
20305   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20306     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20307   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20308     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20309   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20310     {
20311       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20312       const char *p = IDENTIFIER_POINTER (id);
20313
20314       switch (p[0])
20315         {
20316         case 'c':
20317           if (!strcmp ("collapse", p))
20318             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20319           else if (!strcmp ("copyin", p))
20320             result = PRAGMA_OMP_CLAUSE_COPYIN;
20321           else if (!strcmp ("copyprivate", p))
20322             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20323           break;
20324         case 'f':
20325           if (!strcmp ("firstprivate", p))
20326             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20327           break;
20328         case 'l':
20329           if (!strcmp ("lastprivate", p))
20330             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20331           break;
20332         case 'n':
20333           if (!strcmp ("nowait", p))
20334             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20335           else if (!strcmp ("num_threads", p))
20336             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20337           break;
20338         case 'o':
20339           if (!strcmp ("ordered", p))
20340             result = PRAGMA_OMP_CLAUSE_ORDERED;
20341           break;
20342         case 'r':
20343           if (!strcmp ("reduction", p))
20344             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20345           break;
20346         case 's':
20347           if (!strcmp ("schedule", p))
20348             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20349           else if (!strcmp ("shared", p))
20350             result = PRAGMA_OMP_CLAUSE_SHARED;
20351           break;
20352         case 'u':
20353           if (!strcmp ("untied", p))
20354             result = PRAGMA_OMP_CLAUSE_UNTIED;
20355           break;
20356         }
20357     }
20358
20359   if (result != PRAGMA_OMP_CLAUSE_NONE)
20360     cp_lexer_consume_token (parser->lexer);
20361
20362   return result;
20363 }
20364
20365 /* Validate that a clause of the given type does not already exist.  */
20366
20367 static void
20368 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20369                            const char *name, location_t location)
20370 {
20371   tree c;
20372
20373   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20374     if (OMP_CLAUSE_CODE (c) == code)
20375       {
20376         error ("%Htoo many %qs clauses", &location, name);
20377         break;
20378       }
20379 }
20380
20381 /* OpenMP 2.5:
20382    variable-list:
20383      identifier
20384      variable-list , identifier
20385
20386    In addition, we match a closing parenthesis.  An opening parenthesis
20387    will have been consumed by the caller.
20388
20389    If KIND is nonzero, create the appropriate node and install the decl
20390    in OMP_CLAUSE_DECL and add the node to the head of the list.
20391
20392    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20393    return the list created.  */
20394
20395 static tree
20396 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20397                                 tree list)
20398 {
20399   cp_token *token;
20400   while (1)
20401     {
20402       tree name, decl;
20403
20404       token = cp_lexer_peek_token (parser->lexer);
20405       name = cp_parser_id_expression (parser, /*template_p=*/false,
20406                                       /*check_dependency_p=*/true,
20407                                       /*template_p=*/NULL,
20408                                       /*declarator_p=*/false,
20409                                       /*optional_p=*/false);
20410       if (name == error_mark_node)
20411         goto skip_comma;
20412
20413       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20414       if (decl == error_mark_node)
20415         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20416       else if (kind != 0)
20417         {
20418           tree u = build_omp_clause (kind);
20419           OMP_CLAUSE_DECL (u) = decl;
20420           OMP_CLAUSE_CHAIN (u) = list;
20421           list = u;
20422         }
20423       else
20424         list = tree_cons (decl, NULL_TREE, list);
20425
20426     get_comma:
20427       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20428         break;
20429       cp_lexer_consume_token (parser->lexer);
20430     }
20431
20432   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20433     {
20434       int ending;
20435
20436       /* Try to resync to an unnested comma.  Copied from
20437          cp_parser_parenthesized_expression_list.  */
20438     skip_comma:
20439       ending = cp_parser_skip_to_closing_parenthesis (parser,
20440                                                       /*recovering=*/true,
20441                                                       /*or_comma=*/true,
20442                                                       /*consume_paren=*/true);
20443       if (ending < 0)
20444         goto get_comma;
20445     }
20446
20447   return list;
20448 }
20449
20450 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20451    common case for omp clauses.  */
20452
20453 static tree
20454 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20455 {
20456   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20457     return cp_parser_omp_var_list_no_open (parser, kind, list);
20458   return list;
20459 }
20460
20461 /* OpenMP 3.0:
20462    collapse ( constant-expression ) */
20463
20464 static tree
20465 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20466 {
20467   tree c, num;
20468   location_t loc;
20469   HOST_WIDE_INT n;
20470
20471   loc = cp_lexer_peek_token (parser->lexer)->location;
20472   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20473     return list;
20474
20475   num = cp_parser_constant_expression (parser, false, NULL);
20476
20477   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20478     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20479                                            /*or_comma=*/false,
20480                                            /*consume_paren=*/true);
20481
20482   if (num == error_mark_node)
20483     return list;
20484   num = fold_non_dependent_expr (num);
20485   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20486       || !host_integerp (num, 0)
20487       || (n = tree_low_cst (num, 0)) <= 0
20488       || (int) n != n)
20489     {
20490       error ("%Hcollapse argument needs positive constant integer expression",
20491              &loc);
20492       return list;
20493     }
20494
20495   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20496   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20497   OMP_CLAUSE_CHAIN (c) = list;
20498   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20499
20500   return c;
20501 }
20502
20503 /* OpenMP 2.5:
20504    default ( shared | none ) */
20505
20506 static tree
20507 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20508 {
20509   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20510   tree c;
20511
20512   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20513     return list;
20514   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20515     {
20516       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20517       const char *p = IDENTIFIER_POINTER (id);
20518
20519       switch (p[0])
20520         {
20521         case 'n':
20522           if (strcmp ("none", p) != 0)
20523             goto invalid_kind;
20524           kind = OMP_CLAUSE_DEFAULT_NONE;
20525           break;
20526
20527         case 's':
20528           if (strcmp ("shared", p) != 0)
20529             goto invalid_kind;
20530           kind = OMP_CLAUSE_DEFAULT_SHARED;
20531           break;
20532
20533         default:
20534           goto invalid_kind;
20535         }
20536
20537       cp_lexer_consume_token (parser->lexer);
20538     }
20539   else
20540     {
20541     invalid_kind:
20542       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20543     }
20544
20545   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20546     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20547                                            /*or_comma=*/false,
20548                                            /*consume_paren=*/true);
20549
20550   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20551     return list;
20552
20553   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20554   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20555   OMP_CLAUSE_CHAIN (c) = list;
20556   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20557
20558   return c;
20559 }
20560
20561 /* OpenMP 2.5:
20562    if ( expression ) */
20563
20564 static tree
20565 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20566 {
20567   tree t, c;
20568
20569   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20570     return list;
20571
20572   t = cp_parser_condition (parser);
20573
20574   if (t == error_mark_node
20575       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20576     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20577                                            /*or_comma=*/false,
20578                                            /*consume_paren=*/true);
20579
20580   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20581
20582   c = build_omp_clause (OMP_CLAUSE_IF);
20583   OMP_CLAUSE_IF_EXPR (c) = t;
20584   OMP_CLAUSE_CHAIN (c) = list;
20585
20586   return c;
20587 }
20588
20589 /* OpenMP 2.5:
20590    nowait */
20591
20592 static tree
20593 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20594                              tree list, location_t location)
20595 {
20596   tree c;
20597
20598   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20599
20600   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20601   OMP_CLAUSE_CHAIN (c) = list;
20602   return c;
20603 }
20604
20605 /* OpenMP 2.5:
20606    num_threads ( expression ) */
20607
20608 static tree
20609 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20610                                   location_t location)
20611 {
20612   tree t, c;
20613
20614   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20615     return list;
20616
20617   t = cp_parser_expression (parser, false, NULL);
20618
20619   if (t == error_mark_node
20620       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20621     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20622                                            /*or_comma=*/false,
20623                                            /*consume_paren=*/true);
20624
20625   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20626                              "num_threads", location);
20627
20628   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20629   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20630   OMP_CLAUSE_CHAIN (c) = list;
20631
20632   return c;
20633 }
20634
20635 /* OpenMP 2.5:
20636    ordered */
20637
20638 static tree
20639 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20640                               tree list, location_t location)
20641 {
20642   tree c;
20643
20644   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20645                              "ordered", location);
20646
20647   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20648   OMP_CLAUSE_CHAIN (c) = list;
20649   return c;
20650 }
20651
20652 /* OpenMP 2.5:
20653    reduction ( reduction-operator : variable-list )
20654
20655    reduction-operator:
20656      One of: + * - & ^ | && || */
20657
20658 static tree
20659 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20660 {
20661   enum tree_code code;
20662   tree nlist, c;
20663
20664   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20665     return list;
20666
20667   switch (cp_lexer_peek_token (parser->lexer)->type)
20668     {
20669     case CPP_PLUS:
20670       code = PLUS_EXPR;
20671       break;
20672     case CPP_MULT:
20673       code = MULT_EXPR;
20674       break;
20675     case CPP_MINUS:
20676       code = MINUS_EXPR;
20677       break;
20678     case CPP_AND:
20679       code = BIT_AND_EXPR;
20680       break;
20681     case CPP_XOR:
20682       code = BIT_XOR_EXPR;
20683       break;
20684     case CPP_OR:
20685       code = BIT_IOR_EXPR;
20686       break;
20687     case CPP_AND_AND:
20688       code = TRUTH_ANDIF_EXPR;
20689       break;
20690     case CPP_OR_OR:
20691       code = TRUTH_ORIF_EXPR;
20692       break;
20693     default:
20694       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20695                                "%<|%>, %<&&%>, or %<||%>");
20696     resync_fail:
20697       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20698                                              /*or_comma=*/false,
20699                                              /*consume_paren=*/true);
20700       return list;
20701     }
20702   cp_lexer_consume_token (parser->lexer);
20703
20704   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20705     goto resync_fail;
20706
20707   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20708   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20709     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20710
20711   return nlist;
20712 }
20713
20714 /* OpenMP 2.5:
20715    schedule ( schedule-kind )
20716    schedule ( schedule-kind , expression )
20717
20718    schedule-kind:
20719      static | dynamic | guided | runtime | auto  */
20720
20721 static tree
20722 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20723 {
20724   tree c, t;
20725
20726   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20727     return list;
20728
20729   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20730
20731   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20732     {
20733       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20734       const char *p = IDENTIFIER_POINTER (id);
20735
20736       switch (p[0])
20737         {
20738         case 'd':
20739           if (strcmp ("dynamic", p) != 0)
20740             goto invalid_kind;
20741           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20742           break;
20743
20744         case 'g':
20745           if (strcmp ("guided", p) != 0)
20746             goto invalid_kind;
20747           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20748           break;
20749
20750         case 'r':
20751           if (strcmp ("runtime", p) != 0)
20752             goto invalid_kind;
20753           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20754           break;
20755
20756         default:
20757           goto invalid_kind;
20758         }
20759     }
20760   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20761     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20762   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20763     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20764   else
20765     goto invalid_kind;
20766   cp_lexer_consume_token (parser->lexer);
20767
20768   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20769     {
20770       cp_token *token;
20771       cp_lexer_consume_token (parser->lexer);
20772
20773       token = cp_lexer_peek_token (parser->lexer);
20774       t = cp_parser_assignment_expression (parser, false, NULL);
20775
20776       if (t == error_mark_node)
20777         goto resync_fail;
20778       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20779         error ("%Hschedule %<runtime%> does not take "
20780                "a %<chunk_size%> parameter", &token->location);
20781       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20782         error ("%Hschedule %<auto%> does not take "
20783                "a %<chunk_size%> parameter", &token->location);
20784       else
20785         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20786
20787       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20788         goto resync_fail;
20789     }
20790   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20791     goto resync_fail;
20792
20793   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20794   OMP_CLAUSE_CHAIN (c) = list;
20795   return c;
20796
20797  invalid_kind:
20798   cp_parser_error (parser, "invalid schedule kind");
20799  resync_fail:
20800   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20801                                          /*or_comma=*/false,
20802                                          /*consume_paren=*/true);
20803   return list;
20804 }
20805
20806 /* OpenMP 3.0:
20807    untied */
20808
20809 static tree
20810 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20811                              tree list, location_t location)
20812 {
20813   tree c;
20814
20815   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20816
20817   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20818   OMP_CLAUSE_CHAIN (c) = list;
20819   return c;
20820 }
20821
20822 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20823    is a bitmask in MASK.  Return the list of clauses found; the result
20824    of clause default goes in *pdefault.  */
20825
20826 static tree
20827 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20828                            const char *where, cp_token *pragma_tok)
20829 {
20830   tree clauses = NULL;
20831   bool first = true;
20832   cp_token *token = NULL;
20833
20834   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20835     {
20836       pragma_omp_clause c_kind;
20837       const char *c_name;
20838       tree prev = clauses;
20839
20840       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20841         cp_lexer_consume_token (parser->lexer);
20842
20843       token = cp_lexer_peek_token (parser->lexer);
20844       c_kind = cp_parser_omp_clause_name (parser);
20845       first = false;
20846
20847       switch (c_kind)
20848         {
20849         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20850           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20851                                                    token->location);
20852           c_name = "collapse";
20853           break;
20854         case PRAGMA_OMP_CLAUSE_COPYIN:
20855           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20856           c_name = "copyin";
20857           break;
20858         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20859           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20860                                             clauses);
20861           c_name = "copyprivate";
20862           break;
20863         case PRAGMA_OMP_CLAUSE_DEFAULT:
20864           clauses = cp_parser_omp_clause_default (parser, clauses,
20865                                                   token->location);
20866           c_name = "default";
20867           break;
20868         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20869           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20870                                             clauses);
20871           c_name = "firstprivate";
20872           break;
20873         case PRAGMA_OMP_CLAUSE_IF:
20874           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20875           c_name = "if";
20876           break;
20877         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20878           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20879                                             clauses);
20880           c_name = "lastprivate";
20881           break;
20882         case PRAGMA_OMP_CLAUSE_NOWAIT:
20883           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20884           c_name = "nowait";
20885           break;
20886         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20887           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20888                                                       token->location);
20889           c_name = "num_threads";
20890           break;
20891         case PRAGMA_OMP_CLAUSE_ORDERED:
20892           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20893                                                   token->location);
20894           c_name = "ordered";
20895           break;
20896         case PRAGMA_OMP_CLAUSE_PRIVATE:
20897           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20898                                             clauses);
20899           c_name = "private";
20900           break;
20901         case PRAGMA_OMP_CLAUSE_REDUCTION:
20902           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20903           c_name = "reduction";
20904           break;
20905         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20906           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20907                                                    token->location);
20908           c_name = "schedule";
20909           break;
20910         case PRAGMA_OMP_CLAUSE_SHARED:
20911           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20912                                             clauses);
20913           c_name = "shared";
20914           break;
20915         case PRAGMA_OMP_CLAUSE_UNTIED:
20916           clauses = cp_parser_omp_clause_untied (parser, clauses,
20917                                                  token->location);
20918           c_name = "nowait";
20919           break;
20920         default:
20921           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20922           goto saw_error;
20923         }
20924
20925       if (((mask >> c_kind) & 1) == 0)
20926         {
20927           /* Remove the invalid clause(s) from the list to avoid
20928              confusing the rest of the compiler.  */
20929           clauses = prev;
20930           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20931         }
20932     }
20933  saw_error:
20934   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20935   return finish_omp_clauses (clauses);
20936 }
20937
20938 /* OpenMP 2.5:
20939    structured-block:
20940      statement
20941
20942    In practice, we're also interested in adding the statement to an
20943    outer node.  So it is convenient if we work around the fact that
20944    cp_parser_statement calls add_stmt.  */
20945
20946 static unsigned
20947 cp_parser_begin_omp_structured_block (cp_parser *parser)
20948 {
20949   unsigned save = parser->in_statement;
20950
20951   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20952      This preserves the "not within loop or switch" style error messages
20953      for nonsense cases like
20954         void foo() {
20955         #pragma omp single
20956           break;
20957         }
20958   */
20959   if (parser->in_statement)
20960     parser->in_statement = IN_OMP_BLOCK;
20961
20962   return save;
20963 }
20964
20965 static void
20966 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20967 {
20968   parser->in_statement = save;
20969 }
20970
20971 static tree
20972 cp_parser_omp_structured_block (cp_parser *parser)
20973 {
20974   tree stmt = begin_omp_structured_block ();
20975   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20976
20977   cp_parser_statement (parser, NULL_TREE, false, NULL);
20978
20979   cp_parser_end_omp_structured_block (parser, save);
20980   return finish_omp_structured_block (stmt);
20981 }
20982
20983 /* OpenMP 2.5:
20984    # pragma omp atomic new-line
20985      expression-stmt
20986
20987    expression-stmt:
20988      x binop= expr | x++ | ++x | x-- | --x
20989    binop:
20990      +, *, -, /, &, ^, |, <<, >>
20991
20992   where x is an lvalue expression with scalar type.  */
20993
20994 static void
20995 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20996 {
20997   tree lhs, rhs;
20998   enum tree_code code;
20999
21000   cp_parser_require_pragma_eol (parser, pragma_tok);
21001
21002   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21003                                     /*cast_p=*/false, NULL);
21004   switch (TREE_CODE (lhs))
21005     {
21006     case ERROR_MARK:
21007       goto saw_error;
21008
21009     case PREINCREMENT_EXPR:
21010     case POSTINCREMENT_EXPR:
21011       lhs = TREE_OPERAND (lhs, 0);
21012       code = PLUS_EXPR;
21013       rhs = integer_one_node;
21014       break;
21015
21016     case PREDECREMENT_EXPR:
21017     case POSTDECREMENT_EXPR:
21018       lhs = TREE_OPERAND (lhs, 0);
21019       code = MINUS_EXPR;
21020       rhs = integer_one_node;
21021       break;
21022
21023     case COMPOUND_EXPR:
21024       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
21025          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
21026          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
21027          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
21028          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
21029                                              (TREE_OPERAND (lhs, 1), 0), 0)))
21030             == BOOLEAN_TYPE)
21031        /* Undo effects of boolean_increment for post {in,de}crement.  */
21032        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
21033       /* FALLTHRU */
21034     case MODIFY_EXPR:
21035       if (TREE_CODE (lhs) == MODIFY_EXPR
21036          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
21037        {
21038          /* Undo effects of boolean_increment.  */
21039          if (integer_onep (TREE_OPERAND (lhs, 1)))
21040            {
21041              /* This is pre or post increment.  */
21042              rhs = TREE_OPERAND (lhs, 1);
21043              lhs = TREE_OPERAND (lhs, 0);
21044              code = NOP_EXPR;
21045              break;
21046            }
21047        }
21048       /* FALLTHRU */
21049     default:
21050       switch (cp_lexer_peek_token (parser->lexer)->type)
21051         {
21052         case CPP_MULT_EQ:
21053           code = MULT_EXPR;
21054           break;
21055         case CPP_DIV_EQ:
21056           code = TRUNC_DIV_EXPR;
21057           break;
21058         case CPP_PLUS_EQ:
21059           code = PLUS_EXPR;
21060           break;
21061         case CPP_MINUS_EQ:
21062           code = MINUS_EXPR;
21063           break;
21064         case CPP_LSHIFT_EQ:
21065           code = LSHIFT_EXPR;
21066           break;
21067         case CPP_RSHIFT_EQ:
21068           code = RSHIFT_EXPR;
21069           break;
21070         case CPP_AND_EQ:
21071           code = BIT_AND_EXPR;
21072           break;
21073         case CPP_OR_EQ:
21074           code = BIT_IOR_EXPR;
21075           break;
21076         case CPP_XOR_EQ:
21077           code = BIT_XOR_EXPR;
21078           break;
21079         default:
21080           cp_parser_error (parser,
21081                            "invalid operator for %<#pragma omp atomic%>");
21082           goto saw_error;
21083         }
21084       cp_lexer_consume_token (parser->lexer);
21085
21086       rhs = cp_parser_expression (parser, false, NULL);
21087       if (rhs == error_mark_node)
21088         goto saw_error;
21089       break;
21090     }
21091   finish_omp_atomic (code, lhs, rhs);
21092   cp_parser_consume_semicolon_at_end_of_statement (parser);
21093   return;
21094
21095  saw_error:
21096   cp_parser_skip_to_end_of_block_or_statement (parser);
21097 }
21098
21099
21100 /* OpenMP 2.5:
21101    # pragma omp barrier new-line  */
21102
21103 static void
21104 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21105 {
21106   cp_parser_require_pragma_eol (parser, pragma_tok);
21107   finish_omp_barrier ();
21108 }
21109
21110 /* OpenMP 2.5:
21111    # pragma omp critical [(name)] new-line
21112      structured-block  */
21113
21114 static tree
21115 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21116 {
21117   tree stmt, name = NULL;
21118
21119   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21120     {
21121       cp_lexer_consume_token (parser->lexer);
21122
21123       name = cp_parser_identifier (parser);
21124
21125       if (name == error_mark_node
21126           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21127         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21128                                                /*or_comma=*/false,
21129                                                /*consume_paren=*/true);
21130       if (name == error_mark_node)
21131         name = NULL;
21132     }
21133   cp_parser_require_pragma_eol (parser, pragma_tok);
21134
21135   stmt = cp_parser_omp_structured_block (parser);
21136   return c_finish_omp_critical (stmt, name);
21137 }
21138
21139 /* OpenMP 2.5:
21140    # pragma omp flush flush-vars[opt] new-line
21141
21142    flush-vars:
21143      ( variable-list ) */
21144
21145 static void
21146 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21147 {
21148   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21149     (void) cp_parser_omp_var_list (parser, 0, NULL);
21150   cp_parser_require_pragma_eol (parser, pragma_tok);
21151
21152   finish_omp_flush ();
21153 }
21154
21155 /* Helper function, to parse omp for increment expression.  */
21156
21157 static tree
21158 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21159 {
21160   tree cond = cp_parser_binary_expression (parser, false, true,
21161                                            PREC_NOT_OPERATOR, NULL);
21162   bool overloaded_p;
21163
21164   if (cond == error_mark_node
21165       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21166     {
21167       cp_parser_skip_to_end_of_statement (parser);
21168       return error_mark_node;
21169     }
21170
21171   switch (TREE_CODE (cond))
21172     {
21173     case GT_EXPR:
21174     case GE_EXPR:
21175     case LT_EXPR:
21176     case LE_EXPR:
21177       break;
21178     default:
21179       return error_mark_node;
21180     }
21181
21182   /* If decl is an iterator, preserve LHS and RHS of the relational
21183      expr until finish_omp_for.  */
21184   if (decl
21185       && (type_dependent_expression_p (decl)
21186           || CLASS_TYPE_P (TREE_TYPE (decl))))
21187     return cond;
21188
21189   return build_x_binary_op (TREE_CODE (cond),
21190                             TREE_OPERAND (cond, 0), ERROR_MARK,
21191                             TREE_OPERAND (cond, 1), ERROR_MARK,
21192                             &overloaded_p, tf_warning_or_error);
21193 }
21194
21195 /* Helper function, to parse omp for increment expression.  */
21196
21197 static tree
21198 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21199 {
21200   cp_token *token = cp_lexer_peek_token (parser->lexer);
21201   enum tree_code op;
21202   tree lhs, rhs;
21203   cp_id_kind idk;
21204   bool decl_first;
21205
21206   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21207     {
21208       op = (token->type == CPP_PLUS_PLUS
21209             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21210       cp_lexer_consume_token (parser->lexer);
21211       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21212       if (lhs != decl)
21213         return error_mark_node;
21214       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21215     }
21216
21217   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21218   if (lhs != decl)
21219     return error_mark_node;
21220
21221   token = cp_lexer_peek_token (parser->lexer);
21222   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21223     {
21224       op = (token->type == CPP_PLUS_PLUS
21225             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21226       cp_lexer_consume_token (parser->lexer);
21227       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21228     }
21229
21230   op = cp_parser_assignment_operator_opt (parser);
21231   if (op == ERROR_MARK)
21232     return error_mark_node;
21233
21234   if (op != NOP_EXPR)
21235     {
21236       rhs = cp_parser_assignment_expression (parser, false, NULL);
21237       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21238       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21239     }
21240
21241   lhs = cp_parser_binary_expression (parser, false, false,
21242                                      PREC_ADDITIVE_EXPRESSION, NULL);
21243   token = cp_lexer_peek_token (parser->lexer);
21244   decl_first = lhs == decl;
21245   if (decl_first)
21246     lhs = NULL_TREE;
21247   if (token->type != CPP_PLUS
21248       && token->type != CPP_MINUS)
21249     return error_mark_node;
21250
21251   do
21252     {
21253       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21254       cp_lexer_consume_token (parser->lexer);
21255       rhs = cp_parser_binary_expression (parser, false, false,
21256                                          PREC_ADDITIVE_EXPRESSION, NULL);
21257       token = cp_lexer_peek_token (parser->lexer);
21258       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21259         {
21260           if (lhs == NULL_TREE)
21261             {
21262               if (op == PLUS_EXPR)
21263                 lhs = rhs;
21264               else
21265                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21266             }
21267           else
21268             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21269                                      NULL, tf_warning_or_error);
21270         }
21271     }
21272   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21273
21274   if (!decl_first)
21275     {
21276       if (rhs != decl || op == MINUS_EXPR)
21277         return error_mark_node;
21278       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21279     }
21280   else
21281     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21282
21283   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21284 }
21285
21286 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21287
21288 static tree
21289 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21290 {
21291   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21292   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21293   tree this_pre_body, cl;
21294   location_t loc_first;
21295   bool collapse_err = false;
21296   int i, collapse = 1, nbraces = 0;
21297
21298   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21299     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21300       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21301
21302   gcc_assert (collapse >= 1);
21303
21304   declv = make_tree_vec (collapse);
21305   initv = make_tree_vec (collapse);
21306   condv = make_tree_vec (collapse);
21307   incrv = make_tree_vec (collapse);
21308
21309   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21310
21311   for (i = 0; i < collapse; i++)
21312     {
21313       int bracecount = 0;
21314       bool add_private_clause = false;
21315       location_t loc;
21316
21317       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21318         {
21319           cp_parser_error (parser, "for statement expected");
21320           return NULL;
21321         }
21322       loc = cp_lexer_consume_token (parser->lexer)->location;
21323
21324       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21325         return NULL;
21326
21327       init = decl = real_decl = NULL;
21328       this_pre_body = push_stmt_list ();
21329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21330         {
21331           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21332
21333              init-expr:
21334                        var = lb
21335                        integer-type var = lb
21336                        random-access-iterator-type var = lb
21337                        pointer-type var = lb
21338           */
21339           cp_decl_specifier_seq type_specifiers;
21340
21341           /* First, try to parse as an initialized declaration.  See
21342              cp_parser_condition, from whence the bulk of this is copied.  */
21343
21344           cp_parser_parse_tentatively (parser);
21345           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21346                                         /*is_trailing_return=*/false,
21347                                         &type_specifiers);
21348           if (cp_parser_parse_definitely (parser))
21349             {
21350               /* If parsing a type specifier seq succeeded, then this
21351                  MUST be a initialized declaration.  */
21352               tree asm_specification, attributes;
21353               cp_declarator *declarator;
21354
21355               declarator = cp_parser_declarator (parser,
21356                                                  CP_PARSER_DECLARATOR_NAMED,
21357                                                  /*ctor_dtor_or_conv_p=*/NULL,
21358                                                  /*parenthesized_p=*/NULL,
21359                                                  /*member_p=*/false);
21360               attributes = cp_parser_attributes_opt (parser);
21361               asm_specification = cp_parser_asm_specification_opt (parser);
21362
21363               if (declarator == cp_error_declarator) 
21364                 cp_parser_skip_to_end_of_statement (parser);
21365
21366               else 
21367                 {
21368                   tree pushed_scope, auto_node;
21369
21370                   decl = start_decl (declarator, &type_specifiers,
21371                                      SD_INITIALIZED, attributes,
21372                                      /*prefix_attributes=*/NULL_TREE,
21373                                      &pushed_scope);
21374
21375                   auto_node = type_uses_auto (TREE_TYPE (decl));
21376                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21377                     {
21378                       if (cp_lexer_next_token_is (parser->lexer, 
21379                                                   CPP_OPEN_PAREN))
21380                         error ("parenthesized initialization is not allowed in "
21381                                "OpenMP %<for%> loop");
21382                       else
21383                         /* Trigger an error.  */
21384                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21385
21386                       init = error_mark_node;
21387                       cp_parser_skip_to_end_of_statement (parser);
21388                     }
21389                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21390                            || type_dependent_expression_p (decl)
21391                            || auto_node)
21392                     {
21393                       bool is_direct_init, is_non_constant_init;
21394
21395                       init = cp_parser_initializer (parser,
21396                                                     &is_direct_init,
21397                                                     &is_non_constant_init);
21398
21399                       if (auto_node && describable_type (init))
21400                         {
21401                           TREE_TYPE (decl)
21402                             = do_auto_deduction (TREE_TYPE (decl), init,
21403                                                  auto_node);
21404
21405                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21406                               && !type_dependent_expression_p (decl))
21407                             goto non_class;
21408                         }
21409                       
21410                       cp_finish_decl (decl, init, !is_non_constant_init,
21411                                       asm_specification,
21412                                       LOOKUP_ONLYCONVERTING);
21413                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21414                         {
21415                           for_block
21416                             = tree_cons (NULL, this_pre_body, for_block);
21417                           init = NULL_TREE;
21418                         }
21419                       else
21420                         init = pop_stmt_list (this_pre_body);
21421                       this_pre_body = NULL_TREE;
21422                     }
21423                   else
21424                     {
21425                       /* Consume '='.  */
21426                       cp_lexer_consume_token (parser->lexer);
21427                       init = cp_parser_assignment_expression (parser, false, NULL);
21428
21429                     non_class:
21430                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21431                         init = error_mark_node;
21432                       else
21433                         cp_finish_decl (decl, NULL_TREE,
21434                                         /*init_const_expr_p=*/false,
21435                                         asm_specification,
21436                                         LOOKUP_ONLYCONVERTING);
21437                     }
21438
21439                   if (pushed_scope)
21440                     pop_scope (pushed_scope);
21441                 }
21442             }
21443           else 
21444             {
21445               cp_id_kind idk;
21446               /* If parsing a type specifier sequence failed, then
21447                  this MUST be a simple expression.  */
21448               cp_parser_parse_tentatively (parser);
21449               decl = cp_parser_primary_expression (parser, false, false,
21450                                                    false, &idk);
21451               if (!cp_parser_error_occurred (parser)
21452                   && decl
21453                   && DECL_P (decl)
21454                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21455                 {
21456                   tree rhs;
21457
21458                   cp_parser_parse_definitely (parser);
21459                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21460                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21461                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21462                                                          rhs,
21463                                                          tf_warning_or_error));
21464                   add_private_clause = true;
21465                 }
21466               else
21467                 {
21468                   decl = NULL;
21469                   cp_parser_abort_tentative_parse (parser);
21470                   init = cp_parser_expression (parser, false, NULL);
21471                   if (init)
21472                     {
21473                       if (TREE_CODE (init) == MODIFY_EXPR
21474                           || TREE_CODE (init) == MODOP_EXPR)
21475                         real_decl = TREE_OPERAND (init, 0);
21476                     }
21477                 }
21478             }
21479         }
21480       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21481       if (this_pre_body)
21482         {
21483           this_pre_body = pop_stmt_list (this_pre_body);
21484           if (pre_body)
21485             {
21486               tree t = pre_body;
21487               pre_body = push_stmt_list ();
21488               add_stmt (t);
21489               add_stmt (this_pre_body);
21490               pre_body = pop_stmt_list (pre_body);
21491             }
21492           else
21493             pre_body = this_pre_body;
21494         }
21495
21496       if (decl)
21497         real_decl = decl;
21498       if (par_clauses != NULL && real_decl != NULL_TREE)
21499         {
21500           tree *c;
21501           for (c = par_clauses; *c ; )
21502             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21503                 && OMP_CLAUSE_DECL (*c) == real_decl)
21504               {
21505                 error ("%Hiteration variable %qD should not be firstprivate",
21506                        &loc, real_decl);
21507                 *c = OMP_CLAUSE_CHAIN (*c);
21508               }
21509             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21510                      && OMP_CLAUSE_DECL (*c) == real_decl)
21511               {
21512                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21513                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21514                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21515                 OMP_CLAUSE_DECL (l) = real_decl;
21516                 OMP_CLAUSE_CHAIN (l) = clauses;
21517                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21518                 clauses = l;
21519                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21520                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21521                 add_private_clause = false;
21522               }
21523             else
21524               {
21525                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21526                     && OMP_CLAUSE_DECL (*c) == real_decl)
21527                   add_private_clause = false;
21528                 c = &OMP_CLAUSE_CHAIN (*c);
21529               }
21530         }
21531
21532       if (add_private_clause)
21533         {
21534           tree c;
21535           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21536             {
21537               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21538                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21539                   && OMP_CLAUSE_DECL (c) == decl)
21540                 break;
21541               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21542                        && OMP_CLAUSE_DECL (c) == decl)
21543                 error ("%Hiteration variable %qD should not be firstprivate",
21544                        &loc, decl);
21545               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21546                        && OMP_CLAUSE_DECL (c) == decl)
21547                 error ("%Hiteration variable %qD should not be reduction",
21548                        &loc, decl);
21549             }
21550           if (c == NULL)
21551             {
21552               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21553               OMP_CLAUSE_DECL (c) = decl;
21554               c = finish_omp_clauses (c);
21555               if (c)
21556                 {
21557                   OMP_CLAUSE_CHAIN (c) = clauses;
21558                   clauses = c;
21559                 }
21560             }
21561         }
21562
21563       cond = NULL;
21564       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21565         cond = cp_parser_omp_for_cond (parser, decl);
21566       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21567
21568       incr = NULL;
21569       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21570         {
21571           /* If decl is an iterator, preserve the operator on decl
21572              until finish_omp_for.  */
21573           if (decl
21574               && (type_dependent_expression_p (decl)
21575                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21576             incr = cp_parser_omp_for_incr (parser, decl);
21577           else
21578             incr = cp_parser_expression (parser, false, NULL);
21579         }
21580
21581       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21582         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21583                                                /*or_comma=*/false,
21584                                                /*consume_paren=*/true);
21585
21586       TREE_VEC_ELT (declv, i) = decl;
21587       TREE_VEC_ELT (initv, i) = init;
21588       TREE_VEC_ELT (condv, i) = cond;
21589       TREE_VEC_ELT (incrv, i) = incr;
21590
21591       if (i == collapse - 1)
21592         break;
21593
21594       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21595          in between the collapsed for loops to be still considered perfectly
21596          nested.  Hopefully the final version clarifies this.
21597          For now handle (multiple) {'s and empty statements.  */
21598       cp_parser_parse_tentatively (parser);
21599       do
21600         {
21601           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21602             break;
21603           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21604             {
21605               cp_lexer_consume_token (parser->lexer);
21606               bracecount++;
21607             }
21608           else if (bracecount
21609                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21610             cp_lexer_consume_token (parser->lexer);
21611           else
21612             {
21613               loc = cp_lexer_peek_token (parser->lexer)->location;
21614               error ("%Hnot enough collapsed for loops", &loc);
21615               collapse_err = true;
21616               cp_parser_abort_tentative_parse (parser);
21617               declv = NULL_TREE;
21618               break;
21619             }
21620         }
21621       while (1);
21622
21623       if (declv)
21624         {
21625           cp_parser_parse_definitely (parser);
21626           nbraces += bracecount;
21627         }
21628     }
21629
21630   /* Note that we saved the original contents of this flag when we entered
21631      the structured block, and so we don't need to re-save it here.  */
21632   parser->in_statement = IN_OMP_FOR;
21633
21634   /* Note that the grammar doesn't call for a structured block here,
21635      though the loop as a whole is a structured block.  */
21636   body = push_stmt_list ();
21637   cp_parser_statement (parser, NULL_TREE, false, NULL);
21638   body = pop_stmt_list (body);
21639
21640   if (declv == NULL_TREE)
21641     ret = NULL_TREE;
21642   else
21643     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21644                           pre_body, clauses);
21645
21646   while (nbraces)
21647     {
21648       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21649         {
21650           cp_lexer_consume_token (parser->lexer);
21651           nbraces--;
21652         }
21653       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21654         cp_lexer_consume_token (parser->lexer);
21655       else
21656         {
21657           if (!collapse_err)
21658             {
21659               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21660               error ("%Hcollapsed loops not perfectly nested", &loc);
21661             }
21662           collapse_err = true;
21663           cp_parser_statement_seq_opt (parser, NULL);
21664           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21665             break;
21666         }
21667     }
21668
21669   while (for_block)
21670     {
21671       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21672       for_block = TREE_CHAIN (for_block);
21673     }
21674
21675   return ret;
21676 }
21677
21678 /* OpenMP 2.5:
21679    #pragma omp for for-clause[optseq] new-line
21680      for-loop  */
21681
21682 #define OMP_FOR_CLAUSE_MASK                             \
21683         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21684         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21685         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21686         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21687         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21688         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21689         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21690         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21691
21692 static tree
21693 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21694 {
21695   tree clauses, sb, ret;
21696   unsigned int save;
21697
21698   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21699                                        "#pragma omp for", pragma_tok);
21700
21701   sb = begin_omp_structured_block ();
21702   save = cp_parser_begin_omp_structured_block (parser);
21703
21704   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21705
21706   cp_parser_end_omp_structured_block (parser, save);
21707   add_stmt (finish_omp_structured_block (sb));
21708
21709   return ret;
21710 }
21711
21712 /* OpenMP 2.5:
21713    # pragma omp master new-line
21714      structured-block  */
21715
21716 static tree
21717 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21718 {
21719   cp_parser_require_pragma_eol (parser, pragma_tok);
21720   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21721 }
21722
21723 /* OpenMP 2.5:
21724    # pragma omp ordered new-line
21725      structured-block  */
21726
21727 static tree
21728 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21729 {
21730   cp_parser_require_pragma_eol (parser, pragma_tok);
21731   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21732 }
21733
21734 /* OpenMP 2.5:
21735
21736    section-scope:
21737      { section-sequence }
21738
21739    section-sequence:
21740      section-directive[opt] structured-block
21741      section-sequence section-directive structured-block  */
21742
21743 static tree
21744 cp_parser_omp_sections_scope (cp_parser *parser)
21745 {
21746   tree stmt, substmt;
21747   bool error_suppress = false;
21748   cp_token *tok;
21749
21750   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21751     return NULL_TREE;
21752
21753   stmt = push_stmt_list ();
21754
21755   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21756     {
21757       unsigned save;
21758
21759       substmt = begin_omp_structured_block ();
21760       save = cp_parser_begin_omp_structured_block (parser);
21761
21762       while (1)
21763         {
21764           cp_parser_statement (parser, NULL_TREE, false, NULL);
21765
21766           tok = cp_lexer_peek_token (parser->lexer);
21767           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21768             break;
21769           if (tok->type == CPP_CLOSE_BRACE)
21770             break;
21771           if (tok->type == CPP_EOF)
21772             break;
21773         }
21774
21775       cp_parser_end_omp_structured_block (parser, save);
21776       substmt = finish_omp_structured_block (substmt);
21777       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21778       add_stmt (substmt);
21779     }
21780
21781   while (1)
21782     {
21783       tok = cp_lexer_peek_token (parser->lexer);
21784       if (tok->type == CPP_CLOSE_BRACE)
21785         break;
21786       if (tok->type == CPP_EOF)
21787         break;
21788
21789       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21790         {
21791           cp_lexer_consume_token (parser->lexer);
21792           cp_parser_require_pragma_eol (parser, tok);
21793           error_suppress = false;
21794         }
21795       else if (!error_suppress)
21796         {
21797           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21798           error_suppress = true;
21799         }
21800
21801       substmt = cp_parser_omp_structured_block (parser);
21802       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21803       add_stmt (substmt);
21804     }
21805   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21806
21807   substmt = pop_stmt_list (stmt);
21808
21809   stmt = make_node (OMP_SECTIONS);
21810   TREE_TYPE (stmt) = void_type_node;
21811   OMP_SECTIONS_BODY (stmt) = substmt;
21812
21813   add_stmt (stmt);
21814   return stmt;
21815 }
21816
21817 /* OpenMP 2.5:
21818    # pragma omp sections sections-clause[optseq] newline
21819      sections-scope  */
21820
21821 #define OMP_SECTIONS_CLAUSE_MASK                        \
21822         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21823         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21824         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21825         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21826         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21827
21828 static tree
21829 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21830 {
21831   tree clauses, ret;
21832
21833   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21834                                        "#pragma omp sections", pragma_tok);
21835
21836   ret = cp_parser_omp_sections_scope (parser);
21837   if (ret)
21838     OMP_SECTIONS_CLAUSES (ret) = clauses;
21839
21840   return ret;
21841 }
21842
21843 /* OpenMP 2.5:
21844    # pragma parallel parallel-clause new-line
21845    # pragma parallel for parallel-for-clause new-line
21846    # pragma parallel sections parallel-sections-clause new-line  */
21847
21848 #define OMP_PARALLEL_CLAUSE_MASK                        \
21849         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21850         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21851         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21852         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21853         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21854         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21855         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21856         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21857
21858 static tree
21859 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21860 {
21861   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21862   const char *p_name = "#pragma omp parallel";
21863   tree stmt, clauses, par_clause, ws_clause, block;
21864   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21865   unsigned int save;
21866
21867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21868     {
21869       cp_lexer_consume_token (parser->lexer);
21870       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21871       p_name = "#pragma omp parallel for";
21872       mask |= OMP_FOR_CLAUSE_MASK;
21873       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21874     }
21875   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21876     {
21877       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21878       const char *p = IDENTIFIER_POINTER (id);
21879       if (strcmp (p, "sections") == 0)
21880         {
21881           cp_lexer_consume_token (parser->lexer);
21882           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21883           p_name = "#pragma omp parallel sections";
21884           mask |= OMP_SECTIONS_CLAUSE_MASK;
21885           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21886         }
21887     }
21888
21889   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21890   block = begin_omp_parallel ();
21891   save = cp_parser_begin_omp_structured_block (parser);
21892
21893   switch (p_kind)
21894     {
21895     case PRAGMA_OMP_PARALLEL:
21896       cp_parser_statement (parser, NULL_TREE, false, NULL);
21897       par_clause = clauses;
21898       break;
21899
21900     case PRAGMA_OMP_PARALLEL_FOR:
21901       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21902       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21903       break;
21904
21905     case PRAGMA_OMP_PARALLEL_SECTIONS:
21906       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21907       stmt = cp_parser_omp_sections_scope (parser);
21908       if (stmt)
21909         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21910       break;
21911
21912     default:
21913       gcc_unreachable ();
21914     }
21915
21916   cp_parser_end_omp_structured_block (parser, save);
21917   stmt = finish_omp_parallel (par_clause, block);
21918   if (p_kind != PRAGMA_OMP_PARALLEL)
21919     OMP_PARALLEL_COMBINED (stmt) = 1;
21920   return stmt;
21921 }
21922
21923 /* OpenMP 2.5:
21924    # pragma omp single single-clause[optseq] new-line
21925      structured-block  */
21926
21927 #define OMP_SINGLE_CLAUSE_MASK                          \
21928         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21929         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21930         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21931         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21932
21933 static tree
21934 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21935 {
21936   tree stmt = make_node (OMP_SINGLE);
21937   TREE_TYPE (stmt) = void_type_node;
21938
21939   OMP_SINGLE_CLAUSES (stmt)
21940     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21941                                  "#pragma omp single", pragma_tok);
21942   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21943
21944   return add_stmt (stmt);
21945 }
21946
21947 /* OpenMP 3.0:
21948    # pragma omp task task-clause[optseq] new-line
21949      structured-block  */
21950
21951 #define OMP_TASK_CLAUSE_MASK                            \
21952         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21953         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21954         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21955         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21956         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21957         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21958
21959 static tree
21960 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21961 {
21962   tree clauses, block;
21963   unsigned int save;
21964
21965   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21966                                        "#pragma omp task", pragma_tok);
21967   block = begin_omp_task ();
21968   save = cp_parser_begin_omp_structured_block (parser);
21969   cp_parser_statement (parser, NULL_TREE, false, NULL);
21970   cp_parser_end_omp_structured_block (parser, save);
21971   return finish_omp_task (clauses, block);
21972 }
21973
21974 /* OpenMP 3.0:
21975    # pragma omp taskwait new-line  */
21976
21977 static void
21978 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21979 {
21980   cp_parser_require_pragma_eol (parser, pragma_tok);
21981   finish_omp_taskwait ();
21982 }
21983
21984 /* OpenMP 2.5:
21985    # pragma omp threadprivate (variable-list) */
21986
21987 static void
21988 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21989 {
21990   tree vars;
21991
21992   vars = cp_parser_omp_var_list (parser, 0, NULL);
21993   cp_parser_require_pragma_eol (parser, pragma_tok);
21994
21995   finish_omp_threadprivate (vars);
21996 }
21997
21998 /* Main entry point to OpenMP statement pragmas.  */
21999
22000 static void
22001 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22002 {
22003   tree stmt;
22004
22005   switch (pragma_tok->pragma_kind)
22006     {
22007     case PRAGMA_OMP_ATOMIC:
22008       cp_parser_omp_atomic (parser, pragma_tok);
22009       return;
22010     case PRAGMA_OMP_CRITICAL:
22011       stmt = cp_parser_omp_critical (parser, pragma_tok);
22012       break;
22013     case PRAGMA_OMP_FOR:
22014       stmt = cp_parser_omp_for (parser, pragma_tok);
22015       break;
22016     case PRAGMA_OMP_MASTER:
22017       stmt = cp_parser_omp_master (parser, pragma_tok);
22018       break;
22019     case PRAGMA_OMP_ORDERED:
22020       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22021       break;
22022     case PRAGMA_OMP_PARALLEL:
22023       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22024       break;
22025     case PRAGMA_OMP_SECTIONS:
22026       stmt = cp_parser_omp_sections (parser, pragma_tok);
22027       break;
22028     case PRAGMA_OMP_SINGLE:
22029       stmt = cp_parser_omp_single (parser, pragma_tok);
22030       break;
22031     case PRAGMA_OMP_TASK:
22032       stmt = cp_parser_omp_task (parser, pragma_tok);
22033       break;
22034     default:
22035       gcc_unreachable ();
22036     }
22037
22038   if (stmt)
22039     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22040 }
22041 \f
22042 /* The parser.  */
22043
22044 static GTY (()) cp_parser *the_parser;
22045
22046 \f
22047 /* Special handling for the first token or line in the file.  The first
22048    thing in the file might be #pragma GCC pch_preprocess, which loads a
22049    PCH file, which is a GC collection point.  So we need to handle this
22050    first pragma without benefit of an existing lexer structure.
22051
22052    Always returns one token to the caller in *FIRST_TOKEN.  This is
22053    either the true first token of the file, or the first token after
22054    the initial pragma.  */
22055
22056 static void
22057 cp_parser_initial_pragma (cp_token *first_token)
22058 {
22059   tree name = NULL;
22060
22061   cp_lexer_get_preprocessor_token (NULL, first_token);
22062   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22063     return;
22064
22065   cp_lexer_get_preprocessor_token (NULL, first_token);
22066   if (first_token->type == CPP_STRING)
22067     {
22068       name = first_token->u.value;
22069
22070       cp_lexer_get_preprocessor_token (NULL, first_token);
22071       if (first_token->type != CPP_PRAGMA_EOL)
22072         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22073                &first_token->location);
22074     }
22075   else
22076     error ("%Hexpected string literal", &first_token->location);
22077
22078   /* Skip to the end of the pragma.  */
22079   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22080     cp_lexer_get_preprocessor_token (NULL, first_token);
22081
22082   /* Now actually load the PCH file.  */
22083   if (name)
22084     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22085
22086   /* Read one more token to return to our caller.  We have to do this
22087      after reading the PCH file in, since its pointers have to be
22088      live.  */
22089   cp_lexer_get_preprocessor_token (NULL, first_token);
22090 }
22091
22092 /* Normal parsing of a pragma token.  Here we can (and must) use the
22093    regular lexer.  */
22094
22095 static bool
22096 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22097 {
22098   cp_token *pragma_tok;
22099   unsigned int id;
22100
22101   pragma_tok = cp_lexer_consume_token (parser->lexer);
22102   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22103   parser->lexer->in_pragma = true;
22104
22105   id = pragma_tok->pragma_kind;
22106   switch (id)
22107     {
22108     case PRAGMA_GCC_PCH_PREPROCESS:
22109       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22110              &pragma_tok->location);
22111       break;
22112
22113     case PRAGMA_OMP_BARRIER:
22114       switch (context)
22115         {
22116         case pragma_compound:
22117           cp_parser_omp_barrier (parser, pragma_tok);
22118           return false;
22119         case pragma_stmt:
22120           error ("%H%<#pragma omp barrier%> may only be "
22121                  "used in compound statements", &pragma_tok->location);
22122           break;
22123         default:
22124           goto bad_stmt;
22125         }
22126       break;
22127
22128     case PRAGMA_OMP_FLUSH:
22129       switch (context)
22130         {
22131         case pragma_compound:
22132           cp_parser_omp_flush (parser, pragma_tok);
22133           return false;
22134         case pragma_stmt:
22135           error ("%H%<#pragma omp flush%> may only be "
22136                  "used in compound statements", &pragma_tok->location);
22137           break;
22138         default:
22139           goto bad_stmt;
22140         }
22141       break;
22142
22143     case PRAGMA_OMP_TASKWAIT:
22144       switch (context)
22145         {
22146         case pragma_compound:
22147           cp_parser_omp_taskwait (parser, pragma_tok);
22148           return false;
22149         case pragma_stmt:
22150           error ("%H%<#pragma omp taskwait%> may only be "
22151                  "used in compound statements",
22152                  &pragma_tok->location);
22153           break;
22154         default:
22155           goto bad_stmt;
22156         }
22157       break;
22158
22159     case PRAGMA_OMP_THREADPRIVATE:
22160       cp_parser_omp_threadprivate (parser, pragma_tok);
22161       return false;
22162
22163     case PRAGMA_OMP_ATOMIC:
22164     case PRAGMA_OMP_CRITICAL:
22165     case PRAGMA_OMP_FOR:
22166     case PRAGMA_OMP_MASTER:
22167     case PRAGMA_OMP_ORDERED:
22168     case PRAGMA_OMP_PARALLEL:
22169     case PRAGMA_OMP_SECTIONS:
22170     case PRAGMA_OMP_SINGLE:
22171     case PRAGMA_OMP_TASK:
22172       if (context == pragma_external)
22173         goto bad_stmt;
22174       cp_parser_omp_construct (parser, pragma_tok);
22175       return true;
22176
22177     case PRAGMA_OMP_SECTION:
22178       error ("%H%<#pragma omp section%> may only be used in "
22179              "%<#pragma omp sections%> construct", &pragma_tok->location);
22180       break;
22181
22182     default:
22183       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22184       c_invoke_pragma_handler (id);
22185       break;
22186
22187     bad_stmt:
22188       cp_parser_error (parser, "expected declaration specifiers");
22189       break;
22190     }
22191
22192   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22193   return false;
22194 }
22195
22196 /* The interface the pragma parsers have to the lexer.  */
22197
22198 enum cpp_ttype
22199 pragma_lex (tree *value)
22200 {
22201   cp_token *tok;
22202   enum cpp_ttype ret;
22203
22204   tok = cp_lexer_peek_token (the_parser->lexer);
22205
22206   ret = tok->type;
22207   *value = tok->u.value;
22208
22209   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22210     ret = CPP_EOF;
22211   else if (ret == CPP_STRING)
22212     *value = cp_parser_string_literal (the_parser, false, false);
22213   else
22214     {
22215       cp_lexer_consume_token (the_parser->lexer);
22216       if (ret == CPP_KEYWORD)
22217         ret = CPP_NAME;
22218     }
22219
22220   return ret;
22221 }
22222
22223 \f
22224 /* External interface.  */
22225
22226 /* Parse one entire translation unit.  */
22227
22228 void
22229 c_parse_file (void)
22230 {
22231   bool error_occurred;
22232   static bool already_called = false;
22233
22234   if (already_called)
22235     {
22236       sorry ("inter-module optimizations not implemented for C++");
22237       return;
22238     }
22239   already_called = true;
22240
22241   the_parser = cp_parser_new ();
22242   push_deferring_access_checks (flag_access_control
22243                                 ? dk_no_deferred : dk_no_check);
22244   error_occurred = cp_parser_translation_unit (the_parser);
22245   the_parser = NULL;
22246 }
22247
22248 #include "gt-cp-parser.h"