OSDN Git Service

gcc/cp/
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010  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 "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "output.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.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 GTY(()) tree_check {
51   /* The value associated with the token.  */
52   tree value;
53   /* The checks that have been associated with value.  */
54   VEC (deferred_access_check, gc)* checks;
55   /* The token's qualifying scope (used when it is a
56      CPP_NESTED_NAME_SPECIFIER).  */
57   tree qualifying_scope;
58 };
59
60 /* A C++ token.  */
61
62 typedef struct GTY (()) cp_token {
63   /* The kind of token.  */
64   ENUM_BITFIELD (cpp_ttype) type : 8;
65   /* If this token is a keyword, this value indicates which keyword.
66      Otherwise, this value is RID_MAX.  */
67   ENUM_BITFIELD (rid) keyword : 8;
68   /* Token flags.  */
69   unsigned char flags;
70   /* Identifier for the pragma.  */
71   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72   /* True if this token is from a context where it is implicitly extern "C" */
73   BOOL_BITFIELD implicit_extern_c : 1;
74   /* True for a CPP_NAME token that is not a keyword (i.e., for which
75      KEYWORD is RID_MAX) iff this name was looked up and found to be
76      ambiguous.  An error has already been reported.  */
77   BOOL_BITFIELD ambiguous_p : 1;
78   /* The location at which this token was found.  */
79   location_t location;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
88
89 /* We use a stack of token pointer for saving token sets.  */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
93
94 static cp_token eof_token =
95 {
96   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
97 };
98
99 /* The cp_lexer structure represents the C++ lexer.  It is responsible
100    for managing the token stream from the preprocessor and supplying
101    it to the parser.  Tokens are never added to the cp_lexer after
102    it is created.  */
103
104 typedef struct GTY (()) cp_lexer {
105   /* The memory allocated for the buffer.  NULL if this lexer does not
106      own the token buffer.  */
107   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108   /* If the lexer owns the buffer, this is the number of tokens in the
109      buffer.  */
110   size_t buffer_length;
111
112   /* A pointer just past the last available token.  The tokens
113      in this lexer are [buffer, last_token).  */
114   cp_token_position GTY ((skip)) last_token;
115
116   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
117      no more available tokens.  */
118   cp_token_position GTY ((skip)) next_token;
119
120   /* A stack indicating positions at which cp_lexer_save_tokens was
121      called.  The top entry is the most recent position at which we
122      began saving tokens.  If the stack is non-empty, we are saving
123      tokens.  */
124   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125
126   /* The next lexer in a linked list of lexers.  */
127   struct cp_lexer *next;
128
129   /* True if we should output debugging information.  */
130   bool debugging_p;
131
132   /* True if we're in the context of parsing a pragma, and should not
133      increment past the end-of-line marker.  */
134   bool in_pragma;
135 } cp_lexer;
136
137 /* cp_token_cache is a range of tokens.  There is no need to represent
138    allocate heap memory for it, since tokens are never removed from the
139    lexer's array.  There is also no need for the GC to walk through
140    a cp_token_cache, since everything in here is referenced through
141    a lexer.  */
142
143 typedef struct GTY(()) cp_token_cache {
144   /* The beginning of the token range.  */
145   cp_token * GTY((skip)) first;
146
147   /* Points immediately after the last token in the range.  */
148   cp_token * GTY ((skip)) last;
149 } cp_token_cache;
150
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153   NIC_NONE,
154   /* floating-point literal */
155   NIC_FLOAT,
156   /* %<this%> */
157   NIC_THIS,
158   /* %<__FUNCTION__%> */
159   NIC_FUNC_NAME,
160   /* %<__PRETTY_FUNCTION__%> */
161   NIC_PRETTY_FUNC,
162   /* %<__func__%> */
163   NIC_C99_FUNC,
164   /* "%<va_arg%> */
165   NIC_VA_ARG,
166   /* a cast */
167   NIC_CAST,
168   /* %<typeid%> operator */
169   NIC_TYPEID,
170   /* non-constant compound literals */
171   NIC_NCC,
172   /* a function call */
173   NIC_FUNC_CALL,
174   /* an increment */
175   NIC_INC,
176   /* an decrement */
177   NIC_DEC,
178   /* an array reference */
179   NIC_ARRAY_REF,
180   /* %<->%> */
181   NIC_ARROW,
182   /* %<.%> */
183   NIC_POINT,
184   /* the address of a label */
185   NIC_ADDR_LABEL,
186   /* %<*%> */
187   NIC_STAR,
188   /* %<&%> */
189   NIC_ADDR,
190   /* %<++%> */
191   NIC_PREINCREMENT,
192   /* %<--%> */
193   NIC_PREDECREMENT,
194   /* %<new%> */
195   NIC_NEW,
196   /* %<delete%> */
197   NIC_DEL,
198   /* calls to overloaded operators */
199   NIC_OVERLOADED,
200   /* an assignment */
201   NIC_ASSIGNMENT,
202   /* a comma operator */
203   NIC_COMMA,
204   /* a call to a constructor */
205   NIC_CONSTRUCTOR
206 } non_integral_constant;
207
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210   /* NULL */
211   NLE_NULL,
212   /* is not a type */
213   NLE_TYPE,
214   /* is not a class or namespace */
215   NLE_CXX98,
216   /* is not a class, namespace, or enumeration */
217   NLE_NOT_CXX98
218 } name_lookup_error;
219
220 /* The various kinds of required token */
221 typedef enum required_token {
222   RT_NONE,
223   RT_SEMICOLON,  /* ';' */
224   RT_OPEN_PAREN, /* '(' */
225   RT_CLOSE_BRACE, /* '}' */
226   RT_OPEN_BRACE,  /* '{' */
227   RT_CLOSE_SQUARE, /* ']' */
228   RT_OPEN_SQUARE,  /* '[' */
229   RT_COMMA, /* ',' */
230   RT_SCOPE, /* '::' */
231   RT_LESS, /* '<' */
232   RT_GREATER, /* '>' */
233   RT_EQ, /* '=' */
234   RT_ELLIPSIS, /* '...' */
235   RT_MULT, /* '*' */
236   RT_COMPL, /* '~' */
237   RT_COLON, /* ':' */
238   RT_COLON_SCOPE, /* ':' or '::' */
239   RT_CLOSE_PAREN, /* ')' */
240   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241   RT_PRAGMA_EOL, /* end of line */
242   RT_NAME, /* identifier */
243
244   /* The type is CPP_KEYWORD */
245   RT_NEW, /* new */
246   RT_DELETE, /* delete */
247   RT_RETURN, /* return */
248   RT_WHILE, /* while */
249   RT_EXTERN, /* extern */
250   RT_STATIC_ASSERT, /* static_assert */
251   RT_DECLTYPE, /* decltype */
252   RT_OPERATOR, /* operator */
253   RT_CLASS, /* class */
254   RT_TEMPLATE, /* template */
255   RT_NAMESPACE, /* namespace */
256   RT_USING, /* using */
257   RT_ASM, /* asm */
258   RT_TRY, /* try */
259   RT_CATCH, /* catch */
260   RT_THROW, /* throw */
261   RT_LABEL, /* __label__ */
262   RT_AT_TRY, /* @try */
263   RT_AT_SYNCHRONIZED, /* @synchronized */
264   RT_AT_THROW, /* @throw */
265
266   RT_SELECT,  /* selection-statement */
267   RT_INTERATION, /* iteration-statement */
268   RT_JUMP, /* jump-statement */
269   RT_CLASS_KEY, /* class-key */
270   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
272
273 /* Prototypes.  */
274
275 static cp_lexer *cp_lexer_new_main
276   (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278   (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280   (cp_lexer *);
281 static int cp_lexer_saving_tokens
282   (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284   (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286   (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288   (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290   (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292   (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294   (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296   (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298   (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300   (cp_lexer *);
301 static void cp_lexer_purge_token
302   (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304   (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306   (cp_lexer *);
307 static void cp_lexer_commit_tokens
308   (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310   (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313   (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315   (cp_lexer *);
316 static void cp_lexer_start_debugging
317   (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319   (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322    about passing NULL to functions that require non-NULL arguments
323    (fputs, fprintf).  It will never be used, so all we need is a value
324    of the right type that's guaranteed not to be NULL.  */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
329
330 static cp_token_cache *cp_token_cache_new
331   (cp_token *, cp_token *);
332
333 static void cp_parser_initial_pragma
334   (cp_token *);
335
336 /* Manifest constants.  */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
339
340 /* A token type for keywords, as opposed to ordinary identifiers.  */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342
343 /* A token type for template-ids.  If a template-id is processed while
344    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345    the value of the CPP_TEMPLATE_ID is whatever was returned by
346    cp_parser_template_id.  */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348
349 /* A token type for nested-name-specifiers.  If a
350    nested-name-specifier is processed while parsing tentatively, it is
351    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353    cp_parser_nested_name_specifier_opt.  */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355
356 /* A token type for tokens that are not tokens at all; these are used
357    to represent slots in the array where there used to be a token
358    that has now been deleted.  */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360
361 /* The number of token types, including C++-specific ones.  */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363
364 /* Variables.  */
365
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written.  */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
370
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372    sizeof, typeof, or alignof.  */
373 int cp_unevaluated_operand;
374
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376    preprocessor.  */
377
378 static cp_lexer *
379 cp_lexer_new_main (void)
380 {
381   cp_token first_token;
382   cp_lexer *lexer;
383   cp_token *pos;
384   size_t alloc;
385   size_t space;
386   cp_token *buffer;
387
388   /* It's possible that parsing the first pragma will load a PCH file,
389      which is a GC collection point.  So we have to do that before
390      allocating any memory.  */
391   cp_parser_initial_pragma (&first_token);
392
393   c_common_no_more_pch ();
394
395   /* Allocate the memory.  */
396   lexer = ggc_alloc_cleared_cp_lexer ();
397
398 #ifdef ENABLE_CHECKING
399   /* Initially we are not debugging.  */
400   lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403                                    CP_SAVED_TOKEN_STACK);
404
405   /* Create the buffer.  */
406   alloc = CP_LEXER_BUFFER_SIZE;
407   buffer = ggc_alloc_vec_cp_token (alloc);
408
409   /* Put the first token in the buffer.  */
410   space = alloc;
411   pos = buffer;
412   *pos = first_token;
413
414   /* Get the remaining tokens from the preprocessor.  */
415   while (pos->type != CPP_EOF)
416     {
417       pos++;
418       if (!--space)
419         {
420           space = alloc;
421           alloc *= 2;
422           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423           pos = buffer + space;
424         }
425       cp_lexer_get_preprocessor_token (lexer, pos);
426     }
427   lexer->buffer = buffer;
428   lexer->buffer_length = alloc - space;
429   lexer->last_token = pos;
430   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431
432   /* Subsequent preprocessor diagnostics should use compiler
433      diagnostic functions to get the compiler source location.  */
434   done_lexing = true;
435
436   gcc_assert (lexer->next_token->type != CPP_PURGED);
437   return lexer;
438 }
439
440 /* Create a new lexer whose token stream is primed with the tokens in
441    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
442
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 {
446   cp_token *first = cache->first;
447   cp_token *last = cache->last;
448   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449
450   /* We do not own the buffer.  */
451   lexer->buffer = NULL;
452   lexer->buffer_length = 0;
453   lexer->next_token = first == last ? &eof_token : first;
454   lexer->last_token = last;
455
456   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457                                    CP_SAVED_TOKEN_STACK);
458
459 #ifdef ENABLE_CHECKING
460   /* Initially we are not debugging.  */
461   lexer->debugging_p = false;
462 #endif
463
464   gcc_assert (lexer->next_token->type != CPP_PURGED);
465   return lexer;
466 }
467
468 /* Frees all resources associated with LEXER.  */
469
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
472 {
473   if (lexer->buffer)
474     ggc_free (lexer->buffer);
475   VEC_free (cp_token_position, heap, lexer->saved_tokens);
476   ggc_free (lexer);
477 }
478
479 /* Returns nonzero if debugging information should be output.  */
480
481 #ifdef ENABLE_CHECKING
482
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
485 {
486   return lexer->debugging_p;
487 }
488
489 #endif /* ENABLE_CHECKING */
490
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 {
494   gcc_assert (!previous_p || lexer->next_token != &eof_token);
495
496   return lexer->next_token - previous_p;
497 }
498
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 {
502   return pos;
503 }
504
505 static inline void
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
507 {
508   lexer->next_token = cp_lexer_token_at (lexer, pos);
509 }
510
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
513 {
514   if (lexer->next_token == &eof_token)
515     return lexer->last_token - 1;
516   else
517     return cp_lexer_token_position (lexer, true);
518 }
519
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
522 {
523   cp_token_position tp = cp_lexer_previous_token_position (lexer);
524
525   return cp_lexer_token_at (lexer, tp);
526 }
527
528 /* nonzero if we are presently saving tokens.  */
529
530 static inline int
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
532 {
533   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
534 }
535
536 /* Store the next token from the preprocessor in *TOKEN.  Return true
537    if we reach EOF.  If LEXER is NULL, assume we are handling an
538    initial #pragma pch_preprocess, and thus want the lexer to return
539    processed strings.  */
540
541 static void
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
543 {
544   static int is_extern_c = 0;
545
546    /* Get a new token from the preprocessor.  */
547   token->type
548     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550   token->keyword = RID_MAX;
551   token->pragma_kind = PRAGMA_NONE;
552
553   /* On some systems, some header files are surrounded by an
554      implicit extern "C" block.  Set a flag in the token if it
555      comes from such a header.  */
556   is_extern_c += pending_lang_change;
557   pending_lang_change = 0;
558   token->implicit_extern_c = is_extern_c > 0;
559
560   /* Check to see if this token is a keyword.  */
561   if (token->type == CPP_NAME)
562     {
563       if (C_IS_RESERVED_WORD (token->u.value))
564         {
565           /* Mark this token as a keyword.  */
566           token->type = CPP_KEYWORD;
567           /* Record which keyword.  */
568           token->keyword = C_RID_CODE (token->u.value);
569         }
570       else
571         {
572           if (warn_cxx0x_compat
573               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
575             {
576               /* Warn about the C++0x keyword (but still treat it as
577                  an identifier).  */
578               warning (OPT_Wc__0x_compat, 
579                        "identifier %qE will become a keyword in C++0x",
580                        token->u.value);
581
582               /* Clear out the C_RID_CODE so we don't warn about this
583                  particular identifier-turned-keyword again.  */
584               C_SET_RID_CODE (token->u.value, RID_MAX);
585             }
586
587           token->ambiguous_p = false;
588           token->keyword = RID_MAX;
589         }
590     }
591   else if (token->type == CPP_AT_NAME)
592     {
593       /* This only happens in Objective-C++; it must be a keyword.  */
594       token->type = CPP_KEYWORD;
595       switch (C_RID_CODE (token->u.value))
596         {
597           /* Replace 'class' with '@class', 'private' with '@private',
598              etc.  This prevents confusion with the C++ keyword
599              'class', and makes the tokens consistent with other
600              Objective-C 'AT' keywords.  For example '@class' is
601              reported as RID_AT_CLASS which is consistent with
602              '@synchronized', which is reported as
603              RID_AT_SYNCHRONIZED.
604           */
605         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
606         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
607         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
609         case RID_THROW:     token->keyword = RID_AT_THROW; break;
610         case RID_TRY:       token->keyword = RID_AT_TRY; break;
611         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
612         default:            token->keyword = C_RID_CODE (token->u.value);
613         }
614     }
615   else if (token->type == CPP_PRAGMA)
616     {
617       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
618       token->pragma_kind = ((enum pragma_kind)
619                             TREE_INT_CST_LOW (token->u.value));
620       token->u.value = NULL_TREE;
621     }
622 }
623
624 /* Update the globals input_location and the input file stack from TOKEN.  */
625 static inline void
626 cp_lexer_set_source_position_from_token (cp_token *token)
627 {
628   if (token->type != CPP_EOF)
629     {
630       input_location = token->location;
631     }
632 }
633
634 /* Return a pointer to the next token in the token stream, but do not
635    consume it.  */
636
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
639 {
640   if (cp_lexer_debugging_p (lexer))
641     {
642       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644       putc ('\n', cp_lexer_debug_stream);
645     }
646   return lexer->next_token;
647 }
648
649 /* Return true if the next token has the indicated TYPE.  */
650
651 static inline bool
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
653 {
654   return cp_lexer_peek_token (lexer)->type == type;
655 }
656
657 /* Return true if the next token does not have the indicated TYPE.  */
658
659 static inline bool
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
661 {
662   return !cp_lexer_next_token_is (lexer, type);
663 }
664
665 /* Return true if the next token is the indicated KEYWORD.  */
666
667 static inline bool
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
669 {
670   return cp_lexer_peek_token (lexer)->keyword == keyword;
671 }
672
673 /* Return true if the next token is not the indicated KEYWORD.  */
674
675 static inline bool
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
677 {
678   return cp_lexer_peek_token (lexer)->keyword != keyword;
679 }
680
681 /* Return true if the next token is a keyword for a decl-specifier.  */
682
683 static bool
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
685 {
686   cp_token *token;
687
688   token = cp_lexer_peek_token (lexer);
689   switch (token->keyword) 
690     {
691       /* auto specifier: storage-class-specifier in C++,
692          simple-type-specifier in C++0x.  */
693     case RID_AUTO:
694       /* Storage classes.  */
695     case RID_REGISTER:
696     case RID_STATIC:
697     case RID_EXTERN:
698     case RID_MUTABLE:
699     case RID_THREAD:
700       /* Elaborated type specifiers.  */
701     case RID_ENUM:
702     case RID_CLASS:
703     case RID_STRUCT:
704     case RID_UNION:
705     case RID_TYPENAME:
706       /* Simple type specifiers.  */
707     case RID_CHAR:
708     case RID_CHAR16:
709     case RID_CHAR32:
710     case RID_WCHAR:
711     case RID_BOOL:
712     case RID_SHORT:
713     case RID_INT:
714     case RID_LONG:
715     case RID_INT128:
716     case RID_SIGNED:
717     case RID_UNSIGNED:
718     case RID_FLOAT:
719     case RID_DOUBLE:
720     case RID_VOID:
721       /* GNU extensions.  */ 
722     case RID_ATTRIBUTE:
723     case RID_TYPEOF:
724       /* C++0x extensions.  */
725     case RID_DECLTYPE:
726       return true;
727
728     default:
729       return false;
730     }
731 }
732
733 /* Return a pointer to the Nth token in the token stream.  If N is 1,
734    then this is precisely equivalent to cp_lexer_peek_token (except
735    that it is not inline).  One would like to disallow that case, but
736    there is one case (cp_parser_nth_token_starts_template_id) where
737    the caller passes a variable for N and it might be 1.  */
738
739 static cp_token *
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
741 {
742   cp_token *token;
743
744   /* N is 1-based, not zero-based.  */
745   gcc_assert (n > 0);
746
747   if (cp_lexer_debugging_p (lexer))
748     fprintf (cp_lexer_debug_stream,
749              "cp_lexer: peeking ahead %ld at token: ", (long)n);
750
751   --n;
752   token = lexer->next_token;
753   gcc_assert (!n || token != &eof_token);
754   while (n != 0)
755     {
756       ++token;
757       if (token == lexer->last_token)
758         {
759           token = &eof_token;
760           break;
761         }
762
763       if (token->type != CPP_PURGED)
764         --n;
765     }
766
767   if (cp_lexer_debugging_p (lexer))
768     {
769       cp_lexer_print_token (cp_lexer_debug_stream, token);
770       putc ('\n', cp_lexer_debug_stream);
771     }
772
773   return token;
774 }
775
776 /* Return the next token, and advance the lexer's next_token pointer
777    to point to the next non-purged token.  */
778
779 static cp_token *
780 cp_lexer_consume_token (cp_lexer* lexer)
781 {
782   cp_token *token = lexer->next_token;
783
784   gcc_assert (token != &eof_token);
785   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
786
787   do
788     {
789       lexer->next_token++;
790       if (lexer->next_token == lexer->last_token)
791         {
792           lexer->next_token = &eof_token;
793           break;
794         }
795
796     }
797   while (lexer->next_token->type == CPP_PURGED);
798
799   cp_lexer_set_source_position_from_token (token);
800
801   /* Provide debugging output.  */
802   if (cp_lexer_debugging_p (lexer))
803     {
804       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805       cp_lexer_print_token (cp_lexer_debug_stream, token);
806       putc ('\n', cp_lexer_debug_stream);
807     }
808
809   return token;
810 }
811
812 /* Permanently remove the next token from the token stream, and
813    advance the next_token pointer to refer to the next non-purged
814    token.  */
815
816 static void
817 cp_lexer_purge_token (cp_lexer *lexer)
818 {
819   cp_token *tok = lexer->next_token;
820
821   gcc_assert (tok != &eof_token);
822   tok->type = CPP_PURGED;
823   tok->location = UNKNOWN_LOCATION;
824   tok->u.value = NULL_TREE;
825   tok->keyword = RID_MAX;
826
827   do
828     {
829       tok++;
830       if (tok == lexer->last_token)
831         {
832           tok = &eof_token;
833           break;
834         }
835     }
836   while (tok->type == CPP_PURGED);
837   lexer->next_token = tok;
838 }
839
840 /* Permanently remove all tokens after TOK, up to, but not
841    including, the token that will be returned next by
842    cp_lexer_peek_token.  */
843
844 static void
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
846 {
847   cp_token *peek = lexer->next_token;
848
849   if (peek == &eof_token)
850     peek = lexer->last_token;
851
852   gcc_assert (tok < peek);
853
854   for ( tok += 1; tok != peek; tok += 1)
855     {
856       tok->type = CPP_PURGED;
857       tok->location = UNKNOWN_LOCATION;
858       tok->u.value = NULL_TREE;
859       tok->keyword = RID_MAX;
860     }
861 }
862
863 /* Begin saving tokens.  All tokens consumed after this point will be
864    preserved.  */
865
866 static void
867 cp_lexer_save_tokens (cp_lexer* lexer)
868 {
869   /* Provide debugging output.  */
870   if (cp_lexer_debugging_p (lexer))
871     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
872
873   VEC_safe_push (cp_token_position, heap,
874                  lexer->saved_tokens, lexer->next_token);
875 }
876
877 /* Commit to the portion of the token stream most recently saved.  */
878
879 static void
880 cp_lexer_commit_tokens (cp_lexer* lexer)
881 {
882   /* Provide debugging output.  */
883   if (cp_lexer_debugging_p (lexer))
884     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
885
886   VEC_pop (cp_token_position, lexer->saved_tokens);
887 }
888
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890    to the token stream.  Stop saving tokens.  */
891
892 static void
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
894 {
895   /* Provide debugging output.  */
896   if (cp_lexer_debugging_p (lexer))
897     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
898
899   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
900 }
901
902 /* Print a representation of the TOKEN on the STREAM.  */
903
904 #ifdef ENABLE_CHECKING
905
906 static void
907 cp_lexer_print_token (FILE * stream, cp_token *token)
908 {
909   /* We don't use cpp_type2name here because the parser defines
910      a few tokens of its own.  */
911   static const char *const token_names[] = {
912     /* cpplib-defined token types */
913 #define OP(e, s) #e,
914 #define TK(e, s) #e,
915     TTYPE_TABLE
916 #undef OP
917 #undef TK
918     /* C++ parser token types - see "Manifest constants", above.  */
919     "KEYWORD",
920     "TEMPLATE_ID",
921     "NESTED_NAME_SPECIFIER",
922     "PURGED"
923   };
924
925   /* If we have a name for the token, print it out.  Otherwise, we
926      simply give the numeric code.  */
927   gcc_assert (token->type < ARRAY_SIZE(token_names));
928   fputs (token_names[token->type], stream);
929
930   /* For some tokens, print the associated data.  */
931   switch (token->type)
932     {
933     case CPP_KEYWORD:
934       /* Some keywords have a value that is not an IDENTIFIER_NODE.
935          For example, `struct' is mapped to an INTEGER_CST.  */
936       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
937         break;
938       /* else fall through */
939     case CPP_NAME:
940       fputs (IDENTIFIER_POINTER (token->u.value), stream);
941       break;
942
943     case CPP_STRING:
944     case CPP_STRING16:
945     case CPP_STRING32:
946     case CPP_WSTRING:
947     case CPP_UTF8STRING:
948       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
949       break;
950
951     default:
952       break;
953     }
954 }
955
956 /* Start emitting debugging information.  */
957
958 static void
959 cp_lexer_start_debugging (cp_lexer* lexer)
960 {
961   lexer->debugging_p = true;
962 }
963
964 /* Stop emitting debugging information.  */
965
966 static void
967 cp_lexer_stop_debugging (cp_lexer* lexer)
968 {
969   lexer->debugging_p = false;
970 }
971
972 #endif /* ENABLE_CHECKING */
973
974 /* Create a new cp_token_cache, representing a range of tokens.  */
975
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
978 {
979   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980   cache->first = first;
981   cache->last = last;
982   return cache;
983 }
984
985 \f
986 /* Decl-specifiers.  */
987
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
989
990 static void
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
992 {
993   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
994 }
995
996 /* Declarators.  */
997
998 /* Nothing other than the parser should be creating declarators;
999    declarators are a semi-syntactic representation of C++ entities.
1000    Other parts of the front end that need to create entities (like
1001    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1002
1003 static cp_declarator *make_call_declarator
1004   (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006   (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008   (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010   (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012   (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014   (cp_cv_quals, tree, cp_declarator *);
1015
1016 /* An erroneous declarator.  */
1017 static cp_declarator *cp_error_declarator;
1018
1019 /* The obstack on which declarators and related data structures are
1020    allocated.  */
1021 static struct obstack declarator_obstack;
1022
1023 /* Alloc BYTES from the declarator memory pool.  */
1024
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1027 {
1028   return obstack_alloc (&declarator_obstack, bytes);
1029 }
1030
1031 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1032    common to all declarators.  */
1033
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1036 {
1037   cp_declarator *declarator;
1038
1039   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040   declarator->kind = kind;
1041   declarator->attributes = NULL_TREE;
1042   declarator->declarator = NULL;
1043   declarator->parameter_pack_p = false;
1044   declarator->id_loc = UNKNOWN_LOCATION;
1045
1046   return declarator;
1047 }
1048
1049 /* Make a declarator for a generalized identifier.  If
1050    QUALIFYING_SCOPE is non-NULL, the identifier is
1051    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1053    is, if any.   */
1054
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057                     special_function_kind sfk)
1058 {
1059   cp_declarator *declarator;
1060
1061   /* It is valid to write:
1062
1063        class C { void f(); };
1064        typedef C D;
1065        void D::f();
1066
1067      The standard is not clear about whether `typedef const C D' is
1068      legal; as of 2002-09-15 the committee is considering that
1069      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1070      well.  */
1071   if (qualifying_scope && TYPE_P (qualifying_scope))
1072     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1073
1074   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1077
1078   declarator = make_declarator (cdk_id);
1079   declarator->u.id.qualifying_scope = qualifying_scope;
1080   declarator->u.id.unqualified_name = unqualified_name;
1081   declarator->u.id.sfk = sfk;
1082   
1083   return declarator;
1084 }
1085
1086 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1087    of modifiers such as const or volatile to apply to the pointer
1088    type, represented as identifiers.  */
1089
1090 cp_declarator *
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1092 {
1093   cp_declarator *declarator;
1094
1095   declarator = make_declarator (cdk_pointer);
1096   declarator->declarator = target;
1097   declarator->u.pointer.qualifiers = cv_qualifiers;
1098   declarator->u.pointer.class_type = NULL_TREE;
1099   if (target)
1100     {
1101       declarator->id_loc = target->id_loc;
1102       declarator->parameter_pack_p = target->parameter_pack_p;
1103       target->parameter_pack_p = false;
1104     }
1105   else
1106     declarator->parameter_pack_p = false;
1107
1108   return declarator;
1109 }
1110
1111 /* Like make_pointer_declarator -- but for references.  */
1112
1113 cp_declarator *
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1115                            bool rvalue_ref)
1116 {
1117   cp_declarator *declarator;
1118
1119   declarator = make_declarator (cdk_reference);
1120   declarator->declarator = target;
1121   declarator->u.reference.qualifiers = cv_qualifiers;
1122   declarator->u.reference.rvalue_ref = rvalue_ref;
1123   if (target)
1124     {
1125       declarator->id_loc = target->id_loc;
1126       declarator->parameter_pack_p = target->parameter_pack_p;
1127       target->parameter_pack_p = false;
1128     }
1129   else
1130     declarator->parameter_pack_p = false;
1131
1132   return declarator;
1133 }
1134
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136    member of CLASS_TYPE.  */
1137
1138 cp_declarator *
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140                         cp_declarator *pointee)
1141 {
1142   cp_declarator *declarator;
1143
1144   declarator = make_declarator (cdk_ptrmem);
1145   declarator->declarator = pointee;
1146   declarator->u.pointer.qualifiers = cv_qualifiers;
1147   declarator->u.pointer.class_type = class_type;
1148
1149   if (pointee)
1150     {
1151       declarator->parameter_pack_p = pointee->parameter_pack_p;
1152       pointee->parameter_pack_p = false;
1153     }
1154   else
1155     declarator->parameter_pack_p = false;
1156
1157   return declarator;
1158 }
1159
1160 /* Make a declarator for the function given by TARGET, with the
1161    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1162    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1163    indicates what exceptions can be thrown.  */
1164
1165 cp_declarator *
1166 make_call_declarator (cp_declarator *target,
1167                       tree parms,
1168                       cp_cv_quals cv_qualifiers,
1169                       tree exception_specification,
1170                       tree late_return_type)
1171 {
1172   cp_declarator *declarator;
1173
1174   declarator = make_declarator (cdk_function);
1175   declarator->declarator = target;
1176   declarator->u.function.parameters = parms;
1177   declarator->u.function.qualifiers = cv_qualifiers;
1178   declarator->u.function.exception_specification = exception_specification;
1179   declarator->u.function.late_return_type = late_return_type;
1180   if (target)
1181     {
1182       declarator->id_loc = target->id_loc;
1183       declarator->parameter_pack_p = target->parameter_pack_p;
1184       target->parameter_pack_p = false;
1185     }
1186   else
1187     declarator->parameter_pack_p = false;
1188
1189   return declarator;
1190 }
1191
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193    defined by ELEMENT.  */
1194
1195 cp_declarator *
1196 make_array_declarator (cp_declarator *element, tree bounds)
1197 {
1198   cp_declarator *declarator;
1199
1200   declarator = make_declarator (cdk_array);
1201   declarator->declarator = element;
1202   declarator->u.array.bounds = bounds;
1203   if (element)
1204     {
1205       declarator->id_loc = element->id_loc;
1206       declarator->parameter_pack_p = element->parameter_pack_p;
1207       element->parameter_pack_p = false;
1208     }
1209   else
1210     declarator->parameter_pack_p = false;
1211
1212   return declarator;
1213 }
1214
1215 /* Determine whether the declarator we've seen so far can be a
1216    parameter pack, when followed by an ellipsis.  */
1217 static bool 
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1219 {
1220   /* Search for a declarator name, or any other declarator that goes
1221      after the point where the ellipsis could appear in a parameter
1222      pack. If we find any of these, then this declarator can not be
1223      made into a parameter pack.  */
1224   bool found = false;
1225   while (declarator && !found)
1226     {
1227       switch ((int)declarator->kind)
1228         {
1229         case cdk_id:
1230         case cdk_array:
1231           found = true;
1232           break;
1233
1234         case cdk_error:
1235           return true;
1236
1237         default:
1238           declarator = declarator->declarator;
1239           break;
1240         }
1241     }
1242
1243   return !found;
1244 }
1245
1246 cp_parameter_declarator *no_parameters;
1247
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249    DECLARATOR and DEFAULT_ARGUMENT.  */
1250
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253                            cp_declarator *declarator,
1254                            tree default_argument)
1255 {
1256   cp_parameter_declarator *parameter;
1257
1258   parameter = ((cp_parameter_declarator *)
1259                alloc_declarator (sizeof (cp_parameter_declarator)));
1260   parameter->next = NULL;
1261   if (decl_specifiers)
1262     parameter->decl_specifiers = *decl_specifiers;
1263   else
1264     clear_decl_specs (&parameter->decl_specifiers);
1265   parameter->declarator = declarator;
1266   parameter->default_argument = default_argument;
1267   parameter->ellipsis_p = false;
1268
1269   return parameter;
1270 }
1271
1272 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1273
1274 static bool
1275 function_declarator_p (const cp_declarator *declarator)
1276 {
1277   while (declarator)
1278     {
1279       if (declarator->kind == cdk_function
1280           && declarator->declarator->kind == cdk_id)
1281         return true;
1282       if (declarator->kind == cdk_id
1283           || declarator->kind == cdk_error)
1284         return false;
1285       declarator = declarator->declarator;
1286     }
1287   return false;
1288 }
1289  
1290 /* The parser.  */
1291
1292 /* Overview
1293    --------
1294
1295    A cp_parser parses the token stream as specified by the C++
1296    grammar.  Its job is purely parsing, not semantic analysis.  For
1297    example, the parser breaks the token stream into declarators,
1298    expressions, statements, and other similar syntactic constructs.
1299    It does not check that the types of the expressions on either side
1300    of an assignment-statement are compatible, or that a function is
1301    not declared with a parameter of type `void'.
1302
1303    The parser invokes routines elsewhere in the compiler to perform
1304    semantic analysis and to build up the abstract syntax tree for the
1305    code processed.
1306
1307    The parser (and the template instantiation code, which is, in a
1308    way, a close relative of parsing) are the only parts of the
1309    compiler that should be calling push_scope and pop_scope, or
1310    related functions.  The parser (and template instantiation code)
1311    keeps track of what scope is presently active; everything else
1312    should simply honor that.  (The code that generates static
1313    initializers may also need to set the scope, in order to check
1314    access control correctly when emitting the initializers.)
1315
1316    Methodology
1317    -----------
1318
1319    The parser is of the standard recursive-descent variety.  Upcoming
1320    tokens in the token stream are examined in order to determine which
1321    production to use when parsing a non-terminal.  Some C++ constructs
1322    require arbitrary look ahead to disambiguate.  For example, it is
1323    impossible, in the general case, to tell whether a statement is an
1324    expression or declaration without scanning the entire statement.
1325    Therefore, the parser is capable of "parsing tentatively."  When the
1326    parser is not sure what construct comes next, it enters this mode.
1327    Then, while we attempt to parse the construct, the parser queues up
1328    error messages, rather than issuing them immediately, and saves the
1329    tokens it consumes.  If the construct is parsed successfully, the
1330    parser "commits", i.e., it issues any queued error messages and
1331    the tokens that were being preserved are permanently discarded.
1332    If, however, the construct is not parsed successfully, the parser
1333    rolls back its state completely so that it can resume parsing using
1334    a different alternative.
1335
1336    Future Improvements
1337    -------------------
1338
1339    The performance of the parser could probably be improved substantially.
1340    We could often eliminate the need to parse tentatively by looking ahead
1341    a little bit.  In some places, this approach might not entirely eliminate
1342    the need to parse tentatively, but it might still speed up the average
1343    case.  */
1344
1345 /* Flags that are passed to some parsing functions.  These values can
1346    be bitwise-ored together.  */
1347
1348 enum
1349 {
1350   /* No flags.  */
1351   CP_PARSER_FLAGS_NONE = 0x0,
1352   /* The construct is optional.  If it is not present, then no error
1353      should be issued.  */
1354   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355   /* When parsing a type-specifier, treat user-defined type-names
1356      as non-type identifiers.  */
1357   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358   /* When parsing a type-specifier, do not try to parse a class-specifier
1359      or enum-specifier.  */
1360   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361   /* When parsing a decl-specifier-seq, only allow type-specifier or
1362      constexpr.  */
1363   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1364 };
1365
1366 /* This type is used for parameters and variables which hold
1367    combinations of the above flags.  */
1368 typedef int cp_parser_flags;
1369
1370 /* The different kinds of declarators we want to parse.  */
1371
1372 typedef enum cp_parser_declarator_kind
1373 {
1374   /* We want an abstract declarator.  */
1375   CP_PARSER_DECLARATOR_ABSTRACT,
1376   /* We want a named declarator.  */
1377   CP_PARSER_DECLARATOR_NAMED,
1378   /* We don't mind, but the name must be an unqualified-id.  */
1379   CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1381
1382 /* The precedence values used to parse binary expressions.  The minimum value
1383    of PREC must be 1, because zero is reserved to quickly discriminate
1384    binary operators from other tokens.  */
1385
1386 enum cp_parser_prec
1387 {
1388   PREC_NOT_OPERATOR,
1389   PREC_LOGICAL_OR_EXPRESSION,
1390   PREC_LOGICAL_AND_EXPRESSION,
1391   PREC_INCLUSIVE_OR_EXPRESSION,
1392   PREC_EXCLUSIVE_OR_EXPRESSION,
1393   PREC_AND_EXPRESSION,
1394   PREC_EQUALITY_EXPRESSION,
1395   PREC_RELATIONAL_EXPRESSION,
1396   PREC_SHIFT_EXPRESSION,
1397   PREC_ADDITIVE_EXPRESSION,
1398   PREC_MULTIPLICATIVE_EXPRESSION,
1399   PREC_PM_EXPRESSION,
1400   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1401 };
1402
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404    precedence value.  */
1405
1406 typedef struct cp_parser_binary_operations_map_node
1407 {
1408   /* The token type.  */
1409   enum cpp_ttype token_type;
1410   /* The corresponding tree code.  */
1411   enum tree_code tree_type;
1412   /* The precedence of this operator.  */
1413   enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1415
1416 /* The status of a tentative parse.  */
1417
1418 typedef enum cp_parser_status_kind
1419 {
1420   /* No errors have occurred.  */
1421   CP_PARSER_STATUS_KIND_NO_ERROR,
1422   /* An error has occurred.  */
1423   CP_PARSER_STATUS_KIND_ERROR,
1424   /* We are committed to this tentative parse, whether or not an error
1425      has occurred.  */
1426   CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1428
1429 typedef struct cp_parser_expression_stack_entry
1430 {
1431   /* Left hand side of the binary operation we are currently
1432      parsing.  */
1433   tree lhs;
1434   /* Original tree code for left hand side, if it was a binary
1435      expression itself (used for -Wparentheses).  */
1436   enum tree_code lhs_type;
1437   /* Tree code for the binary operation we are parsing.  */
1438   enum tree_code tree_type;
1439   /* Precedence of the binary operation we are parsing.  */
1440   enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1442
1443 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1444    entries because precedence levels on the stack are monotonically
1445    increasing.  */
1446 typedef struct cp_parser_expression_stack_entry
1447   cp_parser_expression_stack[NUM_PREC_VALUES];
1448
1449 /* Context that is saved and restored when parsing tentatively.  */
1450 typedef struct GTY (()) cp_parser_context {
1451   /* If this is a tentative parsing context, the status of the
1452      tentative parse.  */
1453   enum cp_parser_status_kind status;
1454   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1455      that are looked up in this context must be looked up both in the
1456      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457      the context of the containing expression.  */
1458   tree object_type;
1459
1460   /* The next parsing context in the stack.  */
1461   struct cp_parser_context *next;
1462 } cp_parser_context;
1463
1464 /* Prototypes.  */
1465
1466 /* Constructors and destructors.  */
1467
1468 static cp_parser_context *cp_parser_context_new
1469   (cp_parser_context *);
1470
1471 /* Class variables.  */
1472
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1474
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476    Transformed into an associative array (binops_by_token) by
1477    cp_parser_new.  */
1478
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1482
1483   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1486
1487   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1489
1490   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1492
1493   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1497
1498   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1500
1501   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1502
1503   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1504
1505   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1506
1507   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1508
1509   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1510 };
1511
1512 /* The same as binops, but initialized by cp_parser_new so that
1513    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1514    for speed.  */
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1516
1517 /* Constructors and destructors.  */
1518
1519 /* Construct a new context.  The context below this one on the stack
1520    is given by NEXT.  */
1521
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1524 {
1525   cp_parser_context *context;
1526
1527   /* Allocate the storage.  */
1528   if (cp_parser_context_free_list != NULL)
1529     {
1530       /* Pull the first entry from the free list.  */
1531       context = cp_parser_context_free_list;
1532       cp_parser_context_free_list = context->next;
1533       memset (context, 0, sizeof (*context));
1534     }
1535   else
1536     context = ggc_alloc_cleared_cp_parser_context ();
1537
1538   /* No errors have occurred yet in this context.  */
1539   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540   /* If this is not the bottommost context, copy information that we
1541      need from the previous context.  */
1542   if (next)
1543     {
1544       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545          expression, then we are parsing one in this context, too.  */
1546       context->object_type = next->object_type;
1547       /* Thread the stack.  */
1548       context->next = next;
1549     }
1550
1551   return context;
1552 }
1553
1554 /* An entry in a queue of function arguments that require post-processing.  */
1555
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557   /* The current_class_type when we parsed this arg.  */
1558   tree class_type;
1559
1560   /* The function decl itself.  */
1561   tree decl;
1562 } cp_default_arg_entry;
1563
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1566
1567 /* An entry in a stack for member functions of local classes.  */
1568
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570   /* Functions with default arguments that require post-processing.
1571      Functions appear in this list in declaration order.  */
1572   VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1573
1574   /* Functions with defintions that require post-processing.  Functions
1575      appear in this list in declaration order.  */
1576   VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1578
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1581
1582 /* The cp_parser structure represents the C++ parser.  */
1583
1584 typedef struct GTY(()) cp_parser {
1585   /* The lexer from which we are obtaining tokens.  */
1586   cp_lexer *lexer;
1587
1588   /* The scope in which names should be looked up.  If NULL_TREE, then
1589      we look up names in the scope that is currently open in the
1590      source program.  If non-NULL, this is either a TYPE or
1591      NAMESPACE_DECL for the scope in which we should look.  It can
1592      also be ERROR_MARK, when we've parsed a bogus scope.
1593
1594      This value is not cleared automatically after a name is looked
1595      up, so we must be careful to clear it before starting a new look
1596      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1597      will look up `Z' in the scope of `X', rather than the current
1598      scope.)  Unfortunately, it is difficult to tell when name lookup
1599      is complete, because we sometimes peek at a token, look it up,
1600      and then decide not to consume it.   */
1601   tree scope;
1602
1603   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604      last lookup took place.  OBJECT_SCOPE is used if an expression
1605      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606      respectively.  QUALIFYING_SCOPE is used for an expression of the
1607      form "X::Y"; it refers to X.  */
1608   tree object_scope;
1609   tree qualifying_scope;
1610
1611   /* A stack of parsing contexts.  All but the bottom entry on the
1612      stack will be tentative contexts.
1613
1614      We parse tentatively in order to determine which construct is in
1615      use in some situations.  For example, in order to determine
1616      whether a statement is an expression-statement or a
1617      declaration-statement we parse it tentatively as a
1618      declaration-statement.  If that fails, we then reparse the same
1619      token stream as an expression-statement.  */
1620   cp_parser_context *context;
1621
1622   /* True if we are parsing GNU C++.  If this flag is not set, then
1623      GNU extensions are not recognized.  */
1624   bool allow_gnu_extensions_p;
1625
1626   /* TRUE if the `>' token should be interpreted as the greater-than
1627      operator.  FALSE if it is the end of a template-id or
1628      template-parameter-list. In C++0x mode, this flag also applies to
1629      `>>' tokens, which are viewed as two consecutive `>' tokens when
1630      this flag is FALSE.  */
1631   bool greater_than_is_operator_p;
1632
1633   /* TRUE if default arguments are allowed within a parameter list
1634      that starts at this point. FALSE if only a gnu extension makes
1635      them permissible.  */
1636   bool default_arg_ok_p;
1637
1638   /* TRUE if we are parsing an integral constant-expression.  See
1639      [expr.const] for a precise definition.  */
1640   bool integral_constant_expression_p;
1641
1642   /* TRUE if we are parsing an integral constant-expression -- but a
1643      non-constant expression should be permitted as well.  This flag
1644      is used when parsing an array bound so that GNU variable-length
1645      arrays are tolerated.  */
1646   bool allow_non_integral_constant_expression_p;
1647
1648   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649      been seen that makes the expression non-constant.  */
1650   bool non_integral_constant_expression_p;
1651
1652   /* TRUE if local variable names and `this' are forbidden in the
1653      current context.  */
1654   bool local_variables_forbidden_p;
1655
1656   /* TRUE if the declaration we are parsing is part of a
1657      linkage-specification of the form `extern string-literal
1658      declaration'.  */
1659   bool in_unbraced_linkage_specification_p;
1660
1661   /* TRUE if we are presently parsing a declarator, after the
1662      direct-declarator.  */
1663   bool in_declarator_p;
1664
1665   /* TRUE if we are presently parsing a template-argument-list.  */
1666   bool in_template_argument_list_p;
1667
1668   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669      to IN_OMP_BLOCK if parsing OpenMP structured block and
1670      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1671      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672      iteration-statement, OpenMP block or loop within that switch.  */
1673 #define IN_SWITCH_STMT          1
1674 #define IN_ITERATION_STMT       2
1675 #define IN_OMP_BLOCK            4
1676 #define IN_OMP_FOR              8
1677 #define IN_IF_STMT             16
1678   unsigned char in_statement;
1679
1680   /* TRUE if we are presently parsing the body of a switch statement.
1681      Note that this doesn't quite overlap with in_statement above.
1682      The difference relates to giving the right sets of error messages:
1683      "case not in switch" vs "break statement used with OpenMP...".  */
1684   bool in_switch_statement_p;
1685
1686   /* TRUE if we are parsing a type-id in an expression context.  In
1687      such a situation, both "type (expr)" and "type (type)" are valid
1688      alternatives.  */
1689   bool in_type_id_in_expr_p;
1690
1691   /* TRUE if we are currently in a header file where declarations are
1692      implicitly extern "C".  */
1693   bool implicit_extern_c;
1694
1695   /* TRUE if strings in expressions should be translated to the execution
1696      character set.  */
1697   bool translate_strings_p;
1698
1699   /* TRUE if we are presently parsing the body of a function, but not
1700      a local class.  */
1701   bool in_function_body;
1702
1703   /* If non-NULL, then we are parsing a construct where new type
1704      definitions are not permitted.  The string stored here will be
1705      issued as an error message if a type is defined.  */
1706   const char *type_definition_forbidden_message;
1707
1708   /* A stack used for member functions of local classes.  The lists
1709      contained in an individual entry can only be processed once the
1710      outermost class being defined is complete.  */
1711   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1712
1713   /* The number of classes whose definitions are currently in
1714      progress.  */
1715   unsigned num_classes_being_defined;
1716
1717   /* The number of template parameter lists that apply directly to the
1718      current declaration.  */
1719   unsigned num_template_parameter_lists;
1720 } cp_parser;
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728
1729 static void
1730 push_unparsed_function_queues (cp_parser *parser)
1731 {
1732   VEC_safe_push (cp_unparsed_functions_entry, gc,
1733                  parser->unparsed_queues, NULL);
1734   unparsed_funs_with_default_args = NULL;
1735   unparsed_funs_with_definitions = make_tree_vector ();
1736 }
1737
1738 static void
1739 pop_unparsed_function_queues (cp_parser *parser)
1740 {
1741   release_tree_vector (unparsed_funs_with_definitions);
1742   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1743 }
1744
1745 /* Prototypes.  */
1746
1747 /* Constructors and destructors.  */
1748
1749 static cp_parser *cp_parser_new
1750   (void);
1751
1752 /* Routines to parse various constructs.
1753
1754    Those that return `tree' will return the error_mark_node (rather
1755    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756    Sometimes, they will return an ordinary node if error-recovery was
1757    attempted, even though a parse error occurred.  So, to check
1758    whether or not a parse error occurred, you should always use
1759    cp_parser_error_occurred.  If the construct is optional (indicated
1760    either by an `_opt' in the name of the function that does the
1761    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762    the construct is not present.  */
1763
1764 /* Lexical conventions [gram.lex]  */
1765
1766 static tree cp_parser_identifier
1767   (cp_parser *);
1768 static tree cp_parser_string_literal
1769   (cp_parser *, bool, bool);
1770
1771 /* Basic concepts [gram.basic]  */
1772
1773 static bool cp_parser_translation_unit
1774   (cp_parser *);
1775
1776 /* Expressions [gram.expr]  */
1777
1778 static tree cp_parser_primary_expression
1779   (cp_parser *, bool, bool, bool, cp_id_kind *);
1780 static tree cp_parser_id_expression
1781   (cp_parser *, bool, bool, bool *, bool, bool);
1782 static tree cp_parser_unqualified_id
1783   (cp_parser *, bool, bool, bool, bool);
1784 static tree cp_parser_nested_name_specifier_opt
1785   (cp_parser *, bool, bool, bool, bool);
1786 static tree cp_parser_nested_name_specifier
1787   (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_qualifying_entity
1789   (cp_parser *, bool, bool, bool, bool, bool);
1790 static tree cp_parser_postfix_expression
1791   (cp_parser *, bool, bool, bool, cp_id_kind *);
1792 static tree cp_parser_postfix_open_square_expression
1793   (cp_parser *, tree, bool);
1794 static tree cp_parser_postfix_dot_deref_expression
1795   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1796 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1797   (cp_parser *, int, bool, bool, bool *);
1798 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1799 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1800 static void cp_parser_pseudo_destructor_name
1801   (cp_parser *, tree *, tree *);
1802 static tree cp_parser_unary_expression
1803   (cp_parser *, bool, bool, cp_id_kind *);
1804 static enum tree_code cp_parser_unary_operator
1805   (cp_token *);
1806 static tree cp_parser_new_expression
1807   (cp_parser *);
1808 static VEC(tree,gc) *cp_parser_new_placement
1809   (cp_parser *);
1810 static tree cp_parser_new_type_id
1811   (cp_parser *, tree *);
1812 static cp_declarator *cp_parser_new_declarator_opt
1813   (cp_parser *);
1814 static cp_declarator *cp_parser_direct_new_declarator
1815   (cp_parser *);
1816 static VEC(tree,gc) *cp_parser_new_initializer
1817   (cp_parser *);
1818 static tree cp_parser_delete_expression
1819   (cp_parser *);
1820 static tree cp_parser_cast_expression
1821   (cp_parser *, bool, bool, cp_id_kind *);
1822 static tree cp_parser_binary_expression
1823   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1824 static tree cp_parser_question_colon_clause
1825   (cp_parser *, tree);
1826 static tree cp_parser_assignment_expression
1827   (cp_parser *, bool, cp_id_kind *);
1828 static enum tree_code cp_parser_assignment_operator_opt
1829   (cp_parser *);
1830 static tree cp_parser_expression
1831   (cp_parser *, bool, cp_id_kind *);
1832 static tree cp_parser_constant_expression
1833   (cp_parser *, bool, bool *);
1834 static tree cp_parser_builtin_offsetof
1835   (cp_parser *);
1836 static tree cp_parser_lambda_expression
1837   (cp_parser *);
1838 static void cp_parser_lambda_introducer
1839   (cp_parser *, tree);
1840 static void cp_parser_lambda_declarator_opt
1841   (cp_parser *, tree);
1842 static void cp_parser_lambda_body
1843   (cp_parser *, tree);
1844
1845 /* Statements [gram.stmt.stmt]  */
1846
1847 static void cp_parser_statement
1848   (cp_parser *, tree, bool, bool *);
1849 static void cp_parser_label_for_labeled_statement
1850   (cp_parser *);
1851 static tree cp_parser_expression_statement
1852   (cp_parser *, tree);
1853 static tree cp_parser_compound_statement
1854   (cp_parser *, tree, bool);
1855 static void cp_parser_statement_seq_opt
1856   (cp_parser *, tree);
1857 static tree cp_parser_selection_statement
1858   (cp_parser *, bool *);
1859 static tree cp_parser_condition
1860   (cp_parser *);
1861 static tree cp_parser_iteration_statement
1862   (cp_parser *);
1863 static void cp_parser_for_init_statement
1864   (cp_parser *);
1865 static tree  cp_parser_c_for
1866   (cp_parser *);
1867 static tree  cp_parser_range_for
1868   (cp_parser *);
1869 static tree cp_parser_jump_statement
1870   (cp_parser *);
1871 static void cp_parser_declaration_statement
1872   (cp_parser *);
1873
1874 static tree cp_parser_implicitly_scoped_statement
1875   (cp_parser *, bool *);
1876 static void cp_parser_already_scoped_statement
1877   (cp_parser *);
1878
1879 /* Declarations [gram.dcl.dcl] */
1880
1881 static void cp_parser_declaration_seq_opt
1882   (cp_parser *);
1883 static void cp_parser_declaration
1884   (cp_parser *);
1885 static void cp_parser_block_declaration
1886   (cp_parser *, bool);
1887 static void cp_parser_simple_declaration
1888   (cp_parser *, bool);
1889 static void cp_parser_decl_specifier_seq
1890   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1891 static tree cp_parser_storage_class_specifier_opt
1892   (cp_parser *);
1893 static tree cp_parser_function_specifier_opt
1894   (cp_parser *, cp_decl_specifier_seq *);
1895 static tree cp_parser_type_specifier
1896   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1897    int *, bool *);
1898 static tree cp_parser_simple_type_specifier
1899   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1900 static tree cp_parser_type_name
1901   (cp_parser *);
1902 static tree cp_parser_nonclass_name 
1903   (cp_parser* parser);
1904 static tree cp_parser_elaborated_type_specifier
1905   (cp_parser *, bool, bool);
1906 static tree cp_parser_enum_specifier
1907   (cp_parser *);
1908 static void cp_parser_enumerator_list
1909   (cp_parser *, tree);
1910 static void cp_parser_enumerator_definition
1911   (cp_parser *, tree);
1912 static tree cp_parser_namespace_name
1913   (cp_parser *);
1914 static void cp_parser_namespace_definition
1915   (cp_parser *);
1916 static void cp_parser_namespace_body
1917   (cp_parser *);
1918 static tree cp_parser_qualified_namespace_specifier
1919   (cp_parser *);
1920 static void cp_parser_namespace_alias_definition
1921   (cp_parser *);
1922 static bool cp_parser_using_declaration
1923   (cp_parser *, bool);
1924 static void cp_parser_using_directive
1925   (cp_parser *);
1926 static void cp_parser_asm_definition
1927   (cp_parser *);
1928 static void cp_parser_linkage_specification
1929   (cp_parser *);
1930 static void cp_parser_static_assert
1931   (cp_parser *, bool);
1932 static tree cp_parser_decltype
1933   (cp_parser *);
1934
1935 /* Declarators [gram.dcl.decl] */
1936
1937 static tree cp_parser_init_declarator
1938   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1939 static cp_declarator *cp_parser_declarator
1940   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1941 static cp_declarator *cp_parser_direct_declarator
1942   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1943 static enum tree_code cp_parser_ptr_operator
1944   (cp_parser *, tree *, cp_cv_quals *);
1945 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1946   (cp_parser *);
1947 static tree cp_parser_late_return_type_opt
1948   (cp_parser *);
1949 static tree cp_parser_declarator_id
1950   (cp_parser *, bool);
1951 static tree cp_parser_type_id
1952   (cp_parser *);
1953 static tree cp_parser_template_type_arg
1954   (cp_parser *);
1955 static tree cp_parser_trailing_type_id (cp_parser *);
1956 static tree cp_parser_type_id_1
1957   (cp_parser *, bool, bool);
1958 static void cp_parser_type_specifier_seq
1959   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1960 static tree cp_parser_parameter_declaration_clause
1961   (cp_parser *);
1962 static tree cp_parser_parameter_declaration_list
1963   (cp_parser *, bool *);
1964 static cp_parameter_declarator *cp_parser_parameter_declaration
1965   (cp_parser *, bool, bool *);
1966 static tree cp_parser_default_argument 
1967   (cp_parser *, bool);
1968 static void cp_parser_function_body
1969   (cp_parser *);
1970 static tree cp_parser_initializer
1971   (cp_parser *, bool *, bool *);
1972 static tree cp_parser_initializer_clause
1973   (cp_parser *, bool *);
1974 static tree cp_parser_braced_list
1975   (cp_parser*, bool*);
1976 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1977   (cp_parser *, bool *);
1978
1979 static bool cp_parser_ctor_initializer_opt_and_function_body
1980   (cp_parser *);
1981
1982 /* Classes [gram.class] */
1983
1984 static tree cp_parser_class_name
1985   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1986 static tree cp_parser_class_specifier
1987   (cp_parser *);
1988 static tree cp_parser_class_head
1989   (cp_parser *, bool *, tree *, tree *);
1990 static enum tag_types cp_parser_class_key
1991   (cp_parser *);
1992 static void cp_parser_member_specification_opt
1993   (cp_parser *);
1994 static void cp_parser_member_declaration
1995   (cp_parser *);
1996 static tree cp_parser_pure_specifier
1997   (cp_parser *);
1998 static tree cp_parser_constant_initializer
1999   (cp_parser *);
2000
2001 /* Derived classes [gram.class.derived] */
2002
2003 static tree cp_parser_base_clause
2004   (cp_parser *);
2005 static tree cp_parser_base_specifier
2006   (cp_parser *);
2007
2008 /* Special member functions [gram.special] */
2009
2010 static tree cp_parser_conversion_function_id
2011   (cp_parser *);
2012 static tree cp_parser_conversion_type_id
2013   (cp_parser *);
2014 static cp_declarator *cp_parser_conversion_declarator_opt
2015   (cp_parser *);
2016 static bool cp_parser_ctor_initializer_opt
2017   (cp_parser *);
2018 static void cp_parser_mem_initializer_list
2019   (cp_parser *);
2020 static tree cp_parser_mem_initializer
2021   (cp_parser *);
2022 static tree cp_parser_mem_initializer_id
2023   (cp_parser *);
2024
2025 /* Overloading [gram.over] */
2026
2027 static tree cp_parser_operator_function_id
2028   (cp_parser *);
2029 static tree cp_parser_operator
2030   (cp_parser *);
2031
2032 /* Templates [gram.temp] */
2033
2034 static void cp_parser_template_declaration
2035   (cp_parser *, bool);
2036 static tree cp_parser_template_parameter_list
2037   (cp_parser *);
2038 static tree cp_parser_template_parameter
2039   (cp_parser *, bool *, bool *);
2040 static tree cp_parser_type_parameter
2041   (cp_parser *, bool *);
2042 static tree cp_parser_template_id
2043   (cp_parser *, bool, bool, bool);
2044 static tree cp_parser_template_name
2045   (cp_parser *, bool, bool, bool, bool *);
2046 static tree cp_parser_template_argument_list
2047   (cp_parser *);
2048 static tree cp_parser_template_argument
2049   (cp_parser *);
2050 static void cp_parser_explicit_instantiation
2051   (cp_parser *);
2052 static void cp_parser_explicit_specialization
2053   (cp_parser *);
2054
2055 /* Exception handling [gram.exception] */
2056
2057 static tree cp_parser_try_block
2058   (cp_parser *);
2059 static bool cp_parser_function_try_block
2060   (cp_parser *);
2061 static void cp_parser_handler_seq
2062   (cp_parser *);
2063 static void cp_parser_handler
2064   (cp_parser *);
2065 static tree cp_parser_exception_declaration
2066   (cp_parser *);
2067 static tree cp_parser_throw_expression
2068   (cp_parser *);
2069 static tree cp_parser_exception_specification_opt
2070   (cp_parser *);
2071 static tree cp_parser_type_id_list
2072   (cp_parser *);
2073
2074 /* GNU Extensions */
2075
2076 static tree cp_parser_asm_specification_opt
2077   (cp_parser *);
2078 static tree cp_parser_asm_operand_list
2079   (cp_parser *);
2080 static tree cp_parser_asm_clobber_list
2081   (cp_parser *);
2082 static tree cp_parser_asm_label_list
2083   (cp_parser *);
2084 static tree cp_parser_attributes_opt
2085   (cp_parser *);
2086 static tree cp_parser_attribute_list
2087   (cp_parser *);
2088 static bool cp_parser_extension_opt
2089   (cp_parser *, int *);
2090 static void cp_parser_label_declaration
2091   (cp_parser *);
2092
2093 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2094 static bool cp_parser_pragma
2095   (cp_parser *, enum pragma_context);
2096
2097 /* Objective-C++ Productions */
2098
2099 static tree cp_parser_objc_message_receiver
2100   (cp_parser *);
2101 static tree cp_parser_objc_message_args
2102   (cp_parser *);
2103 static tree cp_parser_objc_message_expression
2104   (cp_parser *);
2105 static tree cp_parser_objc_encode_expression
2106   (cp_parser *);
2107 static tree cp_parser_objc_defs_expression
2108   (cp_parser *);
2109 static tree cp_parser_objc_protocol_expression
2110   (cp_parser *);
2111 static tree cp_parser_objc_selector_expression
2112   (cp_parser *);
2113 static tree cp_parser_objc_expression
2114   (cp_parser *);
2115 static bool cp_parser_objc_selector_p
2116   (enum cpp_ttype);
2117 static tree cp_parser_objc_selector
2118   (cp_parser *);
2119 static tree cp_parser_objc_protocol_refs_opt
2120   (cp_parser *);
2121 static void cp_parser_objc_declaration
2122   (cp_parser *, tree);
2123 static tree cp_parser_objc_statement
2124   (cp_parser *);
2125 static bool cp_parser_objc_valid_prefix_attributes
2126   (cp_parser *, tree *);
2127 static void cp_parser_objc_at_property_declaration 
2128   (cp_parser *) ;
2129 static void cp_parser_objc_at_synthesize_declaration 
2130   (cp_parser *) ;
2131 static void cp_parser_objc_at_dynamic_declaration
2132   (cp_parser *) ;
2133 static tree cp_parser_objc_struct_declaration
2134   (cp_parser *) ;
2135
2136 /* Utility Routines */
2137
2138 static tree cp_parser_lookup_name
2139   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2140 static tree cp_parser_lookup_name_simple
2141   (cp_parser *, tree, location_t);
2142 static tree cp_parser_maybe_treat_template_as_class
2143   (tree, bool);
2144 static bool cp_parser_check_declarator_template_parameters
2145   (cp_parser *, cp_declarator *, location_t);
2146 static bool cp_parser_check_template_parameters
2147   (cp_parser *, unsigned, location_t, cp_declarator *);
2148 static tree cp_parser_simple_cast_expression
2149   (cp_parser *);
2150 static tree cp_parser_global_scope_opt
2151   (cp_parser *, bool);
2152 static bool cp_parser_constructor_declarator_p
2153   (cp_parser *, bool);
2154 static tree cp_parser_function_definition_from_specifiers_and_declarator
2155   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2156 static tree cp_parser_function_definition_after_declarator
2157   (cp_parser *, bool);
2158 static void cp_parser_template_declaration_after_export
2159   (cp_parser *, bool);
2160 static void cp_parser_perform_template_parameter_access_checks
2161   (VEC (deferred_access_check,gc)*);
2162 static tree cp_parser_single_declaration
2163   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2164 static tree cp_parser_functional_cast
2165   (cp_parser *, tree);
2166 static tree cp_parser_save_member_function_body
2167   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2168 static tree cp_parser_enclosed_template_argument_list
2169   (cp_parser *);
2170 static void cp_parser_save_default_args
2171   (cp_parser *, tree);
2172 static void cp_parser_late_parsing_for_member
2173   (cp_parser *, tree);
2174 static void cp_parser_late_parsing_default_args
2175   (cp_parser *, tree);
2176 static tree cp_parser_sizeof_operand
2177   (cp_parser *, enum rid);
2178 static tree cp_parser_trait_expr
2179   (cp_parser *, enum rid);
2180 static bool cp_parser_declares_only_class_p
2181   (cp_parser *);
2182 static void cp_parser_set_storage_class
2183   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2184 static void cp_parser_set_decl_spec_type
2185   (cp_decl_specifier_seq *, tree, location_t, bool);
2186 static bool cp_parser_friend_p
2187   (const cp_decl_specifier_seq *);
2188 static void cp_parser_required_error
2189   (cp_parser *, required_token, bool);
2190 static cp_token *cp_parser_require
2191   (cp_parser *, enum cpp_ttype, required_token);
2192 static cp_token *cp_parser_require_keyword
2193   (cp_parser *, enum rid, required_token);
2194 static bool cp_parser_token_starts_function_definition_p
2195   (cp_token *);
2196 static bool cp_parser_next_token_starts_class_definition_p
2197   (cp_parser *);
2198 static bool cp_parser_next_token_ends_template_argument_p
2199   (cp_parser *);
2200 static bool cp_parser_nth_token_starts_template_argument_list_p
2201   (cp_parser *, size_t);
2202 static enum tag_types cp_parser_token_is_class_key
2203   (cp_token *);
2204 static void cp_parser_check_class_key
2205   (enum tag_types, tree type);
2206 static void cp_parser_check_access_in_redeclaration
2207   (tree type, location_t location);
2208 static bool cp_parser_optional_template_keyword
2209   (cp_parser *);
2210 static void cp_parser_pre_parsed_nested_name_specifier
2211   (cp_parser *);
2212 static bool cp_parser_cache_group
2213   (cp_parser *, enum cpp_ttype, unsigned);
2214 static void cp_parser_parse_tentatively
2215   (cp_parser *);
2216 static void cp_parser_commit_to_tentative_parse
2217   (cp_parser *);
2218 static void cp_parser_abort_tentative_parse
2219   (cp_parser *);
2220 static bool cp_parser_parse_definitely
2221   (cp_parser *);
2222 static inline bool cp_parser_parsing_tentatively
2223   (cp_parser *);
2224 static bool cp_parser_uncommitted_to_tentative_parse_p
2225   (cp_parser *);
2226 static void cp_parser_error
2227   (cp_parser *, const char *);
2228 static void cp_parser_name_lookup_error
2229   (cp_parser *, tree, tree, name_lookup_error, location_t);
2230 static bool cp_parser_simulate_error
2231   (cp_parser *);
2232 static bool cp_parser_check_type_definition
2233   (cp_parser *);
2234 static void cp_parser_check_for_definition_in_return_type
2235   (cp_declarator *, tree, location_t type_location);
2236 static void cp_parser_check_for_invalid_template_id
2237   (cp_parser *, tree, location_t location);
2238 static bool cp_parser_non_integral_constant_expression
2239   (cp_parser *, non_integral_constant);
2240 static void cp_parser_diagnose_invalid_type_name
2241   (cp_parser *, tree, tree, location_t);
2242 static bool cp_parser_parse_and_diagnose_invalid_type_name
2243   (cp_parser *);
2244 static int cp_parser_skip_to_closing_parenthesis
2245   (cp_parser *, bool, bool, bool);
2246 static void cp_parser_skip_to_end_of_statement
2247   (cp_parser *);
2248 static void cp_parser_consume_semicolon_at_end_of_statement
2249   (cp_parser *);
2250 static void cp_parser_skip_to_end_of_block_or_statement
2251   (cp_parser *);
2252 static bool cp_parser_skip_to_closing_brace
2253   (cp_parser *);
2254 static void cp_parser_skip_to_end_of_template_parameter_list
2255   (cp_parser *);
2256 static void cp_parser_skip_to_pragma_eol
2257   (cp_parser*, cp_token *);
2258 static bool cp_parser_error_occurred
2259   (cp_parser *);
2260 static bool cp_parser_allow_gnu_extensions_p
2261   (cp_parser *);
2262 static bool cp_parser_is_string_literal
2263   (cp_token *);
2264 static bool cp_parser_is_keyword
2265   (cp_token *, enum rid);
2266 static tree cp_parser_make_typename_type
2267   (cp_parser *, tree, tree, location_t location);
2268 static cp_declarator * cp_parser_make_indirect_declarator
2269   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2270
2271 /* Returns nonzero if we are parsing tentatively.  */
2272
2273 static inline bool
2274 cp_parser_parsing_tentatively (cp_parser* parser)
2275 {
2276   return parser->context->next != NULL;
2277 }
2278
2279 /* Returns nonzero if TOKEN is a string literal.  */
2280
2281 static bool
2282 cp_parser_is_string_literal (cp_token* token)
2283 {
2284   return (token->type == CPP_STRING ||
2285           token->type == CPP_STRING16 ||
2286           token->type == CPP_STRING32 ||
2287           token->type == CPP_WSTRING ||
2288           token->type == CPP_UTF8STRING);
2289 }
2290
2291 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2292
2293 static bool
2294 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2295 {
2296   return token->keyword == keyword;
2297 }
2298
2299 /* If not parsing tentatively, issue a diagnostic of the form
2300       FILE:LINE: MESSAGE before TOKEN
2301    where TOKEN is the next token in the input stream.  MESSAGE
2302    (specified by the caller) is usually of the form "expected
2303    OTHER-TOKEN".  */
2304
2305 static void
2306 cp_parser_error (cp_parser* parser, const char* gmsgid)
2307 {
2308   if (!cp_parser_simulate_error (parser))
2309     {
2310       cp_token *token = cp_lexer_peek_token (parser->lexer);
2311       /* This diagnostic makes more sense if it is tagged to the line
2312          of the token we just peeked at.  */
2313       cp_lexer_set_source_position_from_token (token);
2314
2315       if (token->type == CPP_PRAGMA)
2316         {
2317           error_at (token->location,
2318                     "%<#pragma%> is not allowed here");
2319           cp_parser_skip_to_pragma_eol (parser, token);
2320           return;
2321         }
2322
2323       c_parse_error (gmsgid,
2324                      /* Because c_parser_error does not understand
2325                         CPP_KEYWORD, keywords are treated like
2326                         identifiers.  */
2327                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2328                      token->u.value, token->flags);
2329     }
2330 }
2331
2332 /* Issue an error about name-lookup failing.  NAME is the
2333    IDENTIFIER_NODE DECL is the result of
2334    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2335    the thing that we hoped to find.  */
2336
2337 static void
2338 cp_parser_name_lookup_error (cp_parser* parser,
2339                              tree name,
2340                              tree decl,
2341                              name_lookup_error desired,
2342                              location_t location)
2343 {
2344   /* If name lookup completely failed, tell the user that NAME was not
2345      declared.  */
2346   if (decl == error_mark_node)
2347     {
2348       if (parser->scope && parser->scope != global_namespace)
2349         error_at (location, "%<%E::%E%> has not been declared",
2350                   parser->scope, name);
2351       else if (parser->scope == global_namespace)
2352         error_at (location, "%<::%E%> has not been declared", name);
2353       else if (parser->object_scope
2354                && !CLASS_TYPE_P (parser->object_scope))
2355         error_at (location, "request for member %qE in non-class type %qT",
2356                   name, parser->object_scope);
2357       else if (parser->object_scope)
2358         error_at (location, "%<%T::%E%> has not been declared",
2359                   parser->object_scope, name);
2360       else
2361         error_at (location, "%qE has not been declared", name);
2362     }
2363   else if (parser->scope && parser->scope != global_namespace)
2364     {
2365       switch (desired)
2366         {
2367           case NLE_TYPE:
2368             error_at (location, "%<%E::%E%> is not a type",
2369                                 parser->scope, name);
2370             break;
2371           case NLE_CXX98:
2372             error_at (location, "%<%E::%E%> is not a class or namespace",
2373                                 parser->scope, name);
2374             break;
2375           case NLE_NOT_CXX98:
2376             error_at (location,
2377                       "%<%E::%E%> is not a class, namespace, or enumeration",
2378                       parser->scope, name);
2379             break;
2380           default:
2381             gcc_unreachable ();
2382             
2383         }
2384     }
2385   else if (parser->scope == global_namespace)
2386     {
2387       switch (desired)
2388         {
2389           case NLE_TYPE:
2390             error_at (location, "%<::%E%> is not a type", name);
2391             break;
2392           case NLE_CXX98:
2393             error_at (location, "%<::%E%> is not a class or namespace", name);
2394             break;
2395           case NLE_NOT_CXX98:
2396             error_at (location,
2397                       "%<::%E%> is not a class, namespace, or enumeration",
2398                       name);
2399             break;
2400           default:
2401             gcc_unreachable ();
2402         }
2403     }
2404   else
2405     {
2406       switch (desired)
2407         {
2408           case NLE_TYPE:
2409             error_at (location, "%qE is not a type", name);
2410             break;
2411           case NLE_CXX98:
2412             error_at (location, "%qE is not a class or namespace", name);
2413             break;
2414           case NLE_NOT_CXX98:
2415             error_at (location,
2416                       "%qE is not a class, namespace, or enumeration", name);
2417             break;
2418           default:
2419             gcc_unreachable ();
2420         }
2421     }
2422 }
2423
2424 /* If we are parsing tentatively, remember that an error has occurred
2425    during this tentative parse.  Returns true if the error was
2426    simulated; false if a message should be issued by the caller.  */
2427
2428 static bool
2429 cp_parser_simulate_error (cp_parser* parser)
2430 {
2431   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2432     {
2433       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2434       return true;
2435     }
2436   return false;
2437 }
2438
2439 /* Check for repeated decl-specifiers.  */
2440
2441 static void
2442 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2443                            location_t location)
2444 {
2445   int ds;
2446
2447   for (ds = ds_first; ds != ds_last; ++ds)
2448     {
2449       unsigned count = decl_specs->specs[ds];
2450       if (count < 2)
2451         continue;
2452       /* The "long" specifier is a special case because of "long long".  */
2453       if (ds == ds_long)
2454         {
2455           if (count > 2)
2456             error_at (location, "%<long long long%> is too long for GCC");
2457           else 
2458             pedwarn_cxx98 (location, OPT_Wlong_long, 
2459                            "ISO C++ 1998 does not support %<long long%>");
2460         }
2461       else if (count > 1)
2462         {
2463           static const char *const decl_spec_names[] = {
2464             "signed",
2465             "unsigned",
2466             "short",
2467             "long",
2468             "const",
2469             "volatile",
2470             "restrict",
2471             "inline",
2472             "virtual",
2473             "explicit",
2474             "friend",
2475             "typedef",
2476             "constexpr",
2477             "__complex",
2478             "__thread"
2479           };
2480           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2481         }
2482     }
2483 }
2484
2485 /* This function is called when a type is defined.  If type
2486    definitions are forbidden at this point, an error message is
2487    issued.  */
2488
2489 static bool
2490 cp_parser_check_type_definition (cp_parser* parser)
2491 {
2492   /* If types are forbidden here, issue a message.  */
2493   if (parser->type_definition_forbidden_message)
2494     {
2495       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2496          in the message need to be interpreted.  */
2497       error (parser->type_definition_forbidden_message);
2498       return false;
2499     }
2500   return true;
2501 }
2502
2503 /* This function is called when the DECLARATOR is processed.  The TYPE
2504    was a type defined in the decl-specifiers.  If it is invalid to
2505    define a type in the decl-specifiers for DECLARATOR, an error is
2506    issued. TYPE_LOCATION is the location of TYPE and is used
2507    for error reporting.  */
2508
2509 static void
2510 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2511                                                tree type, location_t type_location)
2512 {
2513   /* [dcl.fct] forbids type definitions in return types.
2514      Unfortunately, it's not easy to know whether or not we are
2515      processing a return type until after the fact.  */
2516   while (declarator
2517          && (declarator->kind == cdk_pointer
2518              || declarator->kind == cdk_reference
2519              || declarator->kind == cdk_ptrmem))
2520     declarator = declarator->declarator;
2521   if (declarator
2522       && declarator->kind == cdk_function)
2523     {
2524       error_at (type_location,
2525                 "new types may not be defined in a return type");
2526       inform (type_location, 
2527               "(perhaps a semicolon is missing after the definition of %qT)",
2528               type);
2529     }
2530 }
2531
2532 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2533    "<" in any valid C++ program.  If the next token is indeed "<",
2534    issue a message warning the user about what appears to be an
2535    invalid attempt to form a template-id. LOCATION is the location
2536    of the type-specifier (TYPE) */
2537
2538 static void
2539 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2540                                          tree type, location_t location)
2541 {
2542   cp_token_position start = 0;
2543
2544   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2545     {
2546       if (TYPE_P (type))
2547         error_at (location, "%qT is not a template", type);
2548       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2549         error_at (location, "%qE is not a template", type);
2550       else
2551         error_at (location, "invalid template-id");
2552       /* Remember the location of the invalid "<".  */
2553       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2554         start = cp_lexer_token_position (parser->lexer, true);
2555       /* Consume the "<".  */
2556       cp_lexer_consume_token (parser->lexer);
2557       /* Parse the template arguments.  */
2558       cp_parser_enclosed_template_argument_list (parser);
2559       /* Permanently remove the invalid template arguments so that
2560          this error message is not issued again.  */
2561       if (start)
2562         cp_lexer_purge_tokens_after (parser->lexer, start);
2563     }
2564 }
2565
2566 /* If parsing an integral constant-expression, issue an error message
2567    about the fact that THING appeared and return true.  Otherwise,
2568    return false.  In either case, set
2569    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2570
2571 static bool
2572 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2573                                             non_integral_constant thing)
2574 {
2575   parser->non_integral_constant_expression_p = true;
2576   if (parser->integral_constant_expression_p)
2577     {
2578       if (!parser->allow_non_integral_constant_expression_p)
2579         {
2580           const char *msg = NULL;
2581           switch (thing)
2582             {
2583               case NIC_FLOAT:
2584                 error ("floating-point literal "
2585                        "cannot appear in a constant-expression");
2586                 return true;
2587               case NIC_CAST:
2588                 error ("a cast to a type other than an integral or "
2589                        "enumeration type cannot appear in a "
2590                        "constant-expression");
2591                 return true;
2592               case NIC_TYPEID:
2593                 error ("%<typeid%> operator "
2594                        "cannot appear in a constant-expression");
2595                 return true;
2596               case NIC_NCC:
2597                 error ("non-constant compound literals "
2598                        "cannot appear in a constant-expression");
2599                 return true;
2600               case NIC_FUNC_CALL:
2601                 error ("a function call "
2602                        "cannot appear in a constant-expression");
2603                 return true;
2604               case NIC_INC:
2605                 error ("an increment "
2606                        "cannot appear in a constant-expression");
2607                 return true;
2608               case NIC_DEC:
2609                 error ("an decrement "
2610                        "cannot appear in a constant-expression");
2611                 return true;
2612               case NIC_ARRAY_REF:
2613                 error ("an array reference "
2614                        "cannot appear in a constant-expression");
2615                 return true;
2616               case NIC_ADDR_LABEL:
2617                 error ("the address of a label "
2618                        "cannot appear in a constant-expression");
2619                 return true;
2620               case NIC_OVERLOADED:
2621                 error ("calls to overloaded operators "
2622                        "cannot appear in a constant-expression");
2623                 return true;
2624               case NIC_ASSIGNMENT:
2625                 error ("an assignment cannot appear in a constant-expression");
2626                 return true;
2627               case NIC_COMMA:
2628                 error ("a comma operator "
2629                        "cannot appear in a constant-expression");
2630                 return true;
2631               case NIC_CONSTRUCTOR:
2632                 error ("a call to a constructor "
2633                        "cannot appear in a constant-expression");
2634                 return true;
2635               case NIC_THIS:
2636                 msg = "this";
2637                 break;
2638               case NIC_FUNC_NAME:
2639                 msg = "__FUNCTION__";
2640                 break;
2641               case NIC_PRETTY_FUNC:
2642                 msg = "__PRETTY_FUNCTION__";
2643                 break;
2644               case NIC_C99_FUNC:
2645                 msg = "__func__";
2646                 break;
2647               case NIC_VA_ARG:
2648                 msg = "va_arg";
2649                 break;
2650               case NIC_ARROW:
2651                 msg = "->";
2652                 break;
2653               case NIC_POINT:
2654                 msg = ".";
2655                 break;
2656               case NIC_STAR:
2657                 msg = "*";
2658                 break;
2659               case NIC_ADDR:
2660                 msg = "&";
2661                 break;
2662               case NIC_PREINCREMENT:
2663                 msg = "++";
2664                 break;
2665               case NIC_PREDECREMENT:
2666                 msg = "--";
2667                 break;
2668               case NIC_NEW:
2669                 msg = "new";
2670                 break;
2671               case NIC_DEL:
2672                 msg = "delete";
2673                 break;
2674               default:
2675                 gcc_unreachable ();
2676             }
2677           if (msg)
2678             error ("%qs cannot appear in a constant-expression", msg);
2679           return true;
2680         }
2681     }
2682   return false;
2683 }
2684
2685 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2686    qualifying scope (or NULL, if none) for ID.  This function commits
2687    to the current active tentative parse, if any.  (Otherwise, the
2688    problematic construct might be encountered again later, resulting
2689    in duplicate error messages.) LOCATION is the location of ID.  */
2690
2691 static void
2692 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2693                                       tree scope, tree id,
2694                                       location_t location)
2695 {
2696   tree decl, old_scope;
2697   /* Try to lookup the identifier.  */
2698   old_scope = parser->scope;
2699   parser->scope = scope;
2700   decl = cp_parser_lookup_name_simple (parser, id, location);
2701   parser->scope = old_scope;
2702   /* If the lookup found a template-name, it means that the user forgot
2703   to specify an argument list. Emit a useful error message.  */
2704   if (TREE_CODE (decl) == TEMPLATE_DECL)
2705     error_at (location,
2706               "invalid use of template-name %qE without an argument list",
2707               decl);
2708   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2709     error_at (location, "invalid use of destructor %qD as a type", id);
2710   else if (TREE_CODE (decl) == TYPE_DECL)
2711     /* Something like 'unsigned A a;'  */
2712     error_at (location, "invalid combination of multiple type-specifiers");
2713   else if (!parser->scope)
2714     {
2715       /* Issue an error message.  */
2716       error_at (location, "%qE does not name a type", id);
2717       /* If we're in a template class, it's possible that the user was
2718          referring to a type from a base class.  For example:
2719
2720            template <typename T> struct A { typedef T X; };
2721            template <typename T> struct B : public A<T> { X x; };
2722
2723          The user should have said "typename A<T>::X".  */
2724       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2725         inform (location, "C++0x %<constexpr%> only available with "
2726                 "-std=c++0x or -std=gnu++0x");
2727       else if (processing_template_decl && current_class_type
2728                && TYPE_BINFO (current_class_type))
2729         {
2730           tree b;
2731
2732           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2733                b;
2734                b = TREE_CHAIN (b))
2735             {
2736               tree base_type = BINFO_TYPE (b);
2737               if (CLASS_TYPE_P (base_type)
2738                   && dependent_type_p (base_type))
2739                 {
2740                   tree field;
2741                   /* Go from a particular instantiation of the
2742                      template (which will have an empty TYPE_FIELDs),
2743                      to the main version.  */
2744                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2745                   for (field = TYPE_FIELDS (base_type);
2746                        field;
2747                        field = DECL_CHAIN (field))
2748                     if (TREE_CODE (field) == TYPE_DECL
2749                         && DECL_NAME (field) == id)
2750                       {
2751                         inform (location, 
2752                                 "(perhaps %<typename %T::%E%> was intended)",
2753                                 BINFO_TYPE (b), id);
2754                         break;
2755                       }
2756                   if (field)
2757                     break;
2758                 }
2759             }
2760         }
2761     }
2762   /* Here we diagnose qualified-ids where the scope is actually correct,
2763      but the identifier does not resolve to a valid type name.  */
2764   else if (parser->scope != error_mark_node)
2765     {
2766       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2767         error_at (location, "%qE in namespace %qE does not name a type",
2768                   id, parser->scope);
2769       else if (CLASS_TYPE_P (parser->scope)
2770                && constructor_name_p (id, parser->scope))
2771         {
2772           /* A<T>::A<T>() */
2773           error_at (location, "%<%T::%E%> names the constructor, not"
2774                     " the type", parser->scope, id);
2775           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2776             error_at (location, "and %qT has no template constructors",
2777                       parser->scope);
2778         }
2779       else if (TYPE_P (parser->scope)
2780                && dependent_scope_p (parser->scope))
2781         error_at (location, "need %<typename%> before %<%T::%E%> because "
2782                   "%qT is a dependent scope",
2783                   parser->scope, id, parser->scope);
2784       else if (TYPE_P (parser->scope))
2785         error_at (location, "%qE in class %qT does not name a type",
2786                   id, parser->scope);
2787       else
2788         gcc_unreachable ();
2789     }
2790   cp_parser_commit_to_tentative_parse (parser);
2791 }
2792
2793 /* Check for a common situation where a type-name should be present,
2794    but is not, and issue a sensible error message.  Returns true if an
2795    invalid type-name was detected.
2796
2797    The situation handled by this function are variable declarations of the
2798    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2799    Usually, `ID' should name a type, but if we got here it means that it
2800    does not. We try to emit the best possible error message depending on
2801    how exactly the id-expression looks like.  */
2802
2803 static bool
2804 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2805 {
2806   tree id;
2807   cp_token *token = cp_lexer_peek_token (parser->lexer);
2808
2809   /* Avoid duplicate error about ambiguous lookup.  */
2810   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2811     {
2812       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2813       if (next->type == CPP_NAME && next->ambiguous_p)
2814         goto out;
2815     }
2816
2817   cp_parser_parse_tentatively (parser);
2818   id = cp_parser_id_expression (parser,
2819                                 /*template_keyword_p=*/false,
2820                                 /*check_dependency_p=*/true,
2821                                 /*template_p=*/NULL,
2822                                 /*declarator_p=*/true,
2823                                 /*optional_p=*/false);
2824   /* If the next token is a (, this is a function with no explicit return
2825      type, i.e. constructor, destructor or conversion op.  */
2826   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2827       || TREE_CODE (id) == TYPE_DECL)
2828     {
2829       cp_parser_abort_tentative_parse (parser);
2830       return false;
2831     }
2832   if (!cp_parser_parse_definitely (parser))
2833     return false;
2834
2835   /* Emit a diagnostic for the invalid type.  */
2836   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2837                                         id, token->location);
2838  out:
2839   /* If we aren't in the middle of a declarator (i.e. in a
2840      parameter-declaration-clause), skip to the end of the declaration;
2841      there's no point in trying to process it.  */
2842   if (!parser->in_declarator_p)
2843     cp_parser_skip_to_end_of_block_or_statement (parser);
2844   return true;
2845 }
2846
2847 /* Consume tokens up to, and including, the next non-nested closing `)'.
2848    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2849    are doing error recovery. Returns -1 if OR_COMMA is true and we
2850    found an unnested comma.  */
2851
2852 static int
2853 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2854                                        bool recovering,
2855                                        bool or_comma,
2856                                        bool consume_paren)
2857 {
2858   unsigned paren_depth = 0;
2859   unsigned brace_depth = 0;
2860   unsigned square_depth = 0;
2861
2862   if (recovering && !or_comma
2863       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2864     return 0;
2865
2866   while (true)
2867     {
2868       cp_token * token = cp_lexer_peek_token (parser->lexer);
2869
2870       switch (token->type)
2871         {
2872         case CPP_EOF:
2873         case CPP_PRAGMA_EOL:
2874           /* If we've run out of tokens, then there is no closing `)'.  */
2875           return 0;
2876
2877         /* This is good for lambda expression capture-lists.  */
2878         case CPP_OPEN_SQUARE:
2879           ++square_depth;
2880           break;
2881         case CPP_CLOSE_SQUARE:
2882           if (!square_depth--)
2883             return 0;
2884           break;
2885
2886         case CPP_SEMICOLON:
2887           /* This matches the processing in skip_to_end_of_statement.  */
2888           if (!brace_depth)
2889             return 0;
2890           break;
2891
2892         case CPP_OPEN_BRACE:
2893           ++brace_depth;
2894           break;
2895         case CPP_CLOSE_BRACE:
2896           if (!brace_depth--)
2897             return 0;
2898           break;
2899
2900         case CPP_COMMA:
2901           if (recovering && or_comma && !brace_depth && !paren_depth
2902               && !square_depth)
2903             return -1;
2904           break;
2905
2906         case CPP_OPEN_PAREN:
2907           if (!brace_depth)
2908             ++paren_depth;
2909           break;
2910
2911         case CPP_CLOSE_PAREN:
2912           if (!brace_depth && !paren_depth--)
2913             {
2914               if (consume_paren)
2915                 cp_lexer_consume_token (parser->lexer);
2916               return 1;
2917             }
2918           break;
2919
2920         default:
2921           break;
2922         }
2923
2924       /* Consume the token.  */
2925       cp_lexer_consume_token (parser->lexer);
2926     }
2927 }
2928
2929 /* Consume tokens until we reach the end of the current statement.
2930    Normally, that will be just before consuming a `;'.  However, if a
2931    non-nested `}' comes first, then we stop before consuming that.  */
2932
2933 static void
2934 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2935 {
2936   unsigned nesting_depth = 0;
2937
2938   while (true)
2939     {
2940       cp_token *token = cp_lexer_peek_token (parser->lexer);
2941
2942       switch (token->type)
2943         {
2944         case CPP_EOF:
2945         case CPP_PRAGMA_EOL:
2946           /* If we've run out of tokens, stop.  */
2947           return;
2948
2949         case CPP_SEMICOLON:
2950           /* If the next token is a `;', we have reached the end of the
2951              statement.  */
2952           if (!nesting_depth)
2953             return;
2954           break;
2955
2956         case CPP_CLOSE_BRACE:
2957           /* If this is a non-nested '}', stop before consuming it.
2958              That way, when confronted with something like:
2959
2960                { 3 + }
2961
2962              we stop before consuming the closing '}', even though we
2963              have not yet reached a `;'.  */
2964           if (nesting_depth == 0)
2965             return;
2966
2967           /* If it is the closing '}' for a block that we have
2968              scanned, stop -- but only after consuming the token.
2969              That way given:
2970
2971                 void f g () { ... }
2972                 typedef int I;
2973
2974              we will stop after the body of the erroneously declared
2975              function, but before consuming the following `typedef'
2976              declaration.  */
2977           if (--nesting_depth == 0)
2978             {
2979               cp_lexer_consume_token (parser->lexer);
2980               return;
2981             }
2982
2983         case CPP_OPEN_BRACE:
2984           ++nesting_depth;
2985           break;
2986
2987         default:
2988           break;
2989         }
2990
2991       /* Consume the token.  */
2992       cp_lexer_consume_token (parser->lexer);
2993     }
2994 }
2995
2996 /* This function is called at the end of a statement or declaration.
2997    If the next token is a semicolon, it is consumed; otherwise, error
2998    recovery is attempted.  */
2999
3000 static void
3001 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3002 {
3003   /* Look for the trailing `;'.  */
3004   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3005     {
3006       /* If there is additional (erroneous) input, skip to the end of
3007          the statement.  */
3008       cp_parser_skip_to_end_of_statement (parser);
3009       /* If the next token is now a `;', consume it.  */
3010       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3011         cp_lexer_consume_token (parser->lexer);
3012     }
3013 }
3014
3015 /* Skip tokens until we have consumed an entire block, or until we
3016    have consumed a non-nested `;'.  */
3017
3018 static void
3019 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3020 {
3021   int nesting_depth = 0;
3022
3023   while (nesting_depth >= 0)
3024     {
3025       cp_token *token = cp_lexer_peek_token (parser->lexer);
3026
3027       switch (token->type)
3028         {
3029         case CPP_EOF:
3030         case CPP_PRAGMA_EOL:
3031           /* If we've run out of tokens, stop.  */
3032           return;
3033
3034         case CPP_SEMICOLON:
3035           /* Stop if this is an unnested ';'. */
3036           if (!nesting_depth)
3037             nesting_depth = -1;
3038           break;
3039
3040         case CPP_CLOSE_BRACE:
3041           /* Stop if this is an unnested '}', or closes the outermost
3042              nesting level.  */
3043           nesting_depth--;
3044           if (nesting_depth < 0)
3045             return;
3046           if (!nesting_depth)
3047             nesting_depth = -1;
3048           break;
3049
3050         case CPP_OPEN_BRACE:
3051           /* Nest. */
3052           nesting_depth++;
3053           break;
3054
3055         default:
3056           break;
3057         }
3058
3059       /* Consume the token.  */
3060       cp_lexer_consume_token (parser->lexer);
3061     }
3062 }
3063
3064 /* Skip tokens until a non-nested closing curly brace is the next
3065    token, or there are no more tokens. Return true in the first case,
3066    false otherwise.  */
3067
3068 static bool
3069 cp_parser_skip_to_closing_brace (cp_parser *parser)
3070 {
3071   unsigned nesting_depth = 0;
3072
3073   while (true)
3074     {
3075       cp_token *token = cp_lexer_peek_token (parser->lexer);
3076
3077       switch (token->type)
3078         {
3079         case CPP_EOF:
3080         case CPP_PRAGMA_EOL:
3081           /* If we've run out of tokens, stop.  */
3082           return false;
3083
3084         case CPP_CLOSE_BRACE:
3085           /* If the next token is a non-nested `}', then we have reached
3086              the end of the current block.  */
3087           if (nesting_depth-- == 0)
3088             return true;
3089           break;
3090
3091         case CPP_OPEN_BRACE:
3092           /* If it the next token is a `{', then we are entering a new
3093              block.  Consume the entire block.  */
3094           ++nesting_depth;
3095           break;
3096
3097         default:
3098           break;
3099         }
3100
3101       /* Consume the token.  */
3102       cp_lexer_consume_token (parser->lexer);
3103     }
3104 }
3105
3106 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3107    parameter is the PRAGMA token, allowing us to purge the entire pragma
3108    sequence.  */
3109
3110 static void
3111 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3112 {
3113   cp_token *token;
3114
3115   parser->lexer->in_pragma = false;
3116
3117   do
3118     token = cp_lexer_consume_token (parser->lexer);
3119   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3120
3121   /* Ensure that the pragma is not parsed again.  */
3122   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3123 }
3124
3125 /* Require pragma end of line, resyncing with it as necessary.  The
3126    arguments are as for cp_parser_skip_to_pragma_eol.  */
3127
3128 static void
3129 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3130 {
3131   parser->lexer->in_pragma = false;
3132   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3133     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3134 }
3135
3136 /* This is a simple wrapper around make_typename_type. When the id is
3137    an unresolved identifier node, we can provide a superior diagnostic
3138    using cp_parser_diagnose_invalid_type_name.  */
3139
3140 static tree
3141 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3142                               tree id, location_t id_location)
3143 {
3144   tree result;
3145   if (TREE_CODE (id) == IDENTIFIER_NODE)
3146     {
3147       result = make_typename_type (scope, id, typename_type,
3148                                    /*complain=*/tf_none);
3149       if (result == error_mark_node)
3150         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3151       return result;
3152     }
3153   return make_typename_type (scope, id, typename_type, tf_error);
3154 }
3155
3156 /* This is a wrapper around the
3157    make_{pointer,ptrmem,reference}_declarator functions that decides
3158    which one to call based on the CODE and CLASS_TYPE arguments. The
3159    CODE argument should be one of the values returned by
3160    cp_parser_ptr_operator. */
3161 static cp_declarator *
3162 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3163                                     cp_cv_quals cv_qualifiers,
3164                                     cp_declarator *target)
3165 {
3166   if (code == ERROR_MARK)
3167     return cp_error_declarator;
3168
3169   if (code == INDIRECT_REF)
3170     if (class_type == NULL_TREE)
3171       return make_pointer_declarator (cv_qualifiers, target);
3172     else
3173       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3174   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3175     return make_reference_declarator (cv_qualifiers, target, false);
3176   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3177     return make_reference_declarator (cv_qualifiers, target, true);
3178   gcc_unreachable ();
3179 }
3180
3181 /* Create a new C++ parser.  */
3182
3183 static cp_parser *
3184 cp_parser_new (void)
3185 {
3186   cp_parser *parser;
3187   cp_lexer *lexer;
3188   unsigned i;
3189
3190   /* cp_lexer_new_main is called before doing GC allocation because
3191      cp_lexer_new_main might load a PCH file.  */
3192   lexer = cp_lexer_new_main ();
3193
3194   /* Initialize the binops_by_token so that we can get the tree
3195      directly from the token.  */
3196   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3197     binops_by_token[binops[i].token_type] = binops[i];
3198
3199   parser = ggc_alloc_cleared_cp_parser ();
3200   parser->lexer = lexer;
3201   parser->context = cp_parser_context_new (NULL);
3202
3203   /* For now, we always accept GNU extensions.  */
3204   parser->allow_gnu_extensions_p = 1;
3205
3206   /* The `>' token is a greater-than operator, not the end of a
3207      template-id.  */
3208   parser->greater_than_is_operator_p = true;
3209
3210   parser->default_arg_ok_p = true;
3211
3212   /* We are not parsing a constant-expression.  */
3213   parser->integral_constant_expression_p = false;
3214   parser->allow_non_integral_constant_expression_p = false;
3215   parser->non_integral_constant_expression_p = false;
3216
3217   /* Local variable names are not forbidden.  */
3218   parser->local_variables_forbidden_p = false;
3219
3220   /* We are not processing an `extern "C"' declaration.  */
3221   parser->in_unbraced_linkage_specification_p = false;
3222
3223   /* We are not processing a declarator.  */
3224   parser->in_declarator_p = false;
3225
3226   /* We are not processing a template-argument-list.  */
3227   parser->in_template_argument_list_p = false;
3228
3229   /* We are not in an iteration statement.  */
3230   parser->in_statement = 0;
3231
3232   /* We are not in a switch statement.  */
3233   parser->in_switch_statement_p = false;
3234
3235   /* We are not parsing a type-id inside an expression.  */
3236   parser->in_type_id_in_expr_p = false;
3237
3238   /* Declarations aren't implicitly extern "C".  */
3239   parser->implicit_extern_c = false;
3240
3241   /* String literals should be translated to the execution character set.  */
3242   parser->translate_strings_p = true;
3243
3244   /* We are not parsing a function body.  */
3245   parser->in_function_body = false;
3246
3247   /* The unparsed function queue is empty.  */
3248   push_unparsed_function_queues (parser);
3249
3250   /* There are no classes being defined.  */
3251   parser->num_classes_being_defined = 0;
3252
3253   /* No template parameters apply.  */
3254   parser->num_template_parameter_lists = 0;
3255
3256   return parser;
3257 }
3258
3259 /* Create a cp_lexer structure which will emit the tokens in CACHE
3260    and push it onto the parser's lexer stack.  This is used for delayed
3261    parsing of in-class method bodies and default arguments, and should
3262    not be confused with tentative parsing.  */
3263 static void
3264 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3265 {
3266   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3267   lexer->next = parser->lexer;
3268   parser->lexer = lexer;
3269
3270   /* Move the current source position to that of the first token in the
3271      new lexer.  */
3272   cp_lexer_set_source_position_from_token (lexer->next_token);
3273 }
3274
3275 /* Pop the top lexer off the parser stack.  This is never used for the
3276    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3277 static void
3278 cp_parser_pop_lexer (cp_parser *parser)
3279 {
3280   cp_lexer *lexer = parser->lexer;
3281   parser->lexer = lexer->next;
3282   cp_lexer_destroy (lexer);
3283
3284   /* Put the current source position back where it was before this
3285      lexer was pushed.  */
3286   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3287 }
3288
3289 /* Lexical conventions [gram.lex]  */
3290
3291 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3292    identifier.  */
3293
3294 static tree
3295 cp_parser_identifier (cp_parser* parser)
3296 {
3297   cp_token *token;
3298
3299   /* Look for the identifier.  */
3300   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3301   /* Return the value.  */
3302   return token ? token->u.value : error_mark_node;
3303 }
3304
3305 /* Parse a sequence of adjacent string constants.  Returns a
3306    TREE_STRING representing the combined, nul-terminated string
3307    constant.  If TRANSLATE is true, translate the string to the
3308    execution character set.  If WIDE_OK is true, a wide string is
3309    invalid here.
3310
3311    C++98 [lex.string] says that if a narrow string literal token is
3312    adjacent to a wide string literal token, the behavior is undefined.
3313    However, C99 6.4.5p4 says that this results in a wide string literal.
3314    We follow C99 here, for consistency with the C front end.
3315
3316    This code is largely lifted from lex_string() in c-lex.c.
3317
3318    FUTURE: ObjC++ will need to handle @-strings here.  */
3319 static tree
3320 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3321 {
3322   tree value;
3323   size_t count;
3324   struct obstack str_ob;
3325   cpp_string str, istr, *strs;
3326   cp_token *tok;
3327   enum cpp_ttype type;
3328
3329   tok = cp_lexer_peek_token (parser->lexer);
3330   if (!cp_parser_is_string_literal (tok))
3331     {
3332       cp_parser_error (parser, "expected string-literal");
3333       return error_mark_node;
3334     }
3335
3336   type = tok->type;
3337
3338   /* Try to avoid the overhead of creating and destroying an obstack
3339      for the common case of just one string.  */
3340   if (!cp_parser_is_string_literal
3341       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3342     {
3343       cp_lexer_consume_token (parser->lexer);
3344
3345       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3346       str.len = TREE_STRING_LENGTH (tok->u.value);
3347       count = 1;
3348
3349       strs = &str;
3350     }
3351   else
3352     {
3353       gcc_obstack_init (&str_ob);
3354       count = 0;
3355
3356       do
3357         {
3358           cp_lexer_consume_token (parser->lexer);
3359           count++;
3360           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3361           str.len = TREE_STRING_LENGTH (tok->u.value);
3362
3363           if (type != tok->type)
3364             {
3365               if (type == CPP_STRING)
3366                 type = tok->type;
3367               else if (tok->type != CPP_STRING)
3368                 error_at (tok->location,
3369                           "unsupported non-standard concatenation "
3370                           "of string literals");
3371             }
3372
3373           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3374
3375           tok = cp_lexer_peek_token (parser->lexer);
3376         }
3377       while (cp_parser_is_string_literal (tok));
3378
3379       strs = (cpp_string *) obstack_finish (&str_ob);
3380     }
3381
3382   if (type != CPP_STRING && !wide_ok)
3383     {
3384       cp_parser_error (parser, "a wide string is invalid in this context");
3385       type = CPP_STRING;
3386     }
3387
3388   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3389       (parse_in, strs, count, &istr, type))
3390     {
3391       value = build_string (istr.len, (const char *)istr.text);
3392       free (CONST_CAST (unsigned char *, istr.text));
3393
3394       switch (type)
3395         {
3396         default:
3397         case CPP_STRING:
3398         case CPP_UTF8STRING:
3399           TREE_TYPE (value) = char_array_type_node;
3400           break;
3401         case CPP_STRING16:
3402           TREE_TYPE (value) = char16_array_type_node;
3403           break;
3404         case CPP_STRING32:
3405           TREE_TYPE (value) = char32_array_type_node;
3406           break;
3407         case CPP_WSTRING:
3408           TREE_TYPE (value) = wchar_array_type_node;
3409           break;
3410         }
3411
3412       value = fix_string_type (value);
3413     }
3414   else
3415     /* cpp_interpret_string has issued an error.  */
3416     value = error_mark_node;
3417
3418   if (count > 1)
3419     obstack_free (&str_ob, 0);
3420
3421   return value;
3422 }
3423
3424
3425 /* Basic concepts [gram.basic]  */
3426
3427 /* Parse a translation-unit.
3428
3429    translation-unit:
3430      declaration-seq [opt]
3431
3432    Returns TRUE if all went well.  */
3433
3434 static bool
3435 cp_parser_translation_unit (cp_parser* parser)
3436 {
3437   /* The address of the first non-permanent object on the declarator
3438      obstack.  */
3439   static void *declarator_obstack_base;
3440
3441   bool success;
3442
3443   /* Create the declarator obstack, if necessary.  */
3444   if (!cp_error_declarator)
3445     {
3446       gcc_obstack_init (&declarator_obstack);
3447       /* Create the error declarator.  */
3448       cp_error_declarator = make_declarator (cdk_error);
3449       /* Create the empty parameter list.  */
3450       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3451       /* Remember where the base of the declarator obstack lies.  */
3452       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3453     }
3454
3455   cp_parser_declaration_seq_opt (parser);
3456
3457   /* If there are no tokens left then all went well.  */
3458   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3459     {
3460       /* Get rid of the token array; we don't need it any more.  */
3461       cp_lexer_destroy (parser->lexer);
3462       parser->lexer = NULL;
3463
3464       /* This file might have been a context that's implicitly extern
3465          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3466       if (parser->implicit_extern_c)
3467         {
3468           pop_lang_context ();
3469           parser->implicit_extern_c = false;
3470         }
3471
3472       /* Finish up.  */
3473       finish_translation_unit ();
3474
3475       success = true;
3476     }
3477   else
3478     {
3479       cp_parser_error (parser, "expected declaration");
3480       success = false;
3481     }
3482
3483   /* Make sure the declarator obstack was fully cleaned up.  */
3484   gcc_assert (obstack_next_free (&declarator_obstack)
3485               == declarator_obstack_base);
3486
3487   /* All went well.  */
3488   return success;
3489 }
3490
3491 /* Expressions [gram.expr] */
3492
3493 /* Parse a primary-expression.
3494
3495    primary-expression:
3496      literal
3497      this
3498      ( expression )
3499      id-expression
3500
3501    GNU Extensions:
3502
3503    primary-expression:
3504      ( compound-statement )
3505      __builtin_va_arg ( assignment-expression , type-id )
3506      __builtin_offsetof ( type-id , offsetof-expression )
3507
3508    C++ Extensions:
3509      __has_nothrow_assign ( type-id )   
3510      __has_nothrow_constructor ( type-id )
3511      __has_nothrow_copy ( type-id )
3512      __has_trivial_assign ( type-id )   
3513      __has_trivial_constructor ( type-id )
3514      __has_trivial_copy ( type-id )
3515      __has_trivial_destructor ( type-id )
3516      __has_virtual_destructor ( type-id )     
3517      __is_abstract ( type-id )
3518      __is_base_of ( type-id , type-id )
3519      __is_class ( type-id )
3520      __is_convertible_to ( type-id , type-id )     
3521      __is_empty ( type-id )
3522      __is_enum ( type-id )
3523      __is_pod ( type-id )
3524      __is_polymorphic ( type-id )
3525      __is_union ( type-id )
3526
3527    Objective-C++ Extension:
3528
3529    primary-expression:
3530      objc-expression
3531
3532    literal:
3533      __null
3534
3535    ADDRESS_P is true iff this expression was immediately preceded by
3536    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3537    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3538    true iff this expression is a template argument.
3539
3540    Returns a representation of the expression.  Upon return, *IDK
3541    indicates what kind of id-expression (if any) was present.  */
3542
3543 static tree
3544 cp_parser_primary_expression (cp_parser *parser,
3545                               bool address_p,
3546                               bool cast_p,
3547                               bool template_arg_p,
3548                               cp_id_kind *idk)
3549 {
3550   cp_token *token = NULL;
3551
3552   /* Assume the primary expression is not an id-expression.  */
3553   *idk = CP_ID_KIND_NONE;
3554
3555   /* Peek at the next token.  */
3556   token = cp_lexer_peek_token (parser->lexer);
3557   switch (token->type)
3558     {
3559       /* literal:
3560            integer-literal
3561            character-literal
3562            floating-literal
3563            string-literal
3564            boolean-literal  */
3565     case CPP_CHAR:
3566     case CPP_CHAR16:
3567     case CPP_CHAR32:
3568     case CPP_WCHAR:
3569     case CPP_NUMBER:
3570       token = cp_lexer_consume_token (parser->lexer);
3571       if (TREE_CODE (token->u.value) == FIXED_CST)
3572         {
3573           error_at (token->location,
3574                     "fixed-point types not supported in C++");
3575           return error_mark_node;
3576         }
3577       /* Floating-point literals are only allowed in an integral
3578          constant expression if they are cast to an integral or
3579          enumeration type.  */
3580       if (TREE_CODE (token->u.value) == REAL_CST
3581           && parser->integral_constant_expression_p
3582           && pedantic)
3583         {
3584           /* CAST_P will be set even in invalid code like "int(2.7 +
3585              ...)".   Therefore, we have to check that the next token
3586              is sure to end the cast.  */
3587           if (cast_p)
3588             {
3589               cp_token *next_token;
3590
3591               next_token = cp_lexer_peek_token (parser->lexer);
3592               if (/* The comma at the end of an
3593                      enumerator-definition.  */
3594                   next_token->type != CPP_COMMA
3595                   /* The curly brace at the end of an enum-specifier.  */
3596                   && next_token->type != CPP_CLOSE_BRACE
3597                   /* The end of a statement.  */
3598                   && next_token->type != CPP_SEMICOLON
3599                   /* The end of the cast-expression.  */
3600                   && next_token->type != CPP_CLOSE_PAREN
3601                   /* The end of an array bound.  */
3602                   && next_token->type != CPP_CLOSE_SQUARE
3603                   /* The closing ">" in a template-argument-list.  */
3604                   && (next_token->type != CPP_GREATER
3605                       || parser->greater_than_is_operator_p)
3606                   /* C++0x only: A ">>" treated like two ">" tokens,
3607                      in a template-argument-list.  */
3608                   && (next_token->type != CPP_RSHIFT
3609                       || (cxx_dialect == cxx98)
3610                       || parser->greater_than_is_operator_p))
3611                 cast_p = false;
3612             }
3613
3614           /* If we are within a cast, then the constraint that the
3615              cast is to an integral or enumeration type will be
3616              checked at that point.  If we are not within a cast, then
3617              this code is invalid.  */
3618           if (!cast_p)
3619             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3620         }
3621       return token->u.value;
3622
3623     case CPP_STRING:
3624     case CPP_STRING16:
3625     case CPP_STRING32:
3626     case CPP_WSTRING:
3627     case CPP_UTF8STRING:
3628       /* ??? Should wide strings be allowed when parser->translate_strings_p
3629          is false (i.e. in attributes)?  If not, we can kill the third
3630          argument to cp_parser_string_literal.  */
3631       return cp_parser_string_literal (parser,
3632                                        parser->translate_strings_p,
3633                                        true);
3634
3635     case CPP_OPEN_PAREN:
3636       {
3637         tree expr;
3638         bool saved_greater_than_is_operator_p;
3639
3640         /* Consume the `('.  */
3641         cp_lexer_consume_token (parser->lexer);
3642         /* Within a parenthesized expression, a `>' token is always
3643            the greater-than operator.  */
3644         saved_greater_than_is_operator_p
3645           = parser->greater_than_is_operator_p;
3646         parser->greater_than_is_operator_p = true;
3647         /* If we see `( { ' then we are looking at the beginning of
3648            a GNU statement-expression.  */
3649         if (cp_parser_allow_gnu_extensions_p (parser)
3650             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3651           {
3652             /* Statement-expressions are not allowed by the standard.  */
3653             pedwarn (token->location, OPT_pedantic, 
3654                      "ISO C++ forbids braced-groups within expressions");
3655
3656             /* And they're not allowed outside of a function-body; you
3657                cannot, for example, write:
3658
3659                  int i = ({ int j = 3; j + 1; });
3660
3661                at class or namespace scope.  */
3662             if (!parser->in_function_body
3663                 || parser->in_template_argument_list_p)
3664               {
3665                 error_at (token->location,
3666                           "statement-expressions are not allowed outside "
3667                           "functions nor in template-argument lists");
3668                 cp_parser_skip_to_end_of_block_or_statement (parser);
3669                 expr = error_mark_node;
3670               }
3671             else
3672               {
3673                 /* Start the statement-expression.  */
3674                 expr = begin_stmt_expr ();
3675                 /* Parse the compound-statement.  */
3676                 cp_parser_compound_statement (parser, expr, false);
3677                 /* Finish up.  */
3678                 expr = finish_stmt_expr (expr, false);
3679               }
3680           }
3681         else
3682           {
3683             /* Parse the parenthesized expression.  */
3684             expr = cp_parser_expression (parser, cast_p, idk);
3685             /* Let the front end know that this expression was
3686                enclosed in parentheses. This matters in case, for
3687                example, the expression is of the form `A::B', since
3688                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3689                not.  */
3690             finish_parenthesized_expr (expr);
3691           }
3692         /* The `>' token might be the end of a template-id or
3693            template-parameter-list now.  */
3694         parser->greater_than_is_operator_p
3695           = saved_greater_than_is_operator_p;
3696         /* Consume the `)'.  */
3697         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3698           cp_parser_skip_to_end_of_statement (parser);
3699
3700         return expr;
3701       }
3702
3703     case CPP_OPEN_SQUARE:
3704       if (c_dialect_objc ())
3705         /* We have an Objective-C++ message. */
3706         return cp_parser_objc_expression (parser);
3707       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3708       return cp_parser_lambda_expression (parser);
3709
3710     case CPP_OBJC_STRING:
3711       if (c_dialect_objc ())
3712         /* We have an Objective-C++ string literal. */
3713         return cp_parser_objc_expression (parser);
3714       cp_parser_error (parser, "expected primary-expression");
3715       return error_mark_node;
3716
3717     case CPP_KEYWORD:
3718       switch (token->keyword)
3719         {
3720           /* These two are the boolean literals.  */
3721         case RID_TRUE:
3722           cp_lexer_consume_token (parser->lexer);
3723           return boolean_true_node;
3724         case RID_FALSE:
3725           cp_lexer_consume_token (parser->lexer);
3726           return boolean_false_node;
3727
3728           /* The `__null' literal.  */
3729         case RID_NULL:
3730           cp_lexer_consume_token (parser->lexer);
3731           return null_node;
3732
3733           /* The `nullptr' literal.  */
3734         case RID_NULLPTR:
3735           cp_lexer_consume_token (parser->lexer);
3736           return nullptr_node;
3737
3738           /* Recognize the `this' keyword.  */
3739         case RID_THIS:
3740           cp_lexer_consume_token (parser->lexer);
3741           if (parser->local_variables_forbidden_p)
3742             {
3743               error_at (token->location,
3744                         "%<this%> may not be used in this context");
3745               return error_mark_node;
3746             }
3747           /* Pointers cannot appear in constant-expressions.  */
3748           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3749             return error_mark_node;
3750           return finish_this_expr ();
3751
3752           /* The `operator' keyword can be the beginning of an
3753              id-expression.  */
3754         case RID_OPERATOR:
3755           goto id_expression;
3756
3757         case RID_FUNCTION_NAME:
3758         case RID_PRETTY_FUNCTION_NAME:
3759         case RID_C99_FUNCTION_NAME:
3760           {
3761             non_integral_constant name;
3762
3763             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3764                __func__ are the names of variables -- but they are
3765                treated specially.  Therefore, they are handled here,
3766                rather than relying on the generic id-expression logic
3767                below.  Grammatically, these names are id-expressions.
3768
3769                Consume the token.  */
3770             token = cp_lexer_consume_token (parser->lexer);
3771
3772             switch (token->keyword)
3773               {
3774               case RID_FUNCTION_NAME:
3775                 name = NIC_FUNC_NAME;
3776                 break;
3777               case RID_PRETTY_FUNCTION_NAME:
3778                 name = NIC_PRETTY_FUNC;
3779                 break;
3780               case RID_C99_FUNCTION_NAME:
3781                 name = NIC_C99_FUNC;
3782                 break;
3783               default:
3784                 gcc_unreachable ();
3785               }
3786
3787             if (cp_parser_non_integral_constant_expression (parser, name))
3788               return error_mark_node;
3789
3790             /* Look up the name.  */
3791             return finish_fname (token->u.value);
3792           }
3793
3794         case RID_VA_ARG:
3795           {
3796             tree expression;
3797             tree type;
3798
3799             /* The `__builtin_va_arg' construct is used to handle
3800                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3801             cp_lexer_consume_token (parser->lexer);
3802             /* Look for the opening `('.  */
3803             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3804             /* Now, parse the assignment-expression.  */
3805             expression = cp_parser_assignment_expression (parser,
3806                                                           /*cast_p=*/false, NULL);
3807             /* Look for the `,'.  */
3808             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3809             /* Parse the type-id.  */
3810             type = cp_parser_type_id (parser);
3811             /* Look for the closing `)'.  */
3812             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3813             /* Using `va_arg' in a constant-expression is not
3814                allowed.  */
3815             if (cp_parser_non_integral_constant_expression (parser,
3816                                                             NIC_VA_ARG))
3817               return error_mark_node;
3818             return build_x_va_arg (expression, type);
3819           }
3820
3821         case RID_OFFSETOF:
3822           return cp_parser_builtin_offsetof (parser);
3823
3824         case RID_HAS_NOTHROW_ASSIGN:
3825         case RID_HAS_NOTHROW_CONSTRUCTOR:
3826         case RID_HAS_NOTHROW_COPY:        
3827         case RID_HAS_TRIVIAL_ASSIGN:
3828         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3829         case RID_HAS_TRIVIAL_COPY:        
3830         case RID_HAS_TRIVIAL_DESTRUCTOR:
3831         case RID_HAS_VIRTUAL_DESTRUCTOR:
3832         case RID_IS_ABSTRACT:
3833         case RID_IS_BASE_OF:
3834         case RID_IS_CLASS:
3835         case RID_IS_CONVERTIBLE_TO:
3836         case RID_IS_EMPTY:
3837         case RID_IS_ENUM:
3838         case RID_IS_POD:
3839         case RID_IS_POLYMORPHIC:
3840         case RID_IS_STD_LAYOUT:
3841         case RID_IS_TRIVIAL:
3842         case RID_IS_UNION:
3843         case RID_IS_LITERAL_TYPE:
3844           return cp_parser_trait_expr (parser, token->keyword);
3845
3846         /* Objective-C++ expressions.  */
3847         case RID_AT_ENCODE:
3848         case RID_AT_PROTOCOL:
3849         case RID_AT_SELECTOR:
3850           return cp_parser_objc_expression (parser);
3851
3852         case RID_TEMPLATE:
3853           if (parser->in_function_body
3854               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3855                   == CPP_LESS))
3856             {
3857               error_at (token->location,
3858                         "a template declaration cannot appear at block scope");
3859               cp_parser_skip_to_end_of_block_or_statement (parser);
3860               return error_mark_node;
3861             }
3862         default:
3863           cp_parser_error (parser, "expected primary-expression");
3864           return error_mark_node;
3865         }
3866
3867       /* An id-expression can start with either an identifier, a
3868          `::' as the beginning of a qualified-id, or the "operator"
3869          keyword.  */
3870     case CPP_NAME:
3871     case CPP_SCOPE:
3872     case CPP_TEMPLATE_ID:
3873     case CPP_NESTED_NAME_SPECIFIER:
3874       {
3875         tree id_expression;
3876         tree decl;
3877         const char *error_msg;
3878         bool template_p;
3879         bool done;
3880         cp_token *id_expr_token;
3881
3882       id_expression:
3883         /* Parse the id-expression.  */
3884         id_expression
3885           = cp_parser_id_expression (parser,
3886                                      /*template_keyword_p=*/false,
3887                                      /*check_dependency_p=*/true,
3888                                      &template_p,
3889                                      /*declarator_p=*/false,
3890                                      /*optional_p=*/false);
3891         if (id_expression == error_mark_node)
3892           return error_mark_node;
3893         id_expr_token = token;
3894         token = cp_lexer_peek_token (parser->lexer);
3895         done = (token->type != CPP_OPEN_SQUARE
3896                 && token->type != CPP_OPEN_PAREN
3897                 && token->type != CPP_DOT
3898                 && token->type != CPP_DEREF
3899                 && token->type != CPP_PLUS_PLUS
3900                 && token->type != CPP_MINUS_MINUS);
3901         /* If we have a template-id, then no further lookup is
3902            required.  If the template-id was for a template-class, we
3903            will sometimes have a TYPE_DECL at this point.  */
3904         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3905                  || TREE_CODE (id_expression) == TYPE_DECL)
3906           decl = id_expression;
3907         /* Look up the name.  */
3908         else
3909           {
3910             tree ambiguous_decls;
3911
3912             /* If we already know that this lookup is ambiguous, then
3913                we've already issued an error message; there's no reason
3914                to check again.  */
3915             if (id_expr_token->type == CPP_NAME
3916                 && id_expr_token->ambiguous_p)
3917               {
3918                 cp_parser_simulate_error (parser);
3919                 return error_mark_node;
3920               }
3921
3922             decl = cp_parser_lookup_name (parser, id_expression,
3923                                           none_type,
3924                                           template_p,
3925                                           /*is_namespace=*/false,
3926                                           /*check_dependency=*/true,
3927                                           &ambiguous_decls,
3928                                           id_expr_token->location);
3929             /* If the lookup was ambiguous, an error will already have
3930                been issued.  */
3931             if (ambiguous_decls)
3932               return error_mark_node;
3933
3934             /* In Objective-C++, we may have an Objective-C 2.0
3935                dot-syntax for classes here.  */
3936             if (c_dialect_objc ()
3937                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3938                 && TREE_CODE (decl) == TYPE_DECL
3939                 && objc_is_class_name (decl))
3940               {
3941                 tree component;
3942                 cp_lexer_consume_token (parser->lexer);
3943                 component = cp_parser_identifier (parser);
3944                 if (component == error_mark_node)
3945                   return error_mark_node;
3946
3947                 return objc_build_class_component_ref (id_expression, component);
3948               }
3949
3950             /* In Objective-C++, an instance variable (ivar) may be preferred
3951                to whatever cp_parser_lookup_name() found.  */
3952             decl = objc_lookup_ivar (decl, id_expression);
3953
3954             /* If name lookup gives us a SCOPE_REF, then the
3955                qualifying scope was dependent.  */
3956             if (TREE_CODE (decl) == SCOPE_REF)
3957               {
3958                 /* At this point, we do not know if DECL is a valid
3959                    integral constant expression.  We assume that it is
3960                    in fact such an expression, so that code like:
3961
3962                       template <int N> struct A {
3963                         int a[B<N>::i];
3964                       };
3965                      
3966                    is accepted.  At template-instantiation time, we
3967                    will check that B<N>::i is actually a constant.  */
3968                 return decl;
3969               }
3970             /* Check to see if DECL is a local variable in a context
3971                where that is forbidden.  */
3972             if (parser->local_variables_forbidden_p
3973                 && local_variable_p (decl))
3974               {
3975                 /* It might be that we only found DECL because we are
3976                    trying to be generous with pre-ISO scoping rules.
3977                    For example, consider:
3978
3979                      int i;
3980                      void g() {
3981                        for (int i = 0; i < 10; ++i) {}
3982                        extern void f(int j = i);
3983                      }
3984
3985                    Here, name look up will originally find the out
3986                    of scope `i'.  We need to issue a warning message,
3987                    but then use the global `i'.  */
3988                 decl = check_for_out_of_scope_variable (decl);
3989                 if (local_variable_p (decl))
3990                   {
3991                     error_at (id_expr_token->location,
3992                               "local variable %qD may not appear in this context",
3993                               decl);
3994                     return error_mark_node;
3995                   }
3996               }
3997           }
3998
3999         decl = (finish_id_expression
4000                 (id_expression, decl, parser->scope,
4001                  idk,
4002                  parser->integral_constant_expression_p,
4003                  parser->allow_non_integral_constant_expression_p,
4004                  &parser->non_integral_constant_expression_p,
4005                  template_p, done, address_p,
4006                  template_arg_p,
4007                  &error_msg,
4008                  id_expr_token->location));
4009         if (error_msg)
4010           cp_parser_error (parser, error_msg);
4011         return decl;
4012       }
4013
4014       /* Anything else is an error.  */
4015     default:
4016       cp_parser_error (parser, "expected primary-expression");
4017       return error_mark_node;
4018     }
4019 }
4020
4021 /* Parse an id-expression.
4022
4023    id-expression:
4024      unqualified-id
4025      qualified-id
4026
4027    qualified-id:
4028      :: [opt] nested-name-specifier template [opt] unqualified-id
4029      :: identifier
4030      :: operator-function-id
4031      :: template-id
4032
4033    Return a representation of the unqualified portion of the
4034    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4035    a `::' or nested-name-specifier.
4036
4037    Often, if the id-expression was a qualified-id, the caller will
4038    want to make a SCOPE_REF to represent the qualified-id.  This
4039    function does not do this in order to avoid wastefully creating
4040    SCOPE_REFs when they are not required.
4041
4042    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4043    `template' keyword.
4044
4045    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4046    uninstantiated templates.
4047
4048    If *TEMPLATE_P is non-NULL, it is set to true iff the
4049    `template' keyword is used to explicitly indicate that the entity
4050    named is a template.
4051
4052    If DECLARATOR_P is true, the id-expression is appearing as part of
4053    a declarator, rather than as part of an expression.  */
4054
4055 static tree
4056 cp_parser_id_expression (cp_parser *parser,
4057                          bool template_keyword_p,
4058                          bool check_dependency_p,
4059                          bool *template_p,
4060                          bool declarator_p,
4061                          bool optional_p)
4062 {
4063   bool global_scope_p;
4064   bool nested_name_specifier_p;
4065
4066   /* Assume the `template' keyword was not used.  */
4067   if (template_p)
4068     *template_p = template_keyword_p;
4069
4070   /* Look for the optional `::' operator.  */
4071   global_scope_p
4072     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4073        != NULL_TREE);
4074   /* Look for the optional nested-name-specifier.  */
4075   nested_name_specifier_p
4076     = (cp_parser_nested_name_specifier_opt (parser,
4077                                             /*typename_keyword_p=*/false,
4078                                             check_dependency_p,
4079                                             /*type_p=*/false,
4080                                             declarator_p)
4081        != NULL_TREE);
4082   /* If there is a nested-name-specifier, then we are looking at
4083      the first qualified-id production.  */
4084   if (nested_name_specifier_p)
4085     {
4086       tree saved_scope;
4087       tree saved_object_scope;
4088       tree saved_qualifying_scope;
4089       tree unqualified_id;
4090       bool is_template;
4091
4092       /* See if the next token is the `template' keyword.  */
4093       if (!template_p)
4094         template_p = &is_template;
4095       *template_p = cp_parser_optional_template_keyword (parser);
4096       /* Name lookup we do during the processing of the
4097          unqualified-id might obliterate SCOPE.  */
4098       saved_scope = parser->scope;
4099       saved_object_scope = parser->object_scope;
4100       saved_qualifying_scope = parser->qualifying_scope;
4101       /* Process the final unqualified-id.  */
4102       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4103                                                  check_dependency_p,
4104                                                  declarator_p,
4105                                                  /*optional_p=*/false);
4106       /* Restore the SAVED_SCOPE for our caller.  */
4107       parser->scope = saved_scope;
4108       parser->object_scope = saved_object_scope;
4109       parser->qualifying_scope = saved_qualifying_scope;
4110
4111       return unqualified_id;
4112     }
4113   /* Otherwise, if we are in global scope, then we are looking at one
4114      of the other qualified-id productions.  */
4115   else if (global_scope_p)
4116     {
4117       cp_token *token;
4118       tree id;
4119
4120       /* Peek at the next token.  */
4121       token = cp_lexer_peek_token (parser->lexer);
4122
4123       /* If it's an identifier, and the next token is not a "<", then
4124          we can avoid the template-id case.  This is an optimization
4125          for this common case.  */
4126       if (token->type == CPP_NAME
4127           && !cp_parser_nth_token_starts_template_argument_list_p
4128                (parser, 2))
4129         return cp_parser_identifier (parser);
4130
4131       cp_parser_parse_tentatively (parser);
4132       /* Try a template-id.  */
4133       id = cp_parser_template_id (parser,
4134                                   /*template_keyword_p=*/false,
4135                                   /*check_dependency_p=*/true,
4136                                   declarator_p);
4137       /* If that worked, we're done.  */
4138       if (cp_parser_parse_definitely (parser))
4139         return id;
4140
4141       /* Peek at the next token.  (Changes in the token buffer may
4142          have invalidated the pointer obtained above.)  */
4143       token = cp_lexer_peek_token (parser->lexer);
4144
4145       switch (token->type)
4146         {
4147         case CPP_NAME:
4148           return cp_parser_identifier (parser);
4149
4150         case CPP_KEYWORD:
4151           if (token->keyword == RID_OPERATOR)
4152             return cp_parser_operator_function_id (parser);
4153           /* Fall through.  */
4154
4155         default:
4156           cp_parser_error (parser, "expected id-expression");
4157           return error_mark_node;
4158         }
4159     }
4160   else
4161     return cp_parser_unqualified_id (parser, template_keyword_p,
4162                                      /*check_dependency_p=*/true,
4163                                      declarator_p,
4164                                      optional_p);
4165 }
4166
4167 /* Parse an unqualified-id.
4168
4169    unqualified-id:
4170      identifier
4171      operator-function-id
4172      conversion-function-id
4173      ~ class-name
4174      template-id
4175
4176    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4177    keyword, in a construct like `A::template ...'.
4178
4179    Returns a representation of unqualified-id.  For the `identifier'
4180    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4181    production a BIT_NOT_EXPR is returned; the operand of the
4182    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4183    other productions, see the documentation accompanying the
4184    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4185    names are looked up in uninstantiated templates.  If DECLARATOR_P
4186    is true, the unqualified-id is appearing as part of a declarator,
4187    rather than as part of an expression.  */
4188
4189 static tree
4190 cp_parser_unqualified_id (cp_parser* parser,
4191                           bool template_keyword_p,
4192                           bool check_dependency_p,
4193                           bool declarator_p,
4194                           bool optional_p)
4195 {
4196   cp_token *token;
4197
4198   /* Peek at the next token.  */
4199   token = cp_lexer_peek_token (parser->lexer);
4200
4201   switch (token->type)
4202     {
4203     case CPP_NAME:
4204       {
4205         tree id;
4206
4207         /* We don't know yet whether or not this will be a
4208            template-id.  */
4209         cp_parser_parse_tentatively (parser);
4210         /* Try a template-id.  */
4211         id = cp_parser_template_id (parser, template_keyword_p,
4212                                     check_dependency_p,
4213                                     declarator_p);
4214         /* If it worked, we're done.  */
4215         if (cp_parser_parse_definitely (parser))
4216           return id;
4217         /* Otherwise, it's an ordinary identifier.  */
4218         return cp_parser_identifier (parser);
4219       }
4220
4221     case CPP_TEMPLATE_ID:
4222       return cp_parser_template_id (parser, template_keyword_p,
4223                                     check_dependency_p,
4224                                     declarator_p);
4225
4226     case CPP_COMPL:
4227       {
4228         tree type_decl;
4229         tree qualifying_scope;
4230         tree object_scope;
4231         tree scope;
4232         bool done;
4233
4234         /* Consume the `~' token.  */
4235         cp_lexer_consume_token (parser->lexer);
4236         /* Parse the class-name.  The standard, as written, seems to
4237            say that:
4238
4239              template <typename T> struct S { ~S (); };
4240              template <typename T> S<T>::~S() {}
4241
4242            is invalid, since `~' must be followed by a class-name, but
4243            `S<T>' is dependent, and so not known to be a class.
4244            That's not right; we need to look in uninstantiated
4245            templates.  A further complication arises from:
4246
4247              template <typename T> void f(T t) {
4248                t.T::~T();
4249              }
4250
4251            Here, it is not possible to look up `T' in the scope of `T'
4252            itself.  We must look in both the current scope, and the
4253            scope of the containing complete expression.
4254
4255            Yet another issue is:
4256
4257              struct S {
4258                int S;
4259                ~S();
4260              };
4261
4262              S::~S() {}
4263
4264            The standard does not seem to say that the `S' in `~S'
4265            should refer to the type `S' and not the data member
4266            `S::S'.  */
4267
4268         /* DR 244 says that we look up the name after the "~" in the
4269            same scope as we looked up the qualifying name.  That idea
4270            isn't fully worked out; it's more complicated than that.  */
4271         scope = parser->scope;
4272         object_scope = parser->object_scope;
4273         qualifying_scope = parser->qualifying_scope;
4274
4275         /* Check for invalid scopes.  */
4276         if (scope == error_mark_node)
4277           {
4278             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4279               cp_lexer_consume_token (parser->lexer);
4280             return error_mark_node;
4281           }
4282         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4283           {
4284             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4285               error_at (token->location,
4286                         "scope %qT before %<~%> is not a class-name",
4287                         scope);
4288             cp_parser_simulate_error (parser);
4289             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4290               cp_lexer_consume_token (parser->lexer);
4291             return error_mark_node;
4292           }
4293         gcc_assert (!scope || TYPE_P (scope));
4294
4295         /* If the name is of the form "X::~X" it's OK even if X is a
4296            typedef.  */
4297         token = cp_lexer_peek_token (parser->lexer);
4298         if (scope
4299             && token->type == CPP_NAME
4300             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4301                 != CPP_LESS)
4302             && (token->u.value == TYPE_IDENTIFIER (scope)
4303                 || constructor_name_p (token->u.value, scope)))
4304           {
4305             cp_lexer_consume_token (parser->lexer);
4306             return build_nt (BIT_NOT_EXPR, scope);
4307           }
4308
4309         /* If there was an explicit qualification (S::~T), first look
4310            in the scope given by the qualification (i.e., S).
4311
4312            Note: in the calls to cp_parser_class_name below we pass
4313            typename_type so that lookup finds the injected-class-name
4314            rather than the constructor.  */
4315         done = false;
4316         type_decl = NULL_TREE;
4317         if (scope)
4318           {
4319             cp_parser_parse_tentatively (parser);
4320             type_decl = cp_parser_class_name (parser,
4321                                               /*typename_keyword_p=*/false,
4322                                               /*template_keyword_p=*/false,
4323                                               typename_type,
4324                                               /*check_dependency=*/false,
4325                                               /*class_head_p=*/false,
4326                                               declarator_p);
4327             if (cp_parser_parse_definitely (parser))
4328               done = true;
4329           }
4330         /* In "N::S::~S", look in "N" as well.  */
4331         if (!done && scope && qualifying_scope)
4332           {
4333             cp_parser_parse_tentatively (parser);
4334             parser->scope = qualifying_scope;
4335             parser->object_scope = NULL_TREE;
4336             parser->qualifying_scope = NULL_TREE;
4337             type_decl
4338               = cp_parser_class_name (parser,
4339                                       /*typename_keyword_p=*/false,
4340                                       /*template_keyword_p=*/false,
4341                                       typename_type,
4342                                       /*check_dependency=*/false,
4343                                       /*class_head_p=*/false,
4344                                       declarator_p);
4345             if (cp_parser_parse_definitely (parser))
4346               done = true;
4347           }
4348         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4349         else if (!done && object_scope)
4350           {
4351             cp_parser_parse_tentatively (parser);
4352             parser->scope = object_scope;
4353             parser->object_scope = NULL_TREE;
4354             parser->qualifying_scope = NULL_TREE;
4355             type_decl
4356               = cp_parser_class_name (parser,
4357                                       /*typename_keyword_p=*/false,
4358                                       /*template_keyword_p=*/false,
4359                                       typename_type,
4360                                       /*check_dependency=*/false,
4361                                       /*class_head_p=*/false,
4362                                       declarator_p);
4363             if (cp_parser_parse_definitely (parser))
4364               done = true;
4365           }
4366         /* Look in the surrounding context.  */
4367         if (!done)
4368           {
4369             parser->scope = NULL_TREE;
4370             parser->object_scope = NULL_TREE;
4371             parser->qualifying_scope = NULL_TREE;
4372             if (processing_template_decl)
4373               cp_parser_parse_tentatively (parser);
4374             type_decl
4375               = cp_parser_class_name (parser,
4376                                       /*typename_keyword_p=*/false,
4377                                       /*template_keyword_p=*/false,
4378                                       typename_type,
4379                                       /*check_dependency=*/false,
4380                                       /*class_head_p=*/false,
4381                                       declarator_p);
4382             if (processing_template_decl
4383                 && ! cp_parser_parse_definitely (parser))
4384               {
4385                 /* We couldn't find a type with this name, so just accept
4386                    it and check for a match at instantiation time.  */
4387                 type_decl = cp_parser_identifier (parser);
4388                 if (type_decl != error_mark_node)
4389                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4390                 return type_decl;
4391               }
4392           }
4393         /* If an error occurred, assume that the name of the
4394            destructor is the same as the name of the qualifying
4395            class.  That allows us to keep parsing after running
4396            into ill-formed destructor names.  */
4397         if (type_decl == error_mark_node && scope)
4398           return build_nt (BIT_NOT_EXPR, scope);
4399         else if (type_decl == error_mark_node)
4400           return error_mark_node;
4401
4402         /* Check that destructor name and scope match.  */
4403         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4404           {
4405             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4406               error_at (token->location,
4407                         "declaration of %<~%T%> as member of %qT",
4408                         type_decl, scope);
4409             cp_parser_simulate_error (parser);
4410             return error_mark_node;
4411           }
4412
4413         /* [class.dtor]
4414
4415            A typedef-name that names a class shall not be used as the
4416            identifier in the declarator for a destructor declaration.  */
4417         if (declarator_p
4418             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4419             && !DECL_SELF_REFERENCE_P (type_decl)
4420             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4421           error_at (token->location,
4422                     "typedef-name %qD used as destructor declarator",
4423                     type_decl);
4424
4425         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4426       }
4427
4428     case CPP_KEYWORD:
4429       if (token->keyword == RID_OPERATOR)
4430         {
4431           tree id;
4432
4433           /* This could be a template-id, so we try that first.  */
4434           cp_parser_parse_tentatively (parser);
4435           /* Try a template-id.  */
4436           id = cp_parser_template_id (parser, template_keyword_p,
4437                                       /*check_dependency_p=*/true,
4438                                       declarator_p);
4439           /* If that worked, we're done.  */
4440           if (cp_parser_parse_definitely (parser))
4441             return id;
4442           /* We still don't know whether we're looking at an
4443              operator-function-id or a conversion-function-id.  */
4444           cp_parser_parse_tentatively (parser);
4445           /* Try an operator-function-id.  */
4446           id = cp_parser_operator_function_id (parser);
4447           /* If that didn't work, try a conversion-function-id.  */
4448           if (!cp_parser_parse_definitely (parser))
4449             id = cp_parser_conversion_function_id (parser);
4450
4451           return id;
4452         }
4453       /* Fall through.  */
4454
4455     default:
4456       if (optional_p)
4457         return NULL_TREE;
4458       cp_parser_error (parser, "expected unqualified-id");
4459       return error_mark_node;
4460     }
4461 }
4462
4463 /* Parse an (optional) nested-name-specifier.
4464
4465    nested-name-specifier: [C++98]
4466      class-or-namespace-name :: nested-name-specifier [opt]
4467      class-or-namespace-name :: template nested-name-specifier [opt]
4468
4469    nested-name-specifier: [C++0x]
4470      type-name ::
4471      namespace-name ::
4472      nested-name-specifier identifier ::
4473      nested-name-specifier template [opt] simple-template-id ::
4474
4475    PARSER->SCOPE should be set appropriately before this function is
4476    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4477    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4478    in name lookups.
4479
4480    Sets PARSER->SCOPE to the class (TYPE) or namespace
4481    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4482    it unchanged if there is no nested-name-specifier.  Returns the new
4483    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4484
4485    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4486    part of a declaration and/or decl-specifier.  */
4487
4488 static tree
4489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4490                                      bool typename_keyword_p,
4491                                      bool check_dependency_p,
4492                                      bool type_p,
4493                                      bool is_declaration)
4494 {
4495   bool success = false;
4496   cp_token_position start = 0;
4497   cp_token *token;
4498
4499   /* Remember where the nested-name-specifier starts.  */
4500   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4501     {
4502       start = cp_lexer_token_position (parser->lexer, false);
4503       push_deferring_access_checks (dk_deferred);
4504     }
4505
4506   while (true)
4507     {
4508       tree new_scope;
4509       tree old_scope;
4510       tree saved_qualifying_scope;
4511       bool template_keyword_p;
4512
4513       /* Spot cases that cannot be the beginning of a
4514          nested-name-specifier.  */
4515       token = cp_lexer_peek_token (parser->lexer);
4516
4517       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4518          the already parsed nested-name-specifier.  */
4519       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4520         {
4521           /* Grab the nested-name-specifier and continue the loop.  */
4522           cp_parser_pre_parsed_nested_name_specifier (parser);
4523           /* If we originally encountered this nested-name-specifier
4524              with IS_DECLARATION set to false, we will not have
4525              resolved TYPENAME_TYPEs, so we must do so here.  */
4526           if (is_declaration
4527               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4528             {
4529               new_scope = resolve_typename_type (parser->scope,
4530                                                  /*only_current_p=*/false);
4531               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4532                 parser->scope = new_scope;
4533             }
4534           success = true;
4535           continue;
4536         }
4537
4538       /* Spot cases that cannot be the beginning of a
4539          nested-name-specifier.  On the second and subsequent times
4540          through the loop, we look for the `template' keyword.  */
4541       if (success && token->keyword == RID_TEMPLATE)
4542         ;
4543       /* A template-id can start a nested-name-specifier.  */
4544       else if (token->type == CPP_TEMPLATE_ID)
4545         ;
4546       else
4547         {
4548           /* If the next token is not an identifier, then it is
4549              definitely not a type-name or namespace-name.  */
4550           if (token->type != CPP_NAME)
4551             break;
4552           /* If the following token is neither a `<' (to begin a
4553              template-id), nor a `::', then we are not looking at a
4554              nested-name-specifier.  */
4555           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4556           if (token->type != CPP_SCOPE
4557               && !cp_parser_nth_token_starts_template_argument_list_p
4558                   (parser, 2))
4559             break;
4560         }
4561
4562       /* The nested-name-specifier is optional, so we parse
4563          tentatively.  */
4564       cp_parser_parse_tentatively (parser);
4565
4566       /* Look for the optional `template' keyword, if this isn't the
4567          first time through the loop.  */
4568       if (success)
4569         template_keyword_p = cp_parser_optional_template_keyword (parser);
4570       else
4571         template_keyword_p = false;
4572
4573       /* Save the old scope since the name lookup we are about to do
4574          might destroy it.  */
4575       old_scope = parser->scope;
4576       saved_qualifying_scope = parser->qualifying_scope;
4577       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4578          look up names in "X<T>::I" in order to determine that "Y" is
4579          a template.  So, if we have a typename at this point, we make
4580          an effort to look through it.  */
4581       if (is_declaration
4582           && !typename_keyword_p
4583           && parser->scope
4584           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4585         parser->scope = resolve_typename_type (parser->scope,
4586                                                /*only_current_p=*/false);
4587       /* Parse the qualifying entity.  */
4588       new_scope
4589         = cp_parser_qualifying_entity (parser,
4590                                        typename_keyword_p,
4591                                        template_keyword_p,
4592                                        check_dependency_p,
4593                                        type_p,
4594                                        is_declaration);
4595       /* Look for the `::' token.  */
4596       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4597
4598       /* If we found what we wanted, we keep going; otherwise, we're
4599          done.  */
4600       if (!cp_parser_parse_definitely (parser))
4601         {
4602           bool error_p = false;
4603
4604           /* Restore the OLD_SCOPE since it was valid before the
4605              failed attempt at finding the last
4606              class-or-namespace-name.  */
4607           parser->scope = old_scope;
4608           parser->qualifying_scope = saved_qualifying_scope;
4609           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4610             break;
4611           /* If the next token is an identifier, and the one after
4612              that is a `::', then any valid interpretation would have
4613              found a class-or-namespace-name.  */
4614           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4615                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4616                      == CPP_SCOPE)
4617                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4618                      != CPP_COMPL))
4619             {
4620               token = cp_lexer_consume_token (parser->lexer);
4621               if (!error_p)
4622                 {
4623                   if (!token->ambiguous_p)
4624                     {
4625                       tree decl;
4626                       tree ambiguous_decls;
4627
4628                       decl = cp_parser_lookup_name (parser, token->u.value,
4629                                                     none_type,
4630                                                     /*is_template=*/false,
4631                                                     /*is_namespace=*/false,
4632                                                     /*check_dependency=*/true,
4633                                                     &ambiguous_decls,
4634                                                     token->location);
4635                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4636                         error_at (token->location,
4637                                   "%qD used without template parameters",
4638                                   decl);
4639                       else if (ambiguous_decls)
4640                         {
4641                           error_at (token->location,
4642                                     "reference to %qD is ambiguous",
4643                                     token->u.value);
4644                           print_candidates (ambiguous_decls);
4645                           decl = error_mark_node;
4646                         }
4647                       else
4648                         {
4649                           if (cxx_dialect != cxx98)
4650                             cp_parser_name_lookup_error
4651                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4652                              token->location);
4653                           else
4654                             cp_parser_name_lookup_error
4655                             (parser, token->u.value, decl, NLE_CXX98,
4656                              token->location);
4657                         }
4658                     }
4659                   parser->scope = error_mark_node;
4660                   error_p = true;
4661                   /* Treat this as a successful nested-name-specifier
4662                      due to:
4663
4664                      [basic.lookup.qual]
4665
4666                      If the name found is not a class-name (clause
4667                      _class_) or namespace-name (_namespace.def_), the
4668                      program is ill-formed.  */
4669                   success = true;
4670                 }
4671               cp_lexer_consume_token (parser->lexer);
4672             }
4673           break;
4674         }
4675       /* We've found one valid nested-name-specifier.  */
4676       success = true;
4677       /* Name lookup always gives us a DECL.  */
4678       if (TREE_CODE (new_scope) == TYPE_DECL)
4679         new_scope = TREE_TYPE (new_scope);
4680       /* Uses of "template" must be followed by actual templates.  */
4681       if (template_keyword_p
4682           && !(CLASS_TYPE_P (new_scope)
4683                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4684                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4685                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4686           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4687                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4688                    == TEMPLATE_ID_EXPR)))
4689         permerror (input_location, TYPE_P (new_scope)
4690                    ? "%qT is not a template"
4691                    : "%qD is not a template",
4692                    new_scope);
4693       /* If it is a class scope, try to complete it; we are about to
4694          be looking up names inside the class.  */
4695       if (TYPE_P (new_scope)
4696           /* Since checking types for dependency can be expensive,
4697              avoid doing it if the type is already complete.  */
4698           && !COMPLETE_TYPE_P (new_scope)
4699           /* Do not try to complete dependent types.  */
4700           && !dependent_type_p (new_scope))
4701         {
4702           new_scope = complete_type (new_scope);
4703           /* If it is a typedef to current class, use the current
4704              class instead, as the typedef won't have any names inside
4705              it yet.  */
4706           if (!COMPLETE_TYPE_P (new_scope)
4707               && currently_open_class (new_scope))
4708             new_scope = TYPE_MAIN_VARIANT (new_scope);
4709         }
4710       /* Make sure we look in the right scope the next time through
4711          the loop.  */
4712       parser->scope = new_scope;
4713     }
4714
4715   /* If parsing tentatively, replace the sequence of tokens that makes
4716      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4717      token.  That way, should we re-parse the token stream, we will
4718      not have to repeat the effort required to do the parse, nor will
4719      we issue duplicate error messages.  */
4720   if (success && start)
4721     {
4722       cp_token *token;
4723
4724       token = cp_lexer_token_at (parser->lexer, start);
4725       /* Reset the contents of the START token.  */
4726       token->type = CPP_NESTED_NAME_SPECIFIER;
4727       /* Retrieve any deferred checks.  Do not pop this access checks yet
4728          so the memory will not be reclaimed during token replacing below.  */
4729       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4730       token->u.tree_check_value->value = parser->scope;
4731       token->u.tree_check_value->checks = get_deferred_access_checks ();
4732       token->u.tree_check_value->qualifying_scope =
4733         parser->qualifying_scope;
4734       token->keyword = RID_MAX;
4735
4736       /* Purge all subsequent tokens.  */
4737       cp_lexer_purge_tokens_after (parser->lexer, start);
4738     }
4739
4740   if (start)
4741     pop_to_parent_deferring_access_checks ();
4742
4743   return success ? parser->scope : NULL_TREE;
4744 }
4745
4746 /* Parse a nested-name-specifier.  See
4747    cp_parser_nested_name_specifier_opt for details.  This function
4748    behaves identically, except that it will an issue an error if no
4749    nested-name-specifier is present.  */
4750
4751 static tree
4752 cp_parser_nested_name_specifier (cp_parser *parser,
4753                                  bool typename_keyword_p,
4754                                  bool check_dependency_p,
4755                                  bool type_p,
4756                                  bool is_declaration)
4757 {
4758   tree scope;
4759
4760   /* Look for the nested-name-specifier.  */
4761   scope = cp_parser_nested_name_specifier_opt (parser,
4762                                                typename_keyword_p,
4763                                                check_dependency_p,
4764                                                type_p,
4765                                                is_declaration);
4766   /* If it was not present, issue an error message.  */
4767   if (!scope)
4768     {
4769       cp_parser_error (parser, "expected nested-name-specifier");
4770       parser->scope = NULL_TREE;
4771     }
4772
4773   return scope;
4774 }
4775
4776 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4777    this is either a class-name or a namespace-name (which corresponds
4778    to the class-or-namespace-name production in the grammar). For
4779    C++0x, it can also be a type-name that refers to an enumeration
4780    type.
4781
4782    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4783    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4784    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4785    TYPE_P is TRUE iff the next name should be taken as a class-name,
4786    even the same name is declared to be another entity in the same
4787    scope.
4788
4789    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4790    specified by the class-or-namespace-name.  If neither is found the
4791    ERROR_MARK_NODE is returned.  */
4792
4793 static tree
4794 cp_parser_qualifying_entity (cp_parser *parser,
4795                              bool typename_keyword_p,
4796                              bool template_keyword_p,
4797                              bool check_dependency_p,
4798                              bool type_p,
4799                              bool is_declaration)
4800 {
4801   tree saved_scope;
4802   tree saved_qualifying_scope;
4803   tree saved_object_scope;
4804   tree scope;
4805   bool only_class_p;
4806   bool successful_parse_p;
4807
4808   /* Before we try to parse the class-name, we must save away the
4809      current PARSER->SCOPE since cp_parser_class_name will destroy
4810      it.  */
4811   saved_scope = parser->scope;
4812   saved_qualifying_scope = parser->qualifying_scope;
4813   saved_object_scope = parser->object_scope;
4814   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4815      there is no need to look for a namespace-name.  */
4816   only_class_p = template_keyword_p 
4817     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4818   if (!only_class_p)
4819     cp_parser_parse_tentatively (parser);
4820   scope = cp_parser_class_name (parser,
4821                                 typename_keyword_p,
4822                                 template_keyword_p,
4823                                 type_p ? class_type : none_type,
4824                                 check_dependency_p,
4825                                 /*class_head_p=*/false,
4826                                 is_declaration);
4827   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4828   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4829   if (!only_class_p 
4830       && cxx_dialect != cxx98
4831       && !successful_parse_p)
4832     {
4833       /* Restore the saved scope.  */
4834       parser->scope = saved_scope;
4835       parser->qualifying_scope = saved_qualifying_scope;
4836       parser->object_scope = saved_object_scope;
4837
4838       /* Parse tentatively.  */
4839       cp_parser_parse_tentatively (parser);
4840      
4841       /* Parse a typedef-name or enum-name.  */
4842       scope = cp_parser_nonclass_name (parser);
4843
4844       /* "If the name found does not designate a namespace or a class,
4845          enumeration, or dependent type, the program is ill-formed."
4846
4847          We cover classes and dependent types above and namespaces below,
4848          so this code is only looking for enums.  */
4849       if (!scope || TREE_CODE (scope) != TYPE_DECL
4850           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4851         cp_parser_simulate_error (parser);
4852
4853       successful_parse_p = cp_parser_parse_definitely (parser);
4854     }
4855   /* If that didn't work, try for a namespace-name.  */
4856   if (!only_class_p && !successful_parse_p)
4857     {
4858       /* Restore the saved scope.  */
4859       parser->scope = saved_scope;
4860       parser->qualifying_scope = saved_qualifying_scope;
4861       parser->object_scope = saved_object_scope;
4862       /* If we are not looking at an identifier followed by the scope
4863          resolution operator, then this is not part of a
4864          nested-name-specifier.  (Note that this function is only used
4865          to parse the components of a nested-name-specifier.)  */
4866       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4867           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4868         return error_mark_node;
4869       scope = cp_parser_namespace_name (parser);
4870     }
4871
4872   return scope;
4873 }
4874
4875 /* Parse a postfix-expression.
4876
4877    postfix-expression:
4878      primary-expression
4879      postfix-expression [ expression ]
4880      postfix-expression ( expression-list [opt] )
4881      simple-type-specifier ( expression-list [opt] )
4882      typename :: [opt] nested-name-specifier identifier
4883        ( expression-list [opt] )
4884      typename :: [opt] nested-name-specifier template [opt] template-id
4885        ( expression-list [opt] )
4886      postfix-expression . template [opt] id-expression
4887      postfix-expression -> template [opt] id-expression
4888      postfix-expression . pseudo-destructor-name
4889      postfix-expression -> pseudo-destructor-name
4890      postfix-expression ++
4891      postfix-expression --
4892      dynamic_cast < type-id > ( expression )
4893      static_cast < type-id > ( expression )
4894      reinterpret_cast < type-id > ( expression )
4895      const_cast < type-id > ( expression )
4896      typeid ( expression )
4897      typeid ( type-id )
4898
4899    GNU Extension:
4900
4901    postfix-expression:
4902      ( type-id ) { initializer-list , [opt] }
4903
4904    This extension is a GNU version of the C99 compound-literal
4905    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4906    but they are essentially the same concept.)
4907
4908    If ADDRESS_P is true, the postfix expression is the operand of the
4909    `&' operator.  CAST_P is true if this expression is the target of a
4910    cast.
4911
4912    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4913    class member access expressions [expr.ref].
4914
4915    Returns a representation of the expression.  */
4916
4917 static tree
4918 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4919                               bool member_access_only_p,
4920                               cp_id_kind * pidk_return)
4921 {
4922   cp_token *token;
4923   enum rid keyword;
4924   cp_id_kind idk = CP_ID_KIND_NONE;
4925   tree postfix_expression = NULL_TREE;
4926   bool is_member_access = false;
4927
4928   /* Peek at the next token.  */
4929   token = cp_lexer_peek_token (parser->lexer);
4930   /* Some of the productions are determined by keywords.  */
4931   keyword = token->keyword;
4932   switch (keyword)
4933     {
4934     case RID_DYNCAST:
4935     case RID_STATCAST:
4936     case RID_REINTCAST:
4937     case RID_CONSTCAST:
4938       {
4939         tree type;
4940         tree expression;
4941         const char *saved_message;
4942
4943         /* All of these can be handled in the same way from the point
4944            of view of parsing.  Begin by consuming the token
4945            identifying the cast.  */
4946         cp_lexer_consume_token (parser->lexer);
4947
4948         /* New types cannot be defined in the cast.  */
4949         saved_message = parser->type_definition_forbidden_message;
4950         parser->type_definition_forbidden_message
4951           = G_("types may not be defined in casts");
4952
4953         /* Look for the opening `<'.  */
4954         cp_parser_require (parser, CPP_LESS, RT_LESS);
4955         /* Parse the type to which we are casting.  */
4956         type = cp_parser_type_id (parser);
4957         /* Look for the closing `>'.  */
4958         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4959         /* Restore the old message.  */
4960         parser->type_definition_forbidden_message = saved_message;
4961
4962         /* And the expression which is being cast.  */
4963         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4964         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4965         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4966
4967         /* Only type conversions to integral or enumeration types
4968            can be used in constant-expressions.  */
4969         if (!cast_valid_in_integral_constant_expression_p (type)
4970             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4971           return error_mark_node;
4972
4973         switch (keyword)
4974           {
4975           case RID_DYNCAST:
4976             postfix_expression
4977               = build_dynamic_cast (type, expression, tf_warning_or_error);
4978             break;
4979           case RID_STATCAST:
4980             postfix_expression
4981               = build_static_cast (type, expression, tf_warning_or_error);
4982             break;
4983           case RID_REINTCAST:
4984             postfix_expression
4985               = build_reinterpret_cast (type, expression, 
4986                                         tf_warning_or_error);
4987             break;
4988           case RID_CONSTCAST:
4989             postfix_expression
4990               = build_const_cast (type, expression, tf_warning_or_error);
4991             break;
4992           default:
4993             gcc_unreachable ();
4994           }
4995       }
4996       break;
4997
4998     case RID_TYPEID:
4999       {
5000         tree type;
5001         const char *saved_message;
5002         bool saved_in_type_id_in_expr_p;
5003
5004         /* Consume the `typeid' token.  */
5005         cp_lexer_consume_token (parser->lexer);
5006         /* Look for the `(' token.  */
5007         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5008         /* Types cannot be defined in a `typeid' expression.  */
5009         saved_message = parser->type_definition_forbidden_message;
5010         parser->type_definition_forbidden_message
5011           = G_("types may not be defined in a %<typeid%> expression");
5012         /* We can't be sure yet whether we're looking at a type-id or an
5013            expression.  */
5014         cp_parser_parse_tentatively (parser);
5015         /* Try a type-id first.  */
5016         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5017         parser->in_type_id_in_expr_p = true;
5018         type = cp_parser_type_id (parser);
5019         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5020         /* Look for the `)' token.  Otherwise, we can't be sure that
5021            we're not looking at an expression: consider `typeid (int
5022            (3))', for example.  */
5023         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5024         /* If all went well, simply lookup the type-id.  */
5025         if (cp_parser_parse_definitely (parser))
5026           postfix_expression = get_typeid (type);
5027         /* Otherwise, fall back to the expression variant.  */
5028         else
5029           {
5030             tree expression;
5031
5032             /* Look for an expression.  */
5033             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5034             /* Compute its typeid.  */
5035             postfix_expression = build_typeid (expression);
5036             /* Look for the `)' token.  */
5037             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5038           }
5039         /* Restore the saved message.  */
5040         parser->type_definition_forbidden_message = saved_message;
5041         /* `typeid' may not appear in an integral constant expression.  */
5042         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5043           return error_mark_node;
5044       }
5045       break;
5046
5047     case RID_TYPENAME:
5048       {
5049         tree type;
5050         /* The syntax permitted here is the same permitted for an
5051            elaborated-type-specifier.  */
5052         type = cp_parser_elaborated_type_specifier (parser,
5053                                                     /*is_friend=*/false,
5054                                                     /*is_declaration=*/false);
5055         postfix_expression = cp_parser_functional_cast (parser, type);
5056       }
5057       break;
5058
5059     default:
5060       {
5061         tree type;
5062
5063         /* If the next thing is a simple-type-specifier, we may be
5064            looking at a functional cast.  We could also be looking at
5065            an id-expression.  So, we try the functional cast, and if
5066            that doesn't work we fall back to the primary-expression.  */
5067         cp_parser_parse_tentatively (parser);
5068         /* Look for the simple-type-specifier.  */
5069         type = cp_parser_simple_type_specifier (parser,
5070                                                 /*decl_specs=*/NULL,
5071                                                 CP_PARSER_FLAGS_NONE);
5072         /* Parse the cast itself.  */
5073         if (!cp_parser_error_occurred (parser))
5074           postfix_expression
5075             = cp_parser_functional_cast (parser, type);
5076         /* If that worked, we're done.  */
5077         if (cp_parser_parse_definitely (parser))
5078           break;
5079
5080         /* If the functional-cast didn't work out, try a
5081            compound-literal.  */
5082         if (cp_parser_allow_gnu_extensions_p (parser)
5083             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5084           {
5085             VEC(constructor_elt,gc) *initializer_list = NULL;
5086             bool saved_in_type_id_in_expr_p;
5087
5088             cp_parser_parse_tentatively (parser);
5089             /* Consume the `('.  */
5090             cp_lexer_consume_token (parser->lexer);
5091             /* Parse the type.  */
5092             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5093             parser->in_type_id_in_expr_p = true;
5094             type = cp_parser_type_id (parser);
5095             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5096             /* Look for the `)'.  */
5097             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5098             /* Look for the `{'.  */
5099             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5100             /* If things aren't going well, there's no need to
5101                keep going.  */
5102             if (!cp_parser_error_occurred (parser))
5103               {
5104                 bool non_constant_p;
5105                 /* Parse the initializer-list.  */
5106                 initializer_list
5107                   = cp_parser_initializer_list (parser, &non_constant_p);
5108                 /* Allow a trailing `,'.  */
5109                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5110                   cp_lexer_consume_token (parser->lexer);
5111                 /* Look for the final `}'.  */
5112                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5113               }
5114             /* If that worked, we're definitely looking at a
5115                compound-literal expression.  */
5116             if (cp_parser_parse_definitely (parser))
5117               {
5118                 /* Warn the user that a compound literal is not
5119                    allowed in standard C++.  */
5120                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5121                 /* For simplicity, we disallow compound literals in
5122                    constant-expressions.  We could
5123                    allow compound literals of integer type, whose
5124                    initializer was a constant, in constant
5125                    expressions.  Permitting that usage, as a further
5126                    extension, would not change the meaning of any
5127                    currently accepted programs.  (Of course, as
5128                    compound literals are not part of ISO C++, the
5129                    standard has nothing to say.)  */
5130                 if (cp_parser_non_integral_constant_expression (parser,
5131                                                                 NIC_NCC))
5132                   {
5133                     postfix_expression = error_mark_node;
5134                     break;
5135                   }
5136                 /* Form the representation of the compound-literal.  */
5137                 postfix_expression
5138                   = (finish_compound_literal
5139                      (type, build_constructor (init_list_type_node,
5140                                                initializer_list)));
5141                 break;
5142               }
5143           }
5144
5145         /* It must be a primary-expression.  */
5146         postfix_expression
5147           = cp_parser_primary_expression (parser, address_p, cast_p,
5148                                           /*template_arg_p=*/false,
5149                                           &idk);
5150       }
5151       break;
5152     }
5153
5154   /* Keep looping until the postfix-expression is complete.  */
5155   while (true)
5156     {
5157       if (idk == CP_ID_KIND_UNQUALIFIED
5158           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5159           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5160         /* It is not a Koenig lookup function call.  */
5161         postfix_expression
5162           = unqualified_name_lookup_error (postfix_expression);
5163
5164       /* Peek at the next token.  */
5165       token = cp_lexer_peek_token (parser->lexer);
5166
5167       switch (token->type)
5168         {
5169         case CPP_OPEN_SQUARE:
5170           postfix_expression
5171             = cp_parser_postfix_open_square_expression (parser,
5172                                                         postfix_expression,
5173                                                         false);
5174           idk = CP_ID_KIND_NONE;
5175           is_member_access = false;
5176           break;
5177
5178         case CPP_OPEN_PAREN:
5179           /* postfix-expression ( expression-list [opt] ) */
5180           {
5181             bool koenig_p;
5182             bool is_builtin_constant_p;
5183             bool saved_integral_constant_expression_p = false;
5184             bool saved_non_integral_constant_expression_p = false;
5185             VEC(tree,gc) *args;
5186
5187             is_member_access = false;
5188
5189             is_builtin_constant_p
5190               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5191             if (is_builtin_constant_p)
5192               {
5193                 /* The whole point of __builtin_constant_p is to allow
5194                    non-constant expressions to appear as arguments.  */
5195                 saved_integral_constant_expression_p
5196                   = parser->integral_constant_expression_p;
5197                 saved_non_integral_constant_expression_p
5198                   = parser->non_integral_constant_expression_p;
5199                 parser->integral_constant_expression_p = false;
5200               }
5201             args = (cp_parser_parenthesized_expression_list
5202                     (parser, non_attr,
5203                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5204                      /*non_constant_p=*/NULL));
5205             if (is_builtin_constant_p)
5206               {
5207                 parser->integral_constant_expression_p
5208                   = saved_integral_constant_expression_p;
5209                 parser->non_integral_constant_expression_p
5210                   = saved_non_integral_constant_expression_p;
5211               }
5212
5213             if (args == NULL)
5214               {
5215                 postfix_expression = error_mark_node;
5216                 break;
5217               }
5218
5219             /* Function calls are not permitted in
5220                constant-expressions.  */
5221             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5222                 && cp_parser_non_integral_constant_expression (parser,
5223                                                                NIC_FUNC_CALL))
5224               {
5225                 postfix_expression = error_mark_node;
5226                 release_tree_vector (args);
5227                 break;
5228               }
5229
5230             koenig_p = false;
5231             if (idk == CP_ID_KIND_UNQUALIFIED
5232                 || idk == CP_ID_KIND_TEMPLATE_ID)
5233               {
5234                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5235                   {
5236                     if (!VEC_empty (tree, args))
5237                       {
5238                         koenig_p = true;
5239                         if (!any_type_dependent_arguments_p (args))
5240                           postfix_expression
5241                             = perform_koenig_lookup (postfix_expression, args,
5242                                                      /*include_std=*/false);
5243                       }
5244                     else
5245                       postfix_expression
5246                         = unqualified_fn_lookup_error (postfix_expression);
5247                   }
5248                 /* We do not perform argument-dependent lookup if
5249                    normal lookup finds a non-function, in accordance
5250                    with the expected resolution of DR 218.  */
5251                 else if (!VEC_empty (tree, args)
5252                          && is_overloaded_fn (postfix_expression))
5253                   {
5254                     tree fn = get_first_fn (postfix_expression);
5255                     fn = STRIP_TEMPLATE (fn);
5256
5257                     /* Do not do argument dependent lookup if regular
5258                        lookup finds a member function or a block-scope
5259                        function declaration.  [basic.lookup.argdep]/3  */
5260                     if (!DECL_FUNCTION_MEMBER_P (fn)
5261                         && !DECL_LOCAL_FUNCTION_P (fn))
5262                       {
5263                         koenig_p = true;
5264                         if (!any_type_dependent_arguments_p (args))
5265                           postfix_expression
5266                             = perform_koenig_lookup (postfix_expression, args,
5267                                                      /*include_std=*/false);
5268                       }
5269                   }
5270               }
5271
5272             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5273               {
5274                 tree instance = TREE_OPERAND (postfix_expression, 0);
5275                 tree fn = TREE_OPERAND (postfix_expression, 1);
5276
5277                 if (processing_template_decl
5278                     && (type_dependent_expression_p (instance)
5279                         || (!BASELINK_P (fn)
5280                             && TREE_CODE (fn) != FIELD_DECL)
5281                         || type_dependent_expression_p (fn)
5282                         || any_type_dependent_arguments_p (args)))
5283                   {
5284                     postfix_expression
5285                       = build_nt_call_vec (postfix_expression, args);
5286                     release_tree_vector (args);
5287                     break;
5288                   }
5289
5290                 if (BASELINK_P (fn))
5291                   {
5292                   postfix_expression
5293                     = (build_new_method_call
5294                        (instance, fn, &args, NULL_TREE,
5295                         (idk == CP_ID_KIND_QUALIFIED
5296                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5297                         /*fn_p=*/NULL,
5298                         tf_warning_or_error));
5299                   }
5300                 else
5301                   postfix_expression
5302                     = finish_call_expr (postfix_expression, &args,
5303                                         /*disallow_virtual=*/false,
5304                                         /*koenig_p=*/false,
5305                                         tf_warning_or_error);
5306               }
5307             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5308                      || TREE_CODE (postfix_expression) == MEMBER_REF
5309                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5310               postfix_expression = (build_offset_ref_call_from_tree
5311                                     (postfix_expression, &args));
5312             else if (idk == CP_ID_KIND_QUALIFIED)
5313               /* A call to a static class member, or a namespace-scope
5314                  function.  */
5315               postfix_expression
5316                 = finish_call_expr (postfix_expression, &args,
5317                                     /*disallow_virtual=*/true,
5318                                     koenig_p,
5319                                     tf_warning_or_error);
5320             else
5321               /* All other function calls.  */
5322               postfix_expression
5323                 = finish_call_expr (postfix_expression, &args,
5324                                     /*disallow_virtual=*/false,
5325                                     koenig_p,
5326                                     tf_warning_or_error);
5327
5328             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5329             idk = CP_ID_KIND_NONE;
5330
5331             release_tree_vector (args);
5332           }
5333           break;
5334
5335         case CPP_DOT:
5336         case CPP_DEREF:
5337           /* postfix-expression . template [opt] id-expression
5338              postfix-expression . pseudo-destructor-name
5339              postfix-expression -> template [opt] id-expression
5340              postfix-expression -> pseudo-destructor-name */
5341
5342           /* Consume the `.' or `->' operator.  */
5343           cp_lexer_consume_token (parser->lexer);
5344
5345           postfix_expression
5346             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5347                                                       postfix_expression,
5348                                                       false, &idk,
5349                                                       token->location);
5350
5351           is_member_access = true;
5352           break;
5353
5354         case CPP_PLUS_PLUS:
5355           /* postfix-expression ++  */
5356           /* Consume the `++' token.  */
5357           cp_lexer_consume_token (parser->lexer);
5358           /* Generate a representation for the complete expression.  */
5359           postfix_expression
5360             = finish_increment_expr (postfix_expression,
5361                                      POSTINCREMENT_EXPR);
5362           /* Increments may not appear in constant-expressions.  */
5363           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5364             postfix_expression = error_mark_node;
5365           idk = CP_ID_KIND_NONE;
5366           is_member_access = false;
5367           break;
5368
5369         case CPP_MINUS_MINUS:
5370           /* postfix-expression -- */
5371           /* Consume the `--' token.  */
5372           cp_lexer_consume_token (parser->lexer);
5373           /* Generate a representation for the complete expression.  */
5374           postfix_expression
5375             = finish_increment_expr (postfix_expression,
5376                                      POSTDECREMENT_EXPR);
5377           /* Decrements may not appear in constant-expressions.  */
5378           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5379             postfix_expression = error_mark_node;
5380           idk = CP_ID_KIND_NONE;
5381           is_member_access = false;
5382           break;
5383
5384         default:
5385           if (pidk_return != NULL)
5386             * pidk_return = idk;
5387           if (member_access_only_p)
5388             return is_member_access? postfix_expression : error_mark_node;
5389           else
5390             return postfix_expression;
5391         }
5392     }
5393
5394   /* We should never get here.  */
5395   gcc_unreachable ();
5396   return error_mark_node;
5397 }
5398
5399 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5400    by cp_parser_builtin_offsetof.  We're looking for
5401
5402      postfix-expression [ expression ]
5403
5404    FOR_OFFSETOF is set if we're being called in that context, which
5405    changes how we deal with integer constant expressions.  */
5406
5407 static tree
5408 cp_parser_postfix_open_square_expression (cp_parser *parser,
5409                                           tree postfix_expression,
5410                                           bool for_offsetof)
5411 {
5412   tree index;
5413
5414   /* Consume the `[' token.  */
5415   cp_lexer_consume_token (parser->lexer);
5416
5417   /* Parse the index expression.  */
5418   /* ??? For offsetof, there is a question of what to allow here.  If
5419      offsetof is not being used in an integral constant expression context,
5420      then we *could* get the right answer by computing the value at runtime.
5421      If we are in an integral constant expression context, then we might
5422      could accept any constant expression; hard to say without analysis.
5423      Rather than open the barn door too wide right away, allow only integer
5424      constant expressions here.  */
5425   if (for_offsetof)
5426     index = cp_parser_constant_expression (parser, false, NULL);
5427   else
5428     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5429
5430   /* Look for the closing `]'.  */
5431   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5432
5433   /* Build the ARRAY_REF.  */
5434   postfix_expression = grok_array_decl (postfix_expression, index);
5435
5436   /* When not doing offsetof, array references are not permitted in
5437      constant-expressions.  */
5438   if (!for_offsetof
5439       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5440     postfix_expression = error_mark_node;
5441
5442   return postfix_expression;
5443 }
5444
5445 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5446    by cp_parser_builtin_offsetof.  We're looking for
5447
5448      postfix-expression . template [opt] id-expression
5449      postfix-expression . pseudo-destructor-name
5450      postfix-expression -> template [opt] id-expression
5451      postfix-expression -> pseudo-destructor-name
5452
5453    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5454    limits what of the above we'll actually accept, but nevermind.
5455    TOKEN_TYPE is the "." or "->" token, which will already have been
5456    removed from the stream.  */
5457
5458 static tree
5459 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5460                                         enum cpp_ttype token_type,
5461                                         tree postfix_expression,
5462                                         bool for_offsetof, cp_id_kind *idk,
5463                                         location_t location)
5464 {
5465   tree name;
5466   bool dependent_p;
5467   bool pseudo_destructor_p;
5468   tree scope = NULL_TREE;
5469
5470   /* If this is a `->' operator, dereference the pointer.  */
5471   if (token_type == CPP_DEREF)
5472     postfix_expression = build_x_arrow (postfix_expression);
5473   /* Check to see whether or not the expression is type-dependent.  */
5474   dependent_p = type_dependent_expression_p (postfix_expression);
5475   /* The identifier following the `->' or `.' is not qualified.  */
5476   parser->scope = NULL_TREE;
5477   parser->qualifying_scope = NULL_TREE;
5478   parser->object_scope = NULL_TREE;
5479   *idk = CP_ID_KIND_NONE;
5480
5481   /* Enter the scope corresponding to the type of the object
5482      given by the POSTFIX_EXPRESSION.  */
5483   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5484     {
5485       scope = TREE_TYPE (postfix_expression);
5486       /* According to the standard, no expression should ever have
5487          reference type.  Unfortunately, we do not currently match
5488          the standard in this respect in that our internal representation
5489          of an expression may have reference type even when the standard
5490          says it does not.  Therefore, we have to manually obtain the
5491          underlying type here.  */
5492       scope = non_reference (scope);
5493       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5494       if (scope == unknown_type_node)
5495         {
5496           error_at (location, "%qE does not have class type",
5497                     postfix_expression);
5498           scope = NULL_TREE;
5499         }
5500       else
5501         scope = complete_type_or_else (scope, NULL_TREE);
5502       /* Let the name lookup machinery know that we are processing a
5503          class member access expression.  */
5504       parser->context->object_type = scope;
5505       /* If something went wrong, we want to be able to discern that case,
5506          as opposed to the case where there was no SCOPE due to the type
5507          of expression being dependent.  */
5508       if (!scope)
5509         scope = error_mark_node;
5510       /* If the SCOPE was erroneous, make the various semantic analysis
5511          functions exit quickly -- and without issuing additional error
5512          messages.  */
5513       if (scope == error_mark_node)
5514         postfix_expression = error_mark_node;
5515     }
5516
5517   /* Assume this expression is not a pseudo-destructor access.  */
5518   pseudo_destructor_p = false;
5519
5520   /* If the SCOPE is a scalar type, then, if this is a valid program,
5521      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5522      is type dependent, it can be pseudo-destructor-name or something else.
5523      Try to parse it as pseudo-destructor-name first.  */
5524   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5525     {
5526       tree s;
5527       tree type;
5528
5529       cp_parser_parse_tentatively (parser);
5530       /* Parse the pseudo-destructor-name.  */
5531       s = NULL_TREE;
5532       cp_parser_pseudo_destructor_name (parser, &s, &type);
5533       if (dependent_p
5534           && (cp_parser_error_occurred (parser)
5535               || TREE_CODE (type) != TYPE_DECL
5536               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5537         cp_parser_abort_tentative_parse (parser);
5538       else if (cp_parser_parse_definitely (parser))
5539         {
5540           pseudo_destructor_p = true;
5541           postfix_expression
5542             = finish_pseudo_destructor_expr (postfix_expression,
5543                                              s, TREE_TYPE (type));
5544         }
5545     }
5546
5547   if (!pseudo_destructor_p)
5548     {
5549       /* If the SCOPE is not a scalar type, we are looking at an
5550          ordinary class member access expression, rather than a
5551          pseudo-destructor-name.  */
5552       bool template_p;
5553       cp_token *token = cp_lexer_peek_token (parser->lexer);
5554       /* Parse the id-expression.  */
5555       name = (cp_parser_id_expression
5556               (parser,
5557                cp_parser_optional_template_keyword (parser),
5558                /*check_dependency_p=*/true,
5559                &template_p,
5560                /*declarator_p=*/false,
5561                /*optional_p=*/false));
5562       /* In general, build a SCOPE_REF if the member name is qualified.
5563          However, if the name was not dependent and has already been
5564          resolved; there is no need to build the SCOPE_REF.  For example;
5565
5566              struct X { void f(); };
5567              template <typename T> void f(T* t) { t->X::f(); }
5568
5569          Even though "t" is dependent, "X::f" is not and has been resolved
5570          to a BASELINK; there is no need to include scope information.  */
5571
5572       /* But we do need to remember that there was an explicit scope for
5573          virtual function calls.  */
5574       if (parser->scope)
5575         *idk = CP_ID_KIND_QUALIFIED;
5576
5577       /* If the name is a template-id that names a type, we will get a
5578          TYPE_DECL here.  That is invalid code.  */
5579       if (TREE_CODE (name) == TYPE_DECL)
5580         {
5581           error_at (token->location, "invalid use of %qD", name);
5582           postfix_expression = error_mark_node;
5583         }
5584       else
5585         {
5586           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5587             {
5588               name = build_qualified_name (/*type=*/NULL_TREE,
5589                                            parser->scope,
5590                                            name,
5591                                            template_p);
5592               parser->scope = NULL_TREE;
5593               parser->qualifying_scope = NULL_TREE;
5594               parser->object_scope = NULL_TREE;
5595             }
5596           if (scope && name && BASELINK_P (name))
5597             adjust_result_of_qualified_name_lookup
5598               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5599           postfix_expression
5600             = finish_class_member_access_expr (postfix_expression, name,
5601                                                template_p, 
5602                                                tf_warning_or_error);
5603         }
5604     }
5605
5606   /* We no longer need to look up names in the scope of the object on
5607      the left-hand side of the `.' or `->' operator.  */
5608   parser->context->object_type = NULL_TREE;
5609
5610   /* Outside of offsetof, these operators may not appear in
5611      constant-expressions.  */
5612   if (!for_offsetof
5613       && (cp_parser_non_integral_constant_expression
5614           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5615     postfix_expression = error_mark_node;
5616
5617   return postfix_expression;
5618 }
5619
5620 /* Parse a parenthesized expression-list.
5621
5622    expression-list:
5623      assignment-expression
5624      expression-list, assignment-expression
5625
5626    attribute-list:
5627      expression-list
5628      identifier
5629      identifier, expression-list
5630
5631    CAST_P is true if this expression is the target of a cast.
5632
5633    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5634    argument pack.
5635
5636    Returns a vector of trees.  Each element is a representation of an
5637    assignment-expression.  NULL is returned if the ( and or ) are
5638    missing.  An empty, but allocated, vector is returned on no
5639    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5640    if we are parsing an attribute list for an attribute that wants a
5641    plain identifier argument, normal_attr for an attribute that wants
5642    an expression, or non_attr if we aren't parsing an attribute list.  If
5643    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5644    not all of the expressions in the list were constant.  */
5645
5646 static VEC(tree,gc) *
5647 cp_parser_parenthesized_expression_list (cp_parser* parser,
5648                                          int is_attribute_list,
5649                                          bool cast_p,
5650                                          bool allow_expansion_p,
5651                                          bool *non_constant_p)
5652 {
5653   VEC(tree,gc) *expression_list;
5654   bool fold_expr_p = is_attribute_list != non_attr;
5655   tree identifier = NULL_TREE;
5656   bool saved_greater_than_is_operator_p;
5657
5658   /* Assume all the expressions will be constant.  */
5659   if (non_constant_p)
5660     *non_constant_p = false;
5661
5662   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5663     return NULL;
5664
5665   expression_list = make_tree_vector ();
5666
5667   /* Within a parenthesized expression, a `>' token is always
5668      the greater-than operator.  */
5669   saved_greater_than_is_operator_p
5670     = parser->greater_than_is_operator_p;
5671   parser->greater_than_is_operator_p = true;
5672
5673   /* Consume expressions until there are no more.  */
5674   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5675     while (true)
5676       {
5677         tree expr;
5678
5679         /* At the beginning of attribute lists, check to see if the
5680            next token is an identifier.  */
5681         if (is_attribute_list == id_attr
5682             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5683           {
5684             cp_token *token;
5685
5686             /* Consume the identifier.  */
5687             token = cp_lexer_consume_token (parser->lexer);
5688             /* Save the identifier.  */
5689             identifier = token->u.value;
5690           }
5691         else
5692           {
5693             bool expr_non_constant_p;
5694
5695             /* Parse the next assignment-expression.  */
5696             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5697               {
5698                 /* A braced-init-list.  */
5699                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5700                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5701                 if (non_constant_p && expr_non_constant_p)
5702                   *non_constant_p = true;
5703               }
5704             else if (non_constant_p)
5705               {
5706                 expr = (cp_parser_constant_expression
5707                         (parser, /*allow_non_constant_p=*/true,
5708                          &expr_non_constant_p));
5709                 if (expr_non_constant_p)
5710                   *non_constant_p = true;
5711               }
5712             else
5713               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5714
5715             if (fold_expr_p)
5716               expr = fold_non_dependent_expr (expr);
5717
5718             /* If we have an ellipsis, then this is an expression
5719                expansion.  */
5720             if (allow_expansion_p
5721                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5722               {
5723                 /* Consume the `...'.  */
5724                 cp_lexer_consume_token (parser->lexer);
5725
5726                 /* Build the argument pack.  */
5727                 expr = make_pack_expansion (expr);
5728               }
5729
5730              /* Add it to the list.  We add error_mark_node
5731                 expressions to the list, so that we can still tell if
5732                 the correct form for a parenthesized expression-list
5733                 is found. That gives better errors.  */
5734             VEC_safe_push (tree, gc, expression_list, expr);
5735
5736             if (expr == error_mark_node)
5737               goto skip_comma;
5738           }
5739
5740         /* After the first item, attribute lists look the same as
5741            expression lists.  */
5742         is_attribute_list = non_attr;
5743
5744       get_comma:;
5745         /* If the next token isn't a `,', then we are done.  */
5746         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5747           break;
5748
5749         /* Otherwise, consume the `,' and keep going.  */
5750         cp_lexer_consume_token (parser->lexer);
5751       }
5752
5753   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5754     {
5755       int ending;
5756
5757     skip_comma:;
5758       /* We try and resync to an unnested comma, as that will give the
5759          user better diagnostics.  */
5760       ending = cp_parser_skip_to_closing_parenthesis (parser,
5761                                                       /*recovering=*/true,
5762                                                       /*or_comma=*/true,
5763                                                       /*consume_paren=*/true);
5764       if (ending < 0)
5765         goto get_comma;
5766       if (!ending)
5767         {
5768           parser->greater_than_is_operator_p
5769             = saved_greater_than_is_operator_p;
5770           return NULL;
5771         }
5772     }
5773
5774   parser->greater_than_is_operator_p
5775     = saved_greater_than_is_operator_p;
5776
5777   if (identifier)
5778     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5779
5780   return expression_list;
5781 }
5782
5783 /* Parse a pseudo-destructor-name.
5784
5785    pseudo-destructor-name:
5786      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5787      :: [opt] nested-name-specifier template template-id :: ~ type-name
5788      :: [opt] nested-name-specifier [opt] ~ type-name
5789
5790    If either of the first two productions is used, sets *SCOPE to the
5791    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5792    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5793    or ERROR_MARK_NODE if the parse fails.  */
5794
5795 static void
5796 cp_parser_pseudo_destructor_name (cp_parser* parser,
5797                                   tree* scope,
5798                                   tree* type)
5799 {
5800   bool nested_name_specifier_p;
5801
5802   /* Assume that things will not work out.  */
5803   *type = error_mark_node;
5804
5805   /* Look for the optional `::' operator.  */
5806   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5807   /* Look for the optional nested-name-specifier.  */
5808   nested_name_specifier_p
5809     = (cp_parser_nested_name_specifier_opt (parser,
5810                                             /*typename_keyword_p=*/false,
5811                                             /*check_dependency_p=*/true,
5812                                             /*type_p=*/false,
5813                                             /*is_declaration=*/false)
5814        != NULL_TREE);
5815   /* Now, if we saw a nested-name-specifier, we might be doing the
5816      second production.  */
5817   if (nested_name_specifier_p
5818       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5819     {
5820       /* Consume the `template' keyword.  */
5821       cp_lexer_consume_token (parser->lexer);
5822       /* Parse the template-id.  */
5823       cp_parser_template_id (parser,
5824                              /*template_keyword_p=*/true,
5825                              /*check_dependency_p=*/false,
5826                              /*is_declaration=*/true);
5827       /* Look for the `::' token.  */
5828       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5829     }
5830   /* If the next token is not a `~', then there might be some
5831      additional qualification.  */
5832   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5833     {
5834       /* At this point, we're looking for "type-name :: ~".  The type-name
5835          must not be a class-name, since this is a pseudo-destructor.  So,
5836          it must be either an enum-name, or a typedef-name -- both of which
5837          are just identifiers.  So, we peek ahead to check that the "::"
5838          and "~" tokens are present; if they are not, then we can avoid
5839          calling type_name.  */
5840       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5841           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5842           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5843         {
5844           cp_parser_error (parser, "non-scalar type");
5845           return;
5846         }
5847
5848       /* Look for the type-name.  */
5849       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5850       if (*scope == error_mark_node)
5851         return;
5852
5853       /* Look for the `::' token.  */
5854       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5855     }
5856   else
5857     *scope = NULL_TREE;
5858
5859   /* Look for the `~'.  */
5860   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5861   /* Look for the type-name again.  We are not responsible for
5862      checking that it matches the first type-name.  */
5863   *type = cp_parser_nonclass_name (parser);
5864 }
5865
5866 /* Parse a unary-expression.
5867
5868    unary-expression:
5869      postfix-expression
5870      ++ cast-expression
5871      -- cast-expression
5872      unary-operator cast-expression
5873      sizeof unary-expression
5874      sizeof ( type-id )
5875      new-expression
5876      delete-expression
5877
5878    GNU Extensions:
5879
5880    unary-expression:
5881      __extension__ cast-expression
5882      __alignof__ unary-expression
5883      __alignof__ ( type-id )
5884      __real__ cast-expression
5885      __imag__ cast-expression
5886      && identifier
5887
5888    ADDRESS_P is true iff the unary-expression is appearing as the
5889    operand of the `&' operator.   CAST_P is true if this expression is
5890    the target of a cast.
5891
5892    Returns a representation of the expression.  */
5893
5894 static tree
5895 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5896                             cp_id_kind * pidk)
5897 {
5898   cp_token *token;
5899   enum tree_code unary_operator;
5900
5901   /* Peek at the next token.  */
5902   token = cp_lexer_peek_token (parser->lexer);
5903   /* Some keywords give away the kind of expression.  */
5904   if (token->type == CPP_KEYWORD)
5905     {
5906       enum rid keyword = token->keyword;
5907
5908       switch (keyword)
5909         {
5910         case RID_ALIGNOF:
5911         case RID_SIZEOF:
5912           {
5913             tree operand;
5914             enum tree_code op;
5915
5916             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5917             /* Consume the token.  */
5918             cp_lexer_consume_token (parser->lexer);
5919             /* Parse the operand.  */
5920             operand = cp_parser_sizeof_operand (parser, keyword);
5921
5922             if (TYPE_P (operand))
5923               return cxx_sizeof_or_alignof_type (operand, op, true);
5924             else
5925               return cxx_sizeof_or_alignof_expr (operand, op, true);
5926           }
5927
5928         case RID_NEW:
5929           return cp_parser_new_expression (parser);
5930
5931         case RID_DELETE:
5932           return cp_parser_delete_expression (parser);
5933
5934         case RID_EXTENSION:
5935           {
5936             /* The saved value of the PEDANTIC flag.  */
5937             int saved_pedantic;
5938             tree expr;
5939
5940             /* Save away the PEDANTIC flag.  */
5941             cp_parser_extension_opt (parser, &saved_pedantic);
5942             /* Parse the cast-expression.  */
5943             expr = cp_parser_simple_cast_expression (parser);
5944             /* Restore the PEDANTIC flag.  */
5945             pedantic = saved_pedantic;
5946
5947             return expr;
5948           }
5949
5950         case RID_REALPART:
5951         case RID_IMAGPART:
5952           {
5953             tree expression;
5954
5955             /* Consume the `__real__' or `__imag__' token.  */
5956             cp_lexer_consume_token (parser->lexer);
5957             /* Parse the cast-expression.  */
5958             expression = cp_parser_simple_cast_expression (parser);
5959             /* Create the complete representation.  */
5960             return build_x_unary_op ((keyword == RID_REALPART
5961                                       ? REALPART_EXPR : IMAGPART_EXPR),
5962                                      expression,
5963                                      tf_warning_or_error);
5964           }
5965           break;
5966
5967         case RID_NOEXCEPT:
5968           {
5969             tree expr;
5970             const char *saved_message;
5971             bool saved_integral_constant_expression_p;
5972             bool saved_non_integral_constant_expression_p;
5973             bool saved_greater_than_is_operator_p;
5974
5975             cp_lexer_consume_token (parser->lexer);
5976             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5977
5978             saved_message = parser->type_definition_forbidden_message;
5979             parser->type_definition_forbidden_message
5980               = G_("types may not be defined in %<noexcept%> expressions");
5981
5982             saved_integral_constant_expression_p
5983               = parser->integral_constant_expression_p;
5984             saved_non_integral_constant_expression_p
5985               = parser->non_integral_constant_expression_p;
5986             parser->integral_constant_expression_p = false;
5987
5988             saved_greater_than_is_operator_p
5989               = parser->greater_than_is_operator_p;
5990             parser->greater_than_is_operator_p = true;
5991
5992             ++cp_unevaluated_operand;
5993             ++c_inhibit_evaluation_warnings;
5994             expr = cp_parser_expression (parser, false, NULL);
5995             --c_inhibit_evaluation_warnings;
5996             --cp_unevaluated_operand;
5997
5998             parser->greater_than_is_operator_p
5999               = saved_greater_than_is_operator_p;
6000
6001             parser->integral_constant_expression_p
6002               = saved_integral_constant_expression_p;
6003             parser->non_integral_constant_expression_p
6004               = saved_non_integral_constant_expression_p;
6005
6006             parser->type_definition_forbidden_message = saved_message;
6007
6008             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6009             return finish_noexcept_expr (expr, tf_warning_or_error);
6010           }
6011
6012         default:
6013           break;
6014         }
6015     }
6016
6017   /* Look for the `:: new' and `:: delete', which also signal the
6018      beginning of a new-expression, or delete-expression,
6019      respectively.  If the next token is `::', then it might be one of
6020      these.  */
6021   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6022     {
6023       enum rid keyword;
6024
6025       /* See if the token after the `::' is one of the keywords in
6026          which we're interested.  */
6027       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6028       /* If it's `new', we have a new-expression.  */
6029       if (keyword == RID_NEW)
6030         return cp_parser_new_expression (parser);
6031       /* Similarly, for `delete'.  */
6032       else if (keyword == RID_DELETE)
6033         return cp_parser_delete_expression (parser);
6034     }
6035
6036   /* Look for a unary operator.  */
6037   unary_operator = cp_parser_unary_operator (token);
6038   /* The `++' and `--' operators can be handled similarly, even though
6039      they are not technically unary-operators in the grammar.  */
6040   if (unary_operator == ERROR_MARK)
6041     {
6042       if (token->type == CPP_PLUS_PLUS)
6043         unary_operator = PREINCREMENT_EXPR;
6044       else if (token->type == CPP_MINUS_MINUS)
6045         unary_operator = PREDECREMENT_EXPR;
6046       /* Handle the GNU address-of-label extension.  */
6047       else if (cp_parser_allow_gnu_extensions_p (parser)
6048                && token->type == CPP_AND_AND)
6049         {
6050           tree identifier;
6051           tree expression;
6052           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6053
6054           /* Consume the '&&' token.  */
6055           cp_lexer_consume_token (parser->lexer);
6056           /* Look for the identifier.  */
6057           identifier = cp_parser_identifier (parser);
6058           /* Create an expression representing the address.  */
6059           expression = finish_label_address_expr (identifier, loc);
6060           if (cp_parser_non_integral_constant_expression (parser,
6061                                                           NIC_ADDR_LABEL))
6062             expression = error_mark_node;
6063           return expression;
6064         }
6065     }
6066   if (unary_operator != ERROR_MARK)
6067     {
6068       tree cast_expression;
6069       tree expression = error_mark_node;
6070       non_integral_constant non_constant_p = NIC_NONE;
6071
6072       /* Consume the operator token.  */
6073       token = cp_lexer_consume_token (parser->lexer);
6074       /* Parse the cast-expression.  */
6075       cast_expression
6076         = cp_parser_cast_expression (parser,
6077                                      unary_operator == ADDR_EXPR,
6078                                      /*cast_p=*/false, pidk);
6079       /* Now, build an appropriate representation.  */
6080       switch (unary_operator)
6081         {
6082         case INDIRECT_REF:
6083           non_constant_p = NIC_STAR;
6084           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6085                                              tf_warning_or_error);
6086           break;
6087
6088         case ADDR_EXPR:
6089            non_constant_p = NIC_ADDR;
6090           /* Fall through.  */
6091         case BIT_NOT_EXPR:
6092           expression = build_x_unary_op (unary_operator, cast_expression,
6093                                          tf_warning_or_error);
6094           break;
6095
6096         case PREINCREMENT_EXPR:
6097         case PREDECREMENT_EXPR:
6098           non_constant_p = unary_operator == PREINCREMENT_EXPR
6099                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6100           /* Fall through.  */
6101         case UNARY_PLUS_EXPR:
6102         case NEGATE_EXPR:
6103         case TRUTH_NOT_EXPR:
6104           expression = finish_unary_op_expr (unary_operator, cast_expression);
6105           break;
6106
6107         default:
6108           gcc_unreachable ();
6109         }
6110
6111       if (non_constant_p != NIC_NONE
6112           && cp_parser_non_integral_constant_expression (parser,
6113                                                          non_constant_p))
6114         expression = error_mark_node;
6115
6116       return expression;
6117     }
6118
6119   return cp_parser_postfix_expression (parser, address_p, cast_p,
6120                                        /*member_access_only_p=*/false,
6121                                        pidk);
6122 }
6123
6124 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6125    unary-operator, the corresponding tree code is returned.  */
6126
6127 static enum tree_code
6128 cp_parser_unary_operator (cp_token* token)
6129 {
6130   switch (token->type)
6131     {
6132     case CPP_MULT:
6133       return INDIRECT_REF;
6134
6135     case CPP_AND:
6136       return ADDR_EXPR;
6137
6138     case CPP_PLUS:
6139       return UNARY_PLUS_EXPR;
6140
6141     case CPP_MINUS:
6142       return NEGATE_EXPR;
6143
6144     case CPP_NOT:
6145       return TRUTH_NOT_EXPR;
6146
6147     case CPP_COMPL:
6148       return BIT_NOT_EXPR;
6149
6150     default:
6151       return ERROR_MARK;
6152     }
6153 }
6154
6155 /* Parse a new-expression.
6156
6157    new-expression:
6158      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6159      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6160
6161    Returns a representation of the expression.  */
6162
6163 static tree
6164 cp_parser_new_expression (cp_parser* parser)
6165 {
6166   bool global_scope_p;
6167   VEC(tree,gc) *placement;
6168   tree type;
6169   VEC(tree,gc) *initializer;
6170   tree nelts;
6171   tree ret;
6172
6173   /* Look for the optional `::' operator.  */
6174   global_scope_p
6175     = (cp_parser_global_scope_opt (parser,
6176                                    /*current_scope_valid_p=*/false)
6177        != NULL_TREE);
6178   /* Look for the `new' operator.  */
6179   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6180   /* There's no easy way to tell a new-placement from the
6181      `( type-id )' construct.  */
6182   cp_parser_parse_tentatively (parser);
6183   /* Look for a new-placement.  */
6184   placement = cp_parser_new_placement (parser);
6185   /* If that didn't work out, there's no new-placement.  */
6186   if (!cp_parser_parse_definitely (parser))
6187     {
6188       if (placement != NULL)
6189         release_tree_vector (placement);
6190       placement = NULL;
6191     }
6192
6193   /* If the next token is a `(', then we have a parenthesized
6194      type-id.  */
6195   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6196     {
6197       cp_token *token;
6198       /* Consume the `('.  */
6199       cp_lexer_consume_token (parser->lexer);
6200       /* Parse the type-id.  */
6201       type = cp_parser_type_id (parser);
6202       /* Look for the closing `)'.  */
6203       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6204       token = cp_lexer_peek_token (parser->lexer);
6205       /* There should not be a direct-new-declarator in this production,
6206          but GCC used to allowed this, so we check and emit a sensible error
6207          message for this case.  */
6208       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6209         {
6210           error_at (token->location,
6211                     "array bound forbidden after parenthesized type-id");
6212           inform (token->location, 
6213                   "try removing the parentheses around the type-id");
6214           cp_parser_direct_new_declarator (parser);
6215         }
6216       nelts = NULL_TREE;
6217     }
6218   /* Otherwise, there must be a new-type-id.  */
6219   else
6220     type = cp_parser_new_type_id (parser, &nelts);
6221
6222   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6224       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6225     initializer = cp_parser_new_initializer (parser);
6226   else
6227     initializer = NULL;
6228
6229   /* A new-expression may not appear in an integral constant
6230      expression.  */
6231   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6232     ret = error_mark_node;
6233   else
6234     {
6235       /* Create a representation of the new-expression.  */
6236       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6237                        tf_warning_or_error);
6238     }
6239
6240   if (placement != NULL)
6241     release_tree_vector (placement);
6242   if (initializer != NULL)
6243     release_tree_vector (initializer);
6244
6245   return ret;
6246 }
6247
6248 /* Parse a new-placement.
6249
6250    new-placement:
6251      ( expression-list )
6252
6253    Returns the same representation as for an expression-list.  */
6254
6255 static VEC(tree,gc) *
6256 cp_parser_new_placement (cp_parser* parser)
6257 {
6258   VEC(tree,gc) *expression_list;
6259
6260   /* Parse the expression-list.  */
6261   expression_list = (cp_parser_parenthesized_expression_list
6262                      (parser, non_attr, /*cast_p=*/false,
6263                       /*allow_expansion_p=*/true,
6264                       /*non_constant_p=*/NULL));
6265
6266   return expression_list;
6267 }
6268
6269 /* Parse a new-type-id.
6270
6271    new-type-id:
6272      type-specifier-seq new-declarator [opt]
6273
6274    Returns the TYPE allocated.  If the new-type-id indicates an array
6275    type, *NELTS is set to the number of elements in the last array
6276    bound; the TYPE will not include the last array bound.  */
6277
6278 static tree
6279 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6280 {
6281   cp_decl_specifier_seq type_specifier_seq;
6282   cp_declarator *new_declarator;
6283   cp_declarator *declarator;
6284   cp_declarator *outer_declarator;
6285   const char *saved_message;
6286   tree type;
6287
6288   /* The type-specifier sequence must not contain type definitions.
6289      (It cannot contain declarations of new types either, but if they
6290      are not definitions we will catch that because they are not
6291      complete.)  */
6292   saved_message = parser->type_definition_forbidden_message;
6293   parser->type_definition_forbidden_message
6294     = G_("types may not be defined in a new-type-id");
6295   /* Parse the type-specifier-seq.  */
6296   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6297                                 /*is_trailing_return=*/false,
6298                                 &type_specifier_seq);
6299   /* Restore the old message.  */
6300   parser->type_definition_forbidden_message = saved_message;
6301   /* Parse the new-declarator.  */
6302   new_declarator = cp_parser_new_declarator_opt (parser);
6303
6304   /* Determine the number of elements in the last array dimension, if
6305      any.  */
6306   *nelts = NULL_TREE;
6307   /* Skip down to the last array dimension.  */
6308   declarator = new_declarator;
6309   outer_declarator = NULL;
6310   while (declarator && (declarator->kind == cdk_pointer
6311                         || declarator->kind == cdk_ptrmem))
6312     {
6313       outer_declarator = declarator;
6314       declarator = declarator->declarator;
6315     }
6316   while (declarator
6317          && declarator->kind == cdk_array
6318          && declarator->declarator
6319          && declarator->declarator->kind == cdk_array)
6320     {
6321       outer_declarator = declarator;
6322       declarator = declarator->declarator;
6323     }
6324
6325   if (declarator && declarator->kind == cdk_array)
6326     {
6327       *nelts = declarator->u.array.bounds;
6328       if (*nelts == error_mark_node)
6329         *nelts = integer_one_node;
6330
6331       if (outer_declarator)
6332         outer_declarator->declarator = declarator->declarator;
6333       else
6334         new_declarator = NULL;
6335     }
6336
6337   type = groktypename (&type_specifier_seq, new_declarator, false);
6338   return type;
6339 }
6340
6341 /* Parse an (optional) new-declarator.
6342
6343    new-declarator:
6344      ptr-operator new-declarator [opt]
6345      direct-new-declarator
6346
6347    Returns the declarator.  */
6348
6349 static cp_declarator *
6350 cp_parser_new_declarator_opt (cp_parser* parser)
6351 {
6352   enum tree_code code;
6353   tree type;
6354   cp_cv_quals cv_quals;
6355
6356   /* We don't know if there's a ptr-operator next, or not.  */
6357   cp_parser_parse_tentatively (parser);
6358   /* Look for a ptr-operator.  */
6359   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6360   /* If that worked, look for more new-declarators.  */
6361   if (cp_parser_parse_definitely (parser))
6362     {
6363       cp_declarator *declarator;
6364
6365       /* Parse another optional declarator.  */
6366       declarator = cp_parser_new_declarator_opt (parser);
6367
6368       return cp_parser_make_indirect_declarator
6369         (code, type, cv_quals, declarator);
6370     }
6371
6372   /* If the next token is a `[', there is a direct-new-declarator.  */
6373   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6374     return cp_parser_direct_new_declarator (parser);
6375
6376   return NULL;
6377 }
6378
6379 /* Parse a direct-new-declarator.
6380
6381    direct-new-declarator:
6382      [ expression ]
6383      direct-new-declarator [constant-expression]
6384
6385    */
6386
6387 static cp_declarator *
6388 cp_parser_direct_new_declarator (cp_parser* parser)
6389 {
6390   cp_declarator *declarator = NULL;
6391
6392   while (true)
6393     {
6394       tree expression;
6395
6396       /* Look for the opening `['.  */
6397       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6398       /* The first expression is not required to be constant.  */
6399       if (!declarator)
6400         {
6401           cp_token *token = cp_lexer_peek_token (parser->lexer);
6402           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6403           /* The standard requires that the expression have integral
6404              type.  DR 74 adds enumeration types.  We believe that the
6405              real intent is that these expressions be handled like the
6406              expression in a `switch' condition, which also allows
6407              classes with a single conversion to integral or
6408              enumeration type.  */
6409           if (!processing_template_decl)
6410             {
6411               expression
6412                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6413                                               expression,
6414                                               /*complain=*/true);
6415               if (!expression)
6416                 {
6417                   error_at (token->location,
6418                             "expression in new-declarator must have integral "
6419                             "or enumeration type");
6420                   expression = error_mark_node;
6421                 }
6422             }
6423         }
6424       /* But all the other expressions must be.  */
6425       else
6426         expression
6427           = cp_parser_constant_expression (parser,
6428                                            /*allow_non_constant=*/false,
6429                                            NULL);
6430       /* Look for the closing `]'.  */
6431       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6432
6433       /* Add this bound to the declarator.  */
6434       declarator = make_array_declarator (declarator, expression);
6435
6436       /* If the next token is not a `[', then there are no more
6437          bounds.  */
6438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6439         break;
6440     }
6441
6442   return declarator;
6443 }
6444
6445 /* Parse a new-initializer.
6446
6447    new-initializer:
6448      ( expression-list [opt] )
6449      braced-init-list
6450
6451    Returns a representation of the expression-list.  */
6452
6453 static VEC(tree,gc) *
6454 cp_parser_new_initializer (cp_parser* parser)
6455 {
6456   VEC(tree,gc) *expression_list;
6457
6458   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6459     {
6460       tree t;
6461       bool expr_non_constant_p;
6462       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6463       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6464       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6465       expression_list = make_tree_vector_single (t);
6466     }
6467   else
6468     expression_list = (cp_parser_parenthesized_expression_list
6469                        (parser, non_attr, /*cast_p=*/false,
6470                         /*allow_expansion_p=*/true,
6471                         /*non_constant_p=*/NULL));
6472
6473   return expression_list;
6474 }
6475
6476 /* Parse a delete-expression.
6477
6478    delete-expression:
6479      :: [opt] delete cast-expression
6480      :: [opt] delete [ ] cast-expression
6481
6482    Returns a representation of the expression.  */
6483
6484 static tree
6485 cp_parser_delete_expression (cp_parser* parser)
6486 {
6487   bool global_scope_p;
6488   bool array_p;
6489   tree expression;
6490
6491   /* Look for the optional `::' operator.  */
6492   global_scope_p
6493     = (cp_parser_global_scope_opt (parser,
6494                                    /*current_scope_valid_p=*/false)
6495        != NULL_TREE);
6496   /* Look for the `delete' keyword.  */
6497   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6498   /* See if the array syntax is in use.  */
6499   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6500     {
6501       /* Consume the `[' token.  */
6502       cp_lexer_consume_token (parser->lexer);
6503       /* Look for the `]' token.  */
6504       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6505       /* Remember that this is the `[]' construct.  */
6506       array_p = true;
6507     }
6508   else
6509     array_p = false;
6510
6511   /* Parse the cast-expression.  */
6512   expression = cp_parser_simple_cast_expression (parser);
6513
6514   /* A delete-expression may not appear in an integral constant
6515      expression.  */
6516   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6517     return error_mark_node;
6518
6519   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6520 }
6521
6522 /* Returns true if TOKEN may start a cast-expression and false
6523    otherwise.  */
6524
6525 static bool
6526 cp_parser_token_starts_cast_expression (cp_token *token)
6527 {
6528   switch (token->type)
6529     {
6530     case CPP_COMMA:
6531     case CPP_SEMICOLON:
6532     case CPP_QUERY:
6533     case CPP_COLON:
6534     case CPP_CLOSE_SQUARE:
6535     case CPP_CLOSE_PAREN:
6536     case CPP_CLOSE_BRACE:
6537     case CPP_DOT:
6538     case CPP_DOT_STAR:
6539     case CPP_DEREF:
6540     case CPP_DEREF_STAR:
6541     case CPP_DIV:
6542     case CPP_MOD:
6543     case CPP_LSHIFT:
6544     case CPP_RSHIFT:
6545     case CPP_LESS:
6546     case CPP_GREATER:
6547     case CPP_LESS_EQ:
6548     case CPP_GREATER_EQ:
6549     case CPP_EQ_EQ:
6550     case CPP_NOT_EQ:
6551     case CPP_EQ:
6552     case CPP_MULT_EQ:
6553     case CPP_DIV_EQ:
6554     case CPP_MOD_EQ:
6555     case CPP_PLUS_EQ:
6556     case CPP_MINUS_EQ:
6557     case CPP_RSHIFT_EQ:
6558     case CPP_LSHIFT_EQ:
6559     case CPP_AND_EQ:
6560     case CPP_XOR_EQ:
6561     case CPP_OR_EQ:
6562     case CPP_XOR:
6563     case CPP_OR:
6564     case CPP_OR_OR:
6565     case CPP_EOF:
6566       return false;
6567
6568       /* '[' may start a primary-expression in obj-c++.  */
6569     case CPP_OPEN_SQUARE:
6570       return c_dialect_objc ();
6571
6572     default:
6573       return true;
6574     }
6575 }
6576
6577 /* Parse a cast-expression.
6578
6579    cast-expression:
6580      unary-expression
6581      ( type-id ) cast-expression
6582
6583    ADDRESS_P is true iff the unary-expression is appearing as the
6584    operand of the `&' operator.   CAST_P is true if this expression is
6585    the target of a cast.
6586
6587    Returns a representation of the expression.  */
6588
6589 static tree
6590 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6591                            cp_id_kind * pidk)
6592 {
6593   /* If it's a `(', then we might be looking at a cast.  */
6594   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6595     {
6596       tree type = NULL_TREE;
6597       tree expr = NULL_TREE;
6598       bool compound_literal_p;
6599       const char *saved_message;
6600
6601       /* There's no way to know yet whether or not this is a cast.
6602          For example, `(int (3))' is a unary-expression, while `(int)
6603          3' is a cast.  So, we resort to parsing tentatively.  */
6604       cp_parser_parse_tentatively (parser);
6605       /* Types may not be defined in a cast.  */
6606       saved_message = parser->type_definition_forbidden_message;
6607       parser->type_definition_forbidden_message
6608         = G_("types may not be defined in casts");
6609       /* Consume the `('.  */
6610       cp_lexer_consume_token (parser->lexer);
6611       /* A very tricky bit is that `(struct S) { 3 }' is a
6612          compound-literal (which we permit in C++ as an extension).
6613          But, that construct is not a cast-expression -- it is a
6614          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6615          is legal; if the compound-literal were a cast-expression,
6616          you'd need an extra set of parentheses.)  But, if we parse
6617          the type-id, and it happens to be a class-specifier, then we
6618          will commit to the parse at that point, because we cannot
6619          undo the action that is done when creating a new class.  So,
6620          then we cannot back up and do a postfix-expression.
6621
6622          Therefore, we scan ahead to the closing `)', and check to see
6623          if the token after the `)' is a `{'.  If so, we are not
6624          looking at a cast-expression.
6625
6626          Save tokens so that we can put them back.  */
6627       cp_lexer_save_tokens (parser->lexer);
6628       /* Skip tokens until the next token is a closing parenthesis.
6629          If we find the closing `)', and the next token is a `{', then
6630          we are looking at a compound-literal.  */
6631       compound_literal_p
6632         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6633                                                   /*consume_paren=*/true)
6634            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6635       /* Roll back the tokens we skipped.  */
6636       cp_lexer_rollback_tokens (parser->lexer);
6637       /* If we were looking at a compound-literal, simulate an error
6638          so that the call to cp_parser_parse_definitely below will
6639          fail.  */
6640       if (compound_literal_p)
6641         cp_parser_simulate_error (parser);
6642       else
6643         {
6644           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6645           parser->in_type_id_in_expr_p = true;
6646           /* Look for the type-id.  */
6647           type = cp_parser_type_id (parser);
6648           /* Look for the closing `)'.  */
6649           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6650           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6651         }
6652
6653       /* Restore the saved message.  */
6654       parser->type_definition_forbidden_message = saved_message;
6655
6656       /* At this point this can only be either a cast or a
6657          parenthesized ctor such as `(T ())' that looks like a cast to
6658          function returning T.  */
6659       if (!cp_parser_error_occurred (parser)
6660           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6661                                                      (parser->lexer)))
6662         {
6663           cp_parser_parse_definitely (parser);
6664           expr = cp_parser_cast_expression (parser,
6665                                             /*address_p=*/false,
6666                                             /*cast_p=*/true, pidk);
6667
6668           /* Warn about old-style casts, if so requested.  */
6669           if (warn_old_style_cast
6670               && !in_system_header
6671               && !VOID_TYPE_P (type)
6672               && current_lang_name != lang_name_c)
6673             warning (OPT_Wold_style_cast, "use of old-style cast");
6674
6675           /* Only type conversions to integral or enumeration types
6676              can be used in constant-expressions.  */
6677           if (!cast_valid_in_integral_constant_expression_p (type)
6678               && cp_parser_non_integral_constant_expression (parser,
6679                                                              NIC_CAST))
6680             return error_mark_node;
6681
6682           /* Perform the cast.  */
6683           expr = build_c_cast (input_location, type, expr);
6684           return expr;
6685         }
6686       else 
6687         cp_parser_abort_tentative_parse (parser);
6688     }
6689
6690   /* If we get here, then it's not a cast, so it must be a
6691      unary-expression.  */
6692   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6693 }
6694
6695 /* Parse a binary expression of the general form:
6696
6697    pm-expression:
6698      cast-expression
6699      pm-expression .* cast-expression
6700      pm-expression ->* cast-expression
6701
6702    multiplicative-expression:
6703      pm-expression
6704      multiplicative-expression * pm-expression
6705      multiplicative-expression / pm-expression
6706      multiplicative-expression % pm-expression
6707
6708    additive-expression:
6709      multiplicative-expression
6710      additive-expression + multiplicative-expression
6711      additive-expression - multiplicative-expression
6712
6713    shift-expression:
6714      additive-expression
6715      shift-expression << additive-expression
6716      shift-expression >> additive-expression
6717
6718    relational-expression:
6719      shift-expression
6720      relational-expression < shift-expression
6721      relational-expression > shift-expression
6722      relational-expression <= shift-expression
6723      relational-expression >= shift-expression
6724
6725   GNU Extension:
6726
6727    relational-expression:
6728      relational-expression <? shift-expression
6729      relational-expression >? shift-expression
6730
6731    equality-expression:
6732      relational-expression
6733      equality-expression == relational-expression
6734      equality-expression != relational-expression
6735
6736    and-expression:
6737      equality-expression
6738      and-expression & equality-expression
6739
6740    exclusive-or-expression:
6741      and-expression
6742      exclusive-or-expression ^ and-expression
6743
6744    inclusive-or-expression:
6745      exclusive-or-expression
6746      inclusive-or-expression | exclusive-or-expression
6747
6748    logical-and-expression:
6749      inclusive-or-expression
6750      logical-and-expression && inclusive-or-expression
6751
6752    logical-or-expression:
6753      logical-and-expression
6754      logical-or-expression || logical-and-expression
6755
6756    All these are implemented with a single function like:
6757
6758    binary-expression:
6759      simple-cast-expression
6760      binary-expression <token> binary-expression
6761
6762    CAST_P is true if this expression is the target of a cast.
6763
6764    The binops_by_token map is used to get the tree codes for each <token> type.
6765    binary-expressions are associated according to a precedence table.  */
6766
6767 #define TOKEN_PRECEDENCE(token)                              \
6768 (((token->type == CPP_GREATER                                \
6769    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6770   && !parser->greater_than_is_operator_p)                    \
6771  ? PREC_NOT_OPERATOR                                         \
6772  : binops_by_token[token->type].prec)
6773
6774 static tree
6775 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6776                              bool no_toplevel_fold_p,
6777                              enum cp_parser_prec prec,
6778                              cp_id_kind * pidk)
6779 {
6780   cp_parser_expression_stack stack;
6781   cp_parser_expression_stack_entry *sp = &stack[0];
6782   tree lhs, rhs;
6783   cp_token *token;
6784   enum tree_code tree_type, lhs_type, rhs_type;
6785   enum cp_parser_prec new_prec, lookahead_prec;
6786   bool overloaded_p;
6787
6788   /* Parse the first expression.  */
6789   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6790   lhs_type = ERROR_MARK;
6791
6792   for (;;)
6793     {
6794       /* Get an operator token.  */
6795       token = cp_lexer_peek_token (parser->lexer);
6796
6797       if (warn_cxx0x_compat
6798           && token->type == CPP_RSHIFT
6799           && !parser->greater_than_is_operator_p)
6800         {
6801           if (warning_at (token->location, OPT_Wc__0x_compat, 
6802                           "%<>>%> operator will be treated as"
6803                           " two right angle brackets in C++0x"))
6804             inform (token->location,
6805                     "suggest parentheses around %<>>%> expression");
6806         }
6807
6808       new_prec = TOKEN_PRECEDENCE (token);
6809
6810       /* Popping an entry off the stack means we completed a subexpression:
6811          - either we found a token which is not an operator (`>' where it is not
6812            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6813            will happen repeatedly;
6814          - or, we found an operator which has lower priority.  This is the case
6815            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6816            parsing `3 * 4'.  */
6817       if (new_prec <= prec)
6818         {
6819           if (sp == stack)
6820             break;
6821           else
6822             goto pop;
6823         }
6824
6825      get_rhs:
6826       tree_type = binops_by_token[token->type].tree_type;
6827
6828       /* We used the operator token.  */
6829       cp_lexer_consume_token (parser->lexer);
6830
6831       /* For "false && x" or "true || x", x will never be executed;
6832          disable warnings while evaluating it.  */
6833       if (tree_type == TRUTH_ANDIF_EXPR)
6834         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6835       else if (tree_type == TRUTH_ORIF_EXPR)
6836         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6837
6838       /* Extract another operand.  It may be the RHS of this expression
6839          or the LHS of a new, higher priority expression.  */
6840       rhs = cp_parser_simple_cast_expression (parser);
6841       rhs_type = ERROR_MARK;
6842
6843       /* Get another operator token.  Look up its precedence to avoid
6844          building a useless (immediately popped) stack entry for common
6845          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6846       token = cp_lexer_peek_token (parser->lexer);
6847       lookahead_prec = TOKEN_PRECEDENCE (token);
6848       if (lookahead_prec > new_prec)
6849         {
6850           /* ... and prepare to parse the RHS of the new, higher priority
6851              expression.  Since precedence levels on the stack are
6852              monotonically increasing, we do not have to care about
6853              stack overflows.  */
6854           sp->prec = prec;
6855           sp->tree_type = tree_type;
6856           sp->lhs = lhs;
6857           sp->lhs_type = lhs_type;
6858           sp++;
6859           lhs = rhs;
6860           lhs_type = rhs_type;
6861           prec = new_prec;
6862           new_prec = lookahead_prec;
6863           goto get_rhs;
6864
6865          pop:
6866           lookahead_prec = new_prec;
6867           /* If the stack is not empty, we have parsed into LHS the right side
6868              (`4' in the example above) of an expression we had suspended.
6869              We can use the information on the stack to recover the LHS (`3')
6870              from the stack together with the tree code (`MULT_EXPR'), and
6871              the precedence of the higher level subexpression
6872              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6873              which will be used to actually build the additive expression.  */
6874           --sp;
6875           prec = sp->prec;
6876           tree_type = sp->tree_type;
6877           rhs = lhs;
6878           rhs_type = lhs_type;
6879           lhs = sp->lhs;
6880           lhs_type = sp->lhs_type;
6881         }
6882
6883       /* Undo the disabling of warnings done above.  */
6884       if (tree_type == TRUTH_ANDIF_EXPR)
6885         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6886       else if (tree_type == TRUTH_ORIF_EXPR)
6887         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6888
6889       overloaded_p = false;
6890       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6891          ERROR_MARK for everything that is not a binary expression.
6892          This makes warn_about_parentheses miss some warnings that
6893          involve unary operators.  For unary expressions we should
6894          pass the correct tree_code unless the unary expression was
6895          surrounded by parentheses.
6896       */
6897       if (no_toplevel_fold_p
6898           && lookahead_prec <= prec
6899           && sp == stack
6900           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6901         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6902       else
6903         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6904                                  &overloaded_p, tf_warning_or_error);
6905       lhs_type = tree_type;
6906
6907       /* If the binary operator required the use of an overloaded operator,
6908          then this expression cannot be an integral constant-expression.
6909          An overloaded operator can be used even if both operands are
6910          otherwise permissible in an integral constant-expression if at
6911          least one of the operands is of enumeration type.  */
6912
6913       if (overloaded_p
6914           && cp_parser_non_integral_constant_expression (parser,
6915                                                          NIC_OVERLOADED))
6916         return error_mark_node;
6917     }
6918
6919   return lhs;
6920 }
6921
6922
6923 /* Parse the `? expression : assignment-expression' part of a
6924    conditional-expression.  The LOGICAL_OR_EXPR is the
6925    logical-or-expression that started the conditional-expression.
6926    Returns a representation of the entire conditional-expression.
6927
6928    This routine is used by cp_parser_assignment_expression.
6929
6930      ? expression : assignment-expression
6931
6932    GNU Extensions:
6933
6934      ? : assignment-expression */
6935
6936 static tree
6937 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6938 {
6939   tree expr;
6940   tree assignment_expr;
6941   struct cp_token *token;
6942
6943   /* Consume the `?' token.  */
6944   cp_lexer_consume_token (parser->lexer);
6945   token = cp_lexer_peek_token (parser->lexer);
6946   if (cp_parser_allow_gnu_extensions_p (parser)
6947       && token->type == CPP_COLON)
6948     {
6949       pedwarn (token->location, OPT_pedantic, 
6950                "ISO C++ does not allow ?: with omitted middle operand");
6951       /* Implicit true clause.  */
6952       expr = NULL_TREE;
6953       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6954       warn_for_omitted_condop (token->location, logical_or_expr);
6955     }
6956   else
6957     {
6958       /* Parse the expression.  */
6959       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6960       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6961       c_inhibit_evaluation_warnings +=
6962         ((logical_or_expr == truthvalue_true_node)
6963          - (logical_or_expr == truthvalue_false_node));
6964     }
6965
6966   /* The next token should be a `:'.  */
6967   cp_parser_require (parser, CPP_COLON, RT_COLON);
6968   /* Parse the assignment-expression.  */
6969   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6970   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6971
6972   /* Build the conditional-expression.  */
6973   return build_x_conditional_expr (logical_or_expr,
6974                                    expr,
6975                                    assignment_expr,
6976                                    tf_warning_or_error);
6977 }
6978
6979 /* Parse an assignment-expression.
6980
6981    assignment-expression:
6982      conditional-expression
6983      logical-or-expression assignment-operator assignment_expression
6984      throw-expression
6985
6986    CAST_P is true if this expression is the target of a cast.
6987
6988    Returns a representation for the expression.  */
6989
6990 static tree
6991 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6992                                  cp_id_kind * pidk)
6993 {
6994   tree expr;
6995
6996   /* If the next token is the `throw' keyword, then we're looking at
6997      a throw-expression.  */
6998   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6999     expr = cp_parser_throw_expression (parser);
7000   /* Otherwise, it must be that we are looking at a
7001      logical-or-expression.  */
7002   else
7003     {
7004       /* Parse the binary expressions (logical-or-expression).  */
7005       expr = cp_parser_binary_expression (parser, cast_p, false,
7006                                           PREC_NOT_OPERATOR, pidk);
7007       /* If the next token is a `?' then we're actually looking at a
7008          conditional-expression.  */
7009       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7010         return cp_parser_question_colon_clause (parser, expr);
7011       else
7012         {
7013           enum tree_code assignment_operator;
7014
7015           /* If it's an assignment-operator, we're using the second
7016              production.  */
7017           assignment_operator
7018             = cp_parser_assignment_operator_opt (parser);
7019           if (assignment_operator != ERROR_MARK)
7020             {
7021               bool non_constant_p;
7022
7023               /* Parse the right-hand side of the assignment.  */
7024               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7025
7026               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7027                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7028
7029               /* An assignment may not appear in a
7030                  constant-expression.  */
7031               if (cp_parser_non_integral_constant_expression (parser,
7032                                                               NIC_ASSIGNMENT))
7033                 return error_mark_node;
7034               /* Build the assignment expression.  */
7035               expr = build_x_modify_expr (expr,
7036                                           assignment_operator,
7037                                           rhs,
7038                                           tf_warning_or_error);
7039             }
7040         }
7041     }
7042
7043   return expr;
7044 }
7045
7046 /* Parse an (optional) assignment-operator.
7047
7048    assignment-operator: one of
7049      = *= /= %= += -= >>= <<= &= ^= |=
7050
7051    GNU Extension:
7052
7053    assignment-operator: one of
7054      <?= >?=
7055
7056    If the next token is an assignment operator, the corresponding tree
7057    code is returned, and the token is consumed.  For example, for
7058    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7059    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7060    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7061    operator, ERROR_MARK is returned.  */
7062
7063 static enum tree_code
7064 cp_parser_assignment_operator_opt (cp_parser* parser)
7065 {
7066   enum tree_code op;
7067   cp_token *token;
7068
7069   /* Peek at the next token.  */
7070   token = cp_lexer_peek_token (parser->lexer);
7071
7072   switch (token->type)
7073     {
7074     case CPP_EQ:
7075       op = NOP_EXPR;
7076       break;
7077
7078     case CPP_MULT_EQ:
7079       op = MULT_EXPR;
7080       break;
7081
7082     case CPP_DIV_EQ:
7083       op = TRUNC_DIV_EXPR;
7084       break;
7085
7086     case CPP_MOD_EQ:
7087       op = TRUNC_MOD_EXPR;
7088       break;
7089
7090     case CPP_PLUS_EQ:
7091       op = PLUS_EXPR;
7092       break;
7093
7094     case CPP_MINUS_EQ:
7095       op = MINUS_EXPR;
7096       break;
7097
7098     case CPP_RSHIFT_EQ:
7099       op = RSHIFT_EXPR;
7100       break;
7101
7102     case CPP_LSHIFT_EQ:
7103       op = LSHIFT_EXPR;
7104       break;
7105
7106     case CPP_AND_EQ:
7107       op = BIT_AND_EXPR;
7108       break;
7109
7110     case CPP_XOR_EQ:
7111       op = BIT_XOR_EXPR;
7112       break;
7113
7114     case CPP_OR_EQ:
7115       op = BIT_IOR_EXPR;
7116       break;
7117
7118     default:
7119       /* Nothing else is an assignment operator.  */
7120       op = ERROR_MARK;
7121     }
7122
7123   /* If it was an assignment operator, consume it.  */
7124   if (op != ERROR_MARK)
7125     cp_lexer_consume_token (parser->lexer);
7126
7127   return op;
7128 }
7129
7130 /* Parse an expression.
7131
7132    expression:
7133      assignment-expression
7134      expression , assignment-expression
7135
7136    CAST_P is true if this expression is the target of a cast.
7137
7138    Returns a representation of the expression.  */
7139
7140 static tree
7141 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7142 {
7143   tree expression = NULL_TREE;
7144
7145   while (true)
7146     {
7147       tree assignment_expression;
7148
7149       /* Parse the next assignment-expression.  */
7150       assignment_expression
7151         = cp_parser_assignment_expression (parser, cast_p, pidk);
7152       /* If this is the first assignment-expression, we can just
7153          save it away.  */
7154       if (!expression)
7155         expression = assignment_expression;
7156       else
7157         expression = build_x_compound_expr (expression,
7158                                             assignment_expression,
7159                                             tf_warning_or_error);
7160       /* If the next token is not a comma, then we are done with the
7161          expression.  */
7162       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7163         break;
7164       /* Consume the `,'.  */
7165       cp_lexer_consume_token (parser->lexer);
7166       /* A comma operator cannot appear in a constant-expression.  */
7167       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7168         expression = error_mark_node;
7169     }
7170
7171   return expression;
7172 }
7173
7174 /* Parse a constant-expression.
7175
7176    constant-expression:
7177      conditional-expression
7178
7179   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7180   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7181   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7182   is false, NON_CONSTANT_P should be NULL.  */
7183
7184 static tree
7185 cp_parser_constant_expression (cp_parser* parser,
7186                                bool allow_non_constant_p,
7187                                bool *non_constant_p)
7188 {
7189   bool saved_integral_constant_expression_p;
7190   bool saved_allow_non_integral_constant_expression_p;
7191   bool saved_non_integral_constant_expression_p;
7192   tree expression;
7193
7194   /* It might seem that we could simply parse the
7195      conditional-expression, and then check to see if it were
7196      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7197      one that the compiler can figure out is constant, possibly after
7198      doing some simplifications or optimizations.  The standard has a
7199      precise definition of constant-expression, and we must honor
7200      that, even though it is somewhat more restrictive.
7201
7202      For example:
7203
7204        int i[(2, 3)];
7205
7206      is not a legal declaration, because `(2, 3)' is not a
7207      constant-expression.  The `,' operator is forbidden in a
7208      constant-expression.  However, GCC's constant-folding machinery
7209      will fold this operation to an INTEGER_CST for `3'.  */
7210
7211   /* Save the old settings.  */
7212   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7213   saved_allow_non_integral_constant_expression_p
7214     = parser->allow_non_integral_constant_expression_p;
7215   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7216   /* We are now parsing a constant-expression.  */
7217   parser->integral_constant_expression_p = true;
7218   parser->allow_non_integral_constant_expression_p
7219     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7220   parser->non_integral_constant_expression_p = false;
7221   /* Although the grammar says "conditional-expression", we parse an
7222      "assignment-expression", which also permits "throw-expression"
7223      and the use of assignment operators.  In the case that
7224      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7225      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7226      actually essential that we look for an assignment-expression.
7227      For example, cp_parser_initializer_clauses uses this function to
7228      determine whether a particular assignment-expression is in fact
7229      constant.  */
7230   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7231   /* Restore the old settings.  */
7232   parser->integral_constant_expression_p
7233     = saved_integral_constant_expression_p;
7234   parser->allow_non_integral_constant_expression_p
7235     = saved_allow_non_integral_constant_expression_p;
7236   if (allow_non_constant_p)
7237     *non_constant_p = parser->non_integral_constant_expression_p;
7238   else if (parser->non_integral_constant_expression_p
7239            && cxx_dialect < cxx0x)
7240     expression = error_mark_node;
7241   parser->non_integral_constant_expression_p
7242     = saved_non_integral_constant_expression_p;
7243
7244   return expression;
7245 }
7246
7247 /* Parse __builtin_offsetof.
7248
7249    offsetof-expression:
7250      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7251
7252    offsetof-member-designator:
7253      id-expression
7254      | offsetof-member-designator "." id-expression
7255      | offsetof-member-designator "[" expression "]"
7256      | offsetof-member-designator "->" id-expression  */
7257
7258 static tree
7259 cp_parser_builtin_offsetof (cp_parser *parser)
7260 {
7261   int save_ice_p, save_non_ice_p;
7262   tree type, expr;
7263   cp_id_kind dummy;
7264   cp_token *token;
7265
7266   /* We're about to accept non-integral-constant things, but will
7267      definitely yield an integral constant expression.  Save and
7268      restore these values around our local parsing.  */
7269   save_ice_p = parser->integral_constant_expression_p;
7270   save_non_ice_p = parser->non_integral_constant_expression_p;
7271
7272   /* Consume the "__builtin_offsetof" token.  */
7273   cp_lexer_consume_token (parser->lexer);
7274   /* Consume the opening `('.  */
7275   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7276   /* Parse the type-id.  */
7277   type = cp_parser_type_id (parser);
7278   /* Look for the `,'.  */
7279   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7280   token = cp_lexer_peek_token (parser->lexer);
7281
7282   /* Build the (type *)null that begins the traditional offsetof macro.  */
7283   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7284                             tf_warning_or_error);
7285
7286   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7287   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7288                                                  true, &dummy, token->location);
7289   while (true)
7290     {
7291       token = cp_lexer_peek_token (parser->lexer);
7292       switch (token->type)
7293         {
7294         case CPP_OPEN_SQUARE:
7295           /* offsetof-member-designator "[" expression "]" */
7296           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7297           break;
7298
7299         case CPP_DEREF:
7300           /* offsetof-member-designator "->" identifier */
7301           expr = grok_array_decl (expr, integer_zero_node);
7302           /* FALLTHRU */
7303
7304         case CPP_DOT:
7305           /* offsetof-member-designator "." identifier */
7306           cp_lexer_consume_token (parser->lexer);
7307           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7308                                                          expr, true, &dummy,
7309                                                          token->location);
7310           break;
7311
7312         case CPP_CLOSE_PAREN:
7313           /* Consume the ")" token.  */
7314           cp_lexer_consume_token (parser->lexer);
7315           goto success;
7316
7317         default:
7318           /* Error.  We know the following require will fail, but
7319              that gives the proper error message.  */
7320           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7321           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7322           expr = error_mark_node;
7323           goto failure;
7324         }
7325     }
7326
7327  success:
7328   /* If we're processing a template, we can't finish the semantics yet.
7329      Otherwise we can fold the entire expression now.  */
7330   if (processing_template_decl)
7331     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7332   else
7333     expr = finish_offsetof (expr);
7334
7335  failure:
7336   parser->integral_constant_expression_p = save_ice_p;
7337   parser->non_integral_constant_expression_p = save_non_ice_p;
7338
7339   return expr;
7340 }
7341
7342 /* Parse a trait expression.  */
7343
7344 static tree
7345 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7346 {
7347   cp_trait_kind kind;
7348   tree type1, type2 = NULL_TREE;
7349   bool binary = false;
7350   cp_decl_specifier_seq decl_specs;
7351
7352   switch (keyword)
7353     {
7354     case RID_HAS_NOTHROW_ASSIGN:
7355       kind = CPTK_HAS_NOTHROW_ASSIGN;
7356       break;
7357     case RID_HAS_NOTHROW_CONSTRUCTOR:
7358       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7359       break;
7360     case RID_HAS_NOTHROW_COPY:
7361       kind = CPTK_HAS_NOTHROW_COPY;
7362       break;
7363     case RID_HAS_TRIVIAL_ASSIGN:
7364       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7365       break;
7366     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7367       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7368       break;
7369     case RID_HAS_TRIVIAL_COPY:
7370       kind = CPTK_HAS_TRIVIAL_COPY;
7371       break;
7372     case RID_HAS_TRIVIAL_DESTRUCTOR:
7373       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7374       break;
7375     case RID_HAS_VIRTUAL_DESTRUCTOR:
7376       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7377       break;
7378     case RID_IS_ABSTRACT:
7379       kind = CPTK_IS_ABSTRACT;
7380       break;
7381     case RID_IS_BASE_OF:
7382       kind = CPTK_IS_BASE_OF;
7383       binary = true;
7384       break;
7385     case RID_IS_CLASS:
7386       kind = CPTK_IS_CLASS;
7387       break;
7388     case RID_IS_CONVERTIBLE_TO:
7389       kind = CPTK_IS_CONVERTIBLE_TO;
7390       binary = true;
7391       break;
7392     case RID_IS_EMPTY:
7393       kind = CPTK_IS_EMPTY;
7394       break;
7395     case RID_IS_ENUM:
7396       kind = CPTK_IS_ENUM;
7397       break;
7398     case RID_IS_POD:
7399       kind = CPTK_IS_POD;
7400       break;
7401     case RID_IS_POLYMORPHIC:
7402       kind = CPTK_IS_POLYMORPHIC;
7403       break;
7404     case RID_IS_STD_LAYOUT:
7405       kind = CPTK_IS_STD_LAYOUT;
7406       break;
7407     case RID_IS_TRIVIAL:
7408       kind = CPTK_IS_TRIVIAL;
7409       break;
7410     case RID_IS_UNION:
7411       kind = CPTK_IS_UNION;
7412       break;
7413     case RID_IS_LITERAL_TYPE:
7414       kind = CPTK_IS_LITERAL_TYPE;
7415       break;
7416     default:
7417       gcc_unreachable ();
7418     }
7419
7420   /* Consume the token.  */
7421   cp_lexer_consume_token (parser->lexer);
7422
7423   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7424
7425   type1 = cp_parser_type_id (parser);
7426
7427   if (type1 == error_mark_node)
7428     return error_mark_node;
7429
7430   /* Build a trivial decl-specifier-seq.  */
7431   clear_decl_specs (&decl_specs);
7432   decl_specs.type = type1;
7433
7434   /* Call grokdeclarator to figure out what type this is.  */
7435   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7436                           /*initialized=*/0, /*attrlist=*/NULL);
7437
7438   if (binary)
7439     {
7440       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7441  
7442       type2 = cp_parser_type_id (parser);
7443
7444       if (type2 == error_mark_node)
7445         return error_mark_node;
7446
7447       /* Build a trivial decl-specifier-seq.  */
7448       clear_decl_specs (&decl_specs);
7449       decl_specs.type = type2;
7450
7451       /* Call grokdeclarator to figure out what type this is.  */
7452       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7453                               /*initialized=*/0, /*attrlist=*/NULL);
7454     }
7455
7456   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7457
7458   /* Complete the trait expression, which may mean either processing
7459      the trait expr now or saving it for template instantiation.  */
7460   return finish_trait_expr (kind, type1, type2);
7461 }
7462
7463 /* Lambdas that appear in variable initializer or default argument scope
7464    get that in their mangling, so we need to record it.  We might as well
7465    use the count for function and namespace scopes as well.  */
7466 static GTY(()) tree lambda_scope;
7467 static GTY(()) int lambda_count;
7468 typedef struct GTY(()) tree_int
7469 {
7470   tree t;
7471   int i;
7472 } tree_int;
7473 DEF_VEC_O(tree_int);
7474 DEF_VEC_ALLOC_O(tree_int,gc);
7475 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7476
7477 static void
7478 start_lambda_scope (tree decl)
7479 {
7480   tree_int ti;
7481   gcc_assert (decl);
7482   /* Once we're inside a function, we ignore other scopes and just push
7483      the function again so that popping works properly.  */
7484   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7485     decl = current_function_decl;
7486   ti.t = lambda_scope;
7487   ti.i = lambda_count;
7488   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7489   if (lambda_scope != decl)
7490     {
7491       /* Don't reset the count if we're still in the same function.  */
7492       lambda_scope = decl;
7493       lambda_count = 0;
7494     }
7495 }
7496
7497 static void
7498 record_lambda_scope (tree lambda)
7499 {
7500   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7501   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7502 }
7503
7504 static void
7505 finish_lambda_scope (void)
7506 {
7507   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7508   if (lambda_scope != p->t)
7509     {
7510       lambda_scope = p->t;
7511       lambda_count = p->i;
7512     }
7513   VEC_pop (tree_int, lambda_scope_stack);
7514 }
7515
7516 /* Parse a lambda expression.
7517
7518    lambda-expression:
7519      lambda-introducer lambda-declarator [opt] compound-statement
7520
7521    Returns a representation of the expression.  */
7522
7523 static tree
7524 cp_parser_lambda_expression (cp_parser* parser)
7525 {
7526   tree lambda_expr = build_lambda_expr ();
7527   tree type;
7528
7529   LAMBDA_EXPR_LOCATION (lambda_expr)
7530     = cp_lexer_peek_token (parser->lexer)->location;
7531
7532   if (cp_unevaluated_operand)
7533     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7534               "lambda-expression in unevaluated context");
7535
7536   /* We may be in the middle of deferred access check.  Disable
7537      it now.  */
7538   push_deferring_access_checks (dk_no_deferred);
7539
7540   cp_parser_lambda_introducer (parser, lambda_expr);
7541
7542   type = begin_lambda_type (lambda_expr);
7543
7544   record_lambda_scope (lambda_expr);
7545
7546   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7547   determine_visibility (TYPE_NAME (type));
7548
7549   /* Now that we've started the type, add the capture fields for any
7550      explicit captures.  */
7551   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7552
7553   {
7554     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7555     unsigned int saved_num_template_parameter_lists
7556         = parser->num_template_parameter_lists;
7557
7558     parser->num_template_parameter_lists = 0;
7559
7560     /* By virtue of defining a local class, a lambda expression has access to
7561        the private variables of enclosing classes.  */
7562
7563     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7564
7565     cp_parser_lambda_body (parser, lambda_expr);
7566
7567     /* The capture list was built up in reverse order; fix that now.  */
7568     {
7569       tree newlist = NULL_TREE;
7570       tree elt, next;
7571
7572       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7573            elt; elt = next)
7574         {
7575           tree field = TREE_PURPOSE (elt);
7576           char *buf;
7577
7578           next = TREE_CHAIN (elt);
7579           TREE_CHAIN (elt) = newlist;
7580           newlist = elt;
7581
7582           /* Also add __ to the beginning of the field name so that code
7583              outside the lambda body can't see the captured name.  We could
7584              just remove the name entirely, but this is more useful for
7585              debugging.  */
7586           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7587             /* The 'this' capture already starts with __.  */
7588             continue;
7589
7590           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7591           buf[1] = buf[0] = '_';
7592           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7593                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7594           DECL_NAME (field) = get_identifier (buf);
7595         }
7596       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7597     }
7598
7599     maybe_add_lambda_conv_op (type);
7600
7601     type = finish_struct (type, /*attributes=*/NULL_TREE);
7602
7603     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7604   }
7605
7606   pop_deferring_access_checks ();
7607
7608   return build_lambda_object (lambda_expr);
7609 }
7610
7611 /* Parse the beginning of a lambda expression.
7612
7613    lambda-introducer:
7614      [ lambda-capture [opt] ]
7615
7616    LAMBDA_EXPR is the current representation of the lambda expression.  */
7617
7618 static void
7619 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7620 {
7621   /* Need commas after the first capture.  */
7622   bool first = true;
7623
7624   /* Eat the leading `['.  */
7625   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7626
7627   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7628   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7629       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7630     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7631   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7632     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7633
7634   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7635     {
7636       cp_lexer_consume_token (parser->lexer);
7637       first = false;
7638     }
7639
7640   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7641     {
7642       cp_token* capture_token;
7643       tree capture_id;
7644       tree capture_init_expr;
7645       cp_id_kind idk = CP_ID_KIND_NONE;
7646       bool explicit_init_p = false;
7647
7648       enum capture_kind_type
7649       {
7650         BY_COPY,
7651         BY_REFERENCE
7652       };
7653       enum capture_kind_type capture_kind = BY_COPY;
7654
7655       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7656         {
7657           error ("expected end of capture-list");
7658           return;
7659         }
7660
7661       if (first)
7662         first = false;
7663       else
7664         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7665
7666       /* Possibly capture `this'.  */
7667       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7668         {
7669           cp_lexer_consume_token (parser->lexer);
7670           add_capture (lambda_expr,
7671                        /*id=*/get_identifier ("__this"),
7672                        /*initializer=*/finish_this_expr(),
7673                        /*by_reference_p=*/false,
7674                        explicit_init_p);
7675           continue;
7676         }
7677
7678       /* Remember whether we want to capture as a reference or not.  */
7679       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7680         {
7681           capture_kind = BY_REFERENCE;
7682           cp_lexer_consume_token (parser->lexer);
7683         }
7684
7685       /* Get the identifier.  */
7686       capture_token = cp_lexer_peek_token (parser->lexer);
7687       capture_id = cp_parser_identifier (parser);
7688
7689       if (capture_id == error_mark_node)
7690         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7691            delimiters, but I modified this to stop on unnested ']' as well.  It
7692            was already changed to stop on unnested '}', so the
7693            "closing_parenthesis" name is no more misleading with my change.  */
7694         {
7695           cp_parser_skip_to_closing_parenthesis (parser,
7696                                                  /*recovering=*/true,
7697                                                  /*or_comma=*/true,
7698                                                  /*consume_paren=*/true);
7699           break;
7700         }
7701
7702       /* Find the initializer for this capture.  */
7703       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7704         {
7705           /* An explicit expression exists.  */
7706           cp_lexer_consume_token (parser->lexer);
7707           pedwarn (input_location, OPT_pedantic,
7708                    "ISO C++ does not allow initializers "
7709                    "in lambda expression capture lists");
7710           capture_init_expr = cp_parser_assignment_expression (parser,
7711                                                                /*cast_p=*/true,
7712                                                                &idk);
7713           explicit_init_p = true;
7714         }
7715       else
7716         {
7717           const char* error_msg;
7718
7719           /* Turn the identifier into an id-expression.  */
7720           capture_init_expr
7721             = cp_parser_lookup_name
7722                 (parser,
7723                  capture_id,
7724                  none_type,
7725                  /*is_template=*/false,
7726                  /*is_namespace=*/false,
7727                  /*check_dependency=*/true,
7728                  /*ambiguous_decls=*/NULL,
7729                  capture_token->location);
7730
7731           capture_init_expr
7732             = finish_id_expression
7733                 (capture_id,
7734                  capture_init_expr,
7735                  parser->scope,
7736                  &idk,
7737                  /*integral_constant_expression_p=*/false,
7738                  /*allow_non_integral_constant_expression_p=*/false,
7739                  /*non_integral_constant_expression_p=*/NULL,
7740                  /*template_p=*/false,
7741                  /*done=*/true,
7742                  /*address_p=*/false,
7743                  /*template_arg_p=*/false,
7744                  &error_msg,
7745                  capture_token->location);
7746         }
7747
7748       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7749         capture_init_expr
7750           = unqualified_name_lookup_error (capture_init_expr);
7751
7752       add_capture (lambda_expr,
7753                    capture_id,
7754                    capture_init_expr,
7755                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7756                    explicit_init_p);
7757     }
7758
7759   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7760 }
7761
7762 /* Parse the (optional) middle of a lambda expression.
7763
7764    lambda-declarator:
7765      ( parameter-declaration-clause [opt] )
7766        attribute-specifier [opt]
7767        mutable [opt]
7768        exception-specification [opt]
7769        lambda-return-type-clause [opt]
7770
7771    LAMBDA_EXPR is the current representation of the lambda expression.  */
7772
7773 static void
7774 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7775 {
7776   /* 5.1.1.4 of the standard says:
7777        If a lambda-expression does not include a lambda-declarator, it is as if
7778        the lambda-declarator were ().
7779      This means an empty parameter list, no attributes, and no exception
7780      specification.  */
7781   tree param_list = void_list_node;
7782   tree attributes = NULL_TREE;
7783   tree exception_spec = NULL_TREE;
7784   tree t;
7785
7786   /* The lambda-declarator is optional, but must begin with an opening
7787      parenthesis if present.  */
7788   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7789     {
7790       cp_lexer_consume_token (parser->lexer);
7791
7792       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7793
7794       /* Parse parameters.  */
7795       param_list = cp_parser_parameter_declaration_clause (parser);
7796
7797       /* Default arguments shall not be specified in the
7798          parameter-declaration-clause of a lambda-declarator.  */
7799       for (t = param_list; t; t = TREE_CHAIN (t))
7800         if (TREE_PURPOSE (t))
7801           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7802                    "default argument specified for lambda parameter");
7803
7804       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7805
7806       attributes = cp_parser_attributes_opt (parser);
7807
7808       /* Parse optional `mutable' keyword.  */
7809       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7810         {
7811           cp_lexer_consume_token (parser->lexer);
7812           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7813         }
7814
7815       /* Parse optional exception specification.  */
7816       exception_spec = cp_parser_exception_specification_opt (parser);
7817
7818       /* Parse optional trailing return type.  */
7819       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7820         {
7821           cp_lexer_consume_token (parser->lexer);
7822           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7823         }
7824
7825       /* The function parameters must be in scope all the way until after the
7826          trailing-return-type in case of decltype.  */
7827       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7828         pop_binding (DECL_NAME (t), t);
7829
7830       leave_scope ();
7831     }
7832
7833   /* Create the function call operator.
7834
7835      Messing with declarators like this is no uglier than building up the
7836      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7837      other code.  */
7838   {
7839     cp_decl_specifier_seq return_type_specs;
7840     cp_declarator* declarator;
7841     tree fco;
7842     int quals;
7843     void *p;
7844
7845     clear_decl_specs (&return_type_specs);
7846     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7847       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7848     else
7849       /* Maybe we will deduce the return type later, but we can use void
7850          as a placeholder return type anyways.  */
7851       return_type_specs.type = void_type_node;
7852
7853     p = obstack_alloc (&declarator_obstack, 0);
7854
7855     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7856                                      sfk_none);
7857
7858     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7859              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7860     declarator = make_call_declarator (declarator, param_list, quals,
7861                                        exception_spec,
7862                                        /*late_return_type=*/NULL_TREE);
7863     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7864
7865     fco = grokmethod (&return_type_specs,
7866                       declarator,
7867                       attributes);
7868     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7869     DECL_ARTIFICIAL (fco) = 1;
7870
7871     finish_member_declaration (fco);
7872
7873     obstack_free (&declarator_obstack, p);
7874   }
7875 }
7876
7877 /* Parse the body of a lambda expression, which is simply
7878
7879    compound-statement
7880
7881    but which requires special handling.
7882    LAMBDA_EXPR is the current representation of the lambda expression.  */
7883
7884 static void
7885 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7886 {
7887   bool nested = (current_function_decl != NULL_TREE);
7888   if (nested)
7889     push_function_context ();
7890
7891   /* Finish the function call operator
7892      - class_specifier
7893      + late_parsing_for_member
7894      + function_definition_after_declarator
7895      + ctor_initializer_opt_and_function_body  */
7896   {
7897     tree fco = lambda_function (lambda_expr);
7898     tree body;
7899     bool done = false;
7900
7901     /* Let the front end know that we are going to be defining this
7902        function.  */
7903     start_preparsed_function (fco,
7904                               NULL_TREE,
7905                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7906
7907     start_lambda_scope (fco);
7908     body = begin_function_body ();
7909
7910     /* 5.1.1.4 of the standard says:
7911          If a lambda-expression does not include a trailing-return-type, it
7912          is as if the trailing-return-type denotes the following type:
7913           * if the compound-statement is of the form
7914                { return attribute-specifier [opt] expression ; }
7915              the type of the returned expression after lvalue-to-rvalue
7916              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7917              (_conv.array_ 4.2), and function-to-pointer conversion
7918              (_conv.func_ 4.3);
7919           * otherwise, void.  */
7920
7921     /* In a lambda that has neither a lambda-return-type-clause
7922        nor a deducible form, errors should be reported for return statements
7923        in the body.  Since we used void as the placeholder return type, parsing
7924        the body as usual will give such desired behavior.  */
7925     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7926         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7927         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7928         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7929       {
7930         tree compound_stmt;
7931         tree expr = NULL_TREE;
7932         cp_id_kind idk = CP_ID_KIND_NONE;
7933
7934         /* Parse tentatively in case there's more after the initial return
7935            statement.  */
7936         cp_parser_parse_tentatively (parser);
7937
7938         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7939         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7940
7941         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7942
7943         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7944         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7945
7946         if (cp_parser_parse_definitely (parser))
7947           {
7948             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7949
7950             compound_stmt = begin_compound_stmt (0);
7951             /* Will get error here if type not deduced yet.  */
7952             finish_return_stmt (expr);
7953             finish_compound_stmt (compound_stmt);
7954
7955             done = true;
7956           }
7957       }
7958
7959     if (!done)
7960       {
7961         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7962           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7963         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7964            cp_parser_compound_stmt does not pass it.  */
7965         cp_parser_function_body (parser);
7966         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7967       }
7968
7969     finish_function_body (body);
7970     finish_lambda_scope ();
7971
7972     /* Finish the function and generate code for it if necessary.  */
7973     expand_or_defer_fn (finish_function (/*inline*/2));
7974   }
7975
7976   if (nested)
7977     pop_function_context();
7978 }
7979
7980 /* Statements [gram.stmt.stmt]  */
7981
7982 /* Parse a statement.
7983
7984    statement:
7985      labeled-statement
7986      expression-statement
7987      compound-statement
7988      selection-statement
7989      iteration-statement
7990      jump-statement
7991      declaration-statement
7992      try-block
7993
7994   IN_COMPOUND is true when the statement is nested inside a
7995   cp_parser_compound_statement; this matters for certain pragmas.
7996
7997   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7998   is a (possibly labeled) if statement which is not enclosed in braces
7999   and has an else clause.  This is used to implement -Wparentheses.  */
8000
8001 static void
8002 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8003                      bool in_compound, bool *if_p)
8004 {
8005   tree statement;
8006   cp_token *token;
8007   location_t statement_location;
8008
8009  restart:
8010   if (if_p != NULL)
8011     *if_p = false;
8012   /* There is no statement yet.  */
8013   statement = NULL_TREE;
8014   /* Peek at the next token.  */
8015   token = cp_lexer_peek_token (parser->lexer);
8016   /* Remember the location of the first token in the statement.  */
8017   statement_location = token->location;
8018   /* If this is a keyword, then that will often determine what kind of
8019      statement we have.  */
8020   if (token->type == CPP_KEYWORD)
8021     {
8022       enum rid keyword = token->keyword;
8023
8024       switch (keyword)
8025         {
8026         case RID_CASE:
8027         case RID_DEFAULT:
8028           /* Looks like a labeled-statement with a case label.
8029              Parse the label, and then use tail recursion to parse
8030              the statement.  */
8031           cp_parser_label_for_labeled_statement (parser);
8032           goto restart;
8033
8034         case RID_IF:
8035         case RID_SWITCH:
8036           statement = cp_parser_selection_statement (parser, if_p);
8037           break;
8038
8039         case RID_WHILE:
8040         case RID_DO:
8041         case RID_FOR:
8042           statement = cp_parser_iteration_statement (parser);
8043           break;
8044
8045         case RID_BREAK:
8046         case RID_CONTINUE:
8047         case RID_RETURN:
8048         case RID_GOTO:
8049           statement = cp_parser_jump_statement (parser);
8050           break;
8051
8052           /* Objective-C++ exception-handling constructs.  */
8053         case RID_AT_TRY:
8054         case RID_AT_CATCH:
8055         case RID_AT_FINALLY:
8056         case RID_AT_SYNCHRONIZED:
8057         case RID_AT_THROW:
8058           statement = cp_parser_objc_statement (parser);
8059           break;
8060
8061         case RID_TRY:
8062           statement = cp_parser_try_block (parser);
8063           break;
8064
8065         case RID_NAMESPACE:
8066           /* This must be a namespace alias definition.  */
8067           cp_parser_declaration_statement (parser);
8068           return;
8069           
8070         default:
8071           /* It might be a keyword like `int' that can start a
8072              declaration-statement.  */
8073           break;
8074         }
8075     }
8076   else if (token->type == CPP_NAME)
8077     {
8078       /* If the next token is a `:', then we are looking at a
8079          labeled-statement.  */
8080       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8081       if (token->type == CPP_COLON)
8082         {
8083           /* Looks like a labeled-statement with an ordinary label.
8084              Parse the label, and then use tail recursion to parse
8085              the statement.  */
8086           cp_parser_label_for_labeled_statement (parser);
8087           goto restart;
8088         }
8089     }
8090   /* Anything that starts with a `{' must be a compound-statement.  */
8091   else if (token->type == CPP_OPEN_BRACE)
8092     statement = cp_parser_compound_statement (parser, NULL, false);
8093   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8094      a statement all its own.  */
8095   else if (token->type == CPP_PRAGMA)
8096     {
8097       /* Only certain OpenMP pragmas are attached to statements, and thus
8098          are considered statements themselves.  All others are not.  In
8099          the context of a compound, accept the pragma as a "statement" and
8100          return so that we can check for a close brace.  Otherwise we
8101          require a real statement and must go back and read one.  */
8102       if (in_compound)
8103         cp_parser_pragma (parser, pragma_compound);
8104       else if (!cp_parser_pragma (parser, pragma_stmt))
8105         goto restart;
8106       return;
8107     }
8108   else if (token->type == CPP_EOF)
8109     {
8110       cp_parser_error (parser, "expected statement");
8111       return;
8112     }
8113
8114   /* Everything else must be a declaration-statement or an
8115      expression-statement.  Try for the declaration-statement
8116      first, unless we are looking at a `;', in which case we know that
8117      we have an expression-statement.  */
8118   if (!statement)
8119     {
8120       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8121         {
8122           cp_parser_parse_tentatively (parser);
8123           /* Try to parse the declaration-statement.  */
8124           cp_parser_declaration_statement (parser);
8125           /* If that worked, we're done.  */
8126           if (cp_parser_parse_definitely (parser))
8127             return;
8128         }
8129       /* Look for an expression-statement instead.  */
8130       statement = cp_parser_expression_statement (parser, in_statement_expr);
8131     }
8132
8133   /* Set the line number for the statement.  */
8134   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8135     SET_EXPR_LOCATION (statement, statement_location);
8136 }
8137
8138 /* Parse the label for a labeled-statement, i.e.
8139
8140    identifier :
8141    case constant-expression :
8142    default :
8143
8144    GNU Extension:
8145    case constant-expression ... constant-expression : statement
8146
8147    When a label is parsed without errors, the label is added to the
8148    parse tree by the finish_* functions, so this function doesn't
8149    have to return the label.  */
8150
8151 static void
8152 cp_parser_label_for_labeled_statement (cp_parser* parser)
8153 {
8154   cp_token *token;
8155   tree label = NULL_TREE;
8156
8157   /* The next token should be an identifier.  */
8158   token = cp_lexer_peek_token (parser->lexer);
8159   if (token->type != CPP_NAME
8160       && token->type != CPP_KEYWORD)
8161     {
8162       cp_parser_error (parser, "expected labeled-statement");
8163       return;
8164     }
8165
8166   switch (token->keyword)
8167     {
8168     case RID_CASE:
8169       {
8170         tree expr, expr_hi;
8171         cp_token *ellipsis;
8172
8173         /* Consume the `case' token.  */
8174         cp_lexer_consume_token (parser->lexer);
8175         /* Parse the constant-expression.  */
8176         expr = cp_parser_constant_expression (parser,
8177                                               /*allow_non_constant_p=*/false,
8178                                               NULL);
8179
8180         ellipsis = cp_lexer_peek_token (parser->lexer);
8181         if (ellipsis->type == CPP_ELLIPSIS)
8182           {
8183             /* Consume the `...' token.  */
8184             cp_lexer_consume_token (parser->lexer);
8185             expr_hi =
8186               cp_parser_constant_expression (parser,
8187                                              /*allow_non_constant_p=*/false,
8188                                              NULL);
8189             /* We don't need to emit warnings here, as the common code
8190                will do this for us.  */
8191           }
8192         else
8193           expr_hi = NULL_TREE;
8194
8195         if (parser->in_switch_statement_p)
8196           finish_case_label (token->location, expr, expr_hi);
8197         else
8198           error_at (token->location,
8199                     "case label %qE not within a switch statement",
8200                     expr);
8201       }
8202       break;
8203
8204     case RID_DEFAULT:
8205       /* Consume the `default' token.  */
8206       cp_lexer_consume_token (parser->lexer);
8207
8208       if (parser->in_switch_statement_p)
8209         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8210       else
8211         error_at (token->location, "case label not within a switch statement");
8212       break;
8213
8214     default:
8215       /* Anything else must be an ordinary label.  */
8216       label = finish_label_stmt (cp_parser_identifier (parser));
8217       break;
8218     }
8219
8220   /* Require the `:' token.  */
8221   cp_parser_require (parser, CPP_COLON, RT_COLON);
8222
8223   /* An ordinary label may optionally be followed by attributes.
8224      However, this is only permitted if the attributes are then
8225      followed by a semicolon.  This is because, for backward
8226      compatibility, when parsing
8227        lab: __attribute__ ((unused)) int i;
8228      we want the attribute to attach to "i", not "lab".  */
8229   if (label != NULL_TREE
8230       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8231     {
8232       tree attrs;
8233
8234       cp_parser_parse_tentatively (parser);
8235       attrs = cp_parser_attributes_opt (parser);
8236       if (attrs == NULL_TREE
8237           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8238         cp_parser_abort_tentative_parse (parser);
8239       else if (!cp_parser_parse_definitely (parser))
8240         ;
8241       else
8242         cplus_decl_attributes (&label, attrs, 0);
8243     }
8244 }
8245
8246 /* Parse an expression-statement.
8247
8248    expression-statement:
8249      expression [opt] ;
8250
8251    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8252    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8253    indicates whether this expression-statement is part of an
8254    expression statement.  */
8255
8256 static tree
8257 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8258 {
8259   tree statement = NULL_TREE;
8260   cp_token *token = cp_lexer_peek_token (parser->lexer);
8261
8262   /* If the next token is a ';', then there is no expression
8263      statement.  */
8264   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8265     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8266
8267   /* Give a helpful message for "A<T>::type t;" and the like.  */
8268   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8269       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8270     {
8271       if (TREE_CODE (statement) == SCOPE_REF)
8272         error_at (token->location, "need %<typename%> before %qE because "
8273                   "%qT is a dependent scope",
8274                   statement, TREE_OPERAND (statement, 0));
8275       else if (is_overloaded_fn (statement)
8276                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8277         {
8278           /* A::A a; */
8279           tree fn = get_first_fn (statement);
8280           error_at (token->location,
8281                     "%<%T::%D%> names the constructor, not the type",
8282                     DECL_CONTEXT (fn), DECL_NAME (fn));
8283         }
8284     }
8285
8286   /* Consume the final `;'.  */
8287   cp_parser_consume_semicolon_at_end_of_statement (parser);
8288
8289   if (in_statement_expr
8290       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8291     /* This is the final expression statement of a statement
8292        expression.  */
8293     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8294   else if (statement)
8295     statement = finish_expr_stmt (statement);
8296   else
8297     finish_stmt ();
8298
8299   return statement;
8300 }
8301
8302 /* Parse a compound-statement.
8303
8304    compound-statement:
8305      { statement-seq [opt] }
8306
8307    GNU extension:
8308
8309    compound-statement:
8310      { label-declaration-seq [opt] statement-seq [opt] }
8311
8312    label-declaration-seq:
8313      label-declaration
8314      label-declaration-seq label-declaration
8315
8316    Returns a tree representing the statement.  */
8317
8318 static tree
8319 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8320                               bool in_try)
8321 {
8322   tree compound_stmt;
8323
8324   /* Consume the `{'.  */
8325   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8326     return error_mark_node;
8327   /* Begin the compound-statement.  */
8328   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8329   /* If the next keyword is `__label__' we have a label declaration.  */
8330   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8331     cp_parser_label_declaration (parser);
8332   /* Parse an (optional) statement-seq.  */
8333   cp_parser_statement_seq_opt (parser, in_statement_expr);
8334   /* Finish the compound-statement.  */
8335   finish_compound_stmt (compound_stmt);
8336   /* Consume the `}'.  */
8337   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8338
8339   return compound_stmt;
8340 }
8341
8342 /* Parse an (optional) statement-seq.
8343
8344    statement-seq:
8345      statement
8346      statement-seq [opt] statement  */
8347
8348 static void
8349 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8350 {
8351   /* Scan statements until there aren't any more.  */
8352   while (true)
8353     {
8354       cp_token *token = cp_lexer_peek_token (parser->lexer);
8355
8356       /* If we are looking at a `}', then we have run out of
8357          statements; the same is true if we have reached the end
8358          of file, or have stumbled upon a stray '@end'.  */
8359       if (token->type == CPP_CLOSE_BRACE
8360           || token->type == CPP_EOF
8361           || token->type == CPP_PRAGMA_EOL
8362           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8363         break;
8364       
8365       /* If we are in a compound statement and find 'else' then
8366          something went wrong.  */
8367       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8368         {
8369           if (parser->in_statement & IN_IF_STMT) 
8370             break;
8371           else
8372             {
8373               token = cp_lexer_consume_token (parser->lexer);
8374               error_at (token->location, "%<else%> without a previous %<if%>");
8375             }
8376         }
8377
8378       /* Parse the statement.  */
8379       cp_parser_statement (parser, in_statement_expr, true, NULL);
8380     }
8381 }
8382
8383 /* Parse a selection-statement.
8384
8385    selection-statement:
8386      if ( condition ) statement
8387      if ( condition ) statement else statement
8388      switch ( condition ) statement
8389
8390    Returns the new IF_STMT or SWITCH_STMT.
8391
8392    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8393    is a (possibly labeled) if statement which is not enclosed in
8394    braces and has an else clause.  This is used to implement
8395    -Wparentheses.  */
8396
8397 static tree
8398 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8399 {
8400   cp_token *token;
8401   enum rid keyword;
8402
8403   if (if_p != NULL)
8404     *if_p = false;
8405
8406   /* Peek at the next token.  */
8407   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8408
8409   /* See what kind of keyword it is.  */
8410   keyword = token->keyword;
8411   switch (keyword)
8412     {
8413     case RID_IF:
8414     case RID_SWITCH:
8415       {
8416         tree statement;
8417         tree condition;
8418
8419         /* Look for the `('.  */
8420         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8421           {
8422             cp_parser_skip_to_end_of_statement (parser);
8423             return error_mark_node;
8424           }
8425
8426         /* Begin the selection-statement.  */
8427         if (keyword == RID_IF)
8428           statement = begin_if_stmt ();
8429         else
8430           statement = begin_switch_stmt ();
8431
8432         /* Parse the condition.  */
8433         condition = cp_parser_condition (parser);
8434         /* Look for the `)'.  */
8435         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8436           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8437                                                  /*consume_paren=*/true);
8438
8439         if (keyword == RID_IF)
8440           {
8441             bool nested_if;
8442             unsigned char in_statement;
8443
8444             /* Add the condition.  */
8445             finish_if_stmt_cond (condition, statement);
8446
8447             /* Parse the then-clause.  */
8448             in_statement = parser->in_statement;
8449             parser->in_statement |= IN_IF_STMT;
8450             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8451               {
8452                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8453                 add_stmt (build_empty_stmt (loc));
8454                 cp_lexer_consume_token (parser->lexer);
8455                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8456                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8457                               "empty body in an %<if%> statement");
8458                 nested_if = false;
8459               }
8460             else
8461               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8462             parser->in_statement = in_statement;
8463
8464             finish_then_clause (statement);
8465
8466             /* If the next token is `else', parse the else-clause.  */
8467             if (cp_lexer_next_token_is_keyword (parser->lexer,
8468                                                 RID_ELSE))
8469               {
8470                 /* Consume the `else' keyword.  */
8471                 cp_lexer_consume_token (parser->lexer);
8472                 begin_else_clause (statement);
8473                 /* Parse the else-clause.  */
8474                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8475                   {
8476                     location_t loc;
8477                     loc = cp_lexer_peek_token (parser->lexer)->location;
8478                     warning_at (loc,
8479                                 OPT_Wempty_body, "suggest braces around "
8480                                 "empty body in an %<else%> statement");
8481                     add_stmt (build_empty_stmt (loc));
8482                     cp_lexer_consume_token (parser->lexer);
8483                   }
8484                 else
8485                   cp_parser_implicitly_scoped_statement (parser, NULL);
8486
8487                 finish_else_clause (statement);
8488
8489                 /* If we are currently parsing a then-clause, then
8490                    IF_P will not be NULL.  We set it to true to
8491                    indicate that this if statement has an else clause.
8492                    This may trigger the Wparentheses warning below
8493                    when we get back up to the parent if statement.  */
8494                 if (if_p != NULL)
8495                   *if_p = true;
8496               }
8497             else
8498               {
8499                 /* This if statement does not have an else clause.  If
8500                    NESTED_IF is true, then the then-clause is an if
8501                    statement which does have an else clause.  We warn
8502                    about the potential ambiguity.  */
8503                 if (nested_if)
8504                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8505                               "suggest explicit braces to avoid ambiguous"
8506                               " %<else%>");
8507               }
8508
8509             /* Now we're all done with the if-statement.  */
8510             finish_if_stmt (statement);
8511           }
8512         else
8513           {
8514             bool in_switch_statement_p;
8515             unsigned char in_statement;
8516
8517             /* Add the condition.  */
8518             finish_switch_cond (condition, statement);
8519
8520             /* Parse the body of the switch-statement.  */
8521             in_switch_statement_p = parser->in_switch_statement_p;
8522             in_statement = parser->in_statement;
8523             parser->in_switch_statement_p = true;
8524             parser->in_statement |= IN_SWITCH_STMT;
8525             cp_parser_implicitly_scoped_statement (parser, NULL);
8526             parser->in_switch_statement_p = in_switch_statement_p;
8527             parser->in_statement = in_statement;
8528
8529             /* Now we're all done with the switch-statement.  */
8530             finish_switch_stmt (statement);
8531           }
8532
8533         return statement;
8534       }
8535       break;
8536
8537     default:
8538       cp_parser_error (parser, "expected selection-statement");
8539       return error_mark_node;
8540     }
8541 }
8542
8543 /* Parse a condition.
8544
8545    condition:
8546      expression
8547      type-specifier-seq declarator = initializer-clause
8548      type-specifier-seq declarator braced-init-list
8549
8550    GNU Extension:
8551
8552    condition:
8553      type-specifier-seq declarator asm-specification [opt]
8554        attributes [opt] = assignment-expression
8555
8556    Returns the expression that should be tested.  */
8557
8558 static tree
8559 cp_parser_condition (cp_parser* parser)
8560 {
8561   cp_decl_specifier_seq type_specifiers;
8562   const char *saved_message;
8563   int declares_class_or_enum;
8564
8565   /* Try the declaration first.  */
8566   cp_parser_parse_tentatively (parser);
8567   /* New types are not allowed in the type-specifier-seq for a
8568      condition.  */
8569   saved_message = parser->type_definition_forbidden_message;
8570   parser->type_definition_forbidden_message
8571     = G_("types may not be defined in conditions");
8572   /* Parse the type-specifier-seq.  */
8573   cp_parser_decl_specifier_seq (parser,
8574                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8575                                 &type_specifiers,
8576                                 &declares_class_or_enum);
8577   /* Restore the saved message.  */
8578   parser->type_definition_forbidden_message = saved_message;
8579   /* If all is well, we might be looking at a declaration.  */
8580   if (!cp_parser_error_occurred (parser))
8581     {
8582       tree decl;
8583       tree asm_specification;
8584       tree attributes;
8585       cp_declarator *declarator;
8586       tree initializer = NULL_TREE;
8587
8588       /* Parse the declarator.  */
8589       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8590                                          /*ctor_dtor_or_conv_p=*/NULL,
8591                                          /*parenthesized_p=*/NULL,
8592                                          /*member_p=*/false);
8593       /* Parse the attributes.  */
8594       attributes = cp_parser_attributes_opt (parser);
8595       /* Parse the asm-specification.  */
8596       asm_specification = cp_parser_asm_specification_opt (parser);
8597       /* If the next token is not an `=' or '{', then we might still be
8598          looking at an expression.  For example:
8599
8600            if (A(a).x)
8601
8602          looks like a decl-specifier-seq and a declarator -- but then
8603          there is no `=', so this is an expression.  */
8604       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8605           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8606         cp_parser_simulate_error (parser);
8607         
8608       /* If we did see an `=' or '{', then we are looking at a declaration
8609          for sure.  */
8610       if (cp_parser_parse_definitely (parser))
8611         {
8612           tree pushed_scope;
8613           bool non_constant_p;
8614           bool flags = LOOKUP_ONLYCONVERTING;
8615
8616           /* Create the declaration.  */
8617           decl = start_decl (declarator, &type_specifiers,
8618                              /*initialized_p=*/true,
8619                              attributes, /*prefix_attributes=*/NULL_TREE,
8620                              &pushed_scope);
8621
8622           /* Parse the initializer.  */
8623           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8624             {
8625               initializer = cp_parser_braced_list (parser, &non_constant_p);
8626               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8627               flags = 0;
8628             }
8629           else
8630             {
8631               /* Consume the `='.  */
8632               cp_parser_require (parser, CPP_EQ, RT_EQ);
8633               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8634             }
8635           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8636             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8637
8638           if (!non_constant_p)
8639             initializer = fold_non_dependent_expr (initializer);
8640
8641           /* Process the initializer.  */
8642           cp_finish_decl (decl,
8643                           initializer, !non_constant_p,
8644                           asm_specification,
8645                           flags);
8646
8647           if (pushed_scope)
8648             pop_scope (pushed_scope);
8649
8650           return convert_from_reference (decl);
8651         }
8652     }
8653   /* If we didn't even get past the declarator successfully, we are
8654      definitely not looking at a declaration.  */
8655   else
8656     cp_parser_abort_tentative_parse (parser);
8657
8658   /* Otherwise, we are looking at an expression.  */
8659   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8660 }
8661
8662 /* Parses a traditional for-statement until the closing ')', not included. */
8663
8664 static tree
8665 cp_parser_c_for (cp_parser *parser)
8666 {
8667   /* Normal for loop */
8668   tree stmt;
8669   tree condition = NULL_TREE;
8670   tree expression = NULL_TREE;
8671
8672   /* Begin the for-statement.  */
8673   stmt = begin_for_stmt ();
8674
8675   /* Parse the initialization.  */
8676   cp_parser_for_init_statement (parser);
8677   finish_for_init_stmt (stmt);
8678
8679   /* If there's a condition, process it.  */
8680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8681     condition = cp_parser_condition (parser);
8682   finish_for_cond (condition, stmt);
8683   /* Look for the `;'.  */
8684   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8685
8686   /* If there's an expression, process it.  */
8687   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8688     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8689   finish_for_expr (expression, stmt);
8690
8691   return stmt;
8692 }
8693
8694 /* Tries to parse a range-based for-statement:
8695
8696   range-based-for:
8697     type-specifier-seq declarator : expression
8698
8699   If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8700   expression. Note that the *DECL is returned unfinished, so
8701   later you should call cp_finish_decl().
8702
8703   Returns TRUE iff a range-based for is parsed. */
8704
8705 static tree
8706 cp_parser_range_for (cp_parser *parser)
8707 {
8708   tree stmt, range_decl, range_expr;
8709   cp_decl_specifier_seq type_specifiers;
8710   cp_declarator *declarator;
8711   const char *saved_message;
8712   tree attributes, pushed_scope;
8713
8714   cp_parser_parse_tentatively (parser);
8715   /* New types are not allowed in the type-specifier-seq for a
8716      range-based for loop.  */
8717   saved_message = parser->type_definition_forbidden_message;
8718   parser->type_definition_forbidden_message
8719     = G_("types may not be defined in range-based for loops");
8720   /* Parse the type-specifier-seq.  */
8721   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8722                                 /*is_trailing_return=*/false,
8723                                 &type_specifiers);
8724   /* Restore the saved message.  */
8725   parser->type_definition_forbidden_message = saved_message;
8726   /* If all is well, we might be looking at a declaration.  */
8727   if (cp_parser_error_occurred (parser))
8728     {
8729       cp_parser_abort_tentative_parse (parser);
8730       return NULL_TREE;
8731     }
8732   /* Parse the declarator.  */
8733   declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8734                                      /*ctor_dtor_or_conv_p=*/NULL,
8735                                      /*parenthesized_p=*/NULL,
8736                                      /*member_p=*/false);
8737   /* Parse the attributes.  */
8738   attributes = cp_parser_attributes_opt (parser);
8739   /* The next token should be `:'. */
8740   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8741     cp_parser_simulate_error (parser);
8742
8743   /* Check if it is a range-based for */
8744   if (!cp_parser_parse_definitely (parser))
8745     return NULL_TREE;
8746
8747   cp_parser_require (parser, CPP_COLON, RT_COLON);
8748   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8749     {
8750       bool expr_non_constant_p;
8751       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8752     }
8753   else
8754     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8755
8756   /* If in template, STMT is converted to a normal for-statements
8757      at instantiation. If not, it is done just ahead. */
8758   if (processing_template_decl)
8759     stmt = begin_range_for_stmt ();
8760   else
8761     stmt = begin_for_stmt ();
8762
8763   /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8764   range_decl = start_decl (declarator, &type_specifiers,
8765                            /*initialized_p=*/SD_INITIALIZED,
8766                            attributes, /*prefix_attributes=*/NULL_TREE,
8767                            &pushed_scope);
8768   /* No scope allowed here */
8769   pop_scope (pushed_scope);
8770
8771   if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8772     finish_range_for_decl (stmt, range_decl, range_expr);
8773   else
8774     /* Convert the range-based for loop into a normal for-statement. */
8775     stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8776
8777   return stmt;
8778 }
8779
8780 /* Converts a range-based for-statement into a normal
8781    for-statement, as per the definition.
8782
8783       for (RANGE_DECL : RANGE_EXPR)
8784         BLOCK
8785
8786    should be equivalent to:
8787
8788       {
8789         auto &&__range = RANGE_EXPR;
8790         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8791               __begin != __end;
8792               ++__begin)
8793           {
8794               RANGE_DECL = *__begin;
8795               BLOCK
8796           }
8797       }
8798
8799    If RANGE_EXPR is an array:
8800        BEGIN_EXPR = __range
8801        END_EXPR = __range + ARRAY_SIZE(__range)
8802    Else:
8803         BEGIN_EXPR = begin(__range)
8804         END_EXPR = end(__range);
8805
8806    When calling begin()/end() we must use argument dependent
8807    lookup, but always considering 'std' as an associated namespace.  */
8808
8809 tree
8810 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8811 {
8812   tree range_type, range_temp;
8813   tree begin, end;
8814   tree iter_type, begin_expr, end_expr;
8815   tree condition, expression;
8816
8817   /* Find out the type deduced by the declaration
8818    * `auto &&__range = range_expr' */
8819   range_type = cp_build_reference_type (make_auto (), true);
8820   range_type = do_auto_deduction (range_type, range_expr,
8821                                   type_uses_auto (range_type));
8822
8823   /* Create the __range variable */
8824   range_temp = build_decl (input_location, VAR_DECL,
8825                            get_identifier ("__for_range"), range_type);
8826   TREE_USED (range_temp) = 1;
8827   DECL_ARTIFICIAL (range_temp) = 1;
8828   pushdecl (range_temp);
8829   cp_finish_decl (range_temp, range_expr,
8830                   /*is_constant_init*/false, NULL_TREE,
8831                   LOOKUP_ONLYCONVERTING);
8832
8833   range_temp = convert_from_reference (range_temp);
8834
8835   if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8836     {
8837       /* If RANGE_TEMP is an array we will use pointer arithmetic */
8838       iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8839       begin_expr = range_temp;
8840       end_expr
8841         = build_binary_op (input_location, PLUS_EXPR,
8842                            range_temp,
8843                            array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8844     }
8845   else
8846     {
8847       /* If it is not an array, we must call begin(__range)/end__range() */
8848       VEC(tree,gc) *vec;
8849
8850       begin_expr = get_identifier ("begin");
8851       vec = make_tree_vector ();
8852       VEC_safe_push (tree, gc, vec, range_temp);
8853       begin_expr = perform_koenig_lookup (begin_expr, vec,
8854                                           /*include_std=*/true);
8855       begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8856                                      tf_warning_or_error);
8857       release_tree_vector (vec);
8858
8859       end_expr = get_identifier ("end");
8860       vec = make_tree_vector ();
8861       VEC_safe_push (tree, gc, vec, range_temp);
8862       end_expr = perform_koenig_lookup (end_expr, vec,
8863                                         /*include_std=*/true);
8864       end_expr = finish_call_expr (end_expr, &vec, false, true,
8865                                    tf_warning_or_error);
8866       release_tree_vector (vec);
8867
8868       /* The unqualified type of the __begin and __end temporaries should
8869       * be the same as required by the multiple auto declaration */
8870       iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8871       if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8872         error ("inconsistent begin/end types in range-based for: %qT and %qT",
8873                TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8874     }
8875
8876   /* The new for initialization statement */
8877   begin = build_decl (input_location, VAR_DECL,
8878                       get_identifier ("__for_begin"), iter_type);
8879   TREE_USED (begin) = 1;
8880   DECL_ARTIFICIAL (begin) = 1;
8881   pushdecl (begin);
8882   cp_finish_decl (begin, begin_expr,
8883                   /*is_constant_init*/false, NULL_TREE,
8884                   LOOKUP_ONLYCONVERTING);
8885
8886   end = build_decl (input_location, VAR_DECL,
8887                     get_identifier ("__for_end"), iter_type);
8888   TREE_USED (end) = 1;
8889   DECL_ARTIFICIAL (end) = 1;
8890   pushdecl (end);
8891   cp_finish_decl (end, end_expr,
8892                   /*is_constant_init*/false, NULL_TREE,
8893                   LOOKUP_ONLYCONVERTING);
8894
8895   finish_for_init_stmt (statement);
8896
8897 /* The new for condition */
8898   condition = build_x_binary_op (NE_EXPR,
8899                                  begin, ERROR_MARK,
8900                                  end, ERROR_MARK,
8901                                  NULL, tf_warning_or_error);
8902   finish_for_cond (condition, statement);
8903
8904   /* The new increment expression */
8905   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8906   finish_for_expr (expression, statement);
8907
8908   /* The declaration is initialized with *__begin inside the loop body */
8909   cp_finish_decl (range_decl,
8910                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8911                   /*is_constant_init*/false, NULL_TREE,
8912                   LOOKUP_ONLYCONVERTING);
8913
8914   return statement;
8915 }
8916
8917
8918 /* Parse an iteration-statement.
8919
8920    iteration-statement:
8921      while ( condition ) statement
8922      do statement while ( expression ) ;
8923      for ( for-init-statement condition [opt] ; expression [opt] )
8924        statement
8925
8926    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8927
8928 static tree
8929 cp_parser_iteration_statement (cp_parser* parser)
8930 {
8931   cp_token *token;
8932   enum rid keyword;
8933   tree statement;
8934   unsigned char in_statement;
8935
8936   /* Peek at the next token.  */
8937   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8938   if (!token)
8939     return error_mark_node;
8940
8941   /* Remember whether or not we are already within an iteration
8942      statement.  */
8943   in_statement = parser->in_statement;
8944
8945   /* See what kind of keyword it is.  */
8946   keyword = token->keyword;
8947   switch (keyword)
8948     {
8949     case RID_WHILE:
8950       {
8951         tree condition;
8952
8953         /* Begin the while-statement.  */
8954         statement = begin_while_stmt ();
8955         /* Look for the `('.  */
8956         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8957         /* Parse the condition.  */
8958         condition = cp_parser_condition (parser);
8959         finish_while_stmt_cond (condition, statement);
8960         /* Look for the `)'.  */
8961         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8962         /* Parse the dependent statement.  */
8963         parser->in_statement = IN_ITERATION_STMT;
8964         cp_parser_already_scoped_statement (parser);
8965         parser->in_statement = in_statement;
8966         /* We're done with the while-statement.  */
8967         finish_while_stmt (statement);
8968       }
8969       break;
8970
8971     case RID_DO:
8972       {
8973         tree expression;
8974
8975         /* Begin the do-statement.  */
8976         statement = begin_do_stmt ();
8977         /* Parse the body of the do-statement.  */
8978         parser->in_statement = IN_ITERATION_STMT;
8979         cp_parser_implicitly_scoped_statement (parser, NULL);
8980         parser->in_statement = in_statement;
8981         finish_do_body (statement);
8982         /* Look for the `while' keyword.  */
8983         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8984         /* Look for the `('.  */
8985         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8986         /* Parse the expression.  */
8987         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8988         /* We're done with the do-statement.  */
8989         finish_do_stmt (expression, statement);
8990         /* Look for the `)'.  */
8991         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8992         /* Look for the `;'.  */
8993         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8994       }
8995       break;
8996
8997     case RID_FOR:
8998       {
8999         /* Look for the `('.  */
9000         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9001
9002         if (cxx_dialect == cxx0x)
9003           statement = cp_parser_range_for (parser);
9004         else
9005           statement = NULL_TREE;
9006         if (statement == NULL_TREE)
9007           statement = cp_parser_c_for (parser);
9008
9009         /* Look for the `)'.  */
9010         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9011
9012         /* Parse the body of the for-statement.  */
9013         parser->in_statement = IN_ITERATION_STMT;
9014         cp_parser_already_scoped_statement (parser);
9015         parser->in_statement = in_statement;
9016
9017         /* We're done with the for-statement.  */
9018         finish_for_stmt (statement);
9019       }
9020       break;
9021
9022     default:
9023       cp_parser_error (parser, "expected iteration-statement");
9024       statement = error_mark_node;
9025       break;
9026     }
9027
9028   return statement;
9029 }
9030
9031 /* Parse a for-init-statement.
9032
9033    for-init-statement:
9034      expression-statement
9035      simple-declaration  */
9036
9037 static void
9038 cp_parser_for_init_statement (cp_parser* parser)
9039 {
9040   /* If the next token is a `;', then we have an empty
9041      expression-statement.  Grammatically, this is also a
9042      simple-declaration, but an invalid one, because it does not
9043      declare anything.  Therefore, if we did not handle this case
9044      specially, we would issue an error message about an invalid
9045      declaration.  */
9046   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9047     {
9048       /* We're going to speculatively look for a declaration, falling back
9049          to an expression, if necessary.  */
9050       cp_parser_parse_tentatively (parser);
9051       /* Parse the declaration.  */
9052       cp_parser_simple_declaration (parser,
9053                                     /*function_definition_allowed_p=*/false);
9054       /* If the tentative parse failed, then we shall need to look for an
9055          expression-statement.  */
9056       if (cp_parser_parse_definitely (parser))
9057         return;
9058     }
9059
9060   cp_parser_expression_statement (parser, NULL_TREE);
9061 }
9062
9063 /* Parse a jump-statement.
9064
9065    jump-statement:
9066      break ;
9067      continue ;
9068      return expression [opt] ;
9069      return braced-init-list ;
9070      goto identifier ;
9071
9072    GNU extension:
9073
9074    jump-statement:
9075      goto * expression ;
9076
9077    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9078
9079 static tree
9080 cp_parser_jump_statement (cp_parser* parser)
9081 {
9082   tree statement = error_mark_node;
9083   cp_token *token;
9084   enum rid keyword;
9085   unsigned char in_statement;
9086
9087   /* Peek at the next token.  */
9088   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9089   if (!token)
9090     return error_mark_node;
9091
9092   /* See what kind of keyword it is.  */
9093   keyword = token->keyword;
9094   switch (keyword)
9095     {
9096     case RID_BREAK:
9097       in_statement = parser->in_statement & ~IN_IF_STMT;      
9098       switch (in_statement)
9099         {
9100         case 0:
9101           error_at (token->location, "break statement not within loop or switch");
9102           break;
9103         default:
9104           gcc_assert ((in_statement & IN_SWITCH_STMT)
9105                       || in_statement == IN_ITERATION_STMT);
9106           statement = finish_break_stmt ();
9107           break;
9108         case IN_OMP_BLOCK:
9109           error_at (token->location, "invalid exit from OpenMP structured block");
9110           break;
9111         case IN_OMP_FOR:
9112           error_at (token->location, "break statement used with OpenMP for loop");
9113           break;
9114         }
9115       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9116       break;
9117
9118     case RID_CONTINUE:
9119       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9120         {
9121         case 0:
9122           error_at (token->location, "continue statement not within a loop");
9123           break;
9124         case IN_ITERATION_STMT:
9125         case IN_OMP_FOR:
9126           statement = finish_continue_stmt ();
9127           break;
9128         case IN_OMP_BLOCK:
9129           error_at (token->location, "invalid exit from OpenMP structured block");
9130           break;
9131         default:
9132           gcc_unreachable ();
9133         }
9134       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9135       break;
9136
9137     case RID_RETURN:
9138       {
9139         tree expr;
9140         bool expr_non_constant_p;
9141
9142         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9143           {
9144             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9145             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9146           }
9147         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9148           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9149         else
9150           /* If the next token is a `;', then there is no
9151              expression.  */
9152           expr = NULL_TREE;
9153         /* Build the return-statement.  */
9154         statement = finish_return_stmt (expr);
9155         /* Look for the final `;'.  */
9156         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9157       }
9158       break;
9159
9160     case RID_GOTO:
9161       /* Create the goto-statement.  */
9162       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9163         {
9164           /* Issue a warning about this use of a GNU extension.  */
9165           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9166           /* Consume the '*' token.  */
9167           cp_lexer_consume_token (parser->lexer);
9168           /* Parse the dependent expression.  */
9169           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9170         }
9171       else
9172         finish_goto_stmt (cp_parser_identifier (parser));
9173       /* Look for the final `;'.  */
9174       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9175       break;
9176
9177     default:
9178       cp_parser_error (parser, "expected jump-statement");
9179       break;
9180     }
9181
9182   return statement;
9183 }
9184
9185 /* Parse a declaration-statement.
9186
9187    declaration-statement:
9188      block-declaration  */
9189
9190 static void
9191 cp_parser_declaration_statement (cp_parser* parser)
9192 {
9193   void *p;
9194
9195   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9196   p = obstack_alloc (&declarator_obstack, 0);
9197
9198  /* Parse the block-declaration.  */
9199   cp_parser_block_declaration (parser, /*statement_p=*/true);
9200
9201   /* Free any declarators allocated.  */
9202   obstack_free (&declarator_obstack, p);
9203
9204   /* Finish off the statement.  */
9205   finish_stmt ();
9206 }
9207
9208 /* Some dependent statements (like `if (cond) statement'), are
9209    implicitly in their own scope.  In other words, if the statement is
9210    a single statement (as opposed to a compound-statement), it is
9211    none-the-less treated as if it were enclosed in braces.  Any
9212    declarations appearing in the dependent statement are out of scope
9213    after control passes that point.  This function parses a statement,
9214    but ensures that is in its own scope, even if it is not a
9215    compound-statement.
9216
9217    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9218    is a (possibly labeled) if statement which is not enclosed in
9219    braces and has an else clause.  This is used to implement
9220    -Wparentheses.
9221
9222    Returns the new statement.  */
9223
9224 static tree
9225 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9226 {
9227   tree statement;
9228
9229   if (if_p != NULL)
9230     *if_p = false;
9231
9232   /* Mark if () ; with a special NOP_EXPR.  */
9233   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9234     {
9235       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9236       cp_lexer_consume_token (parser->lexer);
9237       statement = add_stmt (build_empty_stmt (loc));
9238     }
9239   /* if a compound is opened, we simply parse the statement directly.  */
9240   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9241     statement = cp_parser_compound_statement (parser, NULL, false);
9242   /* If the token is not a `{', then we must take special action.  */
9243   else
9244     {
9245       /* Create a compound-statement.  */
9246       statement = begin_compound_stmt (0);
9247       /* Parse the dependent-statement.  */
9248       cp_parser_statement (parser, NULL_TREE, false, if_p);
9249       /* Finish the dummy compound-statement.  */
9250       finish_compound_stmt (statement);
9251     }
9252
9253   /* Return the statement.  */
9254   return statement;
9255 }
9256
9257 /* For some dependent statements (like `while (cond) statement'), we
9258    have already created a scope.  Therefore, even if the dependent
9259    statement is a compound-statement, we do not want to create another
9260    scope.  */
9261
9262 static void
9263 cp_parser_already_scoped_statement (cp_parser* parser)
9264 {
9265   /* If the token is a `{', then we must take special action.  */
9266   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9267     cp_parser_statement (parser, NULL_TREE, false, NULL);
9268   else
9269     {
9270       /* Avoid calling cp_parser_compound_statement, so that we
9271          don't create a new scope.  Do everything else by hand.  */
9272       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9273       /* If the next keyword is `__label__' we have a label declaration.  */
9274       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9275         cp_parser_label_declaration (parser);
9276       /* Parse an (optional) statement-seq.  */
9277       cp_parser_statement_seq_opt (parser, NULL_TREE);
9278       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9279     }
9280 }
9281
9282 /* Declarations [gram.dcl.dcl] */
9283
9284 /* Parse an optional declaration-sequence.
9285
9286    declaration-seq:
9287      declaration
9288      declaration-seq declaration  */
9289
9290 static void
9291 cp_parser_declaration_seq_opt (cp_parser* parser)
9292 {
9293   while (true)
9294     {
9295       cp_token *token;
9296
9297       token = cp_lexer_peek_token (parser->lexer);
9298
9299       if (token->type == CPP_CLOSE_BRACE
9300           || token->type == CPP_EOF
9301           || token->type == CPP_PRAGMA_EOL)
9302         break;
9303
9304       if (token->type == CPP_SEMICOLON)
9305         {
9306           /* A declaration consisting of a single semicolon is
9307              invalid.  Allow it unless we're being pedantic.  */
9308           cp_lexer_consume_token (parser->lexer);
9309           if (!in_system_header)
9310             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9311           continue;
9312         }
9313
9314       /* If we're entering or exiting a region that's implicitly
9315          extern "C", modify the lang context appropriately.  */
9316       if (!parser->implicit_extern_c && token->implicit_extern_c)
9317         {
9318           push_lang_context (lang_name_c);
9319           parser->implicit_extern_c = true;
9320         }
9321       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9322         {
9323           pop_lang_context ();
9324           parser->implicit_extern_c = false;
9325         }
9326
9327       if (token->type == CPP_PRAGMA)
9328         {
9329           /* A top-level declaration can consist solely of a #pragma.
9330              A nested declaration cannot, so this is done here and not
9331              in cp_parser_declaration.  (A #pragma at block scope is
9332              handled in cp_parser_statement.)  */
9333           cp_parser_pragma (parser, pragma_external);
9334           continue;
9335         }
9336
9337       /* Parse the declaration itself.  */
9338       cp_parser_declaration (parser);
9339     }
9340 }
9341
9342 /* Parse a declaration.
9343
9344    declaration:
9345      block-declaration
9346      function-definition
9347      template-declaration
9348      explicit-instantiation
9349      explicit-specialization
9350      linkage-specification
9351      namespace-definition
9352
9353    GNU extension:
9354
9355    declaration:
9356       __extension__ declaration */
9357
9358 static void
9359 cp_parser_declaration (cp_parser* parser)
9360 {
9361   cp_token token1;
9362   cp_token token2;
9363   int saved_pedantic;
9364   void *p;
9365   tree attributes = NULL_TREE;
9366
9367   /* Check for the `__extension__' keyword.  */
9368   if (cp_parser_extension_opt (parser, &saved_pedantic))
9369     {
9370       /* Parse the qualified declaration.  */
9371       cp_parser_declaration (parser);
9372       /* Restore the PEDANTIC flag.  */
9373       pedantic = saved_pedantic;
9374
9375       return;
9376     }
9377
9378   /* Try to figure out what kind of declaration is present.  */
9379   token1 = *cp_lexer_peek_token (parser->lexer);
9380
9381   if (token1.type != CPP_EOF)
9382     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9383   else
9384     {
9385       token2.type = CPP_EOF;
9386       token2.keyword = RID_MAX;
9387     }
9388
9389   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9390   p = obstack_alloc (&declarator_obstack, 0);
9391
9392   /* If the next token is `extern' and the following token is a string
9393      literal, then we have a linkage specification.  */
9394   if (token1.keyword == RID_EXTERN
9395       && cp_parser_is_string_literal (&token2))
9396     cp_parser_linkage_specification (parser);
9397   /* If the next token is `template', then we have either a template
9398      declaration, an explicit instantiation, or an explicit
9399      specialization.  */
9400   else if (token1.keyword == RID_TEMPLATE)
9401     {
9402       /* `template <>' indicates a template specialization.  */
9403       if (token2.type == CPP_LESS
9404           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9405         cp_parser_explicit_specialization (parser);
9406       /* `template <' indicates a template declaration.  */
9407       else if (token2.type == CPP_LESS)
9408         cp_parser_template_declaration (parser, /*member_p=*/false);
9409       /* Anything else must be an explicit instantiation.  */
9410       else
9411         cp_parser_explicit_instantiation (parser);
9412     }
9413   /* If the next token is `export', then we have a template
9414      declaration.  */
9415   else if (token1.keyword == RID_EXPORT)
9416     cp_parser_template_declaration (parser, /*member_p=*/false);
9417   /* If the next token is `extern', 'static' or 'inline' and the one
9418      after that is `template', we have a GNU extended explicit
9419      instantiation directive.  */
9420   else if (cp_parser_allow_gnu_extensions_p (parser)
9421            && (token1.keyword == RID_EXTERN
9422                || token1.keyword == RID_STATIC
9423                || token1.keyword == RID_INLINE)
9424            && token2.keyword == RID_TEMPLATE)
9425     cp_parser_explicit_instantiation (parser);
9426   /* If the next token is `namespace', check for a named or unnamed
9427      namespace definition.  */
9428   else if (token1.keyword == RID_NAMESPACE
9429            && (/* A named namespace definition.  */
9430                (token2.type == CPP_NAME
9431                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9432                     != CPP_EQ))
9433                /* An unnamed namespace definition.  */
9434                || token2.type == CPP_OPEN_BRACE
9435                || token2.keyword == RID_ATTRIBUTE))
9436     cp_parser_namespace_definition (parser);
9437   /* An inline (associated) namespace definition.  */
9438   else if (token1.keyword == RID_INLINE
9439            && token2.keyword == RID_NAMESPACE)
9440     cp_parser_namespace_definition (parser);
9441   /* Objective-C++ declaration/definition.  */
9442   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9443     cp_parser_objc_declaration (parser, NULL_TREE);
9444   else if (c_dialect_objc ()
9445            && token1.keyword == RID_ATTRIBUTE
9446            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9447     cp_parser_objc_declaration (parser, attributes);
9448   /* We must have either a block declaration or a function
9449      definition.  */
9450   else
9451     /* Try to parse a block-declaration, or a function-definition.  */
9452     cp_parser_block_declaration (parser, /*statement_p=*/false);
9453
9454   /* Free any declarators allocated.  */
9455   obstack_free (&declarator_obstack, p);
9456 }
9457
9458 /* Parse a block-declaration.
9459
9460    block-declaration:
9461      simple-declaration
9462      asm-definition
9463      namespace-alias-definition
9464      using-declaration
9465      using-directive
9466
9467    GNU Extension:
9468
9469    block-declaration:
9470      __extension__ block-declaration
9471
9472    C++0x Extension:
9473
9474    block-declaration:
9475      static_assert-declaration
9476
9477    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9478    part of a declaration-statement.  */
9479
9480 static void
9481 cp_parser_block_declaration (cp_parser *parser,
9482                              bool      statement_p)
9483 {
9484   cp_token *token1;
9485   int saved_pedantic;
9486
9487   /* Check for the `__extension__' keyword.  */
9488   if (cp_parser_extension_opt (parser, &saved_pedantic))
9489     {
9490       /* Parse the qualified declaration.  */
9491       cp_parser_block_declaration (parser, statement_p);
9492       /* Restore the PEDANTIC flag.  */
9493       pedantic = saved_pedantic;
9494
9495       return;
9496     }
9497
9498   /* Peek at the next token to figure out which kind of declaration is
9499      present.  */
9500   token1 = cp_lexer_peek_token (parser->lexer);
9501
9502   /* If the next keyword is `asm', we have an asm-definition.  */
9503   if (token1->keyword == RID_ASM)
9504     {
9505       if (statement_p)
9506         cp_parser_commit_to_tentative_parse (parser);
9507       cp_parser_asm_definition (parser);
9508     }
9509   /* If the next keyword is `namespace', we have a
9510      namespace-alias-definition.  */
9511   else if (token1->keyword == RID_NAMESPACE)
9512     cp_parser_namespace_alias_definition (parser);
9513   /* If the next keyword is `using', we have either a
9514      using-declaration or a using-directive.  */
9515   else if (token1->keyword == RID_USING)
9516     {
9517       cp_token *token2;
9518
9519       if (statement_p)
9520         cp_parser_commit_to_tentative_parse (parser);
9521       /* If the token after `using' is `namespace', then we have a
9522          using-directive.  */
9523       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9524       if (token2->keyword == RID_NAMESPACE)
9525         cp_parser_using_directive (parser);
9526       /* Otherwise, it's a using-declaration.  */
9527       else
9528         cp_parser_using_declaration (parser,
9529                                      /*access_declaration_p=*/false);
9530     }
9531   /* If the next keyword is `__label__' we have a misplaced label
9532      declaration.  */
9533   else if (token1->keyword == RID_LABEL)
9534     {
9535       cp_lexer_consume_token (parser->lexer);
9536       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9537       cp_parser_skip_to_end_of_statement (parser);
9538       /* If the next token is now a `;', consume it.  */
9539       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9540         cp_lexer_consume_token (parser->lexer);
9541     }
9542   /* If the next token is `static_assert' we have a static assertion.  */
9543   else if (token1->keyword == RID_STATIC_ASSERT)
9544     cp_parser_static_assert (parser, /*member_p=*/false);
9545   /* Anything else must be a simple-declaration.  */
9546   else
9547     cp_parser_simple_declaration (parser, !statement_p);
9548 }
9549
9550 /* Parse a simple-declaration.
9551
9552    simple-declaration:
9553      decl-specifier-seq [opt] init-declarator-list [opt] ;
9554
9555    init-declarator-list:
9556      init-declarator
9557      init-declarator-list , init-declarator
9558
9559    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9560    function-definition as a simple-declaration.  */
9561
9562 static void
9563 cp_parser_simple_declaration (cp_parser* parser,
9564                               bool function_definition_allowed_p)
9565 {
9566   cp_decl_specifier_seq decl_specifiers;
9567   int declares_class_or_enum;
9568   bool saw_declarator;
9569
9570   /* Defer access checks until we know what is being declared; the
9571      checks for names appearing in the decl-specifier-seq should be
9572      done as if we were in the scope of the thing being declared.  */
9573   push_deferring_access_checks (dk_deferred);
9574
9575   /* Parse the decl-specifier-seq.  We have to keep track of whether
9576      or not the decl-specifier-seq declares a named class or
9577      enumeration type, since that is the only case in which the
9578      init-declarator-list is allowed to be empty.
9579
9580      [dcl.dcl]
9581
9582      In a simple-declaration, the optional init-declarator-list can be
9583      omitted only when declaring a class or enumeration, that is when
9584      the decl-specifier-seq contains either a class-specifier, an
9585      elaborated-type-specifier, or an enum-specifier.  */
9586   cp_parser_decl_specifier_seq (parser,
9587                                 CP_PARSER_FLAGS_OPTIONAL,
9588                                 &decl_specifiers,
9589                                 &declares_class_or_enum);
9590   /* We no longer need to defer access checks.  */
9591   stop_deferring_access_checks ();
9592
9593   /* In a block scope, a valid declaration must always have a
9594      decl-specifier-seq.  By not trying to parse declarators, we can
9595      resolve the declaration/expression ambiguity more quickly.  */
9596   if (!function_definition_allowed_p
9597       && !decl_specifiers.any_specifiers_p)
9598     {
9599       cp_parser_error (parser, "expected declaration");
9600       goto done;
9601     }
9602
9603   /* If the next two tokens are both identifiers, the code is
9604      erroneous. The usual cause of this situation is code like:
9605
9606        T t;
9607
9608      where "T" should name a type -- but does not.  */
9609   if (!decl_specifiers.any_type_specifiers_p
9610       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9611     {
9612       /* If parsing tentatively, we should commit; we really are
9613          looking at a declaration.  */
9614       cp_parser_commit_to_tentative_parse (parser);
9615       /* Give up.  */
9616       goto done;
9617     }
9618
9619   /* If we have seen at least one decl-specifier, and the next token
9620      is not a parenthesis, then we must be looking at a declaration.
9621      (After "int (" we might be looking at a functional cast.)  */
9622   if (decl_specifiers.any_specifiers_p
9623       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9624       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9625       && !cp_parser_error_occurred (parser))
9626     cp_parser_commit_to_tentative_parse (parser);
9627
9628   /* Keep going until we hit the `;' at the end of the simple
9629      declaration.  */
9630   saw_declarator = false;
9631   while (cp_lexer_next_token_is_not (parser->lexer,
9632                                      CPP_SEMICOLON))
9633     {
9634       cp_token *token;
9635       bool function_definition_p;
9636       tree decl;
9637
9638       if (saw_declarator)
9639         {
9640           /* If we are processing next declarator, coma is expected */
9641           token = cp_lexer_peek_token (parser->lexer);
9642           gcc_assert (token->type == CPP_COMMA);
9643           cp_lexer_consume_token (parser->lexer);
9644         }
9645       else
9646         saw_declarator = true;
9647
9648       /* Parse the init-declarator.  */
9649       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9650                                         /*checks=*/NULL,
9651                                         function_definition_allowed_p,
9652                                         /*member_p=*/false,
9653                                         declares_class_or_enum,
9654                                         &function_definition_p);
9655       /* If an error occurred while parsing tentatively, exit quickly.
9656          (That usually happens when in the body of a function; each
9657          statement is treated as a declaration-statement until proven
9658          otherwise.)  */
9659       if (cp_parser_error_occurred (parser))
9660         goto done;
9661       /* Handle function definitions specially.  */
9662       if (function_definition_p)
9663         {
9664           /* If the next token is a `,', then we are probably
9665              processing something like:
9666
9667                void f() {}, *p;
9668
9669              which is erroneous.  */
9670           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9671             {
9672               cp_token *token = cp_lexer_peek_token (parser->lexer);
9673               error_at (token->location,
9674                         "mixing"
9675                         " declarations and function-definitions is forbidden");
9676             }
9677           /* Otherwise, we're done with the list of declarators.  */
9678           else
9679             {
9680               pop_deferring_access_checks ();
9681               return;
9682             }
9683         }
9684       /* The next token should be either a `,' or a `;'.  */
9685       token = cp_lexer_peek_token (parser->lexer);
9686       /* If it's a `,', there are more declarators to come.  */
9687       if (token->type == CPP_COMMA)
9688         /* will be consumed next time around */;
9689       /* If it's a `;', we are done.  */
9690       else if (token->type == CPP_SEMICOLON)
9691         break;
9692       /* Anything else is an error.  */
9693       else
9694         {
9695           /* If we have already issued an error message we don't need
9696              to issue another one.  */
9697           if (decl != error_mark_node
9698               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9699             cp_parser_error (parser, "expected %<,%> or %<;%>");
9700           /* Skip tokens until we reach the end of the statement.  */
9701           cp_parser_skip_to_end_of_statement (parser);
9702           /* If the next token is now a `;', consume it.  */
9703           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9704             cp_lexer_consume_token (parser->lexer);
9705           goto done;
9706         }
9707       /* After the first time around, a function-definition is not
9708          allowed -- even if it was OK at first.  For example:
9709
9710            int i, f() {}
9711
9712          is not valid.  */
9713       function_definition_allowed_p = false;
9714     }
9715
9716   /* Issue an error message if no declarators are present, and the
9717      decl-specifier-seq does not itself declare a class or
9718      enumeration.  */
9719   if (!saw_declarator)
9720     {
9721       if (cp_parser_declares_only_class_p (parser))
9722         shadow_tag (&decl_specifiers);
9723       /* Perform any deferred access checks.  */
9724       perform_deferred_access_checks ();
9725     }
9726
9727   /* Consume the `;'.  */
9728   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9729
9730  done:
9731   pop_deferring_access_checks ();
9732 }
9733
9734 /* Parse a decl-specifier-seq.
9735
9736    decl-specifier-seq:
9737      decl-specifier-seq [opt] decl-specifier
9738
9739    decl-specifier:
9740      storage-class-specifier
9741      type-specifier
9742      function-specifier
9743      friend
9744      typedef
9745
9746    GNU Extension:
9747
9748    decl-specifier:
9749      attributes
9750
9751    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9752
9753    The parser flags FLAGS is used to control type-specifier parsing.
9754
9755    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9756    flags:
9757
9758      1: one of the decl-specifiers is an elaborated-type-specifier
9759         (i.e., a type declaration)
9760      2: one of the decl-specifiers is an enum-specifier or a
9761         class-specifier (i.e., a type definition)
9762
9763    */
9764
9765 static void
9766 cp_parser_decl_specifier_seq (cp_parser* parser,
9767                               cp_parser_flags flags,
9768                               cp_decl_specifier_seq *decl_specs,
9769                               int* declares_class_or_enum)
9770 {
9771   bool constructor_possible_p = !parser->in_declarator_p;
9772   cp_token *start_token = NULL;
9773
9774   /* Clear DECL_SPECS.  */
9775   clear_decl_specs (decl_specs);
9776
9777   /* Assume no class or enumeration type is declared.  */
9778   *declares_class_or_enum = 0;
9779
9780   /* Keep reading specifiers until there are no more to read.  */
9781   while (true)
9782     {
9783       bool constructor_p;
9784       bool found_decl_spec;
9785       cp_token *token;
9786
9787       /* Peek at the next token.  */
9788       token = cp_lexer_peek_token (parser->lexer);
9789
9790       /* Save the first token of the decl spec list for error
9791          reporting.  */
9792       if (!start_token)
9793         start_token = token;
9794       /* Handle attributes.  */
9795       if (token->keyword == RID_ATTRIBUTE)
9796         {
9797           /* Parse the attributes.  */
9798           decl_specs->attributes
9799             = chainon (decl_specs->attributes,
9800                        cp_parser_attributes_opt (parser));
9801           continue;
9802         }
9803       /* Assume we will find a decl-specifier keyword.  */
9804       found_decl_spec = true;
9805       /* If the next token is an appropriate keyword, we can simply
9806          add it to the list.  */
9807       switch (token->keyword)
9808         {
9809           /* decl-specifier:
9810                friend
9811                constexpr */
9812         case RID_FRIEND:
9813           if (!at_class_scope_p ())
9814             {
9815               error_at (token->location, "%<friend%> used outside of class");
9816               cp_lexer_purge_token (parser->lexer);
9817             }
9818           else
9819             {
9820               ++decl_specs->specs[(int) ds_friend];
9821               /* Consume the token.  */
9822               cp_lexer_consume_token (parser->lexer);
9823             }
9824           break;
9825
9826         case RID_CONSTEXPR:
9827           ++decl_specs->specs[(int) ds_constexpr];
9828           cp_lexer_consume_token (parser->lexer);
9829           break;
9830
9831           /* function-specifier:
9832                inline
9833                virtual
9834                explicit  */
9835         case RID_INLINE:
9836         case RID_VIRTUAL:
9837         case RID_EXPLICIT:
9838           cp_parser_function_specifier_opt (parser, decl_specs);
9839           break;
9840
9841           /* decl-specifier:
9842                typedef  */
9843         case RID_TYPEDEF:
9844           ++decl_specs->specs[(int) ds_typedef];
9845           /* Consume the token.  */
9846           cp_lexer_consume_token (parser->lexer);
9847           /* A constructor declarator cannot appear in a typedef.  */
9848           constructor_possible_p = false;
9849           /* The "typedef" keyword can only occur in a declaration; we
9850              may as well commit at this point.  */
9851           cp_parser_commit_to_tentative_parse (parser);
9852
9853           if (decl_specs->storage_class != sc_none)
9854             decl_specs->conflicting_specifiers_p = true;
9855           break;
9856
9857           /* storage-class-specifier:
9858                auto
9859                register
9860                static
9861                extern
9862                mutable
9863
9864              GNU Extension:
9865                thread  */
9866         case RID_AUTO:
9867           if (cxx_dialect == cxx98) 
9868             {
9869               /* Consume the token.  */
9870               cp_lexer_consume_token (parser->lexer);
9871
9872               /* Complain about `auto' as a storage specifier, if
9873                  we're complaining about C++0x compatibility.  */
9874               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9875                           " will change meaning in C++0x; please remove it");
9876
9877               /* Set the storage class anyway.  */
9878               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9879                                            token->location);
9880             }
9881           else
9882             /* C++0x auto type-specifier.  */
9883             found_decl_spec = false;
9884           break;
9885
9886         case RID_REGISTER:
9887         case RID_STATIC:
9888         case RID_EXTERN:
9889         case RID_MUTABLE:
9890           /* Consume the token.  */
9891           cp_lexer_consume_token (parser->lexer);
9892           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9893                                        token->location);
9894           break;
9895         case RID_THREAD:
9896           /* Consume the token.  */
9897           cp_lexer_consume_token (parser->lexer);
9898           ++decl_specs->specs[(int) ds_thread];
9899           break;
9900
9901         default:
9902           /* We did not yet find a decl-specifier yet.  */
9903           found_decl_spec = false;
9904           break;
9905         }
9906
9907       if (found_decl_spec
9908           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9909           && token->keyword != RID_CONSTEXPR)
9910         error ("decl-specifier invalid in condition");
9911
9912       /* Constructors are a special case.  The `S' in `S()' is not a
9913          decl-specifier; it is the beginning of the declarator.  */
9914       constructor_p
9915         = (!found_decl_spec
9916            && constructor_possible_p
9917            && (cp_parser_constructor_declarator_p
9918                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9919
9920       /* If we don't have a DECL_SPEC yet, then we must be looking at
9921          a type-specifier.  */
9922       if (!found_decl_spec && !constructor_p)
9923         {
9924           int decl_spec_declares_class_or_enum;
9925           bool is_cv_qualifier;
9926           tree type_spec;
9927
9928           type_spec
9929             = cp_parser_type_specifier (parser, flags,
9930                                         decl_specs,
9931                                         /*is_declaration=*/true,
9932                                         &decl_spec_declares_class_or_enum,
9933                                         &is_cv_qualifier);
9934           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9935
9936           /* If this type-specifier referenced a user-defined type
9937              (a typedef, class-name, etc.), then we can't allow any
9938              more such type-specifiers henceforth.
9939
9940              [dcl.spec]
9941
9942              The longest sequence of decl-specifiers that could
9943              possibly be a type name is taken as the
9944              decl-specifier-seq of a declaration.  The sequence shall
9945              be self-consistent as described below.
9946
9947              [dcl.type]
9948
9949              As a general rule, at most one type-specifier is allowed
9950              in the complete decl-specifier-seq of a declaration.  The
9951              only exceptions are the following:
9952
9953              -- const or volatile can be combined with any other
9954                 type-specifier.
9955
9956              -- signed or unsigned can be combined with char, long,
9957                 short, or int.
9958
9959              -- ..
9960
9961              Example:
9962
9963                typedef char* Pc;
9964                void g (const int Pc);
9965
9966              Here, Pc is *not* part of the decl-specifier seq; it's
9967              the declarator.  Therefore, once we see a type-specifier
9968              (other than a cv-qualifier), we forbid any additional
9969              user-defined types.  We *do* still allow things like `int
9970              int' to be considered a decl-specifier-seq, and issue the
9971              error message later.  */
9972           if (type_spec && !is_cv_qualifier)
9973             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9974           /* A constructor declarator cannot follow a type-specifier.  */
9975           if (type_spec)
9976             {
9977               constructor_possible_p = false;
9978               found_decl_spec = true;
9979               if (!is_cv_qualifier)
9980                 decl_specs->any_type_specifiers_p = true;
9981             }
9982         }
9983
9984       /* If we still do not have a DECL_SPEC, then there are no more
9985          decl-specifiers.  */
9986       if (!found_decl_spec)
9987         break;
9988
9989       decl_specs->any_specifiers_p = true;
9990       /* After we see one decl-specifier, further decl-specifiers are
9991          always optional.  */
9992       flags |= CP_PARSER_FLAGS_OPTIONAL;
9993     }
9994
9995   cp_parser_check_decl_spec (decl_specs, start_token->location);
9996
9997   /* Don't allow a friend specifier with a class definition.  */
9998   if (decl_specs->specs[(int) ds_friend] != 0
9999       && (*declares_class_or_enum & 2))
10000     error_at (start_token->location,
10001               "class definition may not be declared a friend");
10002 }
10003
10004 /* Parse an (optional) storage-class-specifier.
10005
10006    storage-class-specifier:
10007      auto
10008      register
10009      static
10010      extern
10011      mutable
10012
10013    GNU Extension:
10014
10015    storage-class-specifier:
10016      thread
10017
10018    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10019
10020 static tree
10021 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10022 {
10023   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10024     {
10025     case RID_AUTO:
10026       if (cxx_dialect != cxx98)
10027         return NULL_TREE;
10028       /* Fall through for C++98.  */
10029
10030     case RID_REGISTER:
10031     case RID_STATIC:
10032     case RID_EXTERN:
10033     case RID_MUTABLE:
10034     case RID_THREAD:
10035       /* Consume the token.  */
10036       return cp_lexer_consume_token (parser->lexer)->u.value;
10037
10038     default:
10039       return NULL_TREE;
10040     }
10041 }
10042
10043 /* Parse an (optional) function-specifier.
10044
10045    function-specifier:
10046      inline
10047      virtual
10048      explicit
10049
10050    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10051    Updates DECL_SPECS, if it is non-NULL.  */
10052
10053 static tree
10054 cp_parser_function_specifier_opt (cp_parser* parser,
10055                                   cp_decl_specifier_seq *decl_specs)
10056 {
10057   cp_token *token = cp_lexer_peek_token (parser->lexer);
10058   switch (token->keyword)
10059     {
10060     case RID_INLINE:
10061       if (decl_specs)
10062         ++decl_specs->specs[(int) ds_inline];
10063       break;
10064
10065     case RID_VIRTUAL:
10066       /* 14.5.2.3 [temp.mem]
10067
10068          A member function template shall not be virtual.  */
10069       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10070         error_at (token->location, "templates may not be %<virtual%>");
10071       else if (decl_specs)
10072         ++decl_specs->specs[(int) ds_virtual];
10073       break;
10074
10075     case RID_EXPLICIT:
10076       if (decl_specs)
10077         ++decl_specs->specs[(int) ds_explicit];
10078       break;
10079
10080     default:
10081       return NULL_TREE;
10082     }
10083
10084   /* Consume the token.  */
10085   return cp_lexer_consume_token (parser->lexer)->u.value;
10086 }
10087
10088 /* Parse a linkage-specification.
10089
10090    linkage-specification:
10091      extern string-literal { declaration-seq [opt] }
10092      extern string-literal declaration  */
10093
10094 static void
10095 cp_parser_linkage_specification (cp_parser* parser)
10096 {
10097   tree linkage;
10098
10099   /* Look for the `extern' keyword.  */
10100   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10101
10102   /* Look for the string-literal.  */
10103   linkage = cp_parser_string_literal (parser, false, false);
10104
10105   /* Transform the literal into an identifier.  If the literal is a
10106      wide-character string, or contains embedded NULs, then we can't
10107      handle it as the user wants.  */
10108   if (strlen (TREE_STRING_POINTER (linkage))
10109       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10110     {
10111       cp_parser_error (parser, "invalid linkage-specification");
10112       /* Assume C++ linkage.  */
10113       linkage = lang_name_cplusplus;
10114     }
10115   else
10116     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10117
10118   /* We're now using the new linkage.  */
10119   push_lang_context (linkage);
10120
10121   /* If the next token is a `{', then we're using the first
10122      production.  */
10123   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10124     {
10125       /* Consume the `{' token.  */
10126       cp_lexer_consume_token (parser->lexer);
10127       /* Parse the declarations.  */
10128       cp_parser_declaration_seq_opt (parser);
10129       /* Look for the closing `}'.  */
10130       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10131     }
10132   /* Otherwise, there's just one declaration.  */
10133   else
10134     {
10135       bool saved_in_unbraced_linkage_specification_p;
10136
10137       saved_in_unbraced_linkage_specification_p
10138         = parser->in_unbraced_linkage_specification_p;
10139       parser->in_unbraced_linkage_specification_p = true;
10140       cp_parser_declaration (parser);
10141       parser->in_unbraced_linkage_specification_p
10142         = saved_in_unbraced_linkage_specification_p;
10143     }
10144
10145   /* We're done with the linkage-specification.  */
10146   pop_lang_context ();
10147 }
10148
10149 /* Parse a static_assert-declaration.
10150
10151    static_assert-declaration:
10152      static_assert ( constant-expression , string-literal ) ; 
10153
10154    If MEMBER_P, this static_assert is a class member.  */
10155
10156 static void 
10157 cp_parser_static_assert(cp_parser *parser, bool member_p)
10158 {
10159   tree condition;
10160   tree message;
10161   cp_token *token;
10162   location_t saved_loc;
10163
10164   /* Peek at the `static_assert' token so we can keep track of exactly
10165      where the static assertion started.  */
10166   token = cp_lexer_peek_token (parser->lexer);
10167   saved_loc = token->location;
10168
10169   /* Look for the `static_assert' keyword.  */
10170   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10171                                   RT_STATIC_ASSERT))
10172     return;
10173
10174   /*  We know we are in a static assertion; commit to any tentative
10175       parse.  */
10176   if (cp_parser_parsing_tentatively (parser))
10177     cp_parser_commit_to_tentative_parse (parser);
10178
10179   /* Parse the `(' starting the static assertion condition.  */
10180   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10181
10182   /* Parse the constant-expression.  */
10183   condition = 
10184     cp_parser_constant_expression (parser,
10185                                    /*allow_non_constant_p=*/false,
10186                                    /*non_constant_p=*/NULL);
10187
10188   /* Parse the separating `,'.  */
10189   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10190
10191   /* Parse the string-literal message.  */
10192   message = cp_parser_string_literal (parser, 
10193                                       /*translate=*/false,
10194                                       /*wide_ok=*/true);
10195
10196   /* A `)' completes the static assertion.  */
10197   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10198     cp_parser_skip_to_closing_parenthesis (parser, 
10199                                            /*recovering=*/true, 
10200                                            /*or_comma=*/false,
10201                                            /*consume_paren=*/true);
10202
10203   /* A semicolon terminates the declaration.  */
10204   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10205
10206   /* Complete the static assertion, which may mean either processing 
10207      the static assert now or saving it for template instantiation.  */
10208   finish_static_assert (condition, message, saved_loc, member_p);
10209 }
10210
10211 /* Parse a `decltype' type. Returns the type. 
10212
10213    simple-type-specifier:
10214      decltype ( expression )  */
10215
10216 static tree
10217 cp_parser_decltype (cp_parser *parser)
10218 {
10219   tree expr;
10220   bool id_expression_or_member_access_p = false;
10221   const char *saved_message;
10222   bool saved_integral_constant_expression_p;
10223   bool saved_non_integral_constant_expression_p;
10224   cp_token *id_expr_start_token;
10225
10226   /* Look for the `decltype' token.  */
10227   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10228     return error_mark_node;
10229
10230   /* Types cannot be defined in a `decltype' expression.  Save away the
10231      old message.  */
10232   saved_message = parser->type_definition_forbidden_message;
10233
10234   /* And create the new one.  */
10235   parser->type_definition_forbidden_message
10236     = G_("types may not be defined in %<decltype%> expressions");
10237
10238   /* The restrictions on constant-expressions do not apply inside
10239      decltype expressions.  */
10240   saved_integral_constant_expression_p
10241     = parser->integral_constant_expression_p;
10242   saved_non_integral_constant_expression_p
10243     = parser->non_integral_constant_expression_p;
10244   parser->integral_constant_expression_p = false;
10245
10246   /* Do not actually evaluate the expression.  */
10247   ++cp_unevaluated_operand;
10248
10249   /* Do not warn about problems with the expression.  */
10250   ++c_inhibit_evaluation_warnings;
10251
10252   /* Parse the opening `('.  */
10253   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10254     return error_mark_node;
10255   
10256   /* First, try parsing an id-expression.  */
10257   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10258   cp_parser_parse_tentatively (parser);
10259   expr = cp_parser_id_expression (parser,
10260                                   /*template_keyword_p=*/false,
10261                                   /*check_dependency_p=*/true,
10262                                   /*template_p=*/NULL,
10263                                   /*declarator_p=*/false,
10264                                   /*optional_p=*/false);
10265
10266   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10267     {
10268       bool non_integral_constant_expression_p = false;
10269       tree id_expression = expr;
10270       cp_id_kind idk;
10271       const char *error_msg;
10272
10273       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10274         /* Lookup the name we got back from the id-expression.  */
10275         expr = cp_parser_lookup_name (parser, expr,
10276                                       none_type,
10277                                       /*is_template=*/false,
10278                                       /*is_namespace=*/false,
10279                                       /*check_dependency=*/true,
10280                                       /*ambiguous_decls=*/NULL,
10281                                       id_expr_start_token->location);
10282
10283       if (expr
10284           && expr != error_mark_node
10285           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10286           && TREE_CODE (expr) != TYPE_DECL
10287           && (TREE_CODE (expr) != BIT_NOT_EXPR
10288               || !TYPE_P (TREE_OPERAND (expr, 0)))
10289           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10290         {
10291           /* Complete lookup of the id-expression.  */
10292           expr = (finish_id_expression
10293                   (id_expression, expr, parser->scope, &idk,
10294                    /*integral_constant_expression_p=*/false,
10295                    /*allow_non_integral_constant_expression_p=*/true,
10296                    &non_integral_constant_expression_p,
10297                    /*template_p=*/false,
10298                    /*done=*/true,
10299                    /*address_p=*/false,
10300                    /*template_arg_p=*/false,
10301                    &error_msg,
10302                    id_expr_start_token->location));
10303
10304           if (expr == error_mark_node)
10305             /* We found an id-expression, but it was something that we
10306                should not have found. This is an error, not something
10307                we can recover from, so note that we found an
10308                id-expression and we'll recover as gracefully as
10309                possible.  */
10310             id_expression_or_member_access_p = true;
10311         }
10312
10313       if (expr 
10314           && expr != error_mark_node
10315           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10316         /* We have an id-expression.  */
10317         id_expression_or_member_access_p = true;
10318     }
10319
10320   if (!id_expression_or_member_access_p)
10321     {
10322       /* Abort the id-expression parse.  */
10323       cp_parser_abort_tentative_parse (parser);
10324
10325       /* Parsing tentatively, again.  */
10326       cp_parser_parse_tentatively (parser);
10327
10328       /* Parse a class member access.  */
10329       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10330                                            /*cast_p=*/false,
10331                                            /*member_access_only_p=*/true, NULL);
10332
10333       if (expr 
10334           && expr != error_mark_node
10335           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10336         /* We have an id-expression.  */
10337         id_expression_or_member_access_p = true;
10338     }
10339
10340   if (id_expression_or_member_access_p)
10341     /* We have parsed the complete id-expression or member access.  */
10342     cp_parser_parse_definitely (parser);
10343   else
10344     {
10345       bool saved_greater_than_is_operator_p;
10346
10347       /* Abort our attempt to parse an id-expression or member access
10348          expression.  */
10349       cp_parser_abort_tentative_parse (parser);
10350
10351       /* Within a parenthesized expression, a `>' token is always
10352          the greater-than operator.  */
10353       saved_greater_than_is_operator_p
10354         = parser->greater_than_is_operator_p;
10355       parser->greater_than_is_operator_p = true;
10356
10357       /* Parse a full expression.  */
10358       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10359
10360       /* The `>' token might be the end of a template-id or
10361          template-parameter-list now.  */
10362       parser->greater_than_is_operator_p
10363         = saved_greater_than_is_operator_p;
10364     }
10365
10366   /* Go back to evaluating expressions.  */
10367   --cp_unevaluated_operand;
10368   --c_inhibit_evaluation_warnings;
10369
10370   /* Restore the old message and the integral constant expression
10371      flags.  */
10372   parser->type_definition_forbidden_message = saved_message;
10373   parser->integral_constant_expression_p
10374     = saved_integral_constant_expression_p;
10375   parser->non_integral_constant_expression_p
10376     = saved_non_integral_constant_expression_p;
10377
10378   if (expr == error_mark_node)
10379     {
10380       /* Skip everything up to the closing `)'.  */
10381       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10382                                              /*consume_paren=*/true);
10383       return error_mark_node;
10384     }
10385   
10386   /* Parse to the closing `)'.  */
10387   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10388     {
10389       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10390                                              /*consume_paren=*/true);
10391       return error_mark_node;
10392     }
10393
10394   return finish_decltype_type (expr, id_expression_or_member_access_p);
10395 }
10396
10397 /* Special member functions [gram.special] */
10398
10399 /* Parse a conversion-function-id.
10400
10401    conversion-function-id:
10402      operator conversion-type-id
10403
10404    Returns an IDENTIFIER_NODE representing the operator.  */
10405
10406 static tree
10407 cp_parser_conversion_function_id (cp_parser* parser)
10408 {
10409   tree type;
10410   tree saved_scope;
10411   tree saved_qualifying_scope;
10412   tree saved_object_scope;
10413   tree pushed_scope = NULL_TREE;
10414
10415   /* Look for the `operator' token.  */
10416   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10417     return error_mark_node;
10418   /* When we parse the conversion-type-id, the current scope will be
10419      reset.  However, we need that information in able to look up the
10420      conversion function later, so we save it here.  */
10421   saved_scope = parser->scope;
10422   saved_qualifying_scope = parser->qualifying_scope;
10423   saved_object_scope = parser->object_scope;
10424   /* We must enter the scope of the class so that the names of
10425      entities declared within the class are available in the
10426      conversion-type-id.  For example, consider:
10427
10428        struct S {
10429          typedef int I;
10430          operator I();
10431        };
10432
10433        S::operator I() { ... }
10434
10435      In order to see that `I' is a type-name in the definition, we
10436      must be in the scope of `S'.  */
10437   if (saved_scope)
10438     pushed_scope = push_scope (saved_scope);
10439   /* Parse the conversion-type-id.  */
10440   type = cp_parser_conversion_type_id (parser);
10441   /* Leave the scope of the class, if any.  */
10442   if (pushed_scope)
10443     pop_scope (pushed_scope);
10444   /* Restore the saved scope.  */
10445   parser->scope = saved_scope;
10446   parser->qualifying_scope = saved_qualifying_scope;
10447   parser->object_scope = saved_object_scope;
10448   /* If the TYPE is invalid, indicate failure.  */
10449   if (type == error_mark_node)
10450     return error_mark_node;
10451   return mangle_conv_op_name_for_type (type);
10452 }
10453
10454 /* Parse a conversion-type-id:
10455
10456    conversion-type-id:
10457      type-specifier-seq conversion-declarator [opt]
10458
10459    Returns the TYPE specified.  */
10460
10461 static tree
10462 cp_parser_conversion_type_id (cp_parser* parser)
10463 {
10464   tree attributes;
10465   cp_decl_specifier_seq type_specifiers;
10466   cp_declarator *declarator;
10467   tree type_specified;
10468
10469   /* Parse the attributes.  */
10470   attributes = cp_parser_attributes_opt (parser);
10471   /* Parse the type-specifiers.  */
10472   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10473                                 /*is_trailing_return=*/false,
10474                                 &type_specifiers);
10475   /* If that didn't work, stop.  */
10476   if (type_specifiers.type == error_mark_node)
10477     return error_mark_node;
10478   /* Parse the conversion-declarator.  */
10479   declarator = cp_parser_conversion_declarator_opt (parser);
10480
10481   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10482                                     /*initialized=*/0, &attributes);
10483   if (attributes)
10484     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10485
10486   /* Don't give this error when parsing tentatively.  This happens to
10487      work because we always parse this definitively once.  */
10488   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10489       && type_uses_auto (type_specified))
10490     {
10491       error ("invalid use of %<auto%> in conversion operator");
10492       return error_mark_node;
10493     }
10494
10495   return type_specified;
10496 }
10497
10498 /* Parse an (optional) conversion-declarator.
10499
10500    conversion-declarator:
10501      ptr-operator conversion-declarator [opt]
10502
10503    */
10504
10505 static cp_declarator *
10506 cp_parser_conversion_declarator_opt (cp_parser* parser)
10507 {
10508   enum tree_code code;
10509   tree class_type;
10510   cp_cv_quals cv_quals;
10511
10512   /* We don't know if there's a ptr-operator next, or not.  */
10513   cp_parser_parse_tentatively (parser);
10514   /* Try the ptr-operator.  */
10515   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10516   /* If it worked, look for more conversion-declarators.  */
10517   if (cp_parser_parse_definitely (parser))
10518     {
10519       cp_declarator *declarator;
10520
10521       /* Parse another optional declarator.  */
10522       declarator = cp_parser_conversion_declarator_opt (parser);
10523
10524       return cp_parser_make_indirect_declarator
10525         (code, class_type, cv_quals, declarator);
10526    }
10527
10528   return NULL;
10529 }
10530
10531 /* Parse an (optional) ctor-initializer.
10532
10533    ctor-initializer:
10534      : mem-initializer-list
10535
10536    Returns TRUE iff the ctor-initializer was actually present.  */
10537
10538 static bool
10539 cp_parser_ctor_initializer_opt (cp_parser* parser)
10540 {
10541   /* If the next token is not a `:', then there is no
10542      ctor-initializer.  */
10543   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10544     {
10545       /* Do default initialization of any bases and members.  */
10546       if (DECL_CONSTRUCTOR_P (current_function_decl))
10547         finish_mem_initializers (NULL_TREE);
10548
10549       return false;
10550     }
10551
10552   /* Consume the `:' token.  */
10553   cp_lexer_consume_token (parser->lexer);
10554   /* And the mem-initializer-list.  */
10555   cp_parser_mem_initializer_list (parser);
10556
10557   return true;
10558 }
10559
10560 /* Parse a mem-initializer-list.
10561
10562    mem-initializer-list:
10563      mem-initializer ... [opt]
10564      mem-initializer ... [opt] , mem-initializer-list  */
10565
10566 static void
10567 cp_parser_mem_initializer_list (cp_parser* parser)
10568 {
10569   tree mem_initializer_list = NULL_TREE;
10570   cp_token *token = cp_lexer_peek_token (parser->lexer);
10571
10572   /* Let the semantic analysis code know that we are starting the
10573      mem-initializer-list.  */
10574   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10575     error_at (token->location,
10576               "only constructors take member initializers");
10577
10578   /* Loop through the list.  */
10579   while (true)
10580     {
10581       tree mem_initializer;
10582
10583       token = cp_lexer_peek_token (parser->lexer);
10584       /* Parse the mem-initializer.  */
10585       mem_initializer = cp_parser_mem_initializer (parser);
10586       /* If the next token is a `...', we're expanding member initializers. */
10587       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10588         {
10589           /* Consume the `...'. */
10590           cp_lexer_consume_token (parser->lexer);
10591
10592           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10593              can be expanded but members cannot. */
10594           if (mem_initializer != error_mark_node
10595               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10596             {
10597               error_at (token->location,
10598                         "cannot expand initializer for member %<%D%>",
10599                         TREE_PURPOSE (mem_initializer));
10600               mem_initializer = error_mark_node;
10601             }
10602
10603           /* Construct the pack expansion type. */
10604           if (mem_initializer != error_mark_node)
10605             mem_initializer = make_pack_expansion (mem_initializer);
10606         }
10607       /* Add it to the list, unless it was erroneous.  */
10608       if (mem_initializer != error_mark_node)
10609         {
10610           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10611           mem_initializer_list = mem_initializer;
10612         }
10613       /* If the next token is not a `,', we're done.  */
10614       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10615         break;
10616       /* Consume the `,' token.  */
10617       cp_lexer_consume_token (parser->lexer);
10618     }
10619
10620   /* Perform semantic analysis.  */
10621   if (DECL_CONSTRUCTOR_P (current_function_decl))
10622     finish_mem_initializers (mem_initializer_list);
10623 }
10624
10625 /* Parse a mem-initializer.
10626
10627    mem-initializer:
10628      mem-initializer-id ( expression-list [opt] )
10629      mem-initializer-id braced-init-list
10630
10631    GNU extension:
10632
10633    mem-initializer:
10634      ( expression-list [opt] )
10635
10636    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10637    class) or FIELD_DECL (for a non-static data member) to initialize;
10638    the TREE_VALUE is the expression-list.  An empty initialization
10639    list is represented by void_list_node.  */
10640
10641 static tree
10642 cp_parser_mem_initializer (cp_parser* parser)
10643 {
10644   tree mem_initializer_id;
10645   tree expression_list;
10646   tree member;
10647   cp_token *token = cp_lexer_peek_token (parser->lexer);
10648
10649   /* Find out what is being initialized.  */
10650   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10651     {
10652       permerror (token->location,
10653                  "anachronistic old-style base class initializer");
10654       mem_initializer_id = NULL_TREE;
10655     }
10656   else
10657     {
10658       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10659       if (mem_initializer_id == error_mark_node)
10660         return mem_initializer_id;
10661     }
10662   member = expand_member_init (mem_initializer_id);
10663   if (member && !DECL_P (member))
10664     in_base_initializer = 1;
10665
10666   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10667     {
10668       bool expr_non_constant_p;
10669       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10670       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10671       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10672       expression_list = build_tree_list (NULL_TREE, expression_list);
10673     }
10674   else
10675     {
10676       VEC(tree,gc)* vec;
10677       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10678                                                      /*cast_p=*/false,
10679                                                      /*allow_expansion_p=*/true,
10680                                                      /*non_constant_p=*/NULL);
10681       if (vec == NULL)
10682         return error_mark_node;
10683       expression_list = build_tree_list_vec (vec);
10684       release_tree_vector (vec);
10685     }
10686
10687   if (expression_list == error_mark_node)
10688     return error_mark_node;
10689   if (!expression_list)
10690     expression_list = void_type_node;
10691
10692   in_base_initializer = 0;
10693
10694   return member ? build_tree_list (member, expression_list) : error_mark_node;
10695 }
10696
10697 /* Parse a mem-initializer-id.
10698
10699    mem-initializer-id:
10700      :: [opt] nested-name-specifier [opt] class-name
10701      identifier
10702
10703    Returns a TYPE indicating the class to be initializer for the first
10704    production.  Returns an IDENTIFIER_NODE indicating the data member
10705    to be initialized for the second production.  */
10706
10707 static tree
10708 cp_parser_mem_initializer_id (cp_parser* parser)
10709 {
10710   bool global_scope_p;
10711   bool nested_name_specifier_p;
10712   bool template_p = false;
10713   tree id;
10714
10715   cp_token *token = cp_lexer_peek_token (parser->lexer);
10716
10717   /* `typename' is not allowed in this context ([temp.res]).  */
10718   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10719     {
10720       error_at (token->location, 
10721                 "keyword %<typename%> not allowed in this context (a qualified "
10722                 "member initializer is implicitly a type)");
10723       cp_lexer_consume_token (parser->lexer);
10724     }
10725   /* Look for the optional `::' operator.  */
10726   global_scope_p
10727     = (cp_parser_global_scope_opt (parser,
10728                                    /*current_scope_valid_p=*/false)
10729        != NULL_TREE);
10730   /* Look for the optional nested-name-specifier.  The simplest way to
10731      implement:
10732
10733        [temp.res]
10734
10735        The keyword `typename' is not permitted in a base-specifier or
10736        mem-initializer; in these contexts a qualified name that
10737        depends on a template-parameter is implicitly assumed to be a
10738        type name.
10739
10740      is to assume that we have seen the `typename' keyword at this
10741      point.  */
10742   nested_name_specifier_p
10743     = (cp_parser_nested_name_specifier_opt (parser,
10744                                             /*typename_keyword_p=*/true,
10745                                             /*check_dependency_p=*/true,
10746                                             /*type_p=*/true,
10747                                             /*is_declaration=*/true)
10748        != NULL_TREE);
10749   if (nested_name_specifier_p)
10750     template_p = cp_parser_optional_template_keyword (parser);
10751   /* If there is a `::' operator or a nested-name-specifier, then we
10752      are definitely looking for a class-name.  */
10753   if (global_scope_p || nested_name_specifier_p)
10754     return cp_parser_class_name (parser,
10755                                  /*typename_keyword_p=*/true,
10756                                  /*template_keyword_p=*/template_p,
10757                                  typename_type,
10758                                  /*check_dependency_p=*/true,
10759                                  /*class_head_p=*/false,
10760                                  /*is_declaration=*/true);
10761   /* Otherwise, we could also be looking for an ordinary identifier.  */
10762   cp_parser_parse_tentatively (parser);
10763   /* Try a class-name.  */
10764   id = cp_parser_class_name (parser,
10765                              /*typename_keyword_p=*/true,
10766                              /*template_keyword_p=*/false,
10767                              none_type,
10768                              /*check_dependency_p=*/true,
10769                              /*class_head_p=*/false,
10770                              /*is_declaration=*/true);
10771   /* If we found one, we're done.  */
10772   if (cp_parser_parse_definitely (parser))
10773     return id;
10774   /* Otherwise, look for an ordinary identifier.  */
10775   return cp_parser_identifier (parser);
10776 }
10777
10778 /* Overloading [gram.over] */
10779
10780 /* Parse an operator-function-id.
10781
10782    operator-function-id:
10783      operator operator
10784
10785    Returns an IDENTIFIER_NODE for the operator which is a
10786    human-readable spelling of the identifier, e.g., `operator +'.  */
10787
10788 static tree
10789 cp_parser_operator_function_id (cp_parser* parser)
10790 {
10791   /* Look for the `operator' keyword.  */
10792   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10793     return error_mark_node;
10794   /* And then the name of the operator itself.  */
10795   return cp_parser_operator (parser);
10796 }
10797
10798 /* Parse an operator.
10799
10800    operator:
10801      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10802      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10803      || ++ -- , ->* -> () []
10804
10805    GNU Extensions:
10806
10807    operator:
10808      <? >? <?= >?=
10809
10810    Returns an IDENTIFIER_NODE for the operator which is a
10811    human-readable spelling of the identifier, e.g., `operator +'.  */
10812
10813 static tree
10814 cp_parser_operator (cp_parser* parser)
10815 {
10816   tree id = NULL_TREE;
10817   cp_token *token;
10818
10819   /* Peek at the next token.  */
10820   token = cp_lexer_peek_token (parser->lexer);
10821   /* Figure out which operator we have.  */
10822   switch (token->type)
10823     {
10824     case CPP_KEYWORD:
10825       {
10826         enum tree_code op;
10827
10828         /* The keyword should be either `new' or `delete'.  */
10829         if (token->keyword == RID_NEW)
10830           op = NEW_EXPR;
10831         else if (token->keyword == RID_DELETE)
10832           op = DELETE_EXPR;
10833         else
10834           break;
10835
10836         /* Consume the `new' or `delete' token.  */
10837         cp_lexer_consume_token (parser->lexer);
10838
10839         /* Peek at the next token.  */
10840         token = cp_lexer_peek_token (parser->lexer);
10841         /* If it's a `[' token then this is the array variant of the
10842            operator.  */
10843         if (token->type == CPP_OPEN_SQUARE)
10844           {
10845             /* Consume the `[' token.  */
10846             cp_lexer_consume_token (parser->lexer);
10847             /* Look for the `]' token.  */
10848             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10849             id = ansi_opname (op == NEW_EXPR
10850                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10851           }
10852         /* Otherwise, we have the non-array variant.  */
10853         else
10854           id = ansi_opname (op);
10855
10856         return id;
10857       }
10858
10859     case CPP_PLUS:
10860       id = ansi_opname (PLUS_EXPR);
10861       break;
10862
10863     case CPP_MINUS:
10864       id = ansi_opname (MINUS_EXPR);
10865       break;
10866
10867     case CPP_MULT:
10868       id = ansi_opname (MULT_EXPR);
10869       break;
10870
10871     case CPP_DIV:
10872       id = ansi_opname (TRUNC_DIV_EXPR);
10873       break;
10874
10875     case CPP_MOD:
10876       id = ansi_opname (TRUNC_MOD_EXPR);
10877       break;
10878
10879     case CPP_XOR:
10880       id = ansi_opname (BIT_XOR_EXPR);
10881       break;
10882
10883     case CPP_AND:
10884       id = ansi_opname (BIT_AND_EXPR);
10885       break;
10886
10887     case CPP_OR:
10888       id = ansi_opname (BIT_IOR_EXPR);
10889       break;
10890
10891     case CPP_COMPL:
10892       id = ansi_opname (BIT_NOT_EXPR);
10893       break;
10894
10895     case CPP_NOT:
10896       id = ansi_opname (TRUTH_NOT_EXPR);
10897       break;
10898
10899     case CPP_EQ:
10900       id = ansi_assopname (NOP_EXPR);
10901       break;
10902
10903     case CPP_LESS:
10904       id = ansi_opname (LT_EXPR);
10905       break;
10906
10907     case CPP_GREATER:
10908       id = ansi_opname (GT_EXPR);
10909       break;
10910
10911     case CPP_PLUS_EQ:
10912       id = ansi_assopname (PLUS_EXPR);
10913       break;
10914
10915     case CPP_MINUS_EQ:
10916       id = ansi_assopname (MINUS_EXPR);
10917       break;
10918
10919     case CPP_MULT_EQ:
10920       id = ansi_assopname (MULT_EXPR);
10921       break;
10922
10923     case CPP_DIV_EQ:
10924       id = ansi_assopname (TRUNC_DIV_EXPR);
10925       break;
10926
10927     case CPP_MOD_EQ:
10928       id = ansi_assopname (TRUNC_MOD_EXPR);
10929       break;
10930
10931     case CPP_XOR_EQ:
10932       id = ansi_assopname (BIT_XOR_EXPR);
10933       break;
10934
10935     case CPP_AND_EQ:
10936       id = ansi_assopname (BIT_AND_EXPR);
10937       break;
10938
10939     case CPP_OR_EQ:
10940       id = ansi_assopname (BIT_IOR_EXPR);
10941       break;
10942
10943     case CPP_LSHIFT:
10944       id = ansi_opname (LSHIFT_EXPR);
10945       break;
10946
10947     case CPP_RSHIFT:
10948       id = ansi_opname (RSHIFT_EXPR);
10949       break;
10950
10951     case CPP_LSHIFT_EQ:
10952       id = ansi_assopname (LSHIFT_EXPR);
10953       break;
10954
10955     case CPP_RSHIFT_EQ:
10956       id = ansi_assopname (RSHIFT_EXPR);
10957       break;
10958
10959     case CPP_EQ_EQ:
10960       id = ansi_opname (EQ_EXPR);
10961       break;
10962
10963     case CPP_NOT_EQ:
10964       id = ansi_opname (NE_EXPR);
10965       break;
10966
10967     case CPP_LESS_EQ:
10968       id = ansi_opname (LE_EXPR);
10969       break;
10970
10971     case CPP_GREATER_EQ:
10972       id = ansi_opname (GE_EXPR);
10973       break;
10974
10975     case CPP_AND_AND:
10976       id = ansi_opname (TRUTH_ANDIF_EXPR);
10977       break;
10978
10979     case CPP_OR_OR:
10980       id = ansi_opname (TRUTH_ORIF_EXPR);
10981       break;
10982
10983     case CPP_PLUS_PLUS:
10984       id = ansi_opname (POSTINCREMENT_EXPR);
10985       break;
10986
10987     case CPP_MINUS_MINUS:
10988       id = ansi_opname (PREDECREMENT_EXPR);
10989       break;
10990
10991     case CPP_COMMA:
10992       id = ansi_opname (COMPOUND_EXPR);
10993       break;
10994
10995     case CPP_DEREF_STAR:
10996       id = ansi_opname (MEMBER_REF);
10997       break;
10998
10999     case CPP_DEREF:
11000       id = ansi_opname (COMPONENT_REF);
11001       break;
11002
11003     case CPP_OPEN_PAREN:
11004       /* Consume the `('.  */
11005       cp_lexer_consume_token (parser->lexer);
11006       /* Look for the matching `)'.  */
11007       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11008       return ansi_opname (CALL_EXPR);
11009
11010     case CPP_OPEN_SQUARE:
11011       /* Consume the `['.  */
11012       cp_lexer_consume_token (parser->lexer);
11013       /* Look for the matching `]'.  */
11014       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11015       return ansi_opname (ARRAY_REF);
11016
11017     default:
11018       /* Anything else is an error.  */
11019       break;
11020     }
11021
11022   /* If we have selected an identifier, we need to consume the
11023      operator token.  */
11024   if (id)
11025     cp_lexer_consume_token (parser->lexer);
11026   /* Otherwise, no valid operator name was present.  */
11027   else
11028     {
11029       cp_parser_error (parser, "expected operator");
11030       id = error_mark_node;
11031     }
11032
11033   return id;
11034 }
11035
11036 /* Parse a template-declaration.
11037
11038    template-declaration:
11039      export [opt] template < template-parameter-list > declaration
11040
11041    If MEMBER_P is TRUE, this template-declaration occurs within a
11042    class-specifier.
11043
11044    The grammar rule given by the standard isn't correct.  What
11045    is really meant is:
11046
11047    template-declaration:
11048      export [opt] template-parameter-list-seq
11049        decl-specifier-seq [opt] init-declarator [opt] ;
11050      export [opt] template-parameter-list-seq
11051        function-definition
11052
11053    template-parameter-list-seq:
11054      template-parameter-list-seq [opt]
11055      template < template-parameter-list >  */
11056
11057 static void
11058 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11059 {
11060   /* Check for `export'.  */
11061   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11062     {
11063       /* Consume the `export' token.  */
11064       cp_lexer_consume_token (parser->lexer);
11065       /* Warn that we do not support `export'.  */
11066       warning (0, "keyword %<export%> not implemented, and will be ignored");
11067     }
11068
11069   cp_parser_template_declaration_after_export (parser, member_p);
11070 }
11071
11072 /* Parse a template-parameter-list.
11073
11074    template-parameter-list:
11075      template-parameter
11076      template-parameter-list , template-parameter
11077
11078    Returns a TREE_LIST.  Each node represents a template parameter.
11079    The nodes are connected via their TREE_CHAINs.  */
11080
11081 static tree
11082 cp_parser_template_parameter_list (cp_parser* parser)
11083 {
11084   tree parameter_list = NULL_TREE;
11085
11086   begin_template_parm_list ();
11087
11088   /* The loop below parses the template parms.  We first need to know
11089      the total number of template parms to be able to compute proper
11090      canonical types of each dependent type. So after the loop, when
11091      we know the total number of template parms,
11092      end_template_parm_list computes the proper canonical types and
11093      fixes up the dependent types accordingly.  */
11094   while (true)
11095     {
11096       tree parameter;
11097       bool is_non_type;
11098       bool is_parameter_pack;
11099       location_t parm_loc;
11100
11101       /* Parse the template-parameter.  */
11102       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11103       parameter = cp_parser_template_parameter (parser, 
11104                                                 &is_non_type,
11105                                                 &is_parameter_pack);
11106       /* Add it to the list.  */
11107       if (parameter != error_mark_node)
11108         parameter_list = process_template_parm (parameter_list,
11109                                                 parm_loc,
11110                                                 parameter,
11111                                                 is_non_type,
11112                                                 is_parameter_pack,
11113                                                 0);
11114       else
11115        {
11116          tree err_parm = build_tree_list (parameter, parameter);
11117          parameter_list = chainon (parameter_list, err_parm);
11118        }
11119
11120       /* If the next token is not a `,', we're done.  */
11121       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11122         break;
11123       /* Otherwise, consume the `,' token.  */
11124       cp_lexer_consume_token (parser->lexer);
11125     }
11126
11127   return end_template_parm_list (parameter_list);
11128 }
11129
11130 /* Parse a template-parameter.
11131
11132    template-parameter:
11133      type-parameter
11134      parameter-declaration
11135
11136    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11137    the parameter.  The TREE_PURPOSE is the default value, if any.
11138    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11139    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11140    set to true iff this parameter is a parameter pack. */
11141
11142 static tree
11143 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11144                               bool *is_parameter_pack)
11145 {
11146   cp_token *token;
11147   cp_parameter_declarator *parameter_declarator;
11148   cp_declarator *id_declarator;
11149   tree parm;
11150
11151   /* Assume it is a type parameter or a template parameter.  */
11152   *is_non_type = false;
11153   /* Assume it not a parameter pack. */
11154   *is_parameter_pack = false;
11155   /* Peek at the next token.  */
11156   token = cp_lexer_peek_token (parser->lexer);
11157   /* If it is `class' or `template', we have a type-parameter.  */
11158   if (token->keyword == RID_TEMPLATE)
11159     return cp_parser_type_parameter (parser, is_parameter_pack);
11160   /* If it is `class' or `typename' we do not know yet whether it is a
11161      type parameter or a non-type parameter.  Consider:
11162
11163        template <typename T, typename T::X X> ...
11164
11165      or:
11166
11167        template <class C, class D*> ...
11168
11169      Here, the first parameter is a type parameter, and the second is
11170      a non-type parameter.  We can tell by looking at the token after
11171      the identifier -- if it is a `,', `=', or `>' then we have a type
11172      parameter.  */
11173   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11174     {
11175       /* Peek at the token after `class' or `typename'.  */
11176       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11177       /* If it's an ellipsis, we have a template type parameter
11178          pack. */
11179       if (token->type == CPP_ELLIPSIS)
11180         return cp_parser_type_parameter (parser, is_parameter_pack);
11181       /* If it's an identifier, skip it.  */
11182       if (token->type == CPP_NAME)
11183         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11184       /* Now, see if the token looks like the end of a template
11185          parameter.  */
11186       if (token->type == CPP_COMMA
11187           || token->type == CPP_EQ
11188           || token->type == CPP_GREATER)
11189         return cp_parser_type_parameter (parser, is_parameter_pack);
11190     }
11191
11192   /* Otherwise, it is a non-type parameter.
11193
11194      [temp.param]
11195
11196      When parsing a default template-argument for a non-type
11197      template-parameter, the first non-nested `>' is taken as the end
11198      of the template parameter-list rather than a greater-than
11199      operator.  */
11200   *is_non_type = true;
11201   parameter_declarator
11202      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11203                                         /*parenthesized_p=*/NULL);
11204
11205   /* If the parameter declaration is marked as a parameter pack, set
11206      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11207      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11208      grokdeclarator. */
11209   if (parameter_declarator
11210       && parameter_declarator->declarator
11211       && parameter_declarator->declarator->parameter_pack_p)
11212     {
11213       *is_parameter_pack = true;
11214       parameter_declarator->declarator->parameter_pack_p = false;
11215     }
11216
11217   /* If the next token is an ellipsis, and we don't already have it
11218      marked as a parameter pack, then we have a parameter pack (that
11219      has no declarator).  */
11220   if (!*is_parameter_pack
11221       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11222       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11223     {
11224       /* Consume the `...'.  */
11225       cp_lexer_consume_token (parser->lexer);
11226       maybe_warn_variadic_templates ();
11227       
11228       *is_parameter_pack = true;
11229     }
11230   /* We might end up with a pack expansion as the type of the non-type
11231      template parameter, in which case this is a non-type template
11232      parameter pack.  */
11233   else if (parameter_declarator
11234            && parameter_declarator->decl_specifiers.type
11235            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11236     {
11237       *is_parameter_pack = true;
11238       parameter_declarator->decl_specifiers.type = 
11239         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11240     }
11241
11242   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11243     {
11244       /* Parameter packs cannot have default arguments.  However, a
11245          user may try to do so, so we'll parse them and give an
11246          appropriate diagnostic here.  */
11247
11248       /* Consume the `='.  */
11249       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11250       cp_lexer_consume_token (parser->lexer);
11251       
11252       /* Find the name of the parameter pack.  */     
11253       id_declarator = parameter_declarator->declarator;
11254       while (id_declarator && id_declarator->kind != cdk_id)
11255         id_declarator = id_declarator->declarator;
11256       
11257       if (id_declarator && id_declarator->kind == cdk_id)
11258         error_at (start_token->location,
11259                   "template parameter pack %qD cannot have a default argument",
11260                   id_declarator->u.id.unqualified_name);
11261       else
11262         error_at (start_token->location,
11263                   "template parameter pack cannot have a default argument");
11264       
11265       /* Parse the default argument, but throw away the result.  */
11266       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11267     }
11268
11269   parm = grokdeclarator (parameter_declarator->declarator,
11270                          &parameter_declarator->decl_specifiers,
11271                          TPARM, /*initialized=*/0,
11272                          /*attrlist=*/NULL);
11273   if (parm == error_mark_node)
11274     return error_mark_node;
11275
11276   return build_tree_list (parameter_declarator->default_argument, parm);
11277 }
11278
11279 /* Parse a type-parameter.
11280
11281    type-parameter:
11282      class identifier [opt]
11283      class identifier [opt] = type-id
11284      typename identifier [opt]
11285      typename identifier [opt] = type-id
11286      template < template-parameter-list > class identifier [opt]
11287      template < template-parameter-list > class identifier [opt]
11288        = id-expression
11289
11290    GNU Extension (variadic templates):
11291
11292    type-parameter:
11293      class ... identifier [opt]
11294      typename ... identifier [opt]
11295
11296    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11297    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11298    the declaration of the parameter.
11299
11300    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11301
11302 static tree
11303 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11304 {
11305   cp_token *token;
11306   tree parameter;
11307
11308   /* Look for a keyword to tell us what kind of parameter this is.  */
11309   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11310   if (!token)
11311     return error_mark_node;
11312
11313   switch (token->keyword)
11314     {
11315     case RID_CLASS:
11316     case RID_TYPENAME:
11317       {
11318         tree identifier;
11319         tree default_argument;
11320
11321         /* If the next token is an ellipsis, we have a template
11322            argument pack. */
11323         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11324           {
11325             /* Consume the `...' token. */
11326             cp_lexer_consume_token (parser->lexer);
11327             maybe_warn_variadic_templates ();
11328
11329             *is_parameter_pack = true;
11330           }
11331
11332         /* If the next token is an identifier, then it names the
11333            parameter.  */
11334         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11335           identifier = cp_parser_identifier (parser);
11336         else
11337           identifier = NULL_TREE;
11338
11339         /* Create the parameter.  */
11340         parameter = finish_template_type_parm (class_type_node, identifier);
11341
11342         /* If the next token is an `=', we have a default argument.  */
11343         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11344           {
11345             /* Consume the `=' token.  */
11346             cp_lexer_consume_token (parser->lexer);
11347             /* Parse the default-argument.  */
11348             push_deferring_access_checks (dk_no_deferred);
11349             default_argument = cp_parser_type_id (parser);
11350
11351             /* Template parameter packs cannot have default
11352                arguments. */
11353             if (*is_parameter_pack)
11354               {
11355                 if (identifier)
11356                   error_at (token->location,
11357                             "template parameter pack %qD cannot have a "
11358                             "default argument", identifier);
11359                 else
11360                   error_at (token->location,
11361                             "template parameter packs cannot have "
11362                             "default arguments");
11363                 default_argument = NULL_TREE;
11364               }
11365             pop_deferring_access_checks ();
11366           }
11367         else
11368           default_argument = NULL_TREE;
11369
11370         /* Create the combined representation of the parameter and the
11371            default argument.  */
11372         parameter = build_tree_list (default_argument, parameter);
11373       }
11374       break;
11375
11376     case RID_TEMPLATE:
11377       {
11378         tree identifier;
11379         tree default_argument;
11380
11381         /* Look for the `<'.  */
11382         cp_parser_require (parser, CPP_LESS, RT_LESS);
11383         /* Parse the template-parameter-list.  */
11384         cp_parser_template_parameter_list (parser);
11385         /* Look for the `>'.  */
11386         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11387         /* Look for the `class' keyword.  */
11388         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11389         /* If the next token is an ellipsis, we have a template
11390            argument pack. */
11391         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11392           {
11393             /* Consume the `...' token. */
11394             cp_lexer_consume_token (parser->lexer);
11395             maybe_warn_variadic_templates ();
11396
11397             *is_parameter_pack = true;
11398           }
11399         /* If the next token is an `=', then there is a
11400            default-argument.  If the next token is a `>', we are at
11401            the end of the parameter-list.  If the next token is a `,',
11402            then we are at the end of this parameter.  */
11403         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11404             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11405             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11406           {
11407             identifier = cp_parser_identifier (parser);
11408             /* Treat invalid names as if the parameter were nameless.  */
11409             if (identifier == error_mark_node)
11410               identifier = NULL_TREE;
11411           }
11412         else
11413           identifier = NULL_TREE;
11414
11415         /* Create the template parameter.  */
11416         parameter = finish_template_template_parm (class_type_node,
11417                                                    identifier);
11418
11419         /* If the next token is an `=', then there is a
11420            default-argument.  */
11421         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11422           {
11423             bool is_template;
11424
11425             /* Consume the `='.  */
11426             cp_lexer_consume_token (parser->lexer);
11427             /* Parse the id-expression.  */
11428             push_deferring_access_checks (dk_no_deferred);
11429             /* save token before parsing the id-expression, for error
11430                reporting */
11431             token = cp_lexer_peek_token (parser->lexer);
11432             default_argument
11433               = cp_parser_id_expression (parser,
11434                                          /*template_keyword_p=*/false,
11435                                          /*check_dependency_p=*/true,
11436                                          /*template_p=*/&is_template,
11437                                          /*declarator_p=*/false,
11438                                          /*optional_p=*/false);
11439             if (TREE_CODE (default_argument) == TYPE_DECL)
11440               /* If the id-expression was a template-id that refers to
11441                  a template-class, we already have the declaration here,
11442                  so no further lookup is needed.  */
11443                  ;
11444             else
11445               /* Look up the name.  */
11446               default_argument
11447                 = cp_parser_lookup_name (parser, default_argument,
11448                                          none_type,
11449                                          /*is_template=*/is_template,
11450                                          /*is_namespace=*/false,
11451                                          /*check_dependency=*/true,
11452                                          /*ambiguous_decls=*/NULL,
11453                                          token->location);
11454             /* See if the default argument is valid.  */
11455             default_argument
11456               = check_template_template_default_arg (default_argument);
11457
11458             /* Template parameter packs cannot have default
11459                arguments. */
11460             if (*is_parameter_pack)
11461               {
11462                 if (identifier)
11463                   error_at (token->location,
11464                             "template parameter pack %qD cannot "
11465                             "have a default argument",
11466                             identifier);
11467                 else
11468                   error_at (token->location, "template parameter packs cannot "
11469                             "have default arguments");
11470                 default_argument = NULL_TREE;
11471               }
11472             pop_deferring_access_checks ();
11473           }
11474         else
11475           default_argument = NULL_TREE;
11476
11477         /* Create the combined representation of the parameter and the
11478            default argument.  */
11479         parameter = build_tree_list (default_argument, parameter);
11480       }
11481       break;
11482
11483     default:
11484       gcc_unreachable ();
11485       break;
11486     }
11487
11488   return parameter;
11489 }
11490
11491 /* Parse a template-id.
11492
11493    template-id:
11494      template-name < template-argument-list [opt] >
11495
11496    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11497    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11498    returned.  Otherwise, if the template-name names a function, or set
11499    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11500    names a class, returns a TYPE_DECL for the specialization.
11501
11502    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11503    uninstantiated templates.  */
11504
11505 static tree
11506 cp_parser_template_id (cp_parser *parser,
11507                        bool template_keyword_p,
11508                        bool check_dependency_p,
11509                        bool is_declaration)
11510 {
11511   int i;
11512   tree templ;
11513   tree arguments;
11514   tree template_id;
11515   cp_token_position start_of_id = 0;
11516   deferred_access_check *chk;
11517   VEC (deferred_access_check,gc) *access_check;
11518   cp_token *next_token = NULL, *next_token_2 = NULL;
11519   bool is_identifier;
11520
11521   /* If the next token corresponds to a template-id, there is no need
11522      to reparse it.  */
11523   next_token = cp_lexer_peek_token (parser->lexer);
11524   if (next_token->type == CPP_TEMPLATE_ID)
11525     {
11526       struct tree_check *check_value;
11527
11528       /* Get the stored value.  */
11529       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11530       /* Perform any access checks that were deferred.  */
11531       access_check = check_value->checks;
11532       if (access_check)
11533         {
11534           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11535             perform_or_defer_access_check (chk->binfo,
11536                                            chk->decl,
11537                                            chk->diag_decl);
11538         }
11539       /* Return the stored value.  */
11540       return check_value->value;
11541     }
11542
11543   /* Avoid performing name lookup if there is no possibility of
11544      finding a template-id.  */
11545   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11546       || (next_token->type == CPP_NAME
11547           && !cp_parser_nth_token_starts_template_argument_list_p
11548                (parser, 2)))
11549     {
11550       cp_parser_error (parser, "expected template-id");
11551       return error_mark_node;
11552     }
11553
11554   /* Remember where the template-id starts.  */
11555   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11556     start_of_id = cp_lexer_token_position (parser->lexer, false);
11557
11558   push_deferring_access_checks (dk_deferred);
11559
11560   /* Parse the template-name.  */
11561   is_identifier = false;
11562   templ = cp_parser_template_name (parser, template_keyword_p,
11563                                    check_dependency_p,
11564                                    is_declaration,
11565                                    &is_identifier);
11566   if (templ == error_mark_node || is_identifier)
11567     {
11568       pop_deferring_access_checks ();
11569       return templ;
11570     }
11571
11572   /* If we find the sequence `[:' after a template-name, it's probably
11573      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11574      parse correctly the argument list.  */
11575   next_token = cp_lexer_peek_token (parser->lexer);
11576   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11577   if (next_token->type == CPP_OPEN_SQUARE
11578       && next_token->flags & DIGRAPH
11579       && next_token_2->type == CPP_COLON
11580       && !(next_token_2->flags & PREV_WHITE))
11581     {
11582       cp_parser_parse_tentatively (parser);
11583       /* Change `:' into `::'.  */
11584       next_token_2->type = CPP_SCOPE;
11585       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11586          CPP_LESS.  */
11587       cp_lexer_consume_token (parser->lexer);
11588
11589       /* Parse the arguments.  */
11590       arguments = cp_parser_enclosed_template_argument_list (parser);
11591       if (!cp_parser_parse_definitely (parser))
11592         {
11593           /* If we couldn't parse an argument list, then we revert our changes
11594              and return simply an error. Maybe this is not a template-id
11595              after all.  */
11596           next_token_2->type = CPP_COLON;
11597           cp_parser_error (parser, "expected %<<%>");
11598           pop_deferring_access_checks ();
11599           return error_mark_node;
11600         }
11601       /* Otherwise, emit an error about the invalid digraph, but continue
11602          parsing because we got our argument list.  */
11603       if (permerror (next_token->location,
11604                      "%<<::%> cannot begin a template-argument list"))
11605         {
11606           static bool hint = false;
11607           inform (next_token->location,
11608                   "%<<:%> is an alternate spelling for %<[%>."
11609                   " Insert whitespace between %<<%> and %<::%>");
11610           if (!hint && !flag_permissive)
11611             {
11612               inform (next_token->location, "(if you use %<-fpermissive%>"
11613                       " G++ will accept your code)");
11614               hint = true;
11615             }
11616         }
11617     }
11618   else
11619     {
11620       /* Look for the `<' that starts the template-argument-list.  */
11621       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11622         {
11623           pop_deferring_access_checks ();
11624           return error_mark_node;
11625         }
11626       /* Parse the arguments.  */
11627       arguments = cp_parser_enclosed_template_argument_list (parser);
11628     }
11629
11630   /* Build a representation of the specialization.  */
11631   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11632     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11633   else if (DECL_CLASS_TEMPLATE_P (templ)
11634            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11635     {
11636       bool entering_scope;
11637       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11638          template (rather than some instantiation thereof) only if
11639          is not nested within some other construct.  For example, in
11640          "template <typename T> void f(T) { A<T>::", A<T> is just an
11641          instantiation of A.  */
11642       entering_scope = (template_parm_scope_p ()
11643                         && cp_lexer_next_token_is (parser->lexer,
11644                                                    CPP_SCOPE));
11645       template_id
11646         = finish_template_type (templ, arguments, entering_scope);
11647     }
11648   else
11649     {
11650       /* If it's not a class-template or a template-template, it should be
11651          a function-template.  */
11652       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11653                    || TREE_CODE (templ) == OVERLOAD
11654                    || BASELINK_P (templ)));
11655
11656       template_id = lookup_template_function (templ, arguments);
11657     }
11658
11659   /* If parsing tentatively, replace the sequence of tokens that makes
11660      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11661      should we re-parse the token stream, we will not have to repeat
11662      the effort required to do the parse, nor will we issue duplicate
11663      error messages about problems during instantiation of the
11664      template.  */
11665   if (start_of_id)
11666     {
11667       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11668
11669       /* Reset the contents of the START_OF_ID token.  */
11670       token->type = CPP_TEMPLATE_ID;
11671       /* Retrieve any deferred checks.  Do not pop this access checks yet
11672          so the memory will not be reclaimed during token replacing below.  */
11673       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11674       token->u.tree_check_value->value = template_id;
11675       token->u.tree_check_value->checks = get_deferred_access_checks ();
11676       token->keyword = RID_MAX;
11677
11678       /* Purge all subsequent tokens.  */
11679       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11680
11681       /* ??? Can we actually assume that, if template_id ==
11682          error_mark_node, we will have issued a diagnostic to the
11683          user, as opposed to simply marking the tentative parse as
11684          failed?  */
11685       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11686         error_at (token->location, "parse error in template argument list");
11687     }
11688
11689   pop_deferring_access_checks ();
11690   return template_id;
11691 }
11692
11693 /* Parse a template-name.
11694
11695    template-name:
11696      identifier
11697
11698    The standard should actually say:
11699
11700    template-name:
11701      identifier
11702      operator-function-id
11703
11704    A defect report has been filed about this issue.
11705
11706    A conversion-function-id cannot be a template name because they cannot
11707    be part of a template-id. In fact, looking at this code:
11708
11709    a.operator K<int>()
11710
11711    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11712    It is impossible to call a templated conversion-function-id with an
11713    explicit argument list, since the only allowed template parameter is
11714    the type to which it is converting.
11715
11716    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11717    `template' keyword, in a construction like:
11718
11719      T::template f<3>()
11720
11721    In that case `f' is taken to be a template-name, even though there
11722    is no way of knowing for sure.
11723
11724    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11725    name refers to a set of overloaded functions, at least one of which
11726    is a template, or an IDENTIFIER_NODE with the name of the template,
11727    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11728    names are looked up inside uninstantiated templates.  */
11729
11730 static tree
11731 cp_parser_template_name (cp_parser* parser,
11732                          bool template_keyword_p,
11733                          bool check_dependency_p,
11734                          bool is_declaration,
11735                          bool *is_identifier)
11736 {
11737   tree identifier;
11738   tree decl;
11739   tree fns;
11740   cp_token *token = cp_lexer_peek_token (parser->lexer);
11741
11742   /* If the next token is `operator', then we have either an
11743      operator-function-id or a conversion-function-id.  */
11744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11745     {
11746       /* We don't know whether we're looking at an
11747          operator-function-id or a conversion-function-id.  */
11748       cp_parser_parse_tentatively (parser);
11749       /* Try an operator-function-id.  */
11750       identifier = cp_parser_operator_function_id (parser);
11751       /* If that didn't work, try a conversion-function-id.  */
11752       if (!cp_parser_parse_definitely (parser))
11753         {
11754           cp_parser_error (parser, "expected template-name");
11755           return error_mark_node;
11756         }
11757     }
11758   /* Look for the identifier.  */
11759   else
11760     identifier = cp_parser_identifier (parser);
11761
11762   /* If we didn't find an identifier, we don't have a template-id.  */
11763   if (identifier == error_mark_node)
11764     return error_mark_node;
11765
11766   /* If the name immediately followed the `template' keyword, then it
11767      is a template-name.  However, if the next token is not `<', then
11768      we do not treat it as a template-name, since it is not being used
11769      as part of a template-id.  This enables us to handle constructs
11770      like:
11771
11772        template <typename T> struct S { S(); };
11773        template <typename T> S<T>::S();
11774
11775      correctly.  We would treat `S' as a template -- if it were `S<T>'
11776      -- but we do not if there is no `<'.  */
11777
11778   if (processing_template_decl
11779       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11780     {
11781       /* In a declaration, in a dependent context, we pretend that the
11782          "template" keyword was present in order to improve error
11783          recovery.  For example, given:
11784
11785            template <typename T> void f(T::X<int>);
11786
11787          we want to treat "X<int>" as a template-id.  */
11788       if (is_declaration
11789           && !template_keyword_p
11790           && parser->scope && TYPE_P (parser->scope)
11791           && check_dependency_p
11792           && dependent_scope_p (parser->scope)
11793           /* Do not do this for dtors (or ctors), since they never
11794              need the template keyword before their name.  */
11795           && !constructor_name_p (identifier, parser->scope))
11796         {
11797           cp_token_position start = 0;
11798
11799           /* Explain what went wrong.  */
11800           error_at (token->location, "non-template %qD used as template",
11801                     identifier);
11802           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11803                   parser->scope, identifier);
11804           /* If parsing tentatively, find the location of the "<" token.  */
11805           if (cp_parser_simulate_error (parser))
11806             start = cp_lexer_token_position (parser->lexer, true);
11807           /* Parse the template arguments so that we can issue error
11808              messages about them.  */
11809           cp_lexer_consume_token (parser->lexer);
11810           cp_parser_enclosed_template_argument_list (parser);
11811           /* Skip tokens until we find a good place from which to
11812              continue parsing.  */
11813           cp_parser_skip_to_closing_parenthesis (parser,
11814                                                  /*recovering=*/true,
11815                                                  /*or_comma=*/true,
11816                                                  /*consume_paren=*/false);
11817           /* If parsing tentatively, permanently remove the
11818              template argument list.  That will prevent duplicate
11819              error messages from being issued about the missing
11820              "template" keyword.  */
11821           if (start)
11822             cp_lexer_purge_tokens_after (parser->lexer, start);
11823           if (is_identifier)
11824             *is_identifier = true;
11825           return identifier;
11826         }
11827
11828       /* If the "template" keyword is present, then there is generally
11829          no point in doing name-lookup, so we just return IDENTIFIER.
11830          But, if the qualifying scope is non-dependent then we can
11831          (and must) do name-lookup normally.  */
11832       if (template_keyword_p
11833           && (!parser->scope
11834               || (TYPE_P (parser->scope)
11835                   && dependent_type_p (parser->scope))))
11836         return identifier;
11837     }
11838
11839   /* Look up the name.  */
11840   decl = cp_parser_lookup_name (parser, identifier,
11841                                 none_type,
11842                                 /*is_template=*/true,
11843                                 /*is_namespace=*/false,
11844                                 check_dependency_p,
11845                                 /*ambiguous_decls=*/NULL,
11846                                 token->location);
11847
11848   /* If DECL is a template, then the name was a template-name.  */
11849   if (TREE_CODE (decl) == TEMPLATE_DECL)
11850     ;
11851   else
11852     {
11853       tree fn = NULL_TREE;
11854
11855       /* The standard does not explicitly indicate whether a name that
11856          names a set of overloaded declarations, some of which are
11857          templates, is a template-name.  However, such a name should
11858          be a template-name; otherwise, there is no way to form a
11859          template-id for the overloaded templates.  */
11860       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11861       if (TREE_CODE (fns) == OVERLOAD)
11862         for (fn = fns; fn; fn = OVL_NEXT (fn))
11863           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11864             break;
11865
11866       if (!fn)
11867         {
11868           /* The name does not name a template.  */
11869           cp_parser_error (parser, "expected template-name");
11870           return error_mark_node;
11871         }
11872     }
11873
11874   /* If DECL is dependent, and refers to a function, then just return
11875      its name; we will look it up again during template instantiation.  */
11876   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11877     {
11878       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11879       if (TYPE_P (scope) && dependent_type_p (scope))
11880         return identifier;
11881     }
11882
11883   return decl;
11884 }
11885
11886 /* Parse a template-argument-list.
11887
11888    template-argument-list:
11889      template-argument ... [opt]
11890      template-argument-list , template-argument ... [opt]
11891
11892    Returns a TREE_VEC containing the arguments.  */
11893
11894 static tree
11895 cp_parser_template_argument_list (cp_parser* parser)
11896 {
11897   tree fixed_args[10];
11898   unsigned n_args = 0;
11899   unsigned alloced = 10;
11900   tree *arg_ary = fixed_args;
11901   tree vec;
11902   bool saved_in_template_argument_list_p;
11903   bool saved_ice_p;
11904   bool saved_non_ice_p;
11905
11906   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11907   parser->in_template_argument_list_p = true;
11908   /* Even if the template-id appears in an integral
11909      constant-expression, the contents of the argument list do
11910      not.  */
11911   saved_ice_p = parser->integral_constant_expression_p;
11912   parser->integral_constant_expression_p = false;
11913   saved_non_ice_p = parser->non_integral_constant_expression_p;
11914   parser->non_integral_constant_expression_p = false;
11915   /* Parse the arguments.  */
11916   do
11917     {
11918       tree argument;
11919
11920       if (n_args)
11921         /* Consume the comma.  */
11922         cp_lexer_consume_token (parser->lexer);
11923
11924       /* Parse the template-argument.  */
11925       argument = cp_parser_template_argument (parser);
11926
11927       /* If the next token is an ellipsis, we're expanding a template
11928          argument pack. */
11929       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11930         {
11931           if (argument == error_mark_node)
11932             {
11933               cp_token *token = cp_lexer_peek_token (parser->lexer);
11934               error_at (token->location,
11935                         "expected parameter pack before %<...%>");
11936             }
11937           /* Consume the `...' token. */
11938           cp_lexer_consume_token (parser->lexer);
11939
11940           /* Make the argument into a TYPE_PACK_EXPANSION or
11941              EXPR_PACK_EXPANSION. */
11942           argument = make_pack_expansion (argument);
11943         }
11944
11945       if (n_args == alloced)
11946         {
11947           alloced *= 2;
11948
11949           if (arg_ary == fixed_args)
11950             {
11951               arg_ary = XNEWVEC (tree, alloced);
11952               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11953             }
11954           else
11955             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11956         }
11957       arg_ary[n_args++] = argument;
11958     }
11959   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11960
11961   vec = make_tree_vec (n_args);
11962
11963   while (n_args--)
11964     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11965
11966   if (arg_ary != fixed_args)
11967     free (arg_ary);
11968   parser->non_integral_constant_expression_p = saved_non_ice_p;
11969   parser->integral_constant_expression_p = saved_ice_p;
11970   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11971 #ifdef ENABLE_CHECKING
11972   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11973 #endif
11974   return vec;
11975 }
11976
11977 /* Parse a template-argument.
11978
11979    template-argument:
11980      assignment-expression
11981      type-id
11982      id-expression
11983
11984    The representation is that of an assignment-expression, type-id, or
11985    id-expression -- except that the qualified id-expression is
11986    evaluated, so that the value returned is either a DECL or an
11987    OVERLOAD.
11988
11989    Although the standard says "assignment-expression", it forbids
11990    throw-expressions or assignments in the template argument.
11991    Therefore, we use "conditional-expression" instead.  */
11992
11993 static tree
11994 cp_parser_template_argument (cp_parser* parser)
11995 {
11996   tree argument;
11997   bool template_p;
11998   bool address_p;
11999   bool maybe_type_id = false;
12000   cp_token *token = NULL, *argument_start_token = NULL;
12001   cp_id_kind idk;
12002
12003   /* There's really no way to know what we're looking at, so we just
12004      try each alternative in order.
12005
12006        [temp.arg]
12007
12008        In a template-argument, an ambiguity between a type-id and an
12009        expression is resolved to a type-id, regardless of the form of
12010        the corresponding template-parameter.
12011
12012      Therefore, we try a type-id first.  */
12013   cp_parser_parse_tentatively (parser);
12014   argument = cp_parser_template_type_arg (parser);
12015   /* If there was no error parsing the type-id but the next token is a
12016      '>>', our behavior depends on which dialect of C++ we're
12017      parsing. In C++98, we probably found a typo for '> >'. But there
12018      are type-id which are also valid expressions. For instance:
12019
12020      struct X { int operator >> (int); };
12021      template <int V> struct Foo {};
12022      Foo<X () >> 5> r;
12023
12024      Here 'X()' is a valid type-id of a function type, but the user just
12025      wanted to write the expression "X() >> 5". Thus, we remember that we
12026      found a valid type-id, but we still try to parse the argument as an
12027      expression to see what happens. 
12028
12029      In C++0x, the '>>' will be considered two separate '>'
12030      tokens.  */
12031   if (!cp_parser_error_occurred (parser)
12032       && cxx_dialect == cxx98
12033       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12034     {
12035       maybe_type_id = true;
12036       cp_parser_abort_tentative_parse (parser);
12037     }
12038   else
12039     {
12040       /* If the next token isn't a `,' or a `>', then this argument wasn't
12041       really finished. This means that the argument is not a valid
12042       type-id.  */
12043       if (!cp_parser_next_token_ends_template_argument_p (parser))
12044         cp_parser_error (parser, "expected template-argument");
12045       /* If that worked, we're done.  */
12046       if (cp_parser_parse_definitely (parser))
12047         return argument;
12048     }
12049   /* We're still not sure what the argument will be.  */
12050   cp_parser_parse_tentatively (parser);
12051   /* Try a template.  */
12052   argument_start_token = cp_lexer_peek_token (parser->lexer);
12053   argument = cp_parser_id_expression (parser,
12054                                       /*template_keyword_p=*/false,
12055                                       /*check_dependency_p=*/true,
12056                                       &template_p,
12057                                       /*declarator_p=*/false,
12058                                       /*optional_p=*/false);
12059   /* If the next token isn't a `,' or a `>', then this argument wasn't
12060      really finished.  */
12061   if (!cp_parser_next_token_ends_template_argument_p (parser))
12062     cp_parser_error (parser, "expected template-argument");
12063   if (!cp_parser_error_occurred (parser))
12064     {
12065       /* Figure out what is being referred to.  If the id-expression
12066          was for a class template specialization, then we will have a
12067          TYPE_DECL at this point.  There is no need to do name lookup
12068          at this point in that case.  */
12069       if (TREE_CODE (argument) != TYPE_DECL)
12070         argument = cp_parser_lookup_name (parser, argument,
12071                                           none_type,
12072                                           /*is_template=*/template_p,
12073                                           /*is_namespace=*/false,
12074                                           /*check_dependency=*/true,
12075                                           /*ambiguous_decls=*/NULL,
12076                                           argument_start_token->location);
12077       if (TREE_CODE (argument) != TEMPLATE_DECL
12078           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12079         cp_parser_error (parser, "expected template-name");
12080     }
12081   if (cp_parser_parse_definitely (parser))
12082     return argument;
12083   /* It must be a non-type argument.  There permitted cases are given
12084      in [temp.arg.nontype]:
12085
12086      -- an integral constant-expression of integral or enumeration
12087         type; or
12088
12089      -- the name of a non-type template-parameter; or
12090
12091      -- the name of an object or function with external linkage...
12092
12093      -- the address of an object or function with external linkage...
12094
12095      -- a pointer to member...  */
12096   /* Look for a non-type template parameter.  */
12097   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12098     {
12099       cp_parser_parse_tentatively (parser);
12100       argument = cp_parser_primary_expression (parser,
12101                                                /*address_p=*/false,
12102                                                /*cast_p=*/false,
12103                                                /*template_arg_p=*/true,
12104                                                &idk);
12105       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12106           || !cp_parser_next_token_ends_template_argument_p (parser))
12107         cp_parser_simulate_error (parser);
12108       if (cp_parser_parse_definitely (parser))
12109         return argument;
12110     }
12111
12112   /* If the next token is "&", the argument must be the address of an
12113      object or function with external linkage.  */
12114   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12115   if (address_p)
12116     cp_lexer_consume_token (parser->lexer);
12117   /* See if we might have an id-expression.  */
12118   token = cp_lexer_peek_token (parser->lexer);
12119   if (token->type == CPP_NAME
12120       || token->keyword == RID_OPERATOR
12121       || token->type == CPP_SCOPE
12122       || token->type == CPP_TEMPLATE_ID
12123       || token->type == CPP_NESTED_NAME_SPECIFIER)
12124     {
12125       cp_parser_parse_tentatively (parser);
12126       argument = cp_parser_primary_expression (parser,
12127                                                address_p,
12128                                                /*cast_p=*/false,
12129                                                /*template_arg_p=*/true,
12130                                                &idk);
12131       if (cp_parser_error_occurred (parser)
12132           || !cp_parser_next_token_ends_template_argument_p (parser))
12133         cp_parser_abort_tentative_parse (parser);
12134       else
12135         {
12136           tree probe;
12137
12138           if (TREE_CODE (argument) == INDIRECT_REF)
12139             {
12140               gcc_assert (REFERENCE_REF_P (argument));
12141               argument = TREE_OPERAND (argument, 0);
12142             }
12143
12144           /* If we're in a template, we represent a qualified-id referring
12145              to a static data member as a SCOPE_REF even if the scope isn't
12146              dependent so that we can check access control later.  */
12147           probe = argument;
12148           if (TREE_CODE (probe) == SCOPE_REF)
12149             probe = TREE_OPERAND (probe, 1);
12150           if (TREE_CODE (probe) == VAR_DECL)
12151             {
12152               /* A variable without external linkage might still be a
12153                  valid constant-expression, so no error is issued here
12154                  if the external-linkage check fails.  */
12155               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12156                 cp_parser_simulate_error (parser);
12157             }
12158           else if (is_overloaded_fn (argument))
12159             /* All overloaded functions are allowed; if the external
12160                linkage test does not pass, an error will be issued
12161                later.  */
12162             ;
12163           else if (address_p
12164                    && (TREE_CODE (argument) == OFFSET_REF
12165                        || TREE_CODE (argument) == SCOPE_REF))
12166             /* A pointer-to-member.  */
12167             ;
12168           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12169             ;
12170           else
12171             cp_parser_simulate_error (parser);
12172
12173           if (cp_parser_parse_definitely (parser))
12174             {
12175               if (address_p)
12176                 argument = build_x_unary_op (ADDR_EXPR, argument,
12177                                              tf_warning_or_error);
12178               return argument;
12179             }
12180         }
12181     }
12182   /* If the argument started with "&", there are no other valid
12183      alternatives at this point.  */
12184   if (address_p)
12185     {
12186       cp_parser_error (parser, "invalid non-type template argument");
12187       return error_mark_node;
12188     }
12189
12190   /* If the argument wasn't successfully parsed as a type-id followed
12191      by '>>', the argument can only be a constant expression now.
12192      Otherwise, we try parsing the constant-expression tentatively,
12193      because the argument could really be a type-id.  */
12194   if (maybe_type_id)
12195     cp_parser_parse_tentatively (parser);
12196   argument = cp_parser_constant_expression (parser,
12197                                             /*allow_non_constant_p=*/false,
12198                                             /*non_constant_p=*/NULL);
12199   argument = fold_non_dependent_expr (argument);
12200   if (!maybe_type_id)
12201     return argument;
12202   if (!cp_parser_next_token_ends_template_argument_p (parser))
12203     cp_parser_error (parser, "expected template-argument");
12204   if (cp_parser_parse_definitely (parser))
12205     return argument;
12206   /* We did our best to parse the argument as a non type-id, but that
12207      was the only alternative that matched (albeit with a '>' after
12208      it). We can assume it's just a typo from the user, and a
12209      diagnostic will then be issued.  */
12210   return cp_parser_template_type_arg (parser);
12211 }
12212
12213 /* Parse an explicit-instantiation.
12214
12215    explicit-instantiation:
12216      template declaration
12217
12218    Although the standard says `declaration', what it really means is:
12219
12220    explicit-instantiation:
12221      template decl-specifier-seq [opt] declarator [opt] ;
12222
12223    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12224    supposed to be allowed.  A defect report has been filed about this
12225    issue.
12226
12227    GNU Extension:
12228
12229    explicit-instantiation:
12230      storage-class-specifier template
12231        decl-specifier-seq [opt] declarator [opt] ;
12232      function-specifier template
12233        decl-specifier-seq [opt] declarator [opt] ;  */
12234
12235 static void
12236 cp_parser_explicit_instantiation (cp_parser* parser)
12237 {
12238   int declares_class_or_enum;
12239   cp_decl_specifier_seq decl_specifiers;
12240   tree extension_specifier = NULL_TREE;
12241
12242   /* Look for an (optional) storage-class-specifier or
12243      function-specifier.  */
12244   if (cp_parser_allow_gnu_extensions_p (parser))
12245     {
12246       extension_specifier
12247         = cp_parser_storage_class_specifier_opt (parser);
12248       if (!extension_specifier)
12249         extension_specifier
12250           = cp_parser_function_specifier_opt (parser,
12251                                               /*decl_specs=*/NULL);
12252     }
12253
12254   /* Look for the `template' keyword.  */
12255   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12256   /* Let the front end know that we are processing an explicit
12257      instantiation.  */
12258   begin_explicit_instantiation ();
12259   /* [temp.explicit] says that we are supposed to ignore access
12260      control while processing explicit instantiation directives.  */
12261   push_deferring_access_checks (dk_no_check);
12262   /* Parse a decl-specifier-seq.  */
12263   cp_parser_decl_specifier_seq (parser,
12264                                 CP_PARSER_FLAGS_OPTIONAL,
12265                                 &decl_specifiers,
12266                                 &declares_class_or_enum);
12267   /* If there was exactly one decl-specifier, and it declared a class,
12268      and there's no declarator, then we have an explicit type
12269      instantiation.  */
12270   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12271     {
12272       tree type;
12273
12274       type = check_tag_decl (&decl_specifiers);
12275       /* Turn access control back on for names used during
12276          template instantiation.  */
12277       pop_deferring_access_checks ();
12278       if (type)
12279         do_type_instantiation (type, extension_specifier,
12280                                /*complain=*/tf_error);
12281     }
12282   else
12283     {
12284       cp_declarator *declarator;
12285       tree decl;
12286
12287       /* Parse the declarator.  */
12288       declarator
12289         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12290                                 /*ctor_dtor_or_conv_p=*/NULL,
12291                                 /*parenthesized_p=*/NULL,
12292                                 /*member_p=*/false);
12293       if (declares_class_or_enum & 2)
12294         cp_parser_check_for_definition_in_return_type (declarator,
12295                                                        decl_specifiers.type,
12296                                                        decl_specifiers.type_location);
12297       if (declarator != cp_error_declarator)
12298         {
12299           if (decl_specifiers.specs[(int)ds_inline])
12300             permerror (input_location, "explicit instantiation shall not use"
12301                        " %<inline%> specifier");
12302           if (decl_specifiers.specs[(int)ds_constexpr])
12303             permerror (input_location, "explicit instantiation shall not use"
12304                        " %<constexpr%> specifier");
12305
12306           decl = grokdeclarator (declarator, &decl_specifiers,
12307                                  NORMAL, 0, &decl_specifiers.attributes);
12308           /* Turn access control back on for names used during
12309              template instantiation.  */
12310           pop_deferring_access_checks ();
12311           /* Do the explicit instantiation.  */
12312           do_decl_instantiation (decl, extension_specifier);
12313         }
12314       else
12315         {
12316           pop_deferring_access_checks ();
12317           /* Skip the body of the explicit instantiation.  */
12318           cp_parser_skip_to_end_of_statement (parser);
12319         }
12320     }
12321   /* We're done with the instantiation.  */
12322   end_explicit_instantiation ();
12323
12324   cp_parser_consume_semicolon_at_end_of_statement (parser);
12325 }
12326
12327 /* Parse an explicit-specialization.
12328
12329    explicit-specialization:
12330      template < > declaration
12331
12332    Although the standard says `declaration', what it really means is:
12333
12334    explicit-specialization:
12335      template <> decl-specifier [opt] init-declarator [opt] ;
12336      template <> function-definition
12337      template <> explicit-specialization
12338      template <> template-declaration  */
12339
12340 static void
12341 cp_parser_explicit_specialization (cp_parser* parser)
12342 {
12343   bool need_lang_pop;
12344   cp_token *token = cp_lexer_peek_token (parser->lexer);
12345
12346   /* Look for the `template' keyword.  */
12347   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12348   /* Look for the `<'.  */
12349   cp_parser_require (parser, CPP_LESS, RT_LESS);
12350   /* Look for the `>'.  */
12351   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12352   /* We have processed another parameter list.  */
12353   ++parser->num_template_parameter_lists;
12354   /* [temp]
12355
12356      A template ... explicit specialization ... shall not have C
12357      linkage.  */
12358   if (current_lang_name == lang_name_c)
12359     {
12360       error_at (token->location, "template specialization with C linkage");
12361       /* Give it C++ linkage to avoid confusing other parts of the
12362          front end.  */
12363       push_lang_context (lang_name_cplusplus);
12364       need_lang_pop = true;
12365     }
12366   else
12367     need_lang_pop = false;
12368   /* Let the front end know that we are beginning a specialization.  */
12369   if (!begin_specialization ())
12370     {
12371       end_specialization ();
12372       return;
12373     }
12374
12375   /* If the next keyword is `template', we need to figure out whether
12376      or not we're looking a template-declaration.  */
12377   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12378     {
12379       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12380           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12381         cp_parser_template_declaration_after_export (parser,
12382                                                      /*member_p=*/false);
12383       else
12384         cp_parser_explicit_specialization (parser);
12385     }
12386   else
12387     /* Parse the dependent declaration.  */
12388     cp_parser_single_declaration (parser,
12389                                   /*checks=*/NULL,
12390                                   /*member_p=*/false,
12391                                   /*explicit_specialization_p=*/true,
12392                                   /*friend_p=*/NULL);
12393   /* We're done with the specialization.  */
12394   end_specialization ();
12395   /* For the erroneous case of a template with C linkage, we pushed an
12396      implicit C++ linkage scope; exit that scope now.  */
12397   if (need_lang_pop)
12398     pop_lang_context ();
12399   /* We're done with this parameter list.  */
12400   --parser->num_template_parameter_lists;
12401 }
12402
12403 /* Parse a type-specifier.
12404
12405    type-specifier:
12406      simple-type-specifier
12407      class-specifier
12408      enum-specifier
12409      elaborated-type-specifier
12410      cv-qualifier
12411
12412    GNU Extension:
12413
12414    type-specifier:
12415      __complex__
12416
12417    Returns a representation of the type-specifier.  For a
12418    class-specifier, enum-specifier, or elaborated-type-specifier, a
12419    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12420
12421    The parser flags FLAGS is used to control type-specifier parsing.
12422
12423    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12424    in a decl-specifier-seq.
12425
12426    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12427    class-specifier, enum-specifier, or elaborated-type-specifier, then
12428    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12429    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12430    zero.
12431
12432    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12433    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12434    is set to FALSE.  */
12435
12436 static tree
12437 cp_parser_type_specifier (cp_parser* parser,
12438                           cp_parser_flags flags,
12439                           cp_decl_specifier_seq *decl_specs,
12440                           bool is_declaration,
12441                           int* declares_class_or_enum,
12442                           bool* is_cv_qualifier)
12443 {
12444   tree type_spec = NULL_TREE;
12445   cp_token *token;
12446   enum rid keyword;
12447   cp_decl_spec ds = ds_last;
12448
12449   /* Assume this type-specifier does not declare a new type.  */
12450   if (declares_class_or_enum)
12451     *declares_class_or_enum = 0;
12452   /* And that it does not specify a cv-qualifier.  */
12453   if (is_cv_qualifier)
12454     *is_cv_qualifier = false;
12455   /* Peek at the next token.  */
12456   token = cp_lexer_peek_token (parser->lexer);
12457
12458   /* If we're looking at a keyword, we can use that to guide the
12459      production we choose.  */
12460   keyword = token->keyword;
12461   switch (keyword)
12462     {
12463     case RID_ENUM:
12464       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12465         goto elaborated_type_specifier;
12466
12467       /* Look for the enum-specifier.  */
12468       type_spec = cp_parser_enum_specifier (parser);
12469       /* If that worked, we're done.  */
12470       if (type_spec)
12471         {
12472           if (declares_class_or_enum)
12473             *declares_class_or_enum = 2;
12474           if (decl_specs)
12475             cp_parser_set_decl_spec_type (decl_specs,
12476                                           type_spec,
12477                                           token->location,
12478                                           /*user_defined_p=*/true);
12479           return type_spec;
12480         }
12481       else
12482         goto elaborated_type_specifier;
12483
12484       /* Any of these indicate either a class-specifier, or an
12485          elaborated-type-specifier.  */
12486     case RID_CLASS:
12487     case RID_STRUCT:
12488     case RID_UNION:
12489       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12490         goto elaborated_type_specifier;
12491
12492       /* Parse tentatively so that we can back up if we don't find a
12493          class-specifier.  */
12494       cp_parser_parse_tentatively (parser);
12495       /* Look for the class-specifier.  */
12496       type_spec = cp_parser_class_specifier (parser);
12497       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12498       /* If that worked, we're done.  */
12499       if (cp_parser_parse_definitely (parser))
12500         {
12501           if (declares_class_or_enum)
12502             *declares_class_or_enum = 2;
12503           if (decl_specs)
12504             cp_parser_set_decl_spec_type (decl_specs,
12505                                           type_spec,
12506                                           token->location,
12507                                           /*user_defined_p=*/true);
12508           return type_spec;
12509         }
12510
12511       /* Fall through.  */
12512     elaborated_type_specifier:
12513       /* We're declaring (not defining) a class or enum.  */
12514       if (declares_class_or_enum)
12515         *declares_class_or_enum = 1;
12516
12517       /* Fall through.  */
12518     case RID_TYPENAME:
12519       /* Look for an elaborated-type-specifier.  */
12520       type_spec
12521         = (cp_parser_elaborated_type_specifier
12522            (parser,
12523             decl_specs && decl_specs->specs[(int) ds_friend],
12524             is_declaration));
12525       if (decl_specs)
12526         cp_parser_set_decl_spec_type (decl_specs,
12527                                       type_spec,
12528                                       token->location,
12529                                       /*user_defined_p=*/true);
12530       return type_spec;
12531
12532     case RID_CONST:
12533       ds = ds_const;
12534       if (is_cv_qualifier)
12535         *is_cv_qualifier = true;
12536       break;
12537
12538     case RID_VOLATILE:
12539       ds = ds_volatile;
12540       if (is_cv_qualifier)
12541         *is_cv_qualifier = true;
12542       break;
12543
12544     case RID_RESTRICT:
12545       ds = ds_restrict;
12546       if (is_cv_qualifier)
12547         *is_cv_qualifier = true;
12548       break;
12549
12550     case RID_COMPLEX:
12551       /* The `__complex__' keyword is a GNU extension.  */
12552       ds = ds_complex;
12553       break;
12554
12555     default:
12556       break;
12557     }
12558
12559   /* Handle simple keywords.  */
12560   if (ds != ds_last)
12561     {
12562       if (decl_specs)
12563         {
12564           ++decl_specs->specs[(int)ds];
12565           decl_specs->any_specifiers_p = true;
12566         }
12567       return cp_lexer_consume_token (parser->lexer)->u.value;
12568     }
12569
12570   /* If we do not already have a type-specifier, assume we are looking
12571      at a simple-type-specifier.  */
12572   type_spec = cp_parser_simple_type_specifier (parser,
12573                                                decl_specs,
12574                                                flags);
12575
12576   /* If we didn't find a type-specifier, and a type-specifier was not
12577      optional in this context, issue an error message.  */
12578   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12579     {
12580       cp_parser_error (parser, "expected type specifier");
12581       return error_mark_node;
12582     }
12583
12584   return type_spec;
12585 }
12586
12587 /* Parse a simple-type-specifier.
12588
12589    simple-type-specifier:
12590      :: [opt] nested-name-specifier [opt] type-name
12591      :: [opt] nested-name-specifier template template-id
12592      char
12593      wchar_t
12594      bool
12595      short
12596      int
12597      long
12598      signed
12599      unsigned
12600      float
12601      double
12602      void
12603
12604    C++0x Extension:
12605
12606    simple-type-specifier:
12607      auto
12608      decltype ( expression )   
12609      char16_t
12610      char32_t
12611
12612    GNU Extension:
12613
12614    simple-type-specifier:
12615      __int128
12616      __typeof__ unary-expression
12617      __typeof__ ( type-id )
12618
12619    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12620    appropriately updated.  */
12621
12622 static tree
12623 cp_parser_simple_type_specifier (cp_parser* parser,
12624                                  cp_decl_specifier_seq *decl_specs,
12625                                  cp_parser_flags flags)
12626 {
12627   tree type = NULL_TREE;
12628   cp_token *token;
12629
12630   /* Peek at the next token.  */
12631   token = cp_lexer_peek_token (parser->lexer);
12632
12633   /* If we're looking at a keyword, things are easy.  */
12634   switch (token->keyword)
12635     {
12636     case RID_CHAR:
12637       if (decl_specs)
12638         decl_specs->explicit_char_p = true;
12639       type = char_type_node;
12640       break;
12641     case RID_CHAR16:
12642       type = char16_type_node;
12643       break;
12644     case RID_CHAR32:
12645       type = char32_type_node;
12646       break;
12647     case RID_WCHAR:
12648       type = wchar_type_node;
12649       break;
12650     case RID_BOOL:
12651       type = boolean_type_node;
12652       break;
12653     case RID_SHORT:
12654       if (decl_specs)
12655         ++decl_specs->specs[(int) ds_short];
12656       type = short_integer_type_node;
12657       break;
12658     case RID_INT:
12659       if (decl_specs)
12660         decl_specs->explicit_int_p = true;
12661       type = integer_type_node;
12662       break;
12663     case RID_INT128:
12664       if (!int128_integer_type_node)
12665         break;
12666       if (decl_specs)
12667         decl_specs->explicit_int128_p = true;
12668       type = int128_integer_type_node;
12669       break;
12670     case RID_LONG:
12671       if (decl_specs)
12672         ++decl_specs->specs[(int) ds_long];
12673       type = long_integer_type_node;
12674       break;
12675     case RID_SIGNED:
12676       if (decl_specs)
12677         ++decl_specs->specs[(int) ds_signed];
12678       type = integer_type_node;
12679       break;
12680     case RID_UNSIGNED:
12681       if (decl_specs)
12682         ++decl_specs->specs[(int) ds_unsigned];
12683       type = unsigned_type_node;
12684       break;
12685     case RID_FLOAT:
12686       type = float_type_node;
12687       break;
12688     case RID_DOUBLE:
12689       type = double_type_node;
12690       break;
12691     case RID_VOID:
12692       type = void_type_node;
12693       break;
12694       
12695     case RID_AUTO:
12696       maybe_warn_cpp0x (CPP0X_AUTO);
12697       type = make_auto ();
12698       break;
12699
12700     case RID_DECLTYPE:
12701       /* Parse the `decltype' type.  */
12702       type = cp_parser_decltype (parser);
12703
12704       if (decl_specs)
12705         cp_parser_set_decl_spec_type (decl_specs, type,
12706                                       token->location,
12707                                       /*user_defined_p=*/true);
12708
12709       return type;
12710
12711     case RID_TYPEOF:
12712       /* Consume the `typeof' token.  */
12713       cp_lexer_consume_token (parser->lexer);
12714       /* Parse the operand to `typeof'.  */
12715       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12716       /* If it is not already a TYPE, take its type.  */
12717       if (!TYPE_P (type))
12718         type = finish_typeof (type);
12719
12720       if (decl_specs)
12721         cp_parser_set_decl_spec_type (decl_specs, type,
12722                                       token->location,
12723                                       /*user_defined_p=*/true);
12724
12725       return type;
12726
12727     default:
12728       break;
12729     }
12730
12731   /* If the type-specifier was for a built-in type, we're done.  */
12732   if (type)
12733     {
12734       /* Record the type.  */
12735       if (decl_specs
12736           && (token->keyword != RID_SIGNED
12737               && token->keyword != RID_UNSIGNED
12738               && token->keyword != RID_SHORT
12739               && token->keyword != RID_LONG))
12740         cp_parser_set_decl_spec_type (decl_specs,
12741                                       type,
12742                                       token->location,
12743                                       /*user_defined=*/false);
12744       if (decl_specs)
12745         decl_specs->any_specifiers_p = true;
12746
12747       /* Consume the token.  */
12748       cp_lexer_consume_token (parser->lexer);
12749
12750       /* There is no valid C++ program where a non-template type is
12751          followed by a "<".  That usually indicates that the user thought
12752          that the type was a template.  */
12753       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12754
12755       return TYPE_NAME (type);
12756     }
12757
12758   /* The type-specifier must be a user-defined type.  */
12759   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12760     {
12761       bool qualified_p;
12762       bool global_p;
12763
12764       /* Don't gobble tokens or issue error messages if this is an
12765          optional type-specifier.  */
12766       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12767         cp_parser_parse_tentatively (parser);
12768
12769       /* Look for the optional `::' operator.  */
12770       global_p
12771         = (cp_parser_global_scope_opt (parser,
12772                                        /*current_scope_valid_p=*/false)
12773            != NULL_TREE);
12774       /* Look for the nested-name specifier.  */
12775       qualified_p
12776         = (cp_parser_nested_name_specifier_opt (parser,
12777                                                 /*typename_keyword_p=*/false,
12778                                                 /*check_dependency_p=*/true,
12779                                                 /*type_p=*/false,
12780                                                 /*is_declaration=*/false)
12781            != NULL_TREE);
12782       token = cp_lexer_peek_token (parser->lexer);
12783       /* If we have seen a nested-name-specifier, and the next token
12784          is `template', then we are using the template-id production.  */
12785       if (parser->scope
12786           && cp_parser_optional_template_keyword (parser))
12787         {
12788           /* Look for the template-id.  */
12789           type = cp_parser_template_id (parser,
12790                                         /*template_keyword_p=*/true,
12791                                         /*check_dependency_p=*/true,
12792                                         /*is_declaration=*/false);
12793           /* If the template-id did not name a type, we are out of
12794              luck.  */
12795           if (TREE_CODE (type) != TYPE_DECL)
12796             {
12797               cp_parser_error (parser, "expected template-id for type");
12798               type = NULL_TREE;
12799             }
12800         }
12801       /* Otherwise, look for a type-name.  */
12802       else
12803         type = cp_parser_type_name (parser);
12804       /* Keep track of all name-lookups performed in class scopes.  */
12805       if (type
12806           && !global_p
12807           && !qualified_p
12808           && TREE_CODE (type) == TYPE_DECL
12809           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12810         maybe_note_name_used_in_class (DECL_NAME (type), type);
12811       /* If it didn't work out, we don't have a TYPE.  */
12812       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12813           && !cp_parser_parse_definitely (parser))
12814         type = NULL_TREE;
12815       if (type && decl_specs)
12816         cp_parser_set_decl_spec_type (decl_specs, type,
12817                                       token->location,
12818                                       /*user_defined=*/true);
12819     }
12820
12821   /* If we didn't get a type-name, issue an error message.  */
12822   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12823     {
12824       cp_parser_error (parser, "expected type-name");
12825       return error_mark_node;
12826     }
12827
12828   if (type && type != error_mark_node)
12829     {
12830       /* See if TYPE is an Objective-C type, and if so, parse and
12831          accept any protocol references following it.  Do this before
12832          the cp_parser_check_for_invalid_template_id() call, because
12833          Objective-C types can be followed by '<...>' which would
12834          enclose protocol names rather than template arguments, and so
12835          everything is fine.  */
12836       if (c_dialect_objc () && !parser->scope
12837           && (objc_is_id (type) || objc_is_class_name (type)))
12838         {
12839           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12840           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12841
12842           /* Clobber the "unqualified" type previously entered into
12843              DECL_SPECS with the new, improved protocol-qualified version.  */
12844           if (decl_specs)
12845             decl_specs->type = qual_type;
12846
12847           return qual_type;
12848         }
12849
12850       /* There is no valid C++ program where a non-template type is
12851          followed by a "<".  That usually indicates that the user
12852          thought that the type was a template.  */
12853       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12854                                                token->location);
12855     }
12856
12857   return type;
12858 }
12859
12860 /* Parse a type-name.
12861
12862    type-name:
12863      class-name
12864      enum-name
12865      typedef-name
12866
12867    enum-name:
12868      identifier
12869
12870    typedef-name:
12871      identifier
12872
12873    Returns a TYPE_DECL for the type.  */
12874
12875 static tree
12876 cp_parser_type_name (cp_parser* parser)
12877 {
12878   tree type_decl;
12879
12880   /* We can't know yet whether it is a class-name or not.  */
12881   cp_parser_parse_tentatively (parser);
12882   /* Try a class-name.  */
12883   type_decl = cp_parser_class_name (parser,
12884                                     /*typename_keyword_p=*/false,
12885                                     /*template_keyword_p=*/false,
12886                                     none_type,
12887                                     /*check_dependency_p=*/true,
12888                                     /*class_head_p=*/false,
12889                                     /*is_declaration=*/false);
12890   /* If it's not a class-name, keep looking.  */
12891   if (!cp_parser_parse_definitely (parser))
12892     {
12893       /* It must be a typedef-name or an enum-name.  */
12894       return cp_parser_nonclass_name (parser);
12895     }
12896
12897   return type_decl;
12898 }
12899
12900 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12901
12902    enum-name:
12903      identifier
12904
12905    typedef-name:
12906      identifier
12907
12908    Returns a TYPE_DECL for the type.  */
12909
12910 static tree
12911 cp_parser_nonclass_name (cp_parser* parser)
12912 {
12913   tree type_decl;
12914   tree identifier;
12915
12916   cp_token *token = cp_lexer_peek_token (parser->lexer);
12917   identifier = cp_parser_identifier (parser);
12918   if (identifier == error_mark_node)
12919     return error_mark_node;
12920
12921   /* Look up the type-name.  */
12922   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12923
12924   if (TREE_CODE (type_decl) != TYPE_DECL
12925       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12926     {
12927       /* See if this is an Objective-C type.  */
12928       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12929       tree type = objc_get_protocol_qualified_type (identifier, protos);
12930       if (type)
12931         type_decl = TYPE_NAME (type);
12932     }
12933
12934   /* Issue an error if we did not find a type-name.  */
12935   if (TREE_CODE (type_decl) != TYPE_DECL
12936       /* In Objective-C, we have the complication that class names are
12937          normally type names and start declarations (eg, the
12938          "NSObject" in "NSObject *object;"), but can be used in an
12939          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12940          is an expression.  So, a classname followed by a dot is not a
12941          valid type-name.  */
12942       || (objc_is_class_name (TREE_TYPE (type_decl))
12943           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12944     {
12945       if (!cp_parser_simulate_error (parser))
12946         cp_parser_name_lookup_error (parser, identifier, type_decl,
12947                                      NLE_TYPE, token->location);
12948       return error_mark_node;
12949     }
12950   /* Remember that the name was used in the definition of the
12951      current class so that we can check later to see if the
12952      meaning would have been different after the class was
12953      entirely defined.  */
12954   else if (type_decl != error_mark_node
12955            && !parser->scope)
12956     maybe_note_name_used_in_class (identifier, type_decl);
12957   
12958   return type_decl;
12959 }
12960
12961 /* Parse an elaborated-type-specifier.  Note that the grammar given
12962    here incorporates the resolution to DR68.
12963
12964    elaborated-type-specifier:
12965      class-key :: [opt] nested-name-specifier [opt] identifier
12966      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12967      enum-key :: [opt] nested-name-specifier [opt] identifier
12968      typename :: [opt] nested-name-specifier identifier
12969      typename :: [opt] nested-name-specifier template [opt]
12970        template-id
12971
12972    GNU extension:
12973
12974    elaborated-type-specifier:
12975      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12976      class-key attributes :: [opt] nested-name-specifier [opt]
12977                template [opt] template-id
12978      enum attributes :: [opt] nested-name-specifier [opt] identifier
12979
12980    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12981    declared `friend'.  If IS_DECLARATION is TRUE, then this
12982    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12983    something is being declared.
12984
12985    Returns the TYPE specified.  */
12986
12987 static tree
12988 cp_parser_elaborated_type_specifier (cp_parser* parser,
12989                                      bool is_friend,
12990                                      bool is_declaration)
12991 {
12992   enum tag_types tag_type;
12993   tree identifier;
12994   tree type = NULL_TREE;
12995   tree attributes = NULL_TREE;
12996   tree globalscope;
12997   cp_token *token = NULL;
12998
12999   /* See if we're looking at the `enum' keyword.  */
13000   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13001     {
13002       /* Consume the `enum' token.  */
13003       cp_lexer_consume_token (parser->lexer);
13004       /* Remember that it's an enumeration type.  */
13005       tag_type = enum_type;
13006       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13007          enums) is used here.  */
13008       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13009           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13010         {
13011             pedwarn (input_location, 0, "elaborated-type-specifier "
13012                       "for a scoped enum must not use the %<%D%> keyword",
13013                       cp_lexer_peek_token (parser->lexer)->u.value);
13014           /* Consume the `struct' or `class' and parse it anyway.  */
13015           cp_lexer_consume_token (parser->lexer);
13016         }
13017       /* Parse the attributes.  */
13018       attributes = cp_parser_attributes_opt (parser);
13019     }
13020   /* Or, it might be `typename'.  */
13021   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13022                                            RID_TYPENAME))
13023     {
13024       /* Consume the `typename' token.  */
13025       cp_lexer_consume_token (parser->lexer);
13026       /* Remember that it's a `typename' type.  */
13027       tag_type = typename_type;
13028     }
13029   /* Otherwise it must be a class-key.  */
13030   else
13031     {
13032       tag_type = cp_parser_class_key (parser);
13033       if (tag_type == none_type)
13034         return error_mark_node;
13035       /* Parse the attributes.  */
13036       attributes = cp_parser_attributes_opt (parser);
13037     }
13038
13039   /* Look for the `::' operator.  */
13040   globalscope =  cp_parser_global_scope_opt (parser,
13041                                              /*current_scope_valid_p=*/false);
13042   /* Look for the nested-name-specifier.  */
13043   if (tag_type == typename_type && !globalscope)
13044     {
13045       if (!cp_parser_nested_name_specifier (parser,
13046                                            /*typename_keyword_p=*/true,
13047                                            /*check_dependency_p=*/true,
13048                                            /*type_p=*/true,
13049                                             is_declaration))
13050         return error_mark_node;
13051     }
13052   else
13053     /* Even though `typename' is not present, the proposed resolution
13054        to Core Issue 180 says that in `class A<T>::B', `B' should be
13055        considered a type-name, even if `A<T>' is dependent.  */
13056     cp_parser_nested_name_specifier_opt (parser,
13057                                          /*typename_keyword_p=*/true,
13058                                          /*check_dependency_p=*/true,
13059                                          /*type_p=*/true,
13060                                          is_declaration);
13061  /* For everything but enumeration types, consider a template-id.
13062     For an enumeration type, consider only a plain identifier.  */
13063   if (tag_type != enum_type)
13064     {
13065       bool template_p = false;
13066       tree decl;
13067
13068       /* Allow the `template' keyword.  */
13069       template_p = cp_parser_optional_template_keyword (parser);
13070       /* If we didn't see `template', we don't know if there's a
13071          template-id or not.  */
13072       if (!template_p)
13073         cp_parser_parse_tentatively (parser);
13074       /* Parse the template-id.  */
13075       token = cp_lexer_peek_token (parser->lexer);
13076       decl = cp_parser_template_id (parser, template_p,
13077                                     /*check_dependency_p=*/true,
13078                                     is_declaration);
13079       /* If we didn't find a template-id, look for an ordinary
13080          identifier.  */
13081       if (!template_p && !cp_parser_parse_definitely (parser))
13082         ;
13083       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13084          in effect, then we must assume that, upon instantiation, the
13085          template will correspond to a class.  */
13086       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13087                && tag_type == typename_type)
13088         type = make_typename_type (parser->scope, decl,
13089                                    typename_type,
13090                                    /*complain=*/tf_error);
13091       /* If the `typename' keyword is in effect and DECL is not a type
13092          decl. Then type is non existant.   */
13093       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13094         type = NULL_TREE; 
13095       else 
13096         type = TREE_TYPE (decl);
13097     }
13098
13099   if (!type)
13100     {
13101       token = cp_lexer_peek_token (parser->lexer);
13102       identifier = cp_parser_identifier (parser);
13103
13104       if (identifier == error_mark_node)
13105         {
13106           parser->scope = NULL_TREE;
13107           return error_mark_node;
13108         }
13109
13110       /* For a `typename', we needn't call xref_tag.  */
13111       if (tag_type == typename_type
13112           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13113         return cp_parser_make_typename_type (parser, parser->scope,
13114                                              identifier,
13115                                              token->location);
13116       /* Look up a qualified name in the usual way.  */
13117       if (parser->scope)
13118         {
13119           tree decl;
13120           tree ambiguous_decls;
13121
13122           decl = cp_parser_lookup_name (parser, identifier,
13123                                         tag_type,
13124                                         /*is_template=*/false,
13125                                         /*is_namespace=*/false,
13126                                         /*check_dependency=*/true,
13127                                         &ambiguous_decls,
13128                                         token->location);
13129
13130           /* If the lookup was ambiguous, an error will already have been
13131              issued.  */
13132           if (ambiguous_decls)
13133             return error_mark_node;
13134
13135           /* If we are parsing friend declaration, DECL may be a
13136              TEMPLATE_DECL tree node here.  However, we need to check
13137              whether this TEMPLATE_DECL results in valid code.  Consider
13138              the following example:
13139
13140                namespace N {
13141                  template <class T> class C {};
13142                }
13143                class X {
13144                  template <class T> friend class N::C; // #1, valid code
13145                };
13146                template <class T> class Y {
13147                  friend class N::C;                    // #2, invalid code
13148                };
13149
13150              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13151              name lookup of `N::C'.  We see that friend declaration must
13152              be template for the code to be valid.  Note that
13153              processing_template_decl does not work here since it is
13154              always 1 for the above two cases.  */
13155
13156           decl = (cp_parser_maybe_treat_template_as_class
13157                   (decl, /*tag_name_p=*/is_friend
13158                          && parser->num_template_parameter_lists));
13159
13160           if (TREE_CODE (decl) != TYPE_DECL)
13161             {
13162               cp_parser_diagnose_invalid_type_name (parser,
13163                                                     parser->scope,
13164                                                     identifier,
13165                                                     token->location);
13166               return error_mark_node;
13167             }
13168
13169           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13170             {
13171               bool allow_template = (parser->num_template_parameter_lists
13172                                       || DECL_SELF_REFERENCE_P (decl));
13173               type = check_elaborated_type_specifier (tag_type, decl, 
13174                                                       allow_template);
13175
13176               if (type == error_mark_node)
13177                 return error_mark_node;
13178             }
13179
13180           /* Forward declarations of nested types, such as
13181
13182                class C1::C2;
13183                class C1::C2::C3;
13184
13185              are invalid unless all components preceding the final '::'
13186              are complete.  If all enclosing types are complete, these
13187              declarations become merely pointless.
13188
13189              Invalid forward declarations of nested types are errors
13190              caught elsewhere in parsing.  Those that are pointless arrive
13191              here.  */
13192
13193           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13194               && !is_friend && !processing_explicit_instantiation)
13195             warning (0, "declaration %qD does not declare anything", decl);
13196
13197           type = TREE_TYPE (decl);
13198         }
13199       else
13200         {
13201           /* An elaborated-type-specifier sometimes introduces a new type and
13202              sometimes names an existing type.  Normally, the rule is that it
13203              introduces a new type only if there is not an existing type of
13204              the same name already in scope.  For example, given:
13205
13206                struct S {};
13207                void f() { struct S s; }
13208
13209              the `struct S' in the body of `f' is the same `struct S' as in
13210              the global scope; the existing definition is used.  However, if
13211              there were no global declaration, this would introduce a new
13212              local class named `S'.
13213
13214              An exception to this rule applies to the following code:
13215
13216                namespace N { struct S; }
13217
13218              Here, the elaborated-type-specifier names a new type
13219              unconditionally; even if there is already an `S' in the
13220              containing scope this declaration names a new type.
13221              This exception only applies if the elaborated-type-specifier
13222              forms the complete declaration:
13223
13224                [class.name]
13225
13226                A declaration consisting solely of `class-key identifier ;' is
13227                either a redeclaration of the name in the current scope or a
13228                forward declaration of the identifier as a class name.  It
13229                introduces the name into the current scope.
13230
13231              We are in this situation precisely when the next token is a `;'.
13232
13233              An exception to the exception is that a `friend' declaration does
13234              *not* name a new type; i.e., given:
13235
13236                struct S { friend struct T; };
13237
13238              `T' is not a new type in the scope of `S'.
13239
13240              Also, `new struct S' or `sizeof (struct S)' never results in the
13241              definition of a new type; a new type can only be declared in a
13242              declaration context.  */
13243
13244           tag_scope ts;
13245           bool template_p;
13246
13247           if (is_friend)
13248             /* Friends have special name lookup rules.  */
13249             ts = ts_within_enclosing_non_class;
13250           else if (is_declaration
13251                    && cp_lexer_next_token_is (parser->lexer,
13252                                               CPP_SEMICOLON))
13253             /* This is a `class-key identifier ;' */
13254             ts = ts_current;
13255           else
13256             ts = ts_global;
13257
13258           template_p =
13259             (parser->num_template_parameter_lists
13260              && (cp_parser_next_token_starts_class_definition_p (parser)
13261                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13262           /* An unqualified name was used to reference this type, so
13263              there were no qualifying templates.  */
13264           if (!cp_parser_check_template_parameters (parser,
13265                                                     /*num_templates=*/0,
13266                                                     token->location,
13267                                                     /*declarator=*/NULL))
13268             return error_mark_node;
13269           type = xref_tag (tag_type, identifier, ts, template_p);
13270         }
13271     }
13272
13273   if (type == error_mark_node)
13274     return error_mark_node;
13275
13276   /* Allow attributes on forward declarations of classes.  */
13277   if (attributes)
13278     {
13279       if (TREE_CODE (type) == TYPENAME_TYPE)
13280         warning (OPT_Wattributes,
13281                  "attributes ignored on uninstantiated type");
13282       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13283                && ! processing_explicit_instantiation)
13284         warning (OPT_Wattributes,
13285                  "attributes ignored on template instantiation");
13286       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13287         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13288       else
13289         warning (OPT_Wattributes,
13290                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13291     }
13292
13293   if (tag_type != enum_type)
13294     cp_parser_check_class_key (tag_type, type);
13295
13296   /* A "<" cannot follow an elaborated type specifier.  If that
13297      happens, the user was probably trying to form a template-id.  */
13298   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13299
13300   return type;
13301 }
13302
13303 /* Parse an enum-specifier.
13304
13305    enum-specifier:
13306      enum-head { enumerator-list [opt] }
13307
13308    enum-head:
13309      enum-key identifier [opt] enum-base [opt]
13310      enum-key nested-name-specifier identifier enum-base [opt]
13311
13312    enum-key:
13313      enum
13314      enum class   [C++0x]
13315      enum struct  [C++0x]
13316
13317    enum-base:   [C++0x]
13318      : type-specifier-seq
13319
13320    opaque-enum-specifier:
13321      enum-key identifier enum-base [opt] ;
13322
13323    GNU Extensions:
13324      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13325        { enumerator-list [opt] }attributes[opt]
13326
13327    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13328    if the token stream isn't an enum-specifier after all.  */
13329
13330 static tree
13331 cp_parser_enum_specifier (cp_parser* parser)
13332 {
13333   tree identifier;
13334   tree type = NULL_TREE;
13335   tree prev_scope;
13336   tree nested_name_specifier = NULL_TREE;
13337   tree attributes;
13338   bool scoped_enum_p = false;
13339   bool has_underlying_type = false;
13340   bool nested_being_defined = false;
13341   bool new_value_list = false;
13342   bool is_new_type = false;
13343   bool is_anonymous = false;
13344   tree underlying_type = NULL_TREE;
13345   cp_token *type_start_token = NULL;
13346
13347   /* Parse tentatively so that we can back up if we don't find a
13348      enum-specifier.  */
13349   cp_parser_parse_tentatively (parser);
13350
13351   /* Caller guarantees that the current token is 'enum', an identifier
13352      possibly follows, and the token after that is an opening brace.
13353      If we don't have an identifier, fabricate an anonymous name for
13354      the enumeration being defined.  */
13355   cp_lexer_consume_token (parser->lexer);
13356
13357   /* Parse the "class" or "struct", which indicates a scoped
13358      enumeration type in C++0x.  */
13359   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13360       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13361     {
13362       if (cxx_dialect < cxx0x)
13363         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13364
13365       /* Consume the `struct' or `class' token.  */
13366       cp_lexer_consume_token (parser->lexer);
13367
13368       scoped_enum_p = true;
13369     }
13370
13371   attributes = cp_parser_attributes_opt (parser);
13372
13373   /* Clear the qualification.  */
13374   parser->scope = NULL_TREE;
13375   parser->qualifying_scope = NULL_TREE;
13376   parser->object_scope = NULL_TREE;
13377
13378   /* Figure out in what scope the declaration is being placed.  */
13379   prev_scope = current_scope ();
13380
13381   type_start_token = cp_lexer_peek_token (parser->lexer);
13382
13383   push_deferring_access_checks (dk_no_check);
13384   nested_name_specifier
13385       = cp_parser_nested_name_specifier_opt (parser,
13386                                              /*typename_keyword_p=*/true,
13387                                              /*check_dependency_p=*/false,
13388                                              /*type_p=*/false,
13389                                              /*is_declaration=*/false);
13390
13391   if (nested_name_specifier)
13392     {
13393       tree name;
13394
13395       identifier = cp_parser_identifier (parser);
13396       name =  cp_parser_lookup_name (parser, identifier,
13397                                      enum_type,
13398                                      /*is_template=*/false,
13399                                      /*is_namespace=*/false,
13400                                      /*check_dependency=*/true,
13401                                      /*ambiguous_decls=*/NULL,
13402                                      input_location);
13403       if (name)
13404         {
13405           type = TREE_TYPE (name);
13406           if (TREE_CODE (type) == TYPENAME_TYPE)
13407             {
13408               /* Are template enums allowed in ISO? */
13409               if (template_parm_scope_p ())
13410                 pedwarn (type_start_token->location, OPT_pedantic,
13411                          "%qD is an enumeration template", name);
13412               /* ignore a typename reference, for it will be solved by name
13413                  in start_enum.  */
13414               type = NULL_TREE;
13415             }
13416         }
13417       else
13418         error_at (type_start_token->location,
13419                   "%qD is not an enumerator-name", identifier);
13420     }
13421   else
13422     {
13423       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13424         identifier = cp_parser_identifier (parser);
13425       else
13426         {
13427           identifier = make_anon_name ();
13428           is_anonymous = true;
13429         }
13430     }
13431   pop_deferring_access_checks ();
13432
13433   /* Check for the `:' that denotes a specified underlying type in C++0x.
13434      Note that a ':' could also indicate a bitfield width, however.  */
13435   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13436     {
13437       cp_decl_specifier_seq type_specifiers;
13438
13439       /* Consume the `:'.  */
13440       cp_lexer_consume_token (parser->lexer);
13441
13442       /* Parse the type-specifier-seq.  */
13443       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13444                                     /*is_trailing_return=*/false,
13445                                     &type_specifiers);
13446
13447       /* At this point this is surely not elaborated type specifier.  */
13448       if (!cp_parser_parse_definitely (parser))
13449         return NULL_TREE;
13450
13451       if (cxx_dialect < cxx0x)
13452         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13453
13454       has_underlying_type = true;
13455
13456       /* If that didn't work, stop.  */
13457       if (type_specifiers.type != error_mark_node)
13458         {
13459           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13460                                             /*initialized=*/0, NULL);
13461           if (underlying_type == error_mark_node)
13462             underlying_type = NULL_TREE;
13463         }
13464     }
13465
13466   /* Look for the `{' but don't consume it yet.  */
13467   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13468     {
13469       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13470         {
13471           cp_parser_error (parser, "expected %<{%>");
13472           if (has_underlying_type)
13473             return NULL_TREE;
13474         }
13475       /* An opaque-enum-specifier must have a ';' here.  */
13476       if ((scoped_enum_p || underlying_type)
13477           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13478         {
13479           cp_parser_error (parser, "expected %<;%> or %<{%>");
13480           if (has_underlying_type)
13481             return NULL_TREE;
13482         }
13483     }
13484
13485   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13486     return NULL_TREE;
13487
13488   if (nested_name_specifier)
13489     {
13490       if (CLASS_TYPE_P (nested_name_specifier))
13491         {
13492           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13493           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13494           push_scope (nested_name_specifier);
13495         }
13496       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13497         {
13498           push_nested_namespace (nested_name_specifier);
13499         }
13500     }
13501
13502   /* Issue an error message if type-definitions are forbidden here.  */
13503   if (!cp_parser_check_type_definition (parser))
13504     type = error_mark_node;
13505   else
13506     /* Create the new type.  We do this before consuming the opening
13507        brace so the enum will be recorded as being on the line of its
13508        tag (or the 'enum' keyword, if there is no tag).  */
13509     type = start_enum (identifier, type, underlying_type,
13510                        scoped_enum_p, &is_new_type);
13511
13512   /* If the next token is not '{' it is an opaque-enum-specifier or an
13513      elaborated-type-specifier.  */
13514   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13515     {
13516       if (nested_name_specifier)
13517         {
13518           /* The following catches invalid code such as:
13519              enum class S<int>::E { A, B, C }; */
13520           if (!processing_specialization
13521               && CLASS_TYPE_P (nested_name_specifier)
13522               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13523             error_at (type_start_token->location, "cannot add an enumerator "
13524                       "list to a template instantiation");
13525
13526           /* If that scope does not contain the scope in which the
13527              class was originally declared, the program is invalid.  */
13528           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13529             {
13530               if (at_namespace_scope_p ())
13531                 error_at (type_start_token->location,
13532                           "declaration of %qD in namespace %qD which does not "
13533                           "enclose %qD",
13534                           type, prev_scope, nested_name_specifier);
13535               else
13536                 error_at (type_start_token->location,
13537                           "declaration of %qD in %qD which does not enclose %qD",
13538                           type, prev_scope, nested_name_specifier);
13539               type = error_mark_node;
13540             }
13541         }
13542
13543       if (scoped_enum_p)
13544         begin_scope (sk_scoped_enum, type);
13545
13546       /* Consume the opening brace.  */
13547       cp_lexer_consume_token (parser->lexer);
13548
13549       if (type == error_mark_node)
13550         ; /* Nothing to add */
13551       else if (OPAQUE_ENUM_P (type)
13552                || (cxx_dialect > cxx98 && processing_specialization))
13553         {
13554           new_value_list = true;
13555           SET_OPAQUE_ENUM_P (type, false);
13556           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13557         }
13558       else
13559         {
13560           error_at (type_start_token->location, "multiple definition of %q#T", type);
13561           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13562                     "previous definition here");
13563           type = error_mark_node;
13564         }
13565
13566       if (type == error_mark_node)
13567         cp_parser_skip_to_end_of_block_or_statement (parser);
13568       /* If the next token is not '}', then there are some enumerators.  */
13569       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13570         cp_parser_enumerator_list (parser, type);
13571
13572       /* Consume the final '}'.  */
13573       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13574
13575       if (scoped_enum_p)
13576         finish_scope ();
13577     }
13578   else
13579     {
13580       /* If a ';' follows, then it is an opaque-enum-specifier
13581         and additional restrictions apply.  */
13582       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13583         {
13584           if (is_anonymous)
13585             error_at (type_start_token->location,
13586                       "opaque-enum-specifier without name");
13587           else if (nested_name_specifier)
13588             error_at (type_start_token->location,
13589                       "opaque-enum-specifier must use a simple identifier");
13590         }
13591     }
13592
13593   /* Look for trailing attributes to apply to this enumeration, and
13594      apply them if appropriate.  */
13595   if (cp_parser_allow_gnu_extensions_p (parser))
13596     {
13597       tree trailing_attr = cp_parser_attributes_opt (parser);
13598       trailing_attr = chainon (trailing_attr, attributes);
13599       cplus_decl_attributes (&type,
13600                              trailing_attr,
13601                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13602     }
13603
13604   /* Finish up the enumeration.  */
13605   if (type != error_mark_node)
13606     {
13607       if (new_value_list)
13608         finish_enum_value_list (type);
13609       if (is_new_type)
13610         finish_enum (type);
13611     }
13612
13613   if (nested_name_specifier)
13614     {
13615       if (CLASS_TYPE_P (nested_name_specifier))
13616         {
13617           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13618           pop_scope (nested_name_specifier);
13619         }
13620       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13621         {
13622           pop_nested_namespace (nested_name_specifier);
13623         }
13624     }
13625   return type;
13626 }
13627
13628 /* Parse an enumerator-list.  The enumerators all have the indicated
13629    TYPE.
13630
13631    enumerator-list:
13632      enumerator-definition
13633      enumerator-list , enumerator-definition  */
13634
13635 static void
13636 cp_parser_enumerator_list (cp_parser* parser, tree type)
13637 {
13638   while (true)
13639     {
13640       /* Parse an enumerator-definition.  */
13641       cp_parser_enumerator_definition (parser, type);
13642
13643       /* If the next token is not a ',', we've reached the end of
13644          the list.  */
13645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13646         break;
13647       /* Otherwise, consume the `,' and keep going.  */
13648       cp_lexer_consume_token (parser->lexer);
13649       /* If the next token is a `}', there is a trailing comma.  */
13650       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13651         {
13652           if (!in_system_header)
13653             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13654           break;
13655         }
13656     }
13657 }
13658
13659 /* Parse an enumerator-definition.  The enumerator has the indicated
13660    TYPE.
13661
13662    enumerator-definition:
13663      enumerator
13664      enumerator = constant-expression
13665
13666    enumerator:
13667      identifier  */
13668
13669 static void
13670 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13671 {
13672   tree identifier;
13673   tree value;
13674   location_t loc;
13675
13676   /* Save the input location because we are interested in the location
13677      of the identifier and not the location of the explicit value.  */
13678   loc = cp_lexer_peek_token (parser->lexer)->location;
13679
13680   /* Look for the identifier.  */
13681   identifier = cp_parser_identifier (parser);
13682   if (identifier == error_mark_node)
13683     return;
13684
13685   /* If the next token is an '=', then there is an explicit value.  */
13686   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13687     {
13688       /* Consume the `=' token.  */
13689       cp_lexer_consume_token (parser->lexer);
13690       /* Parse the value.  */
13691       value = cp_parser_constant_expression (parser,
13692                                              /*allow_non_constant_p=*/false,
13693                                              NULL);
13694     }
13695   else
13696     value = NULL_TREE;
13697
13698   /* If we are processing a template, make sure the initializer of the
13699      enumerator doesn't contain any bare template parameter pack.  */
13700   if (check_for_bare_parameter_packs (value))
13701     value = error_mark_node;
13702
13703   /* Create the enumerator.  */
13704   build_enumerator (identifier, value, type, loc);
13705 }
13706
13707 /* Parse a namespace-name.
13708
13709    namespace-name:
13710      original-namespace-name
13711      namespace-alias
13712
13713    Returns the NAMESPACE_DECL for the namespace.  */
13714
13715 static tree
13716 cp_parser_namespace_name (cp_parser* parser)
13717 {
13718   tree identifier;
13719   tree namespace_decl;
13720
13721   cp_token *token = cp_lexer_peek_token (parser->lexer);
13722
13723   /* Get the name of the namespace.  */
13724   identifier = cp_parser_identifier (parser);
13725   if (identifier == error_mark_node)
13726     return error_mark_node;
13727
13728   /* Look up the identifier in the currently active scope.  Look only
13729      for namespaces, due to:
13730
13731        [basic.lookup.udir]
13732
13733        When looking up a namespace-name in a using-directive or alias
13734        definition, only namespace names are considered.
13735
13736      And:
13737
13738        [basic.lookup.qual]
13739
13740        During the lookup of a name preceding the :: scope resolution
13741        operator, object, function, and enumerator names are ignored.
13742
13743      (Note that cp_parser_qualifying_entity only calls this
13744      function if the token after the name is the scope resolution
13745      operator.)  */
13746   namespace_decl = cp_parser_lookup_name (parser, identifier,
13747                                           none_type,
13748                                           /*is_template=*/false,
13749                                           /*is_namespace=*/true,
13750                                           /*check_dependency=*/true,
13751                                           /*ambiguous_decls=*/NULL,
13752                                           token->location);
13753   /* If it's not a namespace, issue an error.  */
13754   if (namespace_decl == error_mark_node
13755       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13756     {
13757       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13758         error_at (token->location, "%qD is not a namespace-name", identifier);
13759       cp_parser_error (parser, "expected namespace-name");
13760       namespace_decl = error_mark_node;
13761     }
13762
13763   return namespace_decl;
13764 }
13765
13766 /* Parse a namespace-definition.
13767
13768    namespace-definition:
13769      named-namespace-definition
13770      unnamed-namespace-definition
13771
13772    named-namespace-definition:
13773      original-namespace-definition
13774      extension-namespace-definition
13775
13776    original-namespace-definition:
13777      namespace identifier { namespace-body }
13778
13779    extension-namespace-definition:
13780      namespace original-namespace-name { namespace-body }
13781
13782    unnamed-namespace-definition:
13783      namespace { namespace-body } */
13784
13785 static void
13786 cp_parser_namespace_definition (cp_parser* parser)
13787 {
13788   tree identifier, attribs;
13789   bool has_visibility;
13790   bool is_inline;
13791
13792   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13793     {
13794       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13795       is_inline = true;
13796       cp_lexer_consume_token (parser->lexer);
13797     }
13798   else
13799     is_inline = false;
13800
13801   /* Look for the `namespace' keyword.  */
13802   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13803
13804   /* Get the name of the namespace.  We do not attempt to distinguish
13805      between an original-namespace-definition and an
13806      extension-namespace-definition at this point.  The semantic
13807      analysis routines are responsible for that.  */
13808   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13809     identifier = cp_parser_identifier (parser);
13810   else
13811     identifier = NULL_TREE;
13812
13813   /* Parse any specified attributes.  */
13814   attribs = cp_parser_attributes_opt (parser);
13815
13816   /* Look for the `{' to start the namespace.  */
13817   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13818   /* Start the namespace.  */
13819   push_namespace (identifier);
13820
13821   /* "inline namespace" is equivalent to a stub namespace definition
13822      followed by a strong using directive.  */
13823   if (is_inline)
13824     {
13825       tree name_space = current_namespace;
13826       /* Set up namespace association.  */
13827       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13828         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13829                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13830       /* Import the contents of the inline namespace.  */
13831       pop_namespace ();
13832       do_using_directive (name_space);
13833       push_namespace (identifier);
13834     }
13835
13836   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13837
13838   /* Parse the body of the namespace.  */
13839   cp_parser_namespace_body (parser);
13840
13841   if (has_visibility)
13842     pop_visibility (1);
13843
13844   /* Finish the namespace.  */
13845   pop_namespace ();
13846   /* Look for the final `}'.  */
13847   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13848 }
13849
13850 /* Parse a namespace-body.
13851
13852    namespace-body:
13853      declaration-seq [opt]  */
13854
13855 static void
13856 cp_parser_namespace_body (cp_parser* parser)
13857 {
13858   cp_parser_declaration_seq_opt (parser);
13859 }
13860
13861 /* Parse a namespace-alias-definition.
13862
13863    namespace-alias-definition:
13864      namespace identifier = qualified-namespace-specifier ;  */
13865
13866 static void
13867 cp_parser_namespace_alias_definition (cp_parser* parser)
13868 {
13869   tree identifier;
13870   tree namespace_specifier;
13871
13872   cp_token *token = cp_lexer_peek_token (parser->lexer);
13873
13874   /* Look for the `namespace' keyword.  */
13875   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13876   /* Look for the identifier.  */
13877   identifier = cp_parser_identifier (parser);
13878   if (identifier == error_mark_node)
13879     return;
13880   /* Look for the `=' token.  */
13881   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13882       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13883     {
13884       error_at (token->location, "%<namespace%> definition is not allowed here");
13885       /* Skip the definition.  */
13886       cp_lexer_consume_token (parser->lexer);
13887       if (cp_parser_skip_to_closing_brace (parser))
13888         cp_lexer_consume_token (parser->lexer);
13889       return;
13890     }
13891   cp_parser_require (parser, CPP_EQ, RT_EQ);
13892   /* Look for the qualified-namespace-specifier.  */
13893   namespace_specifier
13894     = cp_parser_qualified_namespace_specifier (parser);
13895   /* Look for the `;' token.  */
13896   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13897
13898   /* Register the alias in the symbol table.  */
13899   do_namespace_alias (identifier, namespace_specifier);
13900 }
13901
13902 /* Parse a qualified-namespace-specifier.
13903
13904    qualified-namespace-specifier:
13905      :: [opt] nested-name-specifier [opt] namespace-name
13906
13907    Returns a NAMESPACE_DECL corresponding to the specified
13908    namespace.  */
13909
13910 static tree
13911 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13912 {
13913   /* Look for the optional `::'.  */
13914   cp_parser_global_scope_opt (parser,
13915                               /*current_scope_valid_p=*/false);
13916
13917   /* Look for the optional nested-name-specifier.  */
13918   cp_parser_nested_name_specifier_opt (parser,
13919                                        /*typename_keyword_p=*/false,
13920                                        /*check_dependency_p=*/true,
13921                                        /*type_p=*/false,
13922                                        /*is_declaration=*/true);
13923
13924   return cp_parser_namespace_name (parser);
13925 }
13926
13927 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13928    access declaration.
13929
13930    using-declaration:
13931      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13932      using :: unqualified-id ;  
13933
13934    access-declaration:
13935      qualified-id ;  
13936
13937    */
13938
13939 static bool
13940 cp_parser_using_declaration (cp_parser* parser, 
13941                              bool access_declaration_p)
13942 {
13943   cp_token *token;
13944   bool typename_p = false;
13945   bool global_scope_p;
13946   tree decl;
13947   tree identifier;
13948   tree qscope;
13949
13950   if (access_declaration_p)
13951     cp_parser_parse_tentatively (parser);
13952   else
13953     {
13954       /* Look for the `using' keyword.  */
13955       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13956       
13957       /* Peek at the next token.  */
13958       token = cp_lexer_peek_token (parser->lexer);
13959       /* See if it's `typename'.  */
13960       if (token->keyword == RID_TYPENAME)
13961         {
13962           /* Remember that we've seen it.  */
13963           typename_p = true;
13964           /* Consume the `typename' token.  */
13965           cp_lexer_consume_token (parser->lexer);
13966         }
13967     }
13968
13969   /* Look for the optional global scope qualification.  */
13970   global_scope_p
13971     = (cp_parser_global_scope_opt (parser,
13972                                    /*current_scope_valid_p=*/false)
13973        != NULL_TREE);
13974
13975   /* If we saw `typename', or didn't see `::', then there must be a
13976      nested-name-specifier present.  */
13977   if (typename_p || !global_scope_p)
13978     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13979                                               /*check_dependency_p=*/true,
13980                                               /*type_p=*/false,
13981                                               /*is_declaration=*/true);
13982   /* Otherwise, we could be in either of the two productions.  In that
13983      case, treat the nested-name-specifier as optional.  */
13984   else
13985     qscope = cp_parser_nested_name_specifier_opt (parser,
13986                                                   /*typename_keyword_p=*/false,
13987                                                   /*check_dependency_p=*/true,
13988                                                   /*type_p=*/false,
13989                                                   /*is_declaration=*/true);
13990   if (!qscope)
13991     qscope = global_namespace;
13992
13993   if (access_declaration_p && cp_parser_error_occurred (parser))
13994     /* Something has already gone wrong; there's no need to parse
13995        further.  Since an error has occurred, the return value of
13996        cp_parser_parse_definitely will be false, as required.  */
13997     return cp_parser_parse_definitely (parser);
13998
13999   token = cp_lexer_peek_token (parser->lexer);
14000   /* Parse the unqualified-id.  */
14001   identifier = cp_parser_unqualified_id (parser,
14002                                          /*template_keyword_p=*/false,
14003                                          /*check_dependency_p=*/true,
14004                                          /*declarator_p=*/true,
14005                                          /*optional_p=*/false);
14006
14007   if (access_declaration_p)
14008     {
14009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14010         cp_parser_simulate_error (parser);
14011       if (!cp_parser_parse_definitely (parser))
14012         return false;
14013     }
14014
14015   /* The function we call to handle a using-declaration is different
14016      depending on what scope we are in.  */
14017   if (qscope == error_mark_node || identifier == error_mark_node)
14018     ;
14019   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14020            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14021     /* [namespace.udecl]
14022
14023        A using declaration shall not name a template-id.  */
14024     error_at (token->location,
14025               "a template-id may not appear in a using-declaration");
14026   else
14027     {
14028       if (at_class_scope_p ())
14029         {
14030           /* Create the USING_DECL.  */
14031           decl = do_class_using_decl (parser->scope, identifier);
14032
14033           if (check_for_bare_parameter_packs (decl))
14034             return false;
14035           else
14036             /* Add it to the list of members in this class.  */
14037             finish_member_declaration (decl);
14038         }
14039       else
14040         {
14041           decl = cp_parser_lookup_name_simple (parser,
14042                                                identifier,
14043                                                token->location);
14044           if (decl == error_mark_node)
14045             cp_parser_name_lookup_error (parser, identifier,
14046                                          decl, NLE_NULL,
14047                                          token->location);
14048           else if (check_for_bare_parameter_packs (decl))
14049             return false;
14050           else if (!at_namespace_scope_p ())
14051             do_local_using_decl (decl, qscope, identifier);
14052           else
14053             do_toplevel_using_decl (decl, qscope, identifier);
14054         }
14055     }
14056
14057   /* Look for the final `;'.  */
14058   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14059   
14060   return true;
14061 }
14062
14063 /* Parse a using-directive.
14064
14065    using-directive:
14066      using namespace :: [opt] nested-name-specifier [opt]
14067        namespace-name ;  */
14068
14069 static void
14070 cp_parser_using_directive (cp_parser* parser)
14071 {
14072   tree namespace_decl;
14073   tree attribs;
14074
14075   /* Look for the `using' keyword.  */
14076   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14077   /* And the `namespace' keyword.  */
14078   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14079   /* Look for the optional `::' operator.  */
14080   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14081   /* And the optional nested-name-specifier.  */
14082   cp_parser_nested_name_specifier_opt (parser,
14083                                        /*typename_keyword_p=*/false,
14084                                        /*check_dependency_p=*/true,
14085                                        /*type_p=*/false,
14086                                        /*is_declaration=*/true);
14087   /* Get the namespace being used.  */
14088   namespace_decl = cp_parser_namespace_name (parser);
14089   /* And any specified attributes.  */
14090   attribs = cp_parser_attributes_opt (parser);
14091   /* Update the symbol table.  */
14092   parse_using_directive (namespace_decl, attribs);
14093   /* Look for the final `;'.  */
14094   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14095 }
14096
14097 /* Parse an asm-definition.
14098
14099    asm-definition:
14100      asm ( string-literal ) ;
14101
14102    GNU Extension:
14103
14104    asm-definition:
14105      asm volatile [opt] ( string-literal ) ;
14106      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14107      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14108                           : asm-operand-list [opt] ) ;
14109      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14110                           : asm-operand-list [opt]
14111                           : asm-clobber-list [opt] ) ;
14112      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14113                                : asm-clobber-list [opt]
14114                                : asm-goto-list ) ;  */
14115
14116 static void
14117 cp_parser_asm_definition (cp_parser* parser)
14118 {
14119   tree string;
14120   tree outputs = NULL_TREE;
14121   tree inputs = NULL_TREE;
14122   tree clobbers = NULL_TREE;
14123   tree labels = NULL_TREE;
14124   tree asm_stmt;
14125   bool volatile_p = false;
14126   bool extended_p = false;
14127   bool invalid_inputs_p = false;
14128   bool invalid_outputs_p = false;
14129   bool goto_p = false;
14130   required_token missing = RT_NONE;
14131
14132   /* Look for the `asm' keyword.  */
14133   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14134   /* See if the next token is `volatile'.  */
14135   if (cp_parser_allow_gnu_extensions_p (parser)
14136       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14137     {
14138       /* Remember that we saw the `volatile' keyword.  */
14139       volatile_p = true;
14140       /* Consume the token.  */
14141       cp_lexer_consume_token (parser->lexer);
14142     }
14143   if (cp_parser_allow_gnu_extensions_p (parser)
14144       && parser->in_function_body
14145       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14146     {
14147       /* Remember that we saw the `goto' keyword.  */
14148       goto_p = true;
14149       /* Consume the token.  */
14150       cp_lexer_consume_token (parser->lexer);
14151     }
14152   /* Look for the opening `('.  */
14153   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14154     return;
14155   /* Look for the string.  */
14156   string = cp_parser_string_literal (parser, false, false);
14157   if (string == error_mark_node)
14158     {
14159       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160                                              /*consume_paren=*/true);
14161       return;
14162     }
14163
14164   /* If we're allowing GNU extensions, check for the extended assembly
14165      syntax.  Unfortunately, the `:' tokens need not be separated by
14166      a space in C, and so, for compatibility, we tolerate that here
14167      too.  Doing that means that we have to treat the `::' operator as
14168      two `:' tokens.  */
14169   if (cp_parser_allow_gnu_extensions_p (parser)
14170       && parser->in_function_body
14171       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14172           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14173     {
14174       bool inputs_p = false;
14175       bool clobbers_p = false;
14176       bool labels_p = false;
14177
14178       /* The extended syntax was used.  */
14179       extended_p = true;
14180
14181       /* Look for outputs.  */
14182       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14183         {
14184           /* Consume the `:'.  */
14185           cp_lexer_consume_token (parser->lexer);
14186           /* Parse the output-operands.  */
14187           if (cp_lexer_next_token_is_not (parser->lexer,
14188                                           CPP_COLON)
14189               && cp_lexer_next_token_is_not (parser->lexer,
14190                                              CPP_SCOPE)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_CLOSE_PAREN)
14193               && !goto_p)
14194             outputs = cp_parser_asm_operand_list (parser);
14195
14196             if (outputs == error_mark_node)
14197               invalid_outputs_p = true;
14198         }
14199       /* If the next token is `::', there are no outputs, and the
14200          next token is the beginning of the inputs.  */
14201       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14202         /* The inputs are coming next.  */
14203         inputs_p = true;
14204
14205       /* Look for inputs.  */
14206       if (inputs_p
14207           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14208         {
14209           /* Consume the `:' or `::'.  */
14210           cp_lexer_consume_token (parser->lexer);
14211           /* Parse the output-operands.  */
14212           if (cp_lexer_next_token_is_not (parser->lexer,
14213                                           CPP_COLON)
14214               && cp_lexer_next_token_is_not (parser->lexer,
14215                                              CPP_SCOPE)
14216               && cp_lexer_next_token_is_not (parser->lexer,
14217                                              CPP_CLOSE_PAREN))
14218             inputs = cp_parser_asm_operand_list (parser);
14219
14220             if (inputs == error_mark_node)
14221               invalid_inputs_p = true;
14222         }
14223       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14224         /* The clobbers are coming next.  */
14225         clobbers_p = true;
14226
14227       /* Look for clobbers.  */
14228       if (clobbers_p
14229           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14230         {
14231           clobbers_p = true;
14232           /* Consume the `:' or `::'.  */
14233           cp_lexer_consume_token (parser->lexer);
14234           /* Parse the clobbers.  */
14235           if (cp_lexer_next_token_is_not (parser->lexer,
14236                                           CPP_COLON)
14237               && cp_lexer_next_token_is_not (parser->lexer,
14238                                              CPP_CLOSE_PAREN))
14239             clobbers = cp_parser_asm_clobber_list (parser);
14240         }
14241       else if (goto_p
14242                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14243         /* The labels are coming next.  */
14244         labels_p = true;
14245
14246       /* Look for labels.  */
14247       if (labels_p
14248           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14249         {
14250           labels_p = true;
14251           /* Consume the `:' or `::'.  */
14252           cp_lexer_consume_token (parser->lexer);
14253           /* Parse the labels.  */
14254           labels = cp_parser_asm_label_list (parser);
14255         }
14256
14257       if (goto_p && !labels_p)
14258         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14259     }
14260   else if (goto_p)
14261     missing = RT_COLON_SCOPE;
14262
14263   /* Look for the closing `)'.  */
14264   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14265                           missing ? missing : RT_CLOSE_PAREN))
14266     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14267                                            /*consume_paren=*/true);
14268   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14269
14270   if (!invalid_inputs_p && !invalid_outputs_p)
14271     {
14272       /* Create the ASM_EXPR.  */
14273       if (parser->in_function_body)
14274         {
14275           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14276                                       inputs, clobbers, labels);
14277           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14278           if (!extended_p)
14279             {
14280               tree temp = asm_stmt;
14281               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14282                 temp = TREE_OPERAND (temp, 0);
14283
14284               ASM_INPUT_P (temp) = 1;
14285             }
14286         }
14287       else
14288         cgraph_add_asm_node (string);
14289     }
14290 }
14291
14292 /* Declarators [gram.dcl.decl] */
14293
14294 /* Parse an init-declarator.
14295
14296    init-declarator:
14297      declarator initializer [opt]
14298
14299    GNU Extension:
14300
14301    init-declarator:
14302      declarator asm-specification [opt] attributes [opt] initializer [opt]
14303
14304    function-definition:
14305      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14306        function-body
14307      decl-specifier-seq [opt] declarator function-try-block
14308
14309    GNU Extension:
14310
14311    function-definition:
14312      __extension__ function-definition
14313
14314    The DECL_SPECIFIERS apply to this declarator.  Returns a
14315    representation of the entity declared.  If MEMBER_P is TRUE, then
14316    this declarator appears in a class scope.  The new DECL created by
14317    this declarator is returned.
14318
14319    The CHECKS are access checks that should be performed once we know
14320    what entity is being declared (and, therefore, what classes have
14321    befriended it).
14322
14323    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14324    for a function-definition here as well.  If the declarator is a
14325    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14326    be TRUE upon return.  By that point, the function-definition will
14327    have been completely parsed.
14328
14329    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14330    is FALSE.  */
14331
14332 static tree
14333 cp_parser_init_declarator (cp_parser* parser,
14334                            cp_decl_specifier_seq *decl_specifiers,
14335                            VEC (deferred_access_check,gc)* checks,
14336                            bool function_definition_allowed_p,
14337                            bool member_p,
14338                            int declares_class_or_enum,
14339                            bool* function_definition_p)
14340 {
14341   cp_token *token = NULL, *asm_spec_start_token = NULL,
14342            *attributes_start_token = NULL;
14343   cp_declarator *declarator;
14344   tree prefix_attributes;
14345   tree attributes;
14346   tree asm_specification;
14347   tree initializer;
14348   tree decl = NULL_TREE;
14349   tree scope;
14350   int is_initialized;
14351   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14352      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14353      "(...)".  */
14354   enum cpp_ttype initialization_kind;
14355   bool is_direct_init = false;
14356   bool is_non_constant_init;
14357   int ctor_dtor_or_conv_p;
14358   bool friend_p;
14359   tree pushed_scope = NULL;
14360
14361   /* Gather the attributes that were provided with the
14362      decl-specifiers.  */
14363   prefix_attributes = decl_specifiers->attributes;
14364
14365   /* Assume that this is not the declarator for a function
14366      definition.  */
14367   if (function_definition_p)
14368     *function_definition_p = false;
14369
14370   /* Defer access checks while parsing the declarator; we cannot know
14371      what names are accessible until we know what is being
14372      declared.  */
14373   resume_deferring_access_checks ();
14374
14375   /* Parse the declarator.  */
14376   token = cp_lexer_peek_token (parser->lexer);
14377   declarator
14378     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14379                             &ctor_dtor_or_conv_p,
14380                             /*parenthesized_p=*/NULL,
14381                             /*member_p=*/false);
14382   /* Gather up the deferred checks.  */
14383   stop_deferring_access_checks ();
14384
14385   /* If the DECLARATOR was erroneous, there's no need to go
14386      further.  */
14387   if (declarator == cp_error_declarator)
14388     return error_mark_node;
14389
14390   /* Check that the number of template-parameter-lists is OK.  */
14391   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14392                                                        token->location))
14393     return error_mark_node;
14394
14395   if (declares_class_or_enum & 2)
14396     cp_parser_check_for_definition_in_return_type (declarator,
14397                                                    decl_specifiers->type,
14398                                                    decl_specifiers->type_location);
14399
14400   /* Figure out what scope the entity declared by the DECLARATOR is
14401      located in.  `grokdeclarator' sometimes changes the scope, so
14402      we compute it now.  */
14403   scope = get_scope_of_declarator (declarator);
14404
14405   /* Perform any lookups in the declared type which were thought to be
14406      dependent, but are not in the scope of the declarator.  */
14407   decl_specifiers->type
14408     = maybe_update_decl_type (decl_specifiers->type, scope);
14409
14410   /* If we're allowing GNU extensions, look for an asm-specification
14411      and attributes.  */
14412   if (cp_parser_allow_gnu_extensions_p (parser))
14413     {
14414       /* Look for an asm-specification.  */
14415       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14416       asm_specification = cp_parser_asm_specification_opt (parser);
14417       /* And attributes.  */
14418       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14419       attributes = cp_parser_attributes_opt (parser);
14420     }
14421   else
14422     {
14423       asm_specification = NULL_TREE;
14424       attributes = NULL_TREE;
14425     }
14426
14427   /* Peek at the next token.  */
14428   token = cp_lexer_peek_token (parser->lexer);
14429   /* Check to see if the token indicates the start of a
14430      function-definition.  */
14431   if (function_declarator_p (declarator)
14432       && cp_parser_token_starts_function_definition_p (token))
14433     {
14434       if (!function_definition_allowed_p)
14435         {
14436           /* If a function-definition should not appear here, issue an
14437              error message.  */
14438           cp_parser_error (parser,
14439                            "a function-definition is not allowed here");
14440           return error_mark_node;
14441         }
14442       else
14443         {
14444           location_t func_brace_location
14445             = cp_lexer_peek_token (parser->lexer)->location;
14446
14447           /* Neither attributes nor an asm-specification are allowed
14448              on a function-definition.  */
14449           if (asm_specification)
14450             error_at (asm_spec_start_token->location,
14451                       "an asm-specification is not allowed "
14452                       "on a function-definition");
14453           if (attributes)
14454             error_at (attributes_start_token->location,
14455                       "attributes are not allowed on a function-definition");
14456           /* This is a function-definition.  */
14457           *function_definition_p = true;
14458
14459           /* Parse the function definition.  */
14460           if (member_p)
14461             decl = cp_parser_save_member_function_body (parser,
14462                                                         decl_specifiers,
14463                                                         declarator,
14464                                                         prefix_attributes);
14465           else
14466             decl
14467               = (cp_parser_function_definition_from_specifiers_and_declarator
14468                  (parser, decl_specifiers, prefix_attributes, declarator));
14469
14470           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14471             {
14472               /* This is where the prologue starts...  */
14473               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14474                 = func_brace_location;
14475             }
14476
14477           return decl;
14478         }
14479     }
14480
14481   /* [dcl.dcl]
14482
14483      Only in function declarations for constructors, destructors, and
14484      type conversions can the decl-specifier-seq be omitted.
14485
14486      We explicitly postpone this check past the point where we handle
14487      function-definitions because we tolerate function-definitions
14488      that are missing their return types in some modes.  */
14489   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14490     {
14491       cp_parser_error (parser,
14492                        "expected constructor, destructor, or type conversion");
14493       return error_mark_node;
14494     }
14495
14496   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14497   if (token->type == CPP_EQ
14498       || token->type == CPP_OPEN_PAREN
14499       || token->type == CPP_OPEN_BRACE)
14500     {
14501       is_initialized = SD_INITIALIZED;
14502       initialization_kind = token->type;
14503
14504       if (token->type == CPP_EQ
14505           && function_declarator_p (declarator))
14506         {
14507           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14508           if (t2->keyword == RID_DEFAULT)
14509             is_initialized = SD_DEFAULTED;
14510           else if (t2->keyword == RID_DELETE)
14511             is_initialized = SD_DELETED;
14512         }
14513     }
14514   else
14515     {
14516       /* If the init-declarator isn't initialized and isn't followed by a
14517          `,' or `;', it's not a valid init-declarator.  */
14518       if (token->type != CPP_COMMA
14519           && token->type != CPP_SEMICOLON)
14520         {
14521           cp_parser_error (parser, "expected initializer");
14522           return error_mark_node;
14523         }
14524       is_initialized = SD_UNINITIALIZED;
14525       initialization_kind = CPP_EOF;
14526     }
14527
14528   /* Because start_decl has side-effects, we should only call it if we
14529      know we're going ahead.  By this point, we know that we cannot
14530      possibly be looking at any other construct.  */
14531   cp_parser_commit_to_tentative_parse (parser);
14532
14533   /* If the decl specifiers were bad, issue an error now that we're
14534      sure this was intended to be a declarator.  Then continue
14535      declaring the variable(s), as int, to try to cut down on further
14536      errors.  */
14537   if (decl_specifiers->any_specifiers_p
14538       && decl_specifiers->type == error_mark_node)
14539     {
14540       cp_parser_error (parser, "invalid type in declaration");
14541       decl_specifiers->type = integer_type_node;
14542     }
14543
14544   /* Check to see whether or not this declaration is a friend.  */
14545   friend_p = cp_parser_friend_p (decl_specifiers);
14546
14547   /* Enter the newly declared entry in the symbol table.  If we're
14548      processing a declaration in a class-specifier, we wait until
14549      after processing the initializer.  */
14550   if (!member_p)
14551     {
14552       if (parser->in_unbraced_linkage_specification_p)
14553         decl_specifiers->storage_class = sc_extern;
14554       decl = start_decl (declarator, decl_specifiers,
14555                          is_initialized, attributes, prefix_attributes,
14556                          &pushed_scope);
14557       /* Adjust location of decl if declarator->id_loc is more appropriate:
14558          set, and decl wasn't merged with another decl, in which case its
14559          location would be different from input_location, and more accurate.  */
14560       if (DECL_P (decl)
14561           && declarator->id_loc != UNKNOWN_LOCATION
14562           && DECL_SOURCE_LOCATION (decl) == input_location)
14563         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14564     }
14565   else if (scope)
14566     /* Enter the SCOPE.  That way unqualified names appearing in the
14567        initializer will be looked up in SCOPE.  */
14568     pushed_scope = push_scope (scope);
14569
14570   /* Perform deferred access control checks, now that we know in which
14571      SCOPE the declared entity resides.  */
14572   if (!member_p && decl)
14573     {
14574       tree saved_current_function_decl = NULL_TREE;
14575
14576       /* If the entity being declared is a function, pretend that we
14577          are in its scope.  If it is a `friend', it may have access to
14578          things that would not otherwise be accessible.  */
14579       if (TREE_CODE (decl) == FUNCTION_DECL)
14580         {
14581           saved_current_function_decl = current_function_decl;
14582           current_function_decl = decl;
14583         }
14584
14585       /* Perform access checks for template parameters.  */
14586       cp_parser_perform_template_parameter_access_checks (checks);
14587
14588       /* Perform the access control checks for the declarator and the
14589          decl-specifiers.  */
14590       perform_deferred_access_checks ();
14591
14592       /* Restore the saved value.  */
14593       if (TREE_CODE (decl) == FUNCTION_DECL)
14594         current_function_decl = saved_current_function_decl;
14595     }
14596
14597   /* Parse the initializer.  */
14598   initializer = NULL_TREE;
14599   is_direct_init = false;
14600   is_non_constant_init = true;
14601   if (is_initialized)
14602     {
14603       if (function_declarator_p (declarator))
14604         {
14605           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14606            if (initialization_kind == CPP_EQ)
14607              initializer = cp_parser_pure_specifier (parser);
14608            else
14609              {
14610                /* If the declaration was erroneous, we don't really
14611                   know what the user intended, so just silently
14612                   consume the initializer.  */
14613                if (decl != error_mark_node)
14614                  error_at (initializer_start_token->location,
14615                            "initializer provided for function");
14616                cp_parser_skip_to_closing_parenthesis (parser,
14617                                                       /*recovering=*/true,
14618                                                       /*or_comma=*/false,
14619                                                       /*consume_paren=*/true);
14620              }
14621         }
14622       else
14623         {
14624           /* We want to record the extra mangling scope for in-class
14625              initializers of class members and initializers of static data
14626              member templates.  The former is a C++0x feature which isn't
14627              implemented yet, and I expect it will involve deferring
14628              parsing of the initializer until end of class as with default
14629              arguments.  So right here we only handle the latter.  */
14630           if (!member_p && processing_template_decl)
14631             start_lambda_scope (decl);
14632           initializer = cp_parser_initializer (parser,
14633                                                &is_direct_init,
14634                                                &is_non_constant_init);
14635           if (!member_p && processing_template_decl)
14636             finish_lambda_scope ();
14637         }
14638     }
14639
14640   /* The old parser allows attributes to appear after a parenthesized
14641      initializer.  Mark Mitchell proposed removing this functionality
14642      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14643      attributes -- but ignores them.  */
14644   if (cp_parser_allow_gnu_extensions_p (parser)
14645       && initialization_kind == CPP_OPEN_PAREN)
14646     if (cp_parser_attributes_opt (parser))
14647       warning (OPT_Wattributes,
14648                "attributes after parenthesized initializer ignored");
14649
14650   /* For an in-class declaration, use `grokfield' to create the
14651      declaration.  */
14652   if (member_p)
14653     {
14654       if (pushed_scope)
14655         {
14656           pop_scope (pushed_scope);
14657           pushed_scope = false;
14658         }
14659       decl = grokfield (declarator, decl_specifiers,
14660                         initializer, !is_non_constant_init,
14661                         /*asmspec=*/NULL_TREE,
14662                         prefix_attributes);
14663       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14664         cp_parser_save_default_args (parser, decl);
14665     }
14666
14667   /* Finish processing the declaration.  But, skip friend
14668      declarations.  */
14669   if (!friend_p && decl && decl != error_mark_node)
14670     {
14671       cp_finish_decl (decl,
14672                       initializer, !is_non_constant_init,
14673                       asm_specification,
14674                       /* If the initializer is in parentheses, then this is
14675                          a direct-initialization, which means that an
14676                          `explicit' constructor is OK.  Otherwise, an
14677                          `explicit' constructor cannot be used.  */
14678                       ((is_direct_init || !is_initialized)
14679                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14680     }
14681   else if ((cxx_dialect != cxx98) && friend_p
14682            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14683     /* Core issue #226 (C++0x only): A default template-argument
14684        shall not be specified in a friend class template
14685        declaration. */
14686     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14687                              /*is_partial=*/0, /*is_friend_decl=*/1);
14688
14689   if (!friend_p && pushed_scope)
14690     pop_scope (pushed_scope);
14691
14692   return decl;
14693 }
14694
14695 /* Parse a declarator.
14696
14697    declarator:
14698      direct-declarator
14699      ptr-operator declarator
14700
14701    abstract-declarator:
14702      ptr-operator abstract-declarator [opt]
14703      direct-abstract-declarator
14704
14705    GNU Extensions:
14706
14707    declarator:
14708      attributes [opt] direct-declarator
14709      attributes [opt] ptr-operator declarator
14710
14711    abstract-declarator:
14712      attributes [opt] ptr-operator abstract-declarator [opt]
14713      attributes [opt] direct-abstract-declarator
14714
14715    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14716    detect constructor, destructor or conversion operators. It is set
14717    to -1 if the declarator is a name, and +1 if it is a
14718    function. Otherwise it is set to zero. Usually you just want to
14719    test for >0, but internally the negative value is used.
14720
14721    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14722    a decl-specifier-seq unless it declares a constructor, destructor,
14723    or conversion.  It might seem that we could check this condition in
14724    semantic analysis, rather than parsing, but that makes it difficult
14725    to handle something like `f()'.  We want to notice that there are
14726    no decl-specifiers, and therefore realize that this is an
14727    expression, not a declaration.)
14728
14729    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14730    the declarator is a direct-declarator of the form "(...)".
14731
14732    MEMBER_P is true iff this declarator is a member-declarator.  */
14733
14734 static cp_declarator *
14735 cp_parser_declarator (cp_parser* parser,
14736                       cp_parser_declarator_kind dcl_kind,
14737                       int* ctor_dtor_or_conv_p,
14738                       bool* parenthesized_p,
14739                       bool member_p)
14740 {
14741   cp_declarator *declarator;
14742   enum tree_code code;
14743   cp_cv_quals cv_quals;
14744   tree class_type;
14745   tree attributes = NULL_TREE;
14746
14747   /* Assume this is not a constructor, destructor, or type-conversion
14748      operator.  */
14749   if (ctor_dtor_or_conv_p)
14750     *ctor_dtor_or_conv_p = 0;
14751
14752   if (cp_parser_allow_gnu_extensions_p (parser))
14753     attributes = cp_parser_attributes_opt (parser);
14754
14755   /* Check for the ptr-operator production.  */
14756   cp_parser_parse_tentatively (parser);
14757   /* Parse the ptr-operator.  */
14758   code = cp_parser_ptr_operator (parser,
14759                                  &class_type,
14760                                  &cv_quals);
14761   /* If that worked, then we have a ptr-operator.  */
14762   if (cp_parser_parse_definitely (parser))
14763     {
14764       /* If a ptr-operator was found, then this declarator was not
14765          parenthesized.  */
14766       if (parenthesized_p)
14767         *parenthesized_p = true;
14768       /* The dependent declarator is optional if we are parsing an
14769          abstract-declarator.  */
14770       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14771         cp_parser_parse_tentatively (parser);
14772
14773       /* Parse the dependent declarator.  */
14774       declarator = cp_parser_declarator (parser, dcl_kind,
14775                                          /*ctor_dtor_or_conv_p=*/NULL,
14776                                          /*parenthesized_p=*/NULL,
14777                                          /*member_p=*/false);
14778
14779       /* If we are parsing an abstract-declarator, we must handle the
14780          case where the dependent declarator is absent.  */
14781       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14782           && !cp_parser_parse_definitely (parser))
14783         declarator = NULL;
14784
14785       declarator = cp_parser_make_indirect_declarator
14786         (code, class_type, cv_quals, declarator);
14787     }
14788   /* Everything else is a direct-declarator.  */
14789   else
14790     {
14791       if (parenthesized_p)
14792         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14793                                                    CPP_OPEN_PAREN);
14794       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14795                                                 ctor_dtor_or_conv_p,
14796                                                 member_p);
14797     }
14798
14799   if (attributes && declarator && declarator != cp_error_declarator)
14800     declarator->attributes = attributes;
14801
14802   return declarator;
14803 }
14804
14805 /* Parse a direct-declarator or direct-abstract-declarator.
14806
14807    direct-declarator:
14808      declarator-id
14809      direct-declarator ( parameter-declaration-clause )
14810        cv-qualifier-seq [opt]
14811        exception-specification [opt]
14812      direct-declarator [ constant-expression [opt] ]
14813      ( declarator )
14814
14815    direct-abstract-declarator:
14816      direct-abstract-declarator [opt]
14817        ( parameter-declaration-clause )
14818        cv-qualifier-seq [opt]
14819        exception-specification [opt]
14820      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14821      ( abstract-declarator )
14822
14823    Returns a representation of the declarator.  DCL_KIND is
14824    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14825    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14826    we are parsing a direct-declarator.  It is
14827    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14828    of ambiguity we prefer an abstract declarator, as per
14829    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14830    cp_parser_declarator.  */
14831
14832 static cp_declarator *
14833 cp_parser_direct_declarator (cp_parser* parser,
14834                              cp_parser_declarator_kind dcl_kind,
14835                              int* ctor_dtor_or_conv_p,
14836                              bool member_p)
14837 {
14838   cp_token *token;
14839   cp_declarator *declarator = NULL;
14840   tree scope = NULL_TREE;
14841   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14842   bool saved_in_declarator_p = parser->in_declarator_p;
14843   bool first = true;
14844   tree pushed_scope = NULL_TREE;
14845
14846   while (true)
14847     {
14848       /* Peek at the next token.  */
14849       token = cp_lexer_peek_token (parser->lexer);
14850       if (token->type == CPP_OPEN_PAREN)
14851         {
14852           /* This is either a parameter-declaration-clause, or a
14853              parenthesized declarator. When we know we are parsing a
14854              named declarator, it must be a parenthesized declarator
14855              if FIRST is true. For instance, `(int)' is a
14856              parameter-declaration-clause, with an omitted
14857              direct-abstract-declarator. But `((*))', is a
14858              parenthesized abstract declarator. Finally, when T is a
14859              template parameter `(T)' is a
14860              parameter-declaration-clause, and not a parenthesized
14861              named declarator.
14862
14863              We first try and parse a parameter-declaration-clause,
14864              and then try a nested declarator (if FIRST is true).
14865
14866              It is not an error for it not to be a
14867              parameter-declaration-clause, even when FIRST is
14868              false. Consider,
14869
14870                int i (int);
14871                int i (3);
14872
14873              The first is the declaration of a function while the
14874              second is the definition of a variable, including its
14875              initializer.
14876
14877              Having seen only the parenthesis, we cannot know which of
14878              these two alternatives should be selected.  Even more
14879              complex are examples like:
14880
14881                int i (int (a));
14882                int i (int (3));
14883
14884              The former is a function-declaration; the latter is a
14885              variable initialization.
14886
14887              Thus again, we try a parameter-declaration-clause, and if
14888              that fails, we back out and return.  */
14889
14890           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14891             {
14892               tree params;
14893               unsigned saved_num_template_parameter_lists;
14894               bool is_declarator = false;
14895               tree t;
14896
14897               /* In a member-declarator, the only valid interpretation
14898                  of a parenthesis is the start of a
14899                  parameter-declaration-clause.  (It is invalid to
14900                  initialize a static data member with a parenthesized
14901                  initializer; only the "=" form of initialization is
14902                  permitted.)  */
14903               if (!member_p)
14904                 cp_parser_parse_tentatively (parser);
14905
14906               /* Consume the `('.  */
14907               cp_lexer_consume_token (parser->lexer);
14908               if (first)
14909                 {
14910                   /* If this is going to be an abstract declarator, we're
14911                      in a declarator and we can't have default args.  */
14912                   parser->default_arg_ok_p = false;
14913                   parser->in_declarator_p = true;
14914                 }
14915
14916               /* Inside the function parameter list, surrounding
14917                  template-parameter-lists do not apply.  */
14918               saved_num_template_parameter_lists
14919                 = parser->num_template_parameter_lists;
14920               parser->num_template_parameter_lists = 0;
14921
14922               begin_scope (sk_function_parms, NULL_TREE);
14923
14924               /* Parse the parameter-declaration-clause.  */
14925               params = cp_parser_parameter_declaration_clause (parser);
14926
14927               parser->num_template_parameter_lists
14928                 = saved_num_template_parameter_lists;
14929
14930               /* If all went well, parse the cv-qualifier-seq and the
14931                  exception-specification.  */
14932               if (member_p || cp_parser_parse_definitely (parser))
14933                 {
14934                   cp_cv_quals cv_quals;
14935                   tree exception_specification;
14936                   tree late_return;
14937
14938                   is_declarator = true;
14939
14940                   if (ctor_dtor_or_conv_p)
14941                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14942                   first = false;
14943                   /* Consume the `)'.  */
14944                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14945
14946                   /* Parse the cv-qualifier-seq.  */
14947                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14948                   /* And the exception-specification.  */
14949                   exception_specification
14950                     = cp_parser_exception_specification_opt (parser);
14951
14952                   late_return
14953                     = cp_parser_late_return_type_opt (parser);
14954
14955                   /* Create the function-declarator.  */
14956                   declarator = make_call_declarator (declarator,
14957                                                      params,
14958                                                      cv_quals,
14959                                                      exception_specification,
14960                                                      late_return);
14961                   /* Any subsequent parameter lists are to do with
14962                      return type, so are not those of the declared
14963                      function.  */
14964                   parser->default_arg_ok_p = false;
14965                 }
14966
14967               /* Remove the function parms from scope.  */
14968               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14969                 pop_binding (DECL_NAME (t), t);
14970               leave_scope();
14971
14972               if (is_declarator)
14973                 /* Repeat the main loop.  */
14974                 continue;
14975             }
14976
14977           /* If this is the first, we can try a parenthesized
14978              declarator.  */
14979           if (first)
14980             {
14981               bool saved_in_type_id_in_expr_p;
14982
14983               parser->default_arg_ok_p = saved_default_arg_ok_p;
14984               parser->in_declarator_p = saved_in_declarator_p;
14985
14986               /* Consume the `('.  */
14987               cp_lexer_consume_token (parser->lexer);
14988               /* Parse the nested declarator.  */
14989               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14990               parser->in_type_id_in_expr_p = true;
14991               declarator
14992                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14993                                         /*parenthesized_p=*/NULL,
14994                                         member_p);
14995               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14996               first = false;
14997               /* Expect a `)'.  */
14998               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14999                 declarator = cp_error_declarator;
15000               if (declarator == cp_error_declarator)
15001                 break;
15002
15003               goto handle_declarator;
15004             }
15005           /* Otherwise, we must be done.  */
15006           else
15007             break;
15008         }
15009       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15010                && token->type == CPP_OPEN_SQUARE)
15011         {
15012           /* Parse an array-declarator.  */
15013           tree bounds;
15014
15015           if (ctor_dtor_or_conv_p)
15016             *ctor_dtor_or_conv_p = 0;
15017
15018           first = false;
15019           parser->default_arg_ok_p = false;
15020           parser->in_declarator_p = true;
15021           /* Consume the `['.  */
15022           cp_lexer_consume_token (parser->lexer);
15023           /* Peek at the next token.  */
15024           token = cp_lexer_peek_token (parser->lexer);
15025           /* If the next token is `]', then there is no
15026              constant-expression.  */
15027           if (token->type != CPP_CLOSE_SQUARE)
15028             {
15029               bool non_constant_p;
15030
15031               bounds
15032                 = cp_parser_constant_expression (parser,
15033                                                  /*allow_non_constant=*/true,
15034                                                  &non_constant_p);
15035               if (!non_constant_p || cxx_dialect >= cxx0x)
15036                 /* OK */;
15037               /* Normally, the array bound must be an integral constant
15038                  expression.  However, as an extension, we allow VLAs
15039                  in function scopes as long as they aren't part of a
15040                  parameter declaration.  */
15041               else if (!parser->in_function_body
15042                        || current_binding_level->kind == sk_function_parms)
15043                 {
15044                   cp_parser_error (parser,
15045                                    "array bound is not an integer constant");
15046                   bounds = error_mark_node;
15047                 }
15048               else if (processing_template_decl && !error_operand_p (bounds))
15049                 {
15050                   /* Remember this wasn't a constant-expression.  */
15051                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15052                   TREE_SIDE_EFFECTS (bounds) = 1;
15053                 }
15054             }
15055           else
15056             bounds = NULL_TREE;
15057           /* Look for the closing `]'.  */
15058           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15059             {
15060               declarator = cp_error_declarator;
15061               break;
15062             }
15063
15064           declarator = make_array_declarator (declarator, bounds);
15065         }
15066       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15067         {
15068           {
15069             tree qualifying_scope;
15070             tree unqualified_name;
15071             special_function_kind sfk;
15072             bool abstract_ok;
15073             bool pack_expansion_p = false;
15074             cp_token *declarator_id_start_token;
15075
15076             /* Parse a declarator-id */
15077             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15078             if (abstract_ok)
15079               {
15080                 cp_parser_parse_tentatively (parser);
15081
15082                 /* If we see an ellipsis, we should be looking at a
15083                    parameter pack. */
15084                 if (token->type == CPP_ELLIPSIS)
15085                   {
15086                     /* Consume the `...' */
15087                     cp_lexer_consume_token (parser->lexer);
15088
15089                     pack_expansion_p = true;
15090                   }
15091               }
15092
15093             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15094             unqualified_name
15095               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15096             qualifying_scope = parser->scope;
15097             if (abstract_ok)
15098               {
15099                 bool okay = false;
15100
15101                 if (!unqualified_name && pack_expansion_p)
15102                   {
15103                     /* Check whether an error occurred. */
15104                     okay = !cp_parser_error_occurred (parser);
15105
15106                     /* We already consumed the ellipsis to mark a
15107                        parameter pack, but we have no way to report it,
15108                        so abort the tentative parse. We will be exiting
15109                        immediately anyway. */
15110                     cp_parser_abort_tentative_parse (parser);
15111                   }
15112                 else
15113                   okay = cp_parser_parse_definitely (parser);
15114
15115                 if (!okay)
15116                   unqualified_name = error_mark_node;
15117                 else if (unqualified_name
15118                          && (qualifying_scope
15119                              || (TREE_CODE (unqualified_name)
15120                                  != IDENTIFIER_NODE)))
15121                   {
15122                     cp_parser_error (parser, "expected unqualified-id");
15123                     unqualified_name = error_mark_node;
15124                   }
15125               }
15126
15127             if (!unqualified_name)
15128               return NULL;
15129             if (unqualified_name == error_mark_node)
15130               {
15131                 declarator = cp_error_declarator;
15132                 pack_expansion_p = false;
15133                 declarator->parameter_pack_p = false;
15134                 break;
15135               }
15136
15137             if (qualifying_scope && at_namespace_scope_p ()
15138                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15139               {
15140                 /* In the declaration of a member of a template class
15141                    outside of the class itself, the SCOPE will sometimes
15142                    be a TYPENAME_TYPE.  For example, given:
15143
15144                    template <typename T>
15145                    int S<T>::R::i = 3;
15146
15147                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15148                    this context, we must resolve S<T>::R to an ordinary
15149                    type, rather than a typename type.
15150
15151                    The reason we normally avoid resolving TYPENAME_TYPEs
15152                    is that a specialization of `S' might render
15153                    `S<T>::R' not a type.  However, if `S' is
15154                    specialized, then this `i' will not be used, so there
15155                    is no harm in resolving the types here.  */
15156                 tree type;
15157
15158                 /* Resolve the TYPENAME_TYPE.  */
15159                 type = resolve_typename_type (qualifying_scope,
15160                                               /*only_current_p=*/false);
15161                 /* If that failed, the declarator is invalid.  */
15162                 if (TREE_CODE (type) == TYPENAME_TYPE)
15163                   {
15164                     if (typedef_variant_p (type))
15165                       error_at (declarator_id_start_token->location,
15166                                 "cannot define member of dependent typedef "
15167                                 "%qT", type);
15168                     else
15169                       error_at (declarator_id_start_token->location,
15170                                 "%<%T::%E%> is not a type",
15171                                 TYPE_CONTEXT (qualifying_scope),
15172                                 TYPE_IDENTIFIER (qualifying_scope));
15173                   }
15174                 qualifying_scope = type;
15175               }
15176
15177             sfk = sfk_none;
15178
15179             if (unqualified_name)
15180               {
15181                 tree class_type;
15182
15183                 if (qualifying_scope
15184                     && CLASS_TYPE_P (qualifying_scope))
15185                   class_type = qualifying_scope;
15186                 else
15187                   class_type = current_class_type;
15188
15189                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15190                   {
15191                     tree name_type = TREE_TYPE (unqualified_name);
15192                     if (class_type && same_type_p (name_type, class_type))
15193                       {
15194                         if (qualifying_scope
15195                             && CLASSTYPE_USE_TEMPLATE (name_type))
15196                           {
15197                             error_at (declarator_id_start_token->location,
15198                                       "invalid use of constructor as a template");
15199                             inform (declarator_id_start_token->location,
15200                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15201                                     "name the constructor in a qualified name",
15202                                     class_type,
15203                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15204                                     class_type, name_type);
15205                             declarator = cp_error_declarator;
15206                             break;
15207                           }
15208                         else
15209                           unqualified_name = constructor_name (class_type);
15210                       }
15211                     else
15212                       {
15213                         /* We do not attempt to print the declarator
15214                            here because we do not have enough
15215                            information about its original syntactic
15216                            form.  */
15217                         cp_parser_error (parser, "invalid declarator");
15218                         declarator = cp_error_declarator;
15219                         break;
15220                       }
15221                   }
15222
15223                 if (class_type)
15224                   {
15225                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15226                       sfk = sfk_destructor;
15227                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15228                       sfk = sfk_conversion;
15229                     else if (/* There's no way to declare a constructor
15230                                 for an anonymous type, even if the type
15231                                 got a name for linkage purposes.  */
15232                              !TYPE_WAS_ANONYMOUS (class_type)
15233                              && constructor_name_p (unqualified_name,
15234                                                     class_type))
15235                       {
15236                         unqualified_name = constructor_name (class_type);
15237                         sfk = sfk_constructor;
15238                       }
15239                     else if (is_overloaded_fn (unqualified_name)
15240                              && DECL_CONSTRUCTOR_P (get_first_fn
15241                                                     (unqualified_name)))
15242                       sfk = sfk_constructor;
15243
15244                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15245                       *ctor_dtor_or_conv_p = -1;
15246                   }
15247               }
15248             declarator = make_id_declarator (qualifying_scope,
15249                                              unqualified_name,
15250                                              sfk);
15251             declarator->id_loc = token->location;
15252             declarator->parameter_pack_p = pack_expansion_p;
15253
15254             if (pack_expansion_p)
15255               maybe_warn_variadic_templates ();
15256           }
15257
15258         handle_declarator:;
15259           scope = get_scope_of_declarator (declarator);
15260           if (scope)
15261             /* Any names that appear after the declarator-id for a
15262                member are looked up in the containing scope.  */
15263             pushed_scope = push_scope (scope);
15264           parser->in_declarator_p = true;
15265           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15266               || (declarator && declarator->kind == cdk_id))
15267             /* Default args are only allowed on function
15268                declarations.  */
15269             parser->default_arg_ok_p = saved_default_arg_ok_p;
15270           else
15271             parser->default_arg_ok_p = false;
15272
15273           first = false;
15274         }
15275       /* We're done.  */
15276       else
15277         break;
15278     }
15279
15280   /* For an abstract declarator, we might wind up with nothing at this
15281      point.  That's an error; the declarator is not optional.  */
15282   if (!declarator)
15283     cp_parser_error (parser, "expected declarator");
15284
15285   /* If we entered a scope, we must exit it now.  */
15286   if (pushed_scope)
15287     pop_scope (pushed_scope);
15288
15289   parser->default_arg_ok_p = saved_default_arg_ok_p;
15290   parser->in_declarator_p = saved_in_declarator_p;
15291
15292   return declarator;
15293 }
15294
15295 /* Parse a ptr-operator.
15296
15297    ptr-operator:
15298      * cv-qualifier-seq [opt]
15299      &
15300      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15301
15302    GNU Extension:
15303
15304    ptr-operator:
15305      & cv-qualifier-seq [opt]
15306
15307    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15308    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15309    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15310    filled in with the TYPE containing the member.  *CV_QUALS is
15311    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15312    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15313    Note that the tree codes returned by this function have nothing
15314    to do with the types of trees that will be eventually be created
15315    to represent the pointer or reference type being parsed. They are
15316    just constants with suggestive names. */
15317 static enum tree_code
15318 cp_parser_ptr_operator (cp_parser* parser,
15319                         tree* type,
15320                         cp_cv_quals *cv_quals)
15321 {
15322   enum tree_code code = ERROR_MARK;
15323   cp_token *token;
15324
15325   /* Assume that it's not a pointer-to-member.  */
15326   *type = NULL_TREE;
15327   /* And that there are no cv-qualifiers.  */
15328   *cv_quals = TYPE_UNQUALIFIED;
15329
15330   /* Peek at the next token.  */
15331   token = cp_lexer_peek_token (parser->lexer);
15332
15333   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15334   if (token->type == CPP_MULT)
15335     code = INDIRECT_REF;
15336   else if (token->type == CPP_AND)
15337     code = ADDR_EXPR;
15338   else if ((cxx_dialect != cxx98) &&
15339            token->type == CPP_AND_AND) /* C++0x only */
15340     code = NON_LVALUE_EXPR;
15341
15342   if (code != ERROR_MARK)
15343     {
15344       /* Consume the `*', `&' or `&&'.  */
15345       cp_lexer_consume_token (parser->lexer);
15346
15347       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15348          `&', if we are allowing GNU extensions.  (The only qualifier
15349          that can legally appear after `&' is `restrict', but that is
15350          enforced during semantic analysis.  */
15351       if (code == INDIRECT_REF
15352           || cp_parser_allow_gnu_extensions_p (parser))
15353         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15354     }
15355   else
15356     {
15357       /* Try the pointer-to-member case.  */
15358       cp_parser_parse_tentatively (parser);
15359       /* Look for the optional `::' operator.  */
15360       cp_parser_global_scope_opt (parser,
15361                                   /*current_scope_valid_p=*/false);
15362       /* Look for the nested-name specifier.  */
15363       token = cp_lexer_peek_token (parser->lexer);
15364       cp_parser_nested_name_specifier (parser,
15365                                        /*typename_keyword_p=*/false,
15366                                        /*check_dependency_p=*/true,
15367                                        /*type_p=*/false,
15368                                        /*is_declaration=*/false);
15369       /* If we found it, and the next token is a `*', then we are
15370          indeed looking at a pointer-to-member operator.  */
15371       if (!cp_parser_error_occurred (parser)
15372           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15373         {
15374           /* Indicate that the `*' operator was used.  */
15375           code = INDIRECT_REF;
15376
15377           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15378             error_at (token->location, "%qD is a namespace", parser->scope);
15379           else
15380             {
15381               /* The type of which the member is a member is given by the
15382                  current SCOPE.  */
15383               *type = parser->scope;
15384               /* The next name will not be qualified.  */
15385               parser->scope = NULL_TREE;
15386               parser->qualifying_scope = NULL_TREE;
15387               parser->object_scope = NULL_TREE;
15388               /* Look for the optional cv-qualifier-seq.  */
15389               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15390             }
15391         }
15392       /* If that didn't work we don't have a ptr-operator.  */
15393       if (!cp_parser_parse_definitely (parser))
15394         cp_parser_error (parser, "expected ptr-operator");
15395     }
15396
15397   return code;
15398 }
15399
15400 /* Parse an (optional) cv-qualifier-seq.
15401
15402    cv-qualifier-seq:
15403      cv-qualifier cv-qualifier-seq [opt]
15404
15405    cv-qualifier:
15406      const
15407      volatile
15408
15409    GNU Extension:
15410
15411    cv-qualifier:
15412      __restrict__
15413
15414    Returns a bitmask representing the cv-qualifiers.  */
15415
15416 static cp_cv_quals
15417 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15418 {
15419   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15420
15421   while (true)
15422     {
15423       cp_token *token;
15424       cp_cv_quals cv_qualifier;
15425
15426       /* Peek at the next token.  */
15427       token = cp_lexer_peek_token (parser->lexer);
15428       /* See if it's a cv-qualifier.  */
15429       switch (token->keyword)
15430         {
15431         case RID_CONST:
15432           cv_qualifier = TYPE_QUAL_CONST;
15433           break;
15434
15435         case RID_VOLATILE:
15436           cv_qualifier = TYPE_QUAL_VOLATILE;
15437           break;
15438
15439         case RID_RESTRICT:
15440           cv_qualifier = TYPE_QUAL_RESTRICT;
15441           break;
15442
15443         default:
15444           cv_qualifier = TYPE_UNQUALIFIED;
15445           break;
15446         }
15447
15448       if (!cv_qualifier)
15449         break;
15450
15451       if (cv_quals & cv_qualifier)
15452         {
15453           error_at (token->location, "duplicate cv-qualifier");
15454           cp_lexer_purge_token (parser->lexer);
15455         }
15456       else
15457         {
15458           cp_lexer_consume_token (parser->lexer);
15459           cv_quals |= cv_qualifier;
15460         }
15461     }
15462
15463   return cv_quals;
15464 }
15465
15466 /* Parse a late-specified return type, if any.  This is not a separate
15467    non-terminal, but part of a function declarator, which looks like
15468
15469    -> trailing-type-specifier-seq abstract-declarator(opt)
15470
15471    Returns the type indicated by the type-id.  */
15472
15473 static tree
15474 cp_parser_late_return_type_opt (cp_parser* parser)
15475 {
15476   cp_token *token;
15477
15478   /* Peek at the next token.  */
15479   token = cp_lexer_peek_token (parser->lexer);
15480   /* A late-specified return type is indicated by an initial '->'. */
15481   if (token->type != CPP_DEREF)
15482     return NULL_TREE;
15483
15484   /* Consume the ->.  */
15485   cp_lexer_consume_token (parser->lexer);
15486
15487   return cp_parser_trailing_type_id (parser);
15488 }
15489
15490 /* Parse a declarator-id.
15491
15492    declarator-id:
15493      id-expression
15494      :: [opt] nested-name-specifier [opt] type-name
15495
15496    In the `id-expression' case, the value returned is as for
15497    cp_parser_id_expression if the id-expression was an unqualified-id.
15498    If the id-expression was a qualified-id, then a SCOPE_REF is
15499    returned.  The first operand is the scope (either a NAMESPACE_DECL
15500    or TREE_TYPE), but the second is still just a representation of an
15501    unqualified-id.  */
15502
15503 static tree
15504 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15505 {
15506   tree id;
15507   /* The expression must be an id-expression.  Assume that qualified
15508      names are the names of types so that:
15509
15510        template <class T>
15511        int S<T>::R::i = 3;
15512
15513      will work; we must treat `S<T>::R' as the name of a type.
15514      Similarly, assume that qualified names are templates, where
15515      required, so that:
15516
15517        template <class T>
15518        int S<T>::R<T>::i = 3;
15519
15520      will work, too.  */
15521   id = cp_parser_id_expression (parser,
15522                                 /*template_keyword_p=*/false,
15523                                 /*check_dependency_p=*/false,
15524                                 /*template_p=*/NULL,
15525                                 /*declarator_p=*/true,
15526                                 optional_p);
15527   if (id && BASELINK_P (id))
15528     id = BASELINK_FUNCTIONS (id);
15529   return id;
15530 }
15531
15532 /* Parse a type-id.
15533
15534    type-id:
15535      type-specifier-seq abstract-declarator [opt]
15536
15537    Returns the TYPE specified.  */
15538
15539 static tree
15540 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15541                      bool is_trailing_return)
15542 {
15543   cp_decl_specifier_seq type_specifier_seq;
15544   cp_declarator *abstract_declarator;
15545
15546   /* Parse the type-specifier-seq.  */
15547   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15548                                 is_trailing_return,
15549                                 &type_specifier_seq);
15550   if (type_specifier_seq.type == error_mark_node)
15551     return error_mark_node;
15552
15553   /* There might or might not be an abstract declarator.  */
15554   cp_parser_parse_tentatively (parser);
15555   /* Look for the declarator.  */
15556   abstract_declarator
15557     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15558                             /*parenthesized_p=*/NULL,
15559                             /*member_p=*/false);
15560   /* Check to see if there really was a declarator.  */
15561   if (!cp_parser_parse_definitely (parser))
15562     abstract_declarator = NULL;
15563
15564   if (type_specifier_seq.type
15565       && type_uses_auto (type_specifier_seq.type))
15566     {
15567       /* A type-id with type 'auto' is only ok if the abstract declarator
15568          is a function declarator with a late-specified return type.  */
15569       if (abstract_declarator
15570           && abstract_declarator->kind == cdk_function
15571           && abstract_declarator->u.function.late_return_type)
15572         /* OK */;
15573       else
15574         {
15575           error ("invalid use of %<auto%>");
15576           return error_mark_node;
15577         }
15578     }
15579   
15580   return groktypename (&type_specifier_seq, abstract_declarator,
15581                        is_template_arg);
15582 }
15583
15584 static tree cp_parser_type_id (cp_parser *parser)
15585 {
15586   return cp_parser_type_id_1 (parser, false, false);
15587 }
15588
15589 static tree cp_parser_template_type_arg (cp_parser *parser)
15590 {
15591   return cp_parser_type_id_1 (parser, true, false);
15592 }
15593
15594 static tree cp_parser_trailing_type_id (cp_parser *parser)
15595 {
15596   return cp_parser_type_id_1 (parser, false, true);
15597 }
15598
15599 /* Parse a type-specifier-seq.
15600
15601    type-specifier-seq:
15602      type-specifier type-specifier-seq [opt]
15603
15604    GNU extension:
15605
15606    type-specifier-seq:
15607      attributes type-specifier-seq [opt]
15608
15609    If IS_DECLARATION is true, we are at the start of a "condition" or
15610    exception-declaration, so we might be followed by a declarator-id.
15611
15612    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15613    i.e. we've just seen "->".
15614
15615    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15616
15617 static void
15618 cp_parser_type_specifier_seq (cp_parser* parser,
15619                               bool is_declaration,
15620                               bool is_trailing_return,
15621                               cp_decl_specifier_seq *type_specifier_seq)
15622 {
15623   bool seen_type_specifier = false;
15624   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15625   cp_token *start_token = NULL;
15626
15627   /* Clear the TYPE_SPECIFIER_SEQ.  */
15628   clear_decl_specs (type_specifier_seq);
15629
15630   /* In the context of a trailing return type, enum E { } is an
15631      elaborated-type-specifier followed by a function-body, not an
15632      enum-specifier.  */
15633   if (is_trailing_return)
15634     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15635
15636   /* Parse the type-specifiers and attributes.  */
15637   while (true)
15638     {
15639       tree type_specifier;
15640       bool is_cv_qualifier;
15641
15642       /* Check for attributes first.  */
15643       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15644         {
15645           type_specifier_seq->attributes =
15646             chainon (type_specifier_seq->attributes,
15647                      cp_parser_attributes_opt (parser));
15648           continue;
15649         }
15650
15651       /* record the token of the beginning of the type specifier seq,
15652          for error reporting purposes*/
15653      if (!start_token)
15654        start_token = cp_lexer_peek_token (parser->lexer);
15655
15656       /* Look for the type-specifier.  */
15657       type_specifier = cp_parser_type_specifier (parser,
15658                                                  flags,
15659                                                  type_specifier_seq,
15660                                                  /*is_declaration=*/false,
15661                                                  NULL,
15662                                                  &is_cv_qualifier);
15663       if (!type_specifier)
15664         {
15665           /* If the first type-specifier could not be found, this is not a
15666              type-specifier-seq at all.  */
15667           if (!seen_type_specifier)
15668             {
15669               cp_parser_error (parser, "expected type-specifier");
15670               type_specifier_seq->type = error_mark_node;
15671               return;
15672             }
15673           /* If subsequent type-specifiers could not be found, the
15674              type-specifier-seq is complete.  */
15675           break;
15676         }
15677
15678       seen_type_specifier = true;
15679       /* The standard says that a condition can be:
15680
15681             type-specifier-seq declarator = assignment-expression
15682
15683          However, given:
15684
15685            struct S {};
15686            if (int S = ...)
15687
15688          we should treat the "S" as a declarator, not as a
15689          type-specifier.  The standard doesn't say that explicitly for
15690          type-specifier-seq, but it does say that for
15691          decl-specifier-seq in an ordinary declaration.  Perhaps it
15692          would be clearer just to allow a decl-specifier-seq here, and
15693          then add a semantic restriction that if any decl-specifiers
15694          that are not type-specifiers appear, the program is invalid.  */
15695       if (is_declaration && !is_cv_qualifier)
15696         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15697     }
15698
15699   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15700 }
15701
15702 /* Parse a parameter-declaration-clause.
15703
15704    parameter-declaration-clause:
15705      parameter-declaration-list [opt] ... [opt]
15706      parameter-declaration-list , ...
15707
15708    Returns a representation for the parameter declarations.  A return
15709    value of NULL indicates a parameter-declaration-clause consisting
15710    only of an ellipsis.  */
15711
15712 static tree
15713 cp_parser_parameter_declaration_clause (cp_parser* parser)
15714 {
15715   tree parameters;
15716   cp_token *token;
15717   bool ellipsis_p;
15718   bool is_error;
15719
15720   /* Peek at the next token.  */
15721   token = cp_lexer_peek_token (parser->lexer);
15722   /* Check for trivial parameter-declaration-clauses.  */
15723   if (token->type == CPP_ELLIPSIS)
15724     {
15725       /* Consume the `...' token.  */
15726       cp_lexer_consume_token (parser->lexer);
15727       return NULL_TREE;
15728     }
15729   else if (token->type == CPP_CLOSE_PAREN)
15730     /* There are no parameters.  */
15731     {
15732 #ifndef NO_IMPLICIT_EXTERN_C
15733       if (in_system_header && current_class_type == NULL
15734           && current_lang_name == lang_name_c)
15735         return NULL_TREE;
15736       else
15737 #endif
15738         return void_list_node;
15739     }
15740   /* Check for `(void)', too, which is a special case.  */
15741   else if (token->keyword == RID_VOID
15742            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15743                == CPP_CLOSE_PAREN))
15744     {
15745       /* Consume the `void' token.  */
15746       cp_lexer_consume_token (parser->lexer);
15747       /* There are no parameters.  */
15748       return void_list_node;
15749     }
15750
15751   /* Parse the parameter-declaration-list.  */
15752   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15753   /* If a parse error occurred while parsing the
15754      parameter-declaration-list, then the entire
15755      parameter-declaration-clause is erroneous.  */
15756   if (is_error)
15757     return NULL;
15758
15759   /* Peek at the next token.  */
15760   token = cp_lexer_peek_token (parser->lexer);
15761   /* If it's a `,', the clause should terminate with an ellipsis.  */
15762   if (token->type == CPP_COMMA)
15763     {
15764       /* Consume the `,'.  */
15765       cp_lexer_consume_token (parser->lexer);
15766       /* Expect an ellipsis.  */
15767       ellipsis_p
15768         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15769     }
15770   /* It might also be `...' if the optional trailing `,' was
15771      omitted.  */
15772   else if (token->type == CPP_ELLIPSIS)
15773     {
15774       /* Consume the `...' token.  */
15775       cp_lexer_consume_token (parser->lexer);
15776       /* And remember that we saw it.  */
15777       ellipsis_p = true;
15778     }
15779   else
15780     ellipsis_p = false;
15781
15782   /* Finish the parameter list.  */
15783   if (!ellipsis_p)
15784     parameters = chainon (parameters, void_list_node);
15785
15786   return parameters;
15787 }
15788
15789 /* Parse a parameter-declaration-list.
15790
15791    parameter-declaration-list:
15792      parameter-declaration
15793      parameter-declaration-list , parameter-declaration
15794
15795    Returns a representation of the parameter-declaration-list, as for
15796    cp_parser_parameter_declaration_clause.  However, the
15797    `void_list_node' is never appended to the list.  Upon return,
15798    *IS_ERROR will be true iff an error occurred.  */
15799
15800 static tree
15801 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15802 {
15803   tree parameters = NULL_TREE;
15804   tree *tail = &parameters; 
15805   bool saved_in_unbraced_linkage_specification_p;
15806   int index = 0;
15807
15808   /* Assume all will go well.  */
15809   *is_error = false;
15810   /* The special considerations that apply to a function within an
15811      unbraced linkage specifications do not apply to the parameters
15812      to the function.  */
15813   saved_in_unbraced_linkage_specification_p 
15814     = parser->in_unbraced_linkage_specification_p;
15815   parser->in_unbraced_linkage_specification_p = false;
15816
15817   /* Look for more parameters.  */
15818   while (true)
15819     {
15820       cp_parameter_declarator *parameter;
15821       tree decl = error_mark_node;
15822       bool parenthesized_p;
15823       /* Parse the parameter.  */
15824       parameter
15825         = cp_parser_parameter_declaration (parser,
15826                                            /*template_parm_p=*/false,
15827                                            &parenthesized_p);
15828
15829       /* We don't know yet if the enclosing context is deprecated, so wait
15830          and warn in grokparms if appropriate.  */
15831       deprecated_state = DEPRECATED_SUPPRESS;
15832
15833       if (parameter)
15834         decl = grokdeclarator (parameter->declarator,
15835                                &parameter->decl_specifiers,
15836                                PARM,
15837                                parameter->default_argument != NULL_TREE,
15838                                &parameter->decl_specifiers.attributes);
15839
15840       deprecated_state = DEPRECATED_NORMAL;
15841
15842       /* If a parse error occurred parsing the parameter declaration,
15843          then the entire parameter-declaration-list is erroneous.  */
15844       if (decl == error_mark_node)
15845         {
15846           *is_error = true;
15847           parameters = error_mark_node;
15848           break;
15849         }
15850
15851       if (parameter->decl_specifiers.attributes)
15852         cplus_decl_attributes (&decl,
15853                                parameter->decl_specifiers.attributes,
15854                                0);
15855       if (DECL_NAME (decl))
15856         decl = pushdecl (decl);
15857
15858       if (decl != error_mark_node)
15859         {
15860           retrofit_lang_decl (decl);
15861           DECL_PARM_INDEX (decl) = ++index;
15862         }
15863
15864       /* Add the new parameter to the list.  */
15865       *tail = build_tree_list (parameter->default_argument, decl);
15866       tail = &TREE_CHAIN (*tail);
15867
15868       /* Peek at the next token.  */
15869       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15870           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15871           /* These are for Objective-C++ */
15872           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15873           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15874         /* The parameter-declaration-list is complete.  */
15875         break;
15876       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15877         {
15878           cp_token *token;
15879
15880           /* Peek at the next token.  */
15881           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15882           /* If it's an ellipsis, then the list is complete.  */
15883           if (token->type == CPP_ELLIPSIS)
15884             break;
15885           /* Otherwise, there must be more parameters.  Consume the
15886              `,'.  */
15887           cp_lexer_consume_token (parser->lexer);
15888           /* When parsing something like:
15889
15890                 int i(float f, double d)
15891
15892              we can tell after seeing the declaration for "f" that we
15893              are not looking at an initialization of a variable "i",
15894              but rather at the declaration of a function "i".
15895
15896              Due to the fact that the parsing of template arguments
15897              (as specified to a template-id) requires backtracking we
15898              cannot use this technique when inside a template argument
15899              list.  */
15900           if (!parser->in_template_argument_list_p
15901               && !parser->in_type_id_in_expr_p
15902               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15903               /* However, a parameter-declaration of the form
15904                  "foat(f)" (which is a valid declaration of a
15905                  parameter "f") can also be interpreted as an
15906                  expression (the conversion of "f" to "float").  */
15907               && !parenthesized_p)
15908             cp_parser_commit_to_tentative_parse (parser);
15909         }
15910       else
15911         {
15912           cp_parser_error (parser, "expected %<,%> or %<...%>");
15913           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15914             cp_parser_skip_to_closing_parenthesis (parser,
15915                                                    /*recovering=*/true,
15916                                                    /*or_comma=*/false,
15917                                                    /*consume_paren=*/false);
15918           break;
15919         }
15920     }
15921
15922   parser->in_unbraced_linkage_specification_p
15923     = saved_in_unbraced_linkage_specification_p;
15924
15925   return parameters;
15926 }
15927
15928 /* Parse a parameter declaration.
15929
15930    parameter-declaration:
15931      decl-specifier-seq ... [opt] declarator
15932      decl-specifier-seq declarator = assignment-expression
15933      decl-specifier-seq ... [opt] abstract-declarator [opt]
15934      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15935
15936    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15937    declares a template parameter.  (In that case, a non-nested `>'
15938    token encountered during the parsing of the assignment-expression
15939    is not interpreted as a greater-than operator.)
15940
15941    Returns a representation of the parameter, or NULL if an error
15942    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15943    true iff the declarator is of the form "(p)".  */
15944
15945 static cp_parameter_declarator *
15946 cp_parser_parameter_declaration (cp_parser *parser,
15947                                  bool template_parm_p,
15948                                  bool *parenthesized_p)
15949 {
15950   int declares_class_or_enum;
15951   cp_decl_specifier_seq decl_specifiers;
15952   cp_declarator *declarator;
15953   tree default_argument;
15954   cp_token *token = NULL, *declarator_token_start = NULL;
15955   const char *saved_message;
15956
15957   /* In a template parameter, `>' is not an operator.
15958
15959      [temp.param]
15960
15961      When parsing a default template-argument for a non-type
15962      template-parameter, the first non-nested `>' is taken as the end
15963      of the template parameter-list rather than a greater-than
15964      operator.  */
15965
15966   /* Type definitions may not appear in parameter types.  */
15967   saved_message = parser->type_definition_forbidden_message;
15968   parser->type_definition_forbidden_message
15969     = G_("types may not be defined in parameter types");
15970
15971   /* Parse the declaration-specifiers.  */
15972   cp_parser_decl_specifier_seq (parser,
15973                                 CP_PARSER_FLAGS_NONE,
15974                                 &decl_specifiers,
15975                                 &declares_class_or_enum);
15976
15977   /* Complain about missing 'typename' or other invalid type names.  */
15978   if (!decl_specifiers.any_type_specifiers_p)
15979     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15980
15981   /* If an error occurred, there's no reason to attempt to parse the
15982      rest of the declaration.  */
15983   if (cp_parser_error_occurred (parser))
15984     {
15985       parser->type_definition_forbidden_message = saved_message;
15986       return NULL;
15987     }
15988
15989   /* Peek at the next token.  */
15990   token = cp_lexer_peek_token (parser->lexer);
15991
15992   /* If the next token is a `)', `,', `=', `>', or `...', then there
15993      is no declarator. However, when variadic templates are enabled,
15994      there may be a declarator following `...'.  */
15995   if (token->type == CPP_CLOSE_PAREN
15996       || token->type == CPP_COMMA
15997       || token->type == CPP_EQ
15998       || token->type == CPP_GREATER)
15999     {
16000       declarator = NULL;
16001       if (parenthesized_p)
16002         *parenthesized_p = false;
16003     }
16004   /* Otherwise, there should be a declarator.  */
16005   else
16006     {
16007       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16008       parser->default_arg_ok_p = false;
16009
16010       /* After seeing a decl-specifier-seq, if the next token is not a
16011          "(", there is no possibility that the code is a valid
16012          expression.  Therefore, if parsing tentatively, we commit at
16013          this point.  */
16014       if (!parser->in_template_argument_list_p
16015           /* In an expression context, having seen:
16016
16017                (int((char ...
16018
16019              we cannot be sure whether we are looking at a
16020              function-type (taking a "char" as a parameter) or a cast
16021              of some object of type "char" to "int".  */
16022           && !parser->in_type_id_in_expr_p
16023           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16024           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16025         cp_parser_commit_to_tentative_parse (parser);
16026       /* Parse the declarator.  */
16027       declarator_token_start = token;
16028       declarator = cp_parser_declarator (parser,
16029                                          CP_PARSER_DECLARATOR_EITHER,
16030                                          /*ctor_dtor_or_conv_p=*/NULL,
16031                                          parenthesized_p,
16032                                          /*member_p=*/false);
16033       parser->default_arg_ok_p = saved_default_arg_ok_p;
16034       /* After the declarator, allow more attributes.  */
16035       decl_specifiers.attributes
16036         = chainon (decl_specifiers.attributes,
16037                    cp_parser_attributes_opt (parser));
16038     }
16039
16040   /* If the next token is an ellipsis, and we have not seen a
16041      declarator name, and the type of the declarator contains parameter
16042      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16043      a parameter pack expansion expression. Otherwise, leave the
16044      ellipsis for a C-style variadic function. */
16045   token = cp_lexer_peek_token (parser->lexer);
16046   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16047     {
16048       tree type = decl_specifiers.type;
16049
16050       if (type && DECL_P (type))
16051         type = TREE_TYPE (type);
16052
16053       if (type
16054           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16055           && declarator_can_be_parameter_pack (declarator)
16056           && (!declarator || !declarator->parameter_pack_p)
16057           && uses_parameter_packs (type))
16058         {
16059           /* Consume the `...'. */
16060           cp_lexer_consume_token (parser->lexer);
16061           maybe_warn_variadic_templates ();
16062           
16063           /* Build a pack expansion type */
16064           if (declarator)
16065             declarator->parameter_pack_p = true;
16066           else
16067             decl_specifiers.type = make_pack_expansion (type);
16068         }
16069     }
16070
16071   /* The restriction on defining new types applies only to the type
16072      of the parameter, not to the default argument.  */
16073   parser->type_definition_forbidden_message = saved_message;
16074
16075   /* If the next token is `=', then process a default argument.  */
16076   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16077     {
16078       /* Consume the `='.  */
16079       cp_lexer_consume_token (parser->lexer);
16080
16081       /* If we are defining a class, then the tokens that make up the
16082          default argument must be saved and processed later.  */
16083       if (!template_parm_p && at_class_scope_p ()
16084           && TYPE_BEING_DEFINED (current_class_type)
16085           && !LAMBDA_TYPE_P (current_class_type))
16086         {
16087           unsigned depth = 0;
16088           int maybe_template_id = 0;
16089           cp_token *first_token;
16090           cp_token *token;
16091
16092           /* Add tokens until we have processed the entire default
16093              argument.  We add the range [first_token, token).  */
16094           first_token = cp_lexer_peek_token (parser->lexer);
16095           while (true)
16096             {
16097               bool done = false;
16098
16099               /* Peek at the next token.  */
16100               token = cp_lexer_peek_token (parser->lexer);
16101               /* What we do depends on what token we have.  */
16102               switch (token->type)
16103                 {
16104                   /* In valid code, a default argument must be
16105                      immediately followed by a `,' `)', or `...'.  */
16106                 case CPP_COMMA:
16107                   if (depth == 0 && maybe_template_id)
16108                     {
16109                       /* If we've seen a '<', we might be in a
16110                          template-argument-list.  Until Core issue 325 is
16111                          resolved, we don't know how this situation ought
16112                          to be handled, so try to DTRT.  We check whether
16113                          what comes after the comma is a valid parameter
16114                          declaration list.  If it is, then the comma ends
16115                          the default argument; otherwise the default
16116                          argument continues.  */
16117                       bool error = false;
16118                       tree t;
16119
16120                       /* Set ITALP so cp_parser_parameter_declaration_list
16121                          doesn't decide to commit to this parse.  */
16122                       bool saved_italp = parser->in_template_argument_list_p;
16123                       parser->in_template_argument_list_p = true;
16124
16125                       cp_parser_parse_tentatively (parser);
16126                       cp_lexer_consume_token (parser->lexer);
16127                       begin_scope (sk_function_parms, NULL_TREE);
16128                       cp_parser_parameter_declaration_list (parser, &error);
16129                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16130                         pop_binding (DECL_NAME (t), t);
16131                       leave_scope ();
16132                       if (!cp_parser_error_occurred (parser) && !error)
16133                         done = true;
16134                       cp_parser_abort_tentative_parse (parser);
16135
16136                       parser->in_template_argument_list_p = saved_italp;
16137                       break;
16138                     }
16139                 case CPP_CLOSE_PAREN:
16140                 case CPP_ELLIPSIS:
16141                   /* If we run into a non-nested `;', `}', or `]',
16142                      then the code is invalid -- but the default
16143                      argument is certainly over.  */
16144                 case CPP_SEMICOLON:
16145                 case CPP_CLOSE_BRACE:
16146                 case CPP_CLOSE_SQUARE:
16147                   if (depth == 0)
16148                     done = true;
16149                   /* Update DEPTH, if necessary.  */
16150                   else if (token->type == CPP_CLOSE_PAREN
16151                            || token->type == CPP_CLOSE_BRACE
16152                            || token->type == CPP_CLOSE_SQUARE)
16153                     --depth;
16154                   break;
16155
16156                 case CPP_OPEN_PAREN:
16157                 case CPP_OPEN_SQUARE:
16158                 case CPP_OPEN_BRACE:
16159                   ++depth;
16160                   break;
16161
16162                 case CPP_LESS:
16163                   if (depth == 0)
16164                     /* This might be the comparison operator, or it might
16165                        start a template argument list.  */
16166                     ++maybe_template_id;
16167                   break;
16168
16169                 case CPP_RSHIFT:
16170                   if (cxx_dialect == cxx98)
16171                     break;
16172                   /* Fall through for C++0x, which treats the `>>'
16173                      operator like two `>' tokens in certain
16174                      cases.  */
16175
16176                 case CPP_GREATER:
16177                   if (depth == 0)
16178                     {
16179                       /* This might be an operator, or it might close a
16180                          template argument list.  But if a previous '<'
16181                          started a template argument list, this will have
16182                          closed it, so we can't be in one anymore.  */
16183                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16184                       if (maybe_template_id < 0)
16185                         maybe_template_id = 0;
16186                     }
16187                   break;
16188
16189                   /* If we run out of tokens, issue an error message.  */
16190                 case CPP_EOF:
16191                 case CPP_PRAGMA_EOL:
16192                   error_at (token->location, "file ends in default argument");
16193                   done = true;
16194                   break;
16195
16196                 case CPP_NAME:
16197                 case CPP_SCOPE:
16198                   /* In these cases, we should look for template-ids.
16199                      For example, if the default argument is
16200                      `X<int, double>()', we need to do name lookup to
16201                      figure out whether or not `X' is a template; if
16202                      so, the `,' does not end the default argument.
16203
16204                      That is not yet done.  */
16205                   break;
16206
16207                 default:
16208                   break;
16209                 }
16210
16211               /* If we've reached the end, stop.  */
16212               if (done)
16213                 break;
16214
16215               /* Add the token to the token block.  */
16216               token = cp_lexer_consume_token (parser->lexer);
16217             }
16218
16219           /* Create a DEFAULT_ARG to represent the unparsed default
16220              argument.  */
16221           default_argument = make_node (DEFAULT_ARG);
16222           DEFARG_TOKENS (default_argument)
16223             = cp_token_cache_new (first_token, token);
16224           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16225         }
16226       /* Outside of a class definition, we can just parse the
16227          assignment-expression.  */
16228       else
16229         {
16230           token = cp_lexer_peek_token (parser->lexer);
16231           default_argument 
16232             = cp_parser_default_argument (parser, template_parm_p);
16233         }
16234
16235       if (!parser->default_arg_ok_p)
16236         {
16237           if (flag_permissive)
16238             warning (0, "deprecated use of default argument for parameter of non-function");
16239           else
16240             {
16241               error_at (token->location,
16242                         "default arguments are only "
16243                         "permitted for function parameters");
16244               default_argument = NULL_TREE;
16245             }
16246         }
16247       else if ((declarator && declarator->parameter_pack_p)
16248                || (decl_specifiers.type
16249                    && PACK_EXPANSION_P (decl_specifiers.type)))
16250         {
16251           /* Find the name of the parameter pack.  */     
16252           cp_declarator *id_declarator = declarator;
16253           while (id_declarator && id_declarator->kind != cdk_id)
16254             id_declarator = id_declarator->declarator;
16255           
16256           if (id_declarator && id_declarator->kind == cdk_id)
16257             error_at (declarator_token_start->location,
16258                       template_parm_p 
16259                       ? "template parameter pack %qD"
16260                       " cannot have a default argument"
16261                       : "parameter pack %qD cannot have a default argument",
16262                       id_declarator->u.id.unqualified_name);
16263           else
16264             error_at (declarator_token_start->location,
16265                       template_parm_p 
16266                       ? "template parameter pack cannot have a default argument"
16267                       : "parameter pack cannot have a default argument");
16268           
16269           default_argument = NULL_TREE;
16270         }
16271     }
16272   else
16273     default_argument = NULL_TREE;
16274
16275   return make_parameter_declarator (&decl_specifiers,
16276                                     declarator,
16277                                     default_argument);
16278 }
16279
16280 /* Parse a default argument and return it.
16281
16282    TEMPLATE_PARM_P is true if this is a default argument for a
16283    non-type template parameter.  */
16284 static tree
16285 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16286 {
16287   tree default_argument = NULL_TREE;
16288   bool saved_greater_than_is_operator_p;
16289   bool saved_local_variables_forbidden_p;
16290
16291   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16292      set correctly.  */
16293   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16294   parser->greater_than_is_operator_p = !template_parm_p;
16295   /* Local variable names (and the `this' keyword) may not
16296      appear in a default argument.  */
16297   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16298   parser->local_variables_forbidden_p = true;
16299   /* Parse the assignment-expression.  */
16300   if (template_parm_p)
16301     push_deferring_access_checks (dk_no_deferred);
16302   default_argument
16303     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16304   if (template_parm_p)
16305     pop_deferring_access_checks ();
16306   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16307   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16308
16309   return default_argument;
16310 }
16311
16312 /* Parse a function-body.
16313
16314    function-body:
16315      compound_statement  */
16316
16317 static void
16318 cp_parser_function_body (cp_parser *parser)
16319 {
16320   cp_parser_compound_statement (parser, NULL, false);
16321 }
16322
16323 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16324    true if a ctor-initializer was present.  */
16325
16326 static bool
16327 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16328 {
16329   tree body, list;
16330   bool ctor_initializer_p;
16331   const bool check_body_p =
16332      DECL_CONSTRUCTOR_P (current_function_decl)
16333      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16334   tree last = NULL;
16335
16336   /* Begin the function body.  */
16337   body = begin_function_body ();
16338   /* Parse the optional ctor-initializer.  */
16339   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16340
16341   /* If we're parsing a constexpr constructor definition, we need
16342      to check that the constructor body is indeed empty.  However,
16343      before we get to cp_parser_function_body lot of junk has been
16344      generated, so we can't just check that we have an empty block.
16345      Rather we take a snapshot of the outermost block, and check whether
16346      cp_parser_function_body changed its state.  */
16347   if (check_body_p)
16348     {
16349       list = body;
16350       if (TREE_CODE (list) == BIND_EXPR)
16351         list = BIND_EXPR_BODY (list);
16352       if (TREE_CODE (list) == STATEMENT_LIST
16353           && STATEMENT_LIST_TAIL (list) != NULL)
16354         last = STATEMENT_LIST_TAIL (list)->stmt;
16355     }
16356   /* Parse the function-body.  */
16357   cp_parser_function_body (parser);
16358   if (check_body_p)
16359     check_constexpr_ctor_body (last, list);
16360   /* Finish the function body.  */
16361   finish_function_body (body);
16362
16363   return ctor_initializer_p;
16364 }
16365
16366 /* Parse an initializer.
16367
16368    initializer:
16369      = initializer-clause
16370      ( expression-list )
16371
16372    Returns an expression representing the initializer.  If no
16373    initializer is present, NULL_TREE is returned.
16374
16375    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16376    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16377    set to TRUE if there is no initializer present.  If there is an
16378    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16379    is set to true; otherwise it is set to false.  */
16380
16381 static tree
16382 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16383                        bool* non_constant_p)
16384 {
16385   cp_token *token;
16386   tree init;
16387
16388   /* Peek at the next token.  */
16389   token = cp_lexer_peek_token (parser->lexer);
16390
16391   /* Let our caller know whether or not this initializer was
16392      parenthesized.  */
16393   *is_direct_init = (token->type != CPP_EQ);
16394   /* Assume that the initializer is constant.  */
16395   *non_constant_p = false;
16396
16397   if (token->type == CPP_EQ)
16398     {
16399       /* Consume the `='.  */
16400       cp_lexer_consume_token (parser->lexer);
16401       /* Parse the initializer-clause.  */
16402       init = cp_parser_initializer_clause (parser, non_constant_p);
16403     }
16404   else if (token->type == CPP_OPEN_PAREN)
16405     {
16406       VEC(tree,gc) *vec;
16407       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16408                                                      /*cast_p=*/false,
16409                                                      /*allow_expansion_p=*/true,
16410                                                      non_constant_p);
16411       if (vec == NULL)
16412         return error_mark_node;
16413       init = build_tree_list_vec (vec);
16414       release_tree_vector (vec);
16415     }
16416   else if (token->type == CPP_OPEN_BRACE)
16417     {
16418       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16419       init = cp_parser_braced_list (parser, non_constant_p);
16420       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16421     }
16422   else
16423     {
16424       /* Anything else is an error.  */
16425       cp_parser_error (parser, "expected initializer");
16426       init = error_mark_node;
16427     }
16428
16429   return init;
16430 }
16431
16432 /* Parse an initializer-clause.
16433
16434    initializer-clause:
16435      assignment-expression
16436      braced-init-list
16437
16438    Returns an expression representing the initializer.
16439
16440    If the `assignment-expression' production is used the value
16441    returned is simply a representation for the expression.
16442
16443    Otherwise, calls cp_parser_braced_list.  */
16444
16445 static tree
16446 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16447 {
16448   tree initializer;
16449
16450   /* Assume the expression is constant.  */
16451   *non_constant_p = false;
16452
16453   /* If it is not a `{', then we are looking at an
16454      assignment-expression.  */
16455   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16456     {
16457       initializer
16458         = cp_parser_constant_expression (parser,
16459                                         /*allow_non_constant_p=*/true,
16460                                         non_constant_p);
16461       if (!*non_constant_p)
16462         {
16463           /* We only want to fold if this is really a constant
16464              expression.  FIXME Actually, we don't want to fold here, but in
16465              cp_finish_decl.  */
16466           tree folded = fold_non_dependent_expr (initializer);
16467           folded = maybe_constant_value (folded);
16468           if (TREE_CONSTANT (folded))
16469             initializer = folded;
16470         }
16471     }
16472   else
16473     initializer = cp_parser_braced_list (parser, non_constant_p);
16474
16475   return initializer;
16476 }
16477
16478 /* Parse a brace-enclosed initializer list.
16479
16480    braced-init-list:
16481      { initializer-list , [opt] }
16482      { }
16483
16484    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16485    the elements of the initializer-list (or NULL, if the last
16486    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16487    NULL_TREE.  There is no way to detect whether or not the optional
16488    trailing `,' was provided.  NON_CONSTANT_P is as for
16489    cp_parser_initializer.  */     
16490
16491 static tree
16492 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16493 {
16494   tree initializer;
16495
16496   /* Consume the `{' token.  */
16497   cp_lexer_consume_token (parser->lexer);
16498   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16499   initializer = make_node (CONSTRUCTOR);
16500   /* If it's not a `}', then there is a non-trivial initializer.  */
16501   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16502     {
16503       /* Parse the initializer list.  */
16504       CONSTRUCTOR_ELTS (initializer)
16505         = cp_parser_initializer_list (parser, non_constant_p);
16506       /* A trailing `,' token is allowed.  */
16507       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16508         cp_lexer_consume_token (parser->lexer);
16509     }
16510   /* Now, there should be a trailing `}'.  */
16511   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16512   TREE_TYPE (initializer) = init_list_type_node;
16513   return initializer;
16514 }
16515
16516 /* Parse an initializer-list.
16517
16518    initializer-list:
16519      initializer-clause ... [opt]
16520      initializer-list , initializer-clause ... [opt]
16521
16522    GNU Extension:
16523
16524    initializer-list:
16525      identifier : initializer-clause
16526      initializer-list, identifier : initializer-clause
16527
16528    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16529    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16530    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16531    as for cp_parser_initializer.  */
16532
16533 static VEC(constructor_elt,gc) *
16534 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16535 {
16536   VEC(constructor_elt,gc) *v = NULL;
16537
16538   /* Assume all of the expressions are constant.  */
16539   *non_constant_p = false;
16540
16541   /* Parse the rest of the list.  */
16542   while (true)
16543     {
16544       cp_token *token;
16545       tree identifier;
16546       tree initializer;
16547       bool clause_non_constant_p;
16548
16549       /* If the next token is an identifier and the following one is a
16550          colon, we are looking at the GNU designated-initializer
16551          syntax.  */
16552       if (cp_parser_allow_gnu_extensions_p (parser)
16553           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16554           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16555         {
16556           /* Warn the user that they are using an extension.  */
16557           pedwarn (input_location, OPT_pedantic, 
16558                    "ISO C++ does not allow designated initializers");
16559           /* Consume the identifier.  */
16560           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16561           /* Consume the `:'.  */
16562           cp_lexer_consume_token (parser->lexer);
16563         }
16564       else
16565         identifier = NULL_TREE;
16566
16567       /* Parse the initializer.  */
16568       initializer = cp_parser_initializer_clause (parser,
16569                                                   &clause_non_constant_p);
16570       /* If any clause is non-constant, so is the entire initializer.  */
16571       if (clause_non_constant_p)
16572         *non_constant_p = true;
16573
16574       /* If we have an ellipsis, this is an initializer pack
16575          expansion.  */
16576       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16577         {
16578           /* Consume the `...'.  */
16579           cp_lexer_consume_token (parser->lexer);
16580
16581           /* Turn the initializer into an initializer expansion.  */
16582           initializer = make_pack_expansion (initializer);
16583         }
16584
16585       /* Add it to the vector.  */
16586       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16587
16588       /* If the next token is not a comma, we have reached the end of
16589          the list.  */
16590       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16591         break;
16592
16593       /* Peek at the next token.  */
16594       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16595       /* If the next token is a `}', then we're still done.  An
16596          initializer-clause can have a trailing `,' after the
16597          initializer-list and before the closing `}'.  */
16598       if (token->type == CPP_CLOSE_BRACE)
16599         break;
16600
16601       /* Consume the `,' token.  */
16602       cp_lexer_consume_token (parser->lexer);
16603     }
16604
16605   return v;
16606 }
16607
16608 /* Classes [gram.class] */
16609
16610 /* Parse a class-name.
16611
16612    class-name:
16613      identifier
16614      template-id
16615
16616    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16617    to indicate that names looked up in dependent types should be
16618    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16619    keyword has been used to indicate that the name that appears next
16620    is a template.  TAG_TYPE indicates the explicit tag given before
16621    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16622    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16623    is the class being defined in a class-head.
16624
16625    Returns the TYPE_DECL representing the class.  */
16626
16627 static tree
16628 cp_parser_class_name (cp_parser *parser,
16629                       bool typename_keyword_p,
16630                       bool template_keyword_p,
16631                       enum tag_types tag_type,
16632                       bool check_dependency_p,
16633                       bool class_head_p,
16634                       bool is_declaration)
16635 {
16636   tree decl;
16637   tree scope;
16638   bool typename_p;
16639   cp_token *token;
16640   tree identifier = NULL_TREE;
16641
16642   /* All class-names start with an identifier.  */
16643   token = cp_lexer_peek_token (parser->lexer);
16644   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16645     {
16646       cp_parser_error (parser, "expected class-name");
16647       return error_mark_node;
16648     }
16649
16650   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16651      to a template-id, so we save it here.  */
16652   scope = parser->scope;
16653   if (scope == error_mark_node)
16654     return error_mark_node;
16655
16656   /* Any name names a type if we're following the `typename' keyword
16657      in a qualified name where the enclosing scope is type-dependent.  */
16658   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16659                 && dependent_type_p (scope));
16660   /* Handle the common case (an identifier, but not a template-id)
16661      efficiently.  */
16662   if (token->type == CPP_NAME
16663       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16664     {
16665       cp_token *identifier_token;
16666       bool ambiguous_p;
16667
16668       /* Look for the identifier.  */
16669       identifier_token = cp_lexer_peek_token (parser->lexer);
16670       ambiguous_p = identifier_token->ambiguous_p;
16671       identifier = cp_parser_identifier (parser);
16672       /* If the next token isn't an identifier, we are certainly not
16673          looking at a class-name.  */
16674       if (identifier == error_mark_node)
16675         decl = error_mark_node;
16676       /* If we know this is a type-name, there's no need to look it
16677          up.  */
16678       else if (typename_p)
16679         decl = identifier;
16680       else
16681         {
16682           tree ambiguous_decls;
16683           /* If we already know that this lookup is ambiguous, then
16684              we've already issued an error message; there's no reason
16685              to check again.  */
16686           if (ambiguous_p)
16687             {
16688               cp_parser_simulate_error (parser);
16689               return error_mark_node;
16690             }
16691           /* If the next token is a `::', then the name must be a type
16692              name.
16693
16694              [basic.lookup.qual]
16695
16696              During the lookup for a name preceding the :: scope
16697              resolution operator, object, function, and enumerator
16698              names are ignored.  */
16699           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16700             tag_type = typename_type;
16701           /* Look up the name.  */
16702           decl = cp_parser_lookup_name (parser, identifier,
16703                                         tag_type,
16704                                         /*is_template=*/false,
16705                                         /*is_namespace=*/false,
16706                                         check_dependency_p,
16707                                         &ambiguous_decls,
16708                                         identifier_token->location);
16709           if (ambiguous_decls)
16710             {
16711               if (cp_parser_parsing_tentatively (parser))
16712                 cp_parser_simulate_error (parser);
16713               return error_mark_node;
16714             }
16715         }
16716     }
16717   else
16718     {
16719       /* Try a template-id.  */
16720       decl = cp_parser_template_id (parser, template_keyword_p,
16721                                     check_dependency_p,
16722                                     is_declaration);
16723       if (decl == error_mark_node)
16724         return error_mark_node;
16725     }
16726
16727   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16728
16729   /* If this is a typename, create a TYPENAME_TYPE.  */
16730   if (typename_p && decl != error_mark_node)
16731     {
16732       decl = make_typename_type (scope, decl, typename_type,
16733                                  /*complain=*/tf_error);
16734       if (decl != error_mark_node)
16735         decl = TYPE_NAME (decl);
16736     }
16737
16738   /* Check to see that it is really the name of a class.  */
16739   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16740       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16741       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16742     /* Situations like this:
16743
16744          template <typename T> struct A {
16745            typename T::template X<int>::I i;
16746          };
16747
16748        are problematic.  Is `T::template X<int>' a class-name?  The
16749        standard does not seem to be definitive, but there is no other
16750        valid interpretation of the following `::'.  Therefore, those
16751        names are considered class-names.  */
16752     {
16753       decl = make_typename_type (scope, decl, tag_type, tf_error);
16754       if (decl != error_mark_node)
16755         decl = TYPE_NAME (decl);
16756     }
16757   else if (TREE_CODE (decl) != TYPE_DECL
16758            || TREE_TYPE (decl) == error_mark_node
16759            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16760            /* In Objective-C 2.0, a classname followed by '.' starts a
16761               dot-syntax expression, and it's not a type-name.  */
16762            || (c_dialect_objc ()
16763                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16764                && objc_is_class_name (decl)))
16765     decl = error_mark_node;
16766
16767   if (decl == error_mark_node)
16768     cp_parser_error (parser, "expected class-name");
16769   else if (identifier && !parser->scope)
16770     maybe_note_name_used_in_class (identifier, decl);
16771
16772   return decl;
16773 }
16774
16775 /* Parse a class-specifier.
16776
16777    class-specifier:
16778      class-head { member-specification [opt] }
16779
16780    Returns the TREE_TYPE representing the class.  */
16781
16782 static tree
16783 cp_parser_class_specifier (cp_parser* parser)
16784 {
16785   tree type;
16786   tree attributes = NULL_TREE;
16787   bool nested_name_specifier_p;
16788   unsigned saved_num_template_parameter_lists;
16789   bool saved_in_function_body;
16790   bool saved_in_unbraced_linkage_specification_p;
16791   tree old_scope = NULL_TREE;
16792   tree scope = NULL_TREE;
16793   tree bases;
16794
16795   push_deferring_access_checks (dk_no_deferred);
16796
16797   /* Parse the class-head.  */
16798   type = cp_parser_class_head (parser,
16799                                &nested_name_specifier_p,
16800                                &attributes,
16801                                &bases);
16802   /* If the class-head was a semantic disaster, skip the entire body
16803      of the class.  */
16804   if (!type)
16805     {
16806       cp_parser_skip_to_end_of_block_or_statement (parser);
16807       pop_deferring_access_checks ();
16808       return error_mark_node;
16809     }
16810
16811   /* Look for the `{'.  */
16812   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16813     {
16814       pop_deferring_access_checks ();
16815       return error_mark_node;
16816     }
16817
16818   /* Process the base classes. If they're invalid, skip the 
16819      entire class body.  */
16820   if (!xref_basetypes (type, bases))
16821     {
16822       /* Consuming the closing brace yields better error messages
16823          later on.  */
16824       if (cp_parser_skip_to_closing_brace (parser))
16825         cp_lexer_consume_token (parser->lexer);
16826       pop_deferring_access_checks ();
16827       return error_mark_node;
16828     }
16829
16830   /* Issue an error message if type-definitions are forbidden here.  */
16831   cp_parser_check_type_definition (parser);
16832   /* Remember that we are defining one more class.  */
16833   ++parser->num_classes_being_defined;
16834   /* Inside the class, surrounding template-parameter-lists do not
16835      apply.  */
16836   saved_num_template_parameter_lists
16837     = parser->num_template_parameter_lists;
16838   parser->num_template_parameter_lists = 0;
16839   /* We are not in a function body.  */
16840   saved_in_function_body = parser->in_function_body;
16841   parser->in_function_body = false;
16842   /* We are not immediately inside an extern "lang" block.  */
16843   saved_in_unbraced_linkage_specification_p
16844     = parser->in_unbraced_linkage_specification_p;
16845   parser->in_unbraced_linkage_specification_p = false;
16846
16847   /* Start the class.  */
16848   if (nested_name_specifier_p)
16849     {
16850       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16851       old_scope = push_inner_scope (scope);
16852     }
16853   type = begin_class_definition (type, attributes);
16854
16855   if (type == error_mark_node)
16856     /* If the type is erroneous, skip the entire body of the class.  */
16857     cp_parser_skip_to_closing_brace (parser);
16858   else
16859     /* Parse the member-specification.  */
16860     cp_parser_member_specification_opt (parser);
16861
16862   /* Look for the trailing `}'.  */
16863   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16864   /* Look for trailing attributes to apply to this class.  */
16865   if (cp_parser_allow_gnu_extensions_p (parser))
16866     attributes = cp_parser_attributes_opt (parser);
16867   if (type != error_mark_node)
16868     type = finish_struct (type, attributes);
16869   if (nested_name_specifier_p)
16870     pop_inner_scope (old_scope, scope);
16871
16872   /* We've finished a type definition.  Check for the common syntax
16873      error of forgetting a semicolon after the definition.  We need to
16874      be careful, as we can't just check for not-a-semicolon and be done
16875      with it; the user might have typed:
16876
16877      class X { } c = ...;
16878      class X { } *p = ...;
16879
16880      and so forth.  Instead, enumerate all the possible tokens that
16881      might follow this production; if we don't see one of them, then
16882      complain and silently insert the semicolon.  */
16883   {
16884     cp_token *token = cp_lexer_peek_token (parser->lexer);
16885     bool want_semicolon = true;
16886
16887     switch (token->type)
16888       {
16889       case CPP_NAME:
16890       case CPP_SEMICOLON:
16891       case CPP_MULT:
16892       case CPP_AND:
16893       case CPP_OPEN_PAREN:
16894       case CPP_CLOSE_PAREN:
16895       case CPP_COMMA:
16896         want_semicolon = false;
16897         break;
16898
16899         /* While it's legal for type qualifiers and storage class
16900            specifiers to follow type definitions in the grammar, only
16901            compiler testsuites contain code like that.  Assume that if
16902            we see such code, then what we're really seeing is a case
16903            like:
16904
16905            class X { }
16906            const <type> var = ...;
16907
16908            or
16909
16910            class Y { }
16911            static <type> func (...) ...
16912
16913            i.e. the qualifier or specifier applies to the next
16914            declaration.  To do so, however, we need to look ahead one
16915            more token to see if *that* token is a type specifier.
16916
16917            This code could be improved to handle:
16918
16919            class Z { }
16920            static const <type> var = ...;  */
16921       case CPP_KEYWORD:
16922         if (keyword_is_storage_class_specifier (token->keyword)
16923             || keyword_is_type_qualifier (token->keyword))
16924           {
16925             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16926
16927             if (lookahead->type == CPP_KEYWORD
16928                 && !keyword_begins_type_specifier (lookahead->keyword))
16929               want_semicolon = false;
16930             else if (lookahead->type == CPP_NAME)
16931               /* Handling user-defined types here would be nice, but
16932                  very tricky.  */
16933               want_semicolon = false;
16934           }
16935         break;
16936       default:
16937         break;
16938       }
16939
16940     /* If we don't have a type, then something is very wrong and we
16941        shouldn't try to do anything clever.  */
16942     if (TYPE_P (type) && want_semicolon)
16943       {
16944         cp_token_position prev
16945           = cp_lexer_previous_token_position (parser->lexer);
16946         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16947         location_t loc = prev_token->location;
16948
16949         if (CLASSTYPE_DECLARED_CLASS (type))
16950           error_at (loc, "expected %<;%> after class definition");
16951         else if (TREE_CODE (type) == RECORD_TYPE)
16952           error_at (loc, "expected %<;%> after struct definition");
16953         else if (TREE_CODE (type) == UNION_TYPE)
16954           error_at (loc, "expected %<;%> after union definition");
16955         else
16956           gcc_unreachable ();
16957
16958         /* Unget one token and smash it to look as though we encountered
16959            a semicolon in the input stream.  */
16960         cp_lexer_set_token_position (parser->lexer, prev);
16961         token = cp_lexer_peek_token (parser->lexer);
16962         token->type = CPP_SEMICOLON;
16963         token->keyword = RID_MAX;
16964       }
16965   }
16966
16967   /* If this class is not itself within the scope of another class,
16968      then we need to parse the bodies of all of the queued function
16969      definitions.  Note that the queued functions defined in a class
16970      are not always processed immediately following the
16971      class-specifier for that class.  Consider:
16972
16973        struct A {
16974          struct B { void f() { sizeof (A); } };
16975        };
16976
16977      If `f' were processed before the processing of `A' were
16978      completed, there would be no way to compute the size of `A'.
16979      Note that the nesting we are interested in here is lexical --
16980      not the semantic nesting given by TYPE_CONTEXT.  In particular,
16981      for:
16982
16983        struct A { struct B; };
16984        struct A::B { void f() { } };
16985
16986      there is no need to delay the parsing of `A::B::f'.  */
16987   if (--parser->num_classes_being_defined == 0)
16988     {
16989       tree fn;
16990       tree class_type = NULL_TREE;
16991       tree pushed_scope = NULL_TREE;
16992       unsigned ix;
16993       cp_default_arg_entry *e;
16994
16995       /* In a first pass, parse default arguments to the functions.
16996          Then, in a second pass, parse the bodies of the functions.
16997          This two-phased approach handles cases like:
16998
16999             struct S {
17000               void f() { g(); }
17001               void g(int i = 3);
17002             };
17003
17004          */
17005       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17006                         ix, e)
17007         {
17008           fn = e->decl;
17009           /* If there are default arguments that have not yet been processed,
17010              take care of them now.  */
17011           if (class_type != e->class_type)
17012             {
17013               if (pushed_scope)
17014                 pop_scope (pushed_scope);
17015               class_type = e->class_type;
17016               pushed_scope = push_scope (class_type);
17017             }
17018           /* Make sure that any template parameters are in scope.  */
17019           maybe_begin_member_template_processing (fn);
17020           /* Parse the default argument expressions.  */
17021           cp_parser_late_parsing_default_args (parser, fn);
17022           /* Remove any template parameters from the symbol table.  */
17023           maybe_end_member_template_processing ();
17024         }
17025       if (pushed_scope)
17026         pop_scope (pushed_scope);
17027       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17028       /* Now parse the body of the functions.  */
17029       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17030         cp_parser_late_parsing_for_member (parser, fn);
17031       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17032     }
17033
17034   /* Put back any saved access checks.  */
17035   pop_deferring_access_checks ();
17036
17037   /* Restore saved state.  */
17038   parser->in_function_body = saved_in_function_body;
17039   parser->num_template_parameter_lists
17040     = saved_num_template_parameter_lists;
17041   parser->in_unbraced_linkage_specification_p
17042     = saved_in_unbraced_linkage_specification_p;
17043
17044   return type;
17045 }
17046
17047 /* Parse a class-head.
17048
17049    class-head:
17050      class-key identifier [opt] base-clause [opt]
17051      class-key nested-name-specifier identifier base-clause [opt]
17052      class-key nested-name-specifier [opt] template-id
17053        base-clause [opt]
17054
17055    GNU Extensions:
17056      class-key attributes identifier [opt] base-clause [opt]
17057      class-key attributes nested-name-specifier identifier base-clause [opt]
17058      class-key attributes nested-name-specifier [opt] template-id
17059        base-clause [opt]
17060
17061    Upon return BASES is initialized to the list of base classes (or
17062    NULL, if there are none) in the same form returned by
17063    cp_parser_base_clause.
17064
17065    Returns the TYPE of the indicated class.  Sets
17066    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17067    involving a nested-name-specifier was used, and FALSE otherwise.
17068
17069    Returns error_mark_node if this is not a class-head.
17070
17071    Returns NULL_TREE if the class-head is syntactically valid, but
17072    semantically invalid in a way that means we should skip the entire
17073    body of the class.  */
17074
17075 static tree
17076 cp_parser_class_head (cp_parser* parser,
17077                       bool* nested_name_specifier_p,
17078                       tree *attributes_p,
17079                       tree *bases)
17080 {
17081   tree nested_name_specifier;
17082   enum tag_types class_key;
17083   tree id = NULL_TREE;
17084   tree type = NULL_TREE;
17085   tree attributes;
17086   bool template_id_p = false;
17087   bool qualified_p = false;
17088   bool invalid_nested_name_p = false;
17089   bool invalid_explicit_specialization_p = false;
17090   tree pushed_scope = NULL_TREE;
17091   unsigned num_templates;
17092   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17093   /* Assume no nested-name-specifier will be present.  */
17094   *nested_name_specifier_p = false;
17095   /* Assume no template parameter lists will be used in defining the
17096      type.  */
17097   num_templates = 0;
17098
17099   *bases = NULL_TREE;
17100
17101   /* Look for the class-key.  */
17102   class_key = cp_parser_class_key (parser);
17103   if (class_key == none_type)
17104     return error_mark_node;
17105
17106   /* Parse the attributes.  */
17107   attributes = cp_parser_attributes_opt (parser);
17108
17109   /* If the next token is `::', that is invalid -- but sometimes
17110      people do try to write:
17111
17112        struct ::S {};
17113
17114      Handle this gracefully by accepting the extra qualifier, and then
17115      issuing an error about it later if this really is a
17116      class-head.  If it turns out just to be an elaborated type
17117      specifier, remain silent.  */
17118   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17119     qualified_p = true;
17120
17121   push_deferring_access_checks (dk_no_check);
17122
17123   /* Determine the name of the class.  Begin by looking for an
17124      optional nested-name-specifier.  */
17125   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17126   nested_name_specifier
17127     = cp_parser_nested_name_specifier_opt (parser,
17128                                            /*typename_keyword_p=*/false,
17129                                            /*check_dependency_p=*/false,
17130                                            /*type_p=*/false,
17131                                            /*is_declaration=*/false);
17132   /* If there was a nested-name-specifier, then there *must* be an
17133      identifier.  */
17134   if (nested_name_specifier)
17135     {
17136       type_start_token = cp_lexer_peek_token (parser->lexer);
17137       /* Although the grammar says `identifier', it really means
17138          `class-name' or `template-name'.  You are only allowed to
17139          define a class that has already been declared with this
17140          syntax.
17141
17142          The proposed resolution for Core Issue 180 says that wherever
17143          you see `class T::X' you should treat `X' as a type-name.
17144
17145          It is OK to define an inaccessible class; for example:
17146
17147            class A { class B; };
17148            class A::B {};
17149
17150          We do not know if we will see a class-name, or a
17151          template-name.  We look for a class-name first, in case the
17152          class-name is a template-id; if we looked for the
17153          template-name first we would stop after the template-name.  */
17154       cp_parser_parse_tentatively (parser);
17155       type = cp_parser_class_name (parser,
17156                                    /*typename_keyword_p=*/false,
17157                                    /*template_keyword_p=*/false,
17158                                    class_type,
17159                                    /*check_dependency_p=*/false,
17160                                    /*class_head_p=*/true,
17161                                    /*is_declaration=*/false);
17162       /* If that didn't work, ignore the nested-name-specifier.  */
17163       if (!cp_parser_parse_definitely (parser))
17164         {
17165           invalid_nested_name_p = true;
17166           type_start_token = cp_lexer_peek_token (parser->lexer);
17167           id = cp_parser_identifier (parser);
17168           if (id == error_mark_node)
17169             id = NULL_TREE;
17170         }
17171       /* If we could not find a corresponding TYPE, treat this
17172          declaration like an unqualified declaration.  */
17173       if (type == error_mark_node)
17174         nested_name_specifier = NULL_TREE;
17175       /* Otherwise, count the number of templates used in TYPE and its
17176          containing scopes.  */
17177       else
17178         {
17179           tree scope;
17180
17181           for (scope = TREE_TYPE (type);
17182                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17183                scope = (TYPE_P (scope)
17184                         ? TYPE_CONTEXT (scope)
17185                         : DECL_CONTEXT (scope)))
17186             if (TYPE_P (scope)
17187                 && CLASS_TYPE_P (scope)
17188                 && CLASSTYPE_TEMPLATE_INFO (scope)
17189                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17190                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17191               ++num_templates;
17192         }
17193     }
17194   /* Otherwise, the identifier is optional.  */
17195   else
17196     {
17197       /* We don't know whether what comes next is a template-id,
17198          an identifier, or nothing at all.  */
17199       cp_parser_parse_tentatively (parser);
17200       /* Check for a template-id.  */
17201       type_start_token = cp_lexer_peek_token (parser->lexer);
17202       id = cp_parser_template_id (parser,
17203                                   /*template_keyword_p=*/false,
17204                                   /*check_dependency_p=*/true,
17205                                   /*is_declaration=*/true);
17206       /* If that didn't work, it could still be an identifier.  */
17207       if (!cp_parser_parse_definitely (parser))
17208         {
17209           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17210             {
17211               type_start_token = cp_lexer_peek_token (parser->lexer);
17212               id = cp_parser_identifier (parser);
17213             }
17214           else
17215             id = NULL_TREE;
17216         }
17217       else
17218         {
17219           template_id_p = true;
17220           ++num_templates;
17221         }
17222     }
17223
17224   pop_deferring_access_checks ();
17225
17226   if (id)
17227     cp_parser_check_for_invalid_template_id (parser, id,
17228                                              type_start_token->location);
17229
17230   /* If it's not a `:' or a `{' then we can't really be looking at a
17231      class-head, since a class-head only appears as part of a
17232      class-specifier.  We have to detect this situation before calling
17233      xref_tag, since that has irreversible side-effects.  */
17234   if (!cp_parser_next_token_starts_class_definition_p (parser))
17235     {
17236       cp_parser_error (parser, "expected %<{%> or %<:%>");
17237       return error_mark_node;
17238     }
17239
17240   /* At this point, we're going ahead with the class-specifier, even
17241      if some other problem occurs.  */
17242   cp_parser_commit_to_tentative_parse (parser);
17243   /* Issue the error about the overly-qualified name now.  */
17244   if (qualified_p)
17245     {
17246       cp_parser_error (parser,
17247                        "global qualification of class name is invalid");
17248       return error_mark_node;
17249     }
17250   else if (invalid_nested_name_p)
17251     {
17252       cp_parser_error (parser,
17253                        "qualified name does not name a class");
17254       return error_mark_node;
17255     }
17256   else if (nested_name_specifier)
17257     {
17258       tree scope;
17259
17260       /* Reject typedef-names in class heads.  */
17261       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17262         {
17263           error_at (type_start_token->location,
17264                     "invalid class name in declaration of %qD",
17265                     type);
17266           type = NULL_TREE;
17267           goto done;
17268         }
17269
17270       /* Figure out in what scope the declaration is being placed.  */
17271       scope = current_scope ();
17272       /* If that scope does not contain the scope in which the
17273          class was originally declared, the program is invalid.  */
17274       if (scope && !is_ancestor (scope, nested_name_specifier))
17275         {
17276           if (at_namespace_scope_p ())
17277             error_at (type_start_token->location,
17278                       "declaration of %qD in namespace %qD which does not "
17279                       "enclose %qD",
17280                       type, scope, nested_name_specifier);
17281           else
17282             error_at (type_start_token->location,
17283                       "declaration of %qD in %qD which does not enclose %qD",
17284                       type, scope, nested_name_specifier);
17285           type = NULL_TREE;
17286           goto done;
17287         }
17288       /* [dcl.meaning]
17289
17290          A declarator-id shall not be qualified except for the
17291          definition of a ... nested class outside of its class
17292          ... [or] the definition or explicit instantiation of a
17293          class member of a namespace outside of its namespace.  */
17294       if (scope == nested_name_specifier)
17295         {
17296           permerror (nested_name_specifier_token_start->location,
17297                      "extra qualification not allowed");
17298           nested_name_specifier = NULL_TREE;
17299           num_templates = 0;
17300         }
17301     }
17302   /* An explicit-specialization must be preceded by "template <>".  If
17303      it is not, try to recover gracefully.  */
17304   if (at_namespace_scope_p ()
17305       && parser->num_template_parameter_lists == 0
17306       && template_id_p)
17307     {
17308       error_at (type_start_token->location,
17309                 "an explicit specialization must be preceded by %<template <>%>");
17310       invalid_explicit_specialization_p = true;
17311       /* Take the same action that would have been taken by
17312          cp_parser_explicit_specialization.  */
17313       ++parser->num_template_parameter_lists;
17314       begin_specialization ();
17315     }
17316   /* There must be no "return" statements between this point and the
17317      end of this function; set "type "to the correct return value and
17318      use "goto done;" to return.  */
17319   /* Make sure that the right number of template parameters were
17320      present.  */
17321   if (!cp_parser_check_template_parameters (parser, num_templates,
17322                                             type_start_token->location,
17323                                             /*declarator=*/NULL))
17324     {
17325       /* If something went wrong, there is no point in even trying to
17326          process the class-definition.  */
17327       type = NULL_TREE;
17328       goto done;
17329     }
17330
17331   /* Look up the type.  */
17332   if (template_id_p)
17333     {
17334       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17335           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17336               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17337         {
17338           error_at (type_start_token->location,
17339                     "function template %qD redeclared as a class template", id);
17340           type = error_mark_node;
17341         }
17342       else
17343         {
17344           type = TREE_TYPE (id);
17345           type = maybe_process_partial_specialization (type);
17346         }
17347       if (nested_name_specifier)
17348         pushed_scope = push_scope (nested_name_specifier);
17349     }
17350   else if (nested_name_specifier)
17351     {
17352       tree class_type;
17353
17354       /* Given:
17355
17356             template <typename T> struct S { struct T };
17357             template <typename T> struct S<T>::T { };
17358
17359          we will get a TYPENAME_TYPE when processing the definition of
17360          `S::T'.  We need to resolve it to the actual type before we
17361          try to define it.  */
17362       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17363         {
17364           class_type = resolve_typename_type (TREE_TYPE (type),
17365                                               /*only_current_p=*/false);
17366           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17367             type = TYPE_NAME (class_type);
17368           else
17369             {
17370               cp_parser_error (parser, "could not resolve typename type");
17371               type = error_mark_node;
17372             }
17373         }
17374
17375       if (maybe_process_partial_specialization (TREE_TYPE (type))
17376           == error_mark_node)
17377         {
17378           type = NULL_TREE;
17379           goto done;
17380         }
17381
17382       class_type = current_class_type;
17383       /* Enter the scope indicated by the nested-name-specifier.  */
17384       pushed_scope = push_scope (nested_name_specifier);
17385       /* Get the canonical version of this type.  */
17386       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17387       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17388           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17389         {
17390           type = push_template_decl (type);
17391           if (type == error_mark_node)
17392             {
17393               type = NULL_TREE;
17394               goto done;
17395             }
17396         }
17397
17398       type = TREE_TYPE (type);
17399       *nested_name_specifier_p = true;
17400     }
17401   else      /* The name is not a nested name.  */
17402     {
17403       /* If the class was unnamed, create a dummy name.  */
17404       if (!id)
17405         id = make_anon_name ();
17406       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17407                        parser->num_template_parameter_lists);
17408     }
17409
17410   /* Indicate whether this class was declared as a `class' or as a
17411      `struct'.  */
17412   if (TREE_CODE (type) == RECORD_TYPE)
17413     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17414   cp_parser_check_class_key (class_key, type);
17415
17416   /* If this type was already complete, and we see another definition,
17417      that's an error.  */
17418   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17419     {
17420       error_at (type_start_token->location, "redefinition of %q#T",
17421                 type);
17422       error_at (type_start_token->location, "previous definition of %q+#T",
17423                 type);
17424       type = NULL_TREE;
17425       goto done;
17426     }
17427   else if (type == error_mark_node)
17428     type = NULL_TREE;
17429
17430   /* We will have entered the scope containing the class; the names of
17431      base classes should be looked up in that context.  For example:
17432
17433        struct A { struct B {}; struct C; };
17434        struct A::C : B {};
17435
17436      is valid.  */
17437
17438   /* Get the list of base-classes, if there is one.  */
17439   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17440     *bases = cp_parser_base_clause (parser);
17441
17442  done:
17443   /* Leave the scope given by the nested-name-specifier.  We will
17444      enter the class scope itself while processing the members.  */
17445   if (pushed_scope)
17446     pop_scope (pushed_scope);
17447
17448   if (invalid_explicit_specialization_p)
17449     {
17450       end_specialization ();
17451       --parser->num_template_parameter_lists;
17452     }
17453
17454   if (type)
17455     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17456   *attributes_p = attributes;
17457   return type;
17458 }
17459
17460 /* Parse a class-key.
17461
17462    class-key:
17463      class
17464      struct
17465      union
17466
17467    Returns the kind of class-key specified, or none_type to indicate
17468    error.  */
17469
17470 static enum tag_types
17471 cp_parser_class_key (cp_parser* parser)
17472 {
17473   cp_token *token;
17474   enum tag_types tag_type;
17475
17476   /* Look for the class-key.  */
17477   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17478   if (!token)
17479     return none_type;
17480
17481   /* Check to see if the TOKEN is a class-key.  */
17482   tag_type = cp_parser_token_is_class_key (token);
17483   if (!tag_type)
17484     cp_parser_error (parser, "expected class-key");
17485   return tag_type;
17486 }
17487
17488 /* Parse an (optional) member-specification.
17489
17490    member-specification:
17491      member-declaration member-specification [opt]
17492      access-specifier : member-specification [opt]  */
17493
17494 static void
17495 cp_parser_member_specification_opt (cp_parser* parser)
17496 {
17497   while (true)
17498     {
17499       cp_token *token;
17500       enum rid keyword;
17501
17502       /* Peek at the next token.  */
17503       token = cp_lexer_peek_token (parser->lexer);
17504       /* If it's a `}', or EOF then we've seen all the members.  */
17505       if (token->type == CPP_CLOSE_BRACE
17506           || token->type == CPP_EOF
17507           || token->type == CPP_PRAGMA_EOL)
17508         break;
17509
17510       /* See if this token is a keyword.  */
17511       keyword = token->keyword;
17512       switch (keyword)
17513         {
17514         case RID_PUBLIC:
17515         case RID_PROTECTED:
17516         case RID_PRIVATE:
17517           /* Consume the access-specifier.  */
17518           cp_lexer_consume_token (parser->lexer);
17519           /* Remember which access-specifier is active.  */
17520           current_access_specifier = token->u.value;
17521           /* Look for the `:'.  */
17522           cp_parser_require (parser, CPP_COLON, RT_COLON);
17523           break;
17524
17525         default:
17526           /* Accept #pragmas at class scope.  */
17527           if (token->type == CPP_PRAGMA)
17528             {
17529               cp_parser_pragma (parser, pragma_external);
17530               break;
17531             }
17532
17533           /* Otherwise, the next construction must be a
17534              member-declaration.  */
17535           cp_parser_member_declaration (parser);
17536         }
17537     }
17538 }
17539
17540 /* Parse a member-declaration.
17541
17542    member-declaration:
17543      decl-specifier-seq [opt] member-declarator-list [opt] ;
17544      function-definition ; [opt]
17545      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17546      using-declaration
17547      template-declaration
17548
17549    member-declarator-list:
17550      member-declarator
17551      member-declarator-list , member-declarator
17552
17553    member-declarator:
17554      declarator pure-specifier [opt]
17555      declarator constant-initializer [opt]
17556      identifier [opt] : constant-expression
17557
17558    GNU Extensions:
17559
17560    member-declaration:
17561      __extension__ member-declaration
17562
17563    member-declarator:
17564      declarator attributes [opt] pure-specifier [opt]
17565      declarator attributes [opt] constant-initializer [opt]
17566      identifier [opt] attributes [opt] : constant-expression  
17567
17568    C++0x Extensions:
17569
17570    member-declaration:
17571      static_assert-declaration  */
17572
17573 static void
17574 cp_parser_member_declaration (cp_parser* parser)
17575 {
17576   cp_decl_specifier_seq decl_specifiers;
17577   tree prefix_attributes;
17578   tree decl;
17579   int declares_class_or_enum;
17580   bool friend_p;
17581   cp_token *token = NULL;
17582   cp_token *decl_spec_token_start = NULL;
17583   cp_token *initializer_token_start = NULL;
17584   int saved_pedantic;
17585
17586   /* Check for the `__extension__' keyword.  */
17587   if (cp_parser_extension_opt (parser, &saved_pedantic))
17588     {
17589       /* Recurse.  */
17590       cp_parser_member_declaration (parser);
17591       /* Restore the old value of the PEDANTIC flag.  */
17592       pedantic = saved_pedantic;
17593
17594       return;
17595     }
17596
17597   /* Check for a template-declaration.  */
17598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17599     {
17600       /* An explicit specialization here is an error condition, and we
17601          expect the specialization handler to detect and report this.  */
17602       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17603           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17604         cp_parser_explicit_specialization (parser);
17605       else
17606         cp_parser_template_declaration (parser, /*member_p=*/true);
17607
17608       return;
17609     }
17610
17611   /* Check for a using-declaration.  */
17612   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17613     {
17614       /* Parse the using-declaration.  */
17615       cp_parser_using_declaration (parser,
17616                                    /*access_declaration_p=*/false);
17617       return;
17618     }
17619
17620   /* Check for @defs.  */
17621   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17622     {
17623       tree ivar, member;
17624       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17625       ivar = ivar_chains;
17626       while (ivar)
17627         {
17628           member = ivar;
17629           ivar = TREE_CHAIN (member);
17630           TREE_CHAIN (member) = NULL_TREE;
17631           finish_member_declaration (member);
17632         }
17633       return;
17634     }
17635
17636   /* If the next token is `static_assert' we have a static assertion.  */
17637   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17638     {
17639       cp_parser_static_assert (parser, /*member_p=*/true);
17640       return;
17641     }
17642
17643   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17644     return;
17645
17646   /* Parse the decl-specifier-seq.  */
17647   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17648   cp_parser_decl_specifier_seq (parser,
17649                                 CP_PARSER_FLAGS_OPTIONAL,
17650                                 &decl_specifiers,
17651                                 &declares_class_or_enum);
17652   prefix_attributes = decl_specifiers.attributes;
17653   decl_specifiers.attributes = NULL_TREE;
17654   /* Check for an invalid type-name.  */
17655   if (!decl_specifiers.any_type_specifiers_p
17656       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17657     return;
17658   /* If there is no declarator, then the decl-specifier-seq should
17659      specify a type.  */
17660   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17661     {
17662       /* If there was no decl-specifier-seq, and the next token is a
17663          `;', then we have something like:
17664
17665            struct S { ; };
17666
17667          [class.mem]
17668
17669          Each member-declaration shall declare at least one member
17670          name of the class.  */
17671       if (!decl_specifiers.any_specifiers_p)
17672         {
17673           cp_token *token = cp_lexer_peek_token (parser->lexer);
17674           if (!in_system_header_at (token->location))
17675             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17676         }
17677       else
17678         {
17679           tree type;
17680
17681           /* See if this declaration is a friend.  */
17682           friend_p = cp_parser_friend_p (&decl_specifiers);
17683           /* If there were decl-specifiers, check to see if there was
17684              a class-declaration.  */
17685           type = check_tag_decl (&decl_specifiers);
17686           /* Nested classes have already been added to the class, but
17687              a `friend' needs to be explicitly registered.  */
17688           if (friend_p)
17689             {
17690               /* If the `friend' keyword was present, the friend must
17691                  be introduced with a class-key.  */
17692                if (!declares_class_or_enum)
17693                  error_at (decl_spec_token_start->location,
17694                            "a class-key must be used when declaring a friend");
17695                /* In this case:
17696
17697                     template <typename T> struct A {
17698                       friend struct A<T>::B;
17699                     };
17700
17701                   A<T>::B will be represented by a TYPENAME_TYPE, and
17702                   therefore not recognized by check_tag_decl.  */
17703                if (!type
17704                    && decl_specifiers.type
17705                    && TYPE_P (decl_specifiers.type))
17706                  type = decl_specifiers.type;
17707                if (!type || !TYPE_P (type))
17708                  error_at (decl_spec_token_start->location,
17709                            "friend declaration does not name a class or "
17710                            "function");
17711                else
17712                  make_friend_class (current_class_type, type,
17713                                     /*complain=*/true);
17714             }
17715           /* If there is no TYPE, an error message will already have
17716              been issued.  */
17717           else if (!type || type == error_mark_node)
17718             ;
17719           /* An anonymous aggregate has to be handled specially; such
17720              a declaration really declares a data member (with a
17721              particular type), as opposed to a nested class.  */
17722           else if (ANON_AGGR_TYPE_P (type))
17723             {
17724               /* Remove constructors and such from TYPE, now that we
17725                  know it is an anonymous aggregate.  */
17726               fixup_anonymous_aggr (type);
17727               /* And make the corresponding data member.  */
17728               decl = build_decl (decl_spec_token_start->location,
17729                                  FIELD_DECL, NULL_TREE, type);
17730               /* Add it to the class.  */
17731               finish_member_declaration (decl);
17732             }
17733           else
17734             cp_parser_check_access_in_redeclaration
17735                                               (TYPE_NAME (type),
17736                                                decl_spec_token_start->location);
17737         }
17738     }
17739   else
17740     {
17741       bool assume_semicolon = false;
17742
17743       /* See if these declarations will be friends.  */
17744       friend_p = cp_parser_friend_p (&decl_specifiers);
17745
17746       /* Keep going until we hit the `;' at the end of the
17747          declaration.  */
17748       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17749         {
17750           tree attributes = NULL_TREE;
17751           tree first_attribute;
17752
17753           /* Peek at the next token.  */
17754           token = cp_lexer_peek_token (parser->lexer);
17755
17756           /* Check for a bitfield declaration.  */
17757           if (token->type == CPP_COLON
17758               || (token->type == CPP_NAME
17759                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17760                   == CPP_COLON))
17761             {
17762               tree identifier;
17763               tree width;
17764
17765               /* Get the name of the bitfield.  Note that we cannot just
17766                  check TOKEN here because it may have been invalidated by
17767                  the call to cp_lexer_peek_nth_token above.  */
17768               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17769                 identifier = cp_parser_identifier (parser);
17770               else
17771                 identifier = NULL_TREE;
17772
17773               /* Consume the `:' token.  */
17774               cp_lexer_consume_token (parser->lexer);
17775               /* Get the width of the bitfield.  */
17776               width
17777                 = cp_parser_constant_expression (parser,
17778                                                  /*allow_non_constant=*/false,
17779                                                  NULL);
17780
17781               /* Look for attributes that apply to the bitfield.  */
17782               attributes = cp_parser_attributes_opt (parser);
17783               /* Remember which attributes are prefix attributes and
17784                  which are not.  */
17785               first_attribute = attributes;
17786               /* Combine the attributes.  */
17787               attributes = chainon (prefix_attributes, attributes);
17788
17789               /* Create the bitfield declaration.  */
17790               decl = grokbitfield (identifier
17791                                    ? make_id_declarator (NULL_TREE,
17792                                                          identifier,
17793                                                          sfk_none)
17794                                    : NULL,
17795                                    &decl_specifiers,
17796                                    width,
17797                                    attributes);
17798             }
17799           else
17800             {
17801               cp_declarator *declarator;
17802               tree initializer;
17803               tree asm_specification;
17804               int ctor_dtor_or_conv_p;
17805
17806               /* Parse the declarator.  */
17807               declarator
17808                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17809                                         &ctor_dtor_or_conv_p,
17810                                         /*parenthesized_p=*/NULL,
17811                                         /*member_p=*/true);
17812
17813               /* If something went wrong parsing the declarator, make sure
17814                  that we at least consume some tokens.  */
17815               if (declarator == cp_error_declarator)
17816                 {
17817                   /* Skip to the end of the statement.  */
17818                   cp_parser_skip_to_end_of_statement (parser);
17819                   /* If the next token is not a semicolon, that is
17820                      probably because we just skipped over the body of
17821                      a function.  So, we consume a semicolon if
17822                      present, but do not issue an error message if it
17823                      is not present.  */
17824                   if (cp_lexer_next_token_is (parser->lexer,
17825                                               CPP_SEMICOLON))
17826                     cp_lexer_consume_token (parser->lexer);
17827                   return;
17828                 }
17829
17830               if (declares_class_or_enum & 2)
17831                 cp_parser_check_for_definition_in_return_type
17832                                             (declarator, decl_specifiers.type,
17833                                              decl_specifiers.type_location);
17834
17835               /* Look for an asm-specification.  */
17836               asm_specification = cp_parser_asm_specification_opt (parser);
17837               /* Look for attributes that apply to the declaration.  */
17838               attributes = cp_parser_attributes_opt (parser);
17839               /* Remember which attributes are prefix attributes and
17840                  which are not.  */
17841               first_attribute = attributes;
17842               /* Combine the attributes.  */
17843               attributes = chainon (prefix_attributes, attributes);
17844
17845               /* If it's an `=', then we have a constant-initializer or a
17846                  pure-specifier.  It is not correct to parse the
17847                  initializer before registering the member declaration
17848                  since the member declaration should be in scope while
17849                  its initializer is processed.  However, the rest of the
17850                  front end does not yet provide an interface that allows
17851                  us to handle this correctly.  */
17852               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17853                 {
17854                   /* In [class.mem]:
17855
17856                      A pure-specifier shall be used only in the declaration of
17857                      a virtual function.
17858
17859                      A member-declarator can contain a constant-initializer
17860                      only if it declares a static member of integral or
17861                      enumeration type.
17862
17863                      Therefore, if the DECLARATOR is for a function, we look
17864                      for a pure-specifier; otherwise, we look for a
17865                      constant-initializer.  When we call `grokfield', it will
17866                      perform more stringent semantics checks.  */
17867                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17868                   if (function_declarator_p (declarator))
17869                     initializer = cp_parser_pure_specifier (parser);
17870                   else
17871                     /* Parse the initializer.  */
17872                     initializer = cp_parser_constant_initializer (parser);
17873                 }
17874               /* Otherwise, there is no initializer.  */
17875               else
17876                 initializer = NULL_TREE;
17877
17878               /* See if we are probably looking at a function
17879                  definition.  We are certainly not looking at a
17880                  member-declarator.  Calling `grokfield' has
17881                  side-effects, so we must not do it unless we are sure
17882                  that we are looking at a member-declarator.  */
17883               if (cp_parser_token_starts_function_definition_p
17884                   (cp_lexer_peek_token (parser->lexer)))
17885                 {
17886                   /* The grammar does not allow a pure-specifier to be
17887                      used when a member function is defined.  (It is
17888                      possible that this fact is an oversight in the
17889                      standard, since a pure function may be defined
17890                      outside of the class-specifier.  */
17891                   if (initializer)
17892                     error_at (initializer_token_start->location,
17893                               "pure-specifier on function-definition");
17894                   decl = cp_parser_save_member_function_body (parser,
17895                                                               &decl_specifiers,
17896                                                               declarator,
17897                                                               attributes);
17898                   /* If the member was not a friend, declare it here.  */
17899                   if (!friend_p)
17900                     finish_member_declaration (decl);
17901                   /* Peek at the next token.  */
17902                   token = cp_lexer_peek_token (parser->lexer);
17903                   /* If the next token is a semicolon, consume it.  */
17904                   if (token->type == CPP_SEMICOLON)
17905                     cp_lexer_consume_token (parser->lexer);
17906                   return;
17907                 }
17908               else
17909                 if (declarator->kind == cdk_function)
17910                   declarator->id_loc = token->location;
17911                 /* Create the declaration.  */
17912                 decl = grokfield (declarator, &decl_specifiers,
17913                                   initializer, /*init_const_expr_p=*/true,
17914                                   asm_specification,
17915                                   attributes);
17916             }
17917
17918           /* Reset PREFIX_ATTRIBUTES.  */
17919           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17920             attributes = TREE_CHAIN (attributes);
17921           if (attributes)
17922             TREE_CHAIN (attributes) = NULL_TREE;
17923
17924           /* If there is any qualification still in effect, clear it
17925              now; we will be starting fresh with the next declarator.  */
17926           parser->scope = NULL_TREE;
17927           parser->qualifying_scope = NULL_TREE;
17928           parser->object_scope = NULL_TREE;
17929           /* If it's a `,', then there are more declarators.  */
17930           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17931             cp_lexer_consume_token (parser->lexer);
17932           /* If the next token isn't a `;', then we have a parse error.  */
17933           else if (cp_lexer_next_token_is_not (parser->lexer,
17934                                                CPP_SEMICOLON))
17935             {
17936               /* The next token might be a ways away from where the
17937                  actual semicolon is missing.  Find the previous token
17938                  and use that for our error position.  */
17939               cp_token *token = cp_lexer_previous_token (parser->lexer);
17940               error_at (token->location,
17941                         "expected %<;%> at end of member declaration");
17942
17943               /* Assume that the user meant to provide a semicolon.  If
17944                  we were to cp_parser_skip_to_end_of_statement, we might
17945                  skip to a semicolon inside a member function definition
17946                  and issue nonsensical error messages.  */
17947               assume_semicolon = true;
17948             }
17949
17950           if (decl)
17951             {
17952               /* Add DECL to the list of members.  */
17953               if (!friend_p)
17954                 finish_member_declaration (decl);
17955
17956               if (TREE_CODE (decl) == FUNCTION_DECL)
17957                 cp_parser_save_default_args (parser, decl);
17958             }
17959
17960           if (assume_semicolon)
17961             return;
17962         }
17963     }
17964
17965   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17966 }
17967
17968 /* Parse a pure-specifier.
17969
17970    pure-specifier:
17971      = 0
17972
17973    Returns INTEGER_ZERO_NODE if a pure specifier is found.
17974    Otherwise, ERROR_MARK_NODE is returned.  */
17975
17976 static tree
17977 cp_parser_pure_specifier (cp_parser* parser)
17978 {
17979   cp_token *token;
17980
17981   /* Look for the `=' token.  */
17982   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17983     return error_mark_node;
17984   /* Look for the `0' token.  */
17985   token = cp_lexer_peek_token (parser->lexer);
17986
17987   if (token->type == CPP_EOF
17988       || token->type == CPP_PRAGMA_EOL)
17989     return error_mark_node;
17990
17991   cp_lexer_consume_token (parser->lexer);
17992
17993   /* Accept = default or = delete in c++0x mode.  */
17994   if (token->keyword == RID_DEFAULT
17995       || token->keyword == RID_DELETE)
17996     {
17997       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17998       return token->u.value;
17999     }
18000
18001   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18002   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18003     {
18004       cp_parser_error (parser,
18005                        "invalid pure specifier (only %<= 0%> is allowed)");
18006       cp_parser_skip_to_end_of_statement (parser);
18007       return error_mark_node;
18008     }
18009   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18010     {
18011       error_at (token->location, "templates may not be %<virtual%>");
18012       return error_mark_node;
18013     }
18014
18015   return integer_zero_node;
18016 }
18017
18018 /* Parse a constant-initializer.
18019
18020    constant-initializer:
18021      = constant-expression
18022
18023    Returns a representation of the constant-expression.  */
18024
18025 static tree
18026 cp_parser_constant_initializer (cp_parser* parser)
18027 {
18028   /* Look for the `=' token.  */
18029   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18030     return error_mark_node;
18031
18032   /* It is invalid to write:
18033
18034        struct S { static const int i = { 7 }; };
18035
18036      */
18037   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18038     {
18039       cp_parser_error (parser,
18040                        "a brace-enclosed initializer is not allowed here");
18041       /* Consume the opening brace.  */
18042       cp_lexer_consume_token (parser->lexer);
18043       /* Skip the initializer.  */
18044       cp_parser_skip_to_closing_brace (parser);
18045       /* Look for the trailing `}'.  */
18046       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18047
18048       return error_mark_node;
18049     }
18050
18051   return cp_parser_constant_expression (parser,
18052                                         /*allow_non_constant=*/false,
18053                                         NULL);
18054 }
18055
18056 /* Derived classes [gram.class.derived] */
18057
18058 /* Parse a base-clause.
18059
18060    base-clause:
18061      : base-specifier-list
18062
18063    base-specifier-list:
18064      base-specifier ... [opt]
18065      base-specifier-list , base-specifier ... [opt]
18066
18067    Returns a TREE_LIST representing the base-classes, in the order in
18068    which they were declared.  The representation of each node is as
18069    described by cp_parser_base_specifier.
18070
18071    In the case that no bases are specified, this function will return
18072    NULL_TREE, not ERROR_MARK_NODE.  */
18073
18074 static tree
18075 cp_parser_base_clause (cp_parser* parser)
18076 {
18077   tree bases = NULL_TREE;
18078
18079   /* Look for the `:' that begins the list.  */
18080   cp_parser_require (parser, CPP_COLON, RT_COLON);
18081
18082   /* Scan the base-specifier-list.  */
18083   while (true)
18084     {
18085       cp_token *token;
18086       tree base;
18087       bool pack_expansion_p = false;
18088
18089       /* Look for the base-specifier.  */
18090       base = cp_parser_base_specifier (parser);
18091       /* Look for the (optional) ellipsis. */
18092       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18093         {
18094           /* Consume the `...'. */
18095           cp_lexer_consume_token (parser->lexer);
18096
18097           pack_expansion_p = true;
18098         }
18099
18100       /* Add BASE to the front of the list.  */
18101       if (base != error_mark_node)
18102         {
18103           if (pack_expansion_p)
18104             /* Make this a pack expansion type. */
18105             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18106           
18107
18108           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18109             {
18110               TREE_CHAIN (base) = bases;
18111               bases = base;
18112             }
18113         }
18114       /* Peek at the next token.  */
18115       token = cp_lexer_peek_token (parser->lexer);
18116       /* If it's not a comma, then the list is complete.  */
18117       if (token->type != CPP_COMMA)
18118         break;
18119       /* Consume the `,'.  */
18120       cp_lexer_consume_token (parser->lexer);
18121     }
18122
18123   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18124      base class had a qualified name.  However, the next name that
18125      appears is certainly not qualified.  */
18126   parser->scope = NULL_TREE;
18127   parser->qualifying_scope = NULL_TREE;
18128   parser->object_scope = NULL_TREE;
18129
18130   return nreverse (bases);
18131 }
18132
18133 /* Parse a base-specifier.
18134
18135    base-specifier:
18136      :: [opt] nested-name-specifier [opt] class-name
18137      virtual access-specifier [opt] :: [opt] nested-name-specifier
18138        [opt] class-name
18139      access-specifier virtual [opt] :: [opt] nested-name-specifier
18140        [opt] class-name
18141
18142    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18143    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18144    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18145    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18146
18147 static tree
18148 cp_parser_base_specifier (cp_parser* parser)
18149 {
18150   cp_token *token;
18151   bool done = false;
18152   bool virtual_p = false;
18153   bool duplicate_virtual_error_issued_p = false;
18154   bool duplicate_access_error_issued_p = false;
18155   bool class_scope_p, template_p;
18156   tree access = access_default_node;
18157   tree type;
18158
18159   /* Process the optional `virtual' and `access-specifier'.  */
18160   while (!done)
18161     {
18162       /* Peek at the next token.  */
18163       token = cp_lexer_peek_token (parser->lexer);
18164       /* Process `virtual'.  */
18165       switch (token->keyword)
18166         {
18167         case RID_VIRTUAL:
18168           /* If `virtual' appears more than once, issue an error.  */
18169           if (virtual_p && !duplicate_virtual_error_issued_p)
18170             {
18171               cp_parser_error (parser,
18172                                "%<virtual%> specified more than once in base-specified");
18173               duplicate_virtual_error_issued_p = true;
18174             }
18175
18176           virtual_p = true;
18177
18178           /* Consume the `virtual' token.  */
18179           cp_lexer_consume_token (parser->lexer);
18180
18181           break;
18182
18183         case RID_PUBLIC:
18184         case RID_PROTECTED:
18185         case RID_PRIVATE:
18186           /* If more than one access specifier appears, issue an
18187              error.  */
18188           if (access != access_default_node
18189               && !duplicate_access_error_issued_p)
18190             {
18191               cp_parser_error (parser,
18192                                "more than one access specifier in base-specified");
18193               duplicate_access_error_issued_p = true;
18194             }
18195
18196           access = ridpointers[(int) token->keyword];
18197
18198           /* Consume the access-specifier.  */
18199           cp_lexer_consume_token (parser->lexer);
18200
18201           break;
18202
18203         default:
18204           done = true;
18205           break;
18206         }
18207     }
18208   /* It is not uncommon to see programs mechanically, erroneously, use
18209      the 'typename' keyword to denote (dependent) qualified types
18210      as base classes.  */
18211   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18212     {
18213       token = cp_lexer_peek_token (parser->lexer);
18214       if (!processing_template_decl)
18215         error_at (token->location,
18216                   "keyword %<typename%> not allowed outside of templates");
18217       else
18218         error_at (token->location,
18219                   "keyword %<typename%> not allowed in this context "
18220                   "(the base class is implicitly a type)");
18221       cp_lexer_consume_token (parser->lexer);
18222     }
18223
18224   /* Look for the optional `::' operator.  */
18225   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18226   /* Look for the nested-name-specifier.  The simplest way to
18227      implement:
18228
18229        [temp.res]
18230
18231        The keyword `typename' is not permitted in a base-specifier or
18232        mem-initializer; in these contexts a qualified name that
18233        depends on a template-parameter is implicitly assumed to be a
18234        type name.
18235
18236      is to pretend that we have seen the `typename' keyword at this
18237      point.  */
18238   cp_parser_nested_name_specifier_opt (parser,
18239                                        /*typename_keyword_p=*/true,
18240                                        /*check_dependency_p=*/true,
18241                                        typename_type,
18242                                        /*is_declaration=*/true);
18243   /* If the base class is given by a qualified name, assume that names
18244      we see are type names or templates, as appropriate.  */
18245   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18246   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18247
18248   /* Finally, look for the class-name.  */
18249   type = cp_parser_class_name (parser,
18250                                class_scope_p,
18251                                template_p,
18252                                typename_type,
18253                                /*check_dependency_p=*/true,
18254                                /*class_head_p=*/false,
18255                                /*is_declaration=*/true);
18256
18257   if (type == error_mark_node)
18258     return error_mark_node;
18259
18260   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18261 }
18262
18263 /* Exception handling [gram.exception] */
18264
18265 /* Parse an (optional) exception-specification.
18266
18267    exception-specification:
18268      throw ( type-id-list [opt] )
18269
18270    Returns a TREE_LIST representing the exception-specification.  The
18271    TREE_VALUE of each node is a type.  */
18272
18273 static tree
18274 cp_parser_exception_specification_opt (cp_parser* parser)
18275 {
18276   cp_token *token;
18277   tree type_id_list;
18278   const char *saved_message;
18279
18280   /* Peek at the next token.  */
18281   token = cp_lexer_peek_token (parser->lexer);
18282
18283   /* Is it a noexcept-specification?  */
18284   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18285     {
18286       tree expr;
18287       cp_lexer_consume_token (parser->lexer);
18288
18289       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18290         {
18291           cp_lexer_consume_token (parser->lexer);
18292
18293           /* Types may not be defined in an exception-specification.  */
18294           saved_message = parser->type_definition_forbidden_message;
18295           parser->type_definition_forbidden_message
18296             = G_("types may not be defined in an exception-specification");
18297
18298           expr = cp_parser_constant_expression (parser, false, NULL);
18299
18300           /* Restore the saved message.  */
18301           parser->type_definition_forbidden_message = saved_message;
18302
18303           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18304         }
18305       else
18306         expr = boolean_true_node;
18307
18308       return build_noexcept_spec (expr, tf_warning_or_error);
18309     }
18310
18311   /* If it's not `throw', then there's no exception-specification.  */
18312   if (!cp_parser_is_keyword (token, RID_THROW))
18313     return NULL_TREE;
18314
18315 #if 0
18316   /* Enable this once a lot of code has transitioned to noexcept?  */
18317   if (cxx_dialect == cxx0x && !in_system_header)
18318     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18319              "deprecated in C++0x; use %<noexcept%> instead");
18320 #endif
18321
18322   /* Consume the `throw'.  */
18323   cp_lexer_consume_token (parser->lexer);
18324
18325   /* Look for the `('.  */
18326   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18327
18328   /* Peek at the next token.  */
18329   token = cp_lexer_peek_token (parser->lexer);
18330   /* If it's not a `)', then there is a type-id-list.  */
18331   if (token->type != CPP_CLOSE_PAREN)
18332     {
18333       /* Types may not be defined in an exception-specification.  */
18334       saved_message = parser->type_definition_forbidden_message;
18335       parser->type_definition_forbidden_message
18336         = G_("types may not be defined in an exception-specification");
18337       /* Parse the type-id-list.  */
18338       type_id_list = cp_parser_type_id_list (parser);
18339       /* Restore the saved message.  */
18340       parser->type_definition_forbidden_message = saved_message;
18341     }
18342   else
18343     type_id_list = empty_except_spec;
18344
18345   /* Look for the `)'.  */
18346   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18347
18348   return type_id_list;
18349 }
18350
18351 /* Parse an (optional) type-id-list.
18352
18353    type-id-list:
18354      type-id ... [opt]
18355      type-id-list , type-id ... [opt]
18356
18357    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18358    in the order that the types were presented.  */
18359
18360 static tree
18361 cp_parser_type_id_list (cp_parser* parser)
18362 {
18363   tree types = NULL_TREE;
18364
18365   while (true)
18366     {
18367       cp_token *token;
18368       tree type;
18369
18370       /* Get the next type-id.  */
18371       type = cp_parser_type_id (parser);
18372       /* Parse the optional ellipsis. */
18373       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18374         {
18375           /* Consume the `...'. */
18376           cp_lexer_consume_token (parser->lexer);
18377
18378           /* Turn the type into a pack expansion expression. */
18379           type = make_pack_expansion (type);
18380         }
18381       /* Add it to the list.  */
18382       types = add_exception_specifier (types, type, /*complain=*/1);
18383       /* Peek at the next token.  */
18384       token = cp_lexer_peek_token (parser->lexer);
18385       /* If it is not a `,', we are done.  */
18386       if (token->type != CPP_COMMA)
18387         break;
18388       /* Consume the `,'.  */
18389       cp_lexer_consume_token (parser->lexer);
18390     }
18391
18392   return nreverse (types);
18393 }
18394
18395 /* Parse a try-block.
18396
18397    try-block:
18398      try compound-statement handler-seq  */
18399
18400 static tree
18401 cp_parser_try_block (cp_parser* parser)
18402 {
18403   tree try_block;
18404
18405   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18406   try_block = begin_try_block ();
18407   cp_parser_compound_statement (parser, NULL, true);
18408   finish_try_block (try_block);
18409   cp_parser_handler_seq (parser);
18410   finish_handler_sequence (try_block);
18411
18412   return try_block;
18413 }
18414
18415 /* Parse a function-try-block.
18416
18417    function-try-block:
18418      try ctor-initializer [opt] function-body handler-seq  */
18419
18420 static bool
18421 cp_parser_function_try_block (cp_parser* parser)
18422 {
18423   tree compound_stmt;
18424   tree try_block;
18425   bool ctor_initializer_p;
18426
18427   /* Look for the `try' keyword.  */
18428   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18429     return false;
18430   /* Let the rest of the front end know where we are.  */
18431   try_block = begin_function_try_block (&compound_stmt);
18432   /* Parse the function-body.  */
18433   ctor_initializer_p
18434     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18435   /* We're done with the `try' part.  */
18436   finish_function_try_block (try_block);
18437   /* Parse the handlers.  */
18438   cp_parser_handler_seq (parser);
18439   /* We're done with the handlers.  */
18440   finish_function_handler_sequence (try_block, compound_stmt);
18441
18442   return ctor_initializer_p;
18443 }
18444
18445 /* Parse a handler-seq.
18446
18447    handler-seq:
18448      handler handler-seq [opt]  */
18449
18450 static void
18451 cp_parser_handler_seq (cp_parser* parser)
18452 {
18453   while (true)
18454     {
18455       cp_token *token;
18456
18457       /* Parse the handler.  */
18458       cp_parser_handler (parser);
18459       /* Peek at the next token.  */
18460       token = cp_lexer_peek_token (parser->lexer);
18461       /* If it's not `catch' then there are no more handlers.  */
18462       if (!cp_parser_is_keyword (token, RID_CATCH))
18463         break;
18464     }
18465 }
18466
18467 /* Parse a handler.
18468
18469    handler:
18470      catch ( exception-declaration ) compound-statement  */
18471
18472 static void
18473 cp_parser_handler (cp_parser* parser)
18474 {
18475   tree handler;
18476   tree declaration;
18477
18478   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18479   handler = begin_handler ();
18480   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18481   declaration = cp_parser_exception_declaration (parser);
18482   finish_handler_parms (declaration, handler);
18483   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18484   cp_parser_compound_statement (parser, NULL, false);
18485   finish_handler (handler);
18486 }
18487
18488 /* Parse an exception-declaration.
18489
18490    exception-declaration:
18491      type-specifier-seq declarator
18492      type-specifier-seq abstract-declarator
18493      type-specifier-seq
18494      ...
18495
18496    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18497    ellipsis variant is used.  */
18498
18499 static tree
18500 cp_parser_exception_declaration (cp_parser* parser)
18501 {
18502   cp_decl_specifier_seq type_specifiers;
18503   cp_declarator *declarator;
18504   const char *saved_message;
18505
18506   /* If it's an ellipsis, it's easy to handle.  */
18507   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18508     {
18509       /* Consume the `...' token.  */
18510       cp_lexer_consume_token (parser->lexer);
18511       return NULL_TREE;
18512     }
18513
18514   /* Types may not be defined in exception-declarations.  */
18515   saved_message = parser->type_definition_forbidden_message;
18516   parser->type_definition_forbidden_message
18517     = G_("types may not be defined in exception-declarations");
18518
18519   /* Parse the type-specifier-seq.  */
18520   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18521                                 /*is_trailing_return=*/false,
18522                                 &type_specifiers);
18523   /* If it's a `)', then there is no declarator.  */
18524   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18525     declarator = NULL;
18526   else
18527     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18528                                        /*ctor_dtor_or_conv_p=*/NULL,
18529                                        /*parenthesized_p=*/NULL,
18530                                        /*member_p=*/false);
18531
18532   /* Restore the saved message.  */
18533   parser->type_definition_forbidden_message = saved_message;
18534
18535   if (!type_specifiers.any_specifiers_p)
18536     return error_mark_node;
18537
18538   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18539 }
18540
18541 /* Parse a throw-expression.
18542
18543    throw-expression:
18544      throw assignment-expression [opt]
18545
18546    Returns a THROW_EXPR representing the throw-expression.  */
18547
18548 static tree
18549 cp_parser_throw_expression (cp_parser* parser)
18550 {
18551   tree expression;
18552   cp_token* token;
18553
18554   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18555   token = cp_lexer_peek_token (parser->lexer);
18556   /* Figure out whether or not there is an assignment-expression
18557      following the "throw" keyword.  */
18558   if (token->type == CPP_COMMA
18559       || token->type == CPP_SEMICOLON
18560       || token->type == CPP_CLOSE_PAREN
18561       || token->type == CPP_CLOSE_SQUARE
18562       || token->type == CPP_CLOSE_BRACE
18563       || token->type == CPP_COLON)
18564     expression = NULL_TREE;
18565   else
18566     expression = cp_parser_assignment_expression (parser,
18567                                                   /*cast_p=*/false, NULL);
18568
18569   return build_throw (expression);
18570 }
18571
18572 /* GNU Extensions */
18573
18574 /* Parse an (optional) asm-specification.
18575
18576    asm-specification:
18577      asm ( string-literal )
18578
18579    If the asm-specification is present, returns a STRING_CST
18580    corresponding to the string-literal.  Otherwise, returns
18581    NULL_TREE.  */
18582
18583 static tree
18584 cp_parser_asm_specification_opt (cp_parser* parser)
18585 {
18586   cp_token *token;
18587   tree asm_specification;
18588
18589   /* Peek at the next token.  */
18590   token = cp_lexer_peek_token (parser->lexer);
18591   /* If the next token isn't the `asm' keyword, then there's no
18592      asm-specification.  */
18593   if (!cp_parser_is_keyword (token, RID_ASM))
18594     return NULL_TREE;
18595
18596   /* Consume the `asm' token.  */
18597   cp_lexer_consume_token (parser->lexer);
18598   /* Look for the `('.  */
18599   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18600
18601   /* Look for the string-literal.  */
18602   asm_specification = cp_parser_string_literal (parser, false, false);
18603
18604   /* Look for the `)'.  */
18605   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18606
18607   return asm_specification;
18608 }
18609
18610 /* Parse an asm-operand-list.
18611
18612    asm-operand-list:
18613      asm-operand
18614      asm-operand-list , asm-operand
18615
18616    asm-operand:
18617      string-literal ( expression )
18618      [ string-literal ] string-literal ( expression )
18619
18620    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18621    each node is the expression.  The TREE_PURPOSE is itself a
18622    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18623    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18624    is a STRING_CST for the string literal before the parenthesis. Returns
18625    ERROR_MARK_NODE if any of the operands are invalid.  */
18626
18627 static tree
18628 cp_parser_asm_operand_list (cp_parser* parser)
18629 {
18630   tree asm_operands = NULL_TREE;
18631   bool invalid_operands = false;
18632
18633   while (true)
18634     {
18635       tree string_literal;
18636       tree expression;
18637       tree name;
18638
18639       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18640         {
18641           /* Consume the `[' token.  */
18642           cp_lexer_consume_token (parser->lexer);
18643           /* Read the operand name.  */
18644           name = cp_parser_identifier (parser);
18645           if (name != error_mark_node)
18646             name = build_string (IDENTIFIER_LENGTH (name),
18647                                  IDENTIFIER_POINTER (name));
18648           /* Look for the closing `]'.  */
18649           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18650         }
18651       else
18652         name = NULL_TREE;
18653       /* Look for the string-literal.  */
18654       string_literal = cp_parser_string_literal (parser, false, false);
18655
18656       /* Look for the `('.  */
18657       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18658       /* Parse the expression.  */
18659       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18660       /* Look for the `)'.  */
18661       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18662
18663       if (name == error_mark_node 
18664           || string_literal == error_mark_node 
18665           || expression == error_mark_node)
18666         invalid_operands = true;
18667
18668       /* Add this operand to the list.  */
18669       asm_operands = tree_cons (build_tree_list (name, string_literal),
18670                                 expression,
18671                                 asm_operands);
18672       /* If the next token is not a `,', there are no more
18673          operands.  */
18674       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18675         break;
18676       /* Consume the `,'.  */
18677       cp_lexer_consume_token (parser->lexer);
18678     }
18679
18680   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18681 }
18682
18683 /* Parse an asm-clobber-list.
18684
18685    asm-clobber-list:
18686      string-literal
18687      asm-clobber-list , string-literal
18688
18689    Returns a TREE_LIST, indicating the clobbers in the order that they
18690    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18691
18692 static tree
18693 cp_parser_asm_clobber_list (cp_parser* parser)
18694 {
18695   tree clobbers = NULL_TREE;
18696
18697   while (true)
18698     {
18699       tree string_literal;
18700
18701       /* Look for the string literal.  */
18702       string_literal = cp_parser_string_literal (parser, false, false);
18703       /* Add it to the list.  */
18704       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18705       /* If the next token is not a `,', then the list is
18706          complete.  */
18707       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18708         break;
18709       /* Consume the `,' token.  */
18710       cp_lexer_consume_token (parser->lexer);
18711     }
18712
18713   return clobbers;
18714 }
18715
18716 /* Parse an asm-label-list.
18717
18718    asm-label-list:
18719      identifier
18720      asm-label-list , identifier
18721
18722    Returns a TREE_LIST, indicating the labels in the order that they
18723    appeared.  The TREE_VALUE of each node is a label.  */
18724
18725 static tree
18726 cp_parser_asm_label_list (cp_parser* parser)
18727 {
18728   tree labels = NULL_TREE;
18729
18730   while (true)
18731     {
18732       tree identifier, label, name;
18733
18734       /* Look for the identifier.  */
18735       identifier = cp_parser_identifier (parser);
18736       if (!error_operand_p (identifier))
18737         {
18738           label = lookup_label (identifier);
18739           if (TREE_CODE (label) == LABEL_DECL)
18740             {
18741               TREE_USED (label) = 1;
18742               check_goto (label);
18743               name = build_string (IDENTIFIER_LENGTH (identifier),
18744                                    IDENTIFIER_POINTER (identifier));
18745               labels = tree_cons (name, label, labels);
18746             }
18747         }
18748       /* If the next token is not a `,', then the list is
18749          complete.  */
18750       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18751         break;
18752       /* Consume the `,' token.  */
18753       cp_lexer_consume_token (parser->lexer);
18754     }
18755
18756   return nreverse (labels);
18757 }
18758
18759 /* Parse an (optional) series of attributes.
18760
18761    attributes:
18762      attributes attribute
18763
18764    attribute:
18765      __attribute__ (( attribute-list [opt] ))
18766
18767    The return value is as for cp_parser_attribute_list.  */
18768
18769 static tree
18770 cp_parser_attributes_opt (cp_parser* parser)
18771 {
18772   tree attributes = NULL_TREE;
18773
18774   while (true)
18775     {
18776       cp_token *token;
18777       tree attribute_list;
18778
18779       /* Peek at the next token.  */
18780       token = cp_lexer_peek_token (parser->lexer);
18781       /* If it's not `__attribute__', then we're done.  */
18782       if (token->keyword != RID_ATTRIBUTE)
18783         break;
18784
18785       /* Consume the `__attribute__' keyword.  */
18786       cp_lexer_consume_token (parser->lexer);
18787       /* Look for the two `(' tokens.  */
18788       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18789       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18790
18791       /* Peek at the next token.  */
18792       token = cp_lexer_peek_token (parser->lexer);
18793       if (token->type != CPP_CLOSE_PAREN)
18794         /* Parse the attribute-list.  */
18795         attribute_list = cp_parser_attribute_list (parser);
18796       else
18797         /* If the next token is a `)', then there is no attribute
18798            list.  */
18799         attribute_list = NULL;
18800
18801       /* Look for the two `)' tokens.  */
18802       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18803       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18804
18805       /* Add these new attributes to the list.  */
18806       attributes = chainon (attributes, attribute_list);
18807     }
18808
18809   return attributes;
18810 }
18811
18812 /* Parse an attribute-list.
18813
18814    attribute-list:
18815      attribute
18816      attribute-list , attribute
18817
18818    attribute:
18819      identifier
18820      identifier ( identifier )
18821      identifier ( identifier , expression-list )
18822      identifier ( expression-list )
18823
18824    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18825    to an attribute.  The TREE_PURPOSE of each node is the identifier
18826    indicating which attribute is in use.  The TREE_VALUE represents
18827    the arguments, if any.  */
18828
18829 static tree
18830 cp_parser_attribute_list (cp_parser* parser)
18831 {
18832   tree attribute_list = NULL_TREE;
18833   bool save_translate_strings_p = parser->translate_strings_p;
18834
18835   parser->translate_strings_p = false;
18836   while (true)
18837     {
18838       cp_token *token;
18839       tree identifier;
18840       tree attribute;
18841
18842       /* Look for the identifier.  We also allow keywords here; for
18843          example `__attribute__ ((const))' is legal.  */
18844       token = cp_lexer_peek_token (parser->lexer);
18845       if (token->type == CPP_NAME
18846           || token->type == CPP_KEYWORD)
18847         {
18848           tree arguments = NULL_TREE;
18849
18850           /* Consume the token.  */
18851           token = cp_lexer_consume_token (parser->lexer);
18852
18853           /* Save away the identifier that indicates which attribute
18854              this is.  */
18855           identifier = (token->type == CPP_KEYWORD) 
18856             /* For keywords, use the canonical spelling, not the
18857                parsed identifier.  */
18858             ? ridpointers[(int) token->keyword]
18859             : token->u.value;
18860           
18861           attribute = build_tree_list (identifier, NULL_TREE);
18862
18863           /* Peek at the next token.  */
18864           token = cp_lexer_peek_token (parser->lexer);
18865           /* If it's an `(', then parse the attribute arguments.  */
18866           if (token->type == CPP_OPEN_PAREN)
18867             {
18868               VEC(tree,gc) *vec;
18869               int attr_flag = (attribute_takes_identifier_p (identifier)
18870                                ? id_attr : normal_attr);
18871               vec = cp_parser_parenthesized_expression_list
18872                     (parser, attr_flag, /*cast_p=*/false,
18873                      /*allow_expansion_p=*/false,
18874                      /*non_constant_p=*/NULL);
18875               if (vec == NULL)
18876                 arguments = error_mark_node;
18877               else
18878                 {
18879                   arguments = build_tree_list_vec (vec);
18880                   release_tree_vector (vec);
18881                 }
18882               /* Save the arguments away.  */
18883               TREE_VALUE (attribute) = arguments;
18884             }
18885
18886           if (arguments != error_mark_node)
18887             {
18888               /* Add this attribute to the list.  */
18889               TREE_CHAIN (attribute) = attribute_list;
18890               attribute_list = attribute;
18891             }
18892
18893           token = cp_lexer_peek_token (parser->lexer);
18894         }
18895       /* Now, look for more attributes.  If the next token isn't a
18896          `,', we're done.  */
18897       if (token->type != CPP_COMMA)
18898         break;
18899
18900       /* Consume the comma and keep going.  */
18901       cp_lexer_consume_token (parser->lexer);
18902     }
18903   parser->translate_strings_p = save_translate_strings_p;
18904
18905   /* We built up the list in reverse order.  */
18906   return nreverse (attribute_list);
18907 }
18908
18909 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18910    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18911    current value of the PEDANTIC flag, regardless of whether or not
18912    the `__extension__' keyword is present.  The caller is responsible
18913    for restoring the value of the PEDANTIC flag.  */
18914
18915 static bool
18916 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18917 {
18918   /* Save the old value of the PEDANTIC flag.  */
18919   *saved_pedantic = pedantic;
18920
18921   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18922     {
18923       /* Consume the `__extension__' token.  */
18924       cp_lexer_consume_token (parser->lexer);
18925       /* We're not being pedantic while the `__extension__' keyword is
18926          in effect.  */
18927       pedantic = 0;
18928
18929       return true;
18930     }
18931
18932   return false;
18933 }
18934
18935 /* Parse a label declaration.
18936
18937    label-declaration:
18938      __label__ label-declarator-seq ;
18939
18940    label-declarator-seq:
18941      identifier , label-declarator-seq
18942      identifier  */
18943
18944 static void
18945 cp_parser_label_declaration (cp_parser* parser)
18946 {
18947   /* Look for the `__label__' keyword.  */
18948   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18949
18950   while (true)
18951     {
18952       tree identifier;
18953
18954       /* Look for an identifier.  */
18955       identifier = cp_parser_identifier (parser);
18956       /* If we failed, stop.  */
18957       if (identifier == error_mark_node)
18958         break;
18959       /* Declare it as a label.  */
18960       finish_label_decl (identifier);
18961       /* If the next token is a `;', stop.  */
18962       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18963         break;
18964       /* Look for the `,' separating the label declarations.  */
18965       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18966     }
18967
18968   /* Look for the final `;'.  */
18969   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18970 }
18971
18972 /* Support Functions */
18973
18974 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18975    NAME should have one of the representations used for an
18976    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18977    is returned.  If PARSER->SCOPE is a dependent type, then a
18978    SCOPE_REF is returned.
18979
18980    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18981    returned; the name was already resolved when the TEMPLATE_ID_EXPR
18982    was formed.  Abstractly, such entities should not be passed to this
18983    function, because they do not need to be looked up, but it is
18984    simpler to check for this special case here, rather than at the
18985    call-sites.
18986
18987    In cases not explicitly covered above, this function returns a
18988    DECL, OVERLOAD, or baselink representing the result of the lookup.
18989    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18990    is returned.
18991
18992    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18993    (e.g., "struct") that was used.  In that case bindings that do not
18994    refer to types are ignored.
18995
18996    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18997    ignored.
18998
18999    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19000    are ignored.
19001
19002    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19003    types.
19004
19005    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19006    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19007    NULL_TREE otherwise.  */
19008
19009 static tree
19010 cp_parser_lookup_name (cp_parser *parser, tree name,
19011                        enum tag_types tag_type,
19012                        bool is_template,
19013                        bool is_namespace,
19014                        bool check_dependency,
19015                        tree *ambiguous_decls,
19016                        location_t name_location)
19017 {
19018   int flags = 0;
19019   tree decl;
19020   tree object_type = parser->context->object_type;
19021
19022   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19023     flags |= LOOKUP_COMPLAIN;
19024
19025   /* Assume that the lookup will be unambiguous.  */
19026   if (ambiguous_decls)
19027     *ambiguous_decls = NULL_TREE;
19028
19029   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19030      no longer valid.  Note that if we are parsing tentatively, and
19031      the parse fails, OBJECT_TYPE will be automatically restored.  */
19032   parser->context->object_type = NULL_TREE;
19033
19034   if (name == error_mark_node)
19035     return error_mark_node;
19036
19037   /* A template-id has already been resolved; there is no lookup to
19038      do.  */
19039   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19040     return name;
19041   if (BASELINK_P (name))
19042     {
19043       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19044                   == TEMPLATE_ID_EXPR);
19045       return name;
19046     }
19047
19048   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19049      it should already have been checked to make sure that the name
19050      used matches the type being destroyed.  */
19051   if (TREE_CODE (name) == BIT_NOT_EXPR)
19052     {
19053       tree type;
19054
19055       /* Figure out to which type this destructor applies.  */
19056       if (parser->scope)
19057         type = parser->scope;
19058       else if (object_type)
19059         type = object_type;
19060       else
19061         type = current_class_type;
19062       /* If that's not a class type, there is no destructor.  */
19063       if (!type || !CLASS_TYPE_P (type))
19064         return error_mark_node;
19065       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19066         lazily_declare_fn (sfk_destructor, type);
19067       if (!CLASSTYPE_DESTRUCTORS (type))
19068           return error_mark_node;
19069       /* If it was a class type, return the destructor.  */
19070       return CLASSTYPE_DESTRUCTORS (type);
19071     }
19072
19073   /* By this point, the NAME should be an ordinary identifier.  If
19074      the id-expression was a qualified name, the qualifying scope is
19075      stored in PARSER->SCOPE at this point.  */
19076   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19077
19078   /* Perform the lookup.  */
19079   if (parser->scope)
19080     {
19081       bool dependent_p;
19082
19083       if (parser->scope == error_mark_node)
19084         return error_mark_node;
19085
19086       /* If the SCOPE is dependent, the lookup must be deferred until
19087          the template is instantiated -- unless we are explicitly
19088          looking up names in uninstantiated templates.  Even then, we
19089          cannot look up the name if the scope is not a class type; it
19090          might, for example, be a template type parameter.  */
19091       dependent_p = (TYPE_P (parser->scope)
19092                      && dependent_scope_p (parser->scope));
19093       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19094           && dependent_p)
19095         /* Defer lookup.  */
19096         decl = error_mark_node;
19097       else
19098         {
19099           tree pushed_scope = NULL_TREE;
19100
19101           /* If PARSER->SCOPE is a dependent type, then it must be a
19102              class type, and we must not be checking dependencies;
19103              otherwise, we would have processed this lookup above.  So
19104              that PARSER->SCOPE is not considered a dependent base by
19105              lookup_member, we must enter the scope here.  */
19106           if (dependent_p)
19107             pushed_scope = push_scope (parser->scope);
19108
19109           /* If the PARSER->SCOPE is a template specialization, it
19110              may be instantiated during name lookup.  In that case,
19111              errors may be issued.  Even if we rollback the current
19112              tentative parse, those errors are valid.  */
19113           decl = lookup_qualified_name (parser->scope, name,
19114                                         tag_type != none_type,
19115                                         /*complain=*/true);
19116
19117           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19118              lookup result and the nested-name-specifier nominates a class C:
19119                * if the name specified after the nested-name-specifier, when
19120                looked up in C, is the injected-class-name of C (Clause 9), or
19121                * if the name specified after the nested-name-specifier is the
19122                same as the identifier or the simple-template-id's template-
19123                name in the last component of the nested-name-specifier,
19124              the name is instead considered to name the constructor of
19125              class C. [ Note: for example, the constructor is not an
19126              acceptable lookup result in an elaborated-type-specifier so
19127              the constructor would not be used in place of the
19128              injected-class-name. --end note ] Such a constructor name
19129              shall be used only in the declarator-id of a declaration that
19130              names a constructor or in a using-declaration.  */
19131           if (tag_type == none_type
19132               && DECL_SELF_REFERENCE_P (decl)
19133               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19134             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19135                                           tag_type != none_type,
19136                                           /*complain=*/true);
19137
19138           /* If we have a single function from a using decl, pull it out.  */
19139           if (TREE_CODE (decl) == OVERLOAD
19140               && !really_overloaded_fn (decl))
19141             decl = OVL_FUNCTION (decl);
19142
19143           if (pushed_scope)
19144             pop_scope (pushed_scope);
19145         }
19146
19147       /* If the scope is a dependent type and either we deferred lookup or
19148          we did lookup but didn't find the name, rememeber the name.  */
19149       if (decl == error_mark_node && TYPE_P (parser->scope)
19150           && dependent_type_p (parser->scope))
19151         {
19152           if (tag_type)
19153             {
19154               tree type;
19155
19156               /* The resolution to Core Issue 180 says that `struct
19157                  A::B' should be considered a type-name, even if `A'
19158                  is dependent.  */
19159               type = make_typename_type (parser->scope, name, tag_type,
19160                                          /*complain=*/tf_error);
19161               decl = TYPE_NAME (type);
19162             }
19163           else if (is_template
19164                    && (cp_parser_next_token_ends_template_argument_p (parser)
19165                        || cp_lexer_next_token_is (parser->lexer,
19166                                                   CPP_CLOSE_PAREN)))
19167             decl = make_unbound_class_template (parser->scope,
19168                                                 name, NULL_TREE,
19169                                                 /*complain=*/tf_error);
19170           else
19171             decl = build_qualified_name (/*type=*/NULL_TREE,
19172                                          parser->scope, name,
19173                                          is_template);
19174         }
19175       parser->qualifying_scope = parser->scope;
19176       parser->object_scope = NULL_TREE;
19177     }
19178   else if (object_type)
19179     {
19180       tree object_decl = NULL_TREE;
19181       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19182          OBJECT_TYPE is not a class.  */
19183       if (CLASS_TYPE_P (object_type))
19184         /* If the OBJECT_TYPE is a template specialization, it may
19185            be instantiated during name lookup.  In that case, errors
19186            may be issued.  Even if we rollback the current tentative
19187            parse, those errors are valid.  */
19188         object_decl = lookup_member (object_type,
19189                                      name,
19190                                      /*protect=*/0,
19191                                      tag_type != none_type);
19192       /* Look it up in the enclosing context, too.  */
19193       decl = lookup_name_real (name, tag_type != none_type,
19194                                /*nonclass=*/0,
19195                                /*block_p=*/true, is_namespace, flags);
19196       parser->object_scope = object_type;
19197       parser->qualifying_scope = NULL_TREE;
19198       if (object_decl)
19199         decl = object_decl;
19200     }
19201   else
19202     {
19203       decl = lookup_name_real (name, tag_type != none_type,
19204                                /*nonclass=*/0,
19205                                /*block_p=*/true, is_namespace, flags);
19206       parser->qualifying_scope = NULL_TREE;
19207       parser->object_scope = NULL_TREE;
19208     }
19209
19210   /* If the lookup failed, let our caller know.  */
19211   if (!decl || decl == error_mark_node)
19212     return error_mark_node;
19213
19214   /* Pull out the template from an injected-class-name (or multiple).  */
19215   if (is_template)
19216     decl = maybe_get_template_decl_from_type_decl (decl);
19217
19218   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19219   if (TREE_CODE (decl) == TREE_LIST)
19220     {
19221       if (ambiguous_decls)
19222         *ambiguous_decls = decl;
19223       /* The error message we have to print is too complicated for
19224          cp_parser_error, so we incorporate its actions directly.  */
19225       if (!cp_parser_simulate_error (parser))
19226         {
19227           error_at (name_location, "reference to %qD is ambiguous",
19228                     name);
19229           print_candidates (decl);
19230         }
19231       return error_mark_node;
19232     }
19233
19234   gcc_assert (DECL_P (decl)
19235               || TREE_CODE (decl) == OVERLOAD
19236               || TREE_CODE (decl) == SCOPE_REF
19237               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19238               || BASELINK_P (decl));
19239
19240   /* If we have resolved the name of a member declaration, check to
19241      see if the declaration is accessible.  When the name resolves to
19242      set of overloaded functions, accessibility is checked when
19243      overload resolution is done.
19244
19245      During an explicit instantiation, access is not checked at all,
19246      as per [temp.explicit].  */
19247   if (DECL_P (decl))
19248     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19249
19250   return decl;
19251 }
19252
19253 /* Like cp_parser_lookup_name, but for use in the typical case where
19254    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19255    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19256
19257 static tree
19258 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19259 {
19260   return cp_parser_lookup_name (parser, name,
19261                                 none_type,
19262                                 /*is_template=*/false,
19263                                 /*is_namespace=*/false,
19264                                 /*check_dependency=*/true,
19265                                 /*ambiguous_decls=*/NULL,
19266                                 location);
19267 }
19268
19269 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19270    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19271    true, the DECL indicates the class being defined in a class-head,
19272    or declared in an elaborated-type-specifier.
19273
19274    Otherwise, return DECL.  */
19275
19276 static tree
19277 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19278 {
19279   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19280      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19281
19282        struct A {
19283          template <typename T> struct B;
19284        };
19285
19286        template <typename T> struct A::B {};
19287
19288      Similarly, in an elaborated-type-specifier:
19289
19290        namespace N { struct X{}; }
19291
19292        struct A {
19293          template <typename T> friend struct N::X;
19294        };
19295
19296      However, if the DECL refers to a class type, and we are in
19297      the scope of the class, then the name lookup automatically
19298      finds the TYPE_DECL created by build_self_reference rather
19299      than a TEMPLATE_DECL.  For example, in:
19300
19301        template <class T> struct S {
19302          S s;
19303        };
19304
19305      there is no need to handle such case.  */
19306
19307   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19308     return DECL_TEMPLATE_RESULT (decl);
19309
19310   return decl;
19311 }
19312
19313 /* If too many, or too few, template-parameter lists apply to the
19314    declarator, issue an error message.  Returns TRUE if all went well,
19315    and FALSE otherwise.  */
19316
19317 static bool
19318 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19319                                                 cp_declarator *declarator,
19320                                                 location_t declarator_location)
19321 {
19322   unsigned num_templates;
19323
19324   /* We haven't seen any classes that involve template parameters yet.  */
19325   num_templates = 0;
19326
19327   switch (declarator->kind)
19328     {
19329     case cdk_id:
19330       if (declarator->u.id.qualifying_scope)
19331         {
19332           tree scope;
19333
19334           scope = declarator->u.id.qualifying_scope;
19335
19336           while (scope && CLASS_TYPE_P (scope))
19337             {
19338               /* You're supposed to have one `template <...>'
19339                  for every template class, but you don't need one
19340                  for a full specialization.  For example:
19341
19342                  template <class T> struct S{};
19343                  template <> struct S<int> { void f(); };
19344                  void S<int>::f () {}
19345
19346                  is correct; there shouldn't be a `template <>' for
19347                  the definition of `S<int>::f'.  */
19348               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19349                 /* If SCOPE does not have template information of any
19350                    kind, then it is not a template, nor is it nested
19351                    within a template.  */
19352                 break;
19353               if (explicit_class_specialization_p (scope))
19354                 break;
19355               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19356                 ++num_templates;
19357
19358               scope = TYPE_CONTEXT (scope);
19359             }
19360         }
19361       else if (TREE_CODE (declarator->u.id.unqualified_name)
19362                == TEMPLATE_ID_EXPR)
19363         /* If the DECLARATOR has the form `X<y>' then it uses one
19364            additional level of template parameters.  */
19365         ++num_templates;
19366
19367       return cp_parser_check_template_parameters 
19368         (parser, num_templates, declarator_location, declarator);
19369
19370
19371     case cdk_function:
19372     case cdk_array:
19373     case cdk_pointer:
19374     case cdk_reference:
19375     case cdk_ptrmem:
19376       return (cp_parser_check_declarator_template_parameters
19377               (parser, declarator->declarator, declarator_location));
19378
19379     case cdk_error:
19380       return true;
19381
19382     default:
19383       gcc_unreachable ();
19384     }
19385   return false;
19386 }
19387
19388 /* NUM_TEMPLATES were used in the current declaration.  If that is
19389    invalid, return FALSE and issue an error messages.  Otherwise,
19390    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19391    declarator and we can print more accurate diagnostics.  */
19392
19393 static bool
19394 cp_parser_check_template_parameters (cp_parser* parser,
19395                                      unsigned num_templates,
19396                                      location_t location,
19397                                      cp_declarator *declarator)
19398 {
19399   /* If there are the same number of template classes and parameter
19400      lists, that's OK.  */
19401   if (parser->num_template_parameter_lists == num_templates)
19402     return true;
19403   /* If there are more, but only one more, then we are referring to a
19404      member template.  That's OK too.  */
19405   if (parser->num_template_parameter_lists == num_templates + 1)
19406     return true;
19407   /* If there are more template classes than parameter lists, we have
19408      something like:
19409
19410        template <class T> void S<T>::R<T>::f ();  */
19411   if (parser->num_template_parameter_lists < num_templates)
19412     {
19413       if (declarator && !current_function_decl)
19414         error_at (location, "specializing member %<%T::%E%> "
19415                   "requires %<template<>%> syntax", 
19416                   declarator->u.id.qualifying_scope,
19417                   declarator->u.id.unqualified_name);
19418       else if (declarator)
19419         error_at (location, "invalid declaration of %<%T::%E%>",
19420                   declarator->u.id.qualifying_scope,
19421                   declarator->u.id.unqualified_name);
19422       else 
19423         error_at (location, "too few template-parameter-lists");
19424       return false;
19425     }
19426   /* Otherwise, there are too many template parameter lists.  We have
19427      something like:
19428
19429      template <class T> template <class U> void S::f();  */
19430   error_at (location, "too many template-parameter-lists");
19431   return false;
19432 }
19433
19434 /* Parse an optional `::' token indicating that the following name is
19435    from the global namespace.  If so, PARSER->SCOPE is set to the
19436    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19437    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19438    Returns the new value of PARSER->SCOPE, if the `::' token is
19439    present, and NULL_TREE otherwise.  */
19440
19441 static tree
19442 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19443 {
19444   cp_token *token;
19445
19446   /* Peek at the next token.  */
19447   token = cp_lexer_peek_token (parser->lexer);
19448   /* If we're looking at a `::' token then we're starting from the
19449      global namespace, not our current location.  */
19450   if (token->type == CPP_SCOPE)
19451     {
19452       /* Consume the `::' token.  */
19453       cp_lexer_consume_token (parser->lexer);
19454       /* Set the SCOPE so that we know where to start the lookup.  */
19455       parser->scope = global_namespace;
19456       parser->qualifying_scope = global_namespace;
19457       parser->object_scope = NULL_TREE;
19458
19459       return parser->scope;
19460     }
19461   else if (!current_scope_valid_p)
19462     {
19463       parser->scope = NULL_TREE;
19464       parser->qualifying_scope = NULL_TREE;
19465       parser->object_scope = NULL_TREE;
19466     }
19467
19468   return NULL_TREE;
19469 }
19470
19471 /* Returns TRUE if the upcoming token sequence is the start of a
19472    constructor declarator.  If FRIEND_P is true, the declarator is
19473    preceded by the `friend' specifier.  */
19474
19475 static bool
19476 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19477 {
19478   bool constructor_p;
19479   tree nested_name_specifier;
19480   cp_token *next_token;
19481
19482   /* The common case is that this is not a constructor declarator, so
19483      try to avoid doing lots of work if at all possible.  It's not
19484      valid declare a constructor at function scope.  */
19485   if (parser->in_function_body)
19486     return false;
19487   /* And only certain tokens can begin a constructor declarator.  */
19488   next_token = cp_lexer_peek_token (parser->lexer);
19489   if (next_token->type != CPP_NAME
19490       && next_token->type != CPP_SCOPE
19491       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19492       && next_token->type != CPP_TEMPLATE_ID)
19493     return false;
19494
19495   /* Parse tentatively; we are going to roll back all of the tokens
19496      consumed here.  */
19497   cp_parser_parse_tentatively (parser);
19498   /* Assume that we are looking at a constructor declarator.  */
19499   constructor_p = true;
19500
19501   /* Look for the optional `::' operator.  */
19502   cp_parser_global_scope_opt (parser,
19503                               /*current_scope_valid_p=*/false);
19504   /* Look for the nested-name-specifier.  */
19505   nested_name_specifier
19506     = (cp_parser_nested_name_specifier_opt (parser,
19507                                             /*typename_keyword_p=*/false,
19508                                             /*check_dependency_p=*/false,
19509                                             /*type_p=*/false,
19510                                             /*is_declaration=*/false));
19511   /* Outside of a class-specifier, there must be a
19512      nested-name-specifier.  */
19513   if (!nested_name_specifier &&
19514       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19515        || friend_p))
19516     constructor_p = false;
19517   else if (nested_name_specifier == error_mark_node)
19518     constructor_p = false;
19519
19520   /* If we have a class scope, this is easy; DR 147 says that S::S always
19521      names the constructor, and no other qualified name could.  */
19522   if (constructor_p && nested_name_specifier
19523       && TYPE_P (nested_name_specifier))
19524     {
19525       tree id = cp_parser_unqualified_id (parser,
19526                                           /*template_keyword_p=*/false,
19527                                           /*check_dependency_p=*/false,
19528                                           /*declarator_p=*/true,
19529                                           /*optional_p=*/false);
19530       if (is_overloaded_fn (id))
19531         id = DECL_NAME (get_first_fn (id));
19532       if (!constructor_name_p (id, nested_name_specifier))
19533         constructor_p = false;
19534     }
19535   /* If we still think that this might be a constructor-declarator,
19536      look for a class-name.  */
19537   else if (constructor_p)
19538     {
19539       /* If we have:
19540
19541            template <typename T> struct S {
19542              S();
19543            };
19544
19545          we must recognize that the nested `S' names a class.  */
19546       tree type_decl;
19547       type_decl = cp_parser_class_name (parser,
19548                                         /*typename_keyword_p=*/false,
19549                                         /*template_keyword_p=*/false,
19550                                         none_type,
19551                                         /*check_dependency_p=*/false,
19552                                         /*class_head_p=*/false,
19553                                         /*is_declaration=*/false);
19554       /* If there was no class-name, then this is not a constructor.  */
19555       constructor_p = !cp_parser_error_occurred (parser);
19556
19557       /* If we're still considering a constructor, we have to see a `(',
19558          to begin the parameter-declaration-clause, followed by either a
19559          `)', an `...', or a decl-specifier.  We need to check for a
19560          type-specifier to avoid being fooled into thinking that:
19561
19562            S (f) (int);
19563
19564          is a constructor.  (It is actually a function named `f' that
19565          takes one parameter (of type `int') and returns a value of type
19566          `S'.  */
19567       if (constructor_p
19568           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19569         constructor_p = false;
19570
19571       if (constructor_p
19572           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19573           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19574           /* A parameter declaration begins with a decl-specifier,
19575              which is either the "attribute" keyword, a storage class
19576              specifier, or (usually) a type-specifier.  */
19577           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19578         {
19579           tree type;
19580           tree pushed_scope = NULL_TREE;
19581           unsigned saved_num_template_parameter_lists;
19582
19583           /* Names appearing in the type-specifier should be looked up
19584              in the scope of the class.  */
19585           if (current_class_type)
19586             type = NULL_TREE;
19587           else
19588             {
19589               type = TREE_TYPE (type_decl);
19590               if (TREE_CODE (type) == TYPENAME_TYPE)
19591                 {
19592                   type = resolve_typename_type (type,
19593                                                 /*only_current_p=*/false);
19594                   if (TREE_CODE (type) == TYPENAME_TYPE)
19595                     {
19596                       cp_parser_abort_tentative_parse (parser);
19597                       return false;
19598                     }
19599                 }
19600               pushed_scope = push_scope (type);
19601             }
19602
19603           /* Inside the constructor parameter list, surrounding
19604              template-parameter-lists do not apply.  */
19605           saved_num_template_parameter_lists
19606             = parser->num_template_parameter_lists;
19607           parser->num_template_parameter_lists = 0;
19608
19609           /* Look for the type-specifier.  */
19610           cp_parser_type_specifier (parser,
19611                                     CP_PARSER_FLAGS_NONE,
19612                                     /*decl_specs=*/NULL,
19613                                     /*is_declarator=*/true,
19614                                     /*declares_class_or_enum=*/NULL,
19615                                     /*is_cv_qualifier=*/NULL);
19616
19617           parser->num_template_parameter_lists
19618             = saved_num_template_parameter_lists;
19619
19620           /* Leave the scope of the class.  */
19621           if (pushed_scope)
19622             pop_scope (pushed_scope);
19623
19624           constructor_p = !cp_parser_error_occurred (parser);
19625         }
19626     }
19627
19628   /* We did not really want to consume any tokens.  */
19629   cp_parser_abort_tentative_parse (parser);
19630
19631   return constructor_p;
19632 }
19633
19634 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19635    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19636    they must be performed once we are in the scope of the function.
19637
19638    Returns the function defined.  */
19639
19640 static tree
19641 cp_parser_function_definition_from_specifiers_and_declarator
19642   (cp_parser* parser,
19643    cp_decl_specifier_seq *decl_specifiers,
19644    tree attributes,
19645    const cp_declarator *declarator)
19646 {
19647   tree fn;
19648   bool success_p;
19649
19650   /* Begin the function-definition.  */
19651   success_p = start_function (decl_specifiers, declarator, attributes);
19652
19653   /* The things we're about to see are not directly qualified by any
19654      template headers we've seen thus far.  */
19655   reset_specialization ();
19656
19657   /* If there were names looked up in the decl-specifier-seq that we
19658      did not check, check them now.  We must wait until we are in the
19659      scope of the function to perform the checks, since the function
19660      might be a friend.  */
19661   perform_deferred_access_checks ();
19662
19663   if (!success_p)
19664     {
19665       /* Skip the entire function.  */
19666       cp_parser_skip_to_end_of_block_or_statement (parser);
19667       fn = error_mark_node;
19668     }
19669   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19670     {
19671       /* Seen already, skip it.  An error message has already been output.  */
19672       cp_parser_skip_to_end_of_block_or_statement (parser);
19673       fn = current_function_decl;
19674       current_function_decl = NULL_TREE;
19675       /* If this is a function from a class, pop the nested class.  */
19676       if (current_class_name)
19677         pop_nested_class ();
19678     }
19679   else
19680     fn = cp_parser_function_definition_after_declarator (parser,
19681                                                          /*inline_p=*/false);
19682
19683   return fn;
19684 }
19685
19686 /* Parse the part of a function-definition that follows the
19687    declarator.  INLINE_P is TRUE iff this function is an inline
19688    function defined within a class-specifier.
19689
19690    Returns the function defined.  */
19691
19692 static tree
19693 cp_parser_function_definition_after_declarator (cp_parser* parser,
19694                                                 bool inline_p)
19695 {
19696   tree fn;
19697   bool ctor_initializer_p = false;
19698   bool saved_in_unbraced_linkage_specification_p;
19699   bool saved_in_function_body;
19700   unsigned saved_num_template_parameter_lists;
19701   cp_token *token;
19702
19703   saved_in_function_body = parser->in_function_body;
19704   parser->in_function_body = true;
19705   /* If the next token is `return', then the code may be trying to
19706      make use of the "named return value" extension that G++ used to
19707      support.  */
19708   token = cp_lexer_peek_token (parser->lexer);
19709   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19710     {
19711       /* Consume the `return' keyword.  */
19712       cp_lexer_consume_token (parser->lexer);
19713       /* Look for the identifier that indicates what value is to be
19714          returned.  */
19715       cp_parser_identifier (parser);
19716       /* Issue an error message.  */
19717       error_at (token->location,
19718                 "named return values are no longer supported");
19719       /* Skip tokens until we reach the start of the function body.  */
19720       while (true)
19721         {
19722           cp_token *token = cp_lexer_peek_token (parser->lexer);
19723           if (token->type == CPP_OPEN_BRACE
19724               || token->type == CPP_EOF
19725               || token->type == CPP_PRAGMA_EOL)
19726             break;
19727           cp_lexer_consume_token (parser->lexer);
19728         }
19729     }
19730   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19731      anything declared inside `f'.  */
19732   saved_in_unbraced_linkage_specification_p
19733     = parser->in_unbraced_linkage_specification_p;
19734   parser->in_unbraced_linkage_specification_p = false;
19735   /* Inside the function, surrounding template-parameter-lists do not
19736      apply.  */
19737   saved_num_template_parameter_lists
19738     = parser->num_template_parameter_lists;
19739   parser->num_template_parameter_lists = 0;
19740
19741   start_lambda_scope (current_function_decl);
19742
19743   /* If the next token is `try', then we are looking at a
19744      function-try-block.  */
19745   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19746     ctor_initializer_p = cp_parser_function_try_block (parser);
19747   /* A function-try-block includes the function-body, so we only do
19748      this next part if we're not processing a function-try-block.  */
19749   else
19750     ctor_initializer_p
19751       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19752
19753   finish_lambda_scope ();
19754
19755   /* Finish the function.  */
19756   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19757                         (inline_p ? 2 : 0));
19758   /* Generate code for it, if necessary.  */
19759   expand_or_defer_fn (fn);
19760   /* Restore the saved values.  */
19761   parser->in_unbraced_linkage_specification_p
19762     = saved_in_unbraced_linkage_specification_p;
19763   parser->num_template_parameter_lists
19764     = saved_num_template_parameter_lists;
19765   parser->in_function_body = saved_in_function_body;
19766
19767   return fn;
19768 }
19769
19770 /* Parse a template-declaration, assuming that the `export' (and
19771    `extern') keywords, if present, has already been scanned.  MEMBER_P
19772    is as for cp_parser_template_declaration.  */
19773
19774 static void
19775 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19776 {
19777   tree decl = NULL_TREE;
19778   VEC (deferred_access_check,gc) *checks;
19779   tree parameter_list;
19780   bool friend_p = false;
19781   bool need_lang_pop;
19782   cp_token *token;
19783
19784   /* Look for the `template' keyword.  */
19785   token = cp_lexer_peek_token (parser->lexer);
19786   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19787     return;
19788
19789   /* And the `<'.  */
19790   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19791     return;
19792   if (at_class_scope_p () && current_function_decl)
19793     {
19794       /* 14.5.2.2 [temp.mem]
19795
19796          A local class shall not have member templates.  */
19797       error_at (token->location,
19798                 "invalid declaration of member template in local class");
19799       cp_parser_skip_to_end_of_block_or_statement (parser);
19800       return;
19801     }
19802   /* [temp]
19803
19804      A template ... shall not have C linkage.  */
19805   if (current_lang_name == lang_name_c)
19806     {
19807       error_at (token->location, "template with C linkage");
19808       /* Give it C++ linkage to avoid confusing other parts of the
19809          front end.  */
19810       push_lang_context (lang_name_cplusplus);
19811       need_lang_pop = true;
19812     }
19813   else
19814     need_lang_pop = false;
19815
19816   /* We cannot perform access checks on the template parameter
19817      declarations until we know what is being declared, just as we
19818      cannot check the decl-specifier list.  */
19819   push_deferring_access_checks (dk_deferred);
19820
19821   /* If the next token is `>', then we have an invalid
19822      specialization.  Rather than complain about an invalid template
19823      parameter, issue an error message here.  */
19824   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19825     {
19826       cp_parser_error (parser, "invalid explicit specialization");
19827       begin_specialization ();
19828       parameter_list = NULL_TREE;
19829     }
19830   else
19831     /* Parse the template parameters.  */
19832     parameter_list = cp_parser_template_parameter_list (parser);
19833
19834   /* Get the deferred access checks from the parameter list.  These
19835      will be checked once we know what is being declared, as for a
19836      member template the checks must be performed in the scope of the
19837      class containing the member.  */
19838   checks = get_deferred_access_checks ();
19839
19840   /* Look for the `>'.  */
19841   cp_parser_skip_to_end_of_template_parameter_list (parser);
19842   /* We just processed one more parameter list.  */
19843   ++parser->num_template_parameter_lists;
19844   /* If the next token is `template', there are more template
19845      parameters.  */
19846   if (cp_lexer_next_token_is_keyword (parser->lexer,
19847                                       RID_TEMPLATE))
19848     cp_parser_template_declaration_after_export (parser, member_p);
19849   else
19850     {
19851       /* There are no access checks when parsing a template, as we do not
19852          know if a specialization will be a friend.  */
19853       push_deferring_access_checks (dk_no_check);
19854       token = cp_lexer_peek_token (parser->lexer);
19855       decl = cp_parser_single_declaration (parser,
19856                                            checks,
19857                                            member_p,
19858                                            /*explicit_specialization_p=*/false,
19859                                            &friend_p);
19860       pop_deferring_access_checks ();
19861
19862       /* If this is a member template declaration, let the front
19863          end know.  */
19864       if (member_p && !friend_p && decl)
19865         {
19866           if (TREE_CODE (decl) == TYPE_DECL)
19867             cp_parser_check_access_in_redeclaration (decl, token->location);
19868
19869           decl = finish_member_template_decl (decl);
19870         }
19871       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19872         make_friend_class (current_class_type, TREE_TYPE (decl),
19873                            /*complain=*/true);
19874     }
19875   /* We are done with the current parameter list.  */
19876   --parser->num_template_parameter_lists;
19877
19878   pop_deferring_access_checks ();
19879
19880   /* Finish up.  */
19881   finish_template_decl (parameter_list);
19882
19883   /* Register member declarations.  */
19884   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19885     finish_member_declaration (decl);
19886   /* For the erroneous case of a template with C linkage, we pushed an
19887      implicit C++ linkage scope; exit that scope now.  */
19888   if (need_lang_pop)
19889     pop_lang_context ();
19890   /* If DECL is a function template, we must return to parse it later.
19891      (Even though there is no definition, there might be default
19892      arguments that need handling.)  */
19893   if (member_p && decl
19894       && (TREE_CODE (decl) == FUNCTION_DECL
19895           || DECL_FUNCTION_TEMPLATE_P (decl)))
19896     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19897 }
19898
19899 /* Perform the deferred access checks from a template-parameter-list.
19900    CHECKS is a TREE_LIST of access checks, as returned by
19901    get_deferred_access_checks.  */
19902
19903 static void
19904 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19905 {
19906   ++processing_template_parmlist;
19907   perform_access_checks (checks);
19908   --processing_template_parmlist;
19909 }
19910
19911 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19912    `function-definition' sequence.  MEMBER_P is true, this declaration
19913    appears in a class scope.
19914
19915    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
19916    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
19917
19918 static tree
19919 cp_parser_single_declaration (cp_parser* parser,
19920                               VEC (deferred_access_check,gc)* checks,
19921                               bool member_p,
19922                               bool explicit_specialization_p,
19923                               bool* friend_p)
19924 {
19925   int declares_class_or_enum;
19926   tree decl = NULL_TREE;
19927   cp_decl_specifier_seq decl_specifiers;
19928   bool function_definition_p = false;
19929   cp_token *decl_spec_token_start;
19930
19931   /* This function is only used when processing a template
19932      declaration.  */
19933   gcc_assert (innermost_scope_kind () == sk_template_parms
19934               || innermost_scope_kind () == sk_template_spec);
19935
19936   /* Defer access checks until we know what is being declared.  */
19937   push_deferring_access_checks (dk_deferred);
19938
19939   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19940      alternative.  */
19941   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19942   cp_parser_decl_specifier_seq (parser,
19943                                 CP_PARSER_FLAGS_OPTIONAL,
19944                                 &decl_specifiers,
19945                                 &declares_class_or_enum);
19946   if (friend_p)
19947     *friend_p = cp_parser_friend_p (&decl_specifiers);
19948
19949   /* There are no template typedefs.  */
19950   if (decl_specifiers.specs[(int) ds_typedef])
19951     {
19952       error_at (decl_spec_token_start->location,
19953                 "template declaration of %<typedef%>");
19954       decl = error_mark_node;
19955     }
19956
19957   /* Gather up the access checks that occurred the
19958      decl-specifier-seq.  */
19959   stop_deferring_access_checks ();
19960
19961   /* Check for the declaration of a template class.  */
19962   if (declares_class_or_enum)
19963     {
19964       if (cp_parser_declares_only_class_p (parser))
19965         {
19966           decl = shadow_tag (&decl_specifiers);
19967
19968           /* In this case:
19969
19970                struct C {
19971                  friend template <typename T> struct A<T>::B;
19972                };
19973
19974              A<T>::B will be represented by a TYPENAME_TYPE, and
19975              therefore not recognized by shadow_tag.  */
19976           if (friend_p && *friend_p
19977               && !decl
19978               && decl_specifiers.type
19979               && TYPE_P (decl_specifiers.type))
19980             decl = decl_specifiers.type;
19981
19982           if (decl && decl != error_mark_node)
19983             decl = TYPE_NAME (decl);
19984           else
19985             decl = error_mark_node;
19986
19987           /* Perform access checks for template parameters.  */
19988           cp_parser_perform_template_parameter_access_checks (checks);
19989         }
19990     }
19991
19992   /* Complain about missing 'typename' or other invalid type names.  */
19993   if (!decl_specifiers.any_type_specifiers_p)
19994     cp_parser_parse_and_diagnose_invalid_type_name (parser);
19995
19996   /* If it's not a template class, try for a template function.  If
19997      the next token is a `;', then this declaration does not declare
19998      anything.  But, if there were errors in the decl-specifiers, then
19999      the error might well have come from an attempted class-specifier.
20000      In that case, there's no need to warn about a missing declarator.  */
20001   if (!decl
20002       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20003           || decl_specifiers.type != error_mark_node))
20004     {
20005       decl = cp_parser_init_declarator (parser,
20006                                         &decl_specifiers,
20007                                         checks,
20008                                         /*function_definition_allowed_p=*/true,
20009                                         member_p,
20010                                         declares_class_or_enum,
20011                                         &function_definition_p);
20012
20013     /* 7.1.1-1 [dcl.stc]
20014
20015        A storage-class-specifier shall not be specified in an explicit
20016        specialization...  */
20017     if (decl
20018         && explicit_specialization_p
20019         && decl_specifiers.storage_class != sc_none)
20020       {
20021         error_at (decl_spec_token_start->location,
20022                   "explicit template specialization cannot have a storage class");
20023         decl = error_mark_node;
20024       }
20025     }
20026
20027   pop_deferring_access_checks ();
20028
20029   /* Clear any current qualification; whatever comes next is the start
20030      of something new.  */
20031   parser->scope = NULL_TREE;
20032   parser->qualifying_scope = NULL_TREE;
20033   parser->object_scope = NULL_TREE;
20034   /* Look for a trailing `;' after the declaration.  */
20035   if (!function_definition_p
20036       && (decl == error_mark_node
20037           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20038     cp_parser_skip_to_end_of_block_or_statement (parser);
20039
20040   return decl;
20041 }
20042
20043 /* Parse a cast-expression that is not the operand of a unary "&".  */
20044
20045 static tree
20046 cp_parser_simple_cast_expression (cp_parser *parser)
20047 {
20048   return cp_parser_cast_expression (parser, /*address_p=*/false,
20049                                     /*cast_p=*/false, NULL);
20050 }
20051
20052 /* Parse a functional cast to TYPE.  Returns an expression
20053    representing the cast.  */
20054
20055 static tree
20056 cp_parser_functional_cast (cp_parser* parser, tree type)
20057 {
20058   VEC(tree,gc) *vec;
20059   tree expression_list;
20060   tree cast;
20061   bool nonconst_p;
20062
20063   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20064     {
20065       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20066       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20067       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20068       if (TREE_CODE (type) == TYPE_DECL)
20069         type = TREE_TYPE (type);
20070       return finish_compound_literal (type, expression_list);
20071     }
20072
20073
20074   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20075                                                  /*cast_p=*/true,
20076                                                  /*allow_expansion_p=*/true,
20077                                                  /*non_constant_p=*/NULL);
20078   if (vec == NULL)
20079     expression_list = error_mark_node;
20080   else
20081     {
20082       expression_list = build_tree_list_vec (vec);
20083       release_tree_vector (vec);
20084     }
20085
20086   cast = build_functional_cast (type, expression_list,
20087                                 tf_warning_or_error);
20088   /* [expr.const]/1: In an integral constant expression "only type
20089      conversions to integral or enumeration type can be used".  */
20090   if (TREE_CODE (type) == TYPE_DECL)
20091     type = TREE_TYPE (type);
20092   if (cast != error_mark_node
20093       && !cast_valid_in_integral_constant_expression_p (type)
20094       && cp_parser_non_integral_constant_expression (parser,
20095                                                      NIC_CONSTRUCTOR))
20096     return error_mark_node;
20097   return cast;
20098 }
20099
20100 /* Save the tokens that make up the body of a member function defined
20101    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20102    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20103    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20104    for the member function.  */
20105
20106 static tree
20107 cp_parser_save_member_function_body (cp_parser* parser,
20108                                      cp_decl_specifier_seq *decl_specifiers,
20109                                      cp_declarator *declarator,
20110                                      tree attributes)
20111 {
20112   cp_token *first;
20113   cp_token *last;
20114   tree fn;
20115
20116   /* Create the FUNCTION_DECL.  */
20117   fn = grokmethod (decl_specifiers, declarator, attributes);
20118   /* If something went badly wrong, bail out now.  */
20119   if (fn == error_mark_node)
20120     {
20121       /* If there's a function-body, skip it.  */
20122       if (cp_parser_token_starts_function_definition_p
20123           (cp_lexer_peek_token (parser->lexer)))
20124         cp_parser_skip_to_end_of_block_or_statement (parser);
20125       return error_mark_node;
20126     }
20127
20128   /* Remember it, if there default args to post process.  */
20129   cp_parser_save_default_args (parser, fn);
20130
20131   /* Save away the tokens that make up the body of the
20132      function.  */
20133   first = parser->lexer->next_token;
20134   /* We can have braced-init-list mem-initializers before the fn body.  */
20135   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20136     {
20137       cp_lexer_consume_token (parser->lexer);
20138       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20139              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20140         {
20141           /* cache_group will stop after an un-nested { } pair, too.  */
20142           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20143             break;
20144
20145           /* variadic mem-inits have ... after the ')'.  */
20146           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20147             cp_lexer_consume_token (parser->lexer);
20148         }
20149     }
20150   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20151   /* Handle function try blocks.  */
20152   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20153     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20154   last = parser->lexer->next_token;
20155
20156   /* Save away the inline definition; we will process it when the
20157      class is complete.  */
20158   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20159   DECL_PENDING_INLINE_P (fn) = 1;
20160
20161   /* We need to know that this was defined in the class, so that
20162      friend templates are handled correctly.  */
20163   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20164
20165   /* Add FN to the queue of functions to be parsed later.  */
20166   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20167
20168   return fn;
20169 }
20170
20171 /* Parse a template-argument-list, as well as the trailing ">" (but
20172    not the opening ">").  See cp_parser_template_argument_list for the
20173    return value.  */
20174
20175 static tree
20176 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20177 {
20178   tree arguments;
20179   tree saved_scope;
20180   tree saved_qualifying_scope;
20181   tree saved_object_scope;
20182   bool saved_greater_than_is_operator_p;
20183   int saved_unevaluated_operand;
20184   int saved_inhibit_evaluation_warnings;
20185
20186   /* [temp.names]
20187
20188      When parsing a template-id, the first non-nested `>' is taken as
20189      the end of the template-argument-list rather than a greater-than
20190      operator.  */
20191   saved_greater_than_is_operator_p
20192     = parser->greater_than_is_operator_p;
20193   parser->greater_than_is_operator_p = false;
20194   /* Parsing the argument list may modify SCOPE, so we save it
20195      here.  */
20196   saved_scope = parser->scope;
20197   saved_qualifying_scope = parser->qualifying_scope;
20198   saved_object_scope = parser->object_scope;
20199   /* We need to evaluate the template arguments, even though this
20200      template-id may be nested within a "sizeof".  */
20201   saved_unevaluated_operand = cp_unevaluated_operand;
20202   cp_unevaluated_operand = 0;
20203   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20204   c_inhibit_evaluation_warnings = 0;
20205   /* Parse the template-argument-list itself.  */
20206   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20207       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20208     arguments = NULL_TREE;
20209   else
20210     arguments = cp_parser_template_argument_list (parser);
20211   /* Look for the `>' that ends the template-argument-list. If we find
20212      a '>>' instead, it's probably just a typo.  */
20213   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20214     {
20215       if (cxx_dialect != cxx98)
20216         {
20217           /* In C++0x, a `>>' in a template argument list or cast
20218              expression is considered to be two separate `>'
20219              tokens. So, change the current token to a `>', but don't
20220              consume it: it will be consumed later when the outer
20221              template argument list (or cast expression) is parsed.
20222              Note that this replacement of `>' for `>>' is necessary
20223              even if we are parsing tentatively: in the tentative
20224              case, after calling
20225              cp_parser_enclosed_template_argument_list we will always
20226              throw away all of the template arguments and the first
20227              closing `>', either because the template argument list
20228              was erroneous or because we are replacing those tokens
20229              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20230              not have been thrown away) is needed either to close an
20231              outer template argument list or to complete a new-style
20232              cast.  */
20233           cp_token *token = cp_lexer_peek_token (parser->lexer);
20234           token->type = CPP_GREATER;
20235         }
20236       else if (!saved_greater_than_is_operator_p)
20237         {
20238           /* If we're in a nested template argument list, the '>>' has
20239             to be a typo for '> >'. We emit the error message, but we
20240             continue parsing and we push a '>' as next token, so that
20241             the argument list will be parsed correctly.  Note that the
20242             global source location is still on the token before the
20243             '>>', so we need to say explicitly where we want it.  */
20244           cp_token *token = cp_lexer_peek_token (parser->lexer);
20245           error_at (token->location, "%<>>%> should be %<> >%> "
20246                     "within a nested template argument list");
20247
20248           token->type = CPP_GREATER;
20249         }
20250       else
20251         {
20252           /* If this is not a nested template argument list, the '>>'
20253             is a typo for '>'. Emit an error message and continue.
20254             Same deal about the token location, but here we can get it
20255             right by consuming the '>>' before issuing the diagnostic.  */
20256           cp_token *token = cp_lexer_consume_token (parser->lexer);
20257           error_at (token->location,
20258                     "spurious %<>>%>, use %<>%> to terminate "
20259                     "a template argument list");
20260         }
20261     }
20262   else
20263     cp_parser_skip_to_end_of_template_parameter_list (parser);
20264   /* The `>' token might be a greater-than operator again now.  */
20265   parser->greater_than_is_operator_p
20266     = saved_greater_than_is_operator_p;
20267   /* Restore the SAVED_SCOPE.  */
20268   parser->scope = saved_scope;
20269   parser->qualifying_scope = saved_qualifying_scope;
20270   parser->object_scope = saved_object_scope;
20271   cp_unevaluated_operand = saved_unevaluated_operand;
20272   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20273
20274   return arguments;
20275 }
20276
20277 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20278    arguments, or the body of the function have not yet been parsed,
20279    parse them now.  */
20280
20281 static void
20282 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20283 {
20284   /* If this member is a template, get the underlying
20285      FUNCTION_DECL.  */
20286   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20287     member_function = DECL_TEMPLATE_RESULT (member_function);
20288
20289   /* There should not be any class definitions in progress at this
20290      point; the bodies of members are only parsed outside of all class
20291      definitions.  */
20292   gcc_assert (parser->num_classes_being_defined == 0);
20293   /* While we're parsing the member functions we might encounter more
20294      classes.  We want to handle them right away, but we don't want
20295      them getting mixed up with functions that are currently in the
20296      queue.  */
20297   push_unparsed_function_queues (parser);
20298
20299   /* Make sure that any template parameters are in scope.  */
20300   maybe_begin_member_template_processing (member_function);
20301
20302   /* If the body of the function has not yet been parsed, parse it
20303      now.  */
20304   if (DECL_PENDING_INLINE_P (member_function))
20305     {
20306       tree function_scope;
20307       cp_token_cache *tokens;
20308
20309       /* The function is no longer pending; we are processing it.  */
20310       tokens = DECL_PENDING_INLINE_INFO (member_function);
20311       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20312       DECL_PENDING_INLINE_P (member_function) = 0;
20313
20314       /* If this is a local class, enter the scope of the containing
20315          function.  */
20316       function_scope = current_function_decl;
20317       if (function_scope)
20318         push_function_context ();
20319
20320       /* Push the body of the function onto the lexer stack.  */
20321       cp_parser_push_lexer_for_tokens (parser, tokens);
20322
20323       /* Let the front end know that we going to be defining this
20324          function.  */
20325       start_preparsed_function (member_function, NULL_TREE,
20326                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20327
20328       /* Don't do access checking if it is a templated function.  */
20329       if (processing_template_decl)
20330         push_deferring_access_checks (dk_no_check);
20331
20332       /* Now, parse the body of the function.  */
20333       cp_parser_function_definition_after_declarator (parser,
20334                                                       /*inline_p=*/true);
20335
20336       if (processing_template_decl)
20337         pop_deferring_access_checks ();
20338
20339       /* Leave the scope of the containing function.  */
20340       if (function_scope)
20341         pop_function_context ();
20342       cp_parser_pop_lexer (parser);
20343     }
20344
20345   /* Remove any template parameters from the symbol table.  */
20346   maybe_end_member_template_processing ();
20347
20348   /* Restore the queue.  */
20349   pop_unparsed_function_queues (parser);
20350 }
20351
20352 /* If DECL contains any default args, remember it on the unparsed
20353    functions queue.  */
20354
20355 static void
20356 cp_parser_save_default_args (cp_parser* parser, tree decl)
20357 {
20358   tree probe;
20359
20360   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20361        probe;
20362        probe = TREE_CHAIN (probe))
20363     if (TREE_PURPOSE (probe))
20364       {
20365         cp_default_arg_entry *entry
20366           = VEC_safe_push (cp_default_arg_entry, gc,
20367                            unparsed_funs_with_default_args, NULL);
20368         entry->class_type = current_class_type;
20369         entry->decl = decl;
20370         break;
20371       }
20372 }
20373
20374 /* FN is a FUNCTION_DECL which may contains a parameter with an
20375    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20376    assumes that the current scope is the scope in which the default
20377    argument should be processed.  */
20378
20379 static void
20380 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20381 {
20382   bool saved_local_variables_forbidden_p;
20383   tree parm, parmdecl;
20384
20385   /* While we're parsing the default args, we might (due to the
20386      statement expression extension) encounter more classes.  We want
20387      to handle them right away, but we don't want them getting mixed
20388      up with default args that are currently in the queue.  */
20389   push_unparsed_function_queues (parser);
20390
20391   /* Local variable names (and the `this' keyword) may not appear
20392      in a default argument.  */
20393   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20394   parser->local_variables_forbidden_p = true;
20395
20396   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20397          parmdecl = DECL_ARGUMENTS (fn);
20398        parm && parm != void_list_node;
20399        parm = TREE_CHAIN (parm),
20400          parmdecl = DECL_CHAIN (parmdecl))
20401     {
20402       cp_token_cache *tokens;
20403       tree default_arg = TREE_PURPOSE (parm);
20404       tree parsed_arg;
20405       VEC(tree,gc) *insts;
20406       tree copy;
20407       unsigned ix;
20408
20409       if (!default_arg)
20410         continue;
20411
20412       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20413         /* This can happen for a friend declaration for a function
20414            already declared with default arguments.  */
20415         continue;
20416
20417        /* Push the saved tokens for the default argument onto the parser's
20418           lexer stack.  */
20419       tokens = DEFARG_TOKENS (default_arg);
20420       cp_parser_push_lexer_for_tokens (parser, tokens);
20421
20422       start_lambda_scope (parmdecl);
20423
20424       /* Parse the assignment-expression.  */
20425       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20426       if (parsed_arg == error_mark_node)
20427         {
20428           cp_parser_pop_lexer (parser);
20429           continue;
20430         }
20431
20432       if (!processing_template_decl)
20433         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20434
20435       TREE_PURPOSE (parm) = parsed_arg;
20436
20437       /* Update any instantiations we've already created.  */
20438       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20439            VEC_iterate (tree, insts, ix, copy); ix++)
20440         TREE_PURPOSE (copy) = parsed_arg;
20441
20442       finish_lambda_scope ();
20443
20444       /* If the token stream has not been completely used up, then
20445          there was extra junk after the end of the default
20446          argument.  */
20447       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20448         cp_parser_error (parser, "expected %<,%>");
20449
20450       /* Revert to the main lexer.  */
20451       cp_parser_pop_lexer (parser);
20452     }
20453
20454   /* Make sure no default arg is missing.  */
20455   check_default_args (fn);
20456
20457   /* Restore the state of local_variables_forbidden_p.  */
20458   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20459
20460   /* Restore the queue.  */
20461   pop_unparsed_function_queues (parser);
20462 }
20463
20464 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20465    either a TYPE or an expression, depending on the form of the
20466    input.  The KEYWORD indicates which kind of expression we have
20467    encountered.  */
20468
20469 static tree
20470 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20471 {
20472   tree expr = NULL_TREE;
20473   const char *saved_message;
20474   char *tmp;
20475   bool saved_integral_constant_expression_p;
20476   bool saved_non_integral_constant_expression_p;
20477   bool pack_expansion_p = false;
20478
20479   /* Types cannot be defined in a `sizeof' expression.  Save away the
20480      old message.  */
20481   saved_message = parser->type_definition_forbidden_message;
20482   /* And create the new one.  */
20483   tmp = concat ("types may not be defined in %<",
20484                 IDENTIFIER_POINTER (ridpointers[keyword]),
20485                 "%> expressions", NULL);
20486   parser->type_definition_forbidden_message = tmp;
20487
20488   /* The restrictions on constant-expressions do not apply inside
20489      sizeof expressions.  */
20490   saved_integral_constant_expression_p
20491     = parser->integral_constant_expression_p;
20492   saved_non_integral_constant_expression_p
20493     = parser->non_integral_constant_expression_p;
20494   parser->integral_constant_expression_p = false;
20495
20496   /* If it's a `...', then we are computing the length of a parameter
20497      pack.  */
20498   if (keyword == RID_SIZEOF
20499       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20500     {
20501       /* Consume the `...'.  */
20502       cp_lexer_consume_token (parser->lexer);
20503       maybe_warn_variadic_templates ();
20504
20505       /* Note that this is an expansion.  */
20506       pack_expansion_p = true;
20507     }
20508
20509   /* Do not actually evaluate the expression.  */
20510   ++cp_unevaluated_operand;
20511   ++c_inhibit_evaluation_warnings;
20512   /* If it's a `(', then we might be looking at the type-id
20513      construction.  */
20514   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20515     {
20516       tree type;
20517       bool saved_in_type_id_in_expr_p;
20518
20519       /* We can't be sure yet whether we're looking at a type-id or an
20520          expression.  */
20521       cp_parser_parse_tentatively (parser);
20522       /* Consume the `('.  */
20523       cp_lexer_consume_token (parser->lexer);
20524       /* Parse the type-id.  */
20525       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20526       parser->in_type_id_in_expr_p = true;
20527       type = cp_parser_type_id (parser);
20528       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20529       /* Now, look for the trailing `)'.  */
20530       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20531       /* If all went well, then we're done.  */
20532       if (cp_parser_parse_definitely (parser))
20533         {
20534           cp_decl_specifier_seq decl_specs;
20535
20536           /* Build a trivial decl-specifier-seq.  */
20537           clear_decl_specs (&decl_specs);
20538           decl_specs.type = type;
20539
20540           /* Call grokdeclarator to figure out what type this is.  */
20541           expr = grokdeclarator (NULL,
20542                                  &decl_specs,
20543                                  TYPENAME,
20544                                  /*initialized=*/0,
20545                                  /*attrlist=*/NULL);
20546         }
20547     }
20548
20549   /* If the type-id production did not work out, then we must be
20550      looking at the unary-expression production.  */
20551   if (!expr)
20552     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20553                                        /*cast_p=*/false, NULL);
20554
20555   if (pack_expansion_p)
20556     /* Build a pack expansion. */
20557     expr = make_pack_expansion (expr);
20558
20559   /* Go back to evaluating expressions.  */
20560   --cp_unevaluated_operand;
20561   --c_inhibit_evaluation_warnings;
20562
20563   /* Free the message we created.  */
20564   free (tmp);
20565   /* And restore the old one.  */
20566   parser->type_definition_forbidden_message = saved_message;
20567   parser->integral_constant_expression_p
20568     = saved_integral_constant_expression_p;
20569   parser->non_integral_constant_expression_p
20570     = saved_non_integral_constant_expression_p;
20571
20572   return expr;
20573 }
20574
20575 /* If the current declaration has no declarator, return true.  */
20576
20577 static bool
20578 cp_parser_declares_only_class_p (cp_parser *parser)
20579 {
20580   /* If the next token is a `;' or a `,' then there is no
20581      declarator.  */
20582   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20583           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20584 }
20585
20586 /* Update the DECL_SPECS to reflect the storage class indicated by
20587    KEYWORD.  */
20588
20589 static void
20590 cp_parser_set_storage_class (cp_parser *parser,
20591                              cp_decl_specifier_seq *decl_specs,
20592                              enum rid keyword,
20593                              location_t location)
20594 {
20595   cp_storage_class storage_class;
20596
20597   if (parser->in_unbraced_linkage_specification_p)
20598     {
20599       error_at (location, "invalid use of %qD in linkage specification",
20600                 ridpointers[keyword]);
20601       return;
20602     }
20603   else if (decl_specs->storage_class != sc_none)
20604     {
20605       decl_specs->conflicting_specifiers_p = true;
20606       return;
20607     }
20608
20609   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20610       && decl_specs->specs[(int) ds_thread])
20611     {
20612       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20613       decl_specs->specs[(int) ds_thread] = 0;
20614     }
20615
20616   switch (keyword)
20617     {
20618     case RID_AUTO:
20619       storage_class = sc_auto;
20620       break;
20621     case RID_REGISTER:
20622       storage_class = sc_register;
20623       break;
20624     case RID_STATIC:
20625       storage_class = sc_static;
20626       break;
20627     case RID_EXTERN:
20628       storage_class = sc_extern;
20629       break;
20630     case RID_MUTABLE:
20631       storage_class = sc_mutable;
20632       break;
20633     default:
20634       gcc_unreachable ();
20635     }
20636   decl_specs->storage_class = storage_class;
20637
20638   /* A storage class specifier cannot be applied alongside a typedef 
20639      specifier. If there is a typedef specifier present then set 
20640      conflicting_specifiers_p which will trigger an error later
20641      on in grokdeclarator. */
20642   if (decl_specs->specs[(int)ds_typedef])
20643     decl_specs->conflicting_specifiers_p = true;
20644 }
20645
20646 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20647    is true, the type is a user-defined type; otherwise it is a
20648    built-in type specified by a keyword.  */
20649
20650 static void
20651 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20652                               tree type_spec,
20653                               location_t location,
20654                               bool user_defined_p)
20655 {
20656   decl_specs->any_specifiers_p = true;
20657
20658   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20659      (with, for example, in "typedef int wchar_t;") we remember that
20660      this is what happened.  In system headers, we ignore these
20661      declarations so that G++ can work with system headers that are not
20662      C++-safe.  */
20663   if (decl_specs->specs[(int) ds_typedef]
20664       && !user_defined_p
20665       && (type_spec == boolean_type_node
20666           || type_spec == char16_type_node
20667           || type_spec == char32_type_node
20668           || type_spec == wchar_type_node)
20669       && (decl_specs->type
20670           || decl_specs->specs[(int) ds_long]
20671           || decl_specs->specs[(int) ds_short]
20672           || decl_specs->specs[(int) ds_unsigned]
20673           || decl_specs->specs[(int) ds_signed]))
20674     {
20675       decl_specs->redefined_builtin_type = type_spec;
20676       if (!decl_specs->type)
20677         {
20678           decl_specs->type = type_spec;
20679           decl_specs->user_defined_type_p = false;
20680           decl_specs->type_location = location;
20681         }
20682     }
20683   else if (decl_specs->type)
20684     decl_specs->multiple_types_p = true;
20685   else
20686     {
20687       decl_specs->type = type_spec;
20688       decl_specs->user_defined_type_p = user_defined_p;
20689       decl_specs->redefined_builtin_type = NULL_TREE;
20690       decl_specs->type_location = location;
20691     }
20692 }
20693
20694 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20695    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20696
20697 static bool
20698 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20699 {
20700   return decl_specifiers->specs[(int) ds_friend] != 0;
20701 }
20702
20703 /* Issue an error message indicating that TOKEN_DESC was expected.
20704    If KEYWORD is true, it indicated this function is called by
20705    cp_parser_require_keword and the required token can only be
20706    a indicated keyword. */
20707
20708 static void
20709 cp_parser_required_error (cp_parser *parser,
20710                           required_token token_desc,
20711                           bool keyword)
20712 {
20713   switch (token_desc)
20714     {
20715       case RT_NEW:
20716         cp_parser_error (parser, "expected %<new%>");
20717         return;
20718       case RT_DELETE:
20719         cp_parser_error (parser, "expected %<delete%>");
20720         return;
20721       case RT_RETURN:
20722         cp_parser_error (parser, "expected %<return%>");
20723         return;
20724       case RT_WHILE:
20725         cp_parser_error (parser, "expected %<while%>");
20726         return;
20727       case RT_EXTERN:
20728         cp_parser_error (parser, "expected %<extern%>");
20729         return;
20730       case RT_STATIC_ASSERT:
20731         cp_parser_error (parser, "expected %<static_assert%>");
20732         return;
20733       case RT_DECLTYPE:
20734         cp_parser_error (parser, "expected %<decltype%>");
20735         return;
20736       case RT_OPERATOR:
20737         cp_parser_error (parser, "expected %<operator%>");
20738         return;
20739       case RT_CLASS:
20740         cp_parser_error (parser, "expected %<class%>");
20741         return;
20742       case RT_TEMPLATE:
20743         cp_parser_error (parser, "expected %<template%>");
20744         return;
20745       case RT_NAMESPACE:
20746         cp_parser_error (parser, "expected %<namespace%>");
20747         return;
20748       case RT_USING:
20749         cp_parser_error (parser, "expected %<using%>");
20750         return;
20751       case RT_ASM:
20752         cp_parser_error (parser, "expected %<asm%>");
20753         return;
20754       case RT_TRY:
20755         cp_parser_error (parser, "expected %<try%>");
20756         return;
20757       case RT_CATCH:
20758         cp_parser_error (parser, "expected %<catch%>");
20759         return;
20760       case RT_THROW:
20761         cp_parser_error (parser, "expected %<throw%>");
20762         return;
20763       case RT_LABEL:
20764         cp_parser_error (parser, "expected %<__label__%>");
20765         return;
20766       case RT_AT_TRY:
20767         cp_parser_error (parser, "expected %<@try%>");
20768         return;
20769       case RT_AT_SYNCHRONIZED:
20770         cp_parser_error (parser, "expected %<@synchronized%>");
20771         return;
20772       case RT_AT_THROW:
20773         cp_parser_error (parser, "expected %<@throw%>");
20774         return;
20775       default:
20776         break;
20777     }
20778   if (!keyword)
20779     {
20780       switch (token_desc)
20781         {
20782           case RT_SEMICOLON:
20783             cp_parser_error (parser, "expected %<;%>");
20784             return;
20785           case RT_OPEN_PAREN:
20786             cp_parser_error (parser, "expected %<(%>");
20787             return;
20788           case RT_CLOSE_BRACE:
20789             cp_parser_error (parser, "expected %<}%>");
20790             return;
20791           case RT_OPEN_BRACE:
20792             cp_parser_error (parser, "expected %<{%>");
20793             return;
20794           case RT_CLOSE_SQUARE:
20795             cp_parser_error (parser, "expected %<]%>");
20796             return;
20797           case RT_OPEN_SQUARE:
20798             cp_parser_error (parser, "expected %<[%>");
20799             return;
20800           case RT_COMMA:
20801             cp_parser_error (parser, "expected %<,%>");
20802             return;
20803           case RT_SCOPE:
20804             cp_parser_error (parser, "expected %<::%>");
20805             return;
20806           case RT_LESS:
20807             cp_parser_error (parser, "expected %<<%>");
20808             return;
20809           case RT_GREATER:
20810             cp_parser_error (parser, "expected %<>%>");
20811             return;
20812           case RT_EQ:
20813             cp_parser_error (parser, "expected %<=%>");
20814             return;
20815           case RT_ELLIPSIS:
20816             cp_parser_error (parser, "expected %<...%>");
20817             return;
20818           case RT_MULT:
20819             cp_parser_error (parser, "expected %<*%>");
20820             return;
20821           case RT_COMPL:
20822             cp_parser_error (parser, "expected %<~%>");
20823             return;
20824           case RT_COLON:
20825             cp_parser_error (parser, "expected %<:%>");
20826             return;
20827           case RT_COLON_SCOPE:
20828             cp_parser_error (parser, "expected %<:%> or %<::%>");
20829             return;
20830           case RT_CLOSE_PAREN:
20831             cp_parser_error (parser, "expected %<)%>");
20832             return;
20833           case RT_COMMA_CLOSE_PAREN:
20834             cp_parser_error (parser, "expected %<,%> or %<)%>");
20835             return;
20836           case RT_PRAGMA_EOL:
20837             cp_parser_error (parser, "expected end of line");
20838             return;
20839           case RT_NAME:
20840             cp_parser_error (parser, "expected identifier");
20841             return;
20842           case RT_SELECT:
20843             cp_parser_error (parser, "expected selection-statement");
20844             return;
20845           case RT_INTERATION:
20846             cp_parser_error (parser, "expected iteration-statement");
20847             return;
20848           case RT_JUMP:
20849             cp_parser_error (parser, "expected jump-statement");
20850             return;
20851           case RT_CLASS_KEY:
20852             cp_parser_error (parser, "expected class-key");
20853             return;
20854           case RT_CLASS_TYPENAME_TEMPLATE:
20855             cp_parser_error (parser,
20856                  "expected %<class%>, %<typename%>, or %<template%>");
20857             return;
20858           default:
20859             gcc_unreachable ();
20860         }
20861     }
20862   else
20863     gcc_unreachable ();
20864 }
20865
20866
20867
20868 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20869    issue an error message indicating that TOKEN_DESC was expected.
20870
20871    Returns the token consumed, if the token had the appropriate type.
20872    Otherwise, returns NULL.  */
20873
20874 static cp_token *
20875 cp_parser_require (cp_parser* parser,
20876                    enum cpp_ttype type,
20877                    required_token token_desc)
20878 {
20879   if (cp_lexer_next_token_is (parser->lexer, type))
20880     return cp_lexer_consume_token (parser->lexer);
20881   else
20882     {
20883       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20884       if (!cp_parser_simulate_error (parser))
20885         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20886       return NULL;
20887     }
20888 }
20889
20890 /* An error message is produced if the next token is not '>'.
20891    All further tokens are skipped until the desired token is
20892    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20893
20894 static void
20895 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20896 {
20897   /* Current level of '< ... >'.  */
20898   unsigned level = 0;
20899   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20900   unsigned nesting_depth = 0;
20901
20902   /* Are we ready, yet?  If not, issue error message.  */
20903   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20904     return;
20905
20906   /* Skip tokens until the desired token is found.  */
20907   while (true)
20908     {
20909       /* Peek at the next token.  */
20910       switch (cp_lexer_peek_token (parser->lexer)->type)
20911         {
20912         case CPP_LESS:
20913           if (!nesting_depth)
20914             ++level;
20915           break;
20916
20917         case CPP_RSHIFT:
20918           if (cxx_dialect == cxx98)
20919             /* C++0x views the `>>' operator as two `>' tokens, but
20920                C++98 does not. */
20921             break;
20922           else if (!nesting_depth && level-- == 0)
20923             {
20924               /* We've hit a `>>' where the first `>' closes the
20925                  template argument list, and the second `>' is
20926                  spurious.  Just consume the `>>' and stop; we've
20927                  already produced at least one error.  */
20928               cp_lexer_consume_token (parser->lexer);
20929               return;
20930             }
20931           /* Fall through for C++0x, so we handle the second `>' in
20932              the `>>'.  */
20933
20934         case CPP_GREATER:
20935           if (!nesting_depth && level-- == 0)
20936             {
20937               /* We've reached the token we want, consume it and stop.  */
20938               cp_lexer_consume_token (parser->lexer);
20939               return;
20940             }
20941           break;
20942
20943         case CPP_OPEN_PAREN:
20944         case CPP_OPEN_SQUARE:
20945           ++nesting_depth;
20946           break;
20947
20948         case CPP_CLOSE_PAREN:
20949         case CPP_CLOSE_SQUARE:
20950           if (nesting_depth-- == 0)
20951             return;
20952           break;
20953
20954         case CPP_EOF:
20955         case CPP_PRAGMA_EOL:
20956         case CPP_SEMICOLON:
20957         case CPP_OPEN_BRACE:
20958         case CPP_CLOSE_BRACE:
20959           /* The '>' was probably forgotten, don't look further.  */
20960           return;
20961
20962         default:
20963           break;
20964         }
20965
20966       /* Consume this token.  */
20967       cp_lexer_consume_token (parser->lexer);
20968     }
20969 }
20970
20971 /* If the next token is the indicated keyword, consume it.  Otherwise,
20972    issue an error message indicating that TOKEN_DESC was expected.
20973
20974    Returns the token consumed, if the token had the appropriate type.
20975    Otherwise, returns NULL.  */
20976
20977 static cp_token *
20978 cp_parser_require_keyword (cp_parser* parser,
20979                            enum rid keyword,
20980                            required_token token_desc)
20981 {
20982   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20983
20984   if (token && token->keyword != keyword)
20985     {
20986       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
20987       return NULL;
20988     }
20989
20990   return token;
20991 }
20992
20993 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20994    function-definition.  */
20995
20996 static bool
20997 cp_parser_token_starts_function_definition_p (cp_token* token)
20998 {
20999   return (/* An ordinary function-body begins with an `{'.  */
21000           token->type == CPP_OPEN_BRACE
21001           /* A ctor-initializer begins with a `:'.  */
21002           || token->type == CPP_COLON
21003           /* A function-try-block begins with `try'.  */
21004           || token->keyword == RID_TRY
21005           /* The named return value extension begins with `return'.  */
21006           || token->keyword == RID_RETURN);
21007 }
21008
21009 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21010    definition.  */
21011
21012 static bool
21013 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21014 {
21015   cp_token *token;
21016
21017   token = cp_lexer_peek_token (parser->lexer);
21018   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21019 }
21020
21021 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21022    C++0x) ending a template-argument.  */
21023
21024 static bool
21025 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21026 {
21027   cp_token *token;
21028
21029   token = cp_lexer_peek_token (parser->lexer);
21030   return (token->type == CPP_COMMA 
21031           || token->type == CPP_GREATER
21032           || token->type == CPP_ELLIPSIS
21033           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21034 }
21035
21036 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21037    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21038
21039 static bool
21040 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21041                                                      size_t n)
21042 {
21043   cp_token *token;
21044
21045   token = cp_lexer_peek_nth_token (parser->lexer, n);
21046   if (token->type == CPP_LESS)
21047     return true;
21048   /* Check for the sequence `<::' in the original code. It would be lexed as
21049      `[:', where `[' is a digraph, and there is no whitespace before
21050      `:'.  */
21051   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21052     {
21053       cp_token *token2;
21054       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21055       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21056         return true;
21057     }
21058   return false;
21059 }
21060
21061 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21062    or none_type otherwise.  */
21063
21064 static enum tag_types
21065 cp_parser_token_is_class_key (cp_token* token)
21066 {
21067   switch (token->keyword)
21068     {
21069     case RID_CLASS:
21070       return class_type;
21071     case RID_STRUCT:
21072       return record_type;
21073     case RID_UNION:
21074       return union_type;
21075
21076     default:
21077       return none_type;
21078     }
21079 }
21080
21081 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21082
21083 static void
21084 cp_parser_check_class_key (enum tag_types class_key, tree type)
21085 {
21086   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21087     permerror (input_location, "%qs tag used in naming %q#T",
21088             class_key == union_type ? "union"
21089              : class_key == record_type ? "struct" : "class",
21090              type);
21091 }
21092
21093 /* Issue an error message if DECL is redeclared with different
21094    access than its original declaration [class.access.spec/3].
21095    This applies to nested classes and nested class templates.
21096    [class.mem/1].  */
21097
21098 static void
21099 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21100 {
21101   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21102     return;
21103
21104   if ((TREE_PRIVATE (decl)
21105        != (current_access_specifier == access_private_node))
21106       || (TREE_PROTECTED (decl)
21107           != (current_access_specifier == access_protected_node)))
21108     error_at (location, "%qD redeclared with different access", decl);
21109 }
21110
21111 /* Look for the `template' keyword, as a syntactic disambiguator.
21112    Return TRUE iff it is present, in which case it will be
21113    consumed.  */
21114
21115 static bool
21116 cp_parser_optional_template_keyword (cp_parser *parser)
21117 {
21118   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21119     {
21120       /* The `template' keyword can only be used within templates;
21121          outside templates the parser can always figure out what is a
21122          template and what is not.  */
21123       if (!processing_template_decl)
21124         {
21125           cp_token *token = cp_lexer_peek_token (parser->lexer);
21126           error_at (token->location,
21127                     "%<template%> (as a disambiguator) is only allowed "
21128                     "within templates");
21129           /* If this part of the token stream is rescanned, the same
21130              error message would be generated.  So, we purge the token
21131              from the stream.  */
21132           cp_lexer_purge_token (parser->lexer);
21133           return false;
21134         }
21135       else
21136         {
21137           /* Consume the `template' keyword.  */
21138           cp_lexer_consume_token (parser->lexer);
21139           return true;
21140         }
21141     }
21142
21143   return false;
21144 }
21145
21146 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21147    set PARSER->SCOPE, and perform other related actions.  */
21148
21149 static void
21150 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21151 {
21152   int i;
21153   struct tree_check *check_value;
21154   deferred_access_check *chk;
21155   VEC (deferred_access_check,gc) *checks;
21156
21157   /* Get the stored value.  */
21158   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21159   /* Perform any access checks that were deferred.  */
21160   checks = check_value->checks;
21161   if (checks)
21162     {
21163       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21164         perform_or_defer_access_check (chk->binfo,
21165                                        chk->decl,
21166                                        chk->diag_decl);
21167     }
21168   /* Set the scope from the stored value.  */
21169   parser->scope = check_value->value;
21170   parser->qualifying_scope = check_value->qualifying_scope;
21171   parser->object_scope = NULL_TREE;
21172 }
21173
21174 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21175    encounter the end of a block before what we were looking for.  */
21176
21177 static bool
21178 cp_parser_cache_group (cp_parser *parser,
21179                        enum cpp_ttype end,
21180                        unsigned depth)
21181 {
21182   while (true)
21183     {
21184       cp_token *token = cp_lexer_peek_token (parser->lexer);
21185
21186       /* Abort a parenthesized expression if we encounter a semicolon.  */
21187       if ((end == CPP_CLOSE_PAREN || depth == 0)
21188           && token->type == CPP_SEMICOLON)
21189         return true;
21190       /* If we've reached the end of the file, stop.  */
21191       if (token->type == CPP_EOF
21192           || (end != CPP_PRAGMA_EOL
21193               && token->type == CPP_PRAGMA_EOL))
21194         return true;
21195       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21196         /* We've hit the end of an enclosing block, so there's been some
21197            kind of syntax error.  */
21198         return true;
21199
21200       /* Consume the token.  */
21201       cp_lexer_consume_token (parser->lexer);
21202       /* See if it starts a new group.  */
21203       if (token->type == CPP_OPEN_BRACE)
21204         {
21205           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21206           /* In theory this should probably check end == '}', but
21207              cp_parser_save_member_function_body needs it to exit
21208              after either '}' or ')' when called with ')'.  */
21209           if (depth == 0)
21210             return false;
21211         }
21212       else if (token->type == CPP_OPEN_PAREN)
21213         {
21214           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21215           if (depth == 0 && end == CPP_CLOSE_PAREN)
21216             return false;
21217         }
21218       else if (token->type == CPP_PRAGMA)
21219         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21220       else if (token->type == end)
21221         return false;
21222     }
21223 }
21224
21225 /* Begin parsing tentatively.  We always save tokens while parsing
21226    tentatively so that if the tentative parsing fails we can restore the
21227    tokens.  */
21228
21229 static void
21230 cp_parser_parse_tentatively (cp_parser* parser)
21231 {
21232   /* Enter a new parsing context.  */
21233   parser->context = cp_parser_context_new (parser->context);
21234   /* Begin saving tokens.  */
21235   cp_lexer_save_tokens (parser->lexer);
21236   /* In order to avoid repetitive access control error messages,
21237      access checks are queued up until we are no longer parsing
21238      tentatively.  */
21239   push_deferring_access_checks (dk_deferred);
21240 }
21241
21242 /* Commit to the currently active tentative parse.  */
21243
21244 static void
21245 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21246 {
21247   cp_parser_context *context;
21248   cp_lexer *lexer;
21249
21250   /* Mark all of the levels as committed.  */
21251   lexer = parser->lexer;
21252   for (context = parser->context; context->next; context = context->next)
21253     {
21254       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21255         break;
21256       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21257       while (!cp_lexer_saving_tokens (lexer))
21258         lexer = lexer->next;
21259       cp_lexer_commit_tokens (lexer);
21260     }
21261 }
21262
21263 /* Abort the currently active tentative parse.  All consumed tokens
21264    will be rolled back, and no diagnostics will be issued.  */
21265
21266 static void
21267 cp_parser_abort_tentative_parse (cp_parser* parser)
21268 {
21269   cp_parser_simulate_error (parser);
21270   /* Now, pretend that we want to see if the construct was
21271      successfully parsed.  */
21272   cp_parser_parse_definitely (parser);
21273 }
21274
21275 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21276    token stream.  Otherwise, commit to the tokens we have consumed.
21277    Returns true if no error occurred; false otherwise.  */
21278
21279 static bool
21280 cp_parser_parse_definitely (cp_parser* parser)
21281 {
21282   bool error_occurred;
21283   cp_parser_context *context;
21284
21285   /* Remember whether or not an error occurred, since we are about to
21286      destroy that information.  */
21287   error_occurred = cp_parser_error_occurred (parser);
21288   /* Remove the topmost context from the stack.  */
21289   context = parser->context;
21290   parser->context = context->next;
21291   /* If no parse errors occurred, commit to the tentative parse.  */
21292   if (!error_occurred)
21293     {
21294       /* Commit to the tokens read tentatively, unless that was
21295          already done.  */
21296       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21297         cp_lexer_commit_tokens (parser->lexer);
21298
21299       pop_to_parent_deferring_access_checks ();
21300     }
21301   /* Otherwise, if errors occurred, roll back our state so that things
21302      are just as they were before we began the tentative parse.  */
21303   else
21304     {
21305       cp_lexer_rollback_tokens (parser->lexer);
21306       pop_deferring_access_checks ();
21307     }
21308   /* Add the context to the front of the free list.  */
21309   context->next = cp_parser_context_free_list;
21310   cp_parser_context_free_list = context;
21311
21312   return !error_occurred;
21313 }
21314
21315 /* Returns true if we are parsing tentatively and are not committed to
21316    this tentative parse.  */
21317
21318 static bool
21319 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21320 {
21321   return (cp_parser_parsing_tentatively (parser)
21322           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21323 }
21324
21325 /* Returns nonzero iff an error has occurred during the most recent
21326    tentative parse.  */
21327
21328 static bool
21329 cp_parser_error_occurred (cp_parser* parser)
21330 {
21331   return (cp_parser_parsing_tentatively (parser)
21332           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21333 }
21334
21335 /* Returns nonzero if GNU extensions are allowed.  */
21336
21337 static bool
21338 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21339 {
21340   return parser->allow_gnu_extensions_p;
21341 }
21342 \f
21343 /* Objective-C++ Productions */
21344
21345
21346 /* Parse an Objective-C expression, which feeds into a primary-expression
21347    above.
21348
21349    objc-expression:
21350      objc-message-expression
21351      objc-string-literal
21352      objc-encode-expression
21353      objc-protocol-expression
21354      objc-selector-expression
21355
21356   Returns a tree representation of the expression.  */
21357
21358 static tree
21359 cp_parser_objc_expression (cp_parser* parser)
21360 {
21361   /* Try to figure out what kind of declaration is present.  */
21362   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21363
21364   switch (kwd->type)
21365     {
21366     case CPP_OPEN_SQUARE:
21367       return cp_parser_objc_message_expression (parser);
21368
21369     case CPP_OBJC_STRING:
21370       kwd = cp_lexer_consume_token (parser->lexer);
21371       return objc_build_string_object (kwd->u.value);
21372
21373     case CPP_KEYWORD:
21374       switch (kwd->keyword)
21375         {
21376         case RID_AT_ENCODE:
21377           return cp_parser_objc_encode_expression (parser);
21378
21379         case RID_AT_PROTOCOL:
21380           return cp_parser_objc_protocol_expression (parser);
21381
21382         case RID_AT_SELECTOR:
21383           return cp_parser_objc_selector_expression (parser);
21384
21385         default:
21386           break;
21387         }
21388     default:
21389       error_at (kwd->location,
21390                 "misplaced %<@%D%> Objective-C++ construct",
21391                 kwd->u.value);
21392       cp_parser_skip_to_end_of_block_or_statement (parser);
21393     }
21394
21395   return error_mark_node;
21396 }
21397
21398 /* Parse an Objective-C message expression.
21399
21400    objc-message-expression:
21401      [ objc-message-receiver objc-message-args ]
21402
21403    Returns a representation of an Objective-C message.  */
21404
21405 static tree
21406 cp_parser_objc_message_expression (cp_parser* parser)
21407 {
21408   tree receiver, messageargs;
21409
21410   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21411   receiver = cp_parser_objc_message_receiver (parser);
21412   messageargs = cp_parser_objc_message_args (parser);
21413   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21414
21415   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21416 }
21417
21418 /* Parse an objc-message-receiver.
21419
21420    objc-message-receiver:
21421      expression
21422      simple-type-specifier
21423
21424   Returns a representation of the type or expression.  */
21425
21426 static tree
21427 cp_parser_objc_message_receiver (cp_parser* parser)
21428 {
21429   tree rcv;
21430
21431   /* An Objective-C message receiver may be either (1) a type
21432      or (2) an expression.  */
21433   cp_parser_parse_tentatively (parser);
21434   rcv = cp_parser_expression (parser, false, NULL);
21435
21436   if (cp_parser_parse_definitely (parser))
21437     return rcv;
21438
21439   rcv = cp_parser_simple_type_specifier (parser,
21440                                          /*decl_specs=*/NULL,
21441                                          CP_PARSER_FLAGS_NONE);
21442
21443   return objc_get_class_reference (rcv);
21444 }
21445
21446 /* Parse the arguments and selectors comprising an Objective-C message.
21447
21448    objc-message-args:
21449      objc-selector
21450      objc-selector-args
21451      objc-selector-args , objc-comma-args
21452
21453    objc-selector-args:
21454      objc-selector [opt] : assignment-expression
21455      objc-selector-args objc-selector [opt] : assignment-expression
21456
21457    objc-comma-args:
21458      assignment-expression
21459      objc-comma-args , assignment-expression
21460
21461    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21462    selector arguments and TREE_VALUE containing a list of comma
21463    arguments.  */
21464
21465 static tree
21466 cp_parser_objc_message_args (cp_parser* parser)
21467 {
21468   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21469   bool maybe_unary_selector_p = true;
21470   cp_token *token = cp_lexer_peek_token (parser->lexer);
21471
21472   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21473     {
21474       tree selector = NULL_TREE, arg;
21475
21476       if (token->type != CPP_COLON)
21477         selector = cp_parser_objc_selector (parser);
21478
21479       /* Detect if we have a unary selector.  */
21480       if (maybe_unary_selector_p
21481           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21482         return build_tree_list (selector, NULL_TREE);
21483
21484       maybe_unary_selector_p = false;
21485       cp_parser_require (parser, CPP_COLON, RT_COLON);
21486       arg = cp_parser_assignment_expression (parser, false, NULL);
21487
21488       sel_args
21489         = chainon (sel_args,
21490                    build_tree_list (selector, arg));
21491
21492       token = cp_lexer_peek_token (parser->lexer);
21493     }
21494
21495   /* Handle non-selector arguments, if any. */
21496   while (token->type == CPP_COMMA)
21497     {
21498       tree arg;
21499
21500       cp_lexer_consume_token (parser->lexer);
21501       arg = cp_parser_assignment_expression (parser, false, NULL);
21502
21503       addl_args
21504         = chainon (addl_args,
21505                    build_tree_list (NULL_TREE, arg));
21506
21507       token = cp_lexer_peek_token (parser->lexer);
21508     }
21509
21510   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21511     {
21512       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21513       return build_tree_list (error_mark_node, error_mark_node);
21514     }
21515
21516   return build_tree_list (sel_args, addl_args);
21517 }
21518
21519 /* Parse an Objective-C encode expression.
21520
21521    objc-encode-expression:
21522      @encode objc-typename
21523
21524    Returns an encoded representation of the type argument.  */
21525
21526 static tree
21527 cp_parser_objc_encode_expression (cp_parser* parser)
21528 {
21529   tree type;
21530   cp_token *token;
21531
21532   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21533   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21534   token = cp_lexer_peek_token (parser->lexer);
21535   type = complete_type (cp_parser_type_id (parser));
21536   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21537
21538   if (!type)
21539     {
21540       error_at (token->location, 
21541                 "%<@encode%> must specify a type as an argument");
21542       return error_mark_node;
21543     }
21544
21545   /* This happens if we find @encode(T) (where T is a template
21546      typename or something dependent on a template typename) when
21547      parsing a template.  In that case, we can't compile it
21548      immediately, but we rather create an AT_ENCODE_EXPR which will
21549      need to be instantiated when the template is used.
21550   */
21551   if (dependent_type_p (type))
21552     {
21553       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21554       TREE_READONLY (value) = 1;
21555       return value;
21556     }
21557
21558   return objc_build_encode_expr (type);
21559 }
21560
21561 /* Parse an Objective-C @defs expression.  */
21562
21563 static tree
21564 cp_parser_objc_defs_expression (cp_parser *parser)
21565 {
21566   tree name;
21567
21568   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21569   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21570   name = cp_parser_identifier (parser);
21571   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21572
21573   return objc_get_class_ivars (name);
21574 }
21575
21576 /* Parse an Objective-C protocol expression.
21577
21578   objc-protocol-expression:
21579     @protocol ( identifier )
21580
21581   Returns a representation of the protocol expression.  */
21582
21583 static tree
21584 cp_parser_objc_protocol_expression (cp_parser* parser)
21585 {
21586   tree proto;
21587
21588   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21589   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21590   proto = cp_parser_identifier (parser);
21591   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21592
21593   return objc_build_protocol_expr (proto);
21594 }
21595
21596 /* Parse an Objective-C selector expression.
21597
21598    objc-selector-expression:
21599      @selector ( objc-method-signature )
21600
21601    objc-method-signature:
21602      objc-selector
21603      objc-selector-seq
21604
21605    objc-selector-seq:
21606      objc-selector :
21607      objc-selector-seq objc-selector :
21608
21609   Returns a representation of the method selector.  */
21610
21611 static tree
21612 cp_parser_objc_selector_expression (cp_parser* parser)
21613 {
21614   tree sel_seq = NULL_TREE;
21615   bool maybe_unary_selector_p = true;
21616   cp_token *token;
21617   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21618
21619   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21620   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21621   token = cp_lexer_peek_token (parser->lexer);
21622
21623   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21624          || token->type == CPP_SCOPE)
21625     {
21626       tree selector = NULL_TREE;
21627
21628       if (token->type != CPP_COLON
21629           || token->type == CPP_SCOPE)
21630         selector = cp_parser_objc_selector (parser);
21631
21632       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21633           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21634         {
21635           /* Detect if we have a unary selector.  */
21636           if (maybe_unary_selector_p)
21637             {
21638               sel_seq = selector;
21639               goto finish_selector;
21640             }
21641           else
21642             {
21643               cp_parser_error (parser, "expected %<:%>");
21644             }
21645         }
21646       maybe_unary_selector_p = false;
21647       token = cp_lexer_consume_token (parser->lexer);
21648
21649       if (token->type == CPP_SCOPE)
21650         {
21651           sel_seq
21652             = chainon (sel_seq,
21653                        build_tree_list (selector, NULL_TREE));
21654           sel_seq
21655             = chainon (sel_seq,
21656                        build_tree_list (NULL_TREE, NULL_TREE));
21657         }
21658       else
21659         sel_seq
21660           = chainon (sel_seq,
21661                      build_tree_list (selector, NULL_TREE));
21662
21663       token = cp_lexer_peek_token (parser->lexer);
21664     }
21665
21666  finish_selector:
21667   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21668
21669   return objc_build_selector_expr (loc, sel_seq);
21670 }
21671
21672 /* Parse a list of identifiers.
21673
21674    objc-identifier-list:
21675      identifier
21676      objc-identifier-list , identifier
21677
21678    Returns a TREE_LIST of identifier nodes.  */
21679
21680 static tree
21681 cp_parser_objc_identifier_list (cp_parser* parser)
21682 {
21683   tree identifier;
21684   tree list;
21685   cp_token *sep;
21686
21687   identifier = cp_parser_identifier (parser);
21688   if (identifier == error_mark_node)
21689     return error_mark_node;      
21690
21691   list = build_tree_list (NULL_TREE, identifier);
21692   sep = cp_lexer_peek_token (parser->lexer);
21693
21694   while (sep->type == CPP_COMMA)
21695     {
21696       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21697       identifier = cp_parser_identifier (parser);
21698       if (identifier == error_mark_node)
21699         return list;
21700
21701       list = chainon (list, build_tree_list (NULL_TREE,
21702                                              identifier));
21703       sep = cp_lexer_peek_token (parser->lexer);
21704     }
21705   
21706   return list;
21707 }
21708
21709 /* Parse an Objective-C alias declaration.
21710
21711    objc-alias-declaration:
21712      @compatibility_alias identifier identifier ;
21713
21714    This function registers the alias mapping with the Objective-C front end.
21715    It returns nothing.  */
21716
21717 static void
21718 cp_parser_objc_alias_declaration (cp_parser* parser)
21719 {
21720   tree alias, orig;
21721
21722   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21723   alias = cp_parser_identifier (parser);
21724   orig = cp_parser_identifier (parser);
21725   objc_declare_alias (alias, orig);
21726   cp_parser_consume_semicolon_at_end_of_statement (parser);
21727 }
21728
21729 /* Parse an Objective-C class forward-declaration.
21730
21731    objc-class-declaration:
21732      @class objc-identifier-list ;
21733
21734    The function registers the forward declarations with the Objective-C
21735    front end.  It returns nothing.  */
21736
21737 static void
21738 cp_parser_objc_class_declaration (cp_parser* parser)
21739 {
21740   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21741   objc_declare_class (cp_parser_objc_identifier_list (parser));
21742   cp_parser_consume_semicolon_at_end_of_statement (parser);
21743 }
21744
21745 /* Parse a list of Objective-C protocol references.
21746
21747    objc-protocol-refs-opt:
21748      objc-protocol-refs [opt]
21749
21750    objc-protocol-refs:
21751      < objc-identifier-list >
21752
21753    Returns a TREE_LIST of identifiers, if any.  */
21754
21755 static tree
21756 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21757 {
21758   tree protorefs = NULL_TREE;
21759
21760   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21761     {
21762       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21763       protorefs = cp_parser_objc_identifier_list (parser);
21764       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21765     }
21766
21767   return protorefs;
21768 }
21769
21770 /* Parse a Objective-C visibility specification.  */
21771
21772 static void
21773 cp_parser_objc_visibility_spec (cp_parser* parser)
21774 {
21775   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21776
21777   switch (vis->keyword)
21778     {
21779     case RID_AT_PRIVATE:
21780       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21781       break;
21782     case RID_AT_PROTECTED:
21783       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21784       break;
21785     case RID_AT_PUBLIC:
21786       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21787       break;
21788     case RID_AT_PACKAGE:
21789       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21790       break;
21791     default:
21792       return;
21793     }
21794
21795   /* Eat '@private'/'@protected'/'@public'.  */
21796   cp_lexer_consume_token (parser->lexer);
21797 }
21798
21799 /* Parse an Objective-C method type.  Return 'true' if it is a class
21800    (+) method, and 'false' if it is an instance (-) method.  */
21801
21802 static inline bool
21803 cp_parser_objc_method_type (cp_parser* parser)
21804 {
21805   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21806     return true;
21807   else
21808     return false;
21809 }
21810
21811 /* Parse an Objective-C protocol qualifier.  */
21812
21813 static tree
21814 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21815 {
21816   tree quals = NULL_TREE, node;
21817   cp_token *token = cp_lexer_peek_token (parser->lexer);
21818
21819   node = token->u.value;
21820
21821   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21822          && (node == ridpointers [(int) RID_IN]
21823              || node == ridpointers [(int) RID_OUT]
21824              || node == ridpointers [(int) RID_INOUT]
21825              || node == ridpointers [(int) RID_BYCOPY]
21826              || node == ridpointers [(int) RID_BYREF]
21827              || node == ridpointers [(int) RID_ONEWAY]))
21828     {
21829       quals = tree_cons (NULL_TREE, node, quals);
21830       cp_lexer_consume_token (parser->lexer);
21831       token = cp_lexer_peek_token (parser->lexer);
21832       node = token->u.value;
21833     }
21834
21835   return quals;
21836 }
21837
21838 /* Parse an Objective-C typename.  */
21839
21840 static tree
21841 cp_parser_objc_typename (cp_parser* parser)
21842 {
21843   tree type_name = NULL_TREE;
21844
21845   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21846     {
21847       tree proto_quals, cp_type = NULL_TREE;
21848
21849       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21850       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21851
21852       /* An ObjC type name may consist of just protocol qualifiers, in which
21853          case the type shall default to 'id'.  */
21854       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21855         cp_type = cp_parser_type_id (parser);
21856
21857       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21858       type_name = build_tree_list (proto_quals, cp_type);
21859     }
21860
21861   return type_name;
21862 }
21863
21864 /* Check to see if TYPE refers to an Objective-C selector name.  */
21865
21866 static bool
21867 cp_parser_objc_selector_p (enum cpp_ttype type)
21868 {
21869   return (type == CPP_NAME || type == CPP_KEYWORD
21870           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21871           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21872           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21873           || type == CPP_XOR || type == CPP_XOR_EQ);
21874 }
21875
21876 /* Parse an Objective-C selector.  */
21877
21878 static tree
21879 cp_parser_objc_selector (cp_parser* parser)
21880 {
21881   cp_token *token = cp_lexer_consume_token (parser->lexer);
21882
21883   if (!cp_parser_objc_selector_p (token->type))
21884     {
21885       error_at (token->location, "invalid Objective-C++ selector name");
21886       return error_mark_node;
21887     }
21888
21889   /* C++ operator names are allowed to appear in ObjC selectors.  */
21890   switch (token->type)
21891     {
21892     case CPP_AND_AND: return get_identifier ("and");
21893     case CPP_AND_EQ: return get_identifier ("and_eq");
21894     case CPP_AND: return get_identifier ("bitand");
21895     case CPP_OR: return get_identifier ("bitor");
21896     case CPP_COMPL: return get_identifier ("compl");
21897     case CPP_NOT: return get_identifier ("not");
21898     case CPP_NOT_EQ: return get_identifier ("not_eq");
21899     case CPP_OR_OR: return get_identifier ("or");
21900     case CPP_OR_EQ: return get_identifier ("or_eq");
21901     case CPP_XOR: return get_identifier ("xor");
21902     case CPP_XOR_EQ: return get_identifier ("xor_eq");
21903     default: return token->u.value;
21904     }
21905 }
21906
21907 /* Parse an Objective-C params list.  */
21908
21909 static tree
21910 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21911 {
21912   tree params = NULL_TREE;
21913   bool maybe_unary_selector_p = true;
21914   cp_token *token = cp_lexer_peek_token (parser->lexer);
21915
21916   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21917     {
21918       tree selector = NULL_TREE, type_name, identifier;
21919       tree parm_attr = NULL_TREE;
21920
21921       if (token->keyword == RID_ATTRIBUTE)
21922         break;
21923
21924       if (token->type != CPP_COLON)
21925         selector = cp_parser_objc_selector (parser);
21926
21927       /* Detect if we have a unary selector.  */
21928       if (maybe_unary_selector_p
21929           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21930         {
21931           params = selector; /* Might be followed by attributes.  */
21932           break;
21933         }
21934
21935       maybe_unary_selector_p = false;
21936       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21937         {
21938           /* Something went quite wrong.  There should be a colon
21939              here, but there is not.  Stop parsing parameters.  */
21940           break;
21941         }
21942       type_name = cp_parser_objc_typename (parser);
21943       /* New ObjC allows attributes on parameters too.  */
21944       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21945         parm_attr = cp_parser_attributes_opt (parser);
21946       identifier = cp_parser_identifier (parser);
21947
21948       params
21949         = chainon (params,
21950                    objc_build_keyword_decl (selector,
21951                                             type_name,
21952                                             identifier,
21953                                             parm_attr));
21954
21955       token = cp_lexer_peek_token (parser->lexer);
21956     }
21957
21958   if (params == NULL_TREE)
21959     {
21960       cp_parser_error (parser, "objective-c++ method declaration is expected");
21961       return error_mark_node;
21962     }
21963
21964   /* We allow tail attributes for the method.  */
21965   if (token->keyword == RID_ATTRIBUTE)
21966     {
21967       *attributes = cp_parser_attributes_opt (parser);
21968       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21969           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21970         return params;
21971       cp_parser_error (parser, 
21972                        "method attributes must be specified at the end");
21973       return error_mark_node;
21974     }
21975
21976   if (params == NULL_TREE)
21977     {
21978       cp_parser_error (parser, "objective-c++ method declaration is expected");
21979       return error_mark_node;
21980     }
21981   return params;
21982 }
21983
21984 /* Parse the non-keyword Objective-C params.  */
21985
21986 static tree
21987 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
21988                                        tree* attributes)
21989 {
21990   tree params = make_node (TREE_LIST);
21991   cp_token *token = cp_lexer_peek_token (parser->lexer);
21992   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
21993
21994   while (token->type == CPP_COMMA)
21995     {
21996       cp_parameter_declarator *parmdecl;
21997       tree parm;
21998
21999       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22000       token = cp_lexer_peek_token (parser->lexer);
22001
22002       if (token->type == CPP_ELLIPSIS)
22003         {
22004           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22005           *ellipsisp = true;
22006           token = cp_lexer_peek_token (parser->lexer);
22007           break;
22008         }
22009
22010       /* TODO: parse attributes for tail parameters.  */
22011       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22012       parm = grokdeclarator (parmdecl->declarator,
22013                              &parmdecl->decl_specifiers,
22014                              PARM, /*initialized=*/0,
22015                              /*attrlist=*/NULL);
22016
22017       chainon (params, build_tree_list (NULL_TREE, parm));
22018       token = cp_lexer_peek_token (parser->lexer);
22019     }
22020
22021   /* We allow tail attributes for the method.  */
22022   if (token->keyword == RID_ATTRIBUTE)
22023     {
22024       if (*attributes == NULL_TREE)
22025         {
22026           *attributes = cp_parser_attributes_opt (parser);
22027           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22028               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22029             return params;
22030         }
22031       else        
22032         /* We have an error, but parse the attributes, so that we can 
22033            carry on.  */
22034         *attributes = cp_parser_attributes_opt (parser);
22035
22036       cp_parser_error (parser, 
22037                        "method attributes must be specified at the end");
22038       return error_mark_node;
22039     }
22040
22041   return params;
22042 }
22043
22044 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22045
22046 static void
22047 cp_parser_objc_interstitial_code (cp_parser* parser)
22048 {
22049   cp_token *token = cp_lexer_peek_token (parser->lexer);
22050
22051   /* If the next token is `extern' and the following token is a string
22052      literal, then we have a linkage specification.  */
22053   if (token->keyword == RID_EXTERN
22054       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22055     cp_parser_linkage_specification (parser);
22056   /* Handle #pragma, if any.  */
22057   else if (token->type == CPP_PRAGMA)
22058     cp_parser_pragma (parser, pragma_external);
22059   /* Allow stray semicolons.  */
22060   else if (token->type == CPP_SEMICOLON)
22061     cp_lexer_consume_token (parser->lexer);
22062   /* Mark methods as optional or required, when building protocols.  */
22063   else if (token->keyword == RID_AT_OPTIONAL)
22064     {
22065       cp_lexer_consume_token (parser->lexer);
22066       objc_set_method_opt (true);
22067     }
22068   else if (token->keyword == RID_AT_REQUIRED)
22069     {
22070       cp_lexer_consume_token (parser->lexer);
22071       objc_set_method_opt (false);
22072     }
22073   else if (token->keyword == RID_NAMESPACE)
22074     cp_parser_namespace_definition (parser);
22075   /* Other stray characters must generate errors.  */
22076   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22077     {
22078       cp_lexer_consume_token (parser->lexer);
22079       error ("stray %qs between Objective-C++ methods",
22080              token->type == CPP_OPEN_BRACE ? "{" : "}");
22081     }
22082   /* Finally, try to parse a block-declaration, or a function-definition.  */
22083   else
22084     cp_parser_block_declaration (parser, /*statement_p=*/false);
22085 }
22086
22087 /* Parse a method signature.  */
22088
22089 static tree
22090 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22091 {
22092   tree rettype, kwdparms, optparms;
22093   bool ellipsis = false;
22094   bool is_class_method;
22095
22096   is_class_method = cp_parser_objc_method_type (parser);
22097   rettype = cp_parser_objc_typename (parser);
22098   *attributes = NULL_TREE;
22099   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22100   if (kwdparms == error_mark_node)
22101     return error_mark_node;
22102   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22103   if (optparms == error_mark_node)
22104     return error_mark_node;
22105
22106   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22107 }
22108
22109 static bool
22110 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22111 {
22112   tree tattr;  
22113   cp_lexer_save_tokens (parser->lexer);
22114   tattr = cp_parser_attributes_opt (parser);
22115   gcc_assert (tattr) ;
22116   
22117   /* If the attributes are followed by a method introducer, this is not allowed.
22118      Dump the attributes and flag the situation.  */
22119   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22120       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22121     return true;
22122
22123   /* Otherwise, the attributes introduce some interstitial code, possibly so
22124      rewind to allow that check.  */
22125   cp_lexer_rollback_tokens (parser->lexer);
22126   return false;  
22127 }
22128
22129 /* Parse an Objective-C method prototype list.  */
22130
22131 static void
22132 cp_parser_objc_method_prototype_list (cp_parser* parser)
22133 {
22134   cp_token *token = cp_lexer_peek_token (parser->lexer);
22135
22136   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22137     {
22138       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22139         {
22140           tree attributes, sig;
22141           bool is_class_method;
22142           if (token->type == CPP_PLUS)
22143             is_class_method = true;
22144           else
22145             is_class_method = false;
22146           sig = cp_parser_objc_method_signature (parser, &attributes);
22147           if (sig == error_mark_node)
22148             {
22149               cp_parser_skip_to_end_of_block_or_statement (parser);
22150               token = cp_lexer_peek_token (parser->lexer);
22151               continue;
22152             }
22153           objc_add_method_declaration (is_class_method, sig, attributes);
22154           cp_parser_consume_semicolon_at_end_of_statement (parser);
22155         }
22156       else if (token->keyword == RID_AT_PROPERTY)
22157         cp_parser_objc_at_property_declaration (parser);
22158       else if (token->keyword == RID_ATTRIBUTE 
22159                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22160         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22161                     OPT_Wattributes, 
22162                     "prefix attributes are ignored for methods");
22163       else
22164         /* Allow for interspersed non-ObjC++ code.  */
22165         cp_parser_objc_interstitial_code (parser);
22166
22167       token = cp_lexer_peek_token (parser->lexer);
22168     }
22169
22170   if (token->type != CPP_EOF)
22171     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22172   else
22173     cp_parser_error (parser, "expected %<@end%>");
22174
22175   objc_finish_interface ();
22176 }
22177
22178 /* Parse an Objective-C method definition list.  */
22179
22180 static void
22181 cp_parser_objc_method_definition_list (cp_parser* parser)
22182 {
22183   cp_token *token = cp_lexer_peek_token (parser->lexer);
22184
22185   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22186     {
22187       tree meth;
22188
22189       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22190         {
22191           cp_token *ptk;
22192           tree sig, attribute;
22193           bool is_class_method;
22194           if (token->type == CPP_PLUS)
22195             is_class_method = true;
22196           else
22197             is_class_method = false;
22198           push_deferring_access_checks (dk_deferred);
22199           sig = cp_parser_objc_method_signature (parser, &attribute);
22200           if (sig == error_mark_node)
22201             {
22202               cp_parser_skip_to_end_of_block_or_statement (parser);
22203               token = cp_lexer_peek_token (parser->lexer);
22204               continue;
22205             }
22206           objc_start_method_definition (is_class_method, sig, attribute);
22207
22208           /* For historical reasons, we accept an optional semicolon.  */
22209           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22210             cp_lexer_consume_token (parser->lexer);
22211
22212           ptk = cp_lexer_peek_token (parser->lexer);
22213           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22214                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22215             {
22216               perform_deferred_access_checks ();
22217               stop_deferring_access_checks ();
22218               meth = cp_parser_function_definition_after_declarator (parser,
22219                                                                      false);
22220               pop_deferring_access_checks ();
22221               objc_finish_method_definition (meth);
22222             }
22223         }
22224       /* The following case will be removed once @synthesize is
22225          completely implemented.  */
22226       else if (token->keyword == RID_AT_PROPERTY)
22227         cp_parser_objc_at_property_declaration (parser);
22228       else if (token->keyword == RID_AT_SYNTHESIZE)
22229         cp_parser_objc_at_synthesize_declaration (parser);
22230       else if (token->keyword == RID_AT_DYNAMIC)
22231         cp_parser_objc_at_dynamic_declaration (parser);
22232       else if (token->keyword == RID_ATTRIBUTE 
22233                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22234         warning_at (token->location, OPT_Wattributes,
22235                     "prefix attributes are ignored for methods");
22236       else
22237         /* Allow for interspersed non-ObjC++ code.  */
22238         cp_parser_objc_interstitial_code (parser);
22239
22240       token = cp_lexer_peek_token (parser->lexer);
22241     }
22242
22243   if (token->type != CPP_EOF)
22244     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22245   else
22246     cp_parser_error (parser, "expected %<@end%>");
22247
22248   objc_finish_implementation ();
22249 }
22250
22251 /* Parse Objective-C ivars.  */
22252
22253 static void
22254 cp_parser_objc_class_ivars (cp_parser* parser)
22255 {
22256   cp_token *token = cp_lexer_peek_token (parser->lexer);
22257
22258   if (token->type != CPP_OPEN_BRACE)
22259     return;     /* No ivars specified.  */
22260
22261   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22262   token = cp_lexer_peek_token (parser->lexer);
22263
22264   while (token->type != CPP_CLOSE_BRACE 
22265         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22266     {
22267       cp_decl_specifier_seq declspecs;
22268       int decl_class_or_enum_p;
22269       tree prefix_attributes;
22270
22271       cp_parser_objc_visibility_spec (parser);
22272
22273       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22274         break;
22275
22276       cp_parser_decl_specifier_seq (parser,
22277                                     CP_PARSER_FLAGS_OPTIONAL,
22278                                     &declspecs,
22279                                     &decl_class_or_enum_p);
22280
22281       /* auto, register, static, extern, mutable.  */
22282       if (declspecs.storage_class != sc_none)
22283         {
22284           cp_parser_error (parser, "invalid type for instance variable");         
22285           declspecs.storage_class = sc_none;
22286         }
22287
22288       /* __thread.  */
22289       if (declspecs.specs[(int) ds_thread])
22290         {
22291           cp_parser_error (parser, "invalid type for instance variable");
22292           declspecs.specs[(int) ds_thread] = 0;
22293         }
22294       
22295       /* typedef.  */
22296       if (declspecs.specs[(int) ds_typedef])
22297         {
22298           cp_parser_error (parser, "invalid type for instance variable");
22299           declspecs.specs[(int) ds_typedef] = 0;
22300         }
22301
22302       prefix_attributes = declspecs.attributes;
22303       declspecs.attributes = NULL_TREE;
22304
22305       /* Keep going until we hit the `;' at the end of the
22306          declaration.  */
22307       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22308         {
22309           tree width = NULL_TREE, attributes, first_attribute, decl;
22310           cp_declarator *declarator = NULL;
22311           int ctor_dtor_or_conv_p;
22312
22313           /* Check for a (possibly unnamed) bitfield declaration.  */
22314           token = cp_lexer_peek_token (parser->lexer);
22315           if (token->type == CPP_COLON)
22316             goto eat_colon;
22317
22318           if (token->type == CPP_NAME
22319               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22320                   == CPP_COLON))
22321             {
22322               /* Get the name of the bitfield.  */
22323               declarator = make_id_declarator (NULL_TREE,
22324                                                cp_parser_identifier (parser),
22325                                                sfk_none);
22326
22327              eat_colon:
22328               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22329               /* Get the width of the bitfield.  */
22330               width
22331                 = cp_parser_constant_expression (parser,
22332                                                  /*allow_non_constant=*/false,
22333                                                  NULL);
22334             }
22335           else
22336             {
22337               /* Parse the declarator.  */
22338               declarator
22339                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22340                                         &ctor_dtor_or_conv_p,
22341                                         /*parenthesized_p=*/NULL,
22342                                         /*member_p=*/false);
22343             }
22344
22345           /* Look for attributes that apply to the ivar.  */
22346           attributes = cp_parser_attributes_opt (parser);
22347           /* Remember which attributes are prefix attributes and
22348              which are not.  */
22349           first_attribute = attributes;
22350           /* Combine the attributes.  */
22351           attributes = chainon (prefix_attributes, attributes);
22352
22353           if (width)
22354               /* Create the bitfield declaration.  */
22355               decl = grokbitfield (declarator, &declspecs,
22356                                    width,
22357                                    attributes);
22358           else
22359             decl = grokfield (declarator, &declspecs,
22360                               NULL_TREE, /*init_const_expr_p=*/false,
22361                               NULL_TREE, attributes);
22362
22363           /* Add the instance variable.  */
22364           objc_add_instance_variable (decl);
22365
22366           /* Reset PREFIX_ATTRIBUTES.  */
22367           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22368             attributes = TREE_CHAIN (attributes);
22369           if (attributes)
22370             TREE_CHAIN (attributes) = NULL_TREE;
22371
22372           token = cp_lexer_peek_token (parser->lexer);
22373
22374           if (token->type == CPP_COMMA)
22375             {
22376               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22377               continue;
22378             }
22379           break;
22380         }
22381
22382       cp_parser_consume_semicolon_at_end_of_statement (parser);
22383       token = cp_lexer_peek_token (parser->lexer);
22384     }
22385
22386   if (token->keyword == RID_AT_END)
22387     cp_parser_error (parser, "expected %<}%>");
22388
22389   /* Do not consume the RID_AT_END, so it will be read again as terminating
22390      the @interface of @implementation.  */ 
22391   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22392     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22393     
22394   /* For historical reasons, we accept an optional semicolon.  */
22395   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22396     cp_lexer_consume_token (parser->lexer);
22397 }
22398
22399 /* Parse an Objective-C protocol declaration.  */
22400
22401 static void
22402 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22403 {
22404   tree proto, protorefs;
22405   cp_token *tok;
22406
22407   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22408   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22409     {
22410       tok = cp_lexer_peek_token (parser->lexer);
22411       error_at (tok->location, "identifier expected after %<@protocol%>");
22412       goto finish;
22413     }
22414
22415   /* See if we have a forward declaration or a definition.  */
22416   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22417
22418   /* Try a forward declaration first.  */
22419   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22420     {
22421       objc_declare_protocols (cp_parser_objc_identifier_list (parser), 
22422                               attributes);
22423      finish:
22424       cp_parser_consume_semicolon_at_end_of_statement (parser);
22425     }
22426
22427   /* Ok, we got a full-fledged definition (or at least should).  */
22428   else
22429     {
22430       proto = cp_parser_identifier (parser);
22431       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22432       objc_start_protocol (proto, protorefs, attributes);
22433       cp_parser_objc_method_prototype_list (parser);
22434     }
22435 }
22436
22437 /* Parse an Objective-C superclass or category.  */
22438
22439 static void
22440 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22441                                        bool iface_p,
22442                                        tree *super,
22443                                        tree *categ, bool *is_class_extension)
22444 {
22445   cp_token *next = cp_lexer_peek_token (parser->lexer);
22446
22447   *super = *categ = NULL_TREE;
22448   *is_class_extension = false;
22449   if (next->type == CPP_COLON)
22450     {
22451       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22452       *super = cp_parser_identifier (parser);
22453     }
22454   else if (next->type == CPP_OPEN_PAREN)
22455     {
22456       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22457
22458       /* If there is no category name, and this is an @interface, we
22459          have a class extension.  */
22460       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22461         {
22462           *categ = NULL_TREE;
22463           *is_class_extension = true;
22464         }
22465       else
22466         *categ = cp_parser_identifier (parser);
22467
22468       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22469     }
22470 }
22471
22472 /* Parse an Objective-C class interface.  */
22473
22474 static void
22475 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22476 {
22477   tree name, super, categ, protos;
22478   bool is_class_extension;
22479
22480   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22481   name = cp_parser_identifier (parser);
22482   if (name == error_mark_node)
22483     {
22484       /* It's hard to recover because even if valid @interface stuff
22485          is to follow, we can't compile it (or validate it) if we
22486          don't even know which class it refers to.  Let's assume this
22487          was a stray '@interface' token in the stream and skip it.
22488       */
22489       return;
22490     }
22491   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22492                                          &is_class_extension);
22493   protos = cp_parser_objc_protocol_refs_opt (parser);
22494
22495   /* We have either a class or a category on our hands.  */
22496   if (categ || is_class_extension)
22497     objc_start_category_interface (name, categ, protos, attributes);
22498   else
22499     {
22500       objc_start_class_interface (name, super, protos, attributes);
22501       /* Handle instance variable declarations, if any.  */
22502       cp_parser_objc_class_ivars (parser);
22503       objc_continue_interface ();
22504     }
22505
22506   cp_parser_objc_method_prototype_list (parser);
22507 }
22508
22509 /* Parse an Objective-C class implementation.  */
22510
22511 static void
22512 cp_parser_objc_class_implementation (cp_parser* parser)
22513 {
22514   tree name, super, categ;
22515   bool is_class_extension;
22516
22517   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22518   name = cp_parser_identifier (parser);
22519   if (name == error_mark_node)
22520     {
22521       /* It's hard to recover because even if valid @implementation
22522          stuff is to follow, we can't compile it (or validate it) if
22523          we don't even know which class it refers to.  Let's assume
22524          this was a stray '@implementation' token in the stream and
22525          skip it.
22526       */
22527       return;
22528     }
22529   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22530                                          &is_class_extension);
22531
22532   /* We have either a class or a category on our hands.  */
22533   if (categ)
22534     objc_start_category_implementation (name, categ);
22535   else
22536     {
22537       objc_start_class_implementation (name, super);
22538       /* Handle instance variable declarations, if any.  */
22539       cp_parser_objc_class_ivars (parser);
22540       objc_continue_implementation ();
22541     }
22542
22543   cp_parser_objc_method_definition_list (parser);
22544 }
22545
22546 /* Consume the @end token and finish off the implementation.  */
22547
22548 static void
22549 cp_parser_objc_end_implementation (cp_parser* parser)
22550 {
22551   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22552   objc_finish_implementation ();
22553 }
22554
22555 /* Parse an Objective-C declaration.  */
22556
22557 static void
22558 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22559 {
22560   /* Try to figure out what kind of declaration is present.  */
22561   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22562
22563   if (attributes)
22564     switch (kwd->keyword)
22565       {
22566         case RID_AT_ALIAS:
22567         case RID_AT_CLASS:
22568         case RID_AT_END:
22569           error_at (kwd->location, "attributes may not be specified before"
22570                     " the %<@%D%> Objective-C++ keyword",
22571                     kwd->u.value);
22572           attributes = NULL;
22573           break;
22574         case RID_AT_IMPLEMENTATION:
22575           warning_at (kwd->location, OPT_Wattributes,
22576                       "prefix attributes are ignored before %<@%D%>",
22577                       kwd->u.value);
22578           attributes = NULL;
22579         default:
22580           break;
22581       }
22582
22583   switch (kwd->keyword)
22584     {
22585     case RID_AT_ALIAS:
22586       cp_parser_objc_alias_declaration (parser);
22587       break;
22588     case RID_AT_CLASS:
22589       cp_parser_objc_class_declaration (parser);
22590       break;
22591     case RID_AT_PROTOCOL:
22592       cp_parser_objc_protocol_declaration (parser, attributes);
22593       break;
22594     case RID_AT_INTERFACE:
22595       cp_parser_objc_class_interface (parser, attributes);
22596       break;
22597     case RID_AT_IMPLEMENTATION:
22598       cp_parser_objc_class_implementation (parser);
22599       break;
22600     case RID_AT_END:
22601       cp_parser_objc_end_implementation (parser);
22602       break;
22603     default:
22604       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22605                 kwd->u.value);
22606       cp_parser_skip_to_end_of_block_or_statement (parser);
22607     }
22608 }
22609
22610 /* Parse an Objective-C try-catch-finally statement.
22611
22612    objc-try-catch-finally-stmt:
22613      @try compound-statement objc-catch-clause-seq [opt]
22614        objc-finally-clause [opt]
22615
22616    objc-catch-clause-seq:
22617      objc-catch-clause objc-catch-clause-seq [opt]
22618
22619    objc-catch-clause:
22620      @catch ( objc-exception-declaration ) compound-statement
22621
22622    objc-finally-clause:
22623      @finally compound-statement
22624
22625    objc-exception-declaration:
22626      parameter-declaration
22627      '...'
22628
22629    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22630
22631    Returns NULL_TREE.
22632
22633    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22634    for C.  Keep them in sync.  */   
22635
22636 static tree
22637 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22638 {
22639   location_t location;
22640   tree stmt;
22641
22642   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22643   location = cp_lexer_peek_token (parser->lexer)->location;
22644   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22645      node, lest it get absorbed into the surrounding block.  */
22646   stmt = push_stmt_list ();
22647   cp_parser_compound_statement (parser, NULL, false);
22648   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22649
22650   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22651     {
22652       cp_parameter_declarator *parm;
22653       tree parameter_declaration = error_mark_node;
22654       bool seen_open_paren = false;
22655
22656       cp_lexer_consume_token (parser->lexer);
22657       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22658         seen_open_paren = true;
22659       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22660         {
22661           /* We have "@catch (...)" (where the '...' are literally
22662              what is in the code).  Skip the '...'.
22663              parameter_declaration is set to NULL_TREE, and
22664              objc_being_catch_clauses() knows that that means
22665              '...'.  */
22666           cp_lexer_consume_token (parser->lexer);
22667           parameter_declaration = NULL_TREE;
22668         }
22669       else
22670         {
22671           /* We have "@catch (NSException *exception)" or something
22672              like that.  Parse the parameter declaration.  */
22673           parm = cp_parser_parameter_declaration (parser, false, NULL);
22674           if (parm == NULL)
22675             parameter_declaration = error_mark_node;
22676           else
22677             parameter_declaration = grokdeclarator (parm->declarator,
22678                                                     &parm->decl_specifiers,
22679                                                     PARM, /*initialized=*/0,
22680                                                     /*attrlist=*/NULL);
22681         }
22682       if (seen_open_paren)
22683         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22684       else
22685         {
22686           /* If there was no open parenthesis, we are recovering from
22687              an error, and we are trying to figure out what mistake
22688              the user has made.  */
22689
22690           /* If there is an immediate closing parenthesis, the user
22691              probably forgot the opening one (ie, they typed "@catch
22692              NSException *e)".  Parse the closing parenthesis and keep
22693              going.  */
22694           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22695             cp_lexer_consume_token (parser->lexer);
22696           
22697           /* If these is no immediate closing parenthesis, the user
22698              probably doesn't know that parenthesis are required at
22699              all (ie, they typed "@catch NSException *e").  So, just
22700              forget about the closing parenthesis and keep going.  */
22701         }
22702       objc_begin_catch_clause (parameter_declaration);
22703       cp_parser_compound_statement (parser, NULL, false);
22704       objc_finish_catch_clause ();
22705     }
22706   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22707     {
22708       cp_lexer_consume_token (parser->lexer);
22709       location = cp_lexer_peek_token (parser->lexer)->location;
22710       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22711          node, lest it get absorbed into the surrounding block.  */
22712       stmt = push_stmt_list ();
22713       cp_parser_compound_statement (parser, NULL, false);
22714       objc_build_finally_clause (location, pop_stmt_list (stmt));
22715     }
22716
22717   return objc_finish_try_stmt ();
22718 }
22719
22720 /* Parse an Objective-C synchronized statement.
22721
22722    objc-synchronized-stmt:
22723      @synchronized ( expression ) compound-statement
22724
22725    Returns NULL_TREE.  */
22726
22727 static tree
22728 cp_parser_objc_synchronized_statement (cp_parser *parser)
22729 {
22730   location_t location;
22731   tree lock, stmt;
22732
22733   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22734
22735   location = cp_lexer_peek_token (parser->lexer)->location;
22736   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22737   lock = cp_parser_expression (parser, false, NULL);
22738   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22739
22740   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22741      node, lest it get absorbed into the surrounding block.  */
22742   stmt = push_stmt_list ();
22743   cp_parser_compound_statement (parser, NULL, false);
22744
22745   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22746 }
22747
22748 /* Parse an Objective-C throw statement.
22749
22750    objc-throw-stmt:
22751      @throw assignment-expression [opt] ;
22752
22753    Returns a constructed '@throw' statement.  */
22754
22755 static tree
22756 cp_parser_objc_throw_statement (cp_parser *parser)
22757 {
22758   tree expr = NULL_TREE;
22759   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22760
22761   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22762
22763   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22764     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22765
22766   cp_parser_consume_semicolon_at_end_of_statement (parser);
22767
22768   return objc_build_throw_stmt (loc, expr);
22769 }
22770
22771 /* Parse an Objective-C statement.  */
22772
22773 static tree
22774 cp_parser_objc_statement (cp_parser * parser)
22775 {
22776   /* Try to figure out what kind of declaration is present.  */
22777   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22778
22779   switch (kwd->keyword)
22780     {
22781     case RID_AT_TRY:
22782       return cp_parser_objc_try_catch_finally_statement (parser);
22783     case RID_AT_SYNCHRONIZED:
22784       return cp_parser_objc_synchronized_statement (parser);
22785     case RID_AT_THROW:
22786       return cp_parser_objc_throw_statement (parser);
22787     default:
22788       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22789                kwd->u.value);
22790       cp_parser_skip_to_end_of_block_or_statement (parser);
22791     }
22792
22793   return error_mark_node;
22794 }
22795
22796 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22797    look ahead to see if an objc keyword follows the attributes.  This
22798    is to detect the use of prefix attributes on ObjC @interface and 
22799    @protocol.  */
22800
22801 static bool
22802 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22803 {
22804   cp_lexer_save_tokens (parser->lexer);
22805   *attrib = cp_parser_attributes_opt (parser);
22806   gcc_assert (*attrib);
22807   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22808     {
22809       cp_lexer_commit_tokens (parser->lexer);
22810       return true;
22811     }
22812   cp_lexer_rollback_tokens (parser->lexer);
22813   return false;  
22814 }
22815
22816 /* This routine is a minimal replacement for
22817    c_parser_struct_declaration () used when parsing the list of
22818    types/names or ObjC++ properties.  For example, when parsing the
22819    code
22820
22821    @property (readonly) int a, b, c;
22822
22823    this function is responsible for parsing "int a, int b, int c" and
22824    returning the declarations as CHAIN of DECLs.
22825
22826    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22827    similar parsing.  */
22828 static tree
22829 cp_parser_objc_struct_declaration (cp_parser *parser)
22830 {
22831   tree decls = NULL_TREE;
22832   cp_decl_specifier_seq declspecs;
22833   int decl_class_or_enum_p;
22834   tree prefix_attributes;
22835
22836   cp_parser_decl_specifier_seq (parser,
22837                                 CP_PARSER_FLAGS_NONE,
22838                                 &declspecs,
22839                                 &decl_class_or_enum_p);
22840
22841   if (declspecs.type == error_mark_node)
22842     return error_mark_node;
22843
22844   /* auto, register, static, extern, mutable.  */
22845   if (declspecs.storage_class != sc_none)
22846     {
22847       cp_parser_error (parser, "invalid type for property");
22848       declspecs.storage_class = sc_none;
22849     }
22850   
22851   /* __thread.  */
22852   if (declspecs.specs[(int) ds_thread])
22853     {
22854       cp_parser_error (parser, "invalid type for property");
22855       declspecs.specs[(int) ds_thread] = 0;
22856     }
22857   
22858   /* typedef.  */
22859   if (declspecs.specs[(int) ds_typedef])
22860     {
22861       cp_parser_error (parser, "invalid type for property");
22862       declspecs.specs[(int) ds_typedef] = 0;
22863     }
22864
22865   prefix_attributes = declspecs.attributes;
22866   declspecs.attributes = NULL_TREE;
22867
22868   /* Keep going until we hit the `;' at the end of the declaration. */
22869   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22870     {
22871       tree attributes, first_attribute, decl;
22872       cp_declarator *declarator;
22873       cp_token *token;
22874
22875       /* Parse the declarator.  */
22876       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22877                                          NULL, NULL, false);
22878
22879       /* Look for attributes that apply to the ivar.  */
22880       attributes = cp_parser_attributes_opt (parser);
22881       /* Remember which attributes are prefix attributes and
22882          which are not.  */
22883       first_attribute = attributes;
22884       /* Combine the attributes.  */
22885       attributes = chainon (prefix_attributes, attributes);
22886       
22887       decl = grokfield (declarator, &declspecs,
22888                         NULL_TREE, /*init_const_expr_p=*/false,
22889                         NULL_TREE, attributes);
22890
22891       if (decl == error_mark_node || decl == NULL_TREE)
22892         return error_mark_node;
22893       
22894       /* Reset PREFIX_ATTRIBUTES.  */
22895       while (attributes && TREE_CHAIN (attributes) != first_attribute)
22896         attributes = TREE_CHAIN (attributes);
22897       if (attributes)
22898         TREE_CHAIN (attributes) = NULL_TREE;
22899
22900       DECL_CHAIN (decl) = decls;
22901       decls = decl;
22902
22903       token = cp_lexer_peek_token (parser->lexer);
22904       if (token->type == CPP_COMMA)
22905         {
22906           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22907           continue;
22908         }
22909       else
22910         break;
22911     }
22912   return decls;
22913 }
22914
22915 /* Parse an Objective-C @property declaration.  The syntax is:
22916
22917    objc-property-declaration:
22918      '@property' objc-property-attributes[opt] struct-declaration ;
22919
22920    objc-property-attributes:
22921     '(' objc-property-attribute-list ')'
22922
22923    objc-property-attribute-list:
22924      objc-property-attribute
22925      objc-property-attribute-list, objc-property-attribute
22926
22927    objc-property-attribute
22928      'getter' = identifier
22929      'setter' = identifier
22930      'readonly'
22931      'readwrite'
22932      'assign'
22933      'retain'
22934      'copy'
22935      'nonatomic'
22936
22937   For example:
22938     @property NSString *name;
22939     @property (readonly) id object;
22940     @property (retain, nonatomic, getter=getTheName) id name;
22941     @property int a, b, c;
22942
22943    PS: This function is identical to
22944    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
22945 static void 
22946 cp_parser_objc_at_property_declaration (cp_parser *parser)
22947 {
22948   /* The following variables hold the attributes of the properties as
22949      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
22950      seen.  When we see an attribute, we set them to 'true' (if they
22951      are boolean properties) or to the identifier (if they have an
22952      argument, ie, for getter and setter).  Note that here we only
22953      parse the list of attributes, check the syntax and accumulate the
22954      attributes that we find.  objc_add_property_declaration() will
22955      then process the information.  */
22956   bool property_assign = false;
22957   bool property_copy = false;
22958   tree property_getter_ident = NULL_TREE;
22959   bool property_nonatomic = false;
22960   bool property_readonly = false;
22961   bool property_readwrite = false;
22962   bool property_retain = false;
22963   tree property_setter_ident = NULL_TREE;
22964
22965   /* 'properties' is the list of properties that we read.  Usually a
22966      single one, but maybe more (eg, in "@property int a, b, c;" there
22967      are three).  */
22968   tree properties;
22969   location_t loc;
22970
22971   loc = cp_lexer_peek_token (parser->lexer)->location;
22972
22973   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
22974
22975   /* Parse the optional attribute list...  */
22976   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22977     {
22978       /* Eat the '('.  */
22979       cp_lexer_consume_token (parser->lexer);
22980
22981       while (true)
22982         {
22983           bool syntax_error = false;
22984           cp_token *token = cp_lexer_peek_token (parser->lexer);
22985           enum rid keyword;
22986
22987           if (token->type != CPP_NAME)
22988             {
22989               cp_parser_error (parser, "expected identifier");
22990               break;
22991             }
22992           keyword = C_RID_CODE (token->u.value);
22993           cp_lexer_consume_token (parser->lexer);
22994           switch (keyword)
22995             {
22996             case RID_ASSIGN:    property_assign = true;    break;
22997             case RID_COPY:      property_copy = true;      break;
22998             case RID_NONATOMIC: property_nonatomic = true; break;
22999             case RID_READONLY:  property_readonly = true;  break;
23000             case RID_READWRITE: property_readwrite = true; break;
23001             case RID_RETAIN:    property_retain = true;    break;
23002
23003             case RID_GETTER:
23004             case RID_SETTER:
23005               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23006                 {
23007                   cp_parser_error (parser,
23008                                    "getter/setter/ivar attribute must be followed by %<=%>");
23009                   syntax_error = true;
23010                   break;
23011                 }
23012               cp_lexer_consume_token (parser->lexer); /* eat the = */
23013               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23014                 {
23015                   cp_parser_error (parser, "expected identifier");
23016                   syntax_error = true;
23017                   break;
23018                 }
23019               if (keyword == RID_SETTER)
23020                 {
23021                   if (property_setter_ident != NULL_TREE)
23022                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23023                   else
23024                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23025                   cp_lexer_consume_token (parser->lexer);
23026                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23027                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23028                   else
23029                     cp_lexer_consume_token (parser->lexer);
23030                 }
23031               else
23032                 {
23033                   if (property_getter_ident != NULL_TREE)
23034                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23035                   else
23036                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23037                   cp_lexer_consume_token (parser->lexer);
23038                 }
23039               break;
23040             default:
23041               cp_parser_error (parser, "unknown property attribute");
23042               syntax_error = true;
23043               break;
23044             }
23045
23046           if (syntax_error)
23047             break;
23048           
23049           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23050             cp_lexer_consume_token (parser->lexer);
23051           else
23052             break;
23053         }
23054
23055       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23056         {
23057           cp_parser_skip_to_closing_parenthesis (parser,
23058                                                  /*recovering=*/true,
23059                                                  /*or_comma=*/false,
23060                                                  /*consume_paren=*/true);
23061         }
23062     }
23063
23064   /* ... and the property declaration(s).  */
23065   properties = cp_parser_objc_struct_declaration (parser);
23066
23067   if (properties == error_mark_node)
23068     {
23069       cp_parser_skip_to_end_of_statement (parser);
23070       /* If the next token is now a `;', consume it.  */
23071       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23072         cp_lexer_consume_token (parser->lexer);
23073       return;
23074     }
23075
23076   if (properties == NULL_TREE)
23077     cp_parser_error (parser, "expected identifier");
23078   else
23079     {
23080       /* Comma-separated properties are chained together in
23081          reverse order; add them one by one.  */
23082       properties = nreverse (properties);
23083       
23084       for (; properties; properties = TREE_CHAIN (properties))
23085         objc_add_property_declaration (loc, copy_node (properties),
23086                                        property_readonly, property_readwrite,
23087                                        property_assign, property_retain,
23088                                        property_copy, property_nonatomic,
23089                                        property_getter_ident, property_setter_ident);
23090     }
23091   
23092   cp_parser_consume_semicolon_at_end_of_statement (parser);
23093 }
23094
23095 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23096
23097    objc-synthesize-declaration:
23098      @synthesize objc-synthesize-identifier-list ;
23099
23100    objc-synthesize-identifier-list:
23101      objc-synthesize-identifier
23102      objc-synthesize-identifier-list, objc-synthesize-identifier
23103
23104    objc-synthesize-identifier
23105      identifier
23106      identifier = identifier
23107
23108   For example:
23109     @synthesize MyProperty;
23110     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23111
23112   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23113   for C.  Keep them in sync.
23114 */
23115 static void 
23116 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23117 {
23118   tree list = NULL_TREE;
23119   location_t loc;
23120   loc = cp_lexer_peek_token (parser->lexer)->location;
23121
23122   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23123   while (true)
23124     {
23125       tree property, ivar;
23126       property = cp_parser_identifier (parser);
23127       if (property == error_mark_node)
23128         {
23129           cp_parser_consume_semicolon_at_end_of_statement (parser);
23130           return;
23131         }
23132       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23133         {
23134           cp_lexer_consume_token (parser->lexer);
23135           ivar = cp_parser_identifier (parser);
23136           if (ivar == error_mark_node)
23137             {
23138               cp_parser_consume_semicolon_at_end_of_statement (parser);
23139               return;
23140             }
23141         }
23142       else
23143         ivar = NULL_TREE;
23144       list = chainon (list, build_tree_list (ivar, property));
23145       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23146         cp_lexer_consume_token (parser->lexer);
23147       else
23148         break;
23149     }
23150   cp_parser_consume_semicolon_at_end_of_statement (parser);
23151   objc_add_synthesize_declaration (loc, list);
23152 }
23153
23154 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23155
23156    objc-dynamic-declaration:
23157      @dynamic identifier-list ;
23158
23159    For example:
23160      @dynamic MyProperty;
23161      @dynamic MyProperty, AnotherProperty;
23162
23163   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23164   for C.  Keep them in sync.
23165 */
23166 static void 
23167 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23168 {
23169   tree list = NULL_TREE;
23170   location_t loc;
23171   loc = cp_lexer_peek_token (parser->lexer)->location;
23172
23173   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23174   while (true)
23175     {
23176       tree property;
23177       property = cp_parser_identifier (parser);
23178       if (property == error_mark_node)
23179         {
23180           cp_parser_consume_semicolon_at_end_of_statement (parser);
23181           return;
23182         }
23183       list = chainon (list, build_tree_list (NULL, property));
23184       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23185         cp_lexer_consume_token (parser->lexer);
23186       else
23187         break;
23188     }
23189   cp_parser_consume_semicolon_at_end_of_statement (parser);
23190   objc_add_dynamic_declaration (loc, list);
23191 }
23192
23193 \f
23194 /* OpenMP 2.5 parsing routines.  */
23195
23196 /* Returns name of the next clause.
23197    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23198    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23199    returned and the token is consumed.  */
23200
23201 static pragma_omp_clause
23202 cp_parser_omp_clause_name (cp_parser *parser)
23203 {
23204   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23205
23206   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23207     result = PRAGMA_OMP_CLAUSE_IF;
23208   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23209     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23210   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23211     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23212   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23213     {
23214       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23215       const char *p = IDENTIFIER_POINTER (id);
23216
23217       switch (p[0])
23218         {
23219         case 'c':
23220           if (!strcmp ("collapse", p))
23221             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23222           else if (!strcmp ("copyin", p))
23223             result = PRAGMA_OMP_CLAUSE_COPYIN;
23224           else if (!strcmp ("copyprivate", p))
23225             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23226           break;
23227         case 'f':
23228           if (!strcmp ("firstprivate", p))
23229             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23230           break;
23231         case 'l':
23232           if (!strcmp ("lastprivate", p))
23233             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23234           break;
23235         case 'n':
23236           if (!strcmp ("nowait", p))
23237             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23238           else if (!strcmp ("num_threads", p))
23239             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23240           break;
23241         case 'o':
23242           if (!strcmp ("ordered", p))
23243             result = PRAGMA_OMP_CLAUSE_ORDERED;
23244           break;
23245         case 'r':
23246           if (!strcmp ("reduction", p))
23247             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23248           break;
23249         case 's':
23250           if (!strcmp ("schedule", p))
23251             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23252           else if (!strcmp ("shared", p))
23253             result = PRAGMA_OMP_CLAUSE_SHARED;
23254           break;
23255         case 'u':
23256           if (!strcmp ("untied", p))
23257             result = PRAGMA_OMP_CLAUSE_UNTIED;
23258           break;
23259         }
23260     }
23261
23262   if (result != PRAGMA_OMP_CLAUSE_NONE)
23263     cp_lexer_consume_token (parser->lexer);
23264
23265   return result;
23266 }
23267
23268 /* Validate that a clause of the given type does not already exist.  */
23269
23270 static void
23271 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23272                            const char *name, location_t location)
23273 {
23274   tree c;
23275
23276   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23277     if (OMP_CLAUSE_CODE (c) == code)
23278       {
23279         error_at (location, "too many %qs clauses", name);
23280         break;
23281       }
23282 }
23283
23284 /* OpenMP 2.5:
23285    variable-list:
23286      identifier
23287      variable-list , identifier
23288
23289    In addition, we match a closing parenthesis.  An opening parenthesis
23290    will have been consumed by the caller.
23291
23292    If KIND is nonzero, create the appropriate node and install the decl
23293    in OMP_CLAUSE_DECL and add the node to the head of the list.
23294
23295    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23296    return the list created.  */
23297
23298 static tree
23299 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23300                                 tree list)
23301 {
23302   cp_token *token;
23303   while (1)
23304     {
23305       tree name, decl;
23306
23307       token = cp_lexer_peek_token (parser->lexer);
23308       name = cp_parser_id_expression (parser, /*template_p=*/false,
23309                                       /*check_dependency_p=*/true,
23310                                       /*template_p=*/NULL,
23311                                       /*declarator_p=*/false,
23312                                       /*optional_p=*/false);
23313       if (name == error_mark_node)
23314         goto skip_comma;
23315
23316       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23317       if (decl == error_mark_node)
23318         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23319                                      token->location);
23320       else if (kind != 0)
23321         {
23322           tree u = build_omp_clause (token->location, kind);
23323           OMP_CLAUSE_DECL (u) = decl;
23324           OMP_CLAUSE_CHAIN (u) = list;
23325           list = u;
23326         }
23327       else
23328         list = tree_cons (decl, NULL_TREE, list);
23329
23330     get_comma:
23331       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23332         break;
23333       cp_lexer_consume_token (parser->lexer);
23334     }
23335
23336   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23337     {
23338       int ending;
23339
23340       /* Try to resync to an unnested comma.  Copied from
23341          cp_parser_parenthesized_expression_list.  */
23342     skip_comma:
23343       ending = cp_parser_skip_to_closing_parenthesis (parser,
23344                                                       /*recovering=*/true,
23345                                                       /*or_comma=*/true,
23346                                                       /*consume_paren=*/true);
23347       if (ending < 0)
23348         goto get_comma;
23349     }
23350
23351   return list;
23352 }
23353
23354 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23355    common case for omp clauses.  */
23356
23357 static tree
23358 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23359 {
23360   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23361     return cp_parser_omp_var_list_no_open (parser, kind, list);
23362   return list;
23363 }
23364
23365 /* OpenMP 3.0:
23366    collapse ( constant-expression ) */
23367
23368 static tree
23369 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23370 {
23371   tree c, num;
23372   location_t loc;
23373   HOST_WIDE_INT n;
23374
23375   loc = cp_lexer_peek_token (parser->lexer)->location;
23376   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23377     return list;
23378
23379   num = cp_parser_constant_expression (parser, false, NULL);
23380
23381   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23382     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23383                                            /*or_comma=*/false,
23384                                            /*consume_paren=*/true);
23385
23386   if (num == error_mark_node)
23387     return list;
23388   num = fold_non_dependent_expr (num);
23389   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23390       || !host_integerp (num, 0)
23391       || (n = tree_low_cst (num, 0)) <= 0
23392       || (int) n != n)
23393     {
23394       error_at (loc, "collapse argument needs positive constant integer expression");
23395       return list;
23396     }
23397
23398   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23399   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23400   OMP_CLAUSE_CHAIN (c) = list;
23401   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23402
23403   return c;
23404 }
23405
23406 /* OpenMP 2.5:
23407    default ( shared | none ) */
23408
23409 static tree
23410 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23411 {
23412   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23413   tree c;
23414
23415   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23416     return list;
23417   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23418     {
23419       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23420       const char *p = IDENTIFIER_POINTER (id);
23421
23422       switch (p[0])
23423         {
23424         case 'n':
23425           if (strcmp ("none", p) != 0)
23426             goto invalid_kind;
23427           kind = OMP_CLAUSE_DEFAULT_NONE;
23428           break;
23429
23430         case 's':
23431           if (strcmp ("shared", p) != 0)
23432             goto invalid_kind;
23433           kind = OMP_CLAUSE_DEFAULT_SHARED;
23434           break;
23435
23436         default:
23437           goto invalid_kind;
23438         }
23439
23440       cp_lexer_consume_token (parser->lexer);
23441     }
23442   else
23443     {
23444     invalid_kind:
23445       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23446     }
23447
23448   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23449     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23450                                            /*or_comma=*/false,
23451                                            /*consume_paren=*/true);
23452
23453   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23454     return list;
23455
23456   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23457   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23458   OMP_CLAUSE_CHAIN (c) = list;
23459   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23460
23461   return c;
23462 }
23463
23464 /* OpenMP 2.5:
23465    if ( expression ) */
23466
23467 static tree
23468 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23469 {
23470   tree t, c;
23471
23472   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23473     return list;
23474
23475   t = cp_parser_condition (parser);
23476
23477   if (t == error_mark_node
23478       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23479     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23480                                            /*or_comma=*/false,
23481                                            /*consume_paren=*/true);
23482
23483   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23484
23485   c = build_omp_clause (location, OMP_CLAUSE_IF);
23486   OMP_CLAUSE_IF_EXPR (c) = t;
23487   OMP_CLAUSE_CHAIN (c) = list;
23488
23489   return c;
23490 }
23491
23492 /* OpenMP 2.5:
23493    nowait */
23494
23495 static tree
23496 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23497                              tree list, location_t location)
23498 {
23499   tree c;
23500
23501   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23502
23503   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23504   OMP_CLAUSE_CHAIN (c) = list;
23505   return c;
23506 }
23507
23508 /* OpenMP 2.5:
23509    num_threads ( expression ) */
23510
23511 static tree
23512 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23513                                   location_t location)
23514 {
23515   tree t, c;
23516
23517   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23518     return list;
23519
23520   t = cp_parser_expression (parser, false, NULL);
23521
23522   if (t == error_mark_node
23523       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23524     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23525                                            /*or_comma=*/false,
23526                                            /*consume_paren=*/true);
23527
23528   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23529                              "num_threads", location);
23530
23531   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23532   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23533   OMP_CLAUSE_CHAIN (c) = list;
23534
23535   return c;
23536 }
23537
23538 /* OpenMP 2.5:
23539    ordered */
23540
23541 static tree
23542 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23543                               tree list, location_t location)
23544 {
23545   tree c;
23546
23547   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23548                              "ordered", location);
23549
23550   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23551   OMP_CLAUSE_CHAIN (c) = list;
23552   return c;
23553 }
23554
23555 /* OpenMP 2.5:
23556    reduction ( reduction-operator : variable-list )
23557
23558    reduction-operator:
23559      One of: + * - & ^ | && || */
23560
23561 static tree
23562 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23563 {
23564   enum tree_code code;
23565   tree nlist, c;
23566
23567   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23568     return list;
23569
23570   switch (cp_lexer_peek_token (parser->lexer)->type)
23571     {
23572     case CPP_PLUS:
23573       code = PLUS_EXPR;
23574       break;
23575     case CPP_MULT:
23576       code = MULT_EXPR;
23577       break;
23578     case CPP_MINUS:
23579       code = MINUS_EXPR;
23580       break;
23581     case CPP_AND:
23582       code = BIT_AND_EXPR;
23583       break;
23584     case CPP_XOR:
23585       code = BIT_XOR_EXPR;
23586       break;
23587     case CPP_OR:
23588       code = BIT_IOR_EXPR;
23589       break;
23590     case CPP_AND_AND:
23591       code = TRUTH_ANDIF_EXPR;
23592       break;
23593     case CPP_OR_OR:
23594       code = TRUTH_ORIF_EXPR;
23595       break;
23596     default:
23597       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23598                                "%<|%>, %<&&%>, or %<||%>");
23599     resync_fail:
23600       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23601                                              /*or_comma=*/false,
23602                                              /*consume_paren=*/true);
23603       return list;
23604     }
23605   cp_lexer_consume_token (parser->lexer);
23606
23607   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23608     goto resync_fail;
23609
23610   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23611   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23612     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23613
23614   return nlist;
23615 }
23616
23617 /* OpenMP 2.5:
23618    schedule ( schedule-kind )
23619    schedule ( schedule-kind , expression )
23620
23621    schedule-kind:
23622      static | dynamic | guided | runtime | auto  */
23623
23624 static tree
23625 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23626 {
23627   tree c, t;
23628
23629   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23630     return list;
23631
23632   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23633
23634   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23635     {
23636       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23637       const char *p = IDENTIFIER_POINTER (id);
23638
23639       switch (p[0])
23640         {
23641         case 'd':
23642           if (strcmp ("dynamic", p) != 0)
23643             goto invalid_kind;
23644           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23645           break;
23646
23647         case 'g':
23648           if (strcmp ("guided", p) != 0)
23649             goto invalid_kind;
23650           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23651           break;
23652
23653         case 'r':
23654           if (strcmp ("runtime", p) != 0)
23655             goto invalid_kind;
23656           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23657           break;
23658
23659         default:
23660           goto invalid_kind;
23661         }
23662     }
23663   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23664     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23665   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23666     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23667   else
23668     goto invalid_kind;
23669   cp_lexer_consume_token (parser->lexer);
23670
23671   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23672     {
23673       cp_token *token;
23674       cp_lexer_consume_token (parser->lexer);
23675
23676       token = cp_lexer_peek_token (parser->lexer);
23677       t = cp_parser_assignment_expression (parser, false, NULL);
23678
23679       if (t == error_mark_node)
23680         goto resync_fail;
23681       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23682         error_at (token->location, "schedule %<runtime%> does not take "
23683                   "a %<chunk_size%> parameter");
23684       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23685         error_at (token->location, "schedule %<auto%> does not take "
23686                   "a %<chunk_size%> parameter");
23687       else
23688         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23689
23690       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23691         goto resync_fail;
23692     }
23693   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23694     goto resync_fail;
23695
23696   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23697   OMP_CLAUSE_CHAIN (c) = list;
23698   return c;
23699
23700  invalid_kind:
23701   cp_parser_error (parser, "invalid schedule kind");
23702  resync_fail:
23703   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23704                                          /*or_comma=*/false,
23705                                          /*consume_paren=*/true);
23706   return list;
23707 }
23708
23709 /* OpenMP 3.0:
23710    untied */
23711
23712 static tree
23713 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23714                              tree list, location_t location)
23715 {
23716   tree c;
23717
23718   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23719
23720   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23721   OMP_CLAUSE_CHAIN (c) = list;
23722   return c;
23723 }
23724
23725 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23726    is a bitmask in MASK.  Return the list of clauses found; the result
23727    of clause default goes in *pdefault.  */
23728
23729 static tree
23730 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23731                            const char *where, cp_token *pragma_tok)
23732 {
23733   tree clauses = NULL;
23734   bool first = true;
23735   cp_token *token = NULL;
23736
23737   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23738     {
23739       pragma_omp_clause c_kind;
23740       const char *c_name;
23741       tree prev = clauses;
23742
23743       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23744         cp_lexer_consume_token (parser->lexer);
23745
23746       token = cp_lexer_peek_token (parser->lexer);
23747       c_kind = cp_parser_omp_clause_name (parser);
23748       first = false;
23749
23750       switch (c_kind)
23751         {
23752         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23753           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23754                                                    token->location);
23755           c_name = "collapse";
23756           break;
23757         case PRAGMA_OMP_CLAUSE_COPYIN:
23758           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23759           c_name = "copyin";
23760           break;
23761         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23762           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23763                                             clauses);
23764           c_name = "copyprivate";
23765           break;
23766         case PRAGMA_OMP_CLAUSE_DEFAULT:
23767           clauses = cp_parser_omp_clause_default (parser, clauses,
23768                                                   token->location);
23769           c_name = "default";
23770           break;
23771         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23772           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23773                                             clauses);
23774           c_name = "firstprivate";
23775           break;
23776         case PRAGMA_OMP_CLAUSE_IF:
23777           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23778           c_name = "if";
23779           break;
23780         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23781           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23782                                             clauses);
23783           c_name = "lastprivate";
23784           break;
23785         case PRAGMA_OMP_CLAUSE_NOWAIT:
23786           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23787           c_name = "nowait";
23788           break;
23789         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23790           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23791                                                       token->location);
23792           c_name = "num_threads";
23793           break;
23794         case PRAGMA_OMP_CLAUSE_ORDERED:
23795           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23796                                                   token->location);
23797           c_name = "ordered";
23798           break;
23799         case PRAGMA_OMP_CLAUSE_PRIVATE:
23800           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23801                                             clauses);
23802           c_name = "private";
23803           break;
23804         case PRAGMA_OMP_CLAUSE_REDUCTION:
23805           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23806           c_name = "reduction";
23807           break;
23808         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23809           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23810                                                    token->location);
23811           c_name = "schedule";
23812           break;
23813         case PRAGMA_OMP_CLAUSE_SHARED:
23814           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23815                                             clauses);
23816           c_name = "shared";
23817           break;
23818         case PRAGMA_OMP_CLAUSE_UNTIED:
23819           clauses = cp_parser_omp_clause_untied (parser, clauses,
23820                                                  token->location);
23821           c_name = "nowait";
23822           break;
23823         default:
23824           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23825           goto saw_error;
23826         }
23827
23828       if (((mask >> c_kind) & 1) == 0)
23829         {
23830           /* Remove the invalid clause(s) from the list to avoid
23831              confusing the rest of the compiler.  */
23832           clauses = prev;
23833           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23834         }
23835     }
23836  saw_error:
23837   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23838   return finish_omp_clauses (clauses);
23839 }
23840
23841 /* OpenMP 2.5:
23842    structured-block:
23843      statement
23844
23845    In practice, we're also interested in adding the statement to an
23846    outer node.  So it is convenient if we work around the fact that
23847    cp_parser_statement calls add_stmt.  */
23848
23849 static unsigned
23850 cp_parser_begin_omp_structured_block (cp_parser *parser)
23851 {
23852   unsigned save = parser->in_statement;
23853
23854   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23855      This preserves the "not within loop or switch" style error messages
23856      for nonsense cases like
23857         void foo() {
23858         #pragma omp single
23859           break;
23860         }
23861   */
23862   if (parser->in_statement)
23863     parser->in_statement = IN_OMP_BLOCK;
23864
23865   return save;
23866 }
23867
23868 static void
23869 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23870 {
23871   parser->in_statement = save;
23872 }
23873
23874 static tree
23875 cp_parser_omp_structured_block (cp_parser *parser)
23876 {
23877   tree stmt = begin_omp_structured_block ();
23878   unsigned int save = cp_parser_begin_omp_structured_block (parser);
23879
23880   cp_parser_statement (parser, NULL_TREE, false, NULL);
23881
23882   cp_parser_end_omp_structured_block (parser, save);
23883   return finish_omp_structured_block (stmt);
23884 }
23885
23886 /* OpenMP 2.5:
23887    # pragma omp atomic new-line
23888      expression-stmt
23889
23890    expression-stmt:
23891      x binop= expr | x++ | ++x | x-- | --x
23892    binop:
23893      +, *, -, /, &, ^, |, <<, >>
23894
23895   where x is an lvalue expression with scalar type.  */
23896
23897 static void
23898 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23899 {
23900   tree lhs, rhs;
23901   enum tree_code code;
23902
23903   cp_parser_require_pragma_eol (parser, pragma_tok);
23904
23905   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23906                                     /*cast_p=*/false, NULL);
23907   switch (TREE_CODE (lhs))
23908     {
23909     case ERROR_MARK:
23910       goto saw_error;
23911
23912     case PREINCREMENT_EXPR:
23913     case POSTINCREMENT_EXPR:
23914       lhs = TREE_OPERAND (lhs, 0);
23915       code = PLUS_EXPR;
23916       rhs = integer_one_node;
23917       break;
23918
23919     case PREDECREMENT_EXPR:
23920     case POSTDECREMENT_EXPR:
23921       lhs = TREE_OPERAND (lhs, 0);
23922       code = MINUS_EXPR;
23923       rhs = integer_one_node;
23924       break;
23925
23926     case COMPOUND_EXPR:
23927       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23928          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23929          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23930          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23931          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23932                                              (TREE_OPERAND (lhs, 1), 0), 0)))
23933             == BOOLEAN_TYPE)
23934        /* Undo effects of boolean_increment for post {in,de}crement.  */
23935        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23936       /* FALLTHRU */
23937     case MODIFY_EXPR:
23938       if (TREE_CODE (lhs) == MODIFY_EXPR
23939          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23940        {
23941          /* Undo effects of boolean_increment.  */
23942          if (integer_onep (TREE_OPERAND (lhs, 1)))
23943            {
23944              /* This is pre or post increment.  */
23945              rhs = TREE_OPERAND (lhs, 1);
23946              lhs = TREE_OPERAND (lhs, 0);
23947              code = NOP_EXPR;
23948              break;
23949            }
23950        }
23951       /* FALLTHRU */
23952     default:
23953       switch (cp_lexer_peek_token (parser->lexer)->type)
23954         {
23955         case CPP_MULT_EQ:
23956           code = MULT_EXPR;
23957           break;
23958         case CPP_DIV_EQ:
23959           code = TRUNC_DIV_EXPR;
23960           break;
23961         case CPP_PLUS_EQ:
23962           code = PLUS_EXPR;
23963           break;
23964         case CPP_MINUS_EQ:
23965           code = MINUS_EXPR;
23966           break;
23967         case CPP_LSHIFT_EQ:
23968           code = LSHIFT_EXPR;
23969           break;
23970         case CPP_RSHIFT_EQ:
23971           code = RSHIFT_EXPR;
23972           break;
23973         case CPP_AND_EQ:
23974           code = BIT_AND_EXPR;
23975           break;
23976         case CPP_OR_EQ:
23977           code = BIT_IOR_EXPR;
23978           break;
23979         case CPP_XOR_EQ:
23980           code = BIT_XOR_EXPR;
23981           break;
23982         default:
23983           cp_parser_error (parser,
23984                            "invalid operator for %<#pragma omp atomic%>");
23985           goto saw_error;
23986         }
23987       cp_lexer_consume_token (parser->lexer);
23988
23989       rhs = cp_parser_expression (parser, false, NULL);
23990       if (rhs == error_mark_node)
23991         goto saw_error;
23992       break;
23993     }
23994   finish_omp_atomic (code, lhs, rhs);
23995   cp_parser_consume_semicolon_at_end_of_statement (parser);
23996   return;
23997
23998  saw_error:
23999   cp_parser_skip_to_end_of_block_or_statement (parser);
24000 }
24001
24002
24003 /* OpenMP 2.5:
24004    # pragma omp barrier new-line  */
24005
24006 static void
24007 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24008 {
24009   cp_parser_require_pragma_eol (parser, pragma_tok);
24010   finish_omp_barrier ();
24011 }
24012
24013 /* OpenMP 2.5:
24014    # pragma omp critical [(name)] new-line
24015      structured-block  */
24016
24017 static tree
24018 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24019 {
24020   tree stmt, name = NULL;
24021
24022   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24023     {
24024       cp_lexer_consume_token (parser->lexer);
24025
24026       name = cp_parser_identifier (parser);
24027
24028       if (name == error_mark_node
24029           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24030         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24031                                                /*or_comma=*/false,
24032                                                /*consume_paren=*/true);
24033       if (name == error_mark_node)
24034         name = NULL;
24035     }
24036   cp_parser_require_pragma_eol (parser, pragma_tok);
24037
24038   stmt = cp_parser_omp_structured_block (parser);
24039   return c_finish_omp_critical (input_location, stmt, name);
24040 }
24041
24042 /* OpenMP 2.5:
24043    # pragma omp flush flush-vars[opt] new-line
24044
24045    flush-vars:
24046      ( variable-list ) */
24047
24048 static void
24049 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24050 {
24051   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24052     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24053   cp_parser_require_pragma_eol (parser, pragma_tok);
24054
24055   finish_omp_flush ();
24056 }
24057
24058 /* Helper function, to parse omp for increment expression.  */
24059
24060 static tree
24061 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24062 {
24063   tree cond = cp_parser_binary_expression (parser, false, true,
24064                                            PREC_NOT_OPERATOR, NULL);
24065   bool overloaded_p;
24066
24067   if (cond == error_mark_node
24068       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24069     {
24070       cp_parser_skip_to_end_of_statement (parser);
24071       return error_mark_node;
24072     }
24073
24074   switch (TREE_CODE (cond))
24075     {
24076     case GT_EXPR:
24077     case GE_EXPR:
24078     case LT_EXPR:
24079     case LE_EXPR:
24080       break;
24081     default:
24082       return error_mark_node;
24083     }
24084
24085   /* If decl is an iterator, preserve LHS and RHS of the relational
24086      expr until finish_omp_for.  */
24087   if (decl
24088       && (type_dependent_expression_p (decl)
24089           || CLASS_TYPE_P (TREE_TYPE (decl))))
24090     return cond;
24091
24092   return build_x_binary_op (TREE_CODE (cond),
24093                             TREE_OPERAND (cond, 0), ERROR_MARK,
24094                             TREE_OPERAND (cond, 1), ERROR_MARK,
24095                             &overloaded_p, tf_warning_or_error);
24096 }
24097
24098 /* Helper function, to parse omp for increment expression.  */
24099
24100 static tree
24101 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24102 {
24103   cp_token *token = cp_lexer_peek_token (parser->lexer);
24104   enum tree_code op;
24105   tree lhs, rhs;
24106   cp_id_kind idk;
24107   bool decl_first;
24108
24109   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24110     {
24111       op = (token->type == CPP_PLUS_PLUS
24112             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24113       cp_lexer_consume_token (parser->lexer);
24114       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24115       if (lhs != decl)
24116         return error_mark_node;
24117       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24118     }
24119
24120   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24121   if (lhs != decl)
24122     return error_mark_node;
24123
24124   token = cp_lexer_peek_token (parser->lexer);
24125   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24126     {
24127       op = (token->type == CPP_PLUS_PLUS
24128             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24129       cp_lexer_consume_token (parser->lexer);
24130       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24131     }
24132
24133   op = cp_parser_assignment_operator_opt (parser);
24134   if (op == ERROR_MARK)
24135     return error_mark_node;
24136
24137   if (op != NOP_EXPR)
24138     {
24139       rhs = cp_parser_assignment_expression (parser, false, NULL);
24140       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24141       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24142     }
24143
24144   lhs = cp_parser_binary_expression (parser, false, false,
24145                                      PREC_ADDITIVE_EXPRESSION, NULL);
24146   token = cp_lexer_peek_token (parser->lexer);
24147   decl_first = lhs == decl;
24148   if (decl_first)
24149     lhs = NULL_TREE;
24150   if (token->type != CPP_PLUS
24151       && token->type != CPP_MINUS)
24152     return error_mark_node;
24153
24154   do
24155     {
24156       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24157       cp_lexer_consume_token (parser->lexer);
24158       rhs = cp_parser_binary_expression (parser, false, false,
24159                                          PREC_ADDITIVE_EXPRESSION, NULL);
24160       token = cp_lexer_peek_token (parser->lexer);
24161       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24162         {
24163           if (lhs == NULL_TREE)
24164             {
24165               if (op == PLUS_EXPR)
24166                 lhs = rhs;
24167               else
24168                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24169             }
24170           else
24171             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24172                                      NULL, tf_warning_or_error);
24173         }
24174     }
24175   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24176
24177   if (!decl_first)
24178     {
24179       if (rhs != decl || op == MINUS_EXPR)
24180         return error_mark_node;
24181       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24182     }
24183   else
24184     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24185
24186   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24187 }
24188
24189 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24190
24191 static tree
24192 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24193 {
24194   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24195   tree real_decl, initv, condv, incrv, declv;
24196   tree this_pre_body, cl;
24197   location_t loc_first;
24198   bool collapse_err = false;
24199   int i, collapse = 1, nbraces = 0;
24200   VEC(tree,gc) *for_block = make_tree_vector ();
24201
24202   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24203     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24204       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24205
24206   gcc_assert (collapse >= 1);
24207
24208   declv = make_tree_vec (collapse);
24209   initv = make_tree_vec (collapse);
24210   condv = make_tree_vec (collapse);
24211   incrv = make_tree_vec (collapse);
24212
24213   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24214
24215   for (i = 0; i < collapse; i++)
24216     {
24217       int bracecount = 0;
24218       bool add_private_clause = false;
24219       location_t loc;
24220
24221       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24222         {
24223           cp_parser_error (parser, "for statement expected");
24224           return NULL;
24225         }
24226       loc = cp_lexer_consume_token (parser->lexer)->location;
24227
24228       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24229         return NULL;
24230
24231       init = decl = real_decl = NULL;
24232       this_pre_body = push_stmt_list ();
24233       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24234         {
24235           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24236
24237              init-expr:
24238                        var = lb
24239                        integer-type var = lb
24240                        random-access-iterator-type var = lb
24241                        pointer-type var = lb
24242           */
24243           cp_decl_specifier_seq type_specifiers;
24244
24245           /* First, try to parse as an initialized declaration.  See
24246              cp_parser_condition, from whence the bulk of this is copied.  */
24247
24248           cp_parser_parse_tentatively (parser);
24249           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24250                                         /*is_trailing_return=*/false,
24251                                         &type_specifiers);
24252           if (cp_parser_parse_definitely (parser))
24253             {
24254               /* If parsing a type specifier seq succeeded, then this
24255                  MUST be a initialized declaration.  */
24256               tree asm_specification, attributes;
24257               cp_declarator *declarator;
24258
24259               declarator = cp_parser_declarator (parser,
24260                                                  CP_PARSER_DECLARATOR_NAMED,
24261                                                  /*ctor_dtor_or_conv_p=*/NULL,
24262                                                  /*parenthesized_p=*/NULL,
24263                                                  /*member_p=*/false);
24264               attributes = cp_parser_attributes_opt (parser);
24265               asm_specification = cp_parser_asm_specification_opt (parser);
24266
24267               if (declarator == cp_error_declarator) 
24268                 cp_parser_skip_to_end_of_statement (parser);
24269
24270               else 
24271                 {
24272                   tree pushed_scope, auto_node;
24273
24274                   decl = start_decl (declarator, &type_specifiers,
24275                                      SD_INITIALIZED, attributes,
24276                                      /*prefix_attributes=*/NULL_TREE,
24277                                      &pushed_scope);
24278
24279                   auto_node = type_uses_auto (TREE_TYPE (decl));
24280                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24281                     {
24282                       if (cp_lexer_next_token_is (parser->lexer, 
24283                                                   CPP_OPEN_PAREN))
24284                         error ("parenthesized initialization is not allowed in "
24285                                "OpenMP %<for%> loop");
24286                       else
24287                         /* Trigger an error.  */
24288                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24289
24290                       init = error_mark_node;
24291                       cp_parser_skip_to_end_of_statement (parser);
24292                     }
24293                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24294                            || type_dependent_expression_p (decl)
24295                            || auto_node)
24296                     {
24297                       bool is_direct_init, is_non_constant_init;
24298
24299                       init = cp_parser_initializer (parser,
24300                                                     &is_direct_init,
24301                                                     &is_non_constant_init);
24302
24303                       if (auto_node && describable_type (init))
24304                         {
24305                           TREE_TYPE (decl)
24306                             = do_auto_deduction (TREE_TYPE (decl), init,
24307                                                  auto_node);
24308
24309                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24310                               && !type_dependent_expression_p (decl))
24311                             goto non_class;
24312                         }
24313                       
24314                       cp_finish_decl (decl, init, !is_non_constant_init,
24315                                       asm_specification,
24316                                       LOOKUP_ONLYCONVERTING);
24317                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24318                         {
24319                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24320                           init = NULL_TREE;
24321                         }
24322                       else
24323                         init = pop_stmt_list (this_pre_body);
24324                       this_pre_body = NULL_TREE;
24325                     }
24326                   else
24327                     {
24328                       /* Consume '='.  */
24329                       cp_lexer_consume_token (parser->lexer);
24330                       init = cp_parser_assignment_expression (parser, false, NULL);
24331
24332                     non_class:
24333                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24334                         init = error_mark_node;
24335                       else
24336                         cp_finish_decl (decl, NULL_TREE,
24337                                         /*init_const_expr_p=*/false,
24338                                         asm_specification,
24339                                         LOOKUP_ONLYCONVERTING);
24340                     }
24341
24342                   if (pushed_scope)
24343                     pop_scope (pushed_scope);
24344                 }
24345             }
24346           else 
24347             {
24348               cp_id_kind idk;
24349               /* If parsing a type specifier sequence failed, then
24350                  this MUST be a simple expression.  */
24351               cp_parser_parse_tentatively (parser);
24352               decl = cp_parser_primary_expression (parser, false, false,
24353                                                    false, &idk);
24354               if (!cp_parser_error_occurred (parser)
24355                   && decl
24356                   && DECL_P (decl)
24357                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24358                 {
24359                   tree rhs;
24360
24361                   cp_parser_parse_definitely (parser);
24362                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24363                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24364                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24365                                                          rhs,
24366                                                          tf_warning_or_error));
24367                   add_private_clause = true;
24368                 }
24369               else
24370                 {
24371                   decl = NULL;
24372                   cp_parser_abort_tentative_parse (parser);
24373                   init = cp_parser_expression (parser, false, NULL);
24374                   if (init)
24375                     {
24376                       if (TREE_CODE (init) == MODIFY_EXPR
24377                           || TREE_CODE (init) == MODOP_EXPR)
24378                         real_decl = TREE_OPERAND (init, 0);
24379                     }
24380                 }
24381             }
24382         }
24383       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24384       if (this_pre_body)
24385         {
24386           this_pre_body = pop_stmt_list (this_pre_body);
24387           if (pre_body)
24388             {
24389               tree t = pre_body;
24390               pre_body = push_stmt_list ();
24391               add_stmt (t);
24392               add_stmt (this_pre_body);
24393               pre_body = pop_stmt_list (pre_body);
24394             }
24395           else
24396             pre_body = this_pre_body;
24397         }
24398
24399       if (decl)
24400         real_decl = decl;
24401       if (par_clauses != NULL && real_decl != NULL_TREE)
24402         {
24403           tree *c;
24404           for (c = par_clauses; *c ; )
24405             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24406                 && OMP_CLAUSE_DECL (*c) == real_decl)
24407               {
24408                 error_at (loc, "iteration variable %qD"
24409                           " should not be firstprivate", real_decl);
24410                 *c = OMP_CLAUSE_CHAIN (*c);
24411               }
24412             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24413                      && OMP_CLAUSE_DECL (*c) == real_decl)
24414               {
24415                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24416                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24417                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24418                 OMP_CLAUSE_DECL (l) = real_decl;
24419                 OMP_CLAUSE_CHAIN (l) = clauses;
24420                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24421                 clauses = l;
24422                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24423                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24424                 add_private_clause = false;
24425               }
24426             else
24427               {
24428                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24429                     && OMP_CLAUSE_DECL (*c) == real_decl)
24430                   add_private_clause = false;
24431                 c = &OMP_CLAUSE_CHAIN (*c);
24432               }
24433         }
24434
24435       if (add_private_clause)
24436         {
24437           tree c;
24438           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24439             {
24440               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24441                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24442                   && OMP_CLAUSE_DECL (c) == decl)
24443                 break;
24444               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24445                        && OMP_CLAUSE_DECL (c) == decl)
24446                 error_at (loc, "iteration variable %qD "
24447                           "should not be firstprivate",
24448                           decl);
24449               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24450                        && OMP_CLAUSE_DECL (c) == decl)
24451                 error_at (loc, "iteration variable %qD should not be reduction",
24452                           decl);
24453             }
24454           if (c == NULL)
24455             {
24456               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24457               OMP_CLAUSE_DECL (c) = decl;
24458               c = finish_omp_clauses (c);
24459               if (c)
24460                 {
24461                   OMP_CLAUSE_CHAIN (c) = clauses;
24462                   clauses = c;
24463                 }
24464             }
24465         }
24466
24467       cond = NULL;
24468       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24469         cond = cp_parser_omp_for_cond (parser, decl);
24470       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24471
24472       incr = NULL;
24473       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24474         {
24475           /* If decl is an iterator, preserve the operator on decl
24476              until finish_omp_for.  */
24477           if (decl
24478               && (type_dependent_expression_p (decl)
24479                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24480             incr = cp_parser_omp_for_incr (parser, decl);
24481           else
24482             incr = cp_parser_expression (parser, false, NULL);
24483         }
24484
24485       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24486         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24487                                                /*or_comma=*/false,
24488                                                /*consume_paren=*/true);
24489
24490       TREE_VEC_ELT (declv, i) = decl;
24491       TREE_VEC_ELT (initv, i) = init;
24492       TREE_VEC_ELT (condv, i) = cond;
24493       TREE_VEC_ELT (incrv, i) = incr;
24494
24495       if (i == collapse - 1)
24496         break;
24497
24498       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24499          in between the collapsed for loops to be still considered perfectly
24500          nested.  Hopefully the final version clarifies this.
24501          For now handle (multiple) {'s and empty statements.  */
24502       cp_parser_parse_tentatively (parser);
24503       do
24504         {
24505           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24506             break;
24507           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24508             {
24509               cp_lexer_consume_token (parser->lexer);
24510               bracecount++;
24511             }
24512           else if (bracecount
24513                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24514             cp_lexer_consume_token (parser->lexer);
24515           else
24516             {
24517               loc = cp_lexer_peek_token (parser->lexer)->location;
24518               error_at (loc, "not enough collapsed for loops");
24519               collapse_err = true;
24520               cp_parser_abort_tentative_parse (parser);
24521               declv = NULL_TREE;
24522               break;
24523             }
24524         }
24525       while (1);
24526
24527       if (declv)
24528         {
24529           cp_parser_parse_definitely (parser);
24530           nbraces += bracecount;
24531         }
24532     }
24533
24534   /* Note that we saved the original contents of this flag when we entered
24535      the structured block, and so we don't need to re-save it here.  */
24536   parser->in_statement = IN_OMP_FOR;
24537
24538   /* Note that the grammar doesn't call for a structured block here,
24539      though the loop as a whole is a structured block.  */
24540   body = push_stmt_list ();
24541   cp_parser_statement (parser, NULL_TREE, false, NULL);
24542   body = pop_stmt_list (body);
24543
24544   if (declv == NULL_TREE)
24545     ret = NULL_TREE;
24546   else
24547     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24548                           pre_body, clauses);
24549
24550   while (nbraces)
24551     {
24552       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24553         {
24554           cp_lexer_consume_token (parser->lexer);
24555           nbraces--;
24556         }
24557       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24558         cp_lexer_consume_token (parser->lexer);
24559       else
24560         {
24561           if (!collapse_err)
24562             {
24563               error_at (cp_lexer_peek_token (parser->lexer)->location,
24564                         "collapsed loops not perfectly nested");
24565             }
24566           collapse_err = true;
24567           cp_parser_statement_seq_opt (parser, NULL);
24568           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24569             break;
24570         }
24571     }
24572
24573   while (!VEC_empty (tree, for_block))
24574     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24575   release_tree_vector (for_block);
24576
24577   return ret;
24578 }
24579
24580 /* OpenMP 2.5:
24581    #pragma omp for for-clause[optseq] new-line
24582      for-loop  */
24583
24584 #define OMP_FOR_CLAUSE_MASK                             \
24585         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24586         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24587         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24588         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24589         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24590         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24591         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24592         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24593
24594 static tree
24595 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24596 {
24597   tree clauses, sb, ret;
24598   unsigned int save;
24599
24600   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24601                                        "#pragma omp for", pragma_tok);
24602
24603   sb = begin_omp_structured_block ();
24604   save = cp_parser_begin_omp_structured_block (parser);
24605
24606   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24607
24608   cp_parser_end_omp_structured_block (parser, save);
24609   add_stmt (finish_omp_structured_block (sb));
24610
24611   return ret;
24612 }
24613
24614 /* OpenMP 2.5:
24615    # pragma omp master new-line
24616      structured-block  */
24617
24618 static tree
24619 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24620 {
24621   cp_parser_require_pragma_eol (parser, pragma_tok);
24622   return c_finish_omp_master (input_location,
24623                               cp_parser_omp_structured_block (parser));
24624 }
24625
24626 /* OpenMP 2.5:
24627    # pragma omp ordered new-line
24628      structured-block  */
24629
24630 static tree
24631 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24632 {
24633   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24634   cp_parser_require_pragma_eol (parser, pragma_tok);
24635   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24636 }
24637
24638 /* OpenMP 2.5:
24639
24640    section-scope:
24641      { section-sequence }
24642
24643    section-sequence:
24644      section-directive[opt] structured-block
24645      section-sequence section-directive structured-block  */
24646
24647 static tree
24648 cp_parser_omp_sections_scope (cp_parser *parser)
24649 {
24650   tree stmt, substmt;
24651   bool error_suppress = false;
24652   cp_token *tok;
24653
24654   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24655     return NULL_TREE;
24656
24657   stmt = push_stmt_list ();
24658
24659   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24660     {
24661       unsigned save;
24662
24663       substmt = begin_omp_structured_block ();
24664       save = cp_parser_begin_omp_structured_block (parser);
24665
24666       while (1)
24667         {
24668           cp_parser_statement (parser, NULL_TREE, false, NULL);
24669
24670           tok = cp_lexer_peek_token (parser->lexer);
24671           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24672             break;
24673           if (tok->type == CPP_CLOSE_BRACE)
24674             break;
24675           if (tok->type == CPP_EOF)
24676             break;
24677         }
24678
24679       cp_parser_end_omp_structured_block (parser, save);
24680       substmt = finish_omp_structured_block (substmt);
24681       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24682       add_stmt (substmt);
24683     }
24684
24685   while (1)
24686     {
24687       tok = cp_lexer_peek_token (parser->lexer);
24688       if (tok->type == CPP_CLOSE_BRACE)
24689         break;
24690       if (tok->type == CPP_EOF)
24691         break;
24692
24693       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24694         {
24695           cp_lexer_consume_token (parser->lexer);
24696           cp_parser_require_pragma_eol (parser, tok);
24697           error_suppress = false;
24698         }
24699       else if (!error_suppress)
24700         {
24701           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24702           error_suppress = true;
24703         }
24704
24705       substmt = cp_parser_omp_structured_block (parser);
24706       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24707       add_stmt (substmt);
24708     }
24709   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24710
24711   substmt = pop_stmt_list (stmt);
24712
24713   stmt = make_node (OMP_SECTIONS);
24714   TREE_TYPE (stmt) = void_type_node;
24715   OMP_SECTIONS_BODY (stmt) = substmt;
24716
24717   add_stmt (stmt);
24718   return stmt;
24719 }
24720
24721 /* OpenMP 2.5:
24722    # pragma omp sections sections-clause[optseq] newline
24723      sections-scope  */
24724
24725 #define OMP_SECTIONS_CLAUSE_MASK                        \
24726         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24727         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24728         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24729         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24730         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24731
24732 static tree
24733 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24734 {
24735   tree clauses, ret;
24736
24737   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24738                                        "#pragma omp sections", pragma_tok);
24739
24740   ret = cp_parser_omp_sections_scope (parser);
24741   if (ret)
24742     OMP_SECTIONS_CLAUSES (ret) = clauses;
24743
24744   return ret;
24745 }
24746
24747 /* OpenMP 2.5:
24748    # pragma parallel parallel-clause new-line
24749    # pragma parallel for parallel-for-clause new-line
24750    # pragma parallel sections parallel-sections-clause new-line  */
24751
24752 #define OMP_PARALLEL_CLAUSE_MASK                        \
24753         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24754         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24755         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24756         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24757         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24758         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24759         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24760         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24761
24762 static tree
24763 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24764 {
24765   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24766   const char *p_name = "#pragma omp parallel";
24767   tree stmt, clauses, par_clause, ws_clause, block;
24768   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24769   unsigned int save;
24770   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24771
24772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24773     {
24774       cp_lexer_consume_token (parser->lexer);
24775       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24776       p_name = "#pragma omp parallel for";
24777       mask |= OMP_FOR_CLAUSE_MASK;
24778       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24779     }
24780   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24781     {
24782       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24783       const char *p = IDENTIFIER_POINTER (id);
24784       if (strcmp (p, "sections") == 0)
24785         {
24786           cp_lexer_consume_token (parser->lexer);
24787           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24788           p_name = "#pragma omp parallel sections";
24789           mask |= OMP_SECTIONS_CLAUSE_MASK;
24790           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24791         }
24792     }
24793
24794   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24795   block = begin_omp_parallel ();
24796   save = cp_parser_begin_omp_structured_block (parser);
24797
24798   switch (p_kind)
24799     {
24800     case PRAGMA_OMP_PARALLEL:
24801       cp_parser_statement (parser, NULL_TREE, false, NULL);
24802       par_clause = clauses;
24803       break;
24804
24805     case PRAGMA_OMP_PARALLEL_FOR:
24806       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24807       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24808       break;
24809
24810     case PRAGMA_OMP_PARALLEL_SECTIONS:
24811       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24812       stmt = cp_parser_omp_sections_scope (parser);
24813       if (stmt)
24814         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24815       break;
24816
24817     default:
24818       gcc_unreachable ();
24819     }
24820
24821   cp_parser_end_omp_structured_block (parser, save);
24822   stmt = finish_omp_parallel (par_clause, block);
24823   if (p_kind != PRAGMA_OMP_PARALLEL)
24824     OMP_PARALLEL_COMBINED (stmt) = 1;
24825   return stmt;
24826 }
24827
24828 /* OpenMP 2.5:
24829    # pragma omp single single-clause[optseq] new-line
24830      structured-block  */
24831
24832 #define OMP_SINGLE_CLAUSE_MASK                          \
24833         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24834         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24835         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24836         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24837
24838 static tree
24839 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24840 {
24841   tree stmt = make_node (OMP_SINGLE);
24842   TREE_TYPE (stmt) = void_type_node;
24843
24844   OMP_SINGLE_CLAUSES (stmt)
24845     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24846                                  "#pragma omp single", pragma_tok);
24847   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24848
24849   return add_stmt (stmt);
24850 }
24851
24852 /* OpenMP 3.0:
24853    # pragma omp task task-clause[optseq] new-line
24854      structured-block  */
24855
24856 #define OMP_TASK_CLAUSE_MASK                            \
24857         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24858         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24859         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24860         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24861         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24862         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24863
24864 static tree
24865 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24866 {
24867   tree clauses, block;
24868   unsigned int save;
24869
24870   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24871                                        "#pragma omp task", pragma_tok);
24872   block = begin_omp_task ();
24873   save = cp_parser_begin_omp_structured_block (parser);
24874   cp_parser_statement (parser, NULL_TREE, false, NULL);
24875   cp_parser_end_omp_structured_block (parser, save);
24876   return finish_omp_task (clauses, block);
24877 }
24878
24879 /* OpenMP 3.0:
24880    # pragma omp taskwait new-line  */
24881
24882 static void
24883 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24884 {
24885   cp_parser_require_pragma_eol (parser, pragma_tok);
24886   finish_omp_taskwait ();
24887 }
24888
24889 /* OpenMP 2.5:
24890    # pragma omp threadprivate (variable-list) */
24891
24892 static void
24893 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24894 {
24895   tree vars;
24896
24897   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24898   cp_parser_require_pragma_eol (parser, pragma_tok);
24899
24900   finish_omp_threadprivate (vars);
24901 }
24902
24903 /* Main entry point to OpenMP statement pragmas.  */
24904
24905 static void
24906 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24907 {
24908   tree stmt;
24909
24910   switch (pragma_tok->pragma_kind)
24911     {
24912     case PRAGMA_OMP_ATOMIC:
24913       cp_parser_omp_atomic (parser, pragma_tok);
24914       return;
24915     case PRAGMA_OMP_CRITICAL:
24916       stmt = cp_parser_omp_critical (parser, pragma_tok);
24917       break;
24918     case PRAGMA_OMP_FOR:
24919       stmt = cp_parser_omp_for (parser, pragma_tok);
24920       break;
24921     case PRAGMA_OMP_MASTER:
24922       stmt = cp_parser_omp_master (parser, pragma_tok);
24923       break;
24924     case PRAGMA_OMP_ORDERED:
24925       stmt = cp_parser_omp_ordered (parser, pragma_tok);
24926       break;
24927     case PRAGMA_OMP_PARALLEL:
24928       stmt = cp_parser_omp_parallel (parser, pragma_tok);
24929       break;
24930     case PRAGMA_OMP_SECTIONS:
24931       stmt = cp_parser_omp_sections (parser, pragma_tok);
24932       break;
24933     case PRAGMA_OMP_SINGLE:
24934       stmt = cp_parser_omp_single (parser, pragma_tok);
24935       break;
24936     case PRAGMA_OMP_TASK:
24937       stmt = cp_parser_omp_task (parser, pragma_tok);
24938       break;
24939     default:
24940       gcc_unreachable ();
24941     }
24942
24943   if (stmt)
24944     SET_EXPR_LOCATION (stmt, pragma_tok->location);
24945 }
24946 \f
24947 /* The parser.  */
24948
24949 static GTY (()) cp_parser *the_parser;
24950
24951 \f
24952 /* Special handling for the first token or line in the file.  The first
24953    thing in the file might be #pragma GCC pch_preprocess, which loads a
24954    PCH file, which is a GC collection point.  So we need to handle this
24955    first pragma without benefit of an existing lexer structure.
24956
24957    Always returns one token to the caller in *FIRST_TOKEN.  This is
24958    either the true first token of the file, or the first token after
24959    the initial pragma.  */
24960
24961 static void
24962 cp_parser_initial_pragma (cp_token *first_token)
24963 {
24964   tree name = NULL;
24965
24966   cp_lexer_get_preprocessor_token (NULL, first_token);
24967   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24968     return;
24969
24970   cp_lexer_get_preprocessor_token (NULL, first_token);
24971   if (first_token->type == CPP_STRING)
24972     {
24973       name = first_token->u.value;
24974
24975       cp_lexer_get_preprocessor_token (NULL, first_token);
24976       if (first_token->type != CPP_PRAGMA_EOL)
24977         error_at (first_token->location,
24978                   "junk at end of %<#pragma GCC pch_preprocess%>");
24979     }
24980   else
24981     error_at (first_token->location, "expected string literal");
24982
24983   /* Skip to the end of the pragma.  */
24984   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24985     cp_lexer_get_preprocessor_token (NULL, first_token);
24986
24987   /* Now actually load the PCH file.  */
24988   if (name)
24989     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24990
24991   /* Read one more token to return to our caller.  We have to do this
24992      after reading the PCH file in, since its pointers have to be
24993      live.  */
24994   cp_lexer_get_preprocessor_token (NULL, first_token);
24995 }
24996
24997 /* Normal parsing of a pragma token.  Here we can (and must) use the
24998    regular lexer.  */
24999
25000 static bool
25001 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25002 {
25003   cp_token *pragma_tok;
25004   unsigned int id;
25005
25006   pragma_tok = cp_lexer_consume_token (parser->lexer);
25007   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25008   parser->lexer->in_pragma = true;
25009
25010   id = pragma_tok->pragma_kind;
25011   switch (id)
25012     {
25013     case PRAGMA_GCC_PCH_PREPROCESS:
25014       error_at (pragma_tok->location,
25015                 "%<#pragma GCC pch_preprocess%> must be first");
25016       break;
25017
25018     case PRAGMA_OMP_BARRIER:
25019       switch (context)
25020         {
25021         case pragma_compound:
25022           cp_parser_omp_barrier (parser, pragma_tok);
25023           return false;
25024         case pragma_stmt:
25025           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25026                     "used in compound statements");
25027           break;
25028         default:
25029           goto bad_stmt;
25030         }
25031       break;
25032
25033     case PRAGMA_OMP_FLUSH:
25034       switch (context)
25035         {
25036         case pragma_compound:
25037           cp_parser_omp_flush (parser, pragma_tok);
25038           return false;
25039         case pragma_stmt:
25040           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25041                     "used in compound statements");
25042           break;
25043         default:
25044           goto bad_stmt;
25045         }
25046       break;
25047
25048     case PRAGMA_OMP_TASKWAIT:
25049       switch (context)
25050         {
25051         case pragma_compound:
25052           cp_parser_omp_taskwait (parser, pragma_tok);
25053           return false;
25054         case pragma_stmt:
25055           error_at (pragma_tok->location,
25056                     "%<#pragma omp taskwait%> may only be "
25057                     "used in compound statements");
25058           break;
25059         default:
25060           goto bad_stmt;
25061         }
25062       break;
25063
25064     case PRAGMA_OMP_THREADPRIVATE:
25065       cp_parser_omp_threadprivate (parser, pragma_tok);
25066       return false;
25067
25068     case PRAGMA_OMP_ATOMIC:
25069     case PRAGMA_OMP_CRITICAL:
25070     case PRAGMA_OMP_FOR:
25071     case PRAGMA_OMP_MASTER:
25072     case PRAGMA_OMP_ORDERED:
25073     case PRAGMA_OMP_PARALLEL:
25074     case PRAGMA_OMP_SECTIONS:
25075     case PRAGMA_OMP_SINGLE:
25076     case PRAGMA_OMP_TASK:
25077       if (context == pragma_external)
25078         goto bad_stmt;
25079       cp_parser_omp_construct (parser, pragma_tok);
25080       return true;
25081
25082     case PRAGMA_OMP_SECTION:
25083       error_at (pragma_tok->location, 
25084                 "%<#pragma omp section%> may only be used in "
25085                 "%<#pragma omp sections%> construct");
25086       break;
25087
25088     default:
25089       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25090       c_invoke_pragma_handler (id);
25091       break;
25092
25093     bad_stmt:
25094       cp_parser_error (parser, "expected declaration specifiers");
25095       break;
25096     }
25097
25098   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25099   return false;
25100 }
25101
25102 /* The interface the pragma parsers have to the lexer.  */
25103
25104 enum cpp_ttype
25105 pragma_lex (tree *value)
25106 {
25107   cp_token *tok;
25108   enum cpp_ttype ret;
25109
25110   tok = cp_lexer_peek_token (the_parser->lexer);
25111
25112   ret = tok->type;
25113   *value = tok->u.value;
25114
25115   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25116     ret = CPP_EOF;
25117   else if (ret == CPP_STRING)
25118     *value = cp_parser_string_literal (the_parser, false, false);
25119   else
25120     {
25121       cp_lexer_consume_token (the_parser->lexer);
25122       if (ret == CPP_KEYWORD)
25123         ret = CPP_NAME;
25124     }
25125
25126   return ret;
25127 }
25128
25129 \f
25130 /* External interface.  */
25131
25132 /* Parse one entire translation unit.  */
25133
25134 void
25135 c_parse_file (void)
25136 {
25137   static bool already_called = false;
25138
25139   if (already_called)
25140     {
25141       sorry ("inter-module optimizations not implemented for C++");
25142       return;
25143     }
25144   already_called = true;
25145
25146   the_parser = cp_parser_new ();
25147   push_deferring_access_checks (flag_access_control
25148                                 ? dk_no_deferred : dk_no_check);
25149   cp_parser_translation_unit (the_parser);
25150   the_parser = NULL;
25151 }
25152
25153 #include "gt-cp-parser.h"