Merge branch 'master' into kiconv2
[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, do not allow user-defined types.  */
1202   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1203 } cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse.  */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209   /* We want an abstract declarator.  */
1210   CP_PARSER_DECLARATOR_ABSTRACT,
1211   /* We want a named declarator.  */
1212   CP_PARSER_DECLARATOR_NAMED,
1213   /* We don't mind, but the name must be an unqualified-id.  */
1214   CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions.  The minimum value
1218    of PREC must be 1, because zero is reserved to quickly discriminate
1219    binary operators from other tokens.  */
1220
1221 enum cp_parser_prec
1222 {
1223   PREC_NOT_OPERATOR,
1224   PREC_LOGICAL_OR_EXPRESSION,
1225   PREC_LOGICAL_AND_EXPRESSION,
1226   PREC_INCLUSIVE_OR_EXPRESSION,
1227   PREC_EXCLUSIVE_OR_EXPRESSION,
1228   PREC_AND_EXPRESSION,
1229   PREC_EQUALITY_EXPRESSION,
1230   PREC_RELATIONAL_EXPRESSION,
1231   PREC_SHIFT_EXPRESSION,
1232   PREC_ADDITIVE_EXPRESSION,
1233   PREC_MULTIPLICATIVE_EXPRESSION,
1234   PREC_PM_EXPRESSION,
1235   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239    precedence value.  */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243   /* The token type.  */
1244   enum cpp_ttype token_type;
1245   /* The corresponding tree code.  */
1246   enum tree_code tree_type;
1247   /* The precedence of this operator.  */
1248   enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse.  */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255   /* No errors have occurred.  */
1256   CP_PARSER_STATUS_KIND_NO_ERROR,
1257   /* An error has occurred.  */
1258   CP_PARSER_STATUS_KIND_ERROR,
1259   /* We are committed to this tentative parse, whether or not an error
1260      has occurred.  */
1261   CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266   /* Left hand side of the binary operation we are currently
1267      parsing.  */
1268   tree lhs;
1269   /* Original tree code for left hand side, if it was a binary
1270      expression itself (used for -Wparentheses).  */
1271   enum tree_code lhs_type;
1272   /* Tree code for the binary operation we are parsing.  */
1273   enum tree_code tree_type;
1274   /* Precedence of the binary operation we are parsing.  */
1275   int prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1279    entries because precedence levels on the stack are monotonically
1280    increasing.  */
1281 typedef struct cp_parser_expression_stack_entry
1282   cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively.  */
1285 typedef struct cp_parser_context GTY (())
1286 {
1287   /* If this is a tentative parsing context, the status of the
1288      tentative parse.  */
1289   enum cp_parser_status_kind status;
1290   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1291      that are looked up in this context must be looked up both in the
1292      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1293      the context of the containing expression.  */
1294   tree object_type;
1295
1296   /* The next parsing context in the stack.  */
1297   struct cp_parser_context *next;
1298 } cp_parser_context;
1299
1300 /* Prototypes.  */
1301
1302 /* Constructors and destructors.  */
1303
1304 static cp_parser_context *cp_parser_context_new
1305   (cp_parser_context *);
1306
1307 /* Class variables.  */
1308
1309 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310
1311 /* The operator-precedence table used by cp_parser_binary_expression.
1312    Transformed into an associative array (binops_by_token) by
1313    cp_parser_new.  */
1314
1315 static const cp_parser_binary_operations_map_node binops[] = {
1316   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1317   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318
1319   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322
1323   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325
1326   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328
1329   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333
1334   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1335   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336
1337   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338
1339   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340
1341   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342
1343   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344
1345   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1346 };
1347
1348 /* The same as binops, but initialized by cp_parser_new so that
1349    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1350    for speed.  */
1351 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352
1353 /* Constructors and destructors.  */
1354
1355 /* Construct a new context.  The context below this one on the stack
1356    is given by NEXT.  */
1357
1358 static cp_parser_context *
1359 cp_parser_context_new (cp_parser_context* next)
1360 {
1361   cp_parser_context *context;
1362
1363   /* Allocate the storage.  */
1364   if (cp_parser_context_free_list != NULL)
1365     {
1366       /* Pull the first entry from the free list.  */
1367       context = cp_parser_context_free_list;
1368       cp_parser_context_free_list = context->next;
1369       memset (context, 0, sizeof (*context));
1370     }
1371   else
1372     context = GGC_CNEW (cp_parser_context);
1373
1374   /* No errors have occurred yet in this context.  */
1375   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1376   /* If this is not the bottommost context, copy information that we
1377      need from the previous context.  */
1378   if (next)
1379     {
1380       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1381          expression, then we are parsing one in this context, too.  */
1382       context->object_type = next->object_type;
1383       /* Thread the stack.  */
1384       context->next = next;
1385     }
1386
1387   return context;
1388 }
1389
1390 /* The cp_parser structure represents the C++ parser.  */
1391
1392 typedef struct cp_parser GTY(())
1393 {
1394   /* The lexer from which we are obtaining tokens.  */
1395   cp_lexer *lexer;
1396
1397   /* The scope in which names should be looked up.  If NULL_TREE, then
1398      we look up names in the scope that is currently open in the
1399      source program.  If non-NULL, this is either a TYPE or
1400      NAMESPACE_DECL for the scope in which we should look.  It can
1401      also be ERROR_MARK, when we've parsed a bogus scope.
1402
1403      This value is not cleared automatically after a name is looked
1404      up, so we must be careful to clear it before starting a new look
1405      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1406      will look up `Z' in the scope of `X', rather than the current
1407      scope.)  Unfortunately, it is difficult to tell when name lookup
1408      is complete, because we sometimes peek at a token, look it up,
1409      and then decide not to consume it.   */
1410   tree scope;
1411
1412   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1413      last lookup took place.  OBJECT_SCOPE is used if an expression
1414      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1415      respectively.  QUALIFYING_SCOPE is used for an expression of the
1416      form "X::Y"; it refers to X.  */
1417   tree object_scope;
1418   tree qualifying_scope;
1419
1420   /* A stack of parsing contexts.  All but the bottom entry on the
1421      stack will be tentative contexts.
1422
1423      We parse tentatively in order to determine which construct is in
1424      use in some situations.  For example, in order to determine
1425      whether a statement is an expression-statement or a
1426      declaration-statement we parse it tentatively as a
1427      declaration-statement.  If that fails, we then reparse the same
1428      token stream as an expression-statement.  */
1429   cp_parser_context *context;
1430
1431   /* True if we are parsing GNU C++.  If this flag is not set, then
1432      GNU extensions are not recognized.  */
1433   bool allow_gnu_extensions_p;
1434
1435   /* TRUE if the `>' token should be interpreted as the greater-than
1436      operator.  FALSE if it is the end of a template-id or
1437      template-parameter-list. In C++0x mode, this flag also applies to
1438      `>>' tokens, which are viewed as two consecutive `>' tokens when
1439      this flag is FALSE.  */
1440   bool greater_than_is_operator_p;
1441
1442   /* TRUE if default arguments are allowed within a parameter list
1443      that starts at this point. FALSE if only a gnu extension makes
1444      them permissible.  */
1445   bool default_arg_ok_p;
1446
1447   /* TRUE if we are parsing an integral constant-expression.  See
1448      [expr.const] for a precise definition.  */
1449   bool integral_constant_expression_p;
1450
1451   /* TRUE if we are parsing an integral constant-expression -- but a
1452      non-constant expression should be permitted as well.  This flag
1453      is used when parsing an array bound so that GNU variable-length
1454      arrays are tolerated.  */
1455   bool allow_non_integral_constant_expression_p;
1456
1457   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1458      been seen that makes the expression non-constant.  */
1459   bool non_integral_constant_expression_p;
1460
1461   /* TRUE if local variable names and `this' are forbidden in the
1462      current context.  */
1463   bool local_variables_forbidden_p;
1464
1465   /* TRUE if the declaration we are parsing is part of a
1466      linkage-specification of the form `extern string-literal
1467      declaration'.  */
1468   bool in_unbraced_linkage_specification_p;
1469
1470   /* TRUE if we are presently parsing a declarator, after the
1471      direct-declarator.  */
1472   bool in_declarator_p;
1473
1474   /* TRUE if we are presently parsing a template-argument-list.  */
1475   bool in_template_argument_list_p;
1476
1477   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1478      to IN_OMP_BLOCK if parsing OpenMP structured block and
1479      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1480      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1481      iteration-statement, OpenMP block or loop within that switch.  */
1482 #define IN_SWITCH_STMT          1
1483 #define IN_ITERATION_STMT       2
1484 #define IN_OMP_BLOCK            4
1485 #define IN_OMP_FOR              8
1486 #define IN_IF_STMT             16
1487   unsigned char in_statement;
1488
1489   /* TRUE if we are presently parsing the body of a switch statement.
1490      Note that this doesn't quite overlap with in_statement above.
1491      The difference relates to giving the right sets of error messages:
1492      "case not in switch" vs "break statement used with OpenMP...".  */
1493   bool in_switch_statement_p;
1494
1495   /* TRUE if we are parsing a type-id in an expression context.  In
1496      such a situation, both "type (expr)" and "type (type)" are valid
1497      alternatives.  */
1498   bool in_type_id_in_expr_p;
1499
1500   /* TRUE if we are currently in a header file where declarations are
1501      implicitly extern "C".  */
1502   bool implicit_extern_c;
1503
1504   /* TRUE if strings in expressions should be translated to the execution
1505      character set.  */
1506   bool translate_strings_p;
1507
1508   /* TRUE if we are presently parsing the body of a function, but not
1509      a local class.  */
1510   bool in_function_body;
1511
1512   /* If non-NULL, then we are parsing a construct where new type
1513      definitions are not permitted.  The string stored here will be
1514      issued as an error message if a type is defined.  */
1515   const char *type_definition_forbidden_message;
1516
1517   /* A list of lists. The outer list is a stack, used for member
1518      functions of local classes. At each level there are two sub-list,
1519      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1520      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1521      TREE_VALUE's. The functions are chained in reverse declaration
1522      order.
1523
1524      The TREE_PURPOSE sublist contains those functions with default
1525      arguments that need post processing, and the TREE_VALUE sublist
1526      contains those functions with definitions that need post
1527      processing.
1528
1529      These lists can only be processed once the outermost class being
1530      defined is complete.  */
1531   tree unparsed_functions_queues;
1532
1533   /* The number of classes whose definitions are currently in
1534      progress.  */
1535   unsigned num_classes_being_defined;
1536
1537   /* The number of template parameter lists that apply directly to the
1538      current declaration.  */
1539   unsigned num_template_parameter_lists;
1540 } cp_parser;
1541
1542 /* Prototypes.  */
1543
1544 /* Constructors and destructors.  */
1545
1546 static cp_parser *cp_parser_new
1547   (void);
1548
1549 /* Routines to parse various constructs.
1550
1551    Those that return `tree' will return the error_mark_node (rather
1552    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1553    Sometimes, they will return an ordinary node if error-recovery was
1554    attempted, even though a parse error occurred.  So, to check
1555    whether or not a parse error occurred, you should always use
1556    cp_parser_error_occurred.  If the construct is optional (indicated
1557    either by an `_opt' in the name of the function that does the
1558    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1559    the construct is not present.  */
1560
1561 /* Lexical conventions [gram.lex]  */
1562
1563 static tree cp_parser_identifier
1564   (cp_parser *);
1565 static tree cp_parser_string_literal
1566   (cp_parser *, bool, bool);
1567
1568 /* Basic concepts [gram.basic]  */
1569
1570 static bool cp_parser_translation_unit
1571   (cp_parser *);
1572
1573 /* Expressions [gram.expr]  */
1574
1575 static tree cp_parser_primary_expression
1576   (cp_parser *, bool, bool, bool, cp_id_kind *);
1577 static tree cp_parser_id_expression
1578   (cp_parser *, bool, bool, bool *, bool, bool);
1579 static tree cp_parser_unqualified_id
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier_opt
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_nested_name_specifier
1584   (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_qualifying_entity
1586   (cp_parser *, bool, bool, bool, bool, bool);
1587 static tree cp_parser_postfix_expression
1588   (cp_parser *, bool, bool, bool, cp_id_kind *);
1589 static tree cp_parser_postfix_open_square_expression
1590   (cp_parser *, tree, bool);
1591 static tree cp_parser_postfix_dot_deref_expression
1592   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1593 static tree cp_parser_parenthesized_expression_list
1594   (cp_parser *, bool, bool, bool, bool *);
1595 static void cp_parser_pseudo_destructor_name
1596   (cp_parser *, tree *, tree *);
1597 static tree cp_parser_unary_expression
1598   (cp_parser *, bool, bool, cp_id_kind *);
1599 static enum tree_code cp_parser_unary_operator
1600   (cp_token *);
1601 static tree cp_parser_new_expression
1602   (cp_parser *);
1603 static tree cp_parser_new_placement
1604   (cp_parser *);
1605 static tree cp_parser_new_type_id
1606   (cp_parser *, tree *);
1607 static cp_declarator *cp_parser_new_declarator_opt
1608   (cp_parser *);
1609 static cp_declarator *cp_parser_direct_new_declarator
1610   (cp_parser *);
1611 static tree cp_parser_new_initializer
1612   (cp_parser *);
1613 static tree cp_parser_delete_expression
1614   (cp_parser *);
1615 static tree cp_parser_cast_expression
1616   (cp_parser *, bool, bool, cp_id_kind *);
1617 static tree cp_parser_binary_expression
1618   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1619 static tree cp_parser_question_colon_clause
1620   (cp_parser *, tree);
1621 static tree cp_parser_assignment_expression
1622   (cp_parser *, bool, cp_id_kind *);
1623 static enum tree_code cp_parser_assignment_operator_opt
1624   (cp_parser *);
1625 static tree cp_parser_expression
1626   (cp_parser *, bool, cp_id_kind *);
1627 static tree cp_parser_constant_expression
1628   (cp_parser *, bool, bool *);
1629 static tree cp_parser_builtin_offsetof
1630   (cp_parser *);
1631
1632 /* Statements [gram.stmt.stmt]  */
1633
1634 static void cp_parser_statement
1635   (cp_parser *, tree, bool, bool *);
1636 static void cp_parser_label_for_labeled_statement
1637   (cp_parser *);
1638 static tree cp_parser_expression_statement
1639   (cp_parser *, tree);
1640 static tree cp_parser_compound_statement
1641   (cp_parser *, tree, bool);
1642 static void cp_parser_statement_seq_opt
1643   (cp_parser *, tree);
1644 static tree cp_parser_selection_statement
1645   (cp_parser *, bool *);
1646 static tree cp_parser_condition
1647   (cp_parser *);
1648 static tree cp_parser_iteration_statement
1649   (cp_parser *);
1650 static void cp_parser_for_init_statement
1651   (cp_parser *);
1652 static tree cp_parser_jump_statement
1653   (cp_parser *);
1654 static void cp_parser_declaration_statement
1655   (cp_parser *);
1656
1657 static tree cp_parser_implicitly_scoped_statement
1658   (cp_parser *, bool *);
1659 static void cp_parser_already_scoped_statement
1660   (cp_parser *);
1661
1662 /* Declarations [gram.dcl.dcl] */
1663
1664 static void cp_parser_declaration_seq_opt
1665   (cp_parser *);
1666 static void cp_parser_declaration
1667   (cp_parser *);
1668 static void cp_parser_block_declaration
1669   (cp_parser *, bool);
1670 static void cp_parser_simple_declaration
1671   (cp_parser *, bool);
1672 static void cp_parser_decl_specifier_seq
1673   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1674 static tree cp_parser_storage_class_specifier_opt
1675   (cp_parser *);
1676 static tree cp_parser_function_specifier_opt
1677   (cp_parser *, cp_decl_specifier_seq *);
1678 static tree cp_parser_type_specifier
1679   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1680    int *, bool *);
1681 static tree cp_parser_simple_type_specifier
1682   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1683 static tree cp_parser_type_name
1684   (cp_parser *);
1685 static tree cp_parser_nonclass_name 
1686   (cp_parser* parser);
1687 static tree cp_parser_elaborated_type_specifier
1688   (cp_parser *, bool, bool);
1689 static tree cp_parser_enum_specifier
1690   (cp_parser *);
1691 static void cp_parser_enumerator_list
1692   (cp_parser *, tree);
1693 static void cp_parser_enumerator_definition
1694   (cp_parser *, tree);
1695 static tree cp_parser_namespace_name
1696   (cp_parser *);
1697 static void cp_parser_namespace_definition
1698   (cp_parser *);
1699 static void cp_parser_namespace_body
1700   (cp_parser *);
1701 static tree cp_parser_qualified_namespace_specifier
1702   (cp_parser *);
1703 static void cp_parser_namespace_alias_definition
1704   (cp_parser *);
1705 static bool cp_parser_using_declaration
1706   (cp_parser *, bool);
1707 static void cp_parser_using_directive
1708   (cp_parser *);
1709 static void cp_parser_asm_definition
1710   (cp_parser *);
1711 static void cp_parser_linkage_specification
1712   (cp_parser *);
1713 static void cp_parser_static_assert
1714   (cp_parser *, bool);
1715 static tree cp_parser_decltype
1716   (cp_parser *);
1717
1718 /* Declarators [gram.dcl.decl] */
1719
1720 static tree cp_parser_init_declarator
1721   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1722 static cp_declarator *cp_parser_declarator
1723   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1724 static cp_declarator *cp_parser_direct_declarator
1725   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1726 static enum tree_code cp_parser_ptr_operator
1727   (cp_parser *, tree *, cp_cv_quals *);
1728 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1729   (cp_parser *);
1730 static tree cp_parser_late_return_type_opt
1731   (cp_parser *);
1732 static tree cp_parser_declarator_id
1733   (cp_parser *, bool);
1734 static tree cp_parser_type_id
1735   (cp_parser *);
1736 static tree cp_parser_template_type_arg
1737   (cp_parser *);
1738 static tree cp_parser_type_id_1
1739   (cp_parser *, bool);
1740 static void cp_parser_type_specifier_seq
1741   (cp_parser *, bool, cp_decl_specifier_seq *);
1742 static tree cp_parser_parameter_declaration_clause
1743   (cp_parser *);
1744 static tree cp_parser_parameter_declaration_list
1745   (cp_parser *, bool *);
1746 static cp_parameter_declarator *cp_parser_parameter_declaration
1747   (cp_parser *, bool, bool *);
1748 static tree cp_parser_default_argument 
1749   (cp_parser *, bool);
1750 static void cp_parser_function_body
1751   (cp_parser *);
1752 static tree cp_parser_initializer
1753   (cp_parser *, bool *, bool *);
1754 static tree cp_parser_initializer_clause
1755   (cp_parser *, bool *);
1756 static tree cp_parser_braced_list
1757   (cp_parser*, bool*);
1758 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1759   (cp_parser *, bool *);
1760
1761 static bool cp_parser_ctor_initializer_opt_and_function_body
1762   (cp_parser *);
1763
1764 /* Classes [gram.class] */
1765
1766 static tree cp_parser_class_name
1767   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1768 static tree cp_parser_class_specifier
1769   (cp_parser *);
1770 static tree cp_parser_class_head
1771   (cp_parser *, bool *, tree *, tree *);
1772 static enum tag_types cp_parser_class_key
1773   (cp_parser *);
1774 static void cp_parser_member_specification_opt
1775   (cp_parser *);
1776 static void cp_parser_member_declaration
1777   (cp_parser *);
1778 static tree cp_parser_pure_specifier
1779   (cp_parser *);
1780 static tree cp_parser_constant_initializer
1781   (cp_parser *);
1782
1783 /* Derived classes [gram.class.derived] */
1784
1785 static tree cp_parser_base_clause
1786   (cp_parser *);
1787 static tree cp_parser_base_specifier
1788   (cp_parser *);
1789
1790 /* Special member functions [gram.special] */
1791
1792 static tree cp_parser_conversion_function_id
1793   (cp_parser *);
1794 static tree cp_parser_conversion_type_id
1795   (cp_parser *);
1796 static cp_declarator *cp_parser_conversion_declarator_opt
1797   (cp_parser *);
1798 static bool cp_parser_ctor_initializer_opt
1799   (cp_parser *);
1800 static void cp_parser_mem_initializer_list
1801   (cp_parser *);
1802 static tree cp_parser_mem_initializer
1803   (cp_parser *);
1804 static tree cp_parser_mem_initializer_id
1805   (cp_parser *);
1806
1807 /* Overloading [gram.over] */
1808
1809 static tree cp_parser_operator_function_id
1810   (cp_parser *);
1811 static tree cp_parser_operator
1812   (cp_parser *);
1813
1814 /* Templates [gram.temp] */
1815
1816 static void cp_parser_template_declaration
1817   (cp_parser *, bool);
1818 static tree cp_parser_template_parameter_list
1819   (cp_parser *);
1820 static tree cp_parser_template_parameter
1821   (cp_parser *, bool *, bool *);
1822 static tree cp_parser_type_parameter
1823   (cp_parser *, bool *);
1824 static tree cp_parser_template_id
1825   (cp_parser *, bool, bool, bool);
1826 static tree cp_parser_template_name
1827   (cp_parser *, bool, bool, bool, bool *);
1828 static tree cp_parser_template_argument_list
1829   (cp_parser *);
1830 static tree cp_parser_template_argument
1831   (cp_parser *);
1832 static void cp_parser_explicit_instantiation
1833   (cp_parser *);
1834 static void cp_parser_explicit_specialization
1835   (cp_parser *);
1836
1837 /* Exception handling [gram.exception] */
1838
1839 static tree cp_parser_try_block
1840   (cp_parser *);
1841 static bool cp_parser_function_try_block
1842   (cp_parser *);
1843 static void cp_parser_handler_seq
1844   (cp_parser *);
1845 static void cp_parser_handler
1846   (cp_parser *);
1847 static tree cp_parser_exception_declaration
1848   (cp_parser *);
1849 static tree cp_parser_throw_expression
1850   (cp_parser *);
1851 static tree cp_parser_exception_specification_opt
1852   (cp_parser *);
1853 static tree cp_parser_type_id_list
1854   (cp_parser *);
1855
1856 /* GNU Extensions */
1857
1858 static tree cp_parser_asm_specification_opt
1859   (cp_parser *);
1860 static tree cp_parser_asm_operand_list
1861   (cp_parser *);
1862 static tree cp_parser_asm_clobber_list
1863   (cp_parser *);
1864 static tree cp_parser_attributes_opt
1865   (cp_parser *);
1866 static tree cp_parser_attribute_list
1867   (cp_parser *);
1868 static bool cp_parser_extension_opt
1869   (cp_parser *, int *);
1870 static void cp_parser_label_declaration
1871   (cp_parser *);
1872
1873 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1874 static bool cp_parser_pragma
1875   (cp_parser *, enum pragma_context);
1876
1877 /* Objective-C++ Productions */
1878
1879 static tree cp_parser_objc_message_receiver
1880   (cp_parser *);
1881 static tree cp_parser_objc_message_args
1882   (cp_parser *);
1883 static tree cp_parser_objc_message_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_encode_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_defs_expression
1888   (cp_parser *);
1889 static tree cp_parser_objc_protocol_expression
1890   (cp_parser *);
1891 static tree cp_parser_objc_selector_expression
1892   (cp_parser *);
1893 static tree cp_parser_objc_expression
1894   (cp_parser *);
1895 static bool cp_parser_objc_selector_p
1896   (enum cpp_ttype);
1897 static tree cp_parser_objc_selector
1898   (cp_parser *);
1899 static tree cp_parser_objc_protocol_refs_opt
1900   (cp_parser *);
1901 static void cp_parser_objc_declaration
1902   (cp_parser *);
1903 static tree cp_parser_objc_statement
1904   (cp_parser *);
1905
1906 /* Utility Routines */
1907
1908 static tree cp_parser_lookup_name
1909   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1910 static tree cp_parser_lookup_name_simple
1911   (cp_parser *, tree, location_t);
1912 static tree cp_parser_maybe_treat_template_as_class
1913   (tree, bool);
1914 static bool cp_parser_check_declarator_template_parameters
1915   (cp_parser *, cp_declarator *, location_t);
1916 static bool cp_parser_check_template_parameters
1917   (cp_parser *, unsigned, location_t);
1918 static tree cp_parser_simple_cast_expression
1919   (cp_parser *);
1920 static tree cp_parser_global_scope_opt
1921   (cp_parser *, bool);
1922 static bool cp_parser_constructor_declarator_p
1923   (cp_parser *, bool);
1924 static tree cp_parser_function_definition_from_specifiers_and_declarator
1925   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1926 static tree cp_parser_function_definition_after_declarator
1927   (cp_parser *, bool);
1928 static void cp_parser_template_declaration_after_export
1929   (cp_parser *, bool);
1930 static void cp_parser_perform_template_parameter_access_checks
1931   (VEC (deferred_access_check,gc)*);
1932 static tree cp_parser_single_declaration
1933   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1934 static tree cp_parser_functional_cast
1935   (cp_parser *, tree);
1936 static tree cp_parser_save_member_function_body
1937   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1938 static tree cp_parser_enclosed_template_argument_list
1939   (cp_parser *);
1940 static void cp_parser_save_default_args
1941   (cp_parser *, tree);
1942 static void cp_parser_late_parsing_for_member
1943   (cp_parser *, tree);
1944 static void cp_parser_late_parsing_default_args
1945   (cp_parser *, tree);
1946 static tree cp_parser_sizeof_operand
1947   (cp_parser *, enum rid);
1948 static tree cp_parser_trait_expr
1949   (cp_parser *, enum rid);
1950 static bool cp_parser_declares_only_class_p
1951   (cp_parser *);
1952 static void cp_parser_set_storage_class
1953   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1954 static void cp_parser_set_decl_spec_type
1955   (cp_decl_specifier_seq *, tree, location_t, bool);
1956 static bool cp_parser_friend_p
1957   (const cp_decl_specifier_seq *);
1958 static cp_token *cp_parser_require
1959   (cp_parser *, enum cpp_ttype, const char *);
1960 static cp_token *cp_parser_require_keyword
1961   (cp_parser *, enum rid, const char *);
1962 static bool cp_parser_token_starts_function_definition_p
1963   (cp_token *);
1964 static bool cp_parser_next_token_starts_class_definition_p
1965   (cp_parser *);
1966 static bool cp_parser_next_token_ends_template_argument_p
1967   (cp_parser *);
1968 static bool cp_parser_nth_token_starts_template_argument_list_p
1969   (cp_parser *, size_t);
1970 static enum tag_types cp_parser_token_is_class_key
1971   (cp_token *);
1972 static void cp_parser_check_class_key
1973   (enum tag_types, tree type);
1974 static void cp_parser_check_access_in_redeclaration
1975   (tree type, location_t location);
1976 static bool cp_parser_optional_template_keyword
1977   (cp_parser *);
1978 static void cp_parser_pre_parsed_nested_name_specifier
1979   (cp_parser *);
1980 static bool cp_parser_cache_group
1981   (cp_parser *, enum cpp_ttype, unsigned);
1982 static void cp_parser_parse_tentatively
1983   (cp_parser *);
1984 static void cp_parser_commit_to_tentative_parse
1985   (cp_parser *);
1986 static void cp_parser_abort_tentative_parse
1987   (cp_parser *);
1988 static bool cp_parser_parse_definitely
1989   (cp_parser *);
1990 static inline bool cp_parser_parsing_tentatively
1991   (cp_parser *);
1992 static bool cp_parser_uncommitted_to_tentative_parse_p
1993   (cp_parser *);
1994 static void cp_parser_error
1995   (cp_parser *, const char *);
1996 static void cp_parser_name_lookup_error
1997   (cp_parser *, tree, tree, const char *, location_t);
1998 static bool cp_parser_simulate_error
1999   (cp_parser *);
2000 static bool cp_parser_check_type_definition
2001   (cp_parser *);
2002 static void cp_parser_check_for_definition_in_return_type
2003   (cp_declarator *, tree, location_t type_location);
2004 static void cp_parser_check_for_invalid_template_id
2005   (cp_parser *, tree, location_t location);
2006 static bool cp_parser_non_integral_constant_expression
2007   (cp_parser *, const char *);
2008 static void cp_parser_diagnose_invalid_type_name
2009   (cp_parser *, tree, tree, location_t);
2010 static bool cp_parser_parse_and_diagnose_invalid_type_name
2011   (cp_parser *);
2012 static int cp_parser_skip_to_closing_parenthesis
2013   (cp_parser *, bool, bool, bool);
2014 static void cp_parser_skip_to_end_of_statement
2015   (cp_parser *);
2016 static void cp_parser_consume_semicolon_at_end_of_statement
2017   (cp_parser *);
2018 static void cp_parser_skip_to_end_of_block_or_statement
2019   (cp_parser *);
2020 static bool cp_parser_skip_to_closing_brace
2021   (cp_parser *);
2022 static void cp_parser_skip_to_end_of_template_parameter_list
2023   (cp_parser *);
2024 static void cp_parser_skip_to_pragma_eol
2025   (cp_parser*, cp_token *);
2026 static bool cp_parser_error_occurred
2027   (cp_parser *);
2028 static bool cp_parser_allow_gnu_extensions_p
2029   (cp_parser *);
2030 static bool cp_parser_is_string_literal
2031   (cp_token *);
2032 static bool cp_parser_is_keyword
2033   (cp_token *, enum rid);
2034 static tree cp_parser_make_typename_type
2035   (cp_parser *, tree, tree, location_t location);
2036 static cp_declarator * cp_parser_make_indirect_declarator
2037   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2038
2039 /* Returns nonzero if we are parsing tentatively.  */
2040
2041 static inline bool
2042 cp_parser_parsing_tentatively (cp_parser* parser)
2043 {
2044   return parser->context->next != NULL;
2045 }
2046
2047 /* Returns nonzero if TOKEN is a string literal.  */
2048
2049 static bool
2050 cp_parser_is_string_literal (cp_token* token)
2051 {
2052   return (token->type == CPP_STRING ||
2053           token->type == CPP_STRING16 ||
2054           token->type == CPP_STRING32 ||
2055           token->type == CPP_WSTRING);
2056 }
2057
2058 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2059
2060 static bool
2061 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2062 {
2063   return token->keyword == keyword;
2064 }
2065
2066 /* If not parsing tentatively, issue a diagnostic of the form
2067       FILE:LINE: MESSAGE before TOKEN
2068    where TOKEN is the next token in the input stream.  MESSAGE
2069    (specified by the caller) is usually of the form "expected
2070    OTHER-TOKEN".  */
2071
2072 static void
2073 cp_parser_error (cp_parser* parser, const char* message)
2074 {
2075   if (!cp_parser_simulate_error (parser))
2076     {
2077       cp_token *token = cp_lexer_peek_token (parser->lexer);
2078       /* This diagnostic makes more sense if it is tagged to the line
2079          of the token we just peeked at.  */
2080       cp_lexer_set_source_position_from_token (token);
2081
2082       if (token->type == CPP_PRAGMA)
2083         {
2084           error ("%H%<#pragma%> is not allowed here", &token->location);
2085           cp_parser_skip_to_pragma_eol (parser, token);
2086           return;
2087         }
2088
2089       c_parse_error (message,
2090                      /* Because c_parser_error does not understand
2091                         CPP_KEYWORD, keywords are treated like
2092                         identifiers.  */
2093                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2094                      token->u.value);
2095     }
2096 }
2097
2098 /* Issue an error about name-lookup failing.  NAME is the
2099    IDENTIFIER_NODE DECL is the result of
2100    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2101    the thing that we hoped to find.  */
2102
2103 static void
2104 cp_parser_name_lookup_error (cp_parser* parser,
2105                              tree name,
2106                              tree decl,
2107                              const char* desired,
2108                              location_t location)
2109 {
2110   /* If name lookup completely failed, tell the user that NAME was not
2111      declared.  */
2112   if (decl == error_mark_node)
2113     {
2114       if (parser->scope && parser->scope != global_namespace)
2115         error ("%H%<%E::%E%> has not been declared",
2116                &location, parser->scope, name);
2117       else if (parser->scope == global_namespace)
2118         error ("%H%<::%E%> has not been declared", &location, name);
2119       else if (parser->object_scope
2120                && !CLASS_TYPE_P (parser->object_scope))
2121         error ("%Hrequest for member %qE in non-class type %qT",
2122                &location, name, parser->object_scope);
2123       else if (parser->object_scope)
2124         error ("%H%<%T::%E%> has not been declared",
2125                &location, parser->object_scope, name);
2126       else
2127         error ("%H%qE has not been declared", &location, name);
2128     }
2129   else if (parser->scope && parser->scope != global_namespace)
2130     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2131   else if (parser->scope == global_namespace)
2132     error ("%H%<::%E%> %s", &location, name, desired);
2133   else
2134     error ("%H%qE %s", &location, name, desired);
2135 }
2136
2137 /* If we are parsing tentatively, remember that an error has occurred
2138    during this tentative parse.  Returns true if the error was
2139    simulated; false if a message should be issued by the caller.  */
2140
2141 static bool
2142 cp_parser_simulate_error (cp_parser* parser)
2143 {
2144   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2145     {
2146       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2147       return true;
2148     }
2149   return false;
2150 }
2151
2152 /* Check for repeated decl-specifiers.  */
2153
2154 static void
2155 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2156                            location_t location)
2157 {
2158   cp_decl_spec ds;
2159
2160   for (ds = ds_first; ds != ds_last; ++ds)
2161     {
2162       unsigned count = decl_specs->specs[(int)ds];
2163       if (count < 2)
2164         continue;
2165       /* The "long" specifier is a special case because of "long long".  */
2166       if (ds == ds_long)
2167         {
2168           if (count > 2)
2169             error ("%H%<long long long%> is too long for GCC", &location);
2170           else if (pedantic && !in_system_header && warn_long_long
2171                    && cxx_dialect == cxx98)
2172             pedwarn (location, OPT_Wlong_long, 
2173                      "ISO C++ 1998 does not support %<long long%>");
2174         }
2175       else if (count > 1)
2176         {
2177           static const char *const decl_spec_names[] = {
2178             "signed",
2179             "unsigned",
2180             "short",
2181             "long",
2182             "const",
2183             "volatile",
2184             "restrict",
2185             "inline",
2186             "virtual",
2187             "explicit",
2188             "friend",
2189             "typedef",
2190             "__complex",
2191             "__thread"
2192           };
2193           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2194         }
2195     }
2196 }
2197
2198 /* This function is called when a type is defined.  If type
2199    definitions are forbidden at this point, an error message is
2200    issued.  */
2201
2202 static bool
2203 cp_parser_check_type_definition (cp_parser* parser)
2204 {
2205   /* If types are forbidden here, issue a message.  */
2206   if (parser->type_definition_forbidden_message)
2207     {
2208       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2209          in the message need to be interpreted.  */
2210       error (parser->type_definition_forbidden_message);
2211       return false;
2212     }
2213   return true;
2214 }
2215
2216 /* This function is called when the DECLARATOR is processed.  The TYPE
2217    was a type defined in the decl-specifiers.  If it is invalid to
2218    define a type in the decl-specifiers for DECLARATOR, an error is
2219    issued. TYPE_LOCATION is the location of TYPE and is used
2220    for error reporting.  */
2221
2222 static void
2223 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2224                                                tree type, location_t type_location)
2225 {
2226   /* [dcl.fct] forbids type definitions in return types.
2227      Unfortunately, it's not easy to know whether or not we are
2228      processing a return type until after the fact.  */
2229   while (declarator
2230          && (declarator->kind == cdk_pointer
2231              || declarator->kind == cdk_reference
2232              || declarator->kind == cdk_ptrmem))
2233     declarator = declarator->declarator;
2234   if (declarator
2235       && declarator->kind == cdk_function)
2236     {
2237       error ("%Hnew types may not be defined in a return type", &type_location);
2238       inform (type_location, 
2239               "(perhaps a semicolon is missing after the definition of %qT)",
2240               type);
2241     }
2242 }
2243
2244 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2245    "<" in any valid C++ program.  If the next token is indeed "<",
2246    issue a message warning the user about what appears to be an
2247    invalid attempt to form a template-id. LOCATION is the location
2248    of the type-specifier (TYPE) */
2249
2250 static void
2251 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2252                                          tree type, location_t location)
2253 {
2254   cp_token_position start = 0;
2255
2256   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2257     {
2258       if (TYPE_P (type))
2259         error ("%H%qT is not a template", &location, type);
2260       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2261         error ("%H%qE is not a template", &location, type);
2262       else
2263         error ("%Hinvalid template-id", &location);
2264       /* Remember the location of the invalid "<".  */
2265       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2266         start = cp_lexer_token_position (parser->lexer, true);
2267       /* Consume the "<".  */
2268       cp_lexer_consume_token (parser->lexer);
2269       /* Parse the template arguments.  */
2270       cp_parser_enclosed_template_argument_list (parser);
2271       /* Permanently remove the invalid template arguments so that
2272          this error message is not issued again.  */
2273       if (start)
2274         cp_lexer_purge_tokens_after (parser->lexer, start);
2275     }
2276 }
2277
2278 /* If parsing an integral constant-expression, issue an error message
2279    about the fact that THING appeared and return true.  Otherwise,
2280    return false.  In either case, set
2281    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2282
2283 static bool
2284 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2285                                             const char *thing)
2286 {
2287   parser->non_integral_constant_expression_p = true;
2288   if (parser->integral_constant_expression_p)
2289     {
2290       if (!parser->allow_non_integral_constant_expression_p)
2291         {
2292           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2293              in the message need to be interpreted.  */
2294           char *message = concat (thing,
2295                                   " cannot appear in a constant-expression",
2296                                   NULL);
2297           error (message);
2298           free (message);
2299           return true;
2300         }
2301     }
2302   return false;
2303 }
2304
2305 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2306    qualifying scope (or NULL, if none) for ID.  This function commits
2307    to the current active tentative parse, if any.  (Otherwise, the
2308    problematic construct might be encountered again later, resulting
2309    in duplicate error messages.) LOCATION is the location of ID.  */
2310
2311 static void
2312 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2313                                       tree scope, tree id,
2314                                       location_t location)
2315 {
2316   tree decl, old_scope;
2317   /* Try to lookup the identifier.  */
2318   old_scope = parser->scope;
2319   parser->scope = scope;
2320   decl = cp_parser_lookup_name_simple (parser, id, location);
2321   parser->scope = old_scope;
2322   /* If the lookup found a template-name, it means that the user forgot
2323   to specify an argument list. Emit a useful error message.  */
2324   if (TREE_CODE (decl) == TEMPLATE_DECL)
2325     error ("%Hinvalid use of template-name %qE without an argument list",
2326            &location, decl);
2327   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2328     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2329   else if (TREE_CODE (decl) == TYPE_DECL)
2330     /* Something like 'unsigned A a;'  */
2331     error ("%Hinvalid combination of multiple type-specifiers",
2332            &location);
2333   else if (!parser->scope)
2334     {
2335       /* Issue an error message.  */
2336       error ("%H%qE does not name a type", &location, id);
2337       /* If we're in a template class, it's possible that the user was
2338          referring to a type from a base class.  For example:
2339
2340            template <typename T> struct A { typedef T X; };
2341            template <typename T> struct B : public A<T> { X x; };
2342
2343          The user should have said "typename A<T>::X".  */
2344       if (processing_template_decl && current_class_type
2345           && TYPE_BINFO (current_class_type))
2346         {
2347           tree b;
2348
2349           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350                b;
2351                b = TREE_CHAIN (b))
2352             {
2353               tree base_type = BINFO_TYPE (b);
2354               if (CLASS_TYPE_P (base_type)
2355                   && dependent_type_p (base_type))
2356                 {
2357                   tree field;
2358                   /* Go from a particular instantiation of the
2359                      template (which will have an empty TYPE_FIELDs),
2360                      to the main version.  */
2361                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2362                   for (field = TYPE_FIELDS (base_type);
2363                        field;
2364                        field = TREE_CHAIN (field))
2365                     if (TREE_CODE (field) == TYPE_DECL
2366                         && DECL_NAME (field) == id)
2367                       {
2368                         inform (location, 
2369                                 "(perhaps %<typename %T::%E%> was intended)",
2370                                 BINFO_TYPE (b), id);
2371                         break;
2372                       }
2373                   if (field)
2374                     break;
2375                 }
2376             }
2377         }
2378     }
2379   /* Here we diagnose qualified-ids where the scope is actually correct,
2380      but the identifier does not resolve to a valid type name.  */
2381   else if (parser->scope != error_mark_node)
2382     {
2383       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2384         error ("%H%qE in namespace %qE does not name a type",
2385                &location, id, parser->scope);
2386       else if (TYPE_P (parser->scope))
2387         error ("%H%qE in class %qT does not name a type",
2388                &location, id, parser->scope);
2389       else
2390         gcc_unreachable ();
2391     }
2392   cp_parser_commit_to_tentative_parse (parser);
2393 }
2394
2395 /* Check for a common situation where a type-name should be present,
2396    but is not, and issue a sensible error message.  Returns true if an
2397    invalid type-name was detected.
2398
2399    The situation handled by this function are variable declarations of the
2400    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2401    Usually, `ID' should name a type, but if we got here it means that it
2402    does not. We try to emit the best possible error message depending on
2403    how exactly the id-expression looks like.  */
2404
2405 static bool
2406 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2407 {
2408   tree id;
2409   cp_token *token = cp_lexer_peek_token (parser->lexer);
2410
2411   cp_parser_parse_tentatively (parser);
2412   id = cp_parser_id_expression (parser,
2413                                 /*template_keyword_p=*/false,
2414                                 /*check_dependency_p=*/true,
2415                                 /*template_p=*/NULL,
2416                                 /*declarator_p=*/true,
2417                                 /*optional_p=*/false);
2418   /* After the id-expression, there should be a plain identifier,
2419      otherwise this is not a simple variable declaration. Also, if
2420      the scope is dependent, we cannot do much.  */
2421   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2422       || (parser->scope && TYPE_P (parser->scope)
2423           && dependent_type_p (parser->scope))
2424       || TREE_CODE (id) == TYPE_DECL)
2425     {
2426       cp_parser_abort_tentative_parse (parser);
2427       return false;
2428     }
2429   if (!cp_parser_parse_definitely (parser))
2430     return false;
2431
2432   /* Emit a diagnostic for the invalid type.  */
2433   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2434                                         id, token->location);
2435   /* Skip to the end of the declaration; there's no point in
2436      trying to process it.  */
2437   cp_parser_skip_to_end_of_block_or_statement (parser);
2438   return true;
2439 }
2440
2441 /* Consume tokens up to, and including, the next non-nested closing `)'.
2442    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2443    are doing error recovery. Returns -1 if OR_COMMA is true and we
2444    found an unnested comma.  */
2445
2446 static int
2447 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2448                                        bool recovering,
2449                                        bool or_comma,
2450                                        bool consume_paren)
2451 {
2452   unsigned paren_depth = 0;
2453   unsigned brace_depth = 0;
2454
2455   if (recovering && !or_comma
2456       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2457     return 0;
2458
2459   while (true)
2460     {
2461       cp_token * token = cp_lexer_peek_token (parser->lexer);
2462
2463       switch (token->type)
2464         {
2465         case CPP_EOF:
2466         case CPP_PRAGMA_EOL:
2467           /* If we've run out of tokens, then there is no closing `)'.  */
2468           return 0;
2469
2470         case CPP_SEMICOLON:
2471           /* This matches the processing in skip_to_end_of_statement.  */
2472           if (!brace_depth)
2473             return 0;
2474           break;
2475
2476         case CPP_OPEN_BRACE:
2477           ++brace_depth;
2478           break;
2479         case CPP_CLOSE_BRACE:
2480           if (!brace_depth--)
2481             return 0;
2482           break;
2483
2484         case CPP_COMMA:
2485           if (recovering && or_comma && !brace_depth && !paren_depth)
2486             return -1;
2487           break;
2488
2489         case CPP_OPEN_PAREN:
2490           if (!brace_depth)
2491             ++paren_depth;
2492           break;
2493
2494         case CPP_CLOSE_PAREN:
2495           if (!brace_depth && !paren_depth--)
2496             {
2497               if (consume_paren)
2498                 cp_lexer_consume_token (parser->lexer);
2499               return 1;
2500             }
2501           break;
2502
2503         default:
2504           break;
2505         }
2506
2507       /* Consume the token.  */
2508       cp_lexer_consume_token (parser->lexer);
2509     }
2510 }
2511
2512 /* Consume tokens until we reach the end of the current statement.
2513    Normally, that will be just before consuming a `;'.  However, if a
2514    non-nested `}' comes first, then we stop before consuming that.  */
2515
2516 static void
2517 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2518 {
2519   unsigned nesting_depth = 0;
2520
2521   while (true)
2522     {
2523       cp_token *token = cp_lexer_peek_token (parser->lexer);
2524
2525       switch (token->type)
2526         {
2527         case CPP_EOF:
2528         case CPP_PRAGMA_EOL:
2529           /* If we've run out of tokens, stop.  */
2530           return;
2531
2532         case CPP_SEMICOLON:
2533           /* If the next token is a `;', we have reached the end of the
2534              statement.  */
2535           if (!nesting_depth)
2536             return;
2537           break;
2538
2539         case CPP_CLOSE_BRACE:
2540           /* If this is a non-nested '}', stop before consuming it.
2541              That way, when confronted with something like:
2542
2543                { 3 + }
2544
2545              we stop before consuming the closing '}', even though we
2546              have not yet reached a `;'.  */
2547           if (nesting_depth == 0)
2548             return;
2549
2550           /* If it is the closing '}' for a block that we have
2551              scanned, stop -- but only after consuming the token.
2552              That way given:
2553
2554                 void f g () { ... }
2555                 typedef int I;
2556
2557              we will stop after the body of the erroneously declared
2558              function, but before consuming the following `typedef'
2559              declaration.  */
2560           if (--nesting_depth == 0)
2561             {
2562               cp_lexer_consume_token (parser->lexer);
2563               return;
2564             }
2565
2566         case CPP_OPEN_BRACE:
2567           ++nesting_depth;
2568           break;
2569
2570         default:
2571           break;
2572         }
2573
2574       /* Consume the token.  */
2575       cp_lexer_consume_token (parser->lexer);
2576     }
2577 }
2578
2579 /* This function is called at the end of a statement or declaration.
2580    If the next token is a semicolon, it is consumed; otherwise, error
2581    recovery is attempted.  */
2582
2583 static void
2584 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2585 {
2586   /* Look for the trailing `;'.  */
2587   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2588     {
2589       /* If there is additional (erroneous) input, skip to the end of
2590          the statement.  */
2591       cp_parser_skip_to_end_of_statement (parser);
2592       /* If the next token is now a `;', consume it.  */
2593       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2594         cp_lexer_consume_token (parser->lexer);
2595     }
2596 }
2597
2598 /* Skip tokens until we have consumed an entire block, or until we
2599    have consumed a non-nested `;'.  */
2600
2601 static void
2602 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2603 {
2604   int nesting_depth = 0;
2605
2606   while (nesting_depth >= 0)
2607     {
2608       cp_token *token = cp_lexer_peek_token (parser->lexer);
2609
2610       switch (token->type)
2611         {
2612         case CPP_EOF:
2613         case CPP_PRAGMA_EOL:
2614           /* If we've run out of tokens, stop.  */
2615           return;
2616
2617         case CPP_SEMICOLON:
2618           /* Stop if this is an unnested ';'. */
2619           if (!nesting_depth)
2620             nesting_depth = -1;
2621           break;
2622
2623         case CPP_CLOSE_BRACE:
2624           /* Stop if this is an unnested '}', or closes the outermost
2625              nesting level.  */
2626           nesting_depth--;
2627           if (nesting_depth < 0)
2628             return;
2629           if (!nesting_depth)
2630             nesting_depth = -1;
2631           break;
2632
2633         case CPP_OPEN_BRACE:
2634           /* Nest. */
2635           nesting_depth++;
2636           break;
2637
2638         default:
2639           break;
2640         }
2641
2642       /* Consume the token.  */
2643       cp_lexer_consume_token (parser->lexer);
2644     }
2645 }
2646
2647 /* Skip tokens until a non-nested closing curly brace is the next
2648    token, or there are no more tokens. Return true in the first case,
2649    false otherwise.  */
2650
2651 static bool
2652 cp_parser_skip_to_closing_brace (cp_parser *parser)
2653 {
2654   unsigned nesting_depth = 0;
2655
2656   while (true)
2657     {
2658       cp_token *token = cp_lexer_peek_token (parser->lexer);
2659
2660       switch (token->type)
2661         {
2662         case CPP_EOF:
2663         case CPP_PRAGMA_EOL:
2664           /* If we've run out of tokens, stop.  */
2665           return false;
2666
2667         case CPP_CLOSE_BRACE:
2668           /* If the next token is a non-nested `}', then we have reached
2669              the end of the current block.  */
2670           if (nesting_depth-- == 0)
2671             return true;
2672           break;
2673
2674         case CPP_OPEN_BRACE:
2675           /* If it the next token is a `{', then we are entering a new
2676              block.  Consume the entire block.  */
2677           ++nesting_depth;
2678           break;
2679
2680         default:
2681           break;
2682         }
2683
2684       /* Consume the token.  */
2685       cp_lexer_consume_token (parser->lexer);
2686     }
2687 }
2688
2689 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2690    parameter is the PRAGMA token, allowing us to purge the entire pragma
2691    sequence.  */
2692
2693 static void
2694 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2695 {
2696   cp_token *token;
2697
2698   parser->lexer->in_pragma = false;
2699
2700   do
2701     token = cp_lexer_consume_token (parser->lexer);
2702   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2703
2704   /* Ensure that the pragma is not parsed again.  */
2705   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2706 }
2707
2708 /* Require pragma end of line, resyncing with it as necessary.  The
2709    arguments are as for cp_parser_skip_to_pragma_eol.  */
2710
2711 static void
2712 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2713 {
2714   parser->lexer->in_pragma = false;
2715   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2716     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2717 }
2718
2719 /* This is a simple wrapper around make_typename_type. When the id is
2720    an unresolved identifier node, we can provide a superior diagnostic
2721    using cp_parser_diagnose_invalid_type_name.  */
2722
2723 static tree
2724 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2725                               tree id, location_t id_location)
2726 {
2727   tree result;
2728   if (TREE_CODE (id) == IDENTIFIER_NODE)
2729     {
2730       result = make_typename_type (scope, id, typename_type,
2731                                    /*complain=*/tf_none);
2732       if (result == error_mark_node)
2733         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2734       return result;
2735     }
2736   return make_typename_type (scope, id, typename_type, tf_error);
2737 }
2738
2739 /* This is a wrapper around the
2740    make_{pointer,ptrmem,reference}_declarator functions that decides
2741    which one to call based on the CODE and CLASS_TYPE arguments. The
2742    CODE argument should be one of the values returned by
2743    cp_parser_ptr_operator. */
2744 static cp_declarator *
2745 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2746                                     cp_cv_quals cv_qualifiers,
2747                                     cp_declarator *target)
2748 {
2749   if (code == ERROR_MARK)
2750     return cp_error_declarator;
2751
2752   if (code == INDIRECT_REF)
2753     if (class_type == NULL_TREE)
2754       return make_pointer_declarator (cv_qualifiers, target);
2755     else
2756       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2757   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2758     return make_reference_declarator (cv_qualifiers, target, false);
2759   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2760     return make_reference_declarator (cv_qualifiers, target, true);
2761   gcc_unreachable ();
2762 }
2763
2764 /* Create a new C++ parser.  */
2765
2766 static cp_parser *
2767 cp_parser_new (void)
2768 {
2769   cp_parser *parser;
2770   cp_lexer *lexer;
2771   unsigned i;
2772
2773   /* cp_lexer_new_main is called before calling ggc_alloc because
2774      cp_lexer_new_main might load a PCH file.  */
2775   lexer = cp_lexer_new_main ();
2776
2777   /* Initialize the binops_by_token so that we can get the tree
2778      directly from the token.  */
2779   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2780     binops_by_token[binops[i].token_type] = binops[i];
2781
2782   parser = GGC_CNEW (cp_parser);
2783   parser->lexer = lexer;
2784   parser->context = cp_parser_context_new (NULL);
2785
2786   /* For now, we always accept GNU extensions.  */
2787   parser->allow_gnu_extensions_p = 1;
2788
2789   /* The `>' token is a greater-than operator, not the end of a
2790      template-id.  */
2791   parser->greater_than_is_operator_p = true;
2792
2793   parser->default_arg_ok_p = true;
2794
2795   /* We are not parsing a constant-expression.  */
2796   parser->integral_constant_expression_p = false;
2797   parser->allow_non_integral_constant_expression_p = false;
2798   parser->non_integral_constant_expression_p = false;
2799
2800   /* Local variable names are not forbidden.  */
2801   parser->local_variables_forbidden_p = false;
2802
2803   /* We are not processing an `extern "C"' declaration.  */
2804   parser->in_unbraced_linkage_specification_p = false;
2805
2806   /* We are not processing a declarator.  */
2807   parser->in_declarator_p = false;
2808
2809   /* We are not processing a template-argument-list.  */
2810   parser->in_template_argument_list_p = false;
2811
2812   /* We are not in an iteration statement.  */
2813   parser->in_statement = 0;
2814
2815   /* We are not in a switch statement.  */
2816   parser->in_switch_statement_p = false;
2817
2818   /* We are not parsing a type-id inside an expression.  */
2819   parser->in_type_id_in_expr_p = false;
2820
2821   /* Declarations aren't implicitly extern "C".  */
2822   parser->implicit_extern_c = false;
2823
2824   /* String literals should be translated to the execution character set.  */
2825   parser->translate_strings_p = true;
2826
2827   /* We are not parsing a function body.  */
2828   parser->in_function_body = false;
2829
2830   /* The unparsed function queue is empty.  */
2831   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2832
2833   /* There are no classes being defined.  */
2834   parser->num_classes_being_defined = 0;
2835
2836   /* No template parameters apply.  */
2837   parser->num_template_parameter_lists = 0;
2838
2839   return parser;
2840 }
2841
2842 /* Create a cp_lexer structure which will emit the tokens in CACHE
2843    and push it onto the parser's lexer stack.  This is used for delayed
2844    parsing of in-class method bodies and default arguments, and should
2845    not be confused with tentative parsing.  */
2846 static void
2847 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2848 {
2849   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2850   lexer->next = parser->lexer;
2851   parser->lexer = lexer;
2852
2853   /* Move the current source position to that of the first token in the
2854      new lexer.  */
2855   cp_lexer_set_source_position_from_token (lexer->next_token);
2856 }
2857
2858 /* Pop the top lexer off the parser stack.  This is never used for the
2859    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2860 static void
2861 cp_parser_pop_lexer (cp_parser *parser)
2862 {
2863   cp_lexer *lexer = parser->lexer;
2864   parser->lexer = lexer->next;
2865   cp_lexer_destroy (lexer);
2866
2867   /* Put the current source position back where it was before this
2868      lexer was pushed.  */
2869   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2870 }
2871
2872 /* Lexical conventions [gram.lex]  */
2873
2874 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2875    identifier.  */
2876
2877 static tree
2878 cp_parser_identifier (cp_parser* parser)
2879 {
2880   cp_token *token;
2881
2882   /* Look for the identifier.  */
2883   token = cp_parser_require (parser, CPP_NAME, "identifier");
2884   /* Return the value.  */
2885   return token ? token->u.value : error_mark_node;
2886 }
2887
2888 /* Parse a sequence of adjacent string constants.  Returns a
2889    TREE_STRING representing the combined, nul-terminated string
2890    constant.  If TRANSLATE is true, translate the string to the
2891    execution character set.  If WIDE_OK is true, a wide string is
2892    invalid here.
2893
2894    C++98 [lex.string] says that if a narrow string literal token is
2895    adjacent to a wide string literal token, the behavior is undefined.
2896    However, C99 6.4.5p4 says that this results in a wide string literal.
2897    We follow C99 here, for consistency with the C front end.
2898
2899    This code is largely lifted from lex_string() in c-lex.c.
2900
2901    FUTURE: ObjC++ will need to handle @-strings here.  */
2902 static tree
2903 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2904 {
2905   tree value;
2906   size_t count;
2907   struct obstack str_ob;
2908   cpp_string str, istr, *strs;
2909   cp_token *tok;
2910   enum cpp_ttype type;
2911
2912   tok = cp_lexer_peek_token (parser->lexer);
2913   if (!cp_parser_is_string_literal (tok))
2914     {
2915       cp_parser_error (parser, "expected string-literal");
2916       return error_mark_node;
2917     }
2918
2919   type = tok->type;
2920
2921   /* Try to avoid the overhead of creating and destroying an obstack
2922      for the common case of just one string.  */
2923   if (!cp_parser_is_string_literal
2924       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2925     {
2926       cp_lexer_consume_token (parser->lexer);
2927
2928       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2929       str.len = TREE_STRING_LENGTH (tok->u.value);
2930       count = 1;
2931
2932       strs = &str;
2933     }
2934   else
2935     {
2936       gcc_obstack_init (&str_ob);
2937       count = 0;
2938
2939       do
2940         {
2941           cp_lexer_consume_token (parser->lexer);
2942           count++;
2943           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2944           str.len = TREE_STRING_LENGTH (tok->u.value);
2945
2946           if (type != tok->type)
2947             {
2948               if (type == CPP_STRING)
2949                 type = tok->type;
2950               else if (tok->type != CPP_STRING)
2951                 error ("%Hunsupported non-standard concatenation "
2952                        "of string literals", &tok->location);
2953             }
2954
2955           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2956
2957           tok = cp_lexer_peek_token (parser->lexer);
2958         }
2959       while (cp_parser_is_string_literal (tok));
2960
2961       strs = (cpp_string *) obstack_finish (&str_ob);
2962     }
2963
2964   if (type != CPP_STRING && !wide_ok)
2965     {
2966       cp_parser_error (parser, "a wide string is invalid in this context");
2967       type = CPP_STRING;
2968     }
2969
2970   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2971       (parse_in, strs, count, &istr, type))
2972     {
2973       value = build_string (istr.len, (const char *)istr.text);
2974       free (CONST_CAST (unsigned char *, istr.text));
2975
2976       switch (type)
2977         {
2978         default:
2979         case CPP_STRING:
2980           TREE_TYPE (value) = char_array_type_node;
2981           break;
2982         case CPP_STRING16:
2983           TREE_TYPE (value) = char16_array_type_node;
2984           break;
2985         case CPP_STRING32:
2986           TREE_TYPE (value) = char32_array_type_node;
2987           break;
2988         case CPP_WSTRING:
2989           TREE_TYPE (value) = wchar_array_type_node;
2990           break;
2991         }
2992
2993       value = fix_string_type (value);
2994     }
2995   else
2996     /* cpp_interpret_string has issued an error.  */
2997     value = error_mark_node;
2998
2999   if (count > 1)
3000     obstack_free (&str_ob, 0);
3001
3002   return value;
3003 }
3004
3005
3006 /* Basic concepts [gram.basic]  */
3007
3008 /* Parse a translation-unit.
3009
3010    translation-unit:
3011      declaration-seq [opt]
3012
3013    Returns TRUE if all went well.  */
3014
3015 static bool
3016 cp_parser_translation_unit (cp_parser* parser)
3017 {
3018   /* The address of the first non-permanent object on the declarator
3019      obstack.  */
3020   static void *declarator_obstack_base;
3021
3022   bool success;
3023
3024   /* Create the declarator obstack, if necessary.  */
3025   if (!cp_error_declarator)
3026     {
3027       gcc_obstack_init (&declarator_obstack);
3028       /* Create the error declarator.  */
3029       cp_error_declarator = make_declarator (cdk_error);
3030       /* Create the empty parameter list.  */
3031       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3032       /* Remember where the base of the declarator obstack lies.  */
3033       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3034     }
3035
3036   cp_parser_declaration_seq_opt (parser);
3037
3038   /* If there are no tokens left then all went well.  */
3039   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3040     {
3041       /* Get rid of the token array; we don't need it any more.  */
3042       cp_lexer_destroy (parser->lexer);
3043       parser->lexer = NULL;
3044
3045       /* This file might have been a context that's implicitly extern
3046          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3047       if (parser->implicit_extern_c)
3048         {
3049           pop_lang_context ();
3050           parser->implicit_extern_c = false;
3051         }
3052
3053       /* Finish up.  */
3054       finish_translation_unit ();
3055
3056       success = true;
3057     }
3058   else
3059     {
3060       cp_parser_error (parser, "expected declaration");
3061       success = false;
3062     }
3063
3064   /* Make sure the declarator obstack was fully cleaned up.  */
3065   gcc_assert (obstack_next_free (&declarator_obstack)
3066               == declarator_obstack_base);
3067
3068   /* All went well.  */
3069   return success;
3070 }
3071
3072 /* Expressions [gram.expr] */
3073
3074 /* Parse a primary-expression.
3075
3076    primary-expression:
3077      literal
3078      this
3079      ( expression )
3080      id-expression
3081
3082    GNU Extensions:
3083
3084    primary-expression:
3085      ( compound-statement )
3086      __builtin_va_arg ( assignment-expression , type-id )
3087      __builtin_offsetof ( type-id , offsetof-expression )
3088
3089    C++ Extensions:
3090      __has_nothrow_assign ( type-id )   
3091      __has_nothrow_constructor ( type-id )
3092      __has_nothrow_copy ( type-id )
3093      __has_trivial_assign ( type-id )   
3094      __has_trivial_constructor ( type-id )
3095      __has_trivial_copy ( type-id )
3096      __has_trivial_destructor ( type-id )
3097      __has_virtual_destructor ( type-id )     
3098      __is_abstract ( type-id )
3099      __is_base_of ( type-id , type-id )
3100      __is_class ( type-id )
3101      __is_convertible_to ( type-id , type-id )     
3102      __is_empty ( type-id )
3103      __is_enum ( type-id )
3104      __is_pod ( type-id )
3105      __is_polymorphic ( type-id )
3106      __is_union ( type-id )
3107
3108    Objective-C++ Extension:
3109
3110    primary-expression:
3111      objc-expression
3112
3113    literal:
3114      __null
3115
3116    ADDRESS_P is true iff this expression was immediately preceded by
3117    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3118    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3119    true iff this expression is a template argument.
3120
3121    Returns a representation of the expression.  Upon return, *IDK
3122    indicates what kind of id-expression (if any) was present.  */
3123
3124 static tree
3125 cp_parser_primary_expression (cp_parser *parser,
3126                               bool address_p,
3127                               bool cast_p,
3128                               bool template_arg_p,
3129                               cp_id_kind *idk)
3130 {
3131   cp_token *token = NULL;
3132
3133   /* Assume the primary expression is not an id-expression.  */
3134   *idk = CP_ID_KIND_NONE;
3135
3136   /* Peek at the next token.  */
3137   token = cp_lexer_peek_token (parser->lexer);
3138   switch (token->type)
3139     {
3140       /* literal:
3141            integer-literal
3142            character-literal
3143            floating-literal
3144            string-literal
3145            boolean-literal  */
3146     case CPP_CHAR:
3147     case CPP_CHAR16:
3148     case CPP_CHAR32:
3149     case CPP_WCHAR:
3150     case CPP_NUMBER:
3151       token = cp_lexer_consume_token (parser->lexer);
3152       if (TREE_CODE (token->u.value) == FIXED_CST)
3153         {
3154           error ("%Hfixed-point types not supported in C++",
3155                  &token->location);
3156           return error_mark_node;
3157         }
3158       /* Floating-point literals are only allowed in an integral
3159          constant expression if they are cast to an integral or
3160          enumeration type.  */
3161       if (TREE_CODE (token->u.value) == REAL_CST
3162           && parser->integral_constant_expression_p
3163           && pedantic)
3164         {
3165           /* CAST_P will be set even in invalid code like "int(2.7 +
3166              ...)".   Therefore, we have to check that the next token
3167              is sure to end the cast.  */
3168           if (cast_p)
3169             {
3170               cp_token *next_token;
3171
3172               next_token = cp_lexer_peek_token (parser->lexer);
3173               if (/* The comma at the end of an
3174                      enumerator-definition.  */
3175                   next_token->type != CPP_COMMA
3176                   /* The curly brace at the end of an enum-specifier.  */
3177                   && next_token->type != CPP_CLOSE_BRACE
3178                   /* The end of a statement.  */
3179                   && next_token->type != CPP_SEMICOLON
3180                   /* The end of the cast-expression.  */
3181                   && next_token->type != CPP_CLOSE_PAREN
3182                   /* The end of an array bound.  */
3183                   && next_token->type != CPP_CLOSE_SQUARE
3184                   /* The closing ">" in a template-argument-list.  */
3185                   && (next_token->type != CPP_GREATER
3186                       || parser->greater_than_is_operator_p)
3187                   /* C++0x only: A ">>" treated like two ">" tokens,
3188                      in a template-argument-list.  */
3189                   && (next_token->type != CPP_RSHIFT
3190                       || (cxx_dialect == cxx98)
3191                       || parser->greater_than_is_operator_p))
3192                 cast_p = false;
3193             }
3194
3195           /* If we are within a cast, then the constraint that the
3196              cast is to an integral or enumeration type will be
3197              checked at that point.  If we are not within a cast, then
3198              this code is invalid.  */
3199           if (!cast_p)
3200             cp_parser_non_integral_constant_expression
3201               (parser, "floating-point literal");
3202         }
3203       return token->u.value;
3204
3205     case CPP_STRING:
3206     case CPP_STRING16:
3207     case CPP_STRING32:
3208     case CPP_WSTRING:
3209       /* ??? Should wide strings be allowed when parser->translate_strings_p
3210          is false (i.e. in attributes)?  If not, we can kill the third
3211          argument to cp_parser_string_literal.  */
3212       return cp_parser_string_literal (parser,
3213                                        parser->translate_strings_p,
3214                                        true);
3215
3216     case CPP_OPEN_PAREN:
3217       {
3218         tree expr;
3219         bool saved_greater_than_is_operator_p;
3220
3221         /* Consume the `('.  */
3222         cp_lexer_consume_token (parser->lexer);
3223         /* Within a parenthesized expression, a `>' token is always
3224            the greater-than operator.  */
3225         saved_greater_than_is_operator_p
3226           = parser->greater_than_is_operator_p;
3227         parser->greater_than_is_operator_p = true;
3228         /* If we see `( { ' then we are looking at the beginning of
3229            a GNU statement-expression.  */
3230         if (cp_parser_allow_gnu_extensions_p (parser)
3231             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3232           {
3233             /* Statement-expressions are not allowed by the standard.  */
3234             pedwarn (token->location, OPT_pedantic, 
3235                      "ISO C++ forbids braced-groups within expressions");
3236
3237             /* And they're not allowed outside of a function-body; you
3238                cannot, for example, write:
3239
3240                  int i = ({ int j = 3; j + 1; });
3241
3242                at class or namespace scope.  */
3243             if (!parser->in_function_body
3244                 || parser->in_template_argument_list_p)
3245               {
3246                 error ("%Hstatement-expressions are not allowed outside "
3247                        "functions nor in template-argument lists",
3248                        &token->location);
3249                 cp_parser_skip_to_end_of_block_or_statement (parser);
3250                 expr = error_mark_node;
3251               }
3252             else
3253               {
3254                 /* Start the statement-expression.  */
3255                 expr = begin_stmt_expr ();
3256                 /* Parse the compound-statement.  */
3257                 cp_parser_compound_statement (parser, expr, false);
3258                 /* Finish up.  */
3259                 expr = finish_stmt_expr (expr, false);
3260               }
3261           }
3262         else
3263           {
3264             /* Parse the parenthesized expression.  */
3265             expr = cp_parser_expression (parser, cast_p, idk);
3266             /* Let the front end know that this expression was
3267                enclosed in parentheses. This matters in case, for
3268                example, the expression is of the form `A::B', since
3269                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3270                not.  */
3271             finish_parenthesized_expr (expr);
3272           }
3273         /* The `>' token might be the end of a template-id or
3274            template-parameter-list now.  */
3275         parser->greater_than_is_operator_p
3276           = saved_greater_than_is_operator_p;
3277         /* Consume the `)'.  */
3278         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3279           cp_parser_skip_to_end_of_statement (parser);
3280
3281         return expr;
3282       }
3283
3284     case CPP_KEYWORD:
3285       switch (token->keyword)
3286         {
3287           /* These two are the boolean literals.  */
3288         case RID_TRUE:
3289           cp_lexer_consume_token (parser->lexer);
3290           return boolean_true_node;
3291         case RID_FALSE:
3292           cp_lexer_consume_token (parser->lexer);
3293           return boolean_false_node;
3294
3295           /* The `__null' literal.  */
3296         case RID_NULL:
3297           cp_lexer_consume_token (parser->lexer);
3298           return null_node;
3299
3300           /* Recognize the `this' keyword.  */
3301         case RID_THIS:
3302           cp_lexer_consume_token (parser->lexer);
3303           if (parser->local_variables_forbidden_p)
3304             {
3305               error ("%H%<this%> may not be used in this context",
3306                      &token->location);
3307               return error_mark_node;
3308             }
3309           /* Pointers cannot appear in constant-expressions.  */
3310           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3311             return error_mark_node;
3312           return finish_this_expr ();
3313
3314           /* The `operator' keyword can be the beginning of an
3315              id-expression.  */
3316         case RID_OPERATOR:
3317           goto id_expression;
3318
3319         case RID_FUNCTION_NAME:
3320         case RID_PRETTY_FUNCTION_NAME:
3321         case RID_C99_FUNCTION_NAME:
3322           {
3323             const char *name;
3324
3325             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3326                __func__ are the names of variables -- but they are
3327                treated specially.  Therefore, they are handled here,
3328                rather than relying on the generic id-expression logic
3329                below.  Grammatically, these names are id-expressions.
3330
3331                Consume the token.  */
3332             token = cp_lexer_consume_token (parser->lexer);
3333
3334             switch (token->keyword)
3335               {
3336               case RID_FUNCTION_NAME:
3337                 name = "%<__FUNCTION__%>";
3338                 break;
3339               case RID_PRETTY_FUNCTION_NAME:
3340                 name = "%<__PRETTY_FUNCTION__%>";
3341                 break;
3342               case RID_C99_FUNCTION_NAME:
3343                 name = "%<__func__%>";
3344                 break;
3345               default:
3346                 gcc_unreachable ();
3347               }
3348
3349             if (cp_parser_non_integral_constant_expression (parser, name))
3350               return error_mark_node;
3351
3352             /* Look up the name.  */
3353             return finish_fname (token->u.value);
3354           }
3355
3356         case RID_VA_ARG:
3357           {
3358             tree expression;
3359             tree type;
3360
3361             /* The `__builtin_va_arg' construct is used to handle
3362                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3363             cp_lexer_consume_token (parser->lexer);
3364             /* Look for the opening `('.  */
3365             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3366             /* Now, parse the assignment-expression.  */
3367             expression = cp_parser_assignment_expression (parser,
3368                                                           /*cast_p=*/false, NULL);
3369             /* Look for the `,'.  */
3370             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3371             /* Parse the type-id.  */
3372             type = cp_parser_type_id (parser);
3373             /* Look for the closing `)'.  */
3374             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3375             /* Using `va_arg' in a constant-expression is not
3376                allowed.  */
3377             if (cp_parser_non_integral_constant_expression (parser,
3378                                                             "%<va_arg%>"))
3379               return error_mark_node;
3380             return build_x_va_arg (expression, type);
3381           }
3382
3383         case RID_OFFSETOF:
3384           return cp_parser_builtin_offsetof (parser);
3385
3386         case RID_HAS_NOTHROW_ASSIGN:
3387         case RID_HAS_NOTHROW_CONSTRUCTOR:
3388         case RID_HAS_NOTHROW_COPY:        
3389         case RID_HAS_TRIVIAL_ASSIGN:
3390         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3391         case RID_HAS_TRIVIAL_COPY:        
3392         case RID_HAS_TRIVIAL_DESTRUCTOR:
3393         case RID_HAS_VIRTUAL_DESTRUCTOR:
3394         case RID_IS_ABSTRACT:
3395         case RID_IS_BASE_OF:
3396         case RID_IS_CLASS:
3397         case RID_IS_CONVERTIBLE_TO:
3398         case RID_IS_EMPTY:
3399         case RID_IS_ENUM:
3400         case RID_IS_POD:
3401         case RID_IS_POLYMORPHIC:
3402         case RID_IS_UNION:
3403           return cp_parser_trait_expr (parser, token->keyword);
3404
3405         /* Objective-C++ expressions.  */
3406         case RID_AT_ENCODE:
3407         case RID_AT_PROTOCOL:
3408         case RID_AT_SELECTOR:
3409           return cp_parser_objc_expression (parser);
3410
3411         default:
3412           cp_parser_error (parser, "expected primary-expression");
3413           return error_mark_node;
3414         }
3415
3416       /* An id-expression can start with either an identifier, a
3417          `::' as the beginning of a qualified-id, or the "operator"
3418          keyword.  */
3419     case CPP_NAME:
3420     case CPP_SCOPE:
3421     case CPP_TEMPLATE_ID:
3422     case CPP_NESTED_NAME_SPECIFIER:
3423       {
3424         tree id_expression;
3425         tree decl;
3426         const char *error_msg;
3427         bool template_p;
3428         bool done;
3429         cp_token *id_expr_token;
3430
3431       id_expression:
3432         /* Parse the id-expression.  */
3433         id_expression
3434           = cp_parser_id_expression (parser,
3435                                      /*template_keyword_p=*/false,
3436                                      /*check_dependency_p=*/true,
3437                                      &template_p,
3438                                      /*declarator_p=*/false,
3439                                      /*optional_p=*/false);
3440         if (id_expression == error_mark_node)
3441           return error_mark_node;
3442         id_expr_token = token;
3443         token = cp_lexer_peek_token (parser->lexer);
3444         done = (token->type != CPP_OPEN_SQUARE
3445                 && token->type != CPP_OPEN_PAREN
3446                 && token->type != CPP_DOT
3447                 && token->type != CPP_DEREF
3448                 && token->type != CPP_PLUS_PLUS
3449                 && token->type != CPP_MINUS_MINUS);
3450         /* If we have a template-id, then no further lookup is
3451            required.  If the template-id was for a template-class, we
3452            will sometimes have a TYPE_DECL at this point.  */
3453         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3454                  || TREE_CODE (id_expression) == TYPE_DECL)
3455           decl = id_expression;
3456         /* Look up the name.  */
3457         else
3458           {
3459             tree ambiguous_decls;
3460
3461             decl = cp_parser_lookup_name (parser, id_expression,
3462                                           none_type,
3463                                           template_p,
3464                                           /*is_namespace=*/false,
3465                                           /*check_dependency=*/true,
3466                                           &ambiguous_decls,
3467                                           id_expr_token->location);
3468             /* If the lookup was ambiguous, an error will already have
3469                been issued.  */
3470             if (ambiguous_decls)
3471               return error_mark_node;
3472
3473             /* In Objective-C++, an instance variable (ivar) may be preferred
3474                to whatever cp_parser_lookup_name() found.  */
3475             decl = objc_lookup_ivar (decl, id_expression);
3476
3477             /* If name lookup gives us a SCOPE_REF, then the
3478                qualifying scope was dependent.  */
3479             if (TREE_CODE (decl) == SCOPE_REF)
3480               {
3481                 /* At this point, we do not know if DECL is a valid
3482                    integral constant expression.  We assume that it is
3483                    in fact such an expression, so that code like:
3484
3485                       template <int N> struct A {
3486                         int a[B<N>::i];
3487                       };
3488                      
3489                    is accepted.  At template-instantiation time, we
3490                    will check that B<N>::i is actually a constant.  */
3491                 return decl;
3492               }
3493             /* Check to see if DECL is a local variable in a context
3494                where that is forbidden.  */
3495             if (parser->local_variables_forbidden_p
3496                 && local_variable_p (decl))
3497               {
3498                 /* It might be that we only found DECL because we are
3499                    trying to be generous with pre-ISO scoping rules.
3500                    For example, consider:
3501
3502                      int i;
3503                      void g() {
3504                        for (int i = 0; i < 10; ++i) {}
3505                        extern void f(int j = i);
3506                      }
3507
3508                    Here, name look up will originally find the out
3509                    of scope `i'.  We need to issue a warning message,
3510                    but then use the global `i'.  */
3511                 decl = check_for_out_of_scope_variable (decl);
3512                 if (local_variable_p (decl))
3513                   {
3514                     error ("%Hlocal variable %qD may not appear in this context",
3515                            &id_expr_token->location, decl);
3516                     return error_mark_node;
3517                   }
3518               }
3519           }
3520
3521         decl = (finish_id_expression
3522                 (id_expression, decl, parser->scope,
3523                  idk,
3524                  parser->integral_constant_expression_p,
3525                  parser->allow_non_integral_constant_expression_p,
3526                  &parser->non_integral_constant_expression_p,
3527                  template_p, done, address_p,
3528                  template_arg_p,
3529                  &error_msg,
3530                  id_expr_token->location));
3531         if (error_msg)
3532           cp_parser_error (parser, error_msg);
3533         return decl;
3534       }
3535
3536       /* Anything else is an error.  */
3537     default:
3538       /* ...unless we have an Objective-C++ message or string literal,
3539          that is.  */
3540       if (c_dialect_objc ()
3541           && (token->type == CPP_OPEN_SQUARE
3542               || token->type == CPP_OBJC_STRING))
3543         return cp_parser_objc_expression (parser);
3544
3545       cp_parser_error (parser, "expected primary-expression");
3546       return error_mark_node;
3547     }
3548 }
3549
3550 /* Parse an id-expression.
3551
3552    id-expression:
3553      unqualified-id
3554      qualified-id
3555
3556    qualified-id:
3557      :: [opt] nested-name-specifier template [opt] unqualified-id
3558      :: identifier
3559      :: operator-function-id
3560      :: template-id
3561
3562    Return a representation of the unqualified portion of the
3563    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3564    a `::' or nested-name-specifier.
3565
3566    Often, if the id-expression was a qualified-id, the caller will
3567    want to make a SCOPE_REF to represent the qualified-id.  This
3568    function does not do this in order to avoid wastefully creating
3569    SCOPE_REFs when they are not required.
3570
3571    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3572    `template' keyword.
3573
3574    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3575    uninstantiated templates.
3576
3577    If *TEMPLATE_P is non-NULL, it is set to true iff the
3578    `template' keyword is used to explicitly indicate that the entity
3579    named is a template.
3580
3581    If DECLARATOR_P is true, the id-expression is appearing as part of
3582    a declarator, rather than as part of an expression.  */
3583
3584 static tree
3585 cp_parser_id_expression (cp_parser *parser,
3586                          bool template_keyword_p,
3587                          bool check_dependency_p,
3588                          bool *template_p,
3589                          bool declarator_p,
3590                          bool optional_p)
3591 {
3592   bool global_scope_p;
3593   bool nested_name_specifier_p;
3594
3595   /* Assume the `template' keyword was not used.  */
3596   if (template_p)
3597     *template_p = template_keyword_p;
3598
3599   /* Look for the optional `::' operator.  */
3600   global_scope_p
3601     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3602        != NULL_TREE);
3603   /* Look for the optional nested-name-specifier.  */
3604   nested_name_specifier_p
3605     = (cp_parser_nested_name_specifier_opt (parser,
3606                                             /*typename_keyword_p=*/false,
3607                                             check_dependency_p,
3608                                             /*type_p=*/false,
3609                                             declarator_p)
3610        != NULL_TREE);
3611   /* If there is a nested-name-specifier, then we are looking at
3612      the first qualified-id production.  */
3613   if (nested_name_specifier_p)
3614     {
3615       tree saved_scope;
3616       tree saved_object_scope;
3617       tree saved_qualifying_scope;
3618       tree unqualified_id;
3619       bool is_template;
3620
3621       /* See if the next token is the `template' keyword.  */
3622       if (!template_p)
3623         template_p = &is_template;
3624       *template_p = cp_parser_optional_template_keyword (parser);
3625       /* Name lookup we do during the processing of the
3626          unqualified-id might obliterate SCOPE.  */
3627       saved_scope = parser->scope;
3628       saved_object_scope = parser->object_scope;
3629       saved_qualifying_scope = parser->qualifying_scope;
3630       /* Process the final unqualified-id.  */
3631       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3632                                                  check_dependency_p,
3633                                                  declarator_p,
3634                                                  /*optional_p=*/false);
3635       /* Restore the SAVED_SCOPE for our caller.  */
3636       parser->scope = saved_scope;
3637       parser->object_scope = saved_object_scope;
3638       parser->qualifying_scope = saved_qualifying_scope;
3639
3640       return unqualified_id;
3641     }
3642   /* Otherwise, if we are in global scope, then we are looking at one
3643      of the other qualified-id productions.  */
3644   else if (global_scope_p)
3645     {
3646       cp_token *token;
3647       tree id;
3648
3649       /* Peek at the next token.  */
3650       token = cp_lexer_peek_token (parser->lexer);
3651
3652       /* If it's an identifier, and the next token is not a "<", then
3653          we can avoid the template-id case.  This is an optimization
3654          for this common case.  */
3655       if (token->type == CPP_NAME
3656           && !cp_parser_nth_token_starts_template_argument_list_p
3657                (parser, 2))
3658         return cp_parser_identifier (parser);
3659
3660       cp_parser_parse_tentatively (parser);
3661       /* Try a template-id.  */
3662       id = cp_parser_template_id (parser,
3663                                   /*template_keyword_p=*/false,
3664                                   /*check_dependency_p=*/true,
3665                                   declarator_p);
3666       /* If that worked, we're done.  */
3667       if (cp_parser_parse_definitely (parser))
3668         return id;
3669
3670       /* Peek at the next token.  (Changes in the token buffer may
3671          have invalidated the pointer obtained above.)  */
3672       token = cp_lexer_peek_token (parser->lexer);
3673
3674       switch (token->type)
3675         {
3676         case CPP_NAME:
3677           return cp_parser_identifier (parser);
3678
3679         case CPP_KEYWORD:
3680           if (token->keyword == RID_OPERATOR)
3681             return cp_parser_operator_function_id (parser);
3682           /* Fall through.  */
3683
3684         default:
3685           cp_parser_error (parser, "expected id-expression");
3686           return error_mark_node;
3687         }
3688     }
3689   else
3690     return cp_parser_unqualified_id (parser, template_keyword_p,
3691                                      /*check_dependency_p=*/true,
3692                                      declarator_p,
3693                                      optional_p);
3694 }
3695
3696 /* Parse an unqualified-id.
3697
3698    unqualified-id:
3699      identifier
3700      operator-function-id
3701      conversion-function-id
3702      ~ class-name
3703      template-id
3704
3705    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3706    keyword, in a construct like `A::template ...'.
3707
3708    Returns a representation of unqualified-id.  For the `identifier'
3709    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3710    production a BIT_NOT_EXPR is returned; the operand of the
3711    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3712    other productions, see the documentation accompanying the
3713    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3714    names are looked up in uninstantiated templates.  If DECLARATOR_P
3715    is true, the unqualified-id is appearing as part of a declarator,
3716    rather than as part of an expression.  */
3717
3718 static tree
3719 cp_parser_unqualified_id (cp_parser* parser,
3720                           bool template_keyword_p,
3721                           bool check_dependency_p,
3722                           bool declarator_p,
3723                           bool optional_p)
3724 {
3725   cp_token *token;
3726
3727   /* Peek at the next token.  */
3728   token = cp_lexer_peek_token (parser->lexer);
3729
3730   switch (token->type)
3731     {
3732     case CPP_NAME:
3733       {
3734         tree id;
3735
3736         /* We don't know yet whether or not this will be a
3737            template-id.  */
3738         cp_parser_parse_tentatively (parser);
3739         /* Try a template-id.  */
3740         id = cp_parser_template_id (parser, template_keyword_p,
3741                                     check_dependency_p,
3742                                     declarator_p);
3743         /* If it worked, we're done.  */
3744         if (cp_parser_parse_definitely (parser))
3745           return id;
3746         /* Otherwise, it's an ordinary identifier.  */
3747         return cp_parser_identifier (parser);
3748       }
3749
3750     case CPP_TEMPLATE_ID:
3751       return cp_parser_template_id (parser, template_keyword_p,
3752                                     check_dependency_p,
3753                                     declarator_p);
3754
3755     case CPP_COMPL:
3756       {
3757         tree type_decl;
3758         tree qualifying_scope;
3759         tree object_scope;
3760         tree scope;
3761         bool done;
3762
3763         /* Consume the `~' token.  */
3764         cp_lexer_consume_token (parser->lexer);
3765         /* Parse the class-name.  The standard, as written, seems to
3766            say that:
3767
3768              template <typename T> struct S { ~S (); };
3769              template <typename T> S<T>::~S() {}
3770
3771            is invalid, since `~' must be followed by a class-name, but
3772            `S<T>' is dependent, and so not known to be a class.
3773            That's not right; we need to look in uninstantiated
3774            templates.  A further complication arises from:
3775
3776              template <typename T> void f(T t) {
3777                t.T::~T();
3778              }
3779
3780            Here, it is not possible to look up `T' in the scope of `T'
3781            itself.  We must look in both the current scope, and the
3782            scope of the containing complete expression.
3783
3784            Yet another issue is:
3785
3786              struct S {
3787                int S;
3788                ~S();
3789              };
3790
3791              S::~S() {}
3792
3793            The standard does not seem to say that the `S' in `~S'
3794            should refer to the type `S' and not the data member
3795            `S::S'.  */
3796
3797         /* DR 244 says that we look up the name after the "~" in the
3798            same scope as we looked up the qualifying name.  That idea
3799            isn't fully worked out; it's more complicated than that.  */
3800         scope = parser->scope;
3801         object_scope = parser->object_scope;
3802         qualifying_scope = parser->qualifying_scope;
3803
3804         /* Check for invalid scopes.  */
3805         if (scope == error_mark_node)
3806           {
3807             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3808               cp_lexer_consume_token (parser->lexer);
3809             return error_mark_node;
3810           }
3811         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3812           {
3813             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3814               error ("%Hscope %qT before %<~%> is not a class-name",
3815                      &token->location, scope);
3816             cp_parser_simulate_error (parser);
3817             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3818               cp_lexer_consume_token (parser->lexer);
3819             return error_mark_node;
3820           }
3821         gcc_assert (!scope || TYPE_P (scope));
3822
3823         /* If the name is of the form "X::~X" it's OK.  */
3824         token = cp_lexer_peek_token (parser->lexer);
3825         if (scope
3826             && token->type == CPP_NAME
3827             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3828                 == CPP_OPEN_PAREN)
3829             && constructor_name_p (token->u.value, scope))
3830           {
3831             cp_lexer_consume_token (parser->lexer);
3832             return build_nt (BIT_NOT_EXPR, scope);
3833           }
3834
3835         /* If there was an explicit qualification (S::~T), first look
3836            in the scope given by the qualification (i.e., S).  */
3837         done = false;
3838         type_decl = NULL_TREE;
3839         if (scope)
3840           {
3841             cp_parser_parse_tentatively (parser);
3842             type_decl = cp_parser_class_name (parser,
3843                                               /*typename_keyword_p=*/false,
3844                                               /*template_keyword_p=*/false,
3845                                               none_type,
3846                                               /*check_dependency=*/false,
3847                                               /*class_head_p=*/false,
3848                                               declarator_p);
3849             if (cp_parser_parse_definitely (parser))
3850               done = true;
3851           }
3852         /* In "N::S::~S", look in "N" as well.  */
3853         if (!done && scope && qualifying_scope)
3854           {
3855             cp_parser_parse_tentatively (parser);
3856             parser->scope = qualifying_scope;
3857             parser->object_scope = NULL_TREE;
3858             parser->qualifying_scope = NULL_TREE;
3859             type_decl
3860               = cp_parser_class_name (parser,
3861                                       /*typename_keyword_p=*/false,
3862                                       /*template_keyword_p=*/false,
3863                                       none_type,
3864                                       /*check_dependency=*/false,
3865                                       /*class_head_p=*/false,
3866                                       declarator_p);
3867             if (cp_parser_parse_definitely (parser))
3868               done = true;
3869           }
3870         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3871         else if (!done && object_scope)
3872           {
3873             cp_parser_parse_tentatively (parser);
3874             parser->scope = object_scope;
3875             parser->object_scope = NULL_TREE;
3876             parser->qualifying_scope = NULL_TREE;
3877             type_decl
3878               = cp_parser_class_name (parser,
3879                                       /*typename_keyword_p=*/false,
3880                                       /*template_keyword_p=*/false,
3881                                       none_type,
3882                                       /*check_dependency=*/false,
3883                                       /*class_head_p=*/false,
3884                                       declarator_p);
3885             if (cp_parser_parse_definitely (parser))
3886               done = true;
3887           }
3888         /* Look in the surrounding context.  */
3889         if (!done)
3890           {
3891             parser->scope = NULL_TREE;
3892             parser->object_scope = NULL_TREE;
3893             parser->qualifying_scope = NULL_TREE;
3894             if (processing_template_decl)
3895               cp_parser_parse_tentatively (parser);
3896             type_decl
3897               = cp_parser_class_name (parser,
3898                                       /*typename_keyword_p=*/false,
3899                                       /*template_keyword_p=*/false,
3900                                       none_type,
3901                                       /*check_dependency=*/false,
3902                                       /*class_head_p=*/false,
3903                                       declarator_p);
3904             if (processing_template_decl
3905                 && ! cp_parser_parse_definitely (parser))
3906               {
3907                 /* We couldn't find a type with this name, so just accept
3908                    it and check for a match at instantiation time.  */
3909                 type_decl = cp_parser_identifier (parser);
3910                 if (type_decl != error_mark_node)
3911                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3912                 return type_decl;
3913               }
3914           }
3915         /* If an error occurred, assume that the name of the
3916            destructor is the same as the name of the qualifying
3917            class.  That allows us to keep parsing after running
3918            into ill-formed destructor names.  */
3919         if (type_decl == error_mark_node && scope)
3920           return build_nt (BIT_NOT_EXPR, scope);
3921         else if (type_decl == error_mark_node)
3922           return error_mark_node;
3923
3924         /* Check that destructor name and scope match.  */
3925         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3926           {
3927             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3928               error ("%Hdeclaration of %<~%T%> as member of %qT",
3929                      &token->location, type_decl, scope);
3930             cp_parser_simulate_error (parser);
3931             return error_mark_node;
3932           }
3933
3934         /* [class.dtor]
3935
3936            A typedef-name that names a class shall not be used as the
3937            identifier in the declarator for a destructor declaration.  */
3938         if (declarator_p
3939             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3940             && !DECL_SELF_REFERENCE_P (type_decl)
3941             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3942           error ("%Htypedef-name %qD used as destructor declarator",
3943                  &token->location, type_decl);
3944
3945         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3946       }
3947
3948     case CPP_KEYWORD:
3949       if (token->keyword == RID_OPERATOR)
3950         {
3951           tree id;
3952
3953           /* This could be a template-id, so we try that first.  */
3954           cp_parser_parse_tentatively (parser);
3955           /* Try a template-id.  */
3956           id = cp_parser_template_id (parser, template_keyword_p,
3957                                       /*check_dependency_p=*/true,
3958                                       declarator_p);
3959           /* If that worked, we're done.  */
3960           if (cp_parser_parse_definitely (parser))
3961             return id;
3962           /* We still don't know whether we're looking at an
3963              operator-function-id or a conversion-function-id.  */
3964           cp_parser_parse_tentatively (parser);
3965           /* Try an operator-function-id.  */
3966           id = cp_parser_operator_function_id (parser);
3967           /* If that didn't work, try a conversion-function-id.  */
3968           if (!cp_parser_parse_definitely (parser))
3969             id = cp_parser_conversion_function_id (parser);
3970
3971           return id;
3972         }
3973       /* Fall through.  */
3974
3975     default:
3976       if (optional_p)
3977         return NULL_TREE;
3978       cp_parser_error (parser, "expected unqualified-id");
3979       return error_mark_node;
3980     }
3981 }
3982
3983 /* Parse an (optional) nested-name-specifier.
3984
3985    nested-name-specifier: [C++98]
3986      class-or-namespace-name :: nested-name-specifier [opt]
3987      class-or-namespace-name :: template nested-name-specifier [opt]
3988
3989    nested-name-specifier: [C++0x]
3990      type-name ::
3991      namespace-name ::
3992      nested-name-specifier identifier ::
3993      nested-name-specifier template [opt] simple-template-id ::
3994
3995    PARSER->SCOPE should be set appropriately before this function is
3996    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3997    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3998    in name lookups.
3999
4000    Sets PARSER->SCOPE to the class (TYPE) or namespace
4001    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4002    it unchanged if there is no nested-name-specifier.  Returns the new
4003    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4004
4005    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4006    part of a declaration and/or decl-specifier.  */
4007
4008 static tree
4009 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4010                                      bool typename_keyword_p,
4011                                      bool check_dependency_p,
4012                                      bool type_p,
4013                                      bool is_declaration)
4014 {
4015   bool success = false;
4016   cp_token_position start = 0;
4017   cp_token *token;
4018
4019   /* Remember where the nested-name-specifier starts.  */
4020   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4021     {
4022       start = cp_lexer_token_position (parser->lexer, false);
4023       push_deferring_access_checks (dk_deferred);
4024     }
4025
4026   while (true)
4027     {
4028       tree new_scope;
4029       tree old_scope;
4030       tree saved_qualifying_scope;
4031       bool template_keyword_p;
4032
4033       /* Spot cases that cannot be the beginning of a
4034          nested-name-specifier.  */
4035       token = cp_lexer_peek_token (parser->lexer);
4036
4037       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4038          the already parsed nested-name-specifier.  */
4039       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4040         {
4041           /* Grab the nested-name-specifier and continue the loop.  */
4042           cp_parser_pre_parsed_nested_name_specifier (parser);
4043           /* If we originally encountered this nested-name-specifier
4044              with IS_DECLARATION set to false, we will not have
4045              resolved TYPENAME_TYPEs, so we must do so here.  */
4046           if (is_declaration
4047               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4048             {
4049               new_scope = resolve_typename_type (parser->scope,
4050                                                  /*only_current_p=*/false);
4051               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4052                 parser->scope = new_scope;
4053             }
4054           success = true;
4055           continue;
4056         }
4057
4058       /* Spot cases that cannot be the beginning of a
4059          nested-name-specifier.  On the second and subsequent times
4060          through the loop, we look for the `template' keyword.  */
4061       if (success && token->keyword == RID_TEMPLATE)
4062         ;
4063       /* A template-id can start a nested-name-specifier.  */
4064       else if (token->type == CPP_TEMPLATE_ID)
4065         ;
4066       else
4067         {
4068           /* If the next token is not an identifier, then it is
4069              definitely not a type-name or namespace-name.  */
4070           if (token->type != CPP_NAME)
4071             break;
4072           /* If the following token is neither a `<' (to begin a
4073              template-id), nor a `::', then we are not looking at a
4074              nested-name-specifier.  */
4075           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4076           if (token->type != CPP_SCOPE
4077               && !cp_parser_nth_token_starts_template_argument_list_p
4078                   (parser, 2))
4079             break;
4080         }
4081
4082       /* The nested-name-specifier is optional, so we parse
4083          tentatively.  */
4084       cp_parser_parse_tentatively (parser);
4085
4086       /* Look for the optional `template' keyword, if this isn't the
4087          first time through the loop.  */
4088       if (success)
4089         template_keyword_p = cp_parser_optional_template_keyword (parser);
4090       else
4091         template_keyword_p = false;
4092
4093       /* Save the old scope since the name lookup we are about to do
4094          might destroy it.  */
4095       old_scope = parser->scope;
4096       saved_qualifying_scope = parser->qualifying_scope;
4097       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4098          look up names in "X<T>::I" in order to determine that "Y" is
4099          a template.  So, if we have a typename at this point, we make
4100          an effort to look through it.  */
4101       if (is_declaration
4102           && !typename_keyword_p
4103           && parser->scope
4104           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4105         parser->scope = resolve_typename_type (parser->scope,
4106                                                /*only_current_p=*/false);
4107       /* Parse the qualifying entity.  */
4108       new_scope
4109         = cp_parser_qualifying_entity (parser,
4110                                        typename_keyword_p,
4111                                        template_keyword_p,
4112                                        check_dependency_p,
4113                                        type_p,
4114                                        is_declaration);
4115       /* Look for the `::' token.  */
4116       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4117
4118       /* If we found what we wanted, we keep going; otherwise, we're
4119          done.  */
4120       if (!cp_parser_parse_definitely (parser))
4121         {
4122           bool error_p = false;
4123
4124           /* Restore the OLD_SCOPE since it was valid before the
4125              failed attempt at finding the last
4126              class-or-namespace-name.  */
4127           parser->scope = old_scope;
4128           parser->qualifying_scope = saved_qualifying_scope;
4129           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4130             break;
4131           /* If the next token is an identifier, and the one after
4132              that is a `::', then any valid interpretation would have
4133              found a class-or-namespace-name.  */
4134           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4135                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4136                      == CPP_SCOPE)
4137                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4138                      != CPP_COMPL))
4139             {
4140               token = cp_lexer_consume_token (parser->lexer);
4141               if (!error_p)
4142                 {
4143                   if (!token->ambiguous_p)
4144                     {
4145                       tree decl;
4146                       tree ambiguous_decls;
4147
4148                       decl = cp_parser_lookup_name (parser, token->u.value,
4149                                                     none_type,
4150                                                     /*is_template=*/false,
4151                                                     /*is_namespace=*/false,
4152                                                     /*check_dependency=*/true,
4153                                                     &ambiguous_decls,
4154                                                     token->location);
4155                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4156                         error ("%H%qD used without template parameters",
4157                                &token->location, decl);
4158                       else if (ambiguous_decls)
4159                         {
4160                           error ("%Hreference to %qD is ambiguous",
4161                                  &token->location, token->u.value);
4162                           print_candidates (ambiguous_decls);
4163                           decl = error_mark_node;
4164                         }
4165                       else
4166                         {
4167                           const char* msg = "is not a class or namespace";
4168                           if (cxx_dialect != cxx98)
4169                             msg = "is not a class, namespace, or enumeration";
4170                           cp_parser_name_lookup_error
4171                             (parser, token->u.value, decl, msg,
4172                              token->location);
4173                         }
4174                     }
4175                   parser->scope = error_mark_node;
4176                   error_p = true;
4177                   /* Treat this as a successful nested-name-specifier
4178                      due to:
4179
4180                      [basic.lookup.qual]
4181
4182                      If the name found is not a class-name (clause
4183                      _class_) or namespace-name (_namespace.def_), the
4184                      program is ill-formed.  */
4185                   success = true;
4186                 }
4187               cp_lexer_consume_token (parser->lexer);
4188             }
4189           break;
4190         }
4191       /* We've found one valid nested-name-specifier.  */
4192       success = true;
4193       /* Name lookup always gives us a DECL.  */
4194       if (TREE_CODE (new_scope) == TYPE_DECL)
4195         new_scope = TREE_TYPE (new_scope);
4196       /* Uses of "template" must be followed by actual templates.  */
4197       if (template_keyword_p
4198           && !(CLASS_TYPE_P (new_scope)
4199                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4200                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4201                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4202           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4203                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4204                    == TEMPLATE_ID_EXPR)))
4205         permerror (input_location, TYPE_P (new_scope)
4206                    ? "%qT is not a template"
4207                    : "%qD is not a template",
4208                    new_scope);
4209       /* If it is a class scope, try to complete it; we are about to
4210          be looking up names inside the class.  */
4211       if (TYPE_P (new_scope)
4212           /* Since checking types for dependency can be expensive,
4213              avoid doing it if the type is already complete.  */
4214           && !COMPLETE_TYPE_P (new_scope)
4215           /* Do not try to complete dependent types.  */
4216           && !dependent_type_p (new_scope))
4217         {
4218           new_scope = complete_type (new_scope);
4219           /* If it is a typedef to current class, use the current
4220              class instead, as the typedef won't have any names inside
4221              it yet.  */
4222           if (!COMPLETE_TYPE_P (new_scope)
4223               && currently_open_class (new_scope))
4224             new_scope = TYPE_MAIN_VARIANT (new_scope);
4225         }
4226       /* Make sure we look in the right scope the next time through
4227          the loop.  */
4228       parser->scope = new_scope;
4229     }
4230
4231   /* If parsing tentatively, replace the sequence of tokens that makes
4232      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4233      token.  That way, should we re-parse the token stream, we will
4234      not have to repeat the effort required to do the parse, nor will
4235      we issue duplicate error messages.  */
4236   if (success && start)
4237     {
4238       cp_token *token;
4239
4240       token = cp_lexer_token_at (parser->lexer, start);
4241       /* Reset the contents of the START token.  */
4242       token->type = CPP_NESTED_NAME_SPECIFIER;
4243       /* Retrieve any deferred checks.  Do not pop this access checks yet
4244          so the memory will not be reclaimed during token replacing below.  */
4245       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4246       token->u.tree_check_value->value = parser->scope;
4247       token->u.tree_check_value->checks = get_deferred_access_checks ();
4248       token->u.tree_check_value->qualifying_scope =
4249         parser->qualifying_scope;
4250       token->keyword = RID_MAX;
4251
4252       /* Purge all subsequent tokens.  */
4253       cp_lexer_purge_tokens_after (parser->lexer, start);
4254     }
4255
4256   if (start)
4257     pop_to_parent_deferring_access_checks ();
4258
4259   return success ? parser->scope : NULL_TREE;
4260 }
4261
4262 /* Parse a nested-name-specifier.  See
4263    cp_parser_nested_name_specifier_opt for details.  This function
4264    behaves identically, except that it will an issue an error if no
4265    nested-name-specifier is present.  */
4266
4267 static tree
4268 cp_parser_nested_name_specifier (cp_parser *parser,
4269                                  bool typename_keyword_p,
4270                                  bool check_dependency_p,
4271                                  bool type_p,
4272                                  bool is_declaration)
4273 {
4274   tree scope;
4275
4276   /* Look for the nested-name-specifier.  */
4277   scope = cp_parser_nested_name_specifier_opt (parser,
4278                                                typename_keyword_p,
4279                                                check_dependency_p,
4280                                                type_p,
4281                                                is_declaration);
4282   /* If it was not present, issue an error message.  */
4283   if (!scope)
4284     {
4285       cp_parser_error (parser, "expected nested-name-specifier");
4286       parser->scope = NULL_TREE;
4287     }
4288
4289   return scope;
4290 }
4291
4292 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4293    this is either a class-name or a namespace-name (which corresponds
4294    to the class-or-namespace-name production in the grammar). For
4295    C++0x, it can also be a type-name that refers to an enumeration
4296    type.
4297
4298    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4299    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4300    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4301    TYPE_P is TRUE iff the next name should be taken as a class-name,
4302    even the same name is declared to be another entity in the same
4303    scope.
4304
4305    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4306    specified by the class-or-namespace-name.  If neither is found the
4307    ERROR_MARK_NODE is returned.  */
4308
4309 static tree
4310 cp_parser_qualifying_entity (cp_parser *parser,
4311                              bool typename_keyword_p,
4312                              bool template_keyword_p,
4313                              bool check_dependency_p,
4314                              bool type_p,
4315                              bool is_declaration)
4316 {
4317   tree saved_scope;
4318   tree saved_qualifying_scope;
4319   tree saved_object_scope;
4320   tree scope;
4321   bool only_class_p;
4322   bool successful_parse_p;
4323
4324   /* Before we try to parse the class-name, we must save away the
4325      current PARSER->SCOPE since cp_parser_class_name will destroy
4326      it.  */
4327   saved_scope = parser->scope;
4328   saved_qualifying_scope = parser->qualifying_scope;
4329   saved_object_scope = parser->object_scope;
4330   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4331      there is no need to look for a namespace-name.  */
4332   only_class_p = template_keyword_p 
4333     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4334   if (!only_class_p)
4335     cp_parser_parse_tentatively (parser);
4336   scope = cp_parser_class_name (parser,
4337                                 typename_keyword_p,
4338                                 template_keyword_p,
4339                                 type_p ? class_type : none_type,
4340                                 check_dependency_p,
4341                                 /*class_head_p=*/false,
4342                                 is_declaration);
4343   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4344   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4345   if (!only_class_p 
4346       && cxx_dialect != cxx98
4347       && !successful_parse_p)
4348     {
4349       /* Restore the saved scope.  */
4350       parser->scope = saved_scope;
4351       parser->qualifying_scope = saved_qualifying_scope;
4352       parser->object_scope = saved_object_scope;
4353
4354       /* Parse tentatively.  */
4355       cp_parser_parse_tentatively (parser);
4356      
4357       /* Parse a typedef-name or enum-name.  */
4358       scope = cp_parser_nonclass_name (parser);
4359       successful_parse_p = cp_parser_parse_definitely (parser);
4360     }
4361   /* If that didn't work, try for a namespace-name.  */
4362   if (!only_class_p && !successful_parse_p)
4363     {
4364       /* Restore the saved scope.  */
4365       parser->scope = saved_scope;
4366       parser->qualifying_scope = saved_qualifying_scope;
4367       parser->object_scope = saved_object_scope;
4368       /* If we are not looking at an identifier followed by the scope
4369          resolution operator, then this is not part of a
4370          nested-name-specifier.  (Note that this function is only used
4371          to parse the components of a nested-name-specifier.)  */
4372       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4373           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4374         return error_mark_node;
4375       scope = cp_parser_namespace_name (parser);
4376     }
4377
4378   return scope;
4379 }
4380
4381 /* Parse a postfix-expression.
4382
4383    postfix-expression:
4384      primary-expression
4385      postfix-expression [ expression ]
4386      postfix-expression ( expression-list [opt] )
4387      simple-type-specifier ( expression-list [opt] )
4388      typename :: [opt] nested-name-specifier identifier
4389        ( expression-list [opt] )
4390      typename :: [opt] nested-name-specifier template [opt] template-id
4391        ( expression-list [opt] )
4392      postfix-expression . template [opt] id-expression
4393      postfix-expression -> template [opt] id-expression
4394      postfix-expression . pseudo-destructor-name
4395      postfix-expression -> pseudo-destructor-name
4396      postfix-expression ++
4397      postfix-expression --
4398      dynamic_cast < type-id > ( expression )
4399      static_cast < type-id > ( expression )
4400      reinterpret_cast < type-id > ( expression )
4401      const_cast < type-id > ( expression )
4402      typeid ( expression )
4403      typeid ( type-id )
4404
4405    GNU Extension:
4406
4407    postfix-expression:
4408      ( type-id ) { initializer-list , [opt] }
4409
4410    This extension is a GNU version of the C99 compound-literal
4411    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4412    but they are essentially the same concept.)
4413
4414    If ADDRESS_P is true, the postfix expression is the operand of the
4415    `&' operator.  CAST_P is true if this expression is the target of a
4416    cast.
4417
4418    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4419    class member access expressions [expr.ref].
4420
4421    Returns a representation of the expression.  */
4422
4423 static tree
4424 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4425                               bool member_access_only_p,
4426                               cp_id_kind * pidk_return)
4427 {
4428   cp_token *token;
4429   enum rid keyword;
4430   cp_id_kind idk = CP_ID_KIND_NONE;
4431   tree postfix_expression = NULL_TREE;
4432   bool is_member_access = false;
4433
4434   /* Peek at the next token.  */
4435   token = cp_lexer_peek_token (parser->lexer);
4436   /* Some of the productions are determined by keywords.  */
4437   keyword = token->keyword;
4438   switch (keyword)
4439     {
4440     case RID_DYNCAST:
4441     case RID_STATCAST:
4442     case RID_REINTCAST:
4443     case RID_CONSTCAST:
4444       {
4445         tree type;
4446         tree expression;
4447         const char *saved_message;
4448
4449         /* All of these can be handled in the same way from the point
4450            of view of parsing.  Begin by consuming the token
4451            identifying the cast.  */
4452         cp_lexer_consume_token (parser->lexer);
4453
4454         /* New types cannot be defined in the cast.  */
4455         saved_message = parser->type_definition_forbidden_message;
4456         parser->type_definition_forbidden_message
4457           = "types may not be defined in casts";
4458
4459         /* Look for the opening `<'.  */
4460         cp_parser_require (parser, CPP_LESS, "%<<%>");
4461         /* Parse the type to which we are casting.  */
4462         type = cp_parser_type_id (parser);
4463         /* Look for the closing `>'.  */
4464         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4465         /* Restore the old message.  */
4466         parser->type_definition_forbidden_message = saved_message;
4467
4468         /* And the expression which is being cast.  */
4469         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4471         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4472
4473         /* Only type conversions to integral or enumeration types
4474            can be used in constant-expressions.  */
4475         if (!cast_valid_in_integral_constant_expression_p (type)
4476             && (cp_parser_non_integral_constant_expression
4477                 (parser,
4478                  "a cast to a type other than an integral or "
4479                  "enumeration type")))
4480           return error_mark_node;
4481
4482         switch (keyword)
4483           {
4484           case RID_DYNCAST:
4485             postfix_expression
4486               = build_dynamic_cast (type, expression, tf_warning_or_error);
4487             break;
4488           case RID_STATCAST:
4489             postfix_expression
4490               = build_static_cast (type, expression, tf_warning_or_error);
4491             break;
4492           case RID_REINTCAST:
4493             postfix_expression
4494               = build_reinterpret_cast (type, expression, 
4495                                         tf_warning_or_error);
4496             break;
4497           case RID_CONSTCAST:
4498             postfix_expression
4499               = build_const_cast (type, expression, tf_warning_or_error);
4500             break;
4501           default:
4502             gcc_unreachable ();
4503           }
4504       }
4505       break;
4506
4507     case RID_TYPEID:
4508       {
4509         tree type;
4510         const char *saved_message;
4511         bool saved_in_type_id_in_expr_p;
4512
4513         /* Consume the `typeid' token.  */
4514         cp_lexer_consume_token (parser->lexer);
4515         /* Look for the `(' token.  */
4516         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4517         /* Types cannot be defined in a `typeid' expression.  */
4518         saved_message = parser->type_definition_forbidden_message;
4519         parser->type_definition_forbidden_message
4520           = "types may not be defined in a %<typeid%> expression";
4521         /* We can't be sure yet whether we're looking at a type-id or an
4522            expression.  */
4523         cp_parser_parse_tentatively (parser);
4524         /* Try a type-id first.  */
4525         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4526         parser->in_type_id_in_expr_p = true;
4527         type = cp_parser_type_id (parser);
4528         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4529         /* Look for the `)' token.  Otherwise, we can't be sure that
4530            we're not looking at an expression: consider `typeid (int
4531            (3))', for example.  */
4532         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4533         /* If all went well, simply lookup the type-id.  */
4534         if (cp_parser_parse_definitely (parser))
4535           postfix_expression = get_typeid (type);
4536         /* Otherwise, fall back to the expression variant.  */
4537         else
4538           {
4539             tree expression;
4540
4541             /* Look for an expression.  */
4542             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4543             /* Compute its typeid.  */
4544             postfix_expression = build_typeid (expression);
4545             /* Look for the `)' token.  */
4546             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4547           }
4548         /* Restore the saved message.  */
4549         parser->type_definition_forbidden_message = saved_message;
4550         /* `typeid' may not appear in an integral constant expression.  */
4551         if (cp_parser_non_integral_constant_expression(parser,
4552                                                        "%<typeid%> operator"))
4553           return error_mark_node;
4554       }
4555       break;
4556
4557     case RID_TYPENAME:
4558       {
4559         tree type;
4560         /* The syntax permitted here is the same permitted for an
4561            elaborated-type-specifier.  */
4562         type = cp_parser_elaborated_type_specifier (parser,
4563                                                     /*is_friend=*/false,
4564                                                     /*is_declaration=*/false);
4565         postfix_expression = cp_parser_functional_cast (parser, type);
4566       }
4567       break;
4568
4569     default:
4570       {
4571         tree type;
4572
4573         /* If the next thing is a simple-type-specifier, we may be
4574            looking at a functional cast.  We could also be looking at
4575            an id-expression.  So, we try the functional cast, and if
4576            that doesn't work we fall back to the primary-expression.  */
4577         cp_parser_parse_tentatively (parser);
4578         /* Look for the simple-type-specifier.  */
4579         type = cp_parser_simple_type_specifier (parser,
4580                                                 /*decl_specs=*/NULL,
4581                                                 CP_PARSER_FLAGS_NONE);
4582         /* Parse the cast itself.  */
4583         if (!cp_parser_error_occurred (parser))
4584           postfix_expression
4585             = cp_parser_functional_cast (parser, type);
4586         /* If that worked, we're done.  */
4587         if (cp_parser_parse_definitely (parser))
4588           break;
4589
4590         /* If the functional-cast didn't work out, try a
4591            compound-literal.  */
4592         if (cp_parser_allow_gnu_extensions_p (parser)
4593             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4594           {
4595             VEC(constructor_elt,gc) *initializer_list = NULL;
4596             bool saved_in_type_id_in_expr_p;
4597
4598             cp_parser_parse_tentatively (parser);
4599             /* Consume the `('.  */
4600             cp_lexer_consume_token (parser->lexer);
4601             /* Parse the type.  */
4602             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4603             parser->in_type_id_in_expr_p = true;
4604             type = cp_parser_type_id (parser);
4605             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4606             /* Look for the `)'.  */
4607             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4608             /* Look for the `{'.  */
4609             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4610             /* If things aren't going well, there's no need to
4611                keep going.  */
4612             if (!cp_parser_error_occurred (parser))
4613               {
4614                 bool non_constant_p;
4615                 /* Parse the initializer-list.  */
4616                 initializer_list
4617                   = cp_parser_initializer_list (parser, &non_constant_p);
4618                 /* Allow a trailing `,'.  */
4619                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4620                   cp_lexer_consume_token (parser->lexer);
4621                 /* Look for the final `}'.  */
4622                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4623               }
4624             /* If that worked, we're definitely looking at a
4625                compound-literal expression.  */
4626             if (cp_parser_parse_definitely (parser))
4627               {
4628                 /* Warn the user that a compound literal is not
4629                    allowed in standard C++.  */
4630                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4631                 /* For simplicity, we disallow compound literals in
4632                    constant-expressions.  We could
4633                    allow compound literals of integer type, whose
4634                    initializer was a constant, in constant
4635                    expressions.  Permitting that usage, as a further
4636                    extension, would not change the meaning of any
4637                    currently accepted programs.  (Of course, as
4638                    compound literals are not part of ISO C++, the
4639                    standard has nothing to say.)  */
4640                 if (cp_parser_non_integral_constant_expression 
4641                     (parser, "non-constant compound literals"))
4642                   {
4643                     postfix_expression = error_mark_node;
4644                     break;
4645                   }
4646                 /* Form the representation of the compound-literal.  */
4647                 postfix_expression
4648                   = (finish_compound_literal
4649                      (type, build_constructor (init_list_type_node,
4650                                                initializer_list)));
4651                 break;
4652               }
4653           }
4654
4655         /* It must be a primary-expression.  */
4656         postfix_expression
4657           = cp_parser_primary_expression (parser, address_p, cast_p,
4658                                           /*template_arg_p=*/false,
4659                                           &idk);
4660       }
4661       break;
4662     }
4663
4664   /* Keep looping until the postfix-expression is complete.  */
4665   while (true)
4666     {
4667       if (idk == CP_ID_KIND_UNQUALIFIED
4668           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4669           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4670         /* It is not a Koenig lookup function call.  */
4671         postfix_expression
4672           = unqualified_name_lookup_error (postfix_expression);
4673
4674       /* Peek at the next token.  */
4675       token = cp_lexer_peek_token (parser->lexer);
4676
4677       switch (token->type)
4678         {
4679         case CPP_OPEN_SQUARE:
4680           postfix_expression
4681             = cp_parser_postfix_open_square_expression (parser,
4682                                                         postfix_expression,
4683                                                         false);
4684           idk = CP_ID_KIND_NONE;
4685           is_member_access = false;
4686           break;
4687
4688         case CPP_OPEN_PAREN:
4689           /* postfix-expression ( expression-list [opt] ) */
4690           {
4691             bool koenig_p;
4692             bool is_builtin_constant_p;
4693             bool saved_integral_constant_expression_p = false;
4694             bool saved_non_integral_constant_expression_p = false;
4695             tree args;
4696
4697             is_member_access = false;
4698
4699             is_builtin_constant_p
4700               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4701             if (is_builtin_constant_p)
4702               {
4703                 /* The whole point of __builtin_constant_p is to allow
4704                    non-constant expressions to appear as arguments.  */
4705                 saved_integral_constant_expression_p
4706                   = parser->integral_constant_expression_p;
4707                 saved_non_integral_constant_expression_p
4708                   = parser->non_integral_constant_expression_p;
4709                 parser->integral_constant_expression_p = false;
4710               }
4711             args = (cp_parser_parenthesized_expression_list
4712                     (parser, /*is_attribute_list=*/false,
4713                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4714                      /*non_constant_p=*/NULL));
4715             if (is_builtin_constant_p)
4716               {
4717                 parser->integral_constant_expression_p
4718                   = saved_integral_constant_expression_p;
4719                 parser->non_integral_constant_expression_p
4720                   = saved_non_integral_constant_expression_p;
4721               }
4722
4723             if (args == error_mark_node)
4724               {
4725                 postfix_expression = error_mark_node;
4726                 break;
4727               }
4728
4729             /* Function calls are not permitted in
4730                constant-expressions.  */
4731             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4732                 && cp_parser_non_integral_constant_expression (parser,
4733                                                                "a function call"))
4734               {
4735                 postfix_expression = error_mark_node;
4736                 break;
4737               }
4738
4739             koenig_p = false;
4740             if (idk == CP_ID_KIND_UNQUALIFIED
4741                 || idk == CP_ID_KIND_TEMPLATE_ID)
4742               {
4743                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4744                   {
4745                     if (args)
4746                       {
4747                         koenig_p = true;
4748                         if (!any_type_dependent_arguments_p (args))
4749                           postfix_expression
4750                             = perform_koenig_lookup (postfix_expression, args);
4751                       }
4752                     else
4753                       postfix_expression
4754                         = unqualified_fn_lookup_error (postfix_expression);
4755                   }
4756                 /* We do not perform argument-dependent lookup if
4757                    normal lookup finds a non-function, in accordance
4758                    with the expected resolution of DR 218.  */
4759                 else if (args && is_overloaded_fn (postfix_expression))
4760                   {
4761                     tree fn = get_first_fn (postfix_expression);
4762
4763                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4764                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4765
4766                     /* Only do argument dependent lookup if regular
4767                        lookup does not find a set of member functions.
4768                        [basic.lookup.koenig]/2a  */
4769                     if (!DECL_FUNCTION_MEMBER_P (fn))
4770                       {
4771                         koenig_p = true;
4772                         if (!any_type_dependent_arguments_p (args))
4773                           postfix_expression
4774                             = perform_koenig_lookup (postfix_expression, args);
4775                       }
4776                   }
4777               }
4778
4779             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4780               {
4781                 tree instance = TREE_OPERAND (postfix_expression, 0);
4782                 tree fn = TREE_OPERAND (postfix_expression, 1);
4783
4784                 if (processing_template_decl
4785                     && (type_dependent_expression_p (instance)
4786                         || (!BASELINK_P (fn)
4787                             && TREE_CODE (fn) != FIELD_DECL)
4788                         || type_dependent_expression_p (fn)
4789                         || any_type_dependent_arguments_p (args)))
4790                   {
4791                     postfix_expression
4792                       = build_nt_call_list (postfix_expression, args);
4793                     break;
4794                   }
4795
4796                 if (BASELINK_P (fn))
4797                   {
4798                   postfix_expression
4799                     = (build_new_method_call
4800                        (instance, fn, args, NULL_TREE,
4801                         (idk == CP_ID_KIND_QUALIFIED
4802                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4803                         /*fn_p=*/NULL,
4804                         tf_warning_or_error));
4805                   }
4806                 else
4807                   postfix_expression
4808                     = finish_call_expr (postfix_expression, args,
4809                                         /*disallow_virtual=*/false,
4810                                         /*koenig_p=*/false,
4811                                         tf_warning_or_error);
4812               }
4813             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4814                      || TREE_CODE (postfix_expression) == MEMBER_REF
4815                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4816               postfix_expression = (build_offset_ref_call_from_tree
4817                                     (postfix_expression, args));
4818             else if (idk == CP_ID_KIND_QUALIFIED)
4819               /* A call to a static class member, or a namespace-scope
4820                  function.  */
4821               postfix_expression
4822                 = finish_call_expr (postfix_expression, args,
4823                                     /*disallow_virtual=*/true,
4824                                     koenig_p,
4825                                     tf_warning_or_error);
4826             else
4827               /* All other function calls.  */
4828               postfix_expression
4829                 = finish_call_expr (postfix_expression, args,
4830                                     /*disallow_virtual=*/false,
4831                                     koenig_p,
4832                                     tf_warning_or_error);
4833
4834             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4835             idk = CP_ID_KIND_NONE;
4836           }
4837           break;
4838
4839         case CPP_DOT:
4840         case CPP_DEREF:
4841           /* postfix-expression . template [opt] id-expression
4842              postfix-expression . pseudo-destructor-name
4843              postfix-expression -> template [opt] id-expression
4844              postfix-expression -> pseudo-destructor-name */
4845
4846           /* Consume the `.' or `->' operator.  */
4847           cp_lexer_consume_token (parser->lexer);
4848
4849           postfix_expression
4850             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4851                                                       postfix_expression,
4852                                                       false, &idk,
4853                                                       token->location);
4854
4855           is_member_access = true;
4856           break;
4857
4858         case CPP_PLUS_PLUS:
4859           /* postfix-expression ++  */
4860           /* Consume the `++' token.  */
4861           cp_lexer_consume_token (parser->lexer);
4862           /* Generate a representation for the complete expression.  */
4863           postfix_expression
4864             = finish_increment_expr (postfix_expression,
4865                                      POSTINCREMENT_EXPR);
4866           /* Increments may not appear in constant-expressions.  */
4867           if (cp_parser_non_integral_constant_expression (parser,
4868                                                           "an increment"))
4869             postfix_expression = error_mark_node;
4870           idk = CP_ID_KIND_NONE;
4871           is_member_access = false;
4872           break;
4873
4874         case CPP_MINUS_MINUS:
4875           /* postfix-expression -- */
4876           /* Consume the `--' token.  */
4877           cp_lexer_consume_token (parser->lexer);
4878           /* Generate a representation for the complete expression.  */
4879           postfix_expression
4880             = finish_increment_expr (postfix_expression,
4881                                      POSTDECREMENT_EXPR);
4882           /* Decrements may not appear in constant-expressions.  */
4883           if (cp_parser_non_integral_constant_expression (parser,
4884                                                           "a decrement"))
4885             postfix_expression = error_mark_node;
4886           idk = CP_ID_KIND_NONE;
4887           is_member_access = false;
4888           break;
4889
4890         default:
4891           if (pidk_return != NULL)
4892             * pidk_return = idk;
4893           if (member_access_only_p)
4894             return is_member_access? postfix_expression : error_mark_node;
4895           else
4896             return postfix_expression;
4897         }
4898     }
4899
4900   /* We should never get here.  */
4901   gcc_unreachable ();
4902   return error_mark_node;
4903 }
4904
4905 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4906    by cp_parser_builtin_offsetof.  We're looking for
4907
4908      postfix-expression [ expression ]
4909
4910    FOR_OFFSETOF is set if we're being called in that context, which
4911    changes how we deal with integer constant expressions.  */
4912
4913 static tree
4914 cp_parser_postfix_open_square_expression (cp_parser *parser,
4915                                           tree postfix_expression,
4916                                           bool for_offsetof)
4917 {
4918   tree index;
4919
4920   /* Consume the `[' token.  */
4921   cp_lexer_consume_token (parser->lexer);
4922
4923   /* Parse the index expression.  */
4924   /* ??? For offsetof, there is a question of what to allow here.  If
4925      offsetof is not being used in an integral constant expression context,
4926      then we *could* get the right answer by computing the value at runtime.
4927      If we are in an integral constant expression context, then we might
4928      could accept any constant expression; hard to say without analysis.
4929      Rather than open the barn door too wide right away, allow only integer
4930      constant expressions here.  */
4931   if (for_offsetof)
4932     index = cp_parser_constant_expression (parser, false, NULL);
4933   else
4934     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4935
4936   /* Look for the closing `]'.  */
4937   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4938
4939   /* Build the ARRAY_REF.  */
4940   postfix_expression = grok_array_decl (postfix_expression, index);
4941
4942   /* When not doing offsetof, array references are not permitted in
4943      constant-expressions.  */
4944   if (!for_offsetof
4945       && (cp_parser_non_integral_constant_expression
4946           (parser, "an array reference")))
4947     postfix_expression = error_mark_node;
4948
4949   return postfix_expression;
4950 }
4951
4952 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4953    by cp_parser_builtin_offsetof.  We're looking for
4954
4955      postfix-expression . template [opt] id-expression
4956      postfix-expression . pseudo-destructor-name
4957      postfix-expression -> template [opt] id-expression
4958      postfix-expression -> pseudo-destructor-name
4959
4960    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4961    limits what of the above we'll actually accept, but nevermind.
4962    TOKEN_TYPE is the "." or "->" token, which will already have been
4963    removed from the stream.  */
4964
4965 static tree
4966 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4967                                         enum cpp_ttype token_type,
4968                                         tree postfix_expression,
4969                                         bool for_offsetof, cp_id_kind *idk,
4970                                         location_t location)
4971 {
4972   tree name;
4973   bool dependent_p;
4974   bool pseudo_destructor_p;
4975   tree scope = NULL_TREE;
4976
4977   /* If this is a `->' operator, dereference the pointer.  */
4978   if (token_type == CPP_DEREF)
4979     postfix_expression = build_x_arrow (postfix_expression);
4980   /* Check to see whether or not the expression is type-dependent.  */
4981   dependent_p = type_dependent_expression_p (postfix_expression);
4982   /* The identifier following the `->' or `.' is not qualified.  */
4983   parser->scope = NULL_TREE;
4984   parser->qualifying_scope = NULL_TREE;
4985   parser->object_scope = NULL_TREE;
4986   *idk = CP_ID_KIND_NONE;
4987
4988   /* Enter the scope corresponding to the type of the object
4989      given by the POSTFIX_EXPRESSION.  */
4990   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4991     {
4992       scope = TREE_TYPE (postfix_expression);
4993       /* According to the standard, no expression should ever have
4994          reference type.  Unfortunately, we do not currently match
4995          the standard in this respect in that our internal representation
4996          of an expression may have reference type even when the standard
4997          says it does not.  Therefore, we have to manually obtain the
4998          underlying type here.  */
4999       scope = non_reference (scope);
5000       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5001       if (scope == unknown_type_node)
5002         {
5003           error ("%H%qE does not have class type", &location, postfix_expression);
5004           scope = NULL_TREE;
5005         }
5006       else
5007         scope = complete_type_or_else (scope, NULL_TREE);
5008       /* Let the name lookup machinery know that we are processing a
5009          class member access expression.  */
5010       parser->context->object_type = scope;
5011       /* If something went wrong, we want to be able to discern that case,
5012          as opposed to the case where there was no SCOPE due to the type
5013          of expression being dependent.  */
5014       if (!scope)
5015         scope = error_mark_node;
5016       /* If the SCOPE was erroneous, make the various semantic analysis
5017          functions exit quickly -- and without issuing additional error
5018          messages.  */
5019       if (scope == error_mark_node)
5020         postfix_expression = error_mark_node;
5021     }
5022
5023   /* Assume this expression is not a pseudo-destructor access.  */
5024   pseudo_destructor_p = false;
5025
5026   /* If the SCOPE is a scalar type, then, if this is a valid program,
5027      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5028      is type dependent, it can be pseudo-destructor-name or something else.
5029      Try to parse it as pseudo-destructor-name first.  */
5030   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5031     {
5032       tree s;
5033       tree type;
5034
5035       cp_parser_parse_tentatively (parser);
5036       /* Parse the pseudo-destructor-name.  */
5037       s = NULL_TREE;
5038       cp_parser_pseudo_destructor_name (parser, &s, &type);
5039       if (dependent_p
5040           && (cp_parser_error_occurred (parser)
5041               || TREE_CODE (type) != TYPE_DECL
5042               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5043         cp_parser_abort_tentative_parse (parser);
5044       else if (cp_parser_parse_definitely (parser))
5045         {
5046           pseudo_destructor_p = true;
5047           postfix_expression
5048             = finish_pseudo_destructor_expr (postfix_expression,
5049                                              s, TREE_TYPE (type));
5050         }
5051     }
5052
5053   if (!pseudo_destructor_p)
5054     {
5055       /* If the SCOPE is not a scalar type, we are looking at an
5056          ordinary class member access expression, rather than a
5057          pseudo-destructor-name.  */
5058       bool template_p;
5059       cp_token *token = cp_lexer_peek_token (parser->lexer);
5060       /* Parse the id-expression.  */
5061       name = (cp_parser_id_expression
5062               (parser,
5063                cp_parser_optional_template_keyword (parser),
5064                /*check_dependency_p=*/true,
5065                &template_p,
5066                /*declarator_p=*/false,
5067                /*optional_p=*/false));
5068       /* In general, build a SCOPE_REF if the member name is qualified.
5069          However, if the name was not dependent and has already been
5070          resolved; there is no need to build the SCOPE_REF.  For example;
5071
5072              struct X { void f(); };
5073              template <typename T> void f(T* t) { t->X::f(); }
5074
5075          Even though "t" is dependent, "X::f" is not and has been resolved
5076          to a BASELINK; there is no need to include scope information.  */
5077
5078       /* But we do need to remember that there was an explicit scope for
5079          virtual function calls.  */
5080       if (parser->scope)
5081         *idk = CP_ID_KIND_QUALIFIED;
5082
5083       /* If the name is a template-id that names a type, we will get a
5084          TYPE_DECL here.  That is invalid code.  */
5085       if (TREE_CODE (name) == TYPE_DECL)
5086         {
5087           error ("%Hinvalid use of %qD", &token->location, name);
5088           postfix_expression = error_mark_node;
5089         }
5090       else
5091         {
5092           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5093             {
5094               name = build_qualified_name (/*type=*/NULL_TREE,
5095                                            parser->scope,
5096                                            name,
5097                                            template_p);
5098               parser->scope = NULL_TREE;
5099               parser->qualifying_scope = NULL_TREE;
5100               parser->object_scope = NULL_TREE;
5101             }
5102           if (scope && name && BASELINK_P (name))
5103             adjust_result_of_qualified_name_lookup
5104               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5105           postfix_expression
5106             = finish_class_member_access_expr (postfix_expression, name,
5107                                                template_p, 
5108                                                tf_warning_or_error);
5109         }
5110     }
5111
5112   /* We no longer need to look up names in the scope of the object on
5113      the left-hand side of the `.' or `->' operator.  */
5114   parser->context->object_type = NULL_TREE;
5115
5116   /* Outside of offsetof, these operators may not appear in
5117      constant-expressions.  */
5118   if (!for_offsetof
5119       && (cp_parser_non_integral_constant_expression
5120           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5121     postfix_expression = error_mark_node;
5122
5123   return postfix_expression;
5124 }
5125
5126 /* Parse a parenthesized expression-list.
5127
5128    expression-list:
5129      assignment-expression
5130      expression-list, assignment-expression
5131
5132    attribute-list:
5133      expression-list
5134      identifier
5135      identifier, expression-list
5136
5137    CAST_P is true if this expression is the target of a cast.
5138
5139    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5140    argument pack.
5141
5142    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5143    representation of an assignment-expression.  Note that a TREE_LIST
5144    is returned even if there is only a single expression in the list.
5145    error_mark_node is returned if the ( and or ) are
5146    missing. NULL_TREE is returned on no expressions. The parentheses
5147    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5148    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5149    indicates whether or not all of the expressions in the list were
5150    constant.  */
5151
5152 static tree
5153 cp_parser_parenthesized_expression_list (cp_parser* parser,
5154                                          bool is_attribute_list,
5155                                          bool cast_p,
5156                                          bool allow_expansion_p,
5157                                          bool *non_constant_p)
5158 {
5159   tree expression_list = NULL_TREE;
5160   bool fold_expr_p = is_attribute_list;
5161   tree identifier = NULL_TREE;
5162   bool saved_greater_than_is_operator_p;
5163
5164   /* Assume all the expressions will be constant.  */
5165   if (non_constant_p)
5166     *non_constant_p = false;
5167
5168   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5169     return error_mark_node;
5170
5171   /* Within a parenthesized expression, a `>' token is always
5172      the greater-than operator.  */
5173   saved_greater_than_is_operator_p
5174     = parser->greater_than_is_operator_p;
5175   parser->greater_than_is_operator_p = true;
5176
5177   /* Consume expressions until there are no more.  */
5178   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5179     while (true)
5180       {
5181         tree expr;
5182
5183         /* At the beginning of attribute lists, check to see if the
5184            next token is an identifier.  */
5185         if (is_attribute_list
5186             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5187           {
5188             cp_token *token;
5189
5190             /* Consume the identifier.  */
5191             token = cp_lexer_consume_token (parser->lexer);
5192             /* Save the identifier.  */
5193             identifier = token->u.value;
5194           }
5195         else
5196           {
5197             bool expr_non_constant_p;
5198
5199             /* Parse the next assignment-expression.  */
5200             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5201               {
5202                 /* A braced-init-list.  */
5203                 maybe_warn_cpp0x ("extended initializer lists");
5204                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5205                 if (non_constant_p && expr_non_constant_p)
5206                   *non_constant_p = true;
5207               }
5208             else if (non_constant_p)
5209               {
5210                 expr = (cp_parser_constant_expression
5211                         (parser, /*allow_non_constant_p=*/true,
5212                          &expr_non_constant_p));
5213                 if (expr_non_constant_p)
5214                   *non_constant_p = true;
5215               }
5216             else
5217               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5218
5219             if (fold_expr_p)
5220               expr = fold_non_dependent_expr (expr);
5221
5222             /* If we have an ellipsis, then this is an expression
5223                expansion.  */
5224             if (allow_expansion_p
5225                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5226               {
5227                 /* Consume the `...'.  */
5228                 cp_lexer_consume_token (parser->lexer);
5229
5230                 /* Build the argument pack.  */
5231                 expr = make_pack_expansion (expr);
5232               }
5233
5234              /* Add it to the list.  We add error_mark_node
5235                 expressions to the list, so that we can still tell if
5236                 the correct form for a parenthesized expression-list
5237                 is found. That gives better errors.  */
5238             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5239
5240             if (expr == error_mark_node)
5241               goto skip_comma;
5242           }
5243
5244         /* After the first item, attribute lists look the same as
5245            expression lists.  */
5246         is_attribute_list = false;
5247
5248       get_comma:;
5249         /* If the next token isn't a `,', then we are done.  */
5250         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5251           break;
5252
5253         /* Otherwise, consume the `,' and keep going.  */
5254         cp_lexer_consume_token (parser->lexer);
5255       }
5256
5257   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5258     {
5259       int ending;
5260
5261     skip_comma:;
5262       /* We try and resync to an unnested comma, as that will give the
5263          user better diagnostics.  */
5264       ending = cp_parser_skip_to_closing_parenthesis (parser,
5265                                                       /*recovering=*/true,
5266                                                       /*or_comma=*/true,
5267                                                       /*consume_paren=*/true);
5268       if (ending < 0)
5269         goto get_comma;
5270       if (!ending)
5271         {
5272           parser->greater_than_is_operator_p
5273             = saved_greater_than_is_operator_p;
5274           return error_mark_node;
5275         }
5276     }
5277
5278   parser->greater_than_is_operator_p
5279     = saved_greater_than_is_operator_p;
5280
5281   /* We built up the list in reverse order so we must reverse it now.  */
5282   expression_list = nreverse (expression_list);
5283   if (identifier)
5284     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5285
5286   return expression_list;
5287 }
5288
5289 /* Parse a pseudo-destructor-name.
5290
5291    pseudo-destructor-name:
5292      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5293      :: [opt] nested-name-specifier template template-id :: ~ type-name
5294      :: [opt] nested-name-specifier [opt] ~ type-name
5295
5296    If either of the first two productions is used, sets *SCOPE to the
5297    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5298    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5299    or ERROR_MARK_NODE if the parse fails.  */
5300
5301 static void
5302 cp_parser_pseudo_destructor_name (cp_parser* parser,
5303                                   tree* scope,
5304                                   tree* type)
5305 {
5306   bool nested_name_specifier_p;
5307
5308   /* Assume that things will not work out.  */
5309   *type = error_mark_node;
5310
5311   /* Look for the optional `::' operator.  */
5312   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5313   /* Look for the optional nested-name-specifier.  */
5314   nested_name_specifier_p
5315     = (cp_parser_nested_name_specifier_opt (parser,
5316                                             /*typename_keyword_p=*/false,
5317                                             /*check_dependency_p=*/true,
5318                                             /*type_p=*/false,
5319                                             /*is_declaration=*/false)
5320        != NULL_TREE);
5321   /* Now, if we saw a nested-name-specifier, we might be doing the
5322      second production.  */
5323   if (nested_name_specifier_p
5324       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5325     {
5326       /* Consume the `template' keyword.  */
5327       cp_lexer_consume_token (parser->lexer);
5328       /* Parse the template-id.  */
5329       cp_parser_template_id (parser,
5330                              /*template_keyword_p=*/true,
5331                              /*check_dependency_p=*/false,
5332                              /*is_declaration=*/true);
5333       /* Look for the `::' token.  */
5334       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5335     }
5336   /* If the next token is not a `~', then there might be some
5337      additional qualification.  */
5338   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5339     {
5340       /* At this point, we're looking for "type-name :: ~".  The type-name
5341          must not be a class-name, since this is a pseudo-destructor.  So,
5342          it must be either an enum-name, or a typedef-name -- both of which
5343          are just identifiers.  So, we peek ahead to check that the "::"
5344          and "~" tokens are present; if they are not, then we can avoid
5345          calling type_name.  */
5346       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5347           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5348           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5349         {
5350           cp_parser_error (parser, "non-scalar type");
5351           return;
5352         }
5353
5354       /* Look for the type-name.  */
5355       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5356       if (*scope == error_mark_node)
5357         return;
5358
5359       /* Look for the `::' token.  */
5360       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5361     }
5362   else
5363     *scope = NULL_TREE;
5364
5365   /* Look for the `~'.  */
5366   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5367   /* Look for the type-name again.  We are not responsible for
5368      checking that it matches the first type-name.  */
5369   *type = cp_parser_nonclass_name (parser);
5370 }
5371
5372 /* Parse a unary-expression.
5373
5374    unary-expression:
5375      postfix-expression
5376      ++ cast-expression
5377      -- cast-expression
5378      unary-operator cast-expression
5379      sizeof unary-expression
5380      sizeof ( type-id )
5381      new-expression
5382      delete-expression
5383
5384    GNU Extensions:
5385
5386    unary-expression:
5387      __extension__ cast-expression
5388      __alignof__ unary-expression
5389      __alignof__ ( type-id )
5390      __real__ cast-expression
5391      __imag__ cast-expression
5392      && identifier
5393
5394    ADDRESS_P is true iff the unary-expression is appearing as the
5395    operand of the `&' operator.   CAST_P is true if this expression is
5396    the target of a cast.
5397
5398    Returns a representation of the expression.  */
5399
5400 static tree
5401 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5402                             cp_id_kind * pidk)
5403 {
5404   cp_token *token;
5405   enum tree_code unary_operator;
5406
5407   /* Peek at the next token.  */
5408   token = cp_lexer_peek_token (parser->lexer);
5409   /* Some keywords give away the kind of expression.  */
5410   if (token->type == CPP_KEYWORD)
5411     {
5412       enum rid keyword = token->keyword;
5413
5414       switch (keyword)
5415         {
5416         case RID_ALIGNOF:
5417         case RID_SIZEOF:
5418           {
5419             tree operand;
5420             enum tree_code op;
5421
5422             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5423             /* Consume the token.  */
5424             cp_lexer_consume_token (parser->lexer);
5425             /* Parse the operand.  */
5426             operand = cp_parser_sizeof_operand (parser, keyword);
5427
5428             if (TYPE_P (operand))
5429               return cxx_sizeof_or_alignof_type (operand, op, true);
5430             else
5431               return cxx_sizeof_or_alignof_expr (operand, op, true);
5432           }
5433
5434         case RID_NEW:
5435           return cp_parser_new_expression (parser);
5436
5437         case RID_DELETE:
5438           return cp_parser_delete_expression (parser);
5439
5440         case RID_EXTENSION:
5441           {
5442             /* The saved value of the PEDANTIC flag.  */
5443             int saved_pedantic;
5444             tree expr;
5445
5446             /* Save away the PEDANTIC flag.  */
5447             cp_parser_extension_opt (parser, &saved_pedantic);
5448             /* Parse the cast-expression.  */
5449             expr = cp_parser_simple_cast_expression (parser);
5450             /* Restore the PEDANTIC flag.  */
5451             pedantic = saved_pedantic;
5452
5453             return expr;
5454           }
5455
5456         case RID_REALPART:
5457         case RID_IMAGPART:
5458           {
5459             tree expression;
5460
5461             /* Consume the `__real__' or `__imag__' token.  */
5462             cp_lexer_consume_token (parser->lexer);
5463             /* Parse the cast-expression.  */
5464             expression = cp_parser_simple_cast_expression (parser);
5465             /* Create the complete representation.  */
5466             return build_x_unary_op ((keyword == RID_REALPART
5467                                       ? REALPART_EXPR : IMAGPART_EXPR),
5468                                      expression,
5469                                      tf_warning_or_error);
5470           }
5471           break;
5472
5473         default:
5474           break;
5475         }
5476     }
5477
5478   /* Look for the `:: new' and `:: delete', which also signal the
5479      beginning of a new-expression, or delete-expression,
5480      respectively.  If the next token is `::', then it might be one of
5481      these.  */
5482   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5483     {
5484       enum rid keyword;
5485
5486       /* See if the token after the `::' is one of the keywords in
5487          which we're interested.  */
5488       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5489       /* If it's `new', we have a new-expression.  */
5490       if (keyword == RID_NEW)
5491         return cp_parser_new_expression (parser);
5492       /* Similarly, for `delete'.  */
5493       else if (keyword == RID_DELETE)
5494         return cp_parser_delete_expression (parser);
5495     }
5496
5497   /* Look for a unary operator.  */
5498   unary_operator = cp_parser_unary_operator (token);
5499   /* The `++' and `--' operators can be handled similarly, even though
5500      they are not technically unary-operators in the grammar.  */
5501   if (unary_operator == ERROR_MARK)
5502     {
5503       if (token->type == CPP_PLUS_PLUS)
5504         unary_operator = PREINCREMENT_EXPR;
5505       else if (token->type == CPP_MINUS_MINUS)
5506         unary_operator = PREDECREMENT_EXPR;
5507       /* Handle the GNU address-of-label extension.  */
5508       else if (cp_parser_allow_gnu_extensions_p (parser)
5509                && token->type == CPP_AND_AND)
5510         {
5511           tree identifier;
5512           tree expression;
5513           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5514
5515           /* Consume the '&&' token.  */
5516           cp_lexer_consume_token (parser->lexer);
5517           /* Look for the identifier.  */
5518           identifier = cp_parser_identifier (parser);
5519           /* Create an expression representing the address.  */
5520           expression = finish_label_address_expr (identifier, loc);
5521           if (cp_parser_non_integral_constant_expression (parser,
5522                                                 "the address of a label"))
5523             expression = error_mark_node;
5524           return expression;
5525         }
5526     }
5527   if (unary_operator != ERROR_MARK)
5528     {
5529       tree cast_expression;
5530       tree expression = error_mark_node;
5531       const char *non_constant_p = NULL;
5532
5533       /* Consume the operator token.  */
5534       token = cp_lexer_consume_token (parser->lexer);
5535       /* Parse the cast-expression.  */
5536       cast_expression
5537         = cp_parser_cast_expression (parser,
5538                                      unary_operator == ADDR_EXPR,
5539                                      /*cast_p=*/false, pidk);
5540       /* Now, build an appropriate representation.  */
5541       switch (unary_operator)
5542         {
5543         case INDIRECT_REF:
5544           non_constant_p = "%<*%>";
5545           expression = build_x_indirect_ref (cast_expression, "unary *",
5546                                              tf_warning_or_error);
5547           break;
5548
5549         case ADDR_EXPR:
5550           non_constant_p = "%<&%>";
5551           /* Fall through.  */
5552         case BIT_NOT_EXPR:
5553           expression = build_x_unary_op (unary_operator, cast_expression,
5554                                          tf_warning_or_error);
5555           break;
5556
5557         case PREINCREMENT_EXPR:
5558         case PREDECREMENT_EXPR:
5559           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5560                             ? "%<++%>" : "%<--%>");
5561           /* Fall through.  */
5562         case UNARY_PLUS_EXPR:
5563         case NEGATE_EXPR:
5564         case TRUTH_NOT_EXPR:
5565           expression = finish_unary_op_expr (unary_operator, cast_expression);
5566           break;
5567
5568         default:
5569           gcc_unreachable ();
5570         }
5571
5572       if (non_constant_p
5573           && cp_parser_non_integral_constant_expression (parser,
5574                                                          non_constant_p))
5575         expression = error_mark_node;
5576
5577       return expression;
5578     }
5579
5580   return cp_parser_postfix_expression (parser, address_p, cast_p,
5581                                        /*member_access_only_p=*/false,
5582                                        pidk);
5583 }
5584
5585 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5586    unary-operator, the corresponding tree code is returned.  */
5587
5588 static enum tree_code
5589 cp_parser_unary_operator (cp_token* token)
5590 {
5591   switch (token->type)
5592     {
5593     case CPP_MULT:
5594       return INDIRECT_REF;
5595
5596     case CPP_AND:
5597       return ADDR_EXPR;
5598
5599     case CPP_PLUS:
5600       return UNARY_PLUS_EXPR;
5601
5602     case CPP_MINUS:
5603       return NEGATE_EXPR;
5604
5605     case CPP_NOT:
5606       return TRUTH_NOT_EXPR;
5607
5608     case CPP_COMPL:
5609       return BIT_NOT_EXPR;
5610
5611     default:
5612       return ERROR_MARK;
5613     }
5614 }
5615
5616 /* Parse a new-expression.
5617
5618    new-expression:
5619      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5620      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5621
5622    Returns a representation of the expression.  */
5623
5624 static tree
5625 cp_parser_new_expression (cp_parser* parser)
5626 {
5627   bool global_scope_p;
5628   tree placement;
5629   tree type;
5630   tree initializer;
5631   tree nelts;
5632
5633   /* Look for the optional `::' operator.  */
5634   global_scope_p
5635     = (cp_parser_global_scope_opt (parser,
5636                                    /*current_scope_valid_p=*/false)
5637        != NULL_TREE);
5638   /* Look for the `new' operator.  */
5639   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5640   /* There's no easy way to tell a new-placement from the
5641      `( type-id )' construct.  */
5642   cp_parser_parse_tentatively (parser);
5643   /* Look for a new-placement.  */
5644   placement = cp_parser_new_placement (parser);
5645   /* If that didn't work out, there's no new-placement.  */
5646   if (!cp_parser_parse_definitely (parser))
5647     placement = NULL_TREE;
5648
5649   /* If the next token is a `(', then we have a parenthesized
5650      type-id.  */
5651   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5652     {
5653       cp_token *token;
5654       /* Consume the `('.  */
5655       cp_lexer_consume_token (parser->lexer);
5656       /* Parse the type-id.  */
5657       type = cp_parser_type_id (parser);
5658       /* Look for the closing `)'.  */
5659       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5660       token = cp_lexer_peek_token (parser->lexer);
5661       /* There should not be a direct-new-declarator in this production,
5662          but GCC used to allowed this, so we check and emit a sensible error
5663          message for this case.  */
5664       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5665         {
5666           error ("%Harray bound forbidden after parenthesized type-id",
5667                  &token->location);
5668           inform (token->location, 
5669                   "try removing the parentheses around the type-id");
5670           cp_parser_direct_new_declarator (parser);
5671         }
5672       nelts = NULL_TREE;
5673     }
5674   /* Otherwise, there must be a new-type-id.  */
5675   else
5676     type = cp_parser_new_type_id (parser, &nelts);
5677
5678   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5679   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5680       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5681     initializer = cp_parser_new_initializer (parser);
5682   else
5683     initializer = NULL_TREE;
5684
5685   /* A new-expression may not appear in an integral constant
5686      expression.  */
5687   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5688     return error_mark_node;
5689
5690   /* Create a representation of the new-expression.  */
5691   return build_new (placement, type, nelts, initializer, global_scope_p,
5692                     tf_warning_or_error);
5693 }
5694
5695 /* Parse a new-placement.
5696
5697    new-placement:
5698      ( expression-list )
5699
5700    Returns the same representation as for an expression-list.  */
5701
5702 static tree
5703 cp_parser_new_placement (cp_parser* parser)
5704 {
5705   tree expression_list;
5706
5707   /* Parse the expression-list.  */
5708   expression_list = (cp_parser_parenthesized_expression_list
5709                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5710                       /*non_constant_p=*/NULL));
5711
5712   return expression_list;
5713 }
5714
5715 /* Parse a new-type-id.
5716
5717    new-type-id:
5718      type-specifier-seq new-declarator [opt]
5719
5720    Returns the TYPE allocated.  If the new-type-id indicates an array
5721    type, *NELTS is set to the number of elements in the last array
5722    bound; the TYPE will not include the last array bound.  */
5723
5724 static tree
5725 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5726 {
5727   cp_decl_specifier_seq type_specifier_seq;
5728   cp_declarator *new_declarator;
5729   cp_declarator *declarator;
5730   cp_declarator *outer_declarator;
5731   const char *saved_message;
5732   tree type;
5733
5734   /* The type-specifier sequence must not contain type definitions.
5735      (It cannot contain declarations of new types either, but if they
5736      are not definitions we will catch that because they are not
5737      complete.)  */
5738   saved_message = parser->type_definition_forbidden_message;
5739   parser->type_definition_forbidden_message
5740     = "types may not be defined in a new-type-id";
5741   /* Parse the type-specifier-seq.  */
5742   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5743                                 &type_specifier_seq);
5744   /* Restore the old message.  */
5745   parser->type_definition_forbidden_message = saved_message;
5746   /* Parse the new-declarator.  */
5747   new_declarator = cp_parser_new_declarator_opt (parser);
5748
5749   /* Determine the number of elements in the last array dimension, if
5750      any.  */
5751   *nelts = NULL_TREE;
5752   /* Skip down to the last array dimension.  */
5753   declarator = new_declarator;
5754   outer_declarator = NULL;
5755   while (declarator && (declarator->kind == cdk_pointer
5756                         || declarator->kind == cdk_ptrmem))
5757     {
5758       outer_declarator = declarator;
5759       declarator = declarator->declarator;
5760     }
5761   while (declarator
5762          && declarator->kind == cdk_array
5763          && declarator->declarator
5764          && declarator->declarator->kind == cdk_array)
5765     {
5766       outer_declarator = declarator;
5767       declarator = declarator->declarator;
5768     }
5769
5770   if (declarator && declarator->kind == cdk_array)
5771     {
5772       *nelts = declarator->u.array.bounds;
5773       if (*nelts == error_mark_node)
5774         *nelts = integer_one_node;
5775
5776       if (outer_declarator)
5777         outer_declarator->declarator = declarator->declarator;
5778       else
5779         new_declarator = NULL;
5780     }
5781
5782   type = groktypename (&type_specifier_seq, new_declarator, false);
5783   return type;
5784 }
5785
5786 /* Parse an (optional) new-declarator.
5787
5788    new-declarator:
5789      ptr-operator new-declarator [opt]
5790      direct-new-declarator
5791
5792    Returns the declarator.  */
5793
5794 static cp_declarator *
5795 cp_parser_new_declarator_opt (cp_parser* parser)
5796 {
5797   enum tree_code code;
5798   tree type;
5799   cp_cv_quals cv_quals;
5800
5801   /* We don't know if there's a ptr-operator next, or not.  */
5802   cp_parser_parse_tentatively (parser);
5803   /* Look for a ptr-operator.  */
5804   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5805   /* If that worked, look for more new-declarators.  */
5806   if (cp_parser_parse_definitely (parser))
5807     {
5808       cp_declarator *declarator;
5809
5810       /* Parse another optional declarator.  */
5811       declarator = cp_parser_new_declarator_opt (parser);
5812
5813       return cp_parser_make_indirect_declarator
5814         (code, type, cv_quals, declarator);
5815     }
5816
5817   /* If the next token is a `[', there is a direct-new-declarator.  */
5818   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5819     return cp_parser_direct_new_declarator (parser);
5820
5821   return NULL;
5822 }
5823
5824 /* Parse a direct-new-declarator.
5825
5826    direct-new-declarator:
5827      [ expression ]
5828      direct-new-declarator [constant-expression]
5829
5830    */
5831
5832 static cp_declarator *
5833 cp_parser_direct_new_declarator (cp_parser* parser)
5834 {
5835   cp_declarator *declarator = NULL;
5836
5837   while (true)
5838     {
5839       tree expression;
5840
5841       /* Look for the opening `['.  */
5842       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5843       /* The first expression is not required to be constant.  */
5844       if (!declarator)
5845         {
5846           cp_token *token = cp_lexer_peek_token (parser->lexer);
5847           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5848           /* The standard requires that the expression have integral
5849              type.  DR 74 adds enumeration types.  We believe that the
5850              real intent is that these expressions be handled like the
5851              expression in a `switch' condition, which also allows
5852              classes with a single conversion to integral or
5853              enumeration type.  */
5854           if (!processing_template_decl)
5855             {
5856               expression
5857                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5858                                               expression,
5859                                               /*complain=*/true);
5860               if (!expression)
5861                 {
5862                   error ("%Hexpression in new-declarator must have integral "
5863                          "or enumeration type", &token->location);
5864                   expression = error_mark_node;
5865                 }
5866             }
5867         }
5868       /* But all the other expressions must be.  */
5869       else
5870         expression
5871           = cp_parser_constant_expression (parser,
5872                                            /*allow_non_constant=*/false,
5873                                            NULL);
5874       /* Look for the closing `]'.  */
5875       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5876
5877       /* Add this bound to the declarator.  */
5878       declarator = make_array_declarator (declarator, expression);
5879
5880       /* If the next token is not a `[', then there are no more
5881          bounds.  */
5882       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5883         break;
5884     }
5885
5886   return declarator;
5887 }
5888
5889 /* Parse a new-initializer.
5890
5891    new-initializer:
5892      ( expression-list [opt] )
5893      braced-init-list
5894
5895    Returns a representation of the expression-list.  If there is no
5896    expression-list, VOID_ZERO_NODE is returned.  */
5897
5898 static tree
5899 cp_parser_new_initializer (cp_parser* parser)
5900 {
5901   tree expression_list;
5902
5903   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5904     {
5905       bool expr_non_constant_p;
5906       maybe_warn_cpp0x ("extended initializer lists");
5907       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5908       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5909       expression_list = build_tree_list (NULL_TREE, expression_list);
5910     }
5911   else
5912     expression_list = (cp_parser_parenthesized_expression_list
5913                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5914                         /*non_constant_p=*/NULL));
5915   if (!expression_list)
5916     expression_list = void_zero_node;
5917
5918   return expression_list;
5919 }
5920
5921 /* Parse a delete-expression.
5922
5923    delete-expression:
5924      :: [opt] delete cast-expression
5925      :: [opt] delete [ ] cast-expression
5926
5927    Returns a representation of the expression.  */
5928
5929 static tree
5930 cp_parser_delete_expression (cp_parser* parser)
5931 {
5932   bool global_scope_p;
5933   bool array_p;
5934   tree expression;
5935
5936   /* Look for the optional `::' operator.  */
5937   global_scope_p
5938     = (cp_parser_global_scope_opt (parser,
5939                                    /*current_scope_valid_p=*/false)
5940        != NULL_TREE);
5941   /* Look for the `delete' keyword.  */
5942   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5943   /* See if the array syntax is in use.  */
5944   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5945     {
5946       /* Consume the `[' token.  */
5947       cp_lexer_consume_token (parser->lexer);
5948       /* Look for the `]' token.  */
5949       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5950       /* Remember that this is the `[]' construct.  */
5951       array_p = true;
5952     }
5953   else
5954     array_p = false;
5955
5956   /* Parse the cast-expression.  */
5957   expression = cp_parser_simple_cast_expression (parser);
5958
5959   /* A delete-expression may not appear in an integral constant
5960      expression.  */
5961   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5962     return error_mark_node;
5963
5964   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5965 }
5966
5967 /* Returns true if TOKEN may start a cast-expression and false
5968    otherwise.  */
5969
5970 static bool
5971 cp_parser_token_starts_cast_expression (cp_token *token)
5972 {
5973   switch (token->type)
5974     {
5975     case CPP_COMMA:
5976     case CPP_SEMICOLON:
5977     case CPP_QUERY:
5978     case CPP_COLON:
5979     case CPP_CLOSE_SQUARE:
5980     case CPP_CLOSE_PAREN:
5981     case CPP_CLOSE_BRACE:
5982     case CPP_DOT:
5983     case CPP_DOT_STAR:
5984     case CPP_DEREF:
5985     case CPP_DEREF_STAR:
5986     case CPP_DIV:
5987     case CPP_MOD:
5988     case CPP_LSHIFT:
5989     case CPP_RSHIFT:
5990     case CPP_LESS:
5991     case CPP_GREATER:
5992     case CPP_LESS_EQ:
5993     case CPP_GREATER_EQ:
5994     case CPP_EQ_EQ:
5995     case CPP_NOT_EQ:
5996     case CPP_EQ:
5997     case CPP_MULT_EQ:
5998     case CPP_DIV_EQ:
5999     case CPP_MOD_EQ:
6000     case CPP_PLUS_EQ:
6001     case CPP_MINUS_EQ:
6002     case CPP_RSHIFT_EQ:
6003     case CPP_LSHIFT_EQ:
6004     case CPP_AND_EQ:
6005     case CPP_XOR_EQ:
6006     case CPP_OR_EQ:
6007     case CPP_XOR:
6008     case CPP_OR:
6009     case CPP_OR_OR:
6010     case CPP_EOF:
6011       return false;
6012
6013       /* '[' may start a primary-expression in obj-c++.  */
6014     case CPP_OPEN_SQUARE:
6015       return c_dialect_objc ();
6016
6017     default:
6018       return true;
6019     }
6020 }
6021
6022 /* Parse a cast-expression.
6023
6024    cast-expression:
6025      unary-expression
6026      ( type-id ) cast-expression
6027
6028    ADDRESS_P is true iff the unary-expression is appearing as the
6029    operand of the `&' operator.   CAST_P is true if this expression is
6030    the target of a cast.
6031
6032    Returns a representation of the expression.  */
6033
6034 static tree
6035 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6036                            cp_id_kind * pidk)
6037 {
6038   /* If it's a `(', then we might be looking at a cast.  */
6039   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6040     {
6041       tree type = NULL_TREE;
6042       tree expr = NULL_TREE;
6043       bool compound_literal_p;
6044       const char *saved_message;
6045
6046       /* There's no way to know yet whether or not this is a cast.
6047          For example, `(int (3))' is a unary-expression, while `(int)
6048          3' is a cast.  So, we resort to parsing tentatively.  */
6049       cp_parser_parse_tentatively (parser);
6050       /* Types may not be defined in a cast.  */
6051       saved_message = parser->type_definition_forbidden_message;
6052       parser->type_definition_forbidden_message
6053         = "types may not be defined in casts";
6054       /* Consume the `('.  */
6055       cp_lexer_consume_token (parser->lexer);
6056       /* A very tricky bit is that `(struct S) { 3 }' is a
6057          compound-literal (which we permit in C++ as an extension).
6058          But, that construct is not a cast-expression -- it is a
6059          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6060          is legal; if the compound-literal were a cast-expression,
6061          you'd need an extra set of parentheses.)  But, if we parse
6062          the type-id, and it happens to be a class-specifier, then we
6063          will commit to the parse at that point, because we cannot
6064          undo the action that is done when creating a new class.  So,
6065          then we cannot back up and do a postfix-expression.
6066
6067          Therefore, we scan ahead to the closing `)', and check to see
6068          if the token after the `)' is a `{'.  If so, we are not
6069          looking at a cast-expression.
6070
6071          Save tokens so that we can put them back.  */
6072       cp_lexer_save_tokens (parser->lexer);
6073       /* Skip tokens until the next token is a closing parenthesis.
6074          If we find the closing `)', and the next token is a `{', then
6075          we are looking at a compound-literal.  */
6076       compound_literal_p
6077         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6078                                                   /*consume_paren=*/true)
6079            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6080       /* Roll back the tokens we skipped.  */
6081       cp_lexer_rollback_tokens (parser->lexer);
6082       /* If we were looking at a compound-literal, simulate an error
6083          so that the call to cp_parser_parse_definitely below will
6084          fail.  */
6085       if (compound_literal_p)
6086         cp_parser_simulate_error (parser);
6087       else
6088         {
6089           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6090           parser->in_type_id_in_expr_p = true;
6091           /* Look for the type-id.  */
6092           type = cp_parser_type_id (parser);
6093           /* Look for the closing `)'.  */
6094           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6095           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6096         }
6097
6098       /* Restore the saved message.  */
6099       parser->type_definition_forbidden_message = saved_message;
6100
6101       /* At this point this can only be either a cast or a
6102          parenthesized ctor such as `(T ())' that looks like a cast to
6103          function returning T.  */
6104       if (!cp_parser_error_occurred (parser)
6105           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6106                                                      (parser->lexer)))
6107         {
6108           cp_parser_parse_definitely (parser);
6109           expr = cp_parser_cast_expression (parser,
6110                                             /*address_p=*/false,
6111                                             /*cast_p=*/true, pidk);
6112
6113           /* Warn about old-style casts, if so requested.  */
6114           if (warn_old_style_cast
6115               && !in_system_header
6116               && !VOID_TYPE_P (type)
6117               && current_lang_name != lang_name_c)
6118             warning (OPT_Wold_style_cast, "use of old-style cast");
6119
6120           /* Only type conversions to integral or enumeration types
6121              can be used in constant-expressions.  */
6122           if (!cast_valid_in_integral_constant_expression_p (type)
6123               && (cp_parser_non_integral_constant_expression
6124                   (parser,
6125                    "a cast to a type other than an integral or "
6126                    "enumeration type")))
6127             return error_mark_node;
6128
6129           /* Perform the cast.  */
6130           expr = build_c_cast (type, expr);
6131           return expr;
6132         }
6133       else 
6134         cp_parser_abort_tentative_parse (parser);
6135     }
6136
6137   /* If we get here, then it's not a cast, so it must be a
6138      unary-expression.  */
6139   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6140 }
6141
6142 /* Parse a binary expression of the general form:
6143
6144    pm-expression:
6145      cast-expression
6146      pm-expression .* cast-expression
6147      pm-expression ->* cast-expression
6148
6149    multiplicative-expression:
6150      pm-expression
6151      multiplicative-expression * pm-expression
6152      multiplicative-expression / pm-expression
6153      multiplicative-expression % pm-expression
6154
6155    additive-expression:
6156      multiplicative-expression
6157      additive-expression + multiplicative-expression
6158      additive-expression - multiplicative-expression
6159
6160    shift-expression:
6161      additive-expression
6162      shift-expression << additive-expression
6163      shift-expression >> additive-expression
6164
6165    relational-expression:
6166      shift-expression
6167      relational-expression < shift-expression
6168      relational-expression > shift-expression
6169      relational-expression <= shift-expression
6170      relational-expression >= shift-expression
6171
6172   GNU Extension:
6173
6174    relational-expression:
6175      relational-expression <? shift-expression
6176      relational-expression >? shift-expression
6177
6178    equality-expression:
6179      relational-expression
6180      equality-expression == relational-expression
6181      equality-expression != relational-expression
6182
6183    and-expression:
6184      equality-expression
6185      and-expression & equality-expression
6186
6187    exclusive-or-expression:
6188      and-expression
6189      exclusive-or-expression ^ and-expression
6190
6191    inclusive-or-expression:
6192      exclusive-or-expression
6193      inclusive-or-expression | exclusive-or-expression
6194
6195    logical-and-expression:
6196      inclusive-or-expression
6197      logical-and-expression && inclusive-or-expression
6198
6199    logical-or-expression:
6200      logical-and-expression
6201      logical-or-expression || logical-and-expression
6202
6203    All these are implemented with a single function like:
6204
6205    binary-expression:
6206      simple-cast-expression
6207      binary-expression <token> binary-expression
6208
6209    CAST_P is true if this expression is the target of a cast.
6210
6211    The binops_by_token map is used to get the tree codes for each <token> type.
6212    binary-expressions are associated according to a precedence table.  */
6213
6214 #define TOKEN_PRECEDENCE(token)                              \
6215 (((token->type == CPP_GREATER                                \
6216    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6217   && !parser->greater_than_is_operator_p)                    \
6218  ? PREC_NOT_OPERATOR                                         \
6219  : binops_by_token[token->type].prec)
6220
6221 static tree
6222 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6223                              bool no_toplevel_fold_p,
6224                              enum cp_parser_prec prec,
6225                              cp_id_kind * pidk)
6226 {
6227   cp_parser_expression_stack stack;
6228   cp_parser_expression_stack_entry *sp = &stack[0];
6229   tree lhs, rhs;
6230   cp_token *token;
6231   enum tree_code tree_type, lhs_type, rhs_type;
6232   enum cp_parser_prec new_prec, lookahead_prec;
6233   bool overloaded_p;
6234
6235   /* Parse the first expression.  */
6236   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6237   lhs_type = ERROR_MARK;
6238
6239   for (;;)
6240     {
6241       /* Get an operator token.  */
6242       token = cp_lexer_peek_token (parser->lexer);
6243
6244       if (warn_cxx0x_compat
6245           && token->type == CPP_RSHIFT
6246           && !parser->greater_than_is_operator_p)
6247         {
6248           warning (OPT_Wc__0x_compat, 
6249                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6250                    &token->location);
6251           warning (OPT_Wc__0x_compat, 
6252                    "suggest parentheses around %<>>%> expression");
6253         }
6254
6255       new_prec = TOKEN_PRECEDENCE (token);
6256
6257       /* Popping an entry off the stack means we completed a subexpression:
6258          - either we found a token which is not an operator (`>' where it is not
6259            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6260            will happen repeatedly;
6261          - or, we found an operator which has lower priority.  This is the case
6262            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6263            parsing `3 * 4'.  */
6264       if (new_prec <= prec)
6265         {
6266           if (sp == stack)
6267             break;
6268           else
6269             goto pop;
6270         }
6271
6272      get_rhs:
6273       tree_type = binops_by_token[token->type].tree_type;
6274
6275       /* We used the operator token.  */
6276       cp_lexer_consume_token (parser->lexer);
6277
6278       /* Extract another operand.  It may be the RHS of this expression
6279          or the LHS of a new, higher priority expression.  */
6280       rhs = cp_parser_simple_cast_expression (parser);
6281       rhs_type = ERROR_MARK;
6282
6283       /* Get another operator token.  Look up its precedence to avoid
6284          building a useless (immediately popped) stack entry for common
6285          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6286       token = cp_lexer_peek_token (parser->lexer);
6287       lookahead_prec = TOKEN_PRECEDENCE (token);
6288       if (lookahead_prec > new_prec)
6289         {
6290           /* ... and prepare to parse the RHS of the new, higher priority
6291              expression.  Since precedence levels on the stack are
6292              monotonically increasing, we do not have to care about
6293              stack overflows.  */
6294           sp->prec = prec;
6295           sp->tree_type = tree_type;
6296           sp->lhs = lhs;
6297           sp->lhs_type = lhs_type;
6298           sp++;
6299           lhs = rhs;
6300           lhs_type = rhs_type;
6301           prec = new_prec;
6302           new_prec = lookahead_prec;
6303           goto get_rhs;
6304
6305          pop:
6306           lookahead_prec = new_prec;
6307           /* If the stack is not empty, we have parsed into LHS the right side
6308              (`4' in the example above) of an expression we had suspended.
6309              We can use the information on the stack to recover the LHS (`3')
6310              from the stack together with the tree code (`MULT_EXPR'), and
6311              the precedence of the higher level subexpression
6312              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6313              which will be used to actually build the additive expression.  */
6314           --sp;
6315           prec = sp->prec;
6316           tree_type = sp->tree_type;
6317           rhs = lhs;
6318           rhs_type = lhs_type;
6319           lhs = sp->lhs;
6320           lhs_type = sp->lhs_type;
6321         }
6322
6323       overloaded_p = false;
6324       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6325          ERROR_MARK for everything that is not a binary expression.
6326          This makes warn_about_parentheses miss some warnings that
6327          involve unary operators.  For unary expressions we should
6328          pass the correct tree_code unless the unary expression was
6329          surrounded by parentheses.
6330       */
6331       if (no_toplevel_fold_p
6332           && lookahead_prec <= prec
6333           && sp == stack
6334           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6335         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6336       else
6337         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6338                                  &overloaded_p, tf_warning_or_error);
6339       lhs_type = tree_type;
6340
6341       /* If the binary operator required the use of an overloaded operator,
6342          then this expression cannot be an integral constant-expression.
6343          An overloaded operator can be used even if both operands are
6344          otherwise permissible in an integral constant-expression if at
6345          least one of the operands is of enumeration type.  */
6346
6347       if (overloaded_p
6348           && (cp_parser_non_integral_constant_expression
6349               (parser, "calls to overloaded operators")))
6350         return error_mark_node;
6351     }
6352
6353   return lhs;
6354 }
6355
6356
6357 /* Parse the `? expression : assignment-expression' part of a
6358    conditional-expression.  The LOGICAL_OR_EXPR is the
6359    logical-or-expression that started the conditional-expression.
6360    Returns a representation of the entire conditional-expression.
6361
6362    This routine is used by cp_parser_assignment_expression.
6363
6364      ? expression : assignment-expression
6365
6366    GNU Extensions:
6367
6368      ? : assignment-expression */
6369
6370 static tree
6371 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6372 {
6373   tree expr;
6374   tree assignment_expr;
6375
6376   /* Consume the `?' token.  */
6377   cp_lexer_consume_token (parser->lexer);
6378   if (cp_parser_allow_gnu_extensions_p (parser)
6379       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6380     /* Implicit true clause.  */
6381     expr = NULL_TREE;
6382   else
6383     /* Parse the expression.  */
6384     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6385
6386   /* The next token should be a `:'.  */
6387   cp_parser_require (parser, CPP_COLON, "%<:%>");
6388   /* Parse the assignment-expression.  */
6389   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6390
6391   /* Build the conditional-expression.  */
6392   return build_x_conditional_expr (logical_or_expr,
6393                                    expr,
6394                                    assignment_expr,
6395                                    tf_warning_or_error);
6396 }
6397
6398 /* Parse an assignment-expression.
6399
6400    assignment-expression:
6401      conditional-expression
6402      logical-or-expression assignment-operator assignment_expression
6403      throw-expression
6404
6405    CAST_P is true if this expression is the target of a cast.
6406
6407    Returns a representation for the expression.  */
6408
6409 static tree
6410 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6411                                  cp_id_kind * pidk)
6412 {
6413   tree expr;
6414
6415   /* If the next token is the `throw' keyword, then we're looking at
6416      a throw-expression.  */
6417   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6418     expr = cp_parser_throw_expression (parser);
6419   /* Otherwise, it must be that we are looking at a
6420      logical-or-expression.  */
6421   else
6422     {
6423       /* Parse the binary expressions (logical-or-expression).  */
6424       expr = cp_parser_binary_expression (parser, cast_p, false,
6425                                           PREC_NOT_OPERATOR, pidk);
6426       /* If the next token is a `?' then we're actually looking at a
6427          conditional-expression.  */
6428       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6429         return cp_parser_question_colon_clause (parser, expr);
6430       else
6431         {
6432           enum tree_code assignment_operator;
6433
6434           /* If it's an assignment-operator, we're using the second
6435              production.  */
6436           assignment_operator
6437             = cp_parser_assignment_operator_opt (parser);
6438           if (assignment_operator != ERROR_MARK)
6439             {
6440               bool non_constant_p;
6441
6442               /* Parse the right-hand side of the assignment.  */
6443               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6444
6445               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6446                 maybe_warn_cpp0x ("extended initializer lists");
6447
6448               /* An assignment may not appear in a
6449                  constant-expression.  */
6450               if (cp_parser_non_integral_constant_expression (parser,
6451                                                               "an assignment"))
6452                 return error_mark_node;
6453               /* Build the assignment expression.  */
6454               expr = build_x_modify_expr (expr,
6455                                           assignment_operator,
6456                                           rhs,
6457                                           tf_warning_or_error);
6458             }
6459         }
6460     }
6461
6462   return expr;
6463 }
6464
6465 /* Parse an (optional) assignment-operator.
6466
6467    assignment-operator: one of
6468      = *= /= %= += -= >>= <<= &= ^= |=
6469
6470    GNU Extension:
6471
6472    assignment-operator: one of
6473      <?= >?=
6474
6475    If the next token is an assignment operator, the corresponding tree
6476    code is returned, and the token is consumed.  For example, for
6477    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6478    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6479    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6480    operator, ERROR_MARK is returned.  */
6481
6482 static enum tree_code
6483 cp_parser_assignment_operator_opt (cp_parser* parser)
6484 {
6485   enum tree_code op;
6486   cp_token *token;
6487
6488   /* Peek at the next token.  */
6489   token = cp_lexer_peek_token (parser->lexer);
6490
6491   switch (token->type)
6492     {
6493     case CPP_EQ:
6494       op = NOP_EXPR;
6495       break;
6496
6497     case CPP_MULT_EQ:
6498       op = MULT_EXPR;
6499       break;
6500
6501     case CPP_DIV_EQ:
6502       op = TRUNC_DIV_EXPR;
6503       break;
6504
6505     case CPP_MOD_EQ:
6506       op = TRUNC_MOD_EXPR;
6507       break;
6508
6509     case CPP_PLUS_EQ:
6510       op = PLUS_EXPR;
6511       break;
6512
6513     case CPP_MINUS_EQ:
6514       op = MINUS_EXPR;
6515       break;
6516
6517     case CPP_RSHIFT_EQ:
6518       op = RSHIFT_EXPR;
6519       break;
6520
6521     case CPP_LSHIFT_EQ:
6522       op = LSHIFT_EXPR;
6523       break;
6524
6525     case CPP_AND_EQ:
6526       op = BIT_AND_EXPR;
6527       break;
6528
6529     case CPP_XOR_EQ:
6530       op = BIT_XOR_EXPR;
6531       break;
6532
6533     case CPP_OR_EQ:
6534       op = BIT_IOR_EXPR;
6535       break;
6536
6537     default:
6538       /* Nothing else is an assignment operator.  */
6539       op = ERROR_MARK;
6540     }
6541
6542   /* If it was an assignment operator, consume it.  */
6543   if (op != ERROR_MARK)
6544     cp_lexer_consume_token (parser->lexer);
6545
6546   return op;
6547 }
6548
6549 /* Parse an expression.
6550
6551    expression:
6552      assignment-expression
6553      expression , assignment-expression
6554
6555    CAST_P is true if this expression is the target of a cast.
6556
6557    Returns a representation of the expression.  */
6558
6559 static tree
6560 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6561 {
6562   tree expression = NULL_TREE;
6563
6564   while (true)
6565     {
6566       tree assignment_expression;
6567
6568       /* Parse the next assignment-expression.  */
6569       assignment_expression
6570         = cp_parser_assignment_expression (parser, cast_p, pidk);
6571       /* If this is the first assignment-expression, we can just
6572          save it away.  */
6573       if (!expression)
6574         expression = assignment_expression;
6575       else
6576         expression = build_x_compound_expr (expression,
6577                                             assignment_expression,
6578                                             tf_warning_or_error);
6579       /* If the next token is not a comma, then we are done with the
6580          expression.  */
6581       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6582         break;
6583       /* Consume the `,'.  */
6584       cp_lexer_consume_token (parser->lexer);
6585       /* A comma operator cannot appear in a constant-expression.  */
6586       if (cp_parser_non_integral_constant_expression (parser,
6587                                                       "a comma operator"))
6588         expression = error_mark_node;
6589     }
6590
6591   return expression;
6592 }
6593
6594 /* Parse a constant-expression.
6595
6596    constant-expression:
6597      conditional-expression
6598
6599   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6600   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6601   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6602   is false, NON_CONSTANT_P should be NULL.  */
6603
6604 static tree
6605 cp_parser_constant_expression (cp_parser* parser,
6606                                bool allow_non_constant_p,
6607                                bool *non_constant_p)
6608 {
6609   bool saved_integral_constant_expression_p;
6610   bool saved_allow_non_integral_constant_expression_p;
6611   bool saved_non_integral_constant_expression_p;
6612   tree expression;
6613
6614   /* It might seem that we could simply parse the
6615      conditional-expression, and then check to see if it were
6616      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6617      one that the compiler can figure out is constant, possibly after
6618      doing some simplifications or optimizations.  The standard has a
6619      precise definition of constant-expression, and we must honor
6620      that, even though it is somewhat more restrictive.
6621
6622      For example:
6623
6624        int i[(2, 3)];
6625
6626      is not a legal declaration, because `(2, 3)' is not a
6627      constant-expression.  The `,' operator is forbidden in a
6628      constant-expression.  However, GCC's constant-folding machinery
6629      will fold this operation to an INTEGER_CST for `3'.  */
6630
6631   /* Save the old settings.  */
6632   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6633   saved_allow_non_integral_constant_expression_p
6634     = parser->allow_non_integral_constant_expression_p;
6635   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6636   /* We are now parsing a constant-expression.  */
6637   parser->integral_constant_expression_p = true;
6638   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6639   parser->non_integral_constant_expression_p = false;
6640   /* Although the grammar says "conditional-expression", we parse an
6641      "assignment-expression", which also permits "throw-expression"
6642      and the use of assignment operators.  In the case that
6643      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6644      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6645      actually essential that we look for an assignment-expression.
6646      For example, cp_parser_initializer_clauses uses this function to
6647      determine whether a particular assignment-expression is in fact
6648      constant.  */
6649   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6650   /* Restore the old settings.  */
6651   parser->integral_constant_expression_p
6652     = saved_integral_constant_expression_p;
6653   parser->allow_non_integral_constant_expression_p
6654     = saved_allow_non_integral_constant_expression_p;
6655   if (allow_non_constant_p)
6656     *non_constant_p = parser->non_integral_constant_expression_p;
6657   else if (parser->non_integral_constant_expression_p)
6658     expression = error_mark_node;
6659   parser->non_integral_constant_expression_p
6660     = saved_non_integral_constant_expression_p;
6661
6662   return expression;
6663 }
6664
6665 /* Parse __builtin_offsetof.
6666
6667    offsetof-expression:
6668      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6669
6670    offsetof-member-designator:
6671      id-expression
6672      | offsetof-member-designator "." id-expression
6673      | offsetof-member-designator "[" expression "]"
6674      | offsetof-member-designator "->" id-expression  */
6675
6676 static tree
6677 cp_parser_builtin_offsetof (cp_parser *parser)
6678 {
6679   int save_ice_p, save_non_ice_p;
6680   tree type, expr;
6681   cp_id_kind dummy;
6682   cp_token *token;
6683
6684   /* We're about to accept non-integral-constant things, but will
6685      definitely yield an integral constant expression.  Save and
6686      restore these values around our local parsing.  */
6687   save_ice_p = parser->integral_constant_expression_p;
6688   save_non_ice_p = parser->non_integral_constant_expression_p;
6689
6690   /* Consume the "__builtin_offsetof" token.  */
6691   cp_lexer_consume_token (parser->lexer);
6692   /* Consume the opening `('.  */
6693   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6694   /* Parse the type-id.  */
6695   type = cp_parser_type_id (parser);
6696   /* Look for the `,'.  */
6697   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6698   token = cp_lexer_peek_token (parser->lexer);
6699
6700   /* Build the (type *)null that begins the traditional offsetof macro.  */
6701   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6702                             tf_warning_or_error);
6703
6704   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6705   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6706                                                  true, &dummy, token->location);
6707   while (true)
6708     {
6709       token = cp_lexer_peek_token (parser->lexer);
6710       switch (token->type)
6711         {
6712         case CPP_OPEN_SQUARE:
6713           /* offsetof-member-designator "[" expression "]" */
6714           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6715           break;
6716
6717         case CPP_DEREF:
6718           /* offsetof-member-designator "->" identifier */
6719           expr = grok_array_decl (expr, integer_zero_node);
6720           /* FALLTHRU */
6721
6722         case CPP_DOT:
6723           /* offsetof-member-designator "." identifier */
6724           cp_lexer_consume_token (parser->lexer);
6725           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6726                                                          expr, true, &dummy,
6727                                                          token->location);
6728           break;
6729
6730         case CPP_CLOSE_PAREN:
6731           /* Consume the ")" token.  */
6732           cp_lexer_consume_token (parser->lexer);
6733           goto success;
6734
6735         default:
6736           /* Error.  We know the following require will fail, but
6737              that gives the proper error message.  */
6738           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6739           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6740           expr = error_mark_node;
6741           goto failure;
6742         }
6743     }
6744
6745  success:
6746   /* If we're processing a template, we can't finish the semantics yet.
6747      Otherwise we can fold the entire expression now.  */
6748   if (processing_template_decl)
6749     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6750   else
6751     expr = finish_offsetof (expr);
6752
6753  failure:
6754   parser->integral_constant_expression_p = save_ice_p;
6755   parser->non_integral_constant_expression_p = save_non_ice_p;
6756
6757   return expr;
6758 }
6759
6760 /* Parse a trait expression.  */
6761
6762 static tree
6763 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6764 {
6765   cp_trait_kind kind;
6766   tree type1, type2 = NULL_TREE;
6767   bool binary = false;
6768   cp_decl_specifier_seq decl_specs;
6769
6770   switch (keyword)
6771     {
6772     case RID_HAS_NOTHROW_ASSIGN:
6773       kind = CPTK_HAS_NOTHROW_ASSIGN;
6774       break;
6775     case RID_HAS_NOTHROW_CONSTRUCTOR:
6776       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6777       break;
6778     case RID_HAS_NOTHROW_COPY:
6779       kind = CPTK_HAS_NOTHROW_COPY;
6780       break;
6781     case RID_HAS_TRIVIAL_ASSIGN:
6782       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6783       break;
6784     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6785       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6786       break;
6787     case RID_HAS_TRIVIAL_COPY:
6788       kind = CPTK_HAS_TRIVIAL_COPY;
6789       break;
6790     case RID_HAS_TRIVIAL_DESTRUCTOR:
6791       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6792       break;
6793     case RID_HAS_VIRTUAL_DESTRUCTOR:
6794       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6795       break;
6796     case RID_IS_ABSTRACT:
6797       kind = CPTK_IS_ABSTRACT;
6798       break;
6799     case RID_IS_BASE_OF:
6800       kind = CPTK_IS_BASE_OF;
6801       binary = true;
6802       break;
6803     case RID_IS_CLASS:
6804       kind = CPTK_IS_CLASS;
6805       break;
6806     case RID_IS_CONVERTIBLE_TO:
6807       kind = CPTK_IS_CONVERTIBLE_TO;
6808       binary = true;
6809       break;
6810     case RID_IS_EMPTY:
6811       kind = CPTK_IS_EMPTY;
6812       break;
6813     case RID_IS_ENUM:
6814       kind = CPTK_IS_ENUM;
6815       break;
6816     case RID_IS_POD:
6817       kind = CPTK_IS_POD;
6818       break;
6819     case RID_IS_POLYMORPHIC:
6820       kind = CPTK_IS_POLYMORPHIC;
6821       break;
6822     case RID_IS_UNION:
6823       kind = CPTK_IS_UNION;
6824       break;
6825     default:
6826       gcc_unreachable ();
6827     }
6828
6829   /* Consume the token.  */
6830   cp_lexer_consume_token (parser->lexer);
6831
6832   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6833
6834   type1 = cp_parser_type_id (parser);
6835
6836   if (type1 == error_mark_node)
6837     return error_mark_node;
6838
6839   /* Build a trivial decl-specifier-seq.  */
6840   clear_decl_specs (&decl_specs);
6841   decl_specs.type = type1;
6842
6843   /* Call grokdeclarator to figure out what type this is.  */
6844   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6845                           /*initialized=*/0, /*attrlist=*/NULL);
6846
6847   if (binary)
6848     {
6849       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6850  
6851       type2 = cp_parser_type_id (parser);
6852
6853       if (type2 == error_mark_node)
6854         return error_mark_node;
6855
6856       /* Build a trivial decl-specifier-seq.  */
6857       clear_decl_specs (&decl_specs);
6858       decl_specs.type = type2;
6859
6860       /* Call grokdeclarator to figure out what type this is.  */
6861       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6862                               /*initialized=*/0, /*attrlist=*/NULL);
6863     }
6864
6865   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6866
6867   /* Complete the trait expression, which may mean either processing
6868      the trait expr now or saving it for template instantiation.  */
6869   return finish_trait_expr (kind, type1, type2);
6870 }
6871
6872 /* Statements [gram.stmt.stmt]  */
6873
6874 /* Parse a statement.
6875
6876    statement:
6877      labeled-statement
6878      expression-statement
6879      compound-statement
6880      selection-statement
6881      iteration-statement
6882      jump-statement
6883      declaration-statement
6884      try-block
6885
6886   IN_COMPOUND is true when the statement is nested inside a
6887   cp_parser_compound_statement; this matters for certain pragmas.
6888
6889   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6890   is a (possibly labeled) if statement which is not enclosed in braces
6891   and has an else clause.  This is used to implement -Wparentheses.  */
6892
6893 static void
6894 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6895                      bool in_compound, bool *if_p)
6896 {
6897   tree statement;
6898   cp_token *token;
6899   location_t statement_location;
6900
6901  restart:
6902   if (if_p != NULL)
6903     *if_p = false;
6904   /* There is no statement yet.  */
6905   statement = NULL_TREE;
6906   /* Peek at the next token.  */
6907   token = cp_lexer_peek_token (parser->lexer);
6908   /* Remember the location of the first token in the statement.  */
6909   statement_location = token->location;
6910   /* If this is a keyword, then that will often determine what kind of
6911      statement we have.  */
6912   if (token->type == CPP_KEYWORD)
6913     {
6914       enum rid keyword = token->keyword;
6915
6916       switch (keyword)
6917         {
6918         case RID_CASE:
6919         case RID_DEFAULT:
6920           /* Looks like a labeled-statement with a case label.
6921              Parse the label, and then use tail recursion to parse
6922              the statement.  */
6923           cp_parser_label_for_labeled_statement (parser);
6924           goto restart;
6925
6926         case RID_IF:
6927         case RID_SWITCH:
6928           statement = cp_parser_selection_statement (parser, if_p);
6929           break;
6930
6931         case RID_WHILE:
6932         case RID_DO:
6933         case RID_FOR:
6934           statement = cp_parser_iteration_statement (parser);
6935           break;
6936
6937         case RID_BREAK:
6938         case RID_CONTINUE:
6939         case RID_RETURN:
6940         case RID_GOTO:
6941           statement = cp_parser_jump_statement (parser);
6942           break;
6943
6944           /* Objective-C++ exception-handling constructs.  */
6945         case RID_AT_TRY:
6946         case RID_AT_CATCH:
6947         case RID_AT_FINALLY:
6948         case RID_AT_SYNCHRONIZED:
6949         case RID_AT_THROW:
6950           statement = cp_parser_objc_statement (parser);
6951           break;
6952
6953         case RID_TRY:
6954           statement = cp_parser_try_block (parser);
6955           break;
6956
6957         case RID_NAMESPACE:
6958           /* This must be a namespace alias definition.  */
6959           cp_parser_declaration_statement (parser);
6960           return;
6961           
6962         default:
6963           /* It might be a keyword like `int' that can start a
6964              declaration-statement.  */
6965           break;
6966         }
6967     }
6968   else if (token->type == CPP_NAME)
6969     {
6970       /* If the next token is a `:', then we are looking at a
6971          labeled-statement.  */
6972       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6973       if (token->type == CPP_COLON)
6974         {
6975           /* Looks like a labeled-statement with an ordinary label.
6976              Parse the label, and then use tail recursion to parse
6977              the statement.  */
6978           cp_parser_label_for_labeled_statement (parser);
6979           goto restart;
6980         }
6981     }
6982   /* Anything that starts with a `{' must be a compound-statement.  */
6983   else if (token->type == CPP_OPEN_BRACE)
6984     statement = cp_parser_compound_statement (parser, NULL, false);
6985   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6986      a statement all its own.  */
6987   else if (token->type == CPP_PRAGMA)
6988     {
6989       /* Only certain OpenMP pragmas are attached to statements, and thus
6990          are considered statements themselves.  All others are not.  In
6991          the context of a compound, accept the pragma as a "statement" and
6992          return so that we can check for a close brace.  Otherwise we
6993          require a real statement and must go back and read one.  */
6994       if (in_compound)
6995         cp_parser_pragma (parser, pragma_compound);
6996       else if (!cp_parser_pragma (parser, pragma_stmt))
6997         goto restart;
6998       return;
6999     }
7000   else if (token->type == CPP_EOF)
7001     {
7002       cp_parser_error (parser, "expected statement");
7003       return;
7004     }
7005
7006   /* Everything else must be a declaration-statement or an
7007      expression-statement.  Try for the declaration-statement
7008      first, unless we are looking at a `;', in which case we know that
7009      we have an expression-statement.  */
7010   if (!statement)
7011     {
7012       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7013         {
7014           cp_parser_parse_tentatively (parser);
7015           /* Try to parse the declaration-statement.  */
7016           cp_parser_declaration_statement (parser);
7017           /* If that worked, we're done.  */
7018           if (cp_parser_parse_definitely (parser))
7019             return;
7020         }
7021       /* Look for an expression-statement instead.  */
7022       statement = cp_parser_expression_statement (parser, in_statement_expr);
7023     }
7024
7025   /* Set the line number for the statement.  */
7026   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7027     SET_EXPR_LOCATION (statement, statement_location);
7028 }
7029
7030 /* Parse the label for a labeled-statement, i.e.
7031
7032    identifier :
7033    case constant-expression :
7034    default :
7035
7036    GNU Extension:
7037    case constant-expression ... constant-expression : statement
7038
7039    When a label is parsed without errors, the label is added to the
7040    parse tree by the finish_* functions, so this function doesn't
7041    have to return the label.  */
7042
7043 static void
7044 cp_parser_label_for_labeled_statement (cp_parser* parser)
7045 {
7046   cp_token *token;
7047
7048   /* The next token should be an identifier.  */
7049   token = cp_lexer_peek_token (parser->lexer);
7050   if (token->type != CPP_NAME
7051       && token->type != CPP_KEYWORD)
7052     {
7053       cp_parser_error (parser, "expected labeled-statement");
7054       return;
7055     }
7056
7057   switch (token->keyword)
7058     {
7059     case RID_CASE:
7060       {
7061         tree expr, expr_hi;
7062         cp_token *ellipsis;
7063
7064         /* Consume the `case' token.  */
7065         cp_lexer_consume_token (parser->lexer);
7066         /* Parse the constant-expression.  */
7067         expr = cp_parser_constant_expression (parser,
7068                                               /*allow_non_constant_p=*/false,
7069                                               NULL);
7070
7071         ellipsis = cp_lexer_peek_token (parser->lexer);
7072         if (ellipsis->type == CPP_ELLIPSIS)
7073           {
7074             /* Consume the `...' token.  */
7075             cp_lexer_consume_token (parser->lexer);
7076             expr_hi =
7077               cp_parser_constant_expression (parser,
7078                                              /*allow_non_constant_p=*/false,
7079                                              NULL);
7080             /* We don't need to emit warnings here, as the common code
7081                will do this for us.  */
7082           }
7083         else
7084           expr_hi = NULL_TREE;
7085
7086         if (parser->in_switch_statement_p)
7087           finish_case_label (expr, expr_hi);
7088         else
7089           error ("%Hcase label %qE not within a switch statement",
7090                  &token->location, expr);
7091       }
7092       break;
7093
7094     case RID_DEFAULT:
7095       /* Consume the `default' token.  */
7096       cp_lexer_consume_token (parser->lexer);
7097
7098       if (parser->in_switch_statement_p)
7099         finish_case_label (NULL_TREE, NULL_TREE);
7100       else
7101         error ("%Hcase label not within a switch statement", &token->location);
7102       break;
7103
7104     default:
7105       /* Anything else must be an ordinary label.  */
7106       finish_label_stmt (cp_parser_identifier (parser));
7107       break;
7108     }
7109
7110   /* Require the `:' token.  */
7111   cp_parser_require (parser, CPP_COLON, "%<:%>");
7112 }
7113
7114 /* Parse an expression-statement.
7115
7116    expression-statement:
7117      expression [opt] ;
7118
7119    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7120    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7121    indicates whether this expression-statement is part of an
7122    expression statement.  */
7123
7124 static tree
7125 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7126 {
7127   tree statement = NULL_TREE;
7128
7129   /* If the next token is a ';', then there is no expression
7130      statement.  */
7131   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7132     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7133
7134   /* Consume the final `;'.  */
7135   cp_parser_consume_semicolon_at_end_of_statement (parser);
7136
7137   if (in_statement_expr
7138       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7139     /* This is the final expression statement of a statement
7140        expression.  */
7141     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7142   else if (statement)
7143     statement = finish_expr_stmt (statement);
7144   else
7145     finish_stmt ();
7146
7147   return statement;
7148 }
7149
7150 /* Parse a compound-statement.
7151
7152    compound-statement:
7153      { statement-seq [opt] }
7154
7155    GNU extension:
7156
7157    compound-statement:
7158      { label-declaration-seq [opt] statement-seq [opt] }
7159
7160    label-declaration-seq:
7161      label-declaration
7162      label-declaration-seq label-declaration
7163
7164    Returns a tree representing the statement.  */
7165
7166 static tree
7167 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7168                               bool in_try)
7169 {
7170   tree compound_stmt;
7171
7172   /* Consume the `{'.  */
7173   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7174     return error_mark_node;
7175   /* Begin the compound-statement.  */
7176   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7177   /* If the next keyword is `__label__' we have a label declaration.  */
7178   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7179     cp_parser_label_declaration (parser);
7180   /* Parse an (optional) statement-seq.  */
7181   cp_parser_statement_seq_opt (parser, in_statement_expr);
7182   /* Finish the compound-statement.  */
7183   finish_compound_stmt (compound_stmt);
7184   /* Consume the `}'.  */
7185   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7186
7187   return compound_stmt;
7188 }
7189
7190 /* Parse an (optional) statement-seq.
7191
7192    statement-seq:
7193      statement
7194      statement-seq [opt] statement  */
7195
7196 static void
7197 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7198 {
7199   /* Scan statements until there aren't any more.  */
7200   while (true)
7201     {
7202       cp_token *token = cp_lexer_peek_token (parser->lexer);
7203
7204       /* If we're looking at a `}', then we've run out of statements.  */
7205       if (token->type == CPP_CLOSE_BRACE
7206           || token->type == CPP_EOF
7207           || token->type == CPP_PRAGMA_EOL)
7208         break;
7209       
7210       /* If we are in a compound statement and find 'else' then
7211          something went wrong.  */
7212       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7213         {
7214           if (parser->in_statement & IN_IF_STMT) 
7215             break;
7216           else
7217             {
7218               token = cp_lexer_consume_token (parser->lexer);
7219               error ("%H%<else%> without a previous %<if%>", &token->location);
7220             }
7221         }
7222
7223       /* Parse the statement.  */
7224       cp_parser_statement (parser, in_statement_expr, true, NULL);
7225     }
7226 }
7227
7228 /* Parse a selection-statement.
7229
7230    selection-statement:
7231      if ( condition ) statement
7232      if ( condition ) statement else statement
7233      switch ( condition ) statement
7234
7235    Returns the new IF_STMT or SWITCH_STMT.
7236
7237    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7238    is a (possibly labeled) if statement which is not enclosed in
7239    braces and has an else clause.  This is used to implement
7240    -Wparentheses.  */
7241
7242 static tree
7243 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7244 {
7245   cp_token *token;
7246   enum rid keyword;
7247
7248   if (if_p != NULL)
7249     *if_p = false;
7250
7251   /* Peek at the next token.  */
7252   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7253
7254   /* See what kind of keyword it is.  */
7255   keyword = token->keyword;
7256   switch (keyword)
7257     {
7258     case RID_IF:
7259     case RID_SWITCH:
7260       {
7261         tree statement;
7262         tree condition;
7263
7264         /* Look for the `('.  */
7265         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7266           {
7267             cp_parser_skip_to_end_of_statement (parser);
7268             return error_mark_node;
7269           }
7270
7271         /* Begin the selection-statement.  */
7272         if (keyword == RID_IF)
7273           statement = begin_if_stmt ();
7274         else
7275           statement = begin_switch_stmt ();
7276
7277         /* Parse the condition.  */
7278         condition = cp_parser_condition (parser);
7279         /* Look for the `)'.  */
7280         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7281           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7282                                                  /*consume_paren=*/true);
7283
7284         if (keyword == RID_IF)
7285           {
7286             bool nested_if;
7287             unsigned char in_statement;
7288
7289             /* Add the condition.  */
7290             finish_if_stmt_cond (condition, statement);
7291
7292             /* Parse the then-clause.  */
7293             in_statement = parser->in_statement;
7294             parser->in_statement |= IN_IF_STMT;
7295             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7296               {
7297                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7298                 add_stmt (build_empty_stmt ());
7299                 cp_lexer_consume_token (parser->lexer);
7300                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7301                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7302                               "empty body in an %<if%> statement");
7303                 nested_if = false;
7304               }
7305             else
7306               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7307             parser->in_statement = in_statement;
7308
7309             finish_then_clause (statement);
7310
7311             /* If the next token is `else', parse the else-clause.  */
7312             if (cp_lexer_next_token_is_keyword (parser->lexer,
7313                                                 RID_ELSE))
7314               {
7315                 /* Consume the `else' keyword.  */
7316                 cp_lexer_consume_token (parser->lexer);
7317                 begin_else_clause (statement);
7318                 /* Parse the else-clause.  */
7319                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7320                   {
7321                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7322                                 OPT_Wempty_body, "suggest braces around "
7323                                 "empty body in an %<else%> statement");
7324                     add_stmt (build_empty_stmt ());
7325                     cp_lexer_consume_token (parser->lexer);
7326                   }
7327                 else
7328                   cp_parser_implicitly_scoped_statement (parser, NULL);
7329
7330                 finish_else_clause (statement);
7331
7332                 /* If we are currently parsing a then-clause, then
7333                    IF_P will not be NULL.  We set it to true to
7334                    indicate that this if statement has an else clause.
7335                    This may trigger the Wparentheses warning below
7336                    when we get back up to the parent if statement.  */
7337                 if (if_p != NULL)
7338                   *if_p = true;
7339               }
7340             else
7341               {
7342                 /* This if statement does not have an else clause.  If
7343                    NESTED_IF is true, then the then-clause is an if
7344                    statement which does have an else clause.  We warn
7345                    about the potential ambiguity.  */
7346                 if (nested_if)
7347                   warning (OPT_Wparentheses,
7348                            ("%Hsuggest explicit braces "
7349                             "to avoid ambiguous %<else%>"),
7350                            EXPR_LOCUS (statement));
7351               }
7352
7353             /* Now we're all done with the if-statement.  */
7354             finish_if_stmt (statement);
7355           }
7356         else
7357           {
7358             bool in_switch_statement_p;
7359             unsigned char in_statement;
7360
7361             /* Add the condition.  */
7362             finish_switch_cond (condition, statement);
7363
7364             /* Parse the body of the switch-statement.  */
7365             in_switch_statement_p = parser->in_switch_statement_p;
7366             in_statement = parser->in_statement;
7367             parser->in_switch_statement_p = true;
7368             parser->in_statement |= IN_SWITCH_STMT;
7369             cp_parser_implicitly_scoped_statement (parser, NULL);
7370             parser->in_switch_statement_p = in_switch_statement_p;
7371             parser->in_statement = in_statement;
7372
7373             /* Now we're all done with the switch-statement.  */
7374             finish_switch_stmt (statement);
7375           }
7376
7377         return statement;
7378       }
7379       break;
7380
7381     default:
7382       cp_parser_error (parser, "expected selection-statement");
7383       return error_mark_node;
7384     }
7385 }
7386
7387 /* Parse a condition.
7388
7389    condition:
7390      expression
7391      type-specifier-seq declarator = initializer-clause
7392      type-specifier-seq declarator braced-init-list
7393
7394    GNU Extension:
7395
7396    condition:
7397      type-specifier-seq declarator asm-specification [opt]
7398        attributes [opt] = assignment-expression
7399
7400    Returns the expression that should be tested.  */
7401
7402 static tree
7403 cp_parser_condition (cp_parser* parser)
7404 {
7405   cp_decl_specifier_seq type_specifiers;
7406   const char *saved_message;
7407
7408   /* Try the declaration first.  */
7409   cp_parser_parse_tentatively (parser);
7410   /* New types are not allowed in the type-specifier-seq for a
7411      condition.  */
7412   saved_message = parser->type_definition_forbidden_message;
7413   parser->type_definition_forbidden_message
7414     = "types may not be defined in conditions";
7415   /* Parse the type-specifier-seq.  */
7416   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7417                                 &type_specifiers);
7418   /* Restore the saved message.  */
7419   parser->type_definition_forbidden_message = saved_message;
7420   /* If all is well, we might be looking at a declaration.  */
7421   if (!cp_parser_error_occurred (parser))
7422     {
7423       tree decl;
7424       tree asm_specification;
7425       tree attributes;
7426       cp_declarator *declarator;
7427       tree initializer = NULL_TREE;
7428
7429       /* Parse the declarator.  */
7430       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7431                                          /*ctor_dtor_or_conv_p=*/NULL,
7432                                          /*parenthesized_p=*/NULL,
7433                                          /*member_p=*/false);
7434       /* Parse the attributes.  */
7435       attributes = cp_parser_attributes_opt (parser);
7436       /* Parse the asm-specification.  */
7437       asm_specification = cp_parser_asm_specification_opt (parser);
7438       /* If the next token is not an `=' or '{', then we might still be
7439          looking at an expression.  For example:
7440
7441            if (A(a).x)
7442
7443          looks like a decl-specifier-seq and a declarator -- but then
7444          there is no `=', so this is an expression.  */
7445       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7446           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7447         cp_parser_simulate_error (parser);
7448         
7449       /* If we did see an `=' or '{', then we are looking at a declaration
7450          for sure.  */
7451       if (cp_parser_parse_definitely (parser))
7452         {
7453           tree pushed_scope;
7454           bool non_constant_p;
7455           bool flags = LOOKUP_ONLYCONVERTING;
7456
7457           /* Create the declaration.  */
7458           decl = start_decl (declarator, &type_specifiers,
7459                              /*initialized_p=*/true,
7460                              attributes, /*prefix_attributes=*/NULL_TREE,
7461                              &pushed_scope);
7462
7463           /* Parse the initializer.  */
7464           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7465             {
7466               initializer = cp_parser_braced_list (parser, &non_constant_p);
7467               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7468               flags = 0;
7469             }
7470           else
7471             {
7472               /* Consume the `='.  */
7473               cp_parser_require (parser, CPP_EQ, "%<=%>");
7474               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7475             }
7476           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7477             maybe_warn_cpp0x ("extended initializer lists");
7478
7479           if (!non_constant_p)
7480             initializer = fold_non_dependent_expr (initializer);
7481
7482           /* Process the initializer.  */
7483           cp_finish_decl (decl,
7484                           initializer, !non_constant_p,
7485                           asm_specification,
7486                           flags);
7487
7488           if (pushed_scope)
7489             pop_scope (pushed_scope);
7490
7491           return convert_from_reference (decl);
7492         }
7493     }
7494   /* If we didn't even get past the declarator successfully, we are
7495      definitely not looking at a declaration.  */
7496   else
7497     cp_parser_abort_tentative_parse (parser);
7498
7499   /* Otherwise, we are looking at an expression.  */
7500   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7501 }
7502
7503 /* Parse an iteration-statement.
7504
7505    iteration-statement:
7506      while ( condition ) statement
7507      do statement while ( expression ) ;
7508      for ( for-init-statement condition [opt] ; expression [opt] )
7509        statement
7510
7511    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7512
7513 static tree
7514 cp_parser_iteration_statement (cp_parser* parser)
7515 {
7516   cp_token *token;
7517   enum rid keyword;
7518   tree statement;
7519   unsigned char in_statement;
7520
7521   /* Peek at the next token.  */
7522   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7523   if (!token)
7524     return error_mark_node;
7525
7526   /* Remember whether or not we are already within an iteration
7527      statement.  */
7528   in_statement = parser->in_statement;
7529
7530   /* See what kind of keyword it is.  */
7531   keyword = token->keyword;
7532   switch (keyword)
7533     {
7534     case RID_WHILE:
7535       {
7536         tree condition;
7537
7538         /* Begin the while-statement.  */
7539         statement = begin_while_stmt ();
7540         /* Look for the `('.  */
7541         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7542         /* Parse the condition.  */
7543         condition = cp_parser_condition (parser);
7544         finish_while_stmt_cond (condition, statement);
7545         /* Look for the `)'.  */
7546         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7547         /* Parse the dependent statement.  */
7548         parser->in_statement = IN_ITERATION_STMT;
7549         cp_parser_already_scoped_statement (parser);
7550         parser->in_statement = in_statement;
7551         /* We're done with the while-statement.  */
7552         finish_while_stmt (statement);
7553       }
7554       break;
7555
7556     case RID_DO:
7557       {
7558         tree expression;
7559
7560         /* Begin the do-statement.  */
7561         statement = begin_do_stmt ();
7562         /* Parse the body of the do-statement.  */
7563         parser->in_statement = IN_ITERATION_STMT;
7564         cp_parser_implicitly_scoped_statement (parser, NULL);
7565         parser->in_statement = in_statement;
7566         finish_do_body (statement);
7567         /* Look for the `while' keyword.  */
7568         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7569         /* Look for the `('.  */
7570         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7571         /* Parse the expression.  */
7572         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7573         /* We're done with the do-statement.  */
7574         finish_do_stmt (expression, statement);
7575         /* Look for the `)'.  */
7576         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7577         /* Look for the `;'.  */
7578         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7579       }
7580       break;
7581
7582     case RID_FOR:
7583       {
7584         tree condition = NULL_TREE;
7585         tree expression = NULL_TREE;
7586
7587         /* Begin the for-statement.  */
7588         statement = begin_for_stmt ();
7589         /* Look for the `('.  */
7590         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7591         /* Parse the initialization.  */
7592         cp_parser_for_init_statement (parser);
7593         finish_for_init_stmt (statement);
7594
7595         /* If there's a condition, process it.  */
7596         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7597           condition = cp_parser_condition (parser);
7598         finish_for_cond (condition, statement);
7599         /* Look for the `;'.  */
7600         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7601
7602         /* If there's an expression, process it.  */
7603         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7604           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7605         finish_for_expr (expression, statement);
7606         /* Look for the `)'.  */
7607         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7608
7609         /* Parse the body of the for-statement.  */
7610         parser->in_statement = IN_ITERATION_STMT;
7611         cp_parser_already_scoped_statement (parser);
7612         parser->in_statement = in_statement;
7613
7614         /* We're done with the for-statement.  */
7615         finish_for_stmt (statement);
7616       }
7617       break;
7618
7619     default:
7620       cp_parser_error (parser, "expected iteration-statement");
7621       statement = error_mark_node;
7622       break;
7623     }
7624
7625   return statement;
7626 }
7627
7628 /* Parse a for-init-statement.
7629
7630    for-init-statement:
7631      expression-statement
7632      simple-declaration  */
7633
7634 static void
7635 cp_parser_for_init_statement (cp_parser* parser)
7636 {
7637   /* If the next token is a `;', then we have an empty
7638      expression-statement.  Grammatically, this is also a
7639      simple-declaration, but an invalid one, because it does not
7640      declare anything.  Therefore, if we did not handle this case
7641      specially, we would issue an error message about an invalid
7642      declaration.  */
7643   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7644     {
7645       /* We're going to speculatively look for a declaration, falling back
7646          to an expression, if necessary.  */
7647       cp_parser_parse_tentatively (parser);
7648       /* Parse the declaration.  */
7649       cp_parser_simple_declaration (parser,
7650                                     /*function_definition_allowed_p=*/false);
7651       /* If the tentative parse failed, then we shall need to look for an
7652          expression-statement.  */
7653       if (cp_parser_parse_definitely (parser))
7654         return;
7655     }
7656
7657   cp_parser_expression_statement (parser, false);
7658 }
7659
7660 /* Parse a jump-statement.
7661
7662    jump-statement:
7663      break ;
7664      continue ;
7665      return expression [opt] ;
7666      return braced-init-list ;
7667      goto identifier ;
7668
7669    GNU extension:
7670
7671    jump-statement:
7672      goto * expression ;
7673
7674    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7675
7676 static tree
7677 cp_parser_jump_statement (cp_parser* parser)
7678 {
7679   tree statement = error_mark_node;
7680   cp_token *token;
7681   enum rid keyword;
7682   unsigned char in_statement;
7683
7684   /* Peek at the next token.  */
7685   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7686   if (!token)
7687     return error_mark_node;
7688
7689   /* See what kind of keyword it is.  */
7690   keyword = token->keyword;
7691   switch (keyword)
7692     {
7693     case RID_BREAK:
7694       in_statement = parser->in_statement & ~IN_IF_STMT;      
7695       switch (in_statement)
7696         {
7697         case 0:
7698           error ("%Hbreak statement not within loop or switch", &token->location);
7699           break;
7700         default:
7701           gcc_assert ((in_statement & IN_SWITCH_STMT)
7702                       || in_statement == IN_ITERATION_STMT);
7703           statement = finish_break_stmt ();
7704           break;
7705         case IN_OMP_BLOCK:
7706           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7707           break;
7708         case IN_OMP_FOR:
7709           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7710           break;
7711         }
7712       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7713       break;
7714
7715     case RID_CONTINUE:
7716       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7717         {
7718         case 0:
7719           error ("%Hcontinue statement not within a loop", &token->location);
7720           break;
7721         case IN_ITERATION_STMT:
7722         case IN_OMP_FOR:
7723           statement = finish_continue_stmt ();
7724           break;
7725         case IN_OMP_BLOCK:
7726           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7727           break;
7728         default:
7729           gcc_unreachable ();
7730         }
7731       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7732       break;
7733
7734     case RID_RETURN:
7735       {
7736         tree expr;
7737         bool expr_non_constant_p;
7738
7739         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7740           {
7741             maybe_warn_cpp0x ("extended initializer lists");
7742             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7743           }
7744         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7746         else
7747           /* If the next token is a `;', then there is no
7748              expression.  */
7749           expr = NULL_TREE;
7750         /* Build the return-statement.  */
7751         statement = finish_return_stmt (expr);
7752         /* Look for the final `;'.  */
7753         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7754       }
7755       break;
7756
7757     case RID_GOTO:
7758       /* Create the goto-statement.  */
7759       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7760         {
7761           /* Issue a warning about this use of a GNU extension.  */
7762           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7763           /* Consume the '*' token.  */
7764           cp_lexer_consume_token (parser->lexer);
7765           /* Parse the dependent expression.  */
7766           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7767         }
7768       else
7769         finish_goto_stmt (cp_parser_identifier (parser));
7770       /* Look for the final `;'.  */
7771       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7772       break;
7773
7774     default:
7775       cp_parser_error (parser, "expected jump-statement");
7776       break;
7777     }
7778
7779   return statement;
7780 }
7781
7782 /* Parse a declaration-statement.
7783
7784    declaration-statement:
7785      block-declaration  */
7786
7787 static void
7788 cp_parser_declaration_statement (cp_parser* parser)
7789 {
7790   void *p;
7791
7792   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7793   p = obstack_alloc (&declarator_obstack, 0);
7794
7795  /* Parse the block-declaration.  */
7796   cp_parser_block_declaration (parser, /*statement_p=*/true);
7797
7798   /* Free any declarators allocated.  */
7799   obstack_free (&declarator_obstack, p);
7800
7801   /* Finish off the statement.  */
7802   finish_stmt ();
7803 }
7804
7805 /* Some dependent statements (like `if (cond) statement'), are
7806    implicitly in their own scope.  In other words, if the statement is
7807    a single statement (as opposed to a compound-statement), it is
7808    none-the-less treated as if it were enclosed in braces.  Any
7809    declarations appearing in the dependent statement are out of scope
7810    after control passes that point.  This function parses a statement,
7811    but ensures that is in its own scope, even if it is not a
7812    compound-statement.
7813
7814    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7815    is a (possibly labeled) if statement which is not enclosed in
7816    braces and has an else clause.  This is used to implement
7817    -Wparentheses.
7818
7819    Returns the new statement.  */
7820
7821 static tree
7822 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7823 {
7824   tree statement;
7825
7826   if (if_p != NULL)
7827     *if_p = false;
7828
7829   /* Mark if () ; with a special NOP_EXPR.  */
7830   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7831     {
7832       cp_lexer_consume_token (parser->lexer);
7833       statement = add_stmt (build_empty_stmt ());
7834     }
7835   /* if a compound is opened, we simply parse the statement directly.  */
7836   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7837     statement = cp_parser_compound_statement (parser, NULL, false);
7838   /* If the token is not a `{', then we must take special action.  */
7839   else
7840     {
7841       /* Create a compound-statement.  */
7842       statement = begin_compound_stmt (0);
7843       /* Parse the dependent-statement.  */
7844       cp_parser_statement (parser, NULL_TREE, false, if_p);
7845       /* Finish the dummy compound-statement.  */
7846       finish_compound_stmt (statement);
7847     }
7848
7849   /* Return the statement.  */
7850   return statement;
7851 }
7852
7853 /* For some dependent statements (like `while (cond) statement'), we
7854    have already created a scope.  Therefore, even if the dependent
7855    statement is a compound-statement, we do not want to create another
7856    scope.  */
7857
7858 static void
7859 cp_parser_already_scoped_statement (cp_parser* parser)
7860 {
7861   /* If the token is a `{', then we must take special action.  */
7862   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7863     cp_parser_statement (parser, NULL_TREE, false, NULL);
7864   else
7865     {
7866       /* Avoid calling cp_parser_compound_statement, so that we
7867          don't create a new scope.  Do everything else by hand.  */
7868       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7869       /* If the next keyword is `__label__' we have a label declaration.  */
7870       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7871         cp_parser_label_declaration (parser);
7872       /* Parse an (optional) statement-seq.  */
7873       cp_parser_statement_seq_opt (parser, NULL_TREE);
7874       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7875     }
7876 }
7877
7878 /* Declarations [gram.dcl.dcl] */
7879
7880 /* Parse an optional declaration-sequence.
7881
7882    declaration-seq:
7883      declaration
7884      declaration-seq declaration  */
7885
7886 static void
7887 cp_parser_declaration_seq_opt (cp_parser* parser)
7888 {
7889   while (true)
7890     {
7891       cp_token *token;
7892
7893       token = cp_lexer_peek_token (parser->lexer);
7894
7895       if (token->type == CPP_CLOSE_BRACE
7896           || token->type == CPP_EOF
7897           || token->type == CPP_PRAGMA_EOL)
7898         break;
7899
7900       if (token->type == CPP_SEMICOLON)
7901         {
7902           /* A declaration consisting of a single semicolon is
7903              invalid.  Allow it unless we're being pedantic.  */
7904           cp_lexer_consume_token (parser->lexer);
7905           if (!in_system_header)
7906             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7907           continue;
7908         }
7909
7910       /* If we're entering or exiting a region that's implicitly
7911          extern "C", modify the lang context appropriately.  */
7912       if (!parser->implicit_extern_c && token->implicit_extern_c)
7913         {
7914           push_lang_context (lang_name_c);
7915           parser->implicit_extern_c = true;
7916         }
7917       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7918         {
7919           pop_lang_context ();
7920           parser->implicit_extern_c = false;
7921         }
7922
7923       if (token->type == CPP_PRAGMA)
7924         {
7925           /* A top-level declaration can consist solely of a #pragma.
7926              A nested declaration cannot, so this is done here and not
7927              in cp_parser_declaration.  (A #pragma at block scope is
7928              handled in cp_parser_statement.)  */
7929           cp_parser_pragma (parser, pragma_external);
7930           continue;
7931         }
7932
7933       /* Parse the declaration itself.  */
7934       cp_parser_declaration (parser);
7935     }
7936 }
7937
7938 /* Parse a declaration.
7939
7940    declaration:
7941      block-declaration
7942      function-definition
7943      template-declaration
7944      explicit-instantiation
7945      explicit-specialization
7946      linkage-specification
7947      namespace-definition
7948
7949    GNU extension:
7950
7951    declaration:
7952       __extension__ declaration */
7953
7954 static void
7955 cp_parser_declaration (cp_parser* parser)
7956 {
7957   cp_token token1;
7958   cp_token token2;
7959   int saved_pedantic;
7960   void *p;
7961
7962   /* Check for the `__extension__' keyword.  */
7963   if (cp_parser_extension_opt (parser, &saved_pedantic))
7964     {
7965       /* Parse the qualified declaration.  */
7966       cp_parser_declaration (parser);
7967       /* Restore the PEDANTIC flag.  */
7968       pedantic = saved_pedantic;
7969
7970       return;
7971     }
7972
7973   /* Try to figure out what kind of declaration is present.  */
7974   token1 = *cp_lexer_peek_token (parser->lexer);
7975
7976   if (token1.type != CPP_EOF)
7977     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7978   else
7979     {
7980       token2.type = CPP_EOF;
7981       token2.keyword = RID_MAX;
7982     }
7983
7984   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7985   p = obstack_alloc (&declarator_obstack, 0);
7986
7987   /* If the next token is `extern' and the following token is a string
7988      literal, then we have a linkage specification.  */
7989   if (token1.keyword == RID_EXTERN
7990       && cp_parser_is_string_literal (&token2))
7991     cp_parser_linkage_specification (parser);
7992   /* If the next token is `template', then we have either a template
7993      declaration, an explicit instantiation, or an explicit
7994      specialization.  */
7995   else if (token1.keyword == RID_TEMPLATE)
7996     {
7997       /* `template <>' indicates a template specialization.  */
7998       if (token2.type == CPP_LESS
7999           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8000         cp_parser_explicit_specialization (parser);
8001       /* `template <' indicates a template declaration.  */
8002       else if (token2.type == CPP_LESS)
8003         cp_parser_template_declaration (parser, /*member_p=*/false);
8004       /* Anything else must be an explicit instantiation.  */
8005       else
8006         cp_parser_explicit_instantiation (parser);
8007     }
8008   /* If the next token is `export', then we have a template
8009      declaration.  */
8010   else if (token1.keyword == RID_EXPORT)
8011     cp_parser_template_declaration (parser, /*member_p=*/false);
8012   /* If the next token is `extern', 'static' or 'inline' and the one
8013      after that is `template', we have a GNU extended explicit
8014      instantiation directive.  */
8015   else if (cp_parser_allow_gnu_extensions_p (parser)
8016            && (token1.keyword == RID_EXTERN
8017                || token1.keyword == RID_STATIC
8018                || token1.keyword == RID_INLINE)
8019            && token2.keyword == RID_TEMPLATE)
8020     cp_parser_explicit_instantiation (parser);
8021   /* If the next token is `namespace', check for a named or unnamed
8022      namespace definition.  */
8023   else if (token1.keyword == RID_NAMESPACE
8024            && (/* A named namespace definition.  */
8025                (token2.type == CPP_NAME
8026                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8027                     != CPP_EQ))
8028                /* An unnamed namespace definition.  */
8029                || token2.type == CPP_OPEN_BRACE
8030                || token2.keyword == RID_ATTRIBUTE))
8031     cp_parser_namespace_definition (parser);
8032   /* An inline (associated) namespace definition.  */
8033   else if (token1.keyword == RID_INLINE
8034            && token2.keyword == RID_NAMESPACE)
8035     cp_parser_namespace_definition (parser);
8036   /* Objective-C++ declaration/definition.  */
8037   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8038     cp_parser_objc_declaration (parser);
8039   /* We must have either a block declaration or a function
8040      definition.  */
8041   else
8042     /* Try to parse a block-declaration, or a function-definition.  */
8043     cp_parser_block_declaration (parser, /*statement_p=*/false);
8044
8045   /* Free any declarators allocated.  */
8046   obstack_free (&declarator_obstack, p);
8047 }
8048
8049 /* Parse a block-declaration.
8050
8051    block-declaration:
8052      simple-declaration
8053      asm-definition
8054      namespace-alias-definition
8055      using-declaration
8056      using-directive
8057
8058    GNU Extension:
8059
8060    block-declaration:
8061      __extension__ block-declaration
8062
8063    C++0x Extension:
8064
8065    block-declaration:
8066      static_assert-declaration
8067
8068    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8069    part of a declaration-statement.  */
8070
8071 static void
8072 cp_parser_block_declaration (cp_parser *parser,
8073                              bool      statement_p)
8074 {
8075   cp_token *token1;
8076   int saved_pedantic;
8077
8078   /* Check for the `__extension__' keyword.  */
8079   if (cp_parser_extension_opt (parser, &saved_pedantic))
8080     {
8081       /* Parse the qualified declaration.  */
8082       cp_parser_block_declaration (parser, statement_p);
8083       /* Restore the PEDANTIC flag.  */
8084       pedantic = saved_pedantic;
8085
8086       return;
8087     }
8088
8089   /* Peek at the next token to figure out which kind of declaration is
8090      present.  */
8091   token1 = cp_lexer_peek_token (parser->lexer);
8092
8093   /* If the next keyword is `asm', we have an asm-definition.  */
8094   if (token1->keyword == RID_ASM)
8095     {
8096       if (statement_p)
8097         cp_parser_commit_to_tentative_parse (parser);
8098       cp_parser_asm_definition (parser);
8099     }
8100   /* If the next keyword is `namespace', we have a
8101      namespace-alias-definition.  */
8102   else if (token1->keyword == RID_NAMESPACE)
8103     cp_parser_namespace_alias_definition (parser);
8104   /* If the next keyword is `using', we have either a
8105      using-declaration or a using-directive.  */
8106   else if (token1->keyword == RID_USING)
8107     {
8108       cp_token *token2;
8109
8110       if (statement_p)
8111         cp_parser_commit_to_tentative_parse (parser);
8112       /* If the token after `using' is `namespace', then we have a
8113          using-directive.  */
8114       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8115       if (token2->keyword == RID_NAMESPACE)
8116         cp_parser_using_directive (parser);
8117       /* Otherwise, it's a using-declaration.  */
8118       else
8119         cp_parser_using_declaration (parser,
8120                                      /*access_declaration_p=*/false);
8121     }
8122   /* If the next keyword is `__label__' we have a misplaced label
8123      declaration.  */
8124   else if (token1->keyword == RID_LABEL)
8125     {
8126       cp_lexer_consume_token (parser->lexer);
8127       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8128       cp_parser_skip_to_end_of_statement (parser);
8129       /* If the next token is now a `;', consume it.  */
8130       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8131         cp_lexer_consume_token (parser->lexer);
8132     }
8133   /* If the next token is `static_assert' we have a static assertion.  */
8134   else if (token1->keyword == RID_STATIC_ASSERT)
8135     cp_parser_static_assert (parser, /*member_p=*/false);
8136   /* Anything else must be a simple-declaration.  */
8137   else
8138     cp_parser_simple_declaration (parser, !statement_p);
8139 }
8140
8141 /* Parse a simple-declaration.
8142
8143    simple-declaration:
8144      decl-specifier-seq [opt] init-declarator-list [opt] ;
8145
8146    init-declarator-list:
8147      init-declarator
8148      init-declarator-list , init-declarator
8149
8150    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8151    function-definition as a simple-declaration.  */
8152
8153 static void
8154 cp_parser_simple_declaration (cp_parser* parser,
8155                               bool function_definition_allowed_p)
8156 {
8157   cp_decl_specifier_seq decl_specifiers;
8158   int declares_class_or_enum;
8159   bool saw_declarator;
8160
8161   /* Defer access checks until we know what is being declared; the
8162      checks for names appearing in the decl-specifier-seq should be
8163      done as if we were in the scope of the thing being declared.  */
8164   push_deferring_access_checks (dk_deferred);
8165
8166   /* Parse the decl-specifier-seq.  We have to keep track of whether
8167      or not the decl-specifier-seq declares a named class or
8168      enumeration type, since that is the only case in which the
8169      init-declarator-list is allowed to be empty.
8170
8171      [dcl.dcl]
8172
8173      In a simple-declaration, the optional init-declarator-list can be
8174      omitted only when declaring a class or enumeration, that is when
8175      the decl-specifier-seq contains either a class-specifier, an
8176      elaborated-type-specifier, or an enum-specifier.  */
8177   cp_parser_decl_specifier_seq (parser,
8178                                 CP_PARSER_FLAGS_OPTIONAL,
8179                                 &decl_specifiers,
8180                                 &declares_class_or_enum);
8181   /* We no longer need to defer access checks.  */
8182   stop_deferring_access_checks ();
8183
8184   /* In a block scope, a valid declaration must always have a
8185      decl-specifier-seq.  By not trying to parse declarators, we can
8186      resolve the declaration/expression ambiguity more quickly.  */
8187   if (!function_definition_allowed_p
8188       && !decl_specifiers.any_specifiers_p)
8189     {
8190       cp_parser_error (parser, "expected declaration");
8191       goto done;
8192     }
8193
8194   /* If the next two tokens are both identifiers, the code is
8195      erroneous. The usual cause of this situation is code like:
8196
8197        T t;
8198
8199      where "T" should name a type -- but does not.  */
8200   if (!decl_specifiers.type
8201       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8202     {
8203       /* If parsing tentatively, we should commit; we really are
8204          looking at a declaration.  */
8205       cp_parser_commit_to_tentative_parse (parser);
8206       /* Give up.  */
8207       goto done;
8208     }
8209
8210   /* If we have seen at least one decl-specifier, and the next token
8211      is not a parenthesis, then we must be looking at a declaration.
8212      (After "int (" we might be looking at a functional cast.)  */
8213   if (decl_specifiers.any_specifiers_p
8214       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8215       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8216       && !cp_parser_error_occurred (parser))
8217     cp_parser_commit_to_tentative_parse (parser);
8218
8219   /* Keep going until we hit the `;' at the end of the simple
8220      declaration.  */
8221   saw_declarator = false;
8222   while (cp_lexer_next_token_is_not (parser->lexer,
8223                                      CPP_SEMICOLON))
8224     {
8225       cp_token *token;
8226       bool function_definition_p;
8227       tree decl;
8228
8229       if (saw_declarator)
8230         {
8231           /* If we are processing next declarator, coma is expected */
8232           token = cp_lexer_peek_token (parser->lexer);
8233           gcc_assert (token->type == CPP_COMMA);
8234           cp_lexer_consume_token (parser->lexer);
8235         }
8236       else
8237         saw_declarator = true;
8238
8239       /* Parse the init-declarator.  */
8240       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8241                                         /*checks=*/NULL,
8242                                         function_definition_allowed_p,
8243                                         /*member_p=*/false,
8244                                         declares_class_or_enum,
8245                                         &function_definition_p);
8246       /* If an error occurred while parsing tentatively, exit quickly.
8247          (That usually happens when in the body of a function; each
8248          statement is treated as a declaration-statement until proven
8249          otherwise.)  */
8250       if (cp_parser_error_occurred (parser))
8251         goto done;
8252       /* Handle function definitions specially.  */
8253       if (function_definition_p)
8254         {
8255           /* If the next token is a `,', then we are probably
8256              processing something like:
8257
8258                void f() {}, *p;
8259
8260              which is erroneous.  */
8261           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8262             {
8263               cp_token *token = cp_lexer_peek_token (parser->lexer);
8264               error ("%Hmixing declarations and function-definitions is forbidden",
8265                      &token->location);
8266             }
8267           /* Otherwise, we're done with the list of declarators.  */
8268           else
8269             {
8270               pop_deferring_access_checks ();
8271               return;
8272             }
8273         }
8274       /* The next token should be either a `,' or a `;'.  */
8275       token = cp_lexer_peek_token (parser->lexer);
8276       /* If it's a `,', there are more declarators to come.  */
8277       if (token->type == CPP_COMMA)
8278         /* will be consumed next time around */;
8279       /* If it's a `;', we are done.  */
8280       else if (token->type == CPP_SEMICOLON)
8281         break;
8282       /* Anything else is an error.  */
8283       else
8284         {
8285           /* If we have already issued an error message we don't need
8286              to issue another one.  */
8287           if (decl != error_mark_node
8288               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8289             cp_parser_error (parser, "expected %<,%> or %<;%>");
8290           /* Skip tokens until we reach the end of the statement.  */
8291           cp_parser_skip_to_end_of_statement (parser);
8292           /* If the next token is now a `;', consume it.  */
8293           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8294             cp_lexer_consume_token (parser->lexer);
8295           goto done;
8296         }
8297       /* After the first time around, a function-definition is not
8298          allowed -- even if it was OK at first.  For example:
8299
8300            int i, f() {}
8301
8302          is not valid.  */
8303       function_definition_allowed_p = false;
8304     }
8305
8306   /* Issue an error message if no declarators are present, and the
8307      decl-specifier-seq does not itself declare a class or
8308      enumeration.  */
8309   if (!saw_declarator)
8310     {
8311       if (cp_parser_declares_only_class_p (parser))
8312         shadow_tag (&decl_specifiers);
8313       /* Perform any deferred access checks.  */
8314       perform_deferred_access_checks ();
8315     }
8316
8317   /* Consume the `;'.  */
8318   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8319
8320  done:
8321   pop_deferring_access_checks ();
8322 }
8323
8324 /* Parse a decl-specifier-seq.
8325
8326    decl-specifier-seq:
8327      decl-specifier-seq [opt] decl-specifier
8328
8329    decl-specifier:
8330      storage-class-specifier
8331      type-specifier
8332      function-specifier
8333      friend
8334      typedef
8335
8336    GNU Extension:
8337
8338    decl-specifier:
8339      attributes
8340
8341    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8342
8343    The parser flags FLAGS is used to control type-specifier parsing.
8344
8345    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8346    flags:
8347
8348      1: one of the decl-specifiers is an elaborated-type-specifier
8349         (i.e., a type declaration)
8350      2: one of the decl-specifiers is an enum-specifier or a
8351         class-specifier (i.e., a type definition)
8352
8353    */
8354
8355 static void
8356 cp_parser_decl_specifier_seq (cp_parser* parser,
8357                               cp_parser_flags flags,
8358                               cp_decl_specifier_seq *decl_specs,
8359                               int* declares_class_or_enum)
8360 {
8361   bool constructor_possible_p = !parser->in_declarator_p;
8362   cp_token *start_token = NULL;
8363
8364   /* Clear DECL_SPECS.  */
8365   clear_decl_specs (decl_specs);
8366
8367   /* Assume no class or enumeration type is declared.  */
8368   *declares_class_or_enum = 0;
8369
8370   /* Keep reading specifiers until there are no more to read.  */
8371   while (true)
8372     {
8373       bool constructor_p;
8374       bool found_decl_spec;
8375       cp_token *token;
8376
8377       /* Peek at the next token.  */
8378       token = cp_lexer_peek_token (parser->lexer);
8379
8380       /* Save the first token of the decl spec list for error
8381          reporting.  */
8382       if (!start_token)
8383         start_token = token;
8384       /* Handle attributes.  */
8385       if (token->keyword == RID_ATTRIBUTE)
8386         {
8387           /* Parse the attributes.  */
8388           decl_specs->attributes
8389             = chainon (decl_specs->attributes,
8390                        cp_parser_attributes_opt (parser));
8391           continue;
8392         }
8393       /* Assume we will find a decl-specifier keyword.  */
8394       found_decl_spec = true;
8395       /* If the next token is an appropriate keyword, we can simply
8396          add it to the list.  */
8397       switch (token->keyword)
8398         {
8399           /* decl-specifier:
8400                friend  */
8401         case RID_FRIEND:
8402           if (!at_class_scope_p ())
8403             {
8404               error ("%H%<friend%> used outside of class", &token->location);
8405               cp_lexer_purge_token (parser->lexer);
8406             }
8407           else
8408             {
8409               ++decl_specs->specs[(int) ds_friend];
8410               /* Consume the token.  */
8411               cp_lexer_consume_token (parser->lexer);
8412             }
8413           break;
8414
8415           /* function-specifier:
8416                inline
8417                virtual
8418                explicit  */
8419         case RID_INLINE:
8420         case RID_VIRTUAL:
8421         case RID_EXPLICIT:
8422           cp_parser_function_specifier_opt (parser, decl_specs);
8423           break;
8424
8425           /* decl-specifier:
8426                typedef  */
8427         case RID_TYPEDEF:
8428           ++decl_specs->specs[(int) ds_typedef];
8429           /* Consume the token.  */
8430           cp_lexer_consume_token (parser->lexer);
8431           /* A constructor declarator cannot appear in a typedef.  */
8432           constructor_possible_p = false;
8433           /* The "typedef" keyword can only occur in a declaration; we
8434              may as well commit at this point.  */
8435           cp_parser_commit_to_tentative_parse (parser);
8436
8437           if (decl_specs->storage_class != sc_none)
8438             decl_specs->conflicting_specifiers_p = true;
8439           break;
8440
8441           /* storage-class-specifier:
8442                auto
8443                register
8444                static
8445                extern
8446                mutable
8447
8448              GNU Extension:
8449                thread  */
8450         case RID_AUTO:
8451           if (cxx_dialect == cxx98) 
8452             {
8453               /* Consume the token.  */
8454               cp_lexer_consume_token (parser->lexer);
8455
8456               /* Complain about `auto' as a storage specifier, if
8457                  we're complaining about C++0x compatibility.  */
8458               warning 
8459                 (OPT_Wc__0x_compat, 
8460                  "%H%<auto%> will change meaning in C++0x; please remove it",
8461                  &token->location);
8462
8463               /* Set the storage class anyway.  */
8464               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8465                                            token->location);
8466             }
8467           else
8468             /* C++0x auto type-specifier.  */
8469             found_decl_spec = false;
8470           break;
8471
8472         case RID_REGISTER:
8473         case RID_STATIC:
8474         case RID_EXTERN:
8475         case RID_MUTABLE:
8476           /* Consume the token.  */
8477           cp_lexer_consume_token (parser->lexer);
8478           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8479                                        token->location);
8480           break;
8481         case RID_THREAD:
8482           /* Consume the token.  */
8483           cp_lexer_consume_token (parser->lexer);
8484           ++decl_specs->specs[(int) ds_thread];
8485           break;
8486
8487         default:
8488           /* We did not yet find a decl-specifier yet.  */
8489           found_decl_spec = false;
8490           break;
8491         }
8492
8493       /* Constructors are a special case.  The `S' in `S()' is not a
8494          decl-specifier; it is the beginning of the declarator.  */
8495       constructor_p
8496         = (!found_decl_spec
8497            && constructor_possible_p
8498            && (cp_parser_constructor_declarator_p
8499                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8500
8501       /* If we don't have a DECL_SPEC yet, then we must be looking at
8502          a type-specifier.  */
8503       if (!found_decl_spec && !constructor_p)
8504         {
8505           int decl_spec_declares_class_or_enum;
8506           bool is_cv_qualifier;
8507           tree type_spec;
8508
8509           type_spec
8510             = cp_parser_type_specifier (parser, flags,
8511                                         decl_specs,
8512                                         /*is_declaration=*/true,
8513                                         &decl_spec_declares_class_or_enum,
8514                                         &is_cv_qualifier);
8515           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8516
8517           /* If this type-specifier referenced a user-defined type
8518              (a typedef, class-name, etc.), then we can't allow any
8519              more such type-specifiers henceforth.
8520
8521              [dcl.spec]
8522
8523              The longest sequence of decl-specifiers that could
8524              possibly be a type name is taken as the
8525              decl-specifier-seq of a declaration.  The sequence shall
8526              be self-consistent as described below.
8527
8528              [dcl.type]
8529
8530              As a general rule, at most one type-specifier is allowed
8531              in the complete decl-specifier-seq of a declaration.  The
8532              only exceptions are the following:
8533
8534              -- const or volatile can be combined with any other
8535                 type-specifier.
8536
8537              -- signed or unsigned can be combined with char, long,
8538                 short, or int.
8539
8540              -- ..
8541
8542              Example:
8543
8544                typedef char* Pc;
8545                void g (const int Pc);
8546
8547              Here, Pc is *not* part of the decl-specifier seq; it's
8548              the declarator.  Therefore, once we see a type-specifier
8549              (other than a cv-qualifier), we forbid any additional
8550              user-defined types.  We *do* still allow things like `int
8551              int' to be considered a decl-specifier-seq, and issue the
8552              error message later.  */
8553           if (type_spec && !is_cv_qualifier)
8554             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8555           /* A constructor declarator cannot follow a type-specifier.  */
8556           if (type_spec)
8557             {
8558               constructor_possible_p = false;
8559               found_decl_spec = true;
8560             }
8561         }
8562
8563       /* If we still do not have a DECL_SPEC, then there are no more
8564          decl-specifiers.  */
8565       if (!found_decl_spec)
8566         break;
8567
8568       decl_specs->any_specifiers_p = true;
8569       /* After we see one decl-specifier, further decl-specifiers are
8570          always optional.  */
8571       flags |= CP_PARSER_FLAGS_OPTIONAL;
8572     }
8573
8574   cp_parser_check_decl_spec (decl_specs, start_token->location);
8575
8576   /* Don't allow a friend specifier with a class definition.  */
8577   if (decl_specs->specs[(int) ds_friend] != 0
8578       && (*declares_class_or_enum & 2))
8579     error ("%Hclass definition may not be declared a friend",
8580             &start_token->location);
8581 }
8582
8583 /* Parse an (optional) storage-class-specifier.
8584
8585    storage-class-specifier:
8586      auto
8587      register
8588      static
8589      extern
8590      mutable
8591
8592    GNU Extension:
8593
8594    storage-class-specifier:
8595      thread
8596
8597    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8598
8599 static tree
8600 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8601 {
8602   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8603     {
8604     case RID_AUTO:
8605       if (cxx_dialect != cxx98)
8606         return NULL_TREE;
8607       /* Fall through for C++98.  */
8608
8609     case RID_REGISTER:
8610     case RID_STATIC:
8611     case RID_EXTERN:
8612     case RID_MUTABLE:
8613     case RID_THREAD:
8614       /* Consume the token.  */
8615       return cp_lexer_consume_token (parser->lexer)->u.value;
8616
8617     default:
8618       return NULL_TREE;
8619     }
8620 }
8621
8622 /* Parse an (optional) function-specifier.
8623
8624    function-specifier:
8625      inline
8626      virtual
8627      explicit
8628
8629    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8630    Updates DECL_SPECS, if it is non-NULL.  */
8631
8632 static tree
8633 cp_parser_function_specifier_opt (cp_parser* parser,
8634                                   cp_decl_specifier_seq *decl_specs)
8635 {
8636   cp_token *token = cp_lexer_peek_token (parser->lexer);
8637   switch (token->keyword)
8638     {
8639     case RID_INLINE:
8640       if (decl_specs)
8641         ++decl_specs->specs[(int) ds_inline];
8642       break;
8643
8644     case RID_VIRTUAL:
8645       /* 14.5.2.3 [temp.mem]
8646
8647          A member function template shall not be virtual.  */
8648       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8649         error ("%Htemplates may not be %<virtual%>", &token->location);
8650       else if (decl_specs)
8651         ++decl_specs->specs[(int) ds_virtual];
8652       break;
8653
8654     case RID_EXPLICIT:
8655       if (decl_specs)
8656         ++decl_specs->specs[(int) ds_explicit];
8657       break;
8658
8659     default:
8660       return NULL_TREE;
8661     }
8662
8663   /* Consume the token.  */
8664   return cp_lexer_consume_token (parser->lexer)->u.value;
8665 }
8666
8667 /* Parse a linkage-specification.
8668
8669    linkage-specification:
8670      extern string-literal { declaration-seq [opt] }
8671      extern string-literal declaration  */
8672
8673 static void
8674 cp_parser_linkage_specification (cp_parser* parser)
8675 {
8676   tree linkage;
8677
8678   /* Look for the `extern' keyword.  */
8679   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8680
8681   /* Look for the string-literal.  */
8682   linkage = cp_parser_string_literal (parser, false, false);
8683
8684   /* Transform the literal into an identifier.  If the literal is a
8685      wide-character string, or contains embedded NULs, then we can't
8686      handle it as the user wants.  */
8687   if (strlen (TREE_STRING_POINTER (linkage))
8688       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8689     {
8690       cp_parser_error (parser, "invalid linkage-specification");
8691       /* Assume C++ linkage.  */
8692       linkage = lang_name_cplusplus;
8693     }
8694   else
8695     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8696
8697   /* We're now using the new linkage.  */
8698   push_lang_context (linkage);
8699
8700   /* If the next token is a `{', then we're using the first
8701      production.  */
8702   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8703     {
8704       /* Consume the `{' token.  */
8705       cp_lexer_consume_token (parser->lexer);
8706       /* Parse the declarations.  */
8707       cp_parser_declaration_seq_opt (parser);
8708       /* Look for the closing `}'.  */
8709       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8710     }
8711   /* Otherwise, there's just one declaration.  */
8712   else
8713     {
8714       bool saved_in_unbraced_linkage_specification_p;
8715
8716       saved_in_unbraced_linkage_specification_p
8717         = parser->in_unbraced_linkage_specification_p;
8718       parser->in_unbraced_linkage_specification_p = true;
8719       cp_parser_declaration (parser);
8720       parser->in_unbraced_linkage_specification_p
8721         = saved_in_unbraced_linkage_specification_p;
8722     }
8723
8724   /* We're done with the linkage-specification.  */
8725   pop_lang_context ();
8726 }
8727
8728 /* Parse a static_assert-declaration.
8729
8730    static_assert-declaration:
8731      static_assert ( constant-expression , string-literal ) ; 
8732
8733    If MEMBER_P, this static_assert is a class member.  */
8734
8735 static void 
8736 cp_parser_static_assert(cp_parser *parser, bool member_p)
8737 {
8738   tree condition;
8739   tree message;
8740   cp_token *token;
8741   location_t saved_loc;
8742
8743   /* Peek at the `static_assert' token so we can keep track of exactly
8744      where the static assertion started.  */
8745   token = cp_lexer_peek_token (parser->lexer);
8746   saved_loc = token->location;
8747
8748   /* Look for the `static_assert' keyword.  */
8749   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8750                                   "%<static_assert%>"))
8751     return;
8752
8753   /*  We know we are in a static assertion; commit to any tentative
8754       parse.  */
8755   if (cp_parser_parsing_tentatively (parser))
8756     cp_parser_commit_to_tentative_parse (parser);
8757
8758   /* Parse the `(' starting the static assertion condition.  */
8759   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8760
8761   /* Parse the constant-expression.  */
8762   condition = 
8763     cp_parser_constant_expression (parser,
8764                                    /*allow_non_constant_p=*/false,
8765                                    /*non_constant_p=*/NULL);
8766
8767   /* Parse the separating `,'.  */
8768   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8769
8770   /* Parse the string-literal message.  */
8771   message = cp_parser_string_literal (parser, 
8772                                       /*translate=*/false,
8773                                       /*wide_ok=*/true);
8774
8775   /* A `)' completes the static assertion.  */
8776   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8777     cp_parser_skip_to_closing_parenthesis (parser, 
8778                                            /*recovering=*/true, 
8779                                            /*or_comma=*/false,
8780                                            /*consume_paren=*/true);
8781
8782   /* A semicolon terminates the declaration.  */
8783   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8784
8785   /* Complete the static assertion, which may mean either processing 
8786      the static assert now or saving it for template instantiation.  */
8787   finish_static_assert (condition, message, saved_loc, member_p);
8788 }
8789
8790 /* Parse a `decltype' type. Returns the type. 
8791
8792    simple-type-specifier:
8793      decltype ( expression )  */
8794
8795 static tree
8796 cp_parser_decltype (cp_parser *parser)
8797 {
8798   tree expr;
8799   bool id_expression_or_member_access_p = false;
8800   const char *saved_message;
8801   bool saved_integral_constant_expression_p;
8802   bool saved_non_integral_constant_expression_p;
8803   cp_token *id_expr_start_token;
8804
8805   /* Look for the `decltype' token.  */
8806   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8807     return error_mark_node;
8808
8809   /* Types cannot be defined in a `decltype' expression.  Save away the
8810      old message.  */
8811   saved_message = parser->type_definition_forbidden_message;
8812
8813   /* And create the new one.  */
8814   parser->type_definition_forbidden_message
8815     = "types may not be defined in %<decltype%> expressions";
8816
8817   /* The restrictions on constant-expressions do not apply inside
8818      decltype expressions.  */
8819   saved_integral_constant_expression_p
8820     = parser->integral_constant_expression_p;
8821   saved_non_integral_constant_expression_p
8822     = parser->non_integral_constant_expression_p;
8823   parser->integral_constant_expression_p = false;
8824
8825   /* Do not actually evaluate the expression.  */
8826   ++skip_evaluation;
8827
8828   /* Parse the opening `('.  */
8829   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8830     return error_mark_node;
8831   
8832   /* First, try parsing an id-expression.  */
8833   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8834   cp_parser_parse_tentatively (parser);
8835   expr = cp_parser_id_expression (parser,
8836                                   /*template_keyword_p=*/false,
8837                                   /*check_dependency_p=*/true,
8838                                   /*template_p=*/NULL,
8839                                   /*declarator_p=*/false,
8840                                   /*optional_p=*/false);
8841
8842   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8843     {
8844       bool non_integral_constant_expression_p = false;
8845       tree id_expression = expr;
8846       cp_id_kind idk;
8847       const char *error_msg;
8848
8849       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8850         /* Lookup the name we got back from the id-expression.  */
8851         expr = cp_parser_lookup_name (parser, expr,
8852                                       none_type,
8853                                       /*is_template=*/false,
8854                                       /*is_namespace=*/false,
8855                                       /*check_dependency=*/true,
8856                                       /*ambiguous_decls=*/NULL,
8857                                       id_expr_start_token->location);
8858
8859       if (expr
8860           && expr != error_mark_node
8861           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8862           && TREE_CODE (expr) != TYPE_DECL
8863           && (TREE_CODE (expr) != BIT_NOT_EXPR
8864               || !TYPE_P (TREE_OPERAND (expr, 0)))
8865           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8866         {
8867           /* Complete lookup of the id-expression.  */
8868           expr = (finish_id_expression
8869                   (id_expression, expr, parser->scope, &idk,
8870                    /*integral_constant_expression_p=*/false,
8871                    /*allow_non_integral_constant_expression_p=*/true,
8872                    &non_integral_constant_expression_p,
8873                    /*template_p=*/false,
8874                    /*done=*/true,
8875                    /*address_p=*/false,
8876                    /*template_arg_p=*/false,
8877                    &error_msg,
8878                    id_expr_start_token->location));
8879
8880           if (expr == error_mark_node)
8881             /* We found an id-expression, but it was something that we
8882                should not have found. This is an error, not something
8883                we can recover from, so note that we found an
8884                id-expression and we'll recover as gracefully as
8885                possible.  */
8886             id_expression_or_member_access_p = true;
8887         }
8888
8889       if (expr 
8890           && expr != error_mark_node
8891           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8892         /* We have an id-expression.  */
8893         id_expression_or_member_access_p = true;
8894     }
8895
8896   if (!id_expression_or_member_access_p)
8897     {
8898       /* Abort the id-expression parse.  */
8899       cp_parser_abort_tentative_parse (parser);
8900
8901       /* Parsing tentatively, again.  */
8902       cp_parser_parse_tentatively (parser);
8903
8904       /* Parse a class member access.  */
8905       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8906                                            /*cast_p=*/false,
8907                                            /*member_access_only_p=*/true, NULL);
8908
8909       if (expr 
8910           && expr != error_mark_node
8911           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8912         /* We have an id-expression.  */
8913         id_expression_or_member_access_p = true;
8914     }
8915
8916   if (id_expression_or_member_access_p)
8917     /* We have parsed the complete id-expression or member access.  */
8918     cp_parser_parse_definitely (parser);
8919   else
8920     {
8921       /* Abort our attempt to parse an id-expression or member access
8922          expression.  */
8923       cp_parser_abort_tentative_parse (parser);
8924
8925       /* Parse a full expression.  */
8926       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8927     }
8928
8929   /* Go back to evaluating expressions.  */
8930   --skip_evaluation;
8931
8932   /* Restore the old message and the integral constant expression
8933      flags.  */
8934   parser->type_definition_forbidden_message = saved_message;
8935   parser->integral_constant_expression_p
8936     = saved_integral_constant_expression_p;
8937   parser->non_integral_constant_expression_p
8938     = saved_non_integral_constant_expression_p;
8939
8940   if (expr == error_mark_node)
8941     {
8942       /* Skip everything up to the closing `)'.  */
8943       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8944                                              /*consume_paren=*/true);
8945       return error_mark_node;
8946     }
8947   
8948   /* Parse to the closing `)'.  */
8949   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8950     {
8951       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8952                                              /*consume_paren=*/true);
8953       return error_mark_node;
8954     }
8955
8956   return finish_decltype_type (expr, id_expression_or_member_access_p);
8957 }
8958
8959 /* Special member functions [gram.special] */
8960
8961 /* Parse a conversion-function-id.
8962
8963    conversion-function-id:
8964      operator conversion-type-id
8965
8966    Returns an IDENTIFIER_NODE representing the operator.  */
8967
8968 static tree
8969 cp_parser_conversion_function_id (cp_parser* parser)
8970 {
8971   tree type;
8972   tree saved_scope;
8973   tree saved_qualifying_scope;
8974   tree saved_object_scope;
8975   tree pushed_scope = NULL_TREE;
8976
8977   /* Look for the `operator' token.  */
8978   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8979     return error_mark_node;
8980   /* When we parse the conversion-type-id, the current scope will be
8981      reset.  However, we need that information in able to look up the
8982      conversion function later, so we save it here.  */
8983   saved_scope = parser->scope;
8984   saved_qualifying_scope = parser->qualifying_scope;
8985   saved_object_scope = parser->object_scope;
8986   /* We must enter the scope of the class so that the names of
8987      entities declared within the class are available in the
8988      conversion-type-id.  For example, consider:
8989
8990        struct S {
8991          typedef int I;
8992          operator I();
8993        };
8994
8995        S::operator I() { ... }
8996
8997      In order to see that `I' is a type-name in the definition, we
8998      must be in the scope of `S'.  */
8999   if (saved_scope)
9000     pushed_scope = push_scope (saved_scope);
9001   /* Parse the conversion-type-id.  */
9002   type = cp_parser_conversion_type_id (parser);
9003   /* Leave the scope of the class, if any.  */
9004   if (pushed_scope)
9005     pop_scope (pushed_scope);
9006   /* Restore the saved scope.  */
9007   parser->scope = saved_scope;
9008   parser->qualifying_scope = saved_qualifying_scope;
9009   parser->object_scope = saved_object_scope;
9010   /* If the TYPE is invalid, indicate failure.  */
9011   if (type == error_mark_node)
9012     return error_mark_node;
9013   return mangle_conv_op_name_for_type (type);
9014 }
9015
9016 /* Parse a conversion-type-id:
9017
9018    conversion-type-id:
9019      type-specifier-seq conversion-declarator [opt]
9020
9021    Returns the TYPE specified.  */
9022
9023 static tree
9024 cp_parser_conversion_type_id (cp_parser* parser)
9025 {
9026   tree attributes;
9027   cp_decl_specifier_seq type_specifiers;
9028   cp_declarator *declarator;
9029   tree type_specified;
9030
9031   /* Parse the attributes.  */
9032   attributes = cp_parser_attributes_opt (parser);
9033   /* Parse the type-specifiers.  */
9034   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9035                                 &type_specifiers);
9036   /* If that didn't work, stop.  */
9037   if (type_specifiers.type == error_mark_node)
9038     return error_mark_node;
9039   /* Parse the conversion-declarator.  */
9040   declarator = cp_parser_conversion_declarator_opt (parser);
9041
9042   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9043                                     /*initialized=*/0, &attributes);
9044   if (attributes)
9045     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9046
9047   /* Don't give this error when parsing tentatively.  This happens to
9048      work because we always parse this definitively once.  */
9049   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9050       && type_uses_auto (type_specified))
9051     {
9052       error ("invalid use of %<auto%> in conversion operator");
9053       return error_mark_node;
9054     }
9055
9056   return type_specified;
9057 }
9058
9059 /* Parse an (optional) conversion-declarator.
9060
9061    conversion-declarator:
9062      ptr-operator conversion-declarator [opt]
9063
9064    */
9065
9066 static cp_declarator *
9067 cp_parser_conversion_declarator_opt (cp_parser* parser)
9068 {
9069   enum tree_code code;
9070   tree class_type;
9071   cp_cv_quals cv_quals;
9072
9073   /* We don't know if there's a ptr-operator next, or not.  */
9074   cp_parser_parse_tentatively (parser);
9075   /* Try the ptr-operator.  */
9076   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9077   /* If it worked, look for more conversion-declarators.  */
9078   if (cp_parser_parse_definitely (parser))
9079     {
9080       cp_declarator *declarator;
9081
9082       /* Parse another optional declarator.  */
9083       declarator = cp_parser_conversion_declarator_opt (parser);
9084
9085       return cp_parser_make_indirect_declarator
9086         (code, class_type, cv_quals, declarator);
9087    }
9088
9089   return NULL;
9090 }
9091
9092 /* Parse an (optional) ctor-initializer.
9093
9094    ctor-initializer:
9095      : mem-initializer-list
9096
9097    Returns TRUE iff the ctor-initializer was actually present.  */
9098
9099 static bool
9100 cp_parser_ctor_initializer_opt (cp_parser* parser)
9101 {
9102   /* If the next token is not a `:', then there is no
9103      ctor-initializer.  */
9104   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9105     {
9106       /* Do default initialization of any bases and members.  */
9107       if (DECL_CONSTRUCTOR_P (current_function_decl))
9108         finish_mem_initializers (NULL_TREE);
9109
9110       return false;
9111     }
9112
9113   /* Consume the `:' token.  */
9114   cp_lexer_consume_token (parser->lexer);
9115   /* And the mem-initializer-list.  */
9116   cp_parser_mem_initializer_list (parser);
9117
9118   return true;
9119 }
9120
9121 /* Parse a mem-initializer-list.
9122
9123    mem-initializer-list:
9124      mem-initializer ... [opt]
9125      mem-initializer ... [opt] , mem-initializer-list  */
9126
9127 static void
9128 cp_parser_mem_initializer_list (cp_parser* parser)
9129 {
9130   tree mem_initializer_list = NULL_TREE;
9131   cp_token *token = cp_lexer_peek_token (parser->lexer);
9132
9133   /* Let the semantic analysis code know that we are starting the
9134      mem-initializer-list.  */
9135   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9136     error ("%Honly constructors take base initializers",
9137            &token->location);
9138
9139   /* Loop through the list.  */
9140   while (true)
9141     {
9142       tree mem_initializer;
9143
9144       token = cp_lexer_peek_token (parser->lexer);
9145       /* Parse the mem-initializer.  */
9146       mem_initializer = cp_parser_mem_initializer (parser);
9147       /* If the next token is a `...', we're expanding member initializers. */
9148       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9149         {
9150           /* Consume the `...'. */
9151           cp_lexer_consume_token (parser->lexer);
9152
9153           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9154              can be expanded but members cannot. */
9155           if (mem_initializer != error_mark_node
9156               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9157             {
9158               error ("%Hcannot expand initializer for member %<%D%>",
9159                      &token->location, TREE_PURPOSE (mem_initializer));
9160               mem_initializer = error_mark_node;
9161             }
9162
9163           /* Construct the pack expansion type. */
9164           if (mem_initializer != error_mark_node)
9165             mem_initializer = make_pack_expansion (mem_initializer);
9166         }
9167       /* Add it to the list, unless it was erroneous.  */
9168       if (mem_initializer != error_mark_node)
9169         {
9170           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9171           mem_initializer_list = mem_initializer;
9172         }
9173       /* If the next token is not a `,', we're done.  */
9174       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9175         break;
9176       /* Consume the `,' token.  */
9177       cp_lexer_consume_token (parser->lexer);
9178     }
9179
9180   /* Perform semantic analysis.  */
9181   if (DECL_CONSTRUCTOR_P (current_function_decl))
9182     finish_mem_initializers (mem_initializer_list);
9183 }
9184
9185 /* Parse a mem-initializer.
9186
9187    mem-initializer:
9188      mem-initializer-id ( expression-list [opt] )
9189      mem-initializer-id braced-init-list
9190
9191    GNU extension:
9192
9193    mem-initializer:
9194      ( expression-list [opt] )
9195
9196    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9197    class) or FIELD_DECL (for a non-static data member) to initialize;
9198    the TREE_VALUE is the expression-list.  An empty initialization
9199    list is represented by void_list_node.  */
9200
9201 static tree
9202 cp_parser_mem_initializer (cp_parser* parser)
9203 {
9204   tree mem_initializer_id;
9205   tree expression_list;
9206   tree member;
9207   cp_token *token = cp_lexer_peek_token (parser->lexer);
9208
9209   /* Find out what is being initialized.  */
9210   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9211     {
9212       permerror (token->location,
9213                  "anachronistic old-style base class initializer");
9214       mem_initializer_id = NULL_TREE;
9215     }
9216   else
9217     {
9218       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9219       if (mem_initializer_id == error_mark_node)
9220         return mem_initializer_id;
9221     }
9222   member = expand_member_init (mem_initializer_id);
9223   if (member && !DECL_P (member))
9224     in_base_initializer = 1;
9225
9226   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9227     {
9228       bool expr_non_constant_p;
9229       maybe_warn_cpp0x ("extended initializer lists");
9230       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9231       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9232       expression_list = build_tree_list (NULL_TREE, expression_list);
9233     }
9234   else
9235     expression_list
9236       = cp_parser_parenthesized_expression_list (parser, false,
9237                                                  /*cast_p=*/false,
9238                                                  /*allow_expansion_p=*/true,
9239                                                  /*non_constant_p=*/NULL);
9240   if (expression_list == error_mark_node)
9241     return error_mark_node;
9242   if (!expression_list)
9243     expression_list = void_type_node;
9244
9245   in_base_initializer = 0;
9246
9247   return member ? build_tree_list (member, expression_list) : error_mark_node;
9248 }
9249
9250 /* Parse a mem-initializer-id.
9251
9252    mem-initializer-id:
9253      :: [opt] nested-name-specifier [opt] class-name
9254      identifier
9255
9256    Returns a TYPE indicating the class to be initializer for the first
9257    production.  Returns an IDENTIFIER_NODE indicating the data member
9258    to be initialized for the second production.  */
9259
9260 static tree
9261 cp_parser_mem_initializer_id (cp_parser* parser)
9262 {
9263   bool global_scope_p;
9264   bool nested_name_specifier_p;
9265   bool template_p = false;
9266   tree id;
9267
9268   cp_token *token = cp_lexer_peek_token (parser->lexer);
9269
9270   /* `typename' is not allowed in this context ([temp.res]).  */
9271   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9272     {
9273       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9274              "member initializer is implicitly a type)",
9275              &token->location);
9276       cp_lexer_consume_token (parser->lexer);
9277     }
9278   /* Look for the optional `::' operator.  */
9279   global_scope_p
9280     = (cp_parser_global_scope_opt (parser,
9281                                    /*current_scope_valid_p=*/false)
9282        != NULL_TREE);
9283   /* Look for the optional nested-name-specifier.  The simplest way to
9284      implement:
9285
9286        [temp.res]
9287
9288        The keyword `typename' is not permitted in a base-specifier or
9289        mem-initializer; in these contexts a qualified name that
9290        depends on a template-parameter is implicitly assumed to be a
9291        type name.
9292
9293      is to assume that we have seen the `typename' keyword at this
9294      point.  */
9295   nested_name_specifier_p
9296     = (cp_parser_nested_name_specifier_opt (parser,
9297                                             /*typename_keyword_p=*/true,
9298                                             /*check_dependency_p=*/true,
9299                                             /*type_p=*/true,
9300                                             /*is_declaration=*/true)
9301        != NULL_TREE);
9302   if (nested_name_specifier_p)
9303     template_p = cp_parser_optional_template_keyword (parser);
9304   /* If there is a `::' operator or a nested-name-specifier, then we
9305      are definitely looking for a class-name.  */
9306   if (global_scope_p || nested_name_specifier_p)
9307     return cp_parser_class_name (parser,
9308                                  /*typename_keyword_p=*/true,
9309                                  /*template_keyword_p=*/template_p,
9310                                  none_type,
9311                                  /*check_dependency_p=*/true,
9312                                  /*class_head_p=*/false,
9313                                  /*is_declaration=*/true);
9314   /* Otherwise, we could also be looking for an ordinary identifier.  */
9315   cp_parser_parse_tentatively (parser);
9316   /* Try a class-name.  */
9317   id = cp_parser_class_name (parser,
9318                              /*typename_keyword_p=*/true,
9319                              /*template_keyword_p=*/false,
9320                              none_type,
9321                              /*check_dependency_p=*/true,
9322                              /*class_head_p=*/false,
9323                              /*is_declaration=*/true);
9324   /* If we found one, we're done.  */
9325   if (cp_parser_parse_definitely (parser))
9326     return id;
9327   /* Otherwise, look for an ordinary identifier.  */
9328   return cp_parser_identifier (parser);
9329 }
9330
9331 /* Overloading [gram.over] */
9332
9333 /* Parse an operator-function-id.
9334
9335    operator-function-id:
9336      operator operator
9337
9338    Returns an IDENTIFIER_NODE for the operator which is a
9339    human-readable spelling of the identifier, e.g., `operator +'.  */
9340
9341 static tree
9342 cp_parser_operator_function_id (cp_parser* parser)
9343 {
9344   /* Look for the `operator' keyword.  */
9345   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9346     return error_mark_node;
9347   /* And then the name of the operator itself.  */
9348   return cp_parser_operator (parser);
9349 }
9350
9351 /* Parse an operator.
9352
9353    operator:
9354      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9355      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9356      || ++ -- , ->* -> () []
9357
9358    GNU Extensions:
9359
9360    operator:
9361      <? >? <?= >?=
9362
9363    Returns an IDENTIFIER_NODE for the operator which is a
9364    human-readable spelling of the identifier, e.g., `operator +'.  */
9365
9366 static tree
9367 cp_parser_operator (cp_parser* parser)
9368 {
9369   tree id = NULL_TREE;
9370   cp_token *token;
9371
9372   /* Peek at the next token.  */
9373   token = cp_lexer_peek_token (parser->lexer);
9374   /* Figure out which operator we have.  */
9375   switch (token->type)
9376     {
9377     case CPP_KEYWORD:
9378       {
9379         enum tree_code op;
9380
9381         /* The keyword should be either `new' or `delete'.  */
9382         if (token->keyword == RID_NEW)
9383           op = NEW_EXPR;
9384         else if (token->keyword == RID_DELETE)
9385           op = DELETE_EXPR;
9386         else
9387           break;
9388
9389         /* Consume the `new' or `delete' token.  */
9390         cp_lexer_consume_token (parser->lexer);
9391
9392         /* Peek at the next token.  */
9393         token = cp_lexer_peek_token (parser->lexer);
9394         /* If it's a `[' token then this is the array variant of the
9395            operator.  */
9396         if (token->type == CPP_OPEN_SQUARE)
9397           {
9398             /* Consume the `[' token.  */
9399             cp_lexer_consume_token (parser->lexer);
9400             /* Look for the `]' token.  */
9401             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9402             id = ansi_opname (op == NEW_EXPR
9403                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9404           }
9405         /* Otherwise, we have the non-array variant.  */
9406         else
9407           id = ansi_opname (op);
9408
9409         return id;
9410       }
9411
9412     case CPP_PLUS:
9413       id = ansi_opname (PLUS_EXPR);
9414       break;
9415
9416     case CPP_MINUS:
9417       id = ansi_opname (MINUS_EXPR);
9418       break;
9419
9420     case CPP_MULT:
9421       id = ansi_opname (MULT_EXPR);
9422       break;
9423
9424     case CPP_DIV:
9425       id = ansi_opname (TRUNC_DIV_EXPR);
9426       break;
9427
9428     case CPP_MOD:
9429       id = ansi_opname (TRUNC_MOD_EXPR);
9430       break;
9431
9432     case CPP_XOR:
9433       id = ansi_opname (BIT_XOR_EXPR);
9434       break;
9435
9436     case CPP_AND:
9437       id = ansi_opname (BIT_AND_EXPR);
9438       break;
9439
9440     case CPP_OR:
9441       id = ansi_opname (BIT_IOR_EXPR);
9442       break;
9443
9444     case CPP_COMPL:
9445       id = ansi_opname (BIT_NOT_EXPR);
9446       break;
9447
9448     case CPP_NOT:
9449       id = ansi_opname (TRUTH_NOT_EXPR);
9450       break;
9451
9452     case CPP_EQ:
9453       id = ansi_assopname (NOP_EXPR);
9454       break;
9455
9456     case CPP_LESS:
9457       id = ansi_opname (LT_EXPR);
9458       break;
9459
9460     case CPP_GREATER:
9461       id = ansi_opname (GT_EXPR);
9462       break;
9463
9464     case CPP_PLUS_EQ:
9465       id = ansi_assopname (PLUS_EXPR);
9466       break;
9467
9468     case CPP_MINUS_EQ:
9469       id = ansi_assopname (MINUS_EXPR);
9470       break;
9471
9472     case CPP_MULT_EQ:
9473       id = ansi_assopname (MULT_EXPR);
9474       break;
9475
9476     case CPP_DIV_EQ:
9477       id = ansi_assopname (TRUNC_DIV_EXPR);
9478       break;
9479
9480     case CPP_MOD_EQ:
9481       id = ansi_assopname (TRUNC_MOD_EXPR);
9482       break;
9483
9484     case CPP_XOR_EQ:
9485       id = ansi_assopname (BIT_XOR_EXPR);
9486       break;
9487
9488     case CPP_AND_EQ:
9489       id = ansi_assopname (BIT_AND_EXPR);
9490       break;
9491
9492     case CPP_OR_EQ:
9493       id = ansi_assopname (BIT_IOR_EXPR);
9494       break;
9495
9496     case CPP_LSHIFT:
9497       id = ansi_opname (LSHIFT_EXPR);
9498       break;
9499
9500     case CPP_RSHIFT:
9501       id = ansi_opname (RSHIFT_EXPR);
9502       break;
9503
9504     case CPP_LSHIFT_EQ:
9505       id = ansi_assopname (LSHIFT_EXPR);
9506       break;
9507
9508     case CPP_RSHIFT_EQ:
9509       id = ansi_assopname (RSHIFT_EXPR);
9510       break;
9511
9512     case CPP_EQ_EQ:
9513       id = ansi_opname (EQ_EXPR);
9514       break;
9515
9516     case CPP_NOT_EQ:
9517       id = ansi_opname (NE_EXPR);
9518       break;
9519
9520     case CPP_LESS_EQ:
9521       id = ansi_opname (LE_EXPR);
9522       break;
9523
9524     case CPP_GREATER_EQ:
9525       id = ansi_opname (GE_EXPR);
9526       break;
9527
9528     case CPP_AND_AND:
9529       id = ansi_opname (TRUTH_ANDIF_EXPR);
9530       break;
9531
9532     case CPP_OR_OR:
9533       id = ansi_opname (TRUTH_ORIF_EXPR);
9534       break;
9535
9536     case CPP_PLUS_PLUS:
9537       id = ansi_opname (POSTINCREMENT_EXPR);
9538       break;
9539
9540     case CPP_MINUS_MINUS:
9541       id = ansi_opname (PREDECREMENT_EXPR);
9542       break;
9543
9544     case CPP_COMMA:
9545       id = ansi_opname (COMPOUND_EXPR);
9546       break;
9547
9548     case CPP_DEREF_STAR:
9549       id = ansi_opname (MEMBER_REF);
9550       break;
9551
9552     case CPP_DEREF:
9553       id = ansi_opname (COMPONENT_REF);
9554       break;
9555
9556     case CPP_OPEN_PAREN:
9557       /* Consume the `('.  */
9558       cp_lexer_consume_token (parser->lexer);
9559       /* Look for the matching `)'.  */
9560       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9561       return ansi_opname (CALL_EXPR);
9562
9563     case CPP_OPEN_SQUARE:
9564       /* Consume the `['.  */
9565       cp_lexer_consume_token (parser->lexer);
9566       /* Look for the matching `]'.  */
9567       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9568       return ansi_opname (ARRAY_REF);
9569
9570     default:
9571       /* Anything else is an error.  */
9572       break;
9573     }
9574
9575   /* If we have selected an identifier, we need to consume the
9576      operator token.  */
9577   if (id)
9578     cp_lexer_consume_token (parser->lexer);
9579   /* Otherwise, no valid operator name was present.  */
9580   else
9581     {
9582       cp_parser_error (parser, "expected operator");
9583       id = error_mark_node;
9584     }
9585
9586   return id;
9587 }
9588
9589 /* Parse a template-declaration.
9590
9591    template-declaration:
9592      export [opt] template < template-parameter-list > declaration
9593
9594    If MEMBER_P is TRUE, this template-declaration occurs within a
9595    class-specifier.
9596
9597    The grammar rule given by the standard isn't correct.  What
9598    is really meant is:
9599
9600    template-declaration:
9601      export [opt] template-parameter-list-seq
9602        decl-specifier-seq [opt] init-declarator [opt] ;
9603      export [opt] template-parameter-list-seq
9604        function-definition
9605
9606    template-parameter-list-seq:
9607      template-parameter-list-seq [opt]
9608      template < template-parameter-list >  */
9609
9610 static void
9611 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9612 {
9613   /* Check for `export'.  */
9614   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9615     {
9616       /* Consume the `export' token.  */
9617       cp_lexer_consume_token (parser->lexer);
9618       /* Warn that we do not support `export'.  */
9619       warning (0, "keyword %<export%> not implemented, and will be ignored");
9620     }
9621
9622   cp_parser_template_declaration_after_export (parser, member_p);
9623 }
9624
9625 /* Parse a template-parameter-list.
9626
9627    template-parameter-list:
9628      template-parameter
9629      template-parameter-list , template-parameter
9630
9631    Returns a TREE_LIST.  Each node represents a template parameter.
9632    The nodes are connected via their TREE_CHAINs.  */
9633
9634 static tree
9635 cp_parser_template_parameter_list (cp_parser* parser)
9636 {
9637   tree parameter_list = NULL_TREE;
9638
9639   begin_template_parm_list ();
9640   while (true)
9641     {
9642       tree parameter;
9643       bool is_non_type;
9644       bool is_parameter_pack;
9645
9646       /* Parse the template-parameter.  */
9647       parameter = cp_parser_template_parameter (parser, 
9648                                                 &is_non_type,
9649                                                 &is_parameter_pack);
9650       /* Add it to the list.  */
9651       if (parameter != error_mark_node)
9652         parameter_list = process_template_parm (parameter_list,
9653                                                 parameter,
9654                                                 is_non_type,
9655                                                 is_parameter_pack);
9656       else
9657        {
9658          tree err_parm = build_tree_list (parameter, parameter);
9659          TREE_VALUE (err_parm) = error_mark_node;
9660          parameter_list = chainon (parameter_list, err_parm);
9661        }
9662
9663       /* If the next token is not a `,', we're done.  */
9664       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9665         break;
9666       /* Otherwise, consume the `,' token.  */
9667       cp_lexer_consume_token (parser->lexer);
9668     }
9669
9670   return end_template_parm_list (parameter_list);
9671 }
9672
9673 /* Parse a template-parameter.
9674
9675    template-parameter:
9676      type-parameter
9677      parameter-declaration
9678
9679    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9680    the parameter.  The TREE_PURPOSE is the default value, if any.
9681    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9682    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9683    set to true iff this parameter is a parameter pack. */
9684
9685 static tree
9686 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9687                               bool *is_parameter_pack)
9688 {
9689   cp_token *token;
9690   cp_parameter_declarator *parameter_declarator;
9691   cp_declarator *id_declarator;
9692   tree parm;
9693
9694   /* Assume it is a type parameter or a template parameter.  */
9695   *is_non_type = false;
9696   /* Assume it not a parameter pack. */
9697   *is_parameter_pack = false;
9698   /* Peek at the next token.  */
9699   token = cp_lexer_peek_token (parser->lexer);
9700   /* If it is `class' or `template', we have a type-parameter.  */
9701   if (token->keyword == RID_TEMPLATE)
9702     return cp_parser_type_parameter (parser, is_parameter_pack);
9703   /* If it is `class' or `typename' we do not know yet whether it is a
9704      type parameter or a non-type parameter.  Consider:
9705
9706        template <typename T, typename T::X X> ...
9707
9708      or:
9709
9710        template <class C, class D*> ...
9711
9712      Here, the first parameter is a type parameter, and the second is
9713      a non-type parameter.  We can tell by looking at the token after
9714      the identifier -- if it is a `,', `=', or `>' then we have a type
9715      parameter.  */
9716   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9717     {
9718       /* Peek at the token after `class' or `typename'.  */
9719       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9720       /* If it's an ellipsis, we have a template type parameter
9721          pack. */
9722       if (token->type == CPP_ELLIPSIS)
9723         return cp_parser_type_parameter (parser, is_parameter_pack);
9724       /* If it's an identifier, skip it.  */
9725       if (token->type == CPP_NAME)
9726         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9727       /* Now, see if the token looks like the end of a template
9728          parameter.  */
9729       if (token->type == CPP_COMMA
9730           || token->type == CPP_EQ
9731           || token->type == CPP_GREATER)
9732         return cp_parser_type_parameter (parser, is_parameter_pack);
9733     }
9734
9735   /* Otherwise, it is a non-type parameter.
9736
9737      [temp.param]
9738
9739      When parsing a default template-argument for a non-type
9740      template-parameter, the first non-nested `>' is taken as the end
9741      of the template parameter-list rather than a greater-than
9742      operator.  */
9743   *is_non_type = true;
9744   parameter_declarator
9745      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9746                                         /*parenthesized_p=*/NULL);
9747
9748   /* If the parameter declaration is marked as a parameter pack, set
9749      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9750      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9751      grokdeclarator. */
9752   if (parameter_declarator
9753       && parameter_declarator->declarator
9754       && parameter_declarator->declarator->parameter_pack_p)
9755     {
9756       *is_parameter_pack = true;
9757       parameter_declarator->declarator->parameter_pack_p = false;
9758     }
9759
9760   /* If the next token is an ellipsis, and we don't already have it
9761      marked as a parameter pack, then we have a parameter pack (that
9762      has no declarator).  */
9763   if (!*is_parameter_pack
9764       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9765       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9766     {
9767       /* Consume the `...'.  */
9768       cp_lexer_consume_token (parser->lexer);
9769       maybe_warn_variadic_templates ();
9770       
9771       *is_parameter_pack = true;
9772     }
9773   /* We might end up with a pack expansion as the type of the non-type
9774      template parameter, in which case this is a non-type template
9775      parameter pack.  */
9776   else if (parameter_declarator
9777            && parameter_declarator->decl_specifiers.type
9778            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9779     {
9780       *is_parameter_pack = true;
9781       parameter_declarator->decl_specifiers.type = 
9782         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9783     }
9784
9785   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9786     {
9787       /* Parameter packs cannot have default arguments.  However, a
9788          user may try to do so, so we'll parse them and give an
9789          appropriate diagnostic here.  */
9790
9791       /* Consume the `='.  */
9792       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9793       cp_lexer_consume_token (parser->lexer);
9794       
9795       /* Find the name of the parameter pack.  */     
9796       id_declarator = parameter_declarator->declarator;
9797       while (id_declarator && id_declarator->kind != cdk_id)
9798         id_declarator = id_declarator->declarator;
9799       
9800       if (id_declarator && id_declarator->kind == cdk_id)
9801         error ("%Htemplate parameter pack %qD cannot have a default argument",
9802                &start_token->location, id_declarator->u.id.unqualified_name);
9803       else
9804         error ("%Htemplate parameter pack cannot have a default argument",
9805                &start_token->location);
9806       
9807       /* Parse the default argument, but throw away the result.  */
9808       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9809     }
9810
9811   parm = grokdeclarator (parameter_declarator->declarator,
9812                          &parameter_declarator->decl_specifiers,
9813                          PARM, /*initialized=*/0,
9814                          /*attrlist=*/NULL);
9815   if (parm == error_mark_node)
9816     return error_mark_node;
9817
9818   return build_tree_list (parameter_declarator->default_argument, parm);
9819 }
9820
9821 /* Parse a type-parameter.
9822
9823    type-parameter:
9824      class identifier [opt]
9825      class identifier [opt] = type-id
9826      typename identifier [opt]
9827      typename identifier [opt] = type-id
9828      template < template-parameter-list > class identifier [opt]
9829      template < template-parameter-list > class identifier [opt]
9830        = id-expression
9831
9832    GNU Extension (variadic templates):
9833
9834    type-parameter:
9835      class ... identifier [opt]
9836      typename ... identifier [opt]
9837
9838    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9839    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9840    the declaration of the parameter.
9841
9842    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9843
9844 static tree
9845 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9846 {
9847   cp_token *token;
9848   tree parameter;
9849
9850   /* Look for a keyword to tell us what kind of parameter this is.  */
9851   token = cp_parser_require (parser, CPP_KEYWORD,
9852                              "%<class%>, %<typename%>, or %<template%>");
9853   if (!token)
9854     return error_mark_node;
9855
9856   switch (token->keyword)
9857     {
9858     case RID_CLASS:
9859     case RID_TYPENAME:
9860       {
9861         tree identifier;
9862         tree default_argument;
9863
9864         /* If the next token is an ellipsis, we have a template
9865            argument pack. */
9866         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9867           {
9868             /* Consume the `...' token. */
9869             cp_lexer_consume_token (parser->lexer);
9870             maybe_warn_variadic_templates ();
9871
9872             *is_parameter_pack = true;
9873           }
9874
9875         /* If the next token is an identifier, then it names the
9876            parameter.  */
9877         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9878           identifier = cp_parser_identifier (parser);
9879         else
9880           identifier = NULL_TREE;
9881
9882         /* Create the parameter.  */
9883         parameter = finish_template_type_parm (class_type_node, identifier);
9884
9885         /* If the next token is an `=', we have a default argument.  */
9886         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9887           {
9888             /* Consume the `=' token.  */
9889             cp_lexer_consume_token (parser->lexer);
9890             /* Parse the default-argument.  */
9891             push_deferring_access_checks (dk_no_deferred);
9892             default_argument = cp_parser_type_id (parser);
9893
9894             /* Template parameter packs cannot have default
9895                arguments. */
9896             if (*is_parameter_pack)
9897               {
9898                 if (identifier)
9899                   error ("%Htemplate parameter pack %qD cannot have a "
9900                          "default argument", &token->location, identifier);
9901                 else
9902                   error ("%Htemplate parameter packs cannot have "
9903                          "default arguments", &token->location);
9904                 default_argument = NULL_TREE;
9905               }
9906             pop_deferring_access_checks ();
9907           }
9908         else
9909           default_argument = NULL_TREE;
9910
9911         /* Create the combined representation of the parameter and the
9912            default argument.  */
9913         parameter = build_tree_list (default_argument, parameter);
9914       }
9915       break;
9916
9917     case RID_TEMPLATE:
9918       {
9919         tree parameter_list;
9920         tree identifier;
9921         tree default_argument;
9922
9923         /* Look for the `<'.  */
9924         cp_parser_require (parser, CPP_LESS, "%<<%>");
9925         /* Parse the template-parameter-list.  */
9926         parameter_list = cp_parser_template_parameter_list (parser);
9927         /* Look for the `>'.  */
9928         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9929         /* Look for the `class' keyword.  */
9930         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9931         /* If the next token is an ellipsis, we have a template
9932            argument pack. */
9933         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9934           {
9935             /* Consume the `...' token. */
9936             cp_lexer_consume_token (parser->lexer);
9937             maybe_warn_variadic_templates ();
9938
9939             *is_parameter_pack = true;
9940           }
9941         /* If the next token is an `=', then there is a
9942            default-argument.  If the next token is a `>', we are at
9943            the end of the parameter-list.  If the next token is a `,',
9944            then we are at the end of this parameter.  */
9945         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9946             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9947             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9948           {
9949             identifier = cp_parser_identifier (parser);
9950             /* Treat invalid names as if the parameter were nameless.  */
9951             if (identifier == error_mark_node)
9952               identifier = NULL_TREE;
9953           }
9954         else
9955           identifier = NULL_TREE;
9956
9957         /* Create the template parameter.  */
9958         parameter = finish_template_template_parm (class_type_node,
9959                                                    identifier);
9960
9961         /* If the next token is an `=', then there is a
9962            default-argument.  */
9963         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9964           {
9965             bool is_template;
9966
9967             /* Consume the `='.  */
9968             cp_lexer_consume_token (parser->lexer);
9969             /* Parse the id-expression.  */
9970             push_deferring_access_checks (dk_no_deferred);
9971             /* save token before parsing the id-expression, for error
9972                reporting */
9973             token = cp_lexer_peek_token (parser->lexer);
9974             default_argument
9975               = cp_parser_id_expression (parser,
9976                                          /*template_keyword_p=*/false,
9977                                          /*check_dependency_p=*/true,
9978                                          /*template_p=*/&is_template,
9979                                          /*declarator_p=*/false,
9980                                          /*optional_p=*/false);
9981             if (TREE_CODE (default_argument) == TYPE_DECL)
9982               /* If the id-expression was a template-id that refers to
9983                  a template-class, we already have the declaration here,
9984                  so no further lookup is needed.  */
9985                  ;
9986             else
9987               /* Look up the name.  */
9988               default_argument
9989                 = cp_parser_lookup_name (parser, default_argument,
9990                                          none_type,
9991                                          /*is_template=*/is_template,
9992                                          /*is_namespace=*/false,
9993                                          /*check_dependency=*/true,
9994                                          /*ambiguous_decls=*/NULL,
9995                                          token->location);
9996             /* See if the default argument is valid.  */
9997             default_argument
9998               = check_template_template_default_arg (default_argument);
9999
10000             /* Template parameter packs cannot have default
10001                arguments. */
10002             if (*is_parameter_pack)
10003               {
10004                 if (identifier)
10005                   error ("%Htemplate parameter pack %qD cannot "
10006                          "have a default argument",
10007                          &token->location, identifier);
10008                 else
10009                   error ("%Htemplate parameter packs cannot "
10010                          "have default arguments",
10011                          &token->location);
10012                 default_argument = NULL_TREE;
10013               }
10014             pop_deferring_access_checks ();
10015           }
10016         else
10017           default_argument = NULL_TREE;
10018
10019         /* Create the combined representation of the parameter and the
10020            default argument.  */
10021         parameter = build_tree_list (default_argument, parameter);
10022       }
10023       break;
10024
10025     default:
10026       gcc_unreachable ();
10027       break;
10028     }
10029
10030   return parameter;
10031 }
10032
10033 /* Parse a template-id.
10034
10035    template-id:
10036      template-name < template-argument-list [opt] >
10037
10038    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10039    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10040    returned.  Otherwise, if the template-name names a function, or set
10041    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10042    names a class, returns a TYPE_DECL for the specialization.
10043
10044    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10045    uninstantiated templates.  */
10046
10047 static tree
10048 cp_parser_template_id (cp_parser *parser,
10049                        bool template_keyword_p,
10050                        bool check_dependency_p,
10051                        bool is_declaration)
10052 {
10053   int i;
10054   tree templ;
10055   tree arguments;
10056   tree template_id;
10057   cp_token_position start_of_id = 0;
10058   deferred_access_check *chk;
10059   VEC (deferred_access_check,gc) *access_check;
10060   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10061   bool is_identifier;
10062
10063   /* If the next token corresponds to a template-id, there is no need
10064      to reparse it.  */
10065   next_token = cp_lexer_peek_token (parser->lexer);
10066   if (next_token->type == CPP_TEMPLATE_ID)
10067     {
10068       struct tree_check *check_value;
10069
10070       /* Get the stored value.  */
10071       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10072       /* Perform any access checks that were deferred.  */
10073       access_check = check_value->checks;
10074       if (access_check)
10075         {
10076           for (i = 0 ;
10077                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10078                ++i)
10079             {
10080               perform_or_defer_access_check (chk->binfo,
10081                                              chk->decl,
10082                                              chk->diag_decl);
10083             }
10084         }
10085       /* Return the stored value.  */
10086       return check_value->value;
10087     }
10088
10089   /* Avoid performing name lookup if there is no possibility of
10090      finding a template-id.  */
10091   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10092       || (next_token->type == CPP_NAME
10093           && !cp_parser_nth_token_starts_template_argument_list_p
10094                (parser, 2)))
10095     {
10096       cp_parser_error (parser, "expected template-id");
10097       return error_mark_node;
10098     }
10099
10100   /* Remember where the template-id starts.  */
10101   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10102     start_of_id = cp_lexer_token_position (parser->lexer, false);
10103
10104   push_deferring_access_checks (dk_deferred);
10105
10106   /* Parse the template-name.  */
10107   is_identifier = false;
10108   token = cp_lexer_peek_token (parser->lexer);
10109   templ = cp_parser_template_name (parser, template_keyword_p,
10110                                    check_dependency_p,
10111                                    is_declaration,
10112                                    &is_identifier);
10113   if (templ == error_mark_node || is_identifier)
10114     {
10115       pop_deferring_access_checks ();
10116       return templ;
10117     }
10118
10119   /* If we find the sequence `[:' after a template-name, it's probably
10120      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10121      parse correctly the argument list.  */
10122   next_token = cp_lexer_peek_token (parser->lexer);
10123   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10124   if (next_token->type == CPP_OPEN_SQUARE
10125       && next_token->flags & DIGRAPH
10126       && next_token_2->type == CPP_COLON
10127       && !(next_token_2->flags & PREV_WHITE))
10128     {
10129       cp_parser_parse_tentatively (parser);
10130       /* Change `:' into `::'.  */
10131       next_token_2->type = CPP_SCOPE;
10132       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10133          CPP_LESS.  */
10134       cp_lexer_consume_token (parser->lexer);
10135
10136       /* Parse the arguments.  */
10137       arguments = cp_parser_enclosed_template_argument_list (parser);
10138       if (!cp_parser_parse_definitely (parser))
10139         {
10140           /* If we couldn't parse an argument list, then we revert our changes
10141              and return simply an error. Maybe this is not a template-id
10142              after all.  */
10143           next_token_2->type = CPP_COLON;
10144           cp_parser_error (parser, "expected %<<%>");
10145           pop_deferring_access_checks ();
10146           return error_mark_node;
10147         }
10148       /* Otherwise, emit an error about the invalid digraph, but continue
10149          parsing because we got our argument list.  */
10150       if (permerror (next_token->location,
10151                      "%<<::%> cannot begin a template-argument list"))
10152         {
10153           static bool hint = false;
10154           inform (next_token->location,
10155                   "%<<:%> is an alternate spelling for %<[%>."
10156                   " Insert whitespace between %<<%> and %<::%>");
10157           if (!hint && !flag_permissive)
10158             {
10159               inform (next_token->location, "(if you use %<-fpermissive%>"
10160                       " G++ will accept your code)");
10161               hint = true;
10162             }
10163         }
10164     }
10165   else
10166     {
10167       /* Look for the `<' that starts the template-argument-list.  */
10168       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10169         {
10170           pop_deferring_access_checks ();
10171           return error_mark_node;
10172         }
10173       /* Parse the arguments.  */
10174       arguments = cp_parser_enclosed_template_argument_list (parser);
10175     }
10176
10177   /* Build a representation of the specialization.  */
10178   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10179     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10180   else if (DECL_CLASS_TEMPLATE_P (templ)
10181            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10182     {
10183       bool entering_scope;
10184       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10185          template (rather than some instantiation thereof) only if
10186          is not nested within some other construct.  For example, in
10187          "template <typename T> void f(T) { A<T>::", A<T> is just an
10188          instantiation of A.  */
10189       entering_scope = (template_parm_scope_p ()
10190                         && cp_lexer_next_token_is (parser->lexer,
10191                                                    CPP_SCOPE));
10192       template_id
10193         = finish_template_type (templ, arguments, entering_scope);
10194     }
10195   else
10196     {
10197       /* If it's not a class-template or a template-template, it should be
10198          a function-template.  */
10199       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10200                    || TREE_CODE (templ) == OVERLOAD
10201                    || BASELINK_P (templ)));
10202
10203       template_id = lookup_template_function (templ, arguments);
10204     }
10205
10206   /* If parsing tentatively, replace the sequence of tokens that makes
10207      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10208      should we re-parse the token stream, we will not have to repeat
10209      the effort required to do the parse, nor will we issue duplicate
10210      error messages about problems during instantiation of the
10211      template.  */
10212   if (start_of_id)
10213     {
10214       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10215
10216       /* Reset the contents of the START_OF_ID token.  */
10217       token->type = CPP_TEMPLATE_ID;
10218       /* Retrieve any deferred checks.  Do not pop this access checks yet
10219          so the memory will not be reclaimed during token replacing below.  */
10220       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10221       token->u.tree_check_value->value = template_id;
10222       token->u.tree_check_value->checks = get_deferred_access_checks ();
10223       token->keyword = RID_MAX;
10224
10225       /* Purge all subsequent tokens.  */
10226       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10227
10228       /* ??? Can we actually assume that, if template_id ==
10229          error_mark_node, we will have issued a diagnostic to the
10230          user, as opposed to simply marking the tentative parse as
10231          failed?  */
10232       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10233         error ("%Hparse error in template argument list",
10234                &token->location);
10235     }
10236
10237   pop_deferring_access_checks ();
10238   return template_id;
10239 }
10240
10241 /* Parse a template-name.
10242
10243    template-name:
10244      identifier
10245
10246    The standard should actually say:
10247
10248    template-name:
10249      identifier
10250      operator-function-id
10251
10252    A defect report has been filed about this issue.
10253
10254    A conversion-function-id cannot be a template name because they cannot
10255    be part of a template-id. In fact, looking at this code:
10256
10257    a.operator K<int>()
10258
10259    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10260    It is impossible to call a templated conversion-function-id with an
10261    explicit argument list, since the only allowed template parameter is
10262    the type to which it is converting.
10263
10264    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10265    `template' keyword, in a construction like:
10266
10267      T::template f<3>()
10268
10269    In that case `f' is taken to be a template-name, even though there
10270    is no way of knowing for sure.
10271
10272    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10273    name refers to a set of overloaded functions, at least one of which
10274    is a template, or an IDENTIFIER_NODE with the name of the template,
10275    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10276    names are looked up inside uninstantiated templates.  */
10277
10278 static tree
10279 cp_parser_template_name (cp_parser* parser,
10280                          bool template_keyword_p,
10281                          bool check_dependency_p,
10282                          bool is_declaration,
10283                          bool *is_identifier)
10284 {
10285   tree identifier;
10286   tree decl;
10287   tree fns;
10288   cp_token *token = cp_lexer_peek_token (parser->lexer);
10289
10290   /* If the next token is `operator', then we have either an
10291      operator-function-id or a conversion-function-id.  */
10292   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10293     {
10294       /* We don't know whether we're looking at an
10295          operator-function-id or a conversion-function-id.  */
10296       cp_parser_parse_tentatively (parser);
10297       /* Try an operator-function-id.  */
10298       identifier = cp_parser_operator_function_id (parser);
10299       /* If that didn't work, try a conversion-function-id.  */
10300       if (!cp_parser_parse_definitely (parser))
10301         {
10302           cp_parser_error (parser, "expected template-name");
10303           return error_mark_node;
10304         }
10305     }
10306   /* Look for the identifier.  */
10307   else
10308     identifier = cp_parser_identifier (parser);
10309
10310   /* If we didn't find an identifier, we don't have a template-id.  */
10311   if (identifier == error_mark_node)
10312     return error_mark_node;
10313
10314   /* If the name immediately followed the `template' keyword, then it
10315      is a template-name.  However, if the next token is not `<', then
10316      we do not treat it as a template-name, since it is not being used
10317      as part of a template-id.  This enables us to handle constructs
10318      like:
10319
10320        template <typename T> struct S { S(); };
10321        template <typename T> S<T>::S();
10322
10323      correctly.  We would treat `S' as a template -- if it were `S<T>'
10324      -- but we do not if there is no `<'.  */
10325
10326   if (processing_template_decl
10327       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10328     {
10329       /* In a declaration, in a dependent context, we pretend that the
10330          "template" keyword was present in order to improve error
10331          recovery.  For example, given:
10332
10333            template <typename T> void f(T::X<int>);
10334
10335          we want to treat "X<int>" as a template-id.  */
10336       if (is_declaration
10337           && !template_keyword_p
10338           && parser->scope && TYPE_P (parser->scope)
10339           && check_dependency_p
10340           && dependent_scope_p (parser->scope)
10341           /* Do not do this for dtors (or ctors), since they never
10342              need the template keyword before their name.  */
10343           && !constructor_name_p (identifier, parser->scope))
10344         {
10345           cp_token_position start = 0;
10346
10347           /* Explain what went wrong.  */
10348           error ("%Hnon-template %qD used as template",
10349                  &token->location, identifier);
10350           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10351                   parser->scope, identifier);
10352           /* If parsing tentatively, find the location of the "<" token.  */
10353           if (cp_parser_simulate_error (parser))
10354             start = cp_lexer_token_position (parser->lexer, true);
10355           /* Parse the template arguments so that we can issue error
10356              messages about them.  */
10357           cp_lexer_consume_token (parser->lexer);
10358           cp_parser_enclosed_template_argument_list (parser);
10359           /* Skip tokens until we find a good place from which to
10360              continue parsing.  */
10361           cp_parser_skip_to_closing_parenthesis (parser,
10362                                                  /*recovering=*/true,
10363                                                  /*or_comma=*/true,
10364                                                  /*consume_paren=*/false);
10365           /* If parsing tentatively, permanently remove the
10366              template argument list.  That will prevent duplicate
10367              error messages from being issued about the missing
10368              "template" keyword.  */
10369           if (start)
10370             cp_lexer_purge_tokens_after (parser->lexer, start);
10371           if (is_identifier)
10372             *is_identifier = true;
10373           return identifier;
10374         }
10375
10376       /* If the "template" keyword is present, then there is generally
10377          no point in doing name-lookup, so we just return IDENTIFIER.
10378          But, if the qualifying scope is non-dependent then we can
10379          (and must) do name-lookup normally.  */
10380       if (template_keyword_p
10381           && (!parser->scope
10382               || (TYPE_P (parser->scope)
10383                   && dependent_type_p (parser->scope))))
10384         return identifier;
10385     }
10386
10387   /* Look up the name.  */
10388   decl = cp_parser_lookup_name (parser, identifier,
10389                                 none_type,
10390                                 /*is_template=*/false,
10391                                 /*is_namespace=*/false,
10392                                 check_dependency_p,
10393                                 /*ambiguous_decls=*/NULL,
10394                                 token->location);
10395   decl = maybe_get_template_decl_from_type_decl (decl);
10396
10397   /* If DECL is a template, then the name was a template-name.  */
10398   if (TREE_CODE (decl) == TEMPLATE_DECL)
10399     ;
10400   else
10401     {
10402       tree fn = NULL_TREE;
10403
10404       /* The standard does not explicitly indicate whether a name that
10405          names a set of overloaded declarations, some of which are
10406          templates, is a template-name.  However, such a name should
10407          be a template-name; otherwise, there is no way to form a
10408          template-id for the overloaded templates.  */
10409       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10410       if (TREE_CODE (fns) == OVERLOAD)
10411         for (fn = fns; fn; fn = OVL_NEXT (fn))
10412           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10413             break;
10414
10415       if (!fn)
10416         {
10417           /* The name does not name a template.  */
10418           cp_parser_error (parser, "expected template-name");
10419           return error_mark_node;
10420         }
10421     }
10422
10423   /* If DECL is dependent, and refers to a function, then just return
10424      its name; we will look it up again during template instantiation.  */
10425   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10426     {
10427       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10428       if (TYPE_P (scope) && dependent_type_p (scope))
10429         return identifier;
10430     }
10431
10432   return decl;
10433 }
10434
10435 /* Parse a template-argument-list.
10436
10437    template-argument-list:
10438      template-argument ... [opt]
10439      template-argument-list , template-argument ... [opt]
10440
10441    Returns a TREE_VEC containing the arguments.  */
10442
10443 static tree
10444 cp_parser_template_argument_list (cp_parser* parser)
10445 {
10446   tree fixed_args[10];
10447   unsigned n_args = 0;
10448   unsigned alloced = 10;
10449   tree *arg_ary = fixed_args;
10450   tree vec;
10451   bool saved_in_template_argument_list_p;
10452   bool saved_ice_p;
10453   bool saved_non_ice_p;
10454
10455   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10456   parser->in_template_argument_list_p = true;
10457   /* Even if the template-id appears in an integral
10458      constant-expression, the contents of the argument list do
10459      not.  */
10460   saved_ice_p = parser->integral_constant_expression_p;
10461   parser->integral_constant_expression_p = false;
10462   saved_non_ice_p = parser->non_integral_constant_expression_p;
10463   parser->non_integral_constant_expression_p = false;
10464   /* Parse the arguments.  */
10465   do
10466     {
10467       tree argument;
10468
10469       if (n_args)
10470         /* Consume the comma.  */
10471         cp_lexer_consume_token (parser->lexer);
10472
10473       /* Parse the template-argument.  */
10474       argument = cp_parser_template_argument (parser);
10475
10476       /* If the next token is an ellipsis, we're expanding a template
10477          argument pack. */
10478       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10479         {
10480           if (argument == error_mark_node)
10481             {
10482               cp_token *token = cp_lexer_peek_token (parser->lexer);
10483               error ("%Hexpected parameter pack before %<...%>",
10484                      &token->location);
10485             }
10486           /* Consume the `...' token. */
10487           cp_lexer_consume_token (parser->lexer);
10488
10489           /* Make the argument into a TYPE_PACK_EXPANSION or
10490              EXPR_PACK_EXPANSION. */
10491           argument = make_pack_expansion (argument);
10492         }
10493
10494       if (n_args == alloced)
10495         {
10496           alloced *= 2;
10497
10498           if (arg_ary == fixed_args)
10499             {
10500               arg_ary = XNEWVEC (tree, alloced);
10501               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10502             }
10503           else
10504             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10505         }
10506       arg_ary[n_args++] = argument;
10507     }
10508   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10509
10510   vec = make_tree_vec (n_args);
10511
10512   while (n_args--)
10513     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10514
10515   if (arg_ary != fixed_args)
10516     free (arg_ary);
10517   parser->non_integral_constant_expression_p = saved_non_ice_p;
10518   parser->integral_constant_expression_p = saved_ice_p;
10519   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10520   return vec;
10521 }
10522
10523 /* Parse a template-argument.
10524
10525    template-argument:
10526      assignment-expression
10527      type-id
10528      id-expression
10529
10530    The representation is that of an assignment-expression, type-id, or
10531    id-expression -- except that the qualified id-expression is
10532    evaluated, so that the value returned is either a DECL or an
10533    OVERLOAD.
10534
10535    Although the standard says "assignment-expression", it forbids
10536    throw-expressions or assignments in the template argument.
10537    Therefore, we use "conditional-expression" instead.  */
10538
10539 static tree
10540 cp_parser_template_argument (cp_parser* parser)
10541 {
10542   tree argument;
10543   bool template_p;
10544   bool address_p;
10545   bool maybe_type_id = false;
10546   cp_token *token = NULL, *argument_start_token = NULL;
10547   cp_id_kind idk;
10548
10549   /* There's really no way to know what we're looking at, so we just
10550      try each alternative in order.
10551
10552        [temp.arg]
10553
10554        In a template-argument, an ambiguity between a type-id and an
10555        expression is resolved to a type-id, regardless of the form of
10556        the corresponding template-parameter.
10557
10558      Therefore, we try a type-id first.  */
10559   cp_parser_parse_tentatively (parser);
10560   argument = cp_parser_template_type_arg (parser);
10561   /* If there was no error parsing the type-id but the next token is a
10562      '>>', our behavior depends on which dialect of C++ we're
10563      parsing. In C++98, we probably found a typo for '> >'. But there
10564      are type-id which are also valid expressions. For instance:
10565
10566      struct X { int operator >> (int); };
10567      template <int V> struct Foo {};
10568      Foo<X () >> 5> r;
10569
10570      Here 'X()' is a valid type-id of a function type, but the user just
10571      wanted to write the expression "X() >> 5". Thus, we remember that we
10572      found a valid type-id, but we still try to parse the argument as an
10573      expression to see what happens. 
10574
10575      In C++0x, the '>>' will be considered two separate '>'
10576      tokens.  */
10577   if (!cp_parser_error_occurred (parser)
10578       && cxx_dialect == cxx98
10579       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10580     {
10581       maybe_type_id = true;
10582       cp_parser_abort_tentative_parse (parser);
10583     }
10584   else
10585     {
10586       /* If the next token isn't a `,' or a `>', then this argument wasn't
10587       really finished. This means that the argument is not a valid
10588       type-id.  */
10589       if (!cp_parser_next_token_ends_template_argument_p (parser))
10590         cp_parser_error (parser, "expected template-argument");
10591       /* If that worked, we're done.  */
10592       if (cp_parser_parse_definitely (parser))
10593         return argument;
10594     }
10595   /* We're still not sure what the argument will be.  */
10596   cp_parser_parse_tentatively (parser);
10597   /* Try a template.  */
10598   argument_start_token = cp_lexer_peek_token (parser->lexer);
10599   argument = cp_parser_id_expression (parser,
10600                                       /*template_keyword_p=*/false,
10601                                       /*check_dependency_p=*/true,
10602                                       &template_p,
10603                                       /*declarator_p=*/false,
10604                                       /*optional_p=*/false);
10605   /* If the next token isn't a `,' or a `>', then this argument wasn't
10606      really finished.  */
10607   if (!cp_parser_next_token_ends_template_argument_p (parser))
10608     cp_parser_error (parser, "expected template-argument");
10609   if (!cp_parser_error_occurred (parser))
10610     {
10611       /* Figure out what is being referred to.  If the id-expression
10612          was for a class template specialization, then we will have a
10613          TYPE_DECL at this point.  There is no need to do name lookup
10614          at this point in that case.  */
10615       if (TREE_CODE (argument) != TYPE_DECL)
10616         argument = cp_parser_lookup_name (parser, argument,
10617                                           none_type,
10618                                           /*is_template=*/template_p,
10619                                           /*is_namespace=*/false,
10620                                           /*check_dependency=*/true,
10621                                           /*ambiguous_decls=*/NULL,
10622                                           argument_start_token->location);
10623       if (TREE_CODE (argument) != TEMPLATE_DECL
10624           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10625         cp_parser_error (parser, "expected template-name");
10626     }
10627   if (cp_parser_parse_definitely (parser))
10628     return argument;
10629   /* It must be a non-type argument.  There permitted cases are given
10630      in [temp.arg.nontype]:
10631
10632      -- an integral constant-expression of integral or enumeration
10633         type; or
10634
10635      -- the name of a non-type template-parameter; or
10636
10637      -- the name of an object or function with external linkage...
10638
10639      -- the address of an object or function with external linkage...
10640
10641      -- a pointer to member...  */
10642   /* Look for a non-type template parameter.  */
10643   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10644     {
10645       cp_parser_parse_tentatively (parser);
10646       argument = cp_parser_primary_expression (parser,
10647                                                /*address_p=*/false,
10648                                                /*cast_p=*/false,
10649                                                /*template_arg_p=*/true,
10650                                                &idk);
10651       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10652           || !cp_parser_next_token_ends_template_argument_p (parser))
10653         cp_parser_simulate_error (parser);
10654       if (cp_parser_parse_definitely (parser))
10655         return argument;
10656     }
10657
10658   /* If the next token is "&", the argument must be the address of an
10659      object or function with external linkage.  */
10660   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10661   if (address_p)
10662     cp_lexer_consume_token (parser->lexer);
10663   /* See if we might have an id-expression.  */
10664   token = cp_lexer_peek_token (parser->lexer);
10665   if (token->type == CPP_NAME
10666       || token->keyword == RID_OPERATOR
10667       || token->type == CPP_SCOPE
10668       || token->type == CPP_TEMPLATE_ID
10669       || token->type == CPP_NESTED_NAME_SPECIFIER)
10670     {
10671       cp_parser_parse_tentatively (parser);
10672       argument = cp_parser_primary_expression (parser,
10673                                                address_p,
10674                                                /*cast_p=*/false,
10675                                                /*template_arg_p=*/true,
10676                                                &idk);
10677       if (cp_parser_error_occurred (parser)
10678           || !cp_parser_next_token_ends_template_argument_p (parser))
10679         cp_parser_abort_tentative_parse (parser);
10680       else
10681         {
10682           if (TREE_CODE (argument) == INDIRECT_REF)
10683             {
10684               gcc_assert (REFERENCE_REF_P (argument));
10685               argument = TREE_OPERAND (argument, 0);
10686             }
10687
10688           if (TREE_CODE (argument) == VAR_DECL)
10689             {
10690               /* A variable without external linkage might still be a
10691                  valid constant-expression, so no error is issued here
10692                  if the external-linkage check fails.  */
10693               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10694                 cp_parser_simulate_error (parser);
10695             }
10696           else if (is_overloaded_fn (argument))
10697             /* All overloaded functions are allowed; if the external
10698                linkage test does not pass, an error will be issued
10699                later.  */
10700             ;
10701           else if (address_p
10702                    && (TREE_CODE (argument) == OFFSET_REF
10703                        || TREE_CODE (argument) == SCOPE_REF))
10704             /* A pointer-to-member.  */
10705             ;
10706           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10707             ;
10708           else
10709             cp_parser_simulate_error (parser);
10710
10711           if (cp_parser_parse_definitely (parser))
10712             {
10713               if (address_p)
10714                 argument = build_x_unary_op (ADDR_EXPR, argument,
10715                                              tf_warning_or_error);
10716               return argument;
10717             }
10718         }
10719     }
10720   /* If the argument started with "&", there are no other valid
10721      alternatives at this point.  */
10722   if (address_p)
10723     {
10724       cp_parser_error (parser, "invalid non-type template argument");
10725       return error_mark_node;
10726     }
10727
10728   /* If the argument wasn't successfully parsed as a type-id followed
10729      by '>>', the argument can only be a constant expression now.
10730      Otherwise, we try parsing the constant-expression tentatively,
10731      because the argument could really be a type-id.  */
10732   if (maybe_type_id)
10733     cp_parser_parse_tentatively (parser);
10734   argument = cp_parser_constant_expression (parser,
10735                                             /*allow_non_constant_p=*/false,
10736                                             /*non_constant_p=*/NULL);
10737   argument = fold_non_dependent_expr (argument);
10738   if (!maybe_type_id)
10739     return argument;
10740   if (!cp_parser_next_token_ends_template_argument_p (parser))
10741     cp_parser_error (parser, "expected template-argument");
10742   if (cp_parser_parse_definitely (parser))
10743     return argument;
10744   /* We did our best to parse the argument as a non type-id, but that
10745      was the only alternative that matched (albeit with a '>' after
10746      it). We can assume it's just a typo from the user, and a
10747      diagnostic will then be issued.  */
10748   return cp_parser_template_type_arg (parser);
10749 }
10750
10751 /* Parse an explicit-instantiation.
10752
10753    explicit-instantiation:
10754      template declaration
10755
10756    Although the standard says `declaration', what it really means is:
10757
10758    explicit-instantiation:
10759      template decl-specifier-seq [opt] declarator [opt] ;
10760
10761    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10762    supposed to be allowed.  A defect report has been filed about this
10763    issue.
10764
10765    GNU Extension:
10766
10767    explicit-instantiation:
10768      storage-class-specifier template
10769        decl-specifier-seq [opt] declarator [opt] ;
10770      function-specifier template
10771        decl-specifier-seq [opt] declarator [opt] ;  */
10772
10773 static void
10774 cp_parser_explicit_instantiation (cp_parser* parser)
10775 {
10776   int declares_class_or_enum;
10777   cp_decl_specifier_seq decl_specifiers;
10778   tree extension_specifier = NULL_TREE;
10779   cp_token *token;
10780
10781   /* Look for an (optional) storage-class-specifier or
10782      function-specifier.  */
10783   if (cp_parser_allow_gnu_extensions_p (parser))
10784     {
10785       extension_specifier
10786         = cp_parser_storage_class_specifier_opt (parser);
10787       if (!extension_specifier)
10788         extension_specifier
10789           = cp_parser_function_specifier_opt (parser,
10790                                               /*decl_specs=*/NULL);
10791     }
10792
10793   /* Look for the `template' keyword.  */
10794   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10795   /* Let the front end know that we are processing an explicit
10796      instantiation.  */
10797   begin_explicit_instantiation ();
10798   /* [temp.explicit] says that we are supposed to ignore access
10799      control while processing explicit instantiation directives.  */
10800   push_deferring_access_checks (dk_no_check);
10801   /* Parse a decl-specifier-seq.  */
10802   token = cp_lexer_peek_token (parser->lexer);
10803   cp_parser_decl_specifier_seq (parser,
10804                                 CP_PARSER_FLAGS_OPTIONAL,
10805                                 &decl_specifiers,
10806                                 &declares_class_or_enum);
10807   /* If there was exactly one decl-specifier, and it declared a class,
10808      and there's no declarator, then we have an explicit type
10809      instantiation.  */
10810   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10811     {
10812       tree type;
10813
10814       type = check_tag_decl (&decl_specifiers);
10815       /* Turn access control back on for names used during
10816          template instantiation.  */
10817       pop_deferring_access_checks ();
10818       if (type)
10819         do_type_instantiation (type, extension_specifier,
10820                                /*complain=*/tf_error);
10821     }
10822   else
10823     {
10824       cp_declarator *declarator;
10825       tree decl;
10826
10827       /* Parse the declarator.  */
10828       declarator
10829         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10830                                 /*ctor_dtor_or_conv_p=*/NULL,
10831                                 /*parenthesized_p=*/NULL,
10832                                 /*member_p=*/false);
10833       if (declares_class_or_enum & 2)
10834         cp_parser_check_for_definition_in_return_type (declarator,
10835                                                        decl_specifiers.type,
10836                                                        decl_specifiers.type_location);
10837       if (declarator != cp_error_declarator)
10838         {
10839           decl = grokdeclarator (declarator, &decl_specifiers,
10840                                  NORMAL, 0, &decl_specifiers.attributes);
10841           /* Turn access control back on for names used during
10842              template instantiation.  */
10843           pop_deferring_access_checks ();
10844           /* Do the explicit instantiation.  */
10845           do_decl_instantiation (decl, extension_specifier);
10846         }
10847       else
10848         {
10849           pop_deferring_access_checks ();
10850           /* Skip the body of the explicit instantiation.  */
10851           cp_parser_skip_to_end_of_statement (parser);
10852         }
10853     }
10854   /* We're done with the instantiation.  */
10855   end_explicit_instantiation ();
10856
10857   cp_parser_consume_semicolon_at_end_of_statement (parser);
10858 }
10859
10860 /* Parse an explicit-specialization.
10861
10862    explicit-specialization:
10863      template < > declaration
10864
10865    Although the standard says `declaration', what it really means is:
10866
10867    explicit-specialization:
10868      template <> decl-specifier [opt] init-declarator [opt] ;
10869      template <> function-definition
10870      template <> explicit-specialization
10871      template <> template-declaration  */
10872
10873 static void
10874 cp_parser_explicit_specialization (cp_parser* parser)
10875 {
10876   bool need_lang_pop;
10877   cp_token *token = cp_lexer_peek_token (parser->lexer);
10878
10879   /* Look for the `template' keyword.  */
10880   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10881   /* Look for the `<'.  */
10882   cp_parser_require (parser, CPP_LESS, "%<<%>");
10883   /* Look for the `>'.  */
10884   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10885   /* We have processed another parameter list.  */
10886   ++parser->num_template_parameter_lists;
10887   /* [temp]
10888
10889      A template ... explicit specialization ... shall not have C
10890      linkage.  */
10891   if (current_lang_name == lang_name_c)
10892     {
10893       error ("%Htemplate specialization with C linkage", &token->location);
10894       /* Give it C++ linkage to avoid confusing other parts of the
10895          front end.  */
10896       push_lang_context (lang_name_cplusplus);
10897       need_lang_pop = true;
10898     }
10899   else
10900     need_lang_pop = false;
10901   /* Let the front end know that we are beginning a specialization.  */
10902   if (!begin_specialization ())
10903     {
10904       end_specialization ();
10905       return;
10906     }
10907
10908   /* If the next keyword is `template', we need to figure out whether
10909      or not we're looking a template-declaration.  */
10910   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10911     {
10912       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10913           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10914         cp_parser_template_declaration_after_export (parser,
10915                                                      /*member_p=*/false);
10916       else
10917         cp_parser_explicit_specialization (parser);
10918     }
10919   else
10920     /* Parse the dependent declaration.  */
10921     cp_parser_single_declaration (parser,
10922                                   /*checks=*/NULL,
10923                                   /*member_p=*/false,
10924                                   /*explicit_specialization_p=*/true,
10925                                   /*friend_p=*/NULL);
10926   /* We're done with the specialization.  */
10927   end_specialization ();
10928   /* For the erroneous case of a template with C linkage, we pushed an
10929      implicit C++ linkage scope; exit that scope now.  */
10930   if (need_lang_pop)
10931     pop_lang_context ();
10932   /* We're done with this parameter list.  */
10933   --parser->num_template_parameter_lists;
10934 }
10935
10936 /* Parse a type-specifier.
10937
10938    type-specifier:
10939      simple-type-specifier
10940      class-specifier
10941      enum-specifier
10942      elaborated-type-specifier
10943      cv-qualifier
10944
10945    GNU Extension:
10946
10947    type-specifier:
10948      __complex__
10949
10950    Returns a representation of the type-specifier.  For a
10951    class-specifier, enum-specifier, or elaborated-type-specifier, a
10952    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10953
10954    The parser flags FLAGS is used to control type-specifier parsing.
10955
10956    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10957    in a decl-specifier-seq.
10958
10959    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10960    class-specifier, enum-specifier, or elaborated-type-specifier, then
10961    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10962    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10963    zero.
10964
10965    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10966    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10967    is set to FALSE.  */
10968
10969 static tree
10970 cp_parser_type_specifier (cp_parser* parser,
10971                           cp_parser_flags flags,
10972                           cp_decl_specifier_seq *decl_specs,
10973                           bool is_declaration,
10974                           int* declares_class_or_enum,
10975                           bool* is_cv_qualifier)
10976 {
10977   tree type_spec = NULL_TREE;
10978   cp_token *token;
10979   enum rid keyword;
10980   cp_decl_spec ds = ds_last;
10981
10982   /* Assume this type-specifier does not declare a new type.  */
10983   if (declares_class_or_enum)
10984     *declares_class_or_enum = 0;
10985   /* And that it does not specify a cv-qualifier.  */
10986   if (is_cv_qualifier)
10987     *is_cv_qualifier = false;
10988   /* Peek at the next token.  */
10989   token = cp_lexer_peek_token (parser->lexer);
10990
10991   /* If we're looking at a keyword, we can use that to guide the
10992      production we choose.  */
10993   keyword = token->keyword;
10994   switch (keyword)
10995     {
10996     case RID_ENUM:
10997       /* Look for the enum-specifier.  */
10998       type_spec = cp_parser_enum_specifier (parser);
10999       /* If that worked, we're done.  */
11000       if (type_spec)
11001         {
11002           if (declares_class_or_enum)
11003             *declares_class_or_enum = 2;
11004           if (decl_specs)
11005             cp_parser_set_decl_spec_type (decl_specs,
11006                                           type_spec,
11007                                           token->location,
11008                                           /*user_defined_p=*/true);
11009           return type_spec;
11010         }
11011       else
11012         goto elaborated_type_specifier;
11013
11014       /* Any of these indicate either a class-specifier, or an
11015          elaborated-type-specifier.  */
11016     case RID_CLASS:
11017     case RID_STRUCT:
11018     case RID_UNION:
11019       /* Parse tentatively so that we can back up if we don't find a
11020          class-specifier.  */
11021       cp_parser_parse_tentatively (parser);
11022       /* Look for the class-specifier.  */
11023       type_spec = cp_parser_class_specifier (parser);
11024       /* If that worked, we're done.  */
11025       if (cp_parser_parse_definitely (parser))
11026         {
11027           if (declares_class_or_enum)
11028             *declares_class_or_enum = 2;
11029           if (decl_specs)
11030             cp_parser_set_decl_spec_type (decl_specs,
11031                                           type_spec,
11032                                           token->location,
11033                                           /*user_defined_p=*/true);
11034           return type_spec;
11035         }
11036
11037       /* Fall through.  */
11038     elaborated_type_specifier:
11039       /* We're declaring (not defining) a class or enum.  */
11040       if (declares_class_or_enum)
11041         *declares_class_or_enum = 1;
11042
11043       /* Fall through.  */
11044     case RID_TYPENAME:
11045       /* Look for an elaborated-type-specifier.  */
11046       type_spec
11047         = (cp_parser_elaborated_type_specifier
11048            (parser,
11049             decl_specs && decl_specs->specs[(int) ds_friend],
11050             is_declaration));
11051       if (decl_specs)
11052         cp_parser_set_decl_spec_type (decl_specs,
11053                                       type_spec,
11054                                       token->location,
11055                                       /*user_defined_p=*/true);
11056       return type_spec;
11057
11058     case RID_CONST:
11059       ds = ds_const;
11060       if (is_cv_qualifier)
11061         *is_cv_qualifier = true;
11062       break;
11063
11064     case RID_VOLATILE:
11065       ds = ds_volatile;
11066       if (is_cv_qualifier)
11067         *is_cv_qualifier = true;
11068       break;
11069
11070     case RID_RESTRICT:
11071       ds = ds_restrict;
11072       if (is_cv_qualifier)
11073         *is_cv_qualifier = true;
11074       break;
11075
11076     case RID_COMPLEX:
11077       /* The `__complex__' keyword is a GNU extension.  */
11078       ds = ds_complex;
11079       break;
11080
11081     default:
11082       break;
11083     }
11084
11085   /* Handle simple keywords.  */
11086   if (ds != ds_last)
11087     {
11088       if (decl_specs)
11089         {
11090           ++decl_specs->specs[(int)ds];
11091           decl_specs->any_specifiers_p = true;
11092         }
11093       return cp_lexer_consume_token (parser->lexer)->u.value;
11094     }
11095
11096   /* If we do not already have a type-specifier, assume we are looking
11097      at a simple-type-specifier.  */
11098   type_spec = cp_parser_simple_type_specifier (parser,
11099                                                decl_specs,
11100                                                flags);
11101
11102   /* If we didn't find a type-specifier, and a type-specifier was not
11103      optional in this context, issue an error message.  */
11104   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11105     {
11106       cp_parser_error (parser, "expected type specifier");
11107       return error_mark_node;
11108     }
11109
11110   return type_spec;
11111 }
11112
11113 /* Parse a simple-type-specifier.
11114
11115    simple-type-specifier:
11116      :: [opt] nested-name-specifier [opt] type-name
11117      :: [opt] nested-name-specifier template template-id
11118      char
11119      wchar_t
11120      bool
11121      short
11122      int
11123      long
11124      signed
11125      unsigned
11126      float
11127      double
11128      void
11129
11130    C++0x Extension:
11131
11132    simple-type-specifier:
11133      auto
11134      decltype ( expression )   
11135      char16_t
11136      char32_t
11137
11138    GNU Extension:
11139
11140    simple-type-specifier:
11141      __typeof__ unary-expression
11142      __typeof__ ( type-id )
11143
11144    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11145    appropriately updated.  */
11146
11147 static tree
11148 cp_parser_simple_type_specifier (cp_parser* parser,
11149                                  cp_decl_specifier_seq *decl_specs,
11150                                  cp_parser_flags flags)
11151 {
11152   tree type = NULL_TREE;
11153   cp_token *token;
11154
11155   /* Peek at the next token.  */
11156   token = cp_lexer_peek_token (parser->lexer);
11157
11158   /* If we're looking at a keyword, things are easy.  */
11159   switch (token->keyword)
11160     {
11161     case RID_CHAR:
11162       if (decl_specs)
11163         decl_specs->explicit_char_p = true;
11164       type = char_type_node;
11165       break;
11166     case RID_CHAR16:
11167       type = char16_type_node;
11168       break;
11169     case RID_CHAR32:
11170       type = char32_type_node;
11171       break;
11172     case RID_WCHAR:
11173       type = wchar_type_node;
11174       break;
11175     case RID_BOOL:
11176       type = boolean_type_node;
11177       break;
11178     case RID_SHORT:
11179       if (decl_specs)
11180         ++decl_specs->specs[(int) ds_short];
11181       type = short_integer_type_node;
11182       break;
11183     case RID_INT:
11184       if (decl_specs)
11185         decl_specs->explicit_int_p = true;
11186       type = integer_type_node;
11187       break;
11188     case RID_LONG:
11189       if (decl_specs)
11190         ++decl_specs->specs[(int) ds_long];
11191       type = long_integer_type_node;
11192       break;
11193     case RID_SIGNED:
11194       if (decl_specs)
11195         ++decl_specs->specs[(int) ds_signed];
11196       type = integer_type_node;
11197       break;
11198     case RID_UNSIGNED:
11199       if (decl_specs)
11200         ++decl_specs->specs[(int) ds_unsigned];
11201       type = unsigned_type_node;
11202       break;
11203     case RID_FLOAT:
11204       type = float_type_node;
11205       break;
11206     case RID_DOUBLE:
11207       type = double_type_node;
11208       break;
11209     case RID_VOID:
11210       type = void_type_node;
11211       break;
11212       
11213     case RID_AUTO:
11214       maybe_warn_cpp0x ("C++0x auto");
11215       type = make_auto ();
11216       break;
11217
11218     case RID_DECLTYPE:
11219       /* Parse the `decltype' type.  */
11220       type = cp_parser_decltype (parser);
11221
11222       if (decl_specs)
11223         cp_parser_set_decl_spec_type (decl_specs, type,
11224                                       token->location,
11225                                       /*user_defined_p=*/true);
11226
11227       return type;
11228
11229     case RID_TYPEOF:
11230       /* Consume the `typeof' token.  */
11231       cp_lexer_consume_token (parser->lexer);
11232       /* Parse the operand to `typeof'.  */
11233       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11234       /* If it is not already a TYPE, take its type.  */
11235       if (!TYPE_P (type))
11236         type = finish_typeof (type);
11237
11238       if (decl_specs)
11239         cp_parser_set_decl_spec_type (decl_specs, type,
11240                                       token->location,
11241                                       /*user_defined_p=*/true);
11242
11243       return type;
11244
11245     default:
11246       break;
11247     }
11248
11249   /* If the type-specifier was for a built-in type, we're done.  */
11250   if (type)
11251     {
11252       tree id;
11253
11254       /* Record the type.  */
11255       if (decl_specs
11256           && (token->keyword != RID_SIGNED
11257               && token->keyword != RID_UNSIGNED
11258               && token->keyword != RID_SHORT
11259               && token->keyword != RID_LONG))
11260         cp_parser_set_decl_spec_type (decl_specs,
11261                                       type,
11262                                       token->location,
11263                                       /*user_defined=*/false);
11264       if (decl_specs)
11265         decl_specs->any_specifiers_p = true;
11266
11267       /* Consume the token.  */
11268       id = cp_lexer_consume_token (parser->lexer)->u.value;
11269
11270       /* There is no valid C++ program where a non-template type is
11271          followed by a "<".  That usually indicates that the user thought
11272          that the type was a template.  */
11273       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11274
11275       return TYPE_NAME (type);
11276     }
11277
11278   /* The type-specifier must be a user-defined type.  */
11279   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11280     {
11281       bool qualified_p;
11282       bool global_p;
11283
11284       /* Don't gobble tokens or issue error messages if this is an
11285          optional type-specifier.  */
11286       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11287         cp_parser_parse_tentatively (parser);
11288
11289       /* Look for the optional `::' operator.  */
11290       global_p
11291         = (cp_parser_global_scope_opt (parser,
11292                                        /*current_scope_valid_p=*/false)
11293            != NULL_TREE);
11294       /* Look for the nested-name specifier.  */
11295       qualified_p
11296         = (cp_parser_nested_name_specifier_opt (parser,
11297                                                 /*typename_keyword_p=*/false,
11298                                                 /*check_dependency_p=*/true,
11299                                                 /*type_p=*/false,
11300                                                 /*is_declaration=*/false)
11301            != NULL_TREE);
11302       token = cp_lexer_peek_token (parser->lexer);
11303       /* If we have seen a nested-name-specifier, and the next token
11304          is `template', then we are using the template-id production.  */
11305       if (parser->scope
11306           && cp_parser_optional_template_keyword (parser))
11307         {
11308           /* Look for the template-id.  */
11309           type = cp_parser_template_id (parser,
11310                                         /*template_keyword_p=*/true,
11311                                         /*check_dependency_p=*/true,
11312                                         /*is_declaration=*/false);
11313           /* If the template-id did not name a type, we are out of
11314              luck.  */
11315           if (TREE_CODE (type) != TYPE_DECL)
11316             {
11317               cp_parser_error (parser, "expected template-id for type");
11318               type = NULL_TREE;
11319             }
11320         }
11321       /* Otherwise, look for a type-name.  */
11322       else
11323         type = cp_parser_type_name (parser);
11324       /* Keep track of all name-lookups performed in class scopes.  */
11325       if (type
11326           && !global_p
11327           && !qualified_p
11328           && TREE_CODE (type) == TYPE_DECL
11329           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11330         maybe_note_name_used_in_class (DECL_NAME (type), type);
11331       /* If it didn't work out, we don't have a TYPE.  */
11332       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11333           && !cp_parser_parse_definitely (parser))
11334         type = NULL_TREE;
11335       if (type && decl_specs)
11336         cp_parser_set_decl_spec_type (decl_specs, type,
11337                                       token->location,
11338                                       /*user_defined=*/true);
11339     }
11340
11341   /* If we didn't get a type-name, issue an error message.  */
11342   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11343     {
11344       cp_parser_error (parser, "expected type-name");
11345       return error_mark_node;
11346     }
11347
11348   /* There is no valid C++ program where a non-template type is
11349      followed by a "<".  That usually indicates that the user thought
11350      that the type was a template.  */
11351   if (type && type != error_mark_node)
11352     {
11353       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11354          If it is, then the '<'...'>' enclose protocol names rather than
11355          template arguments, and so everything is fine.  */
11356       if (c_dialect_objc ()
11357           && (objc_is_id (type) || objc_is_class_name (type)))
11358         {
11359           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11360           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11361
11362           /* Clobber the "unqualified" type previously entered into
11363              DECL_SPECS with the new, improved protocol-qualified version.  */
11364           if (decl_specs)
11365             decl_specs->type = qual_type;
11366
11367           return qual_type;
11368         }
11369
11370       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11371                                                token->location);
11372     }
11373
11374   return type;
11375 }
11376
11377 /* Parse a type-name.
11378
11379    type-name:
11380      class-name
11381      enum-name
11382      typedef-name
11383
11384    enum-name:
11385      identifier
11386
11387    typedef-name:
11388      identifier
11389
11390    Returns a TYPE_DECL for the type.  */
11391
11392 static tree
11393 cp_parser_type_name (cp_parser* parser)
11394 {
11395   tree type_decl;
11396
11397   /* We can't know yet whether it is a class-name or not.  */
11398   cp_parser_parse_tentatively (parser);
11399   /* Try a class-name.  */
11400   type_decl = cp_parser_class_name (parser,
11401                                     /*typename_keyword_p=*/false,
11402                                     /*template_keyword_p=*/false,
11403                                     none_type,
11404                                     /*check_dependency_p=*/true,
11405                                     /*class_head_p=*/false,
11406                                     /*is_declaration=*/false);
11407   /* If it's not a class-name, keep looking.  */
11408   if (!cp_parser_parse_definitely (parser))
11409     {
11410       /* It must be a typedef-name or an enum-name.  */
11411       return cp_parser_nonclass_name (parser);
11412     }
11413
11414   return type_decl;
11415 }
11416
11417 /* Parse a non-class type-name, that is, either an enum-name or a 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_nonclass_name (cp_parser* parser)
11429 {
11430   tree type_decl;
11431   tree identifier;
11432
11433   cp_token *token = cp_lexer_peek_token (parser->lexer);
11434   identifier = cp_parser_identifier (parser);
11435   if (identifier == error_mark_node)
11436     return error_mark_node;
11437
11438   /* Look up the type-name.  */
11439   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11440
11441   if (TREE_CODE (type_decl) != TYPE_DECL
11442       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11443     {
11444       /* See if this is an Objective-C type.  */
11445       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11446       tree type = objc_get_protocol_qualified_type (identifier, protos);
11447       if (type)
11448         type_decl = TYPE_NAME (type);
11449     }
11450   
11451   /* Issue an error if we did not find a type-name.  */
11452   if (TREE_CODE (type_decl) != TYPE_DECL)
11453     {
11454       if (!cp_parser_simulate_error (parser))
11455         cp_parser_name_lookup_error (parser, identifier, type_decl,
11456                                      "is not a type", token->location);
11457       return error_mark_node;
11458     }
11459   /* Remember that the name was used in the definition of the
11460      current class so that we can check later to see if the
11461      meaning would have been different after the class was
11462      entirely defined.  */
11463   else if (type_decl != error_mark_node
11464            && !parser->scope)
11465     maybe_note_name_used_in_class (identifier, type_decl);
11466   
11467   return type_decl;
11468 }
11469
11470 /* Parse an elaborated-type-specifier.  Note that the grammar given
11471    here incorporates the resolution to DR68.
11472
11473    elaborated-type-specifier:
11474      class-key :: [opt] nested-name-specifier [opt] identifier
11475      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11476      enum-key :: [opt] nested-name-specifier [opt] identifier
11477      typename :: [opt] nested-name-specifier identifier
11478      typename :: [opt] nested-name-specifier template [opt]
11479        template-id
11480
11481    GNU extension:
11482
11483    elaborated-type-specifier:
11484      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11485      class-key attributes :: [opt] nested-name-specifier [opt]
11486                template [opt] template-id
11487      enum attributes :: [opt] nested-name-specifier [opt] identifier
11488
11489    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11490    declared `friend'.  If IS_DECLARATION is TRUE, then this
11491    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11492    something is being declared.
11493
11494    Returns the TYPE specified.  */
11495
11496 static tree
11497 cp_parser_elaborated_type_specifier (cp_parser* parser,
11498                                      bool is_friend,
11499                                      bool is_declaration)
11500 {
11501   enum tag_types tag_type;
11502   tree identifier;
11503   tree type = NULL_TREE;
11504   tree attributes = NULL_TREE;
11505   cp_token *token = NULL;
11506
11507   /* See if we're looking at the `enum' keyword.  */
11508   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11509     {
11510       /* Consume the `enum' token.  */
11511       cp_lexer_consume_token (parser->lexer);
11512       /* Remember that it's an enumeration type.  */
11513       tag_type = enum_type;
11514       /* Parse the optional `struct' or `class' key (for C++0x scoped
11515          enums).  */
11516       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11517           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11518         {
11519           if (cxx_dialect == cxx98)
11520             maybe_warn_cpp0x ("scoped enums");
11521
11522           /* Consume the `struct' or `class'.  */
11523           cp_lexer_consume_token (parser->lexer);
11524         }
11525       /* Parse the attributes.  */
11526       attributes = cp_parser_attributes_opt (parser);
11527     }
11528   /* Or, it might be `typename'.  */
11529   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11530                                            RID_TYPENAME))
11531     {
11532       /* Consume the `typename' token.  */
11533       cp_lexer_consume_token (parser->lexer);
11534       /* Remember that it's a `typename' type.  */
11535       tag_type = typename_type;
11536       /* The `typename' keyword is only allowed in templates.  */
11537       if (!processing_template_decl)
11538         permerror (input_location, "using %<typename%> outside of template");
11539     }
11540   /* Otherwise it must be a class-key.  */
11541   else
11542     {
11543       tag_type = cp_parser_class_key (parser);
11544       if (tag_type == none_type)
11545         return error_mark_node;
11546       /* Parse the attributes.  */
11547       attributes = cp_parser_attributes_opt (parser);
11548     }
11549
11550   /* Look for the `::' operator.  */
11551   cp_parser_global_scope_opt (parser,
11552                               /*current_scope_valid_p=*/false);
11553   /* Look for the nested-name-specifier.  */
11554   if (tag_type == typename_type)
11555     {
11556       if (!cp_parser_nested_name_specifier (parser,
11557                                            /*typename_keyword_p=*/true,
11558                                            /*check_dependency_p=*/true,
11559                                            /*type_p=*/true,
11560                                             is_declaration))
11561         return error_mark_node;
11562     }
11563   else
11564     /* Even though `typename' is not present, the proposed resolution
11565        to Core Issue 180 says that in `class A<T>::B', `B' should be
11566        considered a type-name, even if `A<T>' is dependent.  */
11567     cp_parser_nested_name_specifier_opt (parser,
11568                                          /*typename_keyword_p=*/true,
11569                                          /*check_dependency_p=*/true,
11570                                          /*type_p=*/true,
11571                                          is_declaration);
11572  /* For everything but enumeration types, consider a template-id.
11573     For an enumeration type, consider only a plain identifier.  */
11574   if (tag_type != enum_type)
11575     {
11576       bool template_p = false;
11577       tree decl;
11578
11579       /* Allow the `template' keyword.  */
11580       template_p = cp_parser_optional_template_keyword (parser);
11581       /* If we didn't see `template', we don't know if there's a
11582          template-id or not.  */
11583       if (!template_p)
11584         cp_parser_parse_tentatively (parser);
11585       /* Parse the template-id.  */
11586       token = cp_lexer_peek_token (parser->lexer);
11587       decl = cp_parser_template_id (parser, template_p,
11588                                     /*check_dependency_p=*/true,
11589                                     is_declaration);
11590       /* If we didn't find a template-id, look for an ordinary
11591          identifier.  */
11592       if (!template_p && !cp_parser_parse_definitely (parser))
11593         ;
11594       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11595          in effect, then we must assume that, upon instantiation, the
11596          template will correspond to a class.  */
11597       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11598                && tag_type == typename_type)
11599         type = make_typename_type (parser->scope, decl,
11600                                    typename_type,
11601                                    /*complain=*/tf_error);
11602       /* If the `typename' keyword is in effect and DECL is not a type
11603          decl. Then type is non existant.   */
11604       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11605         type = NULL_TREE; 
11606       else 
11607         type = TREE_TYPE (decl);
11608     }
11609
11610   if (!type)
11611     {
11612       token = cp_lexer_peek_token (parser->lexer);
11613       identifier = cp_parser_identifier (parser);
11614
11615       if (identifier == error_mark_node)
11616         {
11617           parser->scope = NULL_TREE;
11618           return error_mark_node;
11619         }
11620
11621       /* For a `typename', we needn't call xref_tag.  */
11622       if (tag_type == typename_type
11623           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11624         return cp_parser_make_typename_type (parser, parser->scope,
11625                                              identifier,
11626                                              token->location);
11627       /* Look up a qualified name in the usual way.  */
11628       if (parser->scope)
11629         {
11630           tree decl;
11631           tree ambiguous_decls;
11632
11633           decl = cp_parser_lookup_name (parser, identifier,
11634                                         tag_type,
11635                                         /*is_template=*/false,
11636                                         /*is_namespace=*/false,
11637                                         /*check_dependency=*/true,
11638                                         &ambiguous_decls,
11639                                         token->location);
11640
11641           /* If the lookup was ambiguous, an error will already have been
11642              issued.  */
11643           if (ambiguous_decls)
11644             return error_mark_node;
11645
11646           /* If we are parsing friend declaration, DECL may be a
11647              TEMPLATE_DECL tree node here.  However, we need to check
11648              whether this TEMPLATE_DECL results in valid code.  Consider
11649              the following example:
11650
11651                namespace N {
11652                  template <class T> class C {};
11653                }
11654                class X {
11655                  template <class T> friend class N::C; // #1, valid code
11656                };
11657                template <class T> class Y {
11658                  friend class N::C;                    // #2, invalid code
11659                };
11660
11661              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11662              name lookup of `N::C'.  We see that friend declaration must
11663              be template for the code to be valid.  Note that
11664              processing_template_decl does not work here since it is
11665              always 1 for the above two cases.  */
11666
11667           decl = (cp_parser_maybe_treat_template_as_class
11668                   (decl, /*tag_name_p=*/is_friend
11669                          && parser->num_template_parameter_lists));
11670
11671           if (TREE_CODE (decl) != TYPE_DECL)
11672             {
11673               cp_parser_diagnose_invalid_type_name (parser,
11674                                                     parser->scope,
11675                                                     identifier,
11676                                                     token->location);
11677               return error_mark_node;
11678             }
11679
11680           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11681             {
11682               bool allow_template = (parser->num_template_parameter_lists
11683                                       || DECL_SELF_REFERENCE_P (decl));
11684               type = check_elaborated_type_specifier (tag_type, decl, 
11685                                                       allow_template);
11686
11687               if (type == error_mark_node)
11688                 return error_mark_node;
11689             }
11690
11691           /* Forward declarations of nested types, such as
11692
11693                class C1::C2;
11694                class C1::C2::C3;
11695
11696              are invalid unless all components preceding the final '::'
11697              are complete.  If all enclosing types are complete, these
11698              declarations become merely pointless.
11699
11700              Invalid forward declarations of nested types are errors
11701              caught elsewhere in parsing.  Those that are pointless arrive
11702              here.  */
11703
11704           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11705               && !is_friend && !processing_explicit_instantiation)
11706             warning (0, "declaration %qD does not declare anything", decl);
11707
11708           type = TREE_TYPE (decl);
11709         }
11710       else
11711         {
11712           /* An elaborated-type-specifier sometimes introduces a new type and
11713              sometimes names an existing type.  Normally, the rule is that it
11714              introduces a new type only if there is not an existing type of
11715              the same name already in scope.  For example, given:
11716
11717                struct S {};
11718                void f() { struct S s; }
11719
11720              the `struct S' in the body of `f' is the same `struct S' as in
11721              the global scope; the existing definition is used.  However, if
11722              there were no global declaration, this would introduce a new
11723              local class named `S'.
11724
11725              An exception to this rule applies to the following code:
11726
11727                namespace N { struct S; }
11728
11729              Here, the elaborated-type-specifier names a new type
11730              unconditionally; even if there is already an `S' in the
11731              containing scope this declaration names a new type.
11732              This exception only applies if the elaborated-type-specifier
11733              forms the complete declaration:
11734
11735                [class.name]
11736
11737                A declaration consisting solely of `class-key identifier ;' is
11738                either a redeclaration of the name in the current scope or a
11739                forward declaration of the identifier as a class name.  It
11740                introduces the name into the current scope.
11741
11742              We are in this situation precisely when the next token is a `;'.
11743
11744              An exception to the exception is that a `friend' declaration does
11745              *not* name a new type; i.e., given:
11746
11747                struct S { friend struct T; };
11748
11749              `T' is not a new type in the scope of `S'.
11750
11751              Also, `new struct S' or `sizeof (struct S)' never results in the
11752              definition of a new type; a new type can only be declared in a
11753              declaration context.  */
11754
11755           tag_scope ts;
11756           bool template_p;
11757
11758           if (is_friend)
11759             /* Friends have special name lookup rules.  */
11760             ts = ts_within_enclosing_non_class;
11761           else if (is_declaration
11762                    && cp_lexer_next_token_is (parser->lexer,
11763                                               CPP_SEMICOLON))
11764             /* This is a `class-key identifier ;' */
11765             ts = ts_current;
11766           else
11767             ts = ts_global;
11768
11769           template_p =
11770             (parser->num_template_parameter_lists
11771              && (cp_parser_next_token_starts_class_definition_p (parser)
11772                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11773           /* An unqualified name was used to reference this type, so
11774              there were no qualifying templates.  */
11775           if (!cp_parser_check_template_parameters (parser,
11776                                                     /*num_templates=*/0,
11777                                                     token->location))
11778             return error_mark_node;
11779           type = xref_tag (tag_type, identifier, ts, template_p);
11780         }
11781     }
11782
11783   if (type == error_mark_node)
11784     return error_mark_node;
11785
11786   /* Allow attributes on forward declarations of classes.  */
11787   if (attributes)
11788     {
11789       if (TREE_CODE (type) == TYPENAME_TYPE)
11790         warning (OPT_Wattributes,
11791                  "attributes ignored on uninstantiated type");
11792       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11793                && ! processing_explicit_instantiation)
11794         warning (OPT_Wattributes,
11795                  "attributes ignored on template instantiation");
11796       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11797         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11798       else
11799         warning (OPT_Wattributes,
11800                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11801     }
11802
11803   if (tag_type != enum_type)
11804     cp_parser_check_class_key (tag_type, type);
11805
11806   /* A "<" cannot follow an elaborated type specifier.  If that
11807      happens, the user was probably trying to form a template-id.  */
11808   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11809
11810   return type;
11811 }
11812
11813 /* Parse an enum-specifier.
11814
11815    enum-specifier:
11816      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11817
11818    enum-key:
11819      enum
11820      enum class   [C++0x]
11821      enum struct  [C++0x]
11822
11823    enum-base:   [C++0x]
11824      : type-specifier-seq
11825
11826    GNU Extensions:
11827      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11828        { enumerator-list [opt] }attributes[opt]
11829
11830    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11831    if the token stream isn't an enum-specifier after all.  */
11832
11833 static tree
11834 cp_parser_enum_specifier (cp_parser* parser)
11835 {
11836   tree identifier;
11837   tree type;
11838   tree attributes;
11839   bool scoped_enum_p = false;
11840   bool has_underlying_type = false;
11841   tree underlying_type = NULL_TREE;
11842
11843   /* Parse tentatively so that we can back up if we don't find a
11844      enum-specifier.  */
11845   cp_parser_parse_tentatively (parser);
11846
11847   /* Caller guarantees that the current token is 'enum', an identifier
11848      possibly follows, and the token after that is an opening brace.
11849      If we don't have an identifier, fabricate an anonymous name for
11850      the enumeration being defined.  */
11851   cp_lexer_consume_token (parser->lexer);
11852
11853   /* Parse the "class" or "struct", which indicates a scoped
11854      enumeration type in C++0x.  */
11855   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11856       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11857     {
11858       if (cxx_dialect == cxx98)
11859         maybe_warn_cpp0x ("scoped enums");
11860
11861       /* Consume the `struct' or `class' token.  */
11862       cp_lexer_consume_token (parser->lexer);
11863
11864       scoped_enum_p = true;
11865     }
11866
11867   attributes = cp_parser_attributes_opt (parser);
11868
11869   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11870     identifier = cp_parser_identifier (parser);
11871   else
11872     identifier = make_anon_name ();
11873
11874   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11875   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11876     {
11877       cp_decl_specifier_seq type_specifiers;
11878
11879       /* At this point this is surely not elaborated type specifier.  */
11880       if (!cp_parser_parse_definitely (parser))
11881         return NULL_TREE;
11882
11883       if (cxx_dialect == cxx98)
11884         maybe_warn_cpp0x ("scoped enums");
11885
11886       /* Consume the `:'.  */
11887       cp_lexer_consume_token (parser->lexer);
11888
11889       has_underlying_type = true;
11890
11891       /* Parse the type-specifier-seq.  */
11892       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11893                                     &type_specifiers);
11894
11895       /* If that didn't work, stop.  */
11896       if (type_specifiers.type != error_mark_node)
11897         {
11898           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11899                                             /*initialized=*/0, NULL);
11900           if (underlying_type == error_mark_node)
11901             underlying_type = NULL_TREE;
11902         }
11903     }
11904
11905   /* Look for the `{' but don't consume it yet.  */
11906   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11907     {
11908       cp_parser_error (parser, "expected %<{%>");
11909       if (has_underlying_type)
11910         return NULL_TREE;
11911     }
11912
11913   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11914     return NULL_TREE;
11915
11916   /* Issue an error message if type-definitions are forbidden here.  */
11917   if (!cp_parser_check_type_definition (parser))
11918     type = error_mark_node;
11919   else
11920     /* Create the new type.  We do this before consuming the opening
11921        brace so the enum will be recorded as being on the line of its
11922        tag (or the 'enum' keyword, if there is no tag).  */
11923     type = start_enum (identifier, underlying_type, scoped_enum_p);
11924   
11925   /* Consume the opening brace.  */
11926   cp_lexer_consume_token (parser->lexer);
11927
11928   if (type == error_mark_node)
11929     {
11930       cp_parser_skip_to_end_of_block_or_statement (parser);
11931       return error_mark_node;
11932     }
11933
11934   /* If the next token is not '}', then there are some enumerators.  */
11935   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11936     cp_parser_enumerator_list (parser, type);
11937
11938   /* Consume the final '}'.  */
11939   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11940
11941   /* Look for trailing attributes to apply to this enumeration, and
11942      apply them if appropriate.  */
11943   if (cp_parser_allow_gnu_extensions_p (parser))
11944     {
11945       tree trailing_attr = cp_parser_attributes_opt (parser);
11946       trailing_attr = chainon (trailing_attr, attributes);
11947       cplus_decl_attributes (&type,
11948                              trailing_attr,
11949                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11950     }
11951
11952   /* Finish up the enumeration.  */
11953   finish_enum (type);
11954
11955   return type;
11956 }
11957
11958 /* Parse an enumerator-list.  The enumerators all have the indicated
11959    TYPE.
11960
11961    enumerator-list:
11962      enumerator-definition
11963      enumerator-list , enumerator-definition  */
11964
11965 static void
11966 cp_parser_enumerator_list (cp_parser* parser, tree type)
11967 {
11968   while (true)
11969     {
11970       /* Parse an enumerator-definition.  */
11971       cp_parser_enumerator_definition (parser, type);
11972
11973       /* If the next token is not a ',', we've reached the end of
11974          the list.  */
11975       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11976         break;
11977       /* Otherwise, consume the `,' and keep going.  */
11978       cp_lexer_consume_token (parser->lexer);
11979       /* If the next token is a `}', there is a trailing comma.  */
11980       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11981         {
11982           if (!in_system_header)
11983             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11984           break;
11985         }
11986     }
11987 }
11988
11989 /* Parse an enumerator-definition.  The enumerator has the indicated
11990    TYPE.
11991
11992    enumerator-definition:
11993      enumerator
11994      enumerator = constant-expression
11995
11996    enumerator:
11997      identifier  */
11998
11999 static void
12000 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12001 {
12002   tree identifier;
12003   tree value;
12004
12005   /* Look for the identifier.  */
12006   identifier = cp_parser_identifier (parser);
12007   if (identifier == error_mark_node)
12008     return;
12009
12010   /* If the next token is an '=', then there is an explicit value.  */
12011   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12012     {
12013       /* Consume the `=' token.  */
12014       cp_lexer_consume_token (parser->lexer);
12015       /* Parse the value.  */
12016       value = cp_parser_constant_expression (parser,
12017                                              /*allow_non_constant_p=*/false,
12018                                              NULL);
12019     }
12020   else
12021     value = NULL_TREE;
12022
12023   /* If we are processing a template, make sure the initializer of the
12024      enumerator doesn't contain any bare template parameter pack.  */
12025   if (check_for_bare_parameter_packs (value))
12026     value = error_mark_node;
12027
12028   /* Create the enumerator.  */
12029   build_enumerator (identifier, value, type);
12030 }
12031
12032 /* Parse a namespace-name.
12033
12034    namespace-name:
12035      original-namespace-name
12036      namespace-alias
12037
12038    Returns the NAMESPACE_DECL for the namespace.  */
12039
12040 static tree
12041 cp_parser_namespace_name (cp_parser* parser)
12042 {
12043   tree identifier;
12044   tree namespace_decl;
12045
12046   cp_token *token = cp_lexer_peek_token (parser->lexer);
12047
12048   /* Get the name of the namespace.  */
12049   identifier = cp_parser_identifier (parser);
12050   if (identifier == error_mark_node)
12051     return error_mark_node;
12052
12053   /* Look up the identifier in the currently active scope.  Look only
12054      for namespaces, due to:
12055
12056        [basic.lookup.udir]
12057
12058        When looking up a namespace-name in a using-directive or alias
12059        definition, only namespace names are considered.
12060
12061      And:
12062
12063        [basic.lookup.qual]
12064
12065        During the lookup of a name preceding the :: scope resolution
12066        operator, object, function, and enumerator names are ignored.
12067
12068      (Note that cp_parser_qualifying_entity only calls this
12069      function if the token after the name is the scope resolution
12070      operator.)  */
12071   namespace_decl = cp_parser_lookup_name (parser, identifier,
12072                                           none_type,
12073                                           /*is_template=*/false,
12074                                           /*is_namespace=*/true,
12075                                           /*check_dependency=*/true,
12076                                           /*ambiguous_decls=*/NULL,
12077                                           token->location);
12078   /* If it's not a namespace, issue an error.  */
12079   if (namespace_decl == error_mark_node
12080       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12081     {
12082       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12083         error ("%H%qD is not a namespace-name", &token->location, identifier);
12084       cp_parser_error (parser, "expected namespace-name");
12085       namespace_decl = error_mark_node;
12086     }
12087
12088   return namespace_decl;
12089 }
12090
12091 /* Parse a namespace-definition.
12092
12093    namespace-definition:
12094      named-namespace-definition
12095      unnamed-namespace-definition
12096
12097    named-namespace-definition:
12098      original-namespace-definition
12099      extension-namespace-definition
12100
12101    original-namespace-definition:
12102      namespace identifier { namespace-body }
12103
12104    extension-namespace-definition:
12105      namespace original-namespace-name { namespace-body }
12106
12107    unnamed-namespace-definition:
12108      namespace { namespace-body } */
12109
12110 static void
12111 cp_parser_namespace_definition (cp_parser* parser)
12112 {
12113   tree identifier, attribs;
12114   bool has_visibility;
12115   bool is_inline;
12116
12117   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12118     {
12119       is_inline = true;
12120       cp_lexer_consume_token (parser->lexer);
12121     }
12122   else
12123     is_inline = false;
12124
12125   /* Look for the `namespace' keyword.  */
12126   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12127
12128   /* Get the name of the namespace.  We do not attempt to distinguish
12129      between an original-namespace-definition and an
12130      extension-namespace-definition at this point.  The semantic
12131      analysis routines are responsible for that.  */
12132   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12133     identifier = cp_parser_identifier (parser);
12134   else
12135     identifier = NULL_TREE;
12136
12137   /* Parse any specified attributes.  */
12138   attribs = cp_parser_attributes_opt (parser);
12139
12140   /* Look for the `{' to start the namespace.  */
12141   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12142   /* Start the namespace.  */
12143   push_namespace (identifier);
12144
12145   /* "inline namespace" is equivalent to a stub namespace definition
12146      followed by a strong using directive.  */
12147   if (is_inline)
12148     {
12149       tree name_space = current_namespace;
12150       /* Set up namespace association.  */
12151       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12152         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12153                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12154       /* Import the contents of the inline namespace.  */
12155       pop_namespace ();
12156       do_using_directive (name_space);
12157       push_namespace (identifier);
12158     }
12159
12160   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12161
12162   /* Parse the body of the namespace.  */
12163   cp_parser_namespace_body (parser);
12164
12165 #ifdef HANDLE_PRAGMA_VISIBILITY
12166   if (has_visibility)
12167     pop_visibility ();
12168 #endif
12169
12170   /* Finish the namespace.  */
12171   pop_namespace ();
12172   /* Look for the final `}'.  */
12173   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12174 }
12175
12176 /* Parse a namespace-body.
12177
12178    namespace-body:
12179      declaration-seq [opt]  */
12180
12181 static void
12182 cp_parser_namespace_body (cp_parser* parser)
12183 {
12184   cp_parser_declaration_seq_opt (parser);
12185 }
12186
12187 /* Parse a namespace-alias-definition.
12188
12189    namespace-alias-definition:
12190      namespace identifier = qualified-namespace-specifier ;  */
12191
12192 static void
12193 cp_parser_namespace_alias_definition (cp_parser* parser)
12194 {
12195   tree identifier;
12196   tree namespace_specifier;
12197
12198   cp_token *token = cp_lexer_peek_token (parser->lexer);
12199
12200   /* Look for the `namespace' keyword.  */
12201   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12202   /* Look for the identifier.  */
12203   identifier = cp_parser_identifier (parser);
12204   if (identifier == error_mark_node)
12205     return;
12206   /* Look for the `=' token.  */
12207   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12208       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12209     {
12210       error ("%H%<namespace%> definition is not allowed here", &token->location);
12211       /* Skip the definition.  */
12212       cp_lexer_consume_token (parser->lexer);
12213       if (cp_parser_skip_to_closing_brace (parser))
12214         cp_lexer_consume_token (parser->lexer);
12215       return;
12216     }
12217   cp_parser_require (parser, CPP_EQ, "%<=%>");
12218   /* Look for the qualified-namespace-specifier.  */
12219   namespace_specifier
12220     = cp_parser_qualified_namespace_specifier (parser);
12221   /* Look for the `;' token.  */
12222   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12223
12224   /* Register the alias in the symbol table.  */
12225   do_namespace_alias (identifier, namespace_specifier);
12226 }
12227
12228 /* Parse a qualified-namespace-specifier.
12229
12230    qualified-namespace-specifier:
12231      :: [opt] nested-name-specifier [opt] namespace-name
12232
12233    Returns a NAMESPACE_DECL corresponding to the specified
12234    namespace.  */
12235
12236 static tree
12237 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12238 {
12239   /* Look for the optional `::'.  */
12240   cp_parser_global_scope_opt (parser,
12241                               /*current_scope_valid_p=*/false);
12242
12243   /* Look for the optional nested-name-specifier.  */
12244   cp_parser_nested_name_specifier_opt (parser,
12245                                        /*typename_keyword_p=*/false,
12246                                        /*check_dependency_p=*/true,
12247                                        /*type_p=*/false,
12248                                        /*is_declaration=*/true);
12249
12250   return cp_parser_namespace_name (parser);
12251 }
12252
12253 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12254    access declaration.
12255
12256    using-declaration:
12257      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12258      using :: unqualified-id ;  
12259
12260    access-declaration:
12261      qualified-id ;  
12262
12263    */
12264
12265 static bool
12266 cp_parser_using_declaration (cp_parser* parser, 
12267                              bool access_declaration_p)
12268 {
12269   cp_token *token;
12270   bool typename_p = false;
12271   bool global_scope_p;
12272   tree decl;
12273   tree identifier;
12274   tree qscope;
12275
12276   if (access_declaration_p)
12277     cp_parser_parse_tentatively (parser);
12278   else
12279     {
12280       /* Look for the `using' keyword.  */
12281       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12282       
12283       /* Peek at the next token.  */
12284       token = cp_lexer_peek_token (parser->lexer);
12285       /* See if it's `typename'.  */
12286       if (token->keyword == RID_TYPENAME)
12287         {
12288           /* Remember that we've seen it.  */
12289           typename_p = true;
12290           /* Consume the `typename' token.  */
12291           cp_lexer_consume_token (parser->lexer);
12292         }
12293     }
12294
12295   /* Look for the optional global scope qualification.  */
12296   global_scope_p
12297     = (cp_parser_global_scope_opt (parser,
12298                                    /*current_scope_valid_p=*/false)
12299        != NULL_TREE);
12300
12301   /* If we saw `typename', or didn't see `::', then there must be a
12302      nested-name-specifier present.  */
12303   if (typename_p || !global_scope_p)
12304     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12305                                               /*check_dependency_p=*/true,
12306                                               /*type_p=*/false,
12307                                               /*is_declaration=*/true);
12308   /* Otherwise, we could be in either of the two productions.  In that
12309      case, treat the nested-name-specifier as optional.  */
12310   else
12311     qscope = cp_parser_nested_name_specifier_opt (parser,
12312                                                   /*typename_keyword_p=*/false,
12313                                                   /*check_dependency_p=*/true,
12314                                                   /*type_p=*/false,
12315                                                   /*is_declaration=*/true);
12316   if (!qscope)
12317     qscope = global_namespace;
12318
12319   if (access_declaration_p && cp_parser_error_occurred (parser))
12320     /* Something has already gone wrong; there's no need to parse
12321        further.  Since an error has occurred, the return value of
12322        cp_parser_parse_definitely will be false, as required.  */
12323     return cp_parser_parse_definitely (parser);
12324
12325   token = cp_lexer_peek_token (parser->lexer);
12326   /* Parse the unqualified-id.  */
12327   identifier = cp_parser_unqualified_id (parser,
12328                                          /*template_keyword_p=*/false,
12329                                          /*check_dependency_p=*/true,
12330                                          /*declarator_p=*/true,
12331                                          /*optional_p=*/false);
12332
12333   if (access_declaration_p)
12334     {
12335       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12336         cp_parser_simulate_error (parser);
12337       if (!cp_parser_parse_definitely (parser))
12338         return false;
12339     }
12340
12341   /* The function we call to handle a using-declaration is different
12342      depending on what scope we are in.  */
12343   if (qscope == error_mark_node || identifier == error_mark_node)
12344     ;
12345   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12346            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12347     /* [namespace.udecl]
12348
12349        A using declaration shall not name a template-id.  */
12350     error ("%Ha template-id may not appear in a using-declaration",
12351             &token->location);
12352   else
12353     {
12354       if (at_class_scope_p ())
12355         {
12356           /* Create the USING_DECL.  */
12357           decl = do_class_using_decl (parser->scope, identifier);
12358
12359           if (check_for_bare_parameter_packs (decl))
12360             return false;
12361           else
12362             /* Add it to the list of members in this class.  */
12363             finish_member_declaration (decl);
12364         }
12365       else
12366         {
12367           decl = cp_parser_lookup_name_simple (parser,
12368                                                identifier,
12369                                                token->location);
12370           if (decl == error_mark_node)
12371             cp_parser_name_lookup_error (parser, identifier,
12372                                          decl, NULL,
12373                                          token->location);
12374           else if (check_for_bare_parameter_packs (decl))
12375             return false;
12376           else if (!at_namespace_scope_p ())
12377             do_local_using_decl (decl, qscope, identifier);
12378           else
12379             do_toplevel_using_decl (decl, qscope, identifier);
12380         }
12381     }
12382
12383   /* Look for the final `;'.  */
12384   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12385   
12386   return true;
12387 }
12388
12389 /* Parse a using-directive.
12390
12391    using-directive:
12392      using namespace :: [opt] nested-name-specifier [opt]
12393        namespace-name ;  */
12394
12395 static void
12396 cp_parser_using_directive (cp_parser* parser)
12397 {
12398   tree namespace_decl;
12399   tree attribs;
12400
12401   /* Look for the `using' keyword.  */
12402   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12403   /* And the `namespace' keyword.  */
12404   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12405   /* Look for the optional `::' operator.  */
12406   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12407   /* And the optional nested-name-specifier.  */
12408   cp_parser_nested_name_specifier_opt (parser,
12409                                        /*typename_keyword_p=*/false,
12410                                        /*check_dependency_p=*/true,
12411                                        /*type_p=*/false,
12412                                        /*is_declaration=*/true);
12413   /* Get the namespace being used.  */
12414   namespace_decl = cp_parser_namespace_name (parser);
12415   /* And any specified attributes.  */
12416   attribs = cp_parser_attributes_opt (parser);
12417   /* Update the symbol table.  */
12418   parse_using_directive (namespace_decl, attribs);
12419   /* Look for the final `;'.  */
12420   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12421 }
12422
12423 /* Parse an asm-definition.
12424
12425    asm-definition:
12426      asm ( string-literal ) ;
12427
12428    GNU Extension:
12429
12430    asm-definition:
12431      asm volatile [opt] ( string-literal ) ;
12432      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12433      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12434                           : asm-operand-list [opt] ) ;
12435      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12436                           : asm-operand-list [opt]
12437                           : asm-operand-list [opt] ) ;  */
12438
12439 static void
12440 cp_parser_asm_definition (cp_parser* parser)
12441 {
12442   tree string;
12443   tree outputs = NULL_TREE;
12444   tree inputs = NULL_TREE;
12445   tree clobbers = NULL_TREE;
12446   tree asm_stmt;
12447   bool volatile_p = false;
12448   bool extended_p = false;
12449   bool invalid_inputs_p = false;
12450   bool invalid_outputs_p = false;
12451
12452   /* Look for the `asm' keyword.  */
12453   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12454   /* See if the next token is `volatile'.  */
12455   if (cp_parser_allow_gnu_extensions_p (parser)
12456       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12457     {
12458       /* Remember that we saw the `volatile' keyword.  */
12459       volatile_p = true;
12460       /* Consume the token.  */
12461       cp_lexer_consume_token (parser->lexer);
12462     }
12463   /* Look for the opening `('.  */
12464   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12465     return;
12466   /* Look for the string.  */
12467   string = cp_parser_string_literal (parser, false, false);
12468   if (string == error_mark_node)
12469     {
12470       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12471                                              /*consume_paren=*/true);
12472       return;
12473     }
12474
12475   /* If we're allowing GNU extensions, check for the extended assembly
12476      syntax.  Unfortunately, the `:' tokens need not be separated by
12477      a space in C, and so, for compatibility, we tolerate that here
12478      too.  Doing that means that we have to treat the `::' operator as
12479      two `:' tokens.  */
12480   if (cp_parser_allow_gnu_extensions_p (parser)
12481       && parser->in_function_body
12482       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12483           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12484     {
12485       bool inputs_p = false;
12486       bool clobbers_p = false;
12487
12488       /* The extended syntax was used.  */
12489       extended_p = true;
12490
12491       /* Look for outputs.  */
12492       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12493         {
12494           /* Consume the `:'.  */
12495           cp_lexer_consume_token (parser->lexer);
12496           /* Parse the output-operands.  */
12497           if (cp_lexer_next_token_is_not (parser->lexer,
12498                                           CPP_COLON)
12499               && cp_lexer_next_token_is_not (parser->lexer,
12500                                              CPP_SCOPE)
12501               && cp_lexer_next_token_is_not (parser->lexer,
12502                                              CPP_CLOSE_PAREN))
12503             outputs = cp_parser_asm_operand_list (parser);
12504
12505             if (outputs == error_mark_node)
12506               invalid_outputs_p = true;
12507         }
12508       /* If the next token is `::', there are no outputs, and the
12509          next token is the beginning of the inputs.  */
12510       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12511         /* The inputs are coming next.  */
12512         inputs_p = true;
12513
12514       /* Look for inputs.  */
12515       if (inputs_p
12516           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12517         {
12518           /* Consume the `:' or `::'.  */
12519           cp_lexer_consume_token (parser->lexer);
12520           /* Parse the output-operands.  */
12521           if (cp_lexer_next_token_is_not (parser->lexer,
12522                                           CPP_COLON)
12523               && cp_lexer_next_token_is_not (parser->lexer,
12524                                              CPP_CLOSE_PAREN))
12525             inputs = cp_parser_asm_operand_list (parser);
12526
12527             if (inputs == error_mark_node)
12528               invalid_inputs_p = true;
12529         }
12530       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12531         /* The clobbers are coming next.  */
12532         clobbers_p = true;
12533
12534       /* Look for clobbers.  */
12535       if (clobbers_p
12536           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12537         {
12538           /* Consume the `:' or `::'.  */
12539           cp_lexer_consume_token (parser->lexer);
12540           /* Parse the clobbers.  */
12541           if (cp_lexer_next_token_is_not (parser->lexer,
12542                                           CPP_CLOSE_PAREN))
12543             clobbers = cp_parser_asm_clobber_list (parser);
12544         }
12545     }
12546   /* Look for the closing `)'.  */
12547   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12548     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12549                                            /*consume_paren=*/true);
12550   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12551
12552   if (!invalid_inputs_p && !invalid_outputs_p)
12553     {
12554       /* Create the ASM_EXPR.  */
12555       if (parser->in_function_body)
12556         {
12557           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12558                                       inputs, clobbers);
12559           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12560           if (!extended_p)
12561             {
12562               tree temp = asm_stmt;
12563               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12564                 temp = TREE_OPERAND (temp, 0);
12565
12566               ASM_INPUT_P (temp) = 1;
12567             }
12568         }
12569       else
12570         cgraph_add_asm_node (string);
12571     }
12572 }
12573
12574 /* Declarators [gram.dcl.decl] */
12575
12576 /* Parse an init-declarator.
12577
12578    init-declarator:
12579      declarator initializer [opt]
12580
12581    GNU Extension:
12582
12583    init-declarator:
12584      declarator asm-specification [opt] attributes [opt] initializer [opt]
12585
12586    function-definition:
12587      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12588        function-body
12589      decl-specifier-seq [opt] declarator function-try-block
12590
12591    GNU Extension:
12592
12593    function-definition:
12594      __extension__ function-definition
12595
12596    The DECL_SPECIFIERS apply to this declarator.  Returns a
12597    representation of the entity declared.  If MEMBER_P is TRUE, then
12598    this declarator appears in a class scope.  The new DECL created by
12599    this declarator is returned.
12600
12601    The CHECKS are access checks that should be performed once we know
12602    what entity is being declared (and, therefore, what classes have
12603    befriended it).
12604
12605    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12606    for a function-definition here as well.  If the declarator is a
12607    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12608    be TRUE upon return.  By that point, the function-definition will
12609    have been completely parsed.
12610
12611    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12612    is FALSE.  */
12613
12614 static tree
12615 cp_parser_init_declarator (cp_parser* parser,
12616                            cp_decl_specifier_seq *decl_specifiers,
12617                            VEC (deferred_access_check,gc)* checks,
12618                            bool function_definition_allowed_p,
12619                            bool member_p,
12620                            int declares_class_or_enum,
12621                            bool* function_definition_p)
12622 {
12623   cp_token *token = NULL, *asm_spec_start_token = NULL,
12624            *attributes_start_token = NULL;
12625   cp_declarator *declarator;
12626   tree prefix_attributes;
12627   tree attributes;
12628   tree asm_specification;
12629   tree initializer;
12630   tree decl = NULL_TREE;
12631   tree scope;
12632   int is_initialized;
12633   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12634      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12635      "(...)".  */
12636   enum cpp_ttype initialization_kind;
12637   bool is_direct_init = false;
12638   bool is_non_constant_init;
12639   int ctor_dtor_or_conv_p;
12640   bool friend_p;
12641   tree pushed_scope = NULL;
12642
12643   /* Gather the attributes that were provided with the
12644      decl-specifiers.  */
12645   prefix_attributes = decl_specifiers->attributes;
12646
12647   /* Assume that this is not the declarator for a function
12648      definition.  */
12649   if (function_definition_p)
12650     *function_definition_p = false;
12651
12652   /* Defer access checks while parsing the declarator; we cannot know
12653      what names are accessible until we know what is being
12654      declared.  */
12655   resume_deferring_access_checks ();
12656
12657   /* Parse the declarator.  */
12658   token = cp_lexer_peek_token (parser->lexer);
12659   declarator
12660     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12661                             &ctor_dtor_or_conv_p,
12662                             /*parenthesized_p=*/NULL,
12663                             /*member_p=*/false);
12664   /* Gather up the deferred checks.  */
12665   stop_deferring_access_checks ();
12666
12667   /* If the DECLARATOR was erroneous, there's no need to go
12668      further.  */
12669   if (declarator == cp_error_declarator)
12670     return error_mark_node;
12671
12672   /* Check that the number of template-parameter-lists is OK.  */
12673   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12674                                                        token->location))
12675     return error_mark_node;
12676
12677   if (declares_class_or_enum & 2)
12678     cp_parser_check_for_definition_in_return_type (declarator,
12679                                                    decl_specifiers->type,
12680                                                    decl_specifiers->type_location);
12681
12682   /* Figure out what scope the entity declared by the DECLARATOR is
12683      located in.  `grokdeclarator' sometimes changes the scope, so
12684      we compute it now.  */
12685   scope = get_scope_of_declarator (declarator);
12686
12687   /* If we're allowing GNU extensions, look for an asm-specification
12688      and attributes.  */
12689   if (cp_parser_allow_gnu_extensions_p (parser))
12690     {
12691       /* Look for an asm-specification.  */
12692       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12693       asm_specification = cp_parser_asm_specification_opt (parser);
12694       /* And attributes.  */
12695       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12696       attributes = cp_parser_attributes_opt (parser);
12697     }
12698   else
12699     {
12700       asm_specification = NULL_TREE;
12701       attributes = NULL_TREE;
12702     }
12703
12704   /* Peek at the next token.  */
12705   token = cp_lexer_peek_token (parser->lexer);
12706   /* Check to see if the token indicates the start of a
12707      function-definition.  */
12708   if (function_declarator_p (declarator)
12709       && cp_parser_token_starts_function_definition_p (token))
12710     {
12711       if (!function_definition_allowed_p)
12712         {
12713           /* If a function-definition should not appear here, issue an
12714              error message.  */
12715           cp_parser_error (parser,
12716                            "a function-definition is not allowed here");
12717           return error_mark_node;
12718         }
12719       else
12720         {
12721           location_t func_brace_location
12722             = cp_lexer_peek_token (parser->lexer)->location;
12723
12724           /* Neither attributes nor an asm-specification are allowed
12725              on a function-definition.  */
12726           if (asm_specification)
12727             error ("%Han asm-specification is not allowed "
12728                    "on a function-definition",
12729                    &asm_spec_start_token->location);
12730           if (attributes)
12731             error ("%Hattributes are not allowed on a function-definition",
12732                    &attributes_start_token->location);
12733           /* This is a function-definition.  */
12734           *function_definition_p = true;
12735
12736           /* Parse the function definition.  */
12737           if (member_p)
12738             decl = cp_parser_save_member_function_body (parser,
12739                                                         decl_specifiers,
12740                                                         declarator,
12741                                                         prefix_attributes);
12742           else
12743             decl
12744               = (cp_parser_function_definition_from_specifiers_and_declarator
12745                  (parser, decl_specifiers, prefix_attributes, declarator));
12746
12747           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12748             {
12749               /* This is where the prologue starts...  */
12750               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12751                 = func_brace_location;
12752             }
12753
12754           return decl;
12755         }
12756     }
12757
12758   /* [dcl.dcl]
12759
12760      Only in function declarations for constructors, destructors, and
12761      type conversions can the decl-specifier-seq be omitted.
12762
12763      We explicitly postpone this check past the point where we handle
12764      function-definitions because we tolerate function-definitions
12765      that are missing their return types in some modes.  */
12766   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12767     {
12768       cp_parser_error (parser,
12769                        "expected constructor, destructor, or type conversion");
12770       return error_mark_node;
12771     }
12772
12773   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12774   if (token->type == CPP_EQ
12775       || token->type == CPP_OPEN_PAREN
12776       || token->type == CPP_OPEN_BRACE)
12777     {
12778       is_initialized = SD_INITIALIZED;
12779       initialization_kind = token->type;
12780
12781       if (token->type == CPP_EQ
12782           && function_declarator_p (declarator))
12783         {
12784           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12785           if (t2->keyword == RID_DEFAULT)
12786             is_initialized = SD_DEFAULTED;
12787           else if (t2->keyword == RID_DELETE)
12788             is_initialized = SD_DELETED;
12789         }
12790     }
12791   else
12792     {
12793       /* If the init-declarator isn't initialized and isn't followed by a
12794          `,' or `;', it's not a valid init-declarator.  */
12795       if (token->type != CPP_COMMA
12796           && token->type != CPP_SEMICOLON)
12797         {
12798           cp_parser_error (parser, "expected initializer");
12799           return error_mark_node;
12800         }
12801       is_initialized = SD_UNINITIALIZED;
12802       initialization_kind = CPP_EOF;
12803     }
12804
12805   /* Because start_decl has side-effects, we should only call it if we
12806      know we're going ahead.  By this point, we know that we cannot
12807      possibly be looking at any other construct.  */
12808   cp_parser_commit_to_tentative_parse (parser);
12809
12810   /* If the decl specifiers were bad, issue an error now that we're
12811      sure this was intended to be a declarator.  Then continue
12812      declaring the variable(s), as int, to try to cut down on further
12813      errors.  */
12814   if (decl_specifiers->any_specifiers_p
12815       && decl_specifiers->type == error_mark_node)
12816     {
12817       cp_parser_error (parser, "invalid type in declaration");
12818       decl_specifiers->type = integer_type_node;
12819     }
12820
12821   /* Check to see whether or not this declaration is a friend.  */
12822   friend_p = cp_parser_friend_p (decl_specifiers);
12823
12824   /* Enter the newly declared entry in the symbol table.  If we're
12825      processing a declaration in a class-specifier, we wait until
12826      after processing the initializer.  */
12827   if (!member_p)
12828     {
12829       if (parser->in_unbraced_linkage_specification_p)
12830         decl_specifiers->storage_class = sc_extern;
12831       decl = start_decl (declarator, decl_specifiers,
12832                          is_initialized, attributes, prefix_attributes,
12833                          &pushed_scope);
12834     }
12835   else if (scope)
12836     /* Enter the SCOPE.  That way unqualified names appearing in the
12837        initializer will be looked up in SCOPE.  */
12838     pushed_scope = push_scope (scope);
12839
12840   /* Perform deferred access control checks, now that we know in which
12841      SCOPE the declared entity resides.  */
12842   if (!member_p && decl)
12843     {
12844       tree saved_current_function_decl = NULL_TREE;
12845
12846       /* If the entity being declared is a function, pretend that we
12847          are in its scope.  If it is a `friend', it may have access to
12848          things that would not otherwise be accessible.  */
12849       if (TREE_CODE (decl) == FUNCTION_DECL)
12850         {
12851           saved_current_function_decl = current_function_decl;
12852           current_function_decl = decl;
12853         }
12854
12855       /* Perform access checks for template parameters.  */
12856       cp_parser_perform_template_parameter_access_checks (checks);
12857
12858       /* Perform the access control checks for the declarator and the
12859          decl-specifiers.  */
12860       perform_deferred_access_checks ();
12861
12862       /* Restore the saved value.  */
12863       if (TREE_CODE (decl) == FUNCTION_DECL)
12864         current_function_decl = saved_current_function_decl;
12865     }
12866
12867   /* Parse the initializer.  */
12868   initializer = NULL_TREE;
12869   is_direct_init = false;
12870   is_non_constant_init = true;
12871   if (is_initialized)
12872     {
12873       if (function_declarator_p (declarator))
12874         {
12875           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12876            if (initialization_kind == CPP_EQ)
12877              initializer = cp_parser_pure_specifier (parser);
12878            else
12879              {
12880                /* If the declaration was erroneous, we don't really
12881                   know what the user intended, so just silently
12882                   consume the initializer.  */
12883                if (decl != error_mark_node)
12884                  error ("%Hinitializer provided for function",
12885                         &initializer_start_token->location);
12886                cp_parser_skip_to_closing_parenthesis (parser,
12887                                                       /*recovering=*/true,
12888                                                       /*or_comma=*/false,
12889                                                       /*consume_paren=*/true);
12890              }
12891         }
12892       else
12893         initializer = cp_parser_initializer (parser,
12894                                              &is_direct_init,
12895                                              &is_non_constant_init);
12896     }
12897
12898   /* The old parser allows attributes to appear after a parenthesized
12899      initializer.  Mark Mitchell proposed removing this functionality
12900      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12901      attributes -- but ignores them.  */
12902   if (cp_parser_allow_gnu_extensions_p (parser)
12903       && initialization_kind == CPP_OPEN_PAREN)
12904     if (cp_parser_attributes_opt (parser))
12905       warning (OPT_Wattributes,
12906                "attributes after parenthesized initializer ignored");
12907
12908   /* For an in-class declaration, use `grokfield' to create the
12909      declaration.  */
12910   if (member_p)
12911     {
12912       if (pushed_scope)
12913         {
12914           pop_scope (pushed_scope);
12915           pushed_scope = false;
12916         }
12917       decl = grokfield (declarator, decl_specifiers,
12918                         initializer, !is_non_constant_init,
12919                         /*asmspec=*/NULL_TREE,
12920                         prefix_attributes);
12921       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12922         cp_parser_save_default_args (parser, decl);
12923     }
12924
12925   /* Finish processing the declaration.  But, skip friend
12926      declarations.  */
12927   if (!friend_p && decl && decl != error_mark_node)
12928     {
12929       cp_finish_decl (decl,
12930                       initializer, !is_non_constant_init,
12931                       asm_specification,
12932                       /* If the initializer is in parentheses, then this is
12933                          a direct-initialization, which means that an
12934                          `explicit' constructor is OK.  Otherwise, an
12935                          `explicit' constructor cannot be used.  */
12936                       ((is_direct_init || !is_initialized)
12937                        ? 0 : LOOKUP_ONLYCONVERTING));
12938     }
12939   else if ((cxx_dialect != cxx98) && friend_p
12940            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12941     /* Core issue #226 (C++0x only): A default template-argument
12942        shall not be specified in a friend class template
12943        declaration. */
12944     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12945                              /*is_partial=*/0, /*is_friend_decl=*/1);
12946
12947   if (!friend_p && pushed_scope)
12948     pop_scope (pushed_scope);
12949
12950   return decl;
12951 }
12952
12953 /* Parse a declarator.
12954
12955    declarator:
12956      direct-declarator
12957      ptr-operator declarator
12958
12959    abstract-declarator:
12960      ptr-operator abstract-declarator [opt]
12961      direct-abstract-declarator
12962
12963    GNU Extensions:
12964
12965    declarator:
12966      attributes [opt] direct-declarator
12967      attributes [opt] ptr-operator declarator
12968
12969    abstract-declarator:
12970      attributes [opt] ptr-operator abstract-declarator [opt]
12971      attributes [opt] direct-abstract-declarator
12972
12973    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12974    detect constructor, destructor or conversion operators. It is set
12975    to -1 if the declarator is a name, and +1 if it is a
12976    function. Otherwise it is set to zero. Usually you just want to
12977    test for >0, but internally the negative value is used.
12978
12979    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12980    a decl-specifier-seq unless it declares a constructor, destructor,
12981    or conversion.  It might seem that we could check this condition in
12982    semantic analysis, rather than parsing, but that makes it difficult
12983    to handle something like `f()'.  We want to notice that there are
12984    no decl-specifiers, and therefore realize that this is an
12985    expression, not a declaration.)
12986
12987    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12988    the declarator is a direct-declarator of the form "(...)".
12989
12990    MEMBER_P is true iff this declarator is a member-declarator.  */
12991
12992 static cp_declarator *
12993 cp_parser_declarator (cp_parser* parser,
12994                       cp_parser_declarator_kind dcl_kind,
12995                       int* ctor_dtor_or_conv_p,
12996                       bool* parenthesized_p,
12997                       bool member_p)
12998 {
12999   cp_token *token;
13000   cp_declarator *declarator;
13001   enum tree_code code;
13002   cp_cv_quals cv_quals;
13003   tree class_type;
13004   tree attributes = NULL_TREE;
13005
13006   /* Assume this is not a constructor, destructor, or type-conversion
13007      operator.  */
13008   if (ctor_dtor_or_conv_p)
13009     *ctor_dtor_or_conv_p = 0;
13010
13011   if (cp_parser_allow_gnu_extensions_p (parser))
13012     attributes = cp_parser_attributes_opt (parser);
13013
13014   /* Peek at the next token.  */
13015   token = cp_lexer_peek_token (parser->lexer);
13016
13017   /* Check for the ptr-operator production.  */
13018   cp_parser_parse_tentatively (parser);
13019   /* Parse the ptr-operator.  */
13020   code = cp_parser_ptr_operator (parser,
13021                                  &class_type,
13022                                  &cv_quals);
13023   /* If that worked, then we have a ptr-operator.  */
13024   if (cp_parser_parse_definitely (parser))
13025     {
13026       /* If a ptr-operator was found, then this declarator was not
13027          parenthesized.  */
13028       if (parenthesized_p)
13029         *parenthesized_p = true;
13030       /* The dependent declarator is optional if we are parsing an
13031          abstract-declarator.  */
13032       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13033         cp_parser_parse_tentatively (parser);
13034
13035       /* Parse the dependent declarator.  */
13036       declarator = cp_parser_declarator (parser, dcl_kind,
13037                                          /*ctor_dtor_or_conv_p=*/NULL,
13038                                          /*parenthesized_p=*/NULL,
13039                                          /*member_p=*/false);
13040
13041       /* If we are parsing an abstract-declarator, we must handle the
13042          case where the dependent declarator is absent.  */
13043       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13044           && !cp_parser_parse_definitely (parser))
13045         declarator = NULL;
13046
13047       declarator = cp_parser_make_indirect_declarator
13048         (code, class_type, cv_quals, declarator);
13049     }
13050   /* Everything else is a direct-declarator.  */
13051   else
13052     {
13053       if (parenthesized_p)
13054         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13055                                                    CPP_OPEN_PAREN);
13056       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13057                                                 ctor_dtor_or_conv_p,
13058                                                 member_p);
13059     }
13060
13061   if (attributes && declarator && declarator != cp_error_declarator)
13062     declarator->attributes = attributes;
13063
13064   return declarator;
13065 }
13066
13067 /* Parse a direct-declarator or direct-abstract-declarator.
13068
13069    direct-declarator:
13070      declarator-id
13071      direct-declarator ( parameter-declaration-clause )
13072        cv-qualifier-seq [opt]
13073        exception-specification [opt]
13074      direct-declarator [ constant-expression [opt] ]
13075      ( declarator )
13076
13077    direct-abstract-declarator:
13078      direct-abstract-declarator [opt]
13079        ( parameter-declaration-clause )
13080        cv-qualifier-seq [opt]
13081        exception-specification [opt]
13082      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13083      ( abstract-declarator )
13084
13085    Returns a representation of the declarator.  DCL_KIND is
13086    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13087    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13088    we are parsing a direct-declarator.  It is
13089    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13090    of ambiguity we prefer an abstract declarator, as per
13091    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13092    cp_parser_declarator.  */
13093
13094 static cp_declarator *
13095 cp_parser_direct_declarator (cp_parser* parser,
13096                              cp_parser_declarator_kind dcl_kind,
13097                              int* ctor_dtor_or_conv_p,
13098                              bool member_p)
13099 {
13100   cp_token *token;
13101   cp_declarator *declarator = NULL;
13102   tree scope = NULL_TREE;
13103   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13104   bool saved_in_declarator_p = parser->in_declarator_p;
13105   bool first = true;
13106   tree pushed_scope = NULL_TREE;
13107
13108   while (true)
13109     {
13110       /* Peek at the next token.  */
13111       token = cp_lexer_peek_token (parser->lexer);
13112       if (token->type == CPP_OPEN_PAREN)
13113         {
13114           /* This is either a parameter-declaration-clause, or a
13115              parenthesized declarator. When we know we are parsing a
13116              named declarator, it must be a parenthesized declarator
13117              if FIRST is true. For instance, `(int)' is a
13118              parameter-declaration-clause, with an omitted
13119              direct-abstract-declarator. But `((*))', is a
13120              parenthesized abstract declarator. Finally, when T is a
13121              template parameter `(T)' is a
13122              parameter-declaration-clause, and not a parenthesized
13123              named declarator.
13124
13125              We first try and parse a parameter-declaration-clause,
13126              and then try a nested declarator (if FIRST is true).
13127
13128              It is not an error for it not to be a
13129              parameter-declaration-clause, even when FIRST is
13130              false. Consider,
13131
13132                int i (int);
13133                int i (3);
13134
13135              The first is the declaration of a function while the
13136              second is the definition of a variable, including its
13137              initializer.
13138
13139              Having seen only the parenthesis, we cannot know which of
13140              these two alternatives should be selected.  Even more
13141              complex are examples like:
13142
13143                int i (int (a));
13144                int i (int (3));
13145
13146              The former is a function-declaration; the latter is a
13147              variable initialization.
13148
13149              Thus again, we try a parameter-declaration-clause, and if
13150              that fails, we back out and return.  */
13151
13152           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13153             {
13154               tree params;
13155               unsigned saved_num_template_parameter_lists;
13156               bool is_declarator = false;
13157               tree t;
13158
13159               /* In a member-declarator, the only valid interpretation
13160                  of a parenthesis is the start of a
13161                  parameter-declaration-clause.  (It is invalid to
13162                  initialize a static data member with a parenthesized
13163                  initializer; only the "=" form of initialization is
13164                  permitted.)  */
13165               if (!member_p)
13166                 cp_parser_parse_tentatively (parser);
13167
13168               /* Consume the `('.  */
13169               cp_lexer_consume_token (parser->lexer);
13170               if (first)
13171                 {
13172                   /* If this is going to be an abstract declarator, we're
13173                      in a declarator and we can't have default args.  */
13174                   parser->default_arg_ok_p = false;
13175                   parser->in_declarator_p = true;
13176                 }
13177
13178               /* Inside the function parameter list, surrounding
13179                  template-parameter-lists do not apply.  */
13180               saved_num_template_parameter_lists
13181                 = parser->num_template_parameter_lists;
13182               parser->num_template_parameter_lists = 0;
13183
13184               begin_scope (sk_function_parms, NULL_TREE);
13185
13186               /* Parse the parameter-declaration-clause.  */
13187               params = cp_parser_parameter_declaration_clause (parser);
13188
13189               parser->num_template_parameter_lists
13190                 = saved_num_template_parameter_lists;
13191
13192               /* If all went well, parse the cv-qualifier-seq and the
13193                  exception-specification.  */
13194               if (member_p || cp_parser_parse_definitely (parser))
13195                 {
13196                   cp_cv_quals cv_quals;
13197                   tree exception_specification;
13198                   tree late_return;
13199
13200                   is_declarator = true;
13201
13202                   if (ctor_dtor_or_conv_p)
13203                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13204                   first = false;
13205                   /* Consume the `)'.  */
13206                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13207
13208                   /* Parse the cv-qualifier-seq.  */
13209                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13210                   /* And the exception-specification.  */
13211                   exception_specification
13212                     = cp_parser_exception_specification_opt (parser);
13213
13214                   late_return
13215                     = cp_parser_late_return_type_opt (parser);
13216
13217                   /* Create the function-declarator.  */
13218                   declarator = make_call_declarator (declarator,
13219                                                      params,
13220                                                      cv_quals,
13221                                                      exception_specification,
13222                                                      late_return);
13223                   /* Any subsequent parameter lists are to do with
13224                      return type, so are not those of the declared
13225                      function.  */
13226                   parser->default_arg_ok_p = false;
13227                 }
13228
13229               /* Remove the function parms from scope.  */
13230               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13231                 pop_binding (DECL_NAME (t), t);
13232               leave_scope();
13233
13234               if (is_declarator)
13235                 /* Repeat the main loop.  */
13236                 continue;
13237             }
13238
13239           /* If this is the first, we can try a parenthesized
13240              declarator.  */
13241           if (first)
13242             {
13243               bool saved_in_type_id_in_expr_p;
13244
13245               parser->default_arg_ok_p = saved_default_arg_ok_p;
13246               parser->in_declarator_p = saved_in_declarator_p;
13247
13248               /* Consume the `('.  */
13249               cp_lexer_consume_token (parser->lexer);
13250               /* Parse the nested declarator.  */
13251               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13252               parser->in_type_id_in_expr_p = true;
13253               declarator
13254                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13255                                         /*parenthesized_p=*/NULL,
13256                                         member_p);
13257               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13258               first = false;
13259               /* Expect a `)'.  */
13260               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13261                 declarator = cp_error_declarator;
13262               if (declarator == cp_error_declarator)
13263                 break;
13264
13265               goto handle_declarator;
13266             }
13267           /* Otherwise, we must be done.  */
13268           else
13269             break;
13270         }
13271       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13272                && token->type == CPP_OPEN_SQUARE)
13273         {
13274           /* Parse an array-declarator.  */
13275           tree bounds;
13276
13277           if (ctor_dtor_or_conv_p)
13278             *ctor_dtor_or_conv_p = 0;
13279
13280           first = false;
13281           parser->default_arg_ok_p = false;
13282           parser->in_declarator_p = true;
13283           /* Consume the `['.  */
13284           cp_lexer_consume_token (parser->lexer);
13285           /* Peek at the next token.  */
13286           token = cp_lexer_peek_token (parser->lexer);
13287           /* If the next token is `]', then there is no
13288              constant-expression.  */
13289           if (token->type != CPP_CLOSE_SQUARE)
13290             {
13291               bool non_constant_p;
13292
13293               bounds
13294                 = cp_parser_constant_expression (parser,
13295                                                  /*allow_non_constant=*/true,
13296                                                  &non_constant_p);
13297               if (!non_constant_p)
13298                 bounds = fold_non_dependent_expr (bounds);
13299               /* Normally, the array bound must be an integral constant
13300                  expression.  However, as an extension, we allow VLAs
13301                  in function scopes.  */
13302               else if (!parser->in_function_body)
13303                 {
13304                   error ("%Harray bound is not an integer constant",
13305                          &token->location);
13306                   bounds = error_mark_node;
13307                 }
13308               else if (processing_template_decl && !error_operand_p (bounds))
13309                 {
13310                   /* Remember this wasn't a constant-expression.  */
13311                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13312                   TREE_SIDE_EFFECTS (bounds) = 1;
13313                 }
13314             }
13315           else
13316             bounds = NULL_TREE;
13317           /* Look for the closing `]'.  */
13318           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13319             {
13320               declarator = cp_error_declarator;
13321               break;
13322             }
13323
13324           declarator = make_array_declarator (declarator, bounds);
13325         }
13326       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13327         {
13328           tree qualifying_scope;
13329           tree unqualified_name;
13330           special_function_kind sfk;
13331           bool abstract_ok;
13332           bool pack_expansion_p = false;
13333           cp_token *declarator_id_start_token;
13334
13335           /* Parse a declarator-id */
13336           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13337           if (abstract_ok)
13338             {
13339               cp_parser_parse_tentatively (parser);
13340
13341               /* If we see an ellipsis, we should be looking at a
13342                  parameter pack. */
13343               if (token->type == CPP_ELLIPSIS)
13344                 {
13345                   /* Consume the `...' */
13346                   cp_lexer_consume_token (parser->lexer);
13347
13348                   pack_expansion_p = true;
13349                 }
13350             }
13351
13352           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13353           unqualified_name
13354             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13355           qualifying_scope = parser->scope;
13356           if (abstract_ok)
13357             {
13358               bool okay = false;
13359
13360               if (!unqualified_name && pack_expansion_p)
13361                 {
13362                   /* Check whether an error occurred. */
13363                   okay = !cp_parser_error_occurred (parser);
13364
13365                   /* We already consumed the ellipsis to mark a
13366                      parameter pack, but we have no way to report it,
13367                      so abort the tentative parse. We will be exiting
13368                      immediately anyway. */
13369                   cp_parser_abort_tentative_parse (parser);
13370                 }
13371               else
13372                 okay = cp_parser_parse_definitely (parser);
13373
13374               if (!okay)
13375                 unqualified_name = error_mark_node;
13376               else if (unqualified_name
13377                        && (qualifying_scope
13378                            || (TREE_CODE (unqualified_name)
13379                                != IDENTIFIER_NODE)))
13380                 {
13381                   cp_parser_error (parser, "expected unqualified-id");
13382                   unqualified_name = error_mark_node;
13383                 }
13384             }
13385
13386           if (!unqualified_name)
13387             return NULL;
13388           if (unqualified_name == error_mark_node)
13389             {
13390               declarator = cp_error_declarator;
13391               pack_expansion_p = false;
13392               declarator->parameter_pack_p = false;
13393               break;
13394             }
13395
13396           if (qualifying_scope && at_namespace_scope_p ()
13397               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13398             {
13399               /* In the declaration of a member of a template class
13400                  outside of the class itself, the SCOPE will sometimes
13401                  be a TYPENAME_TYPE.  For example, given:
13402
13403                  template <typename T>
13404                  int S<T>::R::i = 3;
13405
13406                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13407                  this context, we must resolve S<T>::R to an ordinary
13408                  type, rather than a typename type.
13409
13410                  The reason we normally avoid resolving TYPENAME_TYPEs
13411                  is that a specialization of `S' might render
13412                  `S<T>::R' not a type.  However, if `S' is
13413                  specialized, then this `i' will not be used, so there
13414                  is no harm in resolving the types here.  */
13415               tree type;
13416
13417               /* Resolve the TYPENAME_TYPE.  */
13418               type = resolve_typename_type (qualifying_scope,
13419                                             /*only_current_p=*/false);
13420               /* If that failed, the declarator is invalid.  */
13421               if (TREE_CODE (type) == TYPENAME_TYPE)
13422                 error ("%H%<%T::%E%> is not a type",
13423                        &declarator_id_start_token->location,
13424                        TYPE_CONTEXT (qualifying_scope),
13425                        TYPE_IDENTIFIER (qualifying_scope));
13426               qualifying_scope = type;
13427             }
13428
13429           sfk = sfk_none;
13430
13431           if (unqualified_name)
13432             {
13433               tree class_type;
13434
13435               if (qualifying_scope
13436                   && CLASS_TYPE_P (qualifying_scope))
13437                 class_type = qualifying_scope;
13438               else
13439                 class_type = current_class_type;
13440
13441               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13442                 {
13443                   tree name_type = TREE_TYPE (unqualified_name);
13444                   if (class_type && same_type_p (name_type, class_type))
13445                     {
13446                       if (qualifying_scope
13447                           && CLASSTYPE_USE_TEMPLATE (name_type))
13448                         {
13449                           error ("%Hinvalid use of constructor as a template",
13450                                  &declarator_id_start_token->location);
13451                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13452                                   "name the constructor in a qualified name",
13453                                   class_type,
13454                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13455                                   class_type, name_type);
13456                           declarator = cp_error_declarator;
13457                           break;
13458                         }
13459                       else
13460                         unqualified_name = constructor_name (class_type);
13461                     }
13462                   else
13463                     {
13464                       /* We do not attempt to print the declarator
13465                          here because we do not have enough
13466                          information about its original syntactic
13467                          form.  */
13468                       cp_parser_error (parser, "invalid declarator");
13469                       declarator = cp_error_declarator;
13470                       break;
13471                     }
13472                 }
13473
13474               if (class_type)
13475                 {
13476                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13477                     sfk = sfk_destructor;
13478                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13479                     sfk = sfk_conversion;
13480                   else if (/* There's no way to declare a constructor
13481                               for an anonymous type, even if the type
13482                               got a name for linkage purposes.  */
13483                            !TYPE_WAS_ANONYMOUS (class_type)
13484                            && constructor_name_p (unqualified_name,
13485                                                   class_type))
13486                     {
13487                       unqualified_name = constructor_name (class_type);
13488                       sfk = sfk_constructor;
13489                     }
13490
13491                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13492                     *ctor_dtor_or_conv_p = -1;
13493                 }
13494             }
13495           declarator = make_id_declarator (qualifying_scope,
13496                                            unqualified_name,
13497                                            sfk);
13498           declarator->id_loc = token->location;
13499           declarator->parameter_pack_p = pack_expansion_p;
13500
13501           if (pack_expansion_p)
13502             maybe_warn_variadic_templates ();
13503
13504         handle_declarator:;
13505           scope = get_scope_of_declarator (declarator);
13506           if (scope)
13507             /* Any names that appear after the declarator-id for a
13508                member are looked up in the containing scope.  */
13509             pushed_scope = push_scope (scope);
13510           parser->in_declarator_p = true;
13511           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13512               || (declarator && declarator->kind == cdk_id))
13513             /* Default args are only allowed on function
13514                declarations.  */
13515             parser->default_arg_ok_p = saved_default_arg_ok_p;
13516           else
13517             parser->default_arg_ok_p = false;
13518
13519           first = false;
13520         }
13521       /* We're done.  */
13522       else
13523         break;
13524     }
13525
13526   /* For an abstract declarator, we might wind up with nothing at this
13527      point.  That's an error; the declarator is not optional.  */
13528   if (!declarator)
13529     cp_parser_error (parser, "expected declarator");
13530
13531   /* If we entered a scope, we must exit it now.  */
13532   if (pushed_scope)
13533     pop_scope (pushed_scope);
13534
13535   parser->default_arg_ok_p = saved_default_arg_ok_p;
13536   parser->in_declarator_p = saved_in_declarator_p;
13537
13538   return declarator;
13539 }
13540
13541 /* Parse a ptr-operator.
13542
13543    ptr-operator:
13544      * cv-qualifier-seq [opt]
13545      &
13546      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13547
13548    GNU Extension:
13549
13550    ptr-operator:
13551      & cv-qualifier-seq [opt]
13552
13553    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13554    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13555    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13556    filled in with the TYPE containing the member.  *CV_QUALS is
13557    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13558    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13559    Note that the tree codes returned by this function have nothing
13560    to do with the types of trees that will be eventually be created
13561    to represent the pointer or reference type being parsed. They are
13562    just constants with suggestive names. */
13563 static enum tree_code
13564 cp_parser_ptr_operator (cp_parser* parser,
13565                         tree* type,
13566                         cp_cv_quals *cv_quals)
13567 {
13568   enum tree_code code = ERROR_MARK;
13569   cp_token *token;
13570
13571   /* Assume that it's not a pointer-to-member.  */
13572   *type = NULL_TREE;
13573   /* And that there are no cv-qualifiers.  */
13574   *cv_quals = TYPE_UNQUALIFIED;
13575
13576   /* Peek at the next token.  */
13577   token = cp_lexer_peek_token (parser->lexer);
13578
13579   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13580   if (token->type == CPP_MULT)
13581     code = INDIRECT_REF;
13582   else if (token->type == CPP_AND)
13583     code = ADDR_EXPR;
13584   else if ((cxx_dialect != cxx98) &&
13585            token->type == CPP_AND_AND) /* C++0x only */
13586     code = NON_LVALUE_EXPR;
13587
13588   if (code != ERROR_MARK)
13589     {
13590       /* Consume the `*', `&' or `&&'.  */
13591       cp_lexer_consume_token (parser->lexer);
13592
13593       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13594          `&', if we are allowing GNU extensions.  (The only qualifier
13595          that can legally appear after `&' is `restrict', but that is
13596          enforced during semantic analysis.  */
13597       if (code == INDIRECT_REF
13598           || cp_parser_allow_gnu_extensions_p (parser))
13599         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13600     }
13601   else
13602     {
13603       /* Try the pointer-to-member case.  */
13604       cp_parser_parse_tentatively (parser);
13605       /* Look for the optional `::' operator.  */
13606       cp_parser_global_scope_opt (parser,
13607                                   /*current_scope_valid_p=*/false);
13608       /* Look for the nested-name specifier.  */
13609       token = cp_lexer_peek_token (parser->lexer);
13610       cp_parser_nested_name_specifier (parser,
13611                                        /*typename_keyword_p=*/false,
13612                                        /*check_dependency_p=*/true,
13613                                        /*type_p=*/false,
13614                                        /*is_declaration=*/false);
13615       /* If we found it, and the next token is a `*', then we are
13616          indeed looking at a pointer-to-member operator.  */
13617       if (!cp_parser_error_occurred (parser)
13618           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13619         {
13620           /* Indicate that the `*' operator was used.  */
13621           code = INDIRECT_REF;
13622
13623           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13624             error ("%H%qD is a namespace", &token->location, parser->scope);
13625           else
13626             {
13627               /* The type of which the member is a member is given by the
13628                  current SCOPE.  */
13629               *type = parser->scope;
13630               /* The next name will not be qualified.  */
13631               parser->scope = NULL_TREE;
13632               parser->qualifying_scope = NULL_TREE;
13633               parser->object_scope = NULL_TREE;
13634               /* Look for the optional cv-qualifier-seq.  */
13635               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13636             }
13637         }
13638       /* If that didn't work we don't have a ptr-operator.  */
13639       if (!cp_parser_parse_definitely (parser))
13640         cp_parser_error (parser, "expected ptr-operator");
13641     }
13642
13643   return code;
13644 }
13645
13646 /* Parse an (optional) cv-qualifier-seq.
13647
13648    cv-qualifier-seq:
13649      cv-qualifier cv-qualifier-seq [opt]
13650
13651    cv-qualifier:
13652      const
13653      volatile
13654
13655    GNU Extension:
13656
13657    cv-qualifier:
13658      __restrict__
13659
13660    Returns a bitmask representing the cv-qualifiers.  */
13661
13662 static cp_cv_quals
13663 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13664 {
13665   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13666
13667   while (true)
13668     {
13669       cp_token *token;
13670       cp_cv_quals cv_qualifier;
13671
13672       /* Peek at the next token.  */
13673       token = cp_lexer_peek_token (parser->lexer);
13674       /* See if it's a cv-qualifier.  */
13675       switch (token->keyword)
13676         {
13677         case RID_CONST:
13678           cv_qualifier = TYPE_QUAL_CONST;
13679           break;
13680
13681         case RID_VOLATILE:
13682           cv_qualifier = TYPE_QUAL_VOLATILE;
13683           break;
13684
13685         case RID_RESTRICT:
13686           cv_qualifier = TYPE_QUAL_RESTRICT;
13687           break;
13688
13689         default:
13690           cv_qualifier = TYPE_UNQUALIFIED;
13691           break;
13692         }
13693
13694       if (!cv_qualifier)
13695         break;
13696
13697       if (cv_quals & cv_qualifier)
13698         {
13699           error ("%Hduplicate cv-qualifier", &token->location);
13700           cp_lexer_purge_token (parser->lexer);
13701         }
13702       else
13703         {
13704           cp_lexer_consume_token (parser->lexer);
13705           cv_quals |= cv_qualifier;
13706         }
13707     }
13708
13709   return cv_quals;
13710 }
13711
13712 /* Parse a late-specified return type, if any.  This is not a separate
13713    non-terminal, but part of a function declarator, which looks like
13714
13715    -> type-id
13716
13717    Returns the type indicated by the type-id.  */
13718
13719 static tree
13720 cp_parser_late_return_type_opt (cp_parser* parser)
13721 {
13722   cp_token *token;
13723
13724   /* Peek at the next token.  */
13725   token = cp_lexer_peek_token (parser->lexer);
13726   /* A late-specified return type is indicated by an initial '->'. */
13727   if (token->type != CPP_DEREF)
13728     return NULL_TREE;
13729
13730   /* Consume the ->.  */
13731   cp_lexer_consume_token (parser->lexer);
13732
13733   return cp_parser_type_id (parser);
13734 }
13735
13736 /* Parse a declarator-id.
13737
13738    declarator-id:
13739      id-expression
13740      :: [opt] nested-name-specifier [opt] type-name
13741
13742    In the `id-expression' case, the value returned is as for
13743    cp_parser_id_expression if the id-expression was an unqualified-id.
13744    If the id-expression was a qualified-id, then a SCOPE_REF is
13745    returned.  The first operand is the scope (either a NAMESPACE_DECL
13746    or TREE_TYPE), but the second is still just a representation of an
13747    unqualified-id.  */
13748
13749 static tree
13750 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13751 {
13752   tree id;
13753   /* The expression must be an id-expression.  Assume that qualified
13754      names are the names of types so that:
13755
13756        template <class T>
13757        int S<T>::R::i = 3;
13758
13759      will work; we must treat `S<T>::R' as the name of a type.
13760      Similarly, assume that qualified names are templates, where
13761      required, so that:
13762
13763        template <class T>
13764        int S<T>::R<T>::i = 3;
13765
13766      will work, too.  */
13767   id = cp_parser_id_expression (parser,
13768                                 /*template_keyword_p=*/false,
13769                                 /*check_dependency_p=*/false,
13770                                 /*template_p=*/NULL,
13771                                 /*declarator_p=*/true,
13772                                 optional_p);
13773   if (id && BASELINK_P (id))
13774     id = BASELINK_FUNCTIONS (id);
13775   return id;
13776 }
13777
13778 /* Parse a type-id.
13779
13780    type-id:
13781      type-specifier-seq abstract-declarator [opt]
13782
13783    Returns the TYPE specified.  */
13784
13785 static tree
13786 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13787 {
13788   cp_decl_specifier_seq type_specifier_seq;
13789   cp_declarator *abstract_declarator;
13790
13791   /* Parse the type-specifier-seq.  */
13792   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13793                                 &type_specifier_seq);
13794   if (type_specifier_seq.type == error_mark_node)
13795     return error_mark_node;
13796
13797   /* There might or might not be an abstract declarator.  */
13798   cp_parser_parse_tentatively (parser);
13799   /* Look for the declarator.  */
13800   abstract_declarator
13801     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13802                             /*parenthesized_p=*/NULL,
13803                             /*member_p=*/false);
13804   /* Check to see if there really was a declarator.  */
13805   if (!cp_parser_parse_definitely (parser))
13806     abstract_declarator = NULL;
13807
13808   if (type_specifier_seq.type
13809       && type_uses_auto (type_specifier_seq.type))
13810     {
13811       /* A type-id with type 'auto' is only ok if the abstract declarator
13812          is a function declarator with a late-specified return type.  */
13813       if (abstract_declarator
13814           && abstract_declarator->kind == cdk_function
13815           && abstract_declarator->u.function.late_return_type)
13816         /* OK */;
13817       else
13818         {
13819           error ("invalid use of %<auto%>");
13820           return error_mark_node;
13821         }
13822     }
13823   
13824   return groktypename (&type_specifier_seq, abstract_declarator,
13825                        is_template_arg);
13826 }
13827
13828 static tree cp_parser_type_id (cp_parser *parser)
13829 {
13830   return cp_parser_type_id_1 (parser, false);
13831 }
13832
13833 static tree cp_parser_template_type_arg (cp_parser *parser)
13834 {
13835   return cp_parser_type_id_1 (parser, true);
13836 }
13837
13838 /* Parse a type-specifier-seq.
13839
13840    type-specifier-seq:
13841      type-specifier type-specifier-seq [opt]
13842
13843    GNU extension:
13844
13845    type-specifier-seq:
13846      attributes type-specifier-seq [opt]
13847
13848    If IS_CONDITION is true, we are at the start of a "condition",
13849    e.g., we've just seen "if (".
13850
13851    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13852
13853 static void
13854 cp_parser_type_specifier_seq (cp_parser* parser,
13855                               bool is_condition,
13856                               cp_decl_specifier_seq *type_specifier_seq)
13857 {
13858   bool seen_type_specifier = false;
13859   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13860   cp_token *start_token = NULL;
13861
13862   /* Clear the TYPE_SPECIFIER_SEQ.  */
13863   clear_decl_specs (type_specifier_seq);
13864
13865   /* Parse the type-specifiers and attributes.  */
13866   while (true)
13867     {
13868       tree type_specifier;
13869       bool is_cv_qualifier;
13870
13871       /* Check for attributes first.  */
13872       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13873         {
13874           type_specifier_seq->attributes =
13875             chainon (type_specifier_seq->attributes,
13876                      cp_parser_attributes_opt (parser));
13877           continue;
13878         }
13879
13880       /* record the token of the beginning of the type specifier seq,
13881          for error reporting purposes*/
13882      if (!start_token)
13883        start_token = cp_lexer_peek_token (parser->lexer);
13884
13885       /* Look for the type-specifier.  */
13886       type_specifier = cp_parser_type_specifier (parser,
13887                                                  flags,
13888                                                  type_specifier_seq,
13889                                                  /*is_declaration=*/false,
13890                                                  NULL,
13891                                                  &is_cv_qualifier);
13892       if (!type_specifier)
13893         {
13894           /* If the first type-specifier could not be found, this is not a
13895              type-specifier-seq at all.  */
13896           if (!seen_type_specifier)
13897             {
13898               cp_parser_error (parser, "expected type-specifier");
13899               type_specifier_seq->type = error_mark_node;
13900               return;
13901             }
13902           /* If subsequent type-specifiers could not be found, the
13903              type-specifier-seq is complete.  */
13904           break;
13905         }
13906
13907       seen_type_specifier = true;
13908       /* The standard says that a condition can be:
13909
13910             type-specifier-seq declarator = assignment-expression
13911
13912          However, given:
13913
13914            struct S {};
13915            if (int S = ...)
13916
13917          we should treat the "S" as a declarator, not as a
13918          type-specifier.  The standard doesn't say that explicitly for
13919          type-specifier-seq, but it does say that for
13920          decl-specifier-seq in an ordinary declaration.  Perhaps it
13921          would be clearer just to allow a decl-specifier-seq here, and
13922          then add a semantic restriction that if any decl-specifiers
13923          that are not type-specifiers appear, the program is invalid.  */
13924       if (is_condition && !is_cv_qualifier)
13925         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13926     }
13927
13928   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13929 }
13930
13931 /* Parse a parameter-declaration-clause.
13932
13933    parameter-declaration-clause:
13934      parameter-declaration-list [opt] ... [opt]
13935      parameter-declaration-list , ...
13936
13937    Returns a representation for the parameter declarations.  A return
13938    value of NULL indicates a parameter-declaration-clause consisting
13939    only of an ellipsis.  */
13940
13941 static tree
13942 cp_parser_parameter_declaration_clause (cp_parser* parser)
13943 {
13944   tree parameters;
13945   cp_token *token;
13946   bool ellipsis_p;
13947   bool is_error;
13948
13949   /* Peek at the next token.  */
13950   token = cp_lexer_peek_token (parser->lexer);
13951   /* Check for trivial parameter-declaration-clauses.  */
13952   if (token->type == CPP_ELLIPSIS)
13953     {
13954       /* Consume the `...' token.  */
13955       cp_lexer_consume_token (parser->lexer);
13956       return NULL_TREE;
13957     }
13958   else if (token->type == CPP_CLOSE_PAREN)
13959     /* There are no parameters.  */
13960     {
13961 #ifndef NO_IMPLICIT_EXTERN_C
13962       if (in_system_header && current_class_type == NULL
13963           && current_lang_name == lang_name_c)
13964         return NULL_TREE;
13965       else
13966 #endif
13967         return void_list_node;
13968     }
13969   /* Check for `(void)', too, which is a special case.  */
13970   else if (token->keyword == RID_VOID
13971            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13972                == CPP_CLOSE_PAREN))
13973     {
13974       /* Consume the `void' token.  */
13975       cp_lexer_consume_token (parser->lexer);
13976       /* There are no parameters.  */
13977       return void_list_node;
13978     }
13979
13980   /* Parse the parameter-declaration-list.  */
13981   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13982   /* If a parse error occurred while parsing the
13983      parameter-declaration-list, then the entire
13984      parameter-declaration-clause is erroneous.  */
13985   if (is_error)
13986     return NULL;
13987
13988   /* Peek at the next token.  */
13989   token = cp_lexer_peek_token (parser->lexer);
13990   /* If it's a `,', the clause should terminate with an ellipsis.  */
13991   if (token->type == CPP_COMMA)
13992     {
13993       /* Consume the `,'.  */
13994       cp_lexer_consume_token (parser->lexer);
13995       /* Expect an ellipsis.  */
13996       ellipsis_p
13997         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13998     }
13999   /* It might also be `...' if the optional trailing `,' was
14000      omitted.  */
14001   else if (token->type == CPP_ELLIPSIS)
14002     {
14003       /* Consume the `...' token.  */
14004       cp_lexer_consume_token (parser->lexer);
14005       /* And remember that we saw it.  */
14006       ellipsis_p = true;
14007     }
14008   else
14009     ellipsis_p = false;
14010
14011   /* Finish the parameter list.  */
14012   if (!ellipsis_p)
14013     parameters = chainon (parameters, void_list_node);
14014
14015   return parameters;
14016 }
14017
14018 /* Parse a parameter-declaration-list.
14019
14020    parameter-declaration-list:
14021      parameter-declaration
14022      parameter-declaration-list , parameter-declaration
14023
14024    Returns a representation of the parameter-declaration-list, as for
14025    cp_parser_parameter_declaration_clause.  However, the
14026    `void_list_node' is never appended to the list.  Upon return,
14027    *IS_ERROR will be true iff an error occurred.  */
14028
14029 static tree
14030 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14031 {
14032   tree parameters = NULL_TREE;
14033   tree *tail = &parameters; 
14034   bool saved_in_unbraced_linkage_specification_p;
14035
14036   /* Assume all will go well.  */
14037   *is_error = false;
14038   /* The special considerations that apply to a function within an
14039      unbraced linkage specifications do not apply to the parameters
14040      to the function.  */
14041   saved_in_unbraced_linkage_specification_p 
14042     = parser->in_unbraced_linkage_specification_p;
14043   parser->in_unbraced_linkage_specification_p = false;
14044
14045   /* Look for more parameters.  */
14046   while (true)
14047     {
14048       cp_parameter_declarator *parameter;
14049       tree decl = error_mark_node;
14050       bool parenthesized_p;
14051       /* Parse the parameter.  */
14052       parameter
14053         = cp_parser_parameter_declaration (parser,
14054                                            /*template_parm_p=*/false,
14055                                            &parenthesized_p);
14056
14057       /* We don't know yet if the enclosing context is deprecated, so wait
14058          and warn in grokparms if appropriate.  */
14059       deprecated_state = DEPRECATED_SUPPRESS;
14060
14061       if (parameter)
14062         decl = grokdeclarator (parameter->declarator,
14063                                &parameter->decl_specifiers,
14064                                PARM,
14065                                parameter->default_argument != NULL_TREE,
14066                                &parameter->decl_specifiers.attributes);
14067
14068       deprecated_state = DEPRECATED_NORMAL;
14069
14070       /* If a parse error occurred parsing the parameter declaration,
14071          then the entire parameter-declaration-list is erroneous.  */
14072       if (decl == error_mark_node)
14073         {
14074           *is_error = true;
14075           parameters = error_mark_node;
14076           break;
14077         }
14078
14079       if (parameter->decl_specifiers.attributes)
14080         cplus_decl_attributes (&decl,
14081                                parameter->decl_specifiers.attributes,
14082                                0);
14083       if (DECL_NAME (decl))
14084         decl = pushdecl (decl);
14085
14086       /* Add the new parameter to the list.  */
14087       *tail = build_tree_list (parameter->default_argument, decl);
14088       tail = &TREE_CHAIN (*tail);
14089
14090       /* Peek at the next token.  */
14091       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14092           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14093           /* These are for Objective-C++ */
14094           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14095           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14096         /* The parameter-declaration-list is complete.  */
14097         break;
14098       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14099         {
14100           cp_token *token;
14101
14102           /* Peek at the next token.  */
14103           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14104           /* If it's an ellipsis, then the list is complete.  */
14105           if (token->type == CPP_ELLIPSIS)
14106             break;
14107           /* Otherwise, there must be more parameters.  Consume the
14108              `,'.  */
14109           cp_lexer_consume_token (parser->lexer);
14110           /* When parsing something like:
14111
14112                 int i(float f, double d)
14113
14114              we can tell after seeing the declaration for "f" that we
14115              are not looking at an initialization of a variable "i",
14116              but rather at the declaration of a function "i".
14117
14118              Due to the fact that the parsing of template arguments
14119              (as specified to a template-id) requires backtracking we
14120              cannot use this technique when inside a template argument
14121              list.  */
14122           if (!parser->in_template_argument_list_p
14123               && !parser->in_type_id_in_expr_p
14124               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14125               /* However, a parameter-declaration of the form
14126                  "foat(f)" (which is a valid declaration of a
14127                  parameter "f") can also be interpreted as an
14128                  expression (the conversion of "f" to "float").  */
14129               && !parenthesized_p)
14130             cp_parser_commit_to_tentative_parse (parser);
14131         }
14132       else
14133         {
14134           cp_parser_error (parser, "expected %<,%> or %<...%>");
14135           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14136             cp_parser_skip_to_closing_parenthesis (parser,
14137                                                    /*recovering=*/true,
14138                                                    /*or_comma=*/false,
14139                                                    /*consume_paren=*/false);
14140           break;
14141         }
14142     }
14143
14144   parser->in_unbraced_linkage_specification_p
14145     = saved_in_unbraced_linkage_specification_p;
14146
14147   return parameters;
14148 }
14149
14150 /* Parse a parameter declaration.
14151
14152    parameter-declaration:
14153      decl-specifier-seq ... [opt] declarator
14154      decl-specifier-seq declarator = assignment-expression
14155      decl-specifier-seq ... [opt] abstract-declarator [opt]
14156      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14157
14158    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14159    declares a template parameter.  (In that case, a non-nested `>'
14160    token encountered during the parsing of the assignment-expression
14161    is not interpreted as a greater-than operator.)
14162
14163    Returns a representation of the parameter, or NULL if an error
14164    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14165    true iff the declarator is of the form "(p)".  */
14166
14167 static cp_parameter_declarator *
14168 cp_parser_parameter_declaration (cp_parser *parser,
14169                                  bool template_parm_p,
14170                                  bool *parenthesized_p)
14171 {
14172   int declares_class_or_enum;
14173   bool greater_than_is_operator_p;
14174   cp_decl_specifier_seq decl_specifiers;
14175   cp_declarator *declarator;
14176   tree default_argument;
14177   cp_token *token = NULL, *declarator_token_start = NULL;
14178   const char *saved_message;
14179
14180   /* In a template parameter, `>' is not an operator.
14181
14182      [temp.param]
14183
14184      When parsing a default template-argument for a non-type
14185      template-parameter, the first non-nested `>' is taken as the end
14186      of the template parameter-list rather than a greater-than
14187      operator.  */
14188   greater_than_is_operator_p = !template_parm_p;
14189
14190   /* Type definitions may not appear in parameter types.  */
14191   saved_message = parser->type_definition_forbidden_message;
14192   parser->type_definition_forbidden_message
14193     = "types may not be defined in parameter types";
14194
14195   /* Parse the declaration-specifiers.  */
14196   cp_parser_decl_specifier_seq (parser,
14197                                 CP_PARSER_FLAGS_NONE,
14198                                 &decl_specifiers,
14199                                 &declares_class_or_enum);
14200   /* If an error occurred, there's no reason to attempt to parse the
14201      rest of the declaration.  */
14202   if (cp_parser_error_occurred (parser))
14203     {
14204       parser->type_definition_forbidden_message = saved_message;
14205       return NULL;
14206     }
14207
14208   /* Peek at the next token.  */
14209   token = cp_lexer_peek_token (parser->lexer);
14210
14211   /* If the next token is a `)', `,', `=', `>', or `...', then there
14212      is no declarator. However, when variadic templates are enabled,
14213      there may be a declarator following `...'.  */
14214   if (token->type == CPP_CLOSE_PAREN
14215       || token->type == CPP_COMMA
14216       || token->type == CPP_EQ
14217       || token->type == CPP_GREATER)
14218     {
14219       declarator = NULL;
14220       if (parenthesized_p)
14221         *parenthesized_p = false;
14222     }
14223   /* Otherwise, there should be a declarator.  */
14224   else
14225     {
14226       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14227       parser->default_arg_ok_p = false;
14228
14229       /* After seeing a decl-specifier-seq, if the next token is not a
14230          "(", there is no possibility that the code is a valid
14231          expression.  Therefore, if parsing tentatively, we commit at
14232          this point.  */
14233       if (!parser->in_template_argument_list_p
14234           /* In an expression context, having seen:
14235
14236                (int((char ...
14237
14238              we cannot be sure whether we are looking at a
14239              function-type (taking a "char" as a parameter) or a cast
14240              of some object of type "char" to "int".  */
14241           && !parser->in_type_id_in_expr_p
14242           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14243           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14244         cp_parser_commit_to_tentative_parse (parser);
14245       /* Parse the declarator.  */
14246       declarator_token_start = token;
14247       declarator = cp_parser_declarator (parser,
14248                                          CP_PARSER_DECLARATOR_EITHER,
14249                                          /*ctor_dtor_or_conv_p=*/NULL,
14250                                          parenthesized_p,
14251                                          /*member_p=*/false);
14252       parser->default_arg_ok_p = saved_default_arg_ok_p;
14253       /* After the declarator, allow more attributes.  */
14254       decl_specifiers.attributes
14255         = chainon (decl_specifiers.attributes,
14256                    cp_parser_attributes_opt (parser));
14257     }
14258
14259   /* If the next token is an ellipsis, and we have not seen a
14260      declarator name, and the type of the declarator contains parameter
14261      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14262      a parameter pack expansion expression. Otherwise, leave the
14263      ellipsis for a C-style variadic function. */
14264   token = cp_lexer_peek_token (parser->lexer);
14265   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14266     {
14267       tree type = decl_specifiers.type;
14268
14269       if (type && DECL_P (type))
14270         type = TREE_TYPE (type);
14271
14272       if (type
14273           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14274           && declarator_can_be_parameter_pack (declarator)
14275           && (!declarator || !declarator->parameter_pack_p)
14276           && uses_parameter_packs (type))
14277         {
14278           /* Consume the `...'. */
14279           cp_lexer_consume_token (parser->lexer);
14280           maybe_warn_variadic_templates ();
14281           
14282           /* Build a pack expansion type */
14283           if (declarator)
14284             declarator->parameter_pack_p = true;
14285           else
14286             decl_specifiers.type = make_pack_expansion (type);
14287         }
14288     }
14289
14290   /* The restriction on defining new types applies only to the type
14291      of the parameter, not to the default argument.  */
14292   parser->type_definition_forbidden_message = saved_message;
14293
14294   /* If the next token is `=', then process a default argument.  */
14295   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14296     {
14297       /* Consume the `='.  */
14298       cp_lexer_consume_token (parser->lexer);
14299
14300       /* If we are defining a class, then the tokens that make up the
14301          default argument must be saved and processed later.  */
14302       if (!template_parm_p && at_class_scope_p ()
14303           && TYPE_BEING_DEFINED (current_class_type))
14304         {
14305           unsigned depth = 0;
14306           int maybe_template_id = 0;
14307           cp_token *first_token;
14308           cp_token *token;
14309
14310           /* Add tokens until we have processed the entire default
14311              argument.  We add the range [first_token, token).  */
14312           first_token = cp_lexer_peek_token (parser->lexer);
14313           while (true)
14314             {
14315               bool done = false;
14316
14317               /* Peek at the next token.  */
14318               token = cp_lexer_peek_token (parser->lexer);
14319               /* What we do depends on what token we have.  */
14320               switch (token->type)
14321                 {
14322                   /* In valid code, a default argument must be
14323                      immediately followed by a `,' `)', or `...'.  */
14324                 case CPP_COMMA:
14325                   if (depth == 0 && maybe_template_id)
14326                     {
14327                       /* If we've seen a '<', we might be in a
14328                          template-argument-list.  Until Core issue 325 is
14329                          resolved, we don't know how this situation ought
14330                          to be handled, so try to DTRT.  We check whether
14331                          what comes after the comma is a valid parameter
14332                          declaration list.  If it is, then the comma ends
14333                          the default argument; otherwise the default
14334                          argument continues.  */
14335                       bool error = false;
14336
14337                       /* Set ITALP so cp_parser_parameter_declaration_list
14338                          doesn't decide to commit to this parse.  */
14339                       bool saved_italp = parser->in_template_argument_list_p;
14340                       parser->in_template_argument_list_p = true;
14341
14342                       cp_parser_parse_tentatively (parser);
14343                       cp_lexer_consume_token (parser->lexer);
14344                       cp_parser_parameter_declaration_list (parser, &error);
14345                       if (!cp_parser_error_occurred (parser) && !error)
14346                         done = true;
14347                       cp_parser_abort_tentative_parse (parser);
14348
14349                       parser->in_template_argument_list_p = saved_italp;
14350                       break;
14351                     }
14352                 case CPP_CLOSE_PAREN:
14353                 case CPP_ELLIPSIS:
14354                   /* If we run into a non-nested `;', `}', or `]',
14355                      then the code is invalid -- but the default
14356                      argument is certainly over.  */
14357                 case CPP_SEMICOLON:
14358                 case CPP_CLOSE_BRACE:
14359                 case CPP_CLOSE_SQUARE:
14360                   if (depth == 0)
14361                     done = true;
14362                   /* Update DEPTH, if necessary.  */
14363                   else if (token->type == CPP_CLOSE_PAREN
14364                            || token->type == CPP_CLOSE_BRACE
14365                            || token->type == CPP_CLOSE_SQUARE)
14366                     --depth;
14367                   break;
14368
14369                 case CPP_OPEN_PAREN:
14370                 case CPP_OPEN_SQUARE:
14371                 case CPP_OPEN_BRACE:
14372                   ++depth;
14373                   break;
14374
14375                 case CPP_LESS:
14376                   if (depth == 0)
14377                     /* This might be the comparison operator, or it might
14378                        start a template argument list.  */
14379                     ++maybe_template_id;
14380                   break;
14381
14382                 case CPP_RSHIFT:
14383                   if (cxx_dialect == cxx98)
14384                     break;
14385                   /* Fall through for C++0x, which treats the `>>'
14386                      operator like two `>' tokens in certain
14387                      cases.  */
14388
14389                 case CPP_GREATER:
14390                   if (depth == 0)
14391                     {
14392                       /* This might be an operator, or it might close a
14393                          template argument list.  But if a previous '<'
14394                          started a template argument list, this will have
14395                          closed it, so we can't be in one anymore.  */
14396                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14397                       if (maybe_template_id < 0)
14398                         maybe_template_id = 0;
14399                     }
14400                   break;
14401
14402                   /* If we run out of tokens, issue an error message.  */
14403                 case CPP_EOF:
14404                 case CPP_PRAGMA_EOL:
14405                   error ("%Hfile ends in default argument", &token->location);
14406                   done = true;
14407                   break;
14408
14409                 case CPP_NAME:
14410                 case CPP_SCOPE:
14411                   /* In these cases, we should look for template-ids.
14412                      For example, if the default argument is
14413                      `X<int, double>()', we need to do name lookup to
14414                      figure out whether or not `X' is a template; if
14415                      so, the `,' does not end the default argument.
14416
14417                      That is not yet done.  */
14418                   break;
14419
14420                 default:
14421                   break;
14422                 }
14423
14424               /* If we've reached the end, stop.  */
14425               if (done)
14426                 break;
14427
14428               /* Add the token to the token block.  */
14429               token = cp_lexer_consume_token (parser->lexer);
14430             }
14431
14432           /* Create a DEFAULT_ARG to represent the unparsed default
14433              argument.  */
14434           default_argument = make_node (DEFAULT_ARG);
14435           DEFARG_TOKENS (default_argument)
14436             = cp_token_cache_new (first_token, token);
14437           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14438         }
14439       /* Outside of a class definition, we can just parse the
14440          assignment-expression.  */
14441       else
14442         {
14443           token = cp_lexer_peek_token (parser->lexer);
14444           default_argument 
14445             = cp_parser_default_argument (parser, template_parm_p);
14446         }
14447
14448       if (!parser->default_arg_ok_p)
14449         {
14450           if (flag_permissive)
14451             warning (0, "deprecated use of default argument for parameter of non-function");
14452           else
14453             {
14454               error ("%Hdefault arguments are only "
14455                      "permitted for function parameters",
14456                      &token->location);
14457               default_argument = NULL_TREE;
14458             }
14459         }
14460       else if ((declarator && declarator->parameter_pack_p)
14461                || (decl_specifiers.type
14462                    && PACK_EXPANSION_P (decl_specifiers.type)))
14463         {
14464           const char* kind = template_parm_p? "template " : "";
14465           
14466           /* Find the name of the parameter pack.  */     
14467           cp_declarator *id_declarator = declarator;
14468           while (id_declarator && id_declarator->kind != cdk_id)
14469             id_declarator = id_declarator->declarator;
14470           
14471           if (id_declarator && id_declarator->kind == cdk_id)
14472             error ("%H%sparameter pack %qD cannot have a default argument",
14473                    &declarator_token_start->location,
14474                    kind, id_declarator->u.id.unqualified_name);
14475           else
14476             error ("%H%sparameter pack cannot have a default argument",
14477                    &declarator_token_start->location, kind);
14478           
14479           default_argument = NULL_TREE;
14480         }
14481     }
14482   else
14483     default_argument = NULL_TREE;
14484
14485   return make_parameter_declarator (&decl_specifiers,
14486                                     declarator,
14487                                     default_argument);
14488 }
14489
14490 /* Parse a default argument and return it.
14491
14492    TEMPLATE_PARM_P is true if this is a default argument for a
14493    non-type template parameter.  */
14494 static tree
14495 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14496 {
14497   tree default_argument = NULL_TREE;
14498   bool saved_greater_than_is_operator_p;
14499   bool saved_local_variables_forbidden_p;
14500
14501   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14502      set correctly.  */
14503   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14504   parser->greater_than_is_operator_p = !template_parm_p;
14505   /* Local variable names (and the `this' keyword) may not
14506      appear in a default argument.  */
14507   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14508   parser->local_variables_forbidden_p = true;
14509   /* The default argument expression may cause implicitly
14510      defined member functions to be synthesized, which will
14511      result in garbage collection.  We must treat this
14512      situation as if we were within the body of function so as
14513      to avoid collecting live data on the stack.  */
14514   ++function_depth;
14515   /* Parse the assignment-expression.  */
14516   if (template_parm_p)
14517     push_deferring_access_checks (dk_no_deferred);
14518   default_argument
14519     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14520   if (template_parm_p)
14521     pop_deferring_access_checks ();
14522   /* Restore saved state.  */
14523   --function_depth;
14524   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14525   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14526
14527   return default_argument;
14528 }
14529
14530 /* Parse a function-body.
14531
14532    function-body:
14533      compound_statement  */
14534
14535 static void
14536 cp_parser_function_body (cp_parser *parser)
14537 {
14538   cp_parser_compound_statement (parser, NULL, false);
14539 }
14540
14541 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14542    true if a ctor-initializer was present.  */
14543
14544 static bool
14545 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14546 {
14547   tree body;
14548   bool ctor_initializer_p;
14549
14550   /* Begin the function body.  */
14551   body = begin_function_body ();
14552   /* Parse the optional ctor-initializer.  */
14553   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14554   /* Parse the function-body.  */
14555   cp_parser_function_body (parser);
14556   /* Finish the function body.  */
14557   finish_function_body (body);
14558
14559   return ctor_initializer_p;
14560 }
14561
14562 /* Parse an initializer.
14563
14564    initializer:
14565      = initializer-clause
14566      ( expression-list )
14567
14568    Returns an expression representing the initializer.  If no
14569    initializer is present, NULL_TREE is returned.
14570
14571    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14572    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14573    set to TRUE if there is no initializer present.  If there is an
14574    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14575    is set to true; otherwise it is set to false.  */
14576
14577 static tree
14578 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14579                        bool* non_constant_p)
14580 {
14581   cp_token *token;
14582   tree init;
14583
14584   /* Peek at the next token.  */
14585   token = cp_lexer_peek_token (parser->lexer);
14586
14587   /* Let our caller know whether or not this initializer was
14588      parenthesized.  */
14589   *is_direct_init = (token->type != CPP_EQ);
14590   /* Assume that the initializer is constant.  */
14591   *non_constant_p = false;
14592
14593   if (token->type == CPP_EQ)
14594     {
14595       /* Consume the `='.  */
14596       cp_lexer_consume_token (parser->lexer);
14597       /* Parse the initializer-clause.  */
14598       init = cp_parser_initializer_clause (parser, non_constant_p);
14599     }
14600   else if (token->type == CPP_OPEN_PAREN)
14601     init = cp_parser_parenthesized_expression_list (parser, false,
14602                                                     /*cast_p=*/false,
14603                                                     /*allow_expansion_p=*/true,
14604                                                     non_constant_p);
14605   else if (token->type == CPP_OPEN_BRACE)
14606     {
14607       maybe_warn_cpp0x ("extended initializer lists");
14608       init = cp_parser_braced_list (parser, non_constant_p);
14609       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14610     }
14611   else
14612     {
14613       /* Anything else is an error.  */
14614       cp_parser_error (parser, "expected initializer");
14615       init = error_mark_node;
14616     }
14617
14618   return init;
14619 }
14620
14621 /* Parse an initializer-clause.
14622
14623    initializer-clause:
14624      assignment-expression
14625      braced-init-list
14626
14627    Returns an expression representing the initializer.
14628
14629    If the `assignment-expression' production is used the value
14630    returned is simply a representation for the expression.
14631
14632    Otherwise, calls cp_parser_braced_list.  */
14633
14634 static tree
14635 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14636 {
14637   tree initializer;
14638
14639   /* Assume the expression is constant.  */
14640   *non_constant_p = false;
14641
14642   /* If it is not a `{', then we are looking at an
14643      assignment-expression.  */
14644   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14645     {
14646       initializer
14647         = cp_parser_constant_expression (parser,
14648                                         /*allow_non_constant_p=*/true,
14649                                         non_constant_p);
14650       if (!*non_constant_p)
14651         initializer = fold_non_dependent_expr (initializer);
14652     }
14653   else
14654     initializer = cp_parser_braced_list (parser, non_constant_p);
14655
14656   return initializer;
14657 }
14658
14659 /* Parse a brace-enclosed initializer list.
14660
14661    braced-init-list:
14662      { initializer-list , [opt] }
14663      { }
14664
14665    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14666    the elements of the initializer-list (or NULL, if the last
14667    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14668    NULL_TREE.  There is no way to detect whether or not the optional
14669    trailing `,' was provided.  NON_CONSTANT_P is as for
14670    cp_parser_initializer.  */     
14671
14672 static tree
14673 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14674 {
14675   tree initializer;
14676
14677   /* Consume the `{' token.  */
14678   cp_lexer_consume_token (parser->lexer);
14679   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14680   initializer = make_node (CONSTRUCTOR);
14681   /* If it's not a `}', then there is a non-trivial initializer.  */
14682   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14683     {
14684       /* Parse the initializer list.  */
14685       CONSTRUCTOR_ELTS (initializer)
14686         = cp_parser_initializer_list (parser, non_constant_p);
14687       /* A trailing `,' token is allowed.  */
14688       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14689         cp_lexer_consume_token (parser->lexer);
14690     }
14691   /* Now, there should be a trailing `}'.  */
14692   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14693   TREE_TYPE (initializer) = init_list_type_node;
14694   return initializer;
14695 }
14696
14697 /* Parse an initializer-list.
14698
14699    initializer-list:
14700      initializer-clause ... [opt]
14701      initializer-list , initializer-clause ... [opt]
14702
14703    GNU Extension:
14704
14705    initializer-list:
14706      identifier : initializer-clause
14707      initializer-list, identifier : initializer-clause
14708
14709    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14710    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14711    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14712    as for cp_parser_initializer.  */
14713
14714 static VEC(constructor_elt,gc) *
14715 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14716 {
14717   VEC(constructor_elt,gc) *v = NULL;
14718
14719   /* Assume all of the expressions are constant.  */
14720   *non_constant_p = false;
14721
14722   /* Parse the rest of the list.  */
14723   while (true)
14724     {
14725       cp_token *token;
14726       tree identifier;
14727       tree initializer;
14728       bool clause_non_constant_p;
14729
14730       /* If the next token is an identifier and the following one is a
14731          colon, we are looking at the GNU designated-initializer
14732          syntax.  */
14733       if (cp_parser_allow_gnu_extensions_p (parser)
14734           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14735           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14736         {
14737           /* Warn the user that they are using an extension.  */
14738           pedwarn (input_location, OPT_pedantic, 
14739                    "ISO C++ does not allow designated initializers");
14740           /* Consume the identifier.  */
14741           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14742           /* Consume the `:'.  */
14743           cp_lexer_consume_token (parser->lexer);
14744         }
14745       else
14746         identifier = NULL_TREE;
14747
14748       /* Parse the initializer.  */
14749       initializer = cp_parser_initializer_clause (parser,
14750                                                   &clause_non_constant_p);
14751       /* If any clause is non-constant, so is the entire initializer.  */
14752       if (clause_non_constant_p)
14753         *non_constant_p = true;
14754
14755       /* If we have an ellipsis, this is an initializer pack
14756          expansion.  */
14757       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14758         {
14759           /* Consume the `...'.  */
14760           cp_lexer_consume_token (parser->lexer);
14761
14762           /* Turn the initializer into an initializer expansion.  */
14763           initializer = make_pack_expansion (initializer);
14764         }
14765
14766       /* Add it to the vector.  */
14767       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14768
14769       /* If the next token is not a comma, we have reached the end of
14770          the list.  */
14771       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14772         break;
14773
14774       /* Peek at the next token.  */
14775       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14776       /* If the next token is a `}', then we're still done.  An
14777          initializer-clause can have a trailing `,' after the
14778          initializer-list and before the closing `}'.  */
14779       if (token->type == CPP_CLOSE_BRACE)
14780         break;
14781
14782       /* Consume the `,' token.  */
14783       cp_lexer_consume_token (parser->lexer);
14784     }
14785
14786   return v;
14787 }
14788
14789 /* Classes [gram.class] */
14790
14791 /* Parse a class-name.
14792
14793    class-name:
14794      identifier
14795      template-id
14796
14797    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14798    to indicate that names looked up in dependent types should be
14799    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14800    keyword has been used to indicate that the name that appears next
14801    is a template.  TAG_TYPE indicates the explicit tag given before
14802    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14803    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14804    is the class being defined in a class-head.
14805
14806    Returns the TYPE_DECL representing the class.  */
14807
14808 static tree
14809 cp_parser_class_name (cp_parser *parser,
14810                       bool typename_keyword_p,
14811                       bool template_keyword_p,
14812                       enum tag_types tag_type,
14813                       bool check_dependency_p,
14814                       bool class_head_p,
14815                       bool is_declaration)
14816 {
14817   tree decl;
14818   tree scope;
14819   bool typename_p;
14820   cp_token *token;
14821   tree identifier = NULL_TREE;
14822
14823   /* All class-names start with an identifier.  */
14824   token = cp_lexer_peek_token (parser->lexer);
14825   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14826     {
14827       cp_parser_error (parser, "expected class-name");
14828       return error_mark_node;
14829     }
14830
14831   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14832      to a template-id, so we save it here.  */
14833   scope = parser->scope;
14834   if (scope == error_mark_node)
14835     return error_mark_node;
14836
14837   /* Any name names a type if we're following the `typename' keyword
14838      in a qualified name where the enclosing scope is type-dependent.  */
14839   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14840                 && dependent_type_p (scope));
14841   /* Handle the common case (an identifier, but not a template-id)
14842      efficiently.  */
14843   if (token->type == CPP_NAME
14844       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14845     {
14846       cp_token *identifier_token;
14847       bool ambiguous_p;
14848
14849       /* Look for the identifier.  */
14850       identifier_token = cp_lexer_peek_token (parser->lexer);
14851       ambiguous_p = identifier_token->ambiguous_p;
14852       identifier = cp_parser_identifier (parser);
14853       /* If the next token isn't an identifier, we are certainly not
14854          looking at a class-name.  */
14855       if (identifier == error_mark_node)
14856         decl = error_mark_node;
14857       /* If we know this is a type-name, there's no need to look it
14858          up.  */
14859       else if (typename_p)
14860         decl = identifier;
14861       else
14862         {
14863           tree ambiguous_decls;
14864           /* If we already know that this lookup is ambiguous, then
14865              we've already issued an error message; there's no reason
14866              to check again.  */
14867           if (ambiguous_p)
14868             {
14869               cp_parser_simulate_error (parser);
14870               return error_mark_node;
14871             }
14872           /* If the next token is a `::', then the name must be a type
14873              name.
14874
14875              [basic.lookup.qual]
14876
14877              During the lookup for a name preceding the :: scope
14878              resolution operator, object, function, and enumerator
14879              names are ignored.  */
14880           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14881             tag_type = typename_type;
14882           /* Look up the name.  */
14883           decl = cp_parser_lookup_name (parser, identifier,
14884                                         tag_type,
14885                                         /*is_template=*/false,
14886                                         /*is_namespace=*/false,
14887                                         check_dependency_p,
14888                                         &ambiguous_decls,
14889                                         identifier_token->location);
14890           if (ambiguous_decls)
14891             {
14892               error ("%Hreference to %qD is ambiguous",
14893                      &identifier_token->location, identifier);
14894               print_candidates (ambiguous_decls);
14895               if (cp_parser_parsing_tentatively (parser))
14896                 {
14897                   identifier_token->ambiguous_p = true;
14898                   cp_parser_simulate_error (parser);
14899                 }
14900               return error_mark_node;
14901             }
14902         }
14903     }
14904   else
14905     {
14906       /* Try a template-id.  */
14907       decl = cp_parser_template_id (parser, template_keyword_p,
14908                                     check_dependency_p,
14909                                     is_declaration);
14910       if (decl == error_mark_node)
14911         return error_mark_node;
14912     }
14913
14914   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14915
14916   /* If this is a typename, create a TYPENAME_TYPE.  */
14917   if (typename_p && decl != error_mark_node)
14918     {
14919       decl = make_typename_type (scope, decl, typename_type,
14920                                  /*complain=*/tf_error);
14921       if (decl != error_mark_node)
14922         decl = TYPE_NAME (decl);
14923     }
14924
14925   /* Check to see that it is really the name of a class.  */
14926   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14927       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14928       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14929     /* Situations like this:
14930
14931          template <typename T> struct A {
14932            typename T::template X<int>::I i;
14933          };
14934
14935        are problematic.  Is `T::template X<int>' a class-name?  The
14936        standard does not seem to be definitive, but there is no other
14937        valid interpretation of the following `::'.  Therefore, those
14938        names are considered class-names.  */
14939     {
14940       decl = make_typename_type (scope, decl, tag_type, tf_error);
14941       if (decl != error_mark_node)
14942         decl = TYPE_NAME (decl);
14943     }
14944   else if (TREE_CODE (decl) != TYPE_DECL
14945            || TREE_TYPE (decl) == error_mark_node
14946            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14947     decl = error_mark_node;
14948
14949   if (decl == error_mark_node)
14950     cp_parser_error (parser, "expected class-name");
14951   else if (identifier && !parser->scope)
14952     maybe_note_name_used_in_class (identifier, decl);
14953
14954   return decl;
14955 }
14956
14957 /* Parse a class-specifier.
14958
14959    class-specifier:
14960      class-head { member-specification [opt] }
14961
14962    Returns the TREE_TYPE representing the class.  */
14963
14964 static tree
14965 cp_parser_class_specifier (cp_parser* parser)
14966 {
14967   cp_token *token;
14968   tree type;
14969   tree attributes = NULL_TREE;
14970   int has_trailing_semicolon;
14971   bool nested_name_specifier_p;
14972   unsigned saved_num_template_parameter_lists;
14973   bool saved_in_function_body;
14974   bool saved_in_unbraced_linkage_specification_p;
14975   tree old_scope = NULL_TREE;
14976   tree scope = NULL_TREE;
14977   tree bases;
14978
14979   push_deferring_access_checks (dk_no_deferred);
14980
14981   /* Parse the class-head.  */
14982   type = cp_parser_class_head (parser,
14983                                &nested_name_specifier_p,
14984                                &attributes,
14985                                &bases);
14986   /* If the class-head was a semantic disaster, skip the entire body
14987      of the class.  */
14988   if (!type)
14989     {
14990       cp_parser_skip_to_end_of_block_or_statement (parser);
14991       pop_deferring_access_checks ();
14992       return error_mark_node;
14993     }
14994
14995   /* Look for the `{'.  */
14996   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14997     {
14998       pop_deferring_access_checks ();
14999       return error_mark_node;
15000     }
15001
15002   /* Process the base classes. If they're invalid, skip the 
15003      entire class body.  */
15004   if (!xref_basetypes (type, bases))
15005     {
15006       /* Consuming the closing brace yields better error messages
15007          later on.  */
15008       if (cp_parser_skip_to_closing_brace (parser))
15009         cp_lexer_consume_token (parser->lexer);
15010       pop_deferring_access_checks ();
15011       return error_mark_node;
15012     }
15013
15014   /* Issue an error message if type-definitions are forbidden here.  */
15015   cp_parser_check_type_definition (parser);
15016   /* Remember that we are defining one more class.  */
15017   ++parser->num_classes_being_defined;
15018   /* Inside the class, surrounding template-parameter-lists do not
15019      apply.  */
15020   saved_num_template_parameter_lists
15021     = parser->num_template_parameter_lists;
15022   parser->num_template_parameter_lists = 0;
15023   /* We are not in a function body.  */
15024   saved_in_function_body = parser->in_function_body;
15025   parser->in_function_body = false;
15026   /* We are not immediately inside an extern "lang" block.  */
15027   saved_in_unbraced_linkage_specification_p
15028     = parser->in_unbraced_linkage_specification_p;
15029   parser->in_unbraced_linkage_specification_p = false;
15030
15031   /* Start the class.  */
15032   if (nested_name_specifier_p)
15033     {
15034       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15035       old_scope = push_inner_scope (scope);
15036     }
15037   type = begin_class_definition (type, attributes);
15038
15039   if (type == error_mark_node)
15040     /* If the type is erroneous, skip the entire body of the class.  */
15041     cp_parser_skip_to_closing_brace (parser);
15042   else
15043     /* Parse the member-specification.  */
15044     cp_parser_member_specification_opt (parser);
15045
15046   /* Look for the trailing `}'.  */
15047   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15048   /* We get better error messages by noticing a common problem: a
15049      missing trailing `;'.  */
15050   token = cp_lexer_peek_token (parser->lexer);
15051   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15052   /* Look for trailing attributes to apply to this class.  */
15053   if (cp_parser_allow_gnu_extensions_p (parser))
15054     attributes = cp_parser_attributes_opt (parser);
15055   if (type != error_mark_node)
15056     type = finish_struct (type, attributes);
15057   if (nested_name_specifier_p)
15058     pop_inner_scope (old_scope, scope);
15059   /* If this class is not itself within the scope of another class,
15060      then we need to parse the bodies of all of the queued function
15061      definitions.  Note that the queued functions defined in a class
15062      are not always processed immediately following the
15063      class-specifier for that class.  Consider:
15064
15065        struct A {
15066          struct B { void f() { sizeof (A); } };
15067        };
15068
15069      If `f' were processed before the processing of `A' were
15070      completed, there would be no way to compute the size of `A'.
15071      Note that the nesting we are interested in here is lexical --
15072      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15073      for:
15074
15075        struct A { struct B; };
15076        struct A::B { void f() { } };
15077
15078      there is no need to delay the parsing of `A::B::f'.  */
15079   if (--parser->num_classes_being_defined == 0)
15080     {
15081       tree queue_entry;
15082       tree fn;
15083       tree class_type = NULL_TREE;
15084       tree pushed_scope = NULL_TREE;
15085
15086       /* In a first pass, parse default arguments to the functions.
15087          Then, in a second pass, parse the bodies of the functions.
15088          This two-phased approach handles cases like:
15089
15090             struct S {
15091               void f() { g(); }
15092               void g(int i = 3);
15093             };
15094
15095          */
15096       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15097              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15098            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15099            TREE_PURPOSE (parser->unparsed_functions_queues)
15100              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15101         {
15102           fn = TREE_VALUE (queue_entry);
15103           /* If there are default arguments that have not yet been processed,
15104              take care of them now.  */
15105           if (class_type != TREE_PURPOSE (queue_entry))
15106             {
15107               if (pushed_scope)
15108                 pop_scope (pushed_scope);
15109               class_type = TREE_PURPOSE (queue_entry);
15110               pushed_scope = push_scope (class_type);
15111             }
15112           /* Make sure that any template parameters are in scope.  */
15113           maybe_begin_member_template_processing (fn);
15114           /* Parse the default argument expressions.  */
15115           cp_parser_late_parsing_default_args (parser, fn);
15116           /* Remove any template parameters from the symbol table.  */
15117           maybe_end_member_template_processing ();
15118         }
15119       if (pushed_scope)
15120         pop_scope (pushed_scope);
15121       /* Now parse the body of the functions.  */
15122       for (TREE_VALUE (parser->unparsed_functions_queues)
15123              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15124            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15125            TREE_VALUE (parser->unparsed_functions_queues)
15126              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15127         {
15128           /* Figure out which function we need to process.  */
15129           fn = TREE_VALUE (queue_entry);
15130           /* Parse the function.  */
15131           cp_parser_late_parsing_for_member (parser, fn);
15132         }
15133     }
15134
15135   /* Put back any saved access checks.  */
15136   pop_deferring_access_checks ();
15137
15138   /* Restore saved state.  */
15139   parser->in_function_body = saved_in_function_body;
15140   parser->num_template_parameter_lists
15141     = saved_num_template_parameter_lists;
15142   parser->in_unbraced_linkage_specification_p
15143     = saved_in_unbraced_linkage_specification_p;
15144
15145   return type;
15146 }
15147
15148 /* Parse a class-head.
15149
15150    class-head:
15151      class-key identifier [opt] base-clause [opt]
15152      class-key nested-name-specifier identifier base-clause [opt]
15153      class-key nested-name-specifier [opt] template-id
15154        base-clause [opt]
15155
15156    GNU Extensions:
15157      class-key attributes identifier [opt] base-clause [opt]
15158      class-key attributes nested-name-specifier identifier base-clause [opt]
15159      class-key attributes nested-name-specifier [opt] template-id
15160        base-clause [opt]
15161
15162    Upon return BASES is initialized to the list of base classes (or
15163    NULL, if there are none) in the same form returned by
15164    cp_parser_base_clause.
15165
15166    Returns the TYPE of the indicated class.  Sets
15167    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15168    involving a nested-name-specifier was used, and FALSE otherwise.
15169
15170    Returns error_mark_node if this is not a class-head.
15171
15172    Returns NULL_TREE if the class-head is syntactically valid, but
15173    semantically invalid in a way that means we should skip the entire
15174    body of the class.  */
15175
15176 static tree
15177 cp_parser_class_head (cp_parser* parser,
15178                       bool* nested_name_specifier_p,
15179                       tree *attributes_p,
15180                       tree *bases)
15181 {
15182   tree nested_name_specifier;
15183   enum tag_types class_key;
15184   tree id = NULL_TREE;
15185   tree type = NULL_TREE;
15186   tree attributes;
15187   bool template_id_p = false;
15188   bool qualified_p = false;
15189   bool invalid_nested_name_p = false;
15190   bool invalid_explicit_specialization_p = false;
15191   tree pushed_scope = NULL_TREE;
15192   unsigned num_templates;
15193   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15194   /* Assume no nested-name-specifier will be present.  */
15195   *nested_name_specifier_p = false;
15196   /* Assume no template parameter lists will be used in defining the
15197      type.  */
15198   num_templates = 0;
15199
15200   *bases = NULL_TREE;
15201
15202   /* Look for the class-key.  */
15203   class_key = cp_parser_class_key (parser);
15204   if (class_key == none_type)
15205     return error_mark_node;
15206
15207   /* Parse the attributes.  */
15208   attributes = cp_parser_attributes_opt (parser);
15209
15210   /* If the next token is `::', that is invalid -- but sometimes
15211      people do try to write:
15212
15213        struct ::S {};
15214
15215      Handle this gracefully by accepting the extra qualifier, and then
15216      issuing an error about it later if this really is a
15217      class-head.  If it turns out just to be an elaborated type
15218      specifier, remain silent.  */
15219   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15220     qualified_p = true;
15221
15222   push_deferring_access_checks (dk_no_check);
15223
15224   /* Determine the name of the class.  Begin by looking for an
15225      optional nested-name-specifier.  */
15226   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15227   nested_name_specifier
15228     = cp_parser_nested_name_specifier_opt (parser,
15229                                            /*typename_keyword_p=*/false,
15230                                            /*check_dependency_p=*/false,
15231                                            /*type_p=*/false,
15232                                            /*is_declaration=*/false);
15233   /* If there was a nested-name-specifier, then there *must* be an
15234      identifier.  */
15235   if (nested_name_specifier)
15236     {
15237       type_start_token = cp_lexer_peek_token (parser->lexer);
15238       /* Although the grammar says `identifier', it really means
15239          `class-name' or `template-name'.  You are only allowed to
15240          define a class that has already been declared with this
15241          syntax.
15242
15243          The proposed resolution for Core Issue 180 says that wherever
15244          you see `class T::X' you should treat `X' as a type-name.
15245
15246          It is OK to define an inaccessible class; for example:
15247
15248            class A { class B; };
15249            class A::B {};
15250
15251          We do not know if we will see a class-name, or a
15252          template-name.  We look for a class-name first, in case the
15253          class-name is a template-id; if we looked for the
15254          template-name first we would stop after the template-name.  */
15255       cp_parser_parse_tentatively (parser);
15256       type = cp_parser_class_name (parser,
15257                                    /*typename_keyword_p=*/false,
15258                                    /*template_keyword_p=*/false,
15259                                    class_type,
15260                                    /*check_dependency_p=*/false,
15261                                    /*class_head_p=*/true,
15262                                    /*is_declaration=*/false);
15263       /* If that didn't work, ignore the nested-name-specifier.  */
15264       if (!cp_parser_parse_definitely (parser))
15265         {
15266           invalid_nested_name_p = true;
15267           type_start_token = cp_lexer_peek_token (parser->lexer);
15268           id = cp_parser_identifier (parser);
15269           if (id == error_mark_node)
15270             id = NULL_TREE;
15271         }
15272       /* If we could not find a corresponding TYPE, treat this
15273          declaration like an unqualified declaration.  */
15274       if (type == error_mark_node)
15275         nested_name_specifier = NULL_TREE;
15276       /* Otherwise, count the number of templates used in TYPE and its
15277          containing scopes.  */
15278       else
15279         {
15280           tree scope;
15281
15282           for (scope = TREE_TYPE (type);
15283                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15284                scope = (TYPE_P (scope)
15285                         ? TYPE_CONTEXT (scope)
15286                         : DECL_CONTEXT (scope)))
15287             if (TYPE_P (scope)
15288                 && CLASS_TYPE_P (scope)
15289                 && CLASSTYPE_TEMPLATE_INFO (scope)
15290                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15291                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15292               ++num_templates;
15293         }
15294     }
15295   /* Otherwise, the identifier is optional.  */
15296   else
15297     {
15298       /* We don't know whether what comes next is a template-id,
15299          an identifier, or nothing at all.  */
15300       cp_parser_parse_tentatively (parser);
15301       /* Check for a template-id.  */
15302       type_start_token = cp_lexer_peek_token (parser->lexer);
15303       id = cp_parser_template_id (parser,
15304                                   /*template_keyword_p=*/false,
15305                                   /*check_dependency_p=*/true,
15306                                   /*is_declaration=*/true);
15307       /* If that didn't work, it could still be an identifier.  */
15308       if (!cp_parser_parse_definitely (parser))
15309         {
15310           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15311             {
15312               type_start_token = cp_lexer_peek_token (parser->lexer);
15313               id = cp_parser_identifier (parser);
15314             }
15315           else
15316             id = NULL_TREE;
15317         }
15318       else
15319         {
15320           template_id_p = true;
15321           ++num_templates;
15322         }
15323     }
15324
15325   pop_deferring_access_checks ();
15326
15327   if (id)
15328     cp_parser_check_for_invalid_template_id (parser, id,
15329                                              type_start_token->location);
15330
15331   /* If it's not a `:' or a `{' then we can't really be looking at a
15332      class-head, since a class-head only appears as part of a
15333      class-specifier.  We have to detect this situation before calling
15334      xref_tag, since that has irreversible side-effects.  */
15335   if (!cp_parser_next_token_starts_class_definition_p (parser))
15336     {
15337       cp_parser_error (parser, "expected %<{%> or %<:%>");
15338       return error_mark_node;
15339     }
15340
15341   /* At this point, we're going ahead with the class-specifier, even
15342      if some other problem occurs.  */
15343   cp_parser_commit_to_tentative_parse (parser);
15344   /* Issue the error about the overly-qualified name now.  */
15345   if (qualified_p)
15346     {
15347       cp_parser_error (parser,
15348                        "global qualification of class name is invalid");
15349       return error_mark_node;
15350     }
15351   else if (invalid_nested_name_p)
15352     {
15353       cp_parser_error (parser,
15354                        "qualified name does not name a class");
15355       return error_mark_node;
15356     }
15357   else if (nested_name_specifier)
15358     {
15359       tree scope;
15360
15361       /* Reject typedef-names in class heads.  */
15362       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15363         {
15364           error ("%Hinvalid class name in declaration of %qD",
15365                  &type_start_token->location, type);
15366           type = NULL_TREE;
15367           goto done;
15368         }
15369
15370       /* Figure out in what scope the declaration is being placed.  */
15371       scope = current_scope ();
15372       /* If that scope does not contain the scope in which the
15373          class was originally declared, the program is invalid.  */
15374       if (scope && !is_ancestor (scope, nested_name_specifier))
15375         {
15376           if (at_namespace_scope_p ())
15377             error ("%Hdeclaration of %qD in namespace %qD which does not "
15378                    "enclose %qD",
15379                    &type_start_token->location,
15380                    type, scope, nested_name_specifier);
15381           else
15382             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15383                    &type_start_token->location,
15384                    type, scope, nested_name_specifier);
15385           type = NULL_TREE;
15386           goto done;
15387         }
15388       /* [dcl.meaning]
15389
15390          A declarator-id shall not be qualified except for the
15391          definition of a ... nested class outside of its class
15392          ... [or] the definition or explicit instantiation of a
15393          class member of a namespace outside of its namespace.  */
15394       if (scope == nested_name_specifier)
15395         {
15396           permerror (input_location, "%Hextra qualification not allowed",
15397                      &nested_name_specifier_token_start->location);
15398           nested_name_specifier = NULL_TREE;
15399           num_templates = 0;
15400         }
15401     }
15402   /* An explicit-specialization must be preceded by "template <>".  If
15403      it is not, try to recover gracefully.  */
15404   if (at_namespace_scope_p ()
15405       && parser->num_template_parameter_lists == 0
15406       && template_id_p)
15407     {
15408       error ("%Han explicit specialization must be preceded by %<template <>%>",
15409              &type_start_token->location);
15410       invalid_explicit_specialization_p = true;
15411       /* Take the same action that would have been taken by
15412          cp_parser_explicit_specialization.  */
15413       ++parser->num_template_parameter_lists;
15414       begin_specialization ();
15415     }
15416   /* There must be no "return" statements between this point and the
15417      end of this function; set "type "to the correct return value and
15418      use "goto done;" to return.  */
15419   /* Make sure that the right number of template parameters were
15420      present.  */
15421   if (!cp_parser_check_template_parameters (parser, num_templates,
15422                                             type_start_token->location))
15423     {
15424       /* If something went wrong, there is no point in even trying to
15425          process the class-definition.  */
15426       type = NULL_TREE;
15427       goto done;
15428     }
15429
15430   /* Look up the type.  */
15431   if (template_id_p)
15432     {
15433       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15434           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15435               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15436         {
15437           error ("%Hfunction template %qD redeclared as a class template",
15438                  &type_start_token->location, id);
15439           type = error_mark_node;
15440         }
15441       else
15442         {
15443           type = TREE_TYPE (id);
15444           type = maybe_process_partial_specialization (type);
15445         }
15446       if (nested_name_specifier)
15447         pushed_scope = push_scope (nested_name_specifier);
15448     }
15449   else if (nested_name_specifier)
15450     {
15451       tree class_type;
15452
15453       /* Given:
15454
15455             template <typename T> struct S { struct T };
15456             template <typename T> struct S<T>::T { };
15457
15458          we will get a TYPENAME_TYPE when processing the definition of
15459          `S::T'.  We need to resolve it to the actual type before we
15460          try to define it.  */
15461       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15462         {
15463           class_type = resolve_typename_type (TREE_TYPE (type),
15464                                               /*only_current_p=*/false);
15465           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15466             type = TYPE_NAME (class_type);
15467           else
15468             {
15469               cp_parser_error (parser, "could not resolve typename type");
15470               type = error_mark_node;
15471             }
15472         }
15473
15474       if (maybe_process_partial_specialization (TREE_TYPE (type))
15475           == error_mark_node)
15476         {
15477           type = NULL_TREE;
15478           goto done;
15479         }
15480
15481       class_type = current_class_type;
15482       /* Enter the scope indicated by the nested-name-specifier.  */
15483       pushed_scope = push_scope (nested_name_specifier);
15484       /* Get the canonical version of this type.  */
15485       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15486       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15487           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15488         {
15489           type = push_template_decl (type);
15490           if (type == error_mark_node)
15491             {
15492               type = NULL_TREE;
15493               goto done;
15494             }
15495         }
15496
15497       type = TREE_TYPE (type);
15498       *nested_name_specifier_p = true;
15499     }
15500   else      /* The name is not a nested name.  */
15501     {
15502       /* If the class was unnamed, create a dummy name.  */
15503       if (!id)
15504         id = make_anon_name ();
15505       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15506                        parser->num_template_parameter_lists);
15507     }
15508
15509   /* Indicate whether this class was declared as a `class' or as a
15510      `struct'.  */
15511   if (TREE_CODE (type) == RECORD_TYPE)
15512     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15513   cp_parser_check_class_key (class_key, type);
15514
15515   /* If this type was already complete, and we see another definition,
15516      that's an error.  */
15517   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15518     {
15519       error ("%Hredefinition of %q#T",
15520              &type_start_token->location, type);
15521       error ("%Hprevious definition of %q+#T",
15522              &type_start_token->location, type);
15523       type = NULL_TREE;
15524       goto done;
15525     }
15526   else if (type == error_mark_node)
15527     type = NULL_TREE;
15528
15529   /* We will have entered the scope containing the class; the names of
15530      base classes should be looked up in that context.  For example:
15531
15532        struct A { struct B {}; struct C; };
15533        struct A::C : B {};
15534
15535      is valid.  */
15536
15537   /* Get the list of base-classes, if there is one.  */
15538   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15539     *bases = cp_parser_base_clause (parser);
15540
15541  done:
15542   /* Leave the scope given by the nested-name-specifier.  We will
15543      enter the class scope itself while processing the members.  */
15544   if (pushed_scope)
15545     pop_scope (pushed_scope);
15546
15547   if (invalid_explicit_specialization_p)
15548     {
15549       end_specialization ();
15550       --parser->num_template_parameter_lists;
15551     }
15552   *attributes_p = attributes;
15553   return type;
15554 }
15555
15556 /* Parse a class-key.
15557
15558    class-key:
15559      class
15560      struct
15561      union
15562
15563    Returns the kind of class-key specified, or none_type to indicate
15564    error.  */
15565
15566 static enum tag_types
15567 cp_parser_class_key (cp_parser* parser)
15568 {
15569   cp_token *token;
15570   enum tag_types tag_type;
15571
15572   /* Look for the class-key.  */
15573   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15574   if (!token)
15575     return none_type;
15576
15577   /* Check to see if the TOKEN is a class-key.  */
15578   tag_type = cp_parser_token_is_class_key (token);
15579   if (!tag_type)
15580     cp_parser_error (parser, "expected class-key");
15581   return tag_type;
15582 }
15583
15584 /* Parse an (optional) member-specification.
15585
15586    member-specification:
15587      member-declaration member-specification [opt]
15588      access-specifier : member-specification [opt]  */
15589
15590 static void
15591 cp_parser_member_specification_opt (cp_parser* parser)
15592 {
15593   while (true)
15594     {
15595       cp_token *token;
15596       enum rid keyword;
15597
15598       /* Peek at the next token.  */
15599       token = cp_lexer_peek_token (parser->lexer);
15600       /* If it's a `}', or EOF then we've seen all the members.  */
15601       if (token->type == CPP_CLOSE_BRACE
15602           || token->type == CPP_EOF
15603           || token->type == CPP_PRAGMA_EOL)
15604         break;
15605
15606       /* See if this token is a keyword.  */
15607       keyword = token->keyword;
15608       switch (keyword)
15609         {
15610         case RID_PUBLIC:
15611         case RID_PROTECTED:
15612         case RID_PRIVATE:
15613           /* Consume the access-specifier.  */
15614           cp_lexer_consume_token (parser->lexer);
15615           /* Remember which access-specifier is active.  */
15616           current_access_specifier = token->u.value;
15617           /* Look for the `:'.  */
15618           cp_parser_require (parser, CPP_COLON, "%<:%>");
15619           break;
15620
15621         default:
15622           /* Accept #pragmas at class scope.  */
15623           if (token->type == CPP_PRAGMA)
15624             {
15625               cp_parser_pragma (parser, pragma_external);
15626               break;
15627             }
15628
15629           /* Otherwise, the next construction must be a
15630              member-declaration.  */
15631           cp_parser_member_declaration (parser);
15632         }
15633     }
15634 }
15635
15636 /* Parse a member-declaration.
15637
15638    member-declaration:
15639      decl-specifier-seq [opt] member-declarator-list [opt] ;
15640      function-definition ; [opt]
15641      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15642      using-declaration
15643      template-declaration
15644
15645    member-declarator-list:
15646      member-declarator
15647      member-declarator-list , member-declarator
15648
15649    member-declarator:
15650      declarator pure-specifier [opt]
15651      declarator constant-initializer [opt]
15652      identifier [opt] : constant-expression
15653
15654    GNU Extensions:
15655
15656    member-declaration:
15657      __extension__ member-declaration
15658
15659    member-declarator:
15660      declarator attributes [opt] pure-specifier [opt]
15661      declarator attributes [opt] constant-initializer [opt]
15662      identifier [opt] attributes [opt] : constant-expression  
15663
15664    C++0x Extensions:
15665
15666    member-declaration:
15667      static_assert-declaration  */
15668
15669 static void
15670 cp_parser_member_declaration (cp_parser* parser)
15671 {
15672   cp_decl_specifier_seq decl_specifiers;
15673   tree prefix_attributes;
15674   tree decl;
15675   int declares_class_or_enum;
15676   bool friend_p;
15677   cp_token *token = NULL;
15678   cp_token *decl_spec_token_start = NULL;
15679   cp_token *initializer_token_start = NULL;
15680   int saved_pedantic;
15681
15682   /* Check for the `__extension__' keyword.  */
15683   if (cp_parser_extension_opt (parser, &saved_pedantic))
15684     {
15685       /* Recurse.  */
15686       cp_parser_member_declaration (parser);
15687       /* Restore the old value of the PEDANTIC flag.  */
15688       pedantic = saved_pedantic;
15689
15690       return;
15691     }
15692
15693   /* Check for a template-declaration.  */
15694   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15695     {
15696       /* An explicit specialization here is an error condition, and we
15697          expect the specialization handler to detect and report this.  */
15698       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15699           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15700         cp_parser_explicit_specialization (parser);
15701       else
15702         cp_parser_template_declaration (parser, /*member_p=*/true);
15703
15704       return;
15705     }
15706
15707   /* Check for a using-declaration.  */
15708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15709     {
15710       /* Parse the using-declaration.  */
15711       cp_parser_using_declaration (parser,
15712                                    /*access_declaration_p=*/false);
15713       return;
15714     }
15715
15716   /* Check for @defs.  */
15717   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15718     {
15719       tree ivar, member;
15720       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15721       ivar = ivar_chains;
15722       while (ivar)
15723         {
15724           member = ivar;
15725           ivar = TREE_CHAIN (member);
15726           TREE_CHAIN (member) = NULL_TREE;
15727           finish_member_declaration (member);
15728         }
15729       return;
15730     }
15731
15732   /* If the next token is `static_assert' we have a static assertion.  */
15733   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15734     {
15735       cp_parser_static_assert (parser, /*member_p=*/true);
15736       return;
15737     }
15738
15739   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15740     return;
15741
15742   /* Parse the decl-specifier-seq.  */
15743   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15744   cp_parser_decl_specifier_seq (parser,
15745                                 CP_PARSER_FLAGS_OPTIONAL,
15746                                 &decl_specifiers,
15747                                 &declares_class_or_enum);
15748   prefix_attributes = decl_specifiers.attributes;
15749   decl_specifiers.attributes = NULL_TREE;
15750   /* Check for an invalid type-name.  */
15751   if (!decl_specifiers.type
15752       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15753     return;
15754   /* If there is no declarator, then the decl-specifier-seq should
15755      specify a type.  */
15756   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15757     {
15758       /* If there was no decl-specifier-seq, and the next token is a
15759          `;', then we have something like:
15760
15761            struct S { ; };
15762
15763          [class.mem]
15764
15765          Each member-declaration shall declare at least one member
15766          name of the class.  */
15767       if (!decl_specifiers.any_specifiers_p)
15768         {
15769           cp_token *token = cp_lexer_peek_token (parser->lexer);
15770           if (!in_system_header_at (token->location))
15771             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15772         }
15773       else
15774         {
15775           tree type;
15776
15777           /* See if this declaration is a friend.  */
15778           friend_p = cp_parser_friend_p (&decl_specifiers);
15779           /* If there were decl-specifiers, check to see if there was
15780              a class-declaration.  */
15781           type = check_tag_decl (&decl_specifiers);
15782           /* Nested classes have already been added to the class, but
15783              a `friend' needs to be explicitly registered.  */
15784           if (friend_p)
15785             {
15786               /* If the `friend' keyword was present, the friend must
15787                  be introduced with a class-key.  */
15788                if (!declares_class_or_enum)
15789                  error ("%Ha class-key must be used when declaring a friend",
15790                         &decl_spec_token_start->location);
15791                /* In this case:
15792
15793                     template <typename T> struct A {
15794                       friend struct A<T>::B;
15795                     };
15796
15797                   A<T>::B will be represented by a TYPENAME_TYPE, and
15798                   therefore not recognized by check_tag_decl.  */
15799                if (!type
15800                    && decl_specifiers.type
15801                    && TYPE_P (decl_specifiers.type))
15802                  type = decl_specifiers.type;
15803                if (!type || !TYPE_P (type))
15804                  error ("%Hfriend declaration does not name a class or "
15805                         "function", &decl_spec_token_start->location);
15806                else
15807                  make_friend_class (current_class_type, type,
15808                                     /*complain=*/true);
15809             }
15810           /* If there is no TYPE, an error message will already have
15811              been issued.  */
15812           else if (!type || type == error_mark_node)
15813             ;
15814           /* An anonymous aggregate has to be handled specially; such
15815              a declaration really declares a data member (with a
15816              particular type), as opposed to a nested class.  */
15817           else if (ANON_AGGR_TYPE_P (type))
15818             {
15819               /* Remove constructors and such from TYPE, now that we
15820                  know it is an anonymous aggregate.  */
15821               fixup_anonymous_aggr (type);
15822               /* And make the corresponding data member.  */
15823               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15824               /* Add it to the class.  */
15825               finish_member_declaration (decl);
15826             }
15827           else
15828             cp_parser_check_access_in_redeclaration
15829                                               (TYPE_NAME (type),
15830                                                decl_spec_token_start->location);
15831         }
15832     }
15833   else
15834     {
15835       /* See if these declarations will be friends.  */
15836       friend_p = cp_parser_friend_p (&decl_specifiers);
15837
15838       /* Keep going until we hit the `;' at the end of the
15839          declaration.  */
15840       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15841         {
15842           tree attributes = NULL_TREE;
15843           tree first_attribute;
15844
15845           /* Peek at the next token.  */
15846           token = cp_lexer_peek_token (parser->lexer);
15847
15848           /* Check for a bitfield declaration.  */
15849           if (token->type == CPP_COLON
15850               || (token->type == CPP_NAME
15851                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15852                   == CPP_COLON))
15853             {
15854               tree identifier;
15855               tree width;
15856
15857               /* Get the name of the bitfield.  Note that we cannot just
15858                  check TOKEN here because it may have been invalidated by
15859                  the call to cp_lexer_peek_nth_token above.  */
15860               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15861                 identifier = cp_parser_identifier (parser);
15862               else
15863                 identifier = NULL_TREE;
15864
15865               /* Consume the `:' token.  */
15866               cp_lexer_consume_token (parser->lexer);
15867               /* Get the width of the bitfield.  */
15868               width
15869                 = cp_parser_constant_expression (parser,
15870                                                  /*allow_non_constant=*/false,
15871                                                  NULL);
15872
15873               /* Look for attributes that apply to the bitfield.  */
15874               attributes = cp_parser_attributes_opt (parser);
15875               /* Remember which attributes are prefix attributes and
15876                  which are not.  */
15877               first_attribute = attributes;
15878               /* Combine the attributes.  */
15879               attributes = chainon (prefix_attributes, attributes);
15880
15881               /* Create the bitfield declaration.  */
15882               decl = grokbitfield (identifier
15883                                    ? make_id_declarator (NULL_TREE,
15884                                                          identifier,
15885                                                          sfk_none)
15886                                    : NULL,
15887                                    &decl_specifiers,
15888                                    width,
15889                                    attributes);
15890             }
15891           else
15892             {
15893               cp_declarator *declarator;
15894               tree initializer;
15895               tree asm_specification;
15896               int ctor_dtor_or_conv_p;
15897
15898               /* Parse the declarator.  */
15899               declarator
15900                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15901                                         &ctor_dtor_or_conv_p,
15902                                         /*parenthesized_p=*/NULL,
15903                                         /*member_p=*/true);
15904
15905               /* If something went wrong parsing the declarator, make sure
15906                  that we at least consume some tokens.  */
15907               if (declarator == cp_error_declarator)
15908                 {
15909                   /* Skip to the end of the statement.  */
15910                   cp_parser_skip_to_end_of_statement (parser);
15911                   /* If the next token is not a semicolon, that is
15912                      probably because we just skipped over the body of
15913                      a function.  So, we consume a semicolon if
15914                      present, but do not issue an error message if it
15915                      is not present.  */
15916                   if (cp_lexer_next_token_is (parser->lexer,
15917                                               CPP_SEMICOLON))
15918                     cp_lexer_consume_token (parser->lexer);
15919                   return;
15920                 }
15921
15922               if (declares_class_or_enum & 2)
15923                 cp_parser_check_for_definition_in_return_type
15924                                             (declarator, decl_specifiers.type,
15925                                              decl_specifiers.type_location);
15926
15927               /* Look for an asm-specification.  */
15928               asm_specification = cp_parser_asm_specification_opt (parser);
15929               /* Look for attributes that apply to the declaration.  */
15930               attributes = cp_parser_attributes_opt (parser);
15931               /* Remember which attributes are prefix attributes and
15932                  which are not.  */
15933               first_attribute = attributes;
15934               /* Combine the attributes.  */
15935               attributes = chainon (prefix_attributes, attributes);
15936
15937               /* If it's an `=', then we have a constant-initializer or a
15938                  pure-specifier.  It is not correct to parse the
15939                  initializer before registering the member declaration
15940                  since the member declaration should be in scope while
15941                  its initializer is processed.  However, the rest of the
15942                  front end does not yet provide an interface that allows
15943                  us to handle this correctly.  */
15944               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15945                 {
15946                   /* In [class.mem]:
15947
15948                      A pure-specifier shall be used only in the declaration of
15949                      a virtual function.
15950
15951                      A member-declarator can contain a constant-initializer
15952                      only if it declares a static member of integral or
15953                      enumeration type.
15954
15955                      Therefore, if the DECLARATOR is for a function, we look
15956                      for a pure-specifier; otherwise, we look for a
15957                      constant-initializer.  When we call `grokfield', it will
15958                      perform more stringent semantics checks.  */
15959                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15960                   if (function_declarator_p (declarator))
15961                     initializer = cp_parser_pure_specifier (parser);
15962                   else
15963                     /* Parse the initializer.  */
15964                     initializer = cp_parser_constant_initializer (parser);
15965                 }
15966               /* Otherwise, there is no initializer.  */
15967               else
15968                 initializer = NULL_TREE;
15969
15970               /* See if we are probably looking at a function
15971                  definition.  We are certainly not looking at a
15972                  member-declarator.  Calling `grokfield' has
15973                  side-effects, so we must not do it unless we are sure
15974                  that we are looking at a member-declarator.  */
15975               if (cp_parser_token_starts_function_definition_p
15976                   (cp_lexer_peek_token (parser->lexer)))
15977                 {
15978                   /* The grammar does not allow a pure-specifier to be
15979                      used when a member function is defined.  (It is
15980                      possible that this fact is an oversight in the
15981                      standard, since a pure function may be defined
15982                      outside of the class-specifier.  */
15983                   if (initializer)
15984                     error ("%Hpure-specifier on function-definition",
15985                            &initializer_token_start->location);
15986                   decl = cp_parser_save_member_function_body (parser,
15987                                                               &decl_specifiers,
15988                                                               declarator,
15989                                                               attributes);
15990                   /* If the member was not a friend, declare it here.  */
15991                   if (!friend_p)
15992                     finish_member_declaration (decl);
15993                   /* Peek at the next token.  */
15994                   token = cp_lexer_peek_token (parser->lexer);
15995                   /* If the next token is a semicolon, consume it.  */
15996                   if (token->type == CPP_SEMICOLON)
15997                     cp_lexer_consume_token (parser->lexer);
15998                   return;
15999                 }
16000               else
16001                 if (declarator->kind == cdk_function)
16002                   declarator->id_loc = token->location;
16003                 /* Create the declaration.  */
16004                 decl = grokfield (declarator, &decl_specifiers,
16005                                   initializer, /*init_const_expr_p=*/true,
16006                                   asm_specification,
16007                                   attributes);
16008             }
16009
16010           /* Reset PREFIX_ATTRIBUTES.  */
16011           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16012             attributes = TREE_CHAIN (attributes);
16013           if (attributes)
16014             TREE_CHAIN (attributes) = NULL_TREE;
16015
16016           /* If there is any qualification still in effect, clear it
16017              now; we will be starting fresh with the next declarator.  */
16018           parser->scope = NULL_TREE;
16019           parser->qualifying_scope = NULL_TREE;
16020           parser->object_scope = NULL_TREE;
16021           /* If it's a `,', then there are more declarators.  */
16022           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16023             cp_lexer_consume_token (parser->lexer);
16024           /* If the next token isn't a `;', then we have a parse error.  */
16025           else if (cp_lexer_next_token_is_not (parser->lexer,
16026                                                CPP_SEMICOLON))
16027             {
16028               cp_parser_error (parser, "expected %<;%>");
16029               /* Skip tokens until we find a `;'.  */
16030               cp_parser_skip_to_end_of_statement (parser);
16031
16032               break;
16033             }
16034
16035           if (decl)
16036             {
16037               /* Add DECL to the list of members.  */
16038               if (!friend_p)
16039                 finish_member_declaration (decl);
16040
16041               if (TREE_CODE (decl) == FUNCTION_DECL)
16042                 cp_parser_save_default_args (parser, decl);
16043             }
16044         }
16045     }
16046
16047   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16048 }
16049
16050 /* Parse a pure-specifier.
16051
16052    pure-specifier:
16053      = 0
16054
16055    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16056    Otherwise, ERROR_MARK_NODE is returned.  */
16057
16058 static tree
16059 cp_parser_pure_specifier (cp_parser* parser)
16060 {
16061   cp_token *token;
16062
16063   /* Look for the `=' token.  */
16064   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16065     return error_mark_node;
16066   /* Look for the `0' token.  */
16067   token = cp_lexer_peek_token (parser->lexer);
16068
16069   if (token->type == CPP_EOF
16070       || token->type == CPP_PRAGMA_EOL)
16071     return error_mark_node;
16072
16073   cp_lexer_consume_token (parser->lexer);
16074
16075   /* Accept = default or = delete in c++0x mode.  */
16076   if (token->keyword == RID_DEFAULT
16077       || token->keyword == RID_DELETE)
16078     {
16079       maybe_warn_cpp0x ("defaulted and deleted functions");
16080       return token->u.value;
16081     }
16082
16083   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16084   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16085     {
16086       cp_parser_error (parser,
16087                        "invalid pure specifier (only %<= 0%> is allowed)");
16088       cp_parser_skip_to_end_of_statement (parser);
16089       return error_mark_node;
16090     }
16091   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16092     {
16093       error ("%Htemplates may not be %<virtual%>", &token->location);
16094       return error_mark_node;
16095     }
16096
16097   return integer_zero_node;
16098 }
16099
16100 /* Parse a constant-initializer.
16101
16102    constant-initializer:
16103      = constant-expression
16104
16105    Returns a representation of the constant-expression.  */
16106
16107 static tree
16108 cp_parser_constant_initializer (cp_parser* parser)
16109 {
16110   /* Look for the `=' token.  */
16111   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16112     return error_mark_node;
16113
16114   /* It is invalid to write:
16115
16116        struct S { static const int i = { 7 }; };
16117
16118      */
16119   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16120     {
16121       cp_parser_error (parser,
16122                        "a brace-enclosed initializer is not allowed here");
16123       /* Consume the opening brace.  */
16124       cp_lexer_consume_token (parser->lexer);
16125       /* Skip the initializer.  */
16126       cp_parser_skip_to_closing_brace (parser);
16127       /* Look for the trailing `}'.  */
16128       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16129
16130       return error_mark_node;
16131     }
16132
16133   return cp_parser_constant_expression (parser,
16134                                         /*allow_non_constant=*/false,
16135                                         NULL);
16136 }
16137
16138 /* Derived classes [gram.class.derived] */
16139
16140 /* Parse a base-clause.
16141
16142    base-clause:
16143      : base-specifier-list
16144
16145    base-specifier-list:
16146      base-specifier ... [opt]
16147      base-specifier-list , base-specifier ... [opt]
16148
16149    Returns a TREE_LIST representing the base-classes, in the order in
16150    which they were declared.  The representation of each node is as
16151    described by cp_parser_base_specifier.
16152
16153    In the case that no bases are specified, this function will return
16154    NULL_TREE, not ERROR_MARK_NODE.  */
16155
16156 static tree
16157 cp_parser_base_clause (cp_parser* parser)
16158 {
16159   tree bases = NULL_TREE;
16160
16161   /* Look for the `:' that begins the list.  */
16162   cp_parser_require (parser, CPP_COLON, "%<:%>");
16163
16164   /* Scan the base-specifier-list.  */
16165   while (true)
16166     {
16167       cp_token *token;
16168       tree base;
16169       bool pack_expansion_p = false;
16170
16171       /* Look for the base-specifier.  */
16172       base = cp_parser_base_specifier (parser);
16173       /* Look for the (optional) ellipsis. */
16174       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16175         {
16176           /* Consume the `...'. */
16177           cp_lexer_consume_token (parser->lexer);
16178
16179           pack_expansion_p = true;
16180         }
16181
16182       /* Add BASE to the front of the list.  */
16183       if (base != error_mark_node)
16184         {
16185           if (pack_expansion_p)
16186             /* Make this a pack expansion type. */
16187             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16188           
16189
16190           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16191             {
16192               TREE_CHAIN (base) = bases;
16193               bases = base;
16194             }
16195         }
16196       /* Peek at the next token.  */
16197       token = cp_lexer_peek_token (parser->lexer);
16198       /* If it's not a comma, then the list is complete.  */
16199       if (token->type != CPP_COMMA)
16200         break;
16201       /* Consume the `,'.  */
16202       cp_lexer_consume_token (parser->lexer);
16203     }
16204
16205   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16206      base class had a qualified name.  However, the next name that
16207      appears is certainly not qualified.  */
16208   parser->scope = NULL_TREE;
16209   parser->qualifying_scope = NULL_TREE;
16210   parser->object_scope = NULL_TREE;
16211
16212   return nreverse (bases);
16213 }
16214
16215 /* Parse a base-specifier.
16216
16217    base-specifier:
16218      :: [opt] nested-name-specifier [opt] class-name
16219      virtual access-specifier [opt] :: [opt] nested-name-specifier
16220        [opt] class-name
16221      access-specifier virtual [opt] :: [opt] nested-name-specifier
16222        [opt] class-name
16223
16224    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16225    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16226    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16227    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16228
16229 static tree
16230 cp_parser_base_specifier (cp_parser* parser)
16231 {
16232   cp_token *token;
16233   bool done = false;
16234   bool virtual_p = false;
16235   bool duplicate_virtual_error_issued_p = false;
16236   bool duplicate_access_error_issued_p = false;
16237   bool class_scope_p, template_p;
16238   tree access = access_default_node;
16239   tree type;
16240
16241   /* Process the optional `virtual' and `access-specifier'.  */
16242   while (!done)
16243     {
16244       /* Peek at the next token.  */
16245       token = cp_lexer_peek_token (parser->lexer);
16246       /* Process `virtual'.  */
16247       switch (token->keyword)
16248         {
16249         case RID_VIRTUAL:
16250           /* If `virtual' appears more than once, issue an error.  */
16251           if (virtual_p && !duplicate_virtual_error_issued_p)
16252             {
16253               cp_parser_error (parser,
16254                                "%<virtual%> specified more than once in base-specified");
16255               duplicate_virtual_error_issued_p = true;
16256             }
16257
16258           virtual_p = true;
16259
16260           /* Consume the `virtual' token.  */
16261           cp_lexer_consume_token (parser->lexer);
16262
16263           break;
16264
16265         case RID_PUBLIC:
16266         case RID_PROTECTED:
16267         case RID_PRIVATE:
16268           /* If more than one access specifier appears, issue an
16269              error.  */
16270           if (access != access_default_node
16271               && !duplicate_access_error_issued_p)
16272             {
16273               cp_parser_error (parser,
16274                                "more than one access specifier in base-specified");
16275               duplicate_access_error_issued_p = true;
16276             }
16277
16278           access = ridpointers[(int) token->keyword];
16279
16280           /* Consume the access-specifier.  */
16281           cp_lexer_consume_token (parser->lexer);
16282
16283           break;
16284
16285         default:
16286           done = true;
16287           break;
16288         }
16289     }
16290   /* It is not uncommon to see programs mechanically, erroneously, use
16291      the 'typename' keyword to denote (dependent) qualified types
16292      as base classes.  */
16293   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16294     {
16295       token = cp_lexer_peek_token (parser->lexer);
16296       if (!processing_template_decl)
16297         error ("%Hkeyword %<typename%> not allowed outside of templates",
16298                &token->location);
16299       else
16300         error ("%Hkeyword %<typename%> not allowed in this context "
16301                "(the base class is implicitly a type)",
16302                &token->location);
16303       cp_lexer_consume_token (parser->lexer);
16304     }
16305
16306   /* Look for the optional `::' operator.  */
16307   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16308   /* Look for the nested-name-specifier.  The simplest way to
16309      implement:
16310
16311        [temp.res]
16312
16313        The keyword `typename' is not permitted in a base-specifier or
16314        mem-initializer; in these contexts a qualified name that
16315        depends on a template-parameter is implicitly assumed to be a
16316        type name.
16317
16318      is to pretend that we have seen the `typename' keyword at this
16319      point.  */
16320   cp_parser_nested_name_specifier_opt (parser,
16321                                        /*typename_keyword_p=*/true,
16322                                        /*check_dependency_p=*/true,
16323                                        typename_type,
16324                                        /*is_declaration=*/true);
16325   /* If the base class is given by a qualified name, assume that names
16326      we see are type names or templates, as appropriate.  */
16327   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16328   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16329
16330   /* Finally, look for the class-name.  */
16331   type = cp_parser_class_name (parser,
16332                                class_scope_p,
16333                                template_p,
16334                                typename_type,
16335                                /*check_dependency_p=*/true,
16336                                /*class_head_p=*/false,
16337                                /*is_declaration=*/true);
16338
16339   if (type == error_mark_node)
16340     return error_mark_node;
16341
16342   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16343 }
16344
16345 /* Exception handling [gram.exception] */
16346
16347 /* Parse an (optional) exception-specification.
16348
16349    exception-specification:
16350      throw ( type-id-list [opt] )
16351
16352    Returns a TREE_LIST representing the exception-specification.  The
16353    TREE_VALUE of each node is a type.  */
16354
16355 static tree
16356 cp_parser_exception_specification_opt (cp_parser* parser)
16357 {
16358   cp_token *token;
16359   tree type_id_list;
16360
16361   /* Peek at the next token.  */
16362   token = cp_lexer_peek_token (parser->lexer);
16363   /* If it's not `throw', then there's no exception-specification.  */
16364   if (!cp_parser_is_keyword (token, RID_THROW))
16365     return NULL_TREE;
16366
16367   /* Consume the `throw'.  */
16368   cp_lexer_consume_token (parser->lexer);
16369
16370   /* Look for the `('.  */
16371   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16372
16373   /* Peek at the next token.  */
16374   token = cp_lexer_peek_token (parser->lexer);
16375   /* If it's not a `)', then there is a type-id-list.  */
16376   if (token->type != CPP_CLOSE_PAREN)
16377     {
16378       const char *saved_message;
16379
16380       /* Types may not be defined in an exception-specification.  */
16381       saved_message = parser->type_definition_forbidden_message;
16382       parser->type_definition_forbidden_message
16383         = "types may not be defined in an exception-specification";
16384       /* Parse the type-id-list.  */
16385       type_id_list = cp_parser_type_id_list (parser);
16386       /* Restore the saved message.  */
16387       parser->type_definition_forbidden_message = saved_message;
16388     }
16389   else
16390     type_id_list = empty_except_spec;
16391
16392   /* Look for the `)'.  */
16393   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16394
16395   return type_id_list;
16396 }
16397
16398 /* Parse an (optional) type-id-list.
16399
16400    type-id-list:
16401      type-id ... [opt]
16402      type-id-list , type-id ... [opt]
16403
16404    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16405    in the order that the types were presented.  */
16406
16407 static tree
16408 cp_parser_type_id_list (cp_parser* parser)
16409 {
16410   tree types = NULL_TREE;
16411
16412   while (true)
16413     {
16414       cp_token *token;
16415       tree type;
16416
16417       /* Get the next type-id.  */
16418       type = cp_parser_type_id (parser);
16419       /* Parse the optional ellipsis. */
16420       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16421         {
16422           /* Consume the `...'. */
16423           cp_lexer_consume_token (parser->lexer);
16424
16425           /* Turn the type into a pack expansion expression. */
16426           type = make_pack_expansion (type);
16427         }
16428       /* Add it to the list.  */
16429       types = add_exception_specifier (types, type, /*complain=*/1);
16430       /* Peek at the next token.  */
16431       token = cp_lexer_peek_token (parser->lexer);
16432       /* If it is not a `,', we are done.  */
16433       if (token->type != CPP_COMMA)
16434         break;
16435       /* Consume the `,'.  */
16436       cp_lexer_consume_token (parser->lexer);
16437     }
16438
16439   return nreverse (types);
16440 }
16441
16442 /* Parse a try-block.
16443
16444    try-block:
16445      try compound-statement handler-seq  */
16446
16447 static tree
16448 cp_parser_try_block (cp_parser* parser)
16449 {
16450   tree try_block;
16451
16452   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16453   try_block = begin_try_block ();
16454   cp_parser_compound_statement (parser, NULL, true);
16455   finish_try_block (try_block);
16456   cp_parser_handler_seq (parser);
16457   finish_handler_sequence (try_block);
16458
16459   return try_block;
16460 }
16461
16462 /* Parse a function-try-block.
16463
16464    function-try-block:
16465      try ctor-initializer [opt] function-body handler-seq  */
16466
16467 static bool
16468 cp_parser_function_try_block (cp_parser* parser)
16469 {
16470   tree compound_stmt;
16471   tree try_block;
16472   bool ctor_initializer_p;
16473
16474   /* Look for the `try' keyword.  */
16475   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16476     return false;
16477   /* Let the rest of the front end know where we are.  */
16478   try_block = begin_function_try_block (&compound_stmt);
16479   /* Parse the function-body.  */
16480   ctor_initializer_p
16481     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16482   /* We're done with the `try' part.  */
16483   finish_function_try_block (try_block);
16484   /* Parse the handlers.  */
16485   cp_parser_handler_seq (parser);
16486   /* We're done with the handlers.  */
16487   finish_function_handler_sequence (try_block, compound_stmt);
16488
16489   return ctor_initializer_p;
16490 }
16491
16492 /* Parse a handler-seq.
16493
16494    handler-seq:
16495      handler handler-seq [opt]  */
16496
16497 static void
16498 cp_parser_handler_seq (cp_parser* parser)
16499 {
16500   while (true)
16501     {
16502       cp_token *token;
16503
16504       /* Parse the handler.  */
16505       cp_parser_handler (parser);
16506       /* Peek at the next token.  */
16507       token = cp_lexer_peek_token (parser->lexer);
16508       /* If it's not `catch' then there are no more handlers.  */
16509       if (!cp_parser_is_keyword (token, RID_CATCH))
16510         break;
16511     }
16512 }
16513
16514 /* Parse a handler.
16515
16516    handler:
16517      catch ( exception-declaration ) compound-statement  */
16518
16519 static void
16520 cp_parser_handler (cp_parser* parser)
16521 {
16522   tree handler;
16523   tree declaration;
16524
16525   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16526   handler = begin_handler ();
16527   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16528   declaration = cp_parser_exception_declaration (parser);
16529   finish_handler_parms (declaration, handler);
16530   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16531   cp_parser_compound_statement (parser, NULL, false);
16532   finish_handler (handler);
16533 }
16534
16535 /* Parse an exception-declaration.
16536
16537    exception-declaration:
16538      type-specifier-seq declarator
16539      type-specifier-seq abstract-declarator
16540      type-specifier-seq
16541      ...
16542
16543    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16544    ellipsis variant is used.  */
16545
16546 static tree
16547 cp_parser_exception_declaration (cp_parser* parser)
16548 {
16549   cp_decl_specifier_seq type_specifiers;
16550   cp_declarator *declarator;
16551   const char *saved_message;
16552
16553   /* If it's an ellipsis, it's easy to handle.  */
16554   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16555     {
16556       /* Consume the `...' token.  */
16557       cp_lexer_consume_token (parser->lexer);
16558       return NULL_TREE;
16559     }
16560
16561   /* Types may not be defined in exception-declarations.  */
16562   saved_message = parser->type_definition_forbidden_message;
16563   parser->type_definition_forbidden_message
16564     = "types may not be defined in exception-declarations";
16565
16566   /* Parse the type-specifier-seq.  */
16567   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16568                                 &type_specifiers);
16569   /* If it's a `)', then there is no declarator.  */
16570   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16571     declarator = NULL;
16572   else
16573     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16574                                        /*ctor_dtor_or_conv_p=*/NULL,
16575                                        /*parenthesized_p=*/NULL,
16576                                        /*member_p=*/false);
16577
16578   /* Restore the saved message.  */
16579   parser->type_definition_forbidden_message = saved_message;
16580
16581   if (!type_specifiers.any_specifiers_p)
16582     return error_mark_node;
16583
16584   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16585 }
16586
16587 /* Parse a throw-expression.
16588
16589    throw-expression:
16590      throw assignment-expression [opt]
16591
16592    Returns a THROW_EXPR representing the throw-expression.  */
16593
16594 static tree
16595 cp_parser_throw_expression (cp_parser* parser)
16596 {
16597   tree expression;
16598   cp_token* token;
16599
16600   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16601   token = cp_lexer_peek_token (parser->lexer);
16602   /* Figure out whether or not there is an assignment-expression
16603      following the "throw" keyword.  */
16604   if (token->type == CPP_COMMA
16605       || token->type == CPP_SEMICOLON
16606       || token->type == CPP_CLOSE_PAREN
16607       || token->type == CPP_CLOSE_SQUARE
16608       || token->type == CPP_CLOSE_BRACE
16609       || token->type == CPP_COLON)
16610     expression = NULL_TREE;
16611   else
16612     expression = cp_parser_assignment_expression (parser,
16613                                                   /*cast_p=*/false, NULL);
16614
16615   return build_throw (expression);
16616 }
16617
16618 /* GNU Extensions */
16619
16620 /* Parse an (optional) asm-specification.
16621
16622    asm-specification:
16623      asm ( string-literal )
16624
16625    If the asm-specification is present, returns a STRING_CST
16626    corresponding to the string-literal.  Otherwise, returns
16627    NULL_TREE.  */
16628
16629 static tree
16630 cp_parser_asm_specification_opt (cp_parser* parser)
16631 {
16632   cp_token *token;
16633   tree asm_specification;
16634
16635   /* Peek at the next token.  */
16636   token = cp_lexer_peek_token (parser->lexer);
16637   /* If the next token isn't the `asm' keyword, then there's no
16638      asm-specification.  */
16639   if (!cp_parser_is_keyword (token, RID_ASM))
16640     return NULL_TREE;
16641
16642   /* Consume the `asm' token.  */
16643   cp_lexer_consume_token (parser->lexer);
16644   /* Look for the `('.  */
16645   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16646
16647   /* Look for the string-literal.  */
16648   asm_specification = cp_parser_string_literal (parser, false, false);
16649
16650   /* Look for the `)'.  */
16651   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16652
16653   return asm_specification;
16654 }
16655
16656 /* Parse an asm-operand-list.
16657
16658    asm-operand-list:
16659      asm-operand
16660      asm-operand-list , asm-operand
16661
16662    asm-operand:
16663      string-literal ( expression )
16664      [ string-literal ] string-literal ( expression )
16665
16666    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16667    each node is the expression.  The TREE_PURPOSE is itself a
16668    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16669    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16670    is a STRING_CST for the string literal before the parenthesis. Returns
16671    ERROR_MARK_NODE if any of the operands are invalid.  */
16672
16673 static tree
16674 cp_parser_asm_operand_list (cp_parser* parser)
16675 {
16676   tree asm_operands = NULL_TREE;
16677   bool invalid_operands = false;
16678
16679   while (true)
16680     {
16681       tree string_literal;
16682       tree expression;
16683       tree name;
16684
16685       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16686         {
16687           /* Consume the `[' token.  */
16688           cp_lexer_consume_token (parser->lexer);
16689           /* Read the operand name.  */
16690           name = cp_parser_identifier (parser);
16691           if (name != error_mark_node)
16692             name = build_string (IDENTIFIER_LENGTH (name),
16693                                  IDENTIFIER_POINTER (name));
16694           /* Look for the closing `]'.  */
16695           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16696         }
16697       else
16698         name = NULL_TREE;
16699       /* Look for the string-literal.  */
16700       string_literal = cp_parser_string_literal (parser, false, false);
16701
16702       /* Look for the `('.  */
16703       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16704       /* Parse the expression.  */
16705       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16706       /* Look for the `)'.  */
16707       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16708
16709       if (name == error_mark_node 
16710           || string_literal == error_mark_node 
16711           || expression == error_mark_node)
16712         invalid_operands = true;
16713
16714       /* Add this operand to the list.  */
16715       asm_operands = tree_cons (build_tree_list (name, string_literal),
16716                                 expression,
16717                                 asm_operands);
16718       /* If the next token is not a `,', there are no more
16719          operands.  */
16720       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16721         break;
16722       /* Consume the `,'.  */
16723       cp_lexer_consume_token (parser->lexer);
16724     }
16725
16726   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16727 }
16728
16729 /* Parse an asm-clobber-list.
16730
16731    asm-clobber-list:
16732      string-literal
16733      asm-clobber-list , string-literal
16734
16735    Returns a TREE_LIST, indicating the clobbers in the order that they
16736    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16737
16738 static tree
16739 cp_parser_asm_clobber_list (cp_parser* parser)
16740 {
16741   tree clobbers = NULL_TREE;
16742
16743   while (true)
16744     {
16745       tree string_literal;
16746
16747       /* Look for the string literal.  */
16748       string_literal = cp_parser_string_literal (parser, false, false);
16749       /* Add it to the list.  */
16750       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16751       /* If the next token is not a `,', then the list is
16752          complete.  */
16753       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16754         break;
16755       /* Consume the `,' token.  */
16756       cp_lexer_consume_token (parser->lexer);
16757     }
16758
16759   return clobbers;
16760 }
16761
16762 /* Parse an (optional) series of attributes.
16763
16764    attributes:
16765      attributes attribute
16766
16767    attribute:
16768      __attribute__ (( attribute-list [opt] ))
16769
16770    The return value is as for cp_parser_attribute_list.  */
16771
16772 static tree
16773 cp_parser_attributes_opt (cp_parser* parser)
16774 {
16775   tree attributes = NULL_TREE;
16776
16777   while (true)
16778     {
16779       cp_token *token;
16780       tree attribute_list;
16781
16782       /* Peek at the next token.  */
16783       token = cp_lexer_peek_token (parser->lexer);
16784       /* If it's not `__attribute__', then we're done.  */
16785       if (token->keyword != RID_ATTRIBUTE)
16786         break;
16787
16788       /* Consume the `__attribute__' keyword.  */
16789       cp_lexer_consume_token (parser->lexer);
16790       /* Look for the two `(' tokens.  */
16791       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16792       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16793
16794       /* Peek at the next token.  */
16795       token = cp_lexer_peek_token (parser->lexer);
16796       if (token->type != CPP_CLOSE_PAREN)
16797         /* Parse the attribute-list.  */
16798         attribute_list = cp_parser_attribute_list (parser);
16799       else
16800         /* If the next token is a `)', then there is no attribute
16801            list.  */
16802         attribute_list = NULL;
16803
16804       /* Look for the two `)' tokens.  */
16805       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16806       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16807
16808       /* Add these new attributes to the list.  */
16809       attributes = chainon (attributes, attribute_list);
16810     }
16811
16812   return attributes;
16813 }
16814
16815 /* Parse an attribute-list.
16816
16817    attribute-list:
16818      attribute
16819      attribute-list , attribute
16820
16821    attribute:
16822      identifier
16823      identifier ( identifier )
16824      identifier ( identifier , expression-list )
16825      identifier ( expression-list )
16826
16827    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16828    to an attribute.  The TREE_PURPOSE of each node is the identifier
16829    indicating which attribute is in use.  The TREE_VALUE represents
16830    the arguments, if any.  */
16831
16832 static tree
16833 cp_parser_attribute_list (cp_parser* parser)
16834 {
16835   tree attribute_list = NULL_TREE;
16836   bool save_translate_strings_p = parser->translate_strings_p;
16837
16838   parser->translate_strings_p = false;
16839   while (true)
16840     {
16841       cp_token *token;
16842       tree identifier;
16843       tree attribute;
16844
16845       /* Look for the identifier.  We also allow keywords here; for
16846          example `__attribute__ ((const))' is legal.  */
16847       token = cp_lexer_peek_token (parser->lexer);
16848       if (token->type == CPP_NAME
16849           || token->type == CPP_KEYWORD)
16850         {
16851           tree arguments = NULL_TREE;
16852
16853           /* Consume the token.  */
16854           token = cp_lexer_consume_token (parser->lexer);
16855
16856           /* Save away the identifier that indicates which attribute
16857              this is.  */
16858           identifier = token->u.value;
16859           attribute = build_tree_list (identifier, NULL_TREE);
16860
16861           /* Peek at the next token.  */
16862           token = cp_lexer_peek_token (parser->lexer);
16863           /* If it's an `(', then parse the attribute arguments.  */
16864           if (token->type == CPP_OPEN_PAREN)
16865             {
16866               arguments = cp_parser_parenthesized_expression_list
16867                           (parser, true, /*cast_p=*/false,
16868                            /*allow_expansion_p=*/false,
16869                            /*non_constant_p=*/NULL);
16870               /* Save the arguments away.  */
16871               TREE_VALUE (attribute) = arguments;
16872             }
16873
16874           if (arguments != error_mark_node)
16875             {
16876               /* Add this attribute to the list.  */
16877               TREE_CHAIN (attribute) = attribute_list;
16878               attribute_list = attribute;
16879             }
16880
16881           token = cp_lexer_peek_token (parser->lexer);
16882         }
16883       /* Now, look for more attributes.  If the next token isn't a
16884          `,', we're done.  */
16885       if (token->type != CPP_COMMA)
16886         break;
16887
16888       /* Consume the comma and keep going.  */
16889       cp_lexer_consume_token (parser->lexer);
16890     }
16891   parser->translate_strings_p = save_translate_strings_p;
16892
16893   /* We built up the list in reverse order.  */
16894   return nreverse (attribute_list);
16895 }
16896
16897 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16898    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16899    current value of the PEDANTIC flag, regardless of whether or not
16900    the `__extension__' keyword is present.  The caller is responsible
16901    for restoring the value of the PEDANTIC flag.  */
16902
16903 static bool
16904 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16905 {
16906   /* Save the old value of the PEDANTIC flag.  */
16907   *saved_pedantic = pedantic;
16908
16909   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16910     {
16911       /* Consume the `__extension__' token.  */
16912       cp_lexer_consume_token (parser->lexer);
16913       /* We're not being pedantic while the `__extension__' keyword is
16914          in effect.  */
16915       pedantic = 0;
16916
16917       return true;
16918     }
16919
16920   return false;
16921 }
16922
16923 /* Parse a label declaration.
16924
16925    label-declaration:
16926      __label__ label-declarator-seq ;
16927
16928    label-declarator-seq:
16929      identifier , label-declarator-seq
16930      identifier  */
16931
16932 static void
16933 cp_parser_label_declaration (cp_parser* parser)
16934 {
16935   /* Look for the `__label__' keyword.  */
16936   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16937
16938   while (true)
16939     {
16940       tree identifier;
16941
16942       /* Look for an identifier.  */
16943       identifier = cp_parser_identifier (parser);
16944       /* If we failed, stop.  */
16945       if (identifier == error_mark_node)
16946         break;
16947       /* Declare it as a label.  */
16948       finish_label_decl (identifier);
16949       /* If the next token is a `;', stop.  */
16950       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16951         break;
16952       /* Look for the `,' separating the label declarations.  */
16953       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16954     }
16955
16956   /* Look for the final `;'.  */
16957   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16958 }
16959
16960 /* Support Functions */
16961
16962 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16963    NAME should have one of the representations used for an
16964    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16965    is returned.  If PARSER->SCOPE is a dependent type, then a
16966    SCOPE_REF is returned.
16967
16968    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16969    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16970    was formed.  Abstractly, such entities should not be passed to this
16971    function, because they do not need to be looked up, but it is
16972    simpler to check for this special case here, rather than at the
16973    call-sites.
16974
16975    In cases not explicitly covered above, this function returns a
16976    DECL, OVERLOAD, or baselink representing the result of the lookup.
16977    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16978    is returned.
16979
16980    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16981    (e.g., "struct") that was used.  In that case bindings that do not
16982    refer to types are ignored.
16983
16984    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16985    ignored.
16986
16987    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16988    are ignored.
16989
16990    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16991    types.
16992
16993    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16994    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16995    NULL_TREE otherwise.  */
16996
16997 static tree
16998 cp_parser_lookup_name (cp_parser *parser, tree name,
16999                        enum tag_types tag_type,
17000                        bool is_template,
17001                        bool is_namespace,
17002                        bool check_dependency,
17003                        tree *ambiguous_decls,
17004                        location_t name_location)
17005 {
17006   int flags = 0;
17007   tree decl;
17008   tree object_type = parser->context->object_type;
17009
17010   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17011     flags |= LOOKUP_COMPLAIN;
17012
17013   /* Assume that the lookup will be unambiguous.  */
17014   if (ambiguous_decls)
17015     *ambiguous_decls = NULL_TREE;
17016
17017   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17018      no longer valid.  Note that if we are parsing tentatively, and
17019      the parse fails, OBJECT_TYPE will be automatically restored.  */
17020   parser->context->object_type = NULL_TREE;
17021
17022   if (name == error_mark_node)
17023     return error_mark_node;
17024
17025   /* A template-id has already been resolved; there is no lookup to
17026      do.  */
17027   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17028     return name;
17029   if (BASELINK_P (name))
17030     {
17031       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17032                   == TEMPLATE_ID_EXPR);
17033       return name;
17034     }
17035
17036   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17037      it should already have been checked to make sure that the name
17038      used matches the type being destroyed.  */
17039   if (TREE_CODE (name) == BIT_NOT_EXPR)
17040     {
17041       tree type;
17042
17043       /* Figure out to which type this destructor applies.  */
17044       if (parser->scope)
17045         type = parser->scope;
17046       else if (object_type)
17047         type = object_type;
17048       else
17049         type = current_class_type;
17050       /* If that's not a class type, there is no destructor.  */
17051       if (!type || !CLASS_TYPE_P (type))
17052         return error_mark_node;
17053       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17054         lazily_declare_fn (sfk_destructor, type);
17055       if (!CLASSTYPE_DESTRUCTORS (type))
17056           return error_mark_node;
17057       /* If it was a class type, return the destructor.  */
17058       return CLASSTYPE_DESTRUCTORS (type);
17059     }
17060
17061   /* By this point, the NAME should be an ordinary identifier.  If
17062      the id-expression was a qualified name, the qualifying scope is
17063      stored in PARSER->SCOPE at this point.  */
17064   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17065
17066   /* Perform the lookup.  */
17067   if (parser->scope)
17068     {
17069       bool dependent_p;
17070
17071       if (parser->scope == error_mark_node)
17072         return error_mark_node;
17073
17074       /* If the SCOPE is dependent, the lookup must be deferred until
17075          the template is instantiated -- unless we are explicitly
17076          looking up names in uninstantiated templates.  Even then, we
17077          cannot look up the name if the scope is not a class type; it
17078          might, for example, be a template type parameter.  */
17079       dependent_p = (TYPE_P (parser->scope)
17080                      && dependent_scope_p (parser->scope));
17081       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17082           && dependent_p)
17083         /* Defer lookup.  */
17084         decl = error_mark_node;
17085       else
17086         {
17087           tree pushed_scope = NULL_TREE;
17088
17089           /* If PARSER->SCOPE is a dependent type, then it must be a
17090              class type, and we must not be checking dependencies;
17091              otherwise, we would have processed this lookup above.  So
17092              that PARSER->SCOPE is not considered a dependent base by
17093              lookup_member, we must enter the scope here.  */
17094           if (dependent_p)
17095             pushed_scope = push_scope (parser->scope);
17096           /* If the PARSER->SCOPE is a template specialization, it
17097              may be instantiated during name lookup.  In that case,
17098              errors may be issued.  Even if we rollback the current
17099              tentative parse, those errors are valid.  */
17100           decl = lookup_qualified_name (parser->scope, name,
17101                                         tag_type != none_type,
17102                                         /*complain=*/true);
17103
17104           /* If we have a single function from a using decl, pull it out.  */
17105           if (TREE_CODE (decl) == OVERLOAD
17106               && !really_overloaded_fn (decl))
17107             decl = OVL_FUNCTION (decl);
17108
17109           if (pushed_scope)
17110             pop_scope (pushed_scope);
17111         }
17112
17113       /* If the scope is a dependent type and either we deferred lookup or
17114          we did lookup but didn't find the name, rememeber the name.  */
17115       if (decl == error_mark_node && TYPE_P (parser->scope)
17116           && dependent_type_p (parser->scope))
17117         {
17118           if (tag_type)
17119             {
17120               tree type;
17121
17122               /* The resolution to Core Issue 180 says that `struct
17123                  A::B' should be considered a type-name, even if `A'
17124                  is dependent.  */
17125               type = make_typename_type (parser->scope, name, tag_type,
17126                                          /*complain=*/tf_error);
17127               decl = TYPE_NAME (type);
17128             }
17129           else if (is_template
17130                    && (cp_parser_next_token_ends_template_argument_p (parser)
17131                        || cp_lexer_next_token_is (parser->lexer,
17132                                                   CPP_CLOSE_PAREN)))
17133             decl = make_unbound_class_template (parser->scope,
17134                                                 name, NULL_TREE,
17135                                                 /*complain=*/tf_error);
17136           else
17137             decl = build_qualified_name (/*type=*/NULL_TREE,
17138                                          parser->scope, name,
17139                                          is_template);
17140         }
17141       parser->qualifying_scope = parser->scope;
17142       parser->object_scope = NULL_TREE;
17143     }
17144   else if (object_type)
17145     {
17146       tree object_decl = NULL_TREE;
17147       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17148          OBJECT_TYPE is not a class.  */
17149       if (CLASS_TYPE_P (object_type))
17150         /* If the OBJECT_TYPE is a template specialization, it may
17151            be instantiated during name lookup.  In that case, errors
17152            may be issued.  Even if we rollback the current tentative
17153            parse, those errors are valid.  */
17154         object_decl = lookup_member (object_type,
17155                                      name,
17156                                      /*protect=*/0,
17157                                      tag_type != none_type);
17158       /* Look it up in the enclosing context, too.  */
17159       decl = lookup_name_real (name, tag_type != none_type,
17160                                /*nonclass=*/0,
17161                                /*block_p=*/true, is_namespace, flags);
17162       parser->object_scope = object_type;
17163       parser->qualifying_scope = NULL_TREE;
17164       if (object_decl)
17165         decl = object_decl;
17166     }
17167   else
17168     {
17169       decl = lookup_name_real (name, tag_type != none_type,
17170                                /*nonclass=*/0,
17171                                /*block_p=*/true, is_namespace, flags);
17172       parser->qualifying_scope = NULL_TREE;
17173       parser->object_scope = NULL_TREE;
17174     }
17175
17176   /* If the lookup failed, let our caller know.  */
17177   if (!decl || decl == error_mark_node)
17178     return error_mark_node;
17179
17180   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17181   if (TREE_CODE (decl) == TREE_LIST)
17182     {
17183       if (ambiguous_decls)
17184         *ambiguous_decls = decl;
17185       /* The error message we have to print is too complicated for
17186          cp_parser_error, so we incorporate its actions directly.  */
17187       if (!cp_parser_simulate_error (parser))
17188         {
17189           error ("%Hreference to %qD is ambiguous",
17190                  &name_location, name);
17191           print_candidates (decl);
17192         }
17193       return error_mark_node;
17194     }
17195
17196   gcc_assert (DECL_P (decl)
17197               || TREE_CODE (decl) == OVERLOAD
17198               || TREE_CODE (decl) == SCOPE_REF
17199               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17200               || BASELINK_P (decl));
17201
17202   /* If we have resolved the name of a member declaration, check to
17203      see if the declaration is accessible.  When the name resolves to
17204      set of overloaded functions, accessibility is checked when
17205      overload resolution is done.
17206
17207      During an explicit instantiation, access is not checked at all,
17208      as per [temp.explicit].  */
17209   if (DECL_P (decl))
17210     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17211
17212   return decl;
17213 }
17214
17215 /* Like cp_parser_lookup_name, but for use in the typical case where
17216    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17217    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17218
17219 static tree
17220 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17221 {
17222   return cp_parser_lookup_name (parser, name,
17223                                 none_type,
17224                                 /*is_template=*/false,
17225                                 /*is_namespace=*/false,
17226                                 /*check_dependency=*/true,
17227                                 /*ambiguous_decls=*/NULL,
17228                                 location);
17229 }
17230
17231 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17232    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17233    true, the DECL indicates the class being defined in a class-head,
17234    or declared in an elaborated-type-specifier.
17235
17236    Otherwise, return DECL.  */
17237
17238 static tree
17239 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17240 {
17241   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17242      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17243
17244        struct A {
17245          template <typename T> struct B;
17246        };
17247
17248        template <typename T> struct A::B {};
17249
17250      Similarly, in an elaborated-type-specifier:
17251
17252        namespace N { struct X{}; }
17253
17254        struct A {
17255          template <typename T> friend struct N::X;
17256        };
17257
17258      However, if the DECL refers to a class type, and we are in
17259      the scope of the class, then the name lookup automatically
17260      finds the TYPE_DECL created by build_self_reference rather
17261      than a TEMPLATE_DECL.  For example, in:
17262
17263        template <class T> struct S {
17264          S s;
17265        };
17266
17267      there is no need to handle such case.  */
17268
17269   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17270     return DECL_TEMPLATE_RESULT (decl);
17271
17272   return decl;
17273 }
17274
17275 /* If too many, or too few, template-parameter lists apply to the
17276    declarator, issue an error message.  Returns TRUE if all went well,
17277    and FALSE otherwise.  */
17278
17279 static bool
17280 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17281                                                 cp_declarator *declarator,
17282                                                 location_t declarator_location)
17283 {
17284   unsigned num_templates;
17285
17286   /* We haven't seen any classes that involve template parameters yet.  */
17287   num_templates = 0;
17288
17289   switch (declarator->kind)
17290     {
17291     case cdk_id:
17292       if (declarator->u.id.qualifying_scope)
17293         {
17294           tree scope;
17295           tree member;
17296
17297           scope = declarator->u.id.qualifying_scope;
17298           member = declarator->u.id.unqualified_name;
17299
17300           while (scope && CLASS_TYPE_P (scope))
17301             {
17302               /* You're supposed to have one `template <...>'
17303                  for every template class, but you don't need one
17304                  for a full specialization.  For example:
17305
17306                  template <class T> struct S{};
17307                  template <> struct S<int> { void f(); };
17308                  void S<int>::f () {}
17309
17310                  is correct; there shouldn't be a `template <>' for
17311                  the definition of `S<int>::f'.  */
17312               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17313                 /* If SCOPE does not have template information of any
17314                    kind, then it is not a template, nor is it nested
17315                    within a template.  */
17316                 break;
17317               if (explicit_class_specialization_p (scope))
17318                 break;
17319               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17320                 ++num_templates;
17321
17322               scope = TYPE_CONTEXT (scope);
17323             }
17324         }
17325       else if (TREE_CODE (declarator->u.id.unqualified_name)
17326                == TEMPLATE_ID_EXPR)
17327         /* If the DECLARATOR has the form `X<y>' then it uses one
17328            additional level of template parameters.  */
17329         ++num_templates;
17330
17331       return cp_parser_check_template_parameters (parser,
17332                                                   num_templates,
17333                                                   declarator_location);
17334
17335     case cdk_function:
17336     case cdk_array:
17337     case cdk_pointer:
17338     case cdk_reference:
17339     case cdk_ptrmem:
17340       return (cp_parser_check_declarator_template_parameters
17341               (parser, declarator->declarator, declarator_location));
17342
17343     case cdk_error:
17344       return true;
17345
17346     default:
17347       gcc_unreachable ();
17348     }
17349   return false;
17350 }
17351
17352 /* NUM_TEMPLATES were used in the current declaration.  If that is
17353    invalid, return FALSE and issue an error messages.  Otherwise,
17354    return TRUE.  */
17355
17356 static bool
17357 cp_parser_check_template_parameters (cp_parser* parser,
17358                                      unsigned num_templates,
17359                                      location_t location)
17360 {
17361   /* If there are more template classes than parameter lists, we have
17362      something like:
17363
17364        template <class T> void S<T>::R<T>::f ();  */
17365   if (parser->num_template_parameter_lists < num_templates)
17366     {
17367       error ("%Htoo few template-parameter-lists", &location);
17368       return false;
17369     }
17370   /* If there are the same number of template classes and parameter
17371      lists, that's OK.  */
17372   if (parser->num_template_parameter_lists == num_templates)
17373     return true;
17374   /* If there are more, but only one more, then we are referring to a
17375      member template.  That's OK too.  */
17376   if (parser->num_template_parameter_lists == num_templates + 1)
17377       return true;
17378   /* Otherwise, there are too many template parameter lists.  We have
17379      something like:
17380
17381      template <class T> template <class U> void S::f();  */
17382   error ("%Htoo many template-parameter-lists", &location);
17383   return false;
17384 }
17385
17386 /* Parse an optional `::' token indicating that the following name is
17387    from the global namespace.  If so, PARSER->SCOPE is set to the
17388    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17389    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17390    Returns the new value of PARSER->SCOPE, if the `::' token is
17391    present, and NULL_TREE otherwise.  */
17392
17393 static tree
17394 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17395 {
17396   cp_token *token;
17397
17398   /* Peek at the next token.  */
17399   token = cp_lexer_peek_token (parser->lexer);
17400   /* If we're looking at a `::' token then we're starting from the
17401      global namespace, not our current location.  */
17402   if (token->type == CPP_SCOPE)
17403     {
17404       /* Consume the `::' token.  */
17405       cp_lexer_consume_token (parser->lexer);
17406       /* Set the SCOPE so that we know where to start the lookup.  */
17407       parser->scope = global_namespace;
17408       parser->qualifying_scope = global_namespace;
17409       parser->object_scope = NULL_TREE;
17410
17411       return parser->scope;
17412     }
17413   else if (!current_scope_valid_p)
17414     {
17415       parser->scope = NULL_TREE;
17416       parser->qualifying_scope = NULL_TREE;
17417       parser->object_scope = NULL_TREE;
17418     }
17419
17420   return NULL_TREE;
17421 }
17422
17423 /* Returns TRUE if the upcoming token sequence is the start of a
17424    constructor declarator.  If FRIEND_P is true, the declarator is
17425    preceded by the `friend' specifier.  */
17426
17427 static bool
17428 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17429 {
17430   bool constructor_p;
17431   tree type_decl = NULL_TREE;
17432   bool nested_name_p;
17433   cp_token *next_token;
17434
17435   /* The common case is that this is not a constructor declarator, so
17436      try to avoid doing lots of work if at all possible.  It's not
17437      valid declare a constructor at function scope.  */
17438   if (parser->in_function_body)
17439     return false;
17440   /* And only certain tokens can begin a constructor declarator.  */
17441   next_token = cp_lexer_peek_token (parser->lexer);
17442   if (next_token->type != CPP_NAME
17443       && next_token->type != CPP_SCOPE
17444       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17445       && next_token->type != CPP_TEMPLATE_ID)
17446     return false;
17447
17448   /* Parse tentatively; we are going to roll back all of the tokens
17449      consumed here.  */
17450   cp_parser_parse_tentatively (parser);
17451   /* Assume that we are looking at a constructor declarator.  */
17452   constructor_p = true;
17453
17454   /* Look for the optional `::' operator.  */
17455   cp_parser_global_scope_opt (parser,
17456                               /*current_scope_valid_p=*/false);
17457   /* Look for the nested-name-specifier.  */
17458   nested_name_p
17459     = (cp_parser_nested_name_specifier_opt (parser,
17460                                             /*typename_keyword_p=*/false,
17461                                             /*check_dependency_p=*/false,
17462                                             /*type_p=*/false,
17463                                             /*is_declaration=*/false)
17464        != NULL_TREE);
17465   /* Outside of a class-specifier, there must be a
17466      nested-name-specifier.  */
17467   if (!nested_name_p &&
17468       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17469        || friend_p))
17470     constructor_p = false;
17471   /* If we still think that this might be a constructor-declarator,
17472      look for a class-name.  */
17473   if (constructor_p)
17474     {
17475       /* If we have:
17476
17477            template <typename T> struct S { S(); };
17478            template <typename T> S<T>::S ();
17479
17480          we must recognize that the nested `S' names a class.
17481          Similarly, for:
17482
17483            template <typename T> S<T>::S<T> ();
17484
17485          we must recognize that the nested `S' names a template.  */
17486       type_decl = cp_parser_class_name (parser,
17487                                         /*typename_keyword_p=*/false,
17488                                         /*template_keyword_p=*/false,
17489                                         none_type,
17490                                         /*check_dependency_p=*/false,
17491                                         /*class_head_p=*/false,
17492                                         /*is_declaration=*/false);
17493       /* If there was no class-name, then this is not a constructor.  */
17494       constructor_p = !cp_parser_error_occurred (parser);
17495     }
17496
17497   /* If we're still considering a constructor, we have to see a `(',
17498      to begin the parameter-declaration-clause, followed by either a
17499      `)', an `...', or a decl-specifier.  We need to check for a
17500      type-specifier to avoid being fooled into thinking that:
17501
17502        S::S (f) (int);
17503
17504      is a constructor.  (It is actually a function named `f' that
17505      takes one parameter (of type `int') and returns a value of type
17506      `S::S'.  */
17507   if (constructor_p
17508       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17509     {
17510       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17511           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17512           /* A parameter declaration begins with a decl-specifier,
17513              which is either the "attribute" keyword, a storage class
17514              specifier, or (usually) a type-specifier.  */
17515           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17516         {
17517           tree type;
17518           tree pushed_scope = NULL_TREE;
17519           unsigned saved_num_template_parameter_lists;
17520
17521           /* Names appearing in the type-specifier should be looked up
17522              in the scope of the class.  */
17523           if (current_class_type)
17524             type = NULL_TREE;
17525           else
17526             {
17527               type = TREE_TYPE (type_decl);
17528               if (TREE_CODE (type) == TYPENAME_TYPE)
17529                 {
17530                   type = resolve_typename_type (type,
17531                                                 /*only_current_p=*/false);
17532                   if (TREE_CODE (type) == TYPENAME_TYPE)
17533                     {
17534                       cp_parser_abort_tentative_parse (parser);
17535                       return false;
17536                     }
17537                 }
17538               pushed_scope = push_scope (type);
17539             }
17540
17541           /* Inside the constructor parameter list, surrounding
17542              template-parameter-lists do not apply.  */
17543           saved_num_template_parameter_lists
17544             = parser->num_template_parameter_lists;
17545           parser->num_template_parameter_lists = 0;
17546
17547           /* Look for the type-specifier.  */
17548           cp_parser_type_specifier (parser,
17549                                     CP_PARSER_FLAGS_NONE,
17550                                     /*decl_specs=*/NULL,
17551                                     /*is_declarator=*/true,
17552                                     /*declares_class_or_enum=*/NULL,
17553                                     /*is_cv_qualifier=*/NULL);
17554
17555           parser->num_template_parameter_lists
17556             = saved_num_template_parameter_lists;
17557
17558           /* Leave the scope of the class.  */
17559           if (pushed_scope)
17560             pop_scope (pushed_scope);
17561
17562           constructor_p = !cp_parser_error_occurred (parser);
17563         }
17564     }
17565   else
17566     constructor_p = false;
17567   /* We did not really want to consume any tokens.  */
17568   cp_parser_abort_tentative_parse (parser);
17569
17570   return constructor_p;
17571 }
17572
17573 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17574    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17575    they must be performed once we are in the scope of the function.
17576
17577    Returns the function defined.  */
17578
17579 static tree
17580 cp_parser_function_definition_from_specifiers_and_declarator
17581   (cp_parser* parser,
17582    cp_decl_specifier_seq *decl_specifiers,
17583    tree attributes,
17584    const cp_declarator *declarator)
17585 {
17586   tree fn;
17587   bool success_p;
17588
17589   /* Begin the function-definition.  */
17590   success_p = start_function (decl_specifiers, declarator, attributes);
17591
17592   /* The things we're about to see are not directly qualified by any
17593      template headers we've seen thus far.  */
17594   reset_specialization ();
17595
17596   /* If there were names looked up in the decl-specifier-seq that we
17597      did not check, check them now.  We must wait until we are in the
17598      scope of the function to perform the checks, since the function
17599      might be a friend.  */
17600   perform_deferred_access_checks ();
17601
17602   if (!success_p)
17603     {
17604       /* Skip the entire function.  */
17605       cp_parser_skip_to_end_of_block_or_statement (parser);
17606       fn = error_mark_node;
17607     }
17608   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17609     {
17610       /* Seen already, skip it.  An error message has already been output.  */
17611       cp_parser_skip_to_end_of_block_or_statement (parser);
17612       fn = current_function_decl;
17613       current_function_decl = NULL_TREE;
17614       /* If this is a function from a class, pop the nested class.  */
17615       if (current_class_name)
17616         pop_nested_class ();
17617     }
17618   else
17619     fn = cp_parser_function_definition_after_declarator (parser,
17620                                                          /*inline_p=*/false);
17621
17622   return fn;
17623 }
17624
17625 /* Parse the part of a function-definition that follows the
17626    declarator.  INLINE_P is TRUE iff this function is an inline
17627    function defined with a class-specifier.
17628
17629    Returns the function defined.  */
17630
17631 static tree
17632 cp_parser_function_definition_after_declarator (cp_parser* parser,
17633                                                 bool inline_p)
17634 {
17635   tree fn;
17636   bool ctor_initializer_p = false;
17637   bool saved_in_unbraced_linkage_specification_p;
17638   bool saved_in_function_body;
17639   unsigned saved_num_template_parameter_lists;
17640   cp_token *token;
17641
17642   saved_in_function_body = parser->in_function_body;
17643   parser->in_function_body = true;
17644   /* If the next token is `return', then the code may be trying to
17645      make use of the "named return value" extension that G++ used to
17646      support.  */
17647   token = cp_lexer_peek_token (parser->lexer);
17648   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17649     {
17650       /* Consume the `return' keyword.  */
17651       cp_lexer_consume_token (parser->lexer);
17652       /* Look for the identifier that indicates what value is to be
17653          returned.  */
17654       cp_parser_identifier (parser);
17655       /* Issue an error message.  */
17656       error ("%Hnamed return values are no longer supported",
17657              &token->location);
17658       /* Skip tokens until we reach the start of the function body.  */
17659       while (true)
17660         {
17661           cp_token *token = cp_lexer_peek_token (parser->lexer);
17662           if (token->type == CPP_OPEN_BRACE
17663               || token->type == CPP_EOF
17664               || token->type == CPP_PRAGMA_EOL)
17665             break;
17666           cp_lexer_consume_token (parser->lexer);
17667         }
17668     }
17669   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17670      anything declared inside `f'.  */
17671   saved_in_unbraced_linkage_specification_p
17672     = parser->in_unbraced_linkage_specification_p;
17673   parser->in_unbraced_linkage_specification_p = false;
17674   /* Inside the function, surrounding template-parameter-lists do not
17675      apply.  */
17676   saved_num_template_parameter_lists
17677     = parser->num_template_parameter_lists;
17678   parser->num_template_parameter_lists = 0;
17679   /* If the next token is `try', then we are looking at a
17680      function-try-block.  */
17681   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17682     ctor_initializer_p = cp_parser_function_try_block (parser);
17683   /* A function-try-block includes the function-body, so we only do
17684      this next part if we're not processing a function-try-block.  */
17685   else
17686     ctor_initializer_p
17687       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17688
17689   /* Finish the function.  */
17690   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17691                         (inline_p ? 2 : 0));
17692   /* Generate code for it, if necessary.  */
17693   expand_or_defer_fn (fn);
17694   /* Restore the saved values.  */
17695   parser->in_unbraced_linkage_specification_p
17696     = saved_in_unbraced_linkage_specification_p;
17697   parser->num_template_parameter_lists
17698     = saved_num_template_parameter_lists;
17699   parser->in_function_body = saved_in_function_body;
17700
17701   return fn;
17702 }
17703
17704 /* Parse a template-declaration, assuming that the `export' (and
17705    `extern') keywords, if present, has already been scanned.  MEMBER_P
17706    is as for cp_parser_template_declaration.  */
17707
17708 static void
17709 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17710 {
17711   tree decl = NULL_TREE;
17712   VEC (deferred_access_check,gc) *checks;
17713   tree parameter_list;
17714   bool friend_p = false;
17715   bool need_lang_pop;
17716   cp_token *token;
17717
17718   /* Look for the `template' keyword.  */
17719   token = cp_lexer_peek_token (parser->lexer);
17720   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17721     return;
17722
17723   /* And the `<'.  */
17724   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17725     return;
17726   if (at_class_scope_p () && current_function_decl)
17727     {
17728       /* 14.5.2.2 [temp.mem]
17729
17730          A local class shall not have member templates.  */
17731       error ("%Hinvalid declaration of member template in local class",
17732              &token->location);
17733       cp_parser_skip_to_end_of_block_or_statement (parser);
17734       return;
17735     }
17736   /* [temp]
17737
17738      A template ... shall not have C linkage.  */
17739   if (current_lang_name == lang_name_c)
17740     {
17741       error ("%Htemplate with C linkage", &token->location);
17742       /* Give it C++ linkage to avoid confusing other parts of the
17743          front end.  */
17744       push_lang_context (lang_name_cplusplus);
17745       need_lang_pop = true;
17746     }
17747   else
17748     need_lang_pop = false;
17749
17750   /* We cannot perform access checks on the template parameter
17751      declarations until we know what is being declared, just as we
17752      cannot check the decl-specifier list.  */
17753   push_deferring_access_checks (dk_deferred);
17754
17755   /* If the next token is `>', then we have an invalid
17756      specialization.  Rather than complain about an invalid template
17757      parameter, issue an error message here.  */
17758   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17759     {
17760       cp_parser_error (parser, "invalid explicit specialization");
17761       begin_specialization ();
17762       parameter_list = NULL_TREE;
17763     }
17764   else
17765     /* Parse the template parameters.  */
17766     parameter_list = cp_parser_template_parameter_list (parser);
17767
17768   /* Get the deferred access checks from the parameter list.  These
17769      will be checked once we know what is being declared, as for a
17770      member template the checks must be performed in the scope of the
17771      class containing the member.  */
17772   checks = get_deferred_access_checks ();
17773
17774   /* Look for the `>'.  */
17775   cp_parser_skip_to_end_of_template_parameter_list (parser);
17776   /* We just processed one more parameter list.  */
17777   ++parser->num_template_parameter_lists;
17778   /* If the next token is `template', there are more template
17779      parameters.  */
17780   if (cp_lexer_next_token_is_keyword (parser->lexer,
17781                                       RID_TEMPLATE))
17782     cp_parser_template_declaration_after_export (parser, member_p);
17783   else
17784     {
17785       /* There are no access checks when parsing a template, as we do not
17786          know if a specialization will be a friend.  */
17787       push_deferring_access_checks (dk_no_check);
17788       token = cp_lexer_peek_token (parser->lexer);
17789       decl = cp_parser_single_declaration (parser,
17790                                            checks,
17791                                            member_p,
17792                                            /*explicit_specialization_p=*/false,
17793                                            &friend_p);
17794       pop_deferring_access_checks ();
17795
17796       /* If this is a member template declaration, let the front
17797          end know.  */
17798       if (member_p && !friend_p && decl)
17799         {
17800           if (TREE_CODE (decl) == TYPE_DECL)
17801             cp_parser_check_access_in_redeclaration (decl, token->location);
17802
17803           decl = finish_member_template_decl (decl);
17804         }
17805       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17806         make_friend_class (current_class_type, TREE_TYPE (decl),
17807                            /*complain=*/true);
17808     }
17809   /* We are done with the current parameter list.  */
17810   --parser->num_template_parameter_lists;
17811
17812   pop_deferring_access_checks ();
17813
17814   /* Finish up.  */
17815   finish_template_decl (parameter_list);
17816
17817   /* Register member declarations.  */
17818   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17819     finish_member_declaration (decl);
17820   /* For the erroneous case of a template with C linkage, we pushed an
17821      implicit C++ linkage scope; exit that scope now.  */
17822   if (need_lang_pop)
17823     pop_lang_context ();
17824   /* If DECL is a function template, we must return to parse it later.
17825      (Even though there is no definition, there might be default
17826      arguments that need handling.)  */
17827   if (member_p && decl
17828       && (TREE_CODE (decl) == FUNCTION_DECL
17829           || DECL_FUNCTION_TEMPLATE_P (decl)))
17830     TREE_VALUE (parser->unparsed_functions_queues)
17831       = tree_cons (NULL_TREE, decl,
17832                    TREE_VALUE (parser->unparsed_functions_queues));
17833 }
17834
17835 /* Perform the deferred access checks from a template-parameter-list.
17836    CHECKS is a TREE_LIST of access checks, as returned by
17837    get_deferred_access_checks.  */
17838
17839 static void
17840 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17841 {
17842   ++processing_template_parmlist;
17843   perform_access_checks (checks);
17844   --processing_template_parmlist;
17845 }
17846
17847 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17848    `function-definition' sequence.  MEMBER_P is true, this declaration
17849    appears in a class scope.
17850
17851    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17852    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17853
17854 static tree
17855 cp_parser_single_declaration (cp_parser* parser,
17856                               VEC (deferred_access_check,gc)* checks,
17857                               bool member_p,
17858                               bool explicit_specialization_p,
17859                               bool* friend_p)
17860 {
17861   int declares_class_or_enum;
17862   tree decl = NULL_TREE;
17863   cp_decl_specifier_seq decl_specifiers;
17864   bool function_definition_p = false;
17865   cp_token *decl_spec_token_start;
17866
17867   /* This function is only used when processing a template
17868      declaration.  */
17869   gcc_assert (innermost_scope_kind () == sk_template_parms
17870               || innermost_scope_kind () == sk_template_spec);
17871
17872   /* Defer access checks until we know what is being declared.  */
17873   push_deferring_access_checks (dk_deferred);
17874
17875   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17876      alternative.  */
17877   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17878   cp_parser_decl_specifier_seq (parser,
17879                                 CP_PARSER_FLAGS_OPTIONAL,
17880                                 &decl_specifiers,
17881                                 &declares_class_or_enum);
17882   if (friend_p)
17883     *friend_p = cp_parser_friend_p (&decl_specifiers);
17884
17885   /* There are no template typedefs.  */
17886   if (decl_specifiers.specs[(int) ds_typedef])
17887     {
17888       error ("%Htemplate declaration of %qs",
17889              &decl_spec_token_start->location, "typedef");
17890       decl = error_mark_node;
17891     }
17892
17893   /* Gather up the access checks that occurred the
17894      decl-specifier-seq.  */
17895   stop_deferring_access_checks ();
17896
17897   /* Check for the declaration of a template class.  */
17898   if (declares_class_or_enum)
17899     {
17900       if (cp_parser_declares_only_class_p (parser))
17901         {
17902           decl = shadow_tag (&decl_specifiers);
17903
17904           /* In this case:
17905
17906                struct C {
17907                  friend template <typename T> struct A<T>::B;
17908                };
17909
17910              A<T>::B will be represented by a TYPENAME_TYPE, and
17911              therefore not recognized by shadow_tag.  */
17912           if (friend_p && *friend_p
17913               && !decl
17914               && decl_specifiers.type
17915               && TYPE_P (decl_specifiers.type))
17916             decl = decl_specifiers.type;
17917
17918           if (decl && decl != error_mark_node)
17919             decl = TYPE_NAME (decl);
17920           else
17921             decl = error_mark_node;
17922
17923           /* Perform access checks for template parameters.  */
17924           cp_parser_perform_template_parameter_access_checks (checks);
17925         }
17926     }
17927   /* If it's not a template class, try for a template function.  If
17928      the next token is a `;', then this declaration does not declare
17929      anything.  But, if there were errors in the decl-specifiers, then
17930      the error might well have come from an attempted class-specifier.
17931      In that case, there's no need to warn about a missing declarator.  */
17932   if (!decl
17933       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17934           || decl_specifiers.type != error_mark_node))
17935     {
17936       decl = cp_parser_init_declarator (parser,
17937                                         &decl_specifiers,
17938                                         checks,
17939                                         /*function_definition_allowed_p=*/true,
17940                                         member_p,
17941                                         declares_class_or_enum,
17942                                         &function_definition_p);
17943
17944     /* 7.1.1-1 [dcl.stc]
17945
17946        A storage-class-specifier shall not be specified in an explicit
17947        specialization...  */
17948     if (decl
17949         && explicit_specialization_p
17950         && decl_specifiers.storage_class != sc_none)
17951       {
17952         error ("%Hexplicit template specialization cannot have a storage class",
17953                &decl_spec_token_start->location);
17954         decl = error_mark_node;
17955       }
17956     }
17957
17958   pop_deferring_access_checks ();
17959
17960   /* Clear any current qualification; whatever comes next is the start
17961      of something new.  */
17962   parser->scope = NULL_TREE;
17963   parser->qualifying_scope = NULL_TREE;
17964   parser->object_scope = NULL_TREE;
17965   /* Look for a trailing `;' after the declaration.  */
17966   if (!function_definition_p
17967       && (decl == error_mark_node
17968           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17969     cp_parser_skip_to_end_of_block_or_statement (parser);
17970
17971   return decl;
17972 }
17973
17974 /* Parse a cast-expression that is not the operand of a unary "&".  */
17975
17976 static tree
17977 cp_parser_simple_cast_expression (cp_parser *parser)
17978 {
17979   return cp_parser_cast_expression (parser, /*address_p=*/false,
17980                                     /*cast_p=*/false, NULL);
17981 }
17982
17983 /* Parse a functional cast to TYPE.  Returns an expression
17984    representing the cast.  */
17985
17986 static tree
17987 cp_parser_functional_cast (cp_parser* parser, tree type)
17988 {
17989   tree expression_list;
17990   tree cast;
17991   bool nonconst_p;
17992
17993   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17994     {
17995       maybe_warn_cpp0x ("extended initializer lists");
17996       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17997       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17998       if (TREE_CODE (type) == TYPE_DECL)
17999         type = TREE_TYPE (type);
18000       return finish_compound_literal (type, expression_list);
18001     }
18002
18003   expression_list
18004     = cp_parser_parenthesized_expression_list (parser, false,
18005                                                /*cast_p=*/true,
18006                                                /*allow_expansion_p=*/true,
18007                                                /*non_constant_p=*/NULL);
18008
18009   cast = build_functional_cast (type, expression_list,
18010                                 tf_warning_or_error);
18011   /* [expr.const]/1: In an integral constant expression "only type
18012      conversions to integral or enumeration type can be used".  */
18013   if (TREE_CODE (type) == TYPE_DECL)
18014     type = TREE_TYPE (type);
18015   if (cast != error_mark_node
18016       && !cast_valid_in_integral_constant_expression_p (type)
18017       && (cp_parser_non_integral_constant_expression
18018           (parser, "a call to a constructor")))
18019     return error_mark_node;
18020   return cast;
18021 }
18022
18023 /* Save the tokens that make up the body of a member function defined
18024    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18025    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18026    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18027    for the member function.  */
18028
18029 static tree
18030 cp_parser_save_member_function_body (cp_parser* parser,
18031                                      cp_decl_specifier_seq *decl_specifiers,
18032                                      cp_declarator *declarator,
18033                                      tree attributes)
18034 {
18035   cp_token *first;
18036   cp_token *last;
18037   tree fn;
18038
18039   /* Create the function-declaration.  */
18040   fn = start_method (decl_specifiers, declarator, attributes);
18041   /* If something went badly wrong, bail out now.  */
18042   if (fn == error_mark_node)
18043     {
18044       /* If there's a function-body, skip it.  */
18045       if (cp_parser_token_starts_function_definition_p
18046           (cp_lexer_peek_token (parser->lexer)))
18047         cp_parser_skip_to_end_of_block_or_statement (parser);
18048       return error_mark_node;
18049     }
18050
18051   /* Remember it, if there default args to post process.  */
18052   cp_parser_save_default_args (parser, fn);
18053
18054   /* Save away the tokens that make up the body of the
18055      function.  */
18056   first = parser->lexer->next_token;
18057   /* We can have braced-init-list mem-initializers before the fn body.  */
18058   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18059     {
18060       cp_lexer_consume_token (parser->lexer);
18061       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18062              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18063         {
18064           /* cache_group will stop after an un-nested { } pair, too.  */
18065           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18066             break;
18067
18068           /* variadic mem-inits have ... after the ')'.  */
18069           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18070             cp_lexer_consume_token (parser->lexer);
18071         }
18072     }
18073   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18074   /* Handle function try blocks.  */
18075   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18076     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18077   last = parser->lexer->next_token;
18078
18079   /* Save away the inline definition; we will process it when the
18080      class is complete.  */
18081   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18082   DECL_PENDING_INLINE_P (fn) = 1;
18083
18084   /* We need to know that this was defined in the class, so that
18085      friend templates are handled correctly.  */
18086   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18087
18088   /* We're done with the inline definition.  */
18089   finish_method (fn);
18090
18091   /* Add FN to the queue of functions to be parsed later.  */
18092   TREE_VALUE (parser->unparsed_functions_queues)
18093     = tree_cons (NULL_TREE, fn,
18094                  TREE_VALUE (parser->unparsed_functions_queues));
18095
18096   return fn;
18097 }
18098
18099 /* Parse a template-argument-list, as well as the trailing ">" (but
18100    not the opening ">").  See cp_parser_template_argument_list for the
18101    return value.  */
18102
18103 static tree
18104 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18105 {
18106   tree arguments;
18107   tree saved_scope;
18108   tree saved_qualifying_scope;
18109   tree saved_object_scope;
18110   bool saved_greater_than_is_operator_p;
18111   bool saved_skip_evaluation;
18112
18113   /* [temp.names]
18114
18115      When parsing a template-id, the first non-nested `>' is taken as
18116      the end of the template-argument-list rather than a greater-than
18117      operator.  */
18118   saved_greater_than_is_operator_p
18119     = parser->greater_than_is_operator_p;
18120   parser->greater_than_is_operator_p = false;
18121   /* Parsing the argument list may modify SCOPE, so we save it
18122      here.  */
18123   saved_scope = parser->scope;
18124   saved_qualifying_scope = parser->qualifying_scope;
18125   saved_object_scope = parser->object_scope;
18126   /* We need to evaluate the template arguments, even though this
18127      template-id may be nested within a "sizeof".  */
18128   saved_skip_evaluation = skip_evaluation;
18129   skip_evaluation = false;
18130   /* Parse the template-argument-list itself.  */
18131   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18132       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18133     arguments = NULL_TREE;
18134   else
18135     arguments = cp_parser_template_argument_list (parser);
18136   /* Look for the `>' that ends the template-argument-list. If we find
18137      a '>>' instead, it's probably just a typo.  */
18138   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18139     {
18140       if (cxx_dialect != cxx98)
18141         {
18142           /* In C++0x, a `>>' in a template argument list or cast
18143              expression is considered to be two separate `>'
18144              tokens. So, change the current token to a `>', but don't
18145              consume it: it will be consumed later when the outer
18146              template argument list (or cast expression) is parsed.
18147              Note that this replacement of `>' for `>>' is necessary
18148              even if we are parsing tentatively: in the tentative
18149              case, after calling
18150              cp_parser_enclosed_template_argument_list we will always
18151              throw away all of the template arguments and the first
18152              closing `>', either because the template argument list
18153              was erroneous or because we are replacing those tokens
18154              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18155              not have been thrown away) is needed either to close an
18156              outer template argument list or to complete a new-style
18157              cast.  */
18158           cp_token *token = cp_lexer_peek_token (parser->lexer);
18159           token->type = CPP_GREATER;
18160         }
18161       else if (!saved_greater_than_is_operator_p)
18162         {
18163           /* If we're in a nested template argument list, the '>>' has
18164             to be a typo for '> >'. We emit the error message, but we
18165             continue parsing and we push a '>' as next token, so that
18166             the argument list will be parsed correctly.  Note that the
18167             global source location is still on the token before the
18168             '>>', so we need to say explicitly where we want it.  */
18169           cp_token *token = cp_lexer_peek_token (parser->lexer);
18170           error ("%H%<>>%> should be %<> >%> "
18171                  "within a nested template argument list",
18172                  &token->location);
18173
18174           token->type = CPP_GREATER;
18175         }
18176       else
18177         {
18178           /* If this is not a nested template argument list, the '>>'
18179             is a typo for '>'. Emit an error message and continue.
18180             Same deal about the token location, but here we can get it
18181             right by consuming the '>>' before issuing the diagnostic.  */
18182           cp_token *token = cp_lexer_consume_token (parser->lexer);
18183           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18184                  "a template argument list", &token->location);
18185         }
18186     }
18187   else
18188     cp_parser_skip_to_end_of_template_parameter_list (parser);
18189   /* The `>' token might be a greater-than operator again now.  */
18190   parser->greater_than_is_operator_p
18191     = saved_greater_than_is_operator_p;
18192   /* Restore the SAVED_SCOPE.  */
18193   parser->scope = saved_scope;
18194   parser->qualifying_scope = saved_qualifying_scope;
18195   parser->object_scope = saved_object_scope;
18196   skip_evaluation = saved_skip_evaluation;
18197
18198   return arguments;
18199 }
18200
18201 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18202    arguments, or the body of the function have not yet been parsed,
18203    parse them now.  */
18204
18205 static void
18206 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18207 {
18208   /* If this member is a template, get the underlying
18209      FUNCTION_DECL.  */
18210   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18211     member_function = DECL_TEMPLATE_RESULT (member_function);
18212
18213   /* There should not be any class definitions in progress at this
18214      point; the bodies of members are only parsed outside of all class
18215      definitions.  */
18216   gcc_assert (parser->num_classes_being_defined == 0);
18217   /* While we're parsing the member functions we might encounter more
18218      classes.  We want to handle them right away, but we don't want
18219      them getting mixed up with functions that are currently in the
18220      queue.  */
18221   parser->unparsed_functions_queues
18222     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18223
18224   /* Make sure that any template parameters are in scope.  */
18225   maybe_begin_member_template_processing (member_function);
18226
18227   /* If the body of the function has not yet been parsed, parse it
18228      now.  */
18229   if (DECL_PENDING_INLINE_P (member_function))
18230     {
18231       tree function_scope;
18232       cp_token_cache *tokens;
18233
18234       /* The function is no longer pending; we are processing it.  */
18235       tokens = DECL_PENDING_INLINE_INFO (member_function);
18236       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18237       DECL_PENDING_INLINE_P (member_function) = 0;
18238
18239       /* If this is a local class, enter the scope of the containing
18240          function.  */
18241       function_scope = current_function_decl;
18242       if (function_scope)
18243         push_function_context ();
18244
18245       /* Push the body of the function onto the lexer stack.  */
18246       cp_parser_push_lexer_for_tokens (parser, tokens);
18247
18248       /* Let the front end know that we going to be defining this
18249          function.  */
18250       start_preparsed_function (member_function, NULL_TREE,
18251                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18252
18253       /* Don't do access checking if it is a templated function.  */
18254       if (processing_template_decl)
18255         push_deferring_access_checks (dk_no_check);
18256
18257       /* Now, parse the body of the function.  */
18258       cp_parser_function_definition_after_declarator (parser,
18259                                                       /*inline_p=*/true);
18260
18261       if (processing_template_decl)
18262         pop_deferring_access_checks ();
18263
18264       /* Leave the scope of the containing function.  */
18265       if (function_scope)
18266         pop_function_context ();
18267       cp_parser_pop_lexer (parser);
18268     }
18269
18270   /* Remove any template parameters from the symbol table.  */
18271   maybe_end_member_template_processing ();
18272
18273   /* Restore the queue.  */
18274   parser->unparsed_functions_queues
18275     = TREE_CHAIN (parser->unparsed_functions_queues);
18276 }
18277
18278 /* If DECL contains any default args, remember it on the unparsed
18279    functions queue.  */
18280
18281 static void
18282 cp_parser_save_default_args (cp_parser* parser, tree decl)
18283 {
18284   tree probe;
18285
18286   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18287        probe;
18288        probe = TREE_CHAIN (probe))
18289     if (TREE_PURPOSE (probe))
18290       {
18291         TREE_PURPOSE (parser->unparsed_functions_queues)
18292           = tree_cons (current_class_type, decl,
18293                        TREE_PURPOSE (parser->unparsed_functions_queues));
18294         break;
18295       }
18296 }
18297
18298 /* FN is a FUNCTION_DECL which may contains a parameter with an
18299    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18300    assumes that the current scope is the scope in which the default
18301    argument should be processed.  */
18302
18303 static void
18304 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18305 {
18306   bool saved_local_variables_forbidden_p;
18307   tree parm;
18308
18309   /* While we're parsing the default args, we might (due to the
18310      statement expression extension) encounter more classes.  We want
18311      to handle them right away, but we don't want them getting mixed
18312      up with default args that are currently in the queue.  */
18313   parser->unparsed_functions_queues
18314     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18315
18316   /* Local variable names (and the `this' keyword) may not appear
18317      in a default argument.  */
18318   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18319   parser->local_variables_forbidden_p = true;
18320
18321   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18322        parm;
18323        parm = TREE_CHAIN (parm))
18324     {
18325       cp_token_cache *tokens;
18326       tree default_arg = TREE_PURPOSE (parm);
18327       tree parsed_arg;
18328       VEC(tree,gc) *insts;
18329       tree copy;
18330       unsigned ix;
18331
18332       if (!default_arg)
18333         continue;
18334
18335       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18336         /* This can happen for a friend declaration for a function
18337            already declared with default arguments.  */
18338         continue;
18339
18340        /* Push the saved tokens for the default argument onto the parser's
18341           lexer stack.  */
18342       tokens = DEFARG_TOKENS (default_arg);
18343       cp_parser_push_lexer_for_tokens (parser, tokens);
18344
18345       /* Parse the assignment-expression.  */
18346       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18347       if (parsed_arg == error_mark_node)
18348         {
18349           cp_parser_pop_lexer (parser);
18350           continue;
18351         }
18352
18353       if (!processing_template_decl)
18354         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18355
18356       TREE_PURPOSE (parm) = parsed_arg;
18357
18358       /* Update any instantiations we've already created.  */
18359       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18360            VEC_iterate (tree, insts, ix, copy); ix++)
18361         TREE_PURPOSE (copy) = parsed_arg;
18362
18363       /* If the token stream has not been completely used up, then
18364          there was extra junk after the end of the default
18365          argument.  */
18366       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18367         cp_parser_error (parser, "expected %<,%>");
18368
18369       /* Revert to the main lexer.  */
18370       cp_parser_pop_lexer (parser);
18371     }
18372
18373   /* Make sure no default arg is missing.  */
18374   check_default_args (fn);
18375
18376   /* Restore the state of local_variables_forbidden_p.  */
18377   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18378
18379   /* Restore the queue.  */
18380   parser->unparsed_functions_queues
18381     = TREE_CHAIN (parser->unparsed_functions_queues);
18382 }
18383
18384 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18385    either a TYPE or an expression, depending on the form of the
18386    input.  The KEYWORD indicates which kind of expression we have
18387    encountered.  */
18388
18389 static tree
18390 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18391 {
18392   tree expr = NULL_TREE;
18393   const char *saved_message;
18394   char *tmp;
18395   bool saved_integral_constant_expression_p;
18396   bool saved_non_integral_constant_expression_p;
18397   bool pack_expansion_p = false;
18398
18399   /* Types cannot be defined in a `sizeof' expression.  Save away the
18400      old message.  */
18401   saved_message = parser->type_definition_forbidden_message;
18402   /* And create the new one.  */
18403   tmp = concat ("types may not be defined in %<",
18404                 IDENTIFIER_POINTER (ridpointers[keyword]),
18405                 "%> expressions", NULL);
18406   parser->type_definition_forbidden_message = tmp;
18407
18408   /* The restrictions on constant-expressions do not apply inside
18409      sizeof expressions.  */
18410   saved_integral_constant_expression_p
18411     = parser->integral_constant_expression_p;
18412   saved_non_integral_constant_expression_p
18413     = parser->non_integral_constant_expression_p;
18414   parser->integral_constant_expression_p = false;
18415
18416   /* If it's a `...', then we are computing the length of a parameter
18417      pack.  */
18418   if (keyword == RID_SIZEOF
18419       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18420     {
18421       /* Consume the `...'.  */
18422       cp_lexer_consume_token (parser->lexer);
18423       maybe_warn_variadic_templates ();
18424
18425       /* Note that this is an expansion.  */
18426       pack_expansion_p = true;
18427     }
18428
18429   /* Do not actually evaluate the expression.  */
18430   ++skip_evaluation;
18431   /* If it's a `(', then we might be looking at the type-id
18432      construction.  */
18433   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18434     {
18435       tree type;
18436       bool saved_in_type_id_in_expr_p;
18437
18438       /* We can't be sure yet whether we're looking at a type-id or an
18439          expression.  */
18440       cp_parser_parse_tentatively (parser);
18441       /* Consume the `('.  */
18442       cp_lexer_consume_token (parser->lexer);
18443       /* Parse the type-id.  */
18444       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18445       parser->in_type_id_in_expr_p = true;
18446       type = cp_parser_type_id (parser);
18447       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18448       /* Now, look for the trailing `)'.  */
18449       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18450       /* If all went well, then we're done.  */
18451       if (cp_parser_parse_definitely (parser))
18452         {
18453           cp_decl_specifier_seq decl_specs;
18454
18455           /* Build a trivial decl-specifier-seq.  */
18456           clear_decl_specs (&decl_specs);
18457           decl_specs.type = type;
18458
18459           /* Call grokdeclarator to figure out what type this is.  */
18460           expr = grokdeclarator (NULL,
18461                                  &decl_specs,
18462                                  TYPENAME,
18463                                  /*initialized=*/0,
18464                                  /*attrlist=*/NULL);
18465         }
18466     }
18467
18468   /* If the type-id production did not work out, then we must be
18469      looking at the unary-expression production.  */
18470   if (!expr)
18471     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18472                                        /*cast_p=*/false, NULL);
18473
18474   if (pack_expansion_p)
18475     /* Build a pack expansion. */
18476     expr = make_pack_expansion (expr);
18477
18478   /* Go back to evaluating expressions.  */
18479   --skip_evaluation;
18480
18481   /* Free the message we created.  */
18482   free (tmp);
18483   /* And restore the old one.  */
18484   parser->type_definition_forbidden_message = saved_message;
18485   parser->integral_constant_expression_p
18486     = saved_integral_constant_expression_p;
18487   parser->non_integral_constant_expression_p
18488     = saved_non_integral_constant_expression_p;
18489
18490   return expr;
18491 }
18492
18493 /* If the current declaration has no declarator, return true.  */
18494
18495 static bool
18496 cp_parser_declares_only_class_p (cp_parser *parser)
18497 {
18498   /* If the next token is a `;' or a `,' then there is no
18499      declarator.  */
18500   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18501           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18502 }
18503
18504 /* Update the DECL_SPECS to reflect the storage class indicated by
18505    KEYWORD.  */
18506
18507 static void
18508 cp_parser_set_storage_class (cp_parser *parser,
18509                              cp_decl_specifier_seq *decl_specs,
18510                              enum rid keyword,
18511                              location_t location)
18512 {
18513   cp_storage_class storage_class;
18514
18515   if (parser->in_unbraced_linkage_specification_p)
18516     {
18517       error ("%Hinvalid use of %qD in linkage specification",
18518              &location, ridpointers[keyword]);
18519       return;
18520     }
18521   else if (decl_specs->storage_class != sc_none)
18522     {
18523       decl_specs->conflicting_specifiers_p = true;
18524       return;
18525     }
18526
18527   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18528       && decl_specs->specs[(int) ds_thread])
18529     {
18530       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18531       decl_specs->specs[(int) ds_thread] = 0;
18532     }
18533
18534   switch (keyword)
18535     {
18536     case RID_AUTO:
18537       storage_class = sc_auto;
18538       break;
18539     case RID_REGISTER:
18540       storage_class = sc_register;
18541       break;
18542     case RID_STATIC:
18543       storage_class = sc_static;
18544       break;
18545     case RID_EXTERN:
18546       storage_class = sc_extern;
18547       break;
18548     case RID_MUTABLE:
18549       storage_class = sc_mutable;
18550       break;
18551     default:
18552       gcc_unreachable ();
18553     }
18554   decl_specs->storage_class = storage_class;
18555
18556   /* A storage class specifier cannot be applied alongside a typedef 
18557      specifier. If there is a typedef specifier present then set 
18558      conflicting_specifiers_p which will trigger an error later
18559      on in grokdeclarator. */
18560   if (decl_specs->specs[(int)ds_typedef])
18561     decl_specs->conflicting_specifiers_p = true;
18562 }
18563
18564 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18565    is true, the type is a user-defined type; otherwise it is a
18566    built-in type specified by a keyword.  */
18567
18568 static void
18569 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18570                               tree type_spec,
18571                               location_t location,
18572                               bool user_defined_p)
18573 {
18574   decl_specs->any_specifiers_p = true;
18575
18576   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18577      (with, for example, in "typedef int wchar_t;") we remember that
18578      this is what happened.  In system headers, we ignore these
18579      declarations so that G++ can work with system headers that are not
18580      C++-safe.  */
18581   if (decl_specs->specs[(int) ds_typedef]
18582       && !user_defined_p
18583       && (type_spec == boolean_type_node
18584           || type_spec == char16_type_node
18585           || type_spec == char32_type_node
18586           || type_spec == wchar_type_node)
18587       && (decl_specs->type
18588           || decl_specs->specs[(int) ds_long]
18589           || decl_specs->specs[(int) ds_short]
18590           || decl_specs->specs[(int) ds_unsigned]
18591           || decl_specs->specs[(int) ds_signed]))
18592     {
18593       decl_specs->redefined_builtin_type = type_spec;
18594       if (!decl_specs->type)
18595         {
18596           decl_specs->type = type_spec;
18597           decl_specs->user_defined_type_p = false;
18598           decl_specs->type_location = location;
18599         }
18600     }
18601   else if (decl_specs->type)
18602     decl_specs->multiple_types_p = true;
18603   else
18604     {
18605       decl_specs->type = type_spec;
18606       decl_specs->user_defined_type_p = user_defined_p;
18607       decl_specs->redefined_builtin_type = NULL_TREE;
18608       decl_specs->type_location = location;
18609     }
18610 }
18611
18612 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18613    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18614
18615 static bool
18616 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18617 {
18618   return decl_specifiers->specs[(int) ds_friend] != 0;
18619 }
18620
18621 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18622    issue an error message indicating that TOKEN_DESC was expected.
18623
18624    Returns the token consumed, if the token had the appropriate type.
18625    Otherwise, returns NULL.  */
18626
18627 static cp_token *
18628 cp_parser_require (cp_parser* parser,
18629                    enum cpp_ttype type,
18630                    const char* token_desc)
18631 {
18632   if (cp_lexer_next_token_is (parser->lexer, type))
18633     return cp_lexer_consume_token (parser->lexer);
18634   else
18635     {
18636       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18637       if (!cp_parser_simulate_error (parser))
18638         {
18639           char *message = concat ("expected ", token_desc, NULL);
18640           cp_parser_error (parser, message);
18641           free (message);
18642         }
18643       return NULL;
18644     }
18645 }
18646
18647 /* An error message is produced if the next token is not '>'.
18648    All further tokens are skipped until the desired token is
18649    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18650
18651 static void
18652 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18653 {
18654   /* Current level of '< ... >'.  */
18655   unsigned level = 0;
18656   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18657   unsigned nesting_depth = 0;
18658
18659   /* Are we ready, yet?  If not, issue error message.  */
18660   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18661     return;
18662
18663   /* Skip tokens until the desired token is found.  */
18664   while (true)
18665     {
18666       /* Peek at the next token.  */
18667       switch (cp_lexer_peek_token (parser->lexer)->type)
18668         {
18669         case CPP_LESS:
18670           if (!nesting_depth)
18671             ++level;
18672           break;
18673
18674         case CPP_RSHIFT:
18675           if (cxx_dialect == cxx98)
18676             /* C++0x views the `>>' operator as two `>' tokens, but
18677                C++98 does not. */
18678             break;
18679           else if (!nesting_depth && level-- == 0)
18680             {
18681               /* We've hit a `>>' where the first `>' closes the
18682                  template argument list, and the second `>' is
18683                  spurious.  Just consume the `>>' and stop; we've
18684                  already produced at least one error.  */
18685               cp_lexer_consume_token (parser->lexer);
18686               return;
18687             }
18688           /* Fall through for C++0x, so we handle the second `>' in
18689              the `>>'.  */
18690
18691         case CPP_GREATER:
18692           if (!nesting_depth && level-- == 0)
18693             {
18694               /* We've reached the token we want, consume it and stop.  */
18695               cp_lexer_consume_token (parser->lexer);
18696               return;
18697             }
18698           break;
18699
18700         case CPP_OPEN_PAREN:
18701         case CPP_OPEN_SQUARE:
18702           ++nesting_depth;
18703           break;
18704
18705         case CPP_CLOSE_PAREN:
18706         case CPP_CLOSE_SQUARE:
18707           if (nesting_depth-- == 0)
18708             return;
18709           break;
18710
18711         case CPP_EOF:
18712         case CPP_PRAGMA_EOL:
18713         case CPP_SEMICOLON:
18714         case CPP_OPEN_BRACE:
18715         case CPP_CLOSE_BRACE:
18716           /* The '>' was probably forgotten, don't look further.  */
18717           return;
18718
18719         default:
18720           break;
18721         }
18722
18723       /* Consume this token.  */
18724       cp_lexer_consume_token (parser->lexer);
18725     }
18726 }
18727
18728 /* If the next token is the indicated keyword, consume it.  Otherwise,
18729    issue an error message indicating that TOKEN_DESC was expected.
18730
18731    Returns the token consumed, if the token had the appropriate type.
18732    Otherwise, returns NULL.  */
18733
18734 static cp_token *
18735 cp_parser_require_keyword (cp_parser* parser,
18736                            enum rid keyword,
18737                            const char* token_desc)
18738 {
18739   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18740
18741   if (token && token->keyword != keyword)
18742     {
18743       dyn_string_t error_msg;
18744
18745       /* Format the error message.  */
18746       error_msg = dyn_string_new (0);
18747       dyn_string_append_cstr (error_msg, "expected ");
18748       dyn_string_append_cstr (error_msg, token_desc);
18749       cp_parser_error (parser, error_msg->s);
18750       dyn_string_delete (error_msg);
18751       return NULL;
18752     }
18753
18754   return token;
18755 }
18756
18757 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18758    function-definition.  */
18759
18760 static bool
18761 cp_parser_token_starts_function_definition_p (cp_token* token)
18762 {
18763   return (/* An ordinary function-body begins with an `{'.  */
18764           token->type == CPP_OPEN_BRACE
18765           /* A ctor-initializer begins with a `:'.  */
18766           || token->type == CPP_COLON
18767           /* A function-try-block begins with `try'.  */
18768           || token->keyword == RID_TRY
18769           /* The named return value extension begins with `return'.  */
18770           || token->keyword == RID_RETURN);
18771 }
18772
18773 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18774    definition.  */
18775
18776 static bool
18777 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18778 {
18779   cp_token *token;
18780
18781   token = cp_lexer_peek_token (parser->lexer);
18782   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18783 }
18784
18785 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18786    C++0x) ending a template-argument.  */
18787
18788 static bool
18789 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18790 {
18791   cp_token *token;
18792
18793   token = cp_lexer_peek_token (parser->lexer);
18794   return (token->type == CPP_COMMA 
18795           || token->type == CPP_GREATER
18796           || token->type == CPP_ELLIPSIS
18797           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18798 }
18799
18800 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18801    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18802
18803 static bool
18804 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18805                                                      size_t n)
18806 {
18807   cp_token *token;
18808
18809   token = cp_lexer_peek_nth_token (parser->lexer, n);
18810   if (token->type == CPP_LESS)
18811     return true;
18812   /* Check for the sequence `<::' in the original code. It would be lexed as
18813      `[:', where `[' is a digraph, and there is no whitespace before
18814      `:'.  */
18815   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18816     {
18817       cp_token *token2;
18818       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18819       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18820         return true;
18821     }
18822   return false;
18823 }
18824
18825 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18826    or none_type otherwise.  */
18827
18828 static enum tag_types
18829 cp_parser_token_is_class_key (cp_token* token)
18830 {
18831   switch (token->keyword)
18832     {
18833     case RID_CLASS:
18834       return class_type;
18835     case RID_STRUCT:
18836       return record_type;
18837     case RID_UNION:
18838       return union_type;
18839
18840     default:
18841       return none_type;
18842     }
18843 }
18844
18845 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18846
18847 static void
18848 cp_parser_check_class_key (enum tag_types class_key, tree type)
18849 {
18850   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18851     permerror (input_location, "%qs tag used in naming %q#T",
18852             class_key == union_type ? "union"
18853              : class_key == record_type ? "struct" : "class",
18854              type);
18855 }
18856
18857 /* Issue an error message if DECL is redeclared with different
18858    access than its original declaration [class.access.spec/3].
18859    This applies to nested classes and nested class templates.
18860    [class.mem/1].  */
18861
18862 static void
18863 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18864 {
18865   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18866     return;
18867
18868   if ((TREE_PRIVATE (decl)
18869        != (current_access_specifier == access_private_node))
18870       || (TREE_PROTECTED (decl)
18871           != (current_access_specifier == access_protected_node)))
18872     error ("%H%qD redeclared with different access", &location, decl);
18873 }
18874
18875 /* Look for the `template' keyword, as a syntactic disambiguator.
18876    Return TRUE iff it is present, in which case it will be
18877    consumed.  */
18878
18879 static bool
18880 cp_parser_optional_template_keyword (cp_parser *parser)
18881 {
18882   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18883     {
18884       /* The `template' keyword can only be used within templates;
18885          outside templates the parser can always figure out what is a
18886          template and what is not.  */
18887       if (!processing_template_decl)
18888         {
18889           cp_token *token = cp_lexer_peek_token (parser->lexer);
18890           error ("%H%<template%> (as a disambiguator) is only allowed "
18891                  "within templates", &token->location);
18892           /* If this part of the token stream is rescanned, the same
18893              error message would be generated.  So, we purge the token
18894              from the stream.  */
18895           cp_lexer_purge_token (parser->lexer);
18896           return false;
18897         }
18898       else
18899         {
18900           /* Consume the `template' keyword.  */
18901           cp_lexer_consume_token (parser->lexer);
18902           return true;
18903         }
18904     }
18905
18906   return false;
18907 }
18908
18909 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18910    set PARSER->SCOPE, and perform other related actions.  */
18911
18912 static void
18913 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18914 {
18915   int i;
18916   struct tree_check *check_value;
18917   deferred_access_check *chk;
18918   VEC (deferred_access_check,gc) *checks;
18919
18920   /* Get the stored value.  */
18921   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18922   /* Perform any access checks that were deferred.  */
18923   checks = check_value->checks;
18924   if (checks)
18925     {
18926       for (i = 0 ;
18927            VEC_iterate (deferred_access_check, checks, i, chk) ;
18928            ++i)
18929         {
18930           perform_or_defer_access_check (chk->binfo,
18931                                          chk->decl,
18932                                          chk->diag_decl);
18933         }
18934     }
18935   /* Set the scope from the stored value.  */
18936   parser->scope = check_value->value;
18937   parser->qualifying_scope = check_value->qualifying_scope;
18938   parser->object_scope = NULL_TREE;
18939 }
18940
18941 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18942    encounter the end of a block before what we were looking for.  */
18943
18944 static bool
18945 cp_parser_cache_group (cp_parser *parser,
18946                        enum cpp_ttype end,
18947                        unsigned depth)
18948 {
18949   while (true)
18950     {
18951       cp_token *token = cp_lexer_peek_token (parser->lexer);
18952
18953       /* Abort a parenthesized expression if we encounter a semicolon.  */
18954       if ((end == CPP_CLOSE_PAREN || depth == 0)
18955           && token->type == CPP_SEMICOLON)
18956         return true;
18957       /* If we've reached the end of the file, stop.  */
18958       if (token->type == CPP_EOF
18959           || (end != CPP_PRAGMA_EOL
18960               && token->type == CPP_PRAGMA_EOL))
18961         return true;
18962       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18963         /* We've hit the end of an enclosing block, so there's been some
18964            kind of syntax error.  */
18965         return true;
18966
18967       /* Consume the token.  */
18968       cp_lexer_consume_token (parser->lexer);
18969       /* See if it starts a new group.  */
18970       if (token->type == CPP_OPEN_BRACE)
18971         {
18972           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18973           /* In theory this should probably check end == '}', but
18974              cp_parser_save_member_function_body needs it to exit
18975              after either '}' or ')' when called with ')'.  */
18976           if (depth == 0)
18977             return false;
18978         }
18979       else if (token->type == CPP_OPEN_PAREN)
18980         {
18981           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18982           if (depth == 0 && end == CPP_CLOSE_PAREN)
18983             return false;
18984         }
18985       else if (token->type == CPP_PRAGMA)
18986         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18987       else if (token->type == end)
18988         return false;
18989     }
18990 }
18991
18992 /* Begin parsing tentatively.  We always save tokens while parsing
18993    tentatively so that if the tentative parsing fails we can restore the
18994    tokens.  */
18995
18996 static void
18997 cp_parser_parse_tentatively (cp_parser* parser)
18998 {
18999   /* Enter a new parsing context.  */
19000   parser->context = cp_parser_context_new (parser->context);
19001   /* Begin saving tokens.  */
19002   cp_lexer_save_tokens (parser->lexer);
19003   /* In order to avoid repetitive access control error messages,
19004      access checks are queued up until we are no longer parsing
19005      tentatively.  */
19006   push_deferring_access_checks (dk_deferred);
19007 }
19008
19009 /* Commit to the currently active tentative parse.  */
19010
19011 static void
19012 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19013 {
19014   cp_parser_context *context;
19015   cp_lexer *lexer;
19016
19017   /* Mark all of the levels as committed.  */
19018   lexer = parser->lexer;
19019   for (context = parser->context; context->next; context = context->next)
19020     {
19021       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19022         break;
19023       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19024       while (!cp_lexer_saving_tokens (lexer))
19025         lexer = lexer->next;
19026       cp_lexer_commit_tokens (lexer);
19027     }
19028 }
19029
19030 /* Abort the currently active tentative parse.  All consumed tokens
19031    will be rolled back, and no diagnostics will be issued.  */
19032
19033 static void
19034 cp_parser_abort_tentative_parse (cp_parser* parser)
19035 {
19036   cp_parser_simulate_error (parser);
19037   /* Now, pretend that we want to see if the construct was
19038      successfully parsed.  */
19039   cp_parser_parse_definitely (parser);
19040 }
19041
19042 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19043    token stream.  Otherwise, commit to the tokens we have consumed.
19044    Returns true if no error occurred; false otherwise.  */
19045
19046 static bool
19047 cp_parser_parse_definitely (cp_parser* parser)
19048 {
19049   bool error_occurred;
19050   cp_parser_context *context;
19051
19052   /* Remember whether or not an error occurred, since we are about to
19053      destroy that information.  */
19054   error_occurred = cp_parser_error_occurred (parser);
19055   /* Remove the topmost context from the stack.  */
19056   context = parser->context;
19057   parser->context = context->next;
19058   /* If no parse errors occurred, commit to the tentative parse.  */
19059   if (!error_occurred)
19060     {
19061       /* Commit to the tokens read tentatively, unless that was
19062          already done.  */
19063       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19064         cp_lexer_commit_tokens (parser->lexer);
19065
19066       pop_to_parent_deferring_access_checks ();
19067     }
19068   /* Otherwise, if errors occurred, roll back our state so that things
19069      are just as they were before we began the tentative parse.  */
19070   else
19071     {
19072       cp_lexer_rollback_tokens (parser->lexer);
19073       pop_deferring_access_checks ();
19074     }
19075   /* Add the context to the front of the free list.  */
19076   context->next = cp_parser_context_free_list;
19077   cp_parser_context_free_list = context;
19078
19079   return !error_occurred;
19080 }
19081
19082 /* Returns true if we are parsing tentatively and are not committed to
19083    this tentative parse.  */
19084
19085 static bool
19086 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19087 {
19088   return (cp_parser_parsing_tentatively (parser)
19089           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19090 }
19091
19092 /* Returns nonzero iff an error has occurred during the most recent
19093    tentative parse.  */
19094
19095 static bool
19096 cp_parser_error_occurred (cp_parser* parser)
19097 {
19098   return (cp_parser_parsing_tentatively (parser)
19099           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19100 }
19101
19102 /* Returns nonzero if GNU extensions are allowed.  */
19103
19104 static bool
19105 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19106 {
19107   return parser->allow_gnu_extensions_p;
19108 }
19109 \f
19110 /* Objective-C++ Productions */
19111
19112
19113 /* Parse an Objective-C expression, which feeds into a primary-expression
19114    above.
19115
19116    objc-expression:
19117      objc-message-expression
19118      objc-string-literal
19119      objc-encode-expression
19120      objc-protocol-expression
19121      objc-selector-expression
19122
19123   Returns a tree representation of the expression.  */
19124
19125 static tree
19126 cp_parser_objc_expression (cp_parser* parser)
19127 {
19128   /* Try to figure out what kind of declaration is present.  */
19129   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19130
19131   switch (kwd->type)
19132     {
19133     case CPP_OPEN_SQUARE:
19134       return cp_parser_objc_message_expression (parser);
19135
19136     case CPP_OBJC_STRING:
19137       kwd = cp_lexer_consume_token (parser->lexer);
19138       return objc_build_string_object (kwd->u.value);
19139
19140     case CPP_KEYWORD:
19141       switch (kwd->keyword)
19142         {
19143         case RID_AT_ENCODE:
19144           return cp_parser_objc_encode_expression (parser);
19145
19146         case RID_AT_PROTOCOL:
19147           return cp_parser_objc_protocol_expression (parser);
19148
19149         case RID_AT_SELECTOR:
19150           return cp_parser_objc_selector_expression (parser);
19151
19152         default:
19153           break;
19154         }
19155     default:
19156       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19157              &kwd->location, kwd->u.value);
19158       cp_parser_skip_to_end_of_block_or_statement (parser);
19159     }
19160
19161   return error_mark_node;
19162 }
19163
19164 /* Parse an Objective-C message expression.
19165
19166    objc-message-expression:
19167      [ objc-message-receiver objc-message-args ]
19168
19169    Returns a representation of an Objective-C message.  */
19170
19171 static tree
19172 cp_parser_objc_message_expression (cp_parser* parser)
19173 {
19174   tree receiver, messageargs;
19175
19176   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19177   receiver = cp_parser_objc_message_receiver (parser);
19178   messageargs = cp_parser_objc_message_args (parser);
19179   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19180
19181   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19182 }
19183
19184 /* Parse an objc-message-receiver.
19185
19186    objc-message-receiver:
19187      expression
19188      simple-type-specifier
19189
19190   Returns a representation of the type or expression.  */
19191
19192 static tree
19193 cp_parser_objc_message_receiver (cp_parser* parser)
19194 {
19195   tree rcv;
19196
19197   /* An Objective-C message receiver may be either (1) a type
19198      or (2) an expression.  */
19199   cp_parser_parse_tentatively (parser);
19200   rcv = cp_parser_expression (parser, false, NULL);
19201
19202   if (cp_parser_parse_definitely (parser))
19203     return rcv;
19204
19205   rcv = cp_parser_simple_type_specifier (parser,
19206                                          /*decl_specs=*/NULL,
19207                                          CP_PARSER_FLAGS_NONE);
19208
19209   return objc_get_class_reference (rcv);
19210 }
19211
19212 /* Parse the arguments and selectors comprising an Objective-C message.
19213
19214    objc-message-args:
19215      objc-selector
19216      objc-selector-args
19217      objc-selector-args , objc-comma-args
19218
19219    objc-selector-args:
19220      objc-selector [opt] : assignment-expression
19221      objc-selector-args objc-selector [opt] : assignment-expression
19222
19223    objc-comma-args:
19224      assignment-expression
19225      objc-comma-args , assignment-expression
19226
19227    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19228    selector arguments and TREE_VALUE containing a list of comma
19229    arguments.  */
19230
19231 static tree
19232 cp_parser_objc_message_args (cp_parser* parser)
19233 {
19234   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19235   bool maybe_unary_selector_p = true;
19236   cp_token *token = cp_lexer_peek_token (parser->lexer);
19237
19238   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19239     {
19240       tree selector = NULL_TREE, arg;
19241
19242       if (token->type != CPP_COLON)
19243         selector = cp_parser_objc_selector (parser);
19244
19245       /* Detect if we have a unary selector.  */
19246       if (maybe_unary_selector_p
19247           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19248         return build_tree_list (selector, NULL_TREE);
19249
19250       maybe_unary_selector_p = false;
19251       cp_parser_require (parser, CPP_COLON, "%<:%>");
19252       arg = cp_parser_assignment_expression (parser, false, NULL);
19253
19254       sel_args
19255         = chainon (sel_args,
19256                    build_tree_list (selector, arg));
19257
19258       token = cp_lexer_peek_token (parser->lexer);
19259     }
19260
19261   /* Handle non-selector arguments, if any. */
19262   while (token->type == CPP_COMMA)
19263     {
19264       tree arg;
19265
19266       cp_lexer_consume_token (parser->lexer);
19267       arg = cp_parser_assignment_expression (parser, false, NULL);
19268
19269       addl_args
19270         = chainon (addl_args,
19271                    build_tree_list (NULL_TREE, arg));
19272
19273       token = cp_lexer_peek_token (parser->lexer);
19274     }
19275
19276   return build_tree_list (sel_args, addl_args);
19277 }
19278
19279 /* Parse an Objective-C encode expression.
19280
19281    objc-encode-expression:
19282      @encode objc-typename
19283
19284    Returns an encoded representation of the type argument.  */
19285
19286 static tree
19287 cp_parser_objc_encode_expression (cp_parser* parser)
19288 {
19289   tree type;
19290   cp_token *token;
19291
19292   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19293   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19294   token = cp_lexer_peek_token (parser->lexer);
19295   type = complete_type (cp_parser_type_id (parser));
19296   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19297
19298   if (!type)
19299     {
19300       error ("%H%<@encode%> must specify a type as an argument",
19301              &token->location);
19302       return error_mark_node;
19303     }
19304
19305   return objc_build_encode_expr (type);
19306 }
19307
19308 /* Parse an Objective-C @defs expression.  */
19309
19310 static tree
19311 cp_parser_objc_defs_expression (cp_parser *parser)
19312 {
19313   tree name;
19314
19315   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19316   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19317   name = cp_parser_identifier (parser);
19318   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19319
19320   return objc_get_class_ivars (name);
19321 }
19322
19323 /* Parse an Objective-C protocol expression.
19324
19325   objc-protocol-expression:
19326     @protocol ( identifier )
19327
19328   Returns a representation of the protocol expression.  */
19329
19330 static tree
19331 cp_parser_objc_protocol_expression (cp_parser* parser)
19332 {
19333   tree proto;
19334
19335   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19336   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19337   proto = cp_parser_identifier (parser);
19338   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19339
19340   return objc_build_protocol_expr (proto);
19341 }
19342
19343 /* Parse an Objective-C selector expression.
19344
19345    objc-selector-expression:
19346      @selector ( objc-method-signature )
19347
19348    objc-method-signature:
19349      objc-selector
19350      objc-selector-seq
19351
19352    objc-selector-seq:
19353      objc-selector :
19354      objc-selector-seq objc-selector :
19355
19356   Returns a representation of the method selector.  */
19357
19358 static tree
19359 cp_parser_objc_selector_expression (cp_parser* parser)
19360 {
19361   tree sel_seq = NULL_TREE;
19362   bool maybe_unary_selector_p = true;
19363   cp_token *token;
19364
19365   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19366   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19367   token = cp_lexer_peek_token (parser->lexer);
19368
19369   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19370          || token->type == CPP_SCOPE)
19371     {
19372       tree selector = NULL_TREE;
19373
19374       if (token->type != CPP_COLON
19375           || token->type == CPP_SCOPE)
19376         selector = cp_parser_objc_selector (parser);
19377
19378       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19379           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19380         {
19381           /* Detect if we have a unary selector.  */
19382           if (maybe_unary_selector_p)
19383             {
19384               sel_seq = selector;
19385               goto finish_selector;
19386             }
19387           else
19388             {
19389               cp_parser_error (parser, "expected %<:%>");
19390             }
19391         }
19392       maybe_unary_selector_p = false;
19393       token = cp_lexer_consume_token (parser->lexer);
19394
19395       if (token->type == CPP_SCOPE)
19396         {
19397           sel_seq
19398             = chainon (sel_seq,
19399                        build_tree_list (selector, NULL_TREE));
19400           sel_seq
19401             = chainon (sel_seq,
19402                        build_tree_list (NULL_TREE, NULL_TREE));
19403         }
19404       else
19405         sel_seq
19406           = chainon (sel_seq,
19407                      build_tree_list (selector, NULL_TREE));
19408
19409       token = cp_lexer_peek_token (parser->lexer);
19410     }
19411
19412  finish_selector:
19413   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19414
19415   return objc_build_selector_expr (sel_seq);
19416 }
19417
19418 /* Parse a list of identifiers.
19419
19420    objc-identifier-list:
19421      identifier
19422      objc-identifier-list , identifier
19423
19424    Returns a TREE_LIST of identifier nodes.  */
19425
19426 static tree
19427 cp_parser_objc_identifier_list (cp_parser* parser)
19428 {
19429   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19430   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19431
19432   while (sep->type == CPP_COMMA)
19433     {
19434       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19435       list = chainon (list,
19436                       build_tree_list (NULL_TREE,
19437                                        cp_parser_identifier (parser)));
19438       sep = cp_lexer_peek_token (parser->lexer);
19439     }
19440
19441   return list;
19442 }
19443
19444 /* Parse an Objective-C alias declaration.
19445
19446    objc-alias-declaration:
19447      @compatibility_alias identifier identifier ;
19448
19449    This function registers the alias mapping with the Objective-C front end.
19450    It returns nothing.  */
19451
19452 static void
19453 cp_parser_objc_alias_declaration (cp_parser* parser)
19454 {
19455   tree alias, orig;
19456
19457   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19458   alias = cp_parser_identifier (parser);
19459   orig = cp_parser_identifier (parser);
19460   objc_declare_alias (alias, orig);
19461   cp_parser_consume_semicolon_at_end_of_statement (parser);
19462 }
19463
19464 /* Parse an Objective-C class forward-declaration.
19465
19466    objc-class-declaration:
19467      @class objc-identifier-list ;
19468
19469    The function registers the forward declarations with the Objective-C
19470    front end.  It returns nothing.  */
19471
19472 static void
19473 cp_parser_objc_class_declaration (cp_parser* parser)
19474 {
19475   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19476   objc_declare_class (cp_parser_objc_identifier_list (parser));
19477   cp_parser_consume_semicolon_at_end_of_statement (parser);
19478 }
19479
19480 /* Parse a list of Objective-C protocol references.
19481
19482    objc-protocol-refs-opt:
19483      objc-protocol-refs [opt]
19484
19485    objc-protocol-refs:
19486      < objc-identifier-list >
19487
19488    Returns a TREE_LIST of identifiers, if any.  */
19489
19490 static tree
19491 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19492 {
19493   tree protorefs = NULL_TREE;
19494
19495   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19496     {
19497       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19498       protorefs = cp_parser_objc_identifier_list (parser);
19499       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19500     }
19501
19502   return protorefs;
19503 }
19504
19505 /* Parse a Objective-C visibility specification.  */
19506
19507 static void
19508 cp_parser_objc_visibility_spec (cp_parser* parser)
19509 {
19510   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19511
19512   switch (vis->keyword)
19513     {
19514     case RID_AT_PRIVATE:
19515       objc_set_visibility (2);
19516       break;
19517     case RID_AT_PROTECTED:
19518       objc_set_visibility (0);
19519       break;
19520     case RID_AT_PUBLIC:
19521       objc_set_visibility (1);
19522       break;
19523     default:
19524       return;
19525     }
19526
19527   /* Eat '@private'/'@protected'/'@public'.  */
19528   cp_lexer_consume_token (parser->lexer);
19529 }
19530
19531 /* Parse an Objective-C method type.  */
19532
19533 static void
19534 cp_parser_objc_method_type (cp_parser* parser)
19535 {
19536   objc_set_method_type
19537    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19538     ? PLUS_EXPR
19539     : MINUS_EXPR);
19540 }
19541
19542 /* Parse an Objective-C protocol qualifier.  */
19543
19544 static tree
19545 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19546 {
19547   tree quals = NULL_TREE, node;
19548   cp_token *token = cp_lexer_peek_token (parser->lexer);
19549
19550   node = token->u.value;
19551
19552   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19553          && (node == ridpointers [(int) RID_IN]
19554              || node == ridpointers [(int) RID_OUT]
19555              || node == ridpointers [(int) RID_INOUT]
19556              || node == ridpointers [(int) RID_BYCOPY]
19557              || node == ridpointers [(int) RID_BYREF]
19558              || node == ridpointers [(int) RID_ONEWAY]))
19559     {
19560       quals = tree_cons (NULL_TREE, node, quals);
19561       cp_lexer_consume_token (parser->lexer);
19562       token = cp_lexer_peek_token (parser->lexer);
19563       node = token->u.value;
19564     }
19565
19566   return quals;
19567 }
19568
19569 /* Parse an Objective-C typename.  */
19570
19571 static tree
19572 cp_parser_objc_typename (cp_parser* parser)
19573 {
19574   tree type_name = NULL_TREE;
19575
19576   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19577     {
19578       tree proto_quals, cp_type = NULL_TREE;
19579
19580       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19581       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19582
19583       /* An ObjC type name may consist of just protocol qualifiers, in which
19584          case the type shall default to 'id'.  */
19585       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19586         cp_type = cp_parser_type_id (parser);
19587
19588       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19589       type_name = build_tree_list (proto_quals, cp_type);
19590     }
19591
19592   return type_name;
19593 }
19594
19595 /* Check to see if TYPE refers to an Objective-C selector name.  */
19596
19597 static bool
19598 cp_parser_objc_selector_p (enum cpp_ttype type)
19599 {
19600   return (type == CPP_NAME || type == CPP_KEYWORD
19601           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19602           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19603           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19604           || type == CPP_XOR || type == CPP_XOR_EQ);
19605 }
19606
19607 /* Parse an Objective-C selector.  */
19608
19609 static tree
19610 cp_parser_objc_selector (cp_parser* parser)
19611 {
19612   cp_token *token = cp_lexer_consume_token (parser->lexer);
19613
19614   if (!cp_parser_objc_selector_p (token->type))
19615     {
19616       error ("%Hinvalid Objective-C++ selector name", &token->location);
19617       return error_mark_node;
19618     }
19619
19620   /* C++ operator names are allowed to appear in ObjC selectors.  */
19621   switch (token->type)
19622     {
19623     case CPP_AND_AND: return get_identifier ("and");
19624     case CPP_AND_EQ: return get_identifier ("and_eq");
19625     case CPP_AND: return get_identifier ("bitand");
19626     case CPP_OR: return get_identifier ("bitor");
19627     case CPP_COMPL: return get_identifier ("compl");
19628     case CPP_NOT: return get_identifier ("not");
19629     case CPP_NOT_EQ: return get_identifier ("not_eq");
19630     case CPP_OR_OR: return get_identifier ("or");
19631     case CPP_OR_EQ: return get_identifier ("or_eq");
19632     case CPP_XOR: return get_identifier ("xor");
19633     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19634     default: return token->u.value;
19635     }
19636 }
19637
19638 /* Parse an Objective-C params list.  */
19639
19640 static tree
19641 cp_parser_objc_method_keyword_params (cp_parser* parser)
19642 {
19643   tree params = NULL_TREE;
19644   bool maybe_unary_selector_p = true;
19645   cp_token *token = cp_lexer_peek_token (parser->lexer);
19646
19647   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19648     {
19649       tree selector = NULL_TREE, type_name, identifier;
19650
19651       if (token->type != CPP_COLON)
19652         selector = cp_parser_objc_selector (parser);
19653
19654       /* Detect if we have a unary selector.  */
19655       if (maybe_unary_selector_p
19656           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19657         return selector;
19658
19659       maybe_unary_selector_p = false;
19660       cp_parser_require (parser, CPP_COLON, "%<:%>");
19661       type_name = cp_parser_objc_typename (parser);
19662       identifier = cp_parser_identifier (parser);
19663
19664       params
19665         = chainon (params,
19666                    objc_build_keyword_decl (selector,
19667                                             type_name,
19668                                             identifier));
19669
19670       token = cp_lexer_peek_token (parser->lexer);
19671     }
19672
19673   return params;
19674 }
19675
19676 /* Parse the non-keyword Objective-C params.  */
19677
19678 static tree
19679 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19680 {
19681   tree params = make_node (TREE_LIST);
19682   cp_token *token = cp_lexer_peek_token (parser->lexer);
19683   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19684
19685   while (token->type == CPP_COMMA)
19686     {
19687       cp_parameter_declarator *parmdecl;
19688       tree parm;
19689
19690       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19691       token = cp_lexer_peek_token (parser->lexer);
19692
19693       if (token->type == CPP_ELLIPSIS)
19694         {
19695           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19696           *ellipsisp = true;
19697           break;
19698         }
19699
19700       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19701       parm = grokdeclarator (parmdecl->declarator,
19702                              &parmdecl->decl_specifiers,
19703                              PARM, /*initialized=*/0,
19704                              /*attrlist=*/NULL);
19705
19706       chainon (params, build_tree_list (NULL_TREE, parm));
19707       token = cp_lexer_peek_token (parser->lexer);
19708     }
19709
19710   return params;
19711 }
19712
19713 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19714
19715 static void
19716 cp_parser_objc_interstitial_code (cp_parser* parser)
19717 {
19718   cp_token *token = cp_lexer_peek_token (parser->lexer);
19719
19720   /* If the next token is `extern' and the following token is a string
19721      literal, then we have a linkage specification.  */
19722   if (token->keyword == RID_EXTERN
19723       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19724     cp_parser_linkage_specification (parser);
19725   /* Handle #pragma, if any.  */
19726   else if (token->type == CPP_PRAGMA)
19727     cp_parser_pragma (parser, pragma_external);
19728   /* Allow stray semicolons.  */
19729   else if (token->type == CPP_SEMICOLON)
19730     cp_lexer_consume_token (parser->lexer);
19731   /* Finally, try to parse a block-declaration, or a function-definition.  */
19732   else
19733     cp_parser_block_declaration (parser, /*statement_p=*/false);
19734 }
19735
19736 /* Parse a method signature.  */
19737
19738 static tree
19739 cp_parser_objc_method_signature (cp_parser* parser)
19740 {
19741   tree rettype, kwdparms, optparms;
19742   bool ellipsis = false;
19743
19744   cp_parser_objc_method_type (parser);
19745   rettype = cp_parser_objc_typename (parser);
19746   kwdparms = cp_parser_objc_method_keyword_params (parser);
19747   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19748
19749   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19750 }
19751
19752 /* Pars an Objective-C method prototype list.  */
19753
19754 static void
19755 cp_parser_objc_method_prototype_list (cp_parser* parser)
19756 {
19757   cp_token *token = cp_lexer_peek_token (parser->lexer);
19758
19759   while (token->keyword != RID_AT_END)
19760     {
19761       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19762         {
19763           objc_add_method_declaration
19764            (cp_parser_objc_method_signature (parser));
19765           cp_parser_consume_semicolon_at_end_of_statement (parser);
19766         }
19767       else
19768         /* Allow for interspersed non-ObjC++ code.  */
19769         cp_parser_objc_interstitial_code (parser);
19770
19771       token = cp_lexer_peek_token (parser->lexer);
19772     }
19773
19774   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19775   objc_finish_interface ();
19776 }
19777
19778 /* Parse an Objective-C method definition list.  */
19779
19780 static void
19781 cp_parser_objc_method_definition_list (cp_parser* parser)
19782 {
19783   cp_token *token = cp_lexer_peek_token (parser->lexer);
19784
19785   while (token->keyword != RID_AT_END)
19786     {
19787       tree meth;
19788
19789       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19790         {
19791           push_deferring_access_checks (dk_deferred);
19792           objc_start_method_definition
19793            (cp_parser_objc_method_signature (parser));
19794
19795           /* For historical reasons, we accept an optional semicolon.  */
19796           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19797             cp_lexer_consume_token (parser->lexer);
19798
19799           perform_deferred_access_checks ();
19800           stop_deferring_access_checks ();
19801           meth = cp_parser_function_definition_after_declarator (parser,
19802                                                                  false);
19803           pop_deferring_access_checks ();
19804           objc_finish_method_definition (meth);
19805         }
19806       else
19807         /* Allow for interspersed non-ObjC++ code.  */
19808         cp_parser_objc_interstitial_code (parser);
19809
19810       token = cp_lexer_peek_token (parser->lexer);
19811     }
19812
19813   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19814   objc_finish_implementation ();
19815 }
19816
19817 /* Parse Objective-C ivars.  */
19818
19819 static void
19820 cp_parser_objc_class_ivars (cp_parser* parser)
19821 {
19822   cp_token *token = cp_lexer_peek_token (parser->lexer);
19823
19824   if (token->type != CPP_OPEN_BRACE)
19825     return;     /* No ivars specified.  */
19826
19827   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19828   token = cp_lexer_peek_token (parser->lexer);
19829
19830   while (token->type != CPP_CLOSE_BRACE)
19831     {
19832       cp_decl_specifier_seq declspecs;
19833       int decl_class_or_enum_p;
19834       tree prefix_attributes;
19835
19836       cp_parser_objc_visibility_spec (parser);
19837
19838       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19839         break;
19840
19841       cp_parser_decl_specifier_seq (parser,
19842                                     CP_PARSER_FLAGS_OPTIONAL,
19843                                     &declspecs,
19844                                     &decl_class_or_enum_p);
19845       prefix_attributes = declspecs.attributes;
19846       declspecs.attributes = NULL_TREE;
19847
19848       /* Keep going until we hit the `;' at the end of the
19849          declaration.  */
19850       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19851         {
19852           tree width = NULL_TREE, attributes, first_attribute, decl;
19853           cp_declarator *declarator = NULL;
19854           int ctor_dtor_or_conv_p;
19855
19856           /* Check for a (possibly unnamed) bitfield declaration.  */
19857           token = cp_lexer_peek_token (parser->lexer);
19858           if (token->type == CPP_COLON)
19859             goto eat_colon;
19860
19861           if (token->type == CPP_NAME
19862               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19863                   == CPP_COLON))
19864             {
19865               /* Get the name of the bitfield.  */
19866               declarator = make_id_declarator (NULL_TREE,
19867                                                cp_parser_identifier (parser),
19868                                                sfk_none);
19869
19870              eat_colon:
19871               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19872               /* Get the width of the bitfield.  */
19873               width
19874                 = cp_parser_constant_expression (parser,
19875                                                  /*allow_non_constant=*/false,
19876                                                  NULL);
19877             }
19878           else
19879             {
19880               /* Parse the declarator.  */
19881               declarator
19882                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19883                                         &ctor_dtor_or_conv_p,
19884                                         /*parenthesized_p=*/NULL,
19885                                         /*member_p=*/false);
19886             }
19887
19888           /* Look for attributes that apply to the ivar.  */
19889           attributes = cp_parser_attributes_opt (parser);
19890           /* Remember which attributes are prefix attributes and
19891              which are not.  */
19892           first_attribute = attributes;
19893           /* Combine the attributes.  */
19894           attributes = chainon (prefix_attributes, attributes);
19895
19896           if (width)
19897               /* Create the bitfield declaration.  */
19898               decl = grokbitfield (declarator, &declspecs,
19899                                    width,
19900                                    attributes);
19901           else
19902             decl = grokfield (declarator, &declspecs,
19903                               NULL_TREE, /*init_const_expr_p=*/false,
19904                               NULL_TREE, attributes);
19905
19906           /* Add the instance variable.  */
19907           objc_add_instance_variable (decl);
19908
19909           /* Reset PREFIX_ATTRIBUTES.  */
19910           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19911             attributes = TREE_CHAIN (attributes);
19912           if (attributes)
19913             TREE_CHAIN (attributes) = NULL_TREE;
19914
19915           token = cp_lexer_peek_token (parser->lexer);
19916
19917           if (token->type == CPP_COMMA)
19918             {
19919               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19920               continue;
19921             }
19922           break;
19923         }
19924
19925       cp_parser_consume_semicolon_at_end_of_statement (parser);
19926       token = cp_lexer_peek_token (parser->lexer);
19927     }
19928
19929   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19930   /* For historical reasons, we accept an optional semicolon.  */
19931   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19932     cp_lexer_consume_token (parser->lexer);
19933 }
19934
19935 /* Parse an Objective-C protocol declaration.  */
19936
19937 static void
19938 cp_parser_objc_protocol_declaration (cp_parser* parser)
19939 {
19940   tree proto, protorefs;
19941   cp_token *tok;
19942
19943   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19944   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19945     {
19946       tok = cp_lexer_peek_token (parser->lexer);
19947       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19948       goto finish;
19949     }
19950
19951   /* See if we have a forward declaration or a definition.  */
19952   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19953
19954   /* Try a forward declaration first.  */
19955   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19956     {
19957       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19958      finish:
19959       cp_parser_consume_semicolon_at_end_of_statement (parser);
19960     }
19961
19962   /* Ok, we got a full-fledged definition (or at least should).  */
19963   else
19964     {
19965       proto = cp_parser_identifier (parser);
19966       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19967       objc_start_protocol (proto, protorefs);
19968       cp_parser_objc_method_prototype_list (parser);
19969     }
19970 }
19971
19972 /* Parse an Objective-C superclass or category.  */
19973
19974 static void
19975 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19976                                                           tree *categ)
19977 {
19978   cp_token *next = cp_lexer_peek_token (parser->lexer);
19979
19980   *super = *categ = NULL_TREE;
19981   if (next->type == CPP_COLON)
19982     {
19983       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19984       *super = cp_parser_identifier (parser);
19985     }
19986   else if (next->type == CPP_OPEN_PAREN)
19987     {
19988       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19989       *categ = cp_parser_identifier (parser);
19990       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19991     }
19992 }
19993
19994 /* Parse an Objective-C class interface.  */
19995
19996 static void
19997 cp_parser_objc_class_interface (cp_parser* parser)
19998 {
19999   tree name, super, categ, protos;
20000
20001   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20002   name = cp_parser_identifier (parser);
20003   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20004   protos = cp_parser_objc_protocol_refs_opt (parser);
20005
20006   /* We have either a class or a category on our hands.  */
20007   if (categ)
20008     objc_start_category_interface (name, categ, protos);
20009   else
20010     {
20011       objc_start_class_interface (name, super, protos);
20012       /* Handle instance variable declarations, if any.  */
20013       cp_parser_objc_class_ivars (parser);
20014       objc_continue_interface ();
20015     }
20016
20017   cp_parser_objc_method_prototype_list (parser);
20018 }
20019
20020 /* Parse an Objective-C class implementation.  */
20021
20022 static void
20023 cp_parser_objc_class_implementation (cp_parser* parser)
20024 {
20025   tree name, super, categ;
20026
20027   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20028   name = cp_parser_identifier (parser);
20029   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20030
20031   /* We have either a class or a category on our hands.  */
20032   if (categ)
20033     objc_start_category_implementation (name, categ);
20034   else
20035     {
20036       objc_start_class_implementation (name, super);
20037       /* Handle instance variable declarations, if any.  */
20038       cp_parser_objc_class_ivars (parser);
20039       objc_continue_implementation ();
20040     }
20041
20042   cp_parser_objc_method_definition_list (parser);
20043 }
20044
20045 /* Consume the @end token and finish off the implementation.  */
20046
20047 static void
20048 cp_parser_objc_end_implementation (cp_parser* parser)
20049 {
20050   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20051   objc_finish_implementation ();
20052 }
20053
20054 /* Parse an Objective-C declaration.  */
20055
20056 static void
20057 cp_parser_objc_declaration (cp_parser* parser)
20058 {
20059   /* Try to figure out what kind of declaration is present.  */
20060   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20061
20062   switch (kwd->keyword)
20063     {
20064     case RID_AT_ALIAS:
20065       cp_parser_objc_alias_declaration (parser);
20066       break;
20067     case RID_AT_CLASS:
20068       cp_parser_objc_class_declaration (parser);
20069       break;
20070     case RID_AT_PROTOCOL:
20071       cp_parser_objc_protocol_declaration (parser);
20072       break;
20073     case RID_AT_INTERFACE:
20074       cp_parser_objc_class_interface (parser);
20075       break;
20076     case RID_AT_IMPLEMENTATION:
20077       cp_parser_objc_class_implementation (parser);
20078       break;
20079     case RID_AT_END:
20080       cp_parser_objc_end_implementation (parser);
20081       break;
20082     default:
20083       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20084              &kwd->location, kwd->u.value);
20085       cp_parser_skip_to_end_of_block_or_statement (parser);
20086     }
20087 }
20088
20089 /* Parse an Objective-C try-catch-finally statement.
20090
20091    objc-try-catch-finally-stmt:
20092      @try compound-statement objc-catch-clause-seq [opt]
20093        objc-finally-clause [opt]
20094
20095    objc-catch-clause-seq:
20096      objc-catch-clause objc-catch-clause-seq [opt]
20097
20098    objc-catch-clause:
20099      @catch ( exception-declaration ) compound-statement
20100
20101    objc-finally-clause
20102      @finally compound-statement
20103
20104    Returns NULL_TREE.  */
20105
20106 static tree
20107 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20108   location_t location;
20109   tree stmt;
20110
20111   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20112   location = cp_lexer_peek_token (parser->lexer)->location;
20113   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20114      node, lest it get absorbed into the surrounding block.  */
20115   stmt = push_stmt_list ();
20116   cp_parser_compound_statement (parser, NULL, false);
20117   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20118
20119   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20120     {
20121       cp_parameter_declarator *parmdecl;
20122       tree parm;
20123
20124       cp_lexer_consume_token (parser->lexer);
20125       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20126       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20127       parm = grokdeclarator (parmdecl->declarator,
20128                              &parmdecl->decl_specifiers,
20129                              PARM, /*initialized=*/0,
20130                              /*attrlist=*/NULL);
20131       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20132       objc_begin_catch_clause (parm);
20133       cp_parser_compound_statement (parser, NULL, false);
20134       objc_finish_catch_clause ();
20135     }
20136
20137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20138     {
20139       cp_lexer_consume_token (parser->lexer);
20140       location = cp_lexer_peek_token (parser->lexer)->location;
20141       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20142          node, lest it get absorbed into the surrounding block.  */
20143       stmt = push_stmt_list ();
20144       cp_parser_compound_statement (parser, NULL, false);
20145       objc_build_finally_clause (location, pop_stmt_list (stmt));
20146     }
20147
20148   return objc_finish_try_stmt ();
20149 }
20150
20151 /* Parse an Objective-C synchronized statement.
20152
20153    objc-synchronized-stmt:
20154      @synchronized ( expression ) compound-statement
20155
20156    Returns NULL_TREE.  */
20157
20158 static tree
20159 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20160   location_t location;
20161   tree lock, stmt;
20162
20163   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20164
20165   location = cp_lexer_peek_token (parser->lexer)->location;
20166   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20167   lock = cp_parser_expression (parser, false, NULL);
20168   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20169
20170   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20171      node, lest it get absorbed into the surrounding block.  */
20172   stmt = push_stmt_list ();
20173   cp_parser_compound_statement (parser, NULL, false);
20174
20175   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20176 }
20177
20178 /* Parse an Objective-C throw statement.
20179
20180    objc-throw-stmt:
20181      @throw assignment-expression [opt] ;
20182
20183    Returns a constructed '@throw' statement.  */
20184
20185 static tree
20186 cp_parser_objc_throw_statement (cp_parser *parser) {
20187   tree expr = NULL_TREE;
20188
20189   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20190
20191   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20192     expr = cp_parser_assignment_expression (parser, false, NULL);
20193
20194   cp_parser_consume_semicolon_at_end_of_statement (parser);
20195
20196   return objc_build_throw_stmt (expr);
20197 }
20198
20199 /* Parse an Objective-C statement.  */
20200
20201 static tree
20202 cp_parser_objc_statement (cp_parser * parser) {
20203   /* Try to figure out what kind of declaration is present.  */
20204   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20205
20206   switch (kwd->keyword)
20207     {
20208     case RID_AT_TRY:
20209       return cp_parser_objc_try_catch_finally_statement (parser);
20210     case RID_AT_SYNCHRONIZED:
20211       return cp_parser_objc_synchronized_statement (parser);
20212     case RID_AT_THROW:
20213       return cp_parser_objc_throw_statement (parser);
20214     default:
20215       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20216              &kwd->location, kwd->u.value);
20217       cp_parser_skip_to_end_of_block_or_statement (parser);
20218     }
20219
20220   return error_mark_node;
20221 }
20222 \f
20223 /* OpenMP 2.5 parsing routines.  */
20224
20225 /* Returns name of the next clause.
20226    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20227    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20228    returned and the token is consumed.  */
20229
20230 static pragma_omp_clause
20231 cp_parser_omp_clause_name (cp_parser *parser)
20232 {
20233   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20234
20235   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20236     result = PRAGMA_OMP_CLAUSE_IF;
20237   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20238     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20239   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20240     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20241   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20242     {
20243       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20244       const char *p = IDENTIFIER_POINTER (id);
20245
20246       switch (p[0])
20247         {
20248         case 'c':
20249           if (!strcmp ("collapse", p))
20250             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20251           else if (!strcmp ("copyin", p))
20252             result = PRAGMA_OMP_CLAUSE_COPYIN;
20253           else if (!strcmp ("copyprivate", p))
20254             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20255           break;
20256         case 'f':
20257           if (!strcmp ("firstprivate", p))
20258             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20259           break;
20260         case 'l':
20261           if (!strcmp ("lastprivate", p))
20262             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20263           break;
20264         case 'n':
20265           if (!strcmp ("nowait", p))
20266             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20267           else if (!strcmp ("num_threads", p))
20268             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20269           break;
20270         case 'o':
20271           if (!strcmp ("ordered", p))
20272             result = PRAGMA_OMP_CLAUSE_ORDERED;
20273           break;
20274         case 'r':
20275           if (!strcmp ("reduction", p))
20276             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20277           break;
20278         case 's':
20279           if (!strcmp ("schedule", p))
20280             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20281           else if (!strcmp ("shared", p))
20282             result = PRAGMA_OMP_CLAUSE_SHARED;
20283           break;
20284         case 'u':
20285           if (!strcmp ("untied", p))
20286             result = PRAGMA_OMP_CLAUSE_UNTIED;
20287           break;
20288         }
20289     }
20290
20291   if (result != PRAGMA_OMP_CLAUSE_NONE)
20292     cp_lexer_consume_token (parser->lexer);
20293
20294   return result;
20295 }
20296
20297 /* Validate that a clause of the given type does not already exist.  */
20298
20299 static void
20300 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20301                            const char *name, location_t location)
20302 {
20303   tree c;
20304
20305   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20306     if (OMP_CLAUSE_CODE (c) == code)
20307       {
20308         error ("%Htoo many %qs clauses", &location, name);
20309         break;
20310       }
20311 }
20312
20313 /* OpenMP 2.5:
20314    variable-list:
20315      identifier
20316      variable-list , identifier
20317
20318    In addition, we match a closing parenthesis.  An opening parenthesis
20319    will have been consumed by the caller.
20320
20321    If KIND is nonzero, create the appropriate node and install the decl
20322    in OMP_CLAUSE_DECL and add the node to the head of the list.
20323
20324    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20325    return the list created.  */
20326
20327 static tree
20328 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20329                                 tree list)
20330 {
20331   cp_token *token;
20332   while (1)
20333     {
20334       tree name, decl;
20335
20336       token = cp_lexer_peek_token (parser->lexer);
20337       name = cp_parser_id_expression (parser, /*template_p=*/false,
20338                                       /*check_dependency_p=*/true,
20339                                       /*template_p=*/NULL,
20340                                       /*declarator_p=*/false,
20341                                       /*optional_p=*/false);
20342       if (name == error_mark_node)
20343         goto skip_comma;
20344
20345       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20346       if (decl == error_mark_node)
20347         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20348       else if (kind != 0)
20349         {
20350           tree u = build_omp_clause (kind);
20351           OMP_CLAUSE_DECL (u) = decl;
20352           OMP_CLAUSE_CHAIN (u) = list;
20353           list = u;
20354         }
20355       else
20356         list = tree_cons (decl, NULL_TREE, list);
20357
20358     get_comma:
20359       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20360         break;
20361       cp_lexer_consume_token (parser->lexer);
20362     }
20363
20364   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20365     {
20366       int ending;
20367
20368       /* Try to resync to an unnested comma.  Copied from
20369          cp_parser_parenthesized_expression_list.  */
20370     skip_comma:
20371       ending = cp_parser_skip_to_closing_parenthesis (parser,
20372                                                       /*recovering=*/true,
20373                                                       /*or_comma=*/true,
20374                                                       /*consume_paren=*/true);
20375       if (ending < 0)
20376         goto get_comma;
20377     }
20378
20379   return list;
20380 }
20381
20382 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20383    common case for omp clauses.  */
20384
20385 static tree
20386 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20387 {
20388   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20389     return cp_parser_omp_var_list_no_open (parser, kind, list);
20390   return list;
20391 }
20392
20393 /* OpenMP 3.0:
20394    collapse ( constant-expression ) */
20395
20396 static tree
20397 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20398 {
20399   tree c, num;
20400   location_t loc;
20401   HOST_WIDE_INT n;
20402
20403   loc = cp_lexer_peek_token (parser->lexer)->location;
20404   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20405     return list;
20406
20407   num = cp_parser_constant_expression (parser, false, NULL);
20408
20409   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20410     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20411                                            /*or_comma=*/false,
20412                                            /*consume_paren=*/true);
20413
20414   if (num == error_mark_node)
20415     return list;
20416   num = fold_non_dependent_expr (num);
20417   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20418       || !host_integerp (num, 0)
20419       || (n = tree_low_cst (num, 0)) <= 0
20420       || (int) n != n)
20421     {
20422       error ("%Hcollapse argument needs positive constant integer expression",
20423              &loc);
20424       return list;
20425     }
20426
20427   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20428   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20429   OMP_CLAUSE_CHAIN (c) = list;
20430   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20431
20432   return c;
20433 }
20434
20435 /* OpenMP 2.5:
20436    default ( shared | none ) */
20437
20438 static tree
20439 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20440 {
20441   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20442   tree c;
20443
20444   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20445     return list;
20446   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20447     {
20448       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20449       const char *p = IDENTIFIER_POINTER (id);
20450
20451       switch (p[0])
20452         {
20453         case 'n':
20454           if (strcmp ("none", p) != 0)
20455             goto invalid_kind;
20456           kind = OMP_CLAUSE_DEFAULT_NONE;
20457           break;
20458
20459         case 's':
20460           if (strcmp ("shared", p) != 0)
20461             goto invalid_kind;
20462           kind = OMP_CLAUSE_DEFAULT_SHARED;
20463           break;
20464
20465         default:
20466           goto invalid_kind;
20467         }
20468
20469       cp_lexer_consume_token (parser->lexer);
20470     }
20471   else
20472     {
20473     invalid_kind:
20474       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20475     }
20476
20477   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20478     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20479                                            /*or_comma=*/false,
20480                                            /*consume_paren=*/true);
20481
20482   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20483     return list;
20484
20485   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20486   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20487   OMP_CLAUSE_CHAIN (c) = list;
20488   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20489
20490   return c;
20491 }
20492
20493 /* OpenMP 2.5:
20494    if ( expression ) */
20495
20496 static tree
20497 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20498 {
20499   tree t, c;
20500
20501   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20502     return list;
20503
20504   t = cp_parser_condition (parser);
20505
20506   if (t == error_mark_node
20507       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20508     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20509                                            /*or_comma=*/false,
20510                                            /*consume_paren=*/true);
20511
20512   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20513
20514   c = build_omp_clause (OMP_CLAUSE_IF);
20515   OMP_CLAUSE_IF_EXPR (c) = t;
20516   OMP_CLAUSE_CHAIN (c) = list;
20517
20518   return c;
20519 }
20520
20521 /* OpenMP 2.5:
20522    nowait */
20523
20524 static tree
20525 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20526                              tree list, location_t location)
20527 {
20528   tree c;
20529
20530   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20531
20532   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20533   OMP_CLAUSE_CHAIN (c) = list;
20534   return c;
20535 }
20536
20537 /* OpenMP 2.5:
20538    num_threads ( expression ) */
20539
20540 static tree
20541 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20542                                   location_t location)
20543 {
20544   tree t, c;
20545
20546   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20547     return list;
20548
20549   t = cp_parser_expression (parser, false, NULL);
20550
20551   if (t == error_mark_node
20552       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20553     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20554                                            /*or_comma=*/false,
20555                                            /*consume_paren=*/true);
20556
20557   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20558                              "num_threads", location);
20559
20560   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20561   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20562   OMP_CLAUSE_CHAIN (c) = list;
20563
20564   return c;
20565 }
20566
20567 /* OpenMP 2.5:
20568    ordered */
20569
20570 static tree
20571 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20572                               tree list, location_t location)
20573 {
20574   tree c;
20575
20576   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20577                              "ordered", location);
20578
20579   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20580   OMP_CLAUSE_CHAIN (c) = list;
20581   return c;
20582 }
20583
20584 /* OpenMP 2.5:
20585    reduction ( reduction-operator : variable-list )
20586
20587    reduction-operator:
20588      One of: + * - & ^ | && || */
20589
20590 static tree
20591 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20592 {
20593   enum tree_code code;
20594   tree nlist, c;
20595
20596   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20597     return list;
20598
20599   switch (cp_lexer_peek_token (parser->lexer)->type)
20600     {
20601     case CPP_PLUS:
20602       code = PLUS_EXPR;
20603       break;
20604     case CPP_MULT:
20605       code = MULT_EXPR;
20606       break;
20607     case CPP_MINUS:
20608       code = MINUS_EXPR;
20609       break;
20610     case CPP_AND:
20611       code = BIT_AND_EXPR;
20612       break;
20613     case CPP_XOR:
20614       code = BIT_XOR_EXPR;
20615       break;
20616     case CPP_OR:
20617       code = BIT_IOR_EXPR;
20618       break;
20619     case CPP_AND_AND:
20620       code = TRUTH_ANDIF_EXPR;
20621       break;
20622     case CPP_OR_OR:
20623       code = TRUTH_ORIF_EXPR;
20624       break;
20625     default:
20626       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20627                                "%<|%>, %<&&%>, or %<||%>");
20628     resync_fail:
20629       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20630                                              /*or_comma=*/false,
20631                                              /*consume_paren=*/true);
20632       return list;
20633     }
20634   cp_lexer_consume_token (parser->lexer);
20635
20636   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20637     goto resync_fail;
20638
20639   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20640   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20641     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20642
20643   return nlist;
20644 }
20645
20646 /* OpenMP 2.5:
20647    schedule ( schedule-kind )
20648    schedule ( schedule-kind , expression )
20649
20650    schedule-kind:
20651      static | dynamic | guided | runtime | auto  */
20652
20653 static tree
20654 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20655 {
20656   tree c, t;
20657
20658   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20659     return list;
20660
20661   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20662
20663   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20664     {
20665       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20666       const char *p = IDENTIFIER_POINTER (id);
20667
20668       switch (p[0])
20669         {
20670         case 'd':
20671           if (strcmp ("dynamic", p) != 0)
20672             goto invalid_kind;
20673           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20674           break;
20675
20676         case 'g':
20677           if (strcmp ("guided", p) != 0)
20678             goto invalid_kind;
20679           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20680           break;
20681
20682         case 'r':
20683           if (strcmp ("runtime", p) != 0)
20684             goto invalid_kind;
20685           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20686           break;
20687
20688         default:
20689           goto invalid_kind;
20690         }
20691     }
20692   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20693     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20694   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20695     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20696   else
20697     goto invalid_kind;
20698   cp_lexer_consume_token (parser->lexer);
20699
20700   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20701     {
20702       cp_token *token;
20703       cp_lexer_consume_token (parser->lexer);
20704
20705       token = cp_lexer_peek_token (parser->lexer);
20706       t = cp_parser_assignment_expression (parser, false, NULL);
20707
20708       if (t == error_mark_node)
20709         goto resync_fail;
20710       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20711         error ("%Hschedule %<runtime%> does not take "
20712                "a %<chunk_size%> parameter", &token->location);
20713       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20714         error ("%Hschedule %<auto%> does not take "
20715                "a %<chunk_size%> parameter", &token->location);
20716       else
20717         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20718
20719       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20720         goto resync_fail;
20721     }
20722   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20723     goto resync_fail;
20724
20725   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20726   OMP_CLAUSE_CHAIN (c) = list;
20727   return c;
20728
20729  invalid_kind:
20730   cp_parser_error (parser, "invalid schedule kind");
20731  resync_fail:
20732   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20733                                          /*or_comma=*/false,
20734                                          /*consume_paren=*/true);
20735   return list;
20736 }
20737
20738 /* OpenMP 3.0:
20739    untied */
20740
20741 static tree
20742 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20743                              tree list, location_t location)
20744 {
20745   tree c;
20746
20747   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20748
20749   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20750   OMP_CLAUSE_CHAIN (c) = list;
20751   return c;
20752 }
20753
20754 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20755    is a bitmask in MASK.  Return the list of clauses found; the result
20756    of clause default goes in *pdefault.  */
20757
20758 static tree
20759 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20760                            const char *where, cp_token *pragma_tok)
20761 {
20762   tree clauses = NULL;
20763   bool first = true;
20764   cp_token *token = NULL;
20765
20766   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20767     {
20768       pragma_omp_clause c_kind;
20769       const char *c_name;
20770       tree prev = clauses;
20771
20772       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20773         cp_lexer_consume_token (parser->lexer);
20774
20775       token = cp_lexer_peek_token (parser->lexer);
20776       c_kind = cp_parser_omp_clause_name (parser);
20777       first = false;
20778
20779       switch (c_kind)
20780         {
20781         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20782           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20783                                                    token->location);
20784           c_name = "collapse";
20785           break;
20786         case PRAGMA_OMP_CLAUSE_COPYIN:
20787           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20788           c_name = "copyin";
20789           break;
20790         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20791           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20792                                             clauses);
20793           c_name = "copyprivate";
20794           break;
20795         case PRAGMA_OMP_CLAUSE_DEFAULT:
20796           clauses = cp_parser_omp_clause_default (parser, clauses,
20797                                                   token->location);
20798           c_name = "default";
20799           break;
20800         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20801           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20802                                             clauses);
20803           c_name = "firstprivate";
20804           break;
20805         case PRAGMA_OMP_CLAUSE_IF:
20806           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20807           c_name = "if";
20808           break;
20809         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20810           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20811                                             clauses);
20812           c_name = "lastprivate";
20813           break;
20814         case PRAGMA_OMP_CLAUSE_NOWAIT:
20815           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20816           c_name = "nowait";
20817           break;
20818         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20819           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20820                                                       token->location);
20821           c_name = "num_threads";
20822           break;
20823         case PRAGMA_OMP_CLAUSE_ORDERED:
20824           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20825                                                   token->location);
20826           c_name = "ordered";
20827           break;
20828         case PRAGMA_OMP_CLAUSE_PRIVATE:
20829           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20830                                             clauses);
20831           c_name = "private";
20832           break;
20833         case PRAGMA_OMP_CLAUSE_REDUCTION:
20834           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20835           c_name = "reduction";
20836           break;
20837         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20838           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20839                                                    token->location);
20840           c_name = "schedule";
20841           break;
20842         case PRAGMA_OMP_CLAUSE_SHARED:
20843           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20844                                             clauses);
20845           c_name = "shared";
20846           break;
20847         case PRAGMA_OMP_CLAUSE_UNTIED:
20848           clauses = cp_parser_omp_clause_untied (parser, clauses,
20849                                                  token->location);
20850           c_name = "nowait";
20851           break;
20852         default:
20853           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20854           goto saw_error;
20855         }
20856
20857       if (((mask >> c_kind) & 1) == 0)
20858         {
20859           /* Remove the invalid clause(s) from the list to avoid
20860              confusing the rest of the compiler.  */
20861           clauses = prev;
20862           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20863         }
20864     }
20865  saw_error:
20866   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20867   return finish_omp_clauses (clauses);
20868 }
20869
20870 /* OpenMP 2.5:
20871    structured-block:
20872      statement
20873
20874    In practice, we're also interested in adding the statement to an
20875    outer node.  So it is convenient if we work around the fact that
20876    cp_parser_statement calls add_stmt.  */
20877
20878 static unsigned
20879 cp_parser_begin_omp_structured_block (cp_parser *parser)
20880 {
20881   unsigned save = parser->in_statement;
20882
20883   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20884      This preserves the "not within loop or switch" style error messages
20885      for nonsense cases like
20886         void foo() {
20887         #pragma omp single
20888           break;
20889         }
20890   */
20891   if (parser->in_statement)
20892     parser->in_statement = IN_OMP_BLOCK;
20893
20894   return save;
20895 }
20896
20897 static void
20898 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20899 {
20900   parser->in_statement = save;
20901 }
20902
20903 static tree
20904 cp_parser_omp_structured_block (cp_parser *parser)
20905 {
20906   tree stmt = begin_omp_structured_block ();
20907   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20908
20909   cp_parser_statement (parser, NULL_TREE, false, NULL);
20910
20911   cp_parser_end_omp_structured_block (parser, save);
20912   return finish_omp_structured_block (stmt);
20913 }
20914
20915 /* OpenMP 2.5:
20916    # pragma omp atomic new-line
20917      expression-stmt
20918
20919    expression-stmt:
20920      x binop= expr | x++ | ++x | x-- | --x
20921    binop:
20922      +, *, -, /, &, ^, |, <<, >>
20923
20924   where x is an lvalue expression with scalar type.  */
20925
20926 static void
20927 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20928 {
20929   tree lhs, rhs;
20930   enum tree_code code;
20931
20932   cp_parser_require_pragma_eol (parser, pragma_tok);
20933
20934   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20935                                     /*cast_p=*/false, NULL);
20936   switch (TREE_CODE (lhs))
20937     {
20938     case ERROR_MARK:
20939       goto saw_error;
20940
20941     case PREINCREMENT_EXPR:
20942     case POSTINCREMENT_EXPR:
20943       lhs = TREE_OPERAND (lhs, 0);
20944       code = PLUS_EXPR;
20945       rhs = integer_one_node;
20946       break;
20947
20948     case PREDECREMENT_EXPR:
20949     case POSTDECREMENT_EXPR:
20950       lhs = TREE_OPERAND (lhs, 0);
20951       code = MINUS_EXPR;
20952       rhs = integer_one_node;
20953       break;
20954
20955     default:
20956       switch (cp_lexer_peek_token (parser->lexer)->type)
20957         {
20958         case CPP_MULT_EQ:
20959           code = MULT_EXPR;
20960           break;
20961         case CPP_DIV_EQ:
20962           code = TRUNC_DIV_EXPR;
20963           break;
20964         case CPP_PLUS_EQ:
20965           code = PLUS_EXPR;
20966           break;
20967         case CPP_MINUS_EQ:
20968           code = MINUS_EXPR;
20969           break;
20970         case CPP_LSHIFT_EQ:
20971           code = LSHIFT_EXPR;
20972           break;
20973         case CPP_RSHIFT_EQ:
20974           code = RSHIFT_EXPR;
20975           break;
20976         case CPP_AND_EQ:
20977           code = BIT_AND_EXPR;
20978           break;
20979         case CPP_OR_EQ:
20980           code = BIT_IOR_EXPR;
20981           break;
20982         case CPP_XOR_EQ:
20983           code = BIT_XOR_EXPR;
20984           break;
20985         default:
20986           cp_parser_error (parser,
20987                            "invalid operator for %<#pragma omp atomic%>");
20988           goto saw_error;
20989         }
20990       cp_lexer_consume_token (parser->lexer);
20991
20992       rhs = cp_parser_expression (parser, false, NULL);
20993       if (rhs == error_mark_node)
20994         goto saw_error;
20995       break;
20996     }
20997   finish_omp_atomic (code, lhs, rhs);
20998   cp_parser_consume_semicolon_at_end_of_statement (parser);
20999   return;
21000
21001  saw_error:
21002   cp_parser_skip_to_end_of_block_or_statement (parser);
21003 }
21004
21005
21006 /* OpenMP 2.5:
21007    # pragma omp barrier new-line  */
21008
21009 static void
21010 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21011 {
21012   cp_parser_require_pragma_eol (parser, pragma_tok);
21013   finish_omp_barrier ();
21014 }
21015
21016 /* OpenMP 2.5:
21017    # pragma omp critical [(name)] new-line
21018      structured-block  */
21019
21020 static tree
21021 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21022 {
21023   tree stmt, name = NULL;
21024
21025   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21026     {
21027       cp_lexer_consume_token (parser->lexer);
21028
21029       name = cp_parser_identifier (parser);
21030
21031       if (name == error_mark_node
21032           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21033         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21034                                                /*or_comma=*/false,
21035                                                /*consume_paren=*/true);
21036       if (name == error_mark_node)
21037         name = NULL;
21038     }
21039   cp_parser_require_pragma_eol (parser, pragma_tok);
21040
21041   stmt = cp_parser_omp_structured_block (parser);
21042   return c_finish_omp_critical (stmt, name);
21043 }
21044
21045 /* OpenMP 2.5:
21046    # pragma omp flush flush-vars[opt] new-line
21047
21048    flush-vars:
21049      ( variable-list ) */
21050
21051 static void
21052 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21053 {
21054   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21055     (void) cp_parser_omp_var_list (parser, 0, NULL);
21056   cp_parser_require_pragma_eol (parser, pragma_tok);
21057
21058   finish_omp_flush ();
21059 }
21060
21061 /* Helper function, to parse omp for increment expression.  */
21062
21063 static tree
21064 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21065 {
21066   tree cond = cp_parser_binary_expression (parser, false, true,
21067                                            PREC_NOT_OPERATOR, NULL);
21068   bool overloaded_p;
21069
21070   if (cond == error_mark_node
21071       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21072     {
21073       cp_parser_skip_to_end_of_statement (parser);
21074       return error_mark_node;
21075     }
21076
21077   switch (TREE_CODE (cond))
21078     {
21079     case GT_EXPR:
21080     case GE_EXPR:
21081     case LT_EXPR:
21082     case LE_EXPR:
21083       break;
21084     default:
21085       return error_mark_node;
21086     }
21087
21088   /* If decl is an iterator, preserve LHS and RHS of the relational
21089      expr until finish_omp_for.  */
21090   if (decl
21091       && (type_dependent_expression_p (decl)
21092           || CLASS_TYPE_P (TREE_TYPE (decl))))
21093     return cond;
21094
21095   return build_x_binary_op (TREE_CODE (cond),
21096                             TREE_OPERAND (cond, 0), ERROR_MARK,
21097                             TREE_OPERAND (cond, 1), ERROR_MARK,
21098                             &overloaded_p, tf_warning_or_error);
21099 }
21100
21101 /* Helper function, to parse omp for increment expression.  */
21102
21103 static tree
21104 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21105 {
21106   cp_token *token = cp_lexer_peek_token (parser->lexer);
21107   enum tree_code op;
21108   tree lhs, rhs;
21109   cp_id_kind idk;
21110   bool decl_first;
21111
21112   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21113     {
21114       op = (token->type == CPP_PLUS_PLUS
21115             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21116       cp_lexer_consume_token (parser->lexer);
21117       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21118       if (lhs != decl)
21119         return error_mark_node;
21120       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21121     }
21122
21123   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21124   if (lhs != decl)
21125     return error_mark_node;
21126
21127   token = cp_lexer_peek_token (parser->lexer);
21128   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21129     {
21130       op = (token->type == CPP_PLUS_PLUS
21131             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21132       cp_lexer_consume_token (parser->lexer);
21133       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21134     }
21135
21136   op = cp_parser_assignment_operator_opt (parser);
21137   if (op == ERROR_MARK)
21138     return error_mark_node;
21139
21140   if (op != NOP_EXPR)
21141     {
21142       rhs = cp_parser_assignment_expression (parser, false, NULL);
21143       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21144       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21145     }
21146
21147   lhs = cp_parser_binary_expression (parser, false, false,
21148                                      PREC_ADDITIVE_EXPRESSION, NULL);
21149   token = cp_lexer_peek_token (parser->lexer);
21150   decl_first = lhs == decl;
21151   if (decl_first)
21152     lhs = NULL_TREE;
21153   if (token->type != CPP_PLUS
21154       && token->type != CPP_MINUS)
21155     return error_mark_node;
21156
21157   do
21158     {
21159       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21160       cp_lexer_consume_token (parser->lexer);
21161       rhs = cp_parser_binary_expression (parser, false, false,
21162                                          PREC_ADDITIVE_EXPRESSION, NULL);
21163       token = cp_lexer_peek_token (parser->lexer);
21164       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21165         {
21166           if (lhs == NULL_TREE)
21167             {
21168               if (op == PLUS_EXPR)
21169                 lhs = rhs;
21170               else
21171                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21172             }
21173           else
21174             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21175                                      NULL, tf_warning_or_error);
21176         }
21177     }
21178   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21179
21180   if (!decl_first)
21181     {
21182       if (rhs != decl || op == MINUS_EXPR)
21183         return error_mark_node;
21184       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21185     }
21186   else
21187     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21188
21189   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21190 }
21191
21192 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21193
21194 static tree
21195 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21196 {
21197   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21198   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21199   tree this_pre_body, cl;
21200   location_t loc_first;
21201   bool collapse_err = false;
21202   int i, collapse = 1, nbraces = 0;
21203
21204   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21205     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21206       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21207
21208   gcc_assert (collapse >= 1);
21209
21210   declv = make_tree_vec (collapse);
21211   initv = make_tree_vec (collapse);
21212   condv = make_tree_vec (collapse);
21213   incrv = make_tree_vec (collapse);
21214
21215   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21216
21217   for (i = 0; i < collapse; i++)
21218     {
21219       int bracecount = 0;
21220       bool add_private_clause = false;
21221       location_t loc;
21222
21223       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21224         {
21225           cp_parser_error (parser, "for statement expected");
21226           return NULL;
21227         }
21228       loc = cp_lexer_consume_token (parser->lexer)->location;
21229
21230       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21231         return NULL;
21232
21233       init = decl = real_decl = NULL;
21234       this_pre_body = push_stmt_list ();
21235       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21236         {
21237           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21238
21239              init-expr:
21240                        var = lb
21241                        integer-type var = lb
21242                        random-access-iterator-type var = lb
21243                        pointer-type var = lb
21244           */
21245           cp_decl_specifier_seq type_specifiers;
21246
21247           /* First, try to parse as an initialized declaration.  See
21248              cp_parser_condition, from whence the bulk of this is copied.  */
21249
21250           cp_parser_parse_tentatively (parser);
21251           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21252                                         &type_specifiers);
21253           if (cp_parser_parse_definitely (parser))
21254             {
21255               /* If parsing a type specifier seq succeeded, then this
21256                  MUST be a initialized declaration.  */
21257               tree asm_specification, attributes;
21258               cp_declarator *declarator;
21259
21260               declarator = cp_parser_declarator (parser,
21261                                                  CP_PARSER_DECLARATOR_NAMED,
21262                                                  /*ctor_dtor_or_conv_p=*/NULL,
21263                                                  /*parenthesized_p=*/NULL,
21264                                                  /*member_p=*/false);
21265               attributes = cp_parser_attributes_opt (parser);
21266               asm_specification = cp_parser_asm_specification_opt (parser);
21267
21268               if (declarator == cp_error_declarator) 
21269                 cp_parser_skip_to_end_of_statement (parser);
21270
21271               else 
21272                 {
21273                   tree pushed_scope, auto_node;
21274
21275                   decl = start_decl (declarator, &type_specifiers,
21276                                      SD_INITIALIZED, attributes,
21277                                      /*prefix_attributes=*/NULL_TREE,
21278                                      &pushed_scope);
21279
21280                   auto_node = type_uses_auto (TREE_TYPE (decl));
21281                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21282                     {
21283                       if (cp_lexer_next_token_is (parser->lexer, 
21284                                                   CPP_OPEN_PAREN))
21285                         error ("parenthesized initialization is not allowed in "
21286                                "OpenMP %<for%> loop");
21287                       else
21288                         /* Trigger an error.  */
21289                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21290
21291                       init = error_mark_node;
21292                       cp_parser_skip_to_end_of_statement (parser);
21293                     }
21294                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21295                            || type_dependent_expression_p (decl)
21296                            || auto_node)
21297                     {
21298                       bool is_direct_init, is_non_constant_init;
21299
21300                       init = cp_parser_initializer (parser,
21301                                                     &is_direct_init,
21302                                                     &is_non_constant_init);
21303
21304                       if (auto_node && describable_type (init))
21305                         {
21306                           TREE_TYPE (decl)
21307                             = do_auto_deduction (TREE_TYPE (decl), init,
21308                                                  auto_node);
21309
21310                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21311                               && !type_dependent_expression_p (decl))
21312                             goto non_class;
21313                         }
21314                       
21315                       cp_finish_decl (decl, init, !is_non_constant_init,
21316                                       asm_specification,
21317                                       LOOKUP_ONLYCONVERTING);
21318                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21319                         {
21320                           for_block
21321                             = tree_cons (NULL, this_pre_body, for_block);
21322                           init = NULL_TREE;
21323                         }
21324                       else
21325                         init = pop_stmt_list (this_pre_body);
21326                       this_pre_body = NULL_TREE;
21327                     }
21328                   else
21329                     {
21330                       /* Consume '='.  */
21331                       cp_lexer_consume_token (parser->lexer);
21332                       init = cp_parser_assignment_expression (parser, false, NULL);
21333
21334                     non_class:
21335                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21336                         init = error_mark_node;
21337                       else
21338                         cp_finish_decl (decl, NULL_TREE,
21339                                         /*init_const_expr_p=*/false,
21340                                         asm_specification,
21341                                         LOOKUP_ONLYCONVERTING);
21342                     }
21343
21344                   if (pushed_scope)
21345                     pop_scope (pushed_scope);
21346                 }
21347             }
21348           else 
21349             {
21350               cp_id_kind idk;
21351               /* If parsing a type specifier sequence failed, then
21352                  this MUST be a simple expression.  */
21353               cp_parser_parse_tentatively (parser);
21354               decl = cp_parser_primary_expression (parser, false, false,
21355                                                    false, &idk);
21356               if (!cp_parser_error_occurred (parser)
21357                   && decl
21358                   && DECL_P (decl)
21359                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21360                 {
21361                   tree rhs;
21362
21363                   cp_parser_parse_definitely (parser);
21364                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21365                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21366                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21367                                                          rhs,
21368                                                          tf_warning_or_error));
21369                   add_private_clause = true;
21370                 }
21371               else
21372                 {
21373                   decl = NULL;
21374                   cp_parser_abort_tentative_parse (parser);
21375                   init = cp_parser_expression (parser, false, NULL);
21376                   if (init)
21377                     {
21378                       if (TREE_CODE (init) == MODIFY_EXPR
21379                           || TREE_CODE (init) == MODOP_EXPR)
21380                         real_decl = TREE_OPERAND (init, 0);
21381                     }
21382                 }
21383             }
21384         }
21385       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21386       if (this_pre_body)
21387         {
21388           this_pre_body = pop_stmt_list (this_pre_body);
21389           if (pre_body)
21390             {
21391               tree t = pre_body;
21392               pre_body = push_stmt_list ();
21393               add_stmt (t);
21394               add_stmt (this_pre_body);
21395               pre_body = pop_stmt_list (pre_body);
21396             }
21397           else
21398             pre_body = this_pre_body;
21399         }
21400
21401       if (decl)
21402         real_decl = decl;
21403       if (par_clauses != NULL && real_decl != NULL_TREE)
21404         {
21405           tree *c;
21406           for (c = par_clauses; *c ; )
21407             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21408                 && OMP_CLAUSE_DECL (*c) == real_decl)
21409               {
21410                 error ("%Hiteration variable %qD should not be firstprivate",
21411                        &loc, real_decl);
21412                 *c = OMP_CLAUSE_CHAIN (*c);
21413               }
21414             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21415                      && OMP_CLAUSE_DECL (*c) == real_decl)
21416               {
21417                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21418                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21419                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21420                 OMP_CLAUSE_DECL (l) = real_decl;
21421                 OMP_CLAUSE_CHAIN (l) = clauses;
21422                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21423                 clauses = l;
21424                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21425                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21426                 add_private_clause = false;
21427               }
21428             else
21429               {
21430                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21431                     && OMP_CLAUSE_DECL (*c) == real_decl)
21432                   add_private_clause = false;
21433                 c = &OMP_CLAUSE_CHAIN (*c);
21434               }
21435         }
21436
21437       if (add_private_clause)
21438         {
21439           tree c;
21440           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21441             {
21442               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21443                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21444                   && OMP_CLAUSE_DECL (c) == decl)
21445                 break;
21446               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21447                        && OMP_CLAUSE_DECL (c) == decl)
21448                 error ("%Hiteration variable %qD should not be firstprivate",
21449                        &loc, decl);
21450               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21451                        && OMP_CLAUSE_DECL (c) == decl)
21452                 error ("%Hiteration variable %qD should not be reduction",
21453                        &loc, decl);
21454             }
21455           if (c == NULL)
21456             {
21457               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21458               OMP_CLAUSE_DECL (c) = decl;
21459               c = finish_omp_clauses (c);
21460               if (c)
21461                 {
21462                   OMP_CLAUSE_CHAIN (c) = clauses;
21463                   clauses = c;
21464                 }
21465             }
21466         }
21467
21468       cond = NULL;
21469       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21470         cond = cp_parser_omp_for_cond (parser, decl);
21471       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21472
21473       incr = NULL;
21474       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21475         {
21476           /* If decl is an iterator, preserve the operator on decl
21477              until finish_omp_for.  */
21478           if (decl
21479               && (type_dependent_expression_p (decl)
21480                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21481             incr = cp_parser_omp_for_incr (parser, decl);
21482           else
21483             incr = cp_parser_expression (parser, false, NULL);
21484         }
21485
21486       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21487         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21488                                                /*or_comma=*/false,
21489                                                /*consume_paren=*/true);
21490
21491       TREE_VEC_ELT (declv, i) = decl;
21492       TREE_VEC_ELT (initv, i) = init;
21493       TREE_VEC_ELT (condv, i) = cond;
21494       TREE_VEC_ELT (incrv, i) = incr;
21495
21496       if (i == collapse - 1)
21497         break;
21498
21499       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21500          in between the collapsed for loops to be still considered perfectly
21501          nested.  Hopefully the final version clarifies this.
21502          For now handle (multiple) {'s and empty statements.  */
21503       cp_parser_parse_tentatively (parser);
21504       do
21505         {
21506           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21507             break;
21508           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21509             {
21510               cp_lexer_consume_token (parser->lexer);
21511               bracecount++;
21512             }
21513           else if (bracecount
21514                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21515             cp_lexer_consume_token (parser->lexer);
21516           else
21517             {
21518               loc = cp_lexer_peek_token (parser->lexer)->location;
21519               error ("%Hnot enough collapsed for loops", &loc);
21520               collapse_err = true;
21521               cp_parser_abort_tentative_parse (parser);
21522               declv = NULL_TREE;
21523               break;
21524             }
21525         }
21526       while (1);
21527
21528       if (declv)
21529         {
21530           cp_parser_parse_definitely (parser);
21531           nbraces += bracecount;
21532         }
21533     }
21534
21535   /* Note that we saved the original contents of this flag when we entered
21536      the structured block, and so we don't need to re-save it here.  */
21537   parser->in_statement = IN_OMP_FOR;
21538
21539   /* Note that the grammar doesn't call for a structured block here,
21540      though the loop as a whole is a structured block.  */
21541   body = push_stmt_list ();
21542   cp_parser_statement (parser, NULL_TREE, false, NULL);
21543   body = pop_stmt_list (body);
21544
21545   if (declv == NULL_TREE)
21546     ret = NULL_TREE;
21547   else
21548     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21549                           pre_body, clauses);
21550
21551   while (nbraces)
21552     {
21553       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21554         {
21555           cp_lexer_consume_token (parser->lexer);
21556           nbraces--;
21557         }
21558       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21559         cp_lexer_consume_token (parser->lexer);
21560       else
21561         {
21562           if (!collapse_err)
21563             {
21564               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21565               error ("%Hcollapsed loops not perfectly nested", &loc);
21566             }
21567           collapse_err = true;
21568           cp_parser_statement_seq_opt (parser, NULL);
21569           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21570         }
21571     }
21572
21573   while (for_block)
21574     {
21575       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21576       for_block = TREE_CHAIN (for_block);
21577     }
21578
21579   return ret;
21580 }
21581
21582 /* OpenMP 2.5:
21583    #pragma omp for for-clause[optseq] new-line
21584      for-loop  */
21585
21586 #define OMP_FOR_CLAUSE_MASK                             \
21587         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21588         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21589         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21590         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21591         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21592         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21593         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21594         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21595
21596 static tree
21597 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21598 {
21599   tree clauses, sb, ret;
21600   unsigned int save;
21601
21602   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21603                                        "#pragma omp for", pragma_tok);
21604
21605   sb = begin_omp_structured_block ();
21606   save = cp_parser_begin_omp_structured_block (parser);
21607
21608   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21609
21610   cp_parser_end_omp_structured_block (parser, save);
21611   add_stmt (finish_omp_structured_block (sb));
21612
21613   return ret;
21614 }
21615
21616 /* OpenMP 2.5:
21617    # pragma omp master new-line
21618      structured-block  */
21619
21620 static tree
21621 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21622 {
21623   cp_parser_require_pragma_eol (parser, pragma_tok);
21624   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21625 }
21626
21627 /* OpenMP 2.5:
21628    # pragma omp ordered new-line
21629      structured-block  */
21630
21631 static tree
21632 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21633 {
21634   cp_parser_require_pragma_eol (parser, pragma_tok);
21635   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21636 }
21637
21638 /* OpenMP 2.5:
21639
21640    section-scope:
21641      { section-sequence }
21642
21643    section-sequence:
21644      section-directive[opt] structured-block
21645      section-sequence section-directive structured-block  */
21646
21647 static tree
21648 cp_parser_omp_sections_scope (cp_parser *parser)
21649 {
21650   tree stmt, substmt;
21651   bool error_suppress = false;
21652   cp_token *tok;
21653
21654   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21655     return NULL_TREE;
21656
21657   stmt = push_stmt_list ();
21658
21659   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21660     {
21661       unsigned save;
21662
21663       substmt = begin_omp_structured_block ();
21664       save = cp_parser_begin_omp_structured_block (parser);
21665
21666       while (1)
21667         {
21668           cp_parser_statement (parser, NULL_TREE, false, NULL);
21669
21670           tok = cp_lexer_peek_token (parser->lexer);
21671           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21672             break;
21673           if (tok->type == CPP_CLOSE_BRACE)
21674             break;
21675           if (tok->type == CPP_EOF)
21676             break;
21677         }
21678
21679       cp_parser_end_omp_structured_block (parser, save);
21680       substmt = finish_omp_structured_block (substmt);
21681       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21682       add_stmt (substmt);
21683     }
21684
21685   while (1)
21686     {
21687       tok = cp_lexer_peek_token (parser->lexer);
21688       if (tok->type == CPP_CLOSE_BRACE)
21689         break;
21690       if (tok->type == CPP_EOF)
21691         break;
21692
21693       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21694         {
21695           cp_lexer_consume_token (parser->lexer);
21696           cp_parser_require_pragma_eol (parser, tok);
21697           error_suppress = false;
21698         }
21699       else if (!error_suppress)
21700         {
21701           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21702           error_suppress = true;
21703         }
21704
21705       substmt = cp_parser_omp_structured_block (parser);
21706       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21707       add_stmt (substmt);
21708     }
21709   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21710
21711   substmt = pop_stmt_list (stmt);
21712
21713   stmt = make_node (OMP_SECTIONS);
21714   TREE_TYPE (stmt) = void_type_node;
21715   OMP_SECTIONS_BODY (stmt) = substmt;
21716
21717   add_stmt (stmt);
21718   return stmt;
21719 }
21720
21721 /* OpenMP 2.5:
21722    # pragma omp sections sections-clause[optseq] newline
21723      sections-scope  */
21724
21725 #define OMP_SECTIONS_CLAUSE_MASK                        \
21726         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21727         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21728         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21729         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21730         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21731
21732 static tree
21733 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21734 {
21735   tree clauses, ret;
21736
21737   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21738                                        "#pragma omp sections", pragma_tok);
21739
21740   ret = cp_parser_omp_sections_scope (parser);
21741   if (ret)
21742     OMP_SECTIONS_CLAUSES (ret) = clauses;
21743
21744   return ret;
21745 }
21746
21747 /* OpenMP 2.5:
21748    # pragma parallel parallel-clause new-line
21749    # pragma parallel for parallel-for-clause new-line
21750    # pragma parallel sections parallel-sections-clause new-line  */
21751
21752 #define OMP_PARALLEL_CLAUSE_MASK                        \
21753         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21754         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21755         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21756         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21757         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21758         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21759         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21760         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21761
21762 static tree
21763 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21764 {
21765   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21766   const char *p_name = "#pragma omp parallel";
21767   tree stmt, clauses, par_clause, ws_clause, block;
21768   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21769   unsigned int save;
21770
21771   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21772     {
21773       cp_lexer_consume_token (parser->lexer);
21774       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21775       p_name = "#pragma omp parallel for";
21776       mask |= OMP_FOR_CLAUSE_MASK;
21777       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21778     }
21779   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21780     {
21781       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21782       const char *p = IDENTIFIER_POINTER (id);
21783       if (strcmp (p, "sections") == 0)
21784         {
21785           cp_lexer_consume_token (parser->lexer);
21786           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21787           p_name = "#pragma omp parallel sections";
21788           mask |= OMP_SECTIONS_CLAUSE_MASK;
21789           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21790         }
21791     }
21792
21793   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21794   block = begin_omp_parallel ();
21795   save = cp_parser_begin_omp_structured_block (parser);
21796
21797   switch (p_kind)
21798     {
21799     case PRAGMA_OMP_PARALLEL:
21800       cp_parser_statement (parser, NULL_TREE, false, NULL);
21801       par_clause = clauses;
21802       break;
21803
21804     case PRAGMA_OMP_PARALLEL_FOR:
21805       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21806       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21807       break;
21808
21809     case PRAGMA_OMP_PARALLEL_SECTIONS:
21810       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21811       stmt = cp_parser_omp_sections_scope (parser);
21812       if (stmt)
21813         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21814       break;
21815
21816     default:
21817       gcc_unreachable ();
21818     }
21819
21820   cp_parser_end_omp_structured_block (parser, save);
21821   stmt = finish_omp_parallel (par_clause, block);
21822   if (p_kind != PRAGMA_OMP_PARALLEL)
21823     OMP_PARALLEL_COMBINED (stmt) = 1;
21824   return stmt;
21825 }
21826
21827 /* OpenMP 2.5:
21828    # pragma omp single single-clause[optseq] new-line
21829      structured-block  */
21830
21831 #define OMP_SINGLE_CLAUSE_MASK                          \
21832         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21833         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21834         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21835         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21836
21837 static tree
21838 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21839 {
21840   tree stmt = make_node (OMP_SINGLE);
21841   TREE_TYPE (stmt) = void_type_node;
21842
21843   OMP_SINGLE_CLAUSES (stmt)
21844     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21845                                  "#pragma omp single", pragma_tok);
21846   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21847
21848   return add_stmt (stmt);
21849 }
21850
21851 /* OpenMP 3.0:
21852    # pragma omp task task-clause[optseq] new-line
21853      structured-block  */
21854
21855 #define OMP_TASK_CLAUSE_MASK                            \
21856         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21857         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21858         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21859         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21860         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21861         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21862
21863 static tree
21864 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21865 {
21866   tree clauses, block;
21867   unsigned int save;
21868
21869   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21870                                        "#pragma omp task", pragma_tok);
21871   block = begin_omp_task ();
21872   save = cp_parser_begin_omp_structured_block (parser);
21873   cp_parser_statement (parser, NULL_TREE, false, NULL);
21874   cp_parser_end_omp_structured_block (parser, save);
21875   return finish_omp_task (clauses, block);
21876 }
21877
21878 /* OpenMP 3.0:
21879    # pragma omp taskwait new-line  */
21880
21881 static void
21882 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21883 {
21884   cp_parser_require_pragma_eol (parser, pragma_tok);
21885   finish_omp_taskwait ();
21886 }
21887
21888 /* OpenMP 2.5:
21889    # pragma omp threadprivate (variable-list) */
21890
21891 static void
21892 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21893 {
21894   tree vars;
21895
21896   vars = cp_parser_omp_var_list (parser, 0, NULL);
21897   cp_parser_require_pragma_eol (parser, pragma_tok);
21898
21899   finish_omp_threadprivate (vars);
21900 }
21901
21902 /* Main entry point to OpenMP statement pragmas.  */
21903
21904 static void
21905 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21906 {
21907   tree stmt;
21908
21909   switch (pragma_tok->pragma_kind)
21910     {
21911     case PRAGMA_OMP_ATOMIC:
21912       cp_parser_omp_atomic (parser, pragma_tok);
21913       return;
21914     case PRAGMA_OMP_CRITICAL:
21915       stmt = cp_parser_omp_critical (parser, pragma_tok);
21916       break;
21917     case PRAGMA_OMP_FOR:
21918       stmt = cp_parser_omp_for (parser, pragma_tok);
21919       break;
21920     case PRAGMA_OMP_MASTER:
21921       stmt = cp_parser_omp_master (parser, pragma_tok);
21922       break;
21923     case PRAGMA_OMP_ORDERED:
21924       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21925       break;
21926     case PRAGMA_OMP_PARALLEL:
21927       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21928       break;
21929     case PRAGMA_OMP_SECTIONS:
21930       stmt = cp_parser_omp_sections (parser, pragma_tok);
21931       break;
21932     case PRAGMA_OMP_SINGLE:
21933       stmt = cp_parser_omp_single (parser, pragma_tok);
21934       break;
21935     case PRAGMA_OMP_TASK:
21936       stmt = cp_parser_omp_task (parser, pragma_tok);
21937       break;
21938     default:
21939       gcc_unreachable ();
21940     }
21941
21942   if (stmt)
21943     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21944 }
21945 \f
21946 /* The parser.  */
21947
21948 static GTY (()) cp_parser *the_parser;
21949
21950 \f
21951 /* Special handling for the first token or line in the file.  The first
21952    thing in the file might be #pragma GCC pch_preprocess, which loads a
21953    PCH file, which is a GC collection point.  So we need to handle this
21954    first pragma without benefit of an existing lexer structure.
21955
21956    Always returns one token to the caller in *FIRST_TOKEN.  This is
21957    either the true first token of the file, or the first token after
21958    the initial pragma.  */
21959
21960 static void
21961 cp_parser_initial_pragma (cp_token *first_token)
21962 {
21963   tree name = NULL;
21964
21965   cp_lexer_get_preprocessor_token (NULL, first_token);
21966   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21967     return;
21968
21969   cp_lexer_get_preprocessor_token (NULL, first_token);
21970   if (first_token->type == CPP_STRING)
21971     {
21972       name = first_token->u.value;
21973
21974       cp_lexer_get_preprocessor_token (NULL, first_token);
21975       if (first_token->type != CPP_PRAGMA_EOL)
21976         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21977                &first_token->location);
21978     }
21979   else
21980     error ("%Hexpected string literal", &first_token->location);
21981
21982   /* Skip to the end of the pragma.  */
21983   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21984     cp_lexer_get_preprocessor_token (NULL, first_token);
21985
21986   /* Now actually load the PCH file.  */
21987   if (name)
21988     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21989
21990   /* Read one more token to return to our caller.  We have to do this
21991      after reading the PCH file in, since its pointers have to be
21992      live.  */
21993   cp_lexer_get_preprocessor_token (NULL, first_token);
21994 }
21995
21996 /* Normal parsing of a pragma token.  Here we can (and must) use the
21997    regular lexer.  */
21998
21999 static bool
22000 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22001 {
22002   cp_token *pragma_tok;
22003   unsigned int id;
22004
22005   pragma_tok = cp_lexer_consume_token (parser->lexer);
22006   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22007   parser->lexer->in_pragma = true;
22008
22009   id = pragma_tok->pragma_kind;
22010   switch (id)
22011     {
22012     case PRAGMA_GCC_PCH_PREPROCESS:
22013       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22014              &pragma_tok->location);
22015       break;
22016
22017     case PRAGMA_OMP_BARRIER:
22018       switch (context)
22019         {
22020         case pragma_compound:
22021           cp_parser_omp_barrier (parser, pragma_tok);
22022           return false;
22023         case pragma_stmt:
22024           error ("%H%<#pragma omp barrier%> may only be "
22025                  "used in compound statements", &pragma_tok->location);
22026           break;
22027         default:
22028           goto bad_stmt;
22029         }
22030       break;
22031
22032     case PRAGMA_OMP_FLUSH:
22033       switch (context)
22034         {
22035         case pragma_compound:
22036           cp_parser_omp_flush (parser, pragma_tok);
22037           return false;
22038         case pragma_stmt:
22039           error ("%H%<#pragma omp flush%> may only be "
22040                  "used in compound statements", &pragma_tok->location);
22041           break;
22042         default:
22043           goto bad_stmt;
22044         }
22045       break;
22046
22047     case PRAGMA_OMP_TASKWAIT:
22048       switch (context)
22049         {
22050         case pragma_compound:
22051           cp_parser_omp_taskwait (parser, pragma_tok);
22052           return false;
22053         case pragma_stmt:
22054           error ("%H%<#pragma omp taskwait%> may only be "
22055                  "used in compound statements",
22056                  &pragma_tok->location);
22057           break;
22058         default:
22059           goto bad_stmt;
22060         }
22061       break;
22062
22063     case PRAGMA_OMP_THREADPRIVATE:
22064       cp_parser_omp_threadprivate (parser, pragma_tok);
22065       return false;
22066
22067     case PRAGMA_OMP_ATOMIC:
22068     case PRAGMA_OMP_CRITICAL:
22069     case PRAGMA_OMP_FOR:
22070     case PRAGMA_OMP_MASTER:
22071     case PRAGMA_OMP_ORDERED:
22072     case PRAGMA_OMP_PARALLEL:
22073     case PRAGMA_OMP_SECTIONS:
22074     case PRAGMA_OMP_SINGLE:
22075     case PRAGMA_OMP_TASK:
22076       if (context == pragma_external)
22077         goto bad_stmt;
22078       cp_parser_omp_construct (parser, pragma_tok);
22079       return true;
22080
22081     case PRAGMA_OMP_SECTION:
22082       error ("%H%<#pragma omp section%> may only be used in "
22083              "%<#pragma omp sections%> construct", &pragma_tok->location);
22084       break;
22085
22086     default:
22087       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22088       c_invoke_pragma_handler (id);
22089       break;
22090
22091     bad_stmt:
22092       cp_parser_error (parser, "expected declaration specifiers");
22093       break;
22094     }
22095
22096   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22097   return false;
22098 }
22099
22100 /* The interface the pragma parsers have to the lexer.  */
22101
22102 enum cpp_ttype
22103 pragma_lex (tree *value)
22104 {
22105   cp_token *tok;
22106   enum cpp_ttype ret;
22107
22108   tok = cp_lexer_peek_token (the_parser->lexer);
22109
22110   ret = tok->type;
22111   *value = tok->u.value;
22112
22113   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22114     ret = CPP_EOF;
22115   else if (ret == CPP_STRING)
22116     *value = cp_parser_string_literal (the_parser, false, false);
22117   else
22118     {
22119       cp_lexer_consume_token (the_parser->lexer);
22120       if (ret == CPP_KEYWORD)
22121         ret = CPP_NAME;
22122     }
22123
22124   return ret;
22125 }
22126
22127 \f
22128 /* External interface.  */
22129
22130 /* Parse one entire translation unit.  */
22131
22132 void
22133 c_parse_file (void)
22134 {
22135   bool error_occurred;
22136   static bool already_called = false;
22137
22138   if (already_called)
22139     {
22140       sorry ("inter-module optimizations not implemented for C++");
22141       return;
22142     }
22143   already_called = true;
22144
22145   the_parser = cp_parser_new ();
22146   push_deferring_access_checks (flag_access_control
22147                                 ? dk_no_deferred : dk_no_check);
22148   error_occurred = cp_parser_translation_unit (the_parser);
22149   the_parser = NULL;
22150 }
22151
22152 #include "gt-cp-parser.h"