Upgrade GCC from 4.4.2 to 4.4.5 on the vendor branch.
[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           }
3278         /* The `>' token might be the end of a template-id or
3279            template-parameter-list now.  */
3280         parser->greater_than_is_operator_p
3281           = saved_greater_than_is_operator_p;
3282         /* Consume the `)'.  */
3283         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3284           cp_parser_skip_to_end_of_statement (parser);
3285
3286         return expr;
3287       }
3288
3289     case CPP_KEYWORD:
3290       switch (token->keyword)
3291         {
3292           /* These two are the boolean literals.  */
3293         case RID_TRUE:
3294           cp_lexer_consume_token (parser->lexer);
3295           return boolean_true_node;
3296         case RID_FALSE:
3297           cp_lexer_consume_token (parser->lexer);
3298           return boolean_false_node;
3299
3300           /* The `__null' literal.  */
3301         case RID_NULL:
3302           cp_lexer_consume_token (parser->lexer);
3303           return null_node;
3304
3305           /* Recognize the `this' keyword.  */
3306         case RID_THIS:
3307           cp_lexer_consume_token (parser->lexer);
3308           if (parser->local_variables_forbidden_p)
3309             {
3310               error ("%H%<this%> may not be used in this context",
3311                      &token->location);
3312               return error_mark_node;
3313             }
3314           /* Pointers cannot appear in constant-expressions.  */
3315           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3316             return error_mark_node;
3317           return finish_this_expr ();
3318
3319           /* The `operator' keyword can be the beginning of an
3320              id-expression.  */
3321         case RID_OPERATOR:
3322           goto id_expression;
3323
3324         case RID_FUNCTION_NAME:
3325         case RID_PRETTY_FUNCTION_NAME:
3326         case RID_C99_FUNCTION_NAME:
3327           {
3328             const char *name;
3329
3330             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3331                __func__ are the names of variables -- but they are
3332                treated specially.  Therefore, they are handled here,
3333                rather than relying on the generic id-expression logic
3334                below.  Grammatically, these names are id-expressions.
3335
3336                Consume the token.  */
3337             token = cp_lexer_consume_token (parser->lexer);
3338
3339             switch (token->keyword)
3340               {
3341               case RID_FUNCTION_NAME:
3342                 name = "%<__FUNCTION__%>";
3343                 break;
3344               case RID_PRETTY_FUNCTION_NAME:
3345                 name = "%<__PRETTY_FUNCTION__%>";
3346                 break;
3347               case RID_C99_FUNCTION_NAME:
3348                 name = "%<__func__%>";
3349                 break;
3350               default:
3351                 gcc_unreachable ();
3352               }
3353
3354             if (cp_parser_non_integral_constant_expression (parser, name))
3355               return error_mark_node;
3356
3357             /* Look up the name.  */
3358             return finish_fname (token->u.value);
3359           }
3360
3361         case RID_VA_ARG:
3362           {
3363             tree expression;
3364             tree type;
3365
3366             /* The `__builtin_va_arg' construct is used to handle
3367                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3368             cp_lexer_consume_token (parser->lexer);
3369             /* Look for the opening `('.  */
3370             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3371             /* Now, parse the assignment-expression.  */
3372             expression = cp_parser_assignment_expression (parser,
3373                                                           /*cast_p=*/false, NULL);
3374             /* Look for the `,'.  */
3375             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3376             /* Parse the type-id.  */
3377             type = cp_parser_type_id (parser);
3378             /* Look for the closing `)'.  */
3379             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3380             /* Using `va_arg' in a constant-expression is not
3381                allowed.  */
3382             if (cp_parser_non_integral_constant_expression (parser,
3383                                                             "%<va_arg%>"))
3384               return error_mark_node;
3385             return build_x_va_arg (expression, type);
3386           }
3387
3388         case RID_OFFSETOF:
3389           return cp_parser_builtin_offsetof (parser);
3390
3391         case RID_HAS_NOTHROW_ASSIGN:
3392         case RID_HAS_NOTHROW_CONSTRUCTOR:
3393         case RID_HAS_NOTHROW_COPY:        
3394         case RID_HAS_TRIVIAL_ASSIGN:
3395         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3396         case RID_HAS_TRIVIAL_COPY:        
3397         case RID_HAS_TRIVIAL_DESTRUCTOR:
3398         case RID_HAS_VIRTUAL_DESTRUCTOR:
3399         case RID_IS_ABSTRACT:
3400         case RID_IS_BASE_OF:
3401         case RID_IS_CLASS:
3402         case RID_IS_CONVERTIBLE_TO:
3403         case RID_IS_EMPTY:
3404         case RID_IS_ENUM:
3405         case RID_IS_POD:
3406         case RID_IS_POLYMORPHIC:
3407         case RID_IS_UNION:
3408           return cp_parser_trait_expr (parser, token->keyword);
3409
3410         /* Objective-C++ expressions.  */
3411         case RID_AT_ENCODE:
3412         case RID_AT_PROTOCOL:
3413         case RID_AT_SELECTOR:
3414           return cp_parser_objc_expression (parser);
3415
3416         default:
3417           cp_parser_error (parser, "expected primary-expression");
3418           return error_mark_node;
3419         }
3420
3421       /* An id-expression can start with either an identifier, a
3422          `::' as the beginning of a qualified-id, or the "operator"
3423          keyword.  */
3424     case CPP_NAME:
3425     case CPP_SCOPE:
3426     case CPP_TEMPLATE_ID:
3427     case CPP_NESTED_NAME_SPECIFIER:
3428       {
3429         tree id_expression;
3430         tree decl;
3431         const char *error_msg;
3432         bool template_p;
3433         bool done;
3434         cp_token *id_expr_token;
3435
3436       id_expression:
3437         /* Parse the id-expression.  */
3438         id_expression
3439           = cp_parser_id_expression (parser,
3440                                      /*template_keyword_p=*/false,
3441                                      /*check_dependency_p=*/true,
3442                                      &template_p,
3443                                      /*declarator_p=*/false,
3444                                      /*optional_p=*/false);
3445         if (id_expression == error_mark_node)
3446           return error_mark_node;
3447         id_expr_token = token;
3448         token = cp_lexer_peek_token (parser->lexer);
3449         done = (token->type != CPP_OPEN_SQUARE
3450                 && token->type != CPP_OPEN_PAREN
3451                 && token->type != CPP_DOT
3452                 && token->type != CPP_DEREF
3453                 && token->type != CPP_PLUS_PLUS
3454                 && token->type != CPP_MINUS_MINUS);
3455         /* If we have a template-id, then no further lookup is
3456            required.  If the template-id was for a template-class, we
3457            will sometimes have a TYPE_DECL at this point.  */
3458         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3459                  || TREE_CODE (id_expression) == TYPE_DECL)
3460           decl = id_expression;
3461         /* Look up the name.  */
3462         else
3463           {
3464             tree ambiguous_decls;
3465
3466             decl = cp_parser_lookup_name (parser, id_expression,
3467                                           none_type,
3468                                           template_p,
3469                                           /*is_namespace=*/false,
3470                                           /*check_dependency=*/true,
3471                                           &ambiguous_decls,
3472                                           id_expr_token->location);
3473             /* If the lookup was ambiguous, an error will already have
3474                been issued.  */
3475             if (ambiguous_decls)
3476               return error_mark_node;
3477
3478             /* In Objective-C++, an instance variable (ivar) may be preferred
3479                to whatever cp_parser_lookup_name() found.  */
3480             decl = objc_lookup_ivar (decl, id_expression);
3481
3482             /* If name lookup gives us a SCOPE_REF, then the
3483                qualifying scope was dependent.  */
3484             if (TREE_CODE (decl) == SCOPE_REF)
3485               {
3486                 /* At this point, we do not know if DECL is a valid
3487                    integral constant expression.  We assume that it is
3488                    in fact such an expression, so that code like:
3489
3490                       template <int N> struct A {
3491                         int a[B<N>::i];
3492                       };
3493                      
3494                    is accepted.  At template-instantiation time, we
3495                    will check that B<N>::i is actually a constant.  */
3496                 return decl;
3497               }
3498             /* Check to see if DECL is a local variable in a context
3499                where that is forbidden.  */
3500             if (parser->local_variables_forbidden_p
3501                 && local_variable_p (decl))
3502               {
3503                 /* It might be that we only found DECL because we are
3504                    trying to be generous with pre-ISO scoping rules.
3505                    For example, consider:
3506
3507                      int i;
3508                      void g() {
3509                        for (int i = 0; i < 10; ++i) {}
3510                        extern void f(int j = i);
3511                      }
3512
3513                    Here, name look up will originally find the out
3514                    of scope `i'.  We need to issue a warning message,
3515                    but then use the global `i'.  */
3516                 decl = check_for_out_of_scope_variable (decl);
3517                 if (local_variable_p (decl))
3518                   {
3519                     error ("%Hlocal variable %qD may not appear in this context",
3520                            &id_expr_token->location, decl);
3521                     return error_mark_node;
3522                   }
3523               }
3524           }
3525
3526         decl = (finish_id_expression
3527                 (id_expression, decl, parser->scope,
3528                  idk,
3529                  parser->integral_constant_expression_p,
3530                  parser->allow_non_integral_constant_expression_p,
3531                  &parser->non_integral_constant_expression_p,
3532                  template_p, done, address_p,
3533                  template_arg_p,
3534                  &error_msg,
3535                  id_expr_token->location));
3536         if (error_msg)
3537           cp_parser_error (parser, error_msg);
3538         return decl;
3539       }
3540
3541       /* Anything else is an error.  */
3542     default:
3543       /* ...unless we have an Objective-C++ message or string literal,
3544          that is.  */
3545       if (c_dialect_objc ()
3546           && (token->type == CPP_OPEN_SQUARE
3547               || token->type == CPP_OBJC_STRING))
3548         return cp_parser_objc_expression (parser);
3549
3550       cp_parser_error (parser, "expected primary-expression");
3551       return error_mark_node;
3552     }
3553 }
3554
3555 /* Parse an id-expression.
3556
3557    id-expression:
3558      unqualified-id
3559      qualified-id
3560
3561    qualified-id:
3562      :: [opt] nested-name-specifier template [opt] unqualified-id
3563      :: identifier
3564      :: operator-function-id
3565      :: template-id
3566
3567    Return a representation of the unqualified portion of the
3568    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3569    a `::' or nested-name-specifier.
3570
3571    Often, if the id-expression was a qualified-id, the caller will
3572    want to make a SCOPE_REF to represent the qualified-id.  This
3573    function does not do this in order to avoid wastefully creating
3574    SCOPE_REFs when they are not required.
3575
3576    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3577    `template' keyword.
3578
3579    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3580    uninstantiated templates.
3581
3582    If *TEMPLATE_P is non-NULL, it is set to true iff the
3583    `template' keyword is used to explicitly indicate that the entity
3584    named is a template.
3585
3586    If DECLARATOR_P is true, the id-expression is appearing as part of
3587    a declarator, rather than as part of an expression.  */
3588
3589 static tree
3590 cp_parser_id_expression (cp_parser *parser,
3591                          bool template_keyword_p,
3592                          bool check_dependency_p,
3593                          bool *template_p,
3594                          bool declarator_p,
3595                          bool optional_p)
3596 {
3597   bool global_scope_p;
3598   bool nested_name_specifier_p;
3599
3600   /* Assume the `template' keyword was not used.  */
3601   if (template_p)
3602     *template_p = template_keyword_p;
3603
3604   /* Look for the optional `::' operator.  */
3605   global_scope_p
3606     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3607        != NULL_TREE);
3608   /* Look for the optional nested-name-specifier.  */
3609   nested_name_specifier_p
3610     = (cp_parser_nested_name_specifier_opt (parser,
3611                                             /*typename_keyword_p=*/false,
3612                                             check_dependency_p,
3613                                             /*type_p=*/false,
3614                                             declarator_p)
3615        != NULL_TREE);
3616   /* If there is a nested-name-specifier, then we are looking at
3617      the first qualified-id production.  */
3618   if (nested_name_specifier_p)
3619     {
3620       tree saved_scope;
3621       tree saved_object_scope;
3622       tree saved_qualifying_scope;
3623       tree unqualified_id;
3624       bool is_template;
3625
3626       /* See if the next token is the `template' keyword.  */
3627       if (!template_p)
3628         template_p = &is_template;
3629       *template_p = cp_parser_optional_template_keyword (parser);
3630       /* Name lookup we do during the processing of the
3631          unqualified-id might obliterate SCOPE.  */
3632       saved_scope = parser->scope;
3633       saved_object_scope = parser->object_scope;
3634       saved_qualifying_scope = parser->qualifying_scope;
3635       /* Process the final unqualified-id.  */
3636       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3637                                                  check_dependency_p,
3638                                                  declarator_p,
3639                                                  /*optional_p=*/false);
3640       /* Restore the SAVED_SCOPE for our caller.  */
3641       parser->scope = saved_scope;
3642       parser->object_scope = saved_object_scope;
3643       parser->qualifying_scope = saved_qualifying_scope;
3644
3645       return unqualified_id;
3646     }
3647   /* Otherwise, if we are in global scope, then we are looking at one
3648      of the other qualified-id productions.  */
3649   else if (global_scope_p)
3650     {
3651       cp_token *token;
3652       tree id;
3653
3654       /* Peek at the next token.  */
3655       token = cp_lexer_peek_token (parser->lexer);
3656
3657       /* If it's an identifier, and the next token is not a "<", then
3658          we can avoid the template-id case.  This is an optimization
3659          for this common case.  */
3660       if (token->type == CPP_NAME
3661           && !cp_parser_nth_token_starts_template_argument_list_p
3662                (parser, 2))
3663         return cp_parser_identifier (parser);
3664
3665       cp_parser_parse_tentatively (parser);
3666       /* Try a template-id.  */
3667       id = cp_parser_template_id (parser,
3668                                   /*template_keyword_p=*/false,
3669                                   /*check_dependency_p=*/true,
3670                                   declarator_p);
3671       /* If that worked, we're done.  */
3672       if (cp_parser_parse_definitely (parser))
3673         return id;
3674
3675       /* Peek at the next token.  (Changes in the token buffer may
3676          have invalidated the pointer obtained above.)  */
3677       token = cp_lexer_peek_token (parser->lexer);
3678
3679       switch (token->type)
3680         {
3681         case CPP_NAME:
3682           return cp_parser_identifier (parser);
3683
3684         case CPP_KEYWORD:
3685           if (token->keyword == RID_OPERATOR)
3686             return cp_parser_operator_function_id (parser);
3687           /* Fall through.  */
3688
3689         default:
3690           cp_parser_error (parser, "expected id-expression");
3691           return error_mark_node;
3692         }
3693     }
3694   else
3695     return cp_parser_unqualified_id (parser, template_keyword_p,
3696                                      /*check_dependency_p=*/true,
3697                                      declarator_p,
3698                                      optional_p);
3699 }
3700
3701 /* Parse an unqualified-id.
3702
3703    unqualified-id:
3704      identifier
3705      operator-function-id
3706      conversion-function-id
3707      ~ class-name
3708      template-id
3709
3710    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3711    keyword, in a construct like `A::template ...'.
3712
3713    Returns a representation of unqualified-id.  For the `identifier'
3714    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3715    production a BIT_NOT_EXPR is returned; the operand of the
3716    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3717    other productions, see the documentation accompanying the
3718    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3719    names are looked up in uninstantiated templates.  If DECLARATOR_P
3720    is true, the unqualified-id is appearing as part of a declarator,
3721    rather than as part of an expression.  */
3722
3723 static tree
3724 cp_parser_unqualified_id (cp_parser* parser,
3725                           bool template_keyword_p,
3726                           bool check_dependency_p,
3727                           bool declarator_p,
3728                           bool optional_p)
3729 {
3730   cp_token *token;
3731
3732   /* Peek at the next token.  */
3733   token = cp_lexer_peek_token (parser->lexer);
3734
3735   switch (token->type)
3736     {
3737     case CPP_NAME:
3738       {
3739         tree id;
3740
3741         /* We don't know yet whether or not this will be a
3742            template-id.  */
3743         cp_parser_parse_tentatively (parser);
3744         /* Try a template-id.  */
3745         id = cp_parser_template_id (parser, template_keyword_p,
3746                                     check_dependency_p,
3747                                     declarator_p);
3748         /* If it worked, we're done.  */
3749         if (cp_parser_parse_definitely (parser))
3750           return id;
3751         /* Otherwise, it's an ordinary identifier.  */
3752         return cp_parser_identifier (parser);
3753       }
3754
3755     case CPP_TEMPLATE_ID:
3756       return cp_parser_template_id (parser, template_keyword_p,
3757                                     check_dependency_p,
3758                                     declarator_p);
3759
3760     case CPP_COMPL:
3761       {
3762         tree type_decl;
3763         tree qualifying_scope;
3764         tree object_scope;
3765         tree scope;
3766         bool done;
3767
3768         /* Consume the `~' token.  */
3769         cp_lexer_consume_token (parser->lexer);
3770         /* Parse the class-name.  The standard, as written, seems to
3771            say that:
3772
3773              template <typename T> struct S { ~S (); };
3774              template <typename T> S<T>::~S() {}
3775
3776            is invalid, since `~' must be followed by a class-name, but
3777            `S<T>' is dependent, and so not known to be a class.
3778            That's not right; we need to look in uninstantiated
3779            templates.  A further complication arises from:
3780
3781              template <typename T> void f(T t) {
3782                t.T::~T();
3783              }
3784
3785            Here, it is not possible to look up `T' in the scope of `T'
3786            itself.  We must look in both the current scope, and the
3787            scope of the containing complete expression.
3788
3789            Yet another issue is:
3790
3791              struct S {
3792                int S;
3793                ~S();
3794              };
3795
3796              S::~S() {}
3797
3798            The standard does not seem to say that the `S' in `~S'
3799            should refer to the type `S' and not the data member
3800            `S::S'.  */
3801
3802         /* DR 244 says that we look up the name after the "~" in the
3803            same scope as we looked up the qualifying name.  That idea
3804            isn't fully worked out; it's more complicated than that.  */
3805         scope = parser->scope;
3806         object_scope = parser->object_scope;
3807         qualifying_scope = parser->qualifying_scope;
3808
3809         /* Check for invalid scopes.  */
3810         if (scope == error_mark_node)
3811           {
3812             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813               cp_lexer_consume_token (parser->lexer);
3814             return error_mark_node;
3815           }
3816         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3817           {
3818             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3819               error ("%Hscope %qT before %<~%> is not a class-name",
3820                      &token->location, scope);
3821             cp_parser_simulate_error (parser);
3822             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3823               cp_lexer_consume_token (parser->lexer);
3824             return error_mark_node;
3825           }
3826         gcc_assert (!scope || TYPE_P (scope));
3827
3828         /* If the name is of the form "X::~X" it's OK.  */
3829         token = cp_lexer_peek_token (parser->lexer);
3830         if (scope
3831             && token->type == CPP_NAME
3832             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3833                 == CPP_OPEN_PAREN)
3834             && constructor_name_p (token->u.value, scope))
3835           {
3836             cp_lexer_consume_token (parser->lexer);
3837             return build_nt (BIT_NOT_EXPR, scope);
3838           }
3839
3840         /* If there was an explicit qualification (S::~T), first look
3841            in the scope given by the qualification (i.e., S).  */
3842         done = false;
3843         type_decl = NULL_TREE;
3844         if (scope)
3845           {
3846             cp_parser_parse_tentatively (parser);
3847             type_decl = cp_parser_class_name (parser,
3848                                               /*typename_keyword_p=*/false,
3849                                               /*template_keyword_p=*/false,
3850                                               none_type,
3851                                               /*check_dependency=*/false,
3852                                               /*class_head_p=*/false,
3853                                               declarator_p);
3854             if (cp_parser_parse_definitely (parser))
3855               done = true;
3856           }
3857         /* In "N::S::~S", look in "N" as well.  */
3858         if (!done && scope && qualifying_scope)
3859           {
3860             cp_parser_parse_tentatively (parser);
3861             parser->scope = qualifying_scope;
3862             parser->object_scope = NULL_TREE;
3863             parser->qualifying_scope = NULL_TREE;
3864             type_decl
3865               = cp_parser_class_name (parser,
3866                                       /*typename_keyword_p=*/false,
3867                                       /*template_keyword_p=*/false,
3868                                       none_type,
3869                                       /*check_dependency=*/false,
3870                                       /*class_head_p=*/false,
3871                                       declarator_p);
3872             if (cp_parser_parse_definitely (parser))
3873               done = true;
3874           }
3875         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3876         else if (!done && object_scope)
3877           {
3878             cp_parser_parse_tentatively (parser);
3879             parser->scope = object_scope;
3880             parser->object_scope = NULL_TREE;
3881             parser->qualifying_scope = NULL_TREE;
3882             type_decl
3883               = cp_parser_class_name (parser,
3884                                       /*typename_keyword_p=*/false,
3885                                       /*template_keyword_p=*/false,
3886                                       none_type,
3887                                       /*check_dependency=*/false,
3888                                       /*class_head_p=*/false,
3889                                       declarator_p);
3890             if (cp_parser_parse_definitely (parser))
3891               done = true;
3892           }
3893         /* Look in the surrounding context.  */
3894         if (!done)
3895           {
3896             parser->scope = NULL_TREE;
3897             parser->object_scope = NULL_TREE;
3898             parser->qualifying_scope = NULL_TREE;
3899             if (processing_template_decl)
3900               cp_parser_parse_tentatively (parser);
3901             type_decl
3902               = cp_parser_class_name (parser,
3903                                       /*typename_keyword_p=*/false,
3904                                       /*template_keyword_p=*/false,
3905                                       none_type,
3906                                       /*check_dependency=*/false,
3907                                       /*class_head_p=*/false,
3908                                       declarator_p);
3909             if (processing_template_decl
3910                 && ! cp_parser_parse_definitely (parser))
3911               {
3912                 /* We couldn't find a type with this name, so just accept
3913                    it and check for a match at instantiation time.  */
3914                 type_decl = cp_parser_identifier (parser);
3915                 if (type_decl != error_mark_node)
3916                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3917                 return type_decl;
3918               }
3919           }
3920         /* If an error occurred, assume that the name of the
3921            destructor is the same as the name of the qualifying
3922            class.  That allows us to keep parsing after running
3923            into ill-formed destructor names.  */
3924         if (type_decl == error_mark_node && scope)
3925           return build_nt (BIT_NOT_EXPR, scope);
3926         else if (type_decl == error_mark_node)
3927           return error_mark_node;
3928
3929         /* Check that destructor name and scope match.  */
3930         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3931           {
3932             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3933               error ("%Hdeclaration of %<~%T%> as member of %qT",
3934                      &token->location, type_decl, scope);
3935             cp_parser_simulate_error (parser);
3936             return error_mark_node;
3937           }
3938
3939         /* [class.dtor]
3940
3941            A typedef-name that names a class shall not be used as the
3942            identifier in the declarator for a destructor declaration.  */
3943         if (declarator_p
3944             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3945             && !DECL_SELF_REFERENCE_P (type_decl)
3946             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3947           error ("%Htypedef-name %qD used as destructor declarator",
3948                  &token->location, type_decl);
3949
3950         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3951       }
3952
3953     case CPP_KEYWORD:
3954       if (token->keyword == RID_OPERATOR)
3955         {
3956           tree id;
3957
3958           /* This could be a template-id, so we try that first.  */
3959           cp_parser_parse_tentatively (parser);
3960           /* Try a template-id.  */
3961           id = cp_parser_template_id (parser, template_keyword_p,
3962                                       /*check_dependency_p=*/true,
3963                                       declarator_p);
3964           /* If that worked, we're done.  */
3965           if (cp_parser_parse_definitely (parser))
3966             return id;
3967           /* We still don't know whether we're looking at an
3968              operator-function-id or a conversion-function-id.  */
3969           cp_parser_parse_tentatively (parser);
3970           /* Try an operator-function-id.  */
3971           id = cp_parser_operator_function_id (parser);
3972           /* If that didn't work, try a conversion-function-id.  */
3973           if (!cp_parser_parse_definitely (parser))
3974             id = cp_parser_conversion_function_id (parser);
3975
3976           return id;
3977         }
3978       /* Fall through.  */
3979
3980     default:
3981       if (optional_p)
3982         return NULL_TREE;
3983       cp_parser_error (parser, "expected unqualified-id");
3984       return error_mark_node;
3985     }
3986 }
3987
3988 /* Parse an (optional) nested-name-specifier.
3989
3990    nested-name-specifier: [C++98]
3991      class-or-namespace-name :: nested-name-specifier [opt]
3992      class-or-namespace-name :: template nested-name-specifier [opt]
3993
3994    nested-name-specifier: [C++0x]
3995      type-name ::
3996      namespace-name ::
3997      nested-name-specifier identifier ::
3998      nested-name-specifier template [opt] simple-template-id ::
3999
4000    PARSER->SCOPE should be set appropriately before this function is
4001    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4002    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4003    in name lookups.
4004
4005    Sets PARSER->SCOPE to the class (TYPE) or namespace
4006    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4007    it unchanged if there is no nested-name-specifier.  Returns the new
4008    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4009
4010    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4011    part of a declaration and/or decl-specifier.  */
4012
4013 static tree
4014 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4015                                      bool typename_keyword_p,
4016                                      bool check_dependency_p,
4017                                      bool type_p,
4018                                      bool is_declaration)
4019 {
4020   bool success = false;
4021   cp_token_position start = 0;
4022   cp_token *token;
4023
4024   /* Remember where the nested-name-specifier starts.  */
4025   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4026     {
4027       start = cp_lexer_token_position (parser->lexer, false);
4028       push_deferring_access_checks (dk_deferred);
4029     }
4030
4031   while (true)
4032     {
4033       tree new_scope;
4034       tree old_scope;
4035       tree saved_qualifying_scope;
4036       bool template_keyword_p;
4037
4038       /* Spot cases that cannot be the beginning of a
4039          nested-name-specifier.  */
4040       token = cp_lexer_peek_token (parser->lexer);
4041
4042       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4043          the already parsed nested-name-specifier.  */
4044       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4045         {
4046           /* Grab the nested-name-specifier and continue the loop.  */
4047           cp_parser_pre_parsed_nested_name_specifier (parser);
4048           /* If we originally encountered this nested-name-specifier
4049              with IS_DECLARATION set to false, we will not have
4050              resolved TYPENAME_TYPEs, so we must do so here.  */
4051           if (is_declaration
4052               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4053             {
4054               new_scope = resolve_typename_type (parser->scope,
4055                                                  /*only_current_p=*/false);
4056               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4057                 parser->scope = new_scope;
4058             }
4059           success = true;
4060           continue;
4061         }
4062
4063       /* Spot cases that cannot be the beginning of a
4064          nested-name-specifier.  On the second and subsequent times
4065          through the loop, we look for the `template' keyword.  */
4066       if (success && token->keyword == RID_TEMPLATE)
4067         ;
4068       /* A template-id can start a nested-name-specifier.  */
4069       else if (token->type == CPP_TEMPLATE_ID)
4070         ;
4071       else
4072         {
4073           /* If the next token is not an identifier, then it is
4074              definitely not a type-name or namespace-name.  */
4075           if (token->type != CPP_NAME)
4076             break;
4077           /* If the following token is neither a `<' (to begin a
4078              template-id), nor a `::', then we are not looking at a
4079              nested-name-specifier.  */
4080           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4081           if (token->type != CPP_SCOPE
4082               && !cp_parser_nth_token_starts_template_argument_list_p
4083                   (parser, 2))
4084             break;
4085         }
4086
4087       /* The nested-name-specifier is optional, so we parse
4088          tentatively.  */
4089       cp_parser_parse_tentatively (parser);
4090
4091       /* Look for the optional `template' keyword, if this isn't the
4092          first time through the loop.  */
4093       if (success)
4094         template_keyword_p = cp_parser_optional_template_keyword (parser);
4095       else
4096         template_keyword_p = false;
4097
4098       /* Save the old scope since the name lookup we are about to do
4099          might destroy it.  */
4100       old_scope = parser->scope;
4101       saved_qualifying_scope = parser->qualifying_scope;
4102       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4103          look up names in "X<T>::I" in order to determine that "Y" is
4104          a template.  So, if we have a typename at this point, we make
4105          an effort to look through it.  */
4106       if (is_declaration
4107           && !typename_keyword_p
4108           && parser->scope
4109           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4110         parser->scope = resolve_typename_type (parser->scope,
4111                                                /*only_current_p=*/false);
4112       /* Parse the qualifying entity.  */
4113       new_scope
4114         = cp_parser_qualifying_entity (parser,
4115                                        typename_keyword_p,
4116                                        template_keyword_p,
4117                                        check_dependency_p,
4118                                        type_p,
4119                                        is_declaration);
4120       /* Look for the `::' token.  */
4121       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4122
4123       /* If we found what we wanted, we keep going; otherwise, we're
4124          done.  */
4125       if (!cp_parser_parse_definitely (parser))
4126         {
4127           bool error_p = false;
4128
4129           /* Restore the OLD_SCOPE since it was valid before the
4130              failed attempt at finding the last
4131              class-or-namespace-name.  */
4132           parser->scope = old_scope;
4133           parser->qualifying_scope = saved_qualifying_scope;
4134           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4135             break;
4136           /* If the next token is an identifier, and the one after
4137              that is a `::', then any valid interpretation would have
4138              found a class-or-namespace-name.  */
4139           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4140                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4141                      == CPP_SCOPE)
4142                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4143                      != CPP_COMPL))
4144             {
4145               token = cp_lexer_consume_token (parser->lexer);
4146               if (!error_p)
4147                 {
4148                   if (!token->ambiguous_p)
4149                     {
4150                       tree decl;
4151                       tree ambiguous_decls;
4152
4153                       decl = cp_parser_lookup_name (parser, token->u.value,
4154                                                     none_type,
4155                                                     /*is_template=*/false,
4156                                                     /*is_namespace=*/false,
4157                                                     /*check_dependency=*/true,
4158                                                     &ambiguous_decls,
4159                                                     token->location);
4160                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4161                         error ("%H%qD used without template parameters",
4162                                &token->location, decl);
4163                       else if (ambiguous_decls)
4164                         {
4165                           error ("%Hreference to %qD is ambiguous",
4166                                  &token->location, token->u.value);
4167                           print_candidates (ambiguous_decls);
4168                           decl = error_mark_node;
4169                         }
4170                       else
4171                         {
4172                           const char* msg = "is not a class or namespace";
4173                           if (cxx_dialect != cxx98)
4174                             msg = "is not a class, namespace, or enumeration";
4175                           cp_parser_name_lookup_error
4176                             (parser, token->u.value, decl, msg,
4177                              token->location);
4178                         }
4179                     }
4180                   parser->scope = error_mark_node;
4181                   error_p = true;
4182                   /* Treat this as a successful nested-name-specifier
4183                      due to:
4184
4185                      [basic.lookup.qual]
4186
4187                      If the name found is not a class-name (clause
4188                      _class_) or namespace-name (_namespace.def_), the
4189                      program is ill-formed.  */
4190                   success = true;
4191                 }
4192               cp_lexer_consume_token (parser->lexer);
4193             }
4194           break;
4195         }
4196       /* We've found one valid nested-name-specifier.  */
4197       success = true;
4198       /* Name lookup always gives us a DECL.  */
4199       if (TREE_CODE (new_scope) == TYPE_DECL)
4200         new_scope = TREE_TYPE (new_scope);
4201       /* Uses of "template" must be followed by actual templates.  */
4202       if (template_keyword_p
4203           && !(CLASS_TYPE_P (new_scope)
4204                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4205                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4206                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4207           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4208                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4209                    == TEMPLATE_ID_EXPR)))
4210         permerror (input_location, TYPE_P (new_scope)
4211                    ? "%qT is not a template"
4212                    : "%qD is not a template",
4213                    new_scope);
4214       /* If it is a class scope, try to complete it; we are about to
4215          be looking up names inside the class.  */
4216       if (TYPE_P (new_scope)
4217           /* Since checking types for dependency can be expensive,
4218              avoid doing it if the type is already complete.  */
4219           && !COMPLETE_TYPE_P (new_scope)
4220           /* Do not try to complete dependent types.  */
4221           && !dependent_type_p (new_scope))
4222         {
4223           new_scope = complete_type (new_scope);
4224           /* If it is a typedef to current class, use the current
4225              class instead, as the typedef won't have any names inside
4226              it yet.  */
4227           if (!COMPLETE_TYPE_P (new_scope)
4228               && currently_open_class (new_scope))
4229             new_scope = TYPE_MAIN_VARIANT (new_scope);
4230         }
4231       /* Make sure we look in the right scope the next time through
4232          the loop.  */
4233       parser->scope = new_scope;
4234     }
4235
4236   /* If parsing tentatively, replace the sequence of tokens that makes
4237      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4238      token.  That way, should we re-parse the token stream, we will
4239      not have to repeat the effort required to do the parse, nor will
4240      we issue duplicate error messages.  */
4241   if (success && start)
4242     {
4243       cp_token *token;
4244
4245       token = cp_lexer_token_at (parser->lexer, start);
4246       /* Reset the contents of the START token.  */
4247       token->type = CPP_NESTED_NAME_SPECIFIER;
4248       /* Retrieve any deferred checks.  Do not pop this access checks yet
4249          so the memory will not be reclaimed during token replacing below.  */
4250       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4251       token->u.tree_check_value->value = parser->scope;
4252       token->u.tree_check_value->checks = get_deferred_access_checks ();
4253       token->u.tree_check_value->qualifying_scope =
4254         parser->qualifying_scope;
4255       token->keyword = RID_MAX;
4256
4257       /* Purge all subsequent tokens.  */
4258       cp_lexer_purge_tokens_after (parser->lexer, start);
4259     }
4260
4261   if (start)
4262     pop_to_parent_deferring_access_checks ();
4263
4264   return success ? parser->scope : NULL_TREE;
4265 }
4266
4267 /* Parse a nested-name-specifier.  See
4268    cp_parser_nested_name_specifier_opt for details.  This function
4269    behaves identically, except that it will an issue an error if no
4270    nested-name-specifier is present.  */
4271
4272 static tree
4273 cp_parser_nested_name_specifier (cp_parser *parser,
4274                                  bool typename_keyword_p,
4275                                  bool check_dependency_p,
4276                                  bool type_p,
4277                                  bool is_declaration)
4278 {
4279   tree scope;
4280
4281   /* Look for the nested-name-specifier.  */
4282   scope = cp_parser_nested_name_specifier_opt (parser,
4283                                                typename_keyword_p,
4284                                                check_dependency_p,
4285                                                type_p,
4286                                                is_declaration);
4287   /* If it was not present, issue an error message.  */
4288   if (!scope)
4289     {
4290       cp_parser_error (parser, "expected nested-name-specifier");
4291       parser->scope = NULL_TREE;
4292     }
4293
4294   return scope;
4295 }
4296
4297 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4298    this is either a class-name or a namespace-name (which corresponds
4299    to the class-or-namespace-name production in the grammar). For
4300    C++0x, it can also be a type-name that refers to an enumeration
4301    type.
4302
4303    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4304    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4305    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4306    TYPE_P is TRUE iff the next name should be taken as a class-name,
4307    even the same name is declared to be another entity in the same
4308    scope.
4309
4310    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4311    specified by the class-or-namespace-name.  If neither is found the
4312    ERROR_MARK_NODE is returned.  */
4313
4314 static tree
4315 cp_parser_qualifying_entity (cp_parser *parser,
4316                              bool typename_keyword_p,
4317                              bool template_keyword_p,
4318                              bool check_dependency_p,
4319                              bool type_p,
4320                              bool is_declaration)
4321 {
4322   tree saved_scope;
4323   tree saved_qualifying_scope;
4324   tree saved_object_scope;
4325   tree scope;
4326   bool only_class_p;
4327   bool successful_parse_p;
4328
4329   /* Before we try to parse the class-name, we must save away the
4330      current PARSER->SCOPE since cp_parser_class_name will destroy
4331      it.  */
4332   saved_scope = parser->scope;
4333   saved_qualifying_scope = parser->qualifying_scope;
4334   saved_object_scope = parser->object_scope;
4335   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4336      there is no need to look for a namespace-name.  */
4337   only_class_p = template_keyword_p 
4338     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4339   if (!only_class_p)
4340     cp_parser_parse_tentatively (parser);
4341   scope = cp_parser_class_name (parser,
4342                                 typename_keyword_p,
4343                                 template_keyword_p,
4344                                 type_p ? class_type : none_type,
4345                                 check_dependency_p,
4346                                 /*class_head_p=*/false,
4347                                 is_declaration);
4348   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4349   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4350   if (!only_class_p 
4351       && cxx_dialect != cxx98
4352       && !successful_parse_p)
4353     {
4354       /* Restore the saved scope.  */
4355       parser->scope = saved_scope;
4356       parser->qualifying_scope = saved_qualifying_scope;
4357       parser->object_scope = saved_object_scope;
4358
4359       /* Parse tentatively.  */
4360       cp_parser_parse_tentatively (parser);
4361      
4362       /* Parse a typedef-name or enum-name.  */
4363       scope = cp_parser_nonclass_name (parser);
4364       successful_parse_p = cp_parser_parse_definitely (parser);
4365     }
4366   /* If that didn't work, try for a namespace-name.  */
4367   if (!only_class_p && !successful_parse_p)
4368     {
4369       /* Restore the saved scope.  */
4370       parser->scope = saved_scope;
4371       parser->qualifying_scope = saved_qualifying_scope;
4372       parser->object_scope = saved_object_scope;
4373       /* If we are not looking at an identifier followed by the scope
4374          resolution operator, then this is not part of a
4375          nested-name-specifier.  (Note that this function is only used
4376          to parse the components of a nested-name-specifier.)  */
4377       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4378           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4379         return error_mark_node;
4380       scope = cp_parser_namespace_name (parser);
4381     }
4382
4383   return scope;
4384 }
4385
4386 /* Parse a postfix-expression.
4387
4388    postfix-expression:
4389      primary-expression
4390      postfix-expression [ expression ]
4391      postfix-expression ( expression-list [opt] )
4392      simple-type-specifier ( expression-list [opt] )
4393      typename :: [opt] nested-name-specifier identifier
4394        ( expression-list [opt] )
4395      typename :: [opt] nested-name-specifier template [opt] template-id
4396        ( expression-list [opt] )
4397      postfix-expression . template [opt] id-expression
4398      postfix-expression -> template [opt] id-expression
4399      postfix-expression . pseudo-destructor-name
4400      postfix-expression -> pseudo-destructor-name
4401      postfix-expression ++
4402      postfix-expression --
4403      dynamic_cast < type-id > ( expression )
4404      static_cast < type-id > ( expression )
4405      reinterpret_cast < type-id > ( expression )
4406      const_cast < type-id > ( expression )
4407      typeid ( expression )
4408      typeid ( type-id )
4409
4410    GNU Extension:
4411
4412    postfix-expression:
4413      ( type-id ) { initializer-list , [opt] }
4414
4415    This extension is a GNU version of the C99 compound-literal
4416    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4417    but they are essentially the same concept.)
4418
4419    If ADDRESS_P is true, the postfix expression is the operand of the
4420    `&' operator.  CAST_P is true if this expression is the target of a
4421    cast.
4422
4423    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4424    class member access expressions [expr.ref].
4425
4426    Returns a representation of the expression.  */
4427
4428 static tree
4429 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4430                               bool member_access_only_p,
4431                               cp_id_kind * pidk_return)
4432 {
4433   cp_token *token;
4434   enum rid keyword;
4435   cp_id_kind idk = CP_ID_KIND_NONE;
4436   tree postfix_expression = NULL_TREE;
4437   bool is_member_access = false;
4438
4439   /* Peek at the next token.  */
4440   token = cp_lexer_peek_token (parser->lexer);
4441   /* Some of the productions are determined by keywords.  */
4442   keyword = token->keyword;
4443   switch (keyword)
4444     {
4445     case RID_DYNCAST:
4446     case RID_STATCAST:
4447     case RID_REINTCAST:
4448     case RID_CONSTCAST:
4449       {
4450         tree type;
4451         tree expression;
4452         const char *saved_message;
4453
4454         /* All of these can be handled in the same way from the point
4455            of view of parsing.  Begin by consuming the token
4456            identifying the cast.  */
4457         cp_lexer_consume_token (parser->lexer);
4458
4459         /* New types cannot be defined in the cast.  */
4460         saved_message = parser->type_definition_forbidden_message;
4461         parser->type_definition_forbidden_message
4462           = "types may not be defined in casts";
4463
4464         /* Look for the opening `<'.  */
4465         cp_parser_require (parser, CPP_LESS, "%<<%>");
4466         /* Parse the type to which we are casting.  */
4467         type = cp_parser_type_id (parser);
4468         /* Look for the closing `>'.  */
4469         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4470         /* Restore the old message.  */
4471         parser->type_definition_forbidden_message = saved_message;
4472
4473         /* And the expression which is being cast.  */
4474         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4475         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4476         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4477
4478         /* Only type conversions to integral or enumeration types
4479            can be used in constant-expressions.  */
4480         if (!cast_valid_in_integral_constant_expression_p (type)
4481             && (cp_parser_non_integral_constant_expression
4482                 (parser,
4483                  "a cast to a type other than an integral or "
4484                  "enumeration type")))
4485           return error_mark_node;
4486
4487         switch (keyword)
4488           {
4489           case RID_DYNCAST:
4490             postfix_expression
4491               = build_dynamic_cast (type, expression, tf_warning_or_error);
4492             break;
4493           case RID_STATCAST:
4494             postfix_expression
4495               = build_static_cast (type, expression, tf_warning_or_error);
4496             break;
4497           case RID_REINTCAST:
4498             postfix_expression
4499               = build_reinterpret_cast (type, expression, 
4500                                         tf_warning_or_error);
4501             break;
4502           case RID_CONSTCAST:
4503             postfix_expression
4504               = build_const_cast (type, expression, tf_warning_or_error);
4505             break;
4506           default:
4507             gcc_unreachable ();
4508           }
4509       }
4510       break;
4511
4512     case RID_TYPEID:
4513       {
4514         tree type;
4515         const char *saved_message;
4516         bool saved_in_type_id_in_expr_p;
4517
4518         /* Consume the `typeid' token.  */
4519         cp_lexer_consume_token (parser->lexer);
4520         /* Look for the `(' token.  */
4521         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4522         /* Types cannot be defined in a `typeid' expression.  */
4523         saved_message = parser->type_definition_forbidden_message;
4524         parser->type_definition_forbidden_message
4525           = "types may not be defined in a %<typeid%> expression";
4526         /* We can't be sure yet whether we're looking at a type-id or an
4527            expression.  */
4528         cp_parser_parse_tentatively (parser);
4529         /* Try a type-id first.  */
4530         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4531         parser->in_type_id_in_expr_p = true;
4532         type = cp_parser_type_id (parser);
4533         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4534         /* Look for the `)' token.  Otherwise, we can't be sure that
4535            we're not looking at an expression: consider `typeid (int
4536            (3))', for example.  */
4537         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4538         /* If all went well, simply lookup the type-id.  */
4539         if (cp_parser_parse_definitely (parser))
4540           postfix_expression = get_typeid (type);
4541         /* Otherwise, fall back to the expression variant.  */
4542         else
4543           {
4544             tree expression;
4545
4546             /* Look for an expression.  */
4547             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4548             /* Compute its typeid.  */
4549             postfix_expression = build_typeid (expression);
4550             /* Look for the `)' token.  */
4551             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4552           }
4553         /* Restore the saved message.  */
4554         parser->type_definition_forbidden_message = saved_message;
4555         /* `typeid' may not appear in an integral constant expression.  */
4556         if (cp_parser_non_integral_constant_expression(parser,
4557                                                        "%<typeid%> operator"))
4558           return error_mark_node;
4559       }
4560       break;
4561
4562     case RID_TYPENAME:
4563       {
4564         tree type;
4565         /* The syntax permitted here is the same permitted for an
4566            elaborated-type-specifier.  */
4567         type = cp_parser_elaborated_type_specifier (parser,
4568                                                     /*is_friend=*/false,
4569                                                     /*is_declaration=*/false);
4570         postfix_expression = cp_parser_functional_cast (parser, type);
4571       }
4572       break;
4573
4574     default:
4575       {
4576         tree type;
4577
4578         /* If the next thing is a simple-type-specifier, we may be
4579            looking at a functional cast.  We could also be looking at
4580            an id-expression.  So, we try the functional cast, and if
4581            that doesn't work we fall back to the primary-expression.  */
4582         cp_parser_parse_tentatively (parser);
4583         /* Look for the simple-type-specifier.  */
4584         type = cp_parser_simple_type_specifier (parser,
4585                                                 /*decl_specs=*/NULL,
4586                                                 CP_PARSER_FLAGS_NONE);
4587         /* Parse the cast itself.  */
4588         if (!cp_parser_error_occurred (parser))
4589           postfix_expression
4590             = cp_parser_functional_cast (parser, type);
4591         /* If that worked, we're done.  */
4592         if (cp_parser_parse_definitely (parser))
4593           break;
4594
4595         /* If the functional-cast didn't work out, try a
4596            compound-literal.  */
4597         if (cp_parser_allow_gnu_extensions_p (parser)
4598             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4599           {
4600             VEC(constructor_elt,gc) *initializer_list = NULL;
4601             bool saved_in_type_id_in_expr_p;
4602
4603             cp_parser_parse_tentatively (parser);
4604             /* Consume the `('.  */
4605             cp_lexer_consume_token (parser->lexer);
4606             /* Parse the type.  */
4607             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4608             parser->in_type_id_in_expr_p = true;
4609             type = cp_parser_type_id (parser);
4610             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4611             /* Look for the `)'.  */
4612             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4613             /* Look for the `{'.  */
4614             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4615             /* If things aren't going well, there's no need to
4616                keep going.  */
4617             if (!cp_parser_error_occurred (parser))
4618               {
4619                 bool non_constant_p;
4620                 /* Parse the initializer-list.  */
4621                 initializer_list
4622                   = cp_parser_initializer_list (parser, &non_constant_p);
4623                 /* Allow a trailing `,'.  */
4624                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4625                   cp_lexer_consume_token (parser->lexer);
4626                 /* Look for the final `}'.  */
4627                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4628               }
4629             /* If that worked, we're definitely looking at a
4630                compound-literal expression.  */
4631             if (cp_parser_parse_definitely (parser))
4632               {
4633                 /* Warn the user that a compound literal is not
4634                    allowed in standard C++.  */
4635                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4636                 /* For simplicity, we disallow compound literals in
4637                    constant-expressions.  We could
4638                    allow compound literals of integer type, whose
4639                    initializer was a constant, in constant
4640                    expressions.  Permitting that usage, as a further
4641                    extension, would not change the meaning of any
4642                    currently accepted programs.  (Of course, as
4643                    compound literals are not part of ISO C++, the
4644                    standard has nothing to say.)  */
4645                 if (cp_parser_non_integral_constant_expression 
4646                     (parser, "non-constant compound literals"))
4647                   {
4648                     postfix_expression = error_mark_node;
4649                     break;
4650                   }
4651                 /* Form the representation of the compound-literal.  */
4652                 postfix_expression
4653                   = (finish_compound_literal
4654                      (type, build_constructor (init_list_type_node,
4655                                                initializer_list)));
4656                 break;
4657               }
4658           }
4659
4660         /* It must be a primary-expression.  */
4661         postfix_expression
4662           = cp_parser_primary_expression (parser, address_p, cast_p,
4663                                           /*template_arg_p=*/false,
4664                                           &idk);
4665       }
4666       break;
4667     }
4668
4669   /* Keep looping until the postfix-expression is complete.  */
4670   while (true)
4671     {
4672       if (idk == CP_ID_KIND_UNQUALIFIED
4673           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4674           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4675         /* It is not a Koenig lookup function call.  */
4676         postfix_expression
4677           = unqualified_name_lookup_error (postfix_expression);
4678
4679       /* Peek at the next token.  */
4680       token = cp_lexer_peek_token (parser->lexer);
4681
4682       switch (token->type)
4683         {
4684         case CPP_OPEN_SQUARE:
4685           postfix_expression
4686             = cp_parser_postfix_open_square_expression (parser,
4687                                                         postfix_expression,
4688                                                         false);
4689           idk = CP_ID_KIND_NONE;
4690           is_member_access = false;
4691           break;
4692
4693         case CPP_OPEN_PAREN:
4694           /* postfix-expression ( expression-list [opt] ) */
4695           {
4696             bool koenig_p;
4697             bool is_builtin_constant_p;
4698             bool saved_integral_constant_expression_p = false;
4699             bool saved_non_integral_constant_expression_p = false;
4700             tree args;
4701
4702             is_member_access = false;
4703
4704             is_builtin_constant_p
4705               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4706             if (is_builtin_constant_p)
4707               {
4708                 /* The whole point of __builtin_constant_p is to allow
4709                    non-constant expressions to appear as arguments.  */
4710                 saved_integral_constant_expression_p
4711                   = parser->integral_constant_expression_p;
4712                 saved_non_integral_constant_expression_p
4713                   = parser->non_integral_constant_expression_p;
4714                 parser->integral_constant_expression_p = false;
4715               }
4716             args = (cp_parser_parenthesized_expression_list
4717                     (parser, /*is_attribute_list=*/false,
4718                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4719                      /*non_constant_p=*/NULL));
4720             if (is_builtin_constant_p)
4721               {
4722                 parser->integral_constant_expression_p
4723                   = saved_integral_constant_expression_p;
4724                 parser->non_integral_constant_expression_p
4725                   = saved_non_integral_constant_expression_p;
4726               }
4727
4728             if (args == error_mark_node)
4729               {
4730                 postfix_expression = error_mark_node;
4731                 break;
4732               }
4733
4734             /* Function calls are not permitted in
4735                constant-expressions.  */
4736             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4737                 && cp_parser_non_integral_constant_expression (parser,
4738                                                                "a function call"))
4739               {
4740                 postfix_expression = error_mark_node;
4741                 break;
4742               }
4743
4744             koenig_p = false;
4745             if (idk == CP_ID_KIND_UNQUALIFIED
4746                 || idk == CP_ID_KIND_TEMPLATE_ID)
4747               {
4748                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4749                   {
4750                     if (args)
4751                       {
4752                         koenig_p = true;
4753                         if (!any_type_dependent_arguments_p (args))
4754                           postfix_expression
4755                             = perform_koenig_lookup (postfix_expression, args);
4756                       }
4757                     else
4758                       postfix_expression
4759                         = unqualified_fn_lookup_error (postfix_expression);
4760                   }
4761                 /* We do not perform argument-dependent lookup if
4762                    normal lookup finds a non-function, in accordance
4763                    with the expected resolution of DR 218.  */
4764                 else if (args && is_overloaded_fn (postfix_expression))
4765                   {
4766                     tree fn = get_first_fn (postfix_expression);
4767
4768                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4769                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4770
4771                     /* Only do argument dependent lookup if regular
4772                        lookup does not find a set of member functions.
4773                        [basic.lookup.koenig]/2a  */
4774                     if (!DECL_FUNCTION_MEMBER_P (fn))
4775                       {
4776                         koenig_p = true;
4777                         if (!any_type_dependent_arguments_p (args))
4778                           postfix_expression
4779                             = perform_koenig_lookup (postfix_expression, args);
4780                       }
4781                   }
4782               }
4783
4784             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4785               {
4786                 tree instance = TREE_OPERAND (postfix_expression, 0);
4787                 tree fn = TREE_OPERAND (postfix_expression, 1);
4788
4789                 if (processing_template_decl
4790                     && (type_dependent_expression_p (instance)
4791                         || (!BASELINK_P (fn)
4792                             && TREE_CODE (fn) != FIELD_DECL)
4793                         || type_dependent_expression_p (fn)
4794                         || any_type_dependent_arguments_p (args)))
4795                   {
4796                     postfix_expression
4797                       = build_nt_call_list (postfix_expression, args);
4798                     break;
4799                   }
4800
4801                 if (BASELINK_P (fn))
4802                   {
4803                   postfix_expression
4804                     = (build_new_method_call
4805                        (instance, fn, args, NULL_TREE,
4806                         (idk == CP_ID_KIND_QUALIFIED
4807                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4808                         /*fn_p=*/NULL,
4809                         tf_warning_or_error));
4810                   }
4811                 else
4812                   postfix_expression
4813                     = finish_call_expr (postfix_expression, args,
4814                                         /*disallow_virtual=*/false,
4815                                         /*koenig_p=*/false,
4816                                         tf_warning_or_error);
4817               }
4818             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4819                      || TREE_CODE (postfix_expression) == MEMBER_REF
4820                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4821               postfix_expression = (build_offset_ref_call_from_tree
4822                                     (postfix_expression, args));
4823             else if (idk == CP_ID_KIND_QUALIFIED)
4824               /* A call to a static class member, or a namespace-scope
4825                  function.  */
4826               postfix_expression
4827                 = finish_call_expr (postfix_expression, args,
4828                                     /*disallow_virtual=*/true,
4829                                     koenig_p,
4830                                     tf_warning_or_error);
4831             else
4832               /* All other function calls.  */
4833               postfix_expression
4834                 = finish_call_expr (postfix_expression, args,
4835                                     /*disallow_virtual=*/false,
4836                                     koenig_p,
4837                                     tf_warning_or_error);
4838
4839             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4840             idk = CP_ID_KIND_NONE;
4841           }
4842           break;
4843
4844         case CPP_DOT:
4845         case CPP_DEREF:
4846           /* postfix-expression . template [opt] id-expression
4847              postfix-expression . pseudo-destructor-name
4848              postfix-expression -> template [opt] id-expression
4849              postfix-expression -> pseudo-destructor-name */
4850
4851           /* Consume the `.' or `->' operator.  */
4852           cp_lexer_consume_token (parser->lexer);
4853
4854           postfix_expression
4855             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4856                                                       postfix_expression,
4857                                                       false, &idk,
4858                                                       token->location);
4859
4860           is_member_access = true;
4861           break;
4862
4863         case CPP_PLUS_PLUS:
4864           /* postfix-expression ++  */
4865           /* Consume the `++' token.  */
4866           cp_lexer_consume_token (parser->lexer);
4867           /* Generate a representation for the complete expression.  */
4868           postfix_expression
4869             = finish_increment_expr (postfix_expression,
4870                                      POSTINCREMENT_EXPR);
4871           /* Increments may not appear in constant-expressions.  */
4872           if (cp_parser_non_integral_constant_expression (parser,
4873                                                           "an increment"))
4874             postfix_expression = error_mark_node;
4875           idk = CP_ID_KIND_NONE;
4876           is_member_access = false;
4877           break;
4878
4879         case CPP_MINUS_MINUS:
4880           /* postfix-expression -- */
4881           /* Consume the `--' token.  */
4882           cp_lexer_consume_token (parser->lexer);
4883           /* Generate a representation for the complete expression.  */
4884           postfix_expression
4885             = finish_increment_expr (postfix_expression,
4886                                      POSTDECREMENT_EXPR);
4887           /* Decrements may not appear in constant-expressions.  */
4888           if (cp_parser_non_integral_constant_expression (parser,
4889                                                           "a decrement"))
4890             postfix_expression = error_mark_node;
4891           idk = CP_ID_KIND_NONE;
4892           is_member_access = false;
4893           break;
4894
4895         default:
4896           if (pidk_return != NULL)
4897             * pidk_return = idk;
4898           if (member_access_only_p)
4899             return is_member_access? postfix_expression : error_mark_node;
4900           else
4901             return postfix_expression;
4902         }
4903     }
4904
4905   /* We should never get here.  */
4906   gcc_unreachable ();
4907   return error_mark_node;
4908 }
4909
4910 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4911    by cp_parser_builtin_offsetof.  We're looking for
4912
4913      postfix-expression [ expression ]
4914
4915    FOR_OFFSETOF is set if we're being called in that context, which
4916    changes how we deal with integer constant expressions.  */
4917
4918 static tree
4919 cp_parser_postfix_open_square_expression (cp_parser *parser,
4920                                           tree postfix_expression,
4921                                           bool for_offsetof)
4922 {
4923   tree index;
4924
4925   /* Consume the `[' token.  */
4926   cp_lexer_consume_token (parser->lexer);
4927
4928   /* Parse the index expression.  */
4929   /* ??? For offsetof, there is a question of what to allow here.  If
4930      offsetof is not being used in an integral constant expression context,
4931      then we *could* get the right answer by computing the value at runtime.
4932      If we are in an integral constant expression context, then we might
4933      could accept any constant expression; hard to say without analysis.
4934      Rather than open the barn door too wide right away, allow only integer
4935      constant expressions here.  */
4936   if (for_offsetof)
4937     index = cp_parser_constant_expression (parser, false, NULL);
4938   else
4939     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4940
4941   /* Look for the closing `]'.  */
4942   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4943
4944   /* Build the ARRAY_REF.  */
4945   postfix_expression = grok_array_decl (postfix_expression, index);
4946
4947   /* When not doing offsetof, array references are not permitted in
4948      constant-expressions.  */
4949   if (!for_offsetof
4950       && (cp_parser_non_integral_constant_expression
4951           (parser, "an array reference")))
4952     postfix_expression = error_mark_node;
4953
4954   return postfix_expression;
4955 }
4956
4957 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4958    by cp_parser_builtin_offsetof.  We're looking for
4959
4960      postfix-expression . template [opt] id-expression
4961      postfix-expression . pseudo-destructor-name
4962      postfix-expression -> template [opt] id-expression
4963      postfix-expression -> pseudo-destructor-name
4964
4965    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4966    limits what of the above we'll actually accept, but nevermind.
4967    TOKEN_TYPE is the "." or "->" token, which will already have been
4968    removed from the stream.  */
4969
4970 static tree
4971 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4972                                         enum cpp_ttype token_type,
4973                                         tree postfix_expression,
4974                                         bool for_offsetof, cp_id_kind *idk,
4975                                         location_t location)
4976 {
4977   tree name;
4978   bool dependent_p;
4979   bool pseudo_destructor_p;
4980   tree scope = NULL_TREE;
4981
4982   /* If this is a `->' operator, dereference the pointer.  */
4983   if (token_type == CPP_DEREF)
4984     postfix_expression = build_x_arrow (postfix_expression);
4985   /* Check to see whether or not the expression is type-dependent.  */
4986   dependent_p = type_dependent_expression_p (postfix_expression);
4987   /* The identifier following the `->' or `.' is not qualified.  */
4988   parser->scope = NULL_TREE;
4989   parser->qualifying_scope = NULL_TREE;
4990   parser->object_scope = NULL_TREE;
4991   *idk = CP_ID_KIND_NONE;
4992
4993   /* Enter the scope corresponding to the type of the object
4994      given by the POSTFIX_EXPRESSION.  */
4995   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4996     {
4997       scope = TREE_TYPE (postfix_expression);
4998       /* According to the standard, no expression should ever have
4999          reference type.  Unfortunately, we do not currently match
5000          the standard in this respect in that our internal representation
5001          of an expression may have reference type even when the standard
5002          says it does not.  Therefore, we have to manually obtain the
5003          underlying type here.  */
5004       scope = non_reference (scope);
5005       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5006       if (scope == unknown_type_node)
5007         {
5008           error ("%H%qE does not have class type", &location, postfix_expression);
5009           scope = NULL_TREE;
5010         }
5011       else
5012         scope = complete_type_or_else (scope, NULL_TREE);
5013       /* Let the name lookup machinery know that we are processing a
5014          class member access expression.  */
5015       parser->context->object_type = scope;
5016       /* If something went wrong, we want to be able to discern that case,
5017          as opposed to the case where there was no SCOPE due to the type
5018          of expression being dependent.  */
5019       if (!scope)
5020         scope = error_mark_node;
5021       /* If the SCOPE was erroneous, make the various semantic analysis
5022          functions exit quickly -- and without issuing additional error
5023          messages.  */
5024       if (scope == error_mark_node)
5025         postfix_expression = error_mark_node;
5026     }
5027
5028   /* Assume this expression is not a pseudo-destructor access.  */
5029   pseudo_destructor_p = false;
5030
5031   /* If the SCOPE is a scalar type, then, if this is a valid program,
5032      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5033      is type dependent, it can be pseudo-destructor-name or something else.
5034      Try to parse it as pseudo-destructor-name first.  */
5035   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5036     {
5037       tree s;
5038       tree type;
5039
5040       cp_parser_parse_tentatively (parser);
5041       /* Parse the pseudo-destructor-name.  */
5042       s = NULL_TREE;
5043       cp_parser_pseudo_destructor_name (parser, &s, &type);
5044       if (dependent_p
5045           && (cp_parser_error_occurred (parser)
5046               || TREE_CODE (type) != TYPE_DECL
5047               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5048         cp_parser_abort_tentative_parse (parser);
5049       else if (cp_parser_parse_definitely (parser))
5050         {
5051           pseudo_destructor_p = true;
5052           postfix_expression
5053             = finish_pseudo_destructor_expr (postfix_expression,
5054                                              s, TREE_TYPE (type));
5055         }
5056     }
5057
5058   if (!pseudo_destructor_p)
5059     {
5060       /* If the SCOPE is not a scalar type, we are looking at an
5061          ordinary class member access expression, rather than a
5062          pseudo-destructor-name.  */
5063       bool template_p;
5064       cp_token *token = cp_lexer_peek_token (parser->lexer);
5065       /* Parse the id-expression.  */
5066       name = (cp_parser_id_expression
5067               (parser,
5068                cp_parser_optional_template_keyword (parser),
5069                /*check_dependency_p=*/true,
5070                &template_p,
5071                /*declarator_p=*/false,
5072                /*optional_p=*/false));
5073       /* In general, build a SCOPE_REF if the member name is qualified.
5074          However, if the name was not dependent and has already been
5075          resolved; there is no need to build the SCOPE_REF.  For example;
5076
5077              struct X { void f(); };
5078              template <typename T> void f(T* t) { t->X::f(); }
5079
5080          Even though "t" is dependent, "X::f" is not and has been resolved
5081          to a BASELINK; there is no need to include scope information.  */
5082
5083       /* But we do need to remember that there was an explicit scope for
5084          virtual function calls.  */
5085       if (parser->scope)
5086         *idk = CP_ID_KIND_QUALIFIED;
5087
5088       /* If the name is a template-id that names a type, we will get a
5089          TYPE_DECL here.  That is invalid code.  */
5090       if (TREE_CODE (name) == TYPE_DECL)
5091         {
5092           error ("%Hinvalid use of %qD", &token->location, name);
5093           postfix_expression = error_mark_node;
5094         }
5095       else
5096         {
5097           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5098             {
5099               name = build_qualified_name (/*type=*/NULL_TREE,
5100                                            parser->scope,
5101                                            name,
5102                                            template_p);
5103               parser->scope = NULL_TREE;
5104               parser->qualifying_scope = NULL_TREE;
5105               parser->object_scope = NULL_TREE;
5106             }
5107           if (scope && name && BASELINK_P (name))
5108             adjust_result_of_qualified_name_lookup
5109               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5110           postfix_expression
5111             = finish_class_member_access_expr (postfix_expression, name,
5112                                                template_p, 
5113                                                tf_warning_or_error);
5114         }
5115     }
5116
5117   /* We no longer need to look up names in the scope of the object on
5118      the left-hand side of the `.' or `->' operator.  */
5119   parser->context->object_type = NULL_TREE;
5120
5121   /* Outside of offsetof, these operators may not appear in
5122      constant-expressions.  */
5123   if (!for_offsetof
5124       && (cp_parser_non_integral_constant_expression
5125           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5126     postfix_expression = error_mark_node;
5127
5128   return postfix_expression;
5129 }
5130
5131 /* Parse a parenthesized expression-list.
5132
5133    expression-list:
5134      assignment-expression
5135      expression-list, assignment-expression
5136
5137    attribute-list:
5138      expression-list
5139      identifier
5140      identifier, expression-list
5141
5142    CAST_P is true if this expression is the target of a cast.
5143
5144    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5145    argument pack.
5146
5147    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5148    representation of an assignment-expression.  Note that a TREE_LIST
5149    is returned even if there is only a single expression in the list.
5150    error_mark_node is returned if the ( and or ) are
5151    missing. NULL_TREE is returned on no expressions. The parentheses
5152    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5153    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5154    indicates whether or not all of the expressions in the list were
5155    constant.  */
5156
5157 static tree
5158 cp_parser_parenthesized_expression_list (cp_parser* parser,
5159                                          bool is_attribute_list,
5160                                          bool cast_p,
5161                                          bool allow_expansion_p,
5162                                          bool *non_constant_p)
5163 {
5164   tree expression_list = NULL_TREE;
5165   bool fold_expr_p = is_attribute_list;
5166   tree identifier = NULL_TREE;
5167   bool saved_greater_than_is_operator_p;
5168
5169   /* Assume all the expressions will be constant.  */
5170   if (non_constant_p)
5171     *non_constant_p = false;
5172
5173   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5174     return error_mark_node;
5175
5176   /* Within a parenthesized expression, a `>' token is always
5177      the greater-than operator.  */
5178   saved_greater_than_is_operator_p
5179     = parser->greater_than_is_operator_p;
5180   parser->greater_than_is_operator_p = true;
5181
5182   /* Consume expressions until there are no more.  */
5183   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5184     while (true)
5185       {
5186         tree expr;
5187
5188         /* At the beginning of attribute lists, check to see if the
5189            next token is an identifier.  */
5190         if (is_attribute_list
5191             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5192           {
5193             cp_token *token;
5194
5195             /* Consume the identifier.  */
5196             token = cp_lexer_consume_token (parser->lexer);
5197             /* Save the identifier.  */
5198             identifier = token->u.value;
5199           }
5200         else
5201           {
5202             bool expr_non_constant_p;
5203
5204             /* Parse the next assignment-expression.  */
5205             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5206               {
5207                 /* A braced-init-list.  */
5208                 maybe_warn_cpp0x ("extended initializer lists");
5209                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5210                 if (non_constant_p && expr_non_constant_p)
5211                   *non_constant_p = true;
5212               }
5213             else if (non_constant_p)
5214               {
5215                 expr = (cp_parser_constant_expression
5216                         (parser, /*allow_non_constant_p=*/true,
5217                          &expr_non_constant_p));
5218                 if (expr_non_constant_p)
5219                   *non_constant_p = true;
5220               }
5221             else
5222               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5223
5224             if (fold_expr_p)
5225               expr = fold_non_dependent_expr (expr);
5226
5227             /* If we have an ellipsis, then this is an expression
5228                expansion.  */
5229             if (allow_expansion_p
5230                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5231               {
5232                 /* Consume the `...'.  */
5233                 cp_lexer_consume_token (parser->lexer);
5234
5235                 /* Build the argument pack.  */
5236                 expr = make_pack_expansion (expr);
5237               }
5238
5239              /* Add it to the list.  We add error_mark_node
5240                 expressions to the list, so that we can still tell if
5241                 the correct form for a parenthesized expression-list
5242                 is found. That gives better errors.  */
5243             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5244
5245             if (expr == error_mark_node)
5246               goto skip_comma;
5247           }
5248
5249         /* After the first item, attribute lists look the same as
5250            expression lists.  */
5251         is_attribute_list = false;
5252
5253       get_comma:;
5254         /* If the next token isn't a `,', then we are done.  */
5255         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5256           break;
5257
5258         /* Otherwise, consume the `,' and keep going.  */
5259         cp_lexer_consume_token (parser->lexer);
5260       }
5261
5262   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5263     {
5264       int ending;
5265
5266     skip_comma:;
5267       /* We try and resync to an unnested comma, as that will give the
5268          user better diagnostics.  */
5269       ending = cp_parser_skip_to_closing_parenthesis (parser,
5270                                                       /*recovering=*/true,
5271                                                       /*or_comma=*/true,
5272                                                       /*consume_paren=*/true);
5273       if (ending < 0)
5274         goto get_comma;
5275       if (!ending)
5276         {
5277           parser->greater_than_is_operator_p
5278             = saved_greater_than_is_operator_p;
5279           return error_mark_node;
5280         }
5281     }
5282
5283   parser->greater_than_is_operator_p
5284     = saved_greater_than_is_operator_p;
5285
5286   /* We built up the list in reverse order so we must reverse it now.  */
5287   expression_list = nreverse (expression_list);
5288   if (identifier)
5289     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5290
5291   return expression_list;
5292 }
5293
5294 /* Parse a pseudo-destructor-name.
5295
5296    pseudo-destructor-name:
5297      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5298      :: [opt] nested-name-specifier template template-id :: ~ type-name
5299      :: [opt] nested-name-specifier [opt] ~ type-name
5300
5301    If either of the first two productions is used, sets *SCOPE to the
5302    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5303    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5304    or ERROR_MARK_NODE if the parse fails.  */
5305
5306 static void
5307 cp_parser_pseudo_destructor_name (cp_parser* parser,
5308                                   tree* scope,
5309                                   tree* type)
5310 {
5311   bool nested_name_specifier_p;
5312
5313   /* Assume that things will not work out.  */
5314   *type = error_mark_node;
5315
5316   /* Look for the optional `::' operator.  */
5317   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5318   /* Look for the optional nested-name-specifier.  */
5319   nested_name_specifier_p
5320     = (cp_parser_nested_name_specifier_opt (parser,
5321                                             /*typename_keyword_p=*/false,
5322                                             /*check_dependency_p=*/true,
5323                                             /*type_p=*/false,
5324                                             /*is_declaration=*/false)
5325        != NULL_TREE);
5326   /* Now, if we saw a nested-name-specifier, we might be doing the
5327      second production.  */
5328   if (nested_name_specifier_p
5329       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5330     {
5331       /* Consume the `template' keyword.  */
5332       cp_lexer_consume_token (parser->lexer);
5333       /* Parse the template-id.  */
5334       cp_parser_template_id (parser,
5335                              /*template_keyword_p=*/true,
5336                              /*check_dependency_p=*/false,
5337                              /*is_declaration=*/true);
5338       /* Look for the `::' token.  */
5339       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5340     }
5341   /* If the next token is not a `~', then there might be some
5342      additional qualification.  */
5343   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5344     {
5345       /* At this point, we're looking for "type-name :: ~".  The type-name
5346          must not be a class-name, since this is a pseudo-destructor.  So,
5347          it must be either an enum-name, or a typedef-name -- both of which
5348          are just identifiers.  So, we peek ahead to check that the "::"
5349          and "~" tokens are present; if they are not, then we can avoid
5350          calling type_name.  */
5351       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5352           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5353           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5354         {
5355           cp_parser_error (parser, "non-scalar type");
5356           return;
5357         }
5358
5359       /* Look for the type-name.  */
5360       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5361       if (*scope == error_mark_node)
5362         return;
5363
5364       /* Look for the `::' token.  */
5365       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5366     }
5367   else
5368     *scope = NULL_TREE;
5369
5370   /* Look for the `~'.  */
5371   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5372   /* Look for the type-name again.  We are not responsible for
5373      checking that it matches the first type-name.  */
5374   *type = cp_parser_nonclass_name (parser);
5375 }
5376
5377 /* Parse a unary-expression.
5378
5379    unary-expression:
5380      postfix-expression
5381      ++ cast-expression
5382      -- cast-expression
5383      unary-operator cast-expression
5384      sizeof unary-expression
5385      sizeof ( type-id )
5386      new-expression
5387      delete-expression
5388
5389    GNU Extensions:
5390
5391    unary-expression:
5392      __extension__ cast-expression
5393      __alignof__ unary-expression
5394      __alignof__ ( type-id )
5395      __real__ cast-expression
5396      __imag__ cast-expression
5397      && identifier
5398
5399    ADDRESS_P is true iff the unary-expression is appearing as the
5400    operand of the `&' operator.   CAST_P is true if this expression is
5401    the target of a cast.
5402
5403    Returns a representation of the expression.  */
5404
5405 static tree
5406 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5407                             cp_id_kind * pidk)
5408 {
5409   cp_token *token;
5410   enum tree_code unary_operator;
5411
5412   /* Peek at the next token.  */
5413   token = cp_lexer_peek_token (parser->lexer);
5414   /* Some keywords give away the kind of expression.  */
5415   if (token->type == CPP_KEYWORD)
5416     {
5417       enum rid keyword = token->keyword;
5418
5419       switch (keyword)
5420         {
5421         case RID_ALIGNOF:
5422         case RID_SIZEOF:
5423           {
5424             tree operand;
5425             enum tree_code op;
5426
5427             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5428             /* Consume the token.  */
5429             cp_lexer_consume_token (parser->lexer);
5430             /* Parse the operand.  */
5431             operand = cp_parser_sizeof_operand (parser, keyword);
5432
5433             if (TYPE_P (operand))
5434               return cxx_sizeof_or_alignof_type (operand, op, true);
5435             else
5436               return cxx_sizeof_or_alignof_expr (operand, op, true);
5437           }
5438
5439         case RID_NEW:
5440           return cp_parser_new_expression (parser);
5441
5442         case RID_DELETE:
5443           return cp_parser_delete_expression (parser);
5444
5445         case RID_EXTENSION:
5446           {
5447             /* The saved value of the PEDANTIC flag.  */
5448             int saved_pedantic;
5449             tree expr;
5450
5451             /* Save away the PEDANTIC flag.  */
5452             cp_parser_extension_opt (parser, &saved_pedantic);
5453             /* Parse the cast-expression.  */
5454             expr = cp_parser_simple_cast_expression (parser);
5455             /* Restore the PEDANTIC flag.  */
5456             pedantic = saved_pedantic;
5457
5458             return expr;
5459           }
5460
5461         case RID_REALPART:
5462         case RID_IMAGPART:
5463           {
5464             tree expression;
5465
5466             /* Consume the `__real__' or `__imag__' token.  */
5467             cp_lexer_consume_token (parser->lexer);
5468             /* Parse the cast-expression.  */
5469             expression = cp_parser_simple_cast_expression (parser);
5470             /* Create the complete representation.  */
5471             return build_x_unary_op ((keyword == RID_REALPART
5472                                       ? REALPART_EXPR : IMAGPART_EXPR),
5473                                      expression,
5474                                      tf_warning_or_error);
5475           }
5476           break;
5477
5478         default:
5479           break;
5480         }
5481     }
5482
5483   /* Look for the `:: new' and `:: delete', which also signal the
5484      beginning of a new-expression, or delete-expression,
5485      respectively.  If the next token is `::', then it might be one of
5486      these.  */
5487   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5488     {
5489       enum rid keyword;
5490
5491       /* See if the token after the `::' is one of the keywords in
5492          which we're interested.  */
5493       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5494       /* If it's `new', we have a new-expression.  */
5495       if (keyword == RID_NEW)
5496         return cp_parser_new_expression (parser);
5497       /* Similarly, for `delete'.  */
5498       else if (keyword == RID_DELETE)
5499         return cp_parser_delete_expression (parser);
5500     }
5501
5502   /* Look for a unary operator.  */
5503   unary_operator = cp_parser_unary_operator (token);
5504   /* The `++' and `--' operators can be handled similarly, even though
5505      they are not technically unary-operators in the grammar.  */
5506   if (unary_operator == ERROR_MARK)
5507     {
5508       if (token->type == CPP_PLUS_PLUS)
5509         unary_operator = PREINCREMENT_EXPR;
5510       else if (token->type == CPP_MINUS_MINUS)
5511         unary_operator = PREDECREMENT_EXPR;
5512       /* Handle the GNU address-of-label extension.  */
5513       else if (cp_parser_allow_gnu_extensions_p (parser)
5514                && token->type == CPP_AND_AND)
5515         {
5516           tree identifier;
5517           tree expression;
5518           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5519
5520           /* Consume the '&&' token.  */
5521           cp_lexer_consume_token (parser->lexer);
5522           /* Look for the identifier.  */
5523           identifier = cp_parser_identifier (parser);
5524           /* Create an expression representing the address.  */
5525           expression = finish_label_address_expr (identifier, loc);
5526           if (cp_parser_non_integral_constant_expression (parser,
5527                                                 "the address of a label"))
5528             expression = error_mark_node;
5529           return expression;
5530         }
5531     }
5532   if (unary_operator != ERROR_MARK)
5533     {
5534       tree cast_expression;
5535       tree expression = error_mark_node;
5536       const char *non_constant_p = NULL;
5537
5538       /* Consume the operator token.  */
5539       token = cp_lexer_consume_token (parser->lexer);
5540       /* Parse the cast-expression.  */
5541       cast_expression
5542         = cp_parser_cast_expression (parser,
5543                                      unary_operator == ADDR_EXPR,
5544                                      /*cast_p=*/false, pidk);
5545       /* Now, build an appropriate representation.  */
5546       switch (unary_operator)
5547         {
5548         case INDIRECT_REF:
5549           non_constant_p = "%<*%>";
5550           expression = build_x_indirect_ref (cast_expression, "unary *",
5551                                              tf_warning_or_error);
5552           break;
5553
5554         case ADDR_EXPR:
5555           non_constant_p = "%<&%>";
5556           /* Fall through.  */
5557         case BIT_NOT_EXPR:
5558           expression = build_x_unary_op (unary_operator, cast_expression,
5559                                          tf_warning_or_error);
5560           break;
5561
5562         case PREINCREMENT_EXPR:
5563         case PREDECREMENT_EXPR:
5564           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5565                             ? "%<++%>" : "%<--%>");
5566           /* Fall through.  */
5567         case UNARY_PLUS_EXPR:
5568         case NEGATE_EXPR:
5569         case TRUTH_NOT_EXPR:
5570           expression = finish_unary_op_expr (unary_operator, cast_expression);
5571           break;
5572
5573         default:
5574           gcc_unreachable ();
5575         }
5576
5577       if (non_constant_p
5578           && cp_parser_non_integral_constant_expression (parser,
5579                                                          non_constant_p))
5580         expression = error_mark_node;
5581
5582       return expression;
5583     }
5584
5585   return cp_parser_postfix_expression (parser, address_p, cast_p,
5586                                        /*member_access_only_p=*/false,
5587                                        pidk);
5588 }
5589
5590 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5591    unary-operator, the corresponding tree code is returned.  */
5592
5593 static enum tree_code
5594 cp_parser_unary_operator (cp_token* token)
5595 {
5596   switch (token->type)
5597     {
5598     case CPP_MULT:
5599       return INDIRECT_REF;
5600
5601     case CPP_AND:
5602       return ADDR_EXPR;
5603
5604     case CPP_PLUS:
5605       return UNARY_PLUS_EXPR;
5606
5607     case CPP_MINUS:
5608       return NEGATE_EXPR;
5609
5610     case CPP_NOT:
5611       return TRUTH_NOT_EXPR;
5612
5613     case CPP_COMPL:
5614       return BIT_NOT_EXPR;
5615
5616     default:
5617       return ERROR_MARK;
5618     }
5619 }
5620
5621 /* Parse a new-expression.
5622
5623    new-expression:
5624      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5625      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5626
5627    Returns a representation of the expression.  */
5628
5629 static tree
5630 cp_parser_new_expression (cp_parser* parser)
5631 {
5632   bool global_scope_p;
5633   tree placement;
5634   tree type;
5635   tree initializer;
5636   tree nelts;
5637
5638   /* Look for the optional `::' operator.  */
5639   global_scope_p
5640     = (cp_parser_global_scope_opt (parser,
5641                                    /*current_scope_valid_p=*/false)
5642        != NULL_TREE);
5643   /* Look for the `new' operator.  */
5644   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5645   /* There's no easy way to tell a new-placement from the
5646      `( type-id )' construct.  */
5647   cp_parser_parse_tentatively (parser);
5648   /* Look for a new-placement.  */
5649   placement = cp_parser_new_placement (parser);
5650   /* If that didn't work out, there's no new-placement.  */
5651   if (!cp_parser_parse_definitely (parser))
5652     placement = NULL_TREE;
5653
5654   /* If the next token is a `(', then we have a parenthesized
5655      type-id.  */
5656   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5657     {
5658       cp_token *token;
5659       /* Consume the `('.  */
5660       cp_lexer_consume_token (parser->lexer);
5661       /* Parse the type-id.  */
5662       type = cp_parser_type_id (parser);
5663       /* Look for the closing `)'.  */
5664       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5665       token = cp_lexer_peek_token (parser->lexer);
5666       /* There should not be a direct-new-declarator in this production,
5667          but GCC used to allowed this, so we check and emit a sensible error
5668          message for this case.  */
5669       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5670         {
5671           error ("%Harray bound forbidden after parenthesized type-id",
5672                  &token->location);
5673           inform (token->location, 
5674                   "try removing the parentheses around the type-id");
5675           cp_parser_direct_new_declarator (parser);
5676         }
5677       nelts = NULL_TREE;
5678     }
5679   /* Otherwise, there must be a new-type-id.  */
5680   else
5681     type = cp_parser_new_type_id (parser, &nelts);
5682
5683   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5684   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5685       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5686     initializer = cp_parser_new_initializer (parser);
5687   else
5688     initializer = NULL_TREE;
5689
5690   /* A new-expression may not appear in an integral constant
5691      expression.  */
5692   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5693     return error_mark_node;
5694
5695   /* Create a representation of the new-expression.  */
5696   return build_new (placement, type, nelts, initializer, global_scope_p,
5697                     tf_warning_or_error);
5698 }
5699
5700 /* Parse a new-placement.
5701
5702    new-placement:
5703      ( expression-list )
5704
5705    Returns the same representation as for an expression-list.  */
5706
5707 static tree
5708 cp_parser_new_placement (cp_parser* parser)
5709 {
5710   tree expression_list;
5711
5712   /* Parse the expression-list.  */
5713   expression_list = (cp_parser_parenthesized_expression_list
5714                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5715                       /*non_constant_p=*/NULL));
5716
5717   return expression_list;
5718 }
5719
5720 /* Parse a new-type-id.
5721
5722    new-type-id:
5723      type-specifier-seq new-declarator [opt]
5724
5725    Returns the TYPE allocated.  If the new-type-id indicates an array
5726    type, *NELTS is set to the number of elements in the last array
5727    bound; the TYPE will not include the last array bound.  */
5728
5729 static tree
5730 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5731 {
5732   cp_decl_specifier_seq type_specifier_seq;
5733   cp_declarator *new_declarator;
5734   cp_declarator *declarator;
5735   cp_declarator *outer_declarator;
5736   const char *saved_message;
5737   tree type;
5738
5739   /* The type-specifier sequence must not contain type definitions.
5740      (It cannot contain declarations of new types either, but if they
5741      are not definitions we will catch that because they are not
5742      complete.)  */
5743   saved_message = parser->type_definition_forbidden_message;
5744   parser->type_definition_forbidden_message
5745     = "types may not be defined in a new-type-id";
5746   /* Parse the type-specifier-seq.  */
5747   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5748                                 /*is_trailing_return=*/false,
5749                                 &type_specifier_seq);
5750   /* Restore the old message.  */
5751   parser->type_definition_forbidden_message = saved_message;
5752   /* Parse the new-declarator.  */
5753   new_declarator = cp_parser_new_declarator_opt (parser);
5754
5755   /* Determine the number of elements in the last array dimension, if
5756      any.  */
5757   *nelts = NULL_TREE;
5758   /* Skip down to the last array dimension.  */
5759   declarator = new_declarator;
5760   outer_declarator = NULL;
5761   while (declarator && (declarator->kind == cdk_pointer
5762                         || declarator->kind == cdk_ptrmem))
5763     {
5764       outer_declarator = declarator;
5765       declarator = declarator->declarator;
5766     }
5767   while (declarator
5768          && declarator->kind == cdk_array
5769          && declarator->declarator
5770          && declarator->declarator->kind == cdk_array)
5771     {
5772       outer_declarator = declarator;
5773       declarator = declarator->declarator;
5774     }
5775
5776   if (declarator && declarator->kind == cdk_array)
5777     {
5778       *nelts = declarator->u.array.bounds;
5779       if (*nelts == error_mark_node)
5780         *nelts = integer_one_node;
5781
5782       if (outer_declarator)
5783         outer_declarator->declarator = declarator->declarator;
5784       else
5785         new_declarator = NULL;
5786     }
5787
5788   type = groktypename (&type_specifier_seq, new_declarator, false);
5789   return type;
5790 }
5791
5792 /* Parse an (optional) new-declarator.
5793
5794    new-declarator:
5795      ptr-operator new-declarator [opt]
5796      direct-new-declarator
5797
5798    Returns the declarator.  */
5799
5800 static cp_declarator *
5801 cp_parser_new_declarator_opt (cp_parser* parser)
5802 {
5803   enum tree_code code;
5804   tree type;
5805   cp_cv_quals cv_quals;
5806
5807   /* We don't know if there's a ptr-operator next, or not.  */
5808   cp_parser_parse_tentatively (parser);
5809   /* Look for a ptr-operator.  */
5810   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5811   /* If that worked, look for more new-declarators.  */
5812   if (cp_parser_parse_definitely (parser))
5813     {
5814       cp_declarator *declarator;
5815
5816       /* Parse another optional declarator.  */
5817       declarator = cp_parser_new_declarator_opt (parser);
5818
5819       return cp_parser_make_indirect_declarator
5820         (code, type, cv_quals, declarator);
5821     }
5822
5823   /* If the next token is a `[', there is a direct-new-declarator.  */
5824   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5825     return cp_parser_direct_new_declarator (parser);
5826
5827   return NULL;
5828 }
5829
5830 /* Parse a direct-new-declarator.
5831
5832    direct-new-declarator:
5833      [ expression ]
5834      direct-new-declarator [constant-expression]
5835
5836    */
5837
5838 static cp_declarator *
5839 cp_parser_direct_new_declarator (cp_parser* parser)
5840 {
5841   cp_declarator *declarator = NULL;
5842
5843   while (true)
5844     {
5845       tree expression;
5846
5847       /* Look for the opening `['.  */
5848       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5849       /* The first expression is not required to be constant.  */
5850       if (!declarator)
5851         {
5852           cp_token *token = cp_lexer_peek_token (parser->lexer);
5853           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5854           /* The standard requires that the expression have integral
5855              type.  DR 74 adds enumeration types.  We believe that the
5856              real intent is that these expressions be handled like the
5857              expression in a `switch' condition, which also allows
5858              classes with a single conversion to integral or
5859              enumeration type.  */
5860           if (!processing_template_decl)
5861             {
5862               expression
5863                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5864                                               expression,
5865                                               /*complain=*/true);
5866               if (!expression)
5867                 {
5868                   error ("%Hexpression in new-declarator must have integral "
5869                          "or enumeration type", &token->location);
5870                   expression = error_mark_node;
5871                 }
5872             }
5873         }
5874       /* But all the other expressions must be.  */
5875       else
5876         expression
5877           = cp_parser_constant_expression (parser,
5878                                            /*allow_non_constant=*/false,
5879                                            NULL);
5880       /* Look for the closing `]'.  */
5881       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5882
5883       /* Add this bound to the declarator.  */
5884       declarator = make_array_declarator (declarator, expression);
5885
5886       /* If the next token is not a `[', then there are no more
5887          bounds.  */
5888       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5889         break;
5890     }
5891
5892   return declarator;
5893 }
5894
5895 /* Parse a new-initializer.
5896
5897    new-initializer:
5898      ( expression-list [opt] )
5899      braced-init-list
5900
5901    Returns a representation of the expression-list.  If there is no
5902    expression-list, VOID_ZERO_NODE is returned.  */
5903
5904 static tree
5905 cp_parser_new_initializer (cp_parser* parser)
5906 {
5907   tree expression_list;
5908
5909   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5910     {
5911       bool expr_non_constant_p;
5912       maybe_warn_cpp0x ("extended initializer lists");
5913       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5914       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5915       expression_list = build_tree_list (NULL_TREE, expression_list);
5916     }
5917   else
5918     expression_list = (cp_parser_parenthesized_expression_list
5919                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5920                         /*non_constant_p=*/NULL));
5921   if (!expression_list)
5922     expression_list = void_zero_node;
5923
5924   return expression_list;
5925 }
5926
5927 /* Parse a delete-expression.
5928
5929    delete-expression:
5930      :: [opt] delete cast-expression
5931      :: [opt] delete [ ] cast-expression
5932
5933    Returns a representation of the expression.  */
5934
5935 static tree
5936 cp_parser_delete_expression (cp_parser* parser)
5937 {
5938   bool global_scope_p;
5939   bool array_p;
5940   tree expression;
5941
5942   /* Look for the optional `::' operator.  */
5943   global_scope_p
5944     = (cp_parser_global_scope_opt (parser,
5945                                    /*current_scope_valid_p=*/false)
5946        != NULL_TREE);
5947   /* Look for the `delete' keyword.  */
5948   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5949   /* See if the array syntax is in use.  */
5950   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5951     {
5952       /* Consume the `[' token.  */
5953       cp_lexer_consume_token (parser->lexer);
5954       /* Look for the `]' token.  */
5955       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5956       /* Remember that this is the `[]' construct.  */
5957       array_p = true;
5958     }
5959   else
5960     array_p = false;
5961
5962   /* Parse the cast-expression.  */
5963   expression = cp_parser_simple_cast_expression (parser);
5964
5965   /* A delete-expression may not appear in an integral constant
5966      expression.  */
5967   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5968     return error_mark_node;
5969
5970   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5971 }
5972
5973 /* Returns true if TOKEN may start a cast-expression and false
5974    otherwise.  */
5975
5976 static bool
5977 cp_parser_token_starts_cast_expression (cp_token *token)
5978 {
5979   switch (token->type)
5980     {
5981     case CPP_COMMA:
5982     case CPP_SEMICOLON:
5983     case CPP_QUERY:
5984     case CPP_COLON:
5985     case CPP_CLOSE_SQUARE:
5986     case CPP_CLOSE_PAREN:
5987     case CPP_CLOSE_BRACE:
5988     case CPP_DOT:
5989     case CPP_DOT_STAR:
5990     case CPP_DEREF:
5991     case CPP_DEREF_STAR:
5992     case CPP_DIV:
5993     case CPP_MOD:
5994     case CPP_LSHIFT:
5995     case CPP_RSHIFT:
5996     case CPP_LESS:
5997     case CPP_GREATER:
5998     case CPP_LESS_EQ:
5999     case CPP_GREATER_EQ:
6000     case CPP_EQ_EQ:
6001     case CPP_NOT_EQ:
6002     case CPP_EQ:
6003     case CPP_MULT_EQ:
6004     case CPP_DIV_EQ:
6005     case CPP_MOD_EQ:
6006     case CPP_PLUS_EQ:
6007     case CPP_MINUS_EQ:
6008     case CPP_RSHIFT_EQ:
6009     case CPP_LSHIFT_EQ:
6010     case CPP_AND_EQ:
6011     case CPP_XOR_EQ:
6012     case CPP_OR_EQ:
6013     case CPP_XOR:
6014     case CPP_OR:
6015     case CPP_OR_OR:
6016     case CPP_EOF:
6017       return false;
6018
6019       /* '[' may start a primary-expression in obj-c++.  */
6020     case CPP_OPEN_SQUARE:
6021       return c_dialect_objc ();
6022
6023     default:
6024       return true;
6025     }
6026 }
6027
6028 /* Parse a cast-expression.
6029
6030    cast-expression:
6031      unary-expression
6032      ( type-id ) cast-expression
6033
6034    ADDRESS_P is true iff the unary-expression is appearing as the
6035    operand of the `&' operator.   CAST_P is true if this expression is
6036    the target of a cast.
6037
6038    Returns a representation of the expression.  */
6039
6040 static tree
6041 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6042                            cp_id_kind * pidk)
6043 {
6044   /* If it's a `(', then we might be looking at a cast.  */
6045   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6046     {
6047       tree type = NULL_TREE;
6048       tree expr = NULL_TREE;
6049       bool compound_literal_p;
6050       const char *saved_message;
6051
6052       /* There's no way to know yet whether or not this is a cast.
6053          For example, `(int (3))' is a unary-expression, while `(int)
6054          3' is a cast.  So, we resort to parsing tentatively.  */
6055       cp_parser_parse_tentatively (parser);
6056       /* Types may not be defined in a cast.  */
6057       saved_message = parser->type_definition_forbidden_message;
6058       parser->type_definition_forbidden_message
6059         = "types may not be defined in casts";
6060       /* Consume the `('.  */
6061       cp_lexer_consume_token (parser->lexer);
6062       /* A very tricky bit is that `(struct S) { 3 }' is a
6063          compound-literal (which we permit in C++ as an extension).
6064          But, that construct is not a cast-expression -- it is a
6065          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6066          is legal; if the compound-literal were a cast-expression,
6067          you'd need an extra set of parentheses.)  But, if we parse
6068          the type-id, and it happens to be a class-specifier, then we
6069          will commit to the parse at that point, because we cannot
6070          undo the action that is done when creating a new class.  So,
6071          then we cannot back up and do a postfix-expression.
6072
6073          Therefore, we scan ahead to the closing `)', and check to see
6074          if the token after the `)' is a `{'.  If so, we are not
6075          looking at a cast-expression.
6076
6077          Save tokens so that we can put them back.  */
6078       cp_lexer_save_tokens (parser->lexer);
6079       /* Skip tokens until the next token is a closing parenthesis.
6080          If we find the closing `)', and the next token is a `{', then
6081          we are looking at a compound-literal.  */
6082       compound_literal_p
6083         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6084                                                   /*consume_paren=*/true)
6085            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6086       /* Roll back the tokens we skipped.  */
6087       cp_lexer_rollback_tokens (parser->lexer);
6088       /* If we were looking at a compound-literal, simulate an error
6089          so that the call to cp_parser_parse_definitely below will
6090          fail.  */
6091       if (compound_literal_p)
6092         cp_parser_simulate_error (parser);
6093       else
6094         {
6095           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6096           parser->in_type_id_in_expr_p = true;
6097           /* Look for the type-id.  */
6098           type = cp_parser_type_id (parser);
6099           /* Look for the closing `)'.  */
6100           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6101           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6102         }
6103
6104       /* Restore the saved message.  */
6105       parser->type_definition_forbidden_message = saved_message;
6106
6107       /* At this point this can only be either a cast or a
6108          parenthesized ctor such as `(T ())' that looks like a cast to
6109          function returning T.  */
6110       if (!cp_parser_error_occurred (parser)
6111           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6112                                                      (parser->lexer)))
6113         {
6114           cp_parser_parse_definitely (parser);
6115           expr = cp_parser_cast_expression (parser,
6116                                             /*address_p=*/false,
6117                                             /*cast_p=*/true, pidk);
6118
6119           /* Warn about old-style casts, if so requested.  */
6120           if (warn_old_style_cast
6121               && !in_system_header
6122               && !VOID_TYPE_P (type)
6123               && current_lang_name != lang_name_c)
6124             warning (OPT_Wold_style_cast, "use of old-style cast");
6125
6126           /* Only type conversions to integral or enumeration types
6127              can be used in constant-expressions.  */
6128           if (!cast_valid_in_integral_constant_expression_p (type)
6129               && (cp_parser_non_integral_constant_expression
6130                   (parser,
6131                    "a cast to a type other than an integral or "
6132                    "enumeration type")))
6133             return error_mark_node;
6134
6135           /* Perform the cast.  */
6136           expr = build_c_cast (type, expr);
6137           return expr;
6138         }
6139       else 
6140         cp_parser_abort_tentative_parse (parser);
6141     }
6142
6143   /* If we get here, then it's not a cast, so it must be a
6144      unary-expression.  */
6145   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6146 }
6147
6148 /* Parse a binary expression of the general form:
6149
6150    pm-expression:
6151      cast-expression
6152      pm-expression .* cast-expression
6153      pm-expression ->* cast-expression
6154
6155    multiplicative-expression:
6156      pm-expression
6157      multiplicative-expression * pm-expression
6158      multiplicative-expression / pm-expression
6159      multiplicative-expression % pm-expression
6160
6161    additive-expression:
6162      multiplicative-expression
6163      additive-expression + multiplicative-expression
6164      additive-expression - multiplicative-expression
6165
6166    shift-expression:
6167      additive-expression
6168      shift-expression << additive-expression
6169      shift-expression >> additive-expression
6170
6171    relational-expression:
6172      shift-expression
6173      relational-expression < shift-expression
6174      relational-expression > shift-expression
6175      relational-expression <= shift-expression
6176      relational-expression >= shift-expression
6177
6178   GNU Extension:
6179
6180    relational-expression:
6181      relational-expression <? shift-expression
6182      relational-expression >? shift-expression
6183
6184    equality-expression:
6185      relational-expression
6186      equality-expression == relational-expression
6187      equality-expression != relational-expression
6188
6189    and-expression:
6190      equality-expression
6191      and-expression & equality-expression
6192
6193    exclusive-or-expression:
6194      and-expression
6195      exclusive-or-expression ^ and-expression
6196
6197    inclusive-or-expression:
6198      exclusive-or-expression
6199      inclusive-or-expression | exclusive-or-expression
6200
6201    logical-and-expression:
6202      inclusive-or-expression
6203      logical-and-expression && inclusive-or-expression
6204
6205    logical-or-expression:
6206      logical-and-expression
6207      logical-or-expression || logical-and-expression
6208
6209    All these are implemented with a single function like:
6210
6211    binary-expression:
6212      simple-cast-expression
6213      binary-expression <token> binary-expression
6214
6215    CAST_P is true if this expression is the target of a cast.
6216
6217    The binops_by_token map is used to get the tree codes for each <token> type.
6218    binary-expressions are associated according to a precedence table.  */
6219
6220 #define TOKEN_PRECEDENCE(token)                              \
6221 (((token->type == CPP_GREATER                                \
6222    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6223   && !parser->greater_than_is_operator_p)                    \
6224  ? PREC_NOT_OPERATOR                                         \
6225  : binops_by_token[token->type].prec)
6226
6227 static tree
6228 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6229                              bool no_toplevel_fold_p,
6230                              enum cp_parser_prec prec,
6231                              cp_id_kind * pidk)
6232 {
6233   cp_parser_expression_stack stack;
6234   cp_parser_expression_stack_entry *sp = &stack[0];
6235   tree lhs, rhs;
6236   cp_token *token;
6237   enum tree_code tree_type, lhs_type, rhs_type;
6238   enum cp_parser_prec new_prec, lookahead_prec;
6239   bool overloaded_p;
6240
6241   /* Parse the first expression.  */
6242   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6243   lhs_type = ERROR_MARK;
6244
6245   for (;;)
6246     {
6247       /* Get an operator token.  */
6248       token = cp_lexer_peek_token (parser->lexer);
6249
6250       if (warn_cxx0x_compat
6251           && token->type == CPP_RSHIFT
6252           && !parser->greater_than_is_operator_p)
6253         {
6254           warning (OPT_Wc__0x_compat, 
6255                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6256                    &token->location);
6257           warning (OPT_Wc__0x_compat, 
6258                    "suggest parentheses around %<>>%> expression");
6259         }
6260
6261       new_prec = TOKEN_PRECEDENCE (token);
6262
6263       /* Popping an entry off the stack means we completed a subexpression:
6264          - either we found a token which is not an operator (`>' where it is not
6265            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6266            will happen repeatedly;
6267          - or, we found an operator which has lower priority.  This is the case
6268            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6269            parsing `3 * 4'.  */
6270       if (new_prec <= prec)
6271         {
6272           if (sp == stack)
6273             break;
6274           else
6275             goto pop;
6276         }
6277
6278      get_rhs:
6279       tree_type = binops_by_token[token->type].tree_type;
6280
6281       /* We used the operator token.  */
6282       cp_lexer_consume_token (parser->lexer);
6283
6284       /* Extract another operand.  It may be the RHS of this expression
6285          or the LHS of a new, higher priority expression.  */
6286       rhs = cp_parser_simple_cast_expression (parser);
6287       rhs_type = ERROR_MARK;
6288
6289       /* Get another operator token.  Look up its precedence to avoid
6290          building a useless (immediately popped) stack entry for common
6291          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6292       token = cp_lexer_peek_token (parser->lexer);
6293       lookahead_prec = TOKEN_PRECEDENCE (token);
6294       if (lookahead_prec > new_prec)
6295         {
6296           /* ... and prepare to parse the RHS of the new, higher priority
6297              expression.  Since precedence levels on the stack are
6298              monotonically increasing, we do not have to care about
6299              stack overflows.  */
6300           sp->prec = prec;
6301           sp->tree_type = tree_type;
6302           sp->lhs = lhs;
6303           sp->lhs_type = lhs_type;
6304           sp++;
6305           lhs = rhs;
6306           lhs_type = rhs_type;
6307           prec = new_prec;
6308           new_prec = lookahead_prec;
6309           goto get_rhs;
6310
6311          pop:
6312           lookahead_prec = new_prec;
6313           /* If the stack is not empty, we have parsed into LHS the right side
6314              (`4' in the example above) of an expression we had suspended.
6315              We can use the information on the stack to recover the LHS (`3')
6316              from the stack together with the tree code (`MULT_EXPR'), and
6317              the precedence of the higher level subexpression
6318              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6319              which will be used to actually build the additive expression.  */
6320           --sp;
6321           prec = sp->prec;
6322           tree_type = sp->tree_type;
6323           rhs = lhs;
6324           rhs_type = lhs_type;
6325           lhs = sp->lhs;
6326           lhs_type = sp->lhs_type;
6327         }
6328
6329       overloaded_p = false;
6330       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6331          ERROR_MARK for everything that is not a binary expression.
6332          This makes warn_about_parentheses miss some warnings that
6333          involve unary operators.  For unary expressions we should
6334          pass the correct tree_code unless the unary expression was
6335          surrounded by parentheses.
6336       */
6337       if (no_toplevel_fold_p
6338           && lookahead_prec <= prec
6339           && sp == stack
6340           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6341         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6342       else
6343         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6344                                  &overloaded_p, tf_warning_or_error);
6345       lhs_type = tree_type;
6346
6347       /* If the binary operator required the use of an overloaded operator,
6348          then this expression cannot be an integral constant-expression.
6349          An overloaded operator can be used even if both operands are
6350          otherwise permissible in an integral constant-expression if at
6351          least one of the operands is of enumeration type.  */
6352
6353       if (overloaded_p
6354           && (cp_parser_non_integral_constant_expression
6355               (parser, "calls to overloaded operators")))
6356         return error_mark_node;
6357     }
6358
6359   return lhs;
6360 }
6361
6362
6363 /* Parse the `? expression : assignment-expression' part of a
6364    conditional-expression.  The LOGICAL_OR_EXPR is the
6365    logical-or-expression that started the conditional-expression.
6366    Returns a representation of the entire conditional-expression.
6367
6368    This routine is used by cp_parser_assignment_expression.
6369
6370      ? expression : assignment-expression
6371
6372    GNU Extensions:
6373
6374      ? : assignment-expression */
6375
6376 static tree
6377 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6378 {
6379   tree expr;
6380   tree assignment_expr;
6381
6382   /* Consume the `?' token.  */
6383   cp_lexer_consume_token (parser->lexer);
6384   if (cp_parser_allow_gnu_extensions_p (parser)
6385       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6386     /* Implicit true clause.  */
6387     expr = NULL_TREE;
6388   else
6389     /* Parse the expression.  */
6390     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6391
6392   /* The next token should be a `:'.  */
6393   cp_parser_require (parser, CPP_COLON, "%<:%>");
6394   /* Parse the assignment-expression.  */
6395   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6396
6397   /* Build the conditional-expression.  */
6398   return build_x_conditional_expr (logical_or_expr,
6399                                    expr,
6400                                    assignment_expr,
6401                                    tf_warning_or_error);
6402 }
6403
6404 /* Parse an assignment-expression.
6405
6406    assignment-expression:
6407      conditional-expression
6408      logical-or-expression assignment-operator assignment_expression
6409      throw-expression
6410
6411    CAST_P is true if this expression is the target of a cast.
6412
6413    Returns a representation for the expression.  */
6414
6415 static tree
6416 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6417                                  cp_id_kind * pidk)
6418 {
6419   tree expr;
6420
6421   /* If the next token is the `throw' keyword, then we're looking at
6422      a throw-expression.  */
6423   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6424     expr = cp_parser_throw_expression (parser);
6425   /* Otherwise, it must be that we are looking at a
6426      logical-or-expression.  */
6427   else
6428     {
6429       /* Parse the binary expressions (logical-or-expression).  */
6430       expr = cp_parser_binary_expression (parser, cast_p, false,
6431                                           PREC_NOT_OPERATOR, pidk);
6432       /* If the next token is a `?' then we're actually looking at a
6433          conditional-expression.  */
6434       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6435         return cp_parser_question_colon_clause (parser, expr);
6436       else
6437         {
6438           enum tree_code assignment_operator;
6439
6440           /* If it's an assignment-operator, we're using the second
6441              production.  */
6442           assignment_operator
6443             = cp_parser_assignment_operator_opt (parser);
6444           if (assignment_operator != ERROR_MARK)
6445             {
6446               bool non_constant_p;
6447
6448               /* Parse the right-hand side of the assignment.  */
6449               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6450
6451               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6452                 maybe_warn_cpp0x ("extended initializer lists");
6453
6454               /* An assignment may not appear in a
6455                  constant-expression.  */
6456               if (cp_parser_non_integral_constant_expression (parser,
6457                                                               "an assignment"))
6458                 return error_mark_node;
6459               /* Build the assignment expression.  */
6460               expr = build_x_modify_expr (expr,
6461                                           assignment_operator,
6462                                           rhs,
6463                                           tf_warning_or_error);
6464             }
6465         }
6466     }
6467
6468   return expr;
6469 }
6470
6471 /* Parse an (optional) assignment-operator.
6472
6473    assignment-operator: one of
6474      = *= /= %= += -= >>= <<= &= ^= |=
6475
6476    GNU Extension:
6477
6478    assignment-operator: one of
6479      <?= >?=
6480
6481    If the next token is an assignment operator, the corresponding tree
6482    code is returned, and the token is consumed.  For example, for
6483    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6484    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6485    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6486    operator, ERROR_MARK is returned.  */
6487
6488 static enum tree_code
6489 cp_parser_assignment_operator_opt (cp_parser* parser)
6490 {
6491   enum tree_code op;
6492   cp_token *token;
6493
6494   /* Peek at the next token.  */
6495   token = cp_lexer_peek_token (parser->lexer);
6496
6497   switch (token->type)
6498     {
6499     case CPP_EQ:
6500       op = NOP_EXPR;
6501       break;
6502
6503     case CPP_MULT_EQ:
6504       op = MULT_EXPR;
6505       break;
6506
6507     case CPP_DIV_EQ:
6508       op = TRUNC_DIV_EXPR;
6509       break;
6510
6511     case CPP_MOD_EQ:
6512       op = TRUNC_MOD_EXPR;
6513       break;
6514
6515     case CPP_PLUS_EQ:
6516       op = PLUS_EXPR;
6517       break;
6518
6519     case CPP_MINUS_EQ:
6520       op = MINUS_EXPR;
6521       break;
6522
6523     case CPP_RSHIFT_EQ:
6524       op = RSHIFT_EXPR;
6525       break;
6526
6527     case CPP_LSHIFT_EQ:
6528       op = LSHIFT_EXPR;
6529       break;
6530
6531     case CPP_AND_EQ:
6532       op = BIT_AND_EXPR;
6533       break;
6534
6535     case CPP_XOR_EQ:
6536       op = BIT_XOR_EXPR;
6537       break;
6538
6539     case CPP_OR_EQ:
6540       op = BIT_IOR_EXPR;
6541       break;
6542
6543     default:
6544       /* Nothing else is an assignment operator.  */
6545       op = ERROR_MARK;
6546     }
6547
6548   /* If it was an assignment operator, consume it.  */
6549   if (op != ERROR_MARK)
6550     cp_lexer_consume_token (parser->lexer);
6551
6552   return op;
6553 }
6554
6555 /* Parse an expression.
6556
6557    expression:
6558      assignment-expression
6559      expression , assignment-expression
6560
6561    CAST_P is true if this expression is the target of a cast.
6562
6563    Returns a representation of the expression.  */
6564
6565 static tree
6566 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6567 {
6568   tree expression = NULL_TREE;
6569
6570   while (true)
6571     {
6572       tree assignment_expression;
6573
6574       /* Parse the next assignment-expression.  */
6575       assignment_expression
6576         = cp_parser_assignment_expression (parser, cast_p, pidk);
6577       /* If this is the first assignment-expression, we can just
6578          save it away.  */
6579       if (!expression)
6580         expression = assignment_expression;
6581       else
6582         expression = build_x_compound_expr (expression,
6583                                             assignment_expression,
6584                                             tf_warning_or_error);
6585       /* If the next token is not a comma, then we are done with the
6586          expression.  */
6587       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6588         break;
6589       /* Consume the `,'.  */
6590       cp_lexer_consume_token (parser->lexer);
6591       /* A comma operator cannot appear in a constant-expression.  */
6592       if (cp_parser_non_integral_constant_expression (parser,
6593                                                       "a comma operator"))
6594         expression = error_mark_node;
6595     }
6596
6597   return expression;
6598 }
6599
6600 /* Parse a constant-expression.
6601
6602    constant-expression:
6603      conditional-expression
6604
6605   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6606   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6607   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6608   is false, NON_CONSTANT_P should be NULL.  */
6609
6610 static tree
6611 cp_parser_constant_expression (cp_parser* parser,
6612                                bool allow_non_constant_p,
6613                                bool *non_constant_p)
6614 {
6615   bool saved_integral_constant_expression_p;
6616   bool saved_allow_non_integral_constant_expression_p;
6617   bool saved_non_integral_constant_expression_p;
6618   tree expression;
6619
6620   /* It might seem that we could simply parse the
6621      conditional-expression, and then check to see if it were
6622      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6623      one that the compiler can figure out is constant, possibly after
6624      doing some simplifications or optimizations.  The standard has a
6625      precise definition of constant-expression, and we must honor
6626      that, even though it is somewhat more restrictive.
6627
6628      For example:
6629
6630        int i[(2, 3)];
6631
6632      is not a legal declaration, because `(2, 3)' is not a
6633      constant-expression.  The `,' operator is forbidden in a
6634      constant-expression.  However, GCC's constant-folding machinery
6635      will fold this operation to an INTEGER_CST for `3'.  */
6636
6637   /* Save the old settings.  */
6638   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6639   saved_allow_non_integral_constant_expression_p
6640     = parser->allow_non_integral_constant_expression_p;
6641   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6642   /* We are now parsing a constant-expression.  */
6643   parser->integral_constant_expression_p = true;
6644   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6645   parser->non_integral_constant_expression_p = false;
6646   /* Although the grammar says "conditional-expression", we parse an
6647      "assignment-expression", which also permits "throw-expression"
6648      and the use of assignment operators.  In the case that
6649      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6650      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6651      actually essential that we look for an assignment-expression.
6652      For example, cp_parser_initializer_clauses uses this function to
6653      determine whether a particular assignment-expression is in fact
6654      constant.  */
6655   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6656   /* Restore the old settings.  */
6657   parser->integral_constant_expression_p
6658     = saved_integral_constant_expression_p;
6659   parser->allow_non_integral_constant_expression_p
6660     = saved_allow_non_integral_constant_expression_p;
6661   if (allow_non_constant_p)
6662     *non_constant_p = parser->non_integral_constant_expression_p;
6663   else if (parser->non_integral_constant_expression_p)
6664     expression = error_mark_node;
6665   parser->non_integral_constant_expression_p
6666     = saved_non_integral_constant_expression_p;
6667
6668   return expression;
6669 }
6670
6671 /* Parse __builtin_offsetof.
6672
6673    offsetof-expression:
6674      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6675
6676    offsetof-member-designator:
6677      id-expression
6678      | offsetof-member-designator "." id-expression
6679      | offsetof-member-designator "[" expression "]"
6680      | offsetof-member-designator "->" id-expression  */
6681
6682 static tree
6683 cp_parser_builtin_offsetof (cp_parser *parser)
6684 {
6685   int save_ice_p, save_non_ice_p;
6686   tree type, expr;
6687   cp_id_kind dummy;
6688   cp_token *token;
6689
6690   /* We're about to accept non-integral-constant things, but will
6691      definitely yield an integral constant expression.  Save and
6692      restore these values around our local parsing.  */
6693   save_ice_p = parser->integral_constant_expression_p;
6694   save_non_ice_p = parser->non_integral_constant_expression_p;
6695
6696   /* Consume the "__builtin_offsetof" token.  */
6697   cp_lexer_consume_token (parser->lexer);
6698   /* Consume the opening `('.  */
6699   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6700   /* Parse the type-id.  */
6701   type = cp_parser_type_id (parser);
6702   /* Look for the `,'.  */
6703   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6704   token = cp_lexer_peek_token (parser->lexer);
6705
6706   /* Build the (type *)null that begins the traditional offsetof macro.  */
6707   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6708                             tf_warning_or_error);
6709
6710   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6711   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6712                                                  true, &dummy, token->location);
6713   while (true)
6714     {
6715       token = cp_lexer_peek_token (parser->lexer);
6716       switch (token->type)
6717         {
6718         case CPP_OPEN_SQUARE:
6719           /* offsetof-member-designator "[" expression "]" */
6720           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6721           break;
6722
6723         case CPP_DEREF:
6724           /* offsetof-member-designator "->" identifier */
6725           expr = grok_array_decl (expr, integer_zero_node);
6726           /* FALLTHRU */
6727
6728         case CPP_DOT:
6729           /* offsetof-member-designator "." identifier */
6730           cp_lexer_consume_token (parser->lexer);
6731           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6732                                                          expr, true, &dummy,
6733                                                          token->location);
6734           break;
6735
6736         case CPP_CLOSE_PAREN:
6737           /* Consume the ")" token.  */
6738           cp_lexer_consume_token (parser->lexer);
6739           goto success;
6740
6741         default:
6742           /* Error.  We know the following require will fail, but
6743              that gives the proper error message.  */
6744           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6745           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6746           expr = error_mark_node;
6747           goto failure;
6748         }
6749     }
6750
6751  success:
6752   /* If we're processing a template, we can't finish the semantics yet.
6753      Otherwise we can fold the entire expression now.  */
6754   if (processing_template_decl)
6755     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6756   else
6757     expr = finish_offsetof (expr);
6758
6759  failure:
6760   parser->integral_constant_expression_p = save_ice_p;
6761   parser->non_integral_constant_expression_p = save_non_ice_p;
6762
6763   return expr;
6764 }
6765
6766 /* Parse a trait expression.  */
6767
6768 static tree
6769 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6770 {
6771   cp_trait_kind kind;
6772   tree type1, type2 = NULL_TREE;
6773   bool binary = false;
6774   cp_decl_specifier_seq decl_specs;
6775
6776   switch (keyword)
6777     {
6778     case RID_HAS_NOTHROW_ASSIGN:
6779       kind = CPTK_HAS_NOTHROW_ASSIGN;
6780       break;
6781     case RID_HAS_NOTHROW_CONSTRUCTOR:
6782       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6783       break;
6784     case RID_HAS_NOTHROW_COPY:
6785       kind = CPTK_HAS_NOTHROW_COPY;
6786       break;
6787     case RID_HAS_TRIVIAL_ASSIGN:
6788       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6789       break;
6790     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6791       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6792       break;
6793     case RID_HAS_TRIVIAL_COPY:
6794       kind = CPTK_HAS_TRIVIAL_COPY;
6795       break;
6796     case RID_HAS_TRIVIAL_DESTRUCTOR:
6797       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6798       break;
6799     case RID_HAS_VIRTUAL_DESTRUCTOR:
6800       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6801       break;
6802     case RID_IS_ABSTRACT:
6803       kind = CPTK_IS_ABSTRACT;
6804       break;
6805     case RID_IS_BASE_OF:
6806       kind = CPTK_IS_BASE_OF;
6807       binary = true;
6808       break;
6809     case RID_IS_CLASS:
6810       kind = CPTK_IS_CLASS;
6811       break;
6812     case RID_IS_CONVERTIBLE_TO:
6813       kind = CPTK_IS_CONVERTIBLE_TO;
6814       binary = true;
6815       break;
6816     case RID_IS_EMPTY:
6817       kind = CPTK_IS_EMPTY;
6818       break;
6819     case RID_IS_ENUM:
6820       kind = CPTK_IS_ENUM;
6821       break;
6822     case RID_IS_POD:
6823       kind = CPTK_IS_POD;
6824       break;
6825     case RID_IS_POLYMORPHIC:
6826       kind = CPTK_IS_POLYMORPHIC;
6827       break;
6828     case RID_IS_UNION:
6829       kind = CPTK_IS_UNION;
6830       break;
6831     default:
6832       gcc_unreachable ();
6833     }
6834
6835   /* Consume the token.  */
6836   cp_lexer_consume_token (parser->lexer);
6837
6838   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6839
6840   type1 = cp_parser_type_id (parser);
6841
6842   if (type1 == error_mark_node)
6843     return error_mark_node;
6844
6845   /* Build a trivial decl-specifier-seq.  */
6846   clear_decl_specs (&decl_specs);
6847   decl_specs.type = type1;
6848
6849   /* Call grokdeclarator to figure out what type this is.  */
6850   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6851                           /*initialized=*/0, /*attrlist=*/NULL);
6852
6853   if (binary)
6854     {
6855       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6856  
6857       type2 = cp_parser_type_id (parser);
6858
6859       if (type2 == error_mark_node)
6860         return error_mark_node;
6861
6862       /* Build a trivial decl-specifier-seq.  */
6863       clear_decl_specs (&decl_specs);
6864       decl_specs.type = type2;
6865
6866       /* Call grokdeclarator to figure out what type this is.  */
6867       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6868                               /*initialized=*/0, /*attrlist=*/NULL);
6869     }
6870
6871   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6872
6873   /* Complete the trait expression, which may mean either processing
6874      the trait expr now or saving it for template instantiation.  */
6875   return finish_trait_expr (kind, type1, type2);
6876 }
6877
6878 /* Statements [gram.stmt.stmt]  */
6879
6880 /* Parse a statement.
6881
6882    statement:
6883      labeled-statement
6884      expression-statement
6885      compound-statement
6886      selection-statement
6887      iteration-statement
6888      jump-statement
6889      declaration-statement
6890      try-block
6891
6892   IN_COMPOUND is true when the statement is nested inside a
6893   cp_parser_compound_statement; this matters for certain pragmas.
6894
6895   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6896   is a (possibly labeled) if statement which is not enclosed in braces
6897   and has an else clause.  This is used to implement -Wparentheses.  */
6898
6899 static void
6900 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6901                      bool in_compound, bool *if_p)
6902 {
6903   tree statement;
6904   cp_token *token;
6905   location_t statement_location;
6906
6907  restart:
6908   if (if_p != NULL)
6909     *if_p = false;
6910   /* There is no statement yet.  */
6911   statement = NULL_TREE;
6912   /* Peek at the next token.  */
6913   token = cp_lexer_peek_token (parser->lexer);
6914   /* Remember the location of the first token in the statement.  */
6915   statement_location = token->location;
6916   /* If this is a keyword, then that will often determine what kind of
6917      statement we have.  */
6918   if (token->type == CPP_KEYWORD)
6919     {
6920       enum rid keyword = token->keyword;
6921
6922       switch (keyword)
6923         {
6924         case RID_CASE:
6925         case RID_DEFAULT:
6926           /* Looks like a labeled-statement with a case label.
6927              Parse the label, and then use tail recursion to parse
6928              the statement.  */
6929           cp_parser_label_for_labeled_statement (parser);
6930           goto restart;
6931
6932         case RID_IF:
6933         case RID_SWITCH:
6934           statement = cp_parser_selection_statement (parser, if_p);
6935           break;
6936
6937         case RID_WHILE:
6938         case RID_DO:
6939         case RID_FOR:
6940           statement = cp_parser_iteration_statement (parser);
6941           break;
6942
6943         case RID_BREAK:
6944         case RID_CONTINUE:
6945         case RID_RETURN:
6946         case RID_GOTO:
6947           statement = cp_parser_jump_statement (parser);
6948           break;
6949
6950           /* Objective-C++ exception-handling constructs.  */
6951         case RID_AT_TRY:
6952         case RID_AT_CATCH:
6953         case RID_AT_FINALLY:
6954         case RID_AT_SYNCHRONIZED:
6955         case RID_AT_THROW:
6956           statement = cp_parser_objc_statement (parser);
6957           break;
6958
6959         case RID_TRY:
6960           statement = cp_parser_try_block (parser);
6961           break;
6962
6963         case RID_NAMESPACE:
6964           /* This must be a namespace alias definition.  */
6965           cp_parser_declaration_statement (parser);
6966           return;
6967           
6968         default:
6969           /* It might be a keyword like `int' that can start a
6970              declaration-statement.  */
6971           break;
6972         }
6973     }
6974   else if (token->type == CPP_NAME)
6975     {
6976       /* If the next token is a `:', then we are looking at a
6977          labeled-statement.  */
6978       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6979       if (token->type == CPP_COLON)
6980         {
6981           /* Looks like a labeled-statement with an ordinary label.
6982              Parse the label, and then use tail recursion to parse
6983              the statement.  */
6984           cp_parser_label_for_labeled_statement (parser);
6985           goto restart;
6986         }
6987     }
6988   /* Anything that starts with a `{' must be a compound-statement.  */
6989   else if (token->type == CPP_OPEN_BRACE)
6990     statement = cp_parser_compound_statement (parser, NULL, false);
6991   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6992      a statement all its own.  */
6993   else if (token->type == CPP_PRAGMA)
6994     {
6995       /* Only certain OpenMP pragmas are attached to statements, and thus
6996          are considered statements themselves.  All others are not.  In
6997          the context of a compound, accept the pragma as a "statement" and
6998          return so that we can check for a close brace.  Otherwise we
6999          require a real statement and must go back and read one.  */
7000       if (in_compound)
7001         cp_parser_pragma (parser, pragma_compound);
7002       else if (!cp_parser_pragma (parser, pragma_stmt))
7003         goto restart;
7004       return;
7005     }
7006   else if (token->type == CPP_EOF)
7007     {
7008       cp_parser_error (parser, "expected statement");
7009       return;
7010     }
7011
7012   /* Everything else must be a declaration-statement or an
7013      expression-statement.  Try for the declaration-statement
7014      first, unless we are looking at a `;', in which case we know that
7015      we have an expression-statement.  */
7016   if (!statement)
7017     {
7018       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7019         {
7020           cp_parser_parse_tentatively (parser);
7021           /* Try to parse the declaration-statement.  */
7022           cp_parser_declaration_statement (parser);
7023           /* If that worked, we're done.  */
7024           if (cp_parser_parse_definitely (parser))
7025             return;
7026         }
7027       /* Look for an expression-statement instead.  */
7028       statement = cp_parser_expression_statement (parser, in_statement_expr);
7029     }
7030
7031   /* Set the line number for the statement.  */
7032   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7033     SET_EXPR_LOCATION (statement, statement_location);
7034 }
7035
7036 /* Parse the label for a labeled-statement, i.e.
7037
7038    identifier :
7039    case constant-expression :
7040    default :
7041
7042    GNU Extension:
7043    case constant-expression ... constant-expression : statement
7044
7045    When a label is parsed without errors, the label is added to the
7046    parse tree by the finish_* functions, so this function doesn't
7047    have to return the label.  */
7048
7049 static void
7050 cp_parser_label_for_labeled_statement (cp_parser* parser)
7051 {
7052   cp_token *token;
7053
7054   /* The next token should be an identifier.  */
7055   token = cp_lexer_peek_token (parser->lexer);
7056   if (token->type != CPP_NAME
7057       && token->type != CPP_KEYWORD)
7058     {
7059       cp_parser_error (parser, "expected labeled-statement");
7060       return;
7061     }
7062
7063   switch (token->keyword)
7064     {
7065     case RID_CASE:
7066       {
7067         tree expr, expr_hi;
7068         cp_token *ellipsis;
7069
7070         /* Consume the `case' token.  */
7071         cp_lexer_consume_token (parser->lexer);
7072         /* Parse the constant-expression.  */
7073         expr = cp_parser_constant_expression (parser,
7074                                               /*allow_non_constant_p=*/false,
7075                                               NULL);
7076
7077         ellipsis = cp_lexer_peek_token (parser->lexer);
7078         if (ellipsis->type == CPP_ELLIPSIS)
7079           {
7080             /* Consume the `...' token.  */
7081             cp_lexer_consume_token (parser->lexer);
7082             expr_hi =
7083               cp_parser_constant_expression (parser,
7084                                              /*allow_non_constant_p=*/false,
7085                                              NULL);
7086             /* We don't need to emit warnings here, as the common code
7087                will do this for us.  */
7088           }
7089         else
7090           expr_hi = NULL_TREE;
7091
7092         if (parser->in_switch_statement_p)
7093           finish_case_label (expr, expr_hi);
7094         else
7095           error ("%Hcase label %qE not within a switch statement",
7096                  &token->location, expr);
7097       }
7098       break;
7099
7100     case RID_DEFAULT:
7101       /* Consume the `default' token.  */
7102       cp_lexer_consume_token (parser->lexer);
7103
7104       if (parser->in_switch_statement_p)
7105         finish_case_label (NULL_TREE, NULL_TREE);
7106       else
7107         error ("%Hcase label not within a switch statement", &token->location);
7108       break;
7109
7110     default:
7111       /* Anything else must be an ordinary label.  */
7112       finish_label_stmt (cp_parser_identifier (parser));
7113       break;
7114     }
7115
7116   /* Require the `:' token.  */
7117   cp_parser_require (parser, CPP_COLON, "%<:%>");
7118 }
7119
7120 /* Parse an expression-statement.
7121
7122    expression-statement:
7123      expression [opt] ;
7124
7125    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7126    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7127    indicates whether this expression-statement is part of an
7128    expression statement.  */
7129
7130 static tree
7131 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7132 {
7133   tree statement = NULL_TREE;
7134
7135   /* If the next token is a ';', then there is no expression
7136      statement.  */
7137   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7138     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7139
7140   /* Consume the final `;'.  */
7141   cp_parser_consume_semicolon_at_end_of_statement (parser);
7142
7143   if (in_statement_expr
7144       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7145     /* This is the final expression statement of a statement
7146        expression.  */
7147     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7148   else if (statement)
7149     statement = finish_expr_stmt (statement);
7150   else
7151     finish_stmt ();
7152
7153   return statement;
7154 }
7155
7156 /* Parse a compound-statement.
7157
7158    compound-statement:
7159      { statement-seq [opt] }
7160
7161    GNU extension:
7162
7163    compound-statement:
7164      { label-declaration-seq [opt] statement-seq [opt] }
7165
7166    label-declaration-seq:
7167      label-declaration
7168      label-declaration-seq label-declaration
7169
7170    Returns a tree representing the statement.  */
7171
7172 static tree
7173 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7174                               bool in_try)
7175 {
7176   tree compound_stmt;
7177
7178   /* Consume the `{'.  */
7179   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7180     return error_mark_node;
7181   /* Begin the compound-statement.  */
7182   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7183   /* If the next keyword is `__label__' we have a label declaration.  */
7184   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7185     cp_parser_label_declaration (parser);
7186   /* Parse an (optional) statement-seq.  */
7187   cp_parser_statement_seq_opt (parser, in_statement_expr);
7188   /* Finish the compound-statement.  */
7189   finish_compound_stmt (compound_stmt);
7190   /* Consume the `}'.  */
7191   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7192
7193   return compound_stmt;
7194 }
7195
7196 /* Parse an (optional) statement-seq.
7197
7198    statement-seq:
7199      statement
7200      statement-seq [opt] statement  */
7201
7202 static void
7203 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7204 {
7205   /* Scan statements until there aren't any more.  */
7206   while (true)
7207     {
7208       cp_token *token = cp_lexer_peek_token (parser->lexer);
7209
7210       /* If we're looking at a `}', then we've run out of statements.  */
7211       if (token->type == CPP_CLOSE_BRACE
7212           || token->type == CPP_EOF
7213           || token->type == CPP_PRAGMA_EOL)
7214         break;
7215       
7216       /* If we are in a compound statement and find 'else' then
7217          something went wrong.  */
7218       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7219         {
7220           if (parser->in_statement & IN_IF_STMT) 
7221             break;
7222           else
7223             {
7224               token = cp_lexer_consume_token (parser->lexer);
7225               error ("%H%<else%> without a previous %<if%>", &token->location);
7226             }
7227         }
7228
7229       /* Parse the statement.  */
7230       cp_parser_statement (parser, in_statement_expr, true, NULL);
7231     }
7232 }
7233
7234 /* Parse a selection-statement.
7235
7236    selection-statement:
7237      if ( condition ) statement
7238      if ( condition ) statement else statement
7239      switch ( condition ) statement
7240
7241    Returns the new IF_STMT or SWITCH_STMT.
7242
7243    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7244    is a (possibly labeled) if statement which is not enclosed in
7245    braces and has an else clause.  This is used to implement
7246    -Wparentheses.  */
7247
7248 static tree
7249 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7250 {
7251   cp_token *token;
7252   enum rid keyword;
7253
7254   if (if_p != NULL)
7255     *if_p = false;
7256
7257   /* Peek at the next token.  */
7258   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7259
7260   /* See what kind of keyword it is.  */
7261   keyword = token->keyword;
7262   switch (keyword)
7263     {
7264     case RID_IF:
7265     case RID_SWITCH:
7266       {
7267         tree statement;
7268         tree condition;
7269
7270         /* Look for the `('.  */
7271         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7272           {
7273             cp_parser_skip_to_end_of_statement (parser);
7274             return error_mark_node;
7275           }
7276
7277         /* Begin the selection-statement.  */
7278         if (keyword == RID_IF)
7279           statement = begin_if_stmt ();
7280         else
7281           statement = begin_switch_stmt ();
7282
7283         /* Parse the condition.  */
7284         condition = cp_parser_condition (parser);
7285         /* Look for the `)'.  */
7286         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7287           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7288                                                  /*consume_paren=*/true);
7289
7290         if (keyword == RID_IF)
7291           {
7292             bool nested_if;
7293             unsigned char in_statement;
7294
7295             /* Add the condition.  */
7296             finish_if_stmt_cond (condition, statement);
7297
7298             /* Parse the then-clause.  */
7299             in_statement = parser->in_statement;
7300             parser->in_statement |= IN_IF_STMT;
7301             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7302               {
7303                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7304                 add_stmt (build_empty_stmt ());
7305                 cp_lexer_consume_token (parser->lexer);
7306                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7307                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7308                               "empty body in an %<if%> statement");
7309                 nested_if = false;
7310               }
7311             else
7312               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7313             parser->in_statement = in_statement;
7314
7315             finish_then_clause (statement);
7316
7317             /* If the next token is `else', parse the else-clause.  */
7318             if (cp_lexer_next_token_is_keyword (parser->lexer,
7319                                                 RID_ELSE))
7320               {
7321                 /* Consume the `else' keyword.  */
7322                 cp_lexer_consume_token (parser->lexer);
7323                 begin_else_clause (statement);
7324                 /* Parse the else-clause.  */
7325                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7326                   {
7327                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7328                                 OPT_Wempty_body, "suggest braces around "
7329                                 "empty body in an %<else%> statement");
7330                     add_stmt (build_empty_stmt ());
7331                     cp_lexer_consume_token (parser->lexer);
7332                   }
7333                 else
7334                   cp_parser_implicitly_scoped_statement (parser, NULL);
7335
7336                 finish_else_clause (statement);
7337
7338                 /* If we are currently parsing a then-clause, then
7339                    IF_P will not be NULL.  We set it to true to
7340                    indicate that this if statement has an else clause.
7341                    This may trigger the Wparentheses warning below
7342                    when we get back up to the parent if statement.  */
7343                 if (if_p != NULL)
7344                   *if_p = true;
7345               }
7346             else
7347               {
7348                 /* This if statement does not have an else clause.  If
7349                    NESTED_IF is true, then the then-clause is an if
7350                    statement which does have an else clause.  We warn
7351                    about the potential ambiguity.  */
7352                 if (nested_if)
7353                   warning (OPT_Wparentheses,
7354                            ("%Hsuggest explicit braces "
7355                             "to avoid ambiguous %<else%>"),
7356                            EXPR_LOCUS (statement));
7357               }
7358
7359             /* Now we're all done with the if-statement.  */
7360             finish_if_stmt (statement);
7361           }
7362         else
7363           {
7364             bool in_switch_statement_p;
7365             unsigned char in_statement;
7366
7367             /* Add the condition.  */
7368             finish_switch_cond (condition, statement);
7369
7370             /* Parse the body of the switch-statement.  */
7371             in_switch_statement_p = parser->in_switch_statement_p;
7372             in_statement = parser->in_statement;
7373             parser->in_switch_statement_p = true;
7374             parser->in_statement |= IN_SWITCH_STMT;
7375             cp_parser_implicitly_scoped_statement (parser, NULL);
7376             parser->in_switch_statement_p = in_switch_statement_p;
7377             parser->in_statement = in_statement;
7378
7379             /* Now we're all done with the switch-statement.  */
7380             finish_switch_stmt (statement);
7381           }
7382
7383         return statement;
7384       }
7385       break;
7386
7387     default:
7388       cp_parser_error (parser, "expected selection-statement");
7389       return error_mark_node;
7390     }
7391 }
7392
7393 /* Parse a condition.
7394
7395    condition:
7396      expression
7397      type-specifier-seq declarator = initializer-clause
7398      type-specifier-seq declarator braced-init-list
7399
7400    GNU Extension:
7401
7402    condition:
7403      type-specifier-seq declarator asm-specification [opt]
7404        attributes [opt] = assignment-expression
7405
7406    Returns the expression that should be tested.  */
7407
7408 static tree
7409 cp_parser_condition (cp_parser* parser)
7410 {
7411   cp_decl_specifier_seq type_specifiers;
7412   const char *saved_message;
7413
7414   /* Try the declaration first.  */
7415   cp_parser_parse_tentatively (parser);
7416   /* New types are not allowed in the type-specifier-seq for a
7417      condition.  */
7418   saved_message = parser->type_definition_forbidden_message;
7419   parser->type_definition_forbidden_message
7420     = "types may not be defined in conditions";
7421   /* Parse the type-specifier-seq.  */
7422   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7423                                 /*is_trailing_return=*/false,
7424                                 &type_specifiers);
7425   /* Restore the saved message.  */
7426   parser->type_definition_forbidden_message = saved_message;
7427   /* If all is well, we might be looking at a declaration.  */
7428   if (!cp_parser_error_occurred (parser))
7429     {
7430       tree decl;
7431       tree asm_specification;
7432       tree attributes;
7433       cp_declarator *declarator;
7434       tree initializer = NULL_TREE;
7435
7436       /* Parse the declarator.  */
7437       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7438                                          /*ctor_dtor_or_conv_p=*/NULL,
7439                                          /*parenthesized_p=*/NULL,
7440                                          /*member_p=*/false);
7441       /* Parse the attributes.  */
7442       attributes = cp_parser_attributes_opt (parser);
7443       /* Parse the asm-specification.  */
7444       asm_specification = cp_parser_asm_specification_opt (parser);
7445       /* If the next token is not an `=' or '{', then we might still be
7446          looking at an expression.  For example:
7447
7448            if (A(a).x)
7449
7450          looks like a decl-specifier-seq and a declarator -- but then
7451          there is no `=', so this is an expression.  */
7452       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7453           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7454         cp_parser_simulate_error (parser);
7455         
7456       /* If we did see an `=' or '{', then we are looking at a declaration
7457          for sure.  */
7458       if (cp_parser_parse_definitely (parser))
7459         {
7460           tree pushed_scope;
7461           bool non_constant_p;
7462           bool flags = LOOKUP_ONLYCONVERTING;
7463
7464           /* Create the declaration.  */
7465           decl = start_decl (declarator, &type_specifiers,
7466                              /*initialized_p=*/true,
7467                              attributes, /*prefix_attributes=*/NULL_TREE,
7468                              &pushed_scope);
7469
7470           /* Parse the initializer.  */
7471           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7472             {
7473               initializer = cp_parser_braced_list (parser, &non_constant_p);
7474               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7475               flags = 0;
7476             }
7477           else
7478             {
7479               /* Consume the `='.  */
7480               cp_parser_require (parser, CPP_EQ, "%<=%>");
7481               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7482             }
7483           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7484             maybe_warn_cpp0x ("extended initializer lists");
7485
7486           if (!non_constant_p)
7487             initializer = fold_non_dependent_expr (initializer);
7488
7489           /* Process the initializer.  */
7490           cp_finish_decl (decl,
7491                           initializer, !non_constant_p,
7492                           asm_specification,
7493                           flags);
7494
7495           if (pushed_scope)
7496             pop_scope (pushed_scope);
7497
7498           return convert_from_reference (decl);
7499         }
7500     }
7501   /* If we didn't even get past the declarator successfully, we are
7502      definitely not looking at a declaration.  */
7503   else
7504     cp_parser_abort_tentative_parse (parser);
7505
7506   /* Otherwise, we are looking at an expression.  */
7507   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7508 }
7509
7510 /* Parse an iteration-statement.
7511
7512    iteration-statement:
7513      while ( condition ) statement
7514      do statement while ( expression ) ;
7515      for ( for-init-statement condition [opt] ; expression [opt] )
7516        statement
7517
7518    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7519
7520 static tree
7521 cp_parser_iteration_statement (cp_parser* parser)
7522 {
7523   cp_token *token;
7524   enum rid keyword;
7525   tree statement;
7526   unsigned char in_statement;
7527
7528   /* Peek at the next token.  */
7529   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7530   if (!token)
7531     return error_mark_node;
7532
7533   /* Remember whether or not we are already within an iteration
7534      statement.  */
7535   in_statement = parser->in_statement;
7536
7537   /* See what kind of keyword it is.  */
7538   keyword = token->keyword;
7539   switch (keyword)
7540     {
7541     case RID_WHILE:
7542       {
7543         tree condition;
7544
7545         /* Begin the while-statement.  */
7546         statement = begin_while_stmt ();
7547         /* Look for the `('.  */
7548         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7549         /* Parse the condition.  */
7550         condition = cp_parser_condition (parser);
7551         finish_while_stmt_cond (condition, statement);
7552         /* Look for the `)'.  */
7553         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7554         /* Parse the dependent statement.  */
7555         parser->in_statement = IN_ITERATION_STMT;
7556         cp_parser_already_scoped_statement (parser);
7557         parser->in_statement = in_statement;
7558         /* We're done with the while-statement.  */
7559         finish_while_stmt (statement);
7560       }
7561       break;
7562
7563     case RID_DO:
7564       {
7565         tree expression;
7566
7567         /* Begin the do-statement.  */
7568         statement = begin_do_stmt ();
7569         /* Parse the body of the do-statement.  */
7570         parser->in_statement = IN_ITERATION_STMT;
7571         cp_parser_implicitly_scoped_statement (parser, NULL);
7572         parser->in_statement = in_statement;
7573         finish_do_body (statement);
7574         /* Look for the `while' keyword.  */
7575         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7576         /* Look for the `('.  */
7577         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7578         /* Parse the expression.  */
7579         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7580         /* We're done with the do-statement.  */
7581         finish_do_stmt (expression, statement);
7582         /* Look for the `)'.  */
7583         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7584         /* Look for the `;'.  */
7585         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7586       }
7587       break;
7588
7589     case RID_FOR:
7590       {
7591         tree condition = NULL_TREE;
7592         tree expression = NULL_TREE;
7593
7594         /* Begin the for-statement.  */
7595         statement = begin_for_stmt ();
7596         /* Look for the `('.  */
7597         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7598         /* Parse the initialization.  */
7599         cp_parser_for_init_statement (parser);
7600         finish_for_init_stmt (statement);
7601
7602         /* If there's a condition, process it.  */
7603         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7604           condition = cp_parser_condition (parser);
7605         finish_for_cond (condition, statement);
7606         /* Look for the `;'.  */
7607         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7608
7609         /* If there's an expression, process it.  */
7610         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7611           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7612         finish_for_expr (expression, statement);
7613         /* Look for the `)'.  */
7614         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7615
7616         /* Parse the body of the for-statement.  */
7617         parser->in_statement = IN_ITERATION_STMT;
7618         cp_parser_already_scoped_statement (parser);
7619         parser->in_statement = in_statement;
7620
7621         /* We're done with the for-statement.  */
7622         finish_for_stmt (statement);
7623       }
7624       break;
7625
7626     default:
7627       cp_parser_error (parser, "expected iteration-statement");
7628       statement = error_mark_node;
7629       break;
7630     }
7631
7632   return statement;
7633 }
7634
7635 /* Parse a for-init-statement.
7636
7637    for-init-statement:
7638      expression-statement
7639      simple-declaration  */
7640
7641 static void
7642 cp_parser_for_init_statement (cp_parser* parser)
7643 {
7644   /* If the next token is a `;', then we have an empty
7645      expression-statement.  Grammatically, this is also a
7646      simple-declaration, but an invalid one, because it does not
7647      declare anything.  Therefore, if we did not handle this case
7648      specially, we would issue an error message about an invalid
7649      declaration.  */
7650   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7651     {
7652       /* We're going to speculatively look for a declaration, falling back
7653          to an expression, if necessary.  */
7654       cp_parser_parse_tentatively (parser);
7655       /* Parse the declaration.  */
7656       cp_parser_simple_declaration (parser,
7657                                     /*function_definition_allowed_p=*/false);
7658       /* If the tentative parse failed, then we shall need to look for an
7659          expression-statement.  */
7660       if (cp_parser_parse_definitely (parser))
7661         return;
7662     }
7663
7664   cp_parser_expression_statement (parser, false);
7665 }
7666
7667 /* Parse a jump-statement.
7668
7669    jump-statement:
7670      break ;
7671      continue ;
7672      return expression [opt] ;
7673      return braced-init-list ;
7674      goto identifier ;
7675
7676    GNU extension:
7677
7678    jump-statement:
7679      goto * expression ;
7680
7681    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7682
7683 static tree
7684 cp_parser_jump_statement (cp_parser* parser)
7685 {
7686   tree statement = error_mark_node;
7687   cp_token *token;
7688   enum rid keyword;
7689   unsigned char in_statement;
7690
7691   /* Peek at the next token.  */
7692   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7693   if (!token)
7694     return error_mark_node;
7695
7696   /* See what kind of keyword it is.  */
7697   keyword = token->keyword;
7698   switch (keyword)
7699     {
7700     case RID_BREAK:
7701       in_statement = parser->in_statement & ~IN_IF_STMT;      
7702       switch (in_statement)
7703         {
7704         case 0:
7705           error ("%Hbreak statement not within loop or switch", &token->location);
7706           break;
7707         default:
7708           gcc_assert ((in_statement & IN_SWITCH_STMT)
7709                       || in_statement == IN_ITERATION_STMT);
7710           statement = finish_break_stmt ();
7711           break;
7712         case IN_OMP_BLOCK:
7713           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7714           break;
7715         case IN_OMP_FOR:
7716           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7717           break;
7718         }
7719       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7720       break;
7721
7722     case RID_CONTINUE:
7723       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7724         {
7725         case 0:
7726           error ("%Hcontinue statement not within a loop", &token->location);
7727           break;
7728         case IN_ITERATION_STMT:
7729         case IN_OMP_FOR:
7730           statement = finish_continue_stmt ();
7731           break;
7732         case IN_OMP_BLOCK:
7733           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7734           break;
7735         default:
7736           gcc_unreachable ();
7737         }
7738       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7739       break;
7740
7741     case RID_RETURN:
7742       {
7743         tree expr;
7744         bool expr_non_constant_p;
7745
7746         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7747           {
7748             maybe_warn_cpp0x ("extended initializer lists");
7749             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7750           }
7751         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7752           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7753         else
7754           /* If the next token is a `;', then there is no
7755              expression.  */
7756           expr = NULL_TREE;
7757         /* Build the return-statement.  */
7758         statement = finish_return_stmt (expr);
7759         /* Look for the final `;'.  */
7760         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7761       }
7762       break;
7763
7764     case RID_GOTO:
7765       /* Create the goto-statement.  */
7766       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7767         {
7768           /* Issue a warning about this use of a GNU extension.  */
7769           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7770           /* Consume the '*' token.  */
7771           cp_lexer_consume_token (parser->lexer);
7772           /* Parse the dependent expression.  */
7773           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7774         }
7775       else
7776         finish_goto_stmt (cp_parser_identifier (parser));
7777       /* Look for the final `;'.  */
7778       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7779       break;
7780
7781     default:
7782       cp_parser_error (parser, "expected jump-statement");
7783       break;
7784     }
7785
7786   return statement;
7787 }
7788
7789 /* Parse a declaration-statement.
7790
7791    declaration-statement:
7792      block-declaration  */
7793
7794 static void
7795 cp_parser_declaration_statement (cp_parser* parser)
7796 {
7797   void *p;
7798
7799   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7800   p = obstack_alloc (&declarator_obstack, 0);
7801
7802  /* Parse the block-declaration.  */
7803   cp_parser_block_declaration (parser, /*statement_p=*/true);
7804
7805   /* Free any declarators allocated.  */
7806   obstack_free (&declarator_obstack, p);
7807
7808   /* Finish off the statement.  */
7809   finish_stmt ();
7810 }
7811
7812 /* Some dependent statements (like `if (cond) statement'), are
7813    implicitly in their own scope.  In other words, if the statement is
7814    a single statement (as opposed to a compound-statement), it is
7815    none-the-less treated as if it were enclosed in braces.  Any
7816    declarations appearing in the dependent statement are out of scope
7817    after control passes that point.  This function parses a statement,
7818    but ensures that is in its own scope, even if it is not a
7819    compound-statement.
7820
7821    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7822    is a (possibly labeled) if statement which is not enclosed in
7823    braces and has an else clause.  This is used to implement
7824    -Wparentheses.
7825
7826    Returns the new statement.  */
7827
7828 static tree
7829 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7830 {
7831   tree statement;
7832
7833   if (if_p != NULL)
7834     *if_p = false;
7835
7836   /* Mark if () ; with a special NOP_EXPR.  */
7837   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7838     {
7839       cp_lexer_consume_token (parser->lexer);
7840       statement = add_stmt (build_empty_stmt ());
7841     }
7842   /* if a compound is opened, we simply parse the statement directly.  */
7843   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7844     statement = cp_parser_compound_statement (parser, NULL, false);
7845   /* If the token is not a `{', then we must take special action.  */
7846   else
7847     {
7848       /* Create a compound-statement.  */
7849       statement = begin_compound_stmt (0);
7850       /* Parse the dependent-statement.  */
7851       cp_parser_statement (parser, NULL_TREE, false, if_p);
7852       /* Finish the dummy compound-statement.  */
7853       finish_compound_stmt (statement);
7854     }
7855
7856   /* Return the statement.  */
7857   return statement;
7858 }
7859
7860 /* For some dependent statements (like `while (cond) statement'), we
7861    have already created a scope.  Therefore, even if the dependent
7862    statement is a compound-statement, we do not want to create another
7863    scope.  */
7864
7865 static void
7866 cp_parser_already_scoped_statement (cp_parser* parser)
7867 {
7868   /* If the token is a `{', then we must take special action.  */
7869   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7870     cp_parser_statement (parser, NULL_TREE, false, NULL);
7871   else
7872     {
7873       /* Avoid calling cp_parser_compound_statement, so that we
7874          don't create a new scope.  Do everything else by hand.  */
7875       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7876       /* If the next keyword is `__label__' we have a label declaration.  */
7877       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7878         cp_parser_label_declaration (parser);
7879       /* Parse an (optional) statement-seq.  */
7880       cp_parser_statement_seq_opt (parser, NULL_TREE);
7881       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7882     }
7883 }
7884
7885 /* Declarations [gram.dcl.dcl] */
7886
7887 /* Parse an optional declaration-sequence.
7888
7889    declaration-seq:
7890      declaration
7891      declaration-seq declaration  */
7892
7893 static void
7894 cp_parser_declaration_seq_opt (cp_parser* parser)
7895 {
7896   while (true)
7897     {
7898       cp_token *token;
7899
7900       token = cp_lexer_peek_token (parser->lexer);
7901
7902       if (token->type == CPP_CLOSE_BRACE
7903           || token->type == CPP_EOF
7904           || token->type == CPP_PRAGMA_EOL)
7905         break;
7906
7907       if (token->type == CPP_SEMICOLON)
7908         {
7909           /* A declaration consisting of a single semicolon is
7910              invalid.  Allow it unless we're being pedantic.  */
7911           cp_lexer_consume_token (parser->lexer);
7912           if (!in_system_header)
7913             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7914           continue;
7915         }
7916
7917       /* If we're entering or exiting a region that's implicitly
7918          extern "C", modify the lang context appropriately.  */
7919       if (!parser->implicit_extern_c && token->implicit_extern_c)
7920         {
7921           push_lang_context (lang_name_c);
7922           parser->implicit_extern_c = true;
7923         }
7924       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7925         {
7926           pop_lang_context ();
7927           parser->implicit_extern_c = false;
7928         }
7929
7930       if (token->type == CPP_PRAGMA)
7931         {
7932           /* A top-level declaration can consist solely of a #pragma.
7933              A nested declaration cannot, so this is done here and not
7934              in cp_parser_declaration.  (A #pragma at block scope is
7935              handled in cp_parser_statement.)  */
7936           cp_parser_pragma (parser, pragma_external);
7937           continue;
7938         }
7939
7940       /* Parse the declaration itself.  */
7941       cp_parser_declaration (parser);
7942     }
7943 }
7944
7945 /* Parse a declaration.
7946
7947    declaration:
7948      block-declaration
7949      function-definition
7950      template-declaration
7951      explicit-instantiation
7952      explicit-specialization
7953      linkage-specification
7954      namespace-definition
7955
7956    GNU extension:
7957
7958    declaration:
7959       __extension__ declaration */
7960
7961 static void
7962 cp_parser_declaration (cp_parser* parser)
7963 {
7964   cp_token token1;
7965   cp_token token2;
7966   int saved_pedantic;
7967   void *p;
7968
7969   /* Check for the `__extension__' keyword.  */
7970   if (cp_parser_extension_opt (parser, &saved_pedantic))
7971     {
7972       /* Parse the qualified declaration.  */
7973       cp_parser_declaration (parser);
7974       /* Restore the PEDANTIC flag.  */
7975       pedantic = saved_pedantic;
7976
7977       return;
7978     }
7979
7980   /* Try to figure out what kind of declaration is present.  */
7981   token1 = *cp_lexer_peek_token (parser->lexer);
7982
7983   if (token1.type != CPP_EOF)
7984     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7985   else
7986     {
7987       token2.type = CPP_EOF;
7988       token2.keyword = RID_MAX;
7989     }
7990
7991   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7992   p = obstack_alloc (&declarator_obstack, 0);
7993
7994   /* If the next token is `extern' and the following token is a string
7995      literal, then we have a linkage specification.  */
7996   if (token1.keyword == RID_EXTERN
7997       && cp_parser_is_string_literal (&token2))
7998     cp_parser_linkage_specification (parser);
7999   /* If the next token is `template', then we have either a template
8000      declaration, an explicit instantiation, or an explicit
8001      specialization.  */
8002   else if (token1.keyword == RID_TEMPLATE)
8003     {
8004       /* `template <>' indicates a template specialization.  */
8005       if (token2.type == CPP_LESS
8006           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8007         cp_parser_explicit_specialization (parser);
8008       /* `template <' indicates a template declaration.  */
8009       else if (token2.type == CPP_LESS)
8010         cp_parser_template_declaration (parser, /*member_p=*/false);
8011       /* Anything else must be an explicit instantiation.  */
8012       else
8013         cp_parser_explicit_instantiation (parser);
8014     }
8015   /* If the next token is `export', then we have a template
8016      declaration.  */
8017   else if (token1.keyword == RID_EXPORT)
8018     cp_parser_template_declaration (parser, /*member_p=*/false);
8019   /* If the next token is `extern', 'static' or 'inline' and the one
8020      after that is `template', we have a GNU extended explicit
8021      instantiation directive.  */
8022   else if (cp_parser_allow_gnu_extensions_p (parser)
8023            && (token1.keyword == RID_EXTERN
8024                || token1.keyword == RID_STATIC
8025                || token1.keyword == RID_INLINE)
8026            && token2.keyword == RID_TEMPLATE)
8027     cp_parser_explicit_instantiation (parser);
8028   /* If the next token is `namespace', check for a named or unnamed
8029      namespace definition.  */
8030   else if (token1.keyword == RID_NAMESPACE
8031            && (/* A named namespace definition.  */
8032                (token2.type == CPP_NAME
8033                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8034                     != CPP_EQ))
8035                /* An unnamed namespace definition.  */
8036                || token2.type == CPP_OPEN_BRACE
8037                || token2.keyword == RID_ATTRIBUTE))
8038     cp_parser_namespace_definition (parser);
8039   /* An inline (associated) namespace definition.  */
8040   else if (token1.keyword == RID_INLINE
8041            && token2.keyword == RID_NAMESPACE)
8042     cp_parser_namespace_definition (parser);
8043   /* Objective-C++ declaration/definition.  */
8044   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8045     cp_parser_objc_declaration (parser);
8046   /* We must have either a block declaration or a function
8047      definition.  */
8048   else
8049     /* Try to parse a block-declaration, or a function-definition.  */
8050     cp_parser_block_declaration (parser, /*statement_p=*/false);
8051
8052   /* Free any declarators allocated.  */
8053   obstack_free (&declarator_obstack, p);
8054 }
8055
8056 /* Parse a block-declaration.
8057
8058    block-declaration:
8059      simple-declaration
8060      asm-definition
8061      namespace-alias-definition
8062      using-declaration
8063      using-directive
8064
8065    GNU Extension:
8066
8067    block-declaration:
8068      __extension__ block-declaration
8069
8070    C++0x Extension:
8071
8072    block-declaration:
8073      static_assert-declaration
8074
8075    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8076    part of a declaration-statement.  */
8077
8078 static void
8079 cp_parser_block_declaration (cp_parser *parser,
8080                              bool      statement_p)
8081 {
8082   cp_token *token1;
8083   int saved_pedantic;
8084
8085   /* Check for the `__extension__' keyword.  */
8086   if (cp_parser_extension_opt (parser, &saved_pedantic))
8087     {
8088       /* Parse the qualified declaration.  */
8089       cp_parser_block_declaration (parser, statement_p);
8090       /* Restore the PEDANTIC flag.  */
8091       pedantic = saved_pedantic;
8092
8093       return;
8094     }
8095
8096   /* Peek at the next token to figure out which kind of declaration is
8097      present.  */
8098   token1 = cp_lexer_peek_token (parser->lexer);
8099
8100   /* If the next keyword is `asm', we have an asm-definition.  */
8101   if (token1->keyword == RID_ASM)
8102     {
8103       if (statement_p)
8104         cp_parser_commit_to_tentative_parse (parser);
8105       cp_parser_asm_definition (parser);
8106     }
8107   /* If the next keyword is `namespace', we have a
8108      namespace-alias-definition.  */
8109   else if (token1->keyword == RID_NAMESPACE)
8110     cp_parser_namespace_alias_definition (parser);
8111   /* If the next keyword is `using', we have either a
8112      using-declaration or a using-directive.  */
8113   else if (token1->keyword == RID_USING)
8114     {
8115       cp_token *token2;
8116
8117       if (statement_p)
8118         cp_parser_commit_to_tentative_parse (parser);
8119       /* If the token after `using' is `namespace', then we have a
8120          using-directive.  */
8121       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8122       if (token2->keyword == RID_NAMESPACE)
8123         cp_parser_using_directive (parser);
8124       /* Otherwise, it's a using-declaration.  */
8125       else
8126         cp_parser_using_declaration (parser,
8127                                      /*access_declaration_p=*/false);
8128     }
8129   /* If the next keyword is `__label__' we have a misplaced label
8130      declaration.  */
8131   else if (token1->keyword == RID_LABEL)
8132     {
8133       cp_lexer_consume_token (parser->lexer);
8134       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8135       cp_parser_skip_to_end_of_statement (parser);
8136       /* If the next token is now a `;', consume it.  */
8137       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8138         cp_lexer_consume_token (parser->lexer);
8139     }
8140   /* If the next token is `static_assert' we have a static assertion.  */
8141   else if (token1->keyword == RID_STATIC_ASSERT)
8142     cp_parser_static_assert (parser, /*member_p=*/false);
8143   /* Anything else must be a simple-declaration.  */
8144   else
8145     cp_parser_simple_declaration (parser, !statement_p);
8146 }
8147
8148 /* Parse a simple-declaration.
8149
8150    simple-declaration:
8151      decl-specifier-seq [opt] init-declarator-list [opt] ;
8152
8153    init-declarator-list:
8154      init-declarator
8155      init-declarator-list , init-declarator
8156
8157    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8158    function-definition as a simple-declaration.  */
8159
8160 static void
8161 cp_parser_simple_declaration (cp_parser* parser,
8162                               bool function_definition_allowed_p)
8163 {
8164   cp_decl_specifier_seq decl_specifiers;
8165   int declares_class_or_enum;
8166   bool saw_declarator;
8167
8168   /* Defer access checks until we know what is being declared; the
8169      checks for names appearing in the decl-specifier-seq should be
8170      done as if we were in the scope of the thing being declared.  */
8171   push_deferring_access_checks (dk_deferred);
8172
8173   /* Parse the decl-specifier-seq.  We have to keep track of whether
8174      or not the decl-specifier-seq declares a named class or
8175      enumeration type, since that is the only case in which the
8176      init-declarator-list is allowed to be empty.
8177
8178      [dcl.dcl]
8179
8180      In a simple-declaration, the optional init-declarator-list can be
8181      omitted only when declaring a class or enumeration, that is when
8182      the decl-specifier-seq contains either a class-specifier, an
8183      elaborated-type-specifier, or an enum-specifier.  */
8184   cp_parser_decl_specifier_seq (parser,
8185                                 CP_PARSER_FLAGS_OPTIONAL,
8186                                 &decl_specifiers,
8187                                 &declares_class_or_enum);
8188   /* We no longer need to defer access checks.  */
8189   stop_deferring_access_checks ();
8190
8191   /* In a block scope, a valid declaration must always have a
8192      decl-specifier-seq.  By not trying to parse declarators, we can
8193      resolve the declaration/expression ambiguity more quickly.  */
8194   if (!function_definition_allowed_p
8195       && !decl_specifiers.any_specifiers_p)
8196     {
8197       cp_parser_error (parser, "expected declaration");
8198       goto done;
8199     }
8200
8201   /* If the next two tokens are both identifiers, the code is
8202      erroneous. The usual cause of this situation is code like:
8203
8204        T t;
8205
8206      where "T" should name a type -- but does not.  */
8207   if (!decl_specifiers.type
8208       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8209     {
8210       /* If parsing tentatively, we should commit; we really are
8211          looking at a declaration.  */
8212       cp_parser_commit_to_tentative_parse (parser);
8213       /* Give up.  */
8214       goto done;
8215     }
8216
8217   /* If we have seen at least one decl-specifier, and the next token
8218      is not a parenthesis, then we must be looking at a declaration.
8219      (After "int (" we might be looking at a functional cast.)  */
8220   if (decl_specifiers.any_specifiers_p
8221       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8222       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8223       && !cp_parser_error_occurred (parser))
8224     cp_parser_commit_to_tentative_parse (parser);
8225
8226   /* Keep going until we hit the `;' at the end of the simple
8227      declaration.  */
8228   saw_declarator = false;
8229   while (cp_lexer_next_token_is_not (parser->lexer,
8230                                      CPP_SEMICOLON))
8231     {
8232       cp_token *token;
8233       bool function_definition_p;
8234       tree decl;
8235
8236       if (saw_declarator)
8237         {
8238           /* If we are processing next declarator, coma is expected */
8239           token = cp_lexer_peek_token (parser->lexer);
8240           gcc_assert (token->type == CPP_COMMA);
8241           cp_lexer_consume_token (parser->lexer);
8242         }
8243       else
8244         saw_declarator = true;
8245
8246       /* Parse the init-declarator.  */
8247       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8248                                         /*checks=*/NULL,
8249                                         function_definition_allowed_p,
8250                                         /*member_p=*/false,
8251                                         declares_class_or_enum,
8252                                         &function_definition_p);
8253       /* If an error occurred while parsing tentatively, exit quickly.
8254          (That usually happens when in the body of a function; each
8255          statement is treated as a declaration-statement until proven
8256          otherwise.)  */
8257       if (cp_parser_error_occurred (parser))
8258         goto done;
8259       /* Handle function definitions specially.  */
8260       if (function_definition_p)
8261         {
8262           /* If the next token is a `,', then we are probably
8263              processing something like:
8264
8265                void f() {}, *p;
8266
8267              which is erroneous.  */
8268           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8269             {
8270               cp_token *token = cp_lexer_peek_token (parser->lexer);
8271               error ("%Hmixing declarations and function-definitions is forbidden",
8272                      &token->location);
8273             }
8274           /* Otherwise, we're done with the list of declarators.  */
8275           else
8276             {
8277               pop_deferring_access_checks ();
8278               return;
8279             }
8280         }
8281       /* The next token should be either a `,' or a `;'.  */
8282       token = cp_lexer_peek_token (parser->lexer);
8283       /* If it's a `,', there are more declarators to come.  */
8284       if (token->type == CPP_COMMA)
8285         /* will be consumed next time around */;
8286       /* If it's a `;', we are done.  */
8287       else if (token->type == CPP_SEMICOLON)
8288         break;
8289       /* Anything else is an error.  */
8290       else
8291         {
8292           /* If we have already issued an error message we don't need
8293              to issue another one.  */
8294           if (decl != error_mark_node
8295               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8296             cp_parser_error (parser, "expected %<,%> or %<;%>");
8297           /* Skip tokens until we reach the end of the statement.  */
8298           cp_parser_skip_to_end_of_statement (parser);
8299           /* If the next token is now a `;', consume it.  */
8300           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8301             cp_lexer_consume_token (parser->lexer);
8302           goto done;
8303         }
8304       /* After the first time around, a function-definition is not
8305          allowed -- even if it was OK at first.  For example:
8306
8307            int i, f() {}
8308
8309          is not valid.  */
8310       function_definition_allowed_p = false;
8311     }
8312
8313   /* Issue an error message if no declarators are present, and the
8314      decl-specifier-seq does not itself declare a class or
8315      enumeration.  */
8316   if (!saw_declarator)
8317     {
8318       if (cp_parser_declares_only_class_p (parser))
8319         shadow_tag (&decl_specifiers);
8320       /* Perform any deferred access checks.  */
8321       perform_deferred_access_checks ();
8322     }
8323
8324   /* Consume the `;'.  */
8325   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8326
8327  done:
8328   pop_deferring_access_checks ();
8329 }
8330
8331 /* Parse a decl-specifier-seq.
8332
8333    decl-specifier-seq:
8334      decl-specifier-seq [opt] decl-specifier
8335
8336    decl-specifier:
8337      storage-class-specifier
8338      type-specifier
8339      function-specifier
8340      friend
8341      typedef
8342
8343    GNU Extension:
8344
8345    decl-specifier:
8346      attributes
8347
8348    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8349
8350    The parser flags FLAGS is used to control type-specifier parsing.
8351
8352    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8353    flags:
8354
8355      1: one of the decl-specifiers is an elaborated-type-specifier
8356         (i.e., a type declaration)
8357      2: one of the decl-specifiers is an enum-specifier or a
8358         class-specifier (i.e., a type definition)
8359
8360    */
8361
8362 static void
8363 cp_parser_decl_specifier_seq (cp_parser* parser,
8364                               cp_parser_flags flags,
8365                               cp_decl_specifier_seq *decl_specs,
8366                               int* declares_class_or_enum)
8367 {
8368   bool constructor_possible_p = !parser->in_declarator_p;
8369   cp_token *start_token = NULL;
8370
8371   /* Clear DECL_SPECS.  */
8372   clear_decl_specs (decl_specs);
8373
8374   /* Assume no class or enumeration type is declared.  */
8375   *declares_class_or_enum = 0;
8376
8377   /* Keep reading specifiers until there are no more to read.  */
8378   while (true)
8379     {
8380       bool constructor_p;
8381       bool found_decl_spec;
8382       cp_token *token;
8383
8384       /* Peek at the next token.  */
8385       token = cp_lexer_peek_token (parser->lexer);
8386
8387       /* Save the first token of the decl spec list for error
8388          reporting.  */
8389       if (!start_token)
8390         start_token = token;
8391       /* Handle attributes.  */
8392       if (token->keyword == RID_ATTRIBUTE)
8393         {
8394           /* Parse the attributes.  */
8395           decl_specs->attributes
8396             = chainon (decl_specs->attributes,
8397                        cp_parser_attributes_opt (parser));
8398           continue;
8399         }
8400       /* Assume we will find a decl-specifier keyword.  */
8401       found_decl_spec = true;
8402       /* If the next token is an appropriate keyword, we can simply
8403          add it to the list.  */
8404       switch (token->keyword)
8405         {
8406           /* decl-specifier:
8407                friend  */
8408         case RID_FRIEND:
8409           if (!at_class_scope_p ())
8410             {
8411               error ("%H%<friend%> used outside of class", &token->location);
8412               cp_lexer_purge_token (parser->lexer);
8413             }
8414           else
8415             {
8416               ++decl_specs->specs[(int) ds_friend];
8417               /* Consume the token.  */
8418               cp_lexer_consume_token (parser->lexer);
8419             }
8420           break;
8421
8422           /* function-specifier:
8423                inline
8424                virtual
8425                explicit  */
8426         case RID_INLINE:
8427         case RID_VIRTUAL:
8428         case RID_EXPLICIT:
8429           cp_parser_function_specifier_opt (parser, decl_specs);
8430           break;
8431
8432           /* decl-specifier:
8433                typedef  */
8434         case RID_TYPEDEF:
8435           ++decl_specs->specs[(int) ds_typedef];
8436           /* Consume the token.  */
8437           cp_lexer_consume_token (parser->lexer);
8438           /* A constructor declarator cannot appear in a typedef.  */
8439           constructor_possible_p = false;
8440           /* The "typedef" keyword can only occur in a declaration; we
8441              may as well commit at this point.  */
8442           cp_parser_commit_to_tentative_parse (parser);
8443
8444           if (decl_specs->storage_class != sc_none)
8445             decl_specs->conflicting_specifiers_p = true;
8446           break;
8447
8448           /* storage-class-specifier:
8449                auto
8450                register
8451                static
8452                extern
8453                mutable
8454
8455              GNU Extension:
8456                thread  */
8457         case RID_AUTO:
8458           if (cxx_dialect == cxx98) 
8459             {
8460               /* Consume the token.  */
8461               cp_lexer_consume_token (parser->lexer);
8462
8463               /* Complain about `auto' as a storage specifier, if
8464                  we're complaining about C++0x compatibility.  */
8465               warning 
8466                 (OPT_Wc__0x_compat, 
8467                  "%H%<auto%> will change meaning in C++0x; please remove it",
8468                  &token->location);
8469
8470               /* Set the storage class anyway.  */
8471               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8472                                            token->location);
8473             }
8474           else
8475             /* C++0x auto type-specifier.  */
8476             found_decl_spec = false;
8477           break;
8478
8479         case RID_REGISTER:
8480         case RID_STATIC:
8481         case RID_EXTERN:
8482         case RID_MUTABLE:
8483           /* Consume the token.  */
8484           cp_lexer_consume_token (parser->lexer);
8485           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8486                                        token->location);
8487           break;
8488         case RID_THREAD:
8489           /* Consume the token.  */
8490           cp_lexer_consume_token (parser->lexer);
8491           ++decl_specs->specs[(int) ds_thread];
8492           break;
8493
8494         default:
8495           /* We did not yet find a decl-specifier yet.  */
8496           found_decl_spec = false;
8497           break;
8498         }
8499
8500       /* Constructors are a special case.  The `S' in `S()' is not a
8501          decl-specifier; it is the beginning of the declarator.  */
8502       constructor_p
8503         = (!found_decl_spec
8504            && constructor_possible_p
8505            && (cp_parser_constructor_declarator_p
8506                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8507
8508       /* If we don't have a DECL_SPEC yet, then we must be looking at
8509          a type-specifier.  */
8510       if (!found_decl_spec && !constructor_p)
8511         {
8512           int decl_spec_declares_class_or_enum;
8513           bool is_cv_qualifier;
8514           tree type_spec;
8515
8516           type_spec
8517             = cp_parser_type_specifier (parser, flags,
8518                                         decl_specs,
8519                                         /*is_declaration=*/true,
8520                                         &decl_spec_declares_class_or_enum,
8521                                         &is_cv_qualifier);
8522           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8523
8524           /* If this type-specifier referenced a user-defined type
8525              (a typedef, class-name, etc.), then we can't allow any
8526              more such type-specifiers henceforth.
8527
8528              [dcl.spec]
8529
8530              The longest sequence of decl-specifiers that could
8531              possibly be a type name is taken as the
8532              decl-specifier-seq of a declaration.  The sequence shall
8533              be self-consistent as described below.
8534
8535              [dcl.type]
8536
8537              As a general rule, at most one type-specifier is allowed
8538              in the complete decl-specifier-seq of a declaration.  The
8539              only exceptions are the following:
8540
8541              -- const or volatile can be combined with any other
8542                 type-specifier.
8543
8544              -- signed or unsigned can be combined with char, long,
8545                 short, or int.
8546
8547              -- ..
8548
8549              Example:
8550
8551                typedef char* Pc;
8552                void g (const int Pc);
8553
8554              Here, Pc is *not* part of the decl-specifier seq; it's
8555              the declarator.  Therefore, once we see a type-specifier
8556              (other than a cv-qualifier), we forbid any additional
8557              user-defined types.  We *do* still allow things like `int
8558              int' to be considered a decl-specifier-seq, and issue the
8559              error message later.  */
8560           if (type_spec && !is_cv_qualifier)
8561             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8562           /* A constructor declarator cannot follow a type-specifier.  */
8563           if (type_spec)
8564             {
8565               constructor_possible_p = false;
8566               found_decl_spec = true;
8567             }
8568         }
8569
8570       /* If we still do not have a DECL_SPEC, then there are no more
8571          decl-specifiers.  */
8572       if (!found_decl_spec)
8573         break;
8574
8575       decl_specs->any_specifiers_p = true;
8576       /* After we see one decl-specifier, further decl-specifiers are
8577          always optional.  */
8578       flags |= CP_PARSER_FLAGS_OPTIONAL;
8579     }
8580
8581   cp_parser_check_decl_spec (decl_specs, start_token->location);
8582
8583   /* Don't allow a friend specifier with a class definition.  */
8584   if (decl_specs->specs[(int) ds_friend] != 0
8585       && (*declares_class_or_enum & 2))
8586     error ("%Hclass definition may not be declared a friend",
8587             &start_token->location);
8588 }
8589
8590 /* Parse an (optional) storage-class-specifier.
8591
8592    storage-class-specifier:
8593      auto
8594      register
8595      static
8596      extern
8597      mutable
8598
8599    GNU Extension:
8600
8601    storage-class-specifier:
8602      thread
8603
8604    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8605
8606 static tree
8607 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8608 {
8609   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8610     {
8611     case RID_AUTO:
8612       if (cxx_dialect != cxx98)
8613         return NULL_TREE;
8614       /* Fall through for C++98.  */
8615
8616     case RID_REGISTER:
8617     case RID_STATIC:
8618     case RID_EXTERN:
8619     case RID_MUTABLE:
8620     case RID_THREAD:
8621       /* Consume the token.  */
8622       return cp_lexer_consume_token (parser->lexer)->u.value;
8623
8624     default:
8625       return NULL_TREE;
8626     }
8627 }
8628
8629 /* Parse an (optional) function-specifier.
8630
8631    function-specifier:
8632      inline
8633      virtual
8634      explicit
8635
8636    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8637    Updates DECL_SPECS, if it is non-NULL.  */
8638
8639 static tree
8640 cp_parser_function_specifier_opt (cp_parser* parser,
8641                                   cp_decl_specifier_seq *decl_specs)
8642 {
8643   cp_token *token = cp_lexer_peek_token (parser->lexer);
8644   switch (token->keyword)
8645     {
8646     case RID_INLINE:
8647       if (decl_specs)
8648         ++decl_specs->specs[(int) ds_inline];
8649       break;
8650
8651     case RID_VIRTUAL:
8652       /* 14.5.2.3 [temp.mem]
8653
8654          A member function template shall not be virtual.  */
8655       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8656         error ("%Htemplates may not be %<virtual%>", &token->location);
8657       else if (decl_specs)
8658         ++decl_specs->specs[(int) ds_virtual];
8659       break;
8660
8661     case RID_EXPLICIT:
8662       if (decl_specs)
8663         ++decl_specs->specs[(int) ds_explicit];
8664       break;
8665
8666     default:
8667       return NULL_TREE;
8668     }
8669
8670   /* Consume the token.  */
8671   return cp_lexer_consume_token (parser->lexer)->u.value;
8672 }
8673
8674 /* Parse a linkage-specification.
8675
8676    linkage-specification:
8677      extern string-literal { declaration-seq [opt] }
8678      extern string-literal declaration  */
8679
8680 static void
8681 cp_parser_linkage_specification (cp_parser* parser)
8682 {
8683   tree linkage;
8684
8685   /* Look for the `extern' keyword.  */
8686   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8687
8688   /* Look for the string-literal.  */
8689   linkage = cp_parser_string_literal (parser, false, false);
8690
8691   /* Transform the literal into an identifier.  If the literal is a
8692      wide-character string, or contains embedded NULs, then we can't
8693      handle it as the user wants.  */
8694   if (strlen (TREE_STRING_POINTER (linkage))
8695       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8696     {
8697       cp_parser_error (parser, "invalid linkage-specification");
8698       /* Assume C++ linkage.  */
8699       linkage = lang_name_cplusplus;
8700     }
8701   else
8702     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8703
8704   /* We're now using the new linkage.  */
8705   push_lang_context (linkage);
8706
8707   /* If the next token is a `{', then we're using the first
8708      production.  */
8709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8710     {
8711       /* Consume the `{' token.  */
8712       cp_lexer_consume_token (parser->lexer);
8713       /* Parse the declarations.  */
8714       cp_parser_declaration_seq_opt (parser);
8715       /* Look for the closing `}'.  */
8716       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8717     }
8718   /* Otherwise, there's just one declaration.  */
8719   else
8720     {
8721       bool saved_in_unbraced_linkage_specification_p;
8722
8723       saved_in_unbraced_linkage_specification_p
8724         = parser->in_unbraced_linkage_specification_p;
8725       parser->in_unbraced_linkage_specification_p = true;
8726       cp_parser_declaration (parser);
8727       parser->in_unbraced_linkage_specification_p
8728         = saved_in_unbraced_linkage_specification_p;
8729     }
8730
8731   /* We're done with the linkage-specification.  */
8732   pop_lang_context ();
8733 }
8734
8735 /* Parse a static_assert-declaration.
8736
8737    static_assert-declaration:
8738      static_assert ( constant-expression , string-literal ) ; 
8739
8740    If MEMBER_P, this static_assert is a class member.  */
8741
8742 static void 
8743 cp_parser_static_assert(cp_parser *parser, bool member_p)
8744 {
8745   tree condition;
8746   tree message;
8747   cp_token *token;
8748   location_t saved_loc;
8749
8750   /* Peek at the `static_assert' token so we can keep track of exactly
8751      where the static assertion started.  */
8752   token = cp_lexer_peek_token (parser->lexer);
8753   saved_loc = token->location;
8754
8755   /* Look for the `static_assert' keyword.  */
8756   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8757                                   "%<static_assert%>"))
8758     return;
8759
8760   /*  We know we are in a static assertion; commit to any tentative
8761       parse.  */
8762   if (cp_parser_parsing_tentatively (parser))
8763     cp_parser_commit_to_tentative_parse (parser);
8764
8765   /* Parse the `(' starting the static assertion condition.  */
8766   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8767
8768   /* Parse the constant-expression.  */
8769   condition = 
8770     cp_parser_constant_expression (parser,
8771                                    /*allow_non_constant_p=*/false,
8772                                    /*non_constant_p=*/NULL);
8773
8774   /* Parse the separating `,'.  */
8775   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8776
8777   /* Parse the string-literal message.  */
8778   message = cp_parser_string_literal (parser, 
8779                                       /*translate=*/false,
8780                                       /*wide_ok=*/true);
8781
8782   /* A `)' completes the static assertion.  */
8783   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8784     cp_parser_skip_to_closing_parenthesis (parser, 
8785                                            /*recovering=*/true, 
8786                                            /*or_comma=*/false,
8787                                            /*consume_paren=*/true);
8788
8789   /* A semicolon terminates the declaration.  */
8790   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8791
8792   /* Complete the static assertion, which may mean either processing 
8793      the static assert now or saving it for template instantiation.  */
8794   finish_static_assert (condition, message, saved_loc, member_p);
8795 }
8796
8797 /* Parse a `decltype' type. Returns the type. 
8798
8799    simple-type-specifier:
8800      decltype ( expression )  */
8801
8802 static tree
8803 cp_parser_decltype (cp_parser *parser)
8804 {
8805   tree expr;
8806   bool id_expression_or_member_access_p = false;
8807   const char *saved_message;
8808   bool saved_integral_constant_expression_p;
8809   bool saved_non_integral_constant_expression_p;
8810   cp_token *id_expr_start_token;
8811
8812   /* Look for the `decltype' token.  */
8813   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8814     return error_mark_node;
8815
8816   /* Types cannot be defined in a `decltype' expression.  Save away the
8817      old message.  */
8818   saved_message = parser->type_definition_forbidden_message;
8819
8820   /* And create the new one.  */
8821   parser->type_definition_forbidden_message
8822     = "types may not be defined in %<decltype%> expressions";
8823
8824   /* The restrictions on constant-expressions do not apply inside
8825      decltype expressions.  */
8826   saved_integral_constant_expression_p
8827     = parser->integral_constant_expression_p;
8828   saved_non_integral_constant_expression_p
8829     = parser->non_integral_constant_expression_p;
8830   parser->integral_constant_expression_p = false;
8831
8832   /* Do not actually evaluate the expression.  */
8833   ++skip_evaluation;
8834
8835   /* Parse the opening `('.  */
8836   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8837     return error_mark_node;
8838   
8839   /* First, try parsing an id-expression.  */
8840   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8841   cp_parser_parse_tentatively (parser);
8842   expr = cp_parser_id_expression (parser,
8843                                   /*template_keyword_p=*/false,
8844                                   /*check_dependency_p=*/true,
8845                                   /*template_p=*/NULL,
8846                                   /*declarator_p=*/false,
8847                                   /*optional_p=*/false);
8848
8849   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8850     {
8851       bool non_integral_constant_expression_p = false;
8852       tree id_expression = expr;
8853       cp_id_kind idk;
8854       const char *error_msg;
8855
8856       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8857         /* Lookup the name we got back from the id-expression.  */
8858         expr = cp_parser_lookup_name (parser, expr,
8859                                       none_type,
8860                                       /*is_template=*/false,
8861                                       /*is_namespace=*/false,
8862                                       /*check_dependency=*/true,
8863                                       /*ambiguous_decls=*/NULL,
8864                                       id_expr_start_token->location);
8865
8866       if (expr
8867           && expr != error_mark_node
8868           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8869           && TREE_CODE (expr) != TYPE_DECL
8870           && (TREE_CODE (expr) != BIT_NOT_EXPR
8871               || !TYPE_P (TREE_OPERAND (expr, 0)))
8872           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8873         {
8874           /* Complete lookup of the id-expression.  */
8875           expr = (finish_id_expression
8876                   (id_expression, expr, parser->scope, &idk,
8877                    /*integral_constant_expression_p=*/false,
8878                    /*allow_non_integral_constant_expression_p=*/true,
8879                    &non_integral_constant_expression_p,
8880                    /*template_p=*/false,
8881                    /*done=*/true,
8882                    /*address_p=*/false,
8883                    /*template_arg_p=*/false,
8884                    &error_msg,
8885                    id_expr_start_token->location));
8886
8887           if (expr == error_mark_node)
8888             /* We found an id-expression, but it was something that we
8889                should not have found. This is an error, not something
8890                we can recover from, so note that we found an
8891                id-expression and we'll recover as gracefully as
8892                possible.  */
8893             id_expression_or_member_access_p = true;
8894         }
8895
8896       if (expr 
8897           && expr != error_mark_node
8898           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8899         /* We have an id-expression.  */
8900         id_expression_or_member_access_p = true;
8901     }
8902
8903   if (!id_expression_or_member_access_p)
8904     {
8905       /* Abort the id-expression parse.  */
8906       cp_parser_abort_tentative_parse (parser);
8907
8908       /* Parsing tentatively, again.  */
8909       cp_parser_parse_tentatively (parser);
8910
8911       /* Parse a class member access.  */
8912       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8913                                            /*cast_p=*/false,
8914                                            /*member_access_only_p=*/true, NULL);
8915
8916       if (expr 
8917           && expr != error_mark_node
8918           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8919         /* We have an id-expression.  */
8920         id_expression_or_member_access_p = true;
8921     }
8922
8923   if (id_expression_or_member_access_p)
8924     /* We have parsed the complete id-expression or member access.  */
8925     cp_parser_parse_definitely (parser);
8926   else
8927     {
8928       bool saved_greater_than_is_operator_p;
8929
8930       /* Abort our attempt to parse an id-expression or member access
8931          expression.  */
8932       cp_parser_abort_tentative_parse (parser);
8933
8934       /* Within a parenthesized expression, a `>' token is always
8935          the greater-than operator.  */
8936       saved_greater_than_is_operator_p
8937         = parser->greater_than_is_operator_p;
8938       parser->greater_than_is_operator_p = true;
8939
8940       /* Parse a full expression.  */
8941       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8942
8943       /* The `>' token might be the end of a template-id or
8944          template-parameter-list now.  */
8945       parser->greater_than_is_operator_p
8946         = saved_greater_than_is_operator_p;
8947     }
8948
8949   /* Go back to evaluating expressions.  */
8950   --skip_evaluation;
8951
8952   /* Restore the old message and the integral constant expression
8953      flags.  */
8954   parser->type_definition_forbidden_message = saved_message;
8955   parser->integral_constant_expression_p
8956     = saved_integral_constant_expression_p;
8957   parser->non_integral_constant_expression_p
8958     = saved_non_integral_constant_expression_p;
8959
8960   if (expr == error_mark_node)
8961     {
8962       /* Skip everything up to the closing `)'.  */
8963       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8964                                              /*consume_paren=*/true);
8965       return error_mark_node;
8966     }
8967   
8968   /* Parse to the closing `)'.  */
8969   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8970     {
8971       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8972                                              /*consume_paren=*/true);
8973       return error_mark_node;
8974     }
8975
8976   return finish_decltype_type (expr, id_expression_or_member_access_p);
8977 }
8978
8979 /* Special member functions [gram.special] */
8980
8981 /* Parse a conversion-function-id.
8982
8983    conversion-function-id:
8984      operator conversion-type-id
8985
8986    Returns an IDENTIFIER_NODE representing the operator.  */
8987
8988 static tree
8989 cp_parser_conversion_function_id (cp_parser* parser)
8990 {
8991   tree type;
8992   tree saved_scope;
8993   tree saved_qualifying_scope;
8994   tree saved_object_scope;
8995   tree pushed_scope = NULL_TREE;
8996
8997   /* Look for the `operator' token.  */
8998   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8999     return error_mark_node;
9000   /* When we parse the conversion-type-id, the current scope will be
9001      reset.  However, we need that information in able to look up the
9002      conversion function later, so we save it here.  */
9003   saved_scope = parser->scope;
9004   saved_qualifying_scope = parser->qualifying_scope;
9005   saved_object_scope = parser->object_scope;
9006   /* We must enter the scope of the class so that the names of
9007      entities declared within the class are available in the
9008      conversion-type-id.  For example, consider:
9009
9010        struct S {
9011          typedef int I;
9012          operator I();
9013        };
9014
9015        S::operator I() { ... }
9016
9017      In order to see that `I' is a type-name in the definition, we
9018      must be in the scope of `S'.  */
9019   if (saved_scope)
9020     pushed_scope = push_scope (saved_scope);
9021   /* Parse the conversion-type-id.  */
9022   type = cp_parser_conversion_type_id (parser);
9023   /* Leave the scope of the class, if any.  */
9024   if (pushed_scope)
9025     pop_scope (pushed_scope);
9026   /* Restore the saved scope.  */
9027   parser->scope = saved_scope;
9028   parser->qualifying_scope = saved_qualifying_scope;
9029   parser->object_scope = saved_object_scope;
9030   /* If the TYPE is invalid, indicate failure.  */
9031   if (type == error_mark_node)
9032     return error_mark_node;
9033   return mangle_conv_op_name_for_type (type);
9034 }
9035
9036 /* Parse a conversion-type-id:
9037
9038    conversion-type-id:
9039      type-specifier-seq conversion-declarator [opt]
9040
9041    Returns the TYPE specified.  */
9042
9043 static tree
9044 cp_parser_conversion_type_id (cp_parser* parser)
9045 {
9046   tree attributes;
9047   cp_decl_specifier_seq type_specifiers;
9048   cp_declarator *declarator;
9049   tree type_specified;
9050
9051   /* Parse the attributes.  */
9052   attributes = cp_parser_attributes_opt (parser);
9053   /* Parse the type-specifiers.  */
9054   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9055                                 /*is_trailing_return=*/false,
9056                                 &type_specifiers);
9057   /* If that didn't work, stop.  */
9058   if (type_specifiers.type == error_mark_node)
9059     return error_mark_node;
9060   /* Parse the conversion-declarator.  */
9061   declarator = cp_parser_conversion_declarator_opt (parser);
9062
9063   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9064                                     /*initialized=*/0, &attributes);
9065   if (attributes)
9066     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9067
9068   /* Don't give this error when parsing tentatively.  This happens to
9069      work because we always parse this definitively once.  */
9070   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9071       && type_uses_auto (type_specified))
9072     {
9073       error ("invalid use of %<auto%> in conversion operator");
9074       return error_mark_node;
9075     }
9076
9077   return type_specified;
9078 }
9079
9080 /* Parse an (optional) conversion-declarator.
9081
9082    conversion-declarator:
9083      ptr-operator conversion-declarator [opt]
9084
9085    */
9086
9087 static cp_declarator *
9088 cp_parser_conversion_declarator_opt (cp_parser* parser)
9089 {
9090   enum tree_code code;
9091   tree class_type;
9092   cp_cv_quals cv_quals;
9093
9094   /* We don't know if there's a ptr-operator next, or not.  */
9095   cp_parser_parse_tentatively (parser);
9096   /* Try the ptr-operator.  */
9097   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9098   /* If it worked, look for more conversion-declarators.  */
9099   if (cp_parser_parse_definitely (parser))
9100     {
9101       cp_declarator *declarator;
9102
9103       /* Parse another optional declarator.  */
9104       declarator = cp_parser_conversion_declarator_opt (parser);
9105
9106       return cp_parser_make_indirect_declarator
9107         (code, class_type, cv_quals, declarator);
9108    }
9109
9110   return NULL;
9111 }
9112
9113 /* Parse an (optional) ctor-initializer.
9114
9115    ctor-initializer:
9116      : mem-initializer-list
9117
9118    Returns TRUE iff the ctor-initializer was actually present.  */
9119
9120 static bool
9121 cp_parser_ctor_initializer_opt (cp_parser* parser)
9122 {
9123   /* If the next token is not a `:', then there is no
9124      ctor-initializer.  */
9125   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9126     {
9127       /* Do default initialization of any bases and members.  */
9128       if (DECL_CONSTRUCTOR_P (current_function_decl))
9129         finish_mem_initializers (NULL_TREE);
9130
9131       return false;
9132     }
9133
9134   /* Consume the `:' token.  */
9135   cp_lexer_consume_token (parser->lexer);
9136   /* And the mem-initializer-list.  */
9137   cp_parser_mem_initializer_list (parser);
9138
9139   return true;
9140 }
9141
9142 /* Parse a mem-initializer-list.
9143
9144    mem-initializer-list:
9145      mem-initializer ... [opt]
9146      mem-initializer ... [opt] , mem-initializer-list  */
9147
9148 static void
9149 cp_parser_mem_initializer_list (cp_parser* parser)
9150 {
9151   tree mem_initializer_list = NULL_TREE;
9152   cp_token *token = cp_lexer_peek_token (parser->lexer);
9153
9154   /* Let the semantic analysis code know that we are starting the
9155      mem-initializer-list.  */
9156   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9157     error ("%Honly constructors take base initializers",
9158            &token->location);
9159
9160   /* Loop through the list.  */
9161   while (true)
9162     {
9163       tree mem_initializer;
9164
9165       token = cp_lexer_peek_token (parser->lexer);
9166       /* Parse the mem-initializer.  */
9167       mem_initializer = cp_parser_mem_initializer (parser);
9168       /* If the next token is a `...', we're expanding member initializers. */
9169       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9170         {
9171           /* Consume the `...'. */
9172           cp_lexer_consume_token (parser->lexer);
9173
9174           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9175              can be expanded but members cannot. */
9176           if (mem_initializer != error_mark_node
9177               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9178             {
9179               error ("%Hcannot expand initializer for member %<%D%>",
9180                      &token->location, TREE_PURPOSE (mem_initializer));
9181               mem_initializer = error_mark_node;
9182             }
9183
9184           /* Construct the pack expansion type. */
9185           if (mem_initializer != error_mark_node)
9186             mem_initializer = make_pack_expansion (mem_initializer);
9187         }
9188       /* Add it to the list, unless it was erroneous.  */
9189       if (mem_initializer != error_mark_node)
9190         {
9191           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9192           mem_initializer_list = mem_initializer;
9193         }
9194       /* If the next token is not a `,', we're done.  */
9195       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9196         break;
9197       /* Consume the `,' token.  */
9198       cp_lexer_consume_token (parser->lexer);
9199     }
9200
9201   /* Perform semantic analysis.  */
9202   if (DECL_CONSTRUCTOR_P (current_function_decl))
9203     finish_mem_initializers (mem_initializer_list);
9204 }
9205
9206 /* Parse a mem-initializer.
9207
9208    mem-initializer:
9209      mem-initializer-id ( expression-list [opt] )
9210      mem-initializer-id braced-init-list
9211
9212    GNU extension:
9213
9214    mem-initializer:
9215      ( expression-list [opt] )
9216
9217    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9218    class) or FIELD_DECL (for a non-static data member) to initialize;
9219    the TREE_VALUE is the expression-list.  An empty initialization
9220    list is represented by void_list_node.  */
9221
9222 static tree
9223 cp_parser_mem_initializer (cp_parser* parser)
9224 {
9225   tree mem_initializer_id;
9226   tree expression_list;
9227   tree member;
9228   cp_token *token = cp_lexer_peek_token (parser->lexer);
9229
9230   /* Find out what is being initialized.  */
9231   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9232     {
9233       permerror (token->location,
9234                  "anachronistic old-style base class initializer");
9235       mem_initializer_id = NULL_TREE;
9236     }
9237   else
9238     {
9239       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9240       if (mem_initializer_id == error_mark_node)
9241         return mem_initializer_id;
9242     }
9243   member = expand_member_init (mem_initializer_id);
9244   if (member && !DECL_P (member))
9245     in_base_initializer = 1;
9246
9247   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9248     {
9249       bool expr_non_constant_p;
9250       maybe_warn_cpp0x ("extended initializer lists");
9251       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9252       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9253       expression_list = build_tree_list (NULL_TREE, expression_list);
9254     }
9255   else
9256     expression_list
9257       = cp_parser_parenthesized_expression_list (parser, false,
9258                                                  /*cast_p=*/false,
9259                                                  /*allow_expansion_p=*/true,
9260                                                  /*non_constant_p=*/NULL);
9261   if (expression_list == error_mark_node)
9262     return error_mark_node;
9263   if (!expression_list)
9264     expression_list = void_type_node;
9265
9266   in_base_initializer = 0;
9267
9268   return member ? build_tree_list (member, expression_list) : error_mark_node;
9269 }
9270
9271 /* Parse a mem-initializer-id.
9272
9273    mem-initializer-id:
9274      :: [opt] nested-name-specifier [opt] class-name
9275      identifier
9276
9277    Returns a TYPE indicating the class to be initializer for the first
9278    production.  Returns an IDENTIFIER_NODE indicating the data member
9279    to be initialized for the second production.  */
9280
9281 static tree
9282 cp_parser_mem_initializer_id (cp_parser* parser)
9283 {
9284   bool global_scope_p;
9285   bool nested_name_specifier_p;
9286   bool template_p = false;
9287   tree id;
9288
9289   cp_token *token = cp_lexer_peek_token (parser->lexer);
9290
9291   /* `typename' is not allowed in this context ([temp.res]).  */
9292   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9293     {
9294       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9295              "member initializer is implicitly a type)",
9296              &token->location);
9297       cp_lexer_consume_token (parser->lexer);
9298     }
9299   /* Look for the optional `::' operator.  */
9300   global_scope_p
9301     = (cp_parser_global_scope_opt (parser,
9302                                    /*current_scope_valid_p=*/false)
9303        != NULL_TREE);
9304   /* Look for the optional nested-name-specifier.  The simplest way to
9305      implement:
9306
9307        [temp.res]
9308
9309        The keyword `typename' is not permitted in a base-specifier or
9310        mem-initializer; in these contexts a qualified name that
9311        depends on a template-parameter is implicitly assumed to be a
9312        type name.
9313
9314      is to assume that we have seen the `typename' keyword at this
9315      point.  */
9316   nested_name_specifier_p
9317     = (cp_parser_nested_name_specifier_opt (parser,
9318                                             /*typename_keyword_p=*/true,
9319                                             /*check_dependency_p=*/true,
9320                                             /*type_p=*/true,
9321                                             /*is_declaration=*/true)
9322        != NULL_TREE);
9323   if (nested_name_specifier_p)
9324     template_p = cp_parser_optional_template_keyword (parser);
9325   /* If there is a `::' operator or a nested-name-specifier, then we
9326      are definitely looking for a class-name.  */
9327   if (global_scope_p || nested_name_specifier_p)
9328     return cp_parser_class_name (parser,
9329                                  /*typename_keyword_p=*/true,
9330                                  /*template_keyword_p=*/template_p,
9331                                  none_type,
9332                                  /*check_dependency_p=*/true,
9333                                  /*class_head_p=*/false,
9334                                  /*is_declaration=*/true);
9335   /* Otherwise, we could also be looking for an ordinary identifier.  */
9336   cp_parser_parse_tentatively (parser);
9337   /* Try a class-name.  */
9338   id = cp_parser_class_name (parser,
9339                              /*typename_keyword_p=*/true,
9340                              /*template_keyword_p=*/false,
9341                              none_type,
9342                              /*check_dependency_p=*/true,
9343                              /*class_head_p=*/false,
9344                              /*is_declaration=*/true);
9345   /* If we found one, we're done.  */
9346   if (cp_parser_parse_definitely (parser))
9347     return id;
9348   /* Otherwise, look for an ordinary identifier.  */
9349   return cp_parser_identifier (parser);
9350 }
9351
9352 /* Overloading [gram.over] */
9353
9354 /* Parse an operator-function-id.
9355
9356    operator-function-id:
9357      operator operator
9358
9359    Returns an IDENTIFIER_NODE for the operator which is a
9360    human-readable spelling of the identifier, e.g., `operator +'.  */
9361
9362 static tree
9363 cp_parser_operator_function_id (cp_parser* parser)
9364 {
9365   /* Look for the `operator' keyword.  */
9366   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9367     return error_mark_node;
9368   /* And then the name of the operator itself.  */
9369   return cp_parser_operator (parser);
9370 }
9371
9372 /* Parse an operator.
9373
9374    operator:
9375      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9376      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9377      || ++ -- , ->* -> () []
9378
9379    GNU Extensions:
9380
9381    operator:
9382      <? >? <?= >?=
9383
9384    Returns an IDENTIFIER_NODE for the operator which is a
9385    human-readable spelling of the identifier, e.g., `operator +'.  */
9386
9387 static tree
9388 cp_parser_operator (cp_parser* parser)
9389 {
9390   tree id = NULL_TREE;
9391   cp_token *token;
9392
9393   /* Peek at the next token.  */
9394   token = cp_lexer_peek_token (parser->lexer);
9395   /* Figure out which operator we have.  */
9396   switch (token->type)
9397     {
9398     case CPP_KEYWORD:
9399       {
9400         enum tree_code op;
9401
9402         /* The keyword should be either `new' or `delete'.  */
9403         if (token->keyword == RID_NEW)
9404           op = NEW_EXPR;
9405         else if (token->keyword == RID_DELETE)
9406           op = DELETE_EXPR;
9407         else
9408           break;
9409
9410         /* Consume the `new' or `delete' token.  */
9411         cp_lexer_consume_token (parser->lexer);
9412
9413         /* Peek at the next token.  */
9414         token = cp_lexer_peek_token (parser->lexer);
9415         /* If it's a `[' token then this is the array variant of the
9416            operator.  */
9417         if (token->type == CPP_OPEN_SQUARE)
9418           {
9419             /* Consume the `[' token.  */
9420             cp_lexer_consume_token (parser->lexer);
9421             /* Look for the `]' token.  */
9422             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9423             id = ansi_opname (op == NEW_EXPR
9424                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9425           }
9426         /* Otherwise, we have the non-array variant.  */
9427         else
9428           id = ansi_opname (op);
9429
9430         return id;
9431       }
9432
9433     case CPP_PLUS:
9434       id = ansi_opname (PLUS_EXPR);
9435       break;
9436
9437     case CPP_MINUS:
9438       id = ansi_opname (MINUS_EXPR);
9439       break;
9440
9441     case CPP_MULT:
9442       id = ansi_opname (MULT_EXPR);
9443       break;
9444
9445     case CPP_DIV:
9446       id = ansi_opname (TRUNC_DIV_EXPR);
9447       break;
9448
9449     case CPP_MOD:
9450       id = ansi_opname (TRUNC_MOD_EXPR);
9451       break;
9452
9453     case CPP_XOR:
9454       id = ansi_opname (BIT_XOR_EXPR);
9455       break;
9456
9457     case CPP_AND:
9458       id = ansi_opname (BIT_AND_EXPR);
9459       break;
9460
9461     case CPP_OR:
9462       id = ansi_opname (BIT_IOR_EXPR);
9463       break;
9464
9465     case CPP_COMPL:
9466       id = ansi_opname (BIT_NOT_EXPR);
9467       break;
9468
9469     case CPP_NOT:
9470       id = ansi_opname (TRUTH_NOT_EXPR);
9471       break;
9472
9473     case CPP_EQ:
9474       id = ansi_assopname (NOP_EXPR);
9475       break;
9476
9477     case CPP_LESS:
9478       id = ansi_opname (LT_EXPR);
9479       break;
9480
9481     case CPP_GREATER:
9482       id = ansi_opname (GT_EXPR);
9483       break;
9484
9485     case CPP_PLUS_EQ:
9486       id = ansi_assopname (PLUS_EXPR);
9487       break;
9488
9489     case CPP_MINUS_EQ:
9490       id = ansi_assopname (MINUS_EXPR);
9491       break;
9492
9493     case CPP_MULT_EQ:
9494       id = ansi_assopname (MULT_EXPR);
9495       break;
9496
9497     case CPP_DIV_EQ:
9498       id = ansi_assopname (TRUNC_DIV_EXPR);
9499       break;
9500
9501     case CPP_MOD_EQ:
9502       id = ansi_assopname (TRUNC_MOD_EXPR);
9503       break;
9504
9505     case CPP_XOR_EQ:
9506       id = ansi_assopname (BIT_XOR_EXPR);
9507       break;
9508
9509     case CPP_AND_EQ:
9510       id = ansi_assopname (BIT_AND_EXPR);
9511       break;
9512
9513     case CPP_OR_EQ:
9514       id = ansi_assopname (BIT_IOR_EXPR);
9515       break;
9516
9517     case CPP_LSHIFT:
9518       id = ansi_opname (LSHIFT_EXPR);
9519       break;
9520
9521     case CPP_RSHIFT:
9522       id = ansi_opname (RSHIFT_EXPR);
9523       break;
9524
9525     case CPP_LSHIFT_EQ:
9526       id = ansi_assopname (LSHIFT_EXPR);
9527       break;
9528
9529     case CPP_RSHIFT_EQ:
9530       id = ansi_assopname (RSHIFT_EXPR);
9531       break;
9532
9533     case CPP_EQ_EQ:
9534       id = ansi_opname (EQ_EXPR);
9535       break;
9536
9537     case CPP_NOT_EQ:
9538       id = ansi_opname (NE_EXPR);
9539       break;
9540
9541     case CPP_LESS_EQ:
9542       id = ansi_opname (LE_EXPR);
9543       break;
9544
9545     case CPP_GREATER_EQ:
9546       id = ansi_opname (GE_EXPR);
9547       break;
9548
9549     case CPP_AND_AND:
9550       id = ansi_opname (TRUTH_ANDIF_EXPR);
9551       break;
9552
9553     case CPP_OR_OR:
9554       id = ansi_opname (TRUTH_ORIF_EXPR);
9555       break;
9556
9557     case CPP_PLUS_PLUS:
9558       id = ansi_opname (POSTINCREMENT_EXPR);
9559       break;
9560
9561     case CPP_MINUS_MINUS:
9562       id = ansi_opname (PREDECREMENT_EXPR);
9563       break;
9564
9565     case CPP_COMMA:
9566       id = ansi_opname (COMPOUND_EXPR);
9567       break;
9568
9569     case CPP_DEREF_STAR:
9570       id = ansi_opname (MEMBER_REF);
9571       break;
9572
9573     case CPP_DEREF:
9574       id = ansi_opname (COMPONENT_REF);
9575       break;
9576
9577     case CPP_OPEN_PAREN:
9578       /* Consume the `('.  */
9579       cp_lexer_consume_token (parser->lexer);
9580       /* Look for the matching `)'.  */
9581       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9582       return ansi_opname (CALL_EXPR);
9583
9584     case CPP_OPEN_SQUARE:
9585       /* Consume the `['.  */
9586       cp_lexer_consume_token (parser->lexer);
9587       /* Look for the matching `]'.  */
9588       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9589       return ansi_opname (ARRAY_REF);
9590
9591     default:
9592       /* Anything else is an error.  */
9593       break;
9594     }
9595
9596   /* If we have selected an identifier, we need to consume the
9597      operator token.  */
9598   if (id)
9599     cp_lexer_consume_token (parser->lexer);
9600   /* Otherwise, no valid operator name was present.  */
9601   else
9602     {
9603       cp_parser_error (parser, "expected operator");
9604       id = error_mark_node;
9605     }
9606
9607   return id;
9608 }
9609
9610 /* Parse a template-declaration.
9611
9612    template-declaration:
9613      export [opt] template < template-parameter-list > declaration
9614
9615    If MEMBER_P is TRUE, this template-declaration occurs within a
9616    class-specifier.
9617
9618    The grammar rule given by the standard isn't correct.  What
9619    is really meant is:
9620
9621    template-declaration:
9622      export [opt] template-parameter-list-seq
9623        decl-specifier-seq [opt] init-declarator [opt] ;
9624      export [opt] template-parameter-list-seq
9625        function-definition
9626
9627    template-parameter-list-seq:
9628      template-parameter-list-seq [opt]
9629      template < template-parameter-list >  */
9630
9631 static void
9632 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9633 {
9634   /* Check for `export'.  */
9635   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9636     {
9637       /* Consume the `export' token.  */
9638       cp_lexer_consume_token (parser->lexer);
9639       /* Warn that we do not support `export'.  */
9640       warning (0, "keyword %<export%> not implemented, and will be ignored");
9641     }
9642
9643   cp_parser_template_declaration_after_export (parser, member_p);
9644 }
9645
9646 /* Parse a template-parameter-list.
9647
9648    template-parameter-list:
9649      template-parameter
9650      template-parameter-list , template-parameter
9651
9652    Returns a TREE_LIST.  Each node represents a template parameter.
9653    The nodes are connected via their TREE_CHAINs.  */
9654
9655 static tree
9656 cp_parser_template_parameter_list (cp_parser* parser)
9657 {
9658   tree parameter_list = NULL_TREE;
9659
9660   begin_template_parm_list ();
9661   while (true)
9662     {
9663       tree parameter;
9664       bool is_non_type;
9665       bool is_parameter_pack;
9666
9667       /* Parse the template-parameter.  */
9668       parameter = cp_parser_template_parameter (parser, 
9669                                                 &is_non_type,
9670                                                 &is_parameter_pack);
9671       /* Add it to the list.  */
9672       if (parameter != error_mark_node)
9673         parameter_list = process_template_parm (parameter_list,
9674                                                 parameter,
9675                                                 is_non_type,
9676                                                 is_parameter_pack);
9677       else
9678        {
9679          tree err_parm = build_tree_list (parameter, parameter);
9680          TREE_VALUE (err_parm) = error_mark_node;
9681          parameter_list = chainon (parameter_list, err_parm);
9682        }
9683
9684       /* If the next token is not a `,', we're done.  */
9685       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9686         break;
9687       /* Otherwise, consume the `,' token.  */
9688       cp_lexer_consume_token (parser->lexer);
9689     }
9690
9691   return end_template_parm_list (parameter_list);
9692 }
9693
9694 /* Parse a template-parameter.
9695
9696    template-parameter:
9697      type-parameter
9698      parameter-declaration
9699
9700    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9701    the parameter.  The TREE_PURPOSE is the default value, if any.
9702    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9703    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9704    set to true iff this parameter is a parameter pack. */
9705
9706 static tree
9707 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9708                               bool *is_parameter_pack)
9709 {
9710   cp_token *token;
9711   cp_parameter_declarator *parameter_declarator;
9712   cp_declarator *id_declarator;
9713   tree parm;
9714
9715   /* Assume it is a type parameter or a template parameter.  */
9716   *is_non_type = false;
9717   /* Assume it not a parameter pack. */
9718   *is_parameter_pack = false;
9719   /* Peek at the next token.  */
9720   token = cp_lexer_peek_token (parser->lexer);
9721   /* If it is `class' or `template', we have a type-parameter.  */
9722   if (token->keyword == RID_TEMPLATE)
9723     return cp_parser_type_parameter (parser, is_parameter_pack);
9724   /* If it is `class' or `typename' we do not know yet whether it is a
9725      type parameter or a non-type parameter.  Consider:
9726
9727        template <typename T, typename T::X X> ...
9728
9729      or:
9730
9731        template <class C, class D*> ...
9732
9733      Here, the first parameter is a type parameter, and the second is
9734      a non-type parameter.  We can tell by looking at the token after
9735      the identifier -- if it is a `,', `=', or `>' then we have a type
9736      parameter.  */
9737   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9738     {
9739       /* Peek at the token after `class' or `typename'.  */
9740       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9741       /* If it's an ellipsis, we have a template type parameter
9742          pack. */
9743       if (token->type == CPP_ELLIPSIS)
9744         return cp_parser_type_parameter (parser, is_parameter_pack);
9745       /* If it's an identifier, skip it.  */
9746       if (token->type == CPP_NAME)
9747         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9748       /* Now, see if the token looks like the end of a template
9749          parameter.  */
9750       if (token->type == CPP_COMMA
9751           || token->type == CPP_EQ
9752           || token->type == CPP_GREATER)
9753         return cp_parser_type_parameter (parser, is_parameter_pack);
9754     }
9755
9756   /* Otherwise, it is a non-type parameter.
9757
9758      [temp.param]
9759
9760      When parsing a default template-argument for a non-type
9761      template-parameter, the first non-nested `>' is taken as the end
9762      of the template parameter-list rather than a greater-than
9763      operator.  */
9764   *is_non_type = true;
9765   parameter_declarator
9766      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9767                                         /*parenthesized_p=*/NULL);
9768
9769   /* If the parameter declaration is marked as a parameter pack, set
9770      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9771      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9772      grokdeclarator. */
9773   if (parameter_declarator
9774       && parameter_declarator->declarator
9775       && parameter_declarator->declarator->parameter_pack_p)
9776     {
9777       *is_parameter_pack = true;
9778       parameter_declarator->declarator->parameter_pack_p = false;
9779     }
9780
9781   /* If the next token is an ellipsis, and we don't already have it
9782      marked as a parameter pack, then we have a parameter pack (that
9783      has no declarator).  */
9784   if (!*is_parameter_pack
9785       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9786       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9787     {
9788       /* Consume the `...'.  */
9789       cp_lexer_consume_token (parser->lexer);
9790       maybe_warn_variadic_templates ();
9791       
9792       *is_parameter_pack = true;
9793     }
9794   /* We might end up with a pack expansion as the type of the non-type
9795      template parameter, in which case this is a non-type template
9796      parameter pack.  */
9797   else if (parameter_declarator
9798            && parameter_declarator->decl_specifiers.type
9799            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9800     {
9801       *is_parameter_pack = true;
9802       parameter_declarator->decl_specifiers.type = 
9803         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9804     }
9805
9806   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9807     {
9808       /* Parameter packs cannot have default arguments.  However, a
9809          user may try to do so, so we'll parse them and give an
9810          appropriate diagnostic here.  */
9811
9812       /* Consume the `='.  */
9813       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9814       cp_lexer_consume_token (parser->lexer);
9815       
9816       /* Find the name of the parameter pack.  */     
9817       id_declarator = parameter_declarator->declarator;
9818       while (id_declarator && id_declarator->kind != cdk_id)
9819         id_declarator = id_declarator->declarator;
9820       
9821       if (id_declarator && id_declarator->kind == cdk_id)
9822         error ("%Htemplate parameter pack %qD cannot have a default argument",
9823                &start_token->location, id_declarator->u.id.unqualified_name);
9824       else
9825         error ("%Htemplate parameter pack cannot have a default argument",
9826                &start_token->location);
9827       
9828       /* Parse the default argument, but throw away the result.  */
9829       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9830     }
9831
9832   parm = grokdeclarator (parameter_declarator->declarator,
9833                          &parameter_declarator->decl_specifiers,
9834                          PARM, /*initialized=*/0,
9835                          /*attrlist=*/NULL);
9836   if (parm == error_mark_node)
9837     return error_mark_node;
9838
9839   return build_tree_list (parameter_declarator->default_argument, parm);
9840 }
9841
9842 /* Parse a type-parameter.
9843
9844    type-parameter:
9845      class identifier [opt]
9846      class identifier [opt] = type-id
9847      typename identifier [opt]
9848      typename identifier [opt] = type-id
9849      template < template-parameter-list > class identifier [opt]
9850      template < template-parameter-list > class identifier [opt]
9851        = id-expression
9852
9853    GNU Extension (variadic templates):
9854
9855    type-parameter:
9856      class ... identifier [opt]
9857      typename ... identifier [opt]
9858
9859    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9860    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9861    the declaration of the parameter.
9862
9863    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9864
9865 static tree
9866 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9867 {
9868   cp_token *token;
9869   tree parameter;
9870
9871   /* Look for a keyword to tell us what kind of parameter this is.  */
9872   token = cp_parser_require (parser, CPP_KEYWORD,
9873                              "%<class%>, %<typename%>, or %<template%>");
9874   if (!token)
9875     return error_mark_node;
9876
9877   switch (token->keyword)
9878     {
9879     case RID_CLASS:
9880     case RID_TYPENAME:
9881       {
9882         tree identifier;
9883         tree default_argument;
9884
9885         /* If the next token is an ellipsis, we have a template
9886            argument pack. */
9887         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9888           {
9889             /* Consume the `...' token. */
9890             cp_lexer_consume_token (parser->lexer);
9891             maybe_warn_variadic_templates ();
9892
9893             *is_parameter_pack = true;
9894           }
9895
9896         /* If the next token is an identifier, then it names the
9897            parameter.  */
9898         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9899           identifier = cp_parser_identifier (parser);
9900         else
9901           identifier = NULL_TREE;
9902
9903         /* Create the parameter.  */
9904         parameter = finish_template_type_parm (class_type_node, identifier);
9905
9906         /* If the next token is an `=', we have a default argument.  */
9907         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9908           {
9909             /* Consume the `=' token.  */
9910             cp_lexer_consume_token (parser->lexer);
9911             /* Parse the default-argument.  */
9912             push_deferring_access_checks (dk_no_deferred);
9913             default_argument = cp_parser_type_id (parser);
9914
9915             /* Template parameter packs cannot have default
9916                arguments. */
9917             if (*is_parameter_pack)
9918               {
9919                 if (identifier)
9920                   error ("%Htemplate parameter pack %qD cannot have a "
9921                          "default argument", &token->location, identifier);
9922                 else
9923                   error ("%Htemplate parameter packs cannot have "
9924                          "default arguments", &token->location);
9925                 default_argument = NULL_TREE;
9926               }
9927             pop_deferring_access_checks ();
9928           }
9929         else
9930           default_argument = NULL_TREE;
9931
9932         /* Create the combined representation of the parameter and the
9933            default argument.  */
9934         parameter = build_tree_list (default_argument, parameter);
9935       }
9936       break;
9937
9938     case RID_TEMPLATE:
9939       {
9940         tree parameter_list;
9941         tree identifier;
9942         tree default_argument;
9943
9944         /* Look for the `<'.  */
9945         cp_parser_require (parser, CPP_LESS, "%<<%>");
9946         /* Parse the template-parameter-list.  */
9947         parameter_list = cp_parser_template_parameter_list (parser);
9948         /* Look for the `>'.  */
9949         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9950         /* Look for the `class' keyword.  */
9951         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9952         /* If the next token is an ellipsis, we have a template
9953            argument pack. */
9954         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9955           {
9956             /* Consume the `...' token. */
9957             cp_lexer_consume_token (parser->lexer);
9958             maybe_warn_variadic_templates ();
9959
9960             *is_parameter_pack = true;
9961           }
9962         /* If the next token is an `=', then there is a
9963            default-argument.  If the next token is a `>', we are at
9964            the end of the parameter-list.  If the next token is a `,',
9965            then we are at the end of this parameter.  */
9966         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9967             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9968             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9969           {
9970             identifier = cp_parser_identifier (parser);
9971             /* Treat invalid names as if the parameter were nameless.  */
9972             if (identifier == error_mark_node)
9973               identifier = NULL_TREE;
9974           }
9975         else
9976           identifier = NULL_TREE;
9977
9978         /* Create the template parameter.  */
9979         parameter = finish_template_template_parm (class_type_node,
9980                                                    identifier);
9981
9982         /* If the next token is an `=', then there is a
9983            default-argument.  */
9984         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9985           {
9986             bool is_template;
9987
9988             /* Consume the `='.  */
9989             cp_lexer_consume_token (parser->lexer);
9990             /* Parse the id-expression.  */
9991             push_deferring_access_checks (dk_no_deferred);
9992             /* save token before parsing the id-expression, for error
9993                reporting */
9994             token = cp_lexer_peek_token (parser->lexer);
9995             default_argument
9996               = cp_parser_id_expression (parser,
9997                                          /*template_keyword_p=*/false,
9998                                          /*check_dependency_p=*/true,
9999                                          /*template_p=*/&is_template,
10000                                          /*declarator_p=*/false,
10001                                          /*optional_p=*/false);
10002             if (TREE_CODE (default_argument) == TYPE_DECL)
10003               /* If the id-expression was a template-id that refers to
10004                  a template-class, we already have the declaration here,
10005                  so no further lookup is needed.  */
10006                  ;
10007             else
10008               /* Look up the name.  */
10009               default_argument
10010                 = cp_parser_lookup_name (parser, default_argument,
10011                                          none_type,
10012                                          /*is_template=*/is_template,
10013                                          /*is_namespace=*/false,
10014                                          /*check_dependency=*/true,
10015                                          /*ambiguous_decls=*/NULL,
10016                                          token->location);
10017             /* See if the default argument is valid.  */
10018             default_argument
10019               = check_template_template_default_arg (default_argument);
10020
10021             /* Template parameter packs cannot have default
10022                arguments. */
10023             if (*is_parameter_pack)
10024               {
10025                 if (identifier)
10026                   error ("%Htemplate parameter pack %qD cannot "
10027                          "have a default argument",
10028                          &token->location, identifier);
10029                 else
10030                   error ("%Htemplate parameter packs cannot "
10031                          "have default arguments",
10032                          &token->location);
10033                 default_argument = NULL_TREE;
10034               }
10035             pop_deferring_access_checks ();
10036           }
10037         else
10038           default_argument = NULL_TREE;
10039
10040         /* Create the combined representation of the parameter and the
10041            default argument.  */
10042         parameter = build_tree_list (default_argument, parameter);
10043       }
10044       break;
10045
10046     default:
10047       gcc_unreachable ();
10048       break;
10049     }
10050
10051   return parameter;
10052 }
10053
10054 /* Parse a template-id.
10055
10056    template-id:
10057      template-name < template-argument-list [opt] >
10058
10059    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10060    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10061    returned.  Otherwise, if the template-name names a function, or set
10062    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10063    names a class, returns a TYPE_DECL for the specialization.
10064
10065    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10066    uninstantiated templates.  */
10067
10068 static tree
10069 cp_parser_template_id (cp_parser *parser,
10070                        bool template_keyword_p,
10071                        bool check_dependency_p,
10072                        bool is_declaration)
10073 {
10074   int i;
10075   tree templ;
10076   tree arguments;
10077   tree template_id;
10078   cp_token_position start_of_id = 0;
10079   deferred_access_check *chk;
10080   VEC (deferred_access_check,gc) *access_check;
10081   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10082   bool is_identifier;
10083
10084   /* If the next token corresponds to a template-id, there is no need
10085      to reparse it.  */
10086   next_token = cp_lexer_peek_token (parser->lexer);
10087   if (next_token->type == CPP_TEMPLATE_ID)
10088     {
10089       struct tree_check *check_value;
10090
10091       /* Get the stored value.  */
10092       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10093       /* Perform any access checks that were deferred.  */
10094       access_check = check_value->checks;
10095       if (access_check)
10096         {
10097           for (i = 0 ;
10098                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10099                ++i)
10100             {
10101               perform_or_defer_access_check (chk->binfo,
10102                                              chk->decl,
10103                                              chk->diag_decl);
10104             }
10105         }
10106       /* Return the stored value.  */
10107       return check_value->value;
10108     }
10109
10110   /* Avoid performing name lookup if there is no possibility of
10111      finding a template-id.  */
10112   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10113       || (next_token->type == CPP_NAME
10114           && !cp_parser_nth_token_starts_template_argument_list_p
10115                (parser, 2)))
10116     {
10117       cp_parser_error (parser, "expected template-id");
10118       return error_mark_node;
10119     }
10120
10121   /* Remember where the template-id starts.  */
10122   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10123     start_of_id = cp_lexer_token_position (parser->lexer, false);
10124
10125   push_deferring_access_checks (dk_deferred);
10126
10127   /* Parse the template-name.  */
10128   is_identifier = false;
10129   token = cp_lexer_peek_token (parser->lexer);
10130   templ = cp_parser_template_name (parser, template_keyword_p,
10131                                    check_dependency_p,
10132                                    is_declaration,
10133                                    &is_identifier);
10134   if (templ == error_mark_node || is_identifier)
10135     {
10136       pop_deferring_access_checks ();
10137       return templ;
10138     }
10139
10140   /* If we find the sequence `[:' after a template-name, it's probably
10141      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10142      parse correctly the argument list.  */
10143   next_token = cp_lexer_peek_token (parser->lexer);
10144   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10145   if (next_token->type == CPP_OPEN_SQUARE
10146       && next_token->flags & DIGRAPH
10147       && next_token_2->type == CPP_COLON
10148       && !(next_token_2->flags & PREV_WHITE))
10149     {
10150       cp_parser_parse_tentatively (parser);
10151       /* Change `:' into `::'.  */
10152       next_token_2->type = CPP_SCOPE;
10153       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10154          CPP_LESS.  */
10155       cp_lexer_consume_token (parser->lexer);
10156
10157       /* Parse the arguments.  */
10158       arguments = cp_parser_enclosed_template_argument_list (parser);
10159       if (!cp_parser_parse_definitely (parser))
10160         {
10161           /* If we couldn't parse an argument list, then we revert our changes
10162              and return simply an error. Maybe this is not a template-id
10163              after all.  */
10164           next_token_2->type = CPP_COLON;
10165           cp_parser_error (parser, "expected %<<%>");
10166           pop_deferring_access_checks ();
10167           return error_mark_node;
10168         }
10169       /* Otherwise, emit an error about the invalid digraph, but continue
10170          parsing because we got our argument list.  */
10171       if (permerror (next_token->location,
10172                      "%<<::%> cannot begin a template-argument list"))
10173         {
10174           static bool hint = false;
10175           inform (next_token->location,
10176                   "%<<:%> is an alternate spelling for %<[%>."
10177                   " Insert whitespace between %<<%> and %<::%>");
10178           if (!hint && !flag_permissive)
10179             {
10180               inform (next_token->location, "(if you use %<-fpermissive%>"
10181                       " G++ will accept your code)");
10182               hint = true;
10183             }
10184         }
10185     }
10186   else
10187     {
10188       /* Look for the `<' that starts the template-argument-list.  */
10189       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10190         {
10191           pop_deferring_access_checks ();
10192           return error_mark_node;
10193         }
10194       /* Parse the arguments.  */
10195       arguments = cp_parser_enclosed_template_argument_list (parser);
10196     }
10197
10198   /* Build a representation of the specialization.  */
10199   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10200     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10201   else if (DECL_CLASS_TEMPLATE_P (templ)
10202            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10203     {
10204       bool entering_scope;
10205       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10206          template (rather than some instantiation thereof) only if
10207          is not nested within some other construct.  For example, in
10208          "template <typename T> void f(T) { A<T>::", A<T> is just an
10209          instantiation of A.  */
10210       entering_scope = (template_parm_scope_p ()
10211                         && cp_lexer_next_token_is (parser->lexer,
10212                                                    CPP_SCOPE));
10213       template_id
10214         = finish_template_type (templ, arguments, entering_scope);
10215     }
10216   else
10217     {
10218       /* If it's not a class-template or a template-template, it should be
10219          a function-template.  */
10220       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10221                    || TREE_CODE (templ) == OVERLOAD
10222                    || BASELINK_P (templ)));
10223
10224       template_id = lookup_template_function (templ, arguments);
10225     }
10226
10227   /* If parsing tentatively, replace the sequence of tokens that makes
10228      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10229      should we re-parse the token stream, we will not have to repeat
10230      the effort required to do the parse, nor will we issue duplicate
10231      error messages about problems during instantiation of the
10232      template.  */
10233   if (start_of_id)
10234     {
10235       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10236
10237       /* Reset the contents of the START_OF_ID token.  */
10238       token->type = CPP_TEMPLATE_ID;
10239       /* Retrieve any deferred checks.  Do not pop this access checks yet
10240          so the memory will not be reclaimed during token replacing below.  */
10241       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10242       token->u.tree_check_value->value = template_id;
10243       token->u.tree_check_value->checks = get_deferred_access_checks ();
10244       token->keyword = RID_MAX;
10245
10246       /* Purge all subsequent tokens.  */
10247       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10248
10249       /* ??? Can we actually assume that, if template_id ==
10250          error_mark_node, we will have issued a diagnostic to the
10251          user, as opposed to simply marking the tentative parse as
10252          failed?  */
10253       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10254         error ("%Hparse error in template argument list",
10255                &token->location);
10256     }
10257
10258   pop_deferring_access_checks ();
10259   return template_id;
10260 }
10261
10262 /* Parse a template-name.
10263
10264    template-name:
10265      identifier
10266
10267    The standard should actually say:
10268
10269    template-name:
10270      identifier
10271      operator-function-id
10272
10273    A defect report has been filed about this issue.
10274
10275    A conversion-function-id cannot be a template name because they cannot
10276    be part of a template-id. In fact, looking at this code:
10277
10278    a.operator K<int>()
10279
10280    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10281    It is impossible to call a templated conversion-function-id with an
10282    explicit argument list, since the only allowed template parameter is
10283    the type to which it is converting.
10284
10285    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10286    `template' keyword, in a construction like:
10287
10288      T::template f<3>()
10289
10290    In that case `f' is taken to be a template-name, even though there
10291    is no way of knowing for sure.
10292
10293    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10294    name refers to a set of overloaded functions, at least one of which
10295    is a template, or an IDENTIFIER_NODE with the name of the template,
10296    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10297    names are looked up inside uninstantiated templates.  */
10298
10299 static tree
10300 cp_parser_template_name (cp_parser* parser,
10301                          bool template_keyword_p,
10302                          bool check_dependency_p,
10303                          bool is_declaration,
10304                          bool *is_identifier)
10305 {
10306   tree identifier;
10307   tree decl;
10308   tree fns;
10309   cp_token *token = cp_lexer_peek_token (parser->lexer);
10310
10311   /* If the next token is `operator', then we have either an
10312      operator-function-id or a conversion-function-id.  */
10313   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10314     {
10315       /* We don't know whether we're looking at an
10316          operator-function-id or a conversion-function-id.  */
10317       cp_parser_parse_tentatively (parser);
10318       /* Try an operator-function-id.  */
10319       identifier = cp_parser_operator_function_id (parser);
10320       /* If that didn't work, try a conversion-function-id.  */
10321       if (!cp_parser_parse_definitely (parser))
10322         {
10323           cp_parser_error (parser, "expected template-name");
10324           return error_mark_node;
10325         }
10326     }
10327   /* Look for the identifier.  */
10328   else
10329     identifier = cp_parser_identifier (parser);
10330
10331   /* If we didn't find an identifier, we don't have a template-id.  */
10332   if (identifier == error_mark_node)
10333     return error_mark_node;
10334
10335   /* If the name immediately followed the `template' keyword, then it
10336      is a template-name.  However, if the next token is not `<', then
10337      we do not treat it as a template-name, since it is not being used
10338      as part of a template-id.  This enables us to handle constructs
10339      like:
10340
10341        template <typename T> struct S { S(); };
10342        template <typename T> S<T>::S();
10343
10344      correctly.  We would treat `S' as a template -- if it were `S<T>'
10345      -- but we do not if there is no `<'.  */
10346
10347   if (processing_template_decl
10348       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10349     {
10350       /* In a declaration, in a dependent context, we pretend that the
10351          "template" keyword was present in order to improve error
10352          recovery.  For example, given:
10353
10354            template <typename T> void f(T::X<int>);
10355
10356          we want to treat "X<int>" as a template-id.  */
10357       if (is_declaration
10358           && !template_keyword_p
10359           && parser->scope && TYPE_P (parser->scope)
10360           && check_dependency_p
10361           && dependent_scope_p (parser->scope)
10362           /* Do not do this for dtors (or ctors), since they never
10363              need the template keyword before their name.  */
10364           && !constructor_name_p (identifier, parser->scope))
10365         {
10366           cp_token_position start = 0;
10367
10368           /* Explain what went wrong.  */
10369           error ("%Hnon-template %qD used as template",
10370                  &token->location, identifier);
10371           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10372                   parser->scope, identifier);
10373           /* If parsing tentatively, find the location of the "<" token.  */
10374           if (cp_parser_simulate_error (parser))
10375             start = cp_lexer_token_position (parser->lexer, true);
10376           /* Parse the template arguments so that we can issue error
10377              messages about them.  */
10378           cp_lexer_consume_token (parser->lexer);
10379           cp_parser_enclosed_template_argument_list (parser);
10380           /* Skip tokens until we find a good place from which to
10381              continue parsing.  */
10382           cp_parser_skip_to_closing_parenthesis (parser,
10383                                                  /*recovering=*/true,
10384                                                  /*or_comma=*/true,
10385                                                  /*consume_paren=*/false);
10386           /* If parsing tentatively, permanently remove the
10387              template argument list.  That will prevent duplicate
10388              error messages from being issued about the missing
10389              "template" keyword.  */
10390           if (start)
10391             cp_lexer_purge_tokens_after (parser->lexer, start);
10392           if (is_identifier)
10393             *is_identifier = true;
10394           return identifier;
10395         }
10396
10397       /* If the "template" keyword is present, then there is generally
10398          no point in doing name-lookup, so we just return IDENTIFIER.
10399          But, if the qualifying scope is non-dependent then we can
10400          (and must) do name-lookup normally.  */
10401       if (template_keyword_p
10402           && (!parser->scope
10403               || (TYPE_P (parser->scope)
10404                   && dependent_type_p (parser->scope))))
10405         return identifier;
10406     }
10407
10408   /* Look up the name.  */
10409   decl = cp_parser_lookup_name (parser, identifier,
10410                                 none_type,
10411                                 /*is_template=*/false,
10412                                 /*is_namespace=*/false,
10413                                 check_dependency_p,
10414                                 /*ambiguous_decls=*/NULL,
10415                                 token->location);
10416   decl = maybe_get_template_decl_from_type_decl (decl);
10417
10418   /* If DECL is a template, then the name was a template-name.  */
10419   if (TREE_CODE (decl) == TEMPLATE_DECL)
10420     ;
10421   else
10422     {
10423       tree fn = NULL_TREE;
10424
10425       /* The standard does not explicitly indicate whether a name that
10426          names a set of overloaded declarations, some of which are
10427          templates, is a template-name.  However, such a name should
10428          be a template-name; otherwise, there is no way to form a
10429          template-id for the overloaded templates.  */
10430       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10431       if (TREE_CODE (fns) == OVERLOAD)
10432         for (fn = fns; fn; fn = OVL_NEXT (fn))
10433           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10434             break;
10435
10436       if (!fn)
10437         {
10438           /* The name does not name a template.  */
10439           cp_parser_error (parser, "expected template-name");
10440           return error_mark_node;
10441         }
10442     }
10443
10444   /* If DECL is dependent, and refers to a function, then just return
10445      its name; we will look it up again during template instantiation.  */
10446   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10447     {
10448       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10449       if (TYPE_P (scope) && dependent_type_p (scope))
10450         return identifier;
10451     }
10452
10453   return decl;
10454 }
10455
10456 /* Parse a template-argument-list.
10457
10458    template-argument-list:
10459      template-argument ... [opt]
10460      template-argument-list , template-argument ... [opt]
10461
10462    Returns a TREE_VEC containing the arguments.  */
10463
10464 static tree
10465 cp_parser_template_argument_list (cp_parser* parser)
10466 {
10467   tree fixed_args[10];
10468   unsigned n_args = 0;
10469   unsigned alloced = 10;
10470   tree *arg_ary = fixed_args;
10471   tree vec;
10472   bool saved_in_template_argument_list_p;
10473   bool saved_ice_p;
10474   bool saved_non_ice_p;
10475
10476   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10477   parser->in_template_argument_list_p = true;
10478   /* Even if the template-id appears in an integral
10479      constant-expression, the contents of the argument list do
10480      not.  */
10481   saved_ice_p = parser->integral_constant_expression_p;
10482   parser->integral_constant_expression_p = false;
10483   saved_non_ice_p = parser->non_integral_constant_expression_p;
10484   parser->non_integral_constant_expression_p = false;
10485   /* Parse the arguments.  */
10486   do
10487     {
10488       tree argument;
10489
10490       if (n_args)
10491         /* Consume the comma.  */
10492         cp_lexer_consume_token (parser->lexer);
10493
10494       /* Parse the template-argument.  */
10495       argument = cp_parser_template_argument (parser);
10496
10497       /* If the next token is an ellipsis, we're expanding a template
10498          argument pack. */
10499       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10500         {
10501           if (argument == error_mark_node)
10502             {
10503               cp_token *token = cp_lexer_peek_token (parser->lexer);
10504               error ("%Hexpected parameter pack before %<...%>",
10505                      &token->location);
10506             }
10507           /* Consume the `...' token. */
10508           cp_lexer_consume_token (parser->lexer);
10509
10510           /* Make the argument into a TYPE_PACK_EXPANSION or
10511              EXPR_PACK_EXPANSION. */
10512           argument = make_pack_expansion (argument);
10513         }
10514
10515       if (n_args == alloced)
10516         {
10517           alloced *= 2;
10518
10519           if (arg_ary == fixed_args)
10520             {
10521               arg_ary = XNEWVEC (tree, alloced);
10522               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10523             }
10524           else
10525             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10526         }
10527       arg_ary[n_args++] = argument;
10528     }
10529   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10530
10531   vec = make_tree_vec (n_args);
10532
10533   while (n_args--)
10534     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10535
10536   if (arg_ary != fixed_args)
10537     free (arg_ary);
10538   parser->non_integral_constant_expression_p = saved_non_ice_p;
10539   parser->integral_constant_expression_p = saved_ice_p;
10540   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10541   return vec;
10542 }
10543
10544 /* Parse a template-argument.
10545
10546    template-argument:
10547      assignment-expression
10548      type-id
10549      id-expression
10550
10551    The representation is that of an assignment-expression, type-id, or
10552    id-expression -- except that the qualified id-expression is
10553    evaluated, so that the value returned is either a DECL or an
10554    OVERLOAD.
10555
10556    Although the standard says "assignment-expression", it forbids
10557    throw-expressions or assignments in the template argument.
10558    Therefore, we use "conditional-expression" instead.  */
10559
10560 static tree
10561 cp_parser_template_argument (cp_parser* parser)
10562 {
10563   tree argument;
10564   bool template_p;
10565   bool address_p;
10566   bool maybe_type_id = false;
10567   cp_token *token = NULL, *argument_start_token = NULL;
10568   cp_id_kind idk;
10569
10570   /* There's really no way to know what we're looking at, so we just
10571      try each alternative in order.
10572
10573        [temp.arg]
10574
10575        In a template-argument, an ambiguity between a type-id and an
10576        expression is resolved to a type-id, regardless of the form of
10577        the corresponding template-parameter.
10578
10579      Therefore, we try a type-id first.  */
10580   cp_parser_parse_tentatively (parser);
10581   argument = cp_parser_template_type_arg (parser);
10582   /* If there was no error parsing the type-id but the next token is a
10583      '>>', our behavior depends on which dialect of C++ we're
10584      parsing. In C++98, we probably found a typo for '> >'. But there
10585      are type-id which are also valid expressions. For instance:
10586
10587      struct X { int operator >> (int); };
10588      template <int V> struct Foo {};
10589      Foo<X () >> 5> r;
10590
10591      Here 'X()' is a valid type-id of a function type, but the user just
10592      wanted to write the expression "X() >> 5". Thus, we remember that we
10593      found a valid type-id, but we still try to parse the argument as an
10594      expression to see what happens. 
10595
10596      In C++0x, the '>>' will be considered two separate '>'
10597      tokens.  */
10598   if (!cp_parser_error_occurred (parser)
10599       && cxx_dialect == cxx98
10600       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10601     {
10602       maybe_type_id = true;
10603       cp_parser_abort_tentative_parse (parser);
10604     }
10605   else
10606     {
10607       /* If the next token isn't a `,' or a `>', then this argument wasn't
10608       really finished. This means that the argument is not a valid
10609       type-id.  */
10610       if (!cp_parser_next_token_ends_template_argument_p (parser))
10611         cp_parser_error (parser, "expected template-argument");
10612       /* If that worked, we're done.  */
10613       if (cp_parser_parse_definitely (parser))
10614         return argument;
10615     }
10616   /* We're still not sure what the argument will be.  */
10617   cp_parser_parse_tentatively (parser);
10618   /* Try a template.  */
10619   argument_start_token = cp_lexer_peek_token (parser->lexer);
10620   argument = cp_parser_id_expression (parser,
10621                                       /*template_keyword_p=*/false,
10622                                       /*check_dependency_p=*/true,
10623                                       &template_p,
10624                                       /*declarator_p=*/false,
10625                                       /*optional_p=*/false);
10626   /* If the next token isn't a `,' or a `>', then this argument wasn't
10627      really finished.  */
10628   if (!cp_parser_next_token_ends_template_argument_p (parser))
10629     cp_parser_error (parser, "expected template-argument");
10630   if (!cp_parser_error_occurred (parser))
10631     {
10632       /* Figure out what is being referred to.  If the id-expression
10633          was for a class template specialization, then we will have a
10634          TYPE_DECL at this point.  There is no need to do name lookup
10635          at this point in that case.  */
10636       if (TREE_CODE (argument) != TYPE_DECL)
10637         argument = cp_parser_lookup_name (parser, argument,
10638                                           none_type,
10639                                           /*is_template=*/template_p,
10640                                           /*is_namespace=*/false,
10641                                           /*check_dependency=*/true,
10642                                           /*ambiguous_decls=*/NULL,
10643                                           argument_start_token->location);
10644       if (TREE_CODE (argument) != TEMPLATE_DECL
10645           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10646         cp_parser_error (parser, "expected template-name");
10647     }
10648   if (cp_parser_parse_definitely (parser))
10649     return argument;
10650   /* It must be a non-type argument.  There permitted cases are given
10651      in [temp.arg.nontype]:
10652
10653      -- an integral constant-expression of integral or enumeration
10654         type; or
10655
10656      -- the name of a non-type template-parameter; or
10657
10658      -- the name of an object or function with external linkage...
10659
10660      -- the address of an object or function with external linkage...
10661
10662      -- a pointer to member...  */
10663   /* Look for a non-type template parameter.  */
10664   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10665     {
10666       cp_parser_parse_tentatively (parser);
10667       argument = cp_parser_primary_expression (parser,
10668                                                /*address_p=*/false,
10669                                                /*cast_p=*/false,
10670                                                /*template_arg_p=*/true,
10671                                                &idk);
10672       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10673           || !cp_parser_next_token_ends_template_argument_p (parser))
10674         cp_parser_simulate_error (parser);
10675       if (cp_parser_parse_definitely (parser))
10676         return argument;
10677     }
10678
10679   /* If the next token is "&", the argument must be the address of an
10680      object or function with external linkage.  */
10681   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10682   if (address_p)
10683     cp_lexer_consume_token (parser->lexer);
10684   /* See if we might have an id-expression.  */
10685   token = cp_lexer_peek_token (parser->lexer);
10686   if (token->type == CPP_NAME
10687       || token->keyword == RID_OPERATOR
10688       || token->type == CPP_SCOPE
10689       || token->type == CPP_TEMPLATE_ID
10690       || token->type == CPP_NESTED_NAME_SPECIFIER)
10691     {
10692       cp_parser_parse_tentatively (parser);
10693       argument = cp_parser_primary_expression (parser,
10694                                                address_p,
10695                                                /*cast_p=*/false,
10696                                                /*template_arg_p=*/true,
10697                                                &idk);
10698       if (cp_parser_error_occurred (parser)
10699           || !cp_parser_next_token_ends_template_argument_p (parser))
10700         cp_parser_abort_tentative_parse (parser);
10701       else
10702         {
10703           tree probe;
10704
10705           if (TREE_CODE (argument) == INDIRECT_REF)
10706             {
10707               gcc_assert (REFERENCE_REF_P (argument));
10708               argument = TREE_OPERAND (argument, 0);
10709             }
10710
10711           /* If we're in a template, we represent a qualified-id referring
10712              to a static data member as a SCOPE_REF even if the scope isn't
10713              dependent so that we can check access control later.  */
10714           probe = argument;
10715           if (TREE_CODE (probe) == SCOPE_REF)
10716             probe = TREE_OPERAND (probe, 1);
10717           if (TREE_CODE (probe) == VAR_DECL)
10718             {
10719               /* A variable without external linkage might still be a
10720                  valid constant-expression, so no error is issued here
10721                  if the external-linkage check fails.  */
10722               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
10723                 cp_parser_simulate_error (parser);
10724             }
10725           else if (is_overloaded_fn (argument))
10726             /* All overloaded functions are allowed; if the external
10727                linkage test does not pass, an error will be issued
10728                later.  */
10729             ;
10730           else if (address_p
10731                    && (TREE_CODE (argument) == OFFSET_REF
10732                        || TREE_CODE (argument) == SCOPE_REF))
10733             /* A pointer-to-member.  */
10734             ;
10735           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10736             ;
10737           else
10738             cp_parser_simulate_error (parser);
10739
10740           if (cp_parser_parse_definitely (parser))
10741             {
10742               if (address_p)
10743                 argument = build_x_unary_op (ADDR_EXPR, argument,
10744                                              tf_warning_or_error);
10745               return argument;
10746             }
10747         }
10748     }
10749   /* If the argument started with "&", there are no other valid
10750      alternatives at this point.  */
10751   if (address_p)
10752     {
10753       cp_parser_error (parser, "invalid non-type template argument");
10754       return error_mark_node;
10755     }
10756
10757   /* If the argument wasn't successfully parsed as a type-id followed
10758      by '>>', the argument can only be a constant expression now.
10759      Otherwise, we try parsing the constant-expression tentatively,
10760      because the argument could really be a type-id.  */
10761   if (maybe_type_id)
10762     cp_parser_parse_tentatively (parser);
10763   argument = cp_parser_constant_expression (parser,
10764                                             /*allow_non_constant_p=*/false,
10765                                             /*non_constant_p=*/NULL);
10766   argument = fold_non_dependent_expr (argument);
10767   if (!maybe_type_id)
10768     return argument;
10769   if (!cp_parser_next_token_ends_template_argument_p (parser))
10770     cp_parser_error (parser, "expected template-argument");
10771   if (cp_parser_parse_definitely (parser))
10772     return argument;
10773   /* We did our best to parse the argument as a non type-id, but that
10774      was the only alternative that matched (albeit with a '>' after
10775      it). We can assume it's just a typo from the user, and a
10776      diagnostic will then be issued.  */
10777   return cp_parser_template_type_arg (parser);
10778 }
10779
10780 /* Parse an explicit-instantiation.
10781
10782    explicit-instantiation:
10783      template declaration
10784
10785    Although the standard says `declaration', what it really means is:
10786
10787    explicit-instantiation:
10788      template decl-specifier-seq [opt] declarator [opt] ;
10789
10790    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10791    supposed to be allowed.  A defect report has been filed about this
10792    issue.
10793
10794    GNU Extension:
10795
10796    explicit-instantiation:
10797      storage-class-specifier template
10798        decl-specifier-seq [opt] declarator [opt] ;
10799      function-specifier template
10800        decl-specifier-seq [opt] declarator [opt] ;  */
10801
10802 static void
10803 cp_parser_explicit_instantiation (cp_parser* parser)
10804 {
10805   int declares_class_or_enum;
10806   cp_decl_specifier_seq decl_specifiers;
10807   tree extension_specifier = NULL_TREE;
10808   cp_token *token;
10809
10810   /* Look for an (optional) storage-class-specifier or
10811      function-specifier.  */
10812   if (cp_parser_allow_gnu_extensions_p (parser))
10813     {
10814       extension_specifier
10815         = cp_parser_storage_class_specifier_opt (parser);
10816       if (!extension_specifier)
10817         extension_specifier
10818           = cp_parser_function_specifier_opt (parser,
10819                                               /*decl_specs=*/NULL);
10820     }
10821
10822   /* Look for the `template' keyword.  */
10823   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10824   /* Let the front end know that we are processing an explicit
10825      instantiation.  */
10826   begin_explicit_instantiation ();
10827   /* [temp.explicit] says that we are supposed to ignore access
10828      control while processing explicit instantiation directives.  */
10829   push_deferring_access_checks (dk_no_check);
10830   /* Parse a decl-specifier-seq.  */
10831   token = cp_lexer_peek_token (parser->lexer);
10832   cp_parser_decl_specifier_seq (parser,
10833                                 CP_PARSER_FLAGS_OPTIONAL,
10834                                 &decl_specifiers,
10835                                 &declares_class_or_enum);
10836   /* If there was exactly one decl-specifier, and it declared a class,
10837      and there's no declarator, then we have an explicit type
10838      instantiation.  */
10839   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10840     {
10841       tree type;
10842
10843       type = check_tag_decl (&decl_specifiers);
10844       /* Turn access control back on for names used during
10845          template instantiation.  */
10846       pop_deferring_access_checks ();
10847       if (type)
10848         do_type_instantiation (type, extension_specifier,
10849                                /*complain=*/tf_error);
10850     }
10851   else
10852     {
10853       cp_declarator *declarator;
10854       tree decl;
10855
10856       /* Parse the declarator.  */
10857       declarator
10858         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10859                                 /*ctor_dtor_or_conv_p=*/NULL,
10860                                 /*parenthesized_p=*/NULL,
10861                                 /*member_p=*/false);
10862       if (declares_class_or_enum & 2)
10863         cp_parser_check_for_definition_in_return_type (declarator,
10864                                                        decl_specifiers.type,
10865                                                        decl_specifiers.type_location);
10866       if (declarator != cp_error_declarator)
10867         {
10868           decl = grokdeclarator (declarator, &decl_specifiers,
10869                                  NORMAL, 0, &decl_specifiers.attributes);
10870           /* Turn access control back on for names used during
10871              template instantiation.  */
10872           pop_deferring_access_checks ();
10873           /* Do the explicit instantiation.  */
10874           do_decl_instantiation (decl, extension_specifier);
10875         }
10876       else
10877         {
10878           pop_deferring_access_checks ();
10879           /* Skip the body of the explicit instantiation.  */
10880           cp_parser_skip_to_end_of_statement (parser);
10881         }
10882     }
10883   /* We're done with the instantiation.  */
10884   end_explicit_instantiation ();
10885
10886   cp_parser_consume_semicolon_at_end_of_statement (parser);
10887 }
10888
10889 /* Parse an explicit-specialization.
10890
10891    explicit-specialization:
10892      template < > declaration
10893
10894    Although the standard says `declaration', what it really means is:
10895
10896    explicit-specialization:
10897      template <> decl-specifier [opt] init-declarator [opt] ;
10898      template <> function-definition
10899      template <> explicit-specialization
10900      template <> template-declaration  */
10901
10902 static void
10903 cp_parser_explicit_specialization (cp_parser* parser)
10904 {
10905   bool need_lang_pop;
10906   cp_token *token = cp_lexer_peek_token (parser->lexer);
10907
10908   /* Look for the `template' keyword.  */
10909   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10910   /* Look for the `<'.  */
10911   cp_parser_require (parser, CPP_LESS, "%<<%>");
10912   /* Look for the `>'.  */
10913   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10914   /* We have processed another parameter list.  */
10915   ++parser->num_template_parameter_lists;
10916   /* [temp]
10917
10918      A template ... explicit specialization ... shall not have C
10919      linkage.  */
10920   if (current_lang_name == lang_name_c)
10921     {
10922       error ("%Htemplate specialization with C linkage", &token->location);
10923       /* Give it C++ linkage to avoid confusing other parts of the
10924          front end.  */
10925       push_lang_context (lang_name_cplusplus);
10926       need_lang_pop = true;
10927     }
10928   else
10929     need_lang_pop = false;
10930   /* Let the front end know that we are beginning a specialization.  */
10931   if (!begin_specialization ())
10932     {
10933       end_specialization ();
10934       return;
10935     }
10936
10937   /* If the next keyword is `template', we need to figure out whether
10938      or not we're looking a template-declaration.  */
10939   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10940     {
10941       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10942           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10943         cp_parser_template_declaration_after_export (parser,
10944                                                      /*member_p=*/false);
10945       else
10946         cp_parser_explicit_specialization (parser);
10947     }
10948   else
10949     /* Parse the dependent declaration.  */
10950     cp_parser_single_declaration (parser,
10951                                   /*checks=*/NULL,
10952                                   /*member_p=*/false,
10953                                   /*explicit_specialization_p=*/true,
10954                                   /*friend_p=*/NULL);
10955   /* We're done with the specialization.  */
10956   end_specialization ();
10957   /* For the erroneous case of a template with C linkage, we pushed an
10958      implicit C++ linkage scope; exit that scope now.  */
10959   if (need_lang_pop)
10960     pop_lang_context ();
10961   /* We're done with this parameter list.  */
10962   --parser->num_template_parameter_lists;
10963 }
10964
10965 /* Parse a type-specifier.
10966
10967    type-specifier:
10968      simple-type-specifier
10969      class-specifier
10970      enum-specifier
10971      elaborated-type-specifier
10972      cv-qualifier
10973
10974    GNU Extension:
10975
10976    type-specifier:
10977      __complex__
10978
10979    Returns a representation of the type-specifier.  For a
10980    class-specifier, enum-specifier, or elaborated-type-specifier, a
10981    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10982
10983    The parser flags FLAGS is used to control type-specifier parsing.
10984
10985    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10986    in a decl-specifier-seq.
10987
10988    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10989    class-specifier, enum-specifier, or elaborated-type-specifier, then
10990    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10991    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10992    zero.
10993
10994    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10995    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10996    is set to FALSE.  */
10997
10998 static tree
10999 cp_parser_type_specifier (cp_parser* parser,
11000                           cp_parser_flags flags,
11001                           cp_decl_specifier_seq *decl_specs,
11002                           bool is_declaration,
11003                           int* declares_class_or_enum,
11004                           bool* is_cv_qualifier)
11005 {
11006   tree type_spec = NULL_TREE;
11007   cp_token *token;
11008   enum rid keyword;
11009   cp_decl_spec ds = ds_last;
11010
11011   /* Assume this type-specifier does not declare a new type.  */
11012   if (declares_class_or_enum)
11013     *declares_class_or_enum = 0;
11014   /* And that it does not specify a cv-qualifier.  */
11015   if (is_cv_qualifier)
11016     *is_cv_qualifier = false;
11017   /* Peek at the next token.  */
11018   token = cp_lexer_peek_token (parser->lexer);
11019
11020   /* If we're looking at a keyword, we can use that to guide the
11021      production we choose.  */
11022   keyword = token->keyword;
11023   switch (keyword)
11024     {
11025     case RID_ENUM:
11026       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11027         goto elaborated_type_specifier;
11028
11029       /* Look for the enum-specifier.  */
11030       type_spec = cp_parser_enum_specifier (parser);
11031       /* If that worked, we're done.  */
11032       if (type_spec)
11033         {
11034           if (declares_class_or_enum)
11035             *declares_class_or_enum = 2;
11036           if (decl_specs)
11037             cp_parser_set_decl_spec_type (decl_specs,
11038                                           type_spec,
11039                                           token->location,
11040                                           /*user_defined_p=*/true);
11041           return type_spec;
11042         }
11043       else
11044         goto elaborated_type_specifier;
11045
11046       /* Any of these indicate either a class-specifier, or an
11047          elaborated-type-specifier.  */
11048     case RID_CLASS:
11049     case RID_STRUCT:
11050     case RID_UNION:
11051       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11052         goto elaborated_type_specifier;
11053
11054       /* Parse tentatively so that we can back up if we don't find a
11055          class-specifier.  */
11056       cp_parser_parse_tentatively (parser);
11057       /* Look for the class-specifier.  */
11058       type_spec = cp_parser_class_specifier (parser);
11059       /* If that worked, we're done.  */
11060       if (cp_parser_parse_definitely (parser))
11061         {
11062           if (declares_class_or_enum)
11063             *declares_class_or_enum = 2;
11064           if (decl_specs)
11065             cp_parser_set_decl_spec_type (decl_specs,
11066                                           type_spec,
11067                                           token->location,
11068                                           /*user_defined_p=*/true);
11069           return type_spec;
11070         }
11071
11072       /* Fall through.  */
11073     elaborated_type_specifier:
11074       /* We're declaring (not defining) a class or enum.  */
11075       if (declares_class_or_enum)
11076         *declares_class_or_enum = 1;
11077
11078       /* Fall through.  */
11079     case RID_TYPENAME:
11080       /* Look for an elaborated-type-specifier.  */
11081       type_spec
11082         = (cp_parser_elaborated_type_specifier
11083            (parser,
11084             decl_specs && decl_specs->specs[(int) ds_friend],
11085             is_declaration));
11086       if (decl_specs)
11087         cp_parser_set_decl_spec_type (decl_specs,
11088                                       type_spec,
11089                                       token->location,
11090                                       /*user_defined_p=*/true);
11091       return type_spec;
11092
11093     case RID_CONST:
11094       ds = ds_const;
11095       if (is_cv_qualifier)
11096         *is_cv_qualifier = true;
11097       break;
11098
11099     case RID_VOLATILE:
11100       ds = ds_volatile;
11101       if (is_cv_qualifier)
11102         *is_cv_qualifier = true;
11103       break;
11104
11105     case RID_RESTRICT:
11106       ds = ds_restrict;
11107       if (is_cv_qualifier)
11108         *is_cv_qualifier = true;
11109       break;
11110
11111     case RID_COMPLEX:
11112       /* The `__complex__' keyword is a GNU extension.  */
11113       ds = ds_complex;
11114       break;
11115
11116     default:
11117       break;
11118     }
11119
11120   /* Handle simple keywords.  */
11121   if (ds != ds_last)
11122     {
11123       if (decl_specs)
11124         {
11125           ++decl_specs->specs[(int)ds];
11126           decl_specs->any_specifiers_p = true;
11127         }
11128       return cp_lexer_consume_token (parser->lexer)->u.value;
11129     }
11130
11131   /* If we do not already have a type-specifier, assume we are looking
11132      at a simple-type-specifier.  */
11133   type_spec = cp_parser_simple_type_specifier (parser,
11134                                                decl_specs,
11135                                                flags);
11136
11137   /* If we didn't find a type-specifier, and a type-specifier was not
11138      optional in this context, issue an error message.  */
11139   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11140     {
11141       cp_parser_error (parser, "expected type specifier");
11142       return error_mark_node;
11143     }
11144
11145   return type_spec;
11146 }
11147
11148 /* Parse a simple-type-specifier.
11149
11150    simple-type-specifier:
11151      :: [opt] nested-name-specifier [opt] type-name
11152      :: [opt] nested-name-specifier template template-id
11153      char
11154      wchar_t
11155      bool
11156      short
11157      int
11158      long
11159      signed
11160      unsigned
11161      float
11162      double
11163      void
11164
11165    C++0x Extension:
11166
11167    simple-type-specifier:
11168      auto
11169      decltype ( expression )   
11170      char16_t
11171      char32_t
11172
11173    GNU Extension:
11174
11175    simple-type-specifier:
11176      __typeof__ unary-expression
11177      __typeof__ ( type-id )
11178
11179    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11180    appropriately updated.  */
11181
11182 static tree
11183 cp_parser_simple_type_specifier (cp_parser* parser,
11184                                  cp_decl_specifier_seq *decl_specs,
11185                                  cp_parser_flags flags)
11186 {
11187   tree type = NULL_TREE;
11188   cp_token *token;
11189
11190   /* Peek at the next token.  */
11191   token = cp_lexer_peek_token (parser->lexer);
11192
11193   /* If we're looking at a keyword, things are easy.  */
11194   switch (token->keyword)
11195     {
11196     case RID_CHAR:
11197       if (decl_specs)
11198         decl_specs->explicit_char_p = true;
11199       type = char_type_node;
11200       break;
11201     case RID_CHAR16:
11202       type = char16_type_node;
11203       break;
11204     case RID_CHAR32:
11205       type = char32_type_node;
11206       break;
11207     case RID_WCHAR:
11208       type = wchar_type_node;
11209       break;
11210     case RID_BOOL:
11211       type = boolean_type_node;
11212       break;
11213     case RID_SHORT:
11214       if (decl_specs)
11215         ++decl_specs->specs[(int) ds_short];
11216       type = short_integer_type_node;
11217       break;
11218     case RID_INT:
11219       if (decl_specs)
11220         decl_specs->explicit_int_p = true;
11221       type = integer_type_node;
11222       break;
11223     case RID_LONG:
11224       if (decl_specs)
11225         ++decl_specs->specs[(int) ds_long];
11226       type = long_integer_type_node;
11227       break;
11228     case RID_SIGNED:
11229       if (decl_specs)
11230         ++decl_specs->specs[(int) ds_signed];
11231       type = integer_type_node;
11232       break;
11233     case RID_UNSIGNED:
11234       if (decl_specs)
11235         ++decl_specs->specs[(int) ds_unsigned];
11236       type = unsigned_type_node;
11237       break;
11238     case RID_FLOAT:
11239       type = float_type_node;
11240       break;
11241     case RID_DOUBLE:
11242       type = double_type_node;
11243       break;
11244     case RID_VOID:
11245       type = void_type_node;
11246       break;
11247       
11248     case RID_AUTO:
11249       maybe_warn_cpp0x ("C++0x auto");
11250       type = make_auto ();
11251       break;
11252
11253     case RID_DECLTYPE:
11254       /* Parse the `decltype' type.  */
11255       type = cp_parser_decltype (parser);
11256
11257       if (decl_specs)
11258         cp_parser_set_decl_spec_type (decl_specs, type,
11259                                       token->location,
11260                                       /*user_defined_p=*/true);
11261
11262       return type;
11263
11264     case RID_TYPEOF:
11265       /* Consume the `typeof' token.  */
11266       cp_lexer_consume_token (parser->lexer);
11267       /* Parse the operand to `typeof'.  */
11268       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11269       /* If it is not already a TYPE, take its type.  */
11270       if (!TYPE_P (type))
11271         type = finish_typeof (type);
11272
11273       if (decl_specs)
11274         cp_parser_set_decl_spec_type (decl_specs, type,
11275                                       token->location,
11276                                       /*user_defined_p=*/true);
11277
11278       return type;
11279
11280     default:
11281       break;
11282     }
11283
11284   /* If the type-specifier was for a built-in type, we're done.  */
11285   if (type)
11286     {
11287       tree id;
11288
11289       /* Record the type.  */
11290       if (decl_specs
11291           && (token->keyword != RID_SIGNED
11292               && token->keyword != RID_UNSIGNED
11293               && token->keyword != RID_SHORT
11294               && token->keyword != RID_LONG))
11295         cp_parser_set_decl_spec_type (decl_specs,
11296                                       type,
11297                                       token->location,
11298                                       /*user_defined=*/false);
11299       if (decl_specs)
11300         decl_specs->any_specifiers_p = true;
11301
11302       /* Consume the token.  */
11303       id = cp_lexer_consume_token (parser->lexer)->u.value;
11304
11305       /* There is no valid C++ program where a non-template type is
11306          followed by a "<".  That usually indicates that the user thought
11307          that the type was a template.  */
11308       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11309
11310       return TYPE_NAME (type);
11311     }
11312
11313   /* The type-specifier must be a user-defined type.  */
11314   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11315     {
11316       bool qualified_p;
11317       bool global_p;
11318
11319       /* Don't gobble tokens or issue error messages if this is an
11320          optional type-specifier.  */
11321       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11322         cp_parser_parse_tentatively (parser);
11323
11324       /* Look for the optional `::' operator.  */
11325       global_p
11326         = (cp_parser_global_scope_opt (parser,
11327                                        /*current_scope_valid_p=*/false)
11328            != NULL_TREE);
11329       /* Look for the nested-name specifier.  */
11330       qualified_p
11331         = (cp_parser_nested_name_specifier_opt (parser,
11332                                                 /*typename_keyword_p=*/false,
11333                                                 /*check_dependency_p=*/true,
11334                                                 /*type_p=*/false,
11335                                                 /*is_declaration=*/false)
11336            != NULL_TREE);
11337       token = cp_lexer_peek_token (parser->lexer);
11338       /* If we have seen a nested-name-specifier, and the next token
11339          is `template', then we are using the template-id production.  */
11340       if (parser->scope
11341           && cp_parser_optional_template_keyword (parser))
11342         {
11343           /* Look for the template-id.  */
11344           type = cp_parser_template_id (parser,
11345                                         /*template_keyword_p=*/true,
11346                                         /*check_dependency_p=*/true,
11347                                         /*is_declaration=*/false);
11348           /* If the template-id did not name a type, we are out of
11349              luck.  */
11350           if (TREE_CODE (type) != TYPE_DECL)
11351             {
11352               cp_parser_error (parser, "expected template-id for type");
11353               type = NULL_TREE;
11354             }
11355         }
11356       /* Otherwise, look for a type-name.  */
11357       else
11358         type = cp_parser_type_name (parser);
11359       /* Keep track of all name-lookups performed in class scopes.  */
11360       if (type
11361           && !global_p
11362           && !qualified_p
11363           && TREE_CODE (type) == TYPE_DECL
11364           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11365         maybe_note_name_used_in_class (DECL_NAME (type), type);
11366       /* If it didn't work out, we don't have a TYPE.  */
11367       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11368           && !cp_parser_parse_definitely (parser))
11369         type = NULL_TREE;
11370       if (type && decl_specs)
11371         cp_parser_set_decl_spec_type (decl_specs, type,
11372                                       token->location,
11373                                       /*user_defined=*/true);
11374     }
11375
11376   /* If we didn't get a type-name, issue an error message.  */
11377   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11378     {
11379       cp_parser_error (parser, "expected type-name");
11380       return error_mark_node;
11381     }
11382
11383   /* There is no valid C++ program where a non-template type is
11384      followed by a "<".  That usually indicates that the user thought
11385      that the type was a template.  */
11386   if (type && type != error_mark_node)
11387     {
11388       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11389          If it is, then the '<'...'>' enclose protocol names rather than
11390          template arguments, and so everything is fine.  */
11391       if (c_dialect_objc ()
11392           && (objc_is_id (type) || objc_is_class_name (type)))
11393         {
11394           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11395           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11396
11397           /* Clobber the "unqualified" type previously entered into
11398              DECL_SPECS with the new, improved protocol-qualified version.  */
11399           if (decl_specs)
11400             decl_specs->type = qual_type;
11401
11402           return qual_type;
11403         }
11404
11405       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11406                                                token->location);
11407     }
11408
11409   return type;
11410 }
11411
11412 /* Parse a type-name.
11413
11414    type-name:
11415      class-name
11416      enum-name
11417      typedef-name
11418
11419    enum-name:
11420      identifier
11421
11422    typedef-name:
11423      identifier
11424
11425    Returns a TYPE_DECL for the type.  */
11426
11427 static tree
11428 cp_parser_type_name (cp_parser* parser)
11429 {
11430   tree type_decl;
11431
11432   /* We can't know yet whether it is a class-name or not.  */
11433   cp_parser_parse_tentatively (parser);
11434   /* Try a class-name.  */
11435   type_decl = cp_parser_class_name (parser,
11436                                     /*typename_keyword_p=*/false,
11437                                     /*template_keyword_p=*/false,
11438                                     none_type,
11439                                     /*check_dependency_p=*/true,
11440                                     /*class_head_p=*/false,
11441                                     /*is_declaration=*/false);
11442   /* If it's not a class-name, keep looking.  */
11443   if (!cp_parser_parse_definitely (parser))
11444     {
11445       /* It must be a typedef-name or an enum-name.  */
11446       return cp_parser_nonclass_name (parser);
11447     }
11448
11449   return type_decl;
11450 }
11451
11452 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11453
11454    enum-name:
11455      identifier
11456
11457    typedef-name:
11458      identifier
11459
11460    Returns a TYPE_DECL for the type.  */
11461
11462 static tree
11463 cp_parser_nonclass_name (cp_parser* parser)
11464 {
11465   tree type_decl;
11466   tree identifier;
11467
11468   cp_token *token = cp_lexer_peek_token (parser->lexer);
11469   identifier = cp_parser_identifier (parser);
11470   if (identifier == error_mark_node)
11471     return error_mark_node;
11472
11473   /* Look up the type-name.  */
11474   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11475
11476   if (TREE_CODE (type_decl) != TYPE_DECL
11477       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11478     {
11479       /* See if this is an Objective-C type.  */
11480       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11481       tree type = objc_get_protocol_qualified_type (identifier, protos);
11482       if (type)
11483         type_decl = TYPE_NAME (type);
11484     }
11485   
11486   /* Issue an error if we did not find a type-name.  */
11487   if (TREE_CODE (type_decl) != TYPE_DECL)
11488     {
11489       if (!cp_parser_simulate_error (parser))
11490         cp_parser_name_lookup_error (parser, identifier, type_decl,
11491                                      "is not a type", token->location);
11492       return error_mark_node;
11493     }
11494   /* Remember that the name was used in the definition of the
11495      current class so that we can check later to see if the
11496      meaning would have been different after the class was
11497      entirely defined.  */
11498   else if (type_decl != error_mark_node
11499            && !parser->scope)
11500     maybe_note_name_used_in_class (identifier, type_decl);
11501   
11502   return type_decl;
11503 }
11504
11505 /* Parse an elaborated-type-specifier.  Note that the grammar given
11506    here incorporates the resolution to DR68.
11507
11508    elaborated-type-specifier:
11509      class-key :: [opt] nested-name-specifier [opt] identifier
11510      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11511      enum-key :: [opt] nested-name-specifier [opt] identifier
11512      typename :: [opt] nested-name-specifier identifier
11513      typename :: [opt] nested-name-specifier template [opt]
11514        template-id
11515
11516    GNU extension:
11517
11518    elaborated-type-specifier:
11519      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11520      class-key attributes :: [opt] nested-name-specifier [opt]
11521                template [opt] template-id
11522      enum attributes :: [opt] nested-name-specifier [opt] identifier
11523
11524    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11525    declared `friend'.  If IS_DECLARATION is TRUE, then this
11526    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11527    something is being declared.
11528
11529    Returns the TYPE specified.  */
11530
11531 static tree
11532 cp_parser_elaborated_type_specifier (cp_parser* parser,
11533                                      bool is_friend,
11534                                      bool is_declaration)
11535 {
11536   enum tag_types tag_type;
11537   tree identifier;
11538   tree type = NULL_TREE;
11539   tree attributes = NULL_TREE;
11540   cp_token *token = NULL;
11541
11542   /* See if we're looking at the `enum' keyword.  */
11543   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11544     {
11545       /* Consume the `enum' token.  */
11546       cp_lexer_consume_token (parser->lexer);
11547       /* Remember that it's an enumeration type.  */
11548       tag_type = enum_type;
11549       /* Parse the optional `struct' or `class' key (for C++0x scoped
11550          enums).  */
11551       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11552           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11553         {
11554           if (cxx_dialect == cxx98)
11555             maybe_warn_cpp0x ("scoped enums");
11556
11557           /* Consume the `struct' or `class'.  */
11558           cp_lexer_consume_token (parser->lexer);
11559         }
11560       /* Parse the attributes.  */
11561       attributes = cp_parser_attributes_opt (parser);
11562     }
11563   /* Or, it might be `typename'.  */
11564   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11565                                            RID_TYPENAME))
11566     {
11567       /* Consume the `typename' token.  */
11568       cp_lexer_consume_token (parser->lexer);
11569       /* Remember that it's a `typename' type.  */
11570       tag_type = typename_type;
11571       /* The `typename' keyword is only allowed in templates.  */
11572       if (!processing_template_decl)
11573         permerror (input_location, "using %<typename%> outside of template");
11574     }
11575   /* Otherwise it must be a class-key.  */
11576   else
11577     {
11578       tag_type = cp_parser_class_key (parser);
11579       if (tag_type == none_type)
11580         return error_mark_node;
11581       /* Parse the attributes.  */
11582       attributes = cp_parser_attributes_opt (parser);
11583     }
11584
11585   /* Look for the `::' operator.  */
11586   cp_parser_global_scope_opt (parser,
11587                               /*current_scope_valid_p=*/false);
11588   /* Look for the nested-name-specifier.  */
11589   if (tag_type == typename_type)
11590     {
11591       if (!cp_parser_nested_name_specifier (parser,
11592                                            /*typename_keyword_p=*/true,
11593                                            /*check_dependency_p=*/true,
11594                                            /*type_p=*/true,
11595                                             is_declaration))
11596         return error_mark_node;
11597     }
11598   else
11599     /* Even though `typename' is not present, the proposed resolution
11600        to Core Issue 180 says that in `class A<T>::B', `B' should be
11601        considered a type-name, even if `A<T>' is dependent.  */
11602     cp_parser_nested_name_specifier_opt (parser,
11603                                          /*typename_keyword_p=*/true,
11604                                          /*check_dependency_p=*/true,
11605                                          /*type_p=*/true,
11606                                          is_declaration);
11607  /* For everything but enumeration types, consider a template-id.
11608     For an enumeration type, consider only a plain identifier.  */
11609   if (tag_type != enum_type)
11610     {
11611       bool template_p = false;
11612       tree decl;
11613
11614       /* Allow the `template' keyword.  */
11615       template_p = cp_parser_optional_template_keyword (parser);
11616       /* If we didn't see `template', we don't know if there's a
11617          template-id or not.  */
11618       if (!template_p)
11619         cp_parser_parse_tentatively (parser);
11620       /* Parse the template-id.  */
11621       token = cp_lexer_peek_token (parser->lexer);
11622       decl = cp_parser_template_id (parser, template_p,
11623                                     /*check_dependency_p=*/true,
11624                                     is_declaration);
11625       /* If we didn't find a template-id, look for an ordinary
11626          identifier.  */
11627       if (!template_p && !cp_parser_parse_definitely (parser))
11628         ;
11629       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11630          in effect, then we must assume that, upon instantiation, the
11631          template will correspond to a class.  */
11632       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11633                && tag_type == typename_type)
11634         type = make_typename_type (parser->scope, decl,
11635                                    typename_type,
11636                                    /*complain=*/tf_error);
11637       /* If the `typename' keyword is in effect and DECL is not a type
11638          decl. Then type is non existant.   */
11639       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11640         type = NULL_TREE; 
11641       else 
11642         type = TREE_TYPE (decl);
11643     }
11644
11645   if (!type)
11646     {
11647       token = cp_lexer_peek_token (parser->lexer);
11648       identifier = cp_parser_identifier (parser);
11649
11650       if (identifier == error_mark_node)
11651         {
11652           parser->scope = NULL_TREE;
11653           return error_mark_node;
11654         }
11655
11656       /* For a `typename', we needn't call xref_tag.  */
11657       if (tag_type == typename_type
11658           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11659         return cp_parser_make_typename_type (parser, parser->scope,
11660                                              identifier,
11661                                              token->location);
11662       /* Look up a qualified name in the usual way.  */
11663       if (parser->scope)
11664         {
11665           tree decl;
11666           tree ambiguous_decls;
11667
11668           decl = cp_parser_lookup_name (parser, identifier,
11669                                         tag_type,
11670                                         /*is_template=*/false,
11671                                         /*is_namespace=*/false,
11672                                         /*check_dependency=*/true,
11673                                         &ambiguous_decls,
11674                                         token->location);
11675
11676           /* If the lookup was ambiguous, an error will already have been
11677              issued.  */
11678           if (ambiguous_decls)
11679             return error_mark_node;
11680
11681           /* If we are parsing friend declaration, DECL may be a
11682              TEMPLATE_DECL tree node here.  However, we need to check
11683              whether this TEMPLATE_DECL results in valid code.  Consider
11684              the following example:
11685
11686                namespace N {
11687                  template <class T> class C {};
11688                }
11689                class X {
11690                  template <class T> friend class N::C; // #1, valid code
11691                };
11692                template <class T> class Y {
11693                  friend class N::C;                    // #2, invalid code
11694                };
11695
11696              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11697              name lookup of `N::C'.  We see that friend declaration must
11698              be template for the code to be valid.  Note that
11699              processing_template_decl does not work here since it is
11700              always 1 for the above two cases.  */
11701
11702           decl = (cp_parser_maybe_treat_template_as_class
11703                   (decl, /*tag_name_p=*/is_friend
11704                          && parser->num_template_parameter_lists));
11705
11706           if (TREE_CODE (decl) != TYPE_DECL)
11707             {
11708               cp_parser_diagnose_invalid_type_name (parser,
11709                                                     parser->scope,
11710                                                     identifier,
11711                                                     token->location);
11712               return error_mark_node;
11713             }
11714
11715           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11716             {
11717               bool allow_template = (parser->num_template_parameter_lists
11718                                       || DECL_SELF_REFERENCE_P (decl));
11719               type = check_elaborated_type_specifier (tag_type, decl, 
11720                                                       allow_template);
11721
11722               if (type == error_mark_node)
11723                 return error_mark_node;
11724             }
11725
11726           /* Forward declarations of nested types, such as
11727
11728                class C1::C2;
11729                class C1::C2::C3;
11730
11731              are invalid unless all components preceding the final '::'
11732              are complete.  If all enclosing types are complete, these
11733              declarations become merely pointless.
11734
11735              Invalid forward declarations of nested types are errors
11736              caught elsewhere in parsing.  Those that are pointless arrive
11737              here.  */
11738
11739           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11740               && !is_friend && !processing_explicit_instantiation)
11741             warning (0, "declaration %qD does not declare anything", decl);
11742
11743           type = TREE_TYPE (decl);
11744         }
11745       else
11746         {
11747           /* An elaborated-type-specifier sometimes introduces a new type and
11748              sometimes names an existing type.  Normally, the rule is that it
11749              introduces a new type only if there is not an existing type of
11750              the same name already in scope.  For example, given:
11751
11752                struct S {};
11753                void f() { struct S s; }
11754
11755              the `struct S' in the body of `f' is the same `struct S' as in
11756              the global scope; the existing definition is used.  However, if
11757              there were no global declaration, this would introduce a new
11758              local class named `S'.
11759
11760              An exception to this rule applies to the following code:
11761
11762                namespace N { struct S; }
11763
11764              Here, the elaborated-type-specifier names a new type
11765              unconditionally; even if there is already an `S' in the
11766              containing scope this declaration names a new type.
11767              This exception only applies if the elaborated-type-specifier
11768              forms the complete declaration:
11769
11770                [class.name]
11771
11772                A declaration consisting solely of `class-key identifier ;' is
11773                either a redeclaration of the name in the current scope or a
11774                forward declaration of the identifier as a class name.  It
11775                introduces the name into the current scope.
11776
11777              We are in this situation precisely when the next token is a `;'.
11778
11779              An exception to the exception is that a `friend' declaration does
11780              *not* name a new type; i.e., given:
11781
11782                struct S { friend struct T; };
11783
11784              `T' is not a new type in the scope of `S'.
11785
11786              Also, `new struct S' or `sizeof (struct S)' never results in the
11787              definition of a new type; a new type can only be declared in a
11788              declaration context.  */
11789
11790           tag_scope ts;
11791           bool template_p;
11792
11793           if (is_friend)
11794             /* Friends have special name lookup rules.  */
11795             ts = ts_within_enclosing_non_class;
11796           else if (is_declaration
11797                    && cp_lexer_next_token_is (parser->lexer,
11798                                               CPP_SEMICOLON))
11799             /* This is a `class-key identifier ;' */
11800             ts = ts_current;
11801           else
11802             ts = ts_global;
11803
11804           template_p =
11805             (parser->num_template_parameter_lists
11806              && (cp_parser_next_token_starts_class_definition_p (parser)
11807                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11808           /* An unqualified name was used to reference this type, so
11809              there were no qualifying templates.  */
11810           if (!cp_parser_check_template_parameters (parser,
11811                                                     /*num_templates=*/0,
11812                                                     token->location))
11813             return error_mark_node;
11814           type = xref_tag (tag_type, identifier, ts, template_p);
11815         }
11816     }
11817
11818   if (type == error_mark_node)
11819     return error_mark_node;
11820
11821   /* Allow attributes on forward declarations of classes.  */
11822   if (attributes)
11823     {
11824       if (TREE_CODE (type) == TYPENAME_TYPE)
11825         warning (OPT_Wattributes,
11826                  "attributes ignored on uninstantiated type");
11827       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11828                && ! processing_explicit_instantiation)
11829         warning (OPT_Wattributes,
11830                  "attributes ignored on template instantiation");
11831       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11832         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11833       else
11834         warning (OPT_Wattributes,
11835                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11836     }
11837
11838   if (tag_type != enum_type)
11839     cp_parser_check_class_key (tag_type, type);
11840
11841   /* A "<" cannot follow an elaborated type specifier.  If that
11842      happens, the user was probably trying to form a template-id.  */
11843   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11844
11845   return type;
11846 }
11847
11848 /* Parse an enum-specifier.
11849
11850    enum-specifier:
11851      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11852
11853    enum-key:
11854      enum
11855      enum class   [C++0x]
11856      enum struct  [C++0x]
11857
11858    enum-base:   [C++0x]
11859      : type-specifier-seq
11860
11861    GNU Extensions:
11862      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11863        { enumerator-list [opt] }attributes[opt]
11864
11865    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11866    if the token stream isn't an enum-specifier after all.  */
11867
11868 static tree
11869 cp_parser_enum_specifier (cp_parser* parser)
11870 {
11871   tree identifier;
11872   tree type;
11873   tree attributes;
11874   bool scoped_enum_p = false;
11875   bool has_underlying_type = false;
11876   tree underlying_type = NULL_TREE;
11877
11878   /* Parse tentatively so that we can back up if we don't find a
11879      enum-specifier.  */
11880   cp_parser_parse_tentatively (parser);
11881
11882   /* Caller guarantees that the current token is 'enum', an identifier
11883      possibly follows, and the token after that is an opening brace.
11884      If we don't have an identifier, fabricate an anonymous name for
11885      the enumeration being defined.  */
11886   cp_lexer_consume_token (parser->lexer);
11887
11888   /* Parse the "class" or "struct", which indicates a scoped
11889      enumeration type in C++0x.  */
11890   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11891       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11892     {
11893       if (cxx_dialect == cxx98)
11894         maybe_warn_cpp0x ("scoped enums");
11895
11896       /* Consume the `struct' or `class' token.  */
11897       cp_lexer_consume_token (parser->lexer);
11898
11899       scoped_enum_p = true;
11900     }
11901
11902   attributes = cp_parser_attributes_opt (parser);
11903
11904   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11905     identifier = cp_parser_identifier (parser);
11906   else
11907     identifier = make_anon_name ();
11908
11909   /* Check for the `:' that denotes a specified underlying type in C++0x.
11910      Note that a ':' could also indicate a bitfield width, however.  */
11911   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11912     {
11913       cp_decl_specifier_seq type_specifiers;
11914
11915       /* Consume the `:'.  */
11916       cp_lexer_consume_token (parser->lexer);
11917
11918       /* Parse the type-specifier-seq.  */
11919       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11920                                     /*is_trailing_return=*/false,
11921                                     &type_specifiers);
11922
11923       /* At this point this is surely not elaborated type specifier.  */
11924       if (!cp_parser_parse_definitely (parser))
11925         return NULL_TREE;
11926
11927       if (cxx_dialect == cxx98)
11928         maybe_warn_cpp0x ("scoped enums");
11929
11930       has_underlying_type = true;
11931
11932       /* If that didn't work, stop.  */
11933       if (type_specifiers.type != error_mark_node)
11934         {
11935           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11936                                             /*initialized=*/0, NULL);
11937           if (underlying_type == error_mark_node)
11938             underlying_type = NULL_TREE;
11939         }
11940     }
11941
11942   /* Look for the `{' but don't consume it yet.  */
11943   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11944     {
11945       cp_parser_error (parser, "expected %<{%>");
11946       if (has_underlying_type)
11947         return NULL_TREE;
11948     }
11949
11950   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11951     return NULL_TREE;
11952
11953   /* Issue an error message if type-definitions are forbidden here.  */
11954   if (!cp_parser_check_type_definition (parser))
11955     type = error_mark_node;
11956   else
11957     /* Create the new type.  We do this before consuming the opening
11958        brace so the enum will be recorded as being on the line of its
11959        tag (or the 'enum' keyword, if there is no tag).  */
11960     type = start_enum (identifier, underlying_type, scoped_enum_p);
11961   
11962   /* Consume the opening brace.  */
11963   cp_lexer_consume_token (parser->lexer);
11964
11965   if (type == error_mark_node)
11966     {
11967       cp_parser_skip_to_end_of_block_or_statement (parser);
11968       return error_mark_node;
11969     }
11970
11971   /* If the next token is not '}', then there are some enumerators.  */
11972   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11973     cp_parser_enumerator_list (parser, type);
11974
11975   /* Consume the final '}'.  */
11976   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11977
11978   /* Look for trailing attributes to apply to this enumeration, and
11979      apply them if appropriate.  */
11980   if (cp_parser_allow_gnu_extensions_p (parser))
11981     {
11982       tree trailing_attr = cp_parser_attributes_opt (parser);
11983       trailing_attr = chainon (trailing_attr, attributes);
11984       cplus_decl_attributes (&type,
11985                              trailing_attr,
11986                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11987     }
11988
11989   /* Finish up the enumeration.  */
11990   finish_enum (type);
11991
11992   return type;
11993 }
11994
11995 /* Parse an enumerator-list.  The enumerators all have the indicated
11996    TYPE.
11997
11998    enumerator-list:
11999      enumerator-definition
12000      enumerator-list , enumerator-definition  */
12001
12002 static void
12003 cp_parser_enumerator_list (cp_parser* parser, tree type)
12004 {
12005   while (true)
12006     {
12007       /* Parse an enumerator-definition.  */
12008       cp_parser_enumerator_definition (parser, type);
12009
12010       /* If the next token is not a ',', we've reached the end of
12011          the list.  */
12012       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12013         break;
12014       /* Otherwise, consume the `,' and keep going.  */
12015       cp_lexer_consume_token (parser->lexer);
12016       /* If the next token is a `}', there is a trailing comma.  */
12017       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12018         {
12019           if (!in_system_header)
12020             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12021           break;
12022         }
12023     }
12024 }
12025
12026 /* Parse an enumerator-definition.  The enumerator has the indicated
12027    TYPE.
12028
12029    enumerator-definition:
12030      enumerator
12031      enumerator = constant-expression
12032
12033    enumerator:
12034      identifier  */
12035
12036 static void
12037 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12038 {
12039   tree identifier;
12040   tree value;
12041
12042   /* Look for the identifier.  */
12043   identifier = cp_parser_identifier (parser);
12044   if (identifier == error_mark_node)
12045     return;
12046
12047   /* If the next token is an '=', then there is an explicit value.  */
12048   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12049     {
12050       /* Consume the `=' token.  */
12051       cp_lexer_consume_token (parser->lexer);
12052       /* Parse the value.  */
12053       value = cp_parser_constant_expression (parser,
12054                                              /*allow_non_constant_p=*/false,
12055                                              NULL);
12056     }
12057   else
12058     value = NULL_TREE;
12059
12060   /* If we are processing a template, make sure the initializer of the
12061      enumerator doesn't contain any bare template parameter pack.  */
12062   if (check_for_bare_parameter_packs (value))
12063     value = error_mark_node;
12064
12065   /* Create the enumerator.  */
12066   build_enumerator (identifier, value, type);
12067 }
12068
12069 /* Parse a namespace-name.
12070
12071    namespace-name:
12072      original-namespace-name
12073      namespace-alias
12074
12075    Returns the NAMESPACE_DECL for the namespace.  */
12076
12077 static tree
12078 cp_parser_namespace_name (cp_parser* parser)
12079 {
12080   tree identifier;
12081   tree namespace_decl;
12082
12083   cp_token *token = cp_lexer_peek_token (parser->lexer);
12084
12085   /* Get the name of the namespace.  */
12086   identifier = cp_parser_identifier (parser);
12087   if (identifier == error_mark_node)
12088     return error_mark_node;
12089
12090   /* Look up the identifier in the currently active scope.  Look only
12091      for namespaces, due to:
12092
12093        [basic.lookup.udir]
12094
12095        When looking up a namespace-name in a using-directive or alias
12096        definition, only namespace names are considered.
12097
12098      And:
12099
12100        [basic.lookup.qual]
12101
12102        During the lookup of a name preceding the :: scope resolution
12103        operator, object, function, and enumerator names are ignored.
12104
12105      (Note that cp_parser_qualifying_entity only calls this
12106      function if the token after the name is the scope resolution
12107      operator.)  */
12108   namespace_decl = cp_parser_lookup_name (parser, identifier,
12109                                           none_type,
12110                                           /*is_template=*/false,
12111                                           /*is_namespace=*/true,
12112                                           /*check_dependency=*/true,
12113                                           /*ambiguous_decls=*/NULL,
12114                                           token->location);
12115   /* If it's not a namespace, issue an error.  */
12116   if (namespace_decl == error_mark_node
12117       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12118     {
12119       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12120         error ("%H%qD is not a namespace-name", &token->location, identifier);
12121       cp_parser_error (parser, "expected namespace-name");
12122       namespace_decl = error_mark_node;
12123     }
12124
12125   return namespace_decl;
12126 }
12127
12128 /* Parse a namespace-definition.
12129
12130    namespace-definition:
12131      named-namespace-definition
12132      unnamed-namespace-definition
12133
12134    named-namespace-definition:
12135      original-namespace-definition
12136      extension-namespace-definition
12137
12138    original-namespace-definition:
12139      namespace identifier { namespace-body }
12140
12141    extension-namespace-definition:
12142      namespace original-namespace-name { namespace-body }
12143
12144    unnamed-namespace-definition:
12145      namespace { namespace-body } */
12146
12147 static void
12148 cp_parser_namespace_definition (cp_parser* parser)
12149 {
12150   tree identifier, attribs;
12151   bool has_visibility;
12152   bool is_inline;
12153
12154   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12155     {
12156       is_inline = true;
12157       cp_lexer_consume_token (parser->lexer);
12158     }
12159   else
12160     is_inline = false;
12161
12162   /* Look for the `namespace' keyword.  */
12163   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12164
12165   /* Get the name of the namespace.  We do not attempt to distinguish
12166      between an original-namespace-definition and an
12167      extension-namespace-definition at this point.  The semantic
12168      analysis routines are responsible for that.  */
12169   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12170     identifier = cp_parser_identifier (parser);
12171   else
12172     identifier = NULL_TREE;
12173
12174   /* Parse any specified attributes.  */
12175   attribs = cp_parser_attributes_opt (parser);
12176
12177   /* Look for the `{' to start the namespace.  */
12178   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12179   /* Start the namespace.  */
12180   push_namespace (identifier);
12181
12182   /* "inline namespace" is equivalent to a stub namespace definition
12183      followed by a strong using directive.  */
12184   if (is_inline)
12185     {
12186       tree name_space = current_namespace;
12187       /* Set up namespace association.  */
12188       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12189         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12190                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12191       /* Import the contents of the inline namespace.  */
12192       pop_namespace ();
12193       do_using_directive (name_space);
12194       push_namespace (identifier);
12195     }
12196
12197   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12198
12199   /* Parse the body of the namespace.  */
12200   cp_parser_namespace_body (parser);
12201
12202 #ifdef HANDLE_PRAGMA_VISIBILITY
12203   if (has_visibility)
12204     pop_visibility ();
12205 #endif
12206
12207   /* Finish the namespace.  */
12208   pop_namespace ();
12209   /* Look for the final `}'.  */
12210   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12211 }
12212
12213 /* Parse a namespace-body.
12214
12215    namespace-body:
12216      declaration-seq [opt]  */
12217
12218 static void
12219 cp_parser_namespace_body (cp_parser* parser)
12220 {
12221   cp_parser_declaration_seq_opt (parser);
12222 }
12223
12224 /* Parse a namespace-alias-definition.
12225
12226    namespace-alias-definition:
12227      namespace identifier = qualified-namespace-specifier ;  */
12228
12229 static void
12230 cp_parser_namespace_alias_definition (cp_parser* parser)
12231 {
12232   tree identifier;
12233   tree namespace_specifier;
12234
12235   cp_token *token = cp_lexer_peek_token (parser->lexer);
12236
12237   /* Look for the `namespace' keyword.  */
12238   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12239   /* Look for the identifier.  */
12240   identifier = cp_parser_identifier (parser);
12241   if (identifier == error_mark_node)
12242     return;
12243   /* Look for the `=' token.  */
12244   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12245       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12246     {
12247       error ("%H%<namespace%> definition is not allowed here", &token->location);
12248       /* Skip the definition.  */
12249       cp_lexer_consume_token (parser->lexer);
12250       if (cp_parser_skip_to_closing_brace (parser))
12251         cp_lexer_consume_token (parser->lexer);
12252       return;
12253     }
12254   cp_parser_require (parser, CPP_EQ, "%<=%>");
12255   /* Look for the qualified-namespace-specifier.  */
12256   namespace_specifier
12257     = cp_parser_qualified_namespace_specifier (parser);
12258   /* Look for the `;' token.  */
12259   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12260
12261   /* Register the alias in the symbol table.  */
12262   do_namespace_alias (identifier, namespace_specifier);
12263 }
12264
12265 /* Parse a qualified-namespace-specifier.
12266
12267    qualified-namespace-specifier:
12268      :: [opt] nested-name-specifier [opt] namespace-name
12269
12270    Returns a NAMESPACE_DECL corresponding to the specified
12271    namespace.  */
12272
12273 static tree
12274 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12275 {
12276   /* Look for the optional `::'.  */
12277   cp_parser_global_scope_opt (parser,
12278                               /*current_scope_valid_p=*/false);
12279
12280   /* Look for the optional nested-name-specifier.  */
12281   cp_parser_nested_name_specifier_opt (parser,
12282                                        /*typename_keyword_p=*/false,
12283                                        /*check_dependency_p=*/true,
12284                                        /*type_p=*/false,
12285                                        /*is_declaration=*/true);
12286
12287   return cp_parser_namespace_name (parser);
12288 }
12289
12290 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12291    access declaration.
12292
12293    using-declaration:
12294      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12295      using :: unqualified-id ;  
12296
12297    access-declaration:
12298      qualified-id ;  
12299
12300    */
12301
12302 static bool
12303 cp_parser_using_declaration (cp_parser* parser, 
12304                              bool access_declaration_p)
12305 {
12306   cp_token *token;
12307   bool typename_p = false;
12308   bool global_scope_p;
12309   tree decl;
12310   tree identifier;
12311   tree qscope;
12312
12313   if (access_declaration_p)
12314     cp_parser_parse_tentatively (parser);
12315   else
12316     {
12317       /* Look for the `using' keyword.  */
12318       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12319       
12320       /* Peek at the next token.  */
12321       token = cp_lexer_peek_token (parser->lexer);
12322       /* See if it's `typename'.  */
12323       if (token->keyword == RID_TYPENAME)
12324         {
12325           /* Remember that we've seen it.  */
12326           typename_p = true;
12327           /* Consume the `typename' token.  */
12328           cp_lexer_consume_token (parser->lexer);
12329         }
12330     }
12331
12332   /* Look for the optional global scope qualification.  */
12333   global_scope_p
12334     = (cp_parser_global_scope_opt (parser,
12335                                    /*current_scope_valid_p=*/false)
12336        != NULL_TREE);
12337
12338   /* If we saw `typename', or didn't see `::', then there must be a
12339      nested-name-specifier present.  */
12340   if (typename_p || !global_scope_p)
12341     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12342                                               /*check_dependency_p=*/true,
12343                                               /*type_p=*/false,
12344                                               /*is_declaration=*/true);
12345   /* Otherwise, we could be in either of the two productions.  In that
12346      case, treat the nested-name-specifier as optional.  */
12347   else
12348     qscope = cp_parser_nested_name_specifier_opt (parser,
12349                                                   /*typename_keyword_p=*/false,
12350                                                   /*check_dependency_p=*/true,
12351                                                   /*type_p=*/false,
12352                                                   /*is_declaration=*/true);
12353   if (!qscope)
12354     qscope = global_namespace;
12355
12356   if (access_declaration_p && cp_parser_error_occurred (parser))
12357     /* Something has already gone wrong; there's no need to parse
12358        further.  Since an error has occurred, the return value of
12359        cp_parser_parse_definitely will be false, as required.  */
12360     return cp_parser_parse_definitely (parser);
12361
12362   token = cp_lexer_peek_token (parser->lexer);
12363   /* Parse the unqualified-id.  */
12364   identifier = cp_parser_unqualified_id (parser,
12365                                          /*template_keyword_p=*/false,
12366                                          /*check_dependency_p=*/true,
12367                                          /*declarator_p=*/true,
12368                                          /*optional_p=*/false);
12369
12370   if (access_declaration_p)
12371     {
12372       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12373         cp_parser_simulate_error (parser);
12374       if (!cp_parser_parse_definitely (parser))
12375         return false;
12376     }
12377
12378   /* The function we call to handle a using-declaration is different
12379      depending on what scope we are in.  */
12380   if (qscope == error_mark_node || identifier == error_mark_node)
12381     ;
12382   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12383            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12384     /* [namespace.udecl]
12385
12386        A using declaration shall not name a template-id.  */
12387     error ("%Ha template-id may not appear in a using-declaration",
12388             &token->location);
12389   else
12390     {
12391       if (at_class_scope_p ())
12392         {
12393           /* Create the USING_DECL.  */
12394           decl = do_class_using_decl (parser->scope, identifier);
12395
12396           if (check_for_bare_parameter_packs (decl))
12397             return false;
12398           else
12399             /* Add it to the list of members in this class.  */
12400             finish_member_declaration (decl);
12401         }
12402       else
12403         {
12404           decl = cp_parser_lookup_name_simple (parser,
12405                                                identifier,
12406                                                token->location);
12407           if (decl == error_mark_node)
12408             cp_parser_name_lookup_error (parser, identifier,
12409                                          decl, NULL,
12410                                          token->location);
12411           else if (check_for_bare_parameter_packs (decl))
12412             return false;
12413           else if (!at_namespace_scope_p ())
12414             do_local_using_decl (decl, qscope, identifier);
12415           else
12416             do_toplevel_using_decl (decl, qscope, identifier);
12417         }
12418     }
12419
12420   /* Look for the final `;'.  */
12421   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12422   
12423   return true;
12424 }
12425
12426 /* Parse a using-directive.
12427
12428    using-directive:
12429      using namespace :: [opt] nested-name-specifier [opt]
12430        namespace-name ;  */
12431
12432 static void
12433 cp_parser_using_directive (cp_parser* parser)
12434 {
12435   tree namespace_decl;
12436   tree attribs;
12437
12438   /* Look for the `using' keyword.  */
12439   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12440   /* And the `namespace' keyword.  */
12441   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12442   /* Look for the optional `::' operator.  */
12443   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12444   /* And the optional nested-name-specifier.  */
12445   cp_parser_nested_name_specifier_opt (parser,
12446                                        /*typename_keyword_p=*/false,
12447                                        /*check_dependency_p=*/true,
12448                                        /*type_p=*/false,
12449                                        /*is_declaration=*/true);
12450   /* Get the namespace being used.  */
12451   namespace_decl = cp_parser_namespace_name (parser);
12452   /* And any specified attributes.  */
12453   attribs = cp_parser_attributes_opt (parser);
12454   /* Update the symbol table.  */
12455   parse_using_directive (namespace_decl, attribs);
12456   /* Look for the final `;'.  */
12457   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12458 }
12459
12460 /* Parse an asm-definition.
12461
12462    asm-definition:
12463      asm ( string-literal ) ;
12464
12465    GNU Extension:
12466
12467    asm-definition:
12468      asm volatile [opt] ( string-literal ) ;
12469      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12470      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12471                           : asm-operand-list [opt] ) ;
12472      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12473                           : asm-operand-list [opt]
12474                           : asm-operand-list [opt] ) ;  */
12475
12476 static void
12477 cp_parser_asm_definition (cp_parser* parser)
12478 {
12479   tree string;
12480   tree outputs = NULL_TREE;
12481   tree inputs = NULL_TREE;
12482   tree clobbers = NULL_TREE;
12483   tree asm_stmt;
12484   bool volatile_p = false;
12485   bool extended_p = false;
12486   bool invalid_inputs_p = false;
12487   bool invalid_outputs_p = false;
12488
12489   /* Look for the `asm' keyword.  */
12490   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12491   /* See if the next token is `volatile'.  */
12492   if (cp_parser_allow_gnu_extensions_p (parser)
12493       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12494     {
12495       /* Remember that we saw the `volatile' keyword.  */
12496       volatile_p = true;
12497       /* Consume the token.  */
12498       cp_lexer_consume_token (parser->lexer);
12499     }
12500   /* Look for the opening `('.  */
12501   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12502     return;
12503   /* Look for the string.  */
12504   string = cp_parser_string_literal (parser, false, false);
12505   if (string == error_mark_node)
12506     {
12507       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12508                                              /*consume_paren=*/true);
12509       return;
12510     }
12511
12512   /* If we're allowing GNU extensions, check for the extended assembly
12513      syntax.  Unfortunately, the `:' tokens need not be separated by
12514      a space in C, and so, for compatibility, we tolerate that here
12515      too.  Doing that means that we have to treat the `::' operator as
12516      two `:' tokens.  */
12517   if (cp_parser_allow_gnu_extensions_p (parser)
12518       && parser->in_function_body
12519       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12520           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12521     {
12522       bool inputs_p = false;
12523       bool clobbers_p = false;
12524
12525       /* The extended syntax was used.  */
12526       extended_p = true;
12527
12528       /* Look for outputs.  */
12529       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12530         {
12531           /* Consume the `:'.  */
12532           cp_lexer_consume_token (parser->lexer);
12533           /* Parse the output-operands.  */
12534           if (cp_lexer_next_token_is_not (parser->lexer,
12535                                           CPP_COLON)
12536               && cp_lexer_next_token_is_not (parser->lexer,
12537                                              CPP_SCOPE)
12538               && cp_lexer_next_token_is_not (parser->lexer,
12539                                              CPP_CLOSE_PAREN))
12540             outputs = cp_parser_asm_operand_list (parser);
12541
12542             if (outputs == error_mark_node)
12543               invalid_outputs_p = true;
12544         }
12545       /* If the next token is `::', there are no outputs, and the
12546          next token is the beginning of the inputs.  */
12547       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12548         /* The inputs are coming next.  */
12549         inputs_p = true;
12550
12551       /* Look for inputs.  */
12552       if (inputs_p
12553           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12554         {
12555           /* Consume the `:' or `::'.  */
12556           cp_lexer_consume_token (parser->lexer);
12557           /* Parse the output-operands.  */
12558           if (cp_lexer_next_token_is_not (parser->lexer,
12559                                           CPP_COLON)
12560               && cp_lexer_next_token_is_not (parser->lexer,
12561                                              CPP_CLOSE_PAREN))
12562             inputs = cp_parser_asm_operand_list (parser);
12563
12564             if (inputs == error_mark_node)
12565               invalid_inputs_p = true;
12566         }
12567       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12568         /* The clobbers are coming next.  */
12569         clobbers_p = true;
12570
12571       /* Look for clobbers.  */
12572       if (clobbers_p
12573           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12574         {
12575           /* Consume the `:' or `::'.  */
12576           cp_lexer_consume_token (parser->lexer);
12577           /* Parse the clobbers.  */
12578           if (cp_lexer_next_token_is_not (parser->lexer,
12579                                           CPP_CLOSE_PAREN))
12580             clobbers = cp_parser_asm_clobber_list (parser);
12581         }
12582     }
12583   /* Look for the closing `)'.  */
12584   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12585     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12586                                            /*consume_paren=*/true);
12587   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12588
12589   if (!invalid_inputs_p && !invalid_outputs_p)
12590     {
12591       /* Create the ASM_EXPR.  */
12592       if (parser->in_function_body)
12593         {
12594           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12595                                       inputs, clobbers);
12596           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12597           if (!extended_p)
12598             {
12599               tree temp = asm_stmt;
12600               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12601                 temp = TREE_OPERAND (temp, 0);
12602
12603               ASM_INPUT_P (temp) = 1;
12604             }
12605         }
12606       else
12607         cgraph_add_asm_node (string);
12608     }
12609 }
12610
12611 /* Declarators [gram.dcl.decl] */
12612
12613 /* Parse an init-declarator.
12614
12615    init-declarator:
12616      declarator initializer [opt]
12617
12618    GNU Extension:
12619
12620    init-declarator:
12621      declarator asm-specification [opt] attributes [opt] initializer [opt]
12622
12623    function-definition:
12624      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12625        function-body
12626      decl-specifier-seq [opt] declarator function-try-block
12627
12628    GNU Extension:
12629
12630    function-definition:
12631      __extension__ function-definition
12632
12633    The DECL_SPECIFIERS apply to this declarator.  Returns a
12634    representation of the entity declared.  If MEMBER_P is TRUE, then
12635    this declarator appears in a class scope.  The new DECL created by
12636    this declarator is returned.
12637
12638    The CHECKS are access checks that should be performed once we know
12639    what entity is being declared (and, therefore, what classes have
12640    befriended it).
12641
12642    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12643    for a function-definition here as well.  If the declarator is a
12644    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12645    be TRUE upon return.  By that point, the function-definition will
12646    have been completely parsed.
12647
12648    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12649    is FALSE.  */
12650
12651 static tree
12652 cp_parser_init_declarator (cp_parser* parser,
12653                            cp_decl_specifier_seq *decl_specifiers,
12654                            VEC (deferred_access_check,gc)* checks,
12655                            bool function_definition_allowed_p,
12656                            bool member_p,
12657                            int declares_class_or_enum,
12658                            bool* function_definition_p)
12659 {
12660   cp_token *token = NULL, *asm_spec_start_token = NULL,
12661            *attributes_start_token = NULL;
12662   cp_declarator *declarator;
12663   tree prefix_attributes;
12664   tree attributes;
12665   tree asm_specification;
12666   tree initializer;
12667   tree decl = NULL_TREE;
12668   tree scope;
12669   int is_initialized;
12670   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12671      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12672      "(...)".  */
12673   enum cpp_ttype initialization_kind;
12674   bool is_direct_init = false;
12675   bool is_non_constant_init;
12676   int ctor_dtor_or_conv_p;
12677   bool friend_p;
12678   tree pushed_scope = NULL;
12679
12680   /* Gather the attributes that were provided with the
12681      decl-specifiers.  */
12682   prefix_attributes = decl_specifiers->attributes;
12683
12684   /* Assume that this is not the declarator for a function
12685      definition.  */
12686   if (function_definition_p)
12687     *function_definition_p = false;
12688
12689   /* Defer access checks while parsing the declarator; we cannot know
12690      what names are accessible until we know what is being
12691      declared.  */
12692   resume_deferring_access_checks ();
12693
12694   /* Parse the declarator.  */
12695   token = cp_lexer_peek_token (parser->lexer);
12696   declarator
12697     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12698                             &ctor_dtor_or_conv_p,
12699                             /*parenthesized_p=*/NULL,
12700                             /*member_p=*/false);
12701   /* Gather up the deferred checks.  */
12702   stop_deferring_access_checks ();
12703
12704   /* If the DECLARATOR was erroneous, there's no need to go
12705      further.  */
12706   if (declarator == cp_error_declarator)
12707     return error_mark_node;
12708
12709   /* Check that the number of template-parameter-lists is OK.  */
12710   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12711                                                        token->location))
12712     return error_mark_node;
12713
12714   if (declares_class_or_enum & 2)
12715     cp_parser_check_for_definition_in_return_type (declarator,
12716                                                    decl_specifiers->type,
12717                                                    decl_specifiers->type_location);
12718
12719   /* Figure out what scope the entity declared by the DECLARATOR is
12720      located in.  `grokdeclarator' sometimes changes the scope, so
12721      we compute it now.  */
12722   scope = get_scope_of_declarator (declarator);
12723
12724   /* If we're allowing GNU extensions, look for an asm-specification
12725      and attributes.  */
12726   if (cp_parser_allow_gnu_extensions_p (parser))
12727     {
12728       /* Look for an asm-specification.  */
12729       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12730       asm_specification = cp_parser_asm_specification_opt (parser);
12731       /* And attributes.  */
12732       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12733       attributes = cp_parser_attributes_opt (parser);
12734     }
12735   else
12736     {
12737       asm_specification = NULL_TREE;
12738       attributes = NULL_TREE;
12739     }
12740
12741   /* Peek at the next token.  */
12742   token = cp_lexer_peek_token (parser->lexer);
12743   /* Check to see if the token indicates the start of a
12744      function-definition.  */
12745   if (function_declarator_p (declarator)
12746       && cp_parser_token_starts_function_definition_p (token))
12747     {
12748       if (!function_definition_allowed_p)
12749         {
12750           /* If a function-definition should not appear here, issue an
12751              error message.  */
12752           cp_parser_error (parser,
12753                            "a function-definition is not allowed here");
12754           return error_mark_node;
12755         }
12756       else
12757         {
12758           location_t func_brace_location
12759             = cp_lexer_peek_token (parser->lexer)->location;
12760
12761           /* Neither attributes nor an asm-specification are allowed
12762              on a function-definition.  */
12763           if (asm_specification)
12764             error ("%Han asm-specification is not allowed "
12765                    "on a function-definition",
12766                    &asm_spec_start_token->location);
12767           if (attributes)
12768             error ("%Hattributes are not allowed on a function-definition",
12769                    &attributes_start_token->location);
12770           /* This is a function-definition.  */
12771           *function_definition_p = true;
12772
12773           /* Parse the function definition.  */
12774           if (member_p)
12775             decl = cp_parser_save_member_function_body (parser,
12776                                                         decl_specifiers,
12777                                                         declarator,
12778                                                         prefix_attributes);
12779           else
12780             decl
12781               = (cp_parser_function_definition_from_specifiers_and_declarator
12782                  (parser, decl_specifiers, prefix_attributes, declarator));
12783
12784           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12785             {
12786               /* This is where the prologue starts...  */
12787               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12788                 = func_brace_location;
12789             }
12790
12791           return decl;
12792         }
12793     }
12794
12795   /* [dcl.dcl]
12796
12797      Only in function declarations for constructors, destructors, and
12798      type conversions can the decl-specifier-seq be omitted.
12799
12800      We explicitly postpone this check past the point where we handle
12801      function-definitions because we tolerate function-definitions
12802      that are missing their return types in some modes.  */
12803   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12804     {
12805       cp_parser_error (parser,
12806                        "expected constructor, destructor, or type conversion");
12807       return error_mark_node;
12808     }
12809
12810   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12811   if (token->type == CPP_EQ
12812       || token->type == CPP_OPEN_PAREN
12813       || token->type == CPP_OPEN_BRACE)
12814     {
12815       is_initialized = SD_INITIALIZED;
12816       initialization_kind = token->type;
12817
12818       if (token->type == CPP_EQ
12819           && function_declarator_p (declarator))
12820         {
12821           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12822           if (t2->keyword == RID_DEFAULT)
12823             is_initialized = SD_DEFAULTED;
12824           else if (t2->keyword == RID_DELETE)
12825             is_initialized = SD_DELETED;
12826         }
12827     }
12828   else
12829     {
12830       /* If the init-declarator isn't initialized and isn't followed by a
12831          `,' or `;', it's not a valid init-declarator.  */
12832       if (token->type != CPP_COMMA
12833           && token->type != CPP_SEMICOLON)
12834         {
12835           cp_parser_error (parser, "expected initializer");
12836           return error_mark_node;
12837         }
12838       is_initialized = SD_UNINITIALIZED;
12839       initialization_kind = CPP_EOF;
12840     }
12841
12842   /* Because start_decl has side-effects, we should only call it if we
12843      know we're going ahead.  By this point, we know that we cannot
12844      possibly be looking at any other construct.  */
12845   cp_parser_commit_to_tentative_parse (parser);
12846
12847   /* If the decl specifiers were bad, issue an error now that we're
12848      sure this was intended to be a declarator.  Then continue
12849      declaring the variable(s), as int, to try to cut down on further
12850      errors.  */
12851   if (decl_specifiers->any_specifiers_p
12852       && decl_specifiers->type == error_mark_node)
12853     {
12854       cp_parser_error (parser, "invalid type in declaration");
12855       decl_specifiers->type = integer_type_node;
12856     }
12857
12858   /* Check to see whether or not this declaration is a friend.  */
12859   friend_p = cp_parser_friend_p (decl_specifiers);
12860
12861   /* Enter the newly declared entry in the symbol table.  If we're
12862      processing a declaration in a class-specifier, we wait until
12863      after processing the initializer.  */
12864   if (!member_p)
12865     {
12866       if (parser->in_unbraced_linkage_specification_p)
12867         decl_specifiers->storage_class = sc_extern;
12868       decl = start_decl (declarator, decl_specifiers,
12869                          is_initialized, attributes, prefix_attributes,
12870                          &pushed_scope);
12871     }
12872   else if (scope)
12873     /* Enter the SCOPE.  That way unqualified names appearing in the
12874        initializer will be looked up in SCOPE.  */
12875     pushed_scope = push_scope (scope);
12876
12877   /* Perform deferred access control checks, now that we know in which
12878      SCOPE the declared entity resides.  */
12879   if (!member_p && decl)
12880     {
12881       tree saved_current_function_decl = NULL_TREE;
12882
12883       /* If the entity being declared is a function, pretend that we
12884          are in its scope.  If it is a `friend', it may have access to
12885          things that would not otherwise be accessible.  */
12886       if (TREE_CODE (decl) == FUNCTION_DECL)
12887         {
12888           saved_current_function_decl = current_function_decl;
12889           current_function_decl = decl;
12890         }
12891
12892       /* Perform access checks for template parameters.  */
12893       cp_parser_perform_template_parameter_access_checks (checks);
12894
12895       /* Perform the access control checks for the declarator and the
12896          decl-specifiers.  */
12897       perform_deferred_access_checks ();
12898
12899       /* Restore the saved value.  */
12900       if (TREE_CODE (decl) == FUNCTION_DECL)
12901         current_function_decl = saved_current_function_decl;
12902     }
12903
12904   /* Parse the initializer.  */
12905   initializer = NULL_TREE;
12906   is_direct_init = false;
12907   is_non_constant_init = true;
12908   if (is_initialized)
12909     {
12910       if (function_declarator_p (declarator))
12911         {
12912           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12913            if (initialization_kind == CPP_EQ)
12914              initializer = cp_parser_pure_specifier (parser);
12915            else
12916              {
12917                /* If the declaration was erroneous, we don't really
12918                   know what the user intended, so just silently
12919                   consume the initializer.  */
12920                if (decl != error_mark_node)
12921                  error ("%Hinitializer provided for function",
12922                         &initializer_start_token->location);
12923                cp_parser_skip_to_closing_parenthesis (parser,
12924                                                       /*recovering=*/true,
12925                                                       /*or_comma=*/false,
12926                                                       /*consume_paren=*/true);
12927              }
12928         }
12929       else
12930         initializer = cp_parser_initializer (parser,
12931                                              &is_direct_init,
12932                                              &is_non_constant_init);
12933     }
12934
12935   /* The old parser allows attributes to appear after a parenthesized
12936      initializer.  Mark Mitchell proposed removing this functionality
12937      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12938      attributes -- but ignores them.  */
12939   if (cp_parser_allow_gnu_extensions_p (parser)
12940       && initialization_kind == CPP_OPEN_PAREN)
12941     if (cp_parser_attributes_opt (parser))
12942       warning (OPT_Wattributes,
12943                "attributes after parenthesized initializer ignored");
12944
12945   /* For an in-class declaration, use `grokfield' to create the
12946      declaration.  */
12947   if (member_p)
12948     {
12949       if (pushed_scope)
12950         {
12951           pop_scope (pushed_scope);
12952           pushed_scope = false;
12953         }
12954       decl = grokfield (declarator, decl_specifiers,
12955                         initializer, !is_non_constant_init,
12956                         /*asmspec=*/NULL_TREE,
12957                         prefix_attributes);
12958       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12959         cp_parser_save_default_args (parser, decl);
12960     }
12961
12962   /* Finish processing the declaration.  But, skip friend
12963      declarations.  */
12964   if (!friend_p && decl && decl != error_mark_node)
12965     {
12966       cp_finish_decl (decl,
12967                       initializer, !is_non_constant_init,
12968                       asm_specification,
12969                       /* If the initializer is in parentheses, then this is
12970                          a direct-initialization, which means that an
12971                          `explicit' constructor is OK.  Otherwise, an
12972                          `explicit' constructor cannot be used.  */
12973                       ((is_direct_init || !is_initialized)
12974                        ? 0 : LOOKUP_ONLYCONVERTING));
12975     }
12976   else if ((cxx_dialect != cxx98) && friend_p
12977            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12978     /* Core issue #226 (C++0x only): A default template-argument
12979        shall not be specified in a friend class template
12980        declaration. */
12981     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12982                              /*is_partial=*/0, /*is_friend_decl=*/1);
12983
12984   if (!friend_p && pushed_scope)
12985     pop_scope (pushed_scope);
12986
12987   return decl;
12988 }
12989
12990 /* Parse a declarator.
12991
12992    declarator:
12993      direct-declarator
12994      ptr-operator declarator
12995
12996    abstract-declarator:
12997      ptr-operator abstract-declarator [opt]
12998      direct-abstract-declarator
12999
13000    GNU Extensions:
13001
13002    declarator:
13003      attributes [opt] direct-declarator
13004      attributes [opt] ptr-operator declarator
13005
13006    abstract-declarator:
13007      attributes [opt] ptr-operator abstract-declarator [opt]
13008      attributes [opt] direct-abstract-declarator
13009
13010    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13011    detect constructor, destructor or conversion operators. It is set
13012    to -1 if the declarator is a name, and +1 if it is a
13013    function. Otherwise it is set to zero. Usually you just want to
13014    test for >0, but internally the negative value is used.
13015
13016    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13017    a decl-specifier-seq unless it declares a constructor, destructor,
13018    or conversion.  It might seem that we could check this condition in
13019    semantic analysis, rather than parsing, but that makes it difficult
13020    to handle something like `f()'.  We want to notice that there are
13021    no decl-specifiers, and therefore realize that this is an
13022    expression, not a declaration.)
13023
13024    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13025    the declarator is a direct-declarator of the form "(...)".
13026
13027    MEMBER_P is true iff this declarator is a member-declarator.  */
13028
13029 static cp_declarator *
13030 cp_parser_declarator (cp_parser* parser,
13031                       cp_parser_declarator_kind dcl_kind,
13032                       int* ctor_dtor_or_conv_p,
13033                       bool* parenthesized_p,
13034                       bool member_p)
13035 {
13036   cp_token *token;
13037   cp_declarator *declarator;
13038   enum tree_code code;
13039   cp_cv_quals cv_quals;
13040   tree class_type;
13041   tree attributes = NULL_TREE;
13042
13043   /* Assume this is not a constructor, destructor, or type-conversion
13044      operator.  */
13045   if (ctor_dtor_or_conv_p)
13046     *ctor_dtor_or_conv_p = 0;
13047
13048   if (cp_parser_allow_gnu_extensions_p (parser))
13049     attributes = cp_parser_attributes_opt (parser);
13050
13051   /* Peek at the next token.  */
13052   token = cp_lexer_peek_token (parser->lexer);
13053
13054   /* Check for the ptr-operator production.  */
13055   cp_parser_parse_tentatively (parser);
13056   /* Parse the ptr-operator.  */
13057   code = cp_parser_ptr_operator (parser,
13058                                  &class_type,
13059                                  &cv_quals);
13060   /* If that worked, then we have a ptr-operator.  */
13061   if (cp_parser_parse_definitely (parser))
13062     {
13063       /* If a ptr-operator was found, then this declarator was not
13064          parenthesized.  */
13065       if (parenthesized_p)
13066         *parenthesized_p = true;
13067       /* The dependent declarator is optional if we are parsing an
13068          abstract-declarator.  */
13069       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13070         cp_parser_parse_tentatively (parser);
13071
13072       /* Parse the dependent declarator.  */
13073       declarator = cp_parser_declarator (parser, dcl_kind,
13074                                          /*ctor_dtor_or_conv_p=*/NULL,
13075                                          /*parenthesized_p=*/NULL,
13076                                          /*member_p=*/false);
13077
13078       /* If we are parsing an abstract-declarator, we must handle the
13079          case where the dependent declarator is absent.  */
13080       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13081           && !cp_parser_parse_definitely (parser))
13082         declarator = NULL;
13083
13084       declarator = cp_parser_make_indirect_declarator
13085         (code, class_type, cv_quals, declarator);
13086     }
13087   /* Everything else is a direct-declarator.  */
13088   else
13089     {
13090       if (parenthesized_p)
13091         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13092                                                    CPP_OPEN_PAREN);
13093       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13094                                                 ctor_dtor_or_conv_p,
13095                                                 member_p);
13096     }
13097
13098   if (attributes && declarator && declarator != cp_error_declarator)
13099     declarator->attributes = attributes;
13100
13101   return declarator;
13102 }
13103
13104 /* Parse a direct-declarator or direct-abstract-declarator.
13105
13106    direct-declarator:
13107      declarator-id
13108      direct-declarator ( parameter-declaration-clause )
13109        cv-qualifier-seq [opt]
13110        exception-specification [opt]
13111      direct-declarator [ constant-expression [opt] ]
13112      ( declarator )
13113
13114    direct-abstract-declarator:
13115      direct-abstract-declarator [opt]
13116        ( parameter-declaration-clause )
13117        cv-qualifier-seq [opt]
13118        exception-specification [opt]
13119      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13120      ( abstract-declarator )
13121
13122    Returns a representation of the declarator.  DCL_KIND is
13123    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13124    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13125    we are parsing a direct-declarator.  It is
13126    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13127    of ambiguity we prefer an abstract declarator, as per
13128    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13129    cp_parser_declarator.  */
13130
13131 static cp_declarator *
13132 cp_parser_direct_declarator (cp_parser* parser,
13133                              cp_parser_declarator_kind dcl_kind,
13134                              int* ctor_dtor_or_conv_p,
13135                              bool member_p)
13136 {
13137   cp_token *token;
13138   cp_declarator *declarator = NULL;
13139   tree scope = NULL_TREE;
13140   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13141   bool saved_in_declarator_p = parser->in_declarator_p;
13142   bool first = true;
13143   tree pushed_scope = NULL_TREE;
13144
13145   while (true)
13146     {
13147       /* Peek at the next token.  */
13148       token = cp_lexer_peek_token (parser->lexer);
13149       if (token->type == CPP_OPEN_PAREN)
13150         {
13151           /* This is either a parameter-declaration-clause, or a
13152              parenthesized declarator. When we know we are parsing a
13153              named declarator, it must be a parenthesized declarator
13154              if FIRST is true. For instance, `(int)' is a
13155              parameter-declaration-clause, with an omitted
13156              direct-abstract-declarator. But `((*))', is a
13157              parenthesized abstract declarator. Finally, when T is a
13158              template parameter `(T)' is a
13159              parameter-declaration-clause, and not a parenthesized
13160              named declarator.
13161
13162              We first try and parse a parameter-declaration-clause,
13163              and then try a nested declarator (if FIRST is true).
13164
13165              It is not an error for it not to be a
13166              parameter-declaration-clause, even when FIRST is
13167              false. Consider,
13168
13169                int i (int);
13170                int i (3);
13171
13172              The first is the declaration of a function while the
13173              second is the definition of a variable, including its
13174              initializer.
13175
13176              Having seen only the parenthesis, we cannot know which of
13177              these two alternatives should be selected.  Even more
13178              complex are examples like:
13179
13180                int i (int (a));
13181                int i (int (3));
13182
13183              The former is a function-declaration; the latter is a
13184              variable initialization.
13185
13186              Thus again, we try a parameter-declaration-clause, and if
13187              that fails, we back out and return.  */
13188
13189           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13190             {
13191               tree params;
13192               unsigned saved_num_template_parameter_lists;
13193               bool is_declarator = false;
13194               tree t;
13195
13196               /* In a member-declarator, the only valid interpretation
13197                  of a parenthesis is the start of a
13198                  parameter-declaration-clause.  (It is invalid to
13199                  initialize a static data member with a parenthesized
13200                  initializer; only the "=" form of initialization is
13201                  permitted.)  */
13202               if (!member_p)
13203                 cp_parser_parse_tentatively (parser);
13204
13205               /* Consume the `('.  */
13206               cp_lexer_consume_token (parser->lexer);
13207               if (first)
13208                 {
13209                   /* If this is going to be an abstract declarator, we're
13210                      in a declarator and we can't have default args.  */
13211                   parser->default_arg_ok_p = false;
13212                   parser->in_declarator_p = true;
13213                 }
13214
13215               /* Inside the function parameter list, surrounding
13216                  template-parameter-lists do not apply.  */
13217               saved_num_template_parameter_lists
13218                 = parser->num_template_parameter_lists;
13219               parser->num_template_parameter_lists = 0;
13220
13221               begin_scope (sk_function_parms, NULL_TREE);
13222
13223               /* Parse the parameter-declaration-clause.  */
13224               params = cp_parser_parameter_declaration_clause (parser);
13225
13226               parser->num_template_parameter_lists
13227                 = saved_num_template_parameter_lists;
13228
13229               /* If all went well, parse the cv-qualifier-seq and the
13230                  exception-specification.  */
13231               if (member_p || cp_parser_parse_definitely (parser))
13232                 {
13233                   cp_cv_quals cv_quals;
13234                   tree exception_specification;
13235                   tree late_return;
13236
13237                   is_declarator = true;
13238
13239                   if (ctor_dtor_or_conv_p)
13240                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13241                   first = false;
13242                   /* Consume the `)'.  */
13243                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13244
13245                   /* Parse the cv-qualifier-seq.  */
13246                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13247                   /* And the exception-specification.  */
13248                   exception_specification
13249                     = cp_parser_exception_specification_opt (parser);
13250
13251                   late_return
13252                     = cp_parser_late_return_type_opt (parser);
13253
13254                   /* Create the function-declarator.  */
13255                   declarator = make_call_declarator (declarator,
13256                                                      params,
13257                                                      cv_quals,
13258                                                      exception_specification,
13259                                                      late_return);
13260                   /* Any subsequent parameter lists are to do with
13261                      return type, so are not those of the declared
13262                      function.  */
13263                   parser->default_arg_ok_p = false;
13264                 }
13265
13266               /* Remove the function parms from scope.  */
13267               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13268                 pop_binding (DECL_NAME (t), t);
13269               leave_scope();
13270
13271               if (is_declarator)
13272                 /* Repeat the main loop.  */
13273                 continue;
13274             }
13275
13276           /* If this is the first, we can try a parenthesized
13277              declarator.  */
13278           if (first)
13279             {
13280               bool saved_in_type_id_in_expr_p;
13281
13282               parser->default_arg_ok_p = saved_default_arg_ok_p;
13283               parser->in_declarator_p = saved_in_declarator_p;
13284
13285               /* Consume the `('.  */
13286               cp_lexer_consume_token (parser->lexer);
13287               /* Parse the nested declarator.  */
13288               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13289               parser->in_type_id_in_expr_p = true;
13290               declarator
13291                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13292                                         /*parenthesized_p=*/NULL,
13293                                         member_p);
13294               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13295               first = false;
13296               /* Expect a `)'.  */
13297               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13298                 declarator = cp_error_declarator;
13299               if (declarator == cp_error_declarator)
13300                 break;
13301
13302               goto handle_declarator;
13303             }
13304           /* Otherwise, we must be done.  */
13305           else
13306             break;
13307         }
13308       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13309                && token->type == CPP_OPEN_SQUARE)
13310         {
13311           /* Parse an array-declarator.  */
13312           tree bounds;
13313
13314           if (ctor_dtor_or_conv_p)
13315             *ctor_dtor_or_conv_p = 0;
13316
13317           first = false;
13318           parser->default_arg_ok_p = false;
13319           parser->in_declarator_p = true;
13320           /* Consume the `['.  */
13321           cp_lexer_consume_token (parser->lexer);
13322           /* Peek at the next token.  */
13323           token = cp_lexer_peek_token (parser->lexer);
13324           /* If the next token is `]', then there is no
13325              constant-expression.  */
13326           if (token->type != CPP_CLOSE_SQUARE)
13327             {
13328               bool non_constant_p;
13329
13330               bounds
13331                 = cp_parser_constant_expression (parser,
13332                                                  /*allow_non_constant=*/true,
13333                                                  &non_constant_p);
13334               if (!non_constant_p)
13335                 bounds = fold_non_dependent_expr (bounds);
13336               /* Normally, the array bound must be an integral constant
13337                  expression.  However, as an extension, we allow VLAs
13338                  in function scopes as long as they aren't part of a
13339                  parameter declaration.  */
13340               else if (!parser->in_function_body
13341                        || current_binding_level->kind == sk_function_parms)
13342                 {
13343                   cp_parser_error (parser,
13344                                    "array bound is not an integer constant");
13345                   bounds = error_mark_node;
13346                 }
13347               else if (processing_template_decl && !error_operand_p (bounds))
13348                 {
13349                   /* Remember this wasn't a constant-expression.  */
13350                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13351                   TREE_SIDE_EFFECTS (bounds) = 1;
13352                 }
13353             }
13354           else
13355             bounds = NULL_TREE;
13356           /* Look for the closing `]'.  */
13357           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13358             {
13359               declarator = cp_error_declarator;
13360               break;
13361             }
13362
13363           declarator = make_array_declarator (declarator, bounds);
13364         }
13365       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13366         {
13367           tree qualifying_scope;
13368           tree unqualified_name;
13369           special_function_kind sfk;
13370           bool abstract_ok;
13371           bool pack_expansion_p = false;
13372           cp_token *declarator_id_start_token;
13373
13374           /* Parse a declarator-id */
13375           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13376           if (abstract_ok)
13377             {
13378               cp_parser_parse_tentatively (parser);
13379
13380               /* If we see an ellipsis, we should be looking at a
13381                  parameter pack. */
13382               if (token->type == CPP_ELLIPSIS)
13383                 {
13384                   /* Consume the `...' */
13385                   cp_lexer_consume_token (parser->lexer);
13386
13387                   pack_expansion_p = true;
13388                 }
13389             }
13390
13391           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13392           unqualified_name
13393             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13394           qualifying_scope = parser->scope;
13395           if (abstract_ok)
13396             {
13397               bool okay = false;
13398
13399               if (!unqualified_name && pack_expansion_p)
13400                 {
13401                   /* Check whether an error occurred. */
13402                   okay = !cp_parser_error_occurred (parser);
13403
13404                   /* We already consumed the ellipsis to mark a
13405                      parameter pack, but we have no way to report it,
13406                      so abort the tentative parse. We will be exiting
13407                      immediately anyway. */
13408                   cp_parser_abort_tentative_parse (parser);
13409                 }
13410               else
13411                 okay = cp_parser_parse_definitely (parser);
13412
13413               if (!okay)
13414                 unqualified_name = error_mark_node;
13415               else if (unqualified_name
13416                        && (qualifying_scope
13417                            || (TREE_CODE (unqualified_name)
13418                                != IDENTIFIER_NODE)))
13419                 {
13420                   cp_parser_error (parser, "expected unqualified-id");
13421                   unqualified_name = error_mark_node;
13422                 }
13423             }
13424
13425           if (!unqualified_name)
13426             return NULL;
13427           if (unqualified_name == error_mark_node)
13428             {
13429               declarator = cp_error_declarator;
13430               pack_expansion_p = false;
13431               declarator->parameter_pack_p = false;
13432               break;
13433             }
13434
13435           if (qualifying_scope && at_namespace_scope_p ()
13436               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13437             {
13438               /* In the declaration of a member of a template class
13439                  outside of the class itself, the SCOPE will sometimes
13440                  be a TYPENAME_TYPE.  For example, given:
13441
13442                  template <typename T>
13443                  int S<T>::R::i = 3;
13444
13445                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13446                  this context, we must resolve S<T>::R to an ordinary
13447                  type, rather than a typename type.
13448
13449                  The reason we normally avoid resolving TYPENAME_TYPEs
13450                  is that a specialization of `S' might render
13451                  `S<T>::R' not a type.  However, if `S' is
13452                  specialized, then this `i' will not be used, so there
13453                  is no harm in resolving the types here.  */
13454               tree type;
13455
13456               /* Resolve the TYPENAME_TYPE.  */
13457               type = resolve_typename_type (qualifying_scope,
13458                                             /*only_current_p=*/false);
13459               /* If that failed, the declarator is invalid.  */
13460               if (TREE_CODE (type) == TYPENAME_TYPE)
13461                 error ("%H%<%T::%E%> is not a type",
13462                        &declarator_id_start_token->location,
13463                        TYPE_CONTEXT (qualifying_scope),
13464                        TYPE_IDENTIFIER (qualifying_scope));
13465               qualifying_scope = type;
13466             }
13467
13468           sfk = sfk_none;
13469
13470           if (unqualified_name)
13471             {
13472               tree class_type;
13473
13474               if (qualifying_scope
13475                   && CLASS_TYPE_P (qualifying_scope))
13476                 class_type = qualifying_scope;
13477               else
13478                 class_type = current_class_type;
13479
13480               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13481                 {
13482                   tree name_type = TREE_TYPE (unqualified_name);
13483                   if (class_type && same_type_p (name_type, class_type))
13484                     {
13485                       if (qualifying_scope
13486                           && CLASSTYPE_USE_TEMPLATE (name_type))
13487                         {
13488                           error ("%Hinvalid use of constructor as a template",
13489                                  &declarator_id_start_token->location);
13490                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13491                                   "name the constructor in a qualified name",
13492                                   class_type,
13493                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13494                                   class_type, name_type);
13495                           declarator = cp_error_declarator;
13496                           break;
13497                         }
13498                       else
13499                         unqualified_name = constructor_name (class_type);
13500                     }
13501                   else
13502                     {
13503                       /* We do not attempt to print the declarator
13504                          here because we do not have enough
13505                          information about its original syntactic
13506                          form.  */
13507                       cp_parser_error (parser, "invalid declarator");
13508                       declarator = cp_error_declarator;
13509                       break;
13510                     }
13511                 }
13512
13513               if (class_type)
13514                 {
13515                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13516                     sfk = sfk_destructor;
13517                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13518                     sfk = sfk_conversion;
13519                   else if (/* There's no way to declare a constructor
13520                               for an anonymous type, even if the type
13521                               got a name for linkage purposes.  */
13522                            !TYPE_WAS_ANONYMOUS (class_type)
13523                            && constructor_name_p (unqualified_name,
13524                                                   class_type))
13525                     {
13526                       unqualified_name = constructor_name (class_type);
13527                       sfk = sfk_constructor;
13528                     }
13529
13530                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13531                     *ctor_dtor_or_conv_p = -1;
13532                 }
13533             }
13534           declarator = make_id_declarator (qualifying_scope,
13535                                            unqualified_name,
13536                                            sfk);
13537           declarator->id_loc = token->location;
13538           declarator->parameter_pack_p = pack_expansion_p;
13539
13540           if (pack_expansion_p)
13541             maybe_warn_variadic_templates ();
13542
13543         handle_declarator:;
13544           scope = get_scope_of_declarator (declarator);
13545           if (scope)
13546             /* Any names that appear after the declarator-id for a
13547                member are looked up in the containing scope.  */
13548             pushed_scope = push_scope (scope);
13549           parser->in_declarator_p = true;
13550           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13551               || (declarator && declarator->kind == cdk_id))
13552             /* Default args are only allowed on function
13553                declarations.  */
13554             parser->default_arg_ok_p = saved_default_arg_ok_p;
13555           else
13556             parser->default_arg_ok_p = false;
13557
13558           first = false;
13559         }
13560       /* We're done.  */
13561       else
13562         break;
13563     }
13564
13565   /* For an abstract declarator, we might wind up with nothing at this
13566      point.  That's an error; the declarator is not optional.  */
13567   if (!declarator)
13568     cp_parser_error (parser, "expected declarator");
13569
13570   /* If we entered a scope, we must exit it now.  */
13571   if (pushed_scope)
13572     pop_scope (pushed_scope);
13573
13574   parser->default_arg_ok_p = saved_default_arg_ok_p;
13575   parser->in_declarator_p = saved_in_declarator_p;
13576
13577   return declarator;
13578 }
13579
13580 /* Parse a ptr-operator.
13581
13582    ptr-operator:
13583      * cv-qualifier-seq [opt]
13584      &
13585      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13586
13587    GNU Extension:
13588
13589    ptr-operator:
13590      & cv-qualifier-seq [opt]
13591
13592    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13593    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13594    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13595    filled in with the TYPE containing the member.  *CV_QUALS is
13596    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13597    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13598    Note that the tree codes returned by this function have nothing
13599    to do with the types of trees that will be eventually be created
13600    to represent the pointer or reference type being parsed. They are
13601    just constants with suggestive names. */
13602 static enum tree_code
13603 cp_parser_ptr_operator (cp_parser* parser,
13604                         tree* type,
13605                         cp_cv_quals *cv_quals)
13606 {
13607   enum tree_code code = ERROR_MARK;
13608   cp_token *token;
13609
13610   /* Assume that it's not a pointer-to-member.  */
13611   *type = NULL_TREE;
13612   /* And that there are no cv-qualifiers.  */
13613   *cv_quals = TYPE_UNQUALIFIED;
13614
13615   /* Peek at the next token.  */
13616   token = cp_lexer_peek_token (parser->lexer);
13617
13618   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13619   if (token->type == CPP_MULT)
13620     code = INDIRECT_REF;
13621   else if (token->type == CPP_AND)
13622     code = ADDR_EXPR;
13623   else if ((cxx_dialect != cxx98) &&
13624            token->type == CPP_AND_AND) /* C++0x only */
13625     code = NON_LVALUE_EXPR;
13626
13627   if (code != ERROR_MARK)
13628     {
13629       /* Consume the `*', `&' or `&&'.  */
13630       cp_lexer_consume_token (parser->lexer);
13631
13632       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13633          `&', if we are allowing GNU extensions.  (The only qualifier
13634          that can legally appear after `&' is `restrict', but that is
13635          enforced during semantic analysis.  */
13636       if (code == INDIRECT_REF
13637           || cp_parser_allow_gnu_extensions_p (parser))
13638         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13639     }
13640   else
13641     {
13642       /* Try the pointer-to-member case.  */
13643       cp_parser_parse_tentatively (parser);
13644       /* Look for the optional `::' operator.  */
13645       cp_parser_global_scope_opt (parser,
13646                                   /*current_scope_valid_p=*/false);
13647       /* Look for the nested-name specifier.  */
13648       token = cp_lexer_peek_token (parser->lexer);
13649       cp_parser_nested_name_specifier (parser,
13650                                        /*typename_keyword_p=*/false,
13651                                        /*check_dependency_p=*/true,
13652                                        /*type_p=*/false,
13653                                        /*is_declaration=*/false);
13654       /* If we found it, and the next token is a `*', then we are
13655          indeed looking at a pointer-to-member operator.  */
13656       if (!cp_parser_error_occurred (parser)
13657           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13658         {
13659           /* Indicate that the `*' operator was used.  */
13660           code = INDIRECT_REF;
13661
13662           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13663             error ("%H%qD is a namespace", &token->location, parser->scope);
13664           else
13665             {
13666               /* The type of which the member is a member is given by the
13667                  current SCOPE.  */
13668               *type = parser->scope;
13669               /* The next name will not be qualified.  */
13670               parser->scope = NULL_TREE;
13671               parser->qualifying_scope = NULL_TREE;
13672               parser->object_scope = NULL_TREE;
13673               /* Look for the optional cv-qualifier-seq.  */
13674               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13675             }
13676         }
13677       /* If that didn't work we don't have a ptr-operator.  */
13678       if (!cp_parser_parse_definitely (parser))
13679         cp_parser_error (parser, "expected ptr-operator");
13680     }
13681
13682   return code;
13683 }
13684
13685 /* Parse an (optional) cv-qualifier-seq.
13686
13687    cv-qualifier-seq:
13688      cv-qualifier cv-qualifier-seq [opt]
13689
13690    cv-qualifier:
13691      const
13692      volatile
13693
13694    GNU Extension:
13695
13696    cv-qualifier:
13697      __restrict__
13698
13699    Returns a bitmask representing the cv-qualifiers.  */
13700
13701 static cp_cv_quals
13702 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13703 {
13704   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13705
13706   while (true)
13707     {
13708       cp_token *token;
13709       cp_cv_quals cv_qualifier;
13710
13711       /* Peek at the next token.  */
13712       token = cp_lexer_peek_token (parser->lexer);
13713       /* See if it's a cv-qualifier.  */
13714       switch (token->keyword)
13715         {
13716         case RID_CONST:
13717           cv_qualifier = TYPE_QUAL_CONST;
13718           break;
13719
13720         case RID_VOLATILE:
13721           cv_qualifier = TYPE_QUAL_VOLATILE;
13722           break;
13723
13724         case RID_RESTRICT:
13725           cv_qualifier = TYPE_QUAL_RESTRICT;
13726           break;
13727
13728         default:
13729           cv_qualifier = TYPE_UNQUALIFIED;
13730           break;
13731         }
13732
13733       if (!cv_qualifier)
13734         break;
13735
13736       if (cv_quals & cv_qualifier)
13737         {
13738           error ("%Hduplicate cv-qualifier", &token->location);
13739           cp_lexer_purge_token (parser->lexer);
13740         }
13741       else
13742         {
13743           cp_lexer_consume_token (parser->lexer);
13744           cv_quals |= cv_qualifier;
13745         }
13746     }
13747
13748   return cv_quals;
13749 }
13750
13751 /* Parse a late-specified return type, if any.  This is not a separate
13752    non-terminal, but part of a function declarator, which looks like
13753
13754    -> trailing-type-specifier-seq abstract-declarator(opt)
13755
13756    Returns the type indicated by the type-id.  */
13757
13758 static tree
13759 cp_parser_late_return_type_opt (cp_parser* parser)
13760 {
13761   cp_token *token;
13762
13763   /* Peek at the next token.  */
13764   token = cp_lexer_peek_token (parser->lexer);
13765   /* A late-specified return type is indicated by an initial '->'. */
13766   if (token->type != CPP_DEREF)
13767     return NULL_TREE;
13768
13769   /* Consume the ->.  */
13770   cp_lexer_consume_token (parser->lexer);
13771
13772   return cp_parser_trailing_type_id (parser);
13773 }
13774
13775 /* Parse a declarator-id.
13776
13777    declarator-id:
13778      id-expression
13779      :: [opt] nested-name-specifier [opt] type-name
13780
13781    In the `id-expression' case, the value returned is as for
13782    cp_parser_id_expression if the id-expression was an unqualified-id.
13783    If the id-expression was a qualified-id, then a SCOPE_REF is
13784    returned.  The first operand is the scope (either a NAMESPACE_DECL
13785    or TREE_TYPE), but the second is still just a representation of an
13786    unqualified-id.  */
13787
13788 static tree
13789 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13790 {
13791   tree id;
13792   /* The expression must be an id-expression.  Assume that qualified
13793      names are the names of types so that:
13794
13795        template <class T>
13796        int S<T>::R::i = 3;
13797
13798      will work; we must treat `S<T>::R' as the name of a type.
13799      Similarly, assume that qualified names are templates, where
13800      required, so that:
13801
13802        template <class T>
13803        int S<T>::R<T>::i = 3;
13804
13805      will work, too.  */
13806   id = cp_parser_id_expression (parser,
13807                                 /*template_keyword_p=*/false,
13808                                 /*check_dependency_p=*/false,
13809                                 /*template_p=*/NULL,
13810                                 /*declarator_p=*/true,
13811                                 optional_p);
13812   if (id && BASELINK_P (id))
13813     id = BASELINK_FUNCTIONS (id);
13814   return id;
13815 }
13816
13817 /* Parse a type-id.
13818
13819    type-id:
13820      type-specifier-seq abstract-declarator [opt]
13821
13822    Returns the TYPE specified.  */
13823
13824 static tree
13825 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
13826                      bool is_trailing_return)
13827 {
13828   cp_decl_specifier_seq type_specifier_seq;
13829   cp_declarator *abstract_declarator;
13830
13831   /* Parse the type-specifier-seq.  */
13832   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13833                                 is_trailing_return,
13834                                 &type_specifier_seq);
13835   if (type_specifier_seq.type == error_mark_node)
13836     return error_mark_node;
13837
13838   /* There might or might not be an abstract declarator.  */
13839   cp_parser_parse_tentatively (parser);
13840   /* Look for the declarator.  */
13841   abstract_declarator
13842     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13843                             /*parenthesized_p=*/NULL,
13844                             /*member_p=*/false);
13845   /* Check to see if there really was a declarator.  */
13846   if (!cp_parser_parse_definitely (parser))
13847     abstract_declarator = NULL;
13848
13849   if (type_specifier_seq.type
13850       && type_uses_auto (type_specifier_seq.type))
13851     {
13852       /* A type-id with type 'auto' is only ok if the abstract declarator
13853          is a function declarator with a late-specified return type.  */
13854       if (abstract_declarator
13855           && abstract_declarator->kind == cdk_function
13856           && abstract_declarator->u.function.late_return_type)
13857         /* OK */;
13858       else
13859         {
13860           error ("invalid use of %<auto%>");
13861           return error_mark_node;
13862         }
13863     }
13864   
13865   return groktypename (&type_specifier_seq, abstract_declarator,
13866                        is_template_arg);
13867 }
13868
13869 static tree cp_parser_type_id (cp_parser *parser)
13870 {
13871   return cp_parser_type_id_1 (parser, false, false);
13872 }
13873
13874 static tree cp_parser_template_type_arg (cp_parser *parser)
13875 {
13876   return cp_parser_type_id_1 (parser, true, false);
13877 }
13878
13879 static tree cp_parser_trailing_type_id (cp_parser *parser)
13880 {
13881   return cp_parser_type_id_1 (parser, false, true);
13882 }
13883
13884 /* Parse a type-specifier-seq.
13885
13886    type-specifier-seq:
13887      type-specifier type-specifier-seq [opt]
13888
13889    GNU extension:
13890
13891    type-specifier-seq:
13892      attributes type-specifier-seq [opt]
13893
13894    If IS_CONDITION is true, we are at the start of a "condition",
13895    e.g., we've just seen "if (".
13896
13897    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
13898    i.e. we've just seen "->".
13899
13900    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13901
13902 static void
13903 cp_parser_type_specifier_seq (cp_parser* parser,
13904                               bool is_condition,
13905                               bool is_trailing_return,
13906                               cp_decl_specifier_seq *type_specifier_seq)
13907 {
13908   bool seen_type_specifier = false;
13909   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13910   cp_token *start_token = NULL;
13911
13912   /* Clear the TYPE_SPECIFIER_SEQ.  */
13913   clear_decl_specs (type_specifier_seq);
13914
13915   /* In the context of a trailing return type, enum E { } is an
13916      elaborated-type-specifier followed by a function-body, not an
13917      enum-specifier.  */
13918   if (is_trailing_return)
13919     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
13920
13921   /* Parse the type-specifiers and attributes.  */
13922   while (true)
13923     {
13924       tree type_specifier;
13925       bool is_cv_qualifier;
13926
13927       /* Check for attributes first.  */
13928       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13929         {
13930           type_specifier_seq->attributes =
13931             chainon (type_specifier_seq->attributes,
13932                      cp_parser_attributes_opt (parser));
13933           continue;
13934         }
13935
13936       /* record the token of the beginning of the type specifier seq,
13937          for error reporting purposes*/
13938      if (!start_token)
13939        start_token = cp_lexer_peek_token (parser->lexer);
13940
13941       /* Look for the type-specifier.  */
13942       type_specifier = cp_parser_type_specifier (parser,
13943                                                  flags,
13944                                                  type_specifier_seq,
13945                                                  /*is_declaration=*/false,
13946                                                  NULL,
13947                                                  &is_cv_qualifier);
13948       if (!type_specifier)
13949         {
13950           /* If the first type-specifier could not be found, this is not a
13951              type-specifier-seq at all.  */
13952           if (!seen_type_specifier)
13953             {
13954               cp_parser_error (parser, "expected type-specifier");
13955               type_specifier_seq->type = error_mark_node;
13956               return;
13957             }
13958           /* If subsequent type-specifiers could not be found, the
13959              type-specifier-seq is complete.  */
13960           break;
13961         }
13962
13963       seen_type_specifier = true;
13964       /* The standard says that a condition can be:
13965
13966             type-specifier-seq declarator = assignment-expression
13967
13968          However, given:
13969
13970            struct S {};
13971            if (int S = ...)
13972
13973          we should treat the "S" as a declarator, not as a
13974          type-specifier.  The standard doesn't say that explicitly for
13975          type-specifier-seq, but it does say that for
13976          decl-specifier-seq in an ordinary declaration.  Perhaps it
13977          would be clearer just to allow a decl-specifier-seq here, and
13978          then add a semantic restriction that if any decl-specifiers
13979          that are not type-specifiers appear, the program is invalid.  */
13980       if (is_condition && !is_cv_qualifier)
13981         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13982     }
13983
13984   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13985 }
13986
13987 /* Parse a parameter-declaration-clause.
13988
13989    parameter-declaration-clause:
13990      parameter-declaration-list [opt] ... [opt]
13991      parameter-declaration-list , ...
13992
13993    Returns a representation for the parameter declarations.  A return
13994    value of NULL indicates a parameter-declaration-clause consisting
13995    only of an ellipsis.  */
13996
13997 static tree
13998 cp_parser_parameter_declaration_clause (cp_parser* parser)
13999 {
14000   tree parameters;
14001   cp_token *token;
14002   bool ellipsis_p;
14003   bool is_error;
14004
14005   /* Peek at the next token.  */
14006   token = cp_lexer_peek_token (parser->lexer);
14007   /* Check for trivial parameter-declaration-clauses.  */
14008   if (token->type == CPP_ELLIPSIS)
14009     {
14010       /* Consume the `...' token.  */
14011       cp_lexer_consume_token (parser->lexer);
14012       return NULL_TREE;
14013     }
14014   else if (token->type == CPP_CLOSE_PAREN)
14015     /* There are no parameters.  */
14016     {
14017 #ifndef NO_IMPLICIT_EXTERN_C
14018       if (in_system_header && current_class_type == NULL
14019           && current_lang_name == lang_name_c)
14020         return NULL_TREE;
14021       else
14022 #endif
14023         return void_list_node;
14024     }
14025   /* Check for `(void)', too, which is a special case.  */
14026   else if (token->keyword == RID_VOID
14027            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14028                == CPP_CLOSE_PAREN))
14029     {
14030       /* Consume the `void' token.  */
14031       cp_lexer_consume_token (parser->lexer);
14032       /* There are no parameters.  */
14033       return void_list_node;
14034     }
14035
14036   /* Parse the parameter-declaration-list.  */
14037   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14038   /* If a parse error occurred while parsing the
14039      parameter-declaration-list, then the entire
14040      parameter-declaration-clause is erroneous.  */
14041   if (is_error)
14042     return NULL;
14043
14044   /* Peek at the next token.  */
14045   token = cp_lexer_peek_token (parser->lexer);
14046   /* If it's a `,', the clause should terminate with an ellipsis.  */
14047   if (token->type == CPP_COMMA)
14048     {
14049       /* Consume the `,'.  */
14050       cp_lexer_consume_token (parser->lexer);
14051       /* Expect an ellipsis.  */
14052       ellipsis_p
14053         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14054     }
14055   /* It might also be `...' if the optional trailing `,' was
14056      omitted.  */
14057   else if (token->type == CPP_ELLIPSIS)
14058     {
14059       /* Consume the `...' token.  */
14060       cp_lexer_consume_token (parser->lexer);
14061       /* And remember that we saw it.  */
14062       ellipsis_p = true;
14063     }
14064   else
14065     ellipsis_p = false;
14066
14067   /* Finish the parameter list.  */
14068   if (!ellipsis_p)
14069     parameters = chainon (parameters, void_list_node);
14070
14071   return parameters;
14072 }
14073
14074 /* Parse a parameter-declaration-list.
14075
14076    parameter-declaration-list:
14077      parameter-declaration
14078      parameter-declaration-list , parameter-declaration
14079
14080    Returns a representation of the parameter-declaration-list, as for
14081    cp_parser_parameter_declaration_clause.  However, the
14082    `void_list_node' is never appended to the list.  Upon return,
14083    *IS_ERROR will be true iff an error occurred.  */
14084
14085 static tree
14086 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14087 {
14088   tree parameters = NULL_TREE;
14089   tree *tail = &parameters; 
14090   bool saved_in_unbraced_linkage_specification_p;
14091
14092   /* Assume all will go well.  */
14093   *is_error = false;
14094   /* The special considerations that apply to a function within an
14095      unbraced linkage specifications do not apply to the parameters
14096      to the function.  */
14097   saved_in_unbraced_linkage_specification_p 
14098     = parser->in_unbraced_linkage_specification_p;
14099   parser->in_unbraced_linkage_specification_p = false;
14100
14101   /* Look for more parameters.  */
14102   while (true)
14103     {
14104       cp_parameter_declarator *parameter;
14105       tree decl = error_mark_node;
14106       bool parenthesized_p;
14107       /* Parse the parameter.  */
14108       parameter
14109         = cp_parser_parameter_declaration (parser,
14110                                            /*template_parm_p=*/false,
14111                                            &parenthesized_p);
14112
14113       /* We don't know yet if the enclosing context is deprecated, so wait
14114          and warn in grokparms if appropriate.  */
14115       deprecated_state = DEPRECATED_SUPPRESS;
14116
14117       if (parameter)
14118         decl = grokdeclarator (parameter->declarator,
14119                                &parameter->decl_specifiers,
14120                                PARM,
14121                                parameter->default_argument != NULL_TREE,
14122                                &parameter->decl_specifiers.attributes);
14123
14124       deprecated_state = DEPRECATED_NORMAL;
14125
14126       /* If a parse error occurred parsing the parameter declaration,
14127          then the entire parameter-declaration-list is erroneous.  */
14128       if (decl == error_mark_node)
14129         {
14130           *is_error = true;
14131           parameters = error_mark_node;
14132           break;
14133         }
14134
14135       if (parameter->decl_specifiers.attributes)
14136         cplus_decl_attributes (&decl,
14137                                parameter->decl_specifiers.attributes,
14138                                0);
14139       if (DECL_NAME (decl))
14140         decl = pushdecl (decl);
14141
14142       /* Add the new parameter to the list.  */
14143       *tail = build_tree_list (parameter->default_argument, decl);
14144       tail = &TREE_CHAIN (*tail);
14145
14146       /* Peek at the next token.  */
14147       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14148           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14149           /* These are for Objective-C++ */
14150           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14151           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14152         /* The parameter-declaration-list is complete.  */
14153         break;
14154       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14155         {
14156           cp_token *token;
14157
14158           /* Peek at the next token.  */
14159           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14160           /* If it's an ellipsis, then the list is complete.  */
14161           if (token->type == CPP_ELLIPSIS)
14162             break;
14163           /* Otherwise, there must be more parameters.  Consume the
14164              `,'.  */
14165           cp_lexer_consume_token (parser->lexer);
14166           /* When parsing something like:
14167
14168                 int i(float f, double d)
14169
14170              we can tell after seeing the declaration for "f" that we
14171              are not looking at an initialization of a variable "i",
14172              but rather at the declaration of a function "i".
14173
14174              Due to the fact that the parsing of template arguments
14175              (as specified to a template-id) requires backtracking we
14176              cannot use this technique when inside a template argument
14177              list.  */
14178           if (!parser->in_template_argument_list_p
14179               && !parser->in_type_id_in_expr_p
14180               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14181               /* However, a parameter-declaration of the form
14182                  "foat(f)" (which is a valid declaration of a
14183                  parameter "f") can also be interpreted as an
14184                  expression (the conversion of "f" to "float").  */
14185               && !parenthesized_p)
14186             cp_parser_commit_to_tentative_parse (parser);
14187         }
14188       else
14189         {
14190           cp_parser_error (parser, "expected %<,%> or %<...%>");
14191           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14192             cp_parser_skip_to_closing_parenthesis (parser,
14193                                                    /*recovering=*/true,
14194                                                    /*or_comma=*/false,
14195                                                    /*consume_paren=*/false);
14196           break;
14197         }
14198     }
14199
14200   parser->in_unbraced_linkage_specification_p
14201     = saved_in_unbraced_linkage_specification_p;
14202
14203   return parameters;
14204 }
14205
14206 /* Parse a parameter declaration.
14207
14208    parameter-declaration:
14209      decl-specifier-seq ... [opt] declarator
14210      decl-specifier-seq declarator = assignment-expression
14211      decl-specifier-seq ... [opt] abstract-declarator [opt]
14212      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14213
14214    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14215    declares a template parameter.  (In that case, a non-nested `>'
14216    token encountered during the parsing of the assignment-expression
14217    is not interpreted as a greater-than operator.)
14218
14219    Returns a representation of the parameter, or NULL if an error
14220    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14221    true iff the declarator is of the form "(p)".  */
14222
14223 static cp_parameter_declarator *
14224 cp_parser_parameter_declaration (cp_parser *parser,
14225                                  bool template_parm_p,
14226                                  bool *parenthesized_p)
14227 {
14228   int declares_class_or_enum;
14229   bool greater_than_is_operator_p;
14230   cp_decl_specifier_seq decl_specifiers;
14231   cp_declarator *declarator;
14232   tree default_argument;
14233   cp_token *token = NULL, *declarator_token_start = NULL;
14234   const char *saved_message;
14235
14236   /* In a template parameter, `>' is not an operator.
14237
14238      [temp.param]
14239
14240      When parsing a default template-argument for a non-type
14241      template-parameter, the first non-nested `>' is taken as the end
14242      of the template parameter-list rather than a greater-than
14243      operator.  */
14244   greater_than_is_operator_p = !template_parm_p;
14245
14246   /* Type definitions may not appear in parameter types.  */
14247   saved_message = parser->type_definition_forbidden_message;
14248   parser->type_definition_forbidden_message
14249     = "types may not be defined in parameter types";
14250
14251   /* Parse the declaration-specifiers.  */
14252   cp_parser_decl_specifier_seq (parser,
14253                                 CP_PARSER_FLAGS_NONE,
14254                                 &decl_specifiers,
14255                                 &declares_class_or_enum);
14256   /* If an error occurred, there's no reason to attempt to parse the
14257      rest of the declaration.  */
14258   if (cp_parser_error_occurred (parser))
14259     {
14260       parser->type_definition_forbidden_message = saved_message;
14261       return NULL;
14262     }
14263
14264   /* Peek at the next token.  */
14265   token = cp_lexer_peek_token (parser->lexer);
14266
14267   /* If the next token is a `)', `,', `=', `>', or `...', then there
14268      is no declarator. However, when variadic templates are enabled,
14269      there may be a declarator following `...'.  */
14270   if (token->type == CPP_CLOSE_PAREN
14271       || token->type == CPP_COMMA
14272       || token->type == CPP_EQ
14273       || token->type == CPP_GREATER)
14274     {
14275       declarator = NULL;
14276       if (parenthesized_p)
14277         *parenthesized_p = false;
14278     }
14279   /* Otherwise, there should be a declarator.  */
14280   else
14281     {
14282       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14283       parser->default_arg_ok_p = false;
14284
14285       /* After seeing a decl-specifier-seq, if the next token is not a
14286          "(", there is no possibility that the code is a valid
14287          expression.  Therefore, if parsing tentatively, we commit at
14288          this point.  */
14289       if (!parser->in_template_argument_list_p
14290           /* In an expression context, having seen:
14291
14292                (int((char ...
14293
14294              we cannot be sure whether we are looking at a
14295              function-type (taking a "char" as a parameter) or a cast
14296              of some object of type "char" to "int".  */
14297           && !parser->in_type_id_in_expr_p
14298           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14299           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14300         cp_parser_commit_to_tentative_parse (parser);
14301       /* Parse the declarator.  */
14302       declarator_token_start = token;
14303       declarator = cp_parser_declarator (parser,
14304                                          CP_PARSER_DECLARATOR_EITHER,
14305                                          /*ctor_dtor_or_conv_p=*/NULL,
14306                                          parenthesized_p,
14307                                          /*member_p=*/false);
14308       parser->default_arg_ok_p = saved_default_arg_ok_p;
14309       /* After the declarator, allow more attributes.  */
14310       decl_specifiers.attributes
14311         = chainon (decl_specifiers.attributes,
14312                    cp_parser_attributes_opt (parser));
14313     }
14314
14315   /* If the next token is an ellipsis, and we have not seen a
14316      declarator name, and the type of the declarator contains parameter
14317      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14318      a parameter pack expansion expression. Otherwise, leave the
14319      ellipsis for a C-style variadic function. */
14320   token = cp_lexer_peek_token (parser->lexer);
14321   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14322     {
14323       tree type = decl_specifiers.type;
14324
14325       if (type && DECL_P (type))
14326         type = TREE_TYPE (type);
14327
14328       if (type
14329           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14330           && declarator_can_be_parameter_pack (declarator)
14331           && (!declarator || !declarator->parameter_pack_p)
14332           && uses_parameter_packs (type))
14333         {
14334           /* Consume the `...'. */
14335           cp_lexer_consume_token (parser->lexer);
14336           maybe_warn_variadic_templates ();
14337           
14338           /* Build a pack expansion type */
14339           if (declarator)
14340             declarator->parameter_pack_p = true;
14341           else
14342             decl_specifiers.type = make_pack_expansion (type);
14343         }
14344     }
14345
14346   /* The restriction on defining new types applies only to the type
14347      of the parameter, not to the default argument.  */
14348   parser->type_definition_forbidden_message = saved_message;
14349
14350   /* If the next token is `=', then process a default argument.  */
14351   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14352     {
14353       /* Consume the `='.  */
14354       cp_lexer_consume_token (parser->lexer);
14355
14356       /* If we are defining a class, then the tokens that make up the
14357          default argument must be saved and processed later.  */
14358       if (!template_parm_p && at_class_scope_p ()
14359           && TYPE_BEING_DEFINED (current_class_type))
14360         {
14361           unsigned depth = 0;
14362           int maybe_template_id = 0;
14363           cp_token *first_token;
14364           cp_token *token;
14365
14366           /* Add tokens until we have processed the entire default
14367              argument.  We add the range [first_token, token).  */
14368           first_token = cp_lexer_peek_token (parser->lexer);
14369           while (true)
14370             {
14371               bool done = false;
14372
14373               /* Peek at the next token.  */
14374               token = cp_lexer_peek_token (parser->lexer);
14375               /* What we do depends on what token we have.  */
14376               switch (token->type)
14377                 {
14378                   /* In valid code, a default argument must be
14379                      immediately followed by a `,' `)', or `...'.  */
14380                 case CPP_COMMA:
14381                   if (depth == 0 && maybe_template_id)
14382                     {
14383                       /* If we've seen a '<', we might be in a
14384                          template-argument-list.  Until Core issue 325 is
14385                          resolved, we don't know how this situation ought
14386                          to be handled, so try to DTRT.  We check whether
14387                          what comes after the comma is a valid parameter
14388                          declaration list.  If it is, then the comma ends
14389                          the default argument; otherwise the default
14390                          argument continues.  */
14391                       bool error = false;
14392                       tree t;
14393
14394                       /* Set ITALP so cp_parser_parameter_declaration_list
14395                          doesn't decide to commit to this parse.  */
14396                       bool saved_italp = parser->in_template_argument_list_p;
14397                       parser->in_template_argument_list_p = true;
14398
14399                       cp_parser_parse_tentatively (parser);
14400                       cp_lexer_consume_token (parser->lexer);
14401                       begin_scope (sk_function_parms, NULL_TREE);
14402                       cp_parser_parameter_declaration_list (parser, &error);
14403                       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14404                         pop_binding (DECL_NAME (t), t);
14405                       leave_scope ();
14406                       if (!cp_parser_error_occurred (parser) && !error)
14407                         done = true;
14408                       cp_parser_abort_tentative_parse (parser);
14409
14410                       parser->in_template_argument_list_p = saved_italp;
14411                       break;
14412                     }
14413                 case CPP_CLOSE_PAREN:
14414                 case CPP_ELLIPSIS:
14415                   /* If we run into a non-nested `;', `}', or `]',
14416                      then the code is invalid -- but the default
14417                      argument is certainly over.  */
14418                 case CPP_SEMICOLON:
14419                 case CPP_CLOSE_BRACE:
14420                 case CPP_CLOSE_SQUARE:
14421                   if (depth == 0)
14422                     done = true;
14423                   /* Update DEPTH, if necessary.  */
14424                   else if (token->type == CPP_CLOSE_PAREN
14425                            || token->type == CPP_CLOSE_BRACE
14426                            || token->type == CPP_CLOSE_SQUARE)
14427                     --depth;
14428                   break;
14429
14430                 case CPP_OPEN_PAREN:
14431                 case CPP_OPEN_SQUARE:
14432                 case CPP_OPEN_BRACE:
14433                   ++depth;
14434                   break;
14435
14436                 case CPP_LESS:
14437                   if (depth == 0)
14438                     /* This might be the comparison operator, or it might
14439                        start a template argument list.  */
14440                     ++maybe_template_id;
14441                   break;
14442
14443                 case CPP_RSHIFT:
14444                   if (cxx_dialect == cxx98)
14445                     break;
14446                   /* Fall through for C++0x, which treats the `>>'
14447                      operator like two `>' tokens in certain
14448                      cases.  */
14449
14450                 case CPP_GREATER:
14451                   if (depth == 0)
14452                     {
14453                       /* This might be an operator, or it might close a
14454                          template argument list.  But if a previous '<'
14455                          started a template argument list, this will have
14456                          closed it, so we can't be in one anymore.  */
14457                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14458                       if (maybe_template_id < 0)
14459                         maybe_template_id = 0;
14460                     }
14461                   break;
14462
14463                   /* If we run out of tokens, issue an error message.  */
14464                 case CPP_EOF:
14465                 case CPP_PRAGMA_EOL:
14466                   error ("%Hfile ends in default argument", &token->location);
14467                   done = true;
14468                   break;
14469
14470                 case CPP_NAME:
14471                 case CPP_SCOPE:
14472                   /* In these cases, we should look for template-ids.
14473                      For example, if the default argument is
14474                      `X<int, double>()', we need to do name lookup to
14475                      figure out whether or not `X' is a template; if
14476                      so, the `,' does not end the default argument.
14477
14478                      That is not yet done.  */
14479                   break;
14480
14481                 default:
14482                   break;
14483                 }
14484
14485               /* If we've reached the end, stop.  */
14486               if (done)
14487                 break;
14488
14489               /* Add the token to the token block.  */
14490               token = cp_lexer_consume_token (parser->lexer);
14491             }
14492
14493           /* Create a DEFAULT_ARG to represent the unparsed default
14494              argument.  */
14495           default_argument = make_node (DEFAULT_ARG);
14496           DEFARG_TOKENS (default_argument)
14497             = cp_token_cache_new (first_token, token);
14498           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14499         }
14500       /* Outside of a class definition, we can just parse the
14501          assignment-expression.  */
14502       else
14503         {
14504           token = cp_lexer_peek_token (parser->lexer);
14505           default_argument 
14506             = cp_parser_default_argument (parser, template_parm_p);
14507         }
14508
14509       if (!parser->default_arg_ok_p)
14510         {
14511           if (flag_permissive)
14512             warning (0, "deprecated use of default argument for parameter of non-function");
14513           else
14514             {
14515               error ("%Hdefault arguments are only "
14516                      "permitted for function parameters",
14517                      &token->location);
14518               default_argument = NULL_TREE;
14519             }
14520         }
14521       else if ((declarator && declarator->parameter_pack_p)
14522                || (decl_specifiers.type
14523                    && PACK_EXPANSION_P (decl_specifiers.type)))
14524         {
14525           const char* kind = template_parm_p? "template " : "";
14526           
14527           /* Find the name of the parameter pack.  */     
14528           cp_declarator *id_declarator = declarator;
14529           while (id_declarator && id_declarator->kind != cdk_id)
14530             id_declarator = id_declarator->declarator;
14531           
14532           if (id_declarator && id_declarator->kind == cdk_id)
14533             error ("%H%sparameter pack %qD cannot have a default argument",
14534                    &declarator_token_start->location,
14535                    kind, id_declarator->u.id.unqualified_name);
14536           else
14537             error ("%H%sparameter pack cannot have a default argument",
14538                    &declarator_token_start->location, kind);
14539           
14540           default_argument = NULL_TREE;
14541         }
14542     }
14543   else
14544     default_argument = NULL_TREE;
14545
14546   return make_parameter_declarator (&decl_specifiers,
14547                                     declarator,
14548                                     default_argument);
14549 }
14550
14551 /* Parse a default argument and return it.
14552
14553    TEMPLATE_PARM_P is true if this is a default argument for a
14554    non-type template parameter.  */
14555 static tree
14556 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14557 {
14558   tree default_argument = NULL_TREE;
14559   bool saved_greater_than_is_operator_p;
14560   bool saved_local_variables_forbidden_p;
14561
14562   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14563      set correctly.  */
14564   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14565   parser->greater_than_is_operator_p = !template_parm_p;
14566   /* Local variable names (and the `this' keyword) may not
14567      appear in a default argument.  */
14568   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14569   parser->local_variables_forbidden_p = true;
14570   /* The default argument expression may cause implicitly
14571      defined member functions to be synthesized, which will
14572      result in garbage collection.  We must treat this
14573      situation as if we were within the body of function so as
14574      to avoid collecting live data on the stack.  */
14575   ++function_depth;
14576   /* Parse the assignment-expression.  */
14577   if (template_parm_p)
14578     push_deferring_access_checks (dk_no_deferred);
14579   default_argument
14580     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14581   if (template_parm_p)
14582     pop_deferring_access_checks ();
14583   /* Restore saved state.  */
14584   --function_depth;
14585   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14586   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14587
14588   return default_argument;
14589 }
14590
14591 /* Parse a function-body.
14592
14593    function-body:
14594      compound_statement  */
14595
14596 static void
14597 cp_parser_function_body (cp_parser *parser)
14598 {
14599   cp_parser_compound_statement (parser, NULL, false);
14600 }
14601
14602 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14603    true if a ctor-initializer was present.  */
14604
14605 static bool
14606 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14607 {
14608   tree body;
14609   bool ctor_initializer_p;
14610
14611   /* Begin the function body.  */
14612   body = begin_function_body ();
14613   /* Parse the optional ctor-initializer.  */
14614   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14615   /* Parse the function-body.  */
14616   cp_parser_function_body (parser);
14617   /* Finish the function body.  */
14618   finish_function_body (body);
14619
14620   return ctor_initializer_p;
14621 }
14622
14623 /* Parse an initializer.
14624
14625    initializer:
14626      = initializer-clause
14627      ( expression-list )
14628
14629    Returns an expression representing the initializer.  If no
14630    initializer is present, NULL_TREE is returned.
14631
14632    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14633    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14634    set to TRUE if there is no initializer present.  If there is an
14635    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14636    is set to true; otherwise it is set to false.  */
14637
14638 static tree
14639 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14640                        bool* non_constant_p)
14641 {
14642   cp_token *token;
14643   tree init;
14644
14645   /* Peek at the next token.  */
14646   token = cp_lexer_peek_token (parser->lexer);
14647
14648   /* Let our caller know whether or not this initializer was
14649      parenthesized.  */
14650   *is_direct_init = (token->type != CPP_EQ);
14651   /* Assume that the initializer is constant.  */
14652   *non_constant_p = false;
14653
14654   if (token->type == CPP_EQ)
14655     {
14656       /* Consume the `='.  */
14657       cp_lexer_consume_token (parser->lexer);
14658       /* Parse the initializer-clause.  */
14659       init = cp_parser_initializer_clause (parser, non_constant_p);
14660     }
14661   else if (token->type == CPP_OPEN_PAREN)
14662     init = cp_parser_parenthesized_expression_list (parser, false,
14663                                                     /*cast_p=*/false,
14664                                                     /*allow_expansion_p=*/true,
14665                                                     non_constant_p);
14666   else if (token->type == CPP_OPEN_BRACE)
14667     {
14668       maybe_warn_cpp0x ("extended initializer lists");
14669       init = cp_parser_braced_list (parser, non_constant_p);
14670       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14671     }
14672   else
14673     {
14674       /* Anything else is an error.  */
14675       cp_parser_error (parser, "expected initializer");
14676       init = error_mark_node;
14677     }
14678
14679   return init;
14680 }
14681
14682 /* Parse an initializer-clause.
14683
14684    initializer-clause:
14685      assignment-expression
14686      braced-init-list
14687
14688    Returns an expression representing the initializer.
14689
14690    If the `assignment-expression' production is used the value
14691    returned is simply a representation for the expression.
14692
14693    Otherwise, calls cp_parser_braced_list.  */
14694
14695 static tree
14696 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14697 {
14698   tree initializer;
14699
14700   /* Assume the expression is constant.  */
14701   *non_constant_p = false;
14702
14703   /* If it is not a `{', then we are looking at an
14704      assignment-expression.  */
14705   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14706     {
14707       initializer
14708         = cp_parser_constant_expression (parser,
14709                                         /*allow_non_constant_p=*/true,
14710                                         non_constant_p);
14711       if (!*non_constant_p)
14712         initializer = fold_non_dependent_expr (initializer);
14713     }
14714   else
14715     initializer = cp_parser_braced_list (parser, non_constant_p);
14716
14717   return initializer;
14718 }
14719
14720 /* Parse a brace-enclosed initializer list.
14721
14722    braced-init-list:
14723      { initializer-list , [opt] }
14724      { }
14725
14726    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14727    the elements of the initializer-list (or NULL, if the last
14728    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14729    NULL_TREE.  There is no way to detect whether or not the optional
14730    trailing `,' was provided.  NON_CONSTANT_P is as for
14731    cp_parser_initializer.  */     
14732
14733 static tree
14734 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14735 {
14736   tree initializer;
14737
14738   /* Consume the `{' token.  */
14739   cp_lexer_consume_token (parser->lexer);
14740   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14741   initializer = make_node (CONSTRUCTOR);
14742   /* If it's not a `}', then there is a non-trivial initializer.  */
14743   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14744     {
14745       /* Parse the initializer list.  */
14746       CONSTRUCTOR_ELTS (initializer)
14747         = cp_parser_initializer_list (parser, non_constant_p);
14748       /* A trailing `,' token is allowed.  */
14749       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14750         cp_lexer_consume_token (parser->lexer);
14751     }
14752   /* Now, there should be a trailing `}'.  */
14753   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14754   TREE_TYPE (initializer) = init_list_type_node;
14755   return initializer;
14756 }
14757
14758 /* Parse an initializer-list.
14759
14760    initializer-list:
14761      initializer-clause ... [opt]
14762      initializer-list , initializer-clause ... [opt]
14763
14764    GNU Extension:
14765
14766    initializer-list:
14767      identifier : initializer-clause
14768      initializer-list, identifier : initializer-clause
14769
14770    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14771    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14772    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14773    as for cp_parser_initializer.  */
14774
14775 static VEC(constructor_elt,gc) *
14776 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14777 {
14778   VEC(constructor_elt,gc) *v = NULL;
14779
14780   /* Assume all of the expressions are constant.  */
14781   *non_constant_p = false;
14782
14783   /* Parse the rest of the list.  */
14784   while (true)
14785     {
14786       cp_token *token;
14787       tree identifier;
14788       tree initializer;
14789       bool clause_non_constant_p;
14790
14791       /* If the next token is an identifier and the following one is a
14792          colon, we are looking at the GNU designated-initializer
14793          syntax.  */
14794       if (cp_parser_allow_gnu_extensions_p (parser)
14795           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14796           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14797         {
14798           /* Warn the user that they are using an extension.  */
14799           pedwarn (input_location, OPT_pedantic, 
14800                    "ISO C++ does not allow designated initializers");
14801           /* Consume the identifier.  */
14802           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14803           /* Consume the `:'.  */
14804           cp_lexer_consume_token (parser->lexer);
14805         }
14806       else
14807         identifier = NULL_TREE;
14808
14809       /* Parse the initializer.  */
14810       initializer = cp_parser_initializer_clause (parser,
14811                                                   &clause_non_constant_p);
14812       /* If any clause is non-constant, so is the entire initializer.  */
14813       if (clause_non_constant_p)
14814         *non_constant_p = true;
14815
14816       /* If we have an ellipsis, this is an initializer pack
14817          expansion.  */
14818       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14819         {
14820           /* Consume the `...'.  */
14821           cp_lexer_consume_token (parser->lexer);
14822
14823           /* Turn the initializer into an initializer expansion.  */
14824           initializer = make_pack_expansion (initializer);
14825         }
14826
14827       /* Add it to the vector.  */
14828       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14829
14830       /* If the next token is not a comma, we have reached the end of
14831          the list.  */
14832       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14833         break;
14834
14835       /* Peek at the next token.  */
14836       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14837       /* If the next token is a `}', then we're still done.  An
14838          initializer-clause can have a trailing `,' after the
14839          initializer-list and before the closing `}'.  */
14840       if (token->type == CPP_CLOSE_BRACE)
14841         break;
14842
14843       /* Consume the `,' token.  */
14844       cp_lexer_consume_token (parser->lexer);
14845     }
14846
14847   return v;
14848 }
14849
14850 /* Classes [gram.class] */
14851
14852 /* Parse a class-name.
14853
14854    class-name:
14855      identifier
14856      template-id
14857
14858    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14859    to indicate that names looked up in dependent types should be
14860    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14861    keyword has been used to indicate that the name that appears next
14862    is a template.  TAG_TYPE indicates the explicit tag given before
14863    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14864    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14865    is the class being defined in a class-head.
14866
14867    Returns the TYPE_DECL representing the class.  */
14868
14869 static tree
14870 cp_parser_class_name (cp_parser *parser,
14871                       bool typename_keyword_p,
14872                       bool template_keyword_p,
14873                       enum tag_types tag_type,
14874                       bool check_dependency_p,
14875                       bool class_head_p,
14876                       bool is_declaration)
14877 {
14878   tree decl;
14879   tree scope;
14880   bool typename_p;
14881   cp_token *token;
14882   tree identifier = NULL_TREE;
14883
14884   /* All class-names start with an identifier.  */
14885   token = cp_lexer_peek_token (parser->lexer);
14886   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14887     {
14888       cp_parser_error (parser, "expected class-name");
14889       return error_mark_node;
14890     }
14891
14892   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14893      to a template-id, so we save it here.  */
14894   scope = parser->scope;
14895   if (scope == error_mark_node)
14896     return error_mark_node;
14897
14898   /* Any name names a type if we're following the `typename' keyword
14899      in a qualified name where the enclosing scope is type-dependent.  */
14900   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14901                 && dependent_type_p (scope));
14902   /* Handle the common case (an identifier, but not a template-id)
14903      efficiently.  */
14904   if (token->type == CPP_NAME
14905       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14906     {
14907       cp_token *identifier_token;
14908       bool ambiguous_p;
14909
14910       /* Look for the identifier.  */
14911       identifier_token = cp_lexer_peek_token (parser->lexer);
14912       ambiguous_p = identifier_token->ambiguous_p;
14913       identifier = cp_parser_identifier (parser);
14914       /* If the next token isn't an identifier, we are certainly not
14915          looking at a class-name.  */
14916       if (identifier == error_mark_node)
14917         decl = error_mark_node;
14918       /* If we know this is a type-name, there's no need to look it
14919          up.  */
14920       else if (typename_p)
14921         decl = identifier;
14922       else
14923         {
14924           tree ambiguous_decls;
14925           /* If we already know that this lookup is ambiguous, then
14926              we've already issued an error message; there's no reason
14927              to check again.  */
14928           if (ambiguous_p)
14929             {
14930               cp_parser_simulate_error (parser);
14931               return error_mark_node;
14932             }
14933           /* If the next token is a `::', then the name must be a type
14934              name.
14935
14936              [basic.lookup.qual]
14937
14938              During the lookup for a name preceding the :: scope
14939              resolution operator, object, function, and enumerator
14940              names are ignored.  */
14941           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14942             tag_type = typename_type;
14943           /* Look up the name.  */
14944           decl = cp_parser_lookup_name (parser, identifier,
14945                                         tag_type,
14946                                         /*is_template=*/false,
14947                                         /*is_namespace=*/false,
14948                                         check_dependency_p,
14949                                         &ambiguous_decls,
14950                                         identifier_token->location);
14951           if (ambiguous_decls)
14952             {
14953               error ("%Hreference to %qD is ambiguous",
14954                      &identifier_token->location, identifier);
14955               print_candidates (ambiguous_decls);
14956               if (cp_parser_parsing_tentatively (parser))
14957                 {
14958                   identifier_token->ambiguous_p = true;
14959                   cp_parser_simulate_error (parser);
14960                 }
14961               return error_mark_node;
14962             }
14963         }
14964     }
14965   else
14966     {
14967       /* Try a template-id.  */
14968       decl = cp_parser_template_id (parser, template_keyword_p,
14969                                     check_dependency_p,
14970                                     is_declaration);
14971       if (decl == error_mark_node)
14972         return error_mark_node;
14973     }
14974
14975   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14976
14977   /* If this is a typename, create a TYPENAME_TYPE.  */
14978   if (typename_p && decl != error_mark_node)
14979     {
14980       decl = make_typename_type (scope, decl, typename_type,
14981                                  /*complain=*/tf_error);
14982       if (decl != error_mark_node)
14983         decl = TYPE_NAME (decl);
14984     }
14985
14986   /* Check to see that it is really the name of a class.  */
14987   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14988       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14989       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14990     /* Situations like this:
14991
14992          template <typename T> struct A {
14993            typename T::template X<int>::I i;
14994          };
14995
14996        are problematic.  Is `T::template X<int>' a class-name?  The
14997        standard does not seem to be definitive, but there is no other
14998        valid interpretation of the following `::'.  Therefore, those
14999        names are considered class-names.  */
15000     {
15001       decl = make_typename_type (scope, decl, tag_type, tf_error);
15002       if (decl != error_mark_node)
15003         decl = TYPE_NAME (decl);
15004     }
15005   else if (TREE_CODE (decl) != TYPE_DECL
15006            || TREE_TYPE (decl) == error_mark_node
15007            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15008     decl = error_mark_node;
15009
15010   if (decl == error_mark_node)
15011     cp_parser_error (parser, "expected class-name");
15012   else if (identifier && !parser->scope)
15013     maybe_note_name_used_in_class (identifier, decl);
15014
15015   return decl;
15016 }
15017
15018 /* Parse a class-specifier.
15019
15020    class-specifier:
15021      class-head { member-specification [opt] }
15022
15023    Returns the TREE_TYPE representing the class.  */
15024
15025 static tree
15026 cp_parser_class_specifier (cp_parser* parser)
15027 {
15028   cp_token *token;
15029   tree type;
15030   tree attributes = NULL_TREE;
15031   int has_trailing_semicolon;
15032   bool nested_name_specifier_p;
15033   unsigned saved_num_template_parameter_lists;
15034   bool saved_in_function_body;
15035   bool saved_in_unbraced_linkage_specification_p;
15036   tree old_scope = NULL_TREE;
15037   tree scope = NULL_TREE;
15038   tree bases;
15039
15040   push_deferring_access_checks (dk_no_deferred);
15041
15042   /* Parse the class-head.  */
15043   type = cp_parser_class_head (parser,
15044                                &nested_name_specifier_p,
15045                                &attributes,
15046                                &bases);
15047   /* If the class-head was a semantic disaster, skip the entire body
15048      of the class.  */
15049   if (!type)
15050     {
15051       cp_parser_skip_to_end_of_block_or_statement (parser);
15052       pop_deferring_access_checks ();
15053       return error_mark_node;
15054     }
15055
15056   /* Look for the `{'.  */
15057   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15058     {
15059       pop_deferring_access_checks ();
15060       return error_mark_node;
15061     }
15062
15063   /* Process the base classes. If they're invalid, skip the 
15064      entire class body.  */
15065   if (!xref_basetypes (type, bases))
15066     {
15067       /* Consuming the closing brace yields better error messages
15068          later on.  */
15069       if (cp_parser_skip_to_closing_brace (parser))
15070         cp_lexer_consume_token (parser->lexer);
15071       pop_deferring_access_checks ();
15072       return error_mark_node;
15073     }
15074
15075   /* Issue an error message if type-definitions are forbidden here.  */
15076   cp_parser_check_type_definition (parser);
15077   /* Remember that we are defining one more class.  */
15078   ++parser->num_classes_being_defined;
15079   /* Inside the class, surrounding template-parameter-lists do not
15080      apply.  */
15081   saved_num_template_parameter_lists
15082     = parser->num_template_parameter_lists;
15083   parser->num_template_parameter_lists = 0;
15084   /* We are not in a function body.  */
15085   saved_in_function_body = parser->in_function_body;
15086   parser->in_function_body = false;
15087   /* We are not immediately inside an extern "lang" block.  */
15088   saved_in_unbraced_linkage_specification_p
15089     = parser->in_unbraced_linkage_specification_p;
15090   parser->in_unbraced_linkage_specification_p = false;
15091
15092   /* Start the class.  */
15093   if (nested_name_specifier_p)
15094     {
15095       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15096       old_scope = push_inner_scope (scope);
15097     }
15098   type = begin_class_definition (type, attributes);
15099
15100   if (type == error_mark_node)
15101     /* If the type is erroneous, skip the entire body of the class.  */
15102     cp_parser_skip_to_closing_brace (parser);
15103   else
15104     /* Parse the member-specification.  */
15105     cp_parser_member_specification_opt (parser);
15106
15107   /* Look for the trailing `}'.  */
15108   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15109   /* We get better error messages by noticing a common problem: a
15110      missing trailing `;'.  */
15111   token = cp_lexer_peek_token (parser->lexer);
15112   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15113   /* Look for trailing attributes to apply to this class.  */
15114   if (cp_parser_allow_gnu_extensions_p (parser))
15115     attributes = cp_parser_attributes_opt (parser);
15116   if (type != error_mark_node)
15117     type = finish_struct (type, attributes);
15118   if (nested_name_specifier_p)
15119     pop_inner_scope (old_scope, scope);
15120   /* If this class is not itself within the scope of another class,
15121      then we need to parse the bodies of all of the queued function
15122      definitions.  Note that the queued functions defined in a class
15123      are not always processed immediately following the
15124      class-specifier for that class.  Consider:
15125
15126        struct A {
15127          struct B { void f() { sizeof (A); } };
15128        };
15129
15130      If `f' were processed before the processing of `A' were
15131      completed, there would be no way to compute the size of `A'.
15132      Note that the nesting we are interested in here is lexical --
15133      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15134      for:
15135
15136        struct A { struct B; };
15137        struct A::B { void f() { } };
15138
15139      there is no need to delay the parsing of `A::B::f'.  */
15140   if (--parser->num_classes_being_defined == 0)
15141     {
15142       tree queue_entry;
15143       tree fn;
15144       tree class_type = NULL_TREE;
15145       tree pushed_scope = NULL_TREE;
15146
15147       /* In a first pass, parse default arguments to the functions.
15148          Then, in a second pass, parse the bodies of the functions.
15149          This two-phased approach handles cases like:
15150
15151             struct S {
15152               void f() { g(); }
15153               void g(int i = 3);
15154             };
15155
15156          */
15157       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15158              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15159            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15160            TREE_PURPOSE (parser->unparsed_functions_queues)
15161              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15162         {
15163           fn = TREE_VALUE (queue_entry);
15164           /* If there are default arguments that have not yet been processed,
15165              take care of them now.  */
15166           if (class_type != TREE_PURPOSE (queue_entry))
15167             {
15168               if (pushed_scope)
15169                 pop_scope (pushed_scope);
15170               class_type = TREE_PURPOSE (queue_entry);
15171               pushed_scope = push_scope (class_type);
15172             }
15173           /* Make sure that any template parameters are in scope.  */
15174           maybe_begin_member_template_processing (fn);
15175           /* Parse the default argument expressions.  */
15176           cp_parser_late_parsing_default_args (parser, fn);
15177           /* Remove any template parameters from the symbol table.  */
15178           maybe_end_member_template_processing ();
15179         }
15180       if (pushed_scope)
15181         pop_scope (pushed_scope);
15182       /* Now parse the body of the functions.  */
15183       for (TREE_VALUE (parser->unparsed_functions_queues)
15184              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15185            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15186            TREE_VALUE (parser->unparsed_functions_queues)
15187              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15188         {
15189           /* Figure out which function we need to process.  */
15190           fn = TREE_VALUE (queue_entry);
15191           /* Parse the function.  */
15192           cp_parser_late_parsing_for_member (parser, fn);
15193         }
15194     }
15195
15196   /* Put back any saved access checks.  */
15197   pop_deferring_access_checks ();
15198
15199   /* Restore saved state.  */
15200   parser->in_function_body = saved_in_function_body;
15201   parser->num_template_parameter_lists
15202     = saved_num_template_parameter_lists;
15203   parser->in_unbraced_linkage_specification_p
15204     = saved_in_unbraced_linkage_specification_p;
15205
15206   return type;
15207 }
15208
15209 /* Parse a class-head.
15210
15211    class-head:
15212      class-key identifier [opt] base-clause [opt]
15213      class-key nested-name-specifier identifier base-clause [opt]
15214      class-key nested-name-specifier [opt] template-id
15215        base-clause [opt]
15216
15217    GNU Extensions:
15218      class-key attributes identifier [opt] base-clause [opt]
15219      class-key attributes nested-name-specifier identifier base-clause [opt]
15220      class-key attributes nested-name-specifier [opt] template-id
15221        base-clause [opt]
15222
15223    Upon return BASES is initialized to the list of base classes (or
15224    NULL, if there are none) in the same form returned by
15225    cp_parser_base_clause.
15226
15227    Returns the TYPE of the indicated class.  Sets
15228    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15229    involving a nested-name-specifier was used, and FALSE otherwise.
15230
15231    Returns error_mark_node if this is not a class-head.
15232
15233    Returns NULL_TREE if the class-head is syntactically valid, but
15234    semantically invalid in a way that means we should skip the entire
15235    body of the class.  */
15236
15237 static tree
15238 cp_parser_class_head (cp_parser* parser,
15239                       bool* nested_name_specifier_p,
15240                       tree *attributes_p,
15241                       tree *bases)
15242 {
15243   tree nested_name_specifier;
15244   enum tag_types class_key;
15245   tree id = NULL_TREE;
15246   tree type = NULL_TREE;
15247   tree attributes;
15248   bool template_id_p = false;
15249   bool qualified_p = false;
15250   bool invalid_nested_name_p = false;
15251   bool invalid_explicit_specialization_p = false;
15252   tree pushed_scope = NULL_TREE;
15253   unsigned num_templates;
15254   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15255   /* Assume no nested-name-specifier will be present.  */
15256   *nested_name_specifier_p = false;
15257   /* Assume no template parameter lists will be used in defining the
15258      type.  */
15259   num_templates = 0;
15260
15261   *bases = NULL_TREE;
15262
15263   /* Look for the class-key.  */
15264   class_key = cp_parser_class_key (parser);
15265   if (class_key == none_type)
15266     return error_mark_node;
15267
15268   /* Parse the attributes.  */
15269   attributes = cp_parser_attributes_opt (parser);
15270
15271   /* If the next token is `::', that is invalid -- but sometimes
15272      people do try to write:
15273
15274        struct ::S {};
15275
15276      Handle this gracefully by accepting the extra qualifier, and then
15277      issuing an error about it later if this really is a
15278      class-head.  If it turns out just to be an elaborated type
15279      specifier, remain silent.  */
15280   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15281     qualified_p = true;
15282
15283   push_deferring_access_checks (dk_no_check);
15284
15285   /* Determine the name of the class.  Begin by looking for an
15286      optional nested-name-specifier.  */
15287   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15288   nested_name_specifier
15289     = cp_parser_nested_name_specifier_opt (parser,
15290                                            /*typename_keyword_p=*/false,
15291                                            /*check_dependency_p=*/false,
15292                                            /*type_p=*/false,
15293                                            /*is_declaration=*/false);
15294   /* If there was a nested-name-specifier, then there *must* be an
15295      identifier.  */
15296   if (nested_name_specifier)
15297     {
15298       type_start_token = cp_lexer_peek_token (parser->lexer);
15299       /* Although the grammar says `identifier', it really means
15300          `class-name' or `template-name'.  You are only allowed to
15301          define a class that has already been declared with this
15302          syntax.
15303
15304          The proposed resolution for Core Issue 180 says that wherever
15305          you see `class T::X' you should treat `X' as a type-name.
15306
15307          It is OK to define an inaccessible class; for example:
15308
15309            class A { class B; };
15310            class A::B {};
15311
15312          We do not know if we will see a class-name, or a
15313          template-name.  We look for a class-name first, in case the
15314          class-name is a template-id; if we looked for the
15315          template-name first we would stop after the template-name.  */
15316       cp_parser_parse_tentatively (parser);
15317       type = cp_parser_class_name (parser,
15318                                    /*typename_keyword_p=*/false,
15319                                    /*template_keyword_p=*/false,
15320                                    class_type,
15321                                    /*check_dependency_p=*/false,
15322                                    /*class_head_p=*/true,
15323                                    /*is_declaration=*/false);
15324       /* If that didn't work, ignore the nested-name-specifier.  */
15325       if (!cp_parser_parse_definitely (parser))
15326         {
15327           invalid_nested_name_p = true;
15328           type_start_token = cp_lexer_peek_token (parser->lexer);
15329           id = cp_parser_identifier (parser);
15330           if (id == error_mark_node)
15331             id = NULL_TREE;
15332         }
15333       /* If we could not find a corresponding TYPE, treat this
15334          declaration like an unqualified declaration.  */
15335       if (type == error_mark_node)
15336         nested_name_specifier = NULL_TREE;
15337       /* Otherwise, count the number of templates used in TYPE and its
15338          containing scopes.  */
15339       else
15340         {
15341           tree scope;
15342
15343           for (scope = TREE_TYPE (type);
15344                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15345                scope = (TYPE_P (scope)
15346                         ? TYPE_CONTEXT (scope)
15347                         : DECL_CONTEXT (scope)))
15348             if (TYPE_P (scope)
15349                 && CLASS_TYPE_P (scope)
15350                 && CLASSTYPE_TEMPLATE_INFO (scope)
15351                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15352                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15353               ++num_templates;
15354         }
15355     }
15356   /* Otherwise, the identifier is optional.  */
15357   else
15358     {
15359       /* We don't know whether what comes next is a template-id,
15360          an identifier, or nothing at all.  */
15361       cp_parser_parse_tentatively (parser);
15362       /* Check for a template-id.  */
15363       type_start_token = cp_lexer_peek_token (parser->lexer);
15364       id = cp_parser_template_id (parser,
15365                                   /*template_keyword_p=*/false,
15366                                   /*check_dependency_p=*/true,
15367                                   /*is_declaration=*/true);
15368       /* If that didn't work, it could still be an identifier.  */
15369       if (!cp_parser_parse_definitely (parser))
15370         {
15371           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15372             {
15373               type_start_token = cp_lexer_peek_token (parser->lexer);
15374               id = cp_parser_identifier (parser);
15375             }
15376           else
15377             id = NULL_TREE;
15378         }
15379       else
15380         {
15381           template_id_p = true;
15382           ++num_templates;
15383         }
15384     }
15385
15386   pop_deferring_access_checks ();
15387
15388   if (id)
15389     cp_parser_check_for_invalid_template_id (parser, id,
15390                                              type_start_token->location);
15391
15392   /* If it's not a `:' or a `{' then we can't really be looking at a
15393      class-head, since a class-head only appears as part of a
15394      class-specifier.  We have to detect this situation before calling
15395      xref_tag, since that has irreversible side-effects.  */
15396   if (!cp_parser_next_token_starts_class_definition_p (parser))
15397     {
15398       cp_parser_error (parser, "expected %<{%> or %<:%>");
15399       return error_mark_node;
15400     }
15401
15402   /* At this point, we're going ahead with the class-specifier, even
15403      if some other problem occurs.  */
15404   cp_parser_commit_to_tentative_parse (parser);
15405   /* Issue the error about the overly-qualified name now.  */
15406   if (qualified_p)
15407     {
15408       cp_parser_error (parser,
15409                        "global qualification of class name is invalid");
15410       return error_mark_node;
15411     }
15412   else if (invalid_nested_name_p)
15413     {
15414       cp_parser_error (parser,
15415                        "qualified name does not name a class");
15416       return error_mark_node;
15417     }
15418   else if (nested_name_specifier)
15419     {
15420       tree scope;
15421
15422       /* Reject typedef-names in class heads.  */
15423       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15424         {
15425           error ("%Hinvalid class name in declaration of %qD",
15426                  &type_start_token->location, type);
15427           type = NULL_TREE;
15428           goto done;
15429         }
15430
15431       /* Figure out in what scope the declaration is being placed.  */
15432       scope = current_scope ();
15433       /* If that scope does not contain the scope in which the
15434          class was originally declared, the program is invalid.  */
15435       if (scope && !is_ancestor (scope, nested_name_specifier))
15436         {
15437           if (at_namespace_scope_p ())
15438             error ("%Hdeclaration of %qD in namespace %qD which does not "
15439                    "enclose %qD",
15440                    &type_start_token->location,
15441                    type, scope, nested_name_specifier);
15442           else
15443             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15444                    &type_start_token->location,
15445                    type, scope, nested_name_specifier);
15446           type = NULL_TREE;
15447           goto done;
15448         }
15449       /* [dcl.meaning]
15450
15451          A declarator-id shall not be qualified except for the
15452          definition of a ... nested class outside of its class
15453          ... [or] the definition or explicit instantiation of a
15454          class member of a namespace outside of its namespace.  */
15455       if (scope == nested_name_specifier)
15456         {
15457           permerror (input_location, "%Hextra qualification not allowed",
15458                      &nested_name_specifier_token_start->location);
15459           nested_name_specifier = NULL_TREE;
15460           num_templates = 0;
15461         }
15462     }
15463   /* An explicit-specialization must be preceded by "template <>".  If
15464      it is not, try to recover gracefully.  */
15465   if (at_namespace_scope_p ()
15466       && parser->num_template_parameter_lists == 0
15467       && template_id_p)
15468     {
15469       error ("%Han explicit specialization must be preceded by %<template <>%>",
15470              &type_start_token->location);
15471       invalid_explicit_specialization_p = true;
15472       /* Take the same action that would have been taken by
15473          cp_parser_explicit_specialization.  */
15474       ++parser->num_template_parameter_lists;
15475       begin_specialization ();
15476     }
15477   /* There must be no "return" statements between this point and the
15478      end of this function; set "type "to the correct return value and
15479      use "goto done;" to return.  */
15480   /* Make sure that the right number of template parameters were
15481      present.  */
15482   if (!cp_parser_check_template_parameters (parser, num_templates,
15483                                             type_start_token->location))
15484     {
15485       /* If something went wrong, there is no point in even trying to
15486          process the class-definition.  */
15487       type = NULL_TREE;
15488       goto done;
15489     }
15490
15491   /* Look up the type.  */
15492   if (template_id_p)
15493     {
15494       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15495           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15496               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15497         {
15498           error ("%Hfunction template %qD redeclared as a class template",
15499                  &type_start_token->location, id);
15500           type = error_mark_node;
15501         }
15502       else
15503         {
15504           type = TREE_TYPE (id);
15505           type = maybe_process_partial_specialization (type);
15506         }
15507       if (nested_name_specifier)
15508         pushed_scope = push_scope (nested_name_specifier);
15509     }
15510   else if (nested_name_specifier)
15511     {
15512       tree class_type;
15513
15514       /* Given:
15515
15516             template <typename T> struct S { struct T };
15517             template <typename T> struct S<T>::T { };
15518
15519          we will get a TYPENAME_TYPE when processing the definition of
15520          `S::T'.  We need to resolve it to the actual type before we
15521          try to define it.  */
15522       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15523         {
15524           class_type = resolve_typename_type (TREE_TYPE (type),
15525                                               /*only_current_p=*/false);
15526           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15527             type = TYPE_NAME (class_type);
15528           else
15529             {
15530               cp_parser_error (parser, "could not resolve typename type");
15531               type = error_mark_node;
15532             }
15533         }
15534
15535       if (maybe_process_partial_specialization (TREE_TYPE (type))
15536           == error_mark_node)
15537         {
15538           type = NULL_TREE;
15539           goto done;
15540         }
15541
15542       class_type = current_class_type;
15543       /* Enter the scope indicated by the nested-name-specifier.  */
15544       pushed_scope = push_scope (nested_name_specifier);
15545       /* Get the canonical version of this type.  */
15546       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15547       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15548           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15549         {
15550           type = push_template_decl (type);
15551           if (type == error_mark_node)
15552             {
15553               type = NULL_TREE;
15554               goto done;
15555             }
15556         }
15557
15558       type = TREE_TYPE (type);
15559       *nested_name_specifier_p = true;
15560     }
15561   else      /* The name is not a nested name.  */
15562     {
15563       /* If the class was unnamed, create a dummy name.  */
15564       if (!id)
15565         id = make_anon_name ();
15566       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15567                        parser->num_template_parameter_lists);
15568     }
15569
15570   /* Indicate whether this class was declared as a `class' or as a
15571      `struct'.  */
15572   if (TREE_CODE (type) == RECORD_TYPE)
15573     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15574   cp_parser_check_class_key (class_key, type);
15575
15576   /* If this type was already complete, and we see another definition,
15577      that's an error.  */
15578   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15579     {
15580       error ("%Hredefinition of %q#T",
15581              &type_start_token->location, type);
15582       error ("%Hprevious definition of %q+#T",
15583              &type_start_token->location, type);
15584       type = NULL_TREE;
15585       goto done;
15586     }
15587   else if (type == error_mark_node)
15588     type = NULL_TREE;
15589
15590   /* We will have entered the scope containing the class; the names of
15591      base classes should be looked up in that context.  For example:
15592
15593        struct A { struct B {}; struct C; };
15594        struct A::C : B {};
15595
15596      is valid.  */
15597
15598   /* Get the list of base-classes, if there is one.  */
15599   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15600     *bases = cp_parser_base_clause (parser);
15601
15602  done:
15603   /* Leave the scope given by the nested-name-specifier.  We will
15604      enter the class scope itself while processing the members.  */
15605   if (pushed_scope)
15606     pop_scope (pushed_scope);
15607
15608   if (invalid_explicit_specialization_p)
15609     {
15610       end_specialization ();
15611       --parser->num_template_parameter_lists;
15612     }
15613   *attributes_p = attributes;
15614   return type;
15615 }
15616
15617 /* Parse a class-key.
15618
15619    class-key:
15620      class
15621      struct
15622      union
15623
15624    Returns the kind of class-key specified, or none_type to indicate
15625    error.  */
15626
15627 static enum tag_types
15628 cp_parser_class_key (cp_parser* parser)
15629 {
15630   cp_token *token;
15631   enum tag_types tag_type;
15632
15633   /* Look for the class-key.  */
15634   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15635   if (!token)
15636     return none_type;
15637
15638   /* Check to see if the TOKEN is a class-key.  */
15639   tag_type = cp_parser_token_is_class_key (token);
15640   if (!tag_type)
15641     cp_parser_error (parser, "expected class-key");
15642   return tag_type;
15643 }
15644
15645 /* Parse an (optional) member-specification.
15646
15647    member-specification:
15648      member-declaration member-specification [opt]
15649      access-specifier : member-specification [opt]  */
15650
15651 static void
15652 cp_parser_member_specification_opt (cp_parser* parser)
15653 {
15654   while (true)
15655     {
15656       cp_token *token;
15657       enum rid keyword;
15658
15659       /* Peek at the next token.  */
15660       token = cp_lexer_peek_token (parser->lexer);
15661       /* If it's a `}', or EOF then we've seen all the members.  */
15662       if (token->type == CPP_CLOSE_BRACE
15663           || token->type == CPP_EOF
15664           || token->type == CPP_PRAGMA_EOL)
15665         break;
15666
15667       /* See if this token is a keyword.  */
15668       keyword = token->keyword;
15669       switch (keyword)
15670         {
15671         case RID_PUBLIC:
15672         case RID_PROTECTED:
15673         case RID_PRIVATE:
15674           /* Consume the access-specifier.  */
15675           cp_lexer_consume_token (parser->lexer);
15676           /* Remember which access-specifier is active.  */
15677           current_access_specifier = token->u.value;
15678           /* Look for the `:'.  */
15679           cp_parser_require (parser, CPP_COLON, "%<:%>");
15680           break;
15681
15682         default:
15683           /* Accept #pragmas at class scope.  */
15684           if (token->type == CPP_PRAGMA)
15685             {
15686               cp_parser_pragma (parser, pragma_external);
15687               break;
15688             }
15689
15690           /* Otherwise, the next construction must be a
15691              member-declaration.  */
15692           cp_parser_member_declaration (parser);
15693         }
15694     }
15695 }
15696
15697 /* Parse a member-declaration.
15698
15699    member-declaration:
15700      decl-specifier-seq [opt] member-declarator-list [opt] ;
15701      function-definition ; [opt]
15702      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15703      using-declaration
15704      template-declaration
15705
15706    member-declarator-list:
15707      member-declarator
15708      member-declarator-list , member-declarator
15709
15710    member-declarator:
15711      declarator pure-specifier [opt]
15712      declarator constant-initializer [opt]
15713      identifier [opt] : constant-expression
15714
15715    GNU Extensions:
15716
15717    member-declaration:
15718      __extension__ member-declaration
15719
15720    member-declarator:
15721      declarator attributes [opt] pure-specifier [opt]
15722      declarator attributes [opt] constant-initializer [opt]
15723      identifier [opt] attributes [opt] : constant-expression  
15724
15725    C++0x Extensions:
15726
15727    member-declaration:
15728      static_assert-declaration  */
15729
15730 static void
15731 cp_parser_member_declaration (cp_parser* parser)
15732 {
15733   cp_decl_specifier_seq decl_specifiers;
15734   tree prefix_attributes;
15735   tree decl;
15736   int declares_class_or_enum;
15737   bool friend_p;
15738   cp_token *token = NULL;
15739   cp_token *decl_spec_token_start = NULL;
15740   cp_token *initializer_token_start = NULL;
15741   int saved_pedantic;
15742
15743   /* Check for the `__extension__' keyword.  */
15744   if (cp_parser_extension_opt (parser, &saved_pedantic))
15745     {
15746       /* Recurse.  */
15747       cp_parser_member_declaration (parser);
15748       /* Restore the old value of the PEDANTIC flag.  */
15749       pedantic = saved_pedantic;
15750
15751       return;
15752     }
15753
15754   /* Check for a template-declaration.  */
15755   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15756     {
15757       /* An explicit specialization here is an error condition, and we
15758          expect the specialization handler to detect and report this.  */
15759       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15760           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15761         cp_parser_explicit_specialization (parser);
15762       else
15763         cp_parser_template_declaration (parser, /*member_p=*/true);
15764
15765       return;
15766     }
15767
15768   /* Check for a using-declaration.  */
15769   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15770     {
15771       /* Parse the using-declaration.  */
15772       cp_parser_using_declaration (parser,
15773                                    /*access_declaration_p=*/false);
15774       return;
15775     }
15776
15777   /* Check for @defs.  */
15778   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15779     {
15780       tree ivar, member;
15781       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15782       ivar = ivar_chains;
15783       while (ivar)
15784         {
15785           member = ivar;
15786           ivar = TREE_CHAIN (member);
15787           TREE_CHAIN (member) = NULL_TREE;
15788           finish_member_declaration (member);
15789         }
15790       return;
15791     }
15792
15793   /* If the next token is `static_assert' we have a static assertion.  */
15794   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15795     {
15796       cp_parser_static_assert (parser, /*member_p=*/true);
15797       return;
15798     }
15799
15800   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15801     return;
15802
15803   /* Parse the decl-specifier-seq.  */
15804   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15805   cp_parser_decl_specifier_seq (parser,
15806                                 CP_PARSER_FLAGS_OPTIONAL,
15807                                 &decl_specifiers,
15808                                 &declares_class_or_enum);
15809   prefix_attributes = decl_specifiers.attributes;
15810   decl_specifiers.attributes = NULL_TREE;
15811   /* Check for an invalid type-name.  */
15812   if (!decl_specifiers.type
15813       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15814     return;
15815   /* If there is no declarator, then the decl-specifier-seq should
15816      specify a type.  */
15817   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15818     {
15819       /* If there was no decl-specifier-seq, and the next token is a
15820          `;', then we have something like:
15821
15822            struct S { ; };
15823
15824          [class.mem]
15825
15826          Each member-declaration shall declare at least one member
15827          name of the class.  */
15828       if (!decl_specifiers.any_specifiers_p)
15829         {
15830           cp_token *token = cp_lexer_peek_token (parser->lexer);
15831           if (!in_system_header_at (token->location))
15832             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15833         }
15834       else
15835         {
15836           tree type;
15837
15838           /* See if this declaration is a friend.  */
15839           friend_p = cp_parser_friend_p (&decl_specifiers);
15840           /* If there were decl-specifiers, check to see if there was
15841              a class-declaration.  */
15842           type = check_tag_decl (&decl_specifiers);
15843           /* Nested classes have already been added to the class, but
15844              a `friend' needs to be explicitly registered.  */
15845           if (friend_p)
15846             {
15847               /* If the `friend' keyword was present, the friend must
15848                  be introduced with a class-key.  */
15849                if (!declares_class_or_enum)
15850                  error ("%Ha class-key must be used when declaring a friend",
15851                         &decl_spec_token_start->location);
15852                /* In this case:
15853
15854                     template <typename T> struct A {
15855                       friend struct A<T>::B;
15856                     };
15857
15858                   A<T>::B will be represented by a TYPENAME_TYPE, and
15859                   therefore not recognized by check_tag_decl.  */
15860                if (!type
15861                    && decl_specifiers.type
15862                    && TYPE_P (decl_specifiers.type))
15863                  type = decl_specifiers.type;
15864                if (!type || !TYPE_P (type))
15865                  error ("%Hfriend declaration does not name a class or "
15866                         "function", &decl_spec_token_start->location);
15867                else
15868                  make_friend_class (current_class_type, type,
15869                                     /*complain=*/true);
15870             }
15871           /* If there is no TYPE, an error message will already have
15872              been issued.  */
15873           else if (!type || type == error_mark_node)
15874             ;
15875           /* An anonymous aggregate has to be handled specially; such
15876              a declaration really declares a data member (with a
15877              particular type), as opposed to a nested class.  */
15878           else if (ANON_AGGR_TYPE_P (type))
15879             {
15880               /* Remove constructors and such from TYPE, now that we
15881                  know it is an anonymous aggregate.  */
15882               fixup_anonymous_aggr (type);
15883               /* And make the corresponding data member.  */
15884               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15885               /* Add it to the class.  */
15886               finish_member_declaration (decl);
15887             }
15888           else
15889             cp_parser_check_access_in_redeclaration
15890                                               (TYPE_NAME (type),
15891                                                decl_spec_token_start->location);
15892         }
15893     }
15894   else
15895     {
15896       /* See if these declarations will be friends.  */
15897       friend_p = cp_parser_friend_p (&decl_specifiers);
15898
15899       /* Keep going until we hit the `;' at the end of the
15900          declaration.  */
15901       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15902         {
15903           tree attributes = NULL_TREE;
15904           tree first_attribute;
15905
15906           /* Peek at the next token.  */
15907           token = cp_lexer_peek_token (parser->lexer);
15908
15909           /* Check for a bitfield declaration.  */
15910           if (token->type == CPP_COLON
15911               || (token->type == CPP_NAME
15912                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15913                   == CPP_COLON))
15914             {
15915               tree identifier;
15916               tree width;
15917
15918               /* Get the name of the bitfield.  Note that we cannot just
15919                  check TOKEN here because it may have been invalidated by
15920                  the call to cp_lexer_peek_nth_token above.  */
15921               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15922                 identifier = cp_parser_identifier (parser);
15923               else
15924                 identifier = NULL_TREE;
15925
15926               /* Consume the `:' token.  */
15927               cp_lexer_consume_token (parser->lexer);
15928               /* Get the width of the bitfield.  */
15929               width
15930                 = cp_parser_constant_expression (parser,
15931                                                  /*allow_non_constant=*/false,
15932                                                  NULL);
15933
15934               /* Look for attributes that apply to the bitfield.  */
15935               attributes = cp_parser_attributes_opt (parser);
15936               /* Remember which attributes are prefix attributes and
15937                  which are not.  */
15938               first_attribute = attributes;
15939               /* Combine the attributes.  */
15940               attributes = chainon (prefix_attributes, attributes);
15941
15942               /* Create the bitfield declaration.  */
15943               decl = grokbitfield (identifier
15944                                    ? make_id_declarator (NULL_TREE,
15945                                                          identifier,
15946                                                          sfk_none)
15947                                    : NULL,
15948                                    &decl_specifiers,
15949                                    width,
15950                                    attributes);
15951             }
15952           else
15953             {
15954               cp_declarator *declarator;
15955               tree initializer;
15956               tree asm_specification;
15957               int ctor_dtor_or_conv_p;
15958
15959               /* Parse the declarator.  */
15960               declarator
15961                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15962                                         &ctor_dtor_or_conv_p,
15963                                         /*parenthesized_p=*/NULL,
15964                                         /*member_p=*/true);
15965
15966               /* If something went wrong parsing the declarator, make sure
15967                  that we at least consume some tokens.  */
15968               if (declarator == cp_error_declarator)
15969                 {
15970                   /* Skip to the end of the statement.  */
15971                   cp_parser_skip_to_end_of_statement (parser);
15972                   /* If the next token is not a semicolon, that is
15973                      probably because we just skipped over the body of
15974                      a function.  So, we consume a semicolon if
15975                      present, but do not issue an error message if it
15976                      is not present.  */
15977                   if (cp_lexer_next_token_is (parser->lexer,
15978                                               CPP_SEMICOLON))
15979                     cp_lexer_consume_token (parser->lexer);
15980                   return;
15981                 }
15982
15983               if (declares_class_or_enum & 2)
15984                 cp_parser_check_for_definition_in_return_type
15985                                             (declarator, decl_specifiers.type,
15986                                              decl_specifiers.type_location);
15987
15988               /* Look for an asm-specification.  */
15989               asm_specification = cp_parser_asm_specification_opt (parser);
15990               /* Look for attributes that apply to the declaration.  */
15991               attributes = cp_parser_attributes_opt (parser);
15992               /* Remember which attributes are prefix attributes and
15993                  which are not.  */
15994               first_attribute = attributes;
15995               /* Combine the attributes.  */
15996               attributes = chainon (prefix_attributes, attributes);
15997
15998               /* If it's an `=', then we have a constant-initializer or a
15999                  pure-specifier.  It is not correct to parse the
16000                  initializer before registering the member declaration
16001                  since the member declaration should be in scope while
16002                  its initializer is processed.  However, the rest of the
16003                  front end does not yet provide an interface that allows
16004                  us to handle this correctly.  */
16005               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16006                 {
16007                   /* In [class.mem]:
16008
16009                      A pure-specifier shall be used only in the declaration of
16010                      a virtual function.
16011
16012                      A member-declarator can contain a constant-initializer
16013                      only if it declares a static member of integral or
16014                      enumeration type.
16015
16016                      Therefore, if the DECLARATOR is for a function, we look
16017                      for a pure-specifier; otherwise, we look for a
16018                      constant-initializer.  When we call `grokfield', it will
16019                      perform more stringent semantics checks.  */
16020                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16021                   if (function_declarator_p (declarator))
16022                     initializer = cp_parser_pure_specifier (parser);
16023                   else
16024                     /* Parse the initializer.  */
16025                     initializer = cp_parser_constant_initializer (parser);
16026                 }
16027               /* Otherwise, there is no initializer.  */
16028               else
16029                 initializer = NULL_TREE;
16030
16031               /* See if we are probably looking at a function
16032                  definition.  We are certainly not looking at a
16033                  member-declarator.  Calling `grokfield' has
16034                  side-effects, so we must not do it unless we are sure
16035                  that we are looking at a member-declarator.  */
16036               if (cp_parser_token_starts_function_definition_p
16037                   (cp_lexer_peek_token (parser->lexer)))
16038                 {
16039                   /* The grammar does not allow a pure-specifier to be
16040                      used when a member function is defined.  (It is
16041                      possible that this fact is an oversight in the
16042                      standard, since a pure function may be defined
16043                      outside of the class-specifier.  */
16044                   if (initializer)
16045                     error ("%Hpure-specifier on function-definition",
16046                            &initializer_token_start->location);
16047                   decl = cp_parser_save_member_function_body (parser,
16048                                                               &decl_specifiers,
16049                                                               declarator,
16050                                                               attributes);
16051                   /* If the member was not a friend, declare it here.  */
16052                   if (!friend_p)
16053                     finish_member_declaration (decl);
16054                   /* Peek at the next token.  */
16055                   token = cp_lexer_peek_token (parser->lexer);
16056                   /* If the next token is a semicolon, consume it.  */
16057                   if (token->type == CPP_SEMICOLON)
16058                     cp_lexer_consume_token (parser->lexer);
16059                   return;
16060                 }
16061               else
16062                 if (declarator->kind == cdk_function)
16063                   declarator->id_loc = token->location;
16064                 /* Create the declaration.  */
16065                 decl = grokfield (declarator, &decl_specifiers,
16066                                   initializer, /*init_const_expr_p=*/true,
16067                                   asm_specification,
16068                                   attributes);
16069             }
16070
16071           /* Reset PREFIX_ATTRIBUTES.  */
16072           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16073             attributes = TREE_CHAIN (attributes);
16074           if (attributes)
16075             TREE_CHAIN (attributes) = NULL_TREE;
16076
16077           /* If there is any qualification still in effect, clear it
16078              now; we will be starting fresh with the next declarator.  */
16079           parser->scope = NULL_TREE;
16080           parser->qualifying_scope = NULL_TREE;
16081           parser->object_scope = NULL_TREE;
16082           /* If it's a `,', then there are more declarators.  */
16083           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16084             cp_lexer_consume_token (parser->lexer);
16085           /* If the next token isn't a `;', then we have a parse error.  */
16086           else if (cp_lexer_next_token_is_not (parser->lexer,
16087                                                CPP_SEMICOLON))
16088             {
16089               cp_parser_error (parser, "expected %<;%>");
16090               /* Skip tokens until we find a `;'.  */
16091               cp_parser_skip_to_end_of_statement (parser);
16092
16093               break;
16094             }
16095
16096           if (decl)
16097             {
16098               /* Add DECL to the list of members.  */
16099               if (!friend_p)
16100                 finish_member_declaration (decl);
16101
16102               if (TREE_CODE (decl) == FUNCTION_DECL)
16103                 cp_parser_save_default_args (parser, decl);
16104             }
16105         }
16106     }
16107
16108   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16109 }
16110
16111 /* Parse a pure-specifier.
16112
16113    pure-specifier:
16114      = 0
16115
16116    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16117    Otherwise, ERROR_MARK_NODE is returned.  */
16118
16119 static tree
16120 cp_parser_pure_specifier (cp_parser* parser)
16121 {
16122   cp_token *token;
16123
16124   /* Look for the `=' token.  */
16125   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16126     return error_mark_node;
16127   /* Look for the `0' token.  */
16128   token = cp_lexer_peek_token (parser->lexer);
16129
16130   if (token->type == CPP_EOF
16131       || token->type == CPP_PRAGMA_EOL)
16132     return error_mark_node;
16133
16134   cp_lexer_consume_token (parser->lexer);
16135
16136   /* Accept = default or = delete in c++0x mode.  */
16137   if (token->keyword == RID_DEFAULT
16138       || token->keyword == RID_DELETE)
16139     {
16140       maybe_warn_cpp0x ("defaulted and deleted functions");
16141       return token->u.value;
16142     }
16143
16144   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16145   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16146     {
16147       cp_parser_error (parser,
16148                        "invalid pure specifier (only %<= 0%> is allowed)");
16149       cp_parser_skip_to_end_of_statement (parser);
16150       return error_mark_node;
16151     }
16152   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16153     {
16154       error ("%Htemplates may not be %<virtual%>", &token->location);
16155       return error_mark_node;
16156     }
16157
16158   return integer_zero_node;
16159 }
16160
16161 /* Parse a constant-initializer.
16162
16163    constant-initializer:
16164      = constant-expression
16165
16166    Returns a representation of the constant-expression.  */
16167
16168 static tree
16169 cp_parser_constant_initializer (cp_parser* parser)
16170 {
16171   /* Look for the `=' token.  */
16172   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16173     return error_mark_node;
16174
16175   /* It is invalid to write:
16176
16177        struct S { static const int i = { 7 }; };
16178
16179      */
16180   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16181     {
16182       cp_parser_error (parser,
16183                        "a brace-enclosed initializer is not allowed here");
16184       /* Consume the opening brace.  */
16185       cp_lexer_consume_token (parser->lexer);
16186       /* Skip the initializer.  */
16187       cp_parser_skip_to_closing_brace (parser);
16188       /* Look for the trailing `}'.  */
16189       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16190
16191       return error_mark_node;
16192     }
16193
16194   return cp_parser_constant_expression (parser,
16195                                         /*allow_non_constant=*/false,
16196                                         NULL);
16197 }
16198
16199 /* Derived classes [gram.class.derived] */
16200
16201 /* Parse a base-clause.
16202
16203    base-clause:
16204      : base-specifier-list
16205
16206    base-specifier-list:
16207      base-specifier ... [opt]
16208      base-specifier-list , base-specifier ... [opt]
16209
16210    Returns a TREE_LIST representing the base-classes, in the order in
16211    which they were declared.  The representation of each node is as
16212    described by cp_parser_base_specifier.
16213
16214    In the case that no bases are specified, this function will return
16215    NULL_TREE, not ERROR_MARK_NODE.  */
16216
16217 static tree
16218 cp_parser_base_clause (cp_parser* parser)
16219 {
16220   tree bases = NULL_TREE;
16221
16222   /* Look for the `:' that begins the list.  */
16223   cp_parser_require (parser, CPP_COLON, "%<:%>");
16224
16225   /* Scan the base-specifier-list.  */
16226   while (true)
16227     {
16228       cp_token *token;
16229       tree base;
16230       bool pack_expansion_p = false;
16231
16232       /* Look for the base-specifier.  */
16233       base = cp_parser_base_specifier (parser);
16234       /* Look for the (optional) ellipsis. */
16235       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16236         {
16237           /* Consume the `...'. */
16238           cp_lexer_consume_token (parser->lexer);
16239
16240           pack_expansion_p = true;
16241         }
16242
16243       /* Add BASE to the front of the list.  */
16244       if (base != error_mark_node)
16245         {
16246           if (pack_expansion_p)
16247             /* Make this a pack expansion type. */
16248             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16249           
16250
16251           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16252             {
16253               TREE_CHAIN (base) = bases;
16254               bases = base;
16255             }
16256         }
16257       /* Peek at the next token.  */
16258       token = cp_lexer_peek_token (parser->lexer);
16259       /* If it's not a comma, then the list is complete.  */
16260       if (token->type != CPP_COMMA)
16261         break;
16262       /* Consume the `,'.  */
16263       cp_lexer_consume_token (parser->lexer);
16264     }
16265
16266   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16267      base class had a qualified name.  However, the next name that
16268      appears is certainly not qualified.  */
16269   parser->scope = NULL_TREE;
16270   parser->qualifying_scope = NULL_TREE;
16271   parser->object_scope = NULL_TREE;
16272
16273   return nreverse (bases);
16274 }
16275
16276 /* Parse a base-specifier.
16277
16278    base-specifier:
16279      :: [opt] nested-name-specifier [opt] class-name
16280      virtual access-specifier [opt] :: [opt] nested-name-specifier
16281        [opt] class-name
16282      access-specifier virtual [opt] :: [opt] nested-name-specifier
16283        [opt] class-name
16284
16285    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16286    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16287    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16288    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16289
16290 static tree
16291 cp_parser_base_specifier (cp_parser* parser)
16292 {
16293   cp_token *token;
16294   bool done = false;
16295   bool virtual_p = false;
16296   bool duplicate_virtual_error_issued_p = false;
16297   bool duplicate_access_error_issued_p = false;
16298   bool class_scope_p, template_p;
16299   tree access = access_default_node;
16300   tree type;
16301
16302   /* Process the optional `virtual' and `access-specifier'.  */
16303   while (!done)
16304     {
16305       /* Peek at the next token.  */
16306       token = cp_lexer_peek_token (parser->lexer);
16307       /* Process `virtual'.  */
16308       switch (token->keyword)
16309         {
16310         case RID_VIRTUAL:
16311           /* If `virtual' appears more than once, issue an error.  */
16312           if (virtual_p && !duplicate_virtual_error_issued_p)
16313             {
16314               cp_parser_error (parser,
16315                                "%<virtual%> specified more than once in base-specified");
16316               duplicate_virtual_error_issued_p = true;
16317             }
16318
16319           virtual_p = true;
16320
16321           /* Consume the `virtual' token.  */
16322           cp_lexer_consume_token (parser->lexer);
16323
16324           break;
16325
16326         case RID_PUBLIC:
16327         case RID_PROTECTED:
16328         case RID_PRIVATE:
16329           /* If more than one access specifier appears, issue an
16330              error.  */
16331           if (access != access_default_node
16332               && !duplicate_access_error_issued_p)
16333             {
16334               cp_parser_error (parser,
16335                                "more than one access specifier in base-specified");
16336               duplicate_access_error_issued_p = true;
16337             }
16338
16339           access = ridpointers[(int) token->keyword];
16340
16341           /* Consume the access-specifier.  */
16342           cp_lexer_consume_token (parser->lexer);
16343
16344           break;
16345
16346         default:
16347           done = true;
16348           break;
16349         }
16350     }
16351   /* It is not uncommon to see programs mechanically, erroneously, use
16352      the 'typename' keyword to denote (dependent) qualified types
16353      as base classes.  */
16354   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16355     {
16356       token = cp_lexer_peek_token (parser->lexer);
16357       if (!processing_template_decl)
16358         error ("%Hkeyword %<typename%> not allowed outside of templates",
16359                &token->location);
16360       else
16361         error ("%Hkeyword %<typename%> not allowed in this context "
16362                "(the base class is implicitly a type)",
16363                &token->location);
16364       cp_lexer_consume_token (parser->lexer);
16365     }
16366
16367   /* Look for the optional `::' operator.  */
16368   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16369   /* Look for the nested-name-specifier.  The simplest way to
16370      implement:
16371
16372        [temp.res]
16373
16374        The keyword `typename' is not permitted in a base-specifier or
16375        mem-initializer; in these contexts a qualified name that
16376        depends on a template-parameter is implicitly assumed to be a
16377        type name.
16378
16379      is to pretend that we have seen the `typename' keyword at this
16380      point.  */
16381   cp_parser_nested_name_specifier_opt (parser,
16382                                        /*typename_keyword_p=*/true,
16383                                        /*check_dependency_p=*/true,
16384                                        typename_type,
16385                                        /*is_declaration=*/true);
16386   /* If the base class is given by a qualified name, assume that names
16387      we see are type names or templates, as appropriate.  */
16388   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16389   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16390
16391   /* Finally, look for the class-name.  */
16392   type = cp_parser_class_name (parser,
16393                                class_scope_p,
16394                                template_p,
16395                                typename_type,
16396                                /*check_dependency_p=*/true,
16397                                /*class_head_p=*/false,
16398                                /*is_declaration=*/true);
16399
16400   if (type == error_mark_node)
16401     return error_mark_node;
16402
16403   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16404 }
16405
16406 /* Exception handling [gram.exception] */
16407
16408 /* Parse an (optional) exception-specification.
16409
16410    exception-specification:
16411      throw ( type-id-list [opt] )
16412
16413    Returns a TREE_LIST representing the exception-specification.  The
16414    TREE_VALUE of each node is a type.  */
16415
16416 static tree
16417 cp_parser_exception_specification_opt (cp_parser* parser)
16418 {
16419   cp_token *token;
16420   tree type_id_list;
16421
16422   /* Peek at the next token.  */
16423   token = cp_lexer_peek_token (parser->lexer);
16424   /* If it's not `throw', then there's no exception-specification.  */
16425   if (!cp_parser_is_keyword (token, RID_THROW))
16426     return NULL_TREE;
16427
16428   /* Consume the `throw'.  */
16429   cp_lexer_consume_token (parser->lexer);
16430
16431   /* Look for the `('.  */
16432   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16433
16434   /* Peek at the next token.  */
16435   token = cp_lexer_peek_token (parser->lexer);
16436   /* If it's not a `)', then there is a type-id-list.  */
16437   if (token->type != CPP_CLOSE_PAREN)
16438     {
16439       const char *saved_message;
16440
16441       /* Types may not be defined in an exception-specification.  */
16442       saved_message = parser->type_definition_forbidden_message;
16443       parser->type_definition_forbidden_message
16444         = "types may not be defined in an exception-specification";
16445       /* Parse the type-id-list.  */
16446       type_id_list = cp_parser_type_id_list (parser);
16447       /* Restore the saved message.  */
16448       parser->type_definition_forbidden_message = saved_message;
16449     }
16450   else
16451     type_id_list = empty_except_spec;
16452
16453   /* Look for the `)'.  */
16454   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16455
16456   return type_id_list;
16457 }
16458
16459 /* Parse an (optional) type-id-list.
16460
16461    type-id-list:
16462      type-id ... [opt]
16463      type-id-list , type-id ... [opt]
16464
16465    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16466    in the order that the types were presented.  */
16467
16468 static tree
16469 cp_parser_type_id_list (cp_parser* parser)
16470 {
16471   tree types = NULL_TREE;
16472
16473   while (true)
16474     {
16475       cp_token *token;
16476       tree type;
16477
16478       /* Get the next type-id.  */
16479       type = cp_parser_type_id (parser);
16480       /* Parse the optional ellipsis. */
16481       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16482         {
16483           /* Consume the `...'. */
16484           cp_lexer_consume_token (parser->lexer);
16485
16486           /* Turn the type into a pack expansion expression. */
16487           type = make_pack_expansion (type);
16488         }
16489       /* Add it to the list.  */
16490       types = add_exception_specifier (types, type, /*complain=*/1);
16491       /* Peek at the next token.  */
16492       token = cp_lexer_peek_token (parser->lexer);
16493       /* If it is not a `,', we are done.  */
16494       if (token->type != CPP_COMMA)
16495         break;
16496       /* Consume the `,'.  */
16497       cp_lexer_consume_token (parser->lexer);
16498     }
16499
16500   return nreverse (types);
16501 }
16502
16503 /* Parse a try-block.
16504
16505    try-block:
16506      try compound-statement handler-seq  */
16507
16508 static tree
16509 cp_parser_try_block (cp_parser* parser)
16510 {
16511   tree try_block;
16512
16513   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16514   try_block = begin_try_block ();
16515   cp_parser_compound_statement (parser, NULL, true);
16516   finish_try_block (try_block);
16517   cp_parser_handler_seq (parser);
16518   finish_handler_sequence (try_block);
16519
16520   return try_block;
16521 }
16522
16523 /* Parse a function-try-block.
16524
16525    function-try-block:
16526      try ctor-initializer [opt] function-body handler-seq  */
16527
16528 static bool
16529 cp_parser_function_try_block (cp_parser* parser)
16530 {
16531   tree compound_stmt;
16532   tree try_block;
16533   bool ctor_initializer_p;
16534
16535   /* Look for the `try' keyword.  */
16536   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16537     return false;
16538   /* Let the rest of the front end know where we are.  */
16539   try_block = begin_function_try_block (&compound_stmt);
16540   /* Parse the function-body.  */
16541   ctor_initializer_p
16542     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16543   /* We're done with the `try' part.  */
16544   finish_function_try_block (try_block);
16545   /* Parse the handlers.  */
16546   cp_parser_handler_seq (parser);
16547   /* We're done with the handlers.  */
16548   finish_function_handler_sequence (try_block, compound_stmt);
16549
16550   return ctor_initializer_p;
16551 }
16552
16553 /* Parse a handler-seq.
16554
16555    handler-seq:
16556      handler handler-seq [opt]  */
16557
16558 static void
16559 cp_parser_handler_seq (cp_parser* parser)
16560 {
16561   while (true)
16562     {
16563       cp_token *token;
16564
16565       /* Parse the handler.  */
16566       cp_parser_handler (parser);
16567       /* Peek at the next token.  */
16568       token = cp_lexer_peek_token (parser->lexer);
16569       /* If it's not `catch' then there are no more handlers.  */
16570       if (!cp_parser_is_keyword (token, RID_CATCH))
16571         break;
16572     }
16573 }
16574
16575 /* Parse a handler.
16576
16577    handler:
16578      catch ( exception-declaration ) compound-statement  */
16579
16580 static void
16581 cp_parser_handler (cp_parser* parser)
16582 {
16583   tree handler;
16584   tree declaration;
16585
16586   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16587   handler = begin_handler ();
16588   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16589   declaration = cp_parser_exception_declaration (parser);
16590   finish_handler_parms (declaration, handler);
16591   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16592   cp_parser_compound_statement (parser, NULL, false);
16593   finish_handler (handler);
16594 }
16595
16596 /* Parse an exception-declaration.
16597
16598    exception-declaration:
16599      type-specifier-seq declarator
16600      type-specifier-seq abstract-declarator
16601      type-specifier-seq
16602      ...
16603
16604    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16605    ellipsis variant is used.  */
16606
16607 static tree
16608 cp_parser_exception_declaration (cp_parser* parser)
16609 {
16610   cp_decl_specifier_seq type_specifiers;
16611   cp_declarator *declarator;
16612   const char *saved_message;
16613
16614   /* If it's an ellipsis, it's easy to handle.  */
16615   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16616     {
16617       /* Consume the `...' token.  */
16618       cp_lexer_consume_token (parser->lexer);
16619       return NULL_TREE;
16620     }
16621
16622   /* Types may not be defined in exception-declarations.  */
16623   saved_message = parser->type_definition_forbidden_message;
16624   parser->type_definition_forbidden_message
16625     = "types may not be defined in exception-declarations";
16626
16627   /* Parse the type-specifier-seq.  */
16628   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
16629                                 /*is_trailing_return=*/false,
16630                                 &type_specifiers);
16631   /* If it's a `)', then there is no declarator.  */
16632   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16633     declarator = NULL;
16634   else
16635     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16636                                        /*ctor_dtor_or_conv_p=*/NULL,
16637                                        /*parenthesized_p=*/NULL,
16638                                        /*member_p=*/false);
16639
16640   /* Restore the saved message.  */
16641   parser->type_definition_forbidden_message = saved_message;
16642
16643   if (!type_specifiers.any_specifiers_p)
16644     return error_mark_node;
16645
16646   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16647 }
16648
16649 /* Parse a throw-expression.
16650
16651    throw-expression:
16652      throw assignment-expression [opt]
16653
16654    Returns a THROW_EXPR representing the throw-expression.  */
16655
16656 static tree
16657 cp_parser_throw_expression (cp_parser* parser)
16658 {
16659   tree expression;
16660   cp_token* token;
16661
16662   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16663   token = cp_lexer_peek_token (parser->lexer);
16664   /* Figure out whether or not there is an assignment-expression
16665      following the "throw" keyword.  */
16666   if (token->type == CPP_COMMA
16667       || token->type == CPP_SEMICOLON
16668       || token->type == CPP_CLOSE_PAREN
16669       || token->type == CPP_CLOSE_SQUARE
16670       || token->type == CPP_CLOSE_BRACE
16671       || token->type == CPP_COLON)
16672     expression = NULL_TREE;
16673   else
16674     expression = cp_parser_assignment_expression (parser,
16675                                                   /*cast_p=*/false, NULL);
16676
16677   return build_throw (expression);
16678 }
16679
16680 /* GNU Extensions */
16681
16682 /* Parse an (optional) asm-specification.
16683
16684    asm-specification:
16685      asm ( string-literal )
16686
16687    If the asm-specification is present, returns a STRING_CST
16688    corresponding to the string-literal.  Otherwise, returns
16689    NULL_TREE.  */
16690
16691 static tree
16692 cp_parser_asm_specification_opt (cp_parser* parser)
16693 {
16694   cp_token *token;
16695   tree asm_specification;
16696
16697   /* Peek at the next token.  */
16698   token = cp_lexer_peek_token (parser->lexer);
16699   /* If the next token isn't the `asm' keyword, then there's no
16700      asm-specification.  */
16701   if (!cp_parser_is_keyword (token, RID_ASM))
16702     return NULL_TREE;
16703
16704   /* Consume the `asm' token.  */
16705   cp_lexer_consume_token (parser->lexer);
16706   /* Look for the `('.  */
16707   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16708
16709   /* Look for the string-literal.  */
16710   asm_specification = cp_parser_string_literal (parser, false, false);
16711
16712   /* Look for the `)'.  */
16713   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16714
16715   return asm_specification;
16716 }
16717
16718 /* Parse an asm-operand-list.
16719
16720    asm-operand-list:
16721      asm-operand
16722      asm-operand-list , asm-operand
16723
16724    asm-operand:
16725      string-literal ( expression )
16726      [ string-literal ] string-literal ( expression )
16727
16728    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16729    each node is the expression.  The TREE_PURPOSE is itself a
16730    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16731    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16732    is a STRING_CST for the string literal before the parenthesis. Returns
16733    ERROR_MARK_NODE if any of the operands are invalid.  */
16734
16735 static tree
16736 cp_parser_asm_operand_list (cp_parser* parser)
16737 {
16738   tree asm_operands = NULL_TREE;
16739   bool invalid_operands = false;
16740
16741   while (true)
16742     {
16743       tree string_literal;
16744       tree expression;
16745       tree name;
16746
16747       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16748         {
16749           /* Consume the `[' token.  */
16750           cp_lexer_consume_token (parser->lexer);
16751           /* Read the operand name.  */
16752           name = cp_parser_identifier (parser);
16753           if (name != error_mark_node)
16754             name = build_string (IDENTIFIER_LENGTH (name),
16755                                  IDENTIFIER_POINTER (name));
16756           /* Look for the closing `]'.  */
16757           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16758         }
16759       else
16760         name = NULL_TREE;
16761       /* Look for the string-literal.  */
16762       string_literal = cp_parser_string_literal (parser, false, false);
16763
16764       /* Look for the `('.  */
16765       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16766       /* Parse the expression.  */
16767       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16768       /* Look for the `)'.  */
16769       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16770
16771       if (name == error_mark_node 
16772           || string_literal == error_mark_node 
16773           || expression == error_mark_node)
16774         invalid_operands = true;
16775
16776       /* Add this operand to the list.  */
16777       asm_operands = tree_cons (build_tree_list (name, string_literal),
16778                                 expression,
16779                                 asm_operands);
16780       /* If the next token is not a `,', there are no more
16781          operands.  */
16782       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16783         break;
16784       /* Consume the `,'.  */
16785       cp_lexer_consume_token (parser->lexer);
16786     }
16787
16788   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16789 }
16790
16791 /* Parse an asm-clobber-list.
16792
16793    asm-clobber-list:
16794      string-literal
16795      asm-clobber-list , string-literal
16796
16797    Returns a TREE_LIST, indicating the clobbers in the order that they
16798    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16799
16800 static tree
16801 cp_parser_asm_clobber_list (cp_parser* parser)
16802 {
16803   tree clobbers = NULL_TREE;
16804
16805   while (true)
16806     {
16807       tree string_literal;
16808
16809       /* Look for the string literal.  */
16810       string_literal = cp_parser_string_literal (parser, false, false);
16811       /* Add it to the list.  */
16812       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16813       /* If the next token is not a `,', then the list is
16814          complete.  */
16815       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16816         break;
16817       /* Consume the `,' token.  */
16818       cp_lexer_consume_token (parser->lexer);
16819     }
16820
16821   return clobbers;
16822 }
16823
16824 /* Parse an (optional) series of attributes.
16825
16826    attributes:
16827      attributes attribute
16828
16829    attribute:
16830      __attribute__ (( attribute-list [opt] ))
16831
16832    The return value is as for cp_parser_attribute_list.  */
16833
16834 static tree
16835 cp_parser_attributes_opt (cp_parser* parser)
16836 {
16837   tree attributes = NULL_TREE;
16838
16839   while (true)
16840     {
16841       cp_token *token;
16842       tree attribute_list;
16843
16844       /* Peek at the next token.  */
16845       token = cp_lexer_peek_token (parser->lexer);
16846       /* If it's not `__attribute__', then we're done.  */
16847       if (token->keyword != RID_ATTRIBUTE)
16848         break;
16849
16850       /* Consume the `__attribute__' keyword.  */
16851       cp_lexer_consume_token (parser->lexer);
16852       /* Look for the two `(' tokens.  */
16853       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16854       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16855
16856       /* Peek at the next token.  */
16857       token = cp_lexer_peek_token (parser->lexer);
16858       if (token->type != CPP_CLOSE_PAREN)
16859         /* Parse the attribute-list.  */
16860         attribute_list = cp_parser_attribute_list (parser);
16861       else
16862         /* If the next token is a `)', then there is no attribute
16863            list.  */
16864         attribute_list = NULL;
16865
16866       /* Look for the two `)' tokens.  */
16867       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16868       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16869
16870       /* Add these new attributes to the list.  */
16871       attributes = chainon (attributes, attribute_list);
16872     }
16873
16874   return attributes;
16875 }
16876
16877 /* Parse an attribute-list.
16878
16879    attribute-list:
16880      attribute
16881      attribute-list , attribute
16882
16883    attribute:
16884      identifier
16885      identifier ( identifier )
16886      identifier ( identifier , expression-list )
16887      identifier ( expression-list )
16888
16889    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16890    to an attribute.  The TREE_PURPOSE of each node is the identifier
16891    indicating which attribute is in use.  The TREE_VALUE represents
16892    the arguments, if any.  */
16893
16894 static tree
16895 cp_parser_attribute_list (cp_parser* parser)
16896 {
16897   tree attribute_list = NULL_TREE;
16898   bool save_translate_strings_p = parser->translate_strings_p;
16899
16900   parser->translate_strings_p = false;
16901   while (true)
16902     {
16903       cp_token *token;
16904       tree identifier;
16905       tree attribute;
16906
16907       /* Look for the identifier.  We also allow keywords here; for
16908          example `__attribute__ ((const))' is legal.  */
16909       token = cp_lexer_peek_token (parser->lexer);
16910       if (token->type == CPP_NAME
16911           || token->type == CPP_KEYWORD)
16912         {
16913           tree arguments = NULL_TREE;
16914
16915           /* Consume the token.  */
16916           token = cp_lexer_consume_token (parser->lexer);
16917
16918           /* Save away the identifier that indicates which attribute
16919              this is.  */
16920           identifier = token->u.value;
16921           attribute = build_tree_list (identifier, NULL_TREE);
16922
16923           /* Peek at the next token.  */
16924           token = cp_lexer_peek_token (parser->lexer);
16925           /* If it's an `(', then parse the attribute arguments.  */
16926           if (token->type == CPP_OPEN_PAREN)
16927             {
16928               arguments = cp_parser_parenthesized_expression_list
16929                           (parser, true, /*cast_p=*/false,
16930                            /*allow_expansion_p=*/false,
16931                            /*non_constant_p=*/NULL);
16932               /* Save the arguments away.  */
16933               TREE_VALUE (attribute) = arguments;
16934             }
16935
16936           if (arguments != error_mark_node)
16937             {
16938               /* Add this attribute to the list.  */
16939               TREE_CHAIN (attribute) = attribute_list;
16940               attribute_list = attribute;
16941             }
16942
16943           token = cp_lexer_peek_token (parser->lexer);
16944         }
16945       /* Now, look for more attributes.  If the next token isn't a
16946          `,', we're done.  */
16947       if (token->type != CPP_COMMA)
16948         break;
16949
16950       /* Consume the comma and keep going.  */
16951       cp_lexer_consume_token (parser->lexer);
16952     }
16953   parser->translate_strings_p = save_translate_strings_p;
16954
16955   /* We built up the list in reverse order.  */
16956   return nreverse (attribute_list);
16957 }
16958
16959 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16960    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16961    current value of the PEDANTIC flag, regardless of whether or not
16962    the `__extension__' keyword is present.  The caller is responsible
16963    for restoring the value of the PEDANTIC flag.  */
16964
16965 static bool
16966 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16967 {
16968   /* Save the old value of the PEDANTIC flag.  */
16969   *saved_pedantic = pedantic;
16970
16971   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16972     {
16973       /* Consume the `__extension__' token.  */
16974       cp_lexer_consume_token (parser->lexer);
16975       /* We're not being pedantic while the `__extension__' keyword is
16976          in effect.  */
16977       pedantic = 0;
16978
16979       return true;
16980     }
16981
16982   return false;
16983 }
16984
16985 /* Parse a label declaration.
16986
16987    label-declaration:
16988      __label__ label-declarator-seq ;
16989
16990    label-declarator-seq:
16991      identifier , label-declarator-seq
16992      identifier  */
16993
16994 static void
16995 cp_parser_label_declaration (cp_parser* parser)
16996 {
16997   /* Look for the `__label__' keyword.  */
16998   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16999
17000   while (true)
17001     {
17002       tree identifier;
17003
17004       /* Look for an identifier.  */
17005       identifier = cp_parser_identifier (parser);
17006       /* If we failed, stop.  */
17007       if (identifier == error_mark_node)
17008         break;
17009       /* Declare it as a label.  */
17010       finish_label_decl (identifier);
17011       /* If the next token is a `;', stop.  */
17012       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17013         break;
17014       /* Look for the `,' separating the label declarations.  */
17015       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17016     }
17017
17018   /* Look for the final `;'.  */
17019   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17020 }
17021
17022 /* Support Functions */
17023
17024 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17025    NAME should have one of the representations used for an
17026    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17027    is returned.  If PARSER->SCOPE is a dependent type, then a
17028    SCOPE_REF is returned.
17029
17030    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17031    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17032    was formed.  Abstractly, such entities should not be passed to this
17033    function, because they do not need to be looked up, but it is
17034    simpler to check for this special case here, rather than at the
17035    call-sites.
17036
17037    In cases not explicitly covered above, this function returns a
17038    DECL, OVERLOAD, or baselink representing the result of the lookup.
17039    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17040    is returned.
17041
17042    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17043    (e.g., "struct") that was used.  In that case bindings that do not
17044    refer to types are ignored.
17045
17046    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17047    ignored.
17048
17049    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17050    are ignored.
17051
17052    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17053    types.
17054
17055    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17056    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17057    NULL_TREE otherwise.  */
17058
17059 static tree
17060 cp_parser_lookup_name (cp_parser *parser, tree name,
17061                        enum tag_types tag_type,
17062                        bool is_template,
17063                        bool is_namespace,
17064                        bool check_dependency,
17065                        tree *ambiguous_decls,
17066                        location_t name_location)
17067 {
17068   int flags = 0;
17069   tree decl;
17070   tree object_type = parser->context->object_type;
17071
17072   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17073     flags |= LOOKUP_COMPLAIN;
17074
17075   /* Assume that the lookup will be unambiguous.  */
17076   if (ambiguous_decls)
17077     *ambiguous_decls = NULL_TREE;
17078
17079   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17080      no longer valid.  Note that if we are parsing tentatively, and
17081      the parse fails, OBJECT_TYPE will be automatically restored.  */
17082   parser->context->object_type = NULL_TREE;
17083
17084   if (name == error_mark_node)
17085     return error_mark_node;
17086
17087   /* A template-id has already been resolved; there is no lookup to
17088      do.  */
17089   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17090     return name;
17091   if (BASELINK_P (name))
17092     {
17093       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17094                   == TEMPLATE_ID_EXPR);
17095       return name;
17096     }
17097
17098   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17099      it should already have been checked to make sure that the name
17100      used matches the type being destroyed.  */
17101   if (TREE_CODE (name) == BIT_NOT_EXPR)
17102     {
17103       tree type;
17104
17105       /* Figure out to which type this destructor applies.  */
17106       if (parser->scope)
17107         type = parser->scope;
17108       else if (object_type)
17109         type = object_type;
17110       else
17111         type = current_class_type;
17112       /* If that's not a class type, there is no destructor.  */
17113       if (!type || !CLASS_TYPE_P (type))
17114         return error_mark_node;
17115       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17116         lazily_declare_fn (sfk_destructor, type);
17117       if (!CLASSTYPE_DESTRUCTORS (type))
17118           return error_mark_node;
17119       /* If it was a class type, return the destructor.  */
17120       return CLASSTYPE_DESTRUCTORS (type);
17121     }
17122
17123   /* By this point, the NAME should be an ordinary identifier.  If
17124      the id-expression was a qualified name, the qualifying scope is
17125      stored in PARSER->SCOPE at this point.  */
17126   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17127
17128   /* Perform the lookup.  */
17129   if (parser->scope)
17130     {
17131       bool dependent_p;
17132
17133       if (parser->scope == error_mark_node)
17134         return error_mark_node;
17135
17136       /* If the SCOPE is dependent, the lookup must be deferred until
17137          the template is instantiated -- unless we are explicitly
17138          looking up names in uninstantiated templates.  Even then, we
17139          cannot look up the name if the scope is not a class type; it
17140          might, for example, be a template type parameter.  */
17141       dependent_p = (TYPE_P (parser->scope)
17142                      && dependent_scope_p (parser->scope));
17143       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17144           && dependent_p)
17145         /* Defer lookup.  */
17146         decl = error_mark_node;
17147       else
17148         {
17149           tree pushed_scope = NULL_TREE;
17150
17151           /* If PARSER->SCOPE is a dependent type, then it must be a
17152              class type, and we must not be checking dependencies;
17153              otherwise, we would have processed this lookup above.  So
17154              that PARSER->SCOPE is not considered a dependent base by
17155              lookup_member, we must enter the scope here.  */
17156           if (dependent_p)
17157             pushed_scope = push_scope (parser->scope);
17158           /* If the PARSER->SCOPE is a template specialization, it
17159              may be instantiated during name lookup.  In that case,
17160              errors may be issued.  Even if we rollback the current
17161              tentative parse, those errors are valid.  */
17162           decl = lookup_qualified_name (parser->scope, name,
17163                                         tag_type != none_type,
17164                                         /*complain=*/true);
17165
17166           /* If we have a single function from a using decl, pull it out.  */
17167           if (TREE_CODE (decl) == OVERLOAD
17168               && !really_overloaded_fn (decl))
17169             decl = OVL_FUNCTION (decl);
17170
17171           if (pushed_scope)
17172             pop_scope (pushed_scope);
17173         }
17174
17175       /* If the scope is a dependent type and either we deferred lookup or
17176          we did lookup but didn't find the name, rememeber the name.  */
17177       if (decl == error_mark_node && TYPE_P (parser->scope)
17178           && dependent_type_p (parser->scope))
17179         {
17180           if (tag_type)
17181             {
17182               tree type;
17183
17184               /* The resolution to Core Issue 180 says that `struct
17185                  A::B' should be considered a type-name, even if `A'
17186                  is dependent.  */
17187               type = make_typename_type (parser->scope, name, tag_type,
17188                                          /*complain=*/tf_error);
17189               decl = TYPE_NAME (type);
17190             }
17191           else if (is_template
17192                    && (cp_parser_next_token_ends_template_argument_p (parser)
17193                        || cp_lexer_next_token_is (parser->lexer,
17194                                                   CPP_CLOSE_PAREN)))
17195             decl = make_unbound_class_template (parser->scope,
17196                                                 name, NULL_TREE,
17197                                                 /*complain=*/tf_error);
17198           else
17199             decl = build_qualified_name (/*type=*/NULL_TREE,
17200                                          parser->scope, name,
17201                                          is_template);
17202         }
17203       parser->qualifying_scope = parser->scope;
17204       parser->object_scope = NULL_TREE;
17205     }
17206   else if (object_type)
17207     {
17208       tree object_decl = NULL_TREE;
17209       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17210          OBJECT_TYPE is not a class.  */
17211       if (CLASS_TYPE_P (object_type))
17212         /* If the OBJECT_TYPE is a template specialization, it may
17213            be instantiated during name lookup.  In that case, errors
17214            may be issued.  Even if we rollback the current tentative
17215            parse, those errors are valid.  */
17216         object_decl = lookup_member (object_type,
17217                                      name,
17218                                      /*protect=*/0,
17219                                      tag_type != none_type);
17220       /* Look it up in the enclosing context, too.  */
17221       decl = lookup_name_real (name, tag_type != none_type,
17222                                /*nonclass=*/0,
17223                                /*block_p=*/true, is_namespace, flags);
17224       parser->object_scope = object_type;
17225       parser->qualifying_scope = NULL_TREE;
17226       if (object_decl)
17227         decl = object_decl;
17228     }
17229   else
17230     {
17231       decl = lookup_name_real (name, tag_type != none_type,
17232                                /*nonclass=*/0,
17233                                /*block_p=*/true, is_namespace, flags);
17234       parser->qualifying_scope = NULL_TREE;
17235       parser->object_scope = NULL_TREE;
17236     }
17237
17238   /* If the lookup failed, let our caller know.  */
17239   if (!decl || decl == error_mark_node)
17240     return error_mark_node;
17241
17242   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17243   if (TREE_CODE (decl) == TREE_LIST)
17244     {
17245       if (ambiguous_decls)
17246         *ambiguous_decls = decl;
17247       /* The error message we have to print is too complicated for
17248          cp_parser_error, so we incorporate its actions directly.  */
17249       if (!cp_parser_simulate_error (parser))
17250         {
17251           error ("%Hreference to %qD is ambiguous",
17252                  &name_location, name);
17253           print_candidates (decl);
17254         }
17255       return error_mark_node;
17256     }
17257
17258   gcc_assert (DECL_P (decl)
17259               || TREE_CODE (decl) == OVERLOAD
17260               || TREE_CODE (decl) == SCOPE_REF
17261               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17262               || BASELINK_P (decl));
17263
17264   /* If we have resolved the name of a member declaration, check to
17265      see if the declaration is accessible.  When the name resolves to
17266      set of overloaded functions, accessibility is checked when
17267      overload resolution is done.
17268
17269      During an explicit instantiation, access is not checked at all,
17270      as per [temp.explicit].  */
17271   if (DECL_P (decl))
17272     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17273
17274   return decl;
17275 }
17276
17277 /* Like cp_parser_lookup_name, but for use in the typical case where
17278    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17279    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17280
17281 static tree
17282 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17283 {
17284   return cp_parser_lookup_name (parser, name,
17285                                 none_type,
17286                                 /*is_template=*/false,
17287                                 /*is_namespace=*/false,
17288                                 /*check_dependency=*/true,
17289                                 /*ambiguous_decls=*/NULL,
17290                                 location);
17291 }
17292
17293 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17294    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17295    true, the DECL indicates the class being defined in a class-head,
17296    or declared in an elaborated-type-specifier.
17297
17298    Otherwise, return DECL.  */
17299
17300 static tree
17301 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17302 {
17303   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17304      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17305
17306        struct A {
17307          template <typename T> struct B;
17308        };
17309
17310        template <typename T> struct A::B {};
17311
17312      Similarly, in an elaborated-type-specifier:
17313
17314        namespace N { struct X{}; }
17315
17316        struct A {
17317          template <typename T> friend struct N::X;
17318        };
17319
17320      However, if the DECL refers to a class type, and we are in
17321      the scope of the class, then the name lookup automatically
17322      finds the TYPE_DECL created by build_self_reference rather
17323      than a TEMPLATE_DECL.  For example, in:
17324
17325        template <class T> struct S {
17326          S s;
17327        };
17328
17329      there is no need to handle such case.  */
17330
17331   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17332     return DECL_TEMPLATE_RESULT (decl);
17333
17334   return decl;
17335 }
17336
17337 /* If too many, or too few, template-parameter lists apply to the
17338    declarator, issue an error message.  Returns TRUE if all went well,
17339    and FALSE otherwise.  */
17340
17341 static bool
17342 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17343                                                 cp_declarator *declarator,
17344                                                 location_t declarator_location)
17345 {
17346   unsigned num_templates;
17347
17348   /* We haven't seen any classes that involve template parameters yet.  */
17349   num_templates = 0;
17350
17351   switch (declarator->kind)
17352     {
17353     case cdk_id:
17354       if (declarator->u.id.qualifying_scope)
17355         {
17356           tree scope;
17357           tree member;
17358
17359           scope = declarator->u.id.qualifying_scope;
17360           member = declarator->u.id.unqualified_name;
17361
17362           while (scope && CLASS_TYPE_P (scope))
17363             {
17364               /* You're supposed to have one `template <...>'
17365                  for every template class, but you don't need one
17366                  for a full specialization.  For example:
17367
17368                  template <class T> struct S{};
17369                  template <> struct S<int> { void f(); };
17370                  void S<int>::f () {}
17371
17372                  is correct; there shouldn't be a `template <>' for
17373                  the definition of `S<int>::f'.  */
17374               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17375                 /* If SCOPE does not have template information of any
17376                    kind, then it is not a template, nor is it nested
17377                    within a template.  */
17378                 break;
17379               if (explicit_class_specialization_p (scope))
17380                 break;
17381               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17382                 ++num_templates;
17383
17384               scope = TYPE_CONTEXT (scope);
17385             }
17386         }
17387       else if (TREE_CODE (declarator->u.id.unqualified_name)
17388                == TEMPLATE_ID_EXPR)
17389         /* If the DECLARATOR has the form `X<y>' then it uses one
17390            additional level of template parameters.  */
17391         ++num_templates;
17392
17393       return cp_parser_check_template_parameters (parser,
17394                                                   num_templates,
17395                                                   declarator_location);
17396
17397     case cdk_function:
17398     case cdk_array:
17399     case cdk_pointer:
17400     case cdk_reference:
17401     case cdk_ptrmem:
17402       return (cp_parser_check_declarator_template_parameters
17403               (parser, declarator->declarator, declarator_location));
17404
17405     case cdk_error:
17406       return true;
17407
17408     default:
17409       gcc_unreachable ();
17410     }
17411   return false;
17412 }
17413
17414 /* NUM_TEMPLATES were used in the current declaration.  If that is
17415    invalid, return FALSE and issue an error messages.  Otherwise,
17416    return TRUE.  */
17417
17418 static bool
17419 cp_parser_check_template_parameters (cp_parser* parser,
17420                                      unsigned num_templates,
17421                                      location_t location)
17422 {
17423   /* If there are more template classes than parameter lists, we have
17424      something like:
17425
17426        template <class T> void S<T>::R<T>::f ();  */
17427   if (parser->num_template_parameter_lists < num_templates)
17428     {
17429       error ("%Htoo few template-parameter-lists", &location);
17430       return false;
17431     }
17432   /* If there are the same number of template classes and parameter
17433      lists, that's OK.  */
17434   if (parser->num_template_parameter_lists == num_templates)
17435     return true;
17436   /* If there are more, but only one more, then we are referring to a
17437      member template.  That's OK too.  */
17438   if (parser->num_template_parameter_lists == num_templates + 1)
17439       return true;
17440   /* Otherwise, there are too many template parameter lists.  We have
17441      something like:
17442
17443      template <class T> template <class U> void S::f();  */
17444   error ("%Htoo many template-parameter-lists", &location);
17445   return false;
17446 }
17447
17448 /* Parse an optional `::' token indicating that the following name is
17449    from the global namespace.  If so, PARSER->SCOPE is set to the
17450    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17451    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17452    Returns the new value of PARSER->SCOPE, if the `::' token is
17453    present, and NULL_TREE otherwise.  */
17454
17455 static tree
17456 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17457 {
17458   cp_token *token;
17459
17460   /* Peek at the next token.  */
17461   token = cp_lexer_peek_token (parser->lexer);
17462   /* If we're looking at a `::' token then we're starting from the
17463      global namespace, not our current location.  */
17464   if (token->type == CPP_SCOPE)
17465     {
17466       /* Consume the `::' token.  */
17467       cp_lexer_consume_token (parser->lexer);
17468       /* Set the SCOPE so that we know where to start the lookup.  */
17469       parser->scope = global_namespace;
17470       parser->qualifying_scope = global_namespace;
17471       parser->object_scope = NULL_TREE;
17472
17473       return parser->scope;
17474     }
17475   else if (!current_scope_valid_p)
17476     {
17477       parser->scope = NULL_TREE;
17478       parser->qualifying_scope = NULL_TREE;
17479       parser->object_scope = NULL_TREE;
17480     }
17481
17482   return NULL_TREE;
17483 }
17484
17485 /* Returns TRUE if the upcoming token sequence is the start of a
17486    constructor declarator.  If FRIEND_P is true, the declarator is
17487    preceded by the `friend' specifier.  */
17488
17489 static bool
17490 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17491 {
17492   bool constructor_p;
17493   tree type_decl = NULL_TREE;
17494   bool nested_name_p;
17495   cp_token *next_token;
17496
17497   /* The common case is that this is not a constructor declarator, so
17498      try to avoid doing lots of work if at all possible.  It's not
17499      valid declare a constructor at function scope.  */
17500   if (parser->in_function_body)
17501     return false;
17502   /* And only certain tokens can begin a constructor declarator.  */
17503   next_token = cp_lexer_peek_token (parser->lexer);
17504   if (next_token->type != CPP_NAME
17505       && next_token->type != CPP_SCOPE
17506       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17507       && next_token->type != CPP_TEMPLATE_ID)
17508     return false;
17509
17510   /* Parse tentatively; we are going to roll back all of the tokens
17511      consumed here.  */
17512   cp_parser_parse_tentatively (parser);
17513   /* Assume that we are looking at a constructor declarator.  */
17514   constructor_p = true;
17515
17516   /* Look for the optional `::' operator.  */
17517   cp_parser_global_scope_opt (parser,
17518                               /*current_scope_valid_p=*/false);
17519   /* Look for the nested-name-specifier.  */
17520   nested_name_p
17521     = (cp_parser_nested_name_specifier_opt (parser,
17522                                             /*typename_keyword_p=*/false,
17523                                             /*check_dependency_p=*/false,
17524                                             /*type_p=*/false,
17525                                             /*is_declaration=*/false)
17526        != NULL_TREE);
17527   /* Outside of a class-specifier, there must be a
17528      nested-name-specifier.  */
17529   if (!nested_name_p &&
17530       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17531        || friend_p))
17532     constructor_p = false;
17533   /* If we still think that this might be a constructor-declarator,
17534      look for a class-name.  */
17535   if (constructor_p)
17536     {
17537       /* If we have:
17538
17539            template <typename T> struct S { S(); };
17540            template <typename T> S<T>::S ();
17541
17542          we must recognize that the nested `S' names a class.
17543          Similarly, for:
17544
17545            template <typename T> S<T>::S<T> ();
17546
17547          we must recognize that the nested `S' names a template.  */
17548       type_decl = cp_parser_class_name (parser,
17549                                         /*typename_keyword_p=*/false,
17550                                         /*template_keyword_p=*/false,
17551                                         none_type,
17552                                         /*check_dependency_p=*/false,
17553                                         /*class_head_p=*/false,
17554                                         /*is_declaration=*/false);
17555       /* If there was no class-name, then this is not a constructor.  */
17556       constructor_p = !cp_parser_error_occurred (parser);
17557     }
17558
17559   /* If we're still considering a constructor, we have to see a `(',
17560      to begin the parameter-declaration-clause, followed by either a
17561      `)', an `...', or a decl-specifier.  We need to check for a
17562      type-specifier to avoid being fooled into thinking that:
17563
17564        S::S (f) (int);
17565
17566      is a constructor.  (It is actually a function named `f' that
17567      takes one parameter (of type `int') and returns a value of type
17568      `S::S'.  */
17569   if (constructor_p
17570       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17571     {
17572       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17573           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17574           /* A parameter declaration begins with a decl-specifier,
17575              which is either the "attribute" keyword, a storage class
17576              specifier, or (usually) a type-specifier.  */
17577           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17578         {
17579           tree type;
17580           tree pushed_scope = NULL_TREE;
17581           unsigned saved_num_template_parameter_lists;
17582
17583           /* Names appearing in the type-specifier should be looked up
17584              in the scope of the class.  */
17585           if (current_class_type)
17586             type = NULL_TREE;
17587           else
17588             {
17589               type = TREE_TYPE (type_decl);
17590               if (TREE_CODE (type) == TYPENAME_TYPE)
17591                 {
17592                   type = resolve_typename_type (type,
17593                                                 /*only_current_p=*/false);
17594                   if (TREE_CODE (type) == TYPENAME_TYPE)
17595                     {
17596                       cp_parser_abort_tentative_parse (parser);
17597                       return false;
17598                     }
17599                 }
17600               pushed_scope = push_scope (type);
17601             }
17602
17603           /* Inside the constructor parameter list, surrounding
17604              template-parameter-lists do not apply.  */
17605           saved_num_template_parameter_lists
17606             = parser->num_template_parameter_lists;
17607           parser->num_template_parameter_lists = 0;
17608
17609           /* Look for the type-specifier.  */
17610           cp_parser_type_specifier (parser,
17611                                     CP_PARSER_FLAGS_NONE,
17612                                     /*decl_specs=*/NULL,
17613                                     /*is_declarator=*/true,
17614                                     /*declares_class_or_enum=*/NULL,
17615                                     /*is_cv_qualifier=*/NULL);
17616
17617           parser->num_template_parameter_lists
17618             = saved_num_template_parameter_lists;
17619
17620           /* Leave the scope of the class.  */
17621           if (pushed_scope)
17622             pop_scope (pushed_scope);
17623
17624           constructor_p = !cp_parser_error_occurred (parser);
17625         }
17626     }
17627   else
17628     constructor_p = false;
17629   /* We did not really want to consume any tokens.  */
17630   cp_parser_abort_tentative_parse (parser);
17631
17632   return constructor_p;
17633 }
17634
17635 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17636    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17637    they must be performed once we are in the scope of the function.
17638
17639    Returns the function defined.  */
17640
17641 static tree
17642 cp_parser_function_definition_from_specifiers_and_declarator
17643   (cp_parser* parser,
17644    cp_decl_specifier_seq *decl_specifiers,
17645    tree attributes,
17646    const cp_declarator *declarator)
17647 {
17648   tree fn;
17649   bool success_p;
17650
17651   /* Begin the function-definition.  */
17652   success_p = start_function (decl_specifiers, declarator, attributes);
17653
17654   /* The things we're about to see are not directly qualified by any
17655      template headers we've seen thus far.  */
17656   reset_specialization ();
17657
17658   /* If there were names looked up in the decl-specifier-seq that we
17659      did not check, check them now.  We must wait until we are in the
17660      scope of the function to perform the checks, since the function
17661      might be a friend.  */
17662   perform_deferred_access_checks ();
17663
17664   if (!success_p)
17665     {
17666       /* Skip the entire function.  */
17667       cp_parser_skip_to_end_of_block_or_statement (parser);
17668       fn = error_mark_node;
17669     }
17670   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17671     {
17672       /* Seen already, skip it.  An error message has already been output.  */
17673       cp_parser_skip_to_end_of_block_or_statement (parser);
17674       fn = current_function_decl;
17675       current_function_decl = NULL_TREE;
17676       /* If this is a function from a class, pop the nested class.  */
17677       if (current_class_name)
17678         pop_nested_class ();
17679     }
17680   else
17681     fn = cp_parser_function_definition_after_declarator (parser,
17682                                                          /*inline_p=*/false);
17683
17684   return fn;
17685 }
17686
17687 /* Parse the part of a function-definition that follows the
17688    declarator.  INLINE_P is TRUE iff this function is an inline
17689    function defined with a class-specifier.
17690
17691    Returns the function defined.  */
17692
17693 static tree
17694 cp_parser_function_definition_after_declarator (cp_parser* parser,
17695                                                 bool inline_p)
17696 {
17697   tree fn;
17698   bool ctor_initializer_p = false;
17699   bool saved_in_unbraced_linkage_specification_p;
17700   bool saved_in_function_body;
17701   unsigned saved_num_template_parameter_lists;
17702   cp_token *token;
17703
17704   saved_in_function_body = parser->in_function_body;
17705   parser->in_function_body = true;
17706   /* If the next token is `return', then the code may be trying to
17707      make use of the "named return value" extension that G++ used to
17708      support.  */
17709   token = cp_lexer_peek_token (parser->lexer);
17710   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17711     {
17712       /* Consume the `return' keyword.  */
17713       cp_lexer_consume_token (parser->lexer);
17714       /* Look for the identifier that indicates what value is to be
17715          returned.  */
17716       cp_parser_identifier (parser);
17717       /* Issue an error message.  */
17718       error ("%Hnamed return values are no longer supported",
17719              &token->location);
17720       /* Skip tokens until we reach the start of the function body.  */
17721       while (true)
17722         {
17723           cp_token *token = cp_lexer_peek_token (parser->lexer);
17724           if (token->type == CPP_OPEN_BRACE
17725               || token->type == CPP_EOF
17726               || token->type == CPP_PRAGMA_EOL)
17727             break;
17728           cp_lexer_consume_token (parser->lexer);
17729         }
17730     }
17731   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17732      anything declared inside `f'.  */
17733   saved_in_unbraced_linkage_specification_p
17734     = parser->in_unbraced_linkage_specification_p;
17735   parser->in_unbraced_linkage_specification_p = false;
17736   /* Inside the function, surrounding template-parameter-lists do not
17737      apply.  */
17738   saved_num_template_parameter_lists
17739     = parser->num_template_parameter_lists;
17740   parser->num_template_parameter_lists = 0;
17741   /* If the next token is `try', then we are looking at a
17742      function-try-block.  */
17743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17744     ctor_initializer_p = cp_parser_function_try_block (parser);
17745   /* A function-try-block includes the function-body, so we only do
17746      this next part if we're not processing a function-try-block.  */
17747   else
17748     ctor_initializer_p
17749       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17750
17751   /* Finish the function.  */
17752   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17753                         (inline_p ? 2 : 0));
17754   /* Generate code for it, if necessary.  */
17755   expand_or_defer_fn (fn);
17756   /* Restore the saved values.  */
17757   parser->in_unbraced_linkage_specification_p
17758     = saved_in_unbraced_linkage_specification_p;
17759   parser->num_template_parameter_lists
17760     = saved_num_template_parameter_lists;
17761   parser->in_function_body = saved_in_function_body;
17762
17763   return fn;
17764 }
17765
17766 /* Parse a template-declaration, assuming that the `export' (and
17767    `extern') keywords, if present, has already been scanned.  MEMBER_P
17768    is as for cp_parser_template_declaration.  */
17769
17770 static void
17771 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17772 {
17773   tree decl = NULL_TREE;
17774   VEC (deferred_access_check,gc) *checks;
17775   tree parameter_list;
17776   bool friend_p = false;
17777   bool need_lang_pop;
17778   cp_token *token;
17779
17780   /* Look for the `template' keyword.  */
17781   token = cp_lexer_peek_token (parser->lexer);
17782   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17783     return;
17784
17785   /* And the `<'.  */
17786   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17787     return;
17788   if (at_class_scope_p () && current_function_decl)
17789     {
17790       /* 14.5.2.2 [temp.mem]
17791
17792          A local class shall not have member templates.  */
17793       error ("%Hinvalid declaration of member template in local class",
17794              &token->location);
17795       cp_parser_skip_to_end_of_block_or_statement (parser);
17796       return;
17797     }
17798   /* [temp]
17799
17800      A template ... shall not have C linkage.  */
17801   if (current_lang_name == lang_name_c)
17802     {
17803       error ("%Htemplate with C linkage", &token->location);
17804       /* Give it C++ linkage to avoid confusing other parts of the
17805          front end.  */
17806       push_lang_context (lang_name_cplusplus);
17807       need_lang_pop = true;
17808     }
17809   else
17810     need_lang_pop = false;
17811
17812   /* We cannot perform access checks on the template parameter
17813      declarations until we know what is being declared, just as we
17814      cannot check the decl-specifier list.  */
17815   push_deferring_access_checks (dk_deferred);
17816
17817   /* If the next token is `>', then we have an invalid
17818      specialization.  Rather than complain about an invalid template
17819      parameter, issue an error message here.  */
17820   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17821     {
17822       cp_parser_error (parser, "invalid explicit specialization");
17823       begin_specialization ();
17824       parameter_list = NULL_TREE;
17825     }
17826   else
17827     /* Parse the template parameters.  */
17828     parameter_list = cp_parser_template_parameter_list (parser);
17829
17830   /* Get the deferred access checks from the parameter list.  These
17831      will be checked once we know what is being declared, as for a
17832      member template the checks must be performed in the scope of the
17833      class containing the member.  */
17834   checks = get_deferred_access_checks ();
17835
17836   /* Look for the `>'.  */
17837   cp_parser_skip_to_end_of_template_parameter_list (parser);
17838   /* We just processed one more parameter list.  */
17839   ++parser->num_template_parameter_lists;
17840   /* If the next token is `template', there are more template
17841      parameters.  */
17842   if (cp_lexer_next_token_is_keyword (parser->lexer,
17843                                       RID_TEMPLATE))
17844     cp_parser_template_declaration_after_export (parser, member_p);
17845   else
17846     {
17847       /* There are no access checks when parsing a template, as we do not
17848          know if a specialization will be a friend.  */
17849       push_deferring_access_checks (dk_no_check);
17850       token = cp_lexer_peek_token (parser->lexer);
17851       decl = cp_parser_single_declaration (parser,
17852                                            checks,
17853                                            member_p,
17854                                            /*explicit_specialization_p=*/false,
17855                                            &friend_p);
17856       pop_deferring_access_checks ();
17857
17858       /* If this is a member template declaration, let the front
17859          end know.  */
17860       if (member_p && !friend_p && decl)
17861         {
17862           if (TREE_CODE (decl) == TYPE_DECL)
17863             cp_parser_check_access_in_redeclaration (decl, token->location);
17864
17865           decl = finish_member_template_decl (decl);
17866         }
17867       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17868         make_friend_class (current_class_type, TREE_TYPE (decl),
17869                            /*complain=*/true);
17870     }
17871   /* We are done with the current parameter list.  */
17872   --parser->num_template_parameter_lists;
17873
17874   pop_deferring_access_checks ();
17875
17876   /* Finish up.  */
17877   finish_template_decl (parameter_list);
17878
17879   /* Register member declarations.  */
17880   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17881     finish_member_declaration (decl);
17882   /* For the erroneous case of a template with C linkage, we pushed an
17883      implicit C++ linkage scope; exit that scope now.  */
17884   if (need_lang_pop)
17885     pop_lang_context ();
17886   /* If DECL is a function template, we must return to parse it later.
17887      (Even though there is no definition, there might be default
17888      arguments that need handling.)  */
17889   if (member_p && decl
17890       && (TREE_CODE (decl) == FUNCTION_DECL
17891           || DECL_FUNCTION_TEMPLATE_P (decl)))
17892     TREE_VALUE (parser->unparsed_functions_queues)
17893       = tree_cons (NULL_TREE, decl,
17894                    TREE_VALUE (parser->unparsed_functions_queues));
17895 }
17896
17897 /* Perform the deferred access checks from a template-parameter-list.
17898    CHECKS is a TREE_LIST of access checks, as returned by
17899    get_deferred_access_checks.  */
17900
17901 static void
17902 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17903 {
17904   ++processing_template_parmlist;
17905   perform_access_checks (checks);
17906   --processing_template_parmlist;
17907 }
17908
17909 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17910    `function-definition' sequence.  MEMBER_P is true, this declaration
17911    appears in a class scope.
17912
17913    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17914    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17915
17916 static tree
17917 cp_parser_single_declaration (cp_parser* parser,
17918                               VEC (deferred_access_check,gc)* checks,
17919                               bool member_p,
17920                               bool explicit_specialization_p,
17921                               bool* friend_p)
17922 {
17923   int declares_class_or_enum;
17924   tree decl = NULL_TREE;
17925   cp_decl_specifier_seq decl_specifiers;
17926   bool function_definition_p = false;
17927   cp_token *decl_spec_token_start;
17928
17929   /* This function is only used when processing a template
17930      declaration.  */
17931   gcc_assert (innermost_scope_kind () == sk_template_parms
17932               || innermost_scope_kind () == sk_template_spec);
17933
17934   /* Defer access checks until we know what is being declared.  */
17935   push_deferring_access_checks (dk_deferred);
17936
17937   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17938      alternative.  */
17939   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17940   cp_parser_decl_specifier_seq (parser,
17941                                 CP_PARSER_FLAGS_OPTIONAL,
17942                                 &decl_specifiers,
17943                                 &declares_class_or_enum);
17944   if (friend_p)
17945     *friend_p = cp_parser_friend_p (&decl_specifiers);
17946
17947   /* There are no template typedefs.  */
17948   if (decl_specifiers.specs[(int) ds_typedef])
17949     {
17950       error ("%Htemplate declaration of %qs",
17951              &decl_spec_token_start->location, "typedef");
17952       decl = error_mark_node;
17953     }
17954
17955   /* Gather up the access checks that occurred the
17956      decl-specifier-seq.  */
17957   stop_deferring_access_checks ();
17958
17959   /* Check for the declaration of a template class.  */
17960   if (declares_class_or_enum)
17961     {
17962       if (cp_parser_declares_only_class_p (parser))
17963         {
17964           decl = shadow_tag (&decl_specifiers);
17965
17966           /* In this case:
17967
17968                struct C {
17969                  friend template <typename T> struct A<T>::B;
17970                };
17971
17972              A<T>::B will be represented by a TYPENAME_TYPE, and
17973              therefore not recognized by shadow_tag.  */
17974           if (friend_p && *friend_p
17975               && !decl
17976               && decl_specifiers.type
17977               && TYPE_P (decl_specifiers.type))
17978             decl = decl_specifiers.type;
17979
17980           if (decl && decl != error_mark_node)
17981             decl = TYPE_NAME (decl);
17982           else
17983             decl = error_mark_node;
17984
17985           /* Perform access checks for template parameters.  */
17986           cp_parser_perform_template_parameter_access_checks (checks);
17987         }
17988     }
17989   /* If it's not a template class, try for a template function.  If
17990      the next token is a `;', then this declaration does not declare
17991      anything.  But, if there were errors in the decl-specifiers, then
17992      the error might well have come from an attempted class-specifier.
17993      In that case, there's no need to warn about a missing declarator.  */
17994   if (!decl
17995       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17996           || decl_specifiers.type != error_mark_node))
17997     {
17998       decl = cp_parser_init_declarator (parser,
17999                                         &decl_specifiers,
18000                                         checks,
18001                                         /*function_definition_allowed_p=*/true,
18002                                         member_p,
18003                                         declares_class_or_enum,
18004                                         &function_definition_p);
18005
18006     /* 7.1.1-1 [dcl.stc]
18007
18008        A storage-class-specifier shall not be specified in an explicit
18009        specialization...  */
18010     if (decl
18011         && explicit_specialization_p
18012         && decl_specifiers.storage_class != sc_none)
18013       {
18014         error ("%Hexplicit template specialization cannot have a storage class",
18015                &decl_spec_token_start->location);
18016         decl = error_mark_node;
18017       }
18018     }
18019
18020   pop_deferring_access_checks ();
18021
18022   /* Clear any current qualification; whatever comes next is the start
18023      of something new.  */
18024   parser->scope = NULL_TREE;
18025   parser->qualifying_scope = NULL_TREE;
18026   parser->object_scope = NULL_TREE;
18027   /* Look for a trailing `;' after the declaration.  */
18028   if (!function_definition_p
18029       && (decl == error_mark_node
18030           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18031     cp_parser_skip_to_end_of_block_or_statement (parser);
18032
18033   return decl;
18034 }
18035
18036 /* Parse a cast-expression that is not the operand of a unary "&".  */
18037
18038 static tree
18039 cp_parser_simple_cast_expression (cp_parser *parser)
18040 {
18041   return cp_parser_cast_expression (parser, /*address_p=*/false,
18042                                     /*cast_p=*/false, NULL);
18043 }
18044
18045 /* Parse a functional cast to TYPE.  Returns an expression
18046    representing the cast.  */
18047
18048 static tree
18049 cp_parser_functional_cast (cp_parser* parser, tree type)
18050 {
18051   tree expression_list;
18052   tree cast;
18053   bool nonconst_p;
18054
18055   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18056     {
18057       maybe_warn_cpp0x ("extended initializer lists");
18058       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18059       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18060       if (TREE_CODE (type) == TYPE_DECL)
18061         type = TREE_TYPE (type);
18062       return finish_compound_literal (type, expression_list);
18063     }
18064
18065   expression_list
18066     = cp_parser_parenthesized_expression_list (parser, false,
18067                                                /*cast_p=*/true,
18068                                                /*allow_expansion_p=*/true,
18069                                                /*non_constant_p=*/NULL);
18070
18071   cast = build_functional_cast (type, expression_list,
18072                                 tf_warning_or_error);
18073   /* [expr.const]/1: In an integral constant expression "only type
18074      conversions to integral or enumeration type can be used".  */
18075   if (TREE_CODE (type) == TYPE_DECL)
18076     type = TREE_TYPE (type);
18077   if (cast != error_mark_node
18078       && !cast_valid_in_integral_constant_expression_p (type)
18079       && (cp_parser_non_integral_constant_expression
18080           (parser, "a call to a constructor")))
18081     return error_mark_node;
18082   return cast;
18083 }
18084
18085 /* Save the tokens that make up the body of a member function defined
18086    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18087    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18088    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18089    for the member function.  */
18090
18091 static tree
18092 cp_parser_save_member_function_body (cp_parser* parser,
18093                                      cp_decl_specifier_seq *decl_specifiers,
18094                                      cp_declarator *declarator,
18095                                      tree attributes)
18096 {
18097   cp_token *first;
18098   cp_token *last;
18099   tree fn;
18100
18101   /* Create the function-declaration.  */
18102   fn = start_method (decl_specifiers, declarator, attributes);
18103   /* If something went badly wrong, bail out now.  */
18104   if (fn == error_mark_node)
18105     {
18106       /* If there's a function-body, skip it.  */
18107       if (cp_parser_token_starts_function_definition_p
18108           (cp_lexer_peek_token (parser->lexer)))
18109         cp_parser_skip_to_end_of_block_or_statement (parser);
18110       return error_mark_node;
18111     }
18112
18113   /* Remember it, if there default args to post process.  */
18114   cp_parser_save_default_args (parser, fn);
18115
18116   /* Save away the tokens that make up the body of the
18117      function.  */
18118   first = parser->lexer->next_token;
18119   /* We can have braced-init-list mem-initializers before the fn body.  */
18120   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18121     {
18122       cp_lexer_consume_token (parser->lexer);
18123       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18124              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18125         {
18126           /* cache_group will stop after an un-nested { } pair, too.  */
18127           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18128             break;
18129
18130           /* variadic mem-inits have ... after the ')'.  */
18131           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18132             cp_lexer_consume_token (parser->lexer);
18133         }
18134     }
18135   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18136   /* Handle function try blocks.  */
18137   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18138     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18139   last = parser->lexer->next_token;
18140
18141   /* Save away the inline definition; we will process it when the
18142      class is complete.  */
18143   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18144   DECL_PENDING_INLINE_P (fn) = 1;
18145
18146   /* We need to know that this was defined in the class, so that
18147      friend templates are handled correctly.  */
18148   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18149
18150   /* We're done with the inline definition.  */
18151   finish_method (fn);
18152
18153   /* Add FN to the queue of functions to be parsed later.  */
18154   TREE_VALUE (parser->unparsed_functions_queues)
18155     = tree_cons (NULL_TREE, fn,
18156                  TREE_VALUE (parser->unparsed_functions_queues));
18157
18158   return fn;
18159 }
18160
18161 /* Parse a template-argument-list, as well as the trailing ">" (but
18162    not the opening ">").  See cp_parser_template_argument_list for the
18163    return value.  */
18164
18165 static tree
18166 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18167 {
18168   tree arguments;
18169   tree saved_scope;
18170   tree saved_qualifying_scope;
18171   tree saved_object_scope;
18172   bool saved_greater_than_is_operator_p;
18173   bool saved_skip_evaluation;
18174
18175   /* [temp.names]
18176
18177      When parsing a template-id, the first non-nested `>' is taken as
18178      the end of the template-argument-list rather than a greater-than
18179      operator.  */
18180   saved_greater_than_is_operator_p
18181     = parser->greater_than_is_operator_p;
18182   parser->greater_than_is_operator_p = false;
18183   /* Parsing the argument list may modify SCOPE, so we save it
18184      here.  */
18185   saved_scope = parser->scope;
18186   saved_qualifying_scope = parser->qualifying_scope;
18187   saved_object_scope = parser->object_scope;
18188   /* We need to evaluate the template arguments, even though this
18189      template-id may be nested within a "sizeof".  */
18190   saved_skip_evaluation = skip_evaluation;
18191   skip_evaluation = false;
18192   /* Parse the template-argument-list itself.  */
18193   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18194       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18195     arguments = NULL_TREE;
18196   else
18197     arguments = cp_parser_template_argument_list (parser);
18198   /* Look for the `>' that ends the template-argument-list. If we find
18199      a '>>' instead, it's probably just a typo.  */
18200   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18201     {
18202       if (cxx_dialect != cxx98)
18203         {
18204           /* In C++0x, a `>>' in a template argument list or cast
18205              expression is considered to be two separate `>'
18206              tokens. So, change the current token to a `>', but don't
18207              consume it: it will be consumed later when the outer
18208              template argument list (or cast expression) is parsed.
18209              Note that this replacement of `>' for `>>' is necessary
18210              even if we are parsing tentatively: in the tentative
18211              case, after calling
18212              cp_parser_enclosed_template_argument_list we will always
18213              throw away all of the template arguments and the first
18214              closing `>', either because the template argument list
18215              was erroneous or because we are replacing those tokens
18216              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18217              not have been thrown away) is needed either to close an
18218              outer template argument list or to complete a new-style
18219              cast.  */
18220           cp_token *token = cp_lexer_peek_token (parser->lexer);
18221           token->type = CPP_GREATER;
18222         }
18223       else if (!saved_greater_than_is_operator_p)
18224         {
18225           /* If we're in a nested template argument list, the '>>' has
18226             to be a typo for '> >'. We emit the error message, but we
18227             continue parsing and we push a '>' as next token, so that
18228             the argument list will be parsed correctly.  Note that the
18229             global source location is still on the token before the
18230             '>>', so we need to say explicitly where we want it.  */
18231           cp_token *token = cp_lexer_peek_token (parser->lexer);
18232           error ("%H%<>>%> should be %<> >%> "
18233                  "within a nested template argument list",
18234                  &token->location);
18235
18236           token->type = CPP_GREATER;
18237         }
18238       else
18239         {
18240           /* If this is not a nested template argument list, the '>>'
18241             is a typo for '>'. Emit an error message and continue.
18242             Same deal about the token location, but here we can get it
18243             right by consuming the '>>' before issuing the diagnostic.  */
18244           cp_token *token = cp_lexer_consume_token (parser->lexer);
18245           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18246                  "a template argument list", &token->location);
18247         }
18248     }
18249   else
18250     cp_parser_skip_to_end_of_template_parameter_list (parser);
18251   /* The `>' token might be a greater-than operator again now.  */
18252   parser->greater_than_is_operator_p
18253     = saved_greater_than_is_operator_p;
18254   /* Restore the SAVED_SCOPE.  */
18255   parser->scope = saved_scope;
18256   parser->qualifying_scope = saved_qualifying_scope;
18257   parser->object_scope = saved_object_scope;
18258   skip_evaluation = saved_skip_evaluation;
18259
18260   return arguments;
18261 }
18262
18263 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18264    arguments, or the body of the function have not yet been parsed,
18265    parse them now.  */
18266
18267 static void
18268 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18269 {
18270   /* If this member is a template, get the underlying
18271      FUNCTION_DECL.  */
18272   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18273     member_function = DECL_TEMPLATE_RESULT (member_function);
18274
18275   /* There should not be any class definitions in progress at this
18276      point; the bodies of members are only parsed outside of all class
18277      definitions.  */
18278   gcc_assert (parser->num_classes_being_defined == 0);
18279   /* While we're parsing the member functions we might encounter more
18280      classes.  We want to handle them right away, but we don't want
18281      them getting mixed up with functions that are currently in the
18282      queue.  */
18283   parser->unparsed_functions_queues
18284     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18285
18286   /* Make sure that any template parameters are in scope.  */
18287   maybe_begin_member_template_processing (member_function);
18288
18289   /* If the body of the function has not yet been parsed, parse it
18290      now.  */
18291   if (DECL_PENDING_INLINE_P (member_function))
18292     {
18293       tree function_scope;
18294       cp_token_cache *tokens;
18295
18296       /* The function is no longer pending; we are processing it.  */
18297       tokens = DECL_PENDING_INLINE_INFO (member_function);
18298       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18299       DECL_PENDING_INLINE_P (member_function) = 0;
18300
18301       /* If this is a local class, enter the scope of the containing
18302          function.  */
18303       function_scope = current_function_decl;
18304       if (function_scope)
18305         push_function_context ();
18306
18307       /* Push the body of the function onto the lexer stack.  */
18308       cp_parser_push_lexer_for_tokens (parser, tokens);
18309
18310       /* Let the front end know that we going to be defining this
18311          function.  */
18312       start_preparsed_function (member_function, NULL_TREE,
18313                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18314
18315       /* Don't do access checking if it is a templated function.  */
18316       if (processing_template_decl)
18317         push_deferring_access_checks (dk_no_check);
18318
18319       /* Now, parse the body of the function.  */
18320       cp_parser_function_definition_after_declarator (parser,
18321                                                       /*inline_p=*/true);
18322
18323       if (processing_template_decl)
18324         pop_deferring_access_checks ();
18325
18326       /* Leave the scope of the containing function.  */
18327       if (function_scope)
18328         pop_function_context ();
18329       cp_parser_pop_lexer (parser);
18330     }
18331
18332   /* Remove any template parameters from the symbol table.  */
18333   maybe_end_member_template_processing ();
18334
18335   /* Restore the queue.  */
18336   parser->unparsed_functions_queues
18337     = TREE_CHAIN (parser->unparsed_functions_queues);
18338 }
18339
18340 /* If DECL contains any default args, remember it on the unparsed
18341    functions queue.  */
18342
18343 static void
18344 cp_parser_save_default_args (cp_parser* parser, tree decl)
18345 {
18346   tree probe;
18347
18348   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18349        probe;
18350        probe = TREE_CHAIN (probe))
18351     if (TREE_PURPOSE (probe))
18352       {
18353         TREE_PURPOSE (parser->unparsed_functions_queues)
18354           = tree_cons (current_class_type, decl,
18355                        TREE_PURPOSE (parser->unparsed_functions_queues));
18356         break;
18357       }
18358 }
18359
18360 /* FN is a FUNCTION_DECL which may contains a parameter with an
18361    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18362    assumes that the current scope is the scope in which the default
18363    argument should be processed.  */
18364
18365 static void
18366 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18367 {
18368   bool saved_local_variables_forbidden_p;
18369   tree parm;
18370
18371   /* While we're parsing the default args, we might (due to the
18372      statement expression extension) encounter more classes.  We want
18373      to handle them right away, but we don't want them getting mixed
18374      up with default args that are currently in the queue.  */
18375   parser->unparsed_functions_queues
18376     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18377
18378   /* Local variable names (and the `this' keyword) may not appear
18379      in a default argument.  */
18380   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18381   parser->local_variables_forbidden_p = true;
18382
18383   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18384        parm;
18385        parm = TREE_CHAIN (parm))
18386     {
18387       cp_token_cache *tokens;
18388       tree default_arg = TREE_PURPOSE (parm);
18389       tree parsed_arg;
18390       VEC(tree,gc) *insts;
18391       tree copy;
18392       unsigned ix;
18393
18394       if (!default_arg)
18395         continue;
18396
18397       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18398         /* This can happen for a friend declaration for a function
18399            already declared with default arguments.  */
18400         continue;
18401
18402        /* Push the saved tokens for the default argument onto the parser's
18403           lexer stack.  */
18404       tokens = DEFARG_TOKENS (default_arg);
18405       cp_parser_push_lexer_for_tokens (parser, tokens);
18406
18407       /* Parse the assignment-expression.  */
18408       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18409       if (parsed_arg == error_mark_node)
18410         {
18411           cp_parser_pop_lexer (parser);
18412           continue;
18413         }
18414
18415       if (!processing_template_decl)
18416         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18417
18418       TREE_PURPOSE (parm) = parsed_arg;
18419
18420       /* Update any instantiations we've already created.  */
18421       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18422            VEC_iterate (tree, insts, ix, copy); ix++)
18423         TREE_PURPOSE (copy) = parsed_arg;
18424
18425       /* If the token stream has not been completely used up, then
18426          there was extra junk after the end of the default
18427          argument.  */
18428       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18429         cp_parser_error (parser, "expected %<,%>");
18430
18431       /* Revert to the main lexer.  */
18432       cp_parser_pop_lexer (parser);
18433     }
18434
18435   /* Make sure no default arg is missing.  */
18436   check_default_args (fn);
18437
18438   /* Restore the state of local_variables_forbidden_p.  */
18439   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18440
18441   /* Restore the queue.  */
18442   parser->unparsed_functions_queues
18443     = TREE_CHAIN (parser->unparsed_functions_queues);
18444 }
18445
18446 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18447    either a TYPE or an expression, depending on the form of the
18448    input.  The KEYWORD indicates which kind of expression we have
18449    encountered.  */
18450
18451 static tree
18452 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18453 {
18454   tree expr = NULL_TREE;
18455   const char *saved_message;
18456   char *tmp;
18457   bool saved_integral_constant_expression_p;
18458   bool saved_non_integral_constant_expression_p;
18459   bool pack_expansion_p = false;
18460
18461   /* Types cannot be defined in a `sizeof' expression.  Save away the
18462      old message.  */
18463   saved_message = parser->type_definition_forbidden_message;
18464   /* And create the new one.  */
18465   tmp = concat ("types may not be defined in %<",
18466                 IDENTIFIER_POINTER (ridpointers[keyword]),
18467                 "%> expressions", NULL);
18468   parser->type_definition_forbidden_message = tmp;
18469
18470   /* The restrictions on constant-expressions do not apply inside
18471      sizeof expressions.  */
18472   saved_integral_constant_expression_p
18473     = parser->integral_constant_expression_p;
18474   saved_non_integral_constant_expression_p
18475     = parser->non_integral_constant_expression_p;
18476   parser->integral_constant_expression_p = false;
18477
18478   /* If it's a `...', then we are computing the length of a parameter
18479      pack.  */
18480   if (keyword == RID_SIZEOF
18481       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18482     {
18483       /* Consume the `...'.  */
18484       cp_lexer_consume_token (parser->lexer);
18485       maybe_warn_variadic_templates ();
18486
18487       /* Note that this is an expansion.  */
18488       pack_expansion_p = true;
18489     }
18490
18491   /* Do not actually evaluate the expression.  */
18492   ++skip_evaluation;
18493   /* If it's a `(', then we might be looking at the type-id
18494      construction.  */
18495   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18496     {
18497       tree type;
18498       bool saved_in_type_id_in_expr_p;
18499
18500       /* We can't be sure yet whether we're looking at a type-id or an
18501          expression.  */
18502       cp_parser_parse_tentatively (parser);
18503       /* Consume the `('.  */
18504       cp_lexer_consume_token (parser->lexer);
18505       /* Parse the type-id.  */
18506       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18507       parser->in_type_id_in_expr_p = true;
18508       type = cp_parser_type_id (parser);
18509       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18510       /* Now, look for the trailing `)'.  */
18511       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18512       /* If all went well, then we're done.  */
18513       if (cp_parser_parse_definitely (parser))
18514         {
18515           cp_decl_specifier_seq decl_specs;
18516
18517           /* Build a trivial decl-specifier-seq.  */
18518           clear_decl_specs (&decl_specs);
18519           decl_specs.type = type;
18520
18521           /* Call grokdeclarator to figure out what type this is.  */
18522           expr = grokdeclarator (NULL,
18523                                  &decl_specs,
18524                                  TYPENAME,
18525                                  /*initialized=*/0,
18526                                  /*attrlist=*/NULL);
18527         }
18528     }
18529
18530   /* If the type-id production did not work out, then we must be
18531      looking at the unary-expression production.  */
18532   if (!expr)
18533     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18534                                        /*cast_p=*/false, NULL);
18535
18536   if (pack_expansion_p)
18537     /* Build a pack expansion. */
18538     expr = make_pack_expansion (expr);
18539
18540   /* Go back to evaluating expressions.  */
18541   --skip_evaluation;
18542
18543   /* Free the message we created.  */
18544   free (tmp);
18545   /* And restore the old one.  */
18546   parser->type_definition_forbidden_message = saved_message;
18547   parser->integral_constant_expression_p
18548     = saved_integral_constant_expression_p;
18549   parser->non_integral_constant_expression_p
18550     = saved_non_integral_constant_expression_p;
18551
18552   return expr;
18553 }
18554
18555 /* If the current declaration has no declarator, return true.  */
18556
18557 static bool
18558 cp_parser_declares_only_class_p (cp_parser *parser)
18559 {
18560   /* If the next token is a `;' or a `,' then there is no
18561      declarator.  */
18562   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18563           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18564 }
18565
18566 /* Update the DECL_SPECS to reflect the storage class indicated by
18567    KEYWORD.  */
18568
18569 static void
18570 cp_parser_set_storage_class (cp_parser *parser,
18571                              cp_decl_specifier_seq *decl_specs,
18572                              enum rid keyword,
18573                              location_t location)
18574 {
18575   cp_storage_class storage_class;
18576
18577   if (parser->in_unbraced_linkage_specification_p)
18578     {
18579       error ("%Hinvalid use of %qD in linkage specification",
18580              &location, ridpointers[keyword]);
18581       return;
18582     }
18583   else if (decl_specs->storage_class != sc_none)
18584     {
18585       decl_specs->conflicting_specifiers_p = true;
18586       return;
18587     }
18588
18589   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18590       && decl_specs->specs[(int) ds_thread])
18591     {
18592       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18593       decl_specs->specs[(int) ds_thread] = 0;
18594     }
18595
18596   switch (keyword)
18597     {
18598     case RID_AUTO:
18599       storage_class = sc_auto;
18600       break;
18601     case RID_REGISTER:
18602       storage_class = sc_register;
18603       break;
18604     case RID_STATIC:
18605       storage_class = sc_static;
18606       break;
18607     case RID_EXTERN:
18608       storage_class = sc_extern;
18609       break;
18610     case RID_MUTABLE:
18611       storage_class = sc_mutable;
18612       break;
18613     default:
18614       gcc_unreachable ();
18615     }
18616   decl_specs->storage_class = storage_class;
18617
18618   /* A storage class specifier cannot be applied alongside a typedef 
18619      specifier. If there is a typedef specifier present then set 
18620      conflicting_specifiers_p which will trigger an error later
18621      on in grokdeclarator. */
18622   if (decl_specs->specs[(int)ds_typedef])
18623     decl_specs->conflicting_specifiers_p = true;
18624 }
18625
18626 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18627    is true, the type is a user-defined type; otherwise it is a
18628    built-in type specified by a keyword.  */
18629
18630 static void
18631 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18632                               tree type_spec,
18633                               location_t location,
18634                               bool user_defined_p)
18635 {
18636   decl_specs->any_specifiers_p = true;
18637
18638   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18639      (with, for example, in "typedef int wchar_t;") we remember that
18640      this is what happened.  In system headers, we ignore these
18641      declarations so that G++ can work with system headers that are not
18642      C++-safe.  */
18643   if (decl_specs->specs[(int) ds_typedef]
18644       && !user_defined_p
18645       && (type_spec == boolean_type_node
18646           || type_spec == char16_type_node
18647           || type_spec == char32_type_node
18648           || type_spec == wchar_type_node)
18649       && (decl_specs->type
18650           || decl_specs->specs[(int) ds_long]
18651           || decl_specs->specs[(int) ds_short]
18652           || decl_specs->specs[(int) ds_unsigned]
18653           || decl_specs->specs[(int) ds_signed]))
18654     {
18655       decl_specs->redefined_builtin_type = type_spec;
18656       if (!decl_specs->type)
18657         {
18658           decl_specs->type = type_spec;
18659           decl_specs->user_defined_type_p = false;
18660           decl_specs->type_location = location;
18661         }
18662     }
18663   else if (decl_specs->type)
18664     decl_specs->multiple_types_p = true;
18665   else
18666     {
18667       decl_specs->type = type_spec;
18668       decl_specs->user_defined_type_p = user_defined_p;
18669       decl_specs->redefined_builtin_type = NULL_TREE;
18670       decl_specs->type_location = location;
18671     }
18672 }
18673
18674 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18675    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18676
18677 static bool
18678 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18679 {
18680   return decl_specifiers->specs[(int) ds_friend] != 0;
18681 }
18682
18683 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18684    issue an error message indicating that TOKEN_DESC was expected.
18685
18686    Returns the token consumed, if the token had the appropriate type.
18687    Otherwise, returns NULL.  */
18688
18689 static cp_token *
18690 cp_parser_require (cp_parser* parser,
18691                    enum cpp_ttype type,
18692                    const char* token_desc)
18693 {
18694   if (cp_lexer_next_token_is (parser->lexer, type))
18695     return cp_lexer_consume_token (parser->lexer);
18696   else
18697     {
18698       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18699       if (!cp_parser_simulate_error (parser))
18700         {
18701           char *message = concat ("expected ", token_desc, NULL);
18702           cp_parser_error (parser, message);
18703           free (message);
18704         }
18705       return NULL;
18706     }
18707 }
18708
18709 /* An error message is produced if the next token is not '>'.
18710    All further tokens are skipped until the desired token is
18711    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18712
18713 static void
18714 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18715 {
18716   /* Current level of '< ... >'.  */
18717   unsigned level = 0;
18718   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18719   unsigned nesting_depth = 0;
18720
18721   /* Are we ready, yet?  If not, issue error message.  */
18722   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18723     return;
18724
18725   /* Skip tokens until the desired token is found.  */
18726   while (true)
18727     {
18728       /* Peek at the next token.  */
18729       switch (cp_lexer_peek_token (parser->lexer)->type)
18730         {
18731         case CPP_LESS:
18732           if (!nesting_depth)
18733             ++level;
18734           break;
18735
18736         case CPP_RSHIFT:
18737           if (cxx_dialect == cxx98)
18738             /* C++0x views the `>>' operator as two `>' tokens, but
18739                C++98 does not. */
18740             break;
18741           else if (!nesting_depth && level-- == 0)
18742             {
18743               /* We've hit a `>>' where the first `>' closes the
18744                  template argument list, and the second `>' is
18745                  spurious.  Just consume the `>>' and stop; we've
18746                  already produced at least one error.  */
18747               cp_lexer_consume_token (parser->lexer);
18748               return;
18749             }
18750           /* Fall through for C++0x, so we handle the second `>' in
18751              the `>>'.  */
18752
18753         case CPP_GREATER:
18754           if (!nesting_depth && level-- == 0)
18755             {
18756               /* We've reached the token we want, consume it and stop.  */
18757               cp_lexer_consume_token (parser->lexer);
18758               return;
18759             }
18760           break;
18761
18762         case CPP_OPEN_PAREN:
18763         case CPP_OPEN_SQUARE:
18764           ++nesting_depth;
18765           break;
18766
18767         case CPP_CLOSE_PAREN:
18768         case CPP_CLOSE_SQUARE:
18769           if (nesting_depth-- == 0)
18770             return;
18771           break;
18772
18773         case CPP_EOF:
18774         case CPP_PRAGMA_EOL:
18775         case CPP_SEMICOLON:
18776         case CPP_OPEN_BRACE:
18777         case CPP_CLOSE_BRACE:
18778           /* The '>' was probably forgotten, don't look further.  */
18779           return;
18780
18781         default:
18782           break;
18783         }
18784
18785       /* Consume this token.  */
18786       cp_lexer_consume_token (parser->lexer);
18787     }
18788 }
18789
18790 /* If the next token is the indicated keyword, consume it.  Otherwise,
18791    issue an error message indicating that TOKEN_DESC was expected.
18792
18793    Returns the token consumed, if the token had the appropriate type.
18794    Otherwise, returns NULL.  */
18795
18796 static cp_token *
18797 cp_parser_require_keyword (cp_parser* parser,
18798                            enum rid keyword,
18799                            const char* token_desc)
18800 {
18801   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18802
18803   if (token && token->keyword != keyword)
18804     {
18805       dyn_string_t error_msg;
18806
18807       /* Format the error message.  */
18808       error_msg = dyn_string_new (0);
18809       dyn_string_append_cstr (error_msg, "expected ");
18810       dyn_string_append_cstr (error_msg, token_desc);
18811       cp_parser_error (parser, error_msg->s);
18812       dyn_string_delete (error_msg);
18813       return NULL;
18814     }
18815
18816   return token;
18817 }
18818
18819 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18820    function-definition.  */
18821
18822 static bool
18823 cp_parser_token_starts_function_definition_p (cp_token* token)
18824 {
18825   return (/* An ordinary function-body begins with an `{'.  */
18826           token->type == CPP_OPEN_BRACE
18827           /* A ctor-initializer begins with a `:'.  */
18828           || token->type == CPP_COLON
18829           /* A function-try-block begins with `try'.  */
18830           || token->keyword == RID_TRY
18831           /* The named return value extension begins with `return'.  */
18832           || token->keyword == RID_RETURN);
18833 }
18834
18835 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18836    definition.  */
18837
18838 static bool
18839 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18840 {
18841   cp_token *token;
18842
18843   token = cp_lexer_peek_token (parser->lexer);
18844   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18845 }
18846
18847 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18848    C++0x) ending a template-argument.  */
18849
18850 static bool
18851 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18852 {
18853   cp_token *token;
18854
18855   token = cp_lexer_peek_token (parser->lexer);
18856   return (token->type == CPP_COMMA 
18857           || token->type == CPP_GREATER
18858           || token->type == CPP_ELLIPSIS
18859           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18860 }
18861
18862 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18863    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18864
18865 static bool
18866 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18867                                                      size_t n)
18868 {
18869   cp_token *token;
18870
18871   token = cp_lexer_peek_nth_token (parser->lexer, n);
18872   if (token->type == CPP_LESS)
18873     return true;
18874   /* Check for the sequence `<::' in the original code. It would be lexed as
18875      `[:', where `[' is a digraph, and there is no whitespace before
18876      `:'.  */
18877   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18878     {
18879       cp_token *token2;
18880       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18881       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18882         return true;
18883     }
18884   return false;
18885 }
18886
18887 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18888    or none_type otherwise.  */
18889
18890 static enum tag_types
18891 cp_parser_token_is_class_key (cp_token* token)
18892 {
18893   switch (token->keyword)
18894     {
18895     case RID_CLASS:
18896       return class_type;
18897     case RID_STRUCT:
18898       return record_type;
18899     case RID_UNION:
18900       return union_type;
18901
18902     default:
18903       return none_type;
18904     }
18905 }
18906
18907 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18908
18909 static void
18910 cp_parser_check_class_key (enum tag_types class_key, tree type)
18911 {
18912   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18913     permerror (input_location, "%qs tag used in naming %q#T",
18914             class_key == union_type ? "union"
18915              : class_key == record_type ? "struct" : "class",
18916              type);
18917 }
18918
18919 /* Issue an error message if DECL is redeclared with different
18920    access than its original declaration [class.access.spec/3].
18921    This applies to nested classes and nested class templates.
18922    [class.mem/1].  */
18923
18924 static void
18925 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18926 {
18927   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18928     return;
18929
18930   if ((TREE_PRIVATE (decl)
18931        != (current_access_specifier == access_private_node))
18932       || (TREE_PROTECTED (decl)
18933           != (current_access_specifier == access_protected_node)))
18934     error ("%H%qD redeclared with different access", &location, decl);
18935 }
18936
18937 /* Look for the `template' keyword, as a syntactic disambiguator.
18938    Return TRUE iff it is present, in which case it will be
18939    consumed.  */
18940
18941 static bool
18942 cp_parser_optional_template_keyword (cp_parser *parser)
18943 {
18944   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18945     {
18946       /* The `template' keyword can only be used within templates;
18947          outside templates the parser can always figure out what is a
18948          template and what is not.  */
18949       if (!processing_template_decl)
18950         {
18951           cp_token *token = cp_lexer_peek_token (parser->lexer);
18952           error ("%H%<template%> (as a disambiguator) is only allowed "
18953                  "within templates", &token->location);
18954           /* If this part of the token stream is rescanned, the same
18955              error message would be generated.  So, we purge the token
18956              from the stream.  */
18957           cp_lexer_purge_token (parser->lexer);
18958           return false;
18959         }
18960       else
18961         {
18962           /* Consume the `template' keyword.  */
18963           cp_lexer_consume_token (parser->lexer);
18964           return true;
18965         }
18966     }
18967
18968   return false;
18969 }
18970
18971 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18972    set PARSER->SCOPE, and perform other related actions.  */
18973
18974 static void
18975 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18976 {
18977   int i;
18978   struct tree_check *check_value;
18979   deferred_access_check *chk;
18980   VEC (deferred_access_check,gc) *checks;
18981
18982   /* Get the stored value.  */
18983   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18984   /* Perform any access checks that were deferred.  */
18985   checks = check_value->checks;
18986   if (checks)
18987     {
18988       for (i = 0 ;
18989            VEC_iterate (deferred_access_check, checks, i, chk) ;
18990            ++i)
18991         {
18992           perform_or_defer_access_check (chk->binfo,
18993                                          chk->decl,
18994                                          chk->diag_decl);
18995         }
18996     }
18997   /* Set the scope from the stored value.  */
18998   parser->scope = check_value->value;
18999   parser->qualifying_scope = check_value->qualifying_scope;
19000   parser->object_scope = NULL_TREE;
19001 }
19002
19003 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19004    encounter the end of a block before what we were looking for.  */
19005
19006 static bool
19007 cp_parser_cache_group (cp_parser *parser,
19008                        enum cpp_ttype end,
19009                        unsigned depth)
19010 {
19011   while (true)
19012     {
19013       cp_token *token = cp_lexer_peek_token (parser->lexer);
19014
19015       /* Abort a parenthesized expression if we encounter a semicolon.  */
19016       if ((end == CPP_CLOSE_PAREN || depth == 0)
19017           && token->type == CPP_SEMICOLON)
19018         return true;
19019       /* If we've reached the end of the file, stop.  */
19020       if (token->type == CPP_EOF
19021           || (end != CPP_PRAGMA_EOL
19022               && token->type == CPP_PRAGMA_EOL))
19023         return true;
19024       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19025         /* We've hit the end of an enclosing block, so there's been some
19026            kind of syntax error.  */
19027         return true;
19028
19029       /* Consume the token.  */
19030       cp_lexer_consume_token (parser->lexer);
19031       /* See if it starts a new group.  */
19032       if (token->type == CPP_OPEN_BRACE)
19033         {
19034           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19035           /* In theory this should probably check end == '}', but
19036              cp_parser_save_member_function_body needs it to exit
19037              after either '}' or ')' when called with ')'.  */
19038           if (depth == 0)
19039             return false;
19040         }
19041       else if (token->type == CPP_OPEN_PAREN)
19042         {
19043           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19044           if (depth == 0 && end == CPP_CLOSE_PAREN)
19045             return false;
19046         }
19047       else if (token->type == CPP_PRAGMA)
19048         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19049       else if (token->type == end)
19050         return false;
19051     }
19052 }
19053
19054 /* Begin parsing tentatively.  We always save tokens while parsing
19055    tentatively so that if the tentative parsing fails we can restore the
19056    tokens.  */
19057
19058 static void
19059 cp_parser_parse_tentatively (cp_parser* parser)
19060 {
19061   /* Enter a new parsing context.  */
19062   parser->context = cp_parser_context_new (parser->context);
19063   /* Begin saving tokens.  */
19064   cp_lexer_save_tokens (parser->lexer);
19065   /* In order to avoid repetitive access control error messages,
19066      access checks are queued up until we are no longer parsing
19067      tentatively.  */
19068   push_deferring_access_checks (dk_deferred);
19069 }
19070
19071 /* Commit to the currently active tentative parse.  */
19072
19073 static void
19074 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19075 {
19076   cp_parser_context *context;
19077   cp_lexer *lexer;
19078
19079   /* Mark all of the levels as committed.  */
19080   lexer = parser->lexer;
19081   for (context = parser->context; context->next; context = context->next)
19082     {
19083       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19084         break;
19085       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19086       while (!cp_lexer_saving_tokens (lexer))
19087         lexer = lexer->next;
19088       cp_lexer_commit_tokens (lexer);
19089     }
19090 }
19091
19092 /* Abort the currently active tentative parse.  All consumed tokens
19093    will be rolled back, and no diagnostics will be issued.  */
19094
19095 static void
19096 cp_parser_abort_tentative_parse (cp_parser* parser)
19097 {
19098   cp_parser_simulate_error (parser);
19099   /* Now, pretend that we want to see if the construct was
19100      successfully parsed.  */
19101   cp_parser_parse_definitely (parser);
19102 }
19103
19104 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19105    token stream.  Otherwise, commit to the tokens we have consumed.
19106    Returns true if no error occurred; false otherwise.  */
19107
19108 static bool
19109 cp_parser_parse_definitely (cp_parser* parser)
19110 {
19111   bool error_occurred;
19112   cp_parser_context *context;
19113
19114   /* Remember whether or not an error occurred, since we are about to
19115      destroy that information.  */
19116   error_occurred = cp_parser_error_occurred (parser);
19117   /* Remove the topmost context from the stack.  */
19118   context = parser->context;
19119   parser->context = context->next;
19120   /* If no parse errors occurred, commit to the tentative parse.  */
19121   if (!error_occurred)
19122     {
19123       /* Commit to the tokens read tentatively, unless that was
19124          already done.  */
19125       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19126         cp_lexer_commit_tokens (parser->lexer);
19127
19128       pop_to_parent_deferring_access_checks ();
19129     }
19130   /* Otherwise, if errors occurred, roll back our state so that things
19131      are just as they were before we began the tentative parse.  */
19132   else
19133     {
19134       cp_lexer_rollback_tokens (parser->lexer);
19135       pop_deferring_access_checks ();
19136     }
19137   /* Add the context to the front of the free list.  */
19138   context->next = cp_parser_context_free_list;
19139   cp_parser_context_free_list = context;
19140
19141   return !error_occurred;
19142 }
19143
19144 /* Returns true if we are parsing tentatively and are not committed to
19145    this tentative parse.  */
19146
19147 static bool
19148 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19149 {
19150   return (cp_parser_parsing_tentatively (parser)
19151           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19152 }
19153
19154 /* Returns nonzero iff an error has occurred during the most recent
19155    tentative parse.  */
19156
19157 static bool
19158 cp_parser_error_occurred (cp_parser* parser)
19159 {
19160   return (cp_parser_parsing_tentatively (parser)
19161           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19162 }
19163
19164 /* Returns nonzero if GNU extensions are allowed.  */
19165
19166 static bool
19167 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19168 {
19169   return parser->allow_gnu_extensions_p;
19170 }
19171 \f
19172 /* Objective-C++ Productions */
19173
19174
19175 /* Parse an Objective-C expression, which feeds into a primary-expression
19176    above.
19177
19178    objc-expression:
19179      objc-message-expression
19180      objc-string-literal
19181      objc-encode-expression
19182      objc-protocol-expression
19183      objc-selector-expression
19184
19185   Returns a tree representation of the expression.  */
19186
19187 static tree
19188 cp_parser_objc_expression (cp_parser* parser)
19189 {
19190   /* Try to figure out what kind of declaration is present.  */
19191   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19192
19193   switch (kwd->type)
19194     {
19195     case CPP_OPEN_SQUARE:
19196       return cp_parser_objc_message_expression (parser);
19197
19198     case CPP_OBJC_STRING:
19199       kwd = cp_lexer_consume_token (parser->lexer);
19200       return objc_build_string_object (kwd->u.value);
19201
19202     case CPP_KEYWORD:
19203       switch (kwd->keyword)
19204         {
19205         case RID_AT_ENCODE:
19206           return cp_parser_objc_encode_expression (parser);
19207
19208         case RID_AT_PROTOCOL:
19209           return cp_parser_objc_protocol_expression (parser);
19210
19211         case RID_AT_SELECTOR:
19212           return cp_parser_objc_selector_expression (parser);
19213
19214         default:
19215           break;
19216         }
19217     default:
19218       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19219              &kwd->location, kwd->u.value);
19220       cp_parser_skip_to_end_of_block_or_statement (parser);
19221     }
19222
19223   return error_mark_node;
19224 }
19225
19226 /* Parse an Objective-C message expression.
19227
19228    objc-message-expression:
19229      [ objc-message-receiver objc-message-args ]
19230
19231    Returns a representation of an Objective-C message.  */
19232
19233 static tree
19234 cp_parser_objc_message_expression (cp_parser* parser)
19235 {
19236   tree receiver, messageargs;
19237
19238   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19239   receiver = cp_parser_objc_message_receiver (parser);
19240   messageargs = cp_parser_objc_message_args (parser);
19241   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19242
19243   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19244 }
19245
19246 /* Parse an objc-message-receiver.
19247
19248    objc-message-receiver:
19249      expression
19250      simple-type-specifier
19251
19252   Returns a representation of the type or expression.  */
19253
19254 static tree
19255 cp_parser_objc_message_receiver (cp_parser* parser)
19256 {
19257   tree rcv;
19258
19259   /* An Objective-C message receiver may be either (1) a type
19260      or (2) an expression.  */
19261   cp_parser_parse_tentatively (parser);
19262   rcv = cp_parser_expression (parser, false, NULL);
19263
19264   if (cp_parser_parse_definitely (parser))
19265     return rcv;
19266
19267   rcv = cp_parser_simple_type_specifier (parser,
19268                                          /*decl_specs=*/NULL,
19269                                          CP_PARSER_FLAGS_NONE);
19270
19271   return objc_get_class_reference (rcv);
19272 }
19273
19274 /* Parse the arguments and selectors comprising an Objective-C message.
19275
19276    objc-message-args:
19277      objc-selector
19278      objc-selector-args
19279      objc-selector-args , objc-comma-args
19280
19281    objc-selector-args:
19282      objc-selector [opt] : assignment-expression
19283      objc-selector-args objc-selector [opt] : assignment-expression
19284
19285    objc-comma-args:
19286      assignment-expression
19287      objc-comma-args , assignment-expression
19288
19289    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19290    selector arguments and TREE_VALUE containing a list of comma
19291    arguments.  */
19292
19293 static tree
19294 cp_parser_objc_message_args (cp_parser* parser)
19295 {
19296   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19297   bool maybe_unary_selector_p = true;
19298   cp_token *token = cp_lexer_peek_token (parser->lexer);
19299
19300   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19301     {
19302       tree selector = NULL_TREE, arg;
19303
19304       if (token->type != CPP_COLON)
19305         selector = cp_parser_objc_selector (parser);
19306
19307       /* Detect if we have a unary selector.  */
19308       if (maybe_unary_selector_p
19309           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19310         return build_tree_list (selector, NULL_TREE);
19311
19312       maybe_unary_selector_p = false;
19313       cp_parser_require (parser, CPP_COLON, "%<:%>");
19314       arg = cp_parser_assignment_expression (parser, false, NULL);
19315
19316       sel_args
19317         = chainon (sel_args,
19318                    build_tree_list (selector, arg));
19319
19320       token = cp_lexer_peek_token (parser->lexer);
19321     }
19322
19323   /* Handle non-selector arguments, if any. */
19324   while (token->type == CPP_COMMA)
19325     {
19326       tree arg;
19327
19328       cp_lexer_consume_token (parser->lexer);
19329       arg = cp_parser_assignment_expression (parser, false, NULL);
19330
19331       addl_args
19332         = chainon (addl_args,
19333                    build_tree_list (NULL_TREE, arg));
19334
19335       token = cp_lexer_peek_token (parser->lexer);
19336     }
19337
19338   return build_tree_list (sel_args, addl_args);
19339 }
19340
19341 /* Parse an Objective-C encode expression.
19342
19343    objc-encode-expression:
19344      @encode objc-typename
19345
19346    Returns an encoded representation of the type argument.  */
19347
19348 static tree
19349 cp_parser_objc_encode_expression (cp_parser* parser)
19350 {
19351   tree type;
19352   cp_token *token;
19353
19354   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19355   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19356   token = cp_lexer_peek_token (parser->lexer);
19357   type = complete_type (cp_parser_type_id (parser));
19358   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19359
19360   if (!type)
19361     {
19362       error ("%H%<@encode%> must specify a type as an argument",
19363              &token->location);
19364       return error_mark_node;
19365     }
19366
19367   return objc_build_encode_expr (type);
19368 }
19369
19370 /* Parse an Objective-C @defs expression.  */
19371
19372 static tree
19373 cp_parser_objc_defs_expression (cp_parser *parser)
19374 {
19375   tree name;
19376
19377   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19378   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19379   name = cp_parser_identifier (parser);
19380   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19381
19382   return objc_get_class_ivars (name);
19383 }
19384
19385 /* Parse an Objective-C protocol expression.
19386
19387   objc-protocol-expression:
19388     @protocol ( identifier )
19389
19390   Returns a representation of the protocol expression.  */
19391
19392 static tree
19393 cp_parser_objc_protocol_expression (cp_parser* parser)
19394 {
19395   tree proto;
19396
19397   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19398   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19399   proto = cp_parser_identifier (parser);
19400   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19401
19402   return objc_build_protocol_expr (proto);
19403 }
19404
19405 /* Parse an Objective-C selector expression.
19406
19407    objc-selector-expression:
19408      @selector ( objc-method-signature )
19409
19410    objc-method-signature:
19411      objc-selector
19412      objc-selector-seq
19413
19414    objc-selector-seq:
19415      objc-selector :
19416      objc-selector-seq objc-selector :
19417
19418   Returns a representation of the method selector.  */
19419
19420 static tree
19421 cp_parser_objc_selector_expression (cp_parser* parser)
19422 {
19423   tree sel_seq = NULL_TREE;
19424   bool maybe_unary_selector_p = true;
19425   cp_token *token;
19426
19427   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19428   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19429   token = cp_lexer_peek_token (parser->lexer);
19430
19431   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19432          || token->type == CPP_SCOPE)
19433     {
19434       tree selector = NULL_TREE;
19435
19436       if (token->type != CPP_COLON
19437           || token->type == CPP_SCOPE)
19438         selector = cp_parser_objc_selector (parser);
19439
19440       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19441           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19442         {
19443           /* Detect if we have a unary selector.  */
19444           if (maybe_unary_selector_p)
19445             {
19446               sel_seq = selector;
19447               goto finish_selector;
19448             }
19449           else
19450             {
19451               cp_parser_error (parser, "expected %<:%>");
19452             }
19453         }
19454       maybe_unary_selector_p = false;
19455       token = cp_lexer_consume_token (parser->lexer);
19456
19457       if (token->type == CPP_SCOPE)
19458         {
19459           sel_seq
19460             = chainon (sel_seq,
19461                        build_tree_list (selector, NULL_TREE));
19462           sel_seq
19463             = chainon (sel_seq,
19464                        build_tree_list (NULL_TREE, NULL_TREE));
19465         }
19466       else
19467         sel_seq
19468           = chainon (sel_seq,
19469                      build_tree_list (selector, NULL_TREE));
19470
19471       token = cp_lexer_peek_token (parser->lexer);
19472     }
19473
19474  finish_selector:
19475   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19476
19477   return objc_build_selector_expr (sel_seq);
19478 }
19479
19480 /* Parse a list of identifiers.
19481
19482    objc-identifier-list:
19483      identifier
19484      objc-identifier-list , identifier
19485
19486    Returns a TREE_LIST of identifier nodes.  */
19487
19488 static tree
19489 cp_parser_objc_identifier_list (cp_parser* parser)
19490 {
19491   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19492   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19493
19494   while (sep->type == CPP_COMMA)
19495     {
19496       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19497       list = chainon (list,
19498                       build_tree_list (NULL_TREE,
19499                                        cp_parser_identifier (parser)));
19500       sep = cp_lexer_peek_token (parser->lexer);
19501     }
19502
19503   return list;
19504 }
19505
19506 /* Parse an Objective-C alias declaration.
19507
19508    objc-alias-declaration:
19509      @compatibility_alias identifier identifier ;
19510
19511    This function registers the alias mapping with the Objective-C front end.
19512    It returns nothing.  */
19513
19514 static void
19515 cp_parser_objc_alias_declaration (cp_parser* parser)
19516 {
19517   tree alias, orig;
19518
19519   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19520   alias = cp_parser_identifier (parser);
19521   orig = cp_parser_identifier (parser);
19522   objc_declare_alias (alias, orig);
19523   cp_parser_consume_semicolon_at_end_of_statement (parser);
19524 }
19525
19526 /* Parse an Objective-C class forward-declaration.
19527
19528    objc-class-declaration:
19529      @class objc-identifier-list ;
19530
19531    The function registers the forward declarations with the Objective-C
19532    front end.  It returns nothing.  */
19533
19534 static void
19535 cp_parser_objc_class_declaration (cp_parser* parser)
19536 {
19537   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19538   objc_declare_class (cp_parser_objc_identifier_list (parser));
19539   cp_parser_consume_semicolon_at_end_of_statement (parser);
19540 }
19541
19542 /* Parse a list of Objective-C protocol references.
19543
19544    objc-protocol-refs-opt:
19545      objc-protocol-refs [opt]
19546
19547    objc-protocol-refs:
19548      < objc-identifier-list >
19549
19550    Returns a TREE_LIST of identifiers, if any.  */
19551
19552 static tree
19553 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19554 {
19555   tree protorefs = NULL_TREE;
19556
19557   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19558     {
19559       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19560       protorefs = cp_parser_objc_identifier_list (parser);
19561       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19562     }
19563
19564   return protorefs;
19565 }
19566
19567 /* Parse a Objective-C visibility specification.  */
19568
19569 static void
19570 cp_parser_objc_visibility_spec (cp_parser* parser)
19571 {
19572   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19573
19574   switch (vis->keyword)
19575     {
19576     case RID_AT_PRIVATE:
19577       objc_set_visibility (2);
19578       break;
19579     case RID_AT_PROTECTED:
19580       objc_set_visibility (0);
19581       break;
19582     case RID_AT_PUBLIC:
19583       objc_set_visibility (1);
19584       break;
19585     default:
19586       return;
19587     }
19588
19589   /* Eat '@private'/'@protected'/'@public'.  */
19590   cp_lexer_consume_token (parser->lexer);
19591 }
19592
19593 /* Parse an Objective-C method type.  */
19594
19595 static void
19596 cp_parser_objc_method_type (cp_parser* parser)
19597 {
19598   objc_set_method_type
19599    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19600     ? PLUS_EXPR
19601     : MINUS_EXPR);
19602 }
19603
19604 /* Parse an Objective-C protocol qualifier.  */
19605
19606 static tree
19607 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19608 {
19609   tree quals = NULL_TREE, node;
19610   cp_token *token = cp_lexer_peek_token (parser->lexer);
19611
19612   node = token->u.value;
19613
19614   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19615          && (node == ridpointers [(int) RID_IN]
19616              || node == ridpointers [(int) RID_OUT]
19617              || node == ridpointers [(int) RID_INOUT]
19618              || node == ridpointers [(int) RID_BYCOPY]
19619              || node == ridpointers [(int) RID_BYREF]
19620              || node == ridpointers [(int) RID_ONEWAY]))
19621     {
19622       quals = tree_cons (NULL_TREE, node, quals);
19623       cp_lexer_consume_token (parser->lexer);
19624       token = cp_lexer_peek_token (parser->lexer);
19625       node = token->u.value;
19626     }
19627
19628   return quals;
19629 }
19630
19631 /* Parse an Objective-C typename.  */
19632
19633 static tree
19634 cp_parser_objc_typename (cp_parser* parser)
19635 {
19636   tree type_name = NULL_TREE;
19637
19638   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19639     {
19640       tree proto_quals, cp_type = NULL_TREE;
19641
19642       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19643       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19644
19645       /* An ObjC type name may consist of just protocol qualifiers, in which
19646          case the type shall default to 'id'.  */
19647       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19648         cp_type = cp_parser_type_id (parser);
19649
19650       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19651       type_name = build_tree_list (proto_quals, cp_type);
19652     }
19653
19654   return type_name;
19655 }
19656
19657 /* Check to see if TYPE refers to an Objective-C selector name.  */
19658
19659 static bool
19660 cp_parser_objc_selector_p (enum cpp_ttype type)
19661 {
19662   return (type == CPP_NAME || type == CPP_KEYWORD
19663           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19664           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19665           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19666           || type == CPP_XOR || type == CPP_XOR_EQ);
19667 }
19668
19669 /* Parse an Objective-C selector.  */
19670
19671 static tree
19672 cp_parser_objc_selector (cp_parser* parser)
19673 {
19674   cp_token *token = cp_lexer_consume_token (parser->lexer);
19675
19676   if (!cp_parser_objc_selector_p (token->type))
19677     {
19678       error ("%Hinvalid Objective-C++ selector name", &token->location);
19679       return error_mark_node;
19680     }
19681
19682   /* C++ operator names are allowed to appear in ObjC selectors.  */
19683   switch (token->type)
19684     {
19685     case CPP_AND_AND: return get_identifier ("and");
19686     case CPP_AND_EQ: return get_identifier ("and_eq");
19687     case CPP_AND: return get_identifier ("bitand");
19688     case CPP_OR: return get_identifier ("bitor");
19689     case CPP_COMPL: return get_identifier ("compl");
19690     case CPP_NOT: return get_identifier ("not");
19691     case CPP_NOT_EQ: return get_identifier ("not_eq");
19692     case CPP_OR_OR: return get_identifier ("or");
19693     case CPP_OR_EQ: return get_identifier ("or_eq");
19694     case CPP_XOR: return get_identifier ("xor");
19695     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19696     default: return token->u.value;
19697     }
19698 }
19699
19700 /* Parse an Objective-C params list.  */
19701
19702 static tree
19703 cp_parser_objc_method_keyword_params (cp_parser* parser)
19704 {
19705   tree params = NULL_TREE;
19706   bool maybe_unary_selector_p = true;
19707   cp_token *token = cp_lexer_peek_token (parser->lexer);
19708
19709   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19710     {
19711       tree selector = NULL_TREE, type_name, identifier;
19712
19713       if (token->type != CPP_COLON)
19714         selector = cp_parser_objc_selector (parser);
19715
19716       /* Detect if we have a unary selector.  */
19717       if (maybe_unary_selector_p
19718           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19719         return selector;
19720
19721       maybe_unary_selector_p = false;
19722       cp_parser_require (parser, CPP_COLON, "%<:%>");
19723       type_name = cp_parser_objc_typename (parser);
19724       identifier = cp_parser_identifier (parser);
19725
19726       params
19727         = chainon (params,
19728                    objc_build_keyword_decl (selector,
19729                                             type_name,
19730                                             identifier));
19731
19732       token = cp_lexer_peek_token (parser->lexer);
19733     }
19734
19735   return params;
19736 }
19737
19738 /* Parse the non-keyword Objective-C params.  */
19739
19740 static tree
19741 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19742 {
19743   tree params = make_node (TREE_LIST);
19744   cp_token *token = cp_lexer_peek_token (parser->lexer);
19745   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19746
19747   while (token->type == CPP_COMMA)
19748     {
19749       cp_parameter_declarator *parmdecl;
19750       tree parm;
19751
19752       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19753       token = cp_lexer_peek_token (parser->lexer);
19754
19755       if (token->type == CPP_ELLIPSIS)
19756         {
19757           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19758           *ellipsisp = true;
19759           break;
19760         }
19761
19762       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19763       parm = grokdeclarator (parmdecl->declarator,
19764                              &parmdecl->decl_specifiers,
19765                              PARM, /*initialized=*/0,
19766                              /*attrlist=*/NULL);
19767
19768       chainon (params, build_tree_list (NULL_TREE, parm));
19769       token = cp_lexer_peek_token (parser->lexer);
19770     }
19771
19772   return params;
19773 }
19774
19775 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19776
19777 static void
19778 cp_parser_objc_interstitial_code (cp_parser* parser)
19779 {
19780   cp_token *token = cp_lexer_peek_token (parser->lexer);
19781
19782   /* If the next token is `extern' and the following token is a string
19783      literal, then we have a linkage specification.  */
19784   if (token->keyword == RID_EXTERN
19785       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19786     cp_parser_linkage_specification (parser);
19787   /* Handle #pragma, if any.  */
19788   else if (token->type == CPP_PRAGMA)
19789     cp_parser_pragma (parser, pragma_external);
19790   /* Allow stray semicolons.  */
19791   else if (token->type == CPP_SEMICOLON)
19792     cp_lexer_consume_token (parser->lexer);
19793   /* Finally, try to parse a block-declaration, or a function-definition.  */
19794   else
19795     cp_parser_block_declaration (parser, /*statement_p=*/false);
19796 }
19797
19798 /* Parse a method signature.  */
19799
19800 static tree
19801 cp_parser_objc_method_signature (cp_parser* parser)
19802 {
19803   tree rettype, kwdparms, optparms;
19804   bool ellipsis = false;
19805
19806   cp_parser_objc_method_type (parser);
19807   rettype = cp_parser_objc_typename (parser);
19808   kwdparms = cp_parser_objc_method_keyword_params (parser);
19809   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19810
19811   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19812 }
19813
19814 /* Pars an Objective-C method prototype list.  */
19815
19816 static void
19817 cp_parser_objc_method_prototype_list (cp_parser* parser)
19818 {
19819   cp_token *token = cp_lexer_peek_token (parser->lexer);
19820
19821   while (token->keyword != RID_AT_END)
19822     {
19823       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19824         {
19825           objc_add_method_declaration
19826            (cp_parser_objc_method_signature (parser));
19827           cp_parser_consume_semicolon_at_end_of_statement (parser);
19828         }
19829       else
19830         /* Allow for interspersed non-ObjC++ code.  */
19831         cp_parser_objc_interstitial_code (parser);
19832
19833       token = cp_lexer_peek_token (parser->lexer);
19834     }
19835
19836   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19837   objc_finish_interface ();
19838 }
19839
19840 /* Parse an Objective-C method definition list.  */
19841
19842 static void
19843 cp_parser_objc_method_definition_list (cp_parser* parser)
19844 {
19845   cp_token *token = cp_lexer_peek_token (parser->lexer);
19846
19847   while (token->keyword != RID_AT_END)
19848     {
19849       tree meth;
19850
19851       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19852         {
19853           push_deferring_access_checks (dk_deferred);
19854           objc_start_method_definition
19855            (cp_parser_objc_method_signature (parser));
19856
19857           /* For historical reasons, we accept an optional semicolon.  */
19858           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19859             cp_lexer_consume_token (parser->lexer);
19860
19861           perform_deferred_access_checks ();
19862           stop_deferring_access_checks ();
19863           meth = cp_parser_function_definition_after_declarator (parser,
19864                                                                  false);
19865           pop_deferring_access_checks ();
19866           objc_finish_method_definition (meth);
19867         }
19868       else
19869         /* Allow for interspersed non-ObjC++ code.  */
19870         cp_parser_objc_interstitial_code (parser);
19871
19872       token = cp_lexer_peek_token (parser->lexer);
19873     }
19874
19875   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19876   objc_finish_implementation ();
19877 }
19878
19879 /* Parse Objective-C ivars.  */
19880
19881 static void
19882 cp_parser_objc_class_ivars (cp_parser* parser)
19883 {
19884   cp_token *token = cp_lexer_peek_token (parser->lexer);
19885
19886   if (token->type != CPP_OPEN_BRACE)
19887     return;     /* No ivars specified.  */
19888
19889   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19890   token = cp_lexer_peek_token (parser->lexer);
19891
19892   while (token->type != CPP_CLOSE_BRACE)
19893     {
19894       cp_decl_specifier_seq declspecs;
19895       int decl_class_or_enum_p;
19896       tree prefix_attributes;
19897
19898       cp_parser_objc_visibility_spec (parser);
19899
19900       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19901         break;
19902
19903       cp_parser_decl_specifier_seq (parser,
19904                                     CP_PARSER_FLAGS_OPTIONAL,
19905                                     &declspecs,
19906                                     &decl_class_or_enum_p);
19907       prefix_attributes = declspecs.attributes;
19908       declspecs.attributes = NULL_TREE;
19909
19910       /* Keep going until we hit the `;' at the end of the
19911          declaration.  */
19912       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19913         {
19914           tree width = NULL_TREE, attributes, first_attribute, decl;
19915           cp_declarator *declarator = NULL;
19916           int ctor_dtor_or_conv_p;
19917
19918           /* Check for a (possibly unnamed) bitfield declaration.  */
19919           token = cp_lexer_peek_token (parser->lexer);
19920           if (token->type == CPP_COLON)
19921             goto eat_colon;
19922
19923           if (token->type == CPP_NAME
19924               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19925                   == CPP_COLON))
19926             {
19927               /* Get the name of the bitfield.  */
19928               declarator = make_id_declarator (NULL_TREE,
19929                                                cp_parser_identifier (parser),
19930                                                sfk_none);
19931
19932              eat_colon:
19933               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19934               /* Get the width of the bitfield.  */
19935               width
19936                 = cp_parser_constant_expression (parser,
19937                                                  /*allow_non_constant=*/false,
19938                                                  NULL);
19939             }
19940           else
19941             {
19942               /* Parse the declarator.  */
19943               declarator
19944                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19945                                         &ctor_dtor_or_conv_p,
19946                                         /*parenthesized_p=*/NULL,
19947                                         /*member_p=*/false);
19948             }
19949
19950           /* Look for attributes that apply to the ivar.  */
19951           attributes = cp_parser_attributes_opt (parser);
19952           /* Remember which attributes are prefix attributes and
19953              which are not.  */
19954           first_attribute = attributes;
19955           /* Combine the attributes.  */
19956           attributes = chainon (prefix_attributes, attributes);
19957
19958           if (width)
19959               /* Create the bitfield declaration.  */
19960               decl = grokbitfield (declarator, &declspecs,
19961                                    width,
19962                                    attributes);
19963           else
19964             decl = grokfield (declarator, &declspecs,
19965                               NULL_TREE, /*init_const_expr_p=*/false,
19966                               NULL_TREE, attributes);
19967
19968           /* Add the instance variable.  */
19969           objc_add_instance_variable (decl);
19970
19971           /* Reset PREFIX_ATTRIBUTES.  */
19972           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19973             attributes = TREE_CHAIN (attributes);
19974           if (attributes)
19975             TREE_CHAIN (attributes) = NULL_TREE;
19976
19977           token = cp_lexer_peek_token (parser->lexer);
19978
19979           if (token->type == CPP_COMMA)
19980             {
19981               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19982               continue;
19983             }
19984           break;
19985         }
19986
19987       cp_parser_consume_semicolon_at_end_of_statement (parser);
19988       token = cp_lexer_peek_token (parser->lexer);
19989     }
19990
19991   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19992   /* For historical reasons, we accept an optional semicolon.  */
19993   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19994     cp_lexer_consume_token (parser->lexer);
19995 }
19996
19997 /* Parse an Objective-C protocol declaration.  */
19998
19999 static void
20000 cp_parser_objc_protocol_declaration (cp_parser* parser)
20001 {
20002   tree proto, protorefs;
20003   cp_token *tok;
20004
20005   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20006   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20007     {
20008       tok = cp_lexer_peek_token (parser->lexer);
20009       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20010       goto finish;
20011     }
20012
20013   /* See if we have a forward declaration or a definition.  */
20014   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20015
20016   /* Try a forward declaration first.  */
20017   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20018     {
20019       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20020      finish:
20021       cp_parser_consume_semicolon_at_end_of_statement (parser);
20022     }
20023
20024   /* Ok, we got a full-fledged definition (or at least should).  */
20025   else
20026     {
20027       proto = cp_parser_identifier (parser);
20028       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20029       objc_start_protocol (proto, protorefs);
20030       cp_parser_objc_method_prototype_list (parser);
20031     }
20032 }
20033
20034 /* Parse an Objective-C superclass or category.  */
20035
20036 static void
20037 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20038                                                           tree *categ)
20039 {
20040   cp_token *next = cp_lexer_peek_token (parser->lexer);
20041
20042   *super = *categ = NULL_TREE;
20043   if (next->type == CPP_COLON)
20044     {
20045       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20046       *super = cp_parser_identifier (parser);
20047     }
20048   else if (next->type == CPP_OPEN_PAREN)
20049     {
20050       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20051       *categ = cp_parser_identifier (parser);
20052       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20053     }
20054 }
20055
20056 /* Parse an Objective-C class interface.  */
20057
20058 static void
20059 cp_parser_objc_class_interface (cp_parser* parser)
20060 {
20061   tree name, super, categ, protos;
20062
20063   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20064   name = cp_parser_identifier (parser);
20065   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20066   protos = cp_parser_objc_protocol_refs_opt (parser);
20067
20068   /* We have either a class or a category on our hands.  */
20069   if (categ)
20070     objc_start_category_interface (name, categ, protos);
20071   else
20072     {
20073       objc_start_class_interface (name, super, protos);
20074       /* Handle instance variable declarations, if any.  */
20075       cp_parser_objc_class_ivars (parser);
20076       objc_continue_interface ();
20077     }
20078
20079   cp_parser_objc_method_prototype_list (parser);
20080 }
20081
20082 /* Parse an Objective-C class implementation.  */
20083
20084 static void
20085 cp_parser_objc_class_implementation (cp_parser* parser)
20086 {
20087   tree name, super, categ;
20088
20089   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20090   name = cp_parser_identifier (parser);
20091   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20092
20093   /* We have either a class or a category on our hands.  */
20094   if (categ)
20095     objc_start_category_implementation (name, categ);
20096   else
20097     {
20098       objc_start_class_implementation (name, super);
20099       /* Handle instance variable declarations, if any.  */
20100       cp_parser_objc_class_ivars (parser);
20101       objc_continue_implementation ();
20102     }
20103
20104   cp_parser_objc_method_definition_list (parser);
20105 }
20106
20107 /* Consume the @end token and finish off the implementation.  */
20108
20109 static void
20110 cp_parser_objc_end_implementation (cp_parser* parser)
20111 {
20112   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20113   objc_finish_implementation ();
20114 }
20115
20116 /* Parse an Objective-C declaration.  */
20117
20118 static void
20119 cp_parser_objc_declaration (cp_parser* parser)
20120 {
20121   /* Try to figure out what kind of declaration is present.  */
20122   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20123
20124   switch (kwd->keyword)
20125     {
20126     case RID_AT_ALIAS:
20127       cp_parser_objc_alias_declaration (parser);
20128       break;
20129     case RID_AT_CLASS:
20130       cp_parser_objc_class_declaration (parser);
20131       break;
20132     case RID_AT_PROTOCOL:
20133       cp_parser_objc_protocol_declaration (parser);
20134       break;
20135     case RID_AT_INTERFACE:
20136       cp_parser_objc_class_interface (parser);
20137       break;
20138     case RID_AT_IMPLEMENTATION:
20139       cp_parser_objc_class_implementation (parser);
20140       break;
20141     case RID_AT_END:
20142       cp_parser_objc_end_implementation (parser);
20143       break;
20144     default:
20145       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20146              &kwd->location, kwd->u.value);
20147       cp_parser_skip_to_end_of_block_or_statement (parser);
20148     }
20149 }
20150
20151 /* Parse an Objective-C try-catch-finally statement.
20152
20153    objc-try-catch-finally-stmt:
20154      @try compound-statement objc-catch-clause-seq [opt]
20155        objc-finally-clause [opt]
20156
20157    objc-catch-clause-seq:
20158      objc-catch-clause objc-catch-clause-seq [opt]
20159
20160    objc-catch-clause:
20161      @catch ( exception-declaration ) compound-statement
20162
20163    objc-finally-clause
20164      @finally compound-statement
20165
20166    Returns NULL_TREE.  */
20167
20168 static tree
20169 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20170   location_t location;
20171   tree stmt;
20172
20173   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20174   location = cp_lexer_peek_token (parser->lexer)->location;
20175   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20176      node, lest it get absorbed into the surrounding block.  */
20177   stmt = push_stmt_list ();
20178   cp_parser_compound_statement (parser, NULL, false);
20179   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20180
20181   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20182     {
20183       cp_parameter_declarator *parmdecl;
20184       tree parm;
20185
20186       cp_lexer_consume_token (parser->lexer);
20187       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20188       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20189       parm = grokdeclarator (parmdecl->declarator,
20190                              &parmdecl->decl_specifiers,
20191                              PARM, /*initialized=*/0,
20192                              /*attrlist=*/NULL);
20193       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20194       objc_begin_catch_clause (parm);
20195       cp_parser_compound_statement (parser, NULL, false);
20196       objc_finish_catch_clause ();
20197     }
20198
20199   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20200     {
20201       cp_lexer_consume_token (parser->lexer);
20202       location = cp_lexer_peek_token (parser->lexer)->location;
20203       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20204          node, lest it get absorbed into the surrounding block.  */
20205       stmt = push_stmt_list ();
20206       cp_parser_compound_statement (parser, NULL, false);
20207       objc_build_finally_clause (location, pop_stmt_list (stmt));
20208     }
20209
20210   return objc_finish_try_stmt ();
20211 }
20212
20213 /* Parse an Objective-C synchronized statement.
20214
20215    objc-synchronized-stmt:
20216      @synchronized ( expression ) compound-statement
20217
20218    Returns NULL_TREE.  */
20219
20220 static tree
20221 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20222   location_t location;
20223   tree lock, stmt;
20224
20225   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20226
20227   location = cp_lexer_peek_token (parser->lexer)->location;
20228   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20229   lock = cp_parser_expression (parser, false, NULL);
20230   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20231
20232   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20233      node, lest it get absorbed into the surrounding block.  */
20234   stmt = push_stmt_list ();
20235   cp_parser_compound_statement (parser, NULL, false);
20236
20237   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20238 }
20239
20240 /* Parse an Objective-C throw statement.
20241
20242    objc-throw-stmt:
20243      @throw assignment-expression [opt] ;
20244
20245    Returns a constructed '@throw' statement.  */
20246
20247 static tree
20248 cp_parser_objc_throw_statement (cp_parser *parser) {
20249   tree expr = NULL_TREE;
20250
20251   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20252
20253   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20254     expr = cp_parser_assignment_expression (parser, false, NULL);
20255
20256   cp_parser_consume_semicolon_at_end_of_statement (parser);
20257
20258   return objc_build_throw_stmt (expr);
20259 }
20260
20261 /* Parse an Objective-C statement.  */
20262
20263 static tree
20264 cp_parser_objc_statement (cp_parser * parser) {
20265   /* Try to figure out what kind of declaration is present.  */
20266   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20267
20268   switch (kwd->keyword)
20269     {
20270     case RID_AT_TRY:
20271       return cp_parser_objc_try_catch_finally_statement (parser);
20272     case RID_AT_SYNCHRONIZED:
20273       return cp_parser_objc_synchronized_statement (parser);
20274     case RID_AT_THROW:
20275       return cp_parser_objc_throw_statement (parser);
20276     default:
20277       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20278              &kwd->location, kwd->u.value);
20279       cp_parser_skip_to_end_of_block_or_statement (parser);
20280     }
20281
20282   return error_mark_node;
20283 }
20284 \f
20285 /* OpenMP 2.5 parsing routines.  */
20286
20287 /* Returns name of the next clause.
20288    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20289    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20290    returned and the token is consumed.  */
20291
20292 static pragma_omp_clause
20293 cp_parser_omp_clause_name (cp_parser *parser)
20294 {
20295   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20296
20297   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20298     result = PRAGMA_OMP_CLAUSE_IF;
20299   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20300     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20301   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20302     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20303   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20304     {
20305       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20306       const char *p = IDENTIFIER_POINTER (id);
20307
20308       switch (p[0])
20309         {
20310         case 'c':
20311           if (!strcmp ("collapse", p))
20312             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20313           else if (!strcmp ("copyin", p))
20314             result = PRAGMA_OMP_CLAUSE_COPYIN;
20315           else if (!strcmp ("copyprivate", p))
20316             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20317           break;
20318         case 'f':
20319           if (!strcmp ("firstprivate", p))
20320             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20321           break;
20322         case 'l':
20323           if (!strcmp ("lastprivate", p))
20324             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20325           break;
20326         case 'n':
20327           if (!strcmp ("nowait", p))
20328             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20329           else if (!strcmp ("num_threads", p))
20330             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20331           break;
20332         case 'o':
20333           if (!strcmp ("ordered", p))
20334             result = PRAGMA_OMP_CLAUSE_ORDERED;
20335           break;
20336         case 'r':
20337           if (!strcmp ("reduction", p))
20338             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20339           break;
20340         case 's':
20341           if (!strcmp ("schedule", p))
20342             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20343           else if (!strcmp ("shared", p))
20344             result = PRAGMA_OMP_CLAUSE_SHARED;
20345           break;
20346         case 'u':
20347           if (!strcmp ("untied", p))
20348             result = PRAGMA_OMP_CLAUSE_UNTIED;
20349           break;
20350         }
20351     }
20352
20353   if (result != PRAGMA_OMP_CLAUSE_NONE)
20354     cp_lexer_consume_token (parser->lexer);
20355
20356   return result;
20357 }
20358
20359 /* Validate that a clause of the given type does not already exist.  */
20360
20361 static void
20362 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20363                            const char *name, location_t location)
20364 {
20365   tree c;
20366
20367   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20368     if (OMP_CLAUSE_CODE (c) == code)
20369       {
20370         error ("%Htoo many %qs clauses", &location, name);
20371         break;
20372       }
20373 }
20374
20375 /* OpenMP 2.5:
20376    variable-list:
20377      identifier
20378      variable-list , identifier
20379
20380    In addition, we match a closing parenthesis.  An opening parenthesis
20381    will have been consumed by the caller.
20382
20383    If KIND is nonzero, create the appropriate node and install the decl
20384    in OMP_CLAUSE_DECL and add the node to the head of the list.
20385
20386    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20387    return the list created.  */
20388
20389 static tree
20390 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20391                                 tree list)
20392 {
20393   cp_token *token;
20394   while (1)
20395     {
20396       tree name, decl;
20397
20398       token = cp_lexer_peek_token (parser->lexer);
20399       name = cp_parser_id_expression (parser, /*template_p=*/false,
20400                                       /*check_dependency_p=*/true,
20401                                       /*template_p=*/NULL,
20402                                       /*declarator_p=*/false,
20403                                       /*optional_p=*/false);
20404       if (name == error_mark_node)
20405         goto skip_comma;
20406
20407       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20408       if (decl == error_mark_node)
20409         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20410       else if (kind != 0)
20411         {
20412           tree u = build_omp_clause (kind);
20413           OMP_CLAUSE_DECL (u) = decl;
20414           OMP_CLAUSE_CHAIN (u) = list;
20415           list = u;
20416         }
20417       else
20418         list = tree_cons (decl, NULL_TREE, list);
20419
20420     get_comma:
20421       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20422         break;
20423       cp_lexer_consume_token (parser->lexer);
20424     }
20425
20426   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20427     {
20428       int ending;
20429
20430       /* Try to resync to an unnested comma.  Copied from
20431          cp_parser_parenthesized_expression_list.  */
20432     skip_comma:
20433       ending = cp_parser_skip_to_closing_parenthesis (parser,
20434                                                       /*recovering=*/true,
20435                                                       /*or_comma=*/true,
20436                                                       /*consume_paren=*/true);
20437       if (ending < 0)
20438         goto get_comma;
20439     }
20440
20441   return list;
20442 }
20443
20444 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20445    common case for omp clauses.  */
20446
20447 static tree
20448 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20449 {
20450   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20451     return cp_parser_omp_var_list_no_open (parser, kind, list);
20452   return list;
20453 }
20454
20455 /* OpenMP 3.0:
20456    collapse ( constant-expression ) */
20457
20458 static tree
20459 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20460 {
20461   tree c, num;
20462   location_t loc;
20463   HOST_WIDE_INT n;
20464
20465   loc = cp_lexer_peek_token (parser->lexer)->location;
20466   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20467     return list;
20468
20469   num = cp_parser_constant_expression (parser, false, NULL);
20470
20471   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20472     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20473                                            /*or_comma=*/false,
20474                                            /*consume_paren=*/true);
20475
20476   if (num == error_mark_node)
20477     return list;
20478   num = fold_non_dependent_expr (num);
20479   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20480       || !host_integerp (num, 0)
20481       || (n = tree_low_cst (num, 0)) <= 0
20482       || (int) n != n)
20483     {
20484       error ("%Hcollapse argument needs positive constant integer expression",
20485              &loc);
20486       return list;
20487     }
20488
20489   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20490   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20491   OMP_CLAUSE_CHAIN (c) = list;
20492   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20493
20494   return c;
20495 }
20496
20497 /* OpenMP 2.5:
20498    default ( shared | none ) */
20499
20500 static tree
20501 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20502 {
20503   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20504   tree c;
20505
20506   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20507     return list;
20508   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20509     {
20510       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20511       const char *p = IDENTIFIER_POINTER (id);
20512
20513       switch (p[0])
20514         {
20515         case 'n':
20516           if (strcmp ("none", p) != 0)
20517             goto invalid_kind;
20518           kind = OMP_CLAUSE_DEFAULT_NONE;
20519           break;
20520
20521         case 's':
20522           if (strcmp ("shared", p) != 0)
20523             goto invalid_kind;
20524           kind = OMP_CLAUSE_DEFAULT_SHARED;
20525           break;
20526
20527         default:
20528           goto invalid_kind;
20529         }
20530
20531       cp_lexer_consume_token (parser->lexer);
20532     }
20533   else
20534     {
20535     invalid_kind:
20536       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20537     }
20538
20539   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20540     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20541                                            /*or_comma=*/false,
20542                                            /*consume_paren=*/true);
20543
20544   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20545     return list;
20546
20547   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20548   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20549   OMP_CLAUSE_CHAIN (c) = list;
20550   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20551
20552   return c;
20553 }
20554
20555 /* OpenMP 2.5:
20556    if ( expression ) */
20557
20558 static tree
20559 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20560 {
20561   tree t, c;
20562
20563   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20564     return list;
20565
20566   t = cp_parser_condition (parser);
20567
20568   if (t == error_mark_node
20569       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20570     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20571                                            /*or_comma=*/false,
20572                                            /*consume_paren=*/true);
20573
20574   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20575
20576   c = build_omp_clause (OMP_CLAUSE_IF);
20577   OMP_CLAUSE_IF_EXPR (c) = t;
20578   OMP_CLAUSE_CHAIN (c) = list;
20579
20580   return c;
20581 }
20582
20583 /* OpenMP 2.5:
20584    nowait */
20585
20586 static tree
20587 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20588                              tree list, location_t location)
20589 {
20590   tree c;
20591
20592   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20593
20594   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20595   OMP_CLAUSE_CHAIN (c) = list;
20596   return c;
20597 }
20598
20599 /* OpenMP 2.5:
20600    num_threads ( expression ) */
20601
20602 static tree
20603 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20604                                   location_t location)
20605 {
20606   tree t, c;
20607
20608   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20609     return list;
20610
20611   t = cp_parser_expression (parser, false, NULL);
20612
20613   if (t == error_mark_node
20614       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20615     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20616                                            /*or_comma=*/false,
20617                                            /*consume_paren=*/true);
20618
20619   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20620                              "num_threads", location);
20621
20622   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20623   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20624   OMP_CLAUSE_CHAIN (c) = list;
20625
20626   return c;
20627 }
20628
20629 /* OpenMP 2.5:
20630    ordered */
20631
20632 static tree
20633 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20634                               tree list, location_t location)
20635 {
20636   tree c;
20637
20638   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20639                              "ordered", location);
20640
20641   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20642   OMP_CLAUSE_CHAIN (c) = list;
20643   return c;
20644 }
20645
20646 /* OpenMP 2.5:
20647    reduction ( reduction-operator : variable-list )
20648
20649    reduction-operator:
20650      One of: + * - & ^ | && || */
20651
20652 static tree
20653 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20654 {
20655   enum tree_code code;
20656   tree nlist, c;
20657
20658   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20659     return list;
20660
20661   switch (cp_lexer_peek_token (parser->lexer)->type)
20662     {
20663     case CPP_PLUS:
20664       code = PLUS_EXPR;
20665       break;
20666     case CPP_MULT:
20667       code = MULT_EXPR;
20668       break;
20669     case CPP_MINUS:
20670       code = MINUS_EXPR;
20671       break;
20672     case CPP_AND:
20673       code = BIT_AND_EXPR;
20674       break;
20675     case CPP_XOR:
20676       code = BIT_XOR_EXPR;
20677       break;
20678     case CPP_OR:
20679       code = BIT_IOR_EXPR;
20680       break;
20681     case CPP_AND_AND:
20682       code = TRUTH_ANDIF_EXPR;
20683       break;
20684     case CPP_OR_OR:
20685       code = TRUTH_ORIF_EXPR;
20686       break;
20687     default:
20688       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20689                                "%<|%>, %<&&%>, or %<||%>");
20690     resync_fail:
20691       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20692                                              /*or_comma=*/false,
20693                                              /*consume_paren=*/true);
20694       return list;
20695     }
20696   cp_lexer_consume_token (parser->lexer);
20697
20698   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20699     goto resync_fail;
20700
20701   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20702   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20703     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20704
20705   return nlist;
20706 }
20707
20708 /* OpenMP 2.5:
20709    schedule ( schedule-kind )
20710    schedule ( schedule-kind , expression )
20711
20712    schedule-kind:
20713      static | dynamic | guided | runtime | auto  */
20714
20715 static tree
20716 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20717 {
20718   tree c, t;
20719
20720   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20721     return list;
20722
20723   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20724
20725   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20726     {
20727       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20728       const char *p = IDENTIFIER_POINTER (id);
20729
20730       switch (p[0])
20731         {
20732         case 'd':
20733           if (strcmp ("dynamic", p) != 0)
20734             goto invalid_kind;
20735           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20736           break;
20737
20738         case 'g':
20739           if (strcmp ("guided", p) != 0)
20740             goto invalid_kind;
20741           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20742           break;
20743
20744         case 'r':
20745           if (strcmp ("runtime", p) != 0)
20746             goto invalid_kind;
20747           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20748           break;
20749
20750         default:
20751           goto invalid_kind;
20752         }
20753     }
20754   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20755     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20756   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20757     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20758   else
20759     goto invalid_kind;
20760   cp_lexer_consume_token (parser->lexer);
20761
20762   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20763     {
20764       cp_token *token;
20765       cp_lexer_consume_token (parser->lexer);
20766
20767       token = cp_lexer_peek_token (parser->lexer);
20768       t = cp_parser_assignment_expression (parser, false, NULL);
20769
20770       if (t == error_mark_node)
20771         goto resync_fail;
20772       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20773         error ("%Hschedule %<runtime%> does not take "
20774                "a %<chunk_size%> parameter", &token->location);
20775       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20776         error ("%Hschedule %<auto%> does not take "
20777                "a %<chunk_size%> parameter", &token->location);
20778       else
20779         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20780
20781       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20782         goto resync_fail;
20783     }
20784   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20785     goto resync_fail;
20786
20787   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20788   OMP_CLAUSE_CHAIN (c) = list;
20789   return c;
20790
20791  invalid_kind:
20792   cp_parser_error (parser, "invalid schedule kind");
20793  resync_fail:
20794   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20795                                          /*or_comma=*/false,
20796                                          /*consume_paren=*/true);
20797   return list;
20798 }
20799
20800 /* OpenMP 3.0:
20801    untied */
20802
20803 static tree
20804 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20805                              tree list, location_t location)
20806 {
20807   tree c;
20808
20809   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20810
20811   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20812   OMP_CLAUSE_CHAIN (c) = list;
20813   return c;
20814 }
20815
20816 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20817    is a bitmask in MASK.  Return the list of clauses found; the result
20818    of clause default goes in *pdefault.  */
20819
20820 static tree
20821 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20822                            const char *where, cp_token *pragma_tok)
20823 {
20824   tree clauses = NULL;
20825   bool first = true;
20826   cp_token *token = NULL;
20827
20828   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20829     {
20830       pragma_omp_clause c_kind;
20831       const char *c_name;
20832       tree prev = clauses;
20833
20834       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20835         cp_lexer_consume_token (parser->lexer);
20836
20837       token = cp_lexer_peek_token (parser->lexer);
20838       c_kind = cp_parser_omp_clause_name (parser);
20839       first = false;
20840
20841       switch (c_kind)
20842         {
20843         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20844           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20845                                                    token->location);
20846           c_name = "collapse";
20847           break;
20848         case PRAGMA_OMP_CLAUSE_COPYIN:
20849           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20850           c_name = "copyin";
20851           break;
20852         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20853           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20854                                             clauses);
20855           c_name = "copyprivate";
20856           break;
20857         case PRAGMA_OMP_CLAUSE_DEFAULT:
20858           clauses = cp_parser_omp_clause_default (parser, clauses,
20859                                                   token->location);
20860           c_name = "default";
20861           break;
20862         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20863           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20864                                             clauses);
20865           c_name = "firstprivate";
20866           break;
20867         case PRAGMA_OMP_CLAUSE_IF:
20868           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20869           c_name = "if";
20870           break;
20871         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20872           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20873                                             clauses);
20874           c_name = "lastprivate";
20875           break;
20876         case PRAGMA_OMP_CLAUSE_NOWAIT:
20877           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20878           c_name = "nowait";
20879           break;
20880         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20881           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20882                                                       token->location);
20883           c_name = "num_threads";
20884           break;
20885         case PRAGMA_OMP_CLAUSE_ORDERED:
20886           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20887                                                   token->location);
20888           c_name = "ordered";
20889           break;
20890         case PRAGMA_OMP_CLAUSE_PRIVATE:
20891           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20892                                             clauses);
20893           c_name = "private";
20894           break;
20895         case PRAGMA_OMP_CLAUSE_REDUCTION:
20896           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20897           c_name = "reduction";
20898           break;
20899         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20900           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20901                                                    token->location);
20902           c_name = "schedule";
20903           break;
20904         case PRAGMA_OMP_CLAUSE_SHARED:
20905           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20906                                             clauses);
20907           c_name = "shared";
20908           break;
20909         case PRAGMA_OMP_CLAUSE_UNTIED:
20910           clauses = cp_parser_omp_clause_untied (parser, clauses,
20911                                                  token->location);
20912           c_name = "nowait";
20913           break;
20914         default:
20915           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20916           goto saw_error;
20917         }
20918
20919       if (((mask >> c_kind) & 1) == 0)
20920         {
20921           /* Remove the invalid clause(s) from the list to avoid
20922              confusing the rest of the compiler.  */
20923           clauses = prev;
20924           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20925         }
20926     }
20927  saw_error:
20928   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20929   return finish_omp_clauses (clauses);
20930 }
20931
20932 /* OpenMP 2.5:
20933    structured-block:
20934      statement
20935
20936    In practice, we're also interested in adding the statement to an
20937    outer node.  So it is convenient if we work around the fact that
20938    cp_parser_statement calls add_stmt.  */
20939
20940 static unsigned
20941 cp_parser_begin_omp_structured_block (cp_parser *parser)
20942 {
20943   unsigned save = parser->in_statement;
20944
20945   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20946      This preserves the "not within loop or switch" style error messages
20947      for nonsense cases like
20948         void foo() {
20949         #pragma omp single
20950           break;
20951         }
20952   */
20953   if (parser->in_statement)
20954     parser->in_statement = IN_OMP_BLOCK;
20955
20956   return save;
20957 }
20958
20959 static void
20960 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20961 {
20962   parser->in_statement = save;
20963 }
20964
20965 static tree
20966 cp_parser_omp_structured_block (cp_parser *parser)
20967 {
20968   tree stmt = begin_omp_structured_block ();
20969   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20970
20971   cp_parser_statement (parser, NULL_TREE, false, NULL);
20972
20973   cp_parser_end_omp_structured_block (parser, save);
20974   return finish_omp_structured_block (stmt);
20975 }
20976
20977 /* OpenMP 2.5:
20978    # pragma omp atomic new-line
20979      expression-stmt
20980
20981    expression-stmt:
20982      x binop= expr | x++ | ++x | x-- | --x
20983    binop:
20984      +, *, -, /, &, ^, |, <<, >>
20985
20986   where x is an lvalue expression with scalar type.  */
20987
20988 static void
20989 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20990 {
20991   tree lhs, rhs;
20992   enum tree_code code;
20993
20994   cp_parser_require_pragma_eol (parser, pragma_tok);
20995
20996   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20997                                     /*cast_p=*/false, NULL);
20998   switch (TREE_CODE (lhs))
20999     {
21000     case ERROR_MARK:
21001       goto saw_error;
21002
21003     case PREINCREMENT_EXPR:
21004     case POSTINCREMENT_EXPR:
21005       lhs = TREE_OPERAND (lhs, 0);
21006       code = PLUS_EXPR;
21007       rhs = integer_one_node;
21008       break;
21009
21010     case PREDECREMENT_EXPR:
21011     case POSTDECREMENT_EXPR:
21012       lhs = TREE_OPERAND (lhs, 0);
21013       code = MINUS_EXPR;
21014       rhs = integer_one_node;
21015       break;
21016
21017     case COMPOUND_EXPR:
21018       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
21019          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
21020          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
21021          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
21022          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
21023                                              (TREE_OPERAND (lhs, 1), 0), 0)))
21024             == BOOLEAN_TYPE)
21025        /* Undo effects of boolean_increment for post {in,de}crement.  */
21026        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
21027       /* FALLTHRU */
21028     case MODIFY_EXPR:
21029       if (TREE_CODE (lhs) == MODIFY_EXPR
21030          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
21031        {
21032          /* Undo effects of boolean_increment.  */
21033          if (integer_onep (TREE_OPERAND (lhs, 1)))
21034            {
21035              /* This is pre or post increment.  */
21036              rhs = TREE_OPERAND (lhs, 1);
21037              lhs = TREE_OPERAND (lhs, 0);
21038              code = NOP_EXPR;
21039              break;
21040            }
21041        }
21042       /* FALLTHRU */
21043     default:
21044       switch (cp_lexer_peek_token (parser->lexer)->type)
21045         {
21046         case CPP_MULT_EQ:
21047           code = MULT_EXPR;
21048           break;
21049         case CPP_DIV_EQ:
21050           code = TRUNC_DIV_EXPR;
21051           break;
21052         case CPP_PLUS_EQ:
21053           code = PLUS_EXPR;
21054           break;
21055         case CPP_MINUS_EQ:
21056           code = MINUS_EXPR;
21057           break;
21058         case CPP_LSHIFT_EQ:
21059           code = LSHIFT_EXPR;
21060           break;
21061         case CPP_RSHIFT_EQ:
21062           code = RSHIFT_EXPR;
21063           break;
21064         case CPP_AND_EQ:
21065           code = BIT_AND_EXPR;
21066           break;
21067         case CPP_OR_EQ:
21068           code = BIT_IOR_EXPR;
21069           break;
21070         case CPP_XOR_EQ:
21071           code = BIT_XOR_EXPR;
21072           break;
21073         default:
21074           cp_parser_error (parser,
21075                            "invalid operator for %<#pragma omp atomic%>");
21076           goto saw_error;
21077         }
21078       cp_lexer_consume_token (parser->lexer);
21079
21080       rhs = cp_parser_expression (parser, false, NULL);
21081       if (rhs == error_mark_node)
21082         goto saw_error;
21083       break;
21084     }
21085   finish_omp_atomic (code, lhs, rhs);
21086   cp_parser_consume_semicolon_at_end_of_statement (parser);
21087   return;
21088
21089  saw_error:
21090   cp_parser_skip_to_end_of_block_or_statement (parser);
21091 }
21092
21093
21094 /* OpenMP 2.5:
21095    # pragma omp barrier new-line  */
21096
21097 static void
21098 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21099 {
21100   cp_parser_require_pragma_eol (parser, pragma_tok);
21101   finish_omp_barrier ();
21102 }
21103
21104 /* OpenMP 2.5:
21105    # pragma omp critical [(name)] new-line
21106      structured-block  */
21107
21108 static tree
21109 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21110 {
21111   tree stmt, name = NULL;
21112
21113   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21114     {
21115       cp_lexer_consume_token (parser->lexer);
21116
21117       name = cp_parser_identifier (parser);
21118
21119       if (name == error_mark_node
21120           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21121         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21122                                                /*or_comma=*/false,
21123                                                /*consume_paren=*/true);
21124       if (name == error_mark_node)
21125         name = NULL;
21126     }
21127   cp_parser_require_pragma_eol (parser, pragma_tok);
21128
21129   stmt = cp_parser_omp_structured_block (parser);
21130   return c_finish_omp_critical (stmt, name);
21131 }
21132
21133 /* OpenMP 2.5:
21134    # pragma omp flush flush-vars[opt] new-line
21135
21136    flush-vars:
21137      ( variable-list ) */
21138
21139 static void
21140 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21141 {
21142   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21143     (void) cp_parser_omp_var_list (parser, 0, NULL);
21144   cp_parser_require_pragma_eol (parser, pragma_tok);
21145
21146   finish_omp_flush ();
21147 }
21148
21149 /* Helper function, to parse omp for increment expression.  */
21150
21151 static tree
21152 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21153 {
21154   tree cond = cp_parser_binary_expression (parser, false, true,
21155                                            PREC_NOT_OPERATOR, NULL);
21156   bool overloaded_p;
21157
21158   if (cond == error_mark_node
21159       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21160     {
21161       cp_parser_skip_to_end_of_statement (parser);
21162       return error_mark_node;
21163     }
21164
21165   switch (TREE_CODE (cond))
21166     {
21167     case GT_EXPR:
21168     case GE_EXPR:
21169     case LT_EXPR:
21170     case LE_EXPR:
21171       break;
21172     default:
21173       return error_mark_node;
21174     }
21175
21176   /* If decl is an iterator, preserve LHS and RHS of the relational
21177      expr until finish_omp_for.  */
21178   if (decl
21179       && (type_dependent_expression_p (decl)
21180           || CLASS_TYPE_P (TREE_TYPE (decl))))
21181     return cond;
21182
21183   return build_x_binary_op (TREE_CODE (cond),
21184                             TREE_OPERAND (cond, 0), ERROR_MARK,
21185                             TREE_OPERAND (cond, 1), ERROR_MARK,
21186                             &overloaded_p, tf_warning_or_error);
21187 }
21188
21189 /* Helper function, to parse omp for increment expression.  */
21190
21191 static tree
21192 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21193 {
21194   cp_token *token = cp_lexer_peek_token (parser->lexer);
21195   enum tree_code op;
21196   tree lhs, rhs;
21197   cp_id_kind idk;
21198   bool decl_first;
21199
21200   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21201     {
21202       op = (token->type == CPP_PLUS_PLUS
21203             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21204       cp_lexer_consume_token (parser->lexer);
21205       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21206       if (lhs != decl)
21207         return error_mark_node;
21208       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21209     }
21210
21211   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21212   if (lhs != decl)
21213     return error_mark_node;
21214
21215   token = cp_lexer_peek_token (parser->lexer);
21216   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21217     {
21218       op = (token->type == CPP_PLUS_PLUS
21219             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21220       cp_lexer_consume_token (parser->lexer);
21221       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21222     }
21223
21224   op = cp_parser_assignment_operator_opt (parser);
21225   if (op == ERROR_MARK)
21226     return error_mark_node;
21227
21228   if (op != NOP_EXPR)
21229     {
21230       rhs = cp_parser_assignment_expression (parser, false, NULL);
21231       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21232       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21233     }
21234
21235   lhs = cp_parser_binary_expression (parser, false, false,
21236                                      PREC_ADDITIVE_EXPRESSION, NULL);
21237   token = cp_lexer_peek_token (parser->lexer);
21238   decl_first = lhs == decl;
21239   if (decl_first)
21240     lhs = NULL_TREE;
21241   if (token->type != CPP_PLUS
21242       && token->type != CPP_MINUS)
21243     return error_mark_node;
21244
21245   do
21246     {
21247       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21248       cp_lexer_consume_token (parser->lexer);
21249       rhs = cp_parser_binary_expression (parser, false, false,
21250                                          PREC_ADDITIVE_EXPRESSION, NULL);
21251       token = cp_lexer_peek_token (parser->lexer);
21252       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21253         {
21254           if (lhs == NULL_TREE)
21255             {
21256               if (op == PLUS_EXPR)
21257                 lhs = rhs;
21258               else
21259                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21260             }
21261           else
21262             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21263                                      NULL, tf_warning_or_error);
21264         }
21265     }
21266   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21267
21268   if (!decl_first)
21269     {
21270       if (rhs != decl || op == MINUS_EXPR)
21271         return error_mark_node;
21272       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21273     }
21274   else
21275     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21276
21277   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21278 }
21279
21280 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21281
21282 static tree
21283 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21284 {
21285   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21286   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21287   tree this_pre_body, cl;
21288   location_t loc_first;
21289   bool collapse_err = false;
21290   int i, collapse = 1, nbraces = 0;
21291
21292   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21293     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21294       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21295
21296   gcc_assert (collapse >= 1);
21297
21298   declv = make_tree_vec (collapse);
21299   initv = make_tree_vec (collapse);
21300   condv = make_tree_vec (collapse);
21301   incrv = make_tree_vec (collapse);
21302
21303   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21304
21305   for (i = 0; i < collapse; i++)
21306     {
21307       int bracecount = 0;
21308       bool add_private_clause = false;
21309       location_t loc;
21310
21311       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21312         {
21313           cp_parser_error (parser, "for statement expected");
21314           return NULL;
21315         }
21316       loc = cp_lexer_consume_token (parser->lexer)->location;
21317
21318       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21319         return NULL;
21320
21321       init = decl = real_decl = NULL;
21322       this_pre_body = push_stmt_list ();
21323       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21324         {
21325           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21326
21327              init-expr:
21328                        var = lb
21329                        integer-type var = lb
21330                        random-access-iterator-type var = lb
21331                        pointer-type var = lb
21332           */
21333           cp_decl_specifier_seq type_specifiers;
21334
21335           /* First, try to parse as an initialized declaration.  See
21336              cp_parser_condition, from whence the bulk of this is copied.  */
21337
21338           cp_parser_parse_tentatively (parser);
21339           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21340                                         /*is_trailing_return=*/false,
21341                                         &type_specifiers);
21342           if (cp_parser_parse_definitely (parser))
21343             {
21344               /* If parsing a type specifier seq succeeded, then this
21345                  MUST be a initialized declaration.  */
21346               tree asm_specification, attributes;
21347               cp_declarator *declarator;
21348
21349               declarator = cp_parser_declarator (parser,
21350                                                  CP_PARSER_DECLARATOR_NAMED,
21351                                                  /*ctor_dtor_or_conv_p=*/NULL,
21352                                                  /*parenthesized_p=*/NULL,
21353                                                  /*member_p=*/false);
21354               attributes = cp_parser_attributes_opt (parser);
21355               asm_specification = cp_parser_asm_specification_opt (parser);
21356
21357               if (declarator == cp_error_declarator) 
21358                 cp_parser_skip_to_end_of_statement (parser);
21359
21360               else 
21361                 {
21362                   tree pushed_scope, auto_node;
21363
21364                   decl = start_decl (declarator, &type_specifiers,
21365                                      SD_INITIALIZED, attributes,
21366                                      /*prefix_attributes=*/NULL_TREE,
21367                                      &pushed_scope);
21368
21369                   auto_node = type_uses_auto (TREE_TYPE (decl));
21370                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21371                     {
21372                       if (cp_lexer_next_token_is (parser->lexer, 
21373                                                   CPP_OPEN_PAREN))
21374                         error ("parenthesized initialization is not allowed in "
21375                                "OpenMP %<for%> loop");
21376                       else
21377                         /* Trigger an error.  */
21378                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21379
21380                       init = error_mark_node;
21381                       cp_parser_skip_to_end_of_statement (parser);
21382                     }
21383                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21384                            || type_dependent_expression_p (decl)
21385                            || auto_node)
21386                     {
21387                       bool is_direct_init, is_non_constant_init;
21388
21389                       init = cp_parser_initializer (parser,
21390                                                     &is_direct_init,
21391                                                     &is_non_constant_init);
21392
21393                       if (auto_node && describable_type (init))
21394                         {
21395                           TREE_TYPE (decl)
21396                             = do_auto_deduction (TREE_TYPE (decl), init,
21397                                                  auto_node);
21398
21399                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21400                               && !type_dependent_expression_p (decl))
21401                             goto non_class;
21402                         }
21403                       
21404                       cp_finish_decl (decl, init, !is_non_constant_init,
21405                                       asm_specification,
21406                                       LOOKUP_ONLYCONVERTING);
21407                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21408                         {
21409                           for_block
21410                             = tree_cons (NULL, this_pre_body, for_block);
21411                           init = NULL_TREE;
21412                         }
21413                       else
21414                         init = pop_stmt_list (this_pre_body);
21415                       this_pre_body = NULL_TREE;
21416                     }
21417                   else
21418                     {
21419                       /* Consume '='.  */
21420                       cp_lexer_consume_token (parser->lexer);
21421                       init = cp_parser_assignment_expression (parser, false, NULL);
21422
21423                     non_class:
21424                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21425                         init = error_mark_node;
21426                       else
21427                         cp_finish_decl (decl, NULL_TREE,
21428                                         /*init_const_expr_p=*/false,
21429                                         asm_specification,
21430                                         LOOKUP_ONLYCONVERTING);
21431                     }
21432
21433                   if (pushed_scope)
21434                     pop_scope (pushed_scope);
21435                 }
21436             }
21437           else 
21438             {
21439               cp_id_kind idk;
21440               /* If parsing a type specifier sequence failed, then
21441                  this MUST be a simple expression.  */
21442               cp_parser_parse_tentatively (parser);
21443               decl = cp_parser_primary_expression (parser, false, false,
21444                                                    false, &idk);
21445               if (!cp_parser_error_occurred (parser)
21446                   && decl
21447                   && DECL_P (decl)
21448                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21449                 {
21450                   tree rhs;
21451
21452                   cp_parser_parse_definitely (parser);
21453                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21454                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21455                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21456                                                          rhs,
21457                                                          tf_warning_or_error));
21458                   add_private_clause = true;
21459                 }
21460               else
21461                 {
21462                   decl = NULL;
21463                   cp_parser_abort_tentative_parse (parser);
21464                   init = cp_parser_expression (parser, false, NULL);
21465                   if (init)
21466                     {
21467                       if (TREE_CODE (init) == MODIFY_EXPR
21468                           || TREE_CODE (init) == MODOP_EXPR)
21469                         real_decl = TREE_OPERAND (init, 0);
21470                     }
21471                 }
21472             }
21473         }
21474       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21475       if (this_pre_body)
21476         {
21477           this_pre_body = pop_stmt_list (this_pre_body);
21478           if (pre_body)
21479             {
21480               tree t = pre_body;
21481               pre_body = push_stmt_list ();
21482               add_stmt (t);
21483               add_stmt (this_pre_body);
21484               pre_body = pop_stmt_list (pre_body);
21485             }
21486           else
21487             pre_body = this_pre_body;
21488         }
21489
21490       if (decl)
21491         real_decl = decl;
21492       if (par_clauses != NULL && real_decl != NULL_TREE)
21493         {
21494           tree *c;
21495           for (c = par_clauses; *c ; )
21496             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21497                 && OMP_CLAUSE_DECL (*c) == real_decl)
21498               {
21499                 error ("%Hiteration variable %qD should not be firstprivate",
21500                        &loc, real_decl);
21501                 *c = OMP_CLAUSE_CHAIN (*c);
21502               }
21503             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21504                      && OMP_CLAUSE_DECL (*c) == real_decl)
21505               {
21506                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21507                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21508                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21509                 OMP_CLAUSE_DECL (l) = real_decl;
21510                 OMP_CLAUSE_CHAIN (l) = clauses;
21511                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21512                 clauses = l;
21513                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21514                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21515                 add_private_clause = false;
21516               }
21517             else
21518               {
21519                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21520                     && OMP_CLAUSE_DECL (*c) == real_decl)
21521                   add_private_clause = false;
21522                 c = &OMP_CLAUSE_CHAIN (*c);
21523               }
21524         }
21525
21526       if (add_private_clause)
21527         {
21528           tree c;
21529           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21530             {
21531               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21532                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21533                   && OMP_CLAUSE_DECL (c) == decl)
21534                 break;
21535               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21536                        && OMP_CLAUSE_DECL (c) == decl)
21537                 error ("%Hiteration variable %qD should not be firstprivate",
21538                        &loc, decl);
21539               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21540                        && OMP_CLAUSE_DECL (c) == decl)
21541                 error ("%Hiteration variable %qD should not be reduction",
21542                        &loc, decl);
21543             }
21544           if (c == NULL)
21545             {
21546               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21547               OMP_CLAUSE_DECL (c) = decl;
21548               c = finish_omp_clauses (c);
21549               if (c)
21550                 {
21551                   OMP_CLAUSE_CHAIN (c) = clauses;
21552                   clauses = c;
21553                 }
21554             }
21555         }
21556
21557       cond = NULL;
21558       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21559         cond = cp_parser_omp_for_cond (parser, decl);
21560       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21561
21562       incr = NULL;
21563       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21564         {
21565           /* If decl is an iterator, preserve the operator on decl
21566              until finish_omp_for.  */
21567           if (decl
21568               && (type_dependent_expression_p (decl)
21569                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21570             incr = cp_parser_omp_for_incr (parser, decl);
21571           else
21572             incr = cp_parser_expression (parser, false, NULL);
21573         }
21574
21575       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21576         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21577                                                /*or_comma=*/false,
21578                                                /*consume_paren=*/true);
21579
21580       TREE_VEC_ELT (declv, i) = decl;
21581       TREE_VEC_ELT (initv, i) = init;
21582       TREE_VEC_ELT (condv, i) = cond;
21583       TREE_VEC_ELT (incrv, i) = incr;
21584
21585       if (i == collapse - 1)
21586         break;
21587
21588       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21589          in between the collapsed for loops to be still considered perfectly
21590          nested.  Hopefully the final version clarifies this.
21591          For now handle (multiple) {'s and empty statements.  */
21592       cp_parser_parse_tentatively (parser);
21593       do
21594         {
21595           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21596             break;
21597           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21598             {
21599               cp_lexer_consume_token (parser->lexer);
21600               bracecount++;
21601             }
21602           else if (bracecount
21603                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21604             cp_lexer_consume_token (parser->lexer);
21605           else
21606             {
21607               loc = cp_lexer_peek_token (parser->lexer)->location;
21608               error ("%Hnot enough collapsed for loops", &loc);
21609               collapse_err = true;
21610               cp_parser_abort_tentative_parse (parser);
21611               declv = NULL_TREE;
21612               break;
21613             }
21614         }
21615       while (1);
21616
21617       if (declv)
21618         {
21619           cp_parser_parse_definitely (parser);
21620           nbraces += bracecount;
21621         }
21622     }
21623
21624   /* Note that we saved the original contents of this flag when we entered
21625      the structured block, and so we don't need to re-save it here.  */
21626   parser->in_statement = IN_OMP_FOR;
21627
21628   /* Note that the grammar doesn't call for a structured block here,
21629      though the loop as a whole is a structured block.  */
21630   body = push_stmt_list ();
21631   cp_parser_statement (parser, NULL_TREE, false, NULL);
21632   body = pop_stmt_list (body);
21633
21634   if (declv == NULL_TREE)
21635     ret = NULL_TREE;
21636   else
21637     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21638                           pre_body, clauses);
21639
21640   while (nbraces)
21641     {
21642       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21643         {
21644           cp_lexer_consume_token (parser->lexer);
21645           nbraces--;
21646         }
21647       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21648         cp_lexer_consume_token (parser->lexer);
21649       else
21650         {
21651           if (!collapse_err)
21652             {
21653               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21654               error ("%Hcollapsed loops not perfectly nested", &loc);
21655             }
21656           collapse_err = true;
21657           cp_parser_statement_seq_opt (parser, NULL);
21658           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21659             break;
21660         }
21661     }
21662
21663   while (for_block)
21664     {
21665       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21666       for_block = TREE_CHAIN (for_block);
21667     }
21668
21669   return ret;
21670 }
21671
21672 /* OpenMP 2.5:
21673    #pragma omp for for-clause[optseq] new-line
21674      for-loop  */
21675
21676 #define OMP_FOR_CLAUSE_MASK                             \
21677         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21678         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21679         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21680         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21681         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21682         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21683         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21684         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21685
21686 static tree
21687 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21688 {
21689   tree clauses, sb, ret;
21690   unsigned int save;
21691
21692   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21693                                        "#pragma omp for", pragma_tok);
21694
21695   sb = begin_omp_structured_block ();
21696   save = cp_parser_begin_omp_structured_block (parser);
21697
21698   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21699
21700   cp_parser_end_omp_structured_block (parser, save);
21701   add_stmt (finish_omp_structured_block (sb));
21702
21703   return ret;
21704 }
21705
21706 /* OpenMP 2.5:
21707    # pragma omp master new-line
21708      structured-block  */
21709
21710 static tree
21711 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21712 {
21713   cp_parser_require_pragma_eol (parser, pragma_tok);
21714   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21715 }
21716
21717 /* OpenMP 2.5:
21718    # pragma omp ordered new-line
21719      structured-block  */
21720
21721 static tree
21722 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21723 {
21724   cp_parser_require_pragma_eol (parser, pragma_tok);
21725   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21726 }
21727
21728 /* OpenMP 2.5:
21729
21730    section-scope:
21731      { section-sequence }
21732
21733    section-sequence:
21734      section-directive[opt] structured-block
21735      section-sequence section-directive structured-block  */
21736
21737 static tree
21738 cp_parser_omp_sections_scope (cp_parser *parser)
21739 {
21740   tree stmt, substmt;
21741   bool error_suppress = false;
21742   cp_token *tok;
21743
21744   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21745     return NULL_TREE;
21746
21747   stmt = push_stmt_list ();
21748
21749   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21750     {
21751       unsigned save;
21752
21753       substmt = begin_omp_structured_block ();
21754       save = cp_parser_begin_omp_structured_block (parser);
21755
21756       while (1)
21757         {
21758           cp_parser_statement (parser, NULL_TREE, false, NULL);
21759
21760           tok = cp_lexer_peek_token (parser->lexer);
21761           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21762             break;
21763           if (tok->type == CPP_CLOSE_BRACE)
21764             break;
21765           if (tok->type == CPP_EOF)
21766             break;
21767         }
21768
21769       cp_parser_end_omp_structured_block (parser, save);
21770       substmt = finish_omp_structured_block (substmt);
21771       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21772       add_stmt (substmt);
21773     }
21774
21775   while (1)
21776     {
21777       tok = cp_lexer_peek_token (parser->lexer);
21778       if (tok->type == CPP_CLOSE_BRACE)
21779         break;
21780       if (tok->type == CPP_EOF)
21781         break;
21782
21783       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21784         {
21785           cp_lexer_consume_token (parser->lexer);
21786           cp_parser_require_pragma_eol (parser, tok);
21787           error_suppress = false;
21788         }
21789       else if (!error_suppress)
21790         {
21791           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21792           error_suppress = true;
21793         }
21794
21795       substmt = cp_parser_omp_structured_block (parser);
21796       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21797       add_stmt (substmt);
21798     }
21799   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21800
21801   substmt = pop_stmt_list (stmt);
21802
21803   stmt = make_node (OMP_SECTIONS);
21804   TREE_TYPE (stmt) = void_type_node;
21805   OMP_SECTIONS_BODY (stmt) = substmt;
21806
21807   add_stmt (stmt);
21808   return stmt;
21809 }
21810
21811 /* OpenMP 2.5:
21812    # pragma omp sections sections-clause[optseq] newline
21813      sections-scope  */
21814
21815 #define OMP_SECTIONS_CLAUSE_MASK                        \
21816         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21817         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21818         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21819         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21820         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21821
21822 static tree
21823 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21824 {
21825   tree clauses, ret;
21826
21827   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21828                                        "#pragma omp sections", pragma_tok);
21829
21830   ret = cp_parser_omp_sections_scope (parser);
21831   if (ret)
21832     OMP_SECTIONS_CLAUSES (ret) = clauses;
21833
21834   return ret;
21835 }
21836
21837 /* OpenMP 2.5:
21838    # pragma parallel parallel-clause new-line
21839    # pragma parallel for parallel-for-clause new-line
21840    # pragma parallel sections parallel-sections-clause new-line  */
21841
21842 #define OMP_PARALLEL_CLAUSE_MASK                        \
21843         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21844         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21845         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21846         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21847         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21848         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21849         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21850         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21851
21852 static tree
21853 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21854 {
21855   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21856   const char *p_name = "#pragma omp parallel";
21857   tree stmt, clauses, par_clause, ws_clause, block;
21858   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21859   unsigned int save;
21860
21861   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21862     {
21863       cp_lexer_consume_token (parser->lexer);
21864       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21865       p_name = "#pragma omp parallel for";
21866       mask |= OMP_FOR_CLAUSE_MASK;
21867       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21868     }
21869   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21870     {
21871       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21872       const char *p = IDENTIFIER_POINTER (id);
21873       if (strcmp (p, "sections") == 0)
21874         {
21875           cp_lexer_consume_token (parser->lexer);
21876           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21877           p_name = "#pragma omp parallel sections";
21878           mask |= OMP_SECTIONS_CLAUSE_MASK;
21879           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21880         }
21881     }
21882
21883   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21884   block = begin_omp_parallel ();
21885   save = cp_parser_begin_omp_structured_block (parser);
21886
21887   switch (p_kind)
21888     {
21889     case PRAGMA_OMP_PARALLEL:
21890       cp_parser_statement (parser, NULL_TREE, false, NULL);
21891       par_clause = clauses;
21892       break;
21893
21894     case PRAGMA_OMP_PARALLEL_FOR:
21895       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21896       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21897       break;
21898
21899     case PRAGMA_OMP_PARALLEL_SECTIONS:
21900       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21901       stmt = cp_parser_omp_sections_scope (parser);
21902       if (stmt)
21903         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21904       break;
21905
21906     default:
21907       gcc_unreachable ();
21908     }
21909
21910   cp_parser_end_omp_structured_block (parser, save);
21911   stmt = finish_omp_parallel (par_clause, block);
21912   if (p_kind != PRAGMA_OMP_PARALLEL)
21913     OMP_PARALLEL_COMBINED (stmt) = 1;
21914   return stmt;
21915 }
21916
21917 /* OpenMP 2.5:
21918    # pragma omp single single-clause[optseq] new-line
21919      structured-block  */
21920
21921 #define OMP_SINGLE_CLAUSE_MASK                          \
21922         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21923         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21924         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21925         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21926
21927 static tree
21928 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21929 {
21930   tree stmt = make_node (OMP_SINGLE);
21931   TREE_TYPE (stmt) = void_type_node;
21932
21933   OMP_SINGLE_CLAUSES (stmt)
21934     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21935                                  "#pragma omp single", pragma_tok);
21936   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21937
21938   return add_stmt (stmt);
21939 }
21940
21941 /* OpenMP 3.0:
21942    # pragma omp task task-clause[optseq] new-line
21943      structured-block  */
21944
21945 #define OMP_TASK_CLAUSE_MASK                            \
21946         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21947         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21948         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21949         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21950         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21951         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21952
21953 static tree
21954 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21955 {
21956   tree clauses, block;
21957   unsigned int save;
21958
21959   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21960                                        "#pragma omp task", pragma_tok);
21961   block = begin_omp_task ();
21962   save = cp_parser_begin_omp_structured_block (parser);
21963   cp_parser_statement (parser, NULL_TREE, false, NULL);
21964   cp_parser_end_omp_structured_block (parser, save);
21965   return finish_omp_task (clauses, block);
21966 }
21967
21968 /* OpenMP 3.0:
21969    # pragma omp taskwait new-line  */
21970
21971 static void
21972 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21973 {
21974   cp_parser_require_pragma_eol (parser, pragma_tok);
21975   finish_omp_taskwait ();
21976 }
21977
21978 /* OpenMP 2.5:
21979    # pragma omp threadprivate (variable-list) */
21980
21981 static void
21982 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21983 {
21984   tree vars;
21985
21986   vars = cp_parser_omp_var_list (parser, 0, NULL);
21987   cp_parser_require_pragma_eol (parser, pragma_tok);
21988
21989   finish_omp_threadprivate (vars);
21990 }
21991
21992 /* Main entry point to OpenMP statement pragmas.  */
21993
21994 static void
21995 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21996 {
21997   tree stmt;
21998
21999   switch (pragma_tok->pragma_kind)
22000     {
22001     case PRAGMA_OMP_ATOMIC:
22002       cp_parser_omp_atomic (parser, pragma_tok);
22003       return;
22004     case PRAGMA_OMP_CRITICAL:
22005       stmt = cp_parser_omp_critical (parser, pragma_tok);
22006       break;
22007     case PRAGMA_OMP_FOR:
22008       stmt = cp_parser_omp_for (parser, pragma_tok);
22009       break;
22010     case PRAGMA_OMP_MASTER:
22011       stmt = cp_parser_omp_master (parser, pragma_tok);
22012       break;
22013     case PRAGMA_OMP_ORDERED:
22014       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22015       break;
22016     case PRAGMA_OMP_PARALLEL:
22017       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22018       break;
22019     case PRAGMA_OMP_SECTIONS:
22020       stmt = cp_parser_omp_sections (parser, pragma_tok);
22021       break;
22022     case PRAGMA_OMP_SINGLE:
22023       stmt = cp_parser_omp_single (parser, pragma_tok);
22024       break;
22025     case PRAGMA_OMP_TASK:
22026       stmt = cp_parser_omp_task (parser, pragma_tok);
22027       break;
22028     default:
22029       gcc_unreachable ();
22030     }
22031
22032   if (stmt)
22033     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22034 }
22035 \f
22036 /* The parser.  */
22037
22038 static GTY (()) cp_parser *the_parser;
22039
22040 \f
22041 /* Special handling for the first token or line in the file.  The first
22042    thing in the file might be #pragma GCC pch_preprocess, which loads a
22043    PCH file, which is a GC collection point.  So we need to handle this
22044    first pragma without benefit of an existing lexer structure.
22045
22046    Always returns one token to the caller in *FIRST_TOKEN.  This is
22047    either the true first token of the file, or the first token after
22048    the initial pragma.  */
22049
22050 static void
22051 cp_parser_initial_pragma (cp_token *first_token)
22052 {
22053   tree name = NULL;
22054
22055   cp_lexer_get_preprocessor_token (NULL, first_token);
22056   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22057     return;
22058
22059   cp_lexer_get_preprocessor_token (NULL, first_token);
22060   if (first_token->type == CPP_STRING)
22061     {
22062       name = first_token->u.value;
22063
22064       cp_lexer_get_preprocessor_token (NULL, first_token);
22065       if (first_token->type != CPP_PRAGMA_EOL)
22066         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22067                &first_token->location);
22068     }
22069   else
22070     error ("%Hexpected string literal", &first_token->location);
22071
22072   /* Skip to the end of the pragma.  */
22073   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22074     cp_lexer_get_preprocessor_token (NULL, first_token);
22075
22076   /* Now actually load the PCH file.  */
22077   if (name)
22078     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22079
22080   /* Read one more token to return to our caller.  We have to do this
22081      after reading the PCH file in, since its pointers have to be
22082      live.  */
22083   cp_lexer_get_preprocessor_token (NULL, first_token);
22084 }
22085
22086 /* Normal parsing of a pragma token.  Here we can (and must) use the
22087    regular lexer.  */
22088
22089 static bool
22090 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22091 {
22092   cp_token *pragma_tok;
22093   unsigned int id;
22094
22095   pragma_tok = cp_lexer_consume_token (parser->lexer);
22096   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22097   parser->lexer->in_pragma = true;
22098
22099   id = pragma_tok->pragma_kind;
22100   switch (id)
22101     {
22102     case PRAGMA_GCC_PCH_PREPROCESS:
22103       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22104              &pragma_tok->location);
22105       break;
22106
22107     case PRAGMA_OMP_BARRIER:
22108       switch (context)
22109         {
22110         case pragma_compound:
22111           cp_parser_omp_barrier (parser, pragma_tok);
22112           return false;
22113         case pragma_stmt:
22114           error ("%H%<#pragma omp barrier%> may only be "
22115                  "used in compound statements", &pragma_tok->location);
22116           break;
22117         default:
22118           goto bad_stmt;
22119         }
22120       break;
22121
22122     case PRAGMA_OMP_FLUSH:
22123       switch (context)
22124         {
22125         case pragma_compound:
22126           cp_parser_omp_flush (parser, pragma_tok);
22127           return false;
22128         case pragma_stmt:
22129           error ("%H%<#pragma omp flush%> may only be "
22130                  "used in compound statements", &pragma_tok->location);
22131           break;
22132         default:
22133           goto bad_stmt;
22134         }
22135       break;
22136
22137     case PRAGMA_OMP_TASKWAIT:
22138       switch (context)
22139         {
22140         case pragma_compound:
22141           cp_parser_omp_taskwait (parser, pragma_tok);
22142           return false;
22143         case pragma_stmt:
22144           error ("%H%<#pragma omp taskwait%> may only be "
22145                  "used in compound statements",
22146                  &pragma_tok->location);
22147           break;
22148         default:
22149           goto bad_stmt;
22150         }
22151       break;
22152
22153     case PRAGMA_OMP_THREADPRIVATE:
22154       cp_parser_omp_threadprivate (parser, pragma_tok);
22155       return false;
22156
22157     case PRAGMA_OMP_ATOMIC:
22158     case PRAGMA_OMP_CRITICAL:
22159     case PRAGMA_OMP_FOR:
22160     case PRAGMA_OMP_MASTER:
22161     case PRAGMA_OMP_ORDERED:
22162     case PRAGMA_OMP_PARALLEL:
22163     case PRAGMA_OMP_SECTIONS:
22164     case PRAGMA_OMP_SINGLE:
22165     case PRAGMA_OMP_TASK:
22166       if (context == pragma_external)
22167         goto bad_stmt;
22168       cp_parser_omp_construct (parser, pragma_tok);
22169       return true;
22170
22171     case PRAGMA_OMP_SECTION:
22172       error ("%H%<#pragma omp section%> may only be used in "
22173              "%<#pragma omp sections%> construct", &pragma_tok->location);
22174       break;
22175
22176     default:
22177       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22178       c_invoke_pragma_handler (id);
22179       break;
22180
22181     bad_stmt:
22182       cp_parser_error (parser, "expected declaration specifiers");
22183       break;
22184     }
22185
22186   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22187   return false;
22188 }
22189
22190 /* The interface the pragma parsers have to the lexer.  */
22191
22192 enum cpp_ttype
22193 pragma_lex (tree *value)
22194 {
22195   cp_token *tok;
22196   enum cpp_ttype ret;
22197
22198   tok = cp_lexer_peek_token (the_parser->lexer);
22199
22200   ret = tok->type;
22201   *value = tok->u.value;
22202
22203   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22204     ret = CPP_EOF;
22205   else if (ret == CPP_STRING)
22206     *value = cp_parser_string_literal (the_parser, false, false);
22207   else
22208     {
22209       cp_lexer_consume_token (the_parser->lexer);
22210       if (ret == CPP_KEYWORD)
22211         ret = CPP_NAME;
22212     }
22213
22214   return ret;
22215 }
22216
22217 \f
22218 /* External interface.  */
22219
22220 /* Parse one entire translation unit.  */
22221
22222 void
22223 c_parse_file (void)
22224 {
22225   bool error_occurred;
22226   static bool already_called = false;
22227
22228   if (already_called)
22229     {
22230       sorry ("inter-module optimizations not implemented for C++");
22231       return;
22232     }
22233   already_called = true;
22234
22235   the_parser = cp_parser_new ();
22236   push_deferring_access_checks (flag_access_control
22237                                 ? dk_no_deferred : dk_no_check);
22238   error_occurred = cp_parser_translation_unit (the_parser);
22239   the_parser = NULL;
22240 }
22241
22242 #include "gt-cp-parser.h"