OSDN Git Service

c63d5b3a0498f536c6dfb9a6fb509d902d46a07f
[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   /* TRUE if we can auto-correct a colon to a scope operator.  */
1704   bool colon_corrects_to_scope_p;
1705
1706   /* If non-NULL, then we are parsing a construct where new type
1707      definitions are not permitted.  The string stored here will be
1708      issued as an error message if a type is defined.  */
1709   const char *type_definition_forbidden_message;
1710
1711   /* A stack used for member functions of local classes.  The lists
1712      contained in an individual entry can only be processed once the
1713      outermost class being defined is complete.  */
1714   VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1715
1716   /* The number of classes whose definitions are currently in
1717      progress.  */
1718   unsigned num_classes_being_defined;
1719
1720   /* The number of template parameter lists that apply directly to the
1721      current declaration.  */
1722   unsigned num_template_parameter_lists;
1723 } cp_parser;
1724
1725 /* Managing the unparsed function queues.  */
1726
1727 #define unparsed_funs_with_default_args \
1728   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1729 #define unparsed_funs_with_definitions \
1730   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1731
1732 static void
1733 push_unparsed_function_queues (cp_parser *parser)
1734 {
1735   VEC_safe_push (cp_unparsed_functions_entry, gc,
1736                  parser->unparsed_queues, NULL);
1737   unparsed_funs_with_default_args = NULL;
1738   unparsed_funs_with_definitions = make_tree_vector ();
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773
1774 /* Basic concepts [gram.basic]  */
1775
1776 static bool cp_parser_translation_unit
1777   (cp_parser *);
1778
1779 /* Expressions [gram.expr]  */
1780
1781 static tree cp_parser_primary_expression
1782   (cp_parser *, bool, bool, bool, cp_id_kind *);
1783 static tree cp_parser_id_expression
1784   (cp_parser *, bool, bool, bool *, bool, bool);
1785 static tree cp_parser_unqualified_id
1786   (cp_parser *, bool, bool, bool, bool);
1787 static tree cp_parser_nested_name_specifier_opt
1788   (cp_parser *, bool, bool, bool, bool);
1789 static tree cp_parser_nested_name_specifier
1790   (cp_parser *, bool, bool, bool, bool);
1791 static tree cp_parser_qualifying_entity
1792   (cp_parser *, bool, bool, bool, bool, bool);
1793 static tree cp_parser_postfix_expression
1794   (cp_parser *, bool, bool, bool, cp_id_kind *);
1795 static tree cp_parser_postfix_open_square_expression
1796   (cp_parser *, tree, bool);
1797 static tree cp_parser_postfix_dot_deref_expression
1798   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1799 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1800   (cp_parser *, int, bool, bool, bool *);
1801 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1802 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1803 static void cp_parser_pseudo_destructor_name
1804   (cp_parser *, tree *, tree *);
1805 static tree cp_parser_unary_expression
1806   (cp_parser *, bool, bool, cp_id_kind *);
1807 static enum tree_code cp_parser_unary_operator
1808   (cp_token *);
1809 static tree cp_parser_new_expression
1810   (cp_parser *);
1811 static VEC(tree,gc) *cp_parser_new_placement
1812   (cp_parser *);
1813 static tree cp_parser_new_type_id
1814   (cp_parser *, tree *);
1815 static cp_declarator *cp_parser_new_declarator_opt
1816   (cp_parser *);
1817 static cp_declarator *cp_parser_direct_new_declarator
1818   (cp_parser *);
1819 static VEC(tree,gc) *cp_parser_new_initializer
1820   (cp_parser *);
1821 static tree cp_parser_delete_expression
1822   (cp_parser *);
1823 static tree cp_parser_cast_expression
1824   (cp_parser *, bool, bool, cp_id_kind *);
1825 static tree cp_parser_binary_expression
1826   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1827 static tree cp_parser_question_colon_clause
1828   (cp_parser *, tree);
1829 static tree cp_parser_assignment_expression
1830   (cp_parser *, bool, cp_id_kind *);
1831 static enum tree_code cp_parser_assignment_operator_opt
1832   (cp_parser *);
1833 static tree cp_parser_expression
1834   (cp_parser *, bool, cp_id_kind *);
1835 static tree cp_parser_constant_expression
1836   (cp_parser *, bool, bool *);
1837 static tree cp_parser_builtin_offsetof
1838   (cp_parser *);
1839 static tree cp_parser_lambda_expression
1840   (cp_parser *);
1841 static void cp_parser_lambda_introducer
1842   (cp_parser *, tree);
1843 static void cp_parser_lambda_declarator_opt
1844   (cp_parser *, tree);
1845 static void cp_parser_lambda_body
1846   (cp_parser *, tree);
1847
1848 /* Statements [gram.stmt.stmt]  */
1849
1850 static void cp_parser_statement
1851   (cp_parser *, tree, bool, bool *);
1852 static void cp_parser_label_for_labeled_statement
1853   (cp_parser *);
1854 static tree cp_parser_expression_statement
1855   (cp_parser *, tree);
1856 static tree cp_parser_compound_statement
1857   (cp_parser *, tree, bool);
1858 static void cp_parser_statement_seq_opt
1859   (cp_parser *, tree);
1860 static tree cp_parser_selection_statement
1861   (cp_parser *, bool *);
1862 static tree cp_parser_condition
1863   (cp_parser *);
1864 static tree cp_parser_iteration_statement
1865   (cp_parser *);
1866 static bool cp_parser_for_init_statement
1867   (cp_parser *, tree *decl);
1868 static tree cp_parser_for
1869   (cp_parser *);
1870 static tree cp_parser_c_for
1871   (cp_parser *, tree, tree);
1872 static tree cp_parser_range_for
1873   (cp_parser *, tree, tree, tree);
1874 static tree cp_parser_jump_statement
1875   (cp_parser *);
1876 static void cp_parser_declaration_statement
1877   (cp_parser *);
1878
1879 static tree cp_parser_implicitly_scoped_statement
1880   (cp_parser *, bool *);
1881 static void cp_parser_already_scoped_statement
1882   (cp_parser *);
1883
1884 /* Declarations [gram.dcl.dcl] */
1885
1886 static void cp_parser_declaration_seq_opt
1887   (cp_parser *);
1888 static void cp_parser_declaration
1889   (cp_parser *);
1890 static void cp_parser_block_declaration
1891   (cp_parser *, bool);
1892 static void cp_parser_simple_declaration
1893   (cp_parser *, bool, tree *);
1894 static void cp_parser_decl_specifier_seq
1895   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1896 static tree cp_parser_storage_class_specifier_opt
1897   (cp_parser *);
1898 static tree cp_parser_function_specifier_opt
1899   (cp_parser *, cp_decl_specifier_seq *);
1900 static tree cp_parser_type_specifier
1901   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1902    int *, bool *);
1903 static tree cp_parser_simple_type_specifier
1904   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1905 static tree cp_parser_type_name
1906   (cp_parser *);
1907 static tree cp_parser_nonclass_name 
1908   (cp_parser* parser);
1909 static tree cp_parser_elaborated_type_specifier
1910   (cp_parser *, bool, bool);
1911 static tree cp_parser_enum_specifier
1912   (cp_parser *);
1913 static void cp_parser_enumerator_list
1914   (cp_parser *, tree);
1915 static void cp_parser_enumerator_definition
1916   (cp_parser *, tree);
1917 static tree cp_parser_namespace_name
1918   (cp_parser *);
1919 static void cp_parser_namespace_definition
1920   (cp_parser *);
1921 static void cp_parser_namespace_body
1922   (cp_parser *);
1923 static tree cp_parser_qualified_namespace_specifier
1924   (cp_parser *);
1925 static void cp_parser_namespace_alias_definition
1926   (cp_parser *);
1927 static bool cp_parser_using_declaration
1928   (cp_parser *, bool);
1929 static void cp_parser_using_directive
1930   (cp_parser *);
1931 static void cp_parser_asm_definition
1932   (cp_parser *);
1933 static void cp_parser_linkage_specification
1934   (cp_parser *);
1935 static void cp_parser_static_assert
1936   (cp_parser *, bool);
1937 static tree cp_parser_decltype
1938   (cp_parser *);
1939
1940 /* Declarators [gram.dcl.decl] */
1941
1942 static tree cp_parser_init_declarator
1943   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1944 static cp_declarator *cp_parser_declarator
1945   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1946 static cp_declarator *cp_parser_direct_declarator
1947   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1948 static enum tree_code cp_parser_ptr_operator
1949   (cp_parser *, tree *, cp_cv_quals *);
1950 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1951   (cp_parser *);
1952 static tree cp_parser_late_return_type_opt
1953   (cp_parser *);
1954 static tree cp_parser_declarator_id
1955   (cp_parser *, bool);
1956 static tree cp_parser_type_id
1957   (cp_parser *);
1958 static tree cp_parser_template_type_arg
1959   (cp_parser *);
1960 static tree cp_parser_trailing_type_id (cp_parser *);
1961 static tree cp_parser_type_id_1
1962   (cp_parser *, bool, bool);
1963 static void cp_parser_type_specifier_seq
1964   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1965 static tree cp_parser_parameter_declaration_clause
1966   (cp_parser *);
1967 static tree cp_parser_parameter_declaration_list
1968   (cp_parser *, bool *);
1969 static cp_parameter_declarator *cp_parser_parameter_declaration
1970   (cp_parser *, bool, bool *);
1971 static tree cp_parser_default_argument 
1972   (cp_parser *, bool);
1973 static void cp_parser_function_body
1974   (cp_parser *);
1975 static tree cp_parser_initializer
1976   (cp_parser *, bool *, bool *);
1977 static tree cp_parser_initializer_clause
1978   (cp_parser *, bool *);
1979 static tree cp_parser_braced_list
1980   (cp_parser*, bool*);
1981 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1982   (cp_parser *, bool *);
1983
1984 static bool cp_parser_ctor_initializer_opt_and_function_body
1985   (cp_parser *);
1986
1987 /* Classes [gram.class] */
1988
1989 static tree cp_parser_class_name
1990   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1991 static tree cp_parser_class_specifier
1992   (cp_parser *);
1993 static tree cp_parser_class_head
1994   (cp_parser *, bool *, tree *, tree *);
1995 static enum tag_types cp_parser_class_key
1996   (cp_parser *);
1997 static void cp_parser_member_specification_opt
1998   (cp_parser *);
1999 static void cp_parser_member_declaration
2000   (cp_parser *);
2001 static tree cp_parser_pure_specifier
2002   (cp_parser *);
2003 static tree cp_parser_constant_initializer
2004   (cp_parser *);
2005
2006 /* Derived classes [gram.class.derived] */
2007
2008 static tree cp_parser_base_clause
2009   (cp_parser *);
2010 static tree cp_parser_base_specifier
2011   (cp_parser *);
2012
2013 /* Special member functions [gram.special] */
2014
2015 static tree cp_parser_conversion_function_id
2016   (cp_parser *);
2017 static tree cp_parser_conversion_type_id
2018   (cp_parser *);
2019 static cp_declarator *cp_parser_conversion_declarator_opt
2020   (cp_parser *);
2021 static bool cp_parser_ctor_initializer_opt
2022   (cp_parser *);
2023 static void cp_parser_mem_initializer_list
2024   (cp_parser *);
2025 static tree cp_parser_mem_initializer
2026   (cp_parser *);
2027 static tree cp_parser_mem_initializer_id
2028   (cp_parser *);
2029
2030 /* Overloading [gram.over] */
2031
2032 static tree cp_parser_operator_function_id
2033   (cp_parser *);
2034 static tree cp_parser_operator
2035   (cp_parser *);
2036
2037 /* Templates [gram.temp] */
2038
2039 static void cp_parser_template_declaration
2040   (cp_parser *, bool);
2041 static tree cp_parser_template_parameter_list
2042   (cp_parser *);
2043 static tree cp_parser_template_parameter
2044   (cp_parser *, bool *, bool *);
2045 static tree cp_parser_type_parameter
2046   (cp_parser *, bool *);
2047 static tree cp_parser_template_id
2048   (cp_parser *, bool, bool, bool);
2049 static tree cp_parser_template_name
2050   (cp_parser *, bool, bool, bool, bool *);
2051 static tree cp_parser_template_argument_list
2052   (cp_parser *);
2053 static tree cp_parser_template_argument
2054   (cp_parser *);
2055 static void cp_parser_explicit_instantiation
2056   (cp_parser *);
2057 static void cp_parser_explicit_specialization
2058   (cp_parser *);
2059
2060 /* Exception handling [gram.exception] */
2061
2062 static tree cp_parser_try_block
2063   (cp_parser *);
2064 static bool cp_parser_function_try_block
2065   (cp_parser *);
2066 static void cp_parser_handler_seq
2067   (cp_parser *);
2068 static void cp_parser_handler
2069   (cp_parser *);
2070 static tree cp_parser_exception_declaration
2071   (cp_parser *);
2072 static tree cp_parser_throw_expression
2073   (cp_parser *);
2074 static tree cp_parser_exception_specification_opt
2075   (cp_parser *);
2076 static tree cp_parser_type_id_list
2077   (cp_parser *);
2078
2079 /* GNU Extensions */
2080
2081 static tree cp_parser_asm_specification_opt
2082   (cp_parser *);
2083 static tree cp_parser_asm_operand_list
2084   (cp_parser *);
2085 static tree cp_parser_asm_clobber_list
2086   (cp_parser *);
2087 static tree cp_parser_asm_label_list
2088   (cp_parser *);
2089 static tree cp_parser_attributes_opt
2090   (cp_parser *);
2091 static tree cp_parser_attribute_list
2092   (cp_parser *);
2093 static bool cp_parser_extension_opt
2094   (cp_parser *, int *);
2095 static void cp_parser_label_declaration
2096   (cp_parser *);
2097
2098 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2099 static bool cp_parser_pragma
2100   (cp_parser *, enum pragma_context);
2101
2102 /* Objective-C++ Productions */
2103
2104 static tree cp_parser_objc_message_receiver
2105   (cp_parser *);
2106 static tree cp_parser_objc_message_args
2107   (cp_parser *);
2108 static tree cp_parser_objc_message_expression
2109   (cp_parser *);
2110 static tree cp_parser_objc_encode_expression
2111   (cp_parser *);
2112 static tree cp_parser_objc_defs_expression
2113   (cp_parser *);
2114 static tree cp_parser_objc_protocol_expression
2115   (cp_parser *);
2116 static tree cp_parser_objc_selector_expression
2117   (cp_parser *);
2118 static tree cp_parser_objc_expression
2119   (cp_parser *);
2120 static bool cp_parser_objc_selector_p
2121   (enum cpp_ttype);
2122 static tree cp_parser_objc_selector
2123   (cp_parser *);
2124 static tree cp_parser_objc_protocol_refs_opt
2125   (cp_parser *);
2126 static void cp_parser_objc_declaration
2127   (cp_parser *, tree);
2128 static tree cp_parser_objc_statement
2129   (cp_parser *);
2130 static bool cp_parser_objc_valid_prefix_attributes
2131   (cp_parser *, tree *);
2132 static void cp_parser_objc_at_property_declaration 
2133   (cp_parser *) ;
2134 static void cp_parser_objc_at_synthesize_declaration 
2135   (cp_parser *) ;
2136 static void cp_parser_objc_at_dynamic_declaration
2137   (cp_parser *) ;
2138 static tree cp_parser_objc_struct_declaration
2139   (cp_parser *) ;
2140
2141 /* Utility Routines */
2142
2143 static tree cp_parser_lookup_name
2144   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2145 static tree cp_parser_lookup_name_simple
2146   (cp_parser *, tree, location_t);
2147 static tree cp_parser_maybe_treat_template_as_class
2148   (tree, bool);
2149 static bool cp_parser_check_declarator_template_parameters
2150   (cp_parser *, cp_declarator *, location_t);
2151 static bool cp_parser_check_template_parameters
2152   (cp_parser *, unsigned, location_t, cp_declarator *);
2153 static tree cp_parser_simple_cast_expression
2154   (cp_parser *);
2155 static tree cp_parser_global_scope_opt
2156   (cp_parser *, bool);
2157 static bool cp_parser_constructor_declarator_p
2158   (cp_parser *, bool);
2159 static tree cp_parser_function_definition_from_specifiers_and_declarator
2160   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2161 static tree cp_parser_function_definition_after_declarator
2162   (cp_parser *, bool);
2163 static void cp_parser_template_declaration_after_export
2164   (cp_parser *, bool);
2165 static void cp_parser_perform_template_parameter_access_checks
2166   (VEC (deferred_access_check,gc)*);
2167 static tree cp_parser_single_declaration
2168   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2169 static tree cp_parser_functional_cast
2170   (cp_parser *, tree);
2171 static tree cp_parser_save_member_function_body
2172   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2173 static tree cp_parser_enclosed_template_argument_list
2174   (cp_parser *);
2175 static void cp_parser_save_default_args
2176   (cp_parser *, tree);
2177 static void cp_parser_late_parsing_for_member
2178   (cp_parser *, tree);
2179 static void cp_parser_late_parsing_default_args
2180   (cp_parser *, tree);
2181 static tree cp_parser_sizeof_operand
2182   (cp_parser *, enum rid);
2183 static tree cp_parser_trait_expr
2184   (cp_parser *, enum rid);
2185 static bool cp_parser_declares_only_class_p
2186   (cp_parser *);
2187 static void cp_parser_set_storage_class
2188   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2189 static void cp_parser_set_decl_spec_type
2190   (cp_decl_specifier_seq *, tree, location_t, bool);
2191 static bool cp_parser_friend_p
2192   (const cp_decl_specifier_seq *);
2193 static void cp_parser_required_error
2194   (cp_parser *, required_token, bool);
2195 static cp_token *cp_parser_require
2196   (cp_parser *, enum cpp_ttype, required_token);
2197 static cp_token *cp_parser_require_keyword
2198   (cp_parser *, enum rid, required_token);
2199 static bool cp_parser_token_starts_function_definition_p
2200   (cp_token *);
2201 static bool cp_parser_next_token_starts_class_definition_p
2202   (cp_parser *);
2203 static bool cp_parser_next_token_ends_template_argument_p
2204   (cp_parser *);
2205 static bool cp_parser_nth_token_starts_template_argument_list_p
2206   (cp_parser *, size_t);
2207 static enum tag_types cp_parser_token_is_class_key
2208   (cp_token *);
2209 static void cp_parser_check_class_key
2210   (enum tag_types, tree type);
2211 static void cp_parser_check_access_in_redeclaration
2212   (tree type, location_t location);
2213 static bool cp_parser_optional_template_keyword
2214   (cp_parser *);
2215 static void cp_parser_pre_parsed_nested_name_specifier
2216   (cp_parser *);
2217 static bool cp_parser_cache_group
2218   (cp_parser *, enum cpp_ttype, unsigned);
2219 static void cp_parser_parse_tentatively
2220   (cp_parser *);
2221 static void cp_parser_commit_to_tentative_parse
2222   (cp_parser *);
2223 static void cp_parser_abort_tentative_parse
2224   (cp_parser *);
2225 static bool cp_parser_parse_definitely
2226   (cp_parser *);
2227 static inline bool cp_parser_parsing_tentatively
2228   (cp_parser *);
2229 static bool cp_parser_uncommitted_to_tentative_parse_p
2230   (cp_parser *);
2231 static void cp_parser_error
2232   (cp_parser *, const char *);
2233 static void cp_parser_name_lookup_error
2234   (cp_parser *, tree, tree, name_lookup_error, location_t);
2235 static bool cp_parser_simulate_error
2236   (cp_parser *);
2237 static bool cp_parser_check_type_definition
2238   (cp_parser *);
2239 static void cp_parser_check_for_definition_in_return_type
2240   (cp_declarator *, tree, location_t type_location);
2241 static void cp_parser_check_for_invalid_template_id
2242   (cp_parser *, tree, location_t location);
2243 static bool cp_parser_non_integral_constant_expression
2244   (cp_parser *, non_integral_constant);
2245 static void cp_parser_diagnose_invalid_type_name
2246   (cp_parser *, tree, tree, location_t);
2247 static bool cp_parser_parse_and_diagnose_invalid_type_name
2248   (cp_parser *);
2249 static int cp_parser_skip_to_closing_parenthesis
2250   (cp_parser *, bool, bool, bool);
2251 static void cp_parser_skip_to_end_of_statement
2252   (cp_parser *);
2253 static void cp_parser_consume_semicolon_at_end_of_statement
2254   (cp_parser *);
2255 static void cp_parser_skip_to_end_of_block_or_statement
2256   (cp_parser *);
2257 static bool cp_parser_skip_to_closing_brace
2258   (cp_parser *);
2259 static void cp_parser_skip_to_end_of_template_parameter_list
2260   (cp_parser *);
2261 static void cp_parser_skip_to_pragma_eol
2262   (cp_parser*, cp_token *);
2263 static bool cp_parser_error_occurred
2264   (cp_parser *);
2265 static bool cp_parser_allow_gnu_extensions_p
2266   (cp_parser *);
2267 static bool cp_parser_is_string_literal
2268   (cp_token *);
2269 static bool cp_parser_is_keyword
2270   (cp_token *, enum rid);
2271 static tree cp_parser_make_typename_type
2272   (cp_parser *, tree, tree, location_t location);
2273 static cp_declarator * cp_parser_make_indirect_declarator
2274   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2275
2276 /* Returns nonzero if we are parsing tentatively.  */
2277
2278 static inline bool
2279 cp_parser_parsing_tentatively (cp_parser* parser)
2280 {
2281   return parser->context->next != NULL;
2282 }
2283
2284 /* Returns nonzero if TOKEN is a string literal.  */
2285
2286 static bool
2287 cp_parser_is_string_literal (cp_token* token)
2288 {
2289   return (token->type == CPP_STRING ||
2290           token->type == CPP_STRING16 ||
2291           token->type == CPP_STRING32 ||
2292           token->type == CPP_WSTRING ||
2293           token->type == CPP_UTF8STRING);
2294 }
2295
2296 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2297
2298 static bool
2299 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2300 {
2301   return token->keyword == keyword;
2302 }
2303
2304 /* If not parsing tentatively, issue a diagnostic of the form
2305       FILE:LINE: MESSAGE before TOKEN
2306    where TOKEN is the next token in the input stream.  MESSAGE
2307    (specified by the caller) is usually of the form "expected
2308    OTHER-TOKEN".  */
2309
2310 static void
2311 cp_parser_error (cp_parser* parser, const char* gmsgid)
2312 {
2313   if (!cp_parser_simulate_error (parser))
2314     {
2315       cp_token *token = cp_lexer_peek_token (parser->lexer);
2316       /* This diagnostic makes more sense if it is tagged to the line
2317          of the token we just peeked at.  */
2318       cp_lexer_set_source_position_from_token (token);
2319
2320       if (token->type == CPP_PRAGMA)
2321         {
2322           error_at (token->location,
2323                     "%<#pragma%> is not allowed here");
2324           cp_parser_skip_to_pragma_eol (parser, token);
2325           return;
2326         }
2327
2328       c_parse_error (gmsgid,
2329                      /* Because c_parser_error does not understand
2330                         CPP_KEYWORD, keywords are treated like
2331                         identifiers.  */
2332                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2333                      token->u.value, token->flags);
2334     }
2335 }
2336
2337 /* Issue an error about name-lookup failing.  NAME is the
2338    IDENTIFIER_NODE DECL is the result of
2339    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2340    the thing that we hoped to find.  */
2341
2342 static void
2343 cp_parser_name_lookup_error (cp_parser* parser,
2344                              tree name,
2345                              tree decl,
2346                              name_lookup_error desired,
2347                              location_t location)
2348 {
2349   /* If name lookup completely failed, tell the user that NAME was not
2350      declared.  */
2351   if (decl == error_mark_node)
2352     {
2353       if (parser->scope && parser->scope != global_namespace)
2354         error_at (location, "%<%E::%E%> has not been declared",
2355                   parser->scope, name);
2356       else if (parser->scope == global_namespace)
2357         error_at (location, "%<::%E%> has not been declared", name);
2358       else if (parser->object_scope
2359                && !CLASS_TYPE_P (parser->object_scope))
2360         error_at (location, "request for member %qE in non-class type %qT",
2361                   name, parser->object_scope);
2362       else if (parser->object_scope)
2363         error_at (location, "%<%T::%E%> has not been declared",
2364                   parser->object_scope, name);
2365       else
2366         error_at (location, "%qE has not been declared", name);
2367     }
2368   else if (parser->scope && parser->scope != global_namespace)
2369     {
2370       switch (desired)
2371         {
2372           case NLE_TYPE:
2373             error_at (location, "%<%E::%E%> is not a type",
2374                                 parser->scope, name);
2375             break;
2376           case NLE_CXX98:
2377             error_at (location, "%<%E::%E%> is not a class or namespace",
2378                                 parser->scope, name);
2379             break;
2380           case NLE_NOT_CXX98:
2381             error_at (location,
2382                       "%<%E::%E%> is not a class, namespace, or enumeration",
2383                       parser->scope, name);
2384             break;
2385           default:
2386             gcc_unreachable ();
2387             
2388         }
2389     }
2390   else if (parser->scope == global_namespace)
2391     {
2392       switch (desired)
2393         {
2394           case NLE_TYPE:
2395             error_at (location, "%<::%E%> is not a type", name);
2396             break;
2397           case NLE_CXX98:
2398             error_at (location, "%<::%E%> is not a class or namespace", name);
2399             break;
2400           case NLE_NOT_CXX98:
2401             error_at (location,
2402                       "%<::%E%> is not a class, namespace, or enumeration",
2403                       name);
2404             break;
2405           default:
2406             gcc_unreachable ();
2407         }
2408     }
2409   else
2410     {
2411       switch (desired)
2412         {
2413           case NLE_TYPE:
2414             error_at (location, "%qE is not a type", name);
2415             break;
2416           case NLE_CXX98:
2417             error_at (location, "%qE is not a class or namespace", name);
2418             break;
2419           case NLE_NOT_CXX98:
2420             error_at (location,
2421                       "%qE is not a class, namespace, or enumeration", name);
2422             break;
2423           default:
2424             gcc_unreachable ();
2425         }
2426     }
2427 }
2428
2429 /* If we are parsing tentatively, remember that an error has occurred
2430    during this tentative parse.  Returns true if the error was
2431    simulated; false if a message should be issued by the caller.  */
2432
2433 static bool
2434 cp_parser_simulate_error (cp_parser* parser)
2435 {
2436   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2437     {
2438       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2439       return true;
2440     }
2441   return false;
2442 }
2443
2444 /* Check for repeated decl-specifiers.  */
2445
2446 static void
2447 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2448                            location_t location)
2449 {
2450   int ds;
2451
2452   for (ds = ds_first; ds != ds_last; ++ds)
2453     {
2454       unsigned count = decl_specs->specs[ds];
2455       if (count < 2)
2456         continue;
2457       /* The "long" specifier is a special case because of "long long".  */
2458       if (ds == ds_long)
2459         {
2460           if (count > 2)
2461             error_at (location, "%<long long long%> is too long for GCC");
2462           else 
2463             pedwarn_cxx98 (location, OPT_Wlong_long, 
2464                            "ISO C++ 1998 does not support %<long long%>");
2465         }
2466       else if (count > 1)
2467         {
2468           static const char *const decl_spec_names[] = {
2469             "signed",
2470             "unsigned",
2471             "short",
2472             "long",
2473             "const",
2474             "volatile",
2475             "restrict",
2476             "inline",
2477             "virtual",
2478             "explicit",
2479             "friend",
2480             "typedef",
2481             "constexpr",
2482             "__complex",
2483             "__thread"
2484           };
2485           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2486         }
2487     }
2488 }
2489
2490 /* This function is called when a type is defined.  If type
2491    definitions are forbidden at this point, an error message is
2492    issued.  */
2493
2494 static bool
2495 cp_parser_check_type_definition (cp_parser* parser)
2496 {
2497   /* If types are forbidden here, issue a message.  */
2498   if (parser->type_definition_forbidden_message)
2499     {
2500       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2501          in the message need to be interpreted.  */
2502       error (parser->type_definition_forbidden_message);
2503       return false;
2504     }
2505   return true;
2506 }
2507
2508 /* This function is called when the DECLARATOR is processed.  The TYPE
2509    was a type defined in the decl-specifiers.  If it is invalid to
2510    define a type in the decl-specifiers for DECLARATOR, an error is
2511    issued. TYPE_LOCATION is the location of TYPE and is used
2512    for error reporting.  */
2513
2514 static void
2515 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2516                                                tree type, location_t type_location)
2517 {
2518   /* [dcl.fct] forbids type definitions in return types.
2519      Unfortunately, it's not easy to know whether or not we are
2520      processing a return type until after the fact.  */
2521   while (declarator
2522          && (declarator->kind == cdk_pointer
2523              || declarator->kind == cdk_reference
2524              || declarator->kind == cdk_ptrmem))
2525     declarator = declarator->declarator;
2526   if (declarator
2527       && declarator->kind == cdk_function)
2528     {
2529       error_at (type_location,
2530                 "new types may not be defined in a return type");
2531       inform (type_location, 
2532               "(perhaps a semicolon is missing after the definition of %qT)",
2533               type);
2534     }
2535 }
2536
2537 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2538    "<" in any valid C++ program.  If the next token is indeed "<",
2539    issue a message warning the user about what appears to be an
2540    invalid attempt to form a template-id. LOCATION is the location
2541    of the type-specifier (TYPE) */
2542
2543 static void
2544 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2545                                          tree type, location_t location)
2546 {
2547   cp_token_position start = 0;
2548
2549   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2550     {
2551       if (TYPE_P (type))
2552         error_at (location, "%qT is not a template", type);
2553       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2554         error_at (location, "%qE is not a template", type);
2555       else
2556         error_at (location, "invalid template-id");
2557       /* Remember the location of the invalid "<".  */
2558       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2559         start = cp_lexer_token_position (parser->lexer, true);
2560       /* Consume the "<".  */
2561       cp_lexer_consume_token (parser->lexer);
2562       /* Parse the template arguments.  */
2563       cp_parser_enclosed_template_argument_list (parser);
2564       /* Permanently remove the invalid template arguments so that
2565          this error message is not issued again.  */
2566       if (start)
2567         cp_lexer_purge_tokens_after (parser->lexer, start);
2568     }
2569 }
2570
2571 /* If parsing an integral constant-expression, issue an error message
2572    about the fact that THING appeared and return true.  Otherwise,
2573    return false.  In either case, set
2574    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2575
2576 static bool
2577 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2578                                             non_integral_constant thing)
2579 {
2580   parser->non_integral_constant_expression_p = true;
2581   if (parser->integral_constant_expression_p)
2582     {
2583       if (!parser->allow_non_integral_constant_expression_p)
2584         {
2585           const char *msg = NULL;
2586           switch (thing)
2587             {
2588               case NIC_FLOAT:
2589                 error ("floating-point literal "
2590                        "cannot appear in a constant-expression");
2591                 return true;
2592               case NIC_CAST:
2593                 error ("a cast to a type other than an integral or "
2594                        "enumeration type cannot appear in a "
2595                        "constant-expression");
2596                 return true;
2597               case NIC_TYPEID:
2598                 error ("%<typeid%> operator "
2599                        "cannot appear in a constant-expression");
2600                 return true;
2601               case NIC_NCC:
2602                 error ("non-constant compound literals "
2603                        "cannot appear in a constant-expression");
2604                 return true;
2605               case NIC_FUNC_CALL:
2606                 error ("a function call "
2607                        "cannot appear in a constant-expression");
2608                 return true;
2609               case NIC_INC:
2610                 error ("an increment "
2611                        "cannot appear in a constant-expression");
2612                 return true;
2613               case NIC_DEC:
2614                 error ("an decrement "
2615                        "cannot appear in a constant-expression");
2616                 return true;
2617               case NIC_ARRAY_REF:
2618                 error ("an array reference "
2619                        "cannot appear in a constant-expression");
2620                 return true;
2621               case NIC_ADDR_LABEL:
2622                 error ("the address of a label "
2623                        "cannot appear in a constant-expression");
2624                 return true;
2625               case NIC_OVERLOADED:
2626                 error ("calls to overloaded operators "
2627                        "cannot appear in a constant-expression");
2628                 return true;
2629               case NIC_ASSIGNMENT:
2630                 error ("an assignment cannot appear in a constant-expression");
2631                 return true;
2632               case NIC_COMMA:
2633                 error ("a comma operator "
2634                        "cannot appear in a constant-expression");
2635                 return true;
2636               case NIC_CONSTRUCTOR:
2637                 error ("a call to a constructor "
2638                        "cannot appear in a constant-expression");
2639                 return true;
2640               case NIC_THIS:
2641                 msg = "this";
2642                 break;
2643               case NIC_FUNC_NAME:
2644                 msg = "__FUNCTION__";
2645                 break;
2646               case NIC_PRETTY_FUNC:
2647                 msg = "__PRETTY_FUNCTION__";
2648                 break;
2649               case NIC_C99_FUNC:
2650                 msg = "__func__";
2651                 break;
2652               case NIC_VA_ARG:
2653                 msg = "va_arg";
2654                 break;
2655               case NIC_ARROW:
2656                 msg = "->";
2657                 break;
2658               case NIC_POINT:
2659                 msg = ".";
2660                 break;
2661               case NIC_STAR:
2662                 msg = "*";
2663                 break;
2664               case NIC_ADDR:
2665                 msg = "&";
2666                 break;
2667               case NIC_PREINCREMENT:
2668                 msg = "++";
2669                 break;
2670               case NIC_PREDECREMENT:
2671                 msg = "--";
2672                 break;
2673               case NIC_NEW:
2674                 msg = "new";
2675                 break;
2676               case NIC_DEL:
2677                 msg = "delete";
2678                 break;
2679               default:
2680                 gcc_unreachable ();
2681             }
2682           if (msg)
2683             error ("%qs cannot appear in a constant-expression", msg);
2684           return true;
2685         }
2686     }
2687   return false;
2688 }
2689
2690 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2691    qualifying scope (or NULL, if none) for ID.  This function commits
2692    to the current active tentative parse, if any.  (Otherwise, the
2693    problematic construct might be encountered again later, resulting
2694    in duplicate error messages.) LOCATION is the location of ID.  */
2695
2696 static void
2697 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2698                                       tree scope, tree id,
2699                                       location_t location)
2700 {
2701   tree decl, old_scope;
2702   /* Try to lookup the identifier.  */
2703   old_scope = parser->scope;
2704   parser->scope = scope;
2705   decl = cp_parser_lookup_name_simple (parser, id, location);
2706   parser->scope = old_scope;
2707   /* If the lookup found a template-name, it means that the user forgot
2708   to specify an argument list. Emit a useful error message.  */
2709   if (TREE_CODE (decl) == TEMPLATE_DECL)
2710     error_at (location,
2711               "invalid use of template-name %qE without an argument list",
2712               decl);
2713   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2714     error_at (location, "invalid use of destructor %qD as a type", id);
2715   else if (TREE_CODE (decl) == TYPE_DECL)
2716     /* Something like 'unsigned A a;'  */
2717     error_at (location, "invalid combination of multiple type-specifiers");
2718   else if (!parser->scope)
2719     {
2720       /* Issue an error message.  */
2721       error_at (location, "%qE does not name a type", id);
2722       /* If we're in a template class, it's possible that the user was
2723          referring to a type from a base class.  For example:
2724
2725            template <typename T> struct A { typedef T X; };
2726            template <typename T> struct B : public A<T> { X x; };
2727
2728          The user should have said "typename A<T>::X".  */
2729       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2730         inform (location, "C++0x %<constexpr%> only available with "
2731                 "-std=c++0x or -std=gnu++0x");
2732       else if (processing_template_decl && current_class_type
2733                && TYPE_BINFO (current_class_type))
2734         {
2735           tree b;
2736
2737           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2738                b;
2739                b = TREE_CHAIN (b))
2740             {
2741               tree base_type = BINFO_TYPE (b);
2742               if (CLASS_TYPE_P (base_type)
2743                   && dependent_type_p (base_type))
2744                 {
2745                   tree field;
2746                   /* Go from a particular instantiation of the
2747                      template (which will have an empty TYPE_FIELDs),
2748                      to the main version.  */
2749                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2750                   for (field = TYPE_FIELDS (base_type);
2751                        field;
2752                        field = DECL_CHAIN (field))
2753                     if (TREE_CODE (field) == TYPE_DECL
2754                         && DECL_NAME (field) == id)
2755                       {
2756                         inform (location, 
2757                                 "(perhaps %<typename %T::%E%> was intended)",
2758                                 BINFO_TYPE (b), id);
2759                         break;
2760                       }
2761                   if (field)
2762                     break;
2763                 }
2764             }
2765         }
2766     }
2767   /* Here we diagnose qualified-ids where the scope is actually correct,
2768      but the identifier does not resolve to a valid type name.  */
2769   else if (parser->scope != error_mark_node)
2770     {
2771       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2772         error_at (location, "%qE in namespace %qE does not name a type",
2773                   id, parser->scope);
2774       else if (CLASS_TYPE_P (parser->scope)
2775                && constructor_name_p (id, parser->scope))
2776         {
2777           /* A<T>::A<T>() */
2778           error_at (location, "%<%T::%E%> names the constructor, not"
2779                     " the type", parser->scope, id);
2780           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2781             error_at (location, "and %qT has no template constructors",
2782                       parser->scope);
2783         }
2784       else if (TYPE_P (parser->scope)
2785                && dependent_scope_p (parser->scope))
2786         error_at (location, "need %<typename%> before %<%T::%E%> because "
2787                   "%qT is a dependent scope",
2788                   parser->scope, id, parser->scope);
2789       else if (TYPE_P (parser->scope))
2790         error_at (location, "%qE in class %qT does not name a type",
2791                   id, parser->scope);
2792       else
2793         gcc_unreachable ();
2794     }
2795   cp_parser_commit_to_tentative_parse (parser);
2796 }
2797
2798 /* Check for a common situation where a type-name should be present,
2799    but is not, and issue a sensible error message.  Returns true if an
2800    invalid type-name was detected.
2801
2802    The situation handled by this function are variable declarations of the
2803    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2804    Usually, `ID' should name a type, but if we got here it means that it
2805    does not. We try to emit the best possible error message depending on
2806    how exactly the id-expression looks like.  */
2807
2808 static bool
2809 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2810 {
2811   tree id;
2812   cp_token *token = cp_lexer_peek_token (parser->lexer);
2813
2814   /* Avoid duplicate error about ambiguous lookup.  */
2815   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2816     {
2817       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2818       if (next->type == CPP_NAME && next->ambiguous_p)
2819         goto out;
2820     }
2821
2822   cp_parser_parse_tentatively (parser);
2823   id = cp_parser_id_expression (parser,
2824                                 /*template_keyword_p=*/false,
2825                                 /*check_dependency_p=*/true,
2826                                 /*template_p=*/NULL,
2827                                 /*declarator_p=*/true,
2828                                 /*optional_p=*/false);
2829   /* If the next token is a (, this is a function with no explicit return
2830      type, i.e. constructor, destructor or conversion op.  */
2831   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2832       || TREE_CODE (id) == TYPE_DECL)
2833     {
2834       cp_parser_abort_tentative_parse (parser);
2835       return false;
2836     }
2837   if (!cp_parser_parse_definitely (parser))
2838     return false;
2839
2840   /* Emit a diagnostic for the invalid type.  */
2841   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2842                                         id, token->location);
2843  out:
2844   /* If we aren't in the middle of a declarator (i.e. in a
2845      parameter-declaration-clause), skip to the end of the declaration;
2846      there's no point in trying to process it.  */
2847   if (!parser->in_declarator_p)
2848     cp_parser_skip_to_end_of_block_or_statement (parser);
2849   return true;
2850 }
2851
2852 /* Consume tokens up to, and including, the next non-nested closing `)'.
2853    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2854    are doing error recovery. Returns -1 if OR_COMMA is true and we
2855    found an unnested comma.  */
2856
2857 static int
2858 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2859                                        bool recovering,
2860                                        bool or_comma,
2861                                        bool consume_paren)
2862 {
2863   unsigned paren_depth = 0;
2864   unsigned brace_depth = 0;
2865   unsigned square_depth = 0;
2866
2867   if (recovering && !or_comma
2868       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2869     return 0;
2870
2871   while (true)
2872     {
2873       cp_token * token = cp_lexer_peek_token (parser->lexer);
2874
2875       switch (token->type)
2876         {
2877         case CPP_EOF:
2878         case CPP_PRAGMA_EOL:
2879           /* If we've run out of tokens, then there is no closing `)'.  */
2880           return 0;
2881
2882         /* This is good for lambda expression capture-lists.  */
2883         case CPP_OPEN_SQUARE:
2884           ++square_depth;
2885           break;
2886         case CPP_CLOSE_SQUARE:
2887           if (!square_depth--)
2888             return 0;
2889           break;
2890
2891         case CPP_SEMICOLON:
2892           /* This matches the processing in skip_to_end_of_statement.  */
2893           if (!brace_depth)
2894             return 0;
2895           break;
2896
2897         case CPP_OPEN_BRACE:
2898           ++brace_depth;
2899           break;
2900         case CPP_CLOSE_BRACE:
2901           if (!brace_depth--)
2902             return 0;
2903           break;
2904
2905         case CPP_COMMA:
2906           if (recovering && or_comma && !brace_depth && !paren_depth
2907               && !square_depth)
2908             return -1;
2909           break;
2910
2911         case CPP_OPEN_PAREN:
2912           if (!brace_depth)
2913             ++paren_depth;
2914           break;
2915
2916         case CPP_CLOSE_PAREN:
2917           if (!brace_depth && !paren_depth--)
2918             {
2919               if (consume_paren)
2920                 cp_lexer_consume_token (parser->lexer);
2921               return 1;
2922             }
2923           break;
2924
2925         default:
2926           break;
2927         }
2928
2929       /* Consume the token.  */
2930       cp_lexer_consume_token (parser->lexer);
2931     }
2932 }
2933
2934 /* Consume tokens until we reach the end of the current statement.
2935    Normally, that will be just before consuming a `;'.  However, if a
2936    non-nested `}' comes first, then we stop before consuming that.  */
2937
2938 static void
2939 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2940 {
2941   unsigned nesting_depth = 0;
2942
2943   while (true)
2944     {
2945       cp_token *token = cp_lexer_peek_token (parser->lexer);
2946
2947       switch (token->type)
2948         {
2949         case CPP_EOF:
2950         case CPP_PRAGMA_EOL:
2951           /* If we've run out of tokens, stop.  */
2952           return;
2953
2954         case CPP_SEMICOLON:
2955           /* If the next token is a `;', we have reached the end of the
2956              statement.  */
2957           if (!nesting_depth)
2958             return;
2959           break;
2960
2961         case CPP_CLOSE_BRACE:
2962           /* If this is a non-nested '}', stop before consuming it.
2963              That way, when confronted with something like:
2964
2965                { 3 + }
2966
2967              we stop before consuming the closing '}', even though we
2968              have not yet reached a `;'.  */
2969           if (nesting_depth == 0)
2970             return;
2971
2972           /* If it is the closing '}' for a block that we have
2973              scanned, stop -- but only after consuming the token.
2974              That way given:
2975
2976                 void f g () { ... }
2977                 typedef int I;
2978
2979              we will stop after the body of the erroneously declared
2980              function, but before consuming the following `typedef'
2981              declaration.  */
2982           if (--nesting_depth == 0)
2983             {
2984               cp_lexer_consume_token (parser->lexer);
2985               return;
2986             }
2987
2988         case CPP_OPEN_BRACE:
2989           ++nesting_depth;
2990           break;
2991
2992         default:
2993           break;
2994         }
2995
2996       /* Consume the token.  */
2997       cp_lexer_consume_token (parser->lexer);
2998     }
2999 }
3000
3001 /* This function is called at the end of a statement or declaration.
3002    If the next token is a semicolon, it is consumed; otherwise, error
3003    recovery is attempted.  */
3004
3005 static void
3006 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3007 {
3008   /* Look for the trailing `;'.  */
3009   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3010     {
3011       /* If there is additional (erroneous) input, skip to the end of
3012          the statement.  */
3013       cp_parser_skip_to_end_of_statement (parser);
3014       /* If the next token is now a `;', consume it.  */
3015       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3016         cp_lexer_consume_token (parser->lexer);
3017     }
3018 }
3019
3020 /* Skip tokens until we have consumed an entire block, or until we
3021    have consumed a non-nested `;'.  */
3022
3023 static void
3024 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3025 {
3026   int nesting_depth = 0;
3027
3028   while (nesting_depth >= 0)
3029     {
3030       cp_token *token = cp_lexer_peek_token (parser->lexer);
3031
3032       switch (token->type)
3033         {
3034         case CPP_EOF:
3035         case CPP_PRAGMA_EOL:
3036           /* If we've run out of tokens, stop.  */
3037           return;
3038
3039         case CPP_SEMICOLON:
3040           /* Stop if this is an unnested ';'. */
3041           if (!nesting_depth)
3042             nesting_depth = -1;
3043           break;
3044
3045         case CPP_CLOSE_BRACE:
3046           /* Stop if this is an unnested '}', or closes the outermost
3047              nesting level.  */
3048           nesting_depth--;
3049           if (nesting_depth < 0)
3050             return;
3051           if (!nesting_depth)
3052             nesting_depth = -1;
3053           break;
3054
3055         case CPP_OPEN_BRACE:
3056           /* Nest. */
3057           nesting_depth++;
3058           break;
3059
3060         default:
3061           break;
3062         }
3063
3064       /* Consume the token.  */
3065       cp_lexer_consume_token (parser->lexer);
3066     }
3067 }
3068
3069 /* Skip tokens until a non-nested closing curly brace is the next
3070    token, or there are no more tokens. Return true in the first case,
3071    false otherwise.  */
3072
3073 static bool
3074 cp_parser_skip_to_closing_brace (cp_parser *parser)
3075 {
3076   unsigned nesting_depth = 0;
3077
3078   while (true)
3079     {
3080       cp_token *token = cp_lexer_peek_token (parser->lexer);
3081
3082       switch (token->type)
3083         {
3084         case CPP_EOF:
3085         case CPP_PRAGMA_EOL:
3086           /* If we've run out of tokens, stop.  */
3087           return false;
3088
3089         case CPP_CLOSE_BRACE:
3090           /* If the next token is a non-nested `}', then we have reached
3091              the end of the current block.  */
3092           if (nesting_depth-- == 0)
3093             return true;
3094           break;
3095
3096         case CPP_OPEN_BRACE:
3097           /* If it the next token is a `{', then we are entering a new
3098              block.  Consume the entire block.  */
3099           ++nesting_depth;
3100           break;
3101
3102         default:
3103           break;
3104         }
3105
3106       /* Consume the token.  */
3107       cp_lexer_consume_token (parser->lexer);
3108     }
3109 }
3110
3111 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3112    parameter is the PRAGMA token, allowing us to purge the entire pragma
3113    sequence.  */
3114
3115 static void
3116 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3117 {
3118   cp_token *token;
3119
3120   parser->lexer->in_pragma = false;
3121
3122   do
3123     token = cp_lexer_consume_token (parser->lexer);
3124   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3125
3126   /* Ensure that the pragma is not parsed again.  */
3127   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3128 }
3129
3130 /* Require pragma end of line, resyncing with it as necessary.  The
3131    arguments are as for cp_parser_skip_to_pragma_eol.  */
3132
3133 static void
3134 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3135 {
3136   parser->lexer->in_pragma = false;
3137   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3138     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3139 }
3140
3141 /* This is a simple wrapper around make_typename_type. When the id is
3142    an unresolved identifier node, we can provide a superior diagnostic
3143    using cp_parser_diagnose_invalid_type_name.  */
3144
3145 static tree
3146 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3147                               tree id, location_t id_location)
3148 {
3149   tree result;
3150   if (TREE_CODE (id) == IDENTIFIER_NODE)
3151     {
3152       result = make_typename_type (scope, id, typename_type,
3153                                    /*complain=*/tf_none);
3154       if (result == error_mark_node)
3155         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3156       return result;
3157     }
3158   return make_typename_type (scope, id, typename_type, tf_error);
3159 }
3160
3161 /* This is a wrapper around the
3162    make_{pointer,ptrmem,reference}_declarator functions that decides
3163    which one to call based on the CODE and CLASS_TYPE arguments. The
3164    CODE argument should be one of the values returned by
3165    cp_parser_ptr_operator. */
3166 static cp_declarator *
3167 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3168                                     cp_cv_quals cv_qualifiers,
3169                                     cp_declarator *target)
3170 {
3171   if (code == ERROR_MARK)
3172     return cp_error_declarator;
3173
3174   if (code == INDIRECT_REF)
3175     if (class_type == NULL_TREE)
3176       return make_pointer_declarator (cv_qualifiers, target);
3177     else
3178       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3179   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3180     return make_reference_declarator (cv_qualifiers, target, false);
3181   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3182     return make_reference_declarator (cv_qualifiers, target, true);
3183   gcc_unreachable ();
3184 }
3185
3186 /* Create a new C++ parser.  */
3187
3188 static cp_parser *
3189 cp_parser_new (void)
3190 {
3191   cp_parser *parser;
3192   cp_lexer *lexer;
3193   unsigned i;
3194
3195   /* cp_lexer_new_main is called before doing GC allocation because
3196      cp_lexer_new_main might load a PCH file.  */
3197   lexer = cp_lexer_new_main ();
3198
3199   /* Initialize the binops_by_token so that we can get the tree
3200      directly from the token.  */
3201   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3202     binops_by_token[binops[i].token_type] = binops[i];
3203
3204   parser = ggc_alloc_cleared_cp_parser ();
3205   parser->lexer = lexer;
3206   parser->context = cp_parser_context_new (NULL);
3207
3208   /* For now, we always accept GNU extensions.  */
3209   parser->allow_gnu_extensions_p = 1;
3210
3211   /* The `>' token is a greater-than operator, not the end of a
3212      template-id.  */
3213   parser->greater_than_is_operator_p = true;
3214
3215   parser->default_arg_ok_p = true;
3216
3217   /* We are not parsing a constant-expression.  */
3218   parser->integral_constant_expression_p = false;
3219   parser->allow_non_integral_constant_expression_p = false;
3220   parser->non_integral_constant_expression_p = false;
3221
3222   /* Local variable names are not forbidden.  */
3223   parser->local_variables_forbidden_p = false;
3224
3225   /* We are not processing an `extern "C"' declaration.  */
3226   parser->in_unbraced_linkage_specification_p = false;
3227
3228   /* We are not processing a declarator.  */
3229   parser->in_declarator_p = false;
3230
3231   /* We are not processing a template-argument-list.  */
3232   parser->in_template_argument_list_p = false;
3233
3234   /* We are not in an iteration statement.  */
3235   parser->in_statement = 0;
3236
3237   /* We are not in a switch statement.  */
3238   parser->in_switch_statement_p = false;
3239
3240   /* We are not parsing a type-id inside an expression.  */
3241   parser->in_type_id_in_expr_p = false;
3242
3243   /* Declarations aren't implicitly extern "C".  */
3244   parser->implicit_extern_c = false;
3245
3246   /* String literals should be translated to the execution character set.  */
3247   parser->translate_strings_p = true;
3248
3249   /* We are not parsing a function body.  */
3250   parser->in_function_body = false;
3251
3252   /* We can correct until told otherwise.  */
3253   parser->colon_corrects_to_scope_p = true;
3254
3255   /* The unparsed function queue is empty.  */
3256   push_unparsed_function_queues (parser);
3257
3258   /* There are no classes being defined.  */
3259   parser->num_classes_being_defined = 0;
3260
3261   /* No template parameters apply.  */
3262   parser->num_template_parameter_lists = 0;
3263
3264   return parser;
3265 }
3266
3267 /* Create a cp_lexer structure which will emit the tokens in CACHE
3268    and push it onto the parser's lexer stack.  This is used for delayed
3269    parsing of in-class method bodies and default arguments, and should
3270    not be confused with tentative parsing.  */
3271 static void
3272 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3273 {
3274   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3275   lexer->next = parser->lexer;
3276   parser->lexer = lexer;
3277
3278   /* Move the current source position to that of the first token in the
3279      new lexer.  */
3280   cp_lexer_set_source_position_from_token (lexer->next_token);
3281 }
3282
3283 /* Pop the top lexer off the parser stack.  This is never used for the
3284    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3285 static void
3286 cp_parser_pop_lexer (cp_parser *parser)
3287 {
3288   cp_lexer *lexer = parser->lexer;
3289   parser->lexer = lexer->next;
3290   cp_lexer_destroy (lexer);
3291
3292   /* Put the current source position back where it was before this
3293      lexer was pushed.  */
3294   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3295 }
3296
3297 /* Lexical conventions [gram.lex]  */
3298
3299 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3300    identifier.  */
3301
3302 static tree
3303 cp_parser_identifier (cp_parser* parser)
3304 {
3305   cp_token *token;
3306
3307   /* Look for the identifier.  */
3308   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3309   /* Return the value.  */
3310   return token ? token->u.value : error_mark_node;
3311 }
3312
3313 /* Parse a sequence of adjacent string constants.  Returns a
3314    TREE_STRING representing the combined, nul-terminated string
3315    constant.  If TRANSLATE is true, translate the string to the
3316    execution character set.  If WIDE_OK is true, a wide string is
3317    invalid here.
3318
3319    C++98 [lex.string] says that if a narrow string literal token is
3320    adjacent to a wide string literal token, the behavior is undefined.
3321    However, C99 6.4.5p4 says that this results in a wide string literal.
3322    We follow C99 here, for consistency with the C front end.
3323
3324    This code is largely lifted from lex_string() in c-lex.c.
3325
3326    FUTURE: ObjC++ will need to handle @-strings here.  */
3327 static tree
3328 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3329 {
3330   tree value;
3331   size_t count;
3332   struct obstack str_ob;
3333   cpp_string str, istr, *strs;
3334   cp_token *tok;
3335   enum cpp_ttype type;
3336
3337   tok = cp_lexer_peek_token (parser->lexer);
3338   if (!cp_parser_is_string_literal (tok))
3339     {
3340       cp_parser_error (parser, "expected string-literal");
3341       return error_mark_node;
3342     }
3343
3344   type = tok->type;
3345
3346   /* Try to avoid the overhead of creating and destroying an obstack
3347      for the common case of just one string.  */
3348   if (!cp_parser_is_string_literal
3349       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3350     {
3351       cp_lexer_consume_token (parser->lexer);
3352
3353       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3354       str.len = TREE_STRING_LENGTH (tok->u.value);
3355       count = 1;
3356
3357       strs = &str;
3358     }
3359   else
3360     {
3361       gcc_obstack_init (&str_ob);
3362       count = 0;
3363
3364       do
3365         {
3366           cp_lexer_consume_token (parser->lexer);
3367           count++;
3368           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3369           str.len = TREE_STRING_LENGTH (tok->u.value);
3370
3371           if (type != tok->type)
3372             {
3373               if (type == CPP_STRING)
3374                 type = tok->type;
3375               else if (tok->type != CPP_STRING)
3376                 error_at (tok->location,
3377                           "unsupported non-standard concatenation "
3378                           "of string literals");
3379             }
3380
3381           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3382
3383           tok = cp_lexer_peek_token (parser->lexer);
3384         }
3385       while (cp_parser_is_string_literal (tok));
3386
3387       strs = (cpp_string *) obstack_finish (&str_ob);
3388     }
3389
3390   if (type != CPP_STRING && !wide_ok)
3391     {
3392       cp_parser_error (parser, "a wide string is invalid in this context");
3393       type = CPP_STRING;
3394     }
3395
3396   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3397       (parse_in, strs, count, &istr, type))
3398     {
3399       value = build_string (istr.len, (const char *)istr.text);
3400       free (CONST_CAST (unsigned char *, istr.text));
3401
3402       switch (type)
3403         {
3404         default:
3405         case CPP_STRING:
3406         case CPP_UTF8STRING:
3407           TREE_TYPE (value) = char_array_type_node;
3408           break;
3409         case CPP_STRING16:
3410           TREE_TYPE (value) = char16_array_type_node;
3411           break;
3412         case CPP_STRING32:
3413           TREE_TYPE (value) = char32_array_type_node;
3414           break;
3415         case CPP_WSTRING:
3416           TREE_TYPE (value) = wchar_array_type_node;
3417           break;
3418         }
3419
3420       value = fix_string_type (value);
3421     }
3422   else
3423     /* cpp_interpret_string has issued an error.  */
3424     value = error_mark_node;
3425
3426   if (count > 1)
3427     obstack_free (&str_ob, 0);
3428
3429   return value;
3430 }
3431
3432
3433 /* Basic concepts [gram.basic]  */
3434
3435 /* Parse a translation-unit.
3436
3437    translation-unit:
3438      declaration-seq [opt]
3439
3440    Returns TRUE if all went well.  */
3441
3442 static bool
3443 cp_parser_translation_unit (cp_parser* parser)
3444 {
3445   /* The address of the first non-permanent object on the declarator
3446      obstack.  */
3447   static void *declarator_obstack_base;
3448
3449   bool success;
3450
3451   /* Create the declarator obstack, if necessary.  */
3452   if (!cp_error_declarator)
3453     {
3454       gcc_obstack_init (&declarator_obstack);
3455       /* Create the error declarator.  */
3456       cp_error_declarator = make_declarator (cdk_error);
3457       /* Create the empty parameter list.  */
3458       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3459       /* Remember where the base of the declarator obstack lies.  */
3460       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3461     }
3462
3463   cp_parser_declaration_seq_opt (parser);
3464
3465   /* If there are no tokens left then all went well.  */
3466   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3467     {
3468       /* Get rid of the token array; we don't need it any more.  */
3469       cp_lexer_destroy (parser->lexer);
3470       parser->lexer = NULL;
3471
3472       /* This file might have been a context that's implicitly extern
3473          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3474       if (parser->implicit_extern_c)
3475         {
3476           pop_lang_context ();
3477           parser->implicit_extern_c = false;
3478         }
3479
3480       /* Finish up.  */
3481       finish_translation_unit ();
3482
3483       success = true;
3484     }
3485   else
3486     {
3487       cp_parser_error (parser, "expected declaration");
3488       success = false;
3489     }
3490
3491   /* Make sure the declarator obstack was fully cleaned up.  */
3492   gcc_assert (obstack_next_free (&declarator_obstack)
3493               == declarator_obstack_base);
3494
3495   /* All went well.  */
3496   return success;
3497 }
3498
3499 /* Expressions [gram.expr] */
3500
3501 /* Parse a primary-expression.
3502
3503    primary-expression:
3504      literal
3505      this
3506      ( expression )
3507      id-expression
3508
3509    GNU Extensions:
3510
3511    primary-expression:
3512      ( compound-statement )
3513      __builtin_va_arg ( assignment-expression , type-id )
3514      __builtin_offsetof ( type-id , offsetof-expression )
3515
3516    C++ Extensions:
3517      __has_nothrow_assign ( type-id )   
3518      __has_nothrow_constructor ( type-id )
3519      __has_nothrow_copy ( type-id )
3520      __has_trivial_assign ( type-id )   
3521      __has_trivial_constructor ( type-id )
3522      __has_trivial_copy ( type-id )
3523      __has_trivial_destructor ( type-id )
3524      __has_virtual_destructor ( type-id )     
3525      __is_abstract ( type-id )
3526      __is_base_of ( type-id , type-id )
3527      __is_class ( type-id )
3528      __is_convertible_to ( type-id , type-id )     
3529      __is_empty ( type-id )
3530      __is_enum ( type-id )
3531      __is_pod ( type-id )
3532      __is_polymorphic ( type-id )
3533      __is_union ( type-id )
3534
3535    Objective-C++ Extension:
3536
3537    primary-expression:
3538      objc-expression
3539
3540    literal:
3541      __null
3542
3543    ADDRESS_P is true iff this expression was immediately preceded by
3544    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3545    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3546    true iff this expression is a template argument.
3547
3548    Returns a representation of the expression.  Upon return, *IDK
3549    indicates what kind of id-expression (if any) was present.  */
3550
3551 static tree
3552 cp_parser_primary_expression (cp_parser *parser,
3553                               bool address_p,
3554                               bool cast_p,
3555                               bool template_arg_p,
3556                               cp_id_kind *idk)
3557 {
3558   cp_token *token = NULL;
3559
3560   /* Assume the primary expression is not an id-expression.  */
3561   *idk = CP_ID_KIND_NONE;
3562
3563   /* Peek at the next token.  */
3564   token = cp_lexer_peek_token (parser->lexer);
3565   switch (token->type)
3566     {
3567       /* literal:
3568            integer-literal
3569            character-literal
3570            floating-literal
3571            string-literal
3572            boolean-literal  */
3573     case CPP_CHAR:
3574     case CPP_CHAR16:
3575     case CPP_CHAR32:
3576     case CPP_WCHAR:
3577     case CPP_NUMBER:
3578       token = cp_lexer_consume_token (parser->lexer);
3579       if (TREE_CODE (token->u.value) == FIXED_CST)
3580         {
3581           error_at (token->location,
3582                     "fixed-point types not supported in C++");
3583           return error_mark_node;
3584         }
3585       /* Floating-point literals are only allowed in an integral
3586          constant expression if they are cast to an integral or
3587          enumeration type.  */
3588       if (TREE_CODE (token->u.value) == REAL_CST
3589           && parser->integral_constant_expression_p
3590           && pedantic)
3591         {
3592           /* CAST_P will be set even in invalid code like "int(2.7 +
3593              ...)".   Therefore, we have to check that the next token
3594              is sure to end the cast.  */
3595           if (cast_p)
3596             {
3597               cp_token *next_token;
3598
3599               next_token = cp_lexer_peek_token (parser->lexer);
3600               if (/* The comma at the end of an
3601                      enumerator-definition.  */
3602                   next_token->type != CPP_COMMA
3603                   /* The curly brace at the end of an enum-specifier.  */
3604                   && next_token->type != CPP_CLOSE_BRACE
3605                   /* The end of a statement.  */
3606                   && next_token->type != CPP_SEMICOLON
3607                   /* The end of the cast-expression.  */
3608                   && next_token->type != CPP_CLOSE_PAREN
3609                   /* The end of an array bound.  */
3610                   && next_token->type != CPP_CLOSE_SQUARE
3611                   /* The closing ">" in a template-argument-list.  */
3612                   && (next_token->type != CPP_GREATER
3613                       || parser->greater_than_is_operator_p)
3614                   /* C++0x only: A ">>" treated like two ">" tokens,
3615                      in a template-argument-list.  */
3616                   && (next_token->type != CPP_RSHIFT
3617                       || (cxx_dialect == cxx98)
3618                       || parser->greater_than_is_operator_p))
3619                 cast_p = false;
3620             }
3621
3622           /* If we are within a cast, then the constraint that the
3623              cast is to an integral or enumeration type will be
3624              checked at that point.  If we are not within a cast, then
3625              this code is invalid.  */
3626           if (!cast_p)
3627             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3628         }
3629       return token->u.value;
3630
3631     case CPP_STRING:
3632     case CPP_STRING16:
3633     case CPP_STRING32:
3634     case CPP_WSTRING:
3635     case CPP_UTF8STRING:
3636       /* ??? Should wide strings be allowed when parser->translate_strings_p
3637          is false (i.e. in attributes)?  If not, we can kill the third
3638          argument to cp_parser_string_literal.  */
3639       return cp_parser_string_literal (parser,
3640                                        parser->translate_strings_p,
3641                                        true);
3642
3643     case CPP_OPEN_PAREN:
3644       {
3645         tree expr;
3646         bool saved_greater_than_is_operator_p;
3647
3648         /* Consume the `('.  */
3649         cp_lexer_consume_token (parser->lexer);
3650         /* Within a parenthesized expression, a `>' token is always
3651            the greater-than operator.  */
3652         saved_greater_than_is_operator_p
3653           = parser->greater_than_is_operator_p;
3654         parser->greater_than_is_operator_p = true;
3655         /* If we see `( { ' then we are looking at the beginning of
3656            a GNU statement-expression.  */
3657         if (cp_parser_allow_gnu_extensions_p (parser)
3658             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3659           {
3660             /* Statement-expressions are not allowed by the standard.  */
3661             pedwarn (token->location, OPT_pedantic, 
3662                      "ISO C++ forbids braced-groups within expressions");
3663
3664             /* And they're not allowed outside of a function-body; you
3665                cannot, for example, write:
3666
3667                  int i = ({ int j = 3; j + 1; });
3668
3669                at class or namespace scope.  */
3670             if (!parser->in_function_body
3671                 || parser->in_template_argument_list_p)
3672               {
3673                 error_at (token->location,
3674                           "statement-expressions are not allowed outside "
3675                           "functions nor in template-argument lists");
3676                 cp_parser_skip_to_end_of_block_or_statement (parser);
3677                 expr = error_mark_node;
3678               }
3679             else
3680               {
3681                 /* Start the statement-expression.  */
3682                 expr = begin_stmt_expr ();
3683                 /* Parse the compound-statement.  */
3684                 cp_parser_compound_statement (parser, expr, false);
3685                 /* Finish up.  */
3686                 expr = finish_stmt_expr (expr, false);
3687               }
3688           }
3689         else
3690           {
3691             /* Parse the parenthesized expression.  */
3692             expr = cp_parser_expression (parser, cast_p, idk);
3693             /* Let the front end know that this expression was
3694                enclosed in parentheses. This matters in case, for
3695                example, the expression is of the form `A::B', since
3696                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3697                not.  */
3698             finish_parenthesized_expr (expr);
3699           }
3700         /* The `>' token might be the end of a template-id or
3701            template-parameter-list now.  */
3702         parser->greater_than_is_operator_p
3703           = saved_greater_than_is_operator_p;
3704         /* Consume the `)'.  */
3705         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3706           cp_parser_skip_to_end_of_statement (parser);
3707
3708         return expr;
3709       }
3710
3711     case CPP_OPEN_SQUARE:
3712       if (c_dialect_objc ())
3713         /* We have an Objective-C++ message. */
3714         return cp_parser_objc_expression (parser);
3715       {
3716         tree lam = cp_parser_lambda_expression (parser);
3717         /* Don't warn about a failed tentative parse.  */
3718         if (cp_parser_error_occurred (parser))
3719           return error_mark_node;
3720         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3721         return lam;
3722       }
3723
3724     case CPP_OBJC_STRING:
3725       if (c_dialect_objc ())
3726         /* We have an Objective-C++ string literal. */
3727         return cp_parser_objc_expression (parser);
3728       cp_parser_error (parser, "expected primary-expression");
3729       return error_mark_node;
3730
3731     case CPP_KEYWORD:
3732       switch (token->keyword)
3733         {
3734           /* These two are the boolean literals.  */
3735         case RID_TRUE:
3736           cp_lexer_consume_token (parser->lexer);
3737           return boolean_true_node;
3738         case RID_FALSE:
3739           cp_lexer_consume_token (parser->lexer);
3740           return boolean_false_node;
3741
3742           /* The `__null' literal.  */
3743         case RID_NULL:
3744           cp_lexer_consume_token (parser->lexer);
3745           return null_node;
3746
3747           /* The `nullptr' literal.  */
3748         case RID_NULLPTR:
3749           cp_lexer_consume_token (parser->lexer);
3750           return nullptr_node;
3751
3752           /* Recognize the `this' keyword.  */
3753         case RID_THIS:
3754           cp_lexer_consume_token (parser->lexer);
3755           if (parser->local_variables_forbidden_p)
3756             {
3757               error_at (token->location,
3758                         "%<this%> may not be used in this context");
3759               return error_mark_node;
3760             }
3761           /* Pointers cannot appear in constant-expressions.  */
3762           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3763             return error_mark_node;
3764           return finish_this_expr ();
3765
3766           /* The `operator' keyword can be the beginning of an
3767              id-expression.  */
3768         case RID_OPERATOR:
3769           goto id_expression;
3770
3771         case RID_FUNCTION_NAME:
3772         case RID_PRETTY_FUNCTION_NAME:
3773         case RID_C99_FUNCTION_NAME:
3774           {
3775             non_integral_constant name;
3776
3777             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3778                __func__ are the names of variables -- but they are
3779                treated specially.  Therefore, they are handled here,
3780                rather than relying on the generic id-expression logic
3781                below.  Grammatically, these names are id-expressions.
3782
3783                Consume the token.  */
3784             token = cp_lexer_consume_token (parser->lexer);
3785
3786             switch (token->keyword)
3787               {
3788               case RID_FUNCTION_NAME:
3789                 name = NIC_FUNC_NAME;
3790                 break;
3791               case RID_PRETTY_FUNCTION_NAME:
3792                 name = NIC_PRETTY_FUNC;
3793                 break;
3794               case RID_C99_FUNCTION_NAME:
3795                 name = NIC_C99_FUNC;
3796                 break;
3797               default:
3798                 gcc_unreachable ();
3799               }
3800
3801             if (cp_parser_non_integral_constant_expression (parser, name))
3802               return error_mark_node;
3803
3804             /* Look up the name.  */
3805             return finish_fname (token->u.value);
3806           }
3807
3808         case RID_VA_ARG:
3809           {
3810             tree expression;
3811             tree type;
3812
3813             /* The `__builtin_va_arg' construct is used to handle
3814                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3815             cp_lexer_consume_token (parser->lexer);
3816             /* Look for the opening `('.  */
3817             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3818             /* Now, parse the assignment-expression.  */
3819             expression = cp_parser_assignment_expression (parser,
3820                                                           /*cast_p=*/false, NULL);
3821             /* Look for the `,'.  */
3822             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3823             /* Parse the type-id.  */
3824             type = cp_parser_type_id (parser);
3825             /* Look for the closing `)'.  */
3826             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3827             /* Using `va_arg' in a constant-expression is not
3828                allowed.  */
3829             if (cp_parser_non_integral_constant_expression (parser,
3830                                                             NIC_VA_ARG))
3831               return error_mark_node;
3832             return build_x_va_arg (expression, type);
3833           }
3834
3835         case RID_OFFSETOF:
3836           return cp_parser_builtin_offsetof (parser);
3837
3838         case RID_HAS_NOTHROW_ASSIGN:
3839         case RID_HAS_NOTHROW_CONSTRUCTOR:
3840         case RID_HAS_NOTHROW_COPY:        
3841         case RID_HAS_TRIVIAL_ASSIGN:
3842         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3843         case RID_HAS_TRIVIAL_COPY:        
3844         case RID_HAS_TRIVIAL_DESTRUCTOR:
3845         case RID_HAS_VIRTUAL_DESTRUCTOR:
3846         case RID_IS_ABSTRACT:
3847         case RID_IS_BASE_OF:
3848         case RID_IS_CLASS:
3849         case RID_IS_CONVERTIBLE_TO:
3850         case RID_IS_EMPTY:
3851         case RID_IS_ENUM:
3852         case RID_IS_POD:
3853         case RID_IS_POLYMORPHIC:
3854         case RID_IS_STD_LAYOUT:
3855         case RID_IS_TRIVIAL:
3856         case RID_IS_UNION:
3857         case RID_IS_LITERAL_TYPE:
3858           return cp_parser_trait_expr (parser, token->keyword);
3859
3860         /* Objective-C++ expressions.  */
3861         case RID_AT_ENCODE:
3862         case RID_AT_PROTOCOL:
3863         case RID_AT_SELECTOR:
3864           return cp_parser_objc_expression (parser);
3865
3866         case RID_TEMPLATE:
3867           if (parser->in_function_body
3868               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3869                   == CPP_LESS))
3870             {
3871               error_at (token->location,
3872                         "a template declaration cannot appear at block scope");
3873               cp_parser_skip_to_end_of_block_or_statement (parser);
3874               return error_mark_node;
3875             }
3876         default:
3877           cp_parser_error (parser, "expected primary-expression");
3878           return error_mark_node;
3879         }
3880
3881       /* An id-expression can start with either an identifier, a
3882          `::' as the beginning of a qualified-id, or the "operator"
3883          keyword.  */
3884     case CPP_NAME:
3885     case CPP_SCOPE:
3886     case CPP_TEMPLATE_ID:
3887     case CPP_NESTED_NAME_SPECIFIER:
3888       {
3889         tree id_expression;
3890         tree decl;
3891         const char *error_msg;
3892         bool template_p;
3893         bool done;
3894         cp_token *id_expr_token;
3895
3896       id_expression:
3897         /* Parse the id-expression.  */
3898         id_expression
3899           = cp_parser_id_expression (parser,
3900                                      /*template_keyword_p=*/false,
3901                                      /*check_dependency_p=*/true,
3902                                      &template_p,
3903                                      /*declarator_p=*/false,
3904                                      /*optional_p=*/false);
3905         if (id_expression == error_mark_node)
3906           return error_mark_node;
3907         id_expr_token = token;
3908         token = cp_lexer_peek_token (parser->lexer);
3909         done = (token->type != CPP_OPEN_SQUARE
3910                 && token->type != CPP_OPEN_PAREN
3911                 && token->type != CPP_DOT
3912                 && token->type != CPP_DEREF
3913                 && token->type != CPP_PLUS_PLUS
3914                 && token->type != CPP_MINUS_MINUS);
3915         /* If we have a template-id, then no further lookup is
3916            required.  If the template-id was for a template-class, we
3917            will sometimes have a TYPE_DECL at this point.  */
3918         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3919                  || TREE_CODE (id_expression) == TYPE_DECL)
3920           decl = id_expression;
3921         /* Look up the name.  */
3922         else
3923           {
3924             tree ambiguous_decls;
3925
3926             /* If we already know that this lookup is ambiguous, then
3927                we've already issued an error message; there's no reason
3928                to check again.  */
3929             if (id_expr_token->type == CPP_NAME
3930                 && id_expr_token->ambiguous_p)
3931               {
3932                 cp_parser_simulate_error (parser);
3933                 return error_mark_node;
3934               }
3935
3936             decl = cp_parser_lookup_name (parser, id_expression,
3937                                           none_type,
3938                                           template_p,
3939                                           /*is_namespace=*/false,
3940                                           /*check_dependency=*/true,
3941                                           &ambiguous_decls,
3942                                           id_expr_token->location);
3943             /* If the lookup was ambiguous, an error will already have
3944                been issued.  */
3945             if (ambiguous_decls)
3946               return error_mark_node;
3947
3948             /* In Objective-C++, we may have an Objective-C 2.0
3949                dot-syntax for classes here.  */
3950             if (c_dialect_objc ()
3951                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3952                 && TREE_CODE (decl) == TYPE_DECL
3953                 && objc_is_class_name (decl))
3954               {
3955                 tree component;
3956                 cp_lexer_consume_token (parser->lexer);
3957                 component = cp_parser_identifier (parser);
3958                 if (component == error_mark_node)
3959                   return error_mark_node;
3960
3961                 return objc_build_class_component_ref (id_expression, component);
3962               }
3963
3964             /* In Objective-C++, an instance variable (ivar) may be preferred
3965                to whatever cp_parser_lookup_name() found.  */
3966             decl = objc_lookup_ivar (decl, id_expression);
3967
3968             /* If name lookup gives us a SCOPE_REF, then the
3969                qualifying scope was dependent.  */
3970             if (TREE_CODE (decl) == SCOPE_REF)
3971               {
3972                 /* At this point, we do not know if DECL is a valid
3973                    integral constant expression.  We assume that it is
3974                    in fact such an expression, so that code like:
3975
3976                       template <int N> struct A {
3977                         int a[B<N>::i];
3978                       };
3979                      
3980                    is accepted.  At template-instantiation time, we
3981                    will check that B<N>::i is actually a constant.  */
3982                 return decl;
3983               }
3984             /* Check to see if DECL is a local variable in a context
3985                where that is forbidden.  */
3986             if (parser->local_variables_forbidden_p
3987                 && local_variable_p (decl))
3988               {
3989                 /* It might be that we only found DECL because we are
3990                    trying to be generous with pre-ISO scoping rules.
3991                    For example, consider:
3992
3993                      int i;
3994                      void g() {
3995                        for (int i = 0; i < 10; ++i) {}
3996                        extern void f(int j = i);
3997                      }
3998
3999                    Here, name look up will originally find the out
4000                    of scope `i'.  We need to issue a warning message,
4001                    but then use the global `i'.  */
4002                 decl = check_for_out_of_scope_variable (decl);
4003                 if (local_variable_p (decl))
4004                   {
4005                     error_at (id_expr_token->location,
4006                               "local variable %qD may not appear in this context",
4007                               decl);
4008                     return error_mark_node;
4009                   }
4010               }
4011           }
4012
4013         decl = (finish_id_expression
4014                 (id_expression, decl, parser->scope,
4015                  idk,
4016                  parser->integral_constant_expression_p,
4017                  parser->allow_non_integral_constant_expression_p,
4018                  &parser->non_integral_constant_expression_p,
4019                  template_p, done, address_p,
4020                  template_arg_p,
4021                  &error_msg,
4022                  id_expr_token->location));
4023         if (error_msg)
4024           cp_parser_error (parser, error_msg);
4025         return decl;
4026       }
4027
4028       /* Anything else is an error.  */
4029     default:
4030       cp_parser_error (parser, "expected primary-expression");
4031       return error_mark_node;
4032     }
4033 }
4034
4035 /* Parse an id-expression.
4036
4037    id-expression:
4038      unqualified-id
4039      qualified-id
4040
4041    qualified-id:
4042      :: [opt] nested-name-specifier template [opt] unqualified-id
4043      :: identifier
4044      :: operator-function-id
4045      :: template-id
4046
4047    Return a representation of the unqualified portion of the
4048    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4049    a `::' or nested-name-specifier.
4050
4051    Often, if the id-expression was a qualified-id, the caller will
4052    want to make a SCOPE_REF to represent the qualified-id.  This
4053    function does not do this in order to avoid wastefully creating
4054    SCOPE_REFs when they are not required.
4055
4056    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4057    `template' keyword.
4058
4059    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4060    uninstantiated templates.
4061
4062    If *TEMPLATE_P is non-NULL, it is set to true iff the
4063    `template' keyword is used to explicitly indicate that the entity
4064    named is a template.
4065
4066    If DECLARATOR_P is true, the id-expression is appearing as part of
4067    a declarator, rather than as part of an expression.  */
4068
4069 static tree
4070 cp_parser_id_expression (cp_parser *parser,
4071                          bool template_keyword_p,
4072                          bool check_dependency_p,
4073                          bool *template_p,
4074                          bool declarator_p,
4075                          bool optional_p)
4076 {
4077   bool global_scope_p;
4078   bool nested_name_specifier_p;
4079
4080   /* Assume the `template' keyword was not used.  */
4081   if (template_p)
4082     *template_p = template_keyword_p;
4083
4084   /* Look for the optional `::' operator.  */
4085   global_scope_p
4086     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4087        != NULL_TREE);
4088   /* Look for the optional nested-name-specifier.  */
4089   nested_name_specifier_p
4090     = (cp_parser_nested_name_specifier_opt (parser,
4091                                             /*typename_keyword_p=*/false,
4092                                             check_dependency_p,
4093                                             /*type_p=*/false,
4094                                             declarator_p)
4095        != NULL_TREE);
4096   /* If there is a nested-name-specifier, then we are looking at
4097      the first qualified-id production.  */
4098   if (nested_name_specifier_p)
4099     {
4100       tree saved_scope;
4101       tree saved_object_scope;
4102       tree saved_qualifying_scope;
4103       tree unqualified_id;
4104       bool is_template;
4105
4106       /* See if the next token is the `template' keyword.  */
4107       if (!template_p)
4108         template_p = &is_template;
4109       *template_p = cp_parser_optional_template_keyword (parser);
4110       /* Name lookup we do during the processing of the
4111          unqualified-id might obliterate SCOPE.  */
4112       saved_scope = parser->scope;
4113       saved_object_scope = parser->object_scope;
4114       saved_qualifying_scope = parser->qualifying_scope;
4115       /* Process the final unqualified-id.  */
4116       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4117                                                  check_dependency_p,
4118                                                  declarator_p,
4119                                                  /*optional_p=*/false);
4120       /* Restore the SAVED_SCOPE for our caller.  */
4121       parser->scope = saved_scope;
4122       parser->object_scope = saved_object_scope;
4123       parser->qualifying_scope = saved_qualifying_scope;
4124
4125       return unqualified_id;
4126     }
4127   /* Otherwise, if we are in global scope, then we are looking at one
4128      of the other qualified-id productions.  */
4129   else if (global_scope_p)
4130     {
4131       cp_token *token;
4132       tree id;
4133
4134       /* Peek at the next token.  */
4135       token = cp_lexer_peek_token (parser->lexer);
4136
4137       /* If it's an identifier, and the next token is not a "<", then
4138          we can avoid the template-id case.  This is an optimization
4139          for this common case.  */
4140       if (token->type == CPP_NAME
4141           && !cp_parser_nth_token_starts_template_argument_list_p
4142                (parser, 2))
4143         return cp_parser_identifier (parser);
4144
4145       cp_parser_parse_tentatively (parser);
4146       /* Try a template-id.  */
4147       id = cp_parser_template_id (parser,
4148                                   /*template_keyword_p=*/false,
4149                                   /*check_dependency_p=*/true,
4150                                   declarator_p);
4151       /* If that worked, we're done.  */
4152       if (cp_parser_parse_definitely (parser))
4153         return id;
4154
4155       /* Peek at the next token.  (Changes in the token buffer may
4156          have invalidated the pointer obtained above.)  */
4157       token = cp_lexer_peek_token (parser->lexer);
4158
4159       switch (token->type)
4160         {
4161         case CPP_NAME:
4162           return cp_parser_identifier (parser);
4163
4164         case CPP_KEYWORD:
4165           if (token->keyword == RID_OPERATOR)
4166             return cp_parser_operator_function_id (parser);
4167           /* Fall through.  */
4168
4169         default:
4170           cp_parser_error (parser, "expected id-expression");
4171           return error_mark_node;
4172         }
4173     }
4174   else
4175     return cp_parser_unqualified_id (parser, template_keyword_p,
4176                                      /*check_dependency_p=*/true,
4177                                      declarator_p,
4178                                      optional_p);
4179 }
4180
4181 /* Parse an unqualified-id.
4182
4183    unqualified-id:
4184      identifier
4185      operator-function-id
4186      conversion-function-id
4187      ~ class-name
4188      template-id
4189
4190    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4191    keyword, in a construct like `A::template ...'.
4192
4193    Returns a representation of unqualified-id.  For the `identifier'
4194    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4195    production a BIT_NOT_EXPR is returned; the operand of the
4196    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4197    other productions, see the documentation accompanying the
4198    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4199    names are looked up in uninstantiated templates.  If DECLARATOR_P
4200    is true, the unqualified-id is appearing as part of a declarator,
4201    rather than as part of an expression.  */
4202
4203 static tree
4204 cp_parser_unqualified_id (cp_parser* parser,
4205                           bool template_keyword_p,
4206                           bool check_dependency_p,
4207                           bool declarator_p,
4208                           bool optional_p)
4209 {
4210   cp_token *token;
4211
4212   /* Peek at the next token.  */
4213   token = cp_lexer_peek_token (parser->lexer);
4214
4215   switch (token->type)
4216     {
4217     case CPP_NAME:
4218       {
4219         tree id;
4220
4221         /* We don't know yet whether or not this will be a
4222            template-id.  */
4223         cp_parser_parse_tentatively (parser);
4224         /* Try a template-id.  */
4225         id = cp_parser_template_id (parser, template_keyword_p,
4226                                     check_dependency_p,
4227                                     declarator_p);
4228         /* If it worked, we're done.  */
4229         if (cp_parser_parse_definitely (parser))
4230           return id;
4231         /* Otherwise, it's an ordinary identifier.  */
4232         return cp_parser_identifier (parser);
4233       }
4234
4235     case CPP_TEMPLATE_ID:
4236       return cp_parser_template_id (parser, template_keyword_p,
4237                                     check_dependency_p,
4238                                     declarator_p);
4239
4240     case CPP_COMPL:
4241       {
4242         tree type_decl;
4243         tree qualifying_scope;
4244         tree object_scope;
4245         tree scope;
4246         bool done;
4247
4248         /* Consume the `~' token.  */
4249         cp_lexer_consume_token (parser->lexer);
4250         /* Parse the class-name.  The standard, as written, seems to
4251            say that:
4252
4253              template <typename T> struct S { ~S (); };
4254              template <typename T> S<T>::~S() {}
4255
4256            is invalid, since `~' must be followed by a class-name, but
4257            `S<T>' is dependent, and so not known to be a class.
4258            That's not right; we need to look in uninstantiated
4259            templates.  A further complication arises from:
4260
4261              template <typename T> void f(T t) {
4262                t.T::~T();
4263              }
4264
4265            Here, it is not possible to look up `T' in the scope of `T'
4266            itself.  We must look in both the current scope, and the
4267            scope of the containing complete expression.
4268
4269            Yet another issue is:
4270
4271              struct S {
4272                int S;
4273                ~S();
4274              };
4275
4276              S::~S() {}
4277
4278            The standard does not seem to say that the `S' in `~S'
4279            should refer to the type `S' and not the data member
4280            `S::S'.  */
4281
4282         /* DR 244 says that we look up the name after the "~" in the
4283            same scope as we looked up the qualifying name.  That idea
4284            isn't fully worked out; it's more complicated than that.  */
4285         scope = parser->scope;
4286         object_scope = parser->object_scope;
4287         qualifying_scope = parser->qualifying_scope;
4288
4289         /* Check for invalid scopes.  */
4290         if (scope == error_mark_node)
4291           {
4292             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4293               cp_lexer_consume_token (parser->lexer);
4294             return error_mark_node;
4295           }
4296         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4297           {
4298             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4299               error_at (token->location,
4300                         "scope %qT before %<~%> is not a class-name",
4301                         scope);
4302             cp_parser_simulate_error (parser);
4303             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4304               cp_lexer_consume_token (parser->lexer);
4305             return error_mark_node;
4306           }
4307         gcc_assert (!scope || TYPE_P (scope));
4308
4309         /* If the name is of the form "X::~X" it's OK even if X is a
4310            typedef.  */
4311         token = cp_lexer_peek_token (parser->lexer);
4312         if (scope
4313             && token->type == CPP_NAME
4314             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4315                 != CPP_LESS)
4316             && (token->u.value == TYPE_IDENTIFIER (scope)
4317                 || constructor_name_p (token->u.value, scope)))
4318           {
4319             cp_lexer_consume_token (parser->lexer);
4320             return build_nt (BIT_NOT_EXPR, scope);
4321           }
4322
4323         /* If there was an explicit qualification (S::~T), first look
4324            in the scope given by the qualification (i.e., S).
4325
4326            Note: in the calls to cp_parser_class_name below we pass
4327            typename_type so that lookup finds the injected-class-name
4328            rather than the constructor.  */
4329         done = false;
4330         type_decl = NULL_TREE;
4331         if (scope)
4332           {
4333             cp_parser_parse_tentatively (parser);
4334             type_decl = cp_parser_class_name (parser,
4335                                               /*typename_keyword_p=*/false,
4336                                               /*template_keyword_p=*/false,
4337                                               typename_type,
4338                                               /*check_dependency=*/false,
4339                                               /*class_head_p=*/false,
4340                                               declarator_p);
4341             if (cp_parser_parse_definitely (parser))
4342               done = true;
4343           }
4344         /* In "N::S::~S", look in "N" as well.  */
4345         if (!done && scope && qualifying_scope)
4346           {
4347             cp_parser_parse_tentatively (parser);
4348             parser->scope = qualifying_scope;
4349             parser->object_scope = NULL_TREE;
4350             parser->qualifying_scope = NULL_TREE;
4351             type_decl
4352               = cp_parser_class_name (parser,
4353                                       /*typename_keyword_p=*/false,
4354                                       /*template_keyword_p=*/false,
4355                                       typename_type,
4356                                       /*check_dependency=*/false,
4357                                       /*class_head_p=*/false,
4358                                       declarator_p);
4359             if (cp_parser_parse_definitely (parser))
4360               done = true;
4361           }
4362         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4363         else if (!done && object_scope)
4364           {
4365             cp_parser_parse_tentatively (parser);
4366             parser->scope = object_scope;
4367             parser->object_scope = NULL_TREE;
4368             parser->qualifying_scope = NULL_TREE;
4369             type_decl
4370               = cp_parser_class_name (parser,
4371                                       /*typename_keyword_p=*/false,
4372                                       /*template_keyword_p=*/false,
4373                                       typename_type,
4374                                       /*check_dependency=*/false,
4375                                       /*class_head_p=*/false,
4376                                       declarator_p);
4377             if (cp_parser_parse_definitely (parser))
4378               done = true;
4379           }
4380         /* Look in the surrounding context.  */
4381         if (!done)
4382           {
4383             parser->scope = NULL_TREE;
4384             parser->object_scope = NULL_TREE;
4385             parser->qualifying_scope = NULL_TREE;
4386             if (processing_template_decl)
4387               cp_parser_parse_tentatively (parser);
4388             type_decl
4389               = cp_parser_class_name (parser,
4390                                       /*typename_keyword_p=*/false,
4391                                       /*template_keyword_p=*/false,
4392                                       typename_type,
4393                                       /*check_dependency=*/false,
4394                                       /*class_head_p=*/false,
4395                                       declarator_p);
4396             if (processing_template_decl
4397                 && ! cp_parser_parse_definitely (parser))
4398               {
4399                 /* We couldn't find a type with this name, so just accept
4400                    it and check for a match at instantiation time.  */
4401                 type_decl = cp_parser_identifier (parser);
4402                 if (type_decl != error_mark_node)
4403                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4404                 return type_decl;
4405               }
4406           }
4407         /* If an error occurred, assume that the name of the
4408            destructor is the same as the name of the qualifying
4409            class.  That allows us to keep parsing after running
4410            into ill-formed destructor names.  */
4411         if (type_decl == error_mark_node && scope)
4412           return build_nt (BIT_NOT_EXPR, scope);
4413         else if (type_decl == error_mark_node)
4414           return error_mark_node;
4415
4416         /* Check that destructor name and scope match.  */
4417         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4418           {
4419             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4420               error_at (token->location,
4421                         "declaration of %<~%T%> as member of %qT",
4422                         type_decl, scope);
4423             cp_parser_simulate_error (parser);
4424             return error_mark_node;
4425           }
4426
4427         /* [class.dtor]
4428
4429            A typedef-name that names a class shall not be used as the
4430            identifier in the declarator for a destructor declaration.  */
4431         if (declarator_p
4432             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4433             && !DECL_SELF_REFERENCE_P (type_decl)
4434             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4435           error_at (token->location,
4436                     "typedef-name %qD used as destructor declarator",
4437                     type_decl);
4438
4439         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4440       }
4441
4442     case CPP_KEYWORD:
4443       if (token->keyword == RID_OPERATOR)
4444         {
4445           tree id;
4446
4447           /* This could be a template-id, so we try that first.  */
4448           cp_parser_parse_tentatively (parser);
4449           /* Try a template-id.  */
4450           id = cp_parser_template_id (parser, template_keyword_p,
4451                                       /*check_dependency_p=*/true,
4452                                       declarator_p);
4453           /* If that worked, we're done.  */
4454           if (cp_parser_parse_definitely (parser))
4455             return id;
4456           /* We still don't know whether we're looking at an
4457              operator-function-id or a conversion-function-id.  */
4458           cp_parser_parse_tentatively (parser);
4459           /* Try an operator-function-id.  */
4460           id = cp_parser_operator_function_id (parser);
4461           /* If that didn't work, try a conversion-function-id.  */
4462           if (!cp_parser_parse_definitely (parser))
4463             id = cp_parser_conversion_function_id (parser);
4464
4465           return id;
4466         }
4467       /* Fall through.  */
4468
4469     default:
4470       if (optional_p)
4471         return NULL_TREE;
4472       cp_parser_error (parser, "expected unqualified-id");
4473       return error_mark_node;
4474     }
4475 }
4476
4477 /* Parse an (optional) nested-name-specifier.
4478
4479    nested-name-specifier: [C++98]
4480      class-or-namespace-name :: nested-name-specifier [opt]
4481      class-or-namespace-name :: template nested-name-specifier [opt]
4482
4483    nested-name-specifier: [C++0x]
4484      type-name ::
4485      namespace-name ::
4486      nested-name-specifier identifier ::
4487      nested-name-specifier template [opt] simple-template-id ::
4488
4489    PARSER->SCOPE should be set appropriately before this function is
4490    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4491    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4492    in name lookups.
4493
4494    Sets PARSER->SCOPE to the class (TYPE) or namespace
4495    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4496    it unchanged if there is no nested-name-specifier.  Returns the new
4497    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4498
4499    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4500    part of a declaration and/or decl-specifier.  */
4501
4502 static tree
4503 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4504                                      bool typename_keyword_p,
4505                                      bool check_dependency_p,
4506                                      bool type_p,
4507                                      bool is_declaration)
4508 {
4509   bool success = false;
4510   cp_token_position start = 0;
4511   cp_token *token;
4512
4513   /* Remember where the nested-name-specifier starts.  */
4514   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4515     {
4516       start = cp_lexer_token_position (parser->lexer, false);
4517       push_deferring_access_checks (dk_deferred);
4518     }
4519
4520   while (true)
4521     {
4522       tree new_scope;
4523       tree old_scope;
4524       tree saved_qualifying_scope;
4525       bool template_keyword_p;
4526
4527       /* Spot cases that cannot be the beginning of a
4528          nested-name-specifier.  */
4529       token = cp_lexer_peek_token (parser->lexer);
4530
4531       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4532          the already parsed nested-name-specifier.  */
4533       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4534         {
4535           /* Grab the nested-name-specifier and continue the loop.  */
4536           cp_parser_pre_parsed_nested_name_specifier (parser);
4537           /* If we originally encountered this nested-name-specifier
4538              with IS_DECLARATION set to false, we will not have
4539              resolved TYPENAME_TYPEs, so we must do so here.  */
4540           if (is_declaration
4541               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4542             {
4543               new_scope = resolve_typename_type (parser->scope,
4544                                                  /*only_current_p=*/false);
4545               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4546                 parser->scope = new_scope;
4547             }
4548           success = true;
4549           continue;
4550         }
4551
4552       /* Spot cases that cannot be the beginning of a
4553          nested-name-specifier.  On the second and subsequent times
4554          through the loop, we look for the `template' keyword.  */
4555       if (success && token->keyword == RID_TEMPLATE)
4556         ;
4557       /* A template-id can start a nested-name-specifier.  */
4558       else if (token->type == CPP_TEMPLATE_ID)
4559         ;
4560       else
4561         {
4562           /* If the next token is not an identifier, then it is
4563              definitely not a type-name or namespace-name.  */
4564           if (token->type != CPP_NAME)
4565             break;
4566           /* If the following token is neither a `<' (to begin a
4567              template-id), nor a `::', then we are not looking at a
4568              nested-name-specifier.  */
4569           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4570
4571           if (token->type == CPP_COLON
4572               && parser->colon_corrects_to_scope_p
4573               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4574             {
4575               error_at (token->location,
4576                         "found %<:%> in nested-name-specifier, expected %<::%>");
4577               token->type = CPP_SCOPE;
4578             }
4579
4580           if (token->type != CPP_SCOPE
4581               && !cp_parser_nth_token_starts_template_argument_list_p
4582                   (parser, 2))
4583             break;
4584         }
4585
4586       /* The nested-name-specifier is optional, so we parse
4587          tentatively.  */
4588       cp_parser_parse_tentatively (parser);
4589
4590       /* Look for the optional `template' keyword, if this isn't the
4591          first time through the loop.  */
4592       if (success)
4593         template_keyword_p = cp_parser_optional_template_keyword (parser);
4594       else
4595         template_keyword_p = false;
4596
4597       /* Save the old scope since the name lookup we are about to do
4598          might destroy it.  */
4599       old_scope = parser->scope;
4600       saved_qualifying_scope = parser->qualifying_scope;
4601       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4602          look up names in "X<T>::I" in order to determine that "Y" is
4603          a template.  So, if we have a typename at this point, we make
4604          an effort to look through it.  */
4605       if (is_declaration
4606           && !typename_keyword_p
4607           && parser->scope
4608           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4609         parser->scope = resolve_typename_type (parser->scope,
4610                                                /*only_current_p=*/false);
4611       /* Parse the qualifying entity.  */
4612       new_scope
4613         = cp_parser_qualifying_entity (parser,
4614                                        typename_keyword_p,
4615                                        template_keyword_p,
4616                                        check_dependency_p,
4617                                        type_p,
4618                                        is_declaration);
4619       /* Look for the `::' token.  */
4620       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4621
4622       /* If we found what we wanted, we keep going; otherwise, we're
4623          done.  */
4624       if (!cp_parser_parse_definitely (parser))
4625         {
4626           bool error_p = false;
4627
4628           /* Restore the OLD_SCOPE since it was valid before the
4629              failed attempt at finding the last
4630              class-or-namespace-name.  */
4631           parser->scope = old_scope;
4632           parser->qualifying_scope = saved_qualifying_scope;
4633           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4634             break;
4635           /* If the next token is an identifier, and the one after
4636              that is a `::', then any valid interpretation would have
4637              found a class-or-namespace-name.  */
4638           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4639                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4640                      == CPP_SCOPE)
4641                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4642                      != CPP_COMPL))
4643             {
4644               token = cp_lexer_consume_token (parser->lexer);
4645               if (!error_p)
4646                 {
4647                   if (!token->ambiguous_p)
4648                     {
4649                       tree decl;
4650                       tree ambiguous_decls;
4651
4652                       decl = cp_parser_lookup_name (parser, token->u.value,
4653                                                     none_type,
4654                                                     /*is_template=*/false,
4655                                                     /*is_namespace=*/false,
4656                                                     /*check_dependency=*/true,
4657                                                     &ambiguous_decls,
4658                                                     token->location);
4659                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4660                         error_at (token->location,
4661                                   "%qD used without template parameters",
4662                                   decl);
4663                       else if (ambiguous_decls)
4664                         {
4665                           error_at (token->location,
4666                                     "reference to %qD is ambiguous",
4667                                     token->u.value);
4668                           print_candidates (ambiguous_decls);
4669                           decl = error_mark_node;
4670                         }
4671                       else
4672                         {
4673                           if (cxx_dialect != cxx98)
4674                             cp_parser_name_lookup_error
4675                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4676                              token->location);
4677                           else
4678                             cp_parser_name_lookup_error
4679                             (parser, token->u.value, decl, NLE_CXX98,
4680                              token->location);
4681                         }
4682                     }
4683                   parser->scope = error_mark_node;
4684                   error_p = true;
4685                   /* Treat this as a successful nested-name-specifier
4686                      due to:
4687
4688                      [basic.lookup.qual]
4689
4690                      If the name found is not a class-name (clause
4691                      _class_) or namespace-name (_namespace.def_), the
4692                      program is ill-formed.  */
4693                   success = true;
4694                 }
4695               cp_lexer_consume_token (parser->lexer);
4696             }
4697           break;
4698         }
4699       /* We've found one valid nested-name-specifier.  */
4700       success = true;
4701       /* Name lookup always gives us a DECL.  */
4702       if (TREE_CODE (new_scope) == TYPE_DECL)
4703         new_scope = TREE_TYPE (new_scope);
4704       /* Uses of "template" must be followed by actual templates.  */
4705       if (template_keyword_p
4706           && !(CLASS_TYPE_P (new_scope)
4707                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4708                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4709                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4710           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4711                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4712                    == TEMPLATE_ID_EXPR)))
4713         permerror (input_location, TYPE_P (new_scope)
4714                    ? "%qT is not a template"
4715                    : "%qD is not a template",
4716                    new_scope);
4717       /* If it is a class scope, try to complete it; we are about to
4718          be looking up names inside the class.  */
4719       if (TYPE_P (new_scope)
4720           /* Since checking types for dependency can be expensive,
4721              avoid doing it if the type is already complete.  */
4722           && !COMPLETE_TYPE_P (new_scope)
4723           /* Do not try to complete dependent types.  */
4724           && !dependent_type_p (new_scope))
4725         {
4726           new_scope = complete_type (new_scope);
4727           /* If it is a typedef to current class, use the current
4728              class instead, as the typedef won't have any names inside
4729              it yet.  */
4730           if (!COMPLETE_TYPE_P (new_scope)
4731               && currently_open_class (new_scope))
4732             new_scope = TYPE_MAIN_VARIANT (new_scope);
4733         }
4734       /* Make sure we look in the right scope the next time through
4735          the loop.  */
4736       parser->scope = new_scope;
4737     }
4738
4739   /* If parsing tentatively, replace the sequence of tokens that makes
4740      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4741      token.  That way, should we re-parse the token stream, we will
4742      not have to repeat the effort required to do the parse, nor will
4743      we issue duplicate error messages.  */
4744   if (success && start)
4745     {
4746       cp_token *token;
4747
4748       token = cp_lexer_token_at (parser->lexer, start);
4749       /* Reset the contents of the START token.  */
4750       token->type = CPP_NESTED_NAME_SPECIFIER;
4751       /* Retrieve any deferred checks.  Do not pop this access checks yet
4752          so the memory will not be reclaimed during token replacing below.  */
4753       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4754       token->u.tree_check_value->value = parser->scope;
4755       token->u.tree_check_value->checks = get_deferred_access_checks ();
4756       token->u.tree_check_value->qualifying_scope =
4757         parser->qualifying_scope;
4758       token->keyword = RID_MAX;
4759
4760       /* Purge all subsequent tokens.  */
4761       cp_lexer_purge_tokens_after (parser->lexer, start);
4762     }
4763
4764   if (start)
4765     pop_to_parent_deferring_access_checks ();
4766
4767   return success ? parser->scope : NULL_TREE;
4768 }
4769
4770 /* Parse a nested-name-specifier.  See
4771    cp_parser_nested_name_specifier_opt for details.  This function
4772    behaves identically, except that it will an issue an error if no
4773    nested-name-specifier is present.  */
4774
4775 static tree
4776 cp_parser_nested_name_specifier (cp_parser *parser,
4777                                  bool typename_keyword_p,
4778                                  bool check_dependency_p,
4779                                  bool type_p,
4780                                  bool is_declaration)
4781 {
4782   tree scope;
4783
4784   /* Look for the nested-name-specifier.  */
4785   scope = cp_parser_nested_name_specifier_opt (parser,
4786                                                typename_keyword_p,
4787                                                check_dependency_p,
4788                                                type_p,
4789                                                is_declaration);
4790   /* If it was not present, issue an error message.  */
4791   if (!scope)
4792     {
4793       cp_parser_error (parser, "expected nested-name-specifier");
4794       parser->scope = NULL_TREE;
4795     }
4796
4797   return scope;
4798 }
4799
4800 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4801    this is either a class-name or a namespace-name (which corresponds
4802    to the class-or-namespace-name production in the grammar). For
4803    C++0x, it can also be a type-name that refers to an enumeration
4804    type.
4805
4806    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4807    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4808    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4809    TYPE_P is TRUE iff the next name should be taken as a class-name,
4810    even the same name is declared to be another entity in the same
4811    scope.
4812
4813    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4814    specified by the class-or-namespace-name.  If neither is found the
4815    ERROR_MARK_NODE is returned.  */
4816
4817 static tree
4818 cp_parser_qualifying_entity (cp_parser *parser,
4819                              bool typename_keyword_p,
4820                              bool template_keyword_p,
4821                              bool check_dependency_p,
4822                              bool type_p,
4823                              bool is_declaration)
4824 {
4825   tree saved_scope;
4826   tree saved_qualifying_scope;
4827   tree saved_object_scope;
4828   tree scope;
4829   bool only_class_p;
4830   bool successful_parse_p;
4831
4832   /* Before we try to parse the class-name, we must save away the
4833      current PARSER->SCOPE since cp_parser_class_name will destroy
4834      it.  */
4835   saved_scope = parser->scope;
4836   saved_qualifying_scope = parser->qualifying_scope;
4837   saved_object_scope = parser->object_scope;
4838   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4839      there is no need to look for a namespace-name.  */
4840   only_class_p = template_keyword_p 
4841     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4842   if (!only_class_p)
4843     cp_parser_parse_tentatively (parser);
4844   scope = cp_parser_class_name (parser,
4845                                 typename_keyword_p,
4846                                 template_keyword_p,
4847                                 type_p ? class_type : none_type,
4848                                 check_dependency_p,
4849                                 /*class_head_p=*/false,
4850                                 is_declaration);
4851   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4852   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4853   if (!only_class_p 
4854       && cxx_dialect != cxx98
4855       && !successful_parse_p)
4856     {
4857       /* Restore the saved scope.  */
4858       parser->scope = saved_scope;
4859       parser->qualifying_scope = saved_qualifying_scope;
4860       parser->object_scope = saved_object_scope;
4861
4862       /* Parse tentatively.  */
4863       cp_parser_parse_tentatively (parser);
4864      
4865       /* Parse a typedef-name or enum-name.  */
4866       scope = cp_parser_nonclass_name (parser);
4867
4868       /* "If the name found does not designate a namespace or a class,
4869          enumeration, or dependent type, the program is ill-formed."
4870
4871          We cover classes and dependent types above and namespaces below,
4872          so this code is only looking for enums.  */
4873       if (!scope || TREE_CODE (scope) != TYPE_DECL
4874           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4875         cp_parser_simulate_error (parser);
4876
4877       successful_parse_p = cp_parser_parse_definitely (parser);
4878     }
4879   /* If that didn't work, try for a namespace-name.  */
4880   if (!only_class_p && !successful_parse_p)
4881     {
4882       /* Restore the saved scope.  */
4883       parser->scope = saved_scope;
4884       parser->qualifying_scope = saved_qualifying_scope;
4885       parser->object_scope = saved_object_scope;
4886       /* If we are not looking at an identifier followed by the scope
4887          resolution operator, then this is not part of a
4888          nested-name-specifier.  (Note that this function is only used
4889          to parse the components of a nested-name-specifier.)  */
4890       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4891           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4892         return error_mark_node;
4893       scope = cp_parser_namespace_name (parser);
4894     }
4895
4896   return scope;
4897 }
4898
4899 /* Parse a postfix-expression.
4900
4901    postfix-expression:
4902      primary-expression
4903      postfix-expression [ expression ]
4904      postfix-expression ( expression-list [opt] )
4905      simple-type-specifier ( expression-list [opt] )
4906      typename :: [opt] nested-name-specifier identifier
4907        ( expression-list [opt] )
4908      typename :: [opt] nested-name-specifier template [opt] template-id
4909        ( expression-list [opt] )
4910      postfix-expression . template [opt] id-expression
4911      postfix-expression -> template [opt] id-expression
4912      postfix-expression . pseudo-destructor-name
4913      postfix-expression -> pseudo-destructor-name
4914      postfix-expression ++
4915      postfix-expression --
4916      dynamic_cast < type-id > ( expression )
4917      static_cast < type-id > ( expression )
4918      reinterpret_cast < type-id > ( expression )
4919      const_cast < type-id > ( expression )
4920      typeid ( expression )
4921      typeid ( type-id )
4922
4923    GNU Extension:
4924
4925    postfix-expression:
4926      ( type-id ) { initializer-list , [opt] }
4927
4928    This extension is a GNU version of the C99 compound-literal
4929    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4930    but they are essentially the same concept.)
4931
4932    If ADDRESS_P is true, the postfix expression is the operand of the
4933    `&' operator.  CAST_P is true if this expression is the target of a
4934    cast.
4935
4936    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4937    class member access expressions [expr.ref].
4938
4939    Returns a representation of the expression.  */
4940
4941 static tree
4942 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4943                               bool member_access_only_p,
4944                               cp_id_kind * pidk_return)
4945 {
4946   cp_token *token;
4947   enum rid keyword;
4948   cp_id_kind idk = CP_ID_KIND_NONE;
4949   tree postfix_expression = NULL_TREE;
4950   bool is_member_access = false;
4951
4952   /* Peek at the next token.  */
4953   token = cp_lexer_peek_token (parser->lexer);
4954   /* Some of the productions are determined by keywords.  */
4955   keyword = token->keyword;
4956   switch (keyword)
4957     {
4958     case RID_DYNCAST:
4959     case RID_STATCAST:
4960     case RID_REINTCAST:
4961     case RID_CONSTCAST:
4962       {
4963         tree type;
4964         tree expression;
4965         const char *saved_message;
4966
4967         /* All of these can be handled in the same way from the point
4968            of view of parsing.  Begin by consuming the token
4969            identifying the cast.  */
4970         cp_lexer_consume_token (parser->lexer);
4971
4972         /* New types cannot be defined in the cast.  */
4973         saved_message = parser->type_definition_forbidden_message;
4974         parser->type_definition_forbidden_message
4975           = G_("types may not be defined in casts");
4976
4977         /* Look for the opening `<'.  */
4978         cp_parser_require (parser, CPP_LESS, RT_LESS);
4979         /* Parse the type to which we are casting.  */
4980         type = cp_parser_type_id (parser);
4981         /* Look for the closing `>'.  */
4982         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4983         /* Restore the old message.  */
4984         parser->type_definition_forbidden_message = saved_message;
4985
4986         /* And the expression which is being cast.  */
4987         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4988         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4989         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4990
4991         /* Only type conversions to integral or enumeration types
4992            can be used in constant-expressions.  */
4993         if (!cast_valid_in_integral_constant_expression_p (type)
4994             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4995           return error_mark_node;
4996
4997         switch (keyword)
4998           {
4999           case RID_DYNCAST:
5000             postfix_expression
5001               = build_dynamic_cast (type, expression, tf_warning_or_error);
5002             break;
5003           case RID_STATCAST:
5004             postfix_expression
5005               = build_static_cast (type, expression, tf_warning_or_error);
5006             break;
5007           case RID_REINTCAST:
5008             postfix_expression
5009               = build_reinterpret_cast (type, expression, 
5010                                         tf_warning_or_error);
5011             break;
5012           case RID_CONSTCAST:
5013             postfix_expression
5014               = build_const_cast (type, expression, tf_warning_or_error);
5015             break;
5016           default:
5017             gcc_unreachable ();
5018           }
5019       }
5020       break;
5021
5022     case RID_TYPEID:
5023       {
5024         tree type;
5025         const char *saved_message;
5026         bool saved_in_type_id_in_expr_p;
5027
5028         /* Consume the `typeid' token.  */
5029         cp_lexer_consume_token (parser->lexer);
5030         /* Look for the `(' token.  */
5031         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5032         /* Types cannot be defined in a `typeid' expression.  */
5033         saved_message = parser->type_definition_forbidden_message;
5034         parser->type_definition_forbidden_message
5035           = G_("types may not be defined in a %<typeid%> expression");
5036         /* We can't be sure yet whether we're looking at a type-id or an
5037            expression.  */
5038         cp_parser_parse_tentatively (parser);
5039         /* Try a type-id first.  */
5040         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5041         parser->in_type_id_in_expr_p = true;
5042         type = cp_parser_type_id (parser);
5043         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5044         /* Look for the `)' token.  Otherwise, we can't be sure that
5045            we're not looking at an expression: consider `typeid (int
5046            (3))', for example.  */
5047         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5048         /* If all went well, simply lookup the type-id.  */
5049         if (cp_parser_parse_definitely (parser))
5050           postfix_expression = get_typeid (type);
5051         /* Otherwise, fall back to the expression variant.  */
5052         else
5053           {
5054             tree expression;
5055
5056             /* Look for an expression.  */
5057             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5058             /* Compute its typeid.  */
5059             postfix_expression = build_typeid (expression);
5060             /* Look for the `)' token.  */
5061             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5062           }
5063         /* Restore the saved message.  */
5064         parser->type_definition_forbidden_message = saved_message;
5065         /* `typeid' may not appear in an integral constant expression.  */
5066         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5067           return error_mark_node;
5068       }
5069       break;
5070
5071     case RID_TYPENAME:
5072       {
5073         tree type;
5074         /* The syntax permitted here is the same permitted for an
5075            elaborated-type-specifier.  */
5076         type = cp_parser_elaborated_type_specifier (parser,
5077                                                     /*is_friend=*/false,
5078                                                     /*is_declaration=*/false);
5079         postfix_expression = cp_parser_functional_cast (parser, type);
5080       }
5081       break;
5082
5083     default:
5084       {
5085         tree type;
5086
5087         /* If the next thing is a simple-type-specifier, we may be
5088            looking at a functional cast.  We could also be looking at
5089            an id-expression.  So, we try the functional cast, and if
5090            that doesn't work we fall back to the primary-expression.  */
5091         cp_parser_parse_tentatively (parser);
5092         /* Look for the simple-type-specifier.  */
5093         type = cp_parser_simple_type_specifier (parser,
5094                                                 /*decl_specs=*/NULL,
5095                                                 CP_PARSER_FLAGS_NONE);
5096         /* Parse the cast itself.  */
5097         if (!cp_parser_error_occurred (parser))
5098           postfix_expression
5099             = cp_parser_functional_cast (parser, type);
5100         /* If that worked, we're done.  */
5101         if (cp_parser_parse_definitely (parser))
5102           break;
5103
5104         /* If the functional-cast didn't work out, try a
5105            compound-literal.  */
5106         if (cp_parser_allow_gnu_extensions_p (parser)
5107             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5108           {
5109             VEC(constructor_elt,gc) *initializer_list = NULL;
5110             bool saved_in_type_id_in_expr_p;
5111
5112             cp_parser_parse_tentatively (parser);
5113             /* Consume the `('.  */
5114             cp_lexer_consume_token (parser->lexer);
5115             /* Parse the type.  */
5116             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5117             parser->in_type_id_in_expr_p = true;
5118             type = cp_parser_type_id (parser);
5119             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5120             /* Look for the `)'.  */
5121             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5122             /* Look for the `{'.  */
5123             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5124             /* If things aren't going well, there's no need to
5125                keep going.  */
5126             if (!cp_parser_error_occurred (parser))
5127               {
5128                 bool non_constant_p;
5129                 /* Parse the initializer-list.  */
5130                 initializer_list
5131                   = cp_parser_initializer_list (parser, &non_constant_p);
5132                 /* Allow a trailing `,'.  */
5133                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5134                   cp_lexer_consume_token (parser->lexer);
5135                 /* Look for the final `}'.  */
5136                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5137               }
5138             /* If that worked, we're definitely looking at a
5139                compound-literal expression.  */
5140             if (cp_parser_parse_definitely (parser))
5141               {
5142                 /* Warn the user that a compound literal is not
5143                    allowed in standard C++.  */
5144                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5145                 /* For simplicity, we disallow compound literals in
5146                    constant-expressions.  We could
5147                    allow compound literals of integer type, whose
5148                    initializer was a constant, in constant
5149                    expressions.  Permitting that usage, as a further
5150                    extension, would not change the meaning of any
5151                    currently accepted programs.  (Of course, as
5152                    compound literals are not part of ISO C++, the
5153                    standard has nothing to say.)  */
5154                 if (cp_parser_non_integral_constant_expression (parser,
5155                                                                 NIC_NCC))
5156                   {
5157                     postfix_expression = error_mark_node;
5158                     break;
5159                   }
5160                 /* Form the representation of the compound-literal.  */
5161                 postfix_expression
5162                   = (finish_compound_literal
5163                      (type, build_constructor (init_list_type_node,
5164                                                initializer_list)));
5165                 break;
5166               }
5167           }
5168
5169         /* It must be a primary-expression.  */
5170         postfix_expression
5171           = cp_parser_primary_expression (parser, address_p, cast_p,
5172                                           /*template_arg_p=*/false,
5173                                           &idk);
5174       }
5175       break;
5176     }
5177
5178   /* Keep looping until the postfix-expression is complete.  */
5179   while (true)
5180     {
5181       if (idk == CP_ID_KIND_UNQUALIFIED
5182           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5183           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5184         /* It is not a Koenig lookup function call.  */
5185         postfix_expression
5186           = unqualified_name_lookup_error (postfix_expression);
5187
5188       /* Peek at the next token.  */
5189       token = cp_lexer_peek_token (parser->lexer);
5190
5191       switch (token->type)
5192         {
5193         case CPP_OPEN_SQUARE:
5194           postfix_expression
5195             = cp_parser_postfix_open_square_expression (parser,
5196                                                         postfix_expression,
5197                                                         false);
5198           idk = CP_ID_KIND_NONE;
5199           is_member_access = false;
5200           break;
5201
5202         case CPP_OPEN_PAREN:
5203           /* postfix-expression ( expression-list [opt] ) */
5204           {
5205             bool koenig_p;
5206             bool is_builtin_constant_p;
5207             bool saved_integral_constant_expression_p = false;
5208             bool saved_non_integral_constant_expression_p = false;
5209             VEC(tree,gc) *args;
5210
5211             is_member_access = false;
5212
5213             is_builtin_constant_p
5214               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5215             if (is_builtin_constant_p)
5216               {
5217                 /* The whole point of __builtin_constant_p is to allow
5218                    non-constant expressions to appear as arguments.  */
5219                 saved_integral_constant_expression_p
5220                   = parser->integral_constant_expression_p;
5221                 saved_non_integral_constant_expression_p
5222                   = parser->non_integral_constant_expression_p;
5223                 parser->integral_constant_expression_p = false;
5224               }
5225             args = (cp_parser_parenthesized_expression_list
5226                     (parser, non_attr,
5227                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5228                      /*non_constant_p=*/NULL));
5229             if (is_builtin_constant_p)
5230               {
5231                 parser->integral_constant_expression_p
5232                   = saved_integral_constant_expression_p;
5233                 parser->non_integral_constant_expression_p
5234                   = saved_non_integral_constant_expression_p;
5235               }
5236
5237             if (args == NULL)
5238               {
5239                 postfix_expression = error_mark_node;
5240                 break;
5241               }
5242
5243             /* Function calls are not permitted in
5244                constant-expressions.  */
5245             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5246                 && cp_parser_non_integral_constant_expression (parser,
5247                                                                NIC_FUNC_CALL))
5248               {
5249                 postfix_expression = error_mark_node;
5250                 release_tree_vector (args);
5251                 break;
5252               }
5253
5254             koenig_p = false;
5255             if (idk == CP_ID_KIND_UNQUALIFIED
5256                 || idk == CP_ID_KIND_TEMPLATE_ID)
5257               {
5258                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5259                   {
5260                     if (!VEC_empty (tree, args))
5261                       {
5262                         koenig_p = true;
5263                         if (!any_type_dependent_arguments_p (args))
5264                           postfix_expression
5265                             = perform_koenig_lookup (postfix_expression, args,
5266                                                      /*include_std=*/false);
5267                       }
5268                     else
5269                       postfix_expression
5270                         = unqualified_fn_lookup_error (postfix_expression);
5271                   }
5272                 /* We do not perform argument-dependent lookup if
5273                    normal lookup finds a non-function, in accordance
5274                    with the expected resolution of DR 218.  */
5275                 else if (!VEC_empty (tree, args)
5276                          && is_overloaded_fn (postfix_expression))
5277                   {
5278                     tree fn = get_first_fn (postfix_expression);
5279                     fn = STRIP_TEMPLATE (fn);
5280
5281                     /* Do not do argument dependent lookup if regular
5282                        lookup finds a member function or a block-scope
5283                        function declaration.  [basic.lookup.argdep]/3  */
5284                     if (!DECL_FUNCTION_MEMBER_P (fn)
5285                         && !DECL_LOCAL_FUNCTION_P (fn))
5286                       {
5287                         koenig_p = true;
5288                         if (!any_type_dependent_arguments_p (args))
5289                           postfix_expression
5290                             = perform_koenig_lookup (postfix_expression, args,
5291                                                      /*include_std=*/false);
5292                       }
5293                   }
5294               }
5295
5296             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5297               {
5298                 tree instance = TREE_OPERAND (postfix_expression, 0);
5299                 tree fn = TREE_OPERAND (postfix_expression, 1);
5300
5301                 if (processing_template_decl
5302                     && (type_dependent_expression_p (instance)
5303                         || (!BASELINK_P (fn)
5304                             && TREE_CODE (fn) != FIELD_DECL)
5305                         || type_dependent_expression_p (fn)
5306                         || any_type_dependent_arguments_p (args)))
5307                   {
5308                     postfix_expression
5309                       = build_nt_call_vec (postfix_expression, args);
5310                     release_tree_vector (args);
5311                     break;
5312                   }
5313
5314                 if (BASELINK_P (fn))
5315                   {
5316                   postfix_expression
5317                     = (build_new_method_call
5318                        (instance, fn, &args, NULL_TREE,
5319                         (idk == CP_ID_KIND_QUALIFIED
5320                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5321                         /*fn_p=*/NULL,
5322                         tf_warning_or_error));
5323                   }
5324                 else
5325                   postfix_expression
5326                     = finish_call_expr (postfix_expression, &args,
5327                                         /*disallow_virtual=*/false,
5328                                         /*koenig_p=*/false,
5329                                         tf_warning_or_error);
5330               }
5331             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5332                      || TREE_CODE (postfix_expression) == MEMBER_REF
5333                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5334               postfix_expression = (build_offset_ref_call_from_tree
5335                                     (postfix_expression, &args));
5336             else if (idk == CP_ID_KIND_QUALIFIED)
5337               /* A call to a static class member, or a namespace-scope
5338                  function.  */
5339               postfix_expression
5340                 = finish_call_expr (postfix_expression, &args,
5341                                     /*disallow_virtual=*/true,
5342                                     koenig_p,
5343                                     tf_warning_or_error);
5344             else
5345               /* All other function calls.  */
5346               postfix_expression
5347                 = finish_call_expr (postfix_expression, &args,
5348                                     /*disallow_virtual=*/false,
5349                                     koenig_p,
5350                                     tf_warning_or_error);
5351
5352             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5353             idk = CP_ID_KIND_NONE;
5354
5355             release_tree_vector (args);
5356           }
5357           break;
5358
5359         case CPP_DOT:
5360         case CPP_DEREF:
5361           /* postfix-expression . template [opt] id-expression
5362              postfix-expression . pseudo-destructor-name
5363              postfix-expression -> template [opt] id-expression
5364              postfix-expression -> pseudo-destructor-name */
5365
5366           /* Consume the `.' or `->' operator.  */
5367           cp_lexer_consume_token (parser->lexer);
5368
5369           postfix_expression
5370             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5371                                                       postfix_expression,
5372                                                       false, &idk,
5373                                                       token->location);
5374
5375           is_member_access = true;
5376           break;
5377
5378         case CPP_PLUS_PLUS:
5379           /* postfix-expression ++  */
5380           /* Consume the `++' token.  */
5381           cp_lexer_consume_token (parser->lexer);
5382           /* Generate a representation for the complete expression.  */
5383           postfix_expression
5384             = finish_increment_expr (postfix_expression,
5385                                      POSTINCREMENT_EXPR);
5386           /* Increments may not appear in constant-expressions.  */
5387           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5388             postfix_expression = error_mark_node;
5389           idk = CP_ID_KIND_NONE;
5390           is_member_access = false;
5391           break;
5392
5393         case CPP_MINUS_MINUS:
5394           /* postfix-expression -- */
5395           /* Consume the `--' token.  */
5396           cp_lexer_consume_token (parser->lexer);
5397           /* Generate a representation for the complete expression.  */
5398           postfix_expression
5399             = finish_increment_expr (postfix_expression,
5400                                      POSTDECREMENT_EXPR);
5401           /* Decrements may not appear in constant-expressions.  */
5402           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5403             postfix_expression = error_mark_node;
5404           idk = CP_ID_KIND_NONE;
5405           is_member_access = false;
5406           break;
5407
5408         default:
5409           if (pidk_return != NULL)
5410             * pidk_return = idk;
5411           if (member_access_only_p)
5412             return is_member_access? postfix_expression : error_mark_node;
5413           else
5414             return postfix_expression;
5415         }
5416     }
5417
5418   /* We should never get here.  */
5419   gcc_unreachable ();
5420   return error_mark_node;
5421 }
5422
5423 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5424    by cp_parser_builtin_offsetof.  We're looking for
5425
5426      postfix-expression [ expression ]
5427
5428    FOR_OFFSETOF is set if we're being called in that context, which
5429    changes how we deal with integer constant expressions.  */
5430
5431 static tree
5432 cp_parser_postfix_open_square_expression (cp_parser *parser,
5433                                           tree postfix_expression,
5434                                           bool for_offsetof)
5435 {
5436   tree index;
5437
5438   /* Consume the `[' token.  */
5439   cp_lexer_consume_token (parser->lexer);
5440
5441   /* Parse the index expression.  */
5442   /* ??? For offsetof, there is a question of what to allow here.  If
5443      offsetof is not being used in an integral constant expression context,
5444      then we *could* get the right answer by computing the value at runtime.
5445      If we are in an integral constant expression context, then we might
5446      could accept any constant expression; hard to say without analysis.
5447      Rather than open the barn door too wide right away, allow only integer
5448      constant expressions here.  */
5449   if (for_offsetof)
5450     index = cp_parser_constant_expression (parser, false, NULL);
5451   else
5452     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5453
5454   /* Look for the closing `]'.  */
5455   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5456
5457   /* Build the ARRAY_REF.  */
5458   postfix_expression = grok_array_decl (postfix_expression, index);
5459
5460   /* When not doing offsetof, array references are not permitted in
5461      constant-expressions.  */
5462   if (!for_offsetof
5463       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5464     postfix_expression = error_mark_node;
5465
5466   return postfix_expression;
5467 }
5468
5469 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5470    by cp_parser_builtin_offsetof.  We're looking for
5471
5472      postfix-expression . template [opt] id-expression
5473      postfix-expression . pseudo-destructor-name
5474      postfix-expression -> template [opt] id-expression
5475      postfix-expression -> pseudo-destructor-name
5476
5477    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5478    limits what of the above we'll actually accept, but nevermind.
5479    TOKEN_TYPE is the "." or "->" token, which will already have been
5480    removed from the stream.  */
5481
5482 static tree
5483 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5484                                         enum cpp_ttype token_type,
5485                                         tree postfix_expression,
5486                                         bool for_offsetof, cp_id_kind *idk,
5487                                         location_t location)
5488 {
5489   tree name;
5490   bool dependent_p;
5491   bool pseudo_destructor_p;
5492   tree scope = NULL_TREE;
5493
5494   /* If this is a `->' operator, dereference the pointer.  */
5495   if (token_type == CPP_DEREF)
5496     postfix_expression = build_x_arrow (postfix_expression);
5497   /* Check to see whether or not the expression is type-dependent.  */
5498   dependent_p = type_dependent_expression_p (postfix_expression);
5499   /* The identifier following the `->' or `.' is not qualified.  */
5500   parser->scope = NULL_TREE;
5501   parser->qualifying_scope = NULL_TREE;
5502   parser->object_scope = NULL_TREE;
5503   *idk = CP_ID_KIND_NONE;
5504
5505   /* Enter the scope corresponding to the type of the object
5506      given by the POSTFIX_EXPRESSION.  */
5507   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5508     {
5509       scope = TREE_TYPE (postfix_expression);
5510       /* According to the standard, no expression should ever have
5511          reference type.  Unfortunately, we do not currently match
5512          the standard in this respect in that our internal representation
5513          of an expression may have reference type even when the standard
5514          says it does not.  Therefore, we have to manually obtain the
5515          underlying type here.  */
5516       scope = non_reference (scope);
5517       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5518       if (scope == unknown_type_node)
5519         {
5520           error_at (location, "%qE does not have class type",
5521                     postfix_expression);
5522           scope = NULL_TREE;
5523         }
5524       else
5525         scope = complete_type_or_else (scope, NULL_TREE);
5526       /* Let the name lookup machinery know that we are processing a
5527          class member access expression.  */
5528       parser->context->object_type = scope;
5529       /* If something went wrong, we want to be able to discern that case,
5530          as opposed to the case where there was no SCOPE due to the type
5531          of expression being dependent.  */
5532       if (!scope)
5533         scope = error_mark_node;
5534       /* If the SCOPE was erroneous, make the various semantic analysis
5535          functions exit quickly -- and without issuing additional error
5536          messages.  */
5537       if (scope == error_mark_node)
5538         postfix_expression = error_mark_node;
5539     }
5540
5541   /* Assume this expression is not a pseudo-destructor access.  */
5542   pseudo_destructor_p = false;
5543
5544   /* If the SCOPE is a scalar type, then, if this is a valid program,
5545      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5546      is type dependent, it can be pseudo-destructor-name or something else.
5547      Try to parse it as pseudo-destructor-name first.  */
5548   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5549     {
5550       tree s;
5551       tree type;
5552
5553       cp_parser_parse_tentatively (parser);
5554       /* Parse the pseudo-destructor-name.  */
5555       s = NULL_TREE;
5556       cp_parser_pseudo_destructor_name (parser, &s, &type);
5557       if (dependent_p
5558           && (cp_parser_error_occurred (parser)
5559               || TREE_CODE (type) != TYPE_DECL
5560               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5561         cp_parser_abort_tentative_parse (parser);
5562       else if (cp_parser_parse_definitely (parser))
5563         {
5564           pseudo_destructor_p = true;
5565           postfix_expression
5566             = finish_pseudo_destructor_expr (postfix_expression,
5567                                              s, TREE_TYPE (type));
5568         }
5569     }
5570
5571   if (!pseudo_destructor_p)
5572     {
5573       /* If the SCOPE is not a scalar type, we are looking at an
5574          ordinary class member access expression, rather than a
5575          pseudo-destructor-name.  */
5576       bool template_p;
5577       cp_token *token = cp_lexer_peek_token (parser->lexer);
5578       /* Parse the id-expression.  */
5579       name = (cp_parser_id_expression
5580               (parser,
5581                cp_parser_optional_template_keyword (parser),
5582                /*check_dependency_p=*/true,
5583                &template_p,
5584                /*declarator_p=*/false,
5585                /*optional_p=*/false));
5586       /* In general, build a SCOPE_REF if the member name is qualified.
5587          However, if the name was not dependent and has already been
5588          resolved; there is no need to build the SCOPE_REF.  For example;
5589
5590              struct X { void f(); };
5591              template <typename T> void f(T* t) { t->X::f(); }
5592
5593          Even though "t" is dependent, "X::f" is not and has been resolved
5594          to a BASELINK; there is no need to include scope information.  */
5595
5596       /* But we do need to remember that there was an explicit scope for
5597          virtual function calls.  */
5598       if (parser->scope)
5599         *idk = CP_ID_KIND_QUALIFIED;
5600
5601       /* If the name is a template-id that names a type, we will get a
5602          TYPE_DECL here.  That is invalid code.  */
5603       if (TREE_CODE (name) == TYPE_DECL)
5604         {
5605           error_at (token->location, "invalid use of %qD", name);
5606           postfix_expression = error_mark_node;
5607         }
5608       else
5609         {
5610           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5611             {
5612               name = build_qualified_name (/*type=*/NULL_TREE,
5613                                            parser->scope,
5614                                            name,
5615                                            template_p);
5616               parser->scope = NULL_TREE;
5617               parser->qualifying_scope = NULL_TREE;
5618               parser->object_scope = NULL_TREE;
5619             }
5620           if (scope && name && BASELINK_P (name))
5621             adjust_result_of_qualified_name_lookup
5622               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5623           postfix_expression
5624             = finish_class_member_access_expr (postfix_expression, name,
5625                                                template_p, 
5626                                                tf_warning_or_error);
5627         }
5628     }
5629
5630   /* We no longer need to look up names in the scope of the object on
5631      the left-hand side of the `.' or `->' operator.  */
5632   parser->context->object_type = NULL_TREE;
5633
5634   /* Outside of offsetof, these operators may not appear in
5635      constant-expressions.  */
5636   if (!for_offsetof
5637       && (cp_parser_non_integral_constant_expression
5638           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5639     postfix_expression = error_mark_node;
5640
5641   return postfix_expression;
5642 }
5643
5644 /* Parse a parenthesized expression-list.
5645
5646    expression-list:
5647      assignment-expression
5648      expression-list, assignment-expression
5649
5650    attribute-list:
5651      expression-list
5652      identifier
5653      identifier, expression-list
5654
5655    CAST_P is true if this expression is the target of a cast.
5656
5657    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5658    argument pack.
5659
5660    Returns a vector of trees.  Each element is a representation of an
5661    assignment-expression.  NULL is returned if the ( and or ) are
5662    missing.  An empty, but allocated, vector is returned on no
5663    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5664    if we are parsing an attribute list for an attribute that wants a
5665    plain identifier argument, normal_attr for an attribute that wants
5666    an expression, or non_attr if we aren't parsing an attribute list.  If
5667    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5668    not all of the expressions in the list were constant.  */
5669
5670 static VEC(tree,gc) *
5671 cp_parser_parenthesized_expression_list (cp_parser* parser,
5672                                          int is_attribute_list,
5673                                          bool cast_p,
5674                                          bool allow_expansion_p,
5675                                          bool *non_constant_p)
5676 {
5677   VEC(tree,gc) *expression_list;
5678   bool fold_expr_p = is_attribute_list != non_attr;
5679   tree identifier = NULL_TREE;
5680   bool saved_greater_than_is_operator_p;
5681
5682   /* Assume all the expressions will be constant.  */
5683   if (non_constant_p)
5684     *non_constant_p = false;
5685
5686   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5687     return NULL;
5688
5689   expression_list = make_tree_vector ();
5690
5691   /* Within a parenthesized expression, a `>' token is always
5692      the greater-than operator.  */
5693   saved_greater_than_is_operator_p
5694     = parser->greater_than_is_operator_p;
5695   parser->greater_than_is_operator_p = true;
5696
5697   /* Consume expressions until there are no more.  */
5698   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5699     while (true)
5700       {
5701         tree expr;
5702
5703         /* At the beginning of attribute lists, check to see if the
5704            next token is an identifier.  */
5705         if (is_attribute_list == id_attr
5706             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5707           {
5708             cp_token *token;
5709
5710             /* Consume the identifier.  */
5711             token = cp_lexer_consume_token (parser->lexer);
5712             /* Save the identifier.  */
5713             identifier = token->u.value;
5714           }
5715         else
5716           {
5717             bool expr_non_constant_p;
5718
5719             /* Parse the next assignment-expression.  */
5720             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5721               {
5722                 /* A braced-init-list.  */
5723                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5724                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5725                 if (non_constant_p && expr_non_constant_p)
5726                   *non_constant_p = true;
5727               }
5728             else if (non_constant_p)
5729               {
5730                 expr = (cp_parser_constant_expression
5731                         (parser, /*allow_non_constant_p=*/true,
5732                          &expr_non_constant_p));
5733                 if (expr_non_constant_p)
5734                   *non_constant_p = true;
5735               }
5736             else
5737               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5738
5739             if (fold_expr_p)
5740               expr = fold_non_dependent_expr (expr);
5741
5742             /* If we have an ellipsis, then this is an expression
5743                expansion.  */
5744             if (allow_expansion_p
5745                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5746               {
5747                 /* Consume the `...'.  */
5748                 cp_lexer_consume_token (parser->lexer);
5749
5750                 /* Build the argument pack.  */
5751                 expr = make_pack_expansion (expr);
5752               }
5753
5754              /* Add it to the list.  We add error_mark_node
5755                 expressions to the list, so that we can still tell if
5756                 the correct form for a parenthesized expression-list
5757                 is found. That gives better errors.  */
5758             VEC_safe_push (tree, gc, expression_list, expr);
5759
5760             if (expr == error_mark_node)
5761               goto skip_comma;
5762           }
5763
5764         /* After the first item, attribute lists look the same as
5765            expression lists.  */
5766         is_attribute_list = non_attr;
5767
5768       get_comma:;
5769         /* If the next token isn't a `,', then we are done.  */
5770         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5771           break;
5772
5773         /* Otherwise, consume the `,' and keep going.  */
5774         cp_lexer_consume_token (parser->lexer);
5775       }
5776
5777   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5778     {
5779       int ending;
5780
5781     skip_comma:;
5782       /* We try and resync to an unnested comma, as that will give the
5783          user better diagnostics.  */
5784       ending = cp_parser_skip_to_closing_parenthesis (parser,
5785                                                       /*recovering=*/true,
5786                                                       /*or_comma=*/true,
5787                                                       /*consume_paren=*/true);
5788       if (ending < 0)
5789         goto get_comma;
5790       if (!ending)
5791         {
5792           parser->greater_than_is_operator_p
5793             = saved_greater_than_is_operator_p;
5794           return NULL;
5795         }
5796     }
5797
5798   parser->greater_than_is_operator_p
5799     = saved_greater_than_is_operator_p;
5800
5801   if (identifier)
5802     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5803
5804   return expression_list;
5805 }
5806
5807 /* Parse a pseudo-destructor-name.
5808
5809    pseudo-destructor-name:
5810      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5811      :: [opt] nested-name-specifier template template-id :: ~ type-name
5812      :: [opt] nested-name-specifier [opt] ~ type-name
5813
5814    If either of the first two productions is used, sets *SCOPE to the
5815    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5816    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5817    or ERROR_MARK_NODE if the parse fails.  */
5818
5819 static void
5820 cp_parser_pseudo_destructor_name (cp_parser* parser,
5821                                   tree* scope,
5822                                   tree* type)
5823 {
5824   bool nested_name_specifier_p;
5825
5826   /* Assume that things will not work out.  */
5827   *type = error_mark_node;
5828
5829   /* Look for the optional `::' operator.  */
5830   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5831   /* Look for the optional nested-name-specifier.  */
5832   nested_name_specifier_p
5833     = (cp_parser_nested_name_specifier_opt (parser,
5834                                             /*typename_keyword_p=*/false,
5835                                             /*check_dependency_p=*/true,
5836                                             /*type_p=*/false,
5837                                             /*is_declaration=*/false)
5838        != NULL_TREE);
5839   /* Now, if we saw a nested-name-specifier, we might be doing the
5840      second production.  */
5841   if (nested_name_specifier_p
5842       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5843     {
5844       /* Consume the `template' keyword.  */
5845       cp_lexer_consume_token (parser->lexer);
5846       /* Parse the template-id.  */
5847       cp_parser_template_id (parser,
5848                              /*template_keyword_p=*/true,
5849                              /*check_dependency_p=*/false,
5850                              /*is_declaration=*/true);
5851       /* Look for the `::' token.  */
5852       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5853     }
5854   /* If the next token is not a `~', then there might be some
5855      additional qualification.  */
5856   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5857     {
5858       /* At this point, we're looking for "type-name :: ~".  The type-name
5859          must not be a class-name, since this is a pseudo-destructor.  So,
5860          it must be either an enum-name, or a typedef-name -- both of which
5861          are just identifiers.  So, we peek ahead to check that the "::"
5862          and "~" tokens are present; if they are not, then we can avoid
5863          calling type_name.  */
5864       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5865           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5866           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5867         {
5868           cp_parser_error (parser, "non-scalar type");
5869           return;
5870         }
5871
5872       /* Look for the type-name.  */
5873       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5874       if (*scope == error_mark_node)
5875         return;
5876
5877       /* Look for the `::' token.  */
5878       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5879     }
5880   else
5881     *scope = NULL_TREE;
5882
5883   /* Look for the `~'.  */
5884   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5885   /* Look for the type-name again.  We are not responsible for
5886      checking that it matches the first type-name.  */
5887   *type = cp_parser_nonclass_name (parser);
5888 }
5889
5890 /* Parse a unary-expression.
5891
5892    unary-expression:
5893      postfix-expression
5894      ++ cast-expression
5895      -- cast-expression
5896      unary-operator cast-expression
5897      sizeof unary-expression
5898      sizeof ( type-id )
5899      alignof ( type-id )  [C++0x]
5900      new-expression
5901      delete-expression
5902
5903    GNU Extensions:
5904
5905    unary-expression:
5906      __extension__ cast-expression
5907      __alignof__ unary-expression
5908      __alignof__ ( type-id )
5909      alignof unary-expression  [C++0x]
5910      __real__ cast-expression
5911      __imag__ cast-expression
5912      && identifier
5913
5914    ADDRESS_P is true iff the unary-expression is appearing as the
5915    operand of the `&' operator.   CAST_P is true if this expression is
5916    the target of a cast.
5917
5918    Returns a representation of the expression.  */
5919
5920 static tree
5921 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5922                             cp_id_kind * pidk)
5923 {
5924   cp_token *token;
5925   enum tree_code unary_operator;
5926
5927   /* Peek at the next token.  */
5928   token = cp_lexer_peek_token (parser->lexer);
5929   /* Some keywords give away the kind of expression.  */
5930   if (token->type == CPP_KEYWORD)
5931     {
5932       enum rid keyword = token->keyword;
5933
5934       switch (keyword)
5935         {
5936         case RID_ALIGNOF:
5937         case RID_SIZEOF:
5938           {
5939             tree operand;
5940             enum tree_code op;
5941
5942             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5943             /* Consume the token.  */
5944             cp_lexer_consume_token (parser->lexer);
5945             /* Parse the operand.  */
5946             operand = cp_parser_sizeof_operand (parser, keyword);
5947
5948             if (TYPE_P (operand))
5949               return cxx_sizeof_or_alignof_type (operand, op, true);
5950             else
5951               {
5952                 /* ISO C++ defines alignof only with types, not with
5953                    expressions. So pedwarn if alignof is used with a non-
5954                    type expression. However, __alignof__ is ok.  */
5955                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5956                   pedwarn (token->location, OPT_pedantic,
5957                            "ISO C++ does not allow %<alignof%> "
5958                            "with a non-type");
5959
5960                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5961               }
5962           }
5963
5964         case RID_NEW:
5965           return cp_parser_new_expression (parser);
5966
5967         case RID_DELETE:
5968           return cp_parser_delete_expression (parser);
5969
5970         case RID_EXTENSION:
5971           {
5972             /* The saved value of the PEDANTIC flag.  */
5973             int saved_pedantic;
5974             tree expr;
5975
5976             /* Save away the PEDANTIC flag.  */
5977             cp_parser_extension_opt (parser, &saved_pedantic);
5978             /* Parse the cast-expression.  */
5979             expr = cp_parser_simple_cast_expression (parser);
5980             /* Restore the PEDANTIC flag.  */
5981             pedantic = saved_pedantic;
5982
5983             return expr;
5984           }
5985
5986         case RID_REALPART:
5987         case RID_IMAGPART:
5988           {
5989             tree expression;
5990
5991             /* Consume the `__real__' or `__imag__' token.  */
5992             cp_lexer_consume_token (parser->lexer);
5993             /* Parse the cast-expression.  */
5994             expression = cp_parser_simple_cast_expression (parser);
5995             /* Create the complete representation.  */
5996             return build_x_unary_op ((keyword == RID_REALPART
5997                                       ? REALPART_EXPR : IMAGPART_EXPR),
5998                                      expression,
5999                                      tf_warning_or_error);
6000           }
6001           break;
6002
6003         case RID_NOEXCEPT:
6004           {
6005             tree expr;
6006             const char *saved_message;
6007             bool saved_integral_constant_expression_p;
6008             bool saved_non_integral_constant_expression_p;
6009             bool saved_greater_than_is_operator_p;
6010
6011             cp_lexer_consume_token (parser->lexer);
6012             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6013
6014             saved_message = parser->type_definition_forbidden_message;
6015             parser->type_definition_forbidden_message
6016               = G_("types may not be defined in %<noexcept%> expressions");
6017
6018             saved_integral_constant_expression_p
6019               = parser->integral_constant_expression_p;
6020             saved_non_integral_constant_expression_p
6021               = parser->non_integral_constant_expression_p;
6022             parser->integral_constant_expression_p = false;
6023
6024             saved_greater_than_is_operator_p
6025               = parser->greater_than_is_operator_p;
6026             parser->greater_than_is_operator_p = true;
6027
6028             ++cp_unevaluated_operand;
6029             ++c_inhibit_evaluation_warnings;
6030             expr = cp_parser_expression (parser, false, NULL);
6031             --c_inhibit_evaluation_warnings;
6032             --cp_unevaluated_operand;
6033
6034             parser->greater_than_is_operator_p
6035               = saved_greater_than_is_operator_p;
6036
6037             parser->integral_constant_expression_p
6038               = saved_integral_constant_expression_p;
6039             parser->non_integral_constant_expression_p
6040               = saved_non_integral_constant_expression_p;
6041
6042             parser->type_definition_forbidden_message = saved_message;
6043
6044             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6045             return finish_noexcept_expr (expr, tf_warning_or_error);
6046           }
6047
6048         default:
6049           break;
6050         }
6051     }
6052
6053   /* Look for the `:: new' and `:: delete', which also signal the
6054      beginning of a new-expression, or delete-expression,
6055      respectively.  If the next token is `::', then it might be one of
6056      these.  */
6057   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6058     {
6059       enum rid keyword;
6060
6061       /* See if the token after the `::' is one of the keywords in
6062          which we're interested.  */
6063       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6064       /* If it's `new', we have a new-expression.  */
6065       if (keyword == RID_NEW)
6066         return cp_parser_new_expression (parser);
6067       /* Similarly, for `delete'.  */
6068       else if (keyword == RID_DELETE)
6069         return cp_parser_delete_expression (parser);
6070     }
6071
6072   /* Look for a unary operator.  */
6073   unary_operator = cp_parser_unary_operator (token);
6074   /* The `++' and `--' operators can be handled similarly, even though
6075      they are not technically unary-operators in the grammar.  */
6076   if (unary_operator == ERROR_MARK)
6077     {
6078       if (token->type == CPP_PLUS_PLUS)
6079         unary_operator = PREINCREMENT_EXPR;
6080       else if (token->type == CPP_MINUS_MINUS)
6081         unary_operator = PREDECREMENT_EXPR;
6082       /* Handle the GNU address-of-label extension.  */
6083       else if (cp_parser_allow_gnu_extensions_p (parser)
6084                && token->type == CPP_AND_AND)
6085         {
6086           tree identifier;
6087           tree expression;
6088           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6089
6090           /* Consume the '&&' token.  */
6091           cp_lexer_consume_token (parser->lexer);
6092           /* Look for the identifier.  */
6093           identifier = cp_parser_identifier (parser);
6094           /* Create an expression representing the address.  */
6095           expression = finish_label_address_expr (identifier, loc);
6096           if (cp_parser_non_integral_constant_expression (parser,
6097                                                           NIC_ADDR_LABEL))
6098             expression = error_mark_node;
6099           return expression;
6100         }
6101     }
6102   if (unary_operator != ERROR_MARK)
6103     {
6104       tree cast_expression;
6105       tree expression = error_mark_node;
6106       non_integral_constant non_constant_p = NIC_NONE;
6107
6108       /* Consume the operator token.  */
6109       token = cp_lexer_consume_token (parser->lexer);
6110       /* Parse the cast-expression.  */
6111       cast_expression
6112         = cp_parser_cast_expression (parser,
6113                                      unary_operator == ADDR_EXPR,
6114                                      /*cast_p=*/false, pidk);
6115       /* Now, build an appropriate representation.  */
6116       switch (unary_operator)
6117         {
6118         case INDIRECT_REF:
6119           non_constant_p = NIC_STAR;
6120           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6121                                              tf_warning_or_error);
6122           break;
6123
6124         case ADDR_EXPR:
6125            non_constant_p = NIC_ADDR;
6126           /* Fall through.  */
6127         case BIT_NOT_EXPR:
6128           expression = build_x_unary_op (unary_operator, cast_expression,
6129                                          tf_warning_or_error);
6130           break;
6131
6132         case PREINCREMENT_EXPR:
6133         case PREDECREMENT_EXPR:
6134           non_constant_p = unary_operator == PREINCREMENT_EXPR
6135                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6136           /* Fall through.  */
6137         case UNARY_PLUS_EXPR:
6138         case NEGATE_EXPR:
6139         case TRUTH_NOT_EXPR:
6140           expression = finish_unary_op_expr (unary_operator, cast_expression);
6141           break;
6142
6143         default:
6144           gcc_unreachable ();
6145         }
6146
6147       if (non_constant_p != NIC_NONE
6148           && cp_parser_non_integral_constant_expression (parser,
6149                                                          non_constant_p))
6150         expression = error_mark_node;
6151
6152       return expression;
6153     }
6154
6155   return cp_parser_postfix_expression (parser, address_p, cast_p,
6156                                        /*member_access_only_p=*/false,
6157                                        pidk);
6158 }
6159
6160 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6161    unary-operator, the corresponding tree code is returned.  */
6162
6163 static enum tree_code
6164 cp_parser_unary_operator (cp_token* token)
6165 {
6166   switch (token->type)
6167     {
6168     case CPP_MULT:
6169       return INDIRECT_REF;
6170
6171     case CPP_AND:
6172       return ADDR_EXPR;
6173
6174     case CPP_PLUS:
6175       return UNARY_PLUS_EXPR;
6176
6177     case CPP_MINUS:
6178       return NEGATE_EXPR;
6179
6180     case CPP_NOT:
6181       return TRUTH_NOT_EXPR;
6182
6183     case CPP_COMPL:
6184       return BIT_NOT_EXPR;
6185
6186     default:
6187       return ERROR_MARK;
6188     }
6189 }
6190
6191 /* Parse a new-expression.
6192
6193    new-expression:
6194      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6195      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6196
6197    Returns a representation of the expression.  */
6198
6199 static tree
6200 cp_parser_new_expression (cp_parser* parser)
6201 {
6202   bool global_scope_p;
6203   VEC(tree,gc) *placement;
6204   tree type;
6205   VEC(tree,gc) *initializer;
6206   tree nelts;
6207   tree ret;
6208
6209   /* Look for the optional `::' operator.  */
6210   global_scope_p
6211     = (cp_parser_global_scope_opt (parser,
6212                                    /*current_scope_valid_p=*/false)
6213        != NULL_TREE);
6214   /* Look for the `new' operator.  */
6215   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6216   /* There's no easy way to tell a new-placement from the
6217      `( type-id )' construct.  */
6218   cp_parser_parse_tentatively (parser);
6219   /* Look for a new-placement.  */
6220   placement = cp_parser_new_placement (parser);
6221   /* If that didn't work out, there's no new-placement.  */
6222   if (!cp_parser_parse_definitely (parser))
6223     {
6224       if (placement != NULL)
6225         release_tree_vector (placement);
6226       placement = NULL;
6227     }
6228
6229   /* If the next token is a `(', then we have a parenthesized
6230      type-id.  */
6231   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6232     {
6233       cp_token *token;
6234       /* Consume the `('.  */
6235       cp_lexer_consume_token (parser->lexer);
6236       /* Parse the type-id.  */
6237       type = cp_parser_type_id (parser);
6238       /* Look for the closing `)'.  */
6239       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6240       token = cp_lexer_peek_token (parser->lexer);
6241       /* There should not be a direct-new-declarator in this production,
6242          but GCC used to allowed this, so we check and emit a sensible error
6243          message for this case.  */
6244       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6245         {
6246           error_at (token->location,
6247                     "array bound forbidden after parenthesized type-id");
6248           inform (token->location, 
6249                   "try removing the parentheses around the type-id");
6250           cp_parser_direct_new_declarator (parser);
6251         }
6252       nelts = NULL_TREE;
6253     }
6254   /* Otherwise, there must be a new-type-id.  */
6255   else
6256     type = cp_parser_new_type_id (parser, &nelts);
6257
6258   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6259   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6260       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6261     initializer = cp_parser_new_initializer (parser);
6262   else
6263     initializer = NULL;
6264
6265   /* A new-expression may not appear in an integral constant
6266      expression.  */
6267   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6268     ret = error_mark_node;
6269   else
6270     {
6271       /* Create a representation of the new-expression.  */
6272       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6273                        tf_warning_or_error);
6274     }
6275
6276   if (placement != NULL)
6277     release_tree_vector (placement);
6278   if (initializer != NULL)
6279     release_tree_vector (initializer);
6280
6281   return ret;
6282 }
6283
6284 /* Parse a new-placement.
6285
6286    new-placement:
6287      ( expression-list )
6288
6289    Returns the same representation as for an expression-list.  */
6290
6291 static VEC(tree,gc) *
6292 cp_parser_new_placement (cp_parser* parser)
6293 {
6294   VEC(tree,gc) *expression_list;
6295
6296   /* Parse the expression-list.  */
6297   expression_list = (cp_parser_parenthesized_expression_list
6298                      (parser, non_attr, /*cast_p=*/false,
6299                       /*allow_expansion_p=*/true,
6300                       /*non_constant_p=*/NULL));
6301
6302   return expression_list;
6303 }
6304
6305 /* Parse a new-type-id.
6306
6307    new-type-id:
6308      type-specifier-seq new-declarator [opt]
6309
6310    Returns the TYPE allocated.  If the new-type-id indicates an array
6311    type, *NELTS is set to the number of elements in the last array
6312    bound; the TYPE will not include the last array bound.  */
6313
6314 static tree
6315 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6316 {
6317   cp_decl_specifier_seq type_specifier_seq;
6318   cp_declarator *new_declarator;
6319   cp_declarator *declarator;
6320   cp_declarator *outer_declarator;
6321   const char *saved_message;
6322   tree type;
6323
6324   /* The type-specifier sequence must not contain type definitions.
6325      (It cannot contain declarations of new types either, but if they
6326      are not definitions we will catch that because they are not
6327      complete.)  */
6328   saved_message = parser->type_definition_forbidden_message;
6329   parser->type_definition_forbidden_message
6330     = G_("types may not be defined in a new-type-id");
6331   /* Parse the type-specifier-seq.  */
6332   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6333                                 /*is_trailing_return=*/false,
6334                                 &type_specifier_seq);
6335   /* Restore the old message.  */
6336   parser->type_definition_forbidden_message = saved_message;
6337   /* Parse the new-declarator.  */
6338   new_declarator = cp_parser_new_declarator_opt (parser);
6339
6340   /* Determine the number of elements in the last array dimension, if
6341      any.  */
6342   *nelts = NULL_TREE;
6343   /* Skip down to the last array dimension.  */
6344   declarator = new_declarator;
6345   outer_declarator = NULL;
6346   while (declarator && (declarator->kind == cdk_pointer
6347                         || declarator->kind == cdk_ptrmem))
6348     {
6349       outer_declarator = declarator;
6350       declarator = declarator->declarator;
6351     }
6352   while (declarator
6353          && declarator->kind == cdk_array
6354          && declarator->declarator
6355          && declarator->declarator->kind == cdk_array)
6356     {
6357       outer_declarator = declarator;
6358       declarator = declarator->declarator;
6359     }
6360
6361   if (declarator && declarator->kind == cdk_array)
6362     {
6363       *nelts = declarator->u.array.bounds;
6364       if (*nelts == error_mark_node)
6365         *nelts = integer_one_node;
6366
6367       if (outer_declarator)
6368         outer_declarator->declarator = declarator->declarator;
6369       else
6370         new_declarator = NULL;
6371     }
6372
6373   type = groktypename (&type_specifier_seq, new_declarator, false);
6374   return type;
6375 }
6376
6377 /* Parse an (optional) new-declarator.
6378
6379    new-declarator:
6380      ptr-operator new-declarator [opt]
6381      direct-new-declarator
6382
6383    Returns the declarator.  */
6384
6385 static cp_declarator *
6386 cp_parser_new_declarator_opt (cp_parser* parser)
6387 {
6388   enum tree_code code;
6389   tree type;
6390   cp_cv_quals cv_quals;
6391
6392   /* We don't know if there's a ptr-operator next, or not.  */
6393   cp_parser_parse_tentatively (parser);
6394   /* Look for a ptr-operator.  */
6395   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6396   /* If that worked, look for more new-declarators.  */
6397   if (cp_parser_parse_definitely (parser))
6398     {
6399       cp_declarator *declarator;
6400
6401       /* Parse another optional declarator.  */
6402       declarator = cp_parser_new_declarator_opt (parser);
6403
6404       return cp_parser_make_indirect_declarator
6405         (code, type, cv_quals, declarator);
6406     }
6407
6408   /* If the next token is a `[', there is a direct-new-declarator.  */
6409   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6410     return cp_parser_direct_new_declarator (parser);
6411
6412   return NULL;
6413 }
6414
6415 /* Parse a direct-new-declarator.
6416
6417    direct-new-declarator:
6418      [ expression ]
6419      direct-new-declarator [constant-expression]
6420
6421    */
6422
6423 static cp_declarator *
6424 cp_parser_direct_new_declarator (cp_parser* parser)
6425 {
6426   cp_declarator *declarator = NULL;
6427
6428   while (true)
6429     {
6430       tree expression;
6431
6432       /* Look for the opening `['.  */
6433       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6434       /* The first expression is not required to be constant.  */
6435       if (!declarator)
6436         {
6437           cp_token *token = cp_lexer_peek_token (parser->lexer);
6438           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6439           /* The standard requires that the expression have integral
6440              type.  DR 74 adds enumeration types.  We believe that the
6441              real intent is that these expressions be handled like the
6442              expression in a `switch' condition, which also allows
6443              classes with a single conversion to integral or
6444              enumeration type.  */
6445           if (!processing_template_decl)
6446             {
6447               expression
6448                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6449                                               expression,
6450                                               /*complain=*/true);
6451               if (!expression)
6452                 {
6453                   error_at (token->location,
6454                             "expression in new-declarator must have integral "
6455                             "or enumeration type");
6456                   expression = error_mark_node;
6457                 }
6458             }
6459         }
6460       /* But all the other expressions must be.  */
6461       else
6462         expression
6463           = cp_parser_constant_expression (parser,
6464                                            /*allow_non_constant=*/false,
6465                                            NULL);
6466       /* Look for the closing `]'.  */
6467       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6468
6469       /* Add this bound to the declarator.  */
6470       declarator = make_array_declarator (declarator, expression);
6471
6472       /* If the next token is not a `[', then there are no more
6473          bounds.  */
6474       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6475         break;
6476     }
6477
6478   return declarator;
6479 }
6480
6481 /* Parse a new-initializer.
6482
6483    new-initializer:
6484      ( expression-list [opt] )
6485      braced-init-list
6486
6487    Returns a representation of the expression-list.  */
6488
6489 static VEC(tree,gc) *
6490 cp_parser_new_initializer (cp_parser* parser)
6491 {
6492   VEC(tree,gc) *expression_list;
6493
6494   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6495     {
6496       tree t;
6497       bool expr_non_constant_p;
6498       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6499       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6500       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6501       expression_list = make_tree_vector_single (t);
6502     }
6503   else
6504     expression_list = (cp_parser_parenthesized_expression_list
6505                        (parser, non_attr, /*cast_p=*/false,
6506                         /*allow_expansion_p=*/true,
6507                         /*non_constant_p=*/NULL));
6508
6509   return expression_list;
6510 }
6511
6512 /* Parse a delete-expression.
6513
6514    delete-expression:
6515      :: [opt] delete cast-expression
6516      :: [opt] delete [ ] cast-expression
6517
6518    Returns a representation of the expression.  */
6519
6520 static tree
6521 cp_parser_delete_expression (cp_parser* parser)
6522 {
6523   bool global_scope_p;
6524   bool array_p;
6525   tree expression;
6526
6527   /* Look for the optional `::' operator.  */
6528   global_scope_p
6529     = (cp_parser_global_scope_opt (parser,
6530                                    /*current_scope_valid_p=*/false)
6531        != NULL_TREE);
6532   /* Look for the `delete' keyword.  */
6533   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6534   /* See if the array syntax is in use.  */
6535   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6536     {
6537       /* Consume the `[' token.  */
6538       cp_lexer_consume_token (parser->lexer);
6539       /* Look for the `]' token.  */
6540       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6541       /* Remember that this is the `[]' construct.  */
6542       array_p = true;
6543     }
6544   else
6545     array_p = false;
6546
6547   /* Parse the cast-expression.  */
6548   expression = cp_parser_simple_cast_expression (parser);
6549
6550   /* A delete-expression may not appear in an integral constant
6551      expression.  */
6552   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6553     return error_mark_node;
6554
6555   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6556 }
6557
6558 /* Returns true if TOKEN may start a cast-expression and false
6559    otherwise.  */
6560
6561 static bool
6562 cp_parser_token_starts_cast_expression (cp_token *token)
6563 {
6564   switch (token->type)
6565     {
6566     case CPP_COMMA:
6567     case CPP_SEMICOLON:
6568     case CPP_QUERY:
6569     case CPP_COLON:
6570     case CPP_CLOSE_SQUARE:
6571     case CPP_CLOSE_PAREN:
6572     case CPP_CLOSE_BRACE:
6573     case CPP_DOT:
6574     case CPP_DOT_STAR:
6575     case CPP_DEREF:
6576     case CPP_DEREF_STAR:
6577     case CPP_DIV:
6578     case CPP_MOD:
6579     case CPP_LSHIFT:
6580     case CPP_RSHIFT:
6581     case CPP_LESS:
6582     case CPP_GREATER:
6583     case CPP_LESS_EQ:
6584     case CPP_GREATER_EQ:
6585     case CPP_EQ_EQ:
6586     case CPP_NOT_EQ:
6587     case CPP_EQ:
6588     case CPP_MULT_EQ:
6589     case CPP_DIV_EQ:
6590     case CPP_MOD_EQ:
6591     case CPP_PLUS_EQ:
6592     case CPP_MINUS_EQ:
6593     case CPP_RSHIFT_EQ:
6594     case CPP_LSHIFT_EQ:
6595     case CPP_AND_EQ:
6596     case CPP_XOR_EQ:
6597     case CPP_OR_EQ:
6598     case CPP_XOR:
6599     case CPP_OR:
6600     case CPP_OR_OR:
6601     case CPP_EOF:
6602       return false;
6603
6604       /* '[' may start a primary-expression in obj-c++.  */
6605     case CPP_OPEN_SQUARE:
6606       return c_dialect_objc ();
6607
6608     default:
6609       return true;
6610     }
6611 }
6612
6613 /* Parse a cast-expression.
6614
6615    cast-expression:
6616      unary-expression
6617      ( type-id ) cast-expression
6618
6619    ADDRESS_P is true iff the unary-expression is appearing as the
6620    operand of the `&' operator.   CAST_P is true if this expression is
6621    the target of a cast.
6622
6623    Returns a representation of the expression.  */
6624
6625 static tree
6626 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6627                            cp_id_kind * pidk)
6628 {
6629   /* If it's a `(', then we might be looking at a cast.  */
6630   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6631     {
6632       tree type = NULL_TREE;
6633       tree expr = NULL_TREE;
6634       bool compound_literal_p;
6635       const char *saved_message;
6636
6637       /* There's no way to know yet whether or not this is a cast.
6638          For example, `(int (3))' is a unary-expression, while `(int)
6639          3' is a cast.  So, we resort to parsing tentatively.  */
6640       cp_parser_parse_tentatively (parser);
6641       /* Types may not be defined in a cast.  */
6642       saved_message = parser->type_definition_forbidden_message;
6643       parser->type_definition_forbidden_message
6644         = G_("types may not be defined in casts");
6645       /* Consume the `('.  */
6646       cp_lexer_consume_token (parser->lexer);
6647       /* A very tricky bit is that `(struct S) { 3 }' is a
6648          compound-literal (which we permit in C++ as an extension).
6649          But, that construct is not a cast-expression -- it is a
6650          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6651          is legal; if the compound-literal were a cast-expression,
6652          you'd need an extra set of parentheses.)  But, if we parse
6653          the type-id, and it happens to be a class-specifier, then we
6654          will commit to the parse at that point, because we cannot
6655          undo the action that is done when creating a new class.  So,
6656          then we cannot back up and do a postfix-expression.
6657
6658          Therefore, we scan ahead to the closing `)', and check to see
6659          if the token after the `)' is a `{'.  If so, we are not
6660          looking at a cast-expression.
6661
6662          Save tokens so that we can put them back.  */
6663       cp_lexer_save_tokens (parser->lexer);
6664       /* Skip tokens until the next token is a closing parenthesis.
6665          If we find the closing `)', and the next token is a `{', then
6666          we are looking at a compound-literal.  */
6667       compound_literal_p
6668         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6669                                                   /*consume_paren=*/true)
6670            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6671       /* Roll back the tokens we skipped.  */
6672       cp_lexer_rollback_tokens (parser->lexer);
6673       /* If we were looking at a compound-literal, simulate an error
6674          so that the call to cp_parser_parse_definitely below will
6675          fail.  */
6676       if (compound_literal_p)
6677         cp_parser_simulate_error (parser);
6678       else
6679         {
6680           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6681           parser->in_type_id_in_expr_p = true;
6682           /* Look for the type-id.  */
6683           type = cp_parser_type_id (parser);
6684           /* Look for the closing `)'.  */
6685           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6686           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6687         }
6688
6689       /* Restore the saved message.  */
6690       parser->type_definition_forbidden_message = saved_message;
6691
6692       /* At this point this can only be either a cast or a
6693          parenthesized ctor such as `(T ())' that looks like a cast to
6694          function returning T.  */
6695       if (!cp_parser_error_occurred (parser)
6696           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6697                                                      (parser->lexer)))
6698         {
6699           cp_parser_parse_definitely (parser);
6700           expr = cp_parser_cast_expression (parser,
6701                                             /*address_p=*/false,
6702                                             /*cast_p=*/true, pidk);
6703
6704           /* Warn about old-style casts, if so requested.  */
6705           if (warn_old_style_cast
6706               && !in_system_header
6707               && !VOID_TYPE_P (type)
6708               && current_lang_name != lang_name_c)
6709             warning (OPT_Wold_style_cast, "use of old-style cast");
6710
6711           /* Only type conversions to integral or enumeration types
6712              can be used in constant-expressions.  */
6713           if (!cast_valid_in_integral_constant_expression_p (type)
6714               && cp_parser_non_integral_constant_expression (parser,
6715                                                              NIC_CAST))
6716             return error_mark_node;
6717
6718           /* Perform the cast.  */
6719           expr = build_c_cast (input_location, type, expr);
6720           return expr;
6721         }
6722       else 
6723         cp_parser_abort_tentative_parse (parser);
6724     }
6725
6726   /* If we get here, then it's not a cast, so it must be a
6727      unary-expression.  */
6728   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6729 }
6730
6731 /* Parse a binary expression of the general form:
6732
6733    pm-expression:
6734      cast-expression
6735      pm-expression .* cast-expression
6736      pm-expression ->* cast-expression
6737
6738    multiplicative-expression:
6739      pm-expression
6740      multiplicative-expression * pm-expression
6741      multiplicative-expression / pm-expression
6742      multiplicative-expression % pm-expression
6743
6744    additive-expression:
6745      multiplicative-expression
6746      additive-expression + multiplicative-expression
6747      additive-expression - multiplicative-expression
6748
6749    shift-expression:
6750      additive-expression
6751      shift-expression << additive-expression
6752      shift-expression >> additive-expression
6753
6754    relational-expression:
6755      shift-expression
6756      relational-expression < shift-expression
6757      relational-expression > shift-expression
6758      relational-expression <= shift-expression
6759      relational-expression >= shift-expression
6760
6761   GNU Extension:
6762
6763    relational-expression:
6764      relational-expression <? shift-expression
6765      relational-expression >? shift-expression
6766
6767    equality-expression:
6768      relational-expression
6769      equality-expression == relational-expression
6770      equality-expression != relational-expression
6771
6772    and-expression:
6773      equality-expression
6774      and-expression & equality-expression
6775
6776    exclusive-or-expression:
6777      and-expression
6778      exclusive-or-expression ^ and-expression
6779
6780    inclusive-or-expression:
6781      exclusive-or-expression
6782      inclusive-or-expression | exclusive-or-expression
6783
6784    logical-and-expression:
6785      inclusive-or-expression
6786      logical-and-expression && inclusive-or-expression
6787
6788    logical-or-expression:
6789      logical-and-expression
6790      logical-or-expression || logical-and-expression
6791
6792    All these are implemented with a single function like:
6793
6794    binary-expression:
6795      simple-cast-expression
6796      binary-expression <token> binary-expression
6797
6798    CAST_P is true if this expression is the target of a cast.
6799
6800    The binops_by_token map is used to get the tree codes for each <token> type.
6801    binary-expressions are associated according to a precedence table.  */
6802
6803 #define TOKEN_PRECEDENCE(token)                              \
6804 (((token->type == CPP_GREATER                                \
6805    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6806   && !parser->greater_than_is_operator_p)                    \
6807  ? PREC_NOT_OPERATOR                                         \
6808  : binops_by_token[token->type].prec)
6809
6810 static tree
6811 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6812                              bool no_toplevel_fold_p,
6813                              enum cp_parser_prec prec,
6814                              cp_id_kind * pidk)
6815 {
6816   cp_parser_expression_stack stack;
6817   cp_parser_expression_stack_entry *sp = &stack[0];
6818   tree lhs, rhs;
6819   cp_token *token;
6820   enum tree_code tree_type, lhs_type, rhs_type;
6821   enum cp_parser_prec new_prec, lookahead_prec;
6822   bool overloaded_p;
6823
6824   /* Parse the first expression.  */
6825   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6826   lhs_type = ERROR_MARK;
6827
6828   for (;;)
6829     {
6830       /* Get an operator token.  */
6831       token = cp_lexer_peek_token (parser->lexer);
6832
6833       if (warn_cxx0x_compat
6834           && token->type == CPP_RSHIFT
6835           && !parser->greater_than_is_operator_p)
6836         {
6837           if (warning_at (token->location, OPT_Wc__0x_compat, 
6838                           "%<>>%> operator will be treated as"
6839                           " two right angle brackets in C++0x"))
6840             inform (token->location,
6841                     "suggest parentheses around %<>>%> expression");
6842         }
6843
6844       new_prec = TOKEN_PRECEDENCE (token);
6845
6846       /* Popping an entry off the stack means we completed a subexpression:
6847          - either we found a token which is not an operator (`>' where it is not
6848            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6849            will happen repeatedly;
6850          - or, we found an operator which has lower priority.  This is the case
6851            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6852            parsing `3 * 4'.  */
6853       if (new_prec <= prec)
6854         {
6855           if (sp == stack)
6856             break;
6857           else
6858             goto pop;
6859         }
6860
6861      get_rhs:
6862       tree_type = binops_by_token[token->type].tree_type;
6863
6864       /* We used the operator token.  */
6865       cp_lexer_consume_token (parser->lexer);
6866
6867       /* For "false && x" or "true || x", x will never be executed;
6868          disable warnings while evaluating it.  */
6869       if (tree_type == TRUTH_ANDIF_EXPR)
6870         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6871       else if (tree_type == TRUTH_ORIF_EXPR)
6872         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6873
6874       /* Extract another operand.  It may be the RHS of this expression
6875          or the LHS of a new, higher priority expression.  */
6876       rhs = cp_parser_simple_cast_expression (parser);
6877       rhs_type = ERROR_MARK;
6878
6879       /* Get another operator token.  Look up its precedence to avoid
6880          building a useless (immediately popped) stack entry for common
6881          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6882       token = cp_lexer_peek_token (parser->lexer);
6883       lookahead_prec = TOKEN_PRECEDENCE (token);
6884       if (lookahead_prec > new_prec)
6885         {
6886           /* ... and prepare to parse the RHS of the new, higher priority
6887              expression.  Since precedence levels on the stack are
6888              monotonically increasing, we do not have to care about
6889              stack overflows.  */
6890           sp->prec = prec;
6891           sp->tree_type = tree_type;
6892           sp->lhs = lhs;
6893           sp->lhs_type = lhs_type;
6894           sp++;
6895           lhs = rhs;
6896           lhs_type = rhs_type;
6897           prec = new_prec;
6898           new_prec = lookahead_prec;
6899           goto get_rhs;
6900
6901          pop:
6902           lookahead_prec = new_prec;
6903           /* If the stack is not empty, we have parsed into LHS the right side
6904              (`4' in the example above) of an expression we had suspended.
6905              We can use the information on the stack to recover the LHS (`3')
6906              from the stack together with the tree code (`MULT_EXPR'), and
6907              the precedence of the higher level subexpression
6908              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6909              which will be used to actually build the additive expression.  */
6910           --sp;
6911           prec = sp->prec;
6912           tree_type = sp->tree_type;
6913           rhs = lhs;
6914           rhs_type = lhs_type;
6915           lhs = sp->lhs;
6916           lhs_type = sp->lhs_type;
6917         }
6918
6919       /* Undo the disabling of warnings done above.  */
6920       if (tree_type == TRUTH_ANDIF_EXPR)
6921         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6922       else if (tree_type == TRUTH_ORIF_EXPR)
6923         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6924
6925       overloaded_p = false;
6926       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6927          ERROR_MARK for everything that is not a binary expression.
6928          This makes warn_about_parentheses miss some warnings that
6929          involve unary operators.  For unary expressions we should
6930          pass the correct tree_code unless the unary expression was
6931          surrounded by parentheses.
6932       */
6933       if (no_toplevel_fold_p
6934           && lookahead_prec <= prec
6935           && sp == stack
6936           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6937         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6938       else
6939         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6940                                  &overloaded_p, tf_warning_or_error);
6941       lhs_type = tree_type;
6942
6943       /* If the binary operator required the use of an overloaded operator,
6944          then this expression cannot be an integral constant-expression.
6945          An overloaded operator can be used even if both operands are
6946          otherwise permissible in an integral constant-expression if at
6947          least one of the operands is of enumeration type.  */
6948
6949       if (overloaded_p
6950           && cp_parser_non_integral_constant_expression (parser,
6951                                                          NIC_OVERLOADED))
6952         return error_mark_node;
6953     }
6954
6955   return lhs;
6956 }
6957
6958
6959 /* Parse the `? expression : assignment-expression' part of a
6960    conditional-expression.  The LOGICAL_OR_EXPR is the
6961    logical-or-expression that started the conditional-expression.
6962    Returns a representation of the entire conditional-expression.
6963
6964    This routine is used by cp_parser_assignment_expression.
6965
6966      ? expression : assignment-expression
6967
6968    GNU Extensions:
6969
6970      ? : assignment-expression */
6971
6972 static tree
6973 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6974 {
6975   tree expr;
6976   tree assignment_expr;
6977   struct cp_token *token;
6978
6979   /* Consume the `?' token.  */
6980   cp_lexer_consume_token (parser->lexer);
6981   token = cp_lexer_peek_token (parser->lexer);
6982   if (cp_parser_allow_gnu_extensions_p (parser)
6983       && token->type == CPP_COLON)
6984     {
6985       pedwarn (token->location, OPT_pedantic, 
6986                "ISO C++ does not allow ?: with omitted middle operand");
6987       /* Implicit true clause.  */
6988       expr = NULL_TREE;
6989       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6990       warn_for_omitted_condop (token->location, logical_or_expr);
6991     }
6992   else
6993     {
6994       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6995       parser->colon_corrects_to_scope_p = false;
6996       /* Parse the expression.  */
6997       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6998       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6999       c_inhibit_evaluation_warnings +=
7000         ((logical_or_expr == truthvalue_true_node)
7001          - (logical_or_expr == truthvalue_false_node));
7002       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7003     }
7004
7005   /* The next token should be a `:'.  */
7006   cp_parser_require (parser, CPP_COLON, RT_COLON);
7007   /* Parse the assignment-expression.  */
7008   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7009   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7010
7011   /* Build the conditional-expression.  */
7012   return build_x_conditional_expr (logical_or_expr,
7013                                    expr,
7014                                    assignment_expr,
7015                                    tf_warning_or_error);
7016 }
7017
7018 /* Parse an assignment-expression.
7019
7020    assignment-expression:
7021      conditional-expression
7022      logical-or-expression assignment-operator assignment_expression
7023      throw-expression
7024
7025    CAST_P is true if this expression is the target of a cast.
7026
7027    Returns a representation for the expression.  */
7028
7029 static tree
7030 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7031                                  cp_id_kind * pidk)
7032 {
7033   tree expr;
7034
7035   /* If the next token is the `throw' keyword, then we're looking at
7036      a throw-expression.  */
7037   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7038     expr = cp_parser_throw_expression (parser);
7039   /* Otherwise, it must be that we are looking at a
7040      logical-or-expression.  */
7041   else
7042     {
7043       /* Parse the binary expressions (logical-or-expression).  */
7044       expr = cp_parser_binary_expression (parser, cast_p, false,
7045                                           PREC_NOT_OPERATOR, pidk);
7046       /* If the next token is a `?' then we're actually looking at a
7047          conditional-expression.  */
7048       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7049         return cp_parser_question_colon_clause (parser, expr);
7050       else
7051         {
7052           enum tree_code assignment_operator;
7053
7054           /* If it's an assignment-operator, we're using the second
7055              production.  */
7056           assignment_operator
7057             = cp_parser_assignment_operator_opt (parser);
7058           if (assignment_operator != ERROR_MARK)
7059             {
7060               bool non_constant_p;
7061
7062               /* Parse the right-hand side of the assignment.  */
7063               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7064
7065               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7066                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7067
7068               /* An assignment may not appear in a
7069                  constant-expression.  */
7070               if (cp_parser_non_integral_constant_expression (parser,
7071                                                               NIC_ASSIGNMENT))
7072                 return error_mark_node;
7073               /* Build the assignment expression.  */
7074               expr = build_x_modify_expr (expr,
7075                                           assignment_operator,
7076                                           rhs,
7077                                           tf_warning_or_error);
7078             }
7079         }
7080     }
7081
7082   return expr;
7083 }
7084
7085 /* Parse an (optional) assignment-operator.
7086
7087    assignment-operator: one of
7088      = *= /= %= += -= >>= <<= &= ^= |=
7089
7090    GNU Extension:
7091
7092    assignment-operator: one of
7093      <?= >?=
7094
7095    If the next token is an assignment operator, the corresponding tree
7096    code is returned, and the token is consumed.  For example, for
7097    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7098    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7099    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7100    operator, ERROR_MARK is returned.  */
7101
7102 static enum tree_code
7103 cp_parser_assignment_operator_opt (cp_parser* parser)
7104 {
7105   enum tree_code op;
7106   cp_token *token;
7107
7108   /* Peek at the next token.  */
7109   token = cp_lexer_peek_token (parser->lexer);
7110
7111   switch (token->type)
7112     {
7113     case CPP_EQ:
7114       op = NOP_EXPR;
7115       break;
7116
7117     case CPP_MULT_EQ:
7118       op = MULT_EXPR;
7119       break;
7120
7121     case CPP_DIV_EQ:
7122       op = TRUNC_DIV_EXPR;
7123       break;
7124
7125     case CPP_MOD_EQ:
7126       op = TRUNC_MOD_EXPR;
7127       break;
7128
7129     case CPP_PLUS_EQ:
7130       op = PLUS_EXPR;
7131       break;
7132
7133     case CPP_MINUS_EQ:
7134       op = MINUS_EXPR;
7135       break;
7136
7137     case CPP_RSHIFT_EQ:
7138       op = RSHIFT_EXPR;
7139       break;
7140
7141     case CPP_LSHIFT_EQ:
7142       op = LSHIFT_EXPR;
7143       break;
7144
7145     case CPP_AND_EQ:
7146       op = BIT_AND_EXPR;
7147       break;
7148
7149     case CPP_XOR_EQ:
7150       op = BIT_XOR_EXPR;
7151       break;
7152
7153     case CPP_OR_EQ:
7154       op = BIT_IOR_EXPR;
7155       break;
7156
7157     default:
7158       /* Nothing else is an assignment operator.  */
7159       op = ERROR_MARK;
7160     }
7161
7162   /* If it was an assignment operator, consume it.  */
7163   if (op != ERROR_MARK)
7164     cp_lexer_consume_token (parser->lexer);
7165
7166   return op;
7167 }
7168
7169 /* Parse an expression.
7170
7171    expression:
7172      assignment-expression
7173      expression , assignment-expression
7174
7175    CAST_P is true if this expression is the target of a cast.
7176
7177    Returns a representation of the expression.  */
7178
7179 static tree
7180 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7181 {
7182   tree expression = NULL_TREE;
7183
7184   while (true)
7185     {
7186       tree assignment_expression;
7187
7188       /* Parse the next assignment-expression.  */
7189       assignment_expression
7190         = cp_parser_assignment_expression (parser, cast_p, pidk);
7191       /* If this is the first assignment-expression, we can just
7192          save it away.  */
7193       if (!expression)
7194         expression = assignment_expression;
7195       else
7196         expression = build_x_compound_expr (expression,
7197                                             assignment_expression,
7198                                             tf_warning_or_error);
7199       /* If the next token is not a comma, then we are done with the
7200          expression.  */
7201       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7202         break;
7203       /* Consume the `,'.  */
7204       cp_lexer_consume_token (parser->lexer);
7205       /* A comma operator cannot appear in a constant-expression.  */
7206       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7207         expression = error_mark_node;
7208     }
7209
7210   return expression;
7211 }
7212
7213 /* Parse a constant-expression.
7214
7215    constant-expression:
7216      conditional-expression
7217
7218   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7219   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7220   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7221   is false, NON_CONSTANT_P should be NULL.  */
7222
7223 static tree
7224 cp_parser_constant_expression (cp_parser* parser,
7225                                bool allow_non_constant_p,
7226                                bool *non_constant_p)
7227 {
7228   bool saved_integral_constant_expression_p;
7229   bool saved_allow_non_integral_constant_expression_p;
7230   bool saved_non_integral_constant_expression_p;
7231   tree expression;
7232
7233   /* It might seem that we could simply parse the
7234      conditional-expression, and then check to see if it were
7235      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7236      one that the compiler can figure out is constant, possibly after
7237      doing some simplifications or optimizations.  The standard has a
7238      precise definition of constant-expression, and we must honor
7239      that, even though it is somewhat more restrictive.
7240
7241      For example:
7242
7243        int i[(2, 3)];
7244
7245      is not a legal declaration, because `(2, 3)' is not a
7246      constant-expression.  The `,' operator is forbidden in a
7247      constant-expression.  However, GCC's constant-folding machinery
7248      will fold this operation to an INTEGER_CST for `3'.  */
7249
7250   /* Save the old settings.  */
7251   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7252   saved_allow_non_integral_constant_expression_p
7253     = parser->allow_non_integral_constant_expression_p;
7254   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7255   /* We are now parsing a constant-expression.  */
7256   parser->integral_constant_expression_p = true;
7257   parser->allow_non_integral_constant_expression_p
7258     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7259   parser->non_integral_constant_expression_p = false;
7260   /* Although the grammar says "conditional-expression", we parse an
7261      "assignment-expression", which also permits "throw-expression"
7262      and the use of assignment operators.  In the case that
7263      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7264      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7265      actually essential that we look for an assignment-expression.
7266      For example, cp_parser_initializer_clauses uses this function to
7267      determine whether a particular assignment-expression is in fact
7268      constant.  */
7269   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7270   /* Restore the old settings.  */
7271   parser->integral_constant_expression_p
7272     = saved_integral_constant_expression_p;
7273   parser->allow_non_integral_constant_expression_p
7274     = saved_allow_non_integral_constant_expression_p;
7275   if (cxx_dialect >= cxx0x)
7276     {
7277       /* Require an rvalue constant expression here; that's what our
7278          callers expect.  Reference constant expressions are handled
7279          separately in e.g. cp_parser_template_argument.  */
7280       bool is_const = potential_rvalue_constant_expression (expression);
7281       parser->non_integral_constant_expression_p = !is_const;
7282       if (!is_const && !allow_non_constant_p)
7283         require_potential_rvalue_constant_expression (expression);
7284     }
7285   if (allow_non_constant_p)
7286     *non_constant_p = parser->non_integral_constant_expression_p;
7287   else if (parser->non_integral_constant_expression_p)
7288     expression = error_mark_node;
7289   parser->non_integral_constant_expression_p
7290     = saved_non_integral_constant_expression_p;
7291
7292   return expression;
7293 }
7294
7295 /* Parse __builtin_offsetof.
7296
7297    offsetof-expression:
7298      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7299
7300    offsetof-member-designator:
7301      id-expression
7302      | offsetof-member-designator "." id-expression
7303      | offsetof-member-designator "[" expression "]"
7304      | offsetof-member-designator "->" id-expression  */
7305
7306 static tree
7307 cp_parser_builtin_offsetof (cp_parser *parser)
7308 {
7309   int save_ice_p, save_non_ice_p;
7310   tree type, expr;
7311   cp_id_kind dummy;
7312   cp_token *token;
7313
7314   /* We're about to accept non-integral-constant things, but will
7315      definitely yield an integral constant expression.  Save and
7316      restore these values around our local parsing.  */
7317   save_ice_p = parser->integral_constant_expression_p;
7318   save_non_ice_p = parser->non_integral_constant_expression_p;
7319
7320   /* Consume the "__builtin_offsetof" token.  */
7321   cp_lexer_consume_token (parser->lexer);
7322   /* Consume the opening `('.  */
7323   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7324   /* Parse the type-id.  */
7325   type = cp_parser_type_id (parser);
7326   /* Look for the `,'.  */
7327   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7328   token = cp_lexer_peek_token (parser->lexer);
7329
7330   /* Build the (type *)null that begins the traditional offsetof macro.  */
7331   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7332                             tf_warning_or_error);
7333
7334   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7335   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7336                                                  true, &dummy, token->location);
7337   while (true)
7338     {
7339       token = cp_lexer_peek_token (parser->lexer);
7340       switch (token->type)
7341         {
7342         case CPP_OPEN_SQUARE:
7343           /* offsetof-member-designator "[" expression "]" */
7344           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7345           break;
7346
7347         case CPP_DEREF:
7348           /* offsetof-member-designator "->" identifier */
7349           expr = grok_array_decl (expr, integer_zero_node);
7350           /* FALLTHRU */
7351
7352         case CPP_DOT:
7353           /* offsetof-member-designator "." identifier */
7354           cp_lexer_consume_token (parser->lexer);
7355           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7356                                                          expr, true, &dummy,
7357                                                          token->location);
7358           break;
7359
7360         case CPP_CLOSE_PAREN:
7361           /* Consume the ")" token.  */
7362           cp_lexer_consume_token (parser->lexer);
7363           goto success;
7364
7365         default:
7366           /* Error.  We know the following require will fail, but
7367              that gives the proper error message.  */
7368           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7369           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7370           expr = error_mark_node;
7371           goto failure;
7372         }
7373     }
7374
7375  success:
7376   /* If we're processing a template, we can't finish the semantics yet.
7377      Otherwise we can fold the entire expression now.  */
7378   if (processing_template_decl)
7379     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7380   else
7381     expr = finish_offsetof (expr);
7382
7383  failure:
7384   parser->integral_constant_expression_p = save_ice_p;
7385   parser->non_integral_constant_expression_p = save_non_ice_p;
7386
7387   return expr;
7388 }
7389
7390 /* Parse a trait expression.  */
7391
7392 static tree
7393 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7394 {
7395   cp_trait_kind kind;
7396   tree type1, type2 = NULL_TREE;
7397   bool binary = false;
7398   cp_decl_specifier_seq decl_specs;
7399
7400   switch (keyword)
7401     {
7402     case RID_HAS_NOTHROW_ASSIGN:
7403       kind = CPTK_HAS_NOTHROW_ASSIGN;
7404       break;
7405     case RID_HAS_NOTHROW_CONSTRUCTOR:
7406       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7407       break;
7408     case RID_HAS_NOTHROW_COPY:
7409       kind = CPTK_HAS_NOTHROW_COPY;
7410       break;
7411     case RID_HAS_TRIVIAL_ASSIGN:
7412       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7413       break;
7414     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7415       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7416       break;
7417     case RID_HAS_TRIVIAL_COPY:
7418       kind = CPTK_HAS_TRIVIAL_COPY;
7419       break;
7420     case RID_HAS_TRIVIAL_DESTRUCTOR:
7421       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7422       break;
7423     case RID_HAS_VIRTUAL_DESTRUCTOR:
7424       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7425       break;
7426     case RID_IS_ABSTRACT:
7427       kind = CPTK_IS_ABSTRACT;
7428       break;
7429     case RID_IS_BASE_OF:
7430       kind = CPTK_IS_BASE_OF;
7431       binary = true;
7432       break;
7433     case RID_IS_CLASS:
7434       kind = CPTK_IS_CLASS;
7435       break;
7436     case RID_IS_CONVERTIBLE_TO:
7437       kind = CPTK_IS_CONVERTIBLE_TO;
7438       binary = true;
7439       break;
7440     case RID_IS_EMPTY:
7441       kind = CPTK_IS_EMPTY;
7442       break;
7443     case RID_IS_ENUM:
7444       kind = CPTK_IS_ENUM;
7445       break;
7446     case RID_IS_POD:
7447       kind = CPTK_IS_POD;
7448       break;
7449     case RID_IS_POLYMORPHIC:
7450       kind = CPTK_IS_POLYMORPHIC;
7451       break;
7452     case RID_IS_STD_LAYOUT:
7453       kind = CPTK_IS_STD_LAYOUT;
7454       break;
7455     case RID_IS_TRIVIAL:
7456       kind = CPTK_IS_TRIVIAL;
7457       break;
7458     case RID_IS_UNION:
7459       kind = CPTK_IS_UNION;
7460       break;
7461     case RID_IS_LITERAL_TYPE:
7462       kind = CPTK_IS_LITERAL_TYPE;
7463       break;
7464     default:
7465       gcc_unreachable ();
7466     }
7467
7468   /* Consume the token.  */
7469   cp_lexer_consume_token (parser->lexer);
7470
7471   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7472
7473   type1 = cp_parser_type_id (parser);
7474
7475   if (type1 == error_mark_node)
7476     return error_mark_node;
7477
7478   /* Build a trivial decl-specifier-seq.  */
7479   clear_decl_specs (&decl_specs);
7480   decl_specs.type = type1;
7481
7482   /* Call grokdeclarator to figure out what type this is.  */
7483   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7484                           /*initialized=*/0, /*attrlist=*/NULL);
7485
7486   if (binary)
7487     {
7488       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7489  
7490       type2 = cp_parser_type_id (parser);
7491
7492       if (type2 == error_mark_node)
7493         return error_mark_node;
7494
7495       /* Build a trivial decl-specifier-seq.  */
7496       clear_decl_specs (&decl_specs);
7497       decl_specs.type = type2;
7498
7499       /* Call grokdeclarator to figure out what type this is.  */
7500       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7501                               /*initialized=*/0, /*attrlist=*/NULL);
7502     }
7503
7504   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7505
7506   /* Complete the trait expression, which may mean either processing
7507      the trait expr now or saving it for template instantiation.  */
7508   return finish_trait_expr (kind, type1, type2);
7509 }
7510
7511 /* Lambdas that appear in variable initializer or default argument scope
7512    get that in their mangling, so we need to record it.  We might as well
7513    use the count for function and namespace scopes as well.  */
7514 static GTY(()) tree lambda_scope;
7515 static GTY(()) int lambda_count;
7516 typedef struct GTY(()) tree_int
7517 {
7518   tree t;
7519   int i;
7520 } tree_int;
7521 DEF_VEC_O(tree_int);
7522 DEF_VEC_ALLOC_O(tree_int,gc);
7523 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7524
7525 static void
7526 start_lambda_scope (tree decl)
7527 {
7528   tree_int ti;
7529   gcc_assert (decl);
7530   /* Once we're inside a function, we ignore other scopes and just push
7531      the function again so that popping works properly.  */
7532   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7533     decl = current_function_decl;
7534   ti.t = lambda_scope;
7535   ti.i = lambda_count;
7536   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7537   if (lambda_scope != decl)
7538     {
7539       /* Don't reset the count if we're still in the same function.  */
7540       lambda_scope = decl;
7541       lambda_count = 0;
7542     }
7543 }
7544
7545 static void
7546 record_lambda_scope (tree lambda)
7547 {
7548   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7549   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7550 }
7551
7552 static void
7553 finish_lambda_scope (void)
7554 {
7555   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7556   if (lambda_scope != p->t)
7557     {
7558       lambda_scope = p->t;
7559       lambda_count = p->i;
7560     }
7561   VEC_pop (tree_int, lambda_scope_stack);
7562 }
7563
7564 /* Parse a lambda expression.
7565
7566    lambda-expression:
7567      lambda-introducer lambda-declarator [opt] compound-statement
7568
7569    Returns a representation of the expression.  */
7570
7571 static tree
7572 cp_parser_lambda_expression (cp_parser* parser)
7573 {
7574   tree lambda_expr = build_lambda_expr ();
7575   tree type;
7576
7577   LAMBDA_EXPR_LOCATION (lambda_expr)
7578     = cp_lexer_peek_token (parser->lexer)->location;
7579
7580   if (cp_unevaluated_operand)
7581     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7582               "lambda-expression in unevaluated context");
7583
7584   /* We may be in the middle of deferred access check.  Disable
7585      it now.  */
7586   push_deferring_access_checks (dk_no_deferred);
7587
7588   cp_parser_lambda_introducer (parser, lambda_expr);
7589
7590   type = begin_lambda_type (lambda_expr);
7591
7592   record_lambda_scope (lambda_expr);
7593
7594   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7595   determine_visibility (TYPE_NAME (type));
7596
7597   /* Now that we've started the type, add the capture fields for any
7598      explicit captures.  */
7599   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7600
7601   {
7602     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7603     unsigned int saved_num_template_parameter_lists
7604         = parser->num_template_parameter_lists;
7605
7606     parser->num_template_parameter_lists = 0;
7607
7608     /* By virtue of defining a local class, a lambda expression has access to
7609        the private variables of enclosing classes.  */
7610
7611     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7612
7613     cp_parser_lambda_body (parser, lambda_expr);
7614
7615     /* The capture list was built up in reverse order; fix that now.  */
7616     {
7617       tree newlist = NULL_TREE;
7618       tree elt, next;
7619
7620       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7621            elt; elt = next)
7622         {
7623           tree field = TREE_PURPOSE (elt);
7624           char *buf;
7625
7626           next = TREE_CHAIN (elt);
7627           TREE_CHAIN (elt) = newlist;
7628           newlist = elt;
7629
7630           /* Also add __ to the beginning of the field name so that code
7631              outside the lambda body can't see the captured name.  We could
7632              just remove the name entirely, but this is more useful for
7633              debugging.  */
7634           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7635             /* The 'this' capture already starts with __.  */
7636             continue;
7637
7638           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7639           buf[1] = buf[0] = '_';
7640           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7641                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7642           DECL_NAME (field) = get_identifier (buf);
7643         }
7644       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7645     }
7646
7647     maybe_add_lambda_conv_op (type);
7648
7649     type = finish_struct (type, /*attributes=*/NULL_TREE);
7650
7651     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7652   }
7653
7654   pop_deferring_access_checks ();
7655
7656   return build_lambda_object (lambda_expr);
7657 }
7658
7659 /* Parse the beginning of a lambda expression.
7660
7661    lambda-introducer:
7662      [ lambda-capture [opt] ]
7663
7664    LAMBDA_EXPR is the current representation of the lambda expression.  */
7665
7666 static void
7667 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7668 {
7669   /* Need commas after the first capture.  */
7670   bool first = true;
7671
7672   /* Eat the leading `['.  */
7673   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7674
7675   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7676   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7677       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7678     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7679   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7680     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7681
7682   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7683     {
7684       cp_lexer_consume_token (parser->lexer);
7685       first = false;
7686     }
7687
7688   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7689     {
7690       cp_token* capture_token;
7691       tree capture_id;
7692       tree capture_init_expr;
7693       cp_id_kind idk = CP_ID_KIND_NONE;
7694       bool explicit_init_p = false;
7695
7696       enum capture_kind_type
7697       {
7698         BY_COPY,
7699         BY_REFERENCE
7700       };
7701       enum capture_kind_type capture_kind = BY_COPY;
7702
7703       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7704         {
7705           error ("expected end of capture-list");
7706           return;
7707         }
7708
7709       if (first)
7710         first = false;
7711       else
7712         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7713
7714       /* Possibly capture `this'.  */
7715       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7716         {
7717           cp_lexer_consume_token (parser->lexer);
7718           add_capture (lambda_expr,
7719                        /*id=*/get_identifier ("__this"),
7720                        /*initializer=*/finish_this_expr(),
7721                        /*by_reference_p=*/false,
7722                        explicit_init_p);
7723           continue;
7724         }
7725
7726       /* Remember whether we want to capture as a reference or not.  */
7727       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7728         {
7729           capture_kind = BY_REFERENCE;
7730           cp_lexer_consume_token (parser->lexer);
7731         }
7732
7733       /* Get the identifier.  */
7734       capture_token = cp_lexer_peek_token (parser->lexer);
7735       capture_id = cp_parser_identifier (parser);
7736
7737       if (capture_id == error_mark_node)
7738         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7739            delimiters, but I modified this to stop on unnested ']' as well.  It
7740            was already changed to stop on unnested '}', so the
7741            "closing_parenthesis" name is no more misleading with my change.  */
7742         {
7743           cp_parser_skip_to_closing_parenthesis (parser,
7744                                                  /*recovering=*/true,
7745                                                  /*or_comma=*/true,
7746                                                  /*consume_paren=*/true);
7747           break;
7748         }
7749
7750       /* Find the initializer for this capture.  */
7751       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7752         {
7753           /* An explicit expression exists.  */
7754           cp_lexer_consume_token (parser->lexer);
7755           pedwarn (input_location, OPT_pedantic,
7756                    "ISO C++ does not allow initializers "
7757                    "in lambda expression capture lists");
7758           capture_init_expr = cp_parser_assignment_expression (parser,
7759                                                                /*cast_p=*/true,
7760                                                                &idk);
7761           explicit_init_p = true;
7762         }
7763       else
7764         {
7765           const char* error_msg;
7766
7767           /* Turn the identifier into an id-expression.  */
7768           capture_init_expr
7769             = cp_parser_lookup_name
7770                 (parser,
7771                  capture_id,
7772                  none_type,
7773                  /*is_template=*/false,
7774                  /*is_namespace=*/false,
7775                  /*check_dependency=*/true,
7776                  /*ambiguous_decls=*/NULL,
7777                  capture_token->location);
7778
7779           capture_init_expr
7780             = finish_id_expression
7781                 (capture_id,
7782                  capture_init_expr,
7783                  parser->scope,
7784                  &idk,
7785                  /*integral_constant_expression_p=*/false,
7786                  /*allow_non_integral_constant_expression_p=*/false,
7787                  /*non_integral_constant_expression_p=*/NULL,
7788                  /*template_p=*/false,
7789                  /*done=*/true,
7790                  /*address_p=*/false,
7791                  /*template_arg_p=*/false,
7792                  &error_msg,
7793                  capture_token->location);
7794         }
7795
7796       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7797         capture_init_expr
7798           = unqualified_name_lookup_error (capture_init_expr);
7799
7800       add_capture (lambda_expr,
7801                    capture_id,
7802                    capture_init_expr,
7803                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7804                    explicit_init_p);
7805     }
7806
7807   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7808 }
7809
7810 /* Parse the (optional) middle of a lambda expression.
7811
7812    lambda-declarator:
7813      ( parameter-declaration-clause [opt] )
7814        attribute-specifier [opt]
7815        mutable [opt]
7816        exception-specification [opt]
7817        lambda-return-type-clause [opt]
7818
7819    LAMBDA_EXPR is the current representation of the lambda expression.  */
7820
7821 static void
7822 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7823 {
7824   /* 5.1.1.4 of the standard says:
7825        If a lambda-expression does not include a lambda-declarator, it is as if
7826        the lambda-declarator were ().
7827      This means an empty parameter list, no attributes, and no exception
7828      specification.  */
7829   tree param_list = void_list_node;
7830   tree attributes = NULL_TREE;
7831   tree exception_spec = NULL_TREE;
7832   tree t;
7833
7834   /* The lambda-declarator is optional, but must begin with an opening
7835      parenthesis if present.  */
7836   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7837     {
7838       cp_lexer_consume_token (parser->lexer);
7839
7840       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7841
7842       /* Parse parameters.  */
7843       param_list = cp_parser_parameter_declaration_clause (parser);
7844
7845       /* Default arguments shall not be specified in the
7846          parameter-declaration-clause of a lambda-declarator.  */
7847       for (t = param_list; t; t = TREE_CHAIN (t))
7848         if (TREE_PURPOSE (t))
7849           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7850                    "default argument specified for lambda parameter");
7851
7852       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7853
7854       attributes = cp_parser_attributes_opt (parser);
7855
7856       /* Parse optional `mutable' keyword.  */
7857       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7858         {
7859           cp_lexer_consume_token (parser->lexer);
7860           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7861         }
7862
7863       /* Parse optional exception specification.  */
7864       exception_spec = cp_parser_exception_specification_opt (parser);
7865
7866       /* Parse optional trailing return type.  */
7867       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7868         {
7869           cp_lexer_consume_token (parser->lexer);
7870           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7871         }
7872
7873       /* The function parameters must be in scope all the way until after the
7874          trailing-return-type in case of decltype.  */
7875       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7876         pop_binding (DECL_NAME (t), t);
7877
7878       leave_scope ();
7879     }
7880
7881   /* Create the function call operator.
7882
7883      Messing with declarators like this is no uglier than building up the
7884      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7885      other code.  */
7886   {
7887     cp_decl_specifier_seq return_type_specs;
7888     cp_declarator* declarator;
7889     tree fco;
7890     int quals;
7891     void *p;
7892
7893     clear_decl_specs (&return_type_specs);
7894     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7895       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7896     else
7897       /* Maybe we will deduce the return type later, but we can use void
7898          as a placeholder return type anyways.  */
7899       return_type_specs.type = void_type_node;
7900
7901     p = obstack_alloc (&declarator_obstack, 0);
7902
7903     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7904                                      sfk_none);
7905
7906     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7907              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7908     declarator = make_call_declarator (declarator, param_list, quals,
7909                                        exception_spec,
7910                                        /*late_return_type=*/NULL_TREE);
7911     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7912
7913     fco = grokmethod (&return_type_specs,
7914                       declarator,
7915                       attributes);
7916     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7917     DECL_ARTIFICIAL (fco) = 1;
7918
7919     finish_member_declaration (fco);
7920
7921     obstack_free (&declarator_obstack, p);
7922   }
7923 }
7924
7925 /* Parse the body of a lambda expression, which is simply
7926
7927    compound-statement
7928
7929    but which requires special handling.
7930    LAMBDA_EXPR is the current representation of the lambda expression.  */
7931
7932 static void
7933 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7934 {
7935   bool nested = (current_function_decl != NULL_TREE);
7936   if (nested)
7937     push_function_context ();
7938
7939   /* Finish the function call operator
7940      - class_specifier
7941      + late_parsing_for_member
7942      + function_definition_after_declarator
7943      + ctor_initializer_opt_and_function_body  */
7944   {
7945     tree fco = lambda_function (lambda_expr);
7946     tree body;
7947     bool done = false;
7948
7949     /* Let the front end know that we are going to be defining this
7950        function.  */
7951     start_preparsed_function (fco,
7952                               NULL_TREE,
7953                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7954
7955     start_lambda_scope (fco);
7956     body = begin_function_body ();
7957
7958     /* 5.1.1.4 of the standard says:
7959          If a lambda-expression does not include a trailing-return-type, it
7960          is as if the trailing-return-type denotes the following type:
7961           * if the compound-statement is of the form
7962                { return attribute-specifier [opt] expression ; }
7963              the type of the returned expression after lvalue-to-rvalue
7964              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7965              (_conv.array_ 4.2), and function-to-pointer conversion
7966              (_conv.func_ 4.3);
7967           * otherwise, void.  */
7968
7969     /* In a lambda that has neither a lambda-return-type-clause
7970        nor a deducible form, errors should be reported for return statements
7971        in the body.  Since we used void as the placeholder return type, parsing
7972        the body as usual will give such desired behavior.  */
7973     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7974         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7975         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7976         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7977       {
7978         tree compound_stmt;
7979         tree expr = NULL_TREE;
7980         cp_id_kind idk = CP_ID_KIND_NONE;
7981
7982         /* Parse tentatively in case there's more after the initial return
7983            statement.  */
7984         cp_parser_parse_tentatively (parser);
7985
7986         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7987         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7988
7989         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7990
7991         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7992         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7993
7994         if (cp_parser_parse_definitely (parser))
7995           {
7996             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7997
7998             compound_stmt = begin_compound_stmt (0);
7999             /* Will get error here if type not deduced yet.  */
8000             finish_return_stmt (expr);
8001             finish_compound_stmt (compound_stmt);
8002
8003             done = true;
8004           }
8005       }
8006
8007     if (!done)
8008       {
8009         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8010           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8011         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
8012            cp_parser_compound_stmt does not pass it.  */
8013         cp_parser_function_body (parser);
8014         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8015       }
8016
8017     finish_function_body (body);
8018     finish_lambda_scope ();
8019
8020     /* Finish the function and generate code for it if necessary.  */
8021     expand_or_defer_fn (finish_function (/*inline*/2));
8022   }
8023
8024   if (nested)
8025     pop_function_context();
8026 }
8027
8028 /* Statements [gram.stmt.stmt]  */
8029
8030 /* Parse a statement.
8031
8032    statement:
8033      labeled-statement
8034      expression-statement
8035      compound-statement
8036      selection-statement
8037      iteration-statement
8038      jump-statement
8039      declaration-statement
8040      try-block
8041
8042   IN_COMPOUND is true when the statement is nested inside a
8043   cp_parser_compound_statement; this matters for certain pragmas.
8044
8045   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8046   is a (possibly labeled) if statement which is not enclosed in braces
8047   and has an else clause.  This is used to implement -Wparentheses.  */
8048
8049 static void
8050 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8051                      bool in_compound, bool *if_p)
8052 {
8053   tree statement;
8054   cp_token *token;
8055   location_t statement_location;
8056
8057  restart:
8058   if (if_p != NULL)
8059     *if_p = false;
8060   /* There is no statement yet.  */
8061   statement = NULL_TREE;
8062   /* Peek at the next token.  */
8063   token = cp_lexer_peek_token (parser->lexer);
8064   /* Remember the location of the first token in the statement.  */
8065   statement_location = token->location;
8066   /* If this is a keyword, then that will often determine what kind of
8067      statement we have.  */
8068   if (token->type == CPP_KEYWORD)
8069     {
8070       enum rid keyword = token->keyword;
8071
8072       switch (keyword)
8073         {
8074         case RID_CASE:
8075         case RID_DEFAULT:
8076           /* Looks like a labeled-statement with a case label.
8077              Parse the label, and then use tail recursion to parse
8078              the statement.  */
8079           cp_parser_label_for_labeled_statement (parser);
8080           goto restart;
8081
8082         case RID_IF:
8083         case RID_SWITCH:
8084           statement = cp_parser_selection_statement (parser, if_p);
8085           break;
8086
8087         case RID_WHILE:
8088         case RID_DO:
8089         case RID_FOR:
8090           statement = cp_parser_iteration_statement (parser);
8091           break;
8092
8093         case RID_BREAK:
8094         case RID_CONTINUE:
8095         case RID_RETURN:
8096         case RID_GOTO:
8097           statement = cp_parser_jump_statement (parser);
8098           break;
8099
8100           /* Objective-C++ exception-handling constructs.  */
8101         case RID_AT_TRY:
8102         case RID_AT_CATCH:
8103         case RID_AT_FINALLY:
8104         case RID_AT_SYNCHRONIZED:
8105         case RID_AT_THROW:
8106           statement = cp_parser_objc_statement (parser);
8107           break;
8108
8109         case RID_TRY:
8110           statement = cp_parser_try_block (parser);
8111           break;
8112
8113         case RID_NAMESPACE:
8114           /* This must be a namespace alias definition.  */
8115           cp_parser_declaration_statement (parser);
8116           return;
8117           
8118         default:
8119           /* It might be a keyword like `int' that can start a
8120              declaration-statement.  */
8121           break;
8122         }
8123     }
8124   else if (token->type == CPP_NAME)
8125     {
8126       /* If the next token is a `:', then we are looking at a
8127          labeled-statement.  */
8128       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8129       if (token->type == CPP_COLON)
8130         {
8131           /* Looks like a labeled-statement with an ordinary label.
8132              Parse the label, and then use tail recursion to parse
8133              the statement.  */
8134           cp_parser_label_for_labeled_statement (parser);
8135           goto restart;
8136         }
8137     }
8138   /* Anything that starts with a `{' must be a compound-statement.  */
8139   else if (token->type == CPP_OPEN_BRACE)
8140     statement = cp_parser_compound_statement (parser, NULL, false);
8141   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8142      a statement all its own.  */
8143   else if (token->type == CPP_PRAGMA)
8144     {
8145       /* Only certain OpenMP pragmas are attached to statements, and thus
8146          are considered statements themselves.  All others are not.  In
8147          the context of a compound, accept the pragma as a "statement" and
8148          return so that we can check for a close brace.  Otherwise we
8149          require a real statement and must go back and read one.  */
8150       if (in_compound)
8151         cp_parser_pragma (parser, pragma_compound);
8152       else if (!cp_parser_pragma (parser, pragma_stmt))
8153         goto restart;
8154       return;
8155     }
8156   else if (token->type == CPP_EOF)
8157     {
8158       cp_parser_error (parser, "expected statement");
8159       return;
8160     }
8161
8162   /* Everything else must be a declaration-statement or an
8163      expression-statement.  Try for the declaration-statement
8164      first, unless we are looking at a `;', in which case we know that
8165      we have an expression-statement.  */
8166   if (!statement)
8167     {
8168       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8169         {
8170           cp_parser_parse_tentatively (parser);
8171           /* Try to parse the declaration-statement.  */
8172           cp_parser_declaration_statement (parser);
8173           /* If that worked, we're done.  */
8174           if (cp_parser_parse_definitely (parser))
8175             return;
8176         }
8177       /* Look for an expression-statement instead.  */
8178       statement = cp_parser_expression_statement (parser, in_statement_expr);
8179     }
8180
8181   /* Set the line number for the statement.  */
8182   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8183     SET_EXPR_LOCATION (statement, statement_location);
8184 }
8185
8186 /* Parse the label for a labeled-statement, i.e.
8187
8188    identifier :
8189    case constant-expression :
8190    default :
8191
8192    GNU Extension:
8193    case constant-expression ... constant-expression : statement
8194
8195    When a label is parsed without errors, the label is added to the
8196    parse tree by the finish_* functions, so this function doesn't
8197    have to return the label.  */
8198
8199 static void
8200 cp_parser_label_for_labeled_statement (cp_parser* parser)
8201 {
8202   cp_token *token;
8203   tree label = NULL_TREE;
8204   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8205
8206   /* The next token should be an identifier.  */
8207   token = cp_lexer_peek_token (parser->lexer);
8208   if (token->type != CPP_NAME
8209       && token->type != CPP_KEYWORD)
8210     {
8211       cp_parser_error (parser, "expected labeled-statement");
8212       return;
8213     }
8214
8215   parser->colon_corrects_to_scope_p = false;
8216   switch (token->keyword)
8217     {
8218     case RID_CASE:
8219       {
8220         tree expr, expr_hi;
8221         cp_token *ellipsis;
8222
8223         /* Consume the `case' token.  */
8224         cp_lexer_consume_token (parser->lexer);
8225         /* Parse the constant-expression.  */
8226         expr = cp_parser_constant_expression (parser,
8227                                               /*allow_non_constant_p=*/false,
8228                                               NULL);
8229
8230         ellipsis = cp_lexer_peek_token (parser->lexer);
8231         if (ellipsis->type == CPP_ELLIPSIS)
8232           {
8233             /* Consume the `...' token.  */
8234             cp_lexer_consume_token (parser->lexer);
8235             expr_hi =
8236               cp_parser_constant_expression (parser,
8237                                              /*allow_non_constant_p=*/false,
8238                                              NULL);
8239             /* We don't need to emit warnings here, as the common code
8240                will do this for us.  */
8241           }
8242         else
8243           expr_hi = NULL_TREE;
8244
8245         if (parser->in_switch_statement_p)
8246           finish_case_label (token->location, expr, expr_hi);
8247         else
8248           error_at (token->location,
8249                     "case label %qE not within a switch statement",
8250                     expr);
8251       }
8252       break;
8253
8254     case RID_DEFAULT:
8255       /* Consume the `default' token.  */
8256       cp_lexer_consume_token (parser->lexer);
8257
8258       if (parser->in_switch_statement_p)
8259         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8260       else
8261         error_at (token->location, "case label not within a switch statement");
8262       break;
8263
8264     default:
8265       /* Anything else must be an ordinary label.  */
8266       label = finish_label_stmt (cp_parser_identifier (parser));
8267       break;
8268     }
8269
8270   /* Require the `:' token.  */
8271   cp_parser_require (parser, CPP_COLON, RT_COLON);
8272
8273   /* An ordinary label may optionally be followed by attributes.
8274      However, this is only permitted if the attributes are then
8275      followed by a semicolon.  This is because, for backward
8276      compatibility, when parsing
8277        lab: __attribute__ ((unused)) int i;
8278      we want the attribute to attach to "i", not "lab".  */
8279   if (label != NULL_TREE
8280       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8281     {
8282       tree attrs;
8283
8284       cp_parser_parse_tentatively (parser);
8285       attrs = cp_parser_attributes_opt (parser);
8286       if (attrs == NULL_TREE
8287           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8288         cp_parser_abort_tentative_parse (parser);
8289       else if (!cp_parser_parse_definitely (parser))
8290         ;
8291       else
8292         cplus_decl_attributes (&label, attrs, 0);
8293     }
8294
8295   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8296 }
8297
8298 /* Parse an expression-statement.
8299
8300    expression-statement:
8301      expression [opt] ;
8302
8303    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8304    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8305    indicates whether this expression-statement is part of an
8306    expression statement.  */
8307
8308 static tree
8309 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8310 {
8311   tree statement = NULL_TREE;
8312   cp_token *token = cp_lexer_peek_token (parser->lexer);
8313
8314   /* If the next token is a ';', then there is no expression
8315      statement.  */
8316   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8317     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8318
8319   /* Give a helpful message for "A<T>::type t;" and the like.  */
8320   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8321       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8322     {
8323       if (TREE_CODE (statement) == SCOPE_REF)
8324         error_at (token->location, "need %<typename%> before %qE because "
8325                   "%qT is a dependent scope",
8326                   statement, TREE_OPERAND (statement, 0));
8327       else if (is_overloaded_fn (statement)
8328                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8329         {
8330           /* A::A a; */
8331           tree fn = get_first_fn (statement);
8332           error_at (token->location,
8333                     "%<%T::%D%> names the constructor, not the type",
8334                     DECL_CONTEXT (fn), DECL_NAME (fn));
8335         }
8336     }
8337
8338   /* Consume the final `;'.  */
8339   cp_parser_consume_semicolon_at_end_of_statement (parser);
8340
8341   if (in_statement_expr
8342       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8343     /* This is the final expression statement of a statement
8344        expression.  */
8345     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8346   else if (statement)
8347     statement = finish_expr_stmt (statement);
8348   else
8349     finish_stmt ();
8350
8351   return statement;
8352 }
8353
8354 /* Parse a compound-statement.
8355
8356    compound-statement:
8357      { statement-seq [opt] }
8358
8359    GNU extension:
8360
8361    compound-statement:
8362      { label-declaration-seq [opt] statement-seq [opt] }
8363
8364    label-declaration-seq:
8365      label-declaration
8366      label-declaration-seq label-declaration
8367
8368    Returns a tree representing the statement.  */
8369
8370 static tree
8371 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8372                               bool in_try)
8373 {
8374   tree compound_stmt;
8375
8376   /* Consume the `{'.  */
8377   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8378     return error_mark_node;
8379   /* Begin the compound-statement.  */
8380   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8381   /* If the next keyword is `__label__' we have a label declaration.  */
8382   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8383     cp_parser_label_declaration (parser);
8384   /* Parse an (optional) statement-seq.  */
8385   cp_parser_statement_seq_opt (parser, in_statement_expr);
8386   /* Finish the compound-statement.  */
8387   finish_compound_stmt (compound_stmt);
8388   /* Consume the `}'.  */
8389   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8390
8391   return compound_stmt;
8392 }
8393
8394 /* Parse an (optional) statement-seq.
8395
8396    statement-seq:
8397      statement
8398      statement-seq [opt] statement  */
8399
8400 static void
8401 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8402 {
8403   /* Scan statements until there aren't any more.  */
8404   while (true)
8405     {
8406       cp_token *token = cp_lexer_peek_token (parser->lexer);
8407
8408       /* If we are looking at a `}', then we have run out of
8409          statements; the same is true if we have reached the end
8410          of file, or have stumbled upon a stray '@end'.  */
8411       if (token->type == CPP_CLOSE_BRACE
8412           || token->type == CPP_EOF
8413           || token->type == CPP_PRAGMA_EOL
8414           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8415         break;
8416       
8417       /* If we are in a compound statement and find 'else' then
8418          something went wrong.  */
8419       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8420         {
8421           if (parser->in_statement & IN_IF_STMT) 
8422             break;
8423           else
8424             {
8425               token = cp_lexer_consume_token (parser->lexer);
8426               error_at (token->location, "%<else%> without a previous %<if%>");
8427             }
8428         }
8429
8430       /* Parse the statement.  */
8431       cp_parser_statement (parser, in_statement_expr, true, NULL);
8432     }
8433 }
8434
8435 /* Parse a selection-statement.
8436
8437    selection-statement:
8438      if ( condition ) statement
8439      if ( condition ) statement else statement
8440      switch ( condition ) statement
8441
8442    Returns the new IF_STMT or SWITCH_STMT.
8443
8444    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8445    is a (possibly labeled) if statement which is not enclosed in
8446    braces and has an else clause.  This is used to implement
8447    -Wparentheses.  */
8448
8449 static tree
8450 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8451 {
8452   cp_token *token;
8453   enum rid keyword;
8454
8455   if (if_p != NULL)
8456     *if_p = false;
8457
8458   /* Peek at the next token.  */
8459   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8460
8461   /* See what kind of keyword it is.  */
8462   keyword = token->keyword;
8463   switch (keyword)
8464     {
8465     case RID_IF:
8466     case RID_SWITCH:
8467       {
8468         tree statement;
8469         tree condition;
8470
8471         /* Look for the `('.  */
8472         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8473           {
8474             cp_parser_skip_to_end_of_statement (parser);
8475             return error_mark_node;
8476           }
8477
8478         /* Begin the selection-statement.  */
8479         if (keyword == RID_IF)
8480           statement = begin_if_stmt ();
8481         else
8482           statement = begin_switch_stmt ();
8483
8484         /* Parse the condition.  */
8485         condition = cp_parser_condition (parser);
8486         /* Look for the `)'.  */
8487         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8488           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8489                                                  /*consume_paren=*/true);
8490
8491         if (keyword == RID_IF)
8492           {
8493             bool nested_if;
8494             unsigned char in_statement;
8495
8496             /* Add the condition.  */
8497             finish_if_stmt_cond (condition, statement);
8498
8499             /* Parse the then-clause.  */
8500             in_statement = parser->in_statement;
8501             parser->in_statement |= IN_IF_STMT;
8502             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8503               {
8504                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8505                 add_stmt (build_empty_stmt (loc));
8506                 cp_lexer_consume_token (parser->lexer);
8507                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8508                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8509                               "empty body in an %<if%> statement");
8510                 nested_if = false;
8511               }
8512             else
8513               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8514             parser->in_statement = in_statement;
8515
8516             finish_then_clause (statement);
8517
8518             /* If the next token is `else', parse the else-clause.  */
8519             if (cp_lexer_next_token_is_keyword (parser->lexer,
8520                                                 RID_ELSE))
8521               {
8522                 /* Consume the `else' keyword.  */
8523                 cp_lexer_consume_token (parser->lexer);
8524                 begin_else_clause (statement);
8525                 /* Parse the else-clause.  */
8526                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8527                   {
8528                     location_t loc;
8529                     loc = cp_lexer_peek_token (parser->lexer)->location;
8530                     warning_at (loc,
8531                                 OPT_Wempty_body, "suggest braces around "
8532                                 "empty body in an %<else%> statement");
8533                     add_stmt (build_empty_stmt (loc));
8534                     cp_lexer_consume_token (parser->lexer);
8535                   }
8536                 else
8537                   cp_parser_implicitly_scoped_statement (parser, NULL);
8538
8539                 finish_else_clause (statement);
8540
8541                 /* If we are currently parsing a then-clause, then
8542                    IF_P will not be NULL.  We set it to true to
8543                    indicate that this if statement has an else clause.
8544                    This may trigger the Wparentheses warning below
8545                    when we get back up to the parent if statement.  */
8546                 if (if_p != NULL)
8547                   *if_p = true;
8548               }
8549             else
8550               {
8551                 /* This if statement does not have an else clause.  If
8552                    NESTED_IF is true, then the then-clause is an if
8553                    statement which does have an else clause.  We warn
8554                    about the potential ambiguity.  */
8555                 if (nested_if)
8556                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8557                               "suggest explicit braces to avoid ambiguous"
8558                               " %<else%>");
8559               }
8560
8561             /* Now we're all done with the if-statement.  */
8562             finish_if_stmt (statement);
8563           }
8564         else
8565           {
8566             bool in_switch_statement_p;
8567             unsigned char in_statement;
8568
8569             /* Add the condition.  */
8570             finish_switch_cond (condition, statement);
8571
8572             /* Parse the body of the switch-statement.  */
8573             in_switch_statement_p = parser->in_switch_statement_p;
8574             in_statement = parser->in_statement;
8575             parser->in_switch_statement_p = true;
8576             parser->in_statement |= IN_SWITCH_STMT;
8577             cp_parser_implicitly_scoped_statement (parser, NULL);
8578             parser->in_switch_statement_p = in_switch_statement_p;
8579             parser->in_statement = in_statement;
8580
8581             /* Now we're all done with the switch-statement.  */
8582             finish_switch_stmt (statement);
8583           }
8584
8585         return statement;
8586       }
8587       break;
8588
8589     default:
8590       cp_parser_error (parser, "expected selection-statement");
8591       return error_mark_node;
8592     }
8593 }
8594
8595 /* Parse a condition.
8596
8597    condition:
8598      expression
8599      type-specifier-seq declarator = initializer-clause
8600      type-specifier-seq declarator braced-init-list
8601
8602    GNU Extension:
8603
8604    condition:
8605      type-specifier-seq declarator asm-specification [opt]
8606        attributes [opt] = assignment-expression
8607
8608    Returns the expression that should be tested.  */
8609
8610 static tree
8611 cp_parser_condition (cp_parser* parser)
8612 {
8613   cp_decl_specifier_seq type_specifiers;
8614   const char *saved_message;
8615   int declares_class_or_enum;
8616
8617   /* Try the declaration first.  */
8618   cp_parser_parse_tentatively (parser);
8619   /* New types are not allowed in the type-specifier-seq for a
8620      condition.  */
8621   saved_message = parser->type_definition_forbidden_message;
8622   parser->type_definition_forbidden_message
8623     = G_("types may not be defined in conditions");
8624   /* Parse the type-specifier-seq.  */
8625   cp_parser_decl_specifier_seq (parser,
8626                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8627                                 &type_specifiers,
8628                                 &declares_class_or_enum);
8629   /* Restore the saved message.  */
8630   parser->type_definition_forbidden_message = saved_message;
8631   /* If all is well, we might be looking at a declaration.  */
8632   if (!cp_parser_error_occurred (parser))
8633     {
8634       tree decl;
8635       tree asm_specification;
8636       tree attributes;
8637       cp_declarator *declarator;
8638       tree initializer = NULL_TREE;
8639
8640       /* Parse the declarator.  */
8641       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8642                                          /*ctor_dtor_or_conv_p=*/NULL,
8643                                          /*parenthesized_p=*/NULL,
8644                                          /*member_p=*/false);
8645       /* Parse the attributes.  */
8646       attributes = cp_parser_attributes_opt (parser);
8647       /* Parse the asm-specification.  */
8648       asm_specification = cp_parser_asm_specification_opt (parser);
8649       /* If the next token is not an `=' or '{', then we might still be
8650          looking at an expression.  For example:
8651
8652            if (A(a).x)
8653
8654          looks like a decl-specifier-seq and a declarator -- but then
8655          there is no `=', so this is an expression.  */
8656       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8657           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8658         cp_parser_simulate_error (parser);
8659         
8660       /* If we did see an `=' or '{', then we are looking at a declaration
8661          for sure.  */
8662       if (cp_parser_parse_definitely (parser))
8663         {
8664           tree pushed_scope;
8665           bool non_constant_p;
8666           bool flags = LOOKUP_ONLYCONVERTING;
8667
8668           /* Create the declaration.  */
8669           decl = start_decl (declarator, &type_specifiers,
8670                              /*initialized_p=*/true,
8671                              attributes, /*prefix_attributes=*/NULL_TREE,
8672                              &pushed_scope);
8673
8674           /* Parse the initializer.  */
8675           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8676             {
8677               initializer = cp_parser_braced_list (parser, &non_constant_p);
8678               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8679               flags = 0;
8680             }
8681           else
8682             {
8683               /* Consume the `='.  */
8684               cp_parser_require (parser, CPP_EQ, RT_EQ);
8685               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8686             }
8687           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8688             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8689
8690           if (!non_constant_p)
8691             initializer = fold_non_dependent_expr (initializer);
8692
8693           /* Process the initializer.  */
8694           cp_finish_decl (decl,
8695                           initializer, !non_constant_p,
8696                           asm_specification,
8697                           flags);
8698
8699           if (pushed_scope)
8700             pop_scope (pushed_scope);
8701
8702           return convert_from_reference (decl);
8703         }
8704     }
8705   /* If we didn't even get past the declarator successfully, we are
8706      definitely not looking at a declaration.  */
8707   else
8708     cp_parser_abort_tentative_parse (parser);
8709
8710   /* Otherwise, we are looking at an expression.  */
8711   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8712 }
8713
8714 /* Parses a for-statement or range-for-statement until the closing ')',
8715    not included. */
8716
8717 static tree
8718 cp_parser_for (cp_parser *parser)
8719 {
8720   tree init, scope, decl;
8721   bool is_range_for;
8722
8723   /* Begin the for-statement.  */
8724   scope = begin_for_scope (&init);
8725
8726   /* Parse the initialization.  */
8727   is_range_for = cp_parser_for_init_statement (parser, &decl);
8728
8729   if (is_range_for)
8730     return cp_parser_range_for (parser, scope, init, decl);
8731   else
8732     return cp_parser_c_for (parser, scope, init);
8733 }
8734
8735 static tree
8736 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8737 {
8738   /* Normal for loop */
8739   tree condition = NULL_TREE;
8740   tree expression = NULL_TREE;
8741   tree stmt;
8742
8743   stmt = begin_for_stmt (scope, init);
8744   /* The for-init-statement has already been parsed in
8745      cp_parser_for_init_statement, so no work is needed here.  */
8746   finish_for_init_stmt (stmt);
8747
8748   /* If there's a condition, process it.  */
8749   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8750     condition = cp_parser_condition (parser);
8751   finish_for_cond (condition, stmt);
8752   /* Look for the `;'.  */
8753   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8754
8755   /* If there's an expression, process it.  */
8756   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8757     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8758   finish_for_expr (expression, stmt);
8759
8760   return stmt;
8761 }
8762
8763 /* Tries to parse a range-based for-statement:
8764
8765   range-based-for:
8766     decl-specifier-seq declarator : expression
8767
8768   The decl-specifier-seq declarator and the `:' are already parsed by
8769   cp_parser_for_init_statement. If processing_template_decl it returns a
8770   newly created RANGE_FOR_STMT; if not, it is converted to a
8771   regular FOR_STMT.  */
8772
8773 static tree
8774 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8775 {
8776   tree stmt, range_expr;
8777
8778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8779     {
8780       bool expr_non_constant_p;
8781       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8782     }
8783   else
8784     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8785
8786   /* If in template, STMT is converted to a normal for-statement
8787      at instantiation. If not, it is done just ahead. */
8788   if (processing_template_decl)
8789     {
8790       stmt = begin_range_for_stmt (scope, init);
8791       finish_range_for_decl (stmt, range_decl, range_expr);
8792     }
8793   else
8794     {
8795       stmt = begin_for_stmt (scope, init);
8796       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8797     }
8798   return stmt;
8799 }
8800
8801 /* Converts a range-based for-statement into a normal
8802    for-statement, as per the definition.
8803
8804       for (RANGE_DECL : RANGE_EXPR)
8805         BLOCK
8806
8807    should be equivalent to:
8808
8809       {
8810         auto &&__range = RANGE_EXPR;
8811         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8812               __begin != __end;
8813               ++__begin)
8814           {
8815               RANGE_DECL = *__begin;
8816               BLOCK
8817           }
8818       }
8819
8820    If RANGE_EXPR is an array:
8821        BEGIN_EXPR = __range
8822        END_EXPR = __range + ARRAY_SIZE(__range)
8823    Else:
8824         BEGIN_EXPR = begin(__range)
8825         END_EXPR = end(__range);
8826
8827    When calling begin()/end() we must use argument dependent
8828    lookup, but always considering 'std' as an associated namespace.  */
8829
8830 tree
8831 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8832 {
8833   tree range_type, range_temp;
8834   tree begin, end;
8835   tree iter_type, begin_expr, end_expr;
8836   tree condition, expression;
8837
8838   if (range_decl == error_mark_node || range_expr == error_mark_node)
8839     /* If an error happened previously do nothing or else a lot of
8840        unhelpful errors would be issued.  */
8841     begin_expr = end_expr = iter_type = error_mark_node;
8842   else
8843     {
8844       /* Find out the type deduced by the declaration
8845        * `auto &&__range = range_expr' */
8846       range_type = cp_build_reference_type (make_auto (), true);
8847       range_type = do_auto_deduction (range_type, range_expr,
8848                                       type_uses_auto (range_type));
8849
8850       /* Create the __range variable */
8851       range_temp = build_decl (input_location, VAR_DECL,
8852                                get_identifier ("__for_range"), range_type);
8853       TREE_USED (range_temp) = 1;
8854       DECL_ARTIFICIAL (range_temp) = 1;
8855       pushdecl (range_temp);
8856       cp_finish_decl (range_temp, range_expr,
8857                       /*is_constant_init*/false, NULL_TREE,
8858                       LOOKUP_ONLYCONVERTING);
8859
8860       range_temp = convert_from_reference (range_temp);
8861
8862       if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8863         {
8864           /* If RANGE_TEMP is an array we will use pointer arithmetic */
8865           iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8866           begin_expr = range_temp;
8867           end_expr
8868               = build_binary_op (input_location, PLUS_EXPR,
8869                                  range_temp,
8870                                  array_type_nelts_top (TREE_TYPE (range_temp)),
8871                                  0);
8872         }
8873       else
8874         {
8875           /* If it is not an array, we must call begin(__range)/end__range() */
8876           VEC(tree,gc) *vec;
8877
8878           begin_expr = get_identifier ("begin");
8879           vec = make_tree_vector ();
8880           VEC_safe_push (tree, gc, vec, range_temp);
8881           begin_expr = perform_koenig_lookup (begin_expr, vec,
8882                                               /*include_std=*/true);
8883           begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8884                                          tf_warning_or_error);
8885           release_tree_vector (vec);
8886
8887           end_expr = get_identifier ("end");
8888           vec = make_tree_vector ();
8889           VEC_safe_push (tree, gc, vec, range_temp);
8890           end_expr = perform_koenig_lookup (end_expr, vec,
8891                                             /*include_std=*/true);
8892           end_expr = finish_call_expr (end_expr, &vec, false, true,
8893                                        tf_warning_or_error);
8894           release_tree_vector (vec);
8895
8896           /* The unqualified type of the __begin and __end temporaries should
8897            * be the same as required by the multiple auto declaration */
8898           iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8899           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8900             error ("inconsistent begin/end types in range-based for: %qT and %qT",
8901                    TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8902         }
8903     }
8904
8905   /* The new for initialization statement */
8906   begin = build_decl (input_location, VAR_DECL,
8907                       get_identifier ("__for_begin"), iter_type);
8908   TREE_USED (begin) = 1;
8909   DECL_ARTIFICIAL (begin) = 1;
8910   pushdecl (begin);
8911   cp_finish_decl (begin, begin_expr,
8912                   /*is_constant_init*/false, NULL_TREE,
8913                   LOOKUP_ONLYCONVERTING);
8914
8915   end = build_decl (input_location, VAR_DECL,
8916                     get_identifier ("__for_end"), iter_type);
8917   TREE_USED (end) = 1;
8918   DECL_ARTIFICIAL (end) = 1;
8919   pushdecl (end);
8920   cp_finish_decl (end, end_expr,
8921                   /*is_constant_init*/false, NULL_TREE,
8922                   LOOKUP_ONLYCONVERTING);
8923
8924   finish_for_init_stmt (statement);
8925
8926 /* The new for condition */
8927   condition = build_x_binary_op (NE_EXPR,
8928                                  begin, ERROR_MARK,
8929                                  end, ERROR_MARK,
8930                                  NULL, tf_warning_or_error);
8931   finish_for_cond (condition, statement);
8932
8933   /* The new increment expression */
8934   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8935   finish_for_expr (expression, statement);
8936
8937   /* The declaration is initialized with *__begin inside the loop body */
8938   cp_finish_decl (range_decl,
8939                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8940                   /*is_constant_init*/false, NULL_TREE,
8941                   LOOKUP_ONLYCONVERTING);
8942
8943   return statement;
8944 }
8945
8946
8947 /* Parse an iteration-statement.
8948
8949    iteration-statement:
8950      while ( condition ) statement
8951      do statement while ( expression ) ;
8952      for ( for-init-statement condition [opt] ; expression [opt] )
8953        statement
8954
8955    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8956
8957 static tree
8958 cp_parser_iteration_statement (cp_parser* parser)
8959 {
8960   cp_token *token;
8961   enum rid keyword;
8962   tree statement;
8963   unsigned char in_statement;
8964
8965   /* Peek at the next token.  */
8966   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8967   if (!token)
8968     return error_mark_node;
8969
8970   /* Remember whether or not we are already within an iteration
8971      statement.  */
8972   in_statement = parser->in_statement;
8973
8974   /* See what kind of keyword it is.  */
8975   keyword = token->keyword;
8976   switch (keyword)
8977     {
8978     case RID_WHILE:
8979       {
8980         tree condition;
8981
8982         /* Begin the while-statement.  */
8983         statement = begin_while_stmt ();
8984         /* Look for the `('.  */
8985         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8986         /* Parse the condition.  */
8987         condition = cp_parser_condition (parser);
8988         finish_while_stmt_cond (condition, statement);
8989         /* Look for the `)'.  */
8990         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8991         /* Parse the dependent statement.  */
8992         parser->in_statement = IN_ITERATION_STMT;
8993         cp_parser_already_scoped_statement (parser);
8994         parser->in_statement = in_statement;
8995         /* We're done with the while-statement.  */
8996         finish_while_stmt (statement);
8997       }
8998       break;
8999
9000     case RID_DO:
9001       {
9002         tree expression;
9003
9004         /* Begin the do-statement.  */
9005         statement = begin_do_stmt ();
9006         /* Parse the body of the do-statement.  */
9007         parser->in_statement = IN_ITERATION_STMT;
9008         cp_parser_implicitly_scoped_statement (parser, NULL);
9009         parser->in_statement = in_statement;
9010         finish_do_body (statement);
9011         /* Look for the `while' keyword.  */
9012         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9013         /* Look for the `('.  */
9014         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9015         /* Parse the expression.  */
9016         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9017         /* We're done with the do-statement.  */
9018         finish_do_stmt (expression, statement);
9019         /* Look for the `)'.  */
9020         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9021         /* Look for the `;'.  */
9022         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9023       }
9024       break;
9025
9026     case RID_FOR:
9027       {
9028         /* Look for the `('.  */
9029         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9030
9031         statement = cp_parser_for (parser);
9032
9033         /* Look for the `)'.  */
9034         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9035
9036         /* Parse the body of the for-statement.  */
9037         parser->in_statement = IN_ITERATION_STMT;
9038         cp_parser_already_scoped_statement (parser);
9039         parser->in_statement = in_statement;
9040
9041         /* We're done with the for-statement.  */
9042         finish_for_stmt (statement);
9043       }
9044       break;
9045
9046     default:
9047       cp_parser_error (parser, "expected iteration-statement");
9048       statement = error_mark_node;
9049       break;
9050     }
9051
9052   return statement;
9053 }
9054
9055 /* Parse a for-init-statement or the declarator of a range-based-for.
9056    Returns true if a range-based-for declaration is seen.
9057
9058    for-init-statement:
9059      expression-statement
9060      simple-declaration  */
9061
9062 static bool
9063 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9064 {
9065   /* If the next token is a `;', then we have an empty
9066      expression-statement.  Grammatically, this is also a
9067      simple-declaration, but an invalid one, because it does not
9068      declare anything.  Therefore, if we did not handle this case
9069      specially, we would issue an error message about an invalid
9070      declaration.  */
9071   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9072     {
9073       bool is_range_for = false;
9074       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9075
9076       parser->colon_corrects_to_scope_p = false;
9077
9078       /* We're going to speculatively look for a declaration, falling back
9079          to an expression, if necessary.  */
9080       cp_parser_parse_tentatively (parser);
9081       /* Parse the declaration.  */
9082       cp_parser_simple_declaration (parser,
9083                                     /*function_definition_allowed_p=*/false,
9084                                     decl);
9085       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9086       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9087         {
9088           /* It is a range-for, consume the ':' */
9089           cp_lexer_consume_token (parser->lexer);
9090           is_range_for = true;
9091           if (cxx_dialect < cxx0x)
9092             {
9093               error_at (cp_lexer_peek_token (parser->lexer)->location,
9094                         "range-based-for loops are not allowed "
9095                         "in C++98 mode");
9096               *decl = error_mark_node;
9097             }
9098         }
9099       else
9100           /* The ';' is not consumed yet because we told
9101              cp_parser_simple_declaration not to.  */
9102           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9103
9104       if (cp_parser_parse_definitely (parser))
9105         return is_range_for;
9106       /* If the tentative parse failed, then we shall need to look for an
9107          expression-statement.  */
9108     }
9109   /* If we are here, it is an expression-statement.  */
9110   cp_parser_expression_statement (parser, NULL_TREE);
9111   return false;
9112 }
9113
9114 /* Parse a jump-statement.
9115
9116    jump-statement:
9117      break ;
9118      continue ;
9119      return expression [opt] ;
9120      return braced-init-list ;
9121      goto identifier ;
9122
9123    GNU extension:
9124
9125    jump-statement:
9126      goto * expression ;
9127
9128    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9129
9130 static tree
9131 cp_parser_jump_statement (cp_parser* parser)
9132 {
9133   tree statement = error_mark_node;
9134   cp_token *token;
9135   enum rid keyword;
9136   unsigned char in_statement;
9137
9138   /* Peek at the next token.  */
9139   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9140   if (!token)
9141     return error_mark_node;
9142
9143   /* See what kind of keyword it is.  */
9144   keyword = token->keyword;
9145   switch (keyword)
9146     {
9147     case RID_BREAK:
9148       in_statement = parser->in_statement & ~IN_IF_STMT;      
9149       switch (in_statement)
9150         {
9151         case 0:
9152           error_at (token->location, "break statement not within loop or switch");
9153           break;
9154         default:
9155           gcc_assert ((in_statement & IN_SWITCH_STMT)
9156                       || in_statement == IN_ITERATION_STMT);
9157           statement = finish_break_stmt ();
9158           break;
9159         case IN_OMP_BLOCK:
9160           error_at (token->location, "invalid exit from OpenMP structured block");
9161           break;
9162         case IN_OMP_FOR:
9163           error_at (token->location, "break statement used with OpenMP for loop");
9164           break;
9165         }
9166       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9167       break;
9168
9169     case RID_CONTINUE:
9170       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9171         {
9172         case 0:
9173           error_at (token->location, "continue statement not within a loop");
9174           break;
9175         case IN_ITERATION_STMT:
9176         case IN_OMP_FOR:
9177           statement = finish_continue_stmt ();
9178           break;
9179         case IN_OMP_BLOCK:
9180           error_at (token->location, "invalid exit from OpenMP structured block");
9181           break;
9182         default:
9183           gcc_unreachable ();
9184         }
9185       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9186       break;
9187
9188     case RID_RETURN:
9189       {
9190         tree expr;
9191         bool expr_non_constant_p;
9192
9193         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9194           {
9195             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9196             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9197           }
9198         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9199           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9200         else
9201           /* If the next token is a `;', then there is no
9202              expression.  */
9203           expr = NULL_TREE;
9204         /* Build the return-statement.  */
9205         statement = finish_return_stmt (expr);
9206         /* Look for the final `;'.  */
9207         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9208       }
9209       break;
9210
9211     case RID_GOTO:
9212       /* Create the goto-statement.  */
9213       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9214         {
9215           /* Issue a warning about this use of a GNU extension.  */
9216           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9217           /* Consume the '*' token.  */
9218           cp_lexer_consume_token (parser->lexer);
9219           /* Parse the dependent expression.  */
9220           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9221         }
9222       else
9223         finish_goto_stmt (cp_parser_identifier (parser));
9224       /* Look for the final `;'.  */
9225       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9226       break;
9227
9228     default:
9229       cp_parser_error (parser, "expected jump-statement");
9230       break;
9231     }
9232
9233   return statement;
9234 }
9235
9236 /* Parse a declaration-statement.
9237
9238    declaration-statement:
9239      block-declaration  */
9240
9241 static void
9242 cp_parser_declaration_statement (cp_parser* parser)
9243 {
9244   void *p;
9245
9246   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9247   p = obstack_alloc (&declarator_obstack, 0);
9248
9249  /* Parse the block-declaration.  */
9250   cp_parser_block_declaration (parser, /*statement_p=*/true);
9251
9252   /* Free any declarators allocated.  */
9253   obstack_free (&declarator_obstack, p);
9254
9255   /* Finish off the statement.  */
9256   finish_stmt ();
9257 }
9258
9259 /* Some dependent statements (like `if (cond) statement'), are
9260    implicitly in their own scope.  In other words, if the statement is
9261    a single statement (as opposed to a compound-statement), it is
9262    none-the-less treated as if it were enclosed in braces.  Any
9263    declarations appearing in the dependent statement are out of scope
9264    after control passes that point.  This function parses a statement,
9265    but ensures that is in its own scope, even if it is not a
9266    compound-statement.
9267
9268    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9269    is a (possibly labeled) if statement which is not enclosed in
9270    braces and has an else clause.  This is used to implement
9271    -Wparentheses.
9272
9273    Returns the new statement.  */
9274
9275 static tree
9276 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9277 {
9278   tree statement;
9279
9280   if (if_p != NULL)
9281     *if_p = false;
9282
9283   /* Mark if () ; with a special NOP_EXPR.  */
9284   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9285     {
9286       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9287       cp_lexer_consume_token (parser->lexer);
9288       statement = add_stmt (build_empty_stmt (loc));
9289     }
9290   /* if a compound is opened, we simply parse the statement directly.  */
9291   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9292     statement = cp_parser_compound_statement (parser, NULL, false);
9293   /* If the token is not a `{', then we must take special action.  */
9294   else
9295     {
9296       /* Create a compound-statement.  */
9297       statement = begin_compound_stmt (0);
9298       /* Parse the dependent-statement.  */
9299       cp_parser_statement (parser, NULL_TREE, false, if_p);
9300       /* Finish the dummy compound-statement.  */
9301       finish_compound_stmt (statement);
9302     }
9303
9304   /* Return the statement.  */
9305   return statement;
9306 }
9307
9308 /* For some dependent statements (like `while (cond) statement'), we
9309    have already created a scope.  Therefore, even if the dependent
9310    statement is a compound-statement, we do not want to create another
9311    scope.  */
9312
9313 static void
9314 cp_parser_already_scoped_statement (cp_parser* parser)
9315 {
9316   /* If the token is a `{', then we must take special action.  */
9317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9318     cp_parser_statement (parser, NULL_TREE, false, NULL);
9319   else
9320     {
9321       /* Avoid calling cp_parser_compound_statement, so that we
9322          don't create a new scope.  Do everything else by hand.  */
9323       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9324       /* If the next keyword is `__label__' we have a label declaration.  */
9325       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9326         cp_parser_label_declaration (parser);
9327       /* Parse an (optional) statement-seq.  */
9328       cp_parser_statement_seq_opt (parser, NULL_TREE);
9329       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9330     }
9331 }
9332
9333 /* Declarations [gram.dcl.dcl] */
9334
9335 /* Parse an optional declaration-sequence.
9336
9337    declaration-seq:
9338      declaration
9339      declaration-seq declaration  */
9340
9341 static void
9342 cp_parser_declaration_seq_opt (cp_parser* parser)
9343 {
9344   while (true)
9345     {
9346       cp_token *token;
9347
9348       token = cp_lexer_peek_token (parser->lexer);
9349
9350       if (token->type == CPP_CLOSE_BRACE
9351           || token->type == CPP_EOF
9352           || token->type == CPP_PRAGMA_EOL)
9353         break;
9354
9355       if (token->type == CPP_SEMICOLON)
9356         {
9357           /* A declaration consisting of a single semicolon is
9358              invalid.  Allow it unless we're being pedantic.  */
9359           cp_lexer_consume_token (parser->lexer);
9360           if (!in_system_header)
9361             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9362           continue;
9363         }
9364
9365       /* If we're entering or exiting a region that's implicitly
9366          extern "C", modify the lang context appropriately.  */
9367       if (!parser->implicit_extern_c && token->implicit_extern_c)
9368         {
9369           push_lang_context (lang_name_c);
9370           parser->implicit_extern_c = true;
9371         }
9372       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9373         {
9374           pop_lang_context ();
9375           parser->implicit_extern_c = false;
9376         }
9377
9378       if (token->type == CPP_PRAGMA)
9379         {
9380           /* A top-level declaration can consist solely of a #pragma.
9381              A nested declaration cannot, so this is done here and not
9382              in cp_parser_declaration.  (A #pragma at block scope is
9383              handled in cp_parser_statement.)  */
9384           cp_parser_pragma (parser, pragma_external);
9385           continue;
9386         }
9387
9388       /* Parse the declaration itself.  */
9389       cp_parser_declaration (parser);
9390     }
9391 }
9392
9393 /* Parse a declaration.
9394
9395    declaration:
9396      block-declaration
9397      function-definition
9398      template-declaration
9399      explicit-instantiation
9400      explicit-specialization
9401      linkage-specification
9402      namespace-definition
9403
9404    GNU extension:
9405
9406    declaration:
9407       __extension__ declaration */
9408
9409 static void
9410 cp_parser_declaration (cp_parser* parser)
9411 {
9412   cp_token token1;
9413   cp_token token2;
9414   int saved_pedantic;
9415   void *p;
9416   tree attributes = NULL_TREE;
9417
9418   /* Check for the `__extension__' keyword.  */
9419   if (cp_parser_extension_opt (parser, &saved_pedantic))
9420     {
9421       /* Parse the qualified declaration.  */
9422       cp_parser_declaration (parser);
9423       /* Restore the PEDANTIC flag.  */
9424       pedantic = saved_pedantic;
9425
9426       return;
9427     }
9428
9429   /* Try to figure out what kind of declaration is present.  */
9430   token1 = *cp_lexer_peek_token (parser->lexer);
9431
9432   if (token1.type != CPP_EOF)
9433     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9434   else
9435     {
9436       token2.type = CPP_EOF;
9437       token2.keyword = RID_MAX;
9438     }
9439
9440   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9441   p = obstack_alloc (&declarator_obstack, 0);
9442
9443   /* If the next token is `extern' and the following token is a string
9444      literal, then we have a linkage specification.  */
9445   if (token1.keyword == RID_EXTERN
9446       && cp_parser_is_string_literal (&token2))
9447     cp_parser_linkage_specification (parser);
9448   /* If the next token is `template', then we have either a template
9449      declaration, an explicit instantiation, or an explicit
9450      specialization.  */
9451   else if (token1.keyword == RID_TEMPLATE)
9452     {
9453       /* `template <>' indicates a template specialization.  */
9454       if (token2.type == CPP_LESS
9455           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9456         cp_parser_explicit_specialization (parser);
9457       /* `template <' indicates a template declaration.  */
9458       else if (token2.type == CPP_LESS)
9459         cp_parser_template_declaration (parser, /*member_p=*/false);
9460       /* Anything else must be an explicit instantiation.  */
9461       else
9462         cp_parser_explicit_instantiation (parser);
9463     }
9464   /* If the next token is `export', then we have a template
9465      declaration.  */
9466   else if (token1.keyword == RID_EXPORT)
9467     cp_parser_template_declaration (parser, /*member_p=*/false);
9468   /* If the next token is `extern', 'static' or 'inline' and the one
9469      after that is `template', we have a GNU extended explicit
9470      instantiation directive.  */
9471   else if (cp_parser_allow_gnu_extensions_p (parser)
9472            && (token1.keyword == RID_EXTERN
9473                || token1.keyword == RID_STATIC
9474                || token1.keyword == RID_INLINE)
9475            && token2.keyword == RID_TEMPLATE)
9476     cp_parser_explicit_instantiation (parser);
9477   /* If the next token is `namespace', check for a named or unnamed
9478      namespace definition.  */
9479   else if (token1.keyword == RID_NAMESPACE
9480            && (/* A named namespace definition.  */
9481                (token2.type == CPP_NAME
9482                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9483                     != CPP_EQ))
9484                /* An unnamed namespace definition.  */
9485                || token2.type == CPP_OPEN_BRACE
9486                || token2.keyword == RID_ATTRIBUTE))
9487     cp_parser_namespace_definition (parser);
9488   /* An inline (associated) namespace definition.  */
9489   else if (token1.keyword == RID_INLINE
9490            && token2.keyword == RID_NAMESPACE)
9491     cp_parser_namespace_definition (parser);
9492   /* Objective-C++ declaration/definition.  */
9493   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9494     cp_parser_objc_declaration (parser, NULL_TREE);
9495   else if (c_dialect_objc ()
9496            && token1.keyword == RID_ATTRIBUTE
9497            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9498     cp_parser_objc_declaration (parser, attributes);
9499   /* We must have either a block declaration or a function
9500      definition.  */
9501   else
9502     /* Try to parse a block-declaration, or a function-definition.  */
9503     cp_parser_block_declaration (parser, /*statement_p=*/false);
9504
9505   /* Free any declarators allocated.  */
9506   obstack_free (&declarator_obstack, p);
9507 }
9508
9509 /* Parse a block-declaration.
9510
9511    block-declaration:
9512      simple-declaration
9513      asm-definition
9514      namespace-alias-definition
9515      using-declaration
9516      using-directive
9517
9518    GNU Extension:
9519
9520    block-declaration:
9521      __extension__ block-declaration
9522
9523    C++0x Extension:
9524
9525    block-declaration:
9526      static_assert-declaration
9527
9528    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9529    part of a declaration-statement.  */
9530
9531 static void
9532 cp_parser_block_declaration (cp_parser *parser,
9533                              bool      statement_p)
9534 {
9535   cp_token *token1;
9536   int saved_pedantic;
9537
9538   /* Check for the `__extension__' keyword.  */
9539   if (cp_parser_extension_opt (parser, &saved_pedantic))
9540     {
9541       /* Parse the qualified declaration.  */
9542       cp_parser_block_declaration (parser, statement_p);
9543       /* Restore the PEDANTIC flag.  */
9544       pedantic = saved_pedantic;
9545
9546       return;
9547     }
9548
9549   /* Peek at the next token to figure out which kind of declaration is
9550      present.  */
9551   token1 = cp_lexer_peek_token (parser->lexer);
9552
9553   /* If the next keyword is `asm', we have an asm-definition.  */
9554   if (token1->keyword == RID_ASM)
9555     {
9556       if (statement_p)
9557         cp_parser_commit_to_tentative_parse (parser);
9558       cp_parser_asm_definition (parser);
9559     }
9560   /* If the next keyword is `namespace', we have a
9561      namespace-alias-definition.  */
9562   else if (token1->keyword == RID_NAMESPACE)
9563     cp_parser_namespace_alias_definition (parser);
9564   /* If the next keyword is `using', we have either a
9565      using-declaration or a using-directive.  */
9566   else if (token1->keyword == RID_USING)
9567     {
9568       cp_token *token2;
9569
9570       if (statement_p)
9571         cp_parser_commit_to_tentative_parse (parser);
9572       /* If the token after `using' is `namespace', then we have a
9573          using-directive.  */
9574       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9575       if (token2->keyword == RID_NAMESPACE)
9576         cp_parser_using_directive (parser);
9577       /* Otherwise, it's a using-declaration.  */
9578       else
9579         cp_parser_using_declaration (parser,
9580                                      /*access_declaration_p=*/false);
9581     }
9582   /* If the next keyword is `__label__' we have a misplaced label
9583      declaration.  */
9584   else if (token1->keyword == RID_LABEL)
9585     {
9586       cp_lexer_consume_token (parser->lexer);
9587       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9588       cp_parser_skip_to_end_of_statement (parser);
9589       /* If the next token is now a `;', consume it.  */
9590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9591         cp_lexer_consume_token (parser->lexer);
9592     }
9593   /* If the next token is `static_assert' we have a static assertion.  */
9594   else if (token1->keyword == RID_STATIC_ASSERT)
9595     cp_parser_static_assert (parser, /*member_p=*/false);
9596   /* Anything else must be a simple-declaration.  */
9597   else
9598     cp_parser_simple_declaration (parser, !statement_p,
9599                                   /*maybe_range_for_decl*/NULL);
9600 }
9601
9602 /* Parse a simple-declaration.
9603
9604    simple-declaration:
9605      decl-specifier-seq [opt] init-declarator-list [opt] ;
9606
9607    init-declarator-list:
9608      init-declarator
9609      init-declarator-list , init-declarator
9610
9611    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9612    function-definition as a simple-declaration.
9613
9614    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9615    parsed declaration if it is an uninitialized single declarator not followed
9616    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9617    if present, will not be consumed.  */
9618
9619 static void
9620 cp_parser_simple_declaration (cp_parser* parser,
9621                               bool function_definition_allowed_p,
9622                               tree *maybe_range_for_decl)
9623 {
9624   cp_decl_specifier_seq decl_specifiers;
9625   int declares_class_or_enum;
9626   bool saw_declarator;
9627
9628   if (maybe_range_for_decl)
9629     *maybe_range_for_decl = NULL_TREE;
9630
9631   /* Defer access checks until we know what is being declared; the
9632      checks for names appearing in the decl-specifier-seq should be
9633      done as if we were in the scope of the thing being declared.  */
9634   push_deferring_access_checks (dk_deferred);
9635
9636   /* Parse the decl-specifier-seq.  We have to keep track of whether
9637      or not the decl-specifier-seq declares a named class or
9638      enumeration type, since that is the only case in which the
9639      init-declarator-list is allowed to be empty.
9640
9641      [dcl.dcl]
9642
9643      In a simple-declaration, the optional init-declarator-list can be
9644      omitted only when declaring a class or enumeration, that is when
9645      the decl-specifier-seq contains either a class-specifier, an
9646      elaborated-type-specifier, or an enum-specifier.  */
9647   cp_parser_decl_specifier_seq (parser,
9648                                 CP_PARSER_FLAGS_OPTIONAL,
9649                                 &decl_specifiers,
9650                                 &declares_class_or_enum);
9651   /* We no longer need to defer access checks.  */
9652   stop_deferring_access_checks ();
9653
9654   /* In a block scope, a valid declaration must always have a
9655      decl-specifier-seq.  By not trying to parse declarators, we can
9656      resolve the declaration/expression ambiguity more quickly.  */
9657   if (!function_definition_allowed_p
9658       && !decl_specifiers.any_specifiers_p)
9659     {
9660       cp_parser_error (parser, "expected declaration");
9661       goto done;
9662     }
9663
9664   /* If the next two tokens are both identifiers, the code is
9665      erroneous. The usual cause of this situation is code like:
9666
9667        T t;
9668
9669      where "T" should name a type -- but does not.  */
9670   if (!decl_specifiers.any_type_specifiers_p
9671       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9672     {
9673       /* If parsing tentatively, we should commit; we really are
9674          looking at a declaration.  */
9675       cp_parser_commit_to_tentative_parse (parser);
9676       /* Give up.  */
9677       goto done;
9678     }
9679
9680   /* If we have seen at least one decl-specifier, and the next token
9681      is not a parenthesis, then we must be looking at a declaration.
9682      (After "int (" we might be looking at a functional cast.)  */
9683   if (decl_specifiers.any_specifiers_p
9684       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9685       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9686       && !cp_parser_error_occurred (parser))
9687     cp_parser_commit_to_tentative_parse (parser);
9688
9689   /* Keep going until we hit the `;' at the end of the simple
9690      declaration.  */
9691   saw_declarator = false;
9692   while (cp_lexer_next_token_is_not (parser->lexer,
9693                                      CPP_SEMICOLON))
9694     {
9695       cp_token *token;
9696       bool function_definition_p;
9697       tree decl;
9698
9699       if (saw_declarator)
9700         {
9701           /* If we are processing next declarator, coma is expected */
9702           token = cp_lexer_peek_token (parser->lexer);
9703           gcc_assert (token->type == CPP_COMMA);
9704           cp_lexer_consume_token (parser->lexer);
9705           if (maybe_range_for_decl)
9706             *maybe_range_for_decl = error_mark_node;
9707         }
9708       else
9709         saw_declarator = true;
9710
9711       /* Parse the init-declarator.  */
9712       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9713                                         /*checks=*/NULL,
9714                                         function_definition_allowed_p,
9715                                         /*member_p=*/false,
9716                                         declares_class_or_enum,
9717                                         &function_definition_p,
9718                                         maybe_range_for_decl);
9719       /* If an error occurred while parsing tentatively, exit quickly.
9720          (That usually happens when in the body of a function; each
9721          statement is treated as a declaration-statement until proven
9722          otherwise.)  */
9723       if (cp_parser_error_occurred (parser))
9724         goto done;
9725       /* Handle function definitions specially.  */
9726       if (function_definition_p)
9727         {
9728           /* If the next token is a `,', then we are probably
9729              processing something like:
9730
9731                void f() {}, *p;
9732
9733              which is erroneous.  */
9734           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9735             {
9736               cp_token *token = cp_lexer_peek_token (parser->lexer);
9737               error_at (token->location,
9738                         "mixing"
9739                         " declarations and function-definitions is forbidden");
9740             }
9741           /* Otherwise, we're done with the list of declarators.  */
9742           else
9743             {
9744               pop_deferring_access_checks ();
9745               return;
9746             }
9747         }
9748       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9749         *maybe_range_for_decl = decl;
9750       /* The next token should be either a `,' or a `;'.  */
9751       token = cp_lexer_peek_token (parser->lexer);
9752       /* If it's a `,', there are more declarators to come.  */
9753       if (token->type == CPP_COMMA)
9754         /* will be consumed next time around */;
9755       /* If it's a `;', we are done.  */
9756       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9757         break;
9758       /* Anything else is an error.  */
9759       else
9760         {
9761           /* If we have already issued an error message we don't need
9762              to issue another one.  */
9763           if (decl != error_mark_node
9764               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9765             cp_parser_error (parser, "expected %<,%> or %<;%>");
9766           /* Skip tokens until we reach the end of the statement.  */
9767           cp_parser_skip_to_end_of_statement (parser);
9768           /* If the next token is now a `;', consume it.  */
9769           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9770             cp_lexer_consume_token (parser->lexer);
9771           goto done;
9772         }
9773       /* After the first time around, a function-definition is not
9774          allowed -- even if it was OK at first.  For example:
9775
9776            int i, f() {}
9777
9778          is not valid.  */
9779       function_definition_allowed_p = false;
9780     }
9781
9782   /* Issue an error message if no declarators are present, and the
9783      decl-specifier-seq does not itself declare a class or
9784      enumeration.  */
9785   if (!saw_declarator)
9786     {
9787       if (cp_parser_declares_only_class_p (parser))
9788         shadow_tag (&decl_specifiers);
9789       /* Perform any deferred access checks.  */
9790       perform_deferred_access_checks ();
9791     }
9792
9793   /* Consume the `;'.  */
9794   if (!maybe_range_for_decl)
9795       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9796
9797  done:
9798   pop_deferring_access_checks ();
9799 }
9800
9801 /* Parse a decl-specifier-seq.
9802
9803    decl-specifier-seq:
9804      decl-specifier-seq [opt] decl-specifier
9805
9806    decl-specifier:
9807      storage-class-specifier
9808      type-specifier
9809      function-specifier
9810      friend
9811      typedef
9812
9813    GNU Extension:
9814
9815    decl-specifier:
9816      attributes
9817
9818    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9819
9820    The parser flags FLAGS is used to control type-specifier parsing.
9821
9822    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9823    flags:
9824
9825      1: one of the decl-specifiers is an elaborated-type-specifier
9826         (i.e., a type declaration)
9827      2: one of the decl-specifiers is an enum-specifier or a
9828         class-specifier (i.e., a type definition)
9829
9830    */
9831
9832 static void
9833 cp_parser_decl_specifier_seq (cp_parser* parser,
9834                               cp_parser_flags flags,
9835                               cp_decl_specifier_seq *decl_specs,
9836                               int* declares_class_or_enum)
9837 {
9838   bool constructor_possible_p = !parser->in_declarator_p;
9839   cp_token *start_token = NULL;
9840
9841   /* Clear DECL_SPECS.  */
9842   clear_decl_specs (decl_specs);
9843
9844   /* Assume no class or enumeration type is declared.  */
9845   *declares_class_or_enum = 0;
9846
9847   /* Keep reading specifiers until there are no more to read.  */
9848   while (true)
9849     {
9850       bool constructor_p;
9851       bool found_decl_spec;
9852       cp_token *token;
9853
9854       /* Peek at the next token.  */
9855       token = cp_lexer_peek_token (parser->lexer);
9856
9857       /* Save the first token of the decl spec list for error
9858          reporting.  */
9859       if (!start_token)
9860         start_token = token;
9861       /* Handle attributes.  */
9862       if (token->keyword == RID_ATTRIBUTE)
9863         {
9864           /* Parse the attributes.  */
9865           decl_specs->attributes
9866             = chainon (decl_specs->attributes,
9867                        cp_parser_attributes_opt (parser));
9868           continue;
9869         }
9870       /* Assume we will find a decl-specifier keyword.  */
9871       found_decl_spec = true;
9872       /* If the next token is an appropriate keyword, we can simply
9873          add it to the list.  */
9874       switch (token->keyword)
9875         {
9876           /* decl-specifier:
9877                friend
9878                constexpr */
9879         case RID_FRIEND:
9880           if (!at_class_scope_p ())
9881             {
9882               error_at (token->location, "%<friend%> used outside of class");
9883               cp_lexer_purge_token (parser->lexer);
9884             }
9885           else
9886             {
9887               ++decl_specs->specs[(int) ds_friend];
9888               /* Consume the token.  */
9889               cp_lexer_consume_token (parser->lexer);
9890             }
9891           break;
9892
9893         case RID_CONSTEXPR:
9894           ++decl_specs->specs[(int) ds_constexpr];
9895           cp_lexer_consume_token (parser->lexer);
9896           break;
9897
9898           /* function-specifier:
9899                inline
9900                virtual
9901                explicit  */
9902         case RID_INLINE:
9903         case RID_VIRTUAL:
9904         case RID_EXPLICIT:
9905           cp_parser_function_specifier_opt (parser, decl_specs);
9906           break;
9907
9908           /* decl-specifier:
9909                typedef  */
9910         case RID_TYPEDEF:
9911           ++decl_specs->specs[(int) ds_typedef];
9912           /* Consume the token.  */
9913           cp_lexer_consume_token (parser->lexer);
9914           /* A constructor declarator cannot appear in a typedef.  */
9915           constructor_possible_p = false;
9916           /* The "typedef" keyword can only occur in a declaration; we
9917              may as well commit at this point.  */
9918           cp_parser_commit_to_tentative_parse (parser);
9919
9920           if (decl_specs->storage_class != sc_none)
9921             decl_specs->conflicting_specifiers_p = true;
9922           break;
9923
9924           /* storage-class-specifier:
9925                auto
9926                register
9927                static
9928                extern
9929                mutable
9930
9931              GNU Extension:
9932                thread  */
9933         case RID_AUTO:
9934           if (cxx_dialect == cxx98) 
9935             {
9936               /* Consume the token.  */
9937               cp_lexer_consume_token (parser->lexer);
9938
9939               /* Complain about `auto' as a storage specifier, if
9940                  we're complaining about C++0x compatibility.  */
9941               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9942                           " will change meaning in C++0x; please remove it");
9943
9944               /* Set the storage class anyway.  */
9945               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9946                                            token->location);
9947             }
9948           else
9949             /* C++0x auto type-specifier.  */
9950             found_decl_spec = false;
9951           break;
9952
9953         case RID_REGISTER:
9954         case RID_STATIC:
9955         case RID_EXTERN:
9956         case RID_MUTABLE:
9957           /* Consume the token.  */
9958           cp_lexer_consume_token (parser->lexer);
9959           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9960                                        token->location);
9961           break;
9962         case RID_THREAD:
9963           /* Consume the token.  */
9964           cp_lexer_consume_token (parser->lexer);
9965           ++decl_specs->specs[(int) ds_thread];
9966           break;
9967
9968         default:
9969           /* We did not yet find a decl-specifier yet.  */
9970           found_decl_spec = false;
9971           break;
9972         }
9973
9974       if (found_decl_spec
9975           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9976           && token->keyword != RID_CONSTEXPR)
9977         error ("decl-specifier invalid in condition");
9978
9979       /* Constructors are a special case.  The `S' in `S()' is not a
9980          decl-specifier; it is the beginning of the declarator.  */
9981       constructor_p
9982         = (!found_decl_spec
9983            && constructor_possible_p
9984            && (cp_parser_constructor_declarator_p
9985                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9986
9987       /* If we don't have a DECL_SPEC yet, then we must be looking at
9988          a type-specifier.  */
9989       if (!found_decl_spec && !constructor_p)
9990         {
9991           int decl_spec_declares_class_or_enum;
9992           bool is_cv_qualifier;
9993           tree type_spec;
9994
9995           type_spec
9996             = cp_parser_type_specifier (parser, flags,
9997                                         decl_specs,
9998                                         /*is_declaration=*/true,
9999                                         &decl_spec_declares_class_or_enum,
10000                                         &is_cv_qualifier);
10001           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10002
10003           /* If this type-specifier referenced a user-defined type
10004              (a typedef, class-name, etc.), then we can't allow any
10005              more such type-specifiers henceforth.
10006
10007              [dcl.spec]
10008
10009              The longest sequence of decl-specifiers that could
10010              possibly be a type name is taken as the
10011              decl-specifier-seq of a declaration.  The sequence shall
10012              be self-consistent as described below.
10013
10014              [dcl.type]
10015
10016              As a general rule, at most one type-specifier is allowed
10017              in the complete decl-specifier-seq of a declaration.  The
10018              only exceptions are the following:
10019
10020              -- const or volatile can be combined with any other
10021                 type-specifier.
10022
10023              -- signed or unsigned can be combined with char, long,
10024                 short, or int.
10025
10026              -- ..
10027
10028              Example:
10029
10030                typedef char* Pc;
10031                void g (const int Pc);
10032
10033              Here, Pc is *not* part of the decl-specifier seq; it's
10034              the declarator.  Therefore, once we see a type-specifier
10035              (other than a cv-qualifier), we forbid any additional
10036              user-defined types.  We *do* still allow things like `int
10037              int' to be considered a decl-specifier-seq, and issue the
10038              error message later.  */
10039           if (type_spec && !is_cv_qualifier)
10040             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10041           /* A constructor declarator cannot follow a type-specifier.  */
10042           if (type_spec)
10043             {
10044               constructor_possible_p = false;
10045               found_decl_spec = true;
10046               if (!is_cv_qualifier)
10047                 decl_specs->any_type_specifiers_p = true;
10048             }
10049         }
10050
10051       /* If we still do not have a DECL_SPEC, then there are no more
10052          decl-specifiers.  */
10053       if (!found_decl_spec)
10054         break;
10055
10056       decl_specs->any_specifiers_p = true;
10057       /* After we see one decl-specifier, further decl-specifiers are
10058          always optional.  */
10059       flags |= CP_PARSER_FLAGS_OPTIONAL;
10060     }
10061
10062   cp_parser_check_decl_spec (decl_specs, start_token->location);
10063
10064   /* Don't allow a friend specifier with a class definition.  */
10065   if (decl_specs->specs[(int) ds_friend] != 0
10066       && (*declares_class_or_enum & 2))
10067     error_at (start_token->location,
10068               "class definition may not be declared a friend");
10069 }
10070
10071 /* Parse an (optional) storage-class-specifier.
10072
10073    storage-class-specifier:
10074      auto
10075      register
10076      static
10077      extern
10078      mutable
10079
10080    GNU Extension:
10081
10082    storage-class-specifier:
10083      thread
10084
10085    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10086
10087 static tree
10088 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10089 {
10090   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10091     {
10092     case RID_AUTO:
10093       if (cxx_dialect != cxx98)
10094         return NULL_TREE;
10095       /* Fall through for C++98.  */
10096
10097     case RID_REGISTER:
10098     case RID_STATIC:
10099     case RID_EXTERN:
10100     case RID_MUTABLE:
10101     case RID_THREAD:
10102       /* Consume the token.  */
10103       return cp_lexer_consume_token (parser->lexer)->u.value;
10104
10105     default:
10106       return NULL_TREE;
10107     }
10108 }
10109
10110 /* Parse an (optional) function-specifier.
10111
10112    function-specifier:
10113      inline
10114      virtual
10115      explicit
10116
10117    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10118    Updates DECL_SPECS, if it is non-NULL.  */
10119
10120 static tree
10121 cp_parser_function_specifier_opt (cp_parser* parser,
10122                                   cp_decl_specifier_seq *decl_specs)
10123 {
10124   cp_token *token = cp_lexer_peek_token (parser->lexer);
10125   switch (token->keyword)
10126     {
10127     case RID_INLINE:
10128       if (decl_specs)
10129         ++decl_specs->specs[(int) ds_inline];
10130       break;
10131
10132     case RID_VIRTUAL:
10133       /* 14.5.2.3 [temp.mem]
10134
10135          A member function template shall not be virtual.  */
10136       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10137         error_at (token->location, "templates may not be %<virtual%>");
10138       else if (decl_specs)
10139         ++decl_specs->specs[(int) ds_virtual];
10140       break;
10141
10142     case RID_EXPLICIT:
10143       if (decl_specs)
10144         ++decl_specs->specs[(int) ds_explicit];
10145       break;
10146
10147     default:
10148       return NULL_TREE;
10149     }
10150
10151   /* Consume the token.  */
10152   return cp_lexer_consume_token (parser->lexer)->u.value;
10153 }
10154
10155 /* Parse a linkage-specification.
10156
10157    linkage-specification:
10158      extern string-literal { declaration-seq [opt] }
10159      extern string-literal declaration  */
10160
10161 static void
10162 cp_parser_linkage_specification (cp_parser* parser)
10163 {
10164   tree linkage;
10165
10166   /* Look for the `extern' keyword.  */
10167   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10168
10169   /* Look for the string-literal.  */
10170   linkage = cp_parser_string_literal (parser, false, false);
10171
10172   /* Transform the literal into an identifier.  If the literal is a
10173      wide-character string, or contains embedded NULs, then we can't
10174      handle it as the user wants.  */
10175   if (strlen (TREE_STRING_POINTER (linkage))
10176       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10177     {
10178       cp_parser_error (parser, "invalid linkage-specification");
10179       /* Assume C++ linkage.  */
10180       linkage = lang_name_cplusplus;
10181     }
10182   else
10183     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10184
10185   /* We're now using the new linkage.  */
10186   push_lang_context (linkage);
10187
10188   /* If the next token is a `{', then we're using the first
10189      production.  */
10190   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10191     {
10192       /* Consume the `{' token.  */
10193       cp_lexer_consume_token (parser->lexer);
10194       /* Parse the declarations.  */
10195       cp_parser_declaration_seq_opt (parser);
10196       /* Look for the closing `}'.  */
10197       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10198     }
10199   /* Otherwise, there's just one declaration.  */
10200   else
10201     {
10202       bool saved_in_unbraced_linkage_specification_p;
10203
10204       saved_in_unbraced_linkage_specification_p
10205         = parser->in_unbraced_linkage_specification_p;
10206       parser->in_unbraced_linkage_specification_p = true;
10207       cp_parser_declaration (parser);
10208       parser->in_unbraced_linkage_specification_p
10209         = saved_in_unbraced_linkage_specification_p;
10210     }
10211
10212   /* We're done with the linkage-specification.  */
10213   pop_lang_context ();
10214 }
10215
10216 /* Parse a static_assert-declaration.
10217
10218    static_assert-declaration:
10219      static_assert ( constant-expression , string-literal ) ; 
10220
10221    If MEMBER_P, this static_assert is a class member.  */
10222
10223 static void 
10224 cp_parser_static_assert(cp_parser *parser, bool member_p)
10225 {
10226   tree condition;
10227   tree message;
10228   cp_token *token;
10229   location_t saved_loc;
10230   bool dummy;
10231
10232   /* Peek at the `static_assert' token so we can keep track of exactly
10233      where the static assertion started.  */
10234   token = cp_lexer_peek_token (parser->lexer);
10235   saved_loc = token->location;
10236
10237   /* Look for the `static_assert' keyword.  */
10238   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10239                                   RT_STATIC_ASSERT))
10240     return;
10241
10242   /*  We know we are in a static assertion; commit to any tentative
10243       parse.  */
10244   if (cp_parser_parsing_tentatively (parser))
10245     cp_parser_commit_to_tentative_parse (parser);
10246
10247   /* Parse the `(' starting the static assertion condition.  */
10248   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10249
10250   /* Parse the constant-expression.  Allow a non-constant expression
10251      here in order to give better diagnostics in finish_static_assert.  */
10252   condition = 
10253     cp_parser_constant_expression (parser,
10254                                    /*allow_non_constant_p=*/true,
10255                                    /*non_constant_p=*/&dummy);
10256
10257   /* Parse the separating `,'.  */
10258   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10259
10260   /* Parse the string-literal message.  */
10261   message = cp_parser_string_literal (parser, 
10262                                       /*translate=*/false,
10263                                       /*wide_ok=*/true);
10264
10265   /* A `)' completes the static assertion.  */
10266   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10267     cp_parser_skip_to_closing_parenthesis (parser, 
10268                                            /*recovering=*/true, 
10269                                            /*or_comma=*/false,
10270                                            /*consume_paren=*/true);
10271
10272   /* A semicolon terminates the declaration.  */
10273   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10274
10275   /* Complete the static assertion, which may mean either processing 
10276      the static assert now or saving it for template instantiation.  */
10277   finish_static_assert (condition, message, saved_loc, member_p);
10278 }
10279
10280 /* Parse a `decltype' type. Returns the type. 
10281
10282    simple-type-specifier:
10283      decltype ( expression )  */
10284
10285 static tree
10286 cp_parser_decltype (cp_parser *parser)
10287 {
10288   tree expr;
10289   bool id_expression_or_member_access_p = false;
10290   const char *saved_message;
10291   bool saved_integral_constant_expression_p;
10292   bool saved_non_integral_constant_expression_p;
10293   cp_token *id_expr_start_token;
10294
10295   /* Look for the `decltype' token.  */
10296   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10297     return error_mark_node;
10298
10299   /* Types cannot be defined in a `decltype' expression.  Save away the
10300      old message.  */
10301   saved_message = parser->type_definition_forbidden_message;
10302
10303   /* And create the new one.  */
10304   parser->type_definition_forbidden_message
10305     = G_("types may not be defined in %<decltype%> expressions");
10306
10307   /* The restrictions on constant-expressions do not apply inside
10308      decltype expressions.  */
10309   saved_integral_constant_expression_p
10310     = parser->integral_constant_expression_p;
10311   saved_non_integral_constant_expression_p
10312     = parser->non_integral_constant_expression_p;
10313   parser->integral_constant_expression_p = false;
10314
10315   /* Do not actually evaluate the expression.  */
10316   ++cp_unevaluated_operand;
10317
10318   /* Do not warn about problems with the expression.  */
10319   ++c_inhibit_evaluation_warnings;
10320
10321   /* Parse the opening `('.  */
10322   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10323     return error_mark_node;
10324   
10325   /* First, try parsing an id-expression.  */
10326   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10327   cp_parser_parse_tentatively (parser);
10328   expr = cp_parser_id_expression (parser,
10329                                   /*template_keyword_p=*/false,
10330                                   /*check_dependency_p=*/true,
10331                                   /*template_p=*/NULL,
10332                                   /*declarator_p=*/false,
10333                                   /*optional_p=*/false);
10334
10335   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10336     {
10337       bool non_integral_constant_expression_p = false;
10338       tree id_expression = expr;
10339       cp_id_kind idk;
10340       const char *error_msg;
10341
10342       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10343         /* Lookup the name we got back from the id-expression.  */
10344         expr = cp_parser_lookup_name (parser, expr,
10345                                       none_type,
10346                                       /*is_template=*/false,
10347                                       /*is_namespace=*/false,
10348                                       /*check_dependency=*/true,
10349                                       /*ambiguous_decls=*/NULL,
10350                                       id_expr_start_token->location);
10351
10352       if (expr
10353           && expr != error_mark_node
10354           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10355           && TREE_CODE (expr) != TYPE_DECL
10356           && (TREE_CODE (expr) != BIT_NOT_EXPR
10357               || !TYPE_P (TREE_OPERAND (expr, 0)))
10358           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10359         {
10360           /* Complete lookup of the id-expression.  */
10361           expr = (finish_id_expression
10362                   (id_expression, expr, parser->scope, &idk,
10363                    /*integral_constant_expression_p=*/false,
10364                    /*allow_non_integral_constant_expression_p=*/true,
10365                    &non_integral_constant_expression_p,
10366                    /*template_p=*/false,
10367                    /*done=*/true,
10368                    /*address_p=*/false,
10369                    /*template_arg_p=*/false,
10370                    &error_msg,
10371                    id_expr_start_token->location));
10372
10373           if (expr == error_mark_node)
10374             /* We found an id-expression, but it was something that we
10375                should not have found. This is an error, not something
10376                we can recover from, so note that we found an
10377                id-expression and we'll recover as gracefully as
10378                possible.  */
10379             id_expression_or_member_access_p = true;
10380         }
10381
10382       if (expr 
10383           && expr != error_mark_node
10384           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10385         /* We have an id-expression.  */
10386         id_expression_or_member_access_p = true;
10387     }
10388
10389   if (!id_expression_or_member_access_p)
10390     {
10391       /* Abort the id-expression parse.  */
10392       cp_parser_abort_tentative_parse (parser);
10393
10394       /* Parsing tentatively, again.  */
10395       cp_parser_parse_tentatively (parser);
10396
10397       /* Parse a class member access.  */
10398       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10399                                            /*cast_p=*/false,
10400                                            /*member_access_only_p=*/true, NULL);
10401
10402       if (expr 
10403           && expr != error_mark_node
10404           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10405         /* We have an id-expression.  */
10406         id_expression_or_member_access_p = true;
10407     }
10408
10409   if (id_expression_or_member_access_p)
10410     /* We have parsed the complete id-expression or member access.  */
10411     cp_parser_parse_definitely (parser);
10412   else
10413     {
10414       bool saved_greater_than_is_operator_p;
10415
10416       /* Abort our attempt to parse an id-expression or member access
10417          expression.  */
10418       cp_parser_abort_tentative_parse (parser);
10419
10420       /* Within a parenthesized expression, a `>' token is always
10421          the greater-than operator.  */
10422       saved_greater_than_is_operator_p
10423         = parser->greater_than_is_operator_p;
10424       parser->greater_than_is_operator_p = true;
10425
10426       /* Parse a full expression.  */
10427       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10428
10429       /* The `>' token might be the end of a template-id or
10430          template-parameter-list now.  */
10431       parser->greater_than_is_operator_p
10432         = saved_greater_than_is_operator_p;
10433     }
10434
10435   /* Go back to evaluating expressions.  */
10436   --cp_unevaluated_operand;
10437   --c_inhibit_evaluation_warnings;
10438
10439   /* Restore the old message and the integral constant expression
10440      flags.  */
10441   parser->type_definition_forbidden_message = saved_message;
10442   parser->integral_constant_expression_p
10443     = saved_integral_constant_expression_p;
10444   parser->non_integral_constant_expression_p
10445     = saved_non_integral_constant_expression_p;
10446
10447   if (expr == error_mark_node)
10448     {
10449       /* Skip everything up to the closing `)'.  */
10450       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10451                                              /*consume_paren=*/true);
10452       return error_mark_node;
10453     }
10454   
10455   /* Parse to the closing `)'.  */
10456   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10457     {
10458       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10459                                              /*consume_paren=*/true);
10460       return error_mark_node;
10461     }
10462
10463   return finish_decltype_type (expr, id_expression_or_member_access_p);
10464 }
10465
10466 /* Special member functions [gram.special] */
10467
10468 /* Parse a conversion-function-id.
10469
10470    conversion-function-id:
10471      operator conversion-type-id
10472
10473    Returns an IDENTIFIER_NODE representing the operator.  */
10474
10475 static tree
10476 cp_parser_conversion_function_id (cp_parser* parser)
10477 {
10478   tree type;
10479   tree saved_scope;
10480   tree saved_qualifying_scope;
10481   tree saved_object_scope;
10482   tree pushed_scope = NULL_TREE;
10483
10484   /* Look for the `operator' token.  */
10485   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10486     return error_mark_node;
10487   /* When we parse the conversion-type-id, the current scope will be
10488      reset.  However, we need that information in able to look up the
10489      conversion function later, so we save it here.  */
10490   saved_scope = parser->scope;
10491   saved_qualifying_scope = parser->qualifying_scope;
10492   saved_object_scope = parser->object_scope;
10493   /* We must enter the scope of the class so that the names of
10494      entities declared within the class are available in the
10495      conversion-type-id.  For example, consider:
10496
10497        struct S {
10498          typedef int I;
10499          operator I();
10500        };
10501
10502        S::operator I() { ... }
10503
10504      In order to see that `I' is a type-name in the definition, we
10505      must be in the scope of `S'.  */
10506   if (saved_scope)
10507     pushed_scope = push_scope (saved_scope);
10508   /* Parse the conversion-type-id.  */
10509   type = cp_parser_conversion_type_id (parser);
10510   /* Leave the scope of the class, if any.  */
10511   if (pushed_scope)
10512     pop_scope (pushed_scope);
10513   /* Restore the saved scope.  */
10514   parser->scope = saved_scope;
10515   parser->qualifying_scope = saved_qualifying_scope;
10516   parser->object_scope = saved_object_scope;
10517   /* If the TYPE is invalid, indicate failure.  */
10518   if (type == error_mark_node)
10519     return error_mark_node;
10520   return mangle_conv_op_name_for_type (type);
10521 }
10522
10523 /* Parse a conversion-type-id:
10524
10525    conversion-type-id:
10526      type-specifier-seq conversion-declarator [opt]
10527
10528    Returns the TYPE specified.  */
10529
10530 static tree
10531 cp_parser_conversion_type_id (cp_parser* parser)
10532 {
10533   tree attributes;
10534   cp_decl_specifier_seq type_specifiers;
10535   cp_declarator *declarator;
10536   tree type_specified;
10537
10538   /* Parse the attributes.  */
10539   attributes = cp_parser_attributes_opt (parser);
10540   /* Parse the type-specifiers.  */
10541   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10542                                 /*is_trailing_return=*/false,
10543                                 &type_specifiers);
10544   /* If that didn't work, stop.  */
10545   if (type_specifiers.type == error_mark_node)
10546     return error_mark_node;
10547   /* Parse the conversion-declarator.  */
10548   declarator = cp_parser_conversion_declarator_opt (parser);
10549
10550   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10551                                     /*initialized=*/0, &attributes);
10552   if (attributes)
10553     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10554
10555   /* Don't give this error when parsing tentatively.  This happens to
10556      work because we always parse this definitively once.  */
10557   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10558       && type_uses_auto (type_specified))
10559     {
10560       error ("invalid use of %<auto%> in conversion operator");
10561       return error_mark_node;
10562     }
10563
10564   return type_specified;
10565 }
10566
10567 /* Parse an (optional) conversion-declarator.
10568
10569    conversion-declarator:
10570      ptr-operator conversion-declarator [opt]
10571
10572    */
10573
10574 static cp_declarator *
10575 cp_parser_conversion_declarator_opt (cp_parser* parser)
10576 {
10577   enum tree_code code;
10578   tree class_type;
10579   cp_cv_quals cv_quals;
10580
10581   /* We don't know if there's a ptr-operator next, or not.  */
10582   cp_parser_parse_tentatively (parser);
10583   /* Try the ptr-operator.  */
10584   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10585   /* If it worked, look for more conversion-declarators.  */
10586   if (cp_parser_parse_definitely (parser))
10587     {
10588       cp_declarator *declarator;
10589
10590       /* Parse another optional declarator.  */
10591       declarator = cp_parser_conversion_declarator_opt (parser);
10592
10593       return cp_parser_make_indirect_declarator
10594         (code, class_type, cv_quals, declarator);
10595    }
10596
10597   return NULL;
10598 }
10599
10600 /* Parse an (optional) ctor-initializer.
10601
10602    ctor-initializer:
10603      : mem-initializer-list
10604
10605    Returns TRUE iff the ctor-initializer was actually present.  */
10606
10607 static bool
10608 cp_parser_ctor_initializer_opt (cp_parser* parser)
10609 {
10610   /* If the next token is not a `:', then there is no
10611      ctor-initializer.  */
10612   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10613     {
10614       /* Do default initialization of any bases and members.  */
10615       if (DECL_CONSTRUCTOR_P (current_function_decl))
10616         finish_mem_initializers (NULL_TREE);
10617
10618       return false;
10619     }
10620
10621   /* Consume the `:' token.  */
10622   cp_lexer_consume_token (parser->lexer);
10623   /* And the mem-initializer-list.  */
10624   cp_parser_mem_initializer_list (parser);
10625
10626   return true;
10627 }
10628
10629 /* Parse a mem-initializer-list.
10630
10631    mem-initializer-list:
10632      mem-initializer ... [opt]
10633      mem-initializer ... [opt] , mem-initializer-list  */
10634
10635 static void
10636 cp_parser_mem_initializer_list (cp_parser* parser)
10637 {
10638   tree mem_initializer_list = NULL_TREE;
10639   cp_token *token = cp_lexer_peek_token (parser->lexer);
10640
10641   /* Let the semantic analysis code know that we are starting the
10642      mem-initializer-list.  */
10643   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10644     error_at (token->location,
10645               "only constructors take member initializers");
10646
10647   /* Loop through the list.  */
10648   while (true)
10649     {
10650       tree mem_initializer;
10651
10652       token = cp_lexer_peek_token (parser->lexer);
10653       /* Parse the mem-initializer.  */
10654       mem_initializer = cp_parser_mem_initializer (parser);
10655       /* If the next token is a `...', we're expanding member initializers. */
10656       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10657         {
10658           /* Consume the `...'. */
10659           cp_lexer_consume_token (parser->lexer);
10660
10661           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10662              can be expanded but members cannot. */
10663           if (mem_initializer != error_mark_node
10664               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10665             {
10666               error_at (token->location,
10667                         "cannot expand initializer for member %<%D%>",
10668                         TREE_PURPOSE (mem_initializer));
10669               mem_initializer = error_mark_node;
10670             }
10671
10672           /* Construct the pack expansion type. */
10673           if (mem_initializer != error_mark_node)
10674             mem_initializer = make_pack_expansion (mem_initializer);
10675         }
10676       /* Add it to the list, unless it was erroneous.  */
10677       if (mem_initializer != error_mark_node)
10678         {
10679           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10680           mem_initializer_list = mem_initializer;
10681         }
10682       /* If the next token is not a `,', we're done.  */
10683       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10684         break;
10685       /* Consume the `,' token.  */
10686       cp_lexer_consume_token (parser->lexer);
10687     }
10688
10689   /* Perform semantic analysis.  */
10690   if (DECL_CONSTRUCTOR_P (current_function_decl))
10691     finish_mem_initializers (mem_initializer_list);
10692 }
10693
10694 /* Parse a mem-initializer.
10695
10696    mem-initializer:
10697      mem-initializer-id ( expression-list [opt] )
10698      mem-initializer-id braced-init-list
10699
10700    GNU extension:
10701
10702    mem-initializer:
10703      ( expression-list [opt] )
10704
10705    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10706    class) or FIELD_DECL (for a non-static data member) to initialize;
10707    the TREE_VALUE is the expression-list.  An empty initialization
10708    list is represented by void_list_node.  */
10709
10710 static tree
10711 cp_parser_mem_initializer (cp_parser* parser)
10712 {
10713   tree mem_initializer_id;
10714   tree expression_list;
10715   tree member;
10716   cp_token *token = cp_lexer_peek_token (parser->lexer);
10717
10718   /* Find out what is being initialized.  */
10719   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10720     {
10721       permerror (token->location,
10722                  "anachronistic old-style base class initializer");
10723       mem_initializer_id = NULL_TREE;
10724     }
10725   else
10726     {
10727       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10728       if (mem_initializer_id == error_mark_node)
10729         return mem_initializer_id;
10730     }
10731   member = expand_member_init (mem_initializer_id);
10732   if (member && !DECL_P (member))
10733     in_base_initializer = 1;
10734
10735   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10736     {
10737       bool expr_non_constant_p;
10738       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10739       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10740       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10741       expression_list = build_tree_list (NULL_TREE, expression_list);
10742     }
10743   else
10744     {
10745       VEC(tree,gc)* vec;
10746       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10747                                                      /*cast_p=*/false,
10748                                                      /*allow_expansion_p=*/true,
10749                                                      /*non_constant_p=*/NULL);
10750       if (vec == NULL)
10751         return error_mark_node;
10752       expression_list = build_tree_list_vec (vec);
10753       release_tree_vector (vec);
10754     }
10755
10756   if (expression_list == error_mark_node)
10757     return error_mark_node;
10758   if (!expression_list)
10759     expression_list = void_type_node;
10760
10761   in_base_initializer = 0;
10762
10763   return member ? build_tree_list (member, expression_list) : error_mark_node;
10764 }
10765
10766 /* Parse a mem-initializer-id.
10767
10768    mem-initializer-id:
10769      :: [opt] nested-name-specifier [opt] class-name
10770      identifier
10771
10772    Returns a TYPE indicating the class to be initializer for the first
10773    production.  Returns an IDENTIFIER_NODE indicating the data member
10774    to be initialized for the second production.  */
10775
10776 static tree
10777 cp_parser_mem_initializer_id (cp_parser* parser)
10778 {
10779   bool global_scope_p;
10780   bool nested_name_specifier_p;
10781   bool template_p = false;
10782   tree id;
10783
10784   cp_token *token = cp_lexer_peek_token (parser->lexer);
10785
10786   /* `typename' is not allowed in this context ([temp.res]).  */
10787   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10788     {
10789       error_at (token->location, 
10790                 "keyword %<typename%> not allowed in this context (a qualified "
10791                 "member initializer is implicitly a type)");
10792       cp_lexer_consume_token (parser->lexer);
10793     }
10794   /* Look for the optional `::' operator.  */
10795   global_scope_p
10796     = (cp_parser_global_scope_opt (parser,
10797                                    /*current_scope_valid_p=*/false)
10798        != NULL_TREE);
10799   /* Look for the optional nested-name-specifier.  The simplest way to
10800      implement:
10801
10802        [temp.res]
10803
10804        The keyword `typename' is not permitted in a base-specifier or
10805        mem-initializer; in these contexts a qualified name that
10806        depends on a template-parameter is implicitly assumed to be a
10807        type name.
10808
10809      is to assume that we have seen the `typename' keyword at this
10810      point.  */
10811   nested_name_specifier_p
10812     = (cp_parser_nested_name_specifier_opt (parser,
10813                                             /*typename_keyword_p=*/true,
10814                                             /*check_dependency_p=*/true,
10815                                             /*type_p=*/true,
10816                                             /*is_declaration=*/true)
10817        != NULL_TREE);
10818   if (nested_name_specifier_p)
10819     template_p = cp_parser_optional_template_keyword (parser);
10820   /* If there is a `::' operator or a nested-name-specifier, then we
10821      are definitely looking for a class-name.  */
10822   if (global_scope_p || nested_name_specifier_p)
10823     return cp_parser_class_name (parser,
10824                                  /*typename_keyword_p=*/true,
10825                                  /*template_keyword_p=*/template_p,
10826                                  typename_type,
10827                                  /*check_dependency_p=*/true,
10828                                  /*class_head_p=*/false,
10829                                  /*is_declaration=*/true);
10830   /* Otherwise, we could also be looking for an ordinary identifier.  */
10831   cp_parser_parse_tentatively (parser);
10832   /* Try a class-name.  */
10833   id = cp_parser_class_name (parser,
10834                              /*typename_keyword_p=*/true,
10835                              /*template_keyword_p=*/false,
10836                              none_type,
10837                              /*check_dependency_p=*/true,
10838                              /*class_head_p=*/false,
10839                              /*is_declaration=*/true);
10840   /* If we found one, we're done.  */
10841   if (cp_parser_parse_definitely (parser))
10842     return id;
10843   /* Otherwise, look for an ordinary identifier.  */
10844   return cp_parser_identifier (parser);
10845 }
10846
10847 /* Overloading [gram.over] */
10848
10849 /* Parse an operator-function-id.
10850
10851    operator-function-id:
10852      operator operator
10853
10854    Returns an IDENTIFIER_NODE for the operator which is a
10855    human-readable spelling of the identifier, e.g., `operator +'.  */
10856
10857 static tree
10858 cp_parser_operator_function_id (cp_parser* parser)
10859 {
10860   /* Look for the `operator' keyword.  */
10861   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10862     return error_mark_node;
10863   /* And then the name of the operator itself.  */
10864   return cp_parser_operator (parser);
10865 }
10866
10867 /* Parse an operator.
10868
10869    operator:
10870      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10871      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10872      || ++ -- , ->* -> () []
10873
10874    GNU Extensions:
10875
10876    operator:
10877      <? >? <?= >?=
10878
10879    Returns an IDENTIFIER_NODE for the operator which is a
10880    human-readable spelling of the identifier, e.g., `operator +'.  */
10881
10882 static tree
10883 cp_parser_operator (cp_parser* parser)
10884 {
10885   tree id = NULL_TREE;
10886   cp_token *token;
10887
10888   /* Peek at the next token.  */
10889   token = cp_lexer_peek_token (parser->lexer);
10890   /* Figure out which operator we have.  */
10891   switch (token->type)
10892     {
10893     case CPP_KEYWORD:
10894       {
10895         enum tree_code op;
10896
10897         /* The keyword should be either `new' or `delete'.  */
10898         if (token->keyword == RID_NEW)
10899           op = NEW_EXPR;
10900         else if (token->keyword == RID_DELETE)
10901           op = DELETE_EXPR;
10902         else
10903           break;
10904
10905         /* Consume the `new' or `delete' token.  */
10906         cp_lexer_consume_token (parser->lexer);
10907
10908         /* Peek at the next token.  */
10909         token = cp_lexer_peek_token (parser->lexer);
10910         /* If it's a `[' token then this is the array variant of the
10911            operator.  */
10912         if (token->type == CPP_OPEN_SQUARE)
10913           {
10914             /* Consume the `[' token.  */
10915             cp_lexer_consume_token (parser->lexer);
10916             /* Look for the `]' token.  */
10917             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10918             id = ansi_opname (op == NEW_EXPR
10919                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10920           }
10921         /* Otherwise, we have the non-array variant.  */
10922         else
10923           id = ansi_opname (op);
10924
10925         return id;
10926       }
10927
10928     case CPP_PLUS:
10929       id = ansi_opname (PLUS_EXPR);
10930       break;
10931
10932     case CPP_MINUS:
10933       id = ansi_opname (MINUS_EXPR);
10934       break;
10935
10936     case CPP_MULT:
10937       id = ansi_opname (MULT_EXPR);
10938       break;
10939
10940     case CPP_DIV:
10941       id = ansi_opname (TRUNC_DIV_EXPR);
10942       break;
10943
10944     case CPP_MOD:
10945       id = ansi_opname (TRUNC_MOD_EXPR);
10946       break;
10947
10948     case CPP_XOR:
10949       id = ansi_opname (BIT_XOR_EXPR);
10950       break;
10951
10952     case CPP_AND:
10953       id = ansi_opname (BIT_AND_EXPR);
10954       break;
10955
10956     case CPP_OR:
10957       id = ansi_opname (BIT_IOR_EXPR);
10958       break;
10959
10960     case CPP_COMPL:
10961       id = ansi_opname (BIT_NOT_EXPR);
10962       break;
10963
10964     case CPP_NOT:
10965       id = ansi_opname (TRUTH_NOT_EXPR);
10966       break;
10967
10968     case CPP_EQ:
10969       id = ansi_assopname (NOP_EXPR);
10970       break;
10971
10972     case CPP_LESS:
10973       id = ansi_opname (LT_EXPR);
10974       break;
10975
10976     case CPP_GREATER:
10977       id = ansi_opname (GT_EXPR);
10978       break;
10979
10980     case CPP_PLUS_EQ:
10981       id = ansi_assopname (PLUS_EXPR);
10982       break;
10983
10984     case CPP_MINUS_EQ:
10985       id = ansi_assopname (MINUS_EXPR);
10986       break;
10987
10988     case CPP_MULT_EQ:
10989       id = ansi_assopname (MULT_EXPR);
10990       break;
10991
10992     case CPP_DIV_EQ:
10993       id = ansi_assopname (TRUNC_DIV_EXPR);
10994       break;
10995
10996     case CPP_MOD_EQ:
10997       id = ansi_assopname (TRUNC_MOD_EXPR);
10998       break;
10999
11000     case CPP_XOR_EQ:
11001       id = ansi_assopname (BIT_XOR_EXPR);
11002       break;
11003
11004     case CPP_AND_EQ:
11005       id = ansi_assopname (BIT_AND_EXPR);
11006       break;
11007
11008     case CPP_OR_EQ:
11009       id = ansi_assopname (BIT_IOR_EXPR);
11010       break;
11011
11012     case CPP_LSHIFT:
11013       id = ansi_opname (LSHIFT_EXPR);
11014       break;
11015
11016     case CPP_RSHIFT:
11017       id = ansi_opname (RSHIFT_EXPR);
11018       break;
11019
11020     case CPP_LSHIFT_EQ:
11021       id = ansi_assopname (LSHIFT_EXPR);
11022       break;
11023
11024     case CPP_RSHIFT_EQ:
11025       id = ansi_assopname (RSHIFT_EXPR);
11026       break;
11027
11028     case CPP_EQ_EQ:
11029       id = ansi_opname (EQ_EXPR);
11030       break;
11031
11032     case CPP_NOT_EQ:
11033       id = ansi_opname (NE_EXPR);
11034       break;
11035
11036     case CPP_LESS_EQ:
11037       id = ansi_opname (LE_EXPR);
11038       break;
11039
11040     case CPP_GREATER_EQ:
11041       id = ansi_opname (GE_EXPR);
11042       break;
11043
11044     case CPP_AND_AND:
11045       id = ansi_opname (TRUTH_ANDIF_EXPR);
11046       break;
11047
11048     case CPP_OR_OR:
11049       id = ansi_opname (TRUTH_ORIF_EXPR);
11050       break;
11051
11052     case CPP_PLUS_PLUS:
11053       id = ansi_opname (POSTINCREMENT_EXPR);
11054       break;
11055
11056     case CPP_MINUS_MINUS:
11057       id = ansi_opname (PREDECREMENT_EXPR);
11058       break;
11059
11060     case CPP_COMMA:
11061       id = ansi_opname (COMPOUND_EXPR);
11062       break;
11063
11064     case CPP_DEREF_STAR:
11065       id = ansi_opname (MEMBER_REF);
11066       break;
11067
11068     case CPP_DEREF:
11069       id = ansi_opname (COMPONENT_REF);
11070       break;
11071
11072     case CPP_OPEN_PAREN:
11073       /* Consume the `('.  */
11074       cp_lexer_consume_token (parser->lexer);
11075       /* Look for the matching `)'.  */
11076       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11077       return ansi_opname (CALL_EXPR);
11078
11079     case CPP_OPEN_SQUARE:
11080       /* Consume the `['.  */
11081       cp_lexer_consume_token (parser->lexer);
11082       /* Look for the matching `]'.  */
11083       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11084       return ansi_opname (ARRAY_REF);
11085
11086     default:
11087       /* Anything else is an error.  */
11088       break;
11089     }
11090
11091   /* If we have selected an identifier, we need to consume the
11092      operator token.  */
11093   if (id)
11094     cp_lexer_consume_token (parser->lexer);
11095   /* Otherwise, no valid operator name was present.  */
11096   else
11097     {
11098       cp_parser_error (parser, "expected operator");
11099       id = error_mark_node;
11100     }
11101
11102   return id;
11103 }
11104
11105 /* Parse a template-declaration.
11106
11107    template-declaration:
11108      export [opt] template < template-parameter-list > declaration
11109
11110    If MEMBER_P is TRUE, this template-declaration occurs within a
11111    class-specifier.
11112
11113    The grammar rule given by the standard isn't correct.  What
11114    is really meant is:
11115
11116    template-declaration:
11117      export [opt] template-parameter-list-seq
11118        decl-specifier-seq [opt] init-declarator [opt] ;
11119      export [opt] template-parameter-list-seq
11120        function-definition
11121
11122    template-parameter-list-seq:
11123      template-parameter-list-seq [opt]
11124      template < template-parameter-list >  */
11125
11126 static void
11127 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11128 {
11129   /* Check for `export'.  */
11130   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11131     {
11132       /* Consume the `export' token.  */
11133       cp_lexer_consume_token (parser->lexer);
11134       /* Warn that we do not support `export'.  */
11135       warning (0, "keyword %<export%> not implemented, and will be ignored");
11136     }
11137
11138   cp_parser_template_declaration_after_export (parser, member_p);
11139 }
11140
11141 /* Parse a template-parameter-list.
11142
11143    template-parameter-list:
11144      template-parameter
11145      template-parameter-list , template-parameter
11146
11147    Returns a TREE_LIST.  Each node represents a template parameter.
11148    The nodes are connected via their TREE_CHAINs.  */
11149
11150 static tree
11151 cp_parser_template_parameter_list (cp_parser* parser)
11152 {
11153   tree parameter_list = NULL_TREE;
11154
11155   begin_template_parm_list ();
11156
11157   /* The loop below parses the template parms.  We first need to know
11158      the total number of template parms to be able to compute proper
11159      canonical types of each dependent type. So after the loop, when
11160      we know the total number of template parms,
11161      end_template_parm_list computes the proper canonical types and
11162      fixes up the dependent types accordingly.  */
11163   while (true)
11164     {
11165       tree parameter;
11166       bool is_non_type;
11167       bool is_parameter_pack;
11168       location_t parm_loc;
11169
11170       /* Parse the template-parameter.  */
11171       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11172       parameter = cp_parser_template_parameter (parser, 
11173                                                 &is_non_type,
11174                                                 &is_parameter_pack);
11175       /* Add it to the list.  */
11176       if (parameter != error_mark_node)
11177         parameter_list = process_template_parm (parameter_list,
11178                                                 parm_loc,
11179                                                 parameter,
11180                                                 is_non_type,
11181                                                 is_parameter_pack,
11182                                                 0);
11183       else
11184        {
11185          tree err_parm = build_tree_list (parameter, parameter);
11186          parameter_list = chainon (parameter_list, err_parm);
11187        }
11188
11189       /* If the next token is not a `,', we're done.  */
11190       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11191         break;
11192       /* Otherwise, consume the `,' token.  */
11193       cp_lexer_consume_token (parser->lexer);
11194     }
11195
11196   return end_template_parm_list (parameter_list);
11197 }
11198
11199 /* Parse a template-parameter.
11200
11201    template-parameter:
11202      type-parameter
11203      parameter-declaration
11204
11205    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11206    the parameter.  The TREE_PURPOSE is the default value, if any.
11207    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11208    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11209    set to true iff this parameter is a parameter pack. */
11210
11211 static tree
11212 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11213                               bool *is_parameter_pack)
11214 {
11215   cp_token *token;
11216   cp_parameter_declarator *parameter_declarator;
11217   cp_declarator *id_declarator;
11218   tree parm;
11219
11220   /* Assume it is a type parameter or a template parameter.  */
11221   *is_non_type = false;
11222   /* Assume it not a parameter pack. */
11223   *is_parameter_pack = false;
11224   /* Peek at the next token.  */
11225   token = cp_lexer_peek_token (parser->lexer);
11226   /* If it is `class' or `template', we have a type-parameter.  */
11227   if (token->keyword == RID_TEMPLATE)
11228     return cp_parser_type_parameter (parser, is_parameter_pack);
11229   /* If it is `class' or `typename' we do not know yet whether it is a
11230      type parameter or a non-type parameter.  Consider:
11231
11232        template <typename T, typename T::X X> ...
11233
11234      or:
11235
11236        template <class C, class D*> ...
11237
11238      Here, the first parameter is a type parameter, and the second is
11239      a non-type parameter.  We can tell by looking at the token after
11240      the identifier -- if it is a `,', `=', or `>' then we have a type
11241      parameter.  */
11242   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11243     {
11244       /* Peek at the token after `class' or `typename'.  */
11245       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11246       /* If it's an ellipsis, we have a template type parameter
11247          pack. */
11248       if (token->type == CPP_ELLIPSIS)
11249         return cp_parser_type_parameter (parser, is_parameter_pack);
11250       /* If it's an identifier, skip it.  */
11251       if (token->type == CPP_NAME)
11252         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11253       /* Now, see if the token looks like the end of a template
11254          parameter.  */
11255       if (token->type == CPP_COMMA
11256           || token->type == CPP_EQ
11257           || token->type == CPP_GREATER)
11258         return cp_parser_type_parameter (parser, is_parameter_pack);
11259     }
11260
11261   /* Otherwise, it is a non-type parameter.
11262
11263      [temp.param]
11264
11265      When parsing a default template-argument for a non-type
11266      template-parameter, the first non-nested `>' is taken as the end
11267      of the template parameter-list rather than a greater-than
11268      operator.  */
11269   *is_non_type = true;
11270   parameter_declarator
11271      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11272                                         /*parenthesized_p=*/NULL);
11273
11274   /* If the parameter declaration is marked as a parameter pack, set
11275      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11276      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11277      grokdeclarator. */
11278   if (parameter_declarator
11279       && parameter_declarator->declarator
11280       && parameter_declarator->declarator->parameter_pack_p)
11281     {
11282       *is_parameter_pack = true;
11283       parameter_declarator->declarator->parameter_pack_p = false;
11284     }
11285
11286   /* If the next token is an ellipsis, and we don't already have it
11287      marked as a parameter pack, then we have a parameter pack (that
11288      has no declarator).  */
11289   if (!*is_parameter_pack
11290       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11291       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11292     {
11293       /* Consume the `...'.  */
11294       cp_lexer_consume_token (parser->lexer);
11295       maybe_warn_variadic_templates ();
11296       
11297       *is_parameter_pack = true;
11298     }
11299   /* We might end up with a pack expansion as the type of the non-type
11300      template parameter, in which case this is a non-type template
11301      parameter pack.  */
11302   else if (parameter_declarator
11303            && parameter_declarator->decl_specifiers.type
11304            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11305     {
11306       *is_parameter_pack = true;
11307       parameter_declarator->decl_specifiers.type = 
11308         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11309     }
11310
11311   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11312     {
11313       /* Parameter packs cannot have default arguments.  However, a
11314          user may try to do so, so we'll parse them and give an
11315          appropriate diagnostic here.  */
11316
11317       /* Consume the `='.  */
11318       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11319       cp_lexer_consume_token (parser->lexer);
11320       
11321       /* Find the name of the parameter pack.  */     
11322       id_declarator = parameter_declarator->declarator;
11323       while (id_declarator && id_declarator->kind != cdk_id)
11324         id_declarator = id_declarator->declarator;
11325       
11326       if (id_declarator && id_declarator->kind == cdk_id)
11327         error_at (start_token->location,
11328                   "template parameter pack %qD cannot have a default argument",
11329                   id_declarator->u.id.unqualified_name);
11330       else
11331         error_at (start_token->location,
11332                   "template parameter pack cannot have a default argument");
11333       
11334       /* Parse the default argument, but throw away the result.  */
11335       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11336     }
11337
11338   parm = grokdeclarator (parameter_declarator->declarator,
11339                          &parameter_declarator->decl_specifiers,
11340                          TPARM, /*initialized=*/0,
11341                          /*attrlist=*/NULL);
11342   if (parm == error_mark_node)
11343     return error_mark_node;
11344
11345   return build_tree_list (parameter_declarator->default_argument, parm);
11346 }
11347
11348 /* Parse a type-parameter.
11349
11350    type-parameter:
11351      class identifier [opt]
11352      class identifier [opt] = type-id
11353      typename identifier [opt]
11354      typename identifier [opt] = type-id
11355      template < template-parameter-list > class identifier [opt]
11356      template < template-parameter-list > class identifier [opt]
11357        = id-expression
11358
11359    GNU Extension (variadic templates):
11360
11361    type-parameter:
11362      class ... identifier [opt]
11363      typename ... identifier [opt]
11364
11365    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11366    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11367    the declaration of the parameter.
11368
11369    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11370
11371 static tree
11372 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11373 {
11374   cp_token *token;
11375   tree parameter;
11376
11377   /* Look for a keyword to tell us what kind of parameter this is.  */
11378   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11379   if (!token)
11380     return error_mark_node;
11381
11382   switch (token->keyword)
11383     {
11384     case RID_CLASS:
11385     case RID_TYPENAME:
11386       {
11387         tree identifier;
11388         tree default_argument;
11389
11390         /* If the next token is an ellipsis, we have a template
11391            argument pack. */
11392         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11393           {
11394             /* Consume the `...' token. */
11395             cp_lexer_consume_token (parser->lexer);
11396             maybe_warn_variadic_templates ();
11397
11398             *is_parameter_pack = true;
11399           }
11400
11401         /* If the next token is an identifier, then it names the
11402            parameter.  */
11403         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11404           identifier = cp_parser_identifier (parser);
11405         else
11406           identifier = NULL_TREE;
11407
11408         /* Create the parameter.  */
11409         parameter = finish_template_type_parm (class_type_node, identifier);
11410
11411         /* If the next token is an `=', we have a default argument.  */
11412         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11413           {
11414             /* Consume the `=' token.  */
11415             cp_lexer_consume_token (parser->lexer);
11416             /* Parse the default-argument.  */
11417             push_deferring_access_checks (dk_no_deferred);
11418             default_argument = cp_parser_type_id (parser);
11419
11420             /* Template parameter packs cannot have default
11421                arguments. */
11422             if (*is_parameter_pack)
11423               {
11424                 if (identifier)
11425                   error_at (token->location,
11426                             "template parameter pack %qD cannot have a "
11427                             "default argument", identifier);
11428                 else
11429                   error_at (token->location,
11430                             "template parameter packs cannot have "
11431                             "default arguments");
11432                 default_argument = NULL_TREE;
11433               }
11434             pop_deferring_access_checks ();
11435           }
11436         else
11437           default_argument = NULL_TREE;
11438
11439         /* Create the combined representation of the parameter and the
11440            default argument.  */
11441         parameter = build_tree_list (default_argument, parameter);
11442       }
11443       break;
11444
11445     case RID_TEMPLATE:
11446       {
11447         tree identifier;
11448         tree default_argument;
11449
11450         /* Look for the `<'.  */
11451         cp_parser_require (parser, CPP_LESS, RT_LESS);
11452         /* Parse the template-parameter-list.  */
11453         cp_parser_template_parameter_list (parser);
11454         /* Look for the `>'.  */
11455         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11456         /* Look for the `class' keyword.  */
11457         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11458         /* If the next token is an ellipsis, we have a template
11459            argument pack. */
11460         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11461           {
11462             /* Consume the `...' token. */
11463             cp_lexer_consume_token (parser->lexer);
11464             maybe_warn_variadic_templates ();
11465
11466             *is_parameter_pack = true;
11467           }
11468         /* If the next token is an `=', then there is a
11469            default-argument.  If the next token is a `>', we are at
11470            the end of the parameter-list.  If the next token is a `,',
11471            then we are at the end of this parameter.  */
11472         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11473             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11474             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11475           {
11476             identifier = cp_parser_identifier (parser);
11477             /* Treat invalid names as if the parameter were nameless.  */
11478             if (identifier == error_mark_node)
11479               identifier = NULL_TREE;
11480           }
11481         else
11482           identifier = NULL_TREE;
11483
11484         /* Create the template parameter.  */
11485         parameter = finish_template_template_parm (class_type_node,
11486                                                    identifier);
11487
11488         /* If the next token is an `=', then there is a
11489            default-argument.  */
11490         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11491           {
11492             bool is_template;
11493
11494             /* Consume the `='.  */
11495             cp_lexer_consume_token (parser->lexer);
11496             /* Parse the id-expression.  */
11497             push_deferring_access_checks (dk_no_deferred);
11498             /* save token before parsing the id-expression, for error
11499                reporting */
11500             token = cp_lexer_peek_token (parser->lexer);
11501             default_argument
11502               = cp_parser_id_expression (parser,
11503                                          /*template_keyword_p=*/false,
11504                                          /*check_dependency_p=*/true,
11505                                          /*template_p=*/&is_template,
11506                                          /*declarator_p=*/false,
11507                                          /*optional_p=*/false);
11508             if (TREE_CODE (default_argument) == TYPE_DECL)
11509               /* If the id-expression was a template-id that refers to
11510                  a template-class, we already have the declaration here,
11511                  so no further lookup is needed.  */
11512                  ;
11513             else
11514               /* Look up the name.  */
11515               default_argument
11516                 = cp_parser_lookup_name (parser, default_argument,
11517                                          none_type,
11518                                          /*is_template=*/is_template,
11519                                          /*is_namespace=*/false,
11520                                          /*check_dependency=*/true,
11521                                          /*ambiguous_decls=*/NULL,
11522                                          token->location);
11523             /* See if the default argument is valid.  */
11524             default_argument
11525               = check_template_template_default_arg (default_argument);
11526
11527             /* Template parameter packs cannot have default
11528                arguments. */
11529             if (*is_parameter_pack)
11530               {
11531                 if (identifier)
11532                   error_at (token->location,
11533                             "template parameter pack %qD cannot "
11534                             "have a default argument",
11535                             identifier);
11536                 else
11537                   error_at (token->location, "template parameter packs cannot "
11538                             "have default arguments");
11539                 default_argument = NULL_TREE;
11540               }
11541             pop_deferring_access_checks ();
11542           }
11543         else
11544           default_argument = NULL_TREE;
11545
11546         /* Create the combined representation of the parameter and the
11547            default argument.  */
11548         parameter = build_tree_list (default_argument, parameter);
11549       }
11550       break;
11551
11552     default:
11553       gcc_unreachable ();
11554       break;
11555     }
11556
11557   return parameter;
11558 }
11559
11560 /* Parse a template-id.
11561
11562    template-id:
11563      template-name < template-argument-list [opt] >
11564
11565    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11566    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11567    returned.  Otherwise, if the template-name names a function, or set
11568    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11569    names a class, returns a TYPE_DECL for the specialization.
11570
11571    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11572    uninstantiated templates.  */
11573
11574 static tree
11575 cp_parser_template_id (cp_parser *parser,
11576                        bool template_keyword_p,
11577                        bool check_dependency_p,
11578                        bool is_declaration)
11579 {
11580   int i;
11581   tree templ;
11582   tree arguments;
11583   tree template_id;
11584   cp_token_position start_of_id = 0;
11585   deferred_access_check *chk;
11586   VEC (deferred_access_check,gc) *access_check;
11587   cp_token *next_token = NULL, *next_token_2 = NULL;
11588   bool is_identifier;
11589
11590   /* If the next token corresponds to a template-id, there is no need
11591      to reparse it.  */
11592   next_token = cp_lexer_peek_token (parser->lexer);
11593   if (next_token->type == CPP_TEMPLATE_ID)
11594     {
11595       struct tree_check *check_value;
11596
11597       /* Get the stored value.  */
11598       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11599       /* Perform any access checks that were deferred.  */
11600       access_check = check_value->checks;
11601       if (access_check)
11602         {
11603           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11604             perform_or_defer_access_check (chk->binfo,
11605                                            chk->decl,
11606                                            chk->diag_decl);
11607         }
11608       /* Return the stored value.  */
11609       return check_value->value;
11610     }
11611
11612   /* Avoid performing name lookup if there is no possibility of
11613      finding a template-id.  */
11614   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11615       || (next_token->type == CPP_NAME
11616           && !cp_parser_nth_token_starts_template_argument_list_p
11617                (parser, 2)))
11618     {
11619       cp_parser_error (parser, "expected template-id");
11620       return error_mark_node;
11621     }
11622
11623   /* Remember where the template-id starts.  */
11624   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11625     start_of_id = cp_lexer_token_position (parser->lexer, false);
11626
11627   push_deferring_access_checks (dk_deferred);
11628
11629   /* Parse the template-name.  */
11630   is_identifier = false;
11631   templ = cp_parser_template_name (parser, template_keyword_p,
11632                                    check_dependency_p,
11633                                    is_declaration,
11634                                    &is_identifier);
11635   if (templ == error_mark_node || is_identifier)
11636     {
11637       pop_deferring_access_checks ();
11638       return templ;
11639     }
11640
11641   /* If we find the sequence `[:' after a template-name, it's probably
11642      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11643      parse correctly the argument list.  */
11644   next_token = cp_lexer_peek_token (parser->lexer);
11645   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11646   if (next_token->type == CPP_OPEN_SQUARE
11647       && next_token->flags & DIGRAPH
11648       && next_token_2->type == CPP_COLON
11649       && !(next_token_2->flags & PREV_WHITE))
11650     {
11651       cp_parser_parse_tentatively (parser);
11652       /* Change `:' into `::'.  */
11653       next_token_2->type = CPP_SCOPE;
11654       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11655          CPP_LESS.  */
11656       cp_lexer_consume_token (parser->lexer);
11657
11658       /* Parse the arguments.  */
11659       arguments = cp_parser_enclosed_template_argument_list (parser);
11660       if (!cp_parser_parse_definitely (parser))
11661         {
11662           /* If we couldn't parse an argument list, then we revert our changes
11663              and return simply an error. Maybe this is not a template-id
11664              after all.  */
11665           next_token_2->type = CPP_COLON;
11666           cp_parser_error (parser, "expected %<<%>");
11667           pop_deferring_access_checks ();
11668           return error_mark_node;
11669         }
11670       /* Otherwise, emit an error about the invalid digraph, but continue
11671          parsing because we got our argument list.  */
11672       if (permerror (next_token->location,
11673                      "%<<::%> cannot begin a template-argument list"))
11674         {
11675           static bool hint = false;
11676           inform (next_token->location,
11677                   "%<<:%> is an alternate spelling for %<[%>."
11678                   " Insert whitespace between %<<%> and %<::%>");
11679           if (!hint && !flag_permissive)
11680             {
11681               inform (next_token->location, "(if you use %<-fpermissive%>"
11682                       " G++ will accept your code)");
11683               hint = true;
11684             }
11685         }
11686     }
11687   else
11688     {
11689       /* Look for the `<' that starts the template-argument-list.  */
11690       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11691         {
11692           pop_deferring_access_checks ();
11693           return error_mark_node;
11694         }
11695       /* Parse the arguments.  */
11696       arguments = cp_parser_enclosed_template_argument_list (parser);
11697     }
11698
11699   /* Build a representation of the specialization.  */
11700   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11701     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11702   else if (DECL_CLASS_TEMPLATE_P (templ)
11703            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11704     {
11705       bool entering_scope;
11706       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11707          template (rather than some instantiation thereof) only if
11708          is not nested within some other construct.  For example, in
11709          "template <typename T> void f(T) { A<T>::", A<T> is just an
11710          instantiation of A.  */
11711       entering_scope = (template_parm_scope_p ()
11712                         && cp_lexer_next_token_is (parser->lexer,
11713                                                    CPP_SCOPE));
11714       template_id
11715         = finish_template_type (templ, arguments, entering_scope);
11716     }
11717   else
11718     {
11719       /* If it's not a class-template or a template-template, it should be
11720          a function-template.  */
11721       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11722                    || TREE_CODE (templ) == OVERLOAD
11723                    || BASELINK_P (templ)));
11724
11725       template_id = lookup_template_function (templ, arguments);
11726     }
11727
11728   /* If parsing tentatively, replace the sequence of tokens that makes
11729      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11730      should we re-parse the token stream, we will not have to repeat
11731      the effort required to do the parse, nor will we issue duplicate
11732      error messages about problems during instantiation of the
11733      template.  */
11734   if (start_of_id)
11735     {
11736       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11737
11738       /* Reset the contents of the START_OF_ID token.  */
11739       token->type = CPP_TEMPLATE_ID;
11740       /* Retrieve any deferred checks.  Do not pop this access checks yet
11741          so the memory will not be reclaimed during token replacing below.  */
11742       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11743       token->u.tree_check_value->value = template_id;
11744       token->u.tree_check_value->checks = get_deferred_access_checks ();
11745       token->keyword = RID_MAX;
11746
11747       /* Purge all subsequent tokens.  */
11748       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11749
11750       /* ??? Can we actually assume that, if template_id ==
11751          error_mark_node, we will have issued a diagnostic to the
11752          user, as opposed to simply marking the tentative parse as
11753          failed?  */
11754       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11755         error_at (token->location, "parse error in template argument list");
11756     }
11757
11758   pop_deferring_access_checks ();
11759   return template_id;
11760 }
11761
11762 /* Parse a template-name.
11763
11764    template-name:
11765      identifier
11766
11767    The standard should actually say:
11768
11769    template-name:
11770      identifier
11771      operator-function-id
11772
11773    A defect report has been filed about this issue.
11774
11775    A conversion-function-id cannot be a template name because they cannot
11776    be part of a template-id. In fact, looking at this code:
11777
11778    a.operator K<int>()
11779
11780    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11781    It is impossible to call a templated conversion-function-id with an
11782    explicit argument list, since the only allowed template parameter is
11783    the type to which it is converting.
11784
11785    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11786    `template' keyword, in a construction like:
11787
11788      T::template f<3>()
11789
11790    In that case `f' is taken to be a template-name, even though there
11791    is no way of knowing for sure.
11792
11793    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11794    name refers to a set of overloaded functions, at least one of which
11795    is a template, or an IDENTIFIER_NODE with the name of the template,
11796    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11797    names are looked up inside uninstantiated templates.  */
11798
11799 static tree
11800 cp_parser_template_name (cp_parser* parser,
11801                          bool template_keyword_p,
11802                          bool check_dependency_p,
11803                          bool is_declaration,
11804                          bool *is_identifier)
11805 {
11806   tree identifier;
11807   tree decl;
11808   tree fns;
11809   cp_token *token = cp_lexer_peek_token (parser->lexer);
11810
11811   /* If the next token is `operator', then we have either an
11812      operator-function-id or a conversion-function-id.  */
11813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11814     {
11815       /* We don't know whether we're looking at an
11816          operator-function-id or a conversion-function-id.  */
11817       cp_parser_parse_tentatively (parser);
11818       /* Try an operator-function-id.  */
11819       identifier = cp_parser_operator_function_id (parser);
11820       /* If that didn't work, try a conversion-function-id.  */
11821       if (!cp_parser_parse_definitely (parser))
11822         {
11823           cp_parser_error (parser, "expected template-name");
11824           return error_mark_node;
11825         }
11826     }
11827   /* Look for the identifier.  */
11828   else
11829     identifier = cp_parser_identifier (parser);
11830
11831   /* If we didn't find an identifier, we don't have a template-id.  */
11832   if (identifier == error_mark_node)
11833     return error_mark_node;
11834
11835   /* If the name immediately followed the `template' keyword, then it
11836      is a template-name.  However, if the next token is not `<', then
11837      we do not treat it as a template-name, since it is not being used
11838      as part of a template-id.  This enables us to handle constructs
11839      like:
11840
11841        template <typename T> struct S { S(); };
11842        template <typename T> S<T>::S();
11843
11844      correctly.  We would treat `S' as a template -- if it were `S<T>'
11845      -- but we do not if there is no `<'.  */
11846
11847   if (processing_template_decl
11848       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11849     {
11850       /* In a declaration, in a dependent context, we pretend that the
11851          "template" keyword was present in order to improve error
11852          recovery.  For example, given:
11853
11854            template <typename T> void f(T::X<int>);
11855
11856          we want to treat "X<int>" as a template-id.  */
11857       if (is_declaration
11858           && !template_keyword_p
11859           && parser->scope && TYPE_P (parser->scope)
11860           && check_dependency_p
11861           && dependent_scope_p (parser->scope)
11862           /* Do not do this for dtors (or ctors), since they never
11863              need the template keyword before their name.  */
11864           && !constructor_name_p (identifier, parser->scope))
11865         {
11866           cp_token_position start = 0;
11867
11868           /* Explain what went wrong.  */
11869           error_at (token->location, "non-template %qD used as template",
11870                     identifier);
11871           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11872                   parser->scope, identifier);
11873           /* If parsing tentatively, find the location of the "<" token.  */
11874           if (cp_parser_simulate_error (parser))
11875             start = cp_lexer_token_position (parser->lexer, true);
11876           /* Parse the template arguments so that we can issue error
11877              messages about them.  */
11878           cp_lexer_consume_token (parser->lexer);
11879           cp_parser_enclosed_template_argument_list (parser);
11880           /* Skip tokens until we find a good place from which to
11881              continue parsing.  */
11882           cp_parser_skip_to_closing_parenthesis (parser,
11883                                                  /*recovering=*/true,
11884                                                  /*or_comma=*/true,
11885                                                  /*consume_paren=*/false);
11886           /* If parsing tentatively, permanently remove the
11887              template argument list.  That will prevent duplicate
11888              error messages from being issued about the missing
11889              "template" keyword.  */
11890           if (start)
11891             cp_lexer_purge_tokens_after (parser->lexer, start);
11892           if (is_identifier)
11893             *is_identifier = true;
11894           return identifier;
11895         }
11896
11897       /* If the "template" keyword is present, then there is generally
11898          no point in doing name-lookup, so we just return IDENTIFIER.
11899          But, if the qualifying scope is non-dependent then we can
11900          (and must) do name-lookup normally.  */
11901       if (template_keyword_p
11902           && (!parser->scope
11903               || (TYPE_P (parser->scope)
11904                   && dependent_type_p (parser->scope))))
11905         return identifier;
11906     }
11907
11908   /* Look up the name.  */
11909   decl = cp_parser_lookup_name (parser, identifier,
11910                                 none_type,
11911                                 /*is_template=*/true,
11912                                 /*is_namespace=*/false,
11913                                 check_dependency_p,
11914                                 /*ambiguous_decls=*/NULL,
11915                                 token->location);
11916
11917   /* If DECL is a template, then the name was a template-name.  */
11918   if (TREE_CODE (decl) == TEMPLATE_DECL)
11919     ;
11920   else
11921     {
11922       tree fn = NULL_TREE;
11923
11924       /* The standard does not explicitly indicate whether a name that
11925          names a set of overloaded declarations, some of which are
11926          templates, is a template-name.  However, such a name should
11927          be a template-name; otherwise, there is no way to form a
11928          template-id for the overloaded templates.  */
11929       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11930       if (TREE_CODE (fns) == OVERLOAD)
11931         for (fn = fns; fn; fn = OVL_NEXT (fn))
11932           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11933             break;
11934
11935       if (!fn)
11936         {
11937           /* The name does not name a template.  */
11938           cp_parser_error (parser, "expected template-name");
11939           return error_mark_node;
11940         }
11941     }
11942
11943   /* If DECL is dependent, and refers to a function, then just return
11944      its name; we will look it up again during template instantiation.  */
11945   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11946     {
11947       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11948       if (TYPE_P (scope) && dependent_type_p (scope))
11949         return identifier;
11950     }
11951
11952   return decl;
11953 }
11954
11955 /* Parse a template-argument-list.
11956
11957    template-argument-list:
11958      template-argument ... [opt]
11959      template-argument-list , template-argument ... [opt]
11960
11961    Returns a TREE_VEC containing the arguments.  */
11962
11963 static tree
11964 cp_parser_template_argument_list (cp_parser* parser)
11965 {
11966   tree fixed_args[10];
11967   unsigned n_args = 0;
11968   unsigned alloced = 10;
11969   tree *arg_ary = fixed_args;
11970   tree vec;
11971   bool saved_in_template_argument_list_p;
11972   bool saved_ice_p;
11973   bool saved_non_ice_p;
11974
11975   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11976   parser->in_template_argument_list_p = true;
11977   /* Even if the template-id appears in an integral
11978      constant-expression, the contents of the argument list do
11979      not.  */
11980   saved_ice_p = parser->integral_constant_expression_p;
11981   parser->integral_constant_expression_p = false;
11982   saved_non_ice_p = parser->non_integral_constant_expression_p;
11983   parser->non_integral_constant_expression_p = false;
11984   /* Parse the arguments.  */
11985   do
11986     {
11987       tree argument;
11988
11989       if (n_args)
11990         /* Consume the comma.  */
11991         cp_lexer_consume_token (parser->lexer);
11992
11993       /* Parse the template-argument.  */
11994       argument = cp_parser_template_argument (parser);
11995
11996       /* If the next token is an ellipsis, we're expanding a template
11997          argument pack. */
11998       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11999         {
12000           if (argument == error_mark_node)
12001             {
12002               cp_token *token = cp_lexer_peek_token (parser->lexer);
12003               error_at (token->location,
12004                         "expected parameter pack before %<...%>");
12005             }
12006           /* Consume the `...' token. */
12007           cp_lexer_consume_token (parser->lexer);
12008
12009           /* Make the argument into a TYPE_PACK_EXPANSION or
12010              EXPR_PACK_EXPANSION. */
12011           argument = make_pack_expansion (argument);
12012         }
12013
12014       if (n_args == alloced)
12015         {
12016           alloced *= 2;
12017
12018           if (arg_ary == fixed_args)
12019             {
12020               arg_ary = XNEWVEC (tree, alloced);
12021               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12022             }
12023           else
12024             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12025         }
12026       arg_ary[n_args++] = argument;
12027     }
12028   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12029
12030   vec = make_tree_vec (n_args);
12031
12032   while (n_args--)
12033     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12034
12035   if (arg_ary != fixed_args)
12036     free (arg_ary);
12037   parser->non_integral_constant_expression_p = saved_non_ice_p;
12038   parser->integral_constant_expression_p = saved_ice_p;
12039   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12040 #ifdef ENABLE_CHECKING
12041   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12042 #endif
12043   return vec;
12044 }
12045
12046 /* Parse a template-argument.
12047
12048    template-argument:
12049      assignment-expression
12050      type-id
12051      id-expression
12052
12053    The representation is that of an assignment-expression, type-id, or
12054    id-expression -- except that the qualified id-expression is
12055    evaluated, so that the value returned is either a DECL or an
12056    OVERLOAD.
12057
12058    Although the standard says "assignment-expression", it forbids
12059    throw-expressions or assignments in the template argument.
12060    Therefore, we use "conditional-expression" instead.  */
12061
12062 static tree
12063 cp_parser_template_argument (cp_parser* parser)
12064 {
12065   tree argument;
12066   bool template_p;
12067   bool address_p;
12068   bool maybe_type_id = false;
12069   cp_token *token = NULL, *argument_start_token = NULL;
12070   cp_id_kind idk;
12071
12072   /* There's really no way to know what we're looking at, so we just
12073      try each alternative in order.
12074
12075        [temp.arg]
12076
12077        In a template-argument, an ambiguity between a type-id and an
12078        expression is resolved to a type-id, regardless of the form of
12079        the corresponding template-parameter.
12080
12081      Therefore, we try a type-id first.  */
12082   cp_parser_parse_tentatively (parser);
12083   argument = cp_parser_template_type_arg (parser);
12084   /* If there was no error parsing the type-id but the next token is a
12085      '>>', our behavior depends on which dialect of C++ we're
12086      parsing. In C++98, we probably found a typo for '> >'. But there
12087      are type-id which are also valid expressions. For instance:
12088
12089      struct X { int operator >> (int); };
12090      template <int V> struct Foo {};
12091      Foo<X () >> 5> r;
12092
12093      Here 'X()' is a valid type-id of a function type, but the user just
12094      wanted to write the expression "X() >> 5". Thus, we remember that we
12095      found a valid type-id, but we still try to parse the argument as an
12096      expression to see what happens. 
12097
12098      In C++0x, the '>>' will be considered two separate '>'
12099      tokens.  */
12100   if (!cp_parser_error_occurred (parser)
12101       && cxx_dialect == cxx98
12102       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12103     {
12104       maybe_type_id = true;
12105       cp_parser_abort_tentative_parse (parser);
12106     }
12107   else
12108     {
12109       /* If the next token isn't a `,' or a `>', then this argument wasn't
12110       really finished. This means that the argument is not a valid
12111       type-id.  */
12112       if (!cp_parser_next_token_ends_template_argument_p (parser))
12113         cp_parser_error (parser, "expected template-argument");
12114       /* If that worked, we're done.  */
12115       if (cp_parser_parse_definitely (parser))
12116         return argument;
12117     }
12118   /* We're still not sure what the argument will be.  */
12119   cp_parser_parse_tentatively (parser);
12120   /* Try a template.  */
12121   argument_start_token = cp_lexer_peek_token (parser->lexer);
12122   argument = cp_parser_id_expression (parser,
12123                                       /*template_keyword_p=*/false,
12124                                       /*check_dependency_p=*/true,
12125                                       &template_p,
12126                                       /*declarator_p=*/false,
12127                                       /*optional_p=*/false);
12128   /* If the next token isn't a `,' or a `>', then this argument wasn't
12129      really finished.  */
12130   if (!cp_parser_next_token_ends_template_argument_p (parser))
12131     cp_parser_error (parser, "expected template-argument");
12132   if (!cp_parser_error_occurred (parser))
12133     {
12134       /* Figure out what is being referred to.  If the id-expression
12135          was for a class template specialization, then we will have a
12136          TYPE_DECL at this point.  There is no need to do name lookup
12137          at this point in that case.  */
12138       if (TREE_CODE (argument) != TYPE_DECL)
12139         argument = cp_parser_lookup_name (parser, argument,
12140                                           none_type,
12141                                           /*is_template=*/template_p,
12142                                           /*is_namespace=*/false,
12143                                           /*check_dependency=*/true,
12144                                           /*ambiguous_decls=*/NULL,
12145                                           argument_start_token->location);
12146       if (TREE_CODE (argument) != TEMPLATE_DECL
12147           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12148         cp_parser_error (parser, "expected template-name");
12149     }
12150   if (cp_parser_parse_definitely (parser))
12151     return argument;
12152   /* It must be a non-type argument.  There permitted cases are given
12153      in [temp.arg.nontype]:
12154
12155      -- an integral constant-expression of integral or enumeration
12156         type; or
12157
12158      -- the name of a non-type template-parameter; or
12159
12160      -- the name of an object or function with external linkage...
12161
12162      -- the address of an object or function with external linkage...
12163
12164      -- a pointer to member...  */
12165   /* Look for a non-type template parameter.  */
12166   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12167     {
12168       cp_parser_parse_tentatively (parser);
12169       argument = cp_parser_primary_expression (parser,
12170                                                /*address_p=*/false,
12171                                                /*cast_p=*/false,
12172                                                /*template_arg_p=*/true,
12173                                                &idk);
12174       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12175           || !cp_parser_next_token_ends_template_argument_p (parser))
12176         cp_parser_simulate_error (parser);
12177       if (cp_parser_parse_definitely (parser))
12178         return argument;
12179     }
12180
12181   /* If the next token is "&", the argument must be the address of an
12182      object or function with external linkage.  */
12183   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12184   if (address_p)
12185     cp_lexer_consume_token (parser->lexer);
12186   /* See if we might have an id-expression.  */
12187   token = cp_lexer_peek_token (parser->lexer);
12188   if (token->type == CPP_NAME
12189       || token->keyword == RID_OPERATOR
12190       || token->type == CPP_SCOPE
12191       || token->type == CPP_TEMPLATE_ID
12192       || token->type == CPP_NESTED_NAME_SPECIFIER)
12193     {
12194       cp_parser_parse_tentatively (parser);
12195       argument = cp_parser_primary_expression (parser,
12196                                                address_p,
12197                                                /*cast_p=*/false,
12198                                                /*template_arg_p=*/true,
12199                                                &idk);
12200       if (cp_parser_error_occurred (parser)
12201           || !cp_parser_next_token_ends_template_argument_p (parser))
12202         cp_parser_abort_tentative_parse (parser);
12203       else
12204         {
12205           tree probe;
12206
12207           if (TREE_CODE (argument) == INDIRECT_REF)
12208             {
12209               gcc_assert (REFERENCE_REF_P (argument));
12210               argument = TREE_OPERAND (argument, 0);
12211             }
12212
12213           /* If we're in a template, we represent a qualified-id referring
12214              to a static data member as a SCOPE_REF even if the scope isn't
12215              dependent so that we can check access control later.  */
12216           probe = argument;
12217           if (TREE_CODE (probe) == SCOPE_REF)
12218             probe = TREE_OPERAND (probe, 1);
12219           if (TREE_CODE (probe) == VAR_DECL)
12220             {
12221               /* A variable without external linkage might still be a
12222                  valid constant-expression, so no error is issued here
12223                  if the external-linkage check fails.  */
12224               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12225                 cp_parser_simulate_error (parser);
12226             }
12227           else if (is_overloaded_fn (argument))
12228             /* All overloaded functions are allowed; if the external
12229                linkage test does not pass, an error will be issued
12230                later.  */
12231             ;
12232           else if (address_p
12233                    && (TREE_CODE (argument) == OFFSET_REF
12234                        || TREE_CODE (argument) == SCOPE_REF))
12235             /* A pointer-to-member.  */
12236             ;
12237           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12238             ;
12239           else
12240             cp_parser_simulate_error (parser);
12241
12242           if (cp_parser_parse_definitely (parser))
12243             {
12244               if (address_p)
12245                 argument = build_x_unary_op (ADDR_EXPR, argument,
12246                                              tf_warning_or_error);
12247               return argument;
12248             }
12249         }
12250     }
12251   /* If the argument started with "&", there are no other valid
12252      alternatives at this point.  */
12253   if (address_p)
12254     {
12255       cp_parser_error (parser, "invalid non-type template argument");
12256       return error_mark_node;
12257     }
12258
12259   /* If the argument wasn't successfully parsed as a type-id followed
12260      by '>>', the argument can only be a constant expression now.
12261      Otherwise, we try parsing the constant-expression tentatively,
12262      because the argument could really be a type-id.  */
12263   if (maybe_type_id)
12264     cp_parser_parse_tentatively (parser);
12265   argument = cp_parser_constant_expression (parser,
12266                                             /*allow_non_constant_p=*/false,
12267                                             /*non_constant_p=*/NULL);
12268   argument = fold_non_dependent_expr (argument);
12269   if (!maybe_type_id)
12270     return argument;
12271   if (!cp_parser_next_token_ends_template_argument_p (parser))
12272     cp_parser_error (parser, "expected template-argument");
12273   if (cp_parser_parse_definitely (parser))
12274     return argument;
12275   /* We did our best to parse the argument as a non type-id, but that
12276      was the only alternative that matched (albeit with a '>' after
12277      it). We can assume it's just a typo from the user, and a
12278      diagnostic will then be issued.  */
12279   return cp_parser_template_type_arg (parser);
12280 }
12281
12282 /* Parse an explicit-instantiation.
12283
12284    explicit-instantiation:
12285      template declaration
12286
12287    Although the standard says `declaration', what it really means is:
12288
12289    explicit-instantiation:
12290      template decl-specifier-seq [opt] declarator [opt] ;
12291
12292    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12293    supposed to be allowed.  A defect report has been filed about this
12294    issue.
12295
12296    GNU Extension:
12297
12298    explicit-instantiation:
12299      storage-class-specifier template
12300        decl-specifier-seq [opt] declarator [opt] ;
12301      function-specifier template
12302        decl-specifier-seq [opt] declarator [opt] ;  */
12303
12304 static void
12305 cp_parser_explicit_instantiation (cp_parser* parser)
12306 {
12307   int declares_class_or_enum;
12308   cp_decl_specifier_seq decl_specifiers;
12309   tree extension_specifier = NULL_TREE;
12310
12311   /* Look for an (optional) storage-class-specifier or
12312      function-specifier.  */
12313   if (cp_parser_allow_gnu_extensions_p (parser))
12314     {
12315       extension_specifier
12316         = cp_parser_storage_class_specifier_opt (parser);
12317       if (!extension_specifier)
12318         extension_specifier
12319           = cp_parser_function_specifier_opt (parser,
12320                                               /*decl_specs=*/NULL);
12321     }
12322
12323   /* Look for the `template' keyword.  */
12324   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12325   /* Let the front end know that we are processing an explicit
12326      instantiation.  */
12327   begin_explicit_instantiation ();
12328   /* [temp.explicit] says that we are supposed to ignore access
12329      control while processing explicit instantiation directives.  */
12330   push_deferring_access_checks (dk_no_check);
12331   /* Parse a decl-specifier-seq.  */
12332   cp_parser_decl_specifier_seq (parser,
12333                                 CP_PARSER_FLAGS_OPTIONAL,
12334                                 &decl_specifiers,
12335                                 &declares_class_or_enum);
12336   /* If there was exactly one decl-specifier, and it declared a class,
12337      and there's no declarator, then we have an explicit type
12338      instantiation.  */
12339   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12340     {
12341       tree type;
12342
12343       type = check_tag_decl (&decl_specifiers);
12344       /* Turn access control back on for names used during
12345          template instantiation.  */
12346       pop_deferring_access_checks ();
12347       if (type)
12348         do_type_instantiation (type, extension_specifier,
12349                                /*complain=*/tf_error);
12350     }
12351   else
12352     {
12353       cp_declarator *declarator;
12354       tree decl;
12355
12356       /* Parse the declarator.  */
12357       declarator
12358         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12359                                 /*ctor_dtor_or_conv_p=*/NULL,
12360                                 /*parenthesized_p=*/NULL,
12361                                 /*member_p=*/false);
12362       if (declares_class_or_enum & 2)
12363         cp_parser_check_for_definition_in_return_type (declarator,
12364                                                        decl_specifiers.type,
12365                                                        decl_specifiers.type_location);
12366       if (declarator != cp_error_declarator)
12367         {
12368           if (decl_specifiers.specs[(int)ds_inline])
12369             permerror (input_location, "explicit instantiation shall not use"
12370                        " %<inline%> specifier");
12371           if (decl_specifiers.specs[(int)ds_constexpr])
12372             permerror (input_location, "explicit instantiation shall not use"
12373                        " %<constexpr%> specifier");
12374
12375           decl = grokdeclarator (declarator, &decl_specifiers,
12376                                  NORMAL, 0, &decl_specifiers.attributes);
12377           /* Turn access control back on for names used during
12378              template instantiation.  */
12379           pop_deferring_access_checks ();
12380           /* Do the explicit instantiation.  */
12381           do_decl_instantiation (decl, extension_specifier);
12382         }
12383       else
12384         {
12385           pop_deferring_access_checks ();
12386           /* Skip the body of the explicit instantiation.  */
12387           cp_parser_skip_to_end_of_statement (parser);
12388         }
12389     }
12390   /* We're done with the instantiation.  */
12391   end_explicit_instantiation ();
12392
12393   cp_parser_consume_semicolon_at_end_of_statement (parser);
12394 }
12395
12396 /* Parse an explicit-specialization.
12397
12398    explicit-specialization:
12399      template < > declaration
12400
12401    Although the standard says `declaration', what it really means is:
12402
12403    explicit-specialization:
12404      template <> decl-specifier [opt] init-declarator [opt] ;
12405      template <> function-definition
12406      template <> explicit-specialization
12407      template <> template-declaration  */
12408
12409 static void
12410 cp_parser_explicit_specialization (cp_parser* parser)
12411 {
12412   bool need_lang_pop;
12413   cp_token *token = cp_lexer_peek_token (parser->lexer);
12414
12415   /* Look for the `template' keyword.  */
12416   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12417   /* Look for the `<'.  */
12418   cp_parser_require (parser, CPP_LESS, RT_LESS);
12419   /* Look for the `>'.  */
12420   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12421   /* We have processed another parameter list.  */
12422   ++parser->num_template_parameter_lists;
12423   /* [temp]
12424
12425      A template ... explicit specialization ... shall not have C
12426      linkage.  */
12427   if (current_lang_name == lang_name_c)
12428     {
12429       error_at (token->location, "template specialization with C linkage");
12430       /* Give it C++ linkage to avoid confusing other parts of the
12431          front end.  */
12432       push_lang_context (lang_name_cplusplus);
12433       need_lang_pop = true;
12434     }
12435   else
12436     need_lang_pop = false;
12437   /* Let the front end know that we are beginning a specialization.  */
12438   if (!begin_specialization ())
12439     {
12440       end_specialization ();
12441       return;
12442     }
12443
12444   /* If the next keyword is `template', we need to figure out whether
12445      or not we're looking a template-declaration.  */
12446   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12447     {
12448       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12449           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12450         cp_parser_template_declaration_after_export (parser,
12451                                                      /*member_p=*/false);
12452       else
12453         cp_parser_explicit_specialization (parser);
12454     }
12455   else
12456     /* Parse the dependent declaration.  */
12457     cp_parser_single_declaration (parser,
12458                                   /*checks=*/NULL,
12459                                   /*member_p=*/false,
12460                                   /*explicit_specialization_p=*/true,
12461                                   /*friend_p=*/NULL);
12462   /* We're done with the specialization.  */
12463   end_specialization ();
12464   /* For the erroneous case of a template with C linkage, we pushed an
12465      implicit C++ linkage scope; exit that scope now.  */
12466   if (need_lang_pop)
12467     pop_lang_context ();
12468   /* We're done with this parameter list.  */
12469   --parser->num_template_parameter_lists;
12470 }
12471
12472 /* Parse a type-specifier.
12473
12474    type-specifier:
12475      simple-type-specifier
12476      class-specifier
12477      enum-specifier
12478      elaborated-type-specifier
12479      cv-qualifier
12480
12481    GNU Extension:
12482
12483    type-specifier:
12484      __complex__
12485
12486    Returns a representation of the type-specifier.  For a
12487    class-specifier, enum-specifier, or elaborated-type-specifier, a
12488    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12489
12490    The parser flags FLAGS is used to control type-specifier parsing.
12491
12492    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12493    in a decl-specifier-seq.
12494
12495    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12496    class-specifier, enum-specifier, or elaborated-type-specifier, then
12497    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12498    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12499    zero.
12500
12501    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12502    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12503    is set to FALSE.  */
12504
12505 static tree
12506 cp_parser_type_specifier (cp_parser* parser,
12507                           cp_parser_flags flags,
12508                           cp_decl_specifier_seq *decl_specs,
12509                           bool is_declaration,
12510                           int* declares_class_or_enum,
12511                           bool* is_cv_qualifier)
12512 {
12513   tree type_spec = NULL_TREE;
12514   cp_token *token;
12515   enum rid keyword;
12516   cp_decl_spec ds = ds_last;
12517
12518   /* Assume this type-specifier does not declare a new type.  */
12519   if (declares_class_or_enum)
12520     *declares_class_or_enum = 0;
12521   /* And that it does not specify a cv-qualifier.  */
12522   if (is_cv_qualifier)
12523     *is_cv_qualifier = false;
12524   /* Peek at the next token.  */
12525   token = cp_lexer_peek_token (parser->lexer);
12526
12527   /* If we're looking at a keyword, we can use that to guide the
12528      production we choose.  */
12529   keyword = token->keyword;
12530   switch (keyword)
12531     {
12532     case RID_ENUM:
12533       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12534         goto elaborated_type_specifier;
12535
12536       /* Look for the enum-specifier.  */
12537       type_spec = cp_parser_enum_specifier (parser);
12538       /* If that worked, we're done.  */
12539       if (type_spec)
12540         {
12541           if (declares_class_or_enum)
12542             *declares_class_or_enum = 2;
12543           if (decl_specs)
12544             cp_parser_set_decl_spec_type (decl_specs,
12545                                           type_spec,
12546                                           token->location,
12547                                           /*user_defined_p=*/true);
12548           return type_spec;
12549         }
12550       else
12551         goto elaborated_type_specifier;
12552
12553       /* Any of these indicate either a class-specifier, or an
12554          elaborated-type-specifier.  */
12555     case RID_CLASS:
12556     case RID_STRUCT:
12557     case RID_UNION:
12558       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12559         goto elaborated_type_specifier;
12560
12561       /* Parse tentatively so that we can back up if we don't find a
12562          class-specifier.  */
12563       cp_parser_parse_tentatively (parser);
12564       /* Look for the class-specifier.  */
12565       type_spec = cp_parser_class_specifier (parser);
12566       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12567       /* If that worked, we're done.  */
12568       if (cp_parser_parse_definitely (parser))
12569         {
12570           if (declares_class_or_enum)
12571             *declares_class_or_enum = 2;
12572           if (decl_specs)
12573             cp_parser_set_decl_spec_type (decl_specs,
12574                                           type_spec,
12575                                           token->location,
12576                                           /*user_defined_p=*/true);
12577           return type_spec;
12578         }
12579
12580       /* Fall through.  */
12581     elaborated_type_specifier:
12582       /* We're declaring (not defining) a class or enum.  */
12583       if (declares_class_or_enum)
12584         *declares_class_or_enum = 1;
12585
12586       /* Fall through.  */
12587     case RID_TYPENAME:
12588       /* Look for an elaborated-type-specifier.  */
12589       type_spec
12590         = (cp_parser_elaborated_type_specifier
12591            (parser,
12592             decl_specs && decl_specs->specs[(int) ds_friend],
12593             is_declaration));
12594       if (decl_specs)
12595         cp_parser_set_decl_spec_type (decl_specs,
12596                                       type_spec,
12597                                       token->location,
12598                                       /*user_defined_p=*/true);
12599       return type_spec;
12600
12601     case RID_CONST:
12602       ds = ds_const;
12603       if (is_cv_qualifier)
12604         *is_cv_qualifier = true;
12605       break;
12606
12607     case RID_VOLATILE:
12608       ds = ds_volatile;
12609       if (is_cv_qualifier)
12610         *is_cv_qualifier = true;
12611       break;
12612
12613     case RID_RESTRICT:
12614       ds = ds_restrict;
12615       if (is_cv_qualifier)
12616         *is_cv_qualifier = true;
12617       break;
12618
12619     case RID_COMPLEX:
12620       /* The `__complex__' keyword is a GNU extension.  */
12621       ds = ds_complex;
12622       break;
12623
12624     default:
12625       break;
12626     }
12627
12628   /* Handle simple keywords.  */
12629   if (ds != ds_last)
12630     {
12631       if (decl_specs)
12632         {
12633           ++decl_specs->specs[(int)ds];
12634           decl_specs->any_specifiers_p = true;
12635         }
12636       return cp_lexer_consume_token (parser->lexer)->u.value;
12637     }
12638
12639   /* If we do not already have a type-specifier, assume we are looking
12640      at a simple-type-specifier.  */
12641   type_spec = cp_parser_simple_type_specifier (parser,
12642                                                decl_specs,
12643                                                flags);
12644
12645   /* If we didn't find a type-specifier, and a type-specifier was not
12646      optional in this context, issue an error message.  */
12647   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12648     {
12649       cp_parser_error (parser, "expected type specifier");
12650       return error_mark_node;
12651     }
12652
12653   return type_spec;
12654 }
12655
12656 /* Parse a simple-type-specifier.
12657
12658    simple-type-specifier:
12659      :: [opt] nested-name-specifier [opt] type-name
12660      :: [opt] nested-name-specifier template template-id
12661      char
12662      wchar_t
12663      bool
12664      short
12665      int
12666      long
12667      signed
12668      unsigned
12669      float
12670      double
12671      void
12672
12673    C++0x Extension:
12674
12675    simple-type-specifier:
12676      auto
12677      decltype ( expression )   
12678      char16_t
12679      char32_t
12680
12681    GNU Extension:
12682
12683    simple-type-specifier:
12684      __int128
12685      __typeof__ unary-expression
12686      __typeof__ ( type-id )
12687
12688    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12689    appropriately updated.  */
12690
12691 static tree
12692 cp_parser_simple_type_specifier (cp_parser* parser,
12693                                  cp_decl_specifier_seq *decl_specs,
12694                                  cp_parser_flags flags)
12695 {
12696   tree type = NULL_TREE;
12697   cp_token *token;
12698
12699   /* Peek at the next token.  */
12700   token = cp_lexer_peek_token (parser->lexer);
12701
12702   /* If we're looking at a keyword, things are easy.  */
12703   switch (token->keyword)
12704     {
12705     case RID_CHAR:
12706       if (decl_specs)
12707         decl_specs->explicit_char_p = true;
12708       type = char_type_node;
12709       break;
12710     case RID_CHAR16:
12711       type = char16_type_node;
12712       break;
12713     case RID_CHAR32:
12714       type = char32_type_node;
12715       break;
12716     case RID_WCHAR:
12717       type = wchar_type_node;
12718       break;
12719     case RID_BOOL:
12720       type = boolean_type_node;
12721       break;
12722     case RID_SHORT:
12723       if (decl_specs)
12724         ++decl_specs->specs[(int) ds_short];
12725       type = short_integer_type_node;
12726       break;
12727     case RID_INT:
12728       if (decl_specs)
12729         decl_specs->explicit_int_p = true;
12730       type = integer_type_node;
12731       break;
12732     case RID_INT128:
12733       if (!int128_integer_type_node)
12734         break;
12735       if (decl_specs)
12736         decl_specs->explicit_int128_p = true;
12737       type = int128_integer_type_node;
12738       break;
12739     case RID_LONG:
12740       if (decl_specs)
12741         ++decl_specs->specs[(int) ds_long];
12742       type = long_integer_type_node;
12743       break;
12744     case RID_SIGNED:
12745       if (decl_specs)
12746         ++decl_specs->specs[(int) ds_signed];
12747       type = integer_type_node;
12748       break;
12749     case RID_UNSIGNED:
12750       if (decl_specs)
12751         ++decl_specs->specs[(int) ds_unsigned];
12752       type = unsigned_type_node;
12753       break;
12754     case RID_FLOAT:
12755       type = float_type_node;
12756       break;
12757     case RID_DOUBLE:
12758       type = double_type_node;
12759       break;
12760     case RID_VOID:
12761       type = void_type_node;
12762       break;
12763       
12764     case RID_AUTO:
12765       maybe_warn_cpp0x (CPP0X_AUTO);
12766       type = make_auto ();
12767       break;
12768
12769     case RID_DECLTYPE:
12770       /* Parse the `decltype' type.  */
12771       type = cp_parser_decltype (parser);
12772
12773       if (decl_specs)
12774         cp_parser_set_decl_spec_type (decl_specs, type,
12775                                       token->location,
12776                                       /*user_defined_p=*/true);
12777
12778       return type;
12779
12780     case RID_TYPEOF:
12781       /* Consume the `typeof' token.  */
12782       cp_lexer_consume_token (parser->lexer);
12783       /* Parse the operand to `typeof'.  */
12784       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12785       /* If it is not already a TYPE, take its type.  */
12786       if (!TYPE_P (type))
12787         type = finish_typeof (type);
12788
12789       if (decl_specs)
12790         cp_parser_set_decl_spec_type (decl_specs, type,
12791                                       token->location,
12792                                       /*user_defined_p=*/true);
12793
12794       return type;
12795
12796     default:
12797       break;
12798     }
12799
12800   /* If the type-specifier was for a built-in type, we're done.  */
12801   if (type)
12802     {
12803       /* Record the type.  */
12804       if (decl_specs
12805           && (token->keyword != RID_SIGNED
12806               && token->keyword != RID_UNSIGNED
12807               && token->keyword != RID_SHORT
12808               && token->keyword != RID_LONG))
12809         cp_parser_set_decl_spec_type (decl_specs,
12810                                       type,
12811                                       token->location,
12812                                       /*user_defined=*/false);
12813       if (decl_specs)
12814         decl_specs->any_specifiers_p = true;
12815
12816       /* Consume the token.  */
12817       cp_lexer_consume_token (parser->lexer);
12818
12819       /* There is no valid C++ program where a non-template type is
12820          followed by a "<".  That usually indicates that the user thought
12821          that the type was a template.  */
12822       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12823
12824       return TYPE_NAME (type);
12825     }
12826
12827   /* The type-specifier must be a user-defined type.  */
12828   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12829     {
12830       bool qualified_p;
12831       bool global_p;
12832
12833       /* Don't gobble tokens or issue error messages if this is an
12834          optional type-specifier.  */
12835       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12836         cp_parser_parse_tentatively (parser);
12837
12838       /* Look for the optional `::' operator.  */
12839       global_p
12840         = (cp_parser_global_scope_opt (parser,
12841                                        /*current_scope_valid_p=*/false)
12842            != NULL_TREE);
12843       /* Look for the nested-name specifier.  */
12844       qualified_p
12845         = (cp_parser_nested_name_specifier_opt (parser,
12846                                                 /*typename_keyword_p=*/false,
12847                                                 /*check_dependency_p=*/true,
12848                                                 /*type_p=*/false,
12849                                                 /*is_declaration=*/false)
12850            != NULL_TREE);
12851       token = cp_lexer_peek_token (parser->lexer);
12852       /* If we have seen a nested-name-specifier, and the next token
12853          is `template', then we are using the template-id production.  */
12854       if (parser->scope
12855           && cp_parser_optional_template_keyword (parser))
12856         {
12857           /* Look for the template-id.  */
12858           type = cp_parser_template_id (parser,
12859                                         /*template_keyword_p=*/true,
12860                                         /*check_dependency_p=*/true,
12861                                         /*is_declaration=*/false);
12862           /* If the template-id did not name a type, we are out of
12863              luck.  */
12864           if (TREE_CODE (type) != TYPE_DECL)
12865             {
12866               cp_parser_error (parser, "expected template-id for type");
12867               type = NULL_TREE;
12868             }
12869         }
12870       /* Otherwise, look for a type-name.  */
12871       else
12872         type = cp_parser_type_name (parser);
12873       /* Keep track of all name-lookups performed in class scopes.  */
12874       if (type
12875           && !global_p
12876           && !qualified_p
12877           && TREE_CODE (type) == TYPE_DECL
12878           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12879         maybe_note_name_used_in_class (DECL_NAME (type), type);
12880       /* If it didn't work out, we don't have a TYPE.  */
12881       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12882           && !cp_parser_parse_definitely (parser))
12883         type = NULL_TREE;
12884       if (type && decl_specs)
12885         cp_parser_set_decl_spec_type (decl_specs, type,
12886                                       token->location,
12887                                       /*user_defined=*/true);
12888     }
12889
12890   /* If we didn't get a type-name, issue an error message.  */
12891   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12892     {
12893       cp_parser_error (parser, "expected type-name");
12894       return error_mark_node;
12895     }
12896
12897   if (type && type != error_mark_node)
12898     {
12899       /* See if TYPE is an Objective-C type, and if so, parse and
12900          accept any protocol references following it.  Do this before
12901          the cp_parser_check_for_invalid_template_id() call, because
12902          Objective-C types can be followed by '<...>' which would
12903          enclose protocol names rather than template arguments, and so
12904          everything is fine.  */
12905       if (c_dialect_objc () && !parser->scope
12906           && (objc_is_id (type) || objc_is_class_name (type)))
12907         {
12908           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12909           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12910
12911           /* Clobber the "unqualified" type previously entered into
12912              DECL_SPECS with the new, improved protocol-qualified version.  */
12913           if (decl_specs)
12914             decl_specs->type = qual_type;
12915
12916           return qual_type;
12917         }
12918
12919       /* There is no valid C++ program where a non-template type is
12920          followed by a "<".  That usually indicates that the user
12921          thought that the type was a template.  */
12922       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12923                                                token->location);
12924     }
12925
12926   return type;
12927 }
12928
12929 /* Parse a type-name.
12930
12931    type-name:
12932      class-name
12933      enum-name
12934      typedef-name
12935
12936    enum-name:
12937      identifier
12938
12939    typedef-name:
12940      identifier
12941
12942    Returns a TYPE_DECL for the type.  */
12943
12944 static tree
12945 cp_parser_type_name (cp_parser* parser)
12946 {
12947   tree type_decl;
12948
12949   /* We can't know yet whether it is a class-name or not.  */
12950   cp_parser_parse_tentatively (parser);
12951   /* Try a class-name.  */
12952   type_decl = cp_parser_class_name (parser,
12953                                     /*typename_keyword_p=*/false,
12954                                     /*template_keyword_p=*/false,
12955                                     none_type,
12956                                     /*check_dependency_p=*/true,
12957                                     /*class_head_p=*/false,
12958                                     /*is_declaration=*/false);
12959   /* If it's not a class-name, keep looking.  */
12960   if (!cp_parser_parse_definitely (parser))
12961     {
12962       /* It must be a typedef-name or an enum-name.  */
12963       return cp_parser_nonclass_name (parser);
12964     }
12965
12966   return type_decl;
12967 }
12968
12969 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12970
12971    enum-name:
12972      identifier
12973
12974    typedef-name:
12975      identifier
12976
12977    Returns a TYPE_DECL for the type.  */
12978
12979 static tree
12980 cp_parser_nonclass_name (cp_parser* parser)
12981 {
12982   tree type_decl;
12983   tree identifier;
12984
12985   cp_token *token = cp_lexer_peek_token (parser->lexer);
12986   identifier = cp_parser_identifier (parser);
12987   if (identifier == error_mark_node)
12988     return error_mark_node;
12989
12990   /* Look up the type-name.  */
12991   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12992
12993   if (TREE_CODE (type_decl) != TYPE_DECL
12994       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12995     {
12996       /* See if this is an Objective-C type.  */
12997       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12998       tree type = objc_get_protocol_qualified_type (identifier, protos);
12999       if (type)
13000         type_decl = TYPE_NAME (type);
13001     }
13002
13003   /* Issue an error if we did not find a type-name.  */
13004   if (TREE_CODE (type_decl) != TYPE_DECL
13005       /* In Objective-C, we have the complication that class names are
13006          normally type names and start declarations (eg, the
13007          "NSObject" in "NSObject *object;"), but can be used in an
13008          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13009          is an expression.  So, a classname followed by a dot is not a
13010          valid type-name.  */
13011       || (objc_is_class_name (TREE_TYPE (type_decl))
13012           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13013     {
13014       if (!cp_parser_simulate_error (parser))
13015         cp_parser_name_lookup_error (parser, identifier, type_decl,
13016                                      NLE_TYPE, token->location);
13017       return error_mark_node;
13018     }
13019   /* Remember that the name was used in the definition of the
13020      current class so that we can check later to see if the
13021      meaning would have been different after the class was
13022      entirely defined.  */
13023   else if (type_decl != error_mark_node
13024            && !parser->scope)
13025     maybe_note_name_used_in_class (identifier, type_decl);
13026   
13027   return type_decl;
13028 }
13029
13030 /* Parse an elaborated-type-specifier.  Note that the grammar given
13031    here incorporates the resolution to DR68.
13032
13033    elaborated-type-specifier:
13034      class-key :: [opt] nested-name-specifier [opt] identifier
13035      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13036      enum-key :: [opt] nested-name-specifier [opt] identifier
13037      typename :: [opt] nested-name-specifier identifier
13038      typename :: [opt] nested-name-specifier template [opt]
13039        template-id
13040
13041    GNU extension:
13042
13043    elaborated-type-specifier:
13044      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13045      class-key attributes :: [opt] nested-name-specifier [opt]
13046                template [opt] template-id
13047      enum attributes :: [opt] nested-name-specifier [opt] identifier
13048
13049    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13050    declared `friend'.  If IS_DECLARATION is TRUE, then this
13051    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13052    something is being declared.
13053
13054    Returns the TYPE specified.  */
13055
13056 static tree
13057 cp_parser_elaborated_type_specifier (cp_parser* parser,
13058                                      bool is_friend,
13059                                      bool is_declaration)
13060 {
13061   enum tag_types tag_type;
13062   tree identifier;
13063   tree type = NULL_TREE;
13064   tree attributes = NULL_TREE;
13065   tree globalscope;
13066   cp_token *token = NULL;
13067
13068   /* See if we're looking at the `enum' keyword.  */
13069   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13070     {
13071       /* Consume the `enum' token.  */
13072       cp_lexer_consume_token (parser->lexer);
13073       /* Remember that it's an enumeration type.  */
13074       tag_type = enum_type;
13075       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13076          enums) is used here.  */
13077       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13078           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13079         {
13080             pedwarn (input_location, 0, "elaborated-type-specifier "
13081                       "for a scoped enum must not use the %<%D%> keyword",
13082                       cp_lexer_peek_token (parser->lexer)->u.value);
13083           /* Consume the `struct' or `class' and parse it anyway.  */
13084           cp_lexer_consume_token (parser->lexer);
13085         }
13086       /* Parse the attributes.  */
13087       attributes = cp_parser_attributes_opt (parser);
13088     }
13089   /* Or, it might be `typename'.  */
13090   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13091                                            RID_TYPENAME))
13092     {
13093       /* Consume the `typename' token.  */
13094       cp_lexer_consume_token (parser->lexer);
13095       /* Remember that it's a `typename' type.  */
13096       tag_type = typename_type;
13097     }
13098   /* Otherwise it must be a class-key.  */
13099   else
13100     {
13101       tag_type = cp_parser_class_key (parser);
13102       if (tag_type == none_type)
13103         return error_mark_node;
13104       /* Parse the attributes.  */
13105       attributes = cp_parser_attributes_opt (parser);
13106     }
13107
13108   /* Look for the `::' operator.  */
13109   globalscope =  cp_parser_global_scope_opt (parser,
13110                                              /*current_scope_valid_p=*/false);
13111   /* Look for the nested-name-specifier.  */
13112   if (tag_type == typename_type && !globalscope)
13113     {
13114       if (!cp_parser_nested_name_specifier (parser,
13115                                            /*typename_keyword_p=*/true,
13116                                            /*check_dependency_p=*/true,
13117                                            /*type_p=*/true,
13118                                             is_declaration))
13119         return error_mark_node;
13120     }
13121   else
13122     /* Even though `typename' is not present, the proposed resolution
13123        to Core Issue 180 says that in `class A<T>::B', `B' should be
13124        considered a type-name, even if `A<T>' is dependent.  */
13125     cp_parser_nested_name_specifier_opt (parser,
13126                                          /*typename_keyword_p=*/true,
13127                                          /*check_dependency_p=*/true,
13128                                          /*type_p=*/true,
13129                                          is_declaration);
13130  /* For everything but enumeration types, consider a template-id.
13131     For an enumeration type, consider only a plain identifier.  */
13132   if (tag_type != enum_type)
13133     {
13134       bool template_p = false;
13135       tree decl;
13136
13137       /* Allow the `template' keyword.  */
13138       template_p = cp_parser_optional_template_keyword (parser);
13139       /* If we didn't see `template', we don't know if there's a
13140          template-id or not.  */
13141       if (!template_p)
13142         cp_parser_parse_tentatively (parser);
13143       /* Parse the template-id.  */
13144       token = cp_lexer_peek_token (parser->lexer);
13145       decl = cp_parser_template_id (parser, template_p,
13146                                     /*check_dependency_p=*/true,
13147                                     is_declaration);
13148       /* If we didn't find a template-id, look for an ordinary
13149          identifier.  */
13150       if (!template_p && !cp_parser_parse_definitely (parser))
13151         ;
13152       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13153          in effect, then we must assume that, upon instantiation, the
13154          template will correspond to a class.  */
13155       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13156                && tag_type == typename_type)
13157         type = make_typename_type (parser->scope, decl,
13158                                    typename_type,
13159                                    /*complain=*/tf_error);
13160       /* If the `typename' keyword is in effect and DECL is not a type
13161          decl. Then type is non existant.   */
13162       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13163         type = NULL_TREE; 
13164       else 
13165         type = TREE_TYPE (decl);
13166     }
13167
13168   if (!type)
13169     {
13170       token = cp_lexer_peek_token (parser->lexer);
13171       identifier = cp_parser_identifier (parser);
13172
13173       if (identifier == error_mark_node)
13174         {
13175           parser->scope = NULL_TREE;
13176           return error_mark_node;
13177         }
13178
13179       /* For a `typename', we needn't call xref_tag.  */
13180       if (tag_type == typename_type
13181           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13182         return cp_parser_make_typename_type (parser, parser->scope,
13183                                              identifier,
13184                                              token->location);
13185       /* Look up a qualified name in the usual way.  */
13186       if (parser->scope)
13187         {
13188           tree decl;
13189           tree ambiguous_decls;
13190
13191           decl = cp_parser_lookup_name (parser, identifier,
13192                                         tag_type,
13193                                         /*is_template=*/false,
13194                                         /*is_namespace=*/false,
13195                                         /*check_dependency=*/true,
13196                                         &ambiguous_decls,
13197                                         token->location);
13198
13199           /* If the lookup was ambiguous, an error will already have been
13200              issued.  */
13201           if (ambiguous_decls)
13202             return error_mark_node;
13203
13204           /* If we are parsing friend declaration, DECL may be a
13205              TEMPLATE_DECL tree node here.  However, we need to check
13206              whether this TEMPLATE_DECL results in valid code.  Consider
13207              the following example:
13208
13209                namespace N {
13210                  template <class T> class C {};
13211                }
13212                class X {
13213                  template <class T> friend class N::C; // #1, valid code
13214                };
13215                template <class T> class Y {
13216                  friend class N::C;                    // #2, invalid code
13217                };
13218
13219              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13220              name lookup of `N::C'.  We see that friend declaration must
13221              be template for the code to be valid.  Note that
13222              processing_template_decl does not work here since it is
13223              always 1 for the above two cases.  */
13224
13225           decl = (cp_parser_maybe_treat_template_as_class
13226                   (decl, /*tag_name_p=*/is_friend
13227                          && parser->num_template_parameter_lists));
13228
13229           if (TREE_CODE (decl) != TYPE_DECL)
13230             {
13231               cp_parser_diagnose_invalid_type_name (parser,
13232                                                     parser->scope,
13233                                                     identifier,
13234                                                     token->location);
13235               return error_mark_node;
13236             }
13237
13238           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13239             {
13240               bool allow_template = (parser->num_template_parameter_lists
13241                                       || DECL_SELF_REFERENCE_P (decl));
13242               type = check_elaborated_type_specifier (tag_type, decl, 
13243                                                       allow_template);
13244
13245               if (type == error_mark_node)
13246                 return error_mark_node;
13247             }
13248
13249           /* Forward declarations of nested types, such as
13250
13251                class C1::C2;
13252                class C1::C2::C3;
13253
13254              are invalid unless all components preceding the final '::'
13255              are complete.  If all enclosing types are complete, these
13256              declarations become merely pointless.
13257
13258              Invalid forward declarations of nested types are errors
13259              caught elsewhere in parsing.  Those that are pointless arrive
13260              here.  */
13261
13262           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13263               && !is_friend && !processing_explicit_instantiation)
13264             warning (0, "declaration %qD does not declare anything", decl);
13265
13266           type = TREE_TYPE (decl);
13267         }
13268       else
13269         {
13270           /* An elaborated-type-specifier sometimes introduces a new type and
13271              sometimes names an existing type.  Normally, the rule is that it
13272              introduces a new type only if there is not an existing type of
13273              the same name already in scope.  For example, given:
13274
13275                struct S {};
13276                void f() { struct S s; }
13277
13278              the `struct S' in the body of `f' is the same `struct S' as in
13279              the global scope; the existing definition is used.  However, if
13280              there were no global declaration, this would introduce a new
13281              local class named `S'.
13282
13283              An exception to this rule applies to the following code:
13284
13285                namespace N { struct S; }
13286
13287              Here, the elaborated-type-specifier names a new type
13288              unconditionally; even if there is already an `S' in the
13289              containing scope this declaration names a new type.
13290              This exception only applies if the elaborated-type-specifier
13291              forms the complete declaration:
13292
13293                [class.name]
13294
13295                A declaration consisting solely of `class-key identifier ;' is
13296                either a redeclaration of the name in the current scope or a
13297                forward declaration of the identifier as a class name.  It
13298                introduces the name into the current scope.
13299
13300              We are in this situation precisely when the next token is a `;'.
13301
13302              An exception to the exception is that a `friend' declaration does
13303              *not* name a new type; i.e., given:
13304
13305                struct S { friend struct T; };
13306
13307              `T' is not a new type in the scope of `S'.
13308
13309              Also, `new struct S' or `sizeof (struct S)' never results in the
13310              definition of a new type; a new type can only be declared in a
13311              declaration context.  */
13312
13313           tag_scope ts;
13314           bool template_p;
13315
13316           if (is_friend)
13317             /* Friends have special name lookup rules.  */
13318             ts = ts_within_enclosing_non_class;
13319           else if (is_declaration
13320                    && cp_lexer_next_token_is (parser->lexer,
13321                                               CPP_SEMICOLON))
13322             /* This is a `class-key identifier ;' */
13323             ts = ts_current;
13324           else
13325             ts = ts_global;
13326
13327           template_p =
13328             (parser->num_template_parameter_lists
13329              && (cp_parser_next_token_starts_class_definition_p (parser)
13330                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13331           /* An unqualified name was used to reference this type, so
13332              there were no qualifying templates.  */
13333           if (!cp_parser_check_template_parameters (parser,
13334                                                     /*num_templates=*/0,
13335                                                     token->location,
13336                                                     /*declarator=*/NULL))
13337             return error_mark_node;
13338           type = xref_tag (tag_type, identifier, ts, template_p);
13339         }
13340     }
13341
13342   if (type == error_mark_node)
13343     return error_mark_node;
13344
13345   /* Allow attributes on forward declarations of classes.  */
13346   if (attributes)
13347     {
13348       if (TREE_CODE (type) == TYPENAME_TYPE)
13349         warning (OPT_Wattributes,
13350                  "attributes ignored on uninstantiated type");
13351       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13352                && ! processing_explicit_instantiation)
13353         warning (OPT_Wattributes,
13354                  "attributes ignored on template instantiation");
13355       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13356         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13357       else
13358         warning (OPT_Wattributes,
13359                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13360     }
13361
13362   if (tag_type != enum_type)
13363     cp_parser_check_class_key (tag_type, type);
13364
13365   /* A "<" cannot follow an elaborated type specifier.  If that
13366      happens, the user was probably trying to form a template-id.  */
13367   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13368
13369   return type;
13370 }
13371
13372 /* Parse an enum-specifier.
13373
13374    enum-specifier:
13375      enum-head { enumerator-list [opt] }
13376
13377    enum-head:
13378      enum-key identifier [opt] enum-base [opt]
13379      enum-key nested-name-specifier identifier enum-base [opt]
13380
13381    enum-key:
13382      enum
13383      enum class   [C++0x]
13384      enum struct  [C++0x]
13385
13386    enum-base:   [C++0x]
13387      : type-specifier-seq
13388
13389    opaque-enum-specifier:
13390      enum-key identifier enum-base [opt] ;
13391
13392    GNU Extensions:
13393      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13394        { enumerator-list [opt] }attributes[opt]
13395
13396    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13397    if the token stream isn't an enum-specifier after all.  */
13398
13399 static tree
13400 cp_parser_enum_specifier (cp_parser* parser)
13401 {
13402   tree identifier;
13403   tree type = NULL_TREE;
13404   tree prev_scope;
13405   tree nested_name_specifier = NULL_TREE;
13406   tree attributes;
13407   bool scoped_enum_p = false;
13408   bool has_underlying_type = false;
13409   bool nested_being_defined = false;
13410   bool new_value_list = false;
13411   bool is_new_type = false;
13412   bool is_anonymous = false;
13413   tree underlying_type = NULL_TREE;
13414   cp_token *type_start_token = NULL;
13415   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13416
13417   parser->colon_corrects_to_scope_p = false;
13418
13419   /* Parse tentatively so that we can back up if we don't find a
13420      enum-specifier.  */
13421   cp_parser_parse_tentatively (parser);
13422
13423   /* Caller guarantees that the current token is 'enum', an identifier
13424      possibly follows, and the token after that is an opening brace.
13425      If we don't have an identifier, fabricate an anonymous name for
13426      the enumeration being defined.  */
13427   cp_lexer_consume_token (parser->lexer);
13428
13429   /* Parse the "class" or "struct", which indicates a scoped
13430      enumeration type in C++0x.  */
13431   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13432       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13433     {
13434       if (cxx_dialect < cxx0x)
13435         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13436
13437       /* Consume the `struct' or `class' token.  */
13438       cp_lexer_consume_token (parser->lexer);
13439
13440       scoped_enum_p = true;
13441     }
13442
13443   attributes = cp_parser_attributes_opt (parser);
13444
13445   /* Clear the qualification.  */
13446   parser->scope = NULL_TREE;
13447   parser->qualifying_scope = NULL_TREE;
13448   parser->object_scope = NULL_TREE;
13449
13450   /* Figure out in what scope the declaration is being placed.  */
13451   prev_scope = current_scope ();
13452
13453   type_start_token = cp_lexer_peek_token (parser->lexer);
13454
13455   push_deferring_access_checks (dk_no_check);
13456   nested_name_specifier
13457       = cp_parser_nested_name_specifier_opt (parser,
13458                                              /*typename_keyword_p=*/true,
13459                                              /*check_dependency_p=*/false,
13460                                              /*type_p=*/false,
13461                                              /*is_declaration=*/false);
13462
13463   if (nested_name_specifier)
13464     {
13465       tree name;
13466
13467       identifier = cp_parser_identifier (parser);
13468       name =  cp_parser_lookup_name (parser, identifier,
13469                                      enum_type,
13470                                      /*is_template=*/false,
13471                                      /*is_namespace=*/false,
13472                                      /*check_dependency=*/true,
13473                                      /*ambiguous_decls=*/NULL,
13474                                      input_location);
13475       if (name)
13476         {
13477           type = TREE_TYPE (name);
13478           if (TREE_CODE (type) == TYPENAME_TYPE)
13479             {
13480               /* Are template enums allowed in ISO? */
13481               if (template_parm_scope_p ())
13482                 pedwarn (type_start_token->location, OPT_pedantic,
13483                          "%qD is an enumeration template", name);
13484               /* ignore a typename reference, for it will be solved by name
13485                  in start_enum.  */
13486               type = NULL_TREE;
13487             }
13488         }
13489       else
13490         error_at (type_start_token->location,
13491                   "%qD is not an enumerator-name", identifier);
13492     }
13493   else
13494     {
13495       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13496         identifier = cp_parser_identifier (parser);
13497       else
13498         {
13499           identifier = make_anon_name ();
13500           is_anonymous = true;
13501         }
13502     }
13503   pop_deferring_access_checks ();
13504
13505   /* Check for the `:' that denotes a specified underlying type in C++0x.
13506      Note that a ':' could also indicate a bitfield width, however.  */
13507   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13508     {
13509       cp_decl_specifier_seq type_specifiers;
13510
13511       /* Consume the `:'.  */
13512       cp_lexer_consume_token (parser->lexer);
13513
13514       /* Parse the type-specifier-seq.  */
13515       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13516                                     /*is_trailing_return=*/false,
13517                                     &type_specifiers);
13518
13519       /* At this point this is surely not elaborated type specifier.  */
13520       if (!cp_parser_parse_definitely (parser))
13521         return NULL_TREE;
13522
13523       if (cxx_dialect < cxx0x)
13524         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13525
13526       has_underlying_type = true;
13527
13528       /* If that didn't work, stop.  */
13529       if (type_specifiers.type != error_mark_node)
13530         {
13531           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13532                                             /*initialized=*/0, NULL);
13533           if (underlying_type == error_mark_node)
13534             underlying_type = NULL_TREE;
13535         }
13536     }
13537
13538   /* Look for the `{' but don't consume it yet.  */
13539   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13540     {
13541       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13542         {
13543           cp_parser_error (parser, "expected %<{%>");
13544           if (has_underlying_type)
13545             {
13546               type = NULL_TREE;
13547               goto out;
13548             }
13549         }
13550       /* An opaque-enum-specifier must have a ';' here.  */
13551       if ((scoped_enum_p || underlying_type)
13552           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13553         {
13554           cp_parser_error (parser, "expected %<;%> or %<{%>");
13555           if (has_underlying_type)
13556             {
13557               type = NULL_TREE;
13558               goto out;
13559             }
13560         }
13561     }
13562
13563   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13564     return NULL_TREE;
13565
13566   if (nested_name_specifier)
13567     {
13568       if (CLASS_TYPE_P (nested_name_specifier))
13569         {
13570           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13571           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13572           push_scope (nested_name_specifier);
13573         }
13574       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13575         {
13576           push_nested_namespace (nested_name_specifier);
13577         }
13578     }
13579
13580   /* Issue an error message if type-definitions are forbidden here.  */
13581   if (!cp_parser_check_type_definition (parser))
13582     type = error_mark_node;
13583   else
13584     /* Create the new type.  We do this before consuming the opening
13585        brace so the enum will be recorded as being on the line of its
13586        tag (or the 'enum' keyword, if there is no tag).  */
13587     type = start_enum (identifier, type, underlying_type,
13588                        scoped_enum_p, &is_new_type);
13589
13590   /* If the next token is not '{' it is an opaque-enum-specifier or an
13591      elaborated-type-specifier.  */
13592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13593     {
13594       if (nested_name_specifier)
13595         {
13596           /* The following catches invalid code such as:
13597              enum class S<int>::E { A, B, C }; */
13598           if (!processing_specialization
13599               && CLASS_TYPE_P (nested_name_specifier)
13600               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13601             error_at (type_start_token->location, "cannot add an enumerator "
13602                       "list to a template instantiation");
13603
13604           /* If that scope does not contain the scope in which the
13605              class was originally declared, the program is invalid.  */
13606           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13607             {
13608               if (at_namespace_scope_p ())
13609                 error_at (type_start_token->location,
13610                           "declaration of %qD in namespace %qD which does not "
13611                           "enclose %qD",
13612                           type, prev_scope, nested_name_specifier);
13613               else
13614                 error_at (type_start_token->location,
13615                           "declaration of %qD in %qD which does not enclose %qD",
13616                           type, prev_scope, nested_name_specifier);
13617               type = error_mark_node;
13618             }
13619         }
13620
13621       if (scoped_enum_p)
13622         begin_scope (sk_scoped_enum, type);
13623
13624       /* Consume the opening brace.  */
13625       cp_lexer_consume_token (parser->lexer);
13626
13627       if (type == error_mark_node)
13628         ; /* Nothing to add */
13629       else if (OPAQUE_ENUM_P (type)
13630                || (cxx_dialect > cxx98 && processing_specialization))
13631         {
13632           new_value_list = true;
13633           SET_OPAQUE_ENUM_P (type, false);
13634           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13635         }
13636       else
13637         {
13638           error_at (type_start_token->location, "multiple definition of %q#T", type);
13639           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13640                     "previous definition here");
13641           type = error_mark_node;
13642         }
13643
13644       if (type == error_mark_node)
13645         cp_parser_skip_to_end_of_block_or_statement (parser);
13646       /* If the next token is not '}', then there are some enumerators.  */
13647       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13648         cp_parser_enumerator_list (parser, type);
13649
13650       /* Consume the final '}'.  */
13651       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13652
13653       if (scoped_enum_p)
13654         finish_scope ();
13655     }
13656   else
13657     {
13658       /* If a ';' follows, then it is an opaque-enum-specifier
13659         and additional restrictions apply.  */
13660       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13661         {
13662           if (is_anonymous)
13663             error_at (type_start_token->location,
13664                       "opaque-enum-specifier without name");
13665           else if (nested_name_specifier)
13666             error_at (type_start_token->location,
13667                       "opaque-enum-specifier must use a simple identifier");
13668         }
13669     }
13670
13671   /* Look for trailing attributes to apply to this enumeration, and
13672      apply them if appropriate.  */
13673   if (cp_parser_allow_gnu_extensions_p (parser))
13674     {
13675       tree trailing_attr = cp_parser_attributes_opt (parser);
13676       trailing_attr = chainon (trailing_attr, attributes);
13677       cplus_decl_attributes (&type,
13678                              trailing_attr,
13679                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13680     }
13681
13682   /* Finish up the enumeration.  */
13683   if (type != error_mark_node)
13684     {
13685       if (new_value_list)
13686         finish_enum_value_list (type);
13687       if (is_new_type)
13688         finish_enum (type);
13689     }
13690
13691   if (nested_name_specifier)
13692     {
13693       if (CLASS_TYPE_P (nested_name_specifier))
13694         {
13695           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13696           pop_scope (nested_name_specifier);
13697         }
13698       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13699         {
13700           pop_nested_namespace (nested_name_specifier);
13701         }
13702     }
13703  out:
13704   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13705   return type;
13706 }
13707
13708 /* Parse an enumerator-list.  The enumerators all have the indicated
13709    TYPE.
13710
13711    enumerator-list:
13712      enumerator-definition
13713      enumerator-list , enumerator-definition  */
13714
13715 static void
13716 cp_parser_enumerator_list (cp_parser* parser, tree type)
13717 {
13718   while (true)
13719     {
13720       /* Parse an enumerator-definition.  */
13721       cp_parser_enumerator_definition (parser, type);
13722
13723       /* If the next token is not a ',', we've reached the end of
13724          the list.  */
13725       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13726         break;
13727       /* Otherwise, consume the `,' and keep going.  */
13728       cp_lexer_consume_token (parser->lexer);
13729       /* If the next token is a `}', there is a trailing comma.  */
13730       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13731         {
13732           if (!in_system_header)
13733             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13734           break;
13735         }
13736     }
13737 }
13738
13739 /* Parse an enumerator-definition.  The enumerator has the indicated
13740    TYPE.
13741
13742    enumerator-definition:
13743      enumerator
13744      enumerator = constant-expression
13745
13746    enumerator:
13747      identifier  */
13748
13749 static void
13750 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13751 {
13752   tree identifier;
13753   tree value;
13754   location_t loc;
13755
13756   /* Save the input location because we are interested in the location
13757      of the identifier and not the location of the explicit value.  */
13758   loc = cp_lexer_peek_token (parser->lexer)->location;
13759
13760   /* Look for the identifier.  */
13761   identifier = cp_parser_identifier (parser);
13762   if (identifier == error_mark_node)
13763     return;
13764
13765   /* If the next token is an '=', then there is an explicit value.  */
13766   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13767     {
13768       /* Consume the `=' token.  */
13769       cp_lexer_consume_token (parser->lexer);
13770       /* Parse the value.  */
13771       value = cp_parser_constant_expression (parser,
13772                                              /*allow_non_constant_p=*/false,
13773                                              NULL);
13774     }
13775   else
13776     value = NULL_TREE;
13777
13778   /* If we are processing a template, make sure the initializer of the
13779      enumerator doesn't contain any bare template parameter pack.  */
13780   if (check_for_bare_parameter_packs (value))
13781     value = error_mark_node;
13782
13783   /* integral_constant_value will pull out this expression, so make sure
13784      it's folded as appropriate.  */
13785   value = fold_non_dependent_expr (value);
13786
13787   /* Create the enumerator.  */
13788   build_enumerator (identifier, value, type, loc);
13789 }
13790
13791 /* Parse a namespace-name.
13792
13793    namespace-name:
13794      original-namespace-name
13795      namespace-alias
13796
13797    Returns the NAMESPACE_DECL for the namespace.  */
13798
13799 static tree
13800 cp_parser_namespace_name (cp_parser* parser)
13801 {
13802   tree identifier;
13803   tree namespace_decl;
13804
13805   cp_token *token = cp_lexer_peek_token (parser->lexer);
13806
13807   /* Get the name of the namespace.  */
13808   identifier = cp_parser_identifier (parser);
13809   if (identifier == error_mark_node)
13810     return error_mark_node;
13811
13812   /* Look up the identifier in the currently active scope.  Look only
13813      for namespaces, due to:
13814
13815        [basic.lookup.udir]
13816
13817        When looking up a namespace-name in a using-directive or alias
13818        definition, only namespace names are considered.
13819
13820      And:
13821
13822        [basic.lookup.qual]
13823
13824        During the lookup of a name preceding the :: scope resolution
13825        operator, object, function, and enumerator names are ignored.
13826
13827      (Note that cp_parser_qualifying_entity only calls this
13828      function if the token after the name is the scope resolution
13829      operator.)  */
13830   namespace_decl = cp_parser_lookup_name (parser, identifier,
13831                                           none_type,
13832                                           /*is_template=*/false,
13833                                           /*is_namespace=*/true,
13834                                           /*check_dependency=*/true,
13835                                           /*ambiguous_decls=*/NULL,
13836                                           token->location);
13837   /* If it's not a namespace, issue an error.  */
13838   if (namespace_decl == error_mark_node
13839       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13840     {
13841       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13842         error_at (token->location, "%qD is not a namespace-name", identifier);
13843       cp_parser_error (parser, "expected namespace-name");
13844       namespace_decl = error_mark_node;
13845     }
13846
13847   return namespace_decl;
13848 }
13849
13850 /* Parse a namespace-definition.
13851
13852    namespace-definition:
13853      named-namespace-definition
13854      unnamed-namespace-definition
13855
13856    named-namespace-definition:
13857      original-namespace-definition
13858      extension-namespace-definition
13859
13860    original-namespace-definition:
13861      namespace identifier { namespace-body }
13862
13863    extension-namespace-definition:
13864      namespace original-namespace-name { namespace-body }
13865
13866    unnamed-namespace-definition:
13867      namespace { namespace-body } */
13868
13869 static void
13870 cp_parser_namespace_definition (cp_parser* parser)
13871 {
13872   tree identifier, attribs;
13873   bool has_visibility;
13874   bool is_inline;
13875
13876   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13877     {
13878       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13879       is_inline = true;
13880       cp_lexer_consume_token (parser->lexer);
13881     }
13882   else
13883     is_inline = false;
13884
13885   /* Look for the `namespace' keyword.  */
13886   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13887
13888   /* Get the name of the namespace.  We do not attempt to distinguish
13889      between an original-namespace-definition and an
13890      extension-namespace-definition at this point.  The semantic
13891      analysis routines are responsible for that.  */
13892   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13893     identifier = cp_parser_identifier (parser);
13894   else
13895     identifier = NULL_TREE;
13896
13897   /* Parse any specified attributes.  */
13898   attribs = cp_parser_attributes_opt (parser);
13899
13900   /* Look for the `{' to start the namespace.  */
13901   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13902   /* Start the namespace.  */
13903   push_namespace (identifier);
13904
13905   /* "inline namespace" is equivalent to a stub namespace definition
13906      followed by a strong using directive.  */
13907   if (is_inline)
13908     {
13909       tree name_space = current_namespace;
13910       /* Set up namespace association.  */
13911       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13912         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13913                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13914       /* Import the contents of the inline namespace.  */
13915       pop_namespace ();
13916       do_using_directive (name_space);
13917       push_namespace (identifier);
13918     }
13919
13920   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13921
13922   /* Parse the body of the namespace.  */
13923   cp_parser_namespace_body (parser);
13924
13925   if (has_visibility)
13926     pop_visibility (1);
13927
13928   /* Finish the namespace.  */
13929   pop_namespace ();
13930   /* Look for the final `}'.  */
13931   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13932 }
13933
13934 /* Parse a namespace-body.
13935
13936    namespace-body:
13937      declaration-seq [opt]  */
13938
13939 static void
13940 cp_parser_namespace_body (cp_parser* parser)
13941 {
13942   cp_parser_declaration_seq_opt (parser);
13943 }
13944
13945 /* Parse a namespace-alias-definition.
13946
13947    namespace-alias-definition:
13948      namespace identifier = qualified-namespace-specifier ;  */
13949
13950 static void
13951 cp_parser_namespace_alias_definition (cp_parser* parser)
13952 {
13953   tree identifier;
13954   tree namespace_specifier;
13955
13956   cp_token *token = cp_lexer_peek_token (parser->lexer);
13957
13958   /* Look for the `namespace' keyword.  */
13959   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13960   /* Look for the identifier.  */
13961   identifier = cp_parser_identifier (parser);
13962   if (identifier == error_mark_node)
13963     return;
13964   /* Look for the `=' token.  */
13965   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13966       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13967     {
13968       error_at (token->location, "%<namespace%> definition is not allowed here");
13969       /* Skip the definition.  */
13970       cp_lexer_consume_token (parser->lexer);
13971       if (cp_parser_skip_to_closing_brace (parser))
13972         cp_lexer_consume_token (parser->lexer);
13973       return;
13974     }
13975   cp_parser_require (parser, CPP_EQ, RT_EQ);
13976   /* Look for the qualified-namespace-specifier.  */
13977   namespace_specifier
13978     = cp_parser_qualified_namespace_specifier (parser);
13979   /* Look for the `;' token.  */
13980   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13981
13982   /* Register the alias in the symbol table.  */
13983   do_namespace_alias (identifier, namespace_specifier);
13984 }
13985
13986 /* Parse a qualified-namespace-specifier.
13987
13988    qualified-namespace-specifier:
13989      :: [opt] nested-name-specifier [opt] namespace-name
13990
13991    Returns a NAMESPACE_DECL corresponding to the specified
13992    namespace.  */
13993
13994 static tree
13995 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13996 {
13997   /* Look for the optional `::'.  */
13998   cp_parser_global_scope_opt (parser,
13999                               /*current_scope_valid_p=*/false);
14000
14001   /* Look for the optional nested-name-specifier.  */
14002   cp_parser_nested_name_specifier_opt (parser,
14003                                        /*typename_keyword_p=*/false,
14004                                        /*check_dependency_p=*/true,
14005                                        /*type_p=*/false,
14006                                        /*is_declaration=*/true);
14007
14008   return cp_parser_namespace_name (parser);
14009 }
14010
14011 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14012    access declaration.
14013
14014    using-declaration:
14015      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14016      using :: unqualified-id ;  
14017
14018    access-declaration:
14019      qualified-id ;  
14020
14021    */
14022
14023 static bool
14024 cp_parser_using_declaration (cp_parser* parser, 
14025                              bool access_declaration_p)
14026 {
14027   cp_token *token;
14028   bool typename_p = false;
14029   bool global_scope_p;
14030   tree decl;
14031   tree identifier;
14032   tree qscope;
14033
14034   if (access_declaration_p)
14035     cp_parser_parse_tentatively (parser);
14036   else
14037     {
14038       /* Look for the `using' keyword.  */
14039       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14040       
14041       /* Peek at the next token.  */
14042       token = cp_lexer_peek_token (parser->lexer);
14043       /* See if it's `typename'.  */
14044       if (token->keyword == RID_TYPENAME)
14045         {
14046           /* Remember that we've seen it.  */
14047           typename_p = true;
14048           /* Consume the `typename' token.  */
14049           cp_lexer_consume_token (parser->lexer);
14050         }
14051     }
14052
14053   /* Look for the optional global scope qualification.  */
14054   global_scope_p
14055     = (cp_parser_global_scope_opt (parser,
14056                                    /*current_scope_valid_p=*/false)
14057        != NULL_TREE);
14058
14059   /* If we saw `typename', or didn't see `::', then there must be a
14060      nested-name-specifier present.  */
14061   if (typename_p || !global_scope_p)
14062     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14063                                               /*check_dependency_p=*/true,
14064                                               /*type_p=*/false,
14065                                               /*is_declaration=*/true);
14066   /* Otherwise, we could be in either of the two productions.  In that
14067      case, treat the nested-name-specifier as optional.  */
14068   else
14069     qscope = cp_parser_nested_name_specifier_opt (parser,
14070                                                   /*typename_keyword_p=*/false,
14071                                                   /*check_dependency_p=*/true,
14072                                                   /*type_p=*/false,
14073                                                   /*is_declaration=*/true);
14074   if (!qscope)
14075     qscope = global_namespace;
14076
14077   if (access_declaration_p && cp_parser_error_occurred (parser))
14078     /* Something has already gone wrong; there's no need to parse
14079        further.  Since an error has occurred, the return value of
14080        cp_parser_parse_definitely will be false, as required.  */
14081     return cp_parser_parse_definitely (parser);
14082
14083   token = cp_lexer_peek_token (parser->lexer);
14084   /* Parse the unqualified-id.  */
14085   identifier = cp_parser_unqualified_id (parser,
14086                                          /*template_keyword_p=*/false,
14087                                          /*check_dependency_p=*/true,
14088                                          /*declarator_p=*/true,
14089                                          /*optional_p=*/false);
14090
14091   if (access_declaration_p)
14092     {
14093       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14094         cp_parser_simulate_error (parser);
14095       if (!cp_parser_parse_definitely (parser))
14096         return false;
14097     }
14098
14099   /* The function we call to handle a using-declaration is different
14100      depending on what scope we are in.  */
14101   if (qscope == error_mark_node || identifier == error_mark_node)
14102     ;
14103   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14104            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14105     /* [namespace.udecl]
14106
14107        A using declaration shall not name a template-id.  */
14108     error_at (token->location,
14109               "a template-id may not appear in a using-declaration");
14110   else
14111     {
14112       if (at_class_scope_p ())
14113         {
14114           /* Create the USING_DECL.  */
14115           decl = do_class_using_decl (parser->scope, identifier);
14116
14117           if (check_for_bare_parameter_packs (decl))
14118             return false;
14119           else
14120             /* Add it to the list of members in this class.  */
14121             finish_member_declaration (decl);
14122         }
14123       else
14124         {
14125           decl = cp_parser_lookup_name_simple (parser,
14126                                                identifier,
14127                                                token->location);
14128           if (decl == error_mark_node)
14129             cp_parser_name_lookup_error (parser, identifier,
14130                                          decl, NLE_NULL,
14131                                          token->location);
14132           else if (check_for_bare_parameter_packs (decl))
14133             return false;
14134           else if (!at_namespace_scope_p ())
14135             do_local_using_decl (decl, qscope, identifier);
14136           else
14137             do_toplevel_using_decl (decl, qscope, identifier);
14138         }
14139     }
14140
14141   /* Look for the final `;'.  */
14142   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14143   
14144   return true;
14145 }
14146
14147 /* Parse a using-directive.
14148
14149    using-directive:
14150      using namespace :: [opt] nested-name-specifier [opt]
14151        namespace-name ;  */
14152
14153 static void
14154 cp_parser_using_directive (cp_parser* parser)
14155 {
14156   tree namespace_decl;
14157   tree attribs;
14158
14159   /* Look for the `using' keyword.  */
14160   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14161   /* And the `namespace' keyword.  */
14162   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14163   /* Look for the optional `::' operator.  */
14164   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14165   /* And the optional nested-name-specifier.  */
14166   cp_parser_nested_name_specifier_opt (parser,
14167                                        /*typename_keyword_p=*/false,
14168                                        /*check_dependency_p=*/true,
14169                                        /*type_p=*/false,
14170                                        /*is_declaration=*/true);
14171   /* Get the namespace being used.  */
14172   namespace_decl = cp_parser_namespace_name (parser);
14173   /* And any specified attributes.  */
14174   attribs = cp_parser_attributes_opt (parser);
14175   /* Update the symbol table.  */
14176   parse_using_directive (namespace_decl, attribs);
14177   /* Look for the final `;'.  */
14178   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14179 }
14180
14181 /* Parse an asm-definition.
14182
14183    asm-definition:
14184      asm ( string-literal ) ;
14185
14186    GNU Extension:
14187
14188    asm-definition:
14189      asm volatile [opt] ( string-literal ) ;
14190      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14191      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14192                           : asm-operand-list [opt] ) ;
14193      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14194                           : asm-operand-list [opt]
14195                           : asm-clobber-list [opt] ) ;
14196      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14197                                : asm-clobber-list [opt]
14198                                : asm-goto-list ) ;  */
14199
14200 static void
14201 cp_parser_asm_definition (cp_parser* parser)
14202 {
14203   tree string;
14204   tree outputs = NULL_TREE;
14205   tree inputs = NULL_TREE;
14206   tree clobbers = NULL_TREE;
14207   tree labels = NULL_TREE;
14208   tree asm_stmt;
14209   bool volatile_p = false;
14210   bool extended_p = false;
14211   bool invalid_inputs_p = false;
14212   bool invalid_outputs_p = false;
14213   bool goto_p = false;
14214   required_token missing = RT_NONE;
14215
14216   /* Look for the `asm' keyword.  */
14217   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14218   /* See if the next token is `volatile'.  */
14219   if (cp_parser_allow_gnu_extensions_p (parser)
14220       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14221     {
14222       /* Remember that we saw the `volatile' keyword.  */
14223       volatile_p = true;
14224       /* Consume the token.  */
14225       cp_lexer_consume_token (parser->lexer);
14226     }
14227   if (cp_parser_allow_gnu_extensions_p (parser)
14228       && parser->in_function_body
14229       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14230     {
14231       /* Remember that we saw the `goto' keyword.  */
14232       goto_p = true;
14233       /* Consume the token.  */
14234       cp_lexer_consume_token (parser->lexer);
14235     }
14236   /* Look for the opening `('.  */
14237   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14238     return;
14239   /* Look for the string.  */
14240   string = cp_parser_string_literal (parser, false, false);
14241   if (string == error_mark_node)
14242     {
14243       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14244                                              /*consume_paren=*/true);
14245       return;
14246     }
14247
14248   /* If we're allowing GNU extensions, check for the extended assembly
14249      syntax.  Unfortunately, the `:' tokens need not be separated by
14250      a space in C, and so, for compatibility, we tolerate that here
14251      too.  Doing that means that we have to treat the `::' operator as
14252      two `:' tokens.  */
14253   if (cp_parser_allow_gnu_extensions_p (parser)
14254       && parser->in_function_body
14255       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14256           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14257     {
14258       bool inputs_p = false;
14259       bool clobbers_p = false;
14260       bool labels_p = false;
14261
14262       /* The extended syntax was used.  */
14263       extended_p = true;
14264
14265       /* Look for outputs.  */
14266       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14267         {
14268           /* Consume the `:'.  */
14269           cp_lexer_consume_token (parser->lexer);
14270           /* Parse the output-operands.  */
14271           if (cp_lexer_next_token_is_not (parser->lexer,
14272                                           CPP_COLON)
14273               && cp_lexer_next_token_is_not (parser->lexer,
14274                                              CPP_SCOPE)
14275               && cp_lexer_next_token_is_not (parser->lexer,
14276                                              CPP_CLOSE_PAREN)
14277               && !goto_p)
14278             outputs = cp_parser_asm_operand_list (parser);
14279
14280             if (outputs == error_mark_node)
14281               invalid_outputs_p = true;
14282         }
14283       /* If the next token is `::', there are no outputs, and the
14284          next token is the beginning of the inputs.  */
14285       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14286         /* The inputs are coming next.  */
14287         inputs_p = true;
14288
14289       /* Look for inputs.  */
14290       if (inputs_p
14291           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14292         {
14293           /* Consume the `:' or `::'.  */
14294           cp_lexer_consume_token (parser->lexer);
14295           /* Parse the output-operands.  */
14296           if (cp_lexer_next_token_is_not (parser->lexer,
14297                                           CPP_COLON)
14298               && cp_lexer_next_token_is_not (parser->lexer,
14299                                              CPP_SCOPE)
14300               && cp_lexer_next_token_is_not (parser->lexer,
14301                                              CPP_CLOSE_PAREN))
14302             inputs = cp_parser_asm_operand_list (parser);
14303
14304             if (inputs == error_mark_node)
14305               invalid_inputs_p = true;
14306         }
14307       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14308         /* The clobbers are coming next.  */
14309         clobbers_p = true;
14310
14311       /* Look for clobbers.  */
14312       if (clobbers_p
14313           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14314         {
14315           clobbers_p = true;
14316           /* Consume the `:' or `::'.  */
14317           cp_lexer_consume_token (parser->lexer);
14318           /* Parse the clobbers.  */
14319           if (cp_lexer_next_token_is_not (parser->lexer,
14320                                           CPP_COLON)
14321               && cp_lexer_next_token_is_not (parser->lexer,
14322                                              CPP_CLOSE_PAREN))
14323             clobbers = cp_parser_asm_clobber_list (parser);
14324         }
14325       else if (goto_p
14326                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14327         /* The labels are coming next.  */
14328         labels_p = true;
14329
14330       /* Look for labels.  */
14331       if (labels_p
14332           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14333         {
14334           labels_p = true;
14335           /* Consume the `:' or `::'.  */
14336           cp_lexer_consume_token (parser->lexer);
14337           /* Parse the labels.  */
14338           labels = cp_parser_asm_label_list (parser);
14339         }
14340
14341       if (goto_p && !labels_p)
14342         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14343     }
14344   else if (goto_p)
14345     missing = RT_COLON_SCOPE;
14346
14347   /* Look for the closing `)'.  */
14348   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14349                           missing ? missing : RT_CLOSE_PAREN))
14350     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14351                                            /*consume_paren=*/true);
14352   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14353
14354   if (!invalid_inputs_p && !invalid_outputs_p)
14355     {
14356       /* Create the ASM_EXPR.  */
14357       if (parser->in_function_body)
14358         {
14359           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14360                                       inputs, clobbers, labels);
14361           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14362           if (!extended_p)
14363             {
14364               tree temp = asm_stmt;
14365               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14366                 temp = TREE_OPERAND (temp, 0);
14367
14368               ASM_INPUT_P (temp) = 1;
14369             }
14370         }
14371       else
14372         cgraph_add_asm_node (string);
14373     }
14374 }
14375
14376 /* Declarators [gram.dcl.decl] */
14377
14378 /* Parse an init-declarator.
14379
14380    init-declarator:
14381      declarator initializer [opt]
14382
14383    GNU Extension:
14384
14385    init-declarator:
14386      declarator asm-specification [opt] attributes [opt] initializer [opt]
14387
14388    function-definition:
14389      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14390        function-body
14391      decl-specifier-seq [opt] declarator function-try-block
14392
14393    GNU Extension:
14394
14395    function-definition:
14396      __extension__ function-definition
14397
14398    The DECL_SPECIFIERS apply to this declarator.  Returns a
14399    representation of the entity declared.  If MEMBER_P is TRUE, then
14400    this declarator appears in a class scope.  The new DECL created by
14401    this declarator is returned.
14402
14403    The CHECKS are access checks that should be performed once we know
14404    what entity is being declared (and, therefore, what classes have
14405    befriended it).
14406
14407    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14408    for a function-definition here as well.  If the declarator is a
14409    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14410    be TRUE upon return.  By that point, the function-definition will
14411    have been completely parsed.
14412
14413    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14414    is FALSE.
14415
14416    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14417    parsed declaration if it is an uninitialized single declarator not followed
14418    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14419    if present, will not be consumed.  If returned, this declarator will be
14420    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14421
14422 static tree
14423 cp_parser_init_declarator (cp_parser* parser,
14424                            cp_decl_specifier_seq *decl_specifiers,
14425                            VEC (deferred_access_check,gc)* checks,
14426                            bool function_definition_allowed_p,
14427                            bool member_p,
14428                            int declares_class_or_enum,
14429                            bool* function_definition_p,
14430                            tree* maybe_range_for_decl)
14431 {
14432   cp_token *token = NULL, *asm_spec_start_token = NULL,
14433            *attributes_start_token = NULL;
14434   cp_declarator *declarator;
14435   tree prefix_attributes;
14436   tree attributes;
14437   tree asm_specification;
14438   tree initializer;
14439   tree decl = NULL_TREE;
14440   tree scope;
14441   int is_initialized;
14442   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14443      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14444      "(...)".  */
14445   enum cpp_ttype initialization_kind;
14446   bool is_direct_init = false;
14447   bool is_non_constant_init;
14448   int ctor_dtor_or_conv_p;
14449   bool friend_p;
14450   tree pushed_scope = NULL;
14451   bool range_for_decl_p = false;
14452
14453   /* Gather the attributes that were provided with the
14454      decl-specifiers.  */
14455   prefix_attributes = decl_specifiers->attributes;
14456
14457   /* Assume that this is not the declarator for a function
14458      definition.  */
14459   if (function_definition_p)
14460     *function_definition_p = false;
14461
14462   /* Defer access checks while parsing the declarator; we cannot know
14463      what names are accessible until we know what is being
14464      declared.  */
14465   resume_deferring_access_checks ();
14466
14467   /* Parse the declarator.  */
14468   token = cp_lexer_peek_token (parser->lexer);
14469   declarator
14470     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14471                             &ctor_dtor_or_conv_p,
14472                             /*parenthesized_p=*/NULL,
14473                             /*member_p=*/false);
14474   /* Gather up the deferred checks.  */
14475   stop_deferring_access_checks ();
14476
14477   /* If the DECLARATOR was erroneous, there's no need to go
14478      further.  */
14479   if (declarator == cp_error_declarator)
14480     return error_mark_node;
14481
14482   /* Check that the number of template-parameter-lists is OK.  */
14483   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14484                                                        token->location))
14485     return error_mark_node;
14486
14487   if (declares_class_or_enum & 2)
14488     cp_parser_check_for_definition_in_return_type (declarator,
14489                                                    decl_specifiers->type,
14490                                                    decl_specifiers->type_location);
14491
14492   /* Figure out what scope the entity declared by the DECLARATOR is
14493      located in.  `grokdeclarator' sometimes changes the scope, so
14494      we compute it now.  */
14495   scope = get_scope_of_declarator (declarator);
14496
14497   /* Perform any lookups in the declared type which were thought to be
14498      dependent, but are not in the scope of the declarator.  */
14499   decl_specifiers->type
14500     = maybe_update_decl_type (decl_specifiers->type, scope);
14501
14502   /* If we're allowing GNU extensions, look for an asm-specification
14503      and attributes.  */
14504   if (cp_parser_allow_gnu_extensions_p (parser))
14505     {
14506       /* Look for an asm-specification.  */
14507       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14508       asm_specification = cp_parser_asm_specification_opt (parser);
14509       /* And attributes.  */
14510       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14511       attributes = cp_parser_attributes_opt (parser);
14512     }
14513   else
14514     {
14515       asm_specification = NULL_TREE;
14516       attributes = NULL_TREE;
14517     }
14518
14519   /* Peek at the next token.  */
14520   token = cp_lexer_peek_token (parser->lexer);
14521   /* Check to see if the token indicates the start of a
14522      function-definition.  */
14523   if (function_declarator_p (declarator)
14524       && cp_parser_token_starts_function_definition_p (token))
14525     {
14526       if (!function_definition_allowed_p)
14527         {
14528           /* If a function-definition should not appear here, issue an
14529              error message.  */
14530           cp_parser_error (parser,
14531                            "a function-definition is not allowed here");
14532           return error_mark_node;
14533         }
14534       else
14535         {
14536           location_t func_brace_location
14537             = cp_lexer_peek_token (parser->lexer)->location;
14538
14539           /* Neither attributes nor an asm-specification are allowed
14540              on a function-definition.  */
14541           if (asm_specification)
14542             error_at (asm_spec_start_token->location,
14543                       "an asm-specification is not allowed "
14544                       "on a function-definition");
14545           if (attributes)
14546             error_at (attributes_start_token->location,
14547                       "attributes are not allowed on a function-definition");
14548           /* This is a function-definition.  */
14549           *function_definition_p = true;
14550
14551           /* Parse the function definition.  */
14552           if (member_p)
14553             decl = cp_parser_save_member_function_body (parser,
14554                                                         decl_specifiers,
14555                                                         declarator,
14556                                                         prefix_attributes);
14557           else
14558             decl
14559               = (cp_parser_function_definition_from_specifiers_and_declarator
14560                  (parser, decl_specifiers, prefix_attributes, declarator));
14561
14562           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14563             {
14564               /* This is where the prologue starts...  */
14565               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14566                 = func_brace_location;
14567             }
14568
14569           return decl;
14570         }
14571     }
14572
14573   /* [dcl.dcl]
14574
14575      Only in function declarations for constructors, destructors, and
14576      type conversions can the decl-specifier-seq be omitted.
14577
14578      We explicitly postpone this check past the point where we handle
14579      function-definitions because we tolerate function-definitions
14580      that are missing their return types in some modes.  */
14581   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14582     {
14583       cp_parser_error (parser,
14584                        "expected constructor, destructor, or type conversion");
14585       return error_mark_node;
14586     }
14587
14588   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14589   if (token->type == CPP_EQ
14590       || token->type == CPP_OPEN_PAREN
14591       || token->type == CPP_OPEN_BRACE)
14592     {
14593       is_initialized = SD_INITIALIZED;
14594       initialization_kind = token->type;
14595       if (maybe_range_for_decl)
14596         *maybe_range_for_decl = error_mark_node;
14597
14598       if (token->type == CPP_EQ
14599           && function_declarator_p (declarator))
14600         {
14601           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14602           if (t2->keyword == RID_DEFAULT)
14603             is_initialized = SD_DEFAULTED;
14604           else if (t2->keyword == RID_DELETE)
14605             is_initialized = SD_DELETED;
14606         }
14607     }
14608   else
14609     {
14610       /* If the init-declarator isn't initialized and isn't followed by a
14611          `,' or `;', it's not a valid init-declarator.  */
14612       if (token->type != CPP_COMMA
14613           && token->type != CPP_SEMICOLON)
14614         {
14615           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14616             range_for_decl_p = true;
14617           else
14618             {
14619               cp_parser_error (parser, "expected initializer");
14620               return error_mark_node;
14621             }
14622         }
14623       is_initialized = SD_UNINITIALIZED;
14624       initialization_kind = CPP_EOF;
14625     }
14626
14627   /* Because start_decl has side-effects, we should only call it if we
14628      know we're going ahead.  By this point, we know that we cannot
14629      possibly be looking at any other construct.  */
14630   cp_parser_commit_to_tentative_parse (parser);
14631
14632   /* If the decl specifiers were bad, issue an error now that we're
14633      sure this was intended to be a declarator.  Then continue
14634      declaring the variable(s), as int, to try to cut down on further
14635      errors.  */
14636   if (decl_specifiers->any_specifiers_p
14637       && decl_specifiers->type == error_mark_node)
14638     {
14639       cp_parser_error (parser, "invalid type in declaration");
14640       decl_specifiers->type = integer_type_node;
14641     }
14642
14643   /* Check to see whether or not this declaration is a friend.  */
14644   friend_p = cp_parser_friend_p (decl_specifiers);
14645
14646   /* Enter the newly declared entry in the symbol table.  If we're
14647      processing a declaration in a class-specifier, we wait until
14648      after processing the initializer.  */
14649   if (!member_p)
14650     {
14651       if (parser->in_unbraced_linkage_specification_p)
14652         decl_specifiers->storage_class = sc_extern;
14653       decl = start_decl (declarator, decl_specifiers,
14654                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14655                          attributes, prefix_attributes,
14656                          &pushed_scope);
14657       /* Adjust location of decl if declarator->id_loc is more appropriate:
14658          set, and decl wasn't merged with another decl, in which case its
14659          location would be different from input_location, and more accurate.  */
14660       if (DECL_P (decl)
14661           && declarator->id_loc != UNKNOWN_LOCATION
14662           && DECL_SOURCE_LOCATION (decl) == input_location)
14663         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14664     }
14665   else if (scope)
14666     /* Enter the SCOPE.  That way unqualified names appearing in the
14667        initializer will be looked up in SCOPE.  */
14668     pushed_scope = push_scope (scope);
14669
14670   /* Perform deferred access control checks, now that we know in which
14671      SCOPE the declared entity resides.  */
14672   if (!member_p && decl)
14673     {
14674       tree saved_current_function_decl = NULL_TREE;
14675
14676       /* If the entity being declared is a function, pretend that we
14677          are in its scope.  If it is a `friend', it may have access to
14678          things that would not otherwise be accessible.  */
14679       if (TREE_CODE (decl) == FUNCTION_DECL)
14680         {
14681           saved_current_function_decl = current_function_decl;
14682           current_function_decl = decl;
14683         }
14684
14685       /* Perform access checks for template parameters.  */
14686       cp_parser_perform_template_parameter_access_checks (checks);
14687
14688       /* Perform the access control checks for the declarator and the
14689          decl-specifiers.  */
14690       perform_deferred_access_checks ();
14691
14692       /* Restore the saved value.  */
14693       if (TREE_CODE (decl) == FUNCTION_DECL)
14694         current_function_decl = saved_current_function_decl;
14695     }
14696
14697   /* Parse the initializer.  */
14698   initializer = NULL_TREE;
14699   is_direct_init = false;
14700   is_non_constant_init = true;
14701   if (is_initialized)
14702     {
14703       if (function_declarator_p (declarator))
14704         {
14705           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14706            if (initialization_kind == CPP_EQ)
14707              initializer = cp_parser_pure_specifier (parser);
14708            else
14709              {
14710                /* If the declaration was erroneous, we don't really
14711                   know what the user intended, so just silently
14712                   consume the initializer.  */
14713                if (decl != error_mark_node)
14714                  error_at (initializer_start_token->location,
14715                            "initializer provided for function");
14716                cp_parser_skip_to_closing_parenthesis (parser,
14717                                                       /*recovering=*/true,
14718                                                       /*or_comma=*/false,
14719                                                       /*consume_paren=*/true);
14720              }
14721         }
14722       else
14723         {
14724           /* We want to record the extra mangling scope for in-class
14725              initializers of class members and initializers of static data
14726              member templates.  The former is a C++0x feature which isn't
14727              implemented yet, and I expect it will involve deferring
14728              parsing of the initializer until end of class as with default
14729              arguments.  So right here we only handle the latter.  */
14730           if (!member_p && processing_template_decl)
14731             start_lambda_scope (decl);
14732           initializer = cp_parser_initializer (parser,
14733                                                &is_direct_init,
14734                                                &is_non_constant_init);
14735           if (!member_p && processing_template_decl)
14736             finish_lambda_scope ();
14737         }
14738     }
14739
14740   /* The old parser allows attributes to appear after a parenthesized
14741      initializer.  Mark Mitchell proposed removing this functionality
14742      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14743      attributes -- but ignores them.  */
14744   if (cp_parser_allow_gnu_extensions_p (parser)
14745       && initialization_kind == CPP_OPEN_PAREN)
14746     if (cp_parser_attributes_opt (parser))
14747       warning (OPT_Wattributes,
14748                "attributes after parenthesized initializer ignored");
14749
14750   /* For an in-class declaration, use `grokfield' to create the
14751      declaration.  */
14752   if (member_p)
14753     {
14754       if (pushed_scope)
14755         {
14756           pop_scope (pushed_scope);
14757           pushed_scope = false;
14758         }
14759       decl = grokfield (declarator, decl_specifiers,
14760                         initializer, !is_non_constant_init,
14761                         /*asmspec=*/NULL_TREE,
14762                         prefix_attributes);
14763       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14764         cp_parser_save_default_args (parser, decl);
14765     }
14766
14767   /* Finish processing the declaration.  But, skip friend
14768      declarations.  */
14769   if (!friend_p && decl && decl != error_mark_node && !range_for_decl_p)
14770     {
14771       cp_finish_decl (decl,
14772                       initializer, !is_non_constant_init,
14773                       asm_specification,
14774                       /* If the initializer is in parentheses, then this is
14775                          a direct-initialization, which means that an
14776                          `explicit' constructor is OK.  Otherwise, an
14777                          `explicit' constructor cannot be used.  */
14778                       ((is_direct_init || !is_initialized)
14779                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14780     }
14781   else if ((cxx_dialect != cxx98) && friend_p
14782            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14783     /* Core issue #226 (C++0x only): A default template-argument
14784        shall not be specified in a friend class template
14785        declaration. */
14786     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14787                              /*is_partial=*/0, /*is_friend_decl=*/1);
14788
14789   if (!friend_p && pushed_scope)
14790     pop_scope (pushed_scope);
14791
14792   return decl;
14793 }
14794
14795 /* Parse a declarator.
14796
14797    declarator:
14798      direct-declarator
14799      ptr-operator declarator
14800
14801    abstract-declarator:
14802      ptr-operator abstract-declarator [opt]
14803      direct-abstract-declarator
14804
14805    GNU Extensions:
14806
14807    declarator:
14808      attributes [opt] direct-declarator
14809      attributes [opt] ptr-operator declarator
14810
14811    abstract-declarator:
14812      attributes [opt] ptr-operator abstract-declarator [opt]
14813      attributes [opt] direct-abstract-declarator
14814
14815    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14816    detect constructor, destructor or conversion operators. It is set
14817    to -1 if the declarator is a name, and +1 if it is a
14818    function. Otherwise it is set to zero. Usually you just want to
14819    test for >0, but internally the negative value is used.
14820
14821    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14822    a decl-specifier-seq unless it declares a constructor, destructor,
14823    or conversion.  It might seem that we could check this condition in
14824    semantic analysis, rather than parsing, but that makes it difficult
14825    to handle something like `f()'.  We want to notice that there are
14826    no decl-specifiers, and therefore realize that this is an
14827    expression, not a declaration.)
14828
14829    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14830    the declarator is a direct-declarator of the form "(...)".
14831
14832    MEMBER_P is true iff this declarator is a member-declarator.  */
14833
14834 static cp_declarator *
14835 cp_parser_declarator (cp_parser* parser,
14836                       cp_parser_declarator_kind dcl_kind,
14837                       int* ctor_dtor_or_conv_p,
14838                       bool* parenthesized_p,
14839                       bool member_p)
14840 {
14841   cp_declarator *declarator;
14842   enum tree_code code;
14843   cp_cv_quals cv_quals;
14844   tree class_type;
14845   tree attributes = NULL_TREE;
14846
14847   /* Assume this is not a constructor, destructor, or type-conversion
14848      operator.  */
14849   if (ctor_dtor_or_conv_p)
14850     *ctor_dtor_or_conv_p = 0;
14851
14852   if (cp_parser_allow_gnu_extensions_p (parser))
14853     attributes = cp_parser_attributes_opt (parser);
14854
14855   /* Check for the ptr-operator production.  */
14856   cp_parser_parse_tentatively (parser);
14857   /* Parse the ptr-operator.  */
14858   code = cp_parser_ptr_operator (parser,
14859                                  &class_type,
14860                                  &cv_quals);
14861   /* If that worked, then we have a ptr-operator.  */
14862   if (cp_parser_parse_definitely (parser))
14863     {
14864       /* If a ptr-operator was found, then this declarator was not
14865          parenthesized.  */
14866       if (parenthesized_p)
14867         *parenthesized_p = true;
14868       /* The dependent declarator is optional if we are parsing an
14869          abstract-declarator.  */
14870       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14871         cp_parser_parse_tentatively (parser);
14872
14873       /* Parse the dependent declarator.  */
14874       declarator = cp_parser_declarator (parser, dcl_kind,
14875                                          /*ctor_dtor_or_conv_p=*/NULL,
14876                                          /*parenthesized_p=*/NULL,
14877                                          /*member_p=*/false);
14878
14879       /* If we are parsing an abstract-declarator, we must handle the
14880          case where the dependent declarator is absent.  */
14881       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14882           && !cp_parser_parse_definitely (parser))
14883         declarator = NULL;
14884
14885       declarator = cp_parser_make_indirect_declarator
14886         (code, class_type, cv_quals, declarator);
14887     }
14888   /* Everything else is a direct-declarator.  */
14889   else
14890     {
14891       if (parenthesized_p)
14892         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14893                                                    CPP_OPEN_PAREN);
14894       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14895                                                 ctor_dtor_or_conv_p,
14896                                                 member_p);
14897     }
14898
14899   if (attributes && declarator && declarator != cp_error_declarator)
14900     declarator->attributes = attributes;
14901
14902   return declarator;
14903 }
14904
14905 /* Parse a direct-declarator or direct-abstract-declarator.
14906
14907    direct-declarator:
14908      declarator-id
14909      direct-declarator ( parameter-declaration-clause )
14910        cv-qualifier-seq [opt]
14911        exception-specification [opt]
14912      direct-declarator [ constant-expression [opt] ]
14913      ( declarator )
14914
14915    direct-abstract-declarator:
14916      direct-abstract-declarator [opt]
14917        ( parameter-declaration-clause )
14918        cv-qualifier-seq [opt]
14919        exception-specification [opt]
14920      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14921      ( abstract-declarator )
14922
14923    Returns a representation of the declarator.  DCL_KIND is
14924    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14925    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14926    we are parsing a direct-declarator.  It is
14927    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14928    of ambiguity we prefer an abstract declarator, as per
14929    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14930    cp_parser_declarator.  */
14931
14932 static cp_declarator *
14933 cp_parser_direct_declarator (cp_parser* parser,
14934                              cp_parser_declarator_kind dcl_kind,
14935                              int* ctor_dtor_or_conv_p,
14936                              bool member_p)
14937 {
14938   cp_token *token;
14939   cp_declarator *declarator = NULL;
14940   tree scope = NULL_TREE;
14941   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14942   bool saved_in_declarator_p = parser->in_declarator_p;
14943   bool first = true;
14944   tree pushed_scope = NULL_TREE;
14945
14946   while (true)
14947     {
14948       /* Peek at the next token.  */
14949       token = cp_lexer_peek_token (parser->lexer);
14950       if (token->type == CPP_OPEN_PAREN)
14951         {
14952           /* This is either a parameter-declaration-clause, or a
14953              parenthesized declarator. When we know we are parsing a
14954              named declarator, it must be a parenthesized declarator
14955              if FIRST is true. For instance, `(int)' is a
14956              parameter-declaration-clause, with an omitted
14957              direct-abstract-declarator. But `((*))', is a
14958              parenthesized abstract declarator. Finally, when T is a
14959              template parameter `(T)' is a
14960              parameter-declaration-clause, and not a parenthesized
14961              named declarator.
14962
14963              We first try and parse a parameter-declaration-clause,
14964              and then try a nested declarator (if FIRST is true).
14965
14966              It is not an error for it not to be a
14967              parameter-declaration-clause, even when FIRST is
14968              false. Consider,
14969
14970                int i (int);
14971                int i (3);
14972
14973              The first is the declaration of a function while the
14974              second is the definition of a variable, including its
14975              initializer.
14976
14977              Having seen only the parenthesis, we cannot know which of
14978              these two alternatives should be selected.  Even more
14979              complex are examples like:
14980
14981                int i (int (a));
14982                int i (int (3));
14983
14984              The former is a function-declaration; the latter is a
14985              variable initialization.
14986
14987              Thus again, we try a parameter-declaration-clause, and if
14988              that fails, we back out and return.  */
14989
14990           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14991             {
14992               tree params;
14993               unsigned saved_num_template_parameter_lists;
14994               bool is_declarator = false;
14995               tree t;
14996
14997               /* In a member-declarator, the only valid interpretation
14998                  of a parenthesis is the start of a
14999                  parameter-declaration-clause.  (It is invalid to
15000                  initialize a static data member with a parenthesized
15001                  initializer; only the "=" form of initialization is
15002                  permitted.)  */
15003               if (!member_p)
15004                 cp_parser_parse_tentatively (parser);
15005
15006               /* Consume the `('.  */
15007               cp_lexer_consume_token (parser->lexer);
15008               if (first)
15009                 {
15010                   /* If this is going to be an abstract declarator, we're
15011                      in a declarator and we can't have default args.  */
15012                   parser->default_arg_ok_p = false;
15013                   parser->in_declarator_p = true;
15014                 }
15015
15016               /* Inside the function parameter list, surrounding
15017                  template-parameter-lists do not apply.  */
15018               saved_num_template_parameter_lists
15019                 = parser->num_template_parameter_lists;
15020               parser->num_template_parameter_lists = 0;
15021
15022               begin_scope (sk_function_parms, NULL_TREE);
15023
15024               /* Parse the parameter-declaration-clause.  */
15025               params = cp_parser_parameter_declaration_clause (parser);
15026
15027               parser->num_template_parameter_lists
15028                 = saved_num_template_parameter_lists;
15029
15030               /* If all went well, parse the cv-qualifier-seq and the
15031                  exception-specification.  */
15032               if (member_p || cp_parser_parse_definitely (parser))
15033                 {
15034                   cp_cv_quals cv_quals;
15035                   tree exception_specification;
15036                   tree late_return;
15037
15038                   is_declarator = true;
15039
15040                   if (ctor_dtor_or_conv_p)
15041                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15042                   first = false;
15043                   /* Consume the `)'.  */
15044                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15045
15046                   /* Parse the cv-qualifier-seq.  */
15047                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15048                   /* And the exception-specification.  */
15049                   exception_specification
15050                     = cp_parser_exception_specification_opt (parser);
15051
15052                   late_return
15053                     = cp_parser_late_return_type_opt (parser);
15054
15055                   /* Create the function-declarator.  */
15056                   declarator = make_call_declarator (declarator,
15057                                                      params,
15058                                                      cv_quals,
15059                                                      exception_specification,
15060                                                      late_return);
15061                   /* Any subsequent parameter lists are to do with
15062                      return type, so are not those of the declared
15063                      function.  */
15064                   parser->default_arg_ok_p = false;
15065                 }
15066
15067               /* Remove the function parms from scope.  */
15068               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15069                 pop_binding (DECL_NAME (t), t);
15070               leave_scope();
15071
15072               if (is_declarator)
15073                 /* Repeat the main loop.  */
15074                 continue;
15075             }
15076
15077           /* If this is the first, we can try a parenthesized
15078              declarator.  */
15079           if (first)
15080             {
15081               bool saved_in_type_id_in_expr_p;
15082
15083               parser->default_arg_ok_p = saved_default_arg_ok_p;
15084               parser->in_declarator_p = saved_in_declarator_p;
15085
15086               /* Consume the `('.  */
15087               cp_lexer_consume_token (parser->lexer);
15088               /* Parse the nested declarator.  */
15089               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15090               parser->in_type_id_in_expr_p = true;
15091               declarator
15092                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15093                                         /*parenthesized_p=*/NULL,
15094                                         member_p);
15095               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15096               first = false;
15097               /* Expect a `)'.  */
15098               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15099                 declarator = cp_error_declarator;
15100               if (declarator == cp_error_declarator)
15101                 break;
15102
15103               goto handle_declarator;
15104             }
15105           /* Otherwise, we must be done.  */
15106           else
15107             break;
15108         }
15109       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15110                && token->type == CPP_OPEN_SQUARE)
15111         {
15112           /* Parse an array-declarator.  */
15113           tree bounds;
15114
15115           if (ctor_dtor_or_conv_p)
15116             *ctor_dtor_or_conv_p = 0;
15117
15118           first = false;
15119           parser->default_arg_ok_p = false;
15120           parser->in_declarator_p = true;
15121           /* Consume the `['.  */
15122           cp_lexer_consume_token (parser->lexer);
15123           /* Peek at the next token.  */
15124           token = cp_lexer_peek_token (parser->lexer);
15125           /* If the next token is `]', then there is no
15126              constant-expression.  */
15127           if (token->type != CPP_CLOSE_SQUARE)
15128             {
15129               bool non_constant_p;
15130
15131               bounds
15132                 = cp_parser_constant_expression (parser,
15133                                                  /*allow_non_constant=*/true,
15134                                                  &non_constant_p);
15135               if (!non_constant_p)
15136                 /* OK */;
15137               /* Normally, the array bound must be an integral constant
15138                  expression.  However, as an extension, we allow VLAs
15139                  in function scopes as long as they aren't part of a
15140                  parameter declaration.  */
15141               else if (!parser->in_function_body
15142                        || current_binding_level->kind == sk_function_parms)
15143                 {
15144                   cp_parser_error (parser,
15145                                    "array bound is not an integer constant");
15146                   bounds = error_mark_node;
15147                 }
15148               else if (processing_template_decl && !error_operand_p (bounds))
15149                 {
15150                   /* Remember this wasn't a constant-expression.  */
15151                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15152                   TREE_SIDE_EFFECTS (bounds) = 1;
15153                 }
15154             }
15155           else
15156             bounds = NULL_TREE;
15157           /* Look for the closing `]'.  */
15158           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15159             {
15160               declarator = cp_error_declarator;
15161               break;
15162             }
15163
15164           declarator = make_array_declarator (declarator, bounds);
15165         }
15166       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15167         {
15168           {
15169             tree qualifying_scope;
15170             tree unqualified_name;
15171             special_function_kind sfk;
15172             bool abstract_ok;
15173             bool pack_expansion_p = false;
15174             cp_token *declarator_id_start_token;
15175
15176             /* Parse a declarator-id */
15177             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15178             if (abstract_ok)
15179               {
15180                 cp_parser_parse_tentatively (parser);
15181
15182                 /* If we see an ellipsis, we should be looking at a
15183                    parameter pack. */
15184                 if (token->type == CPP_ELLIPSIS)
15185                   {
15186                     /* Consume the `...' */
15187                     cp_lexer_consume_token (parser->lexer);
15188
15189                     pack_expansion_p = true;
15190                   }
15191               }
15192
15193             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15194             unqualified_name
15195               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15196             qualifying_scope = parser->scope;
15197             if (abstract_ok)
15198               {
15199                 bool okay = false;
15200
15201                 if (!unqualified_name && pack_expansion_p)
15202                   {
15203                     /* Check whether an error occurred. */
15204                     okay = !cp_parser_error_occurred (parser);
15205
15206                     /* We already consumed the ellipsis to mark a
15207                        parameter pack, but we have no way to report it,
15208                        so abort the tentative parse. We will be exiting
15209                        immediately anyway. */
15210                     cp_parser_abort_tentative_parse (parser);
15211                   }
15212                 else
15213                   okay = cp_parser_parse_definitely (parser);
15214
15215                 if (!okay)
15216                   unqualified_name = error_mark_node;
15217                 else if (unqualified_name
15218                          && (qualifying_scope
15219                              || (TREE_CODE (unqualified_name)
15220                                  != IDENTIFIER_NODE)))
15221                   {
15222                     cp_parser_error (parser, "expected unqualified-id");
15223                     unqualified_name = error_mark_node;
15224                   }
15225               }
15226
15227             if (!unqualified_name)
15228               return NULL;
15229             if (unqualified_name == error_mark_node)
15230               {
15231                 declarator = cp_error_declarator;
15232                 pack_expansion_p = false;
15233                 declarator->parameter_pack_p = false;
15234                 break;
15235               }
15236
15237             if (qualifying_scope && at_namespace_scope_p ()
15238                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15239               {
15240                 /* In the declaration of a member of a template class
15241                    outside of the class itself, the SCOPE will sometimes
15242                    be a TYPENAME_TYPE.  For example, given:
15243
15244                    template <typename T>
15245                    int S<T>::R::i = 3;
15246
15247                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15248                    this context, we must resolve S<T>::R to an ordinary
15249                    type, rather than a typename type.
15250
15251                    The reason we normally avoid resolving TYPENAME_TYPEs
15252                    is that a specialization of `S' might render
15253                    `S<T>::R' not a type.  However, if `S' is
15254                    specialized, then this `i' will not be used, so there
15255                    is no harm in resolving the types here.  */
15256                 tree type;
15257
15258                 /* Resolve the TYPENAME_TYPE.  */
15259                 type = resolve_typename_type (qualifying_scope,
15260                                               /*only_current_p=*/false);
15261                 /* If that failed, the declarator is invalid.  */
15262                 if (TREE_CODE (type) == TYPENAME_TYPE)
15263                   {
15264                     if (typedef_variant_p (type))
15265                       error_at (declarator_id_start_token->location,
15266                                 "cannot define member of dependent typedef "
15267                                 "%qT", type);
15268                     else
15269                       error_at (declarator_id_start_token->location,
15270                                 "%<%T::%E%> is not a type",
15271                                 TYPE_CONTEXT (qualifying_scope),
15272                                 TYPE_IDENTIFIER (qualifying_scope));
15273                   }
15274                 qualifying_scope = type;
15275               }
15276
15277             sfk = sfk_none;
15278
15279             if (unqualified_name)
15280               {
15281                 tree class_type;
15282
15283                 if (qualifying_scope
15284                     && CLASS_TYPE_P (qualifying_scope))
15285                   class_type = qualifying_scope;
15286                 else
15287                   class_type = current_class_type;
15288
15289                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15290                   {
15291                     tree name_type = TREE_TYPE (unqualified_name);
15292                     if (class_type && same_type_p (name_type, class_type))
15293                       {
15294                         if (qualifying_scope
15295                             && CLASSTYPE_USE_TEMPLATE (name_type))
15296                           {
15297                             error_at (declarator_id_start_token->location,
15298                                       "invalid use of constructor as a template");
15299                             inform (declarator_id_start_token->location,
15300                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15301                                     "name the constructor in a qualified name",
15302                                     class_type,
15303                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15304                                     class_type, name_type);
15305                             declarator = cp_error_declarator;
15306                             break;
15307                           }
15308                         else
15309                           unqualified_name = constructor_name (class_type);
15310                       }
15311                     else
15312                       {
15313                         /* We do not attempt to print the declarator
15314                            here because we do not have enough
15315                            information about its original syntactic
15316                            form.  */
15317                         cp_parser_error (parser, "invalid declarator");
15318                         declarator = cp_error_declarator;
15319                         break;
15320                       }
15321                   }
15322
15323                 if (class_type)
15324                   {
15325                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15326                       sfk = sfk_destructor;
15327                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15328                       sfk = sfk_conversion;
15329                     else if (/* There's no way to declare a constructor
15330                                 for an anonymous type, even if the type
15331                                 got a name for linkage purposes.  */
15332                              !TYPE_WAS_ANONYMOUS (class_type)
15333                              && constructor_name_p (unqualified_name,
15334                                                     class_type))
15335                       {
15336                         unqualified_name = constructor_name (class_type);
15337                         sfk = sfk_constructor;
15338                       }
15339                     else if (is_overloaded_fn (unqualified_name)
15340                              && DECL_CONSTRUCTOR_P (get_first_fn
15341                                                     (unqualified_name)))
15342                       sfk = sfk_constructor;
15343
15344                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15345                       *ctor_dtor_or_conv_p = -1;
15346                   }
15347               }
15348             declarator = make_id_declarator (qualifying_scope,
15349                                              unqualified_name,
15350                                              sfk);
15351             declarator->id_loc = token->location;
15352             declarator->parameter_pack_p = pack_expansion_p;
15353
15354             if (pack_expansion_p)
15355               maybe_warn_variadic_templates ();
15356           }
15357
15358         handle_declarator:;
15359           scope = get_scope_of_declarator (declarator);
15360           if (scope)
15361             /* Any names that appear after the declarator-id for a
15362                member are looked up in the containing scope.  */
15363             pushed_scope = push_scope (scope);
15364           parser->in_declarator_p = true;
15365           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15366               || (declarator && declarator->kind == cdk_id))
15367             /* Default args are only allowed on function
15368                declarations.  */
15369             parser->default_arg_ok_p = saved_default_arg_ok_p;
15370           else
15371             parser->default_arg_ok_p = false;
15372
15373           first = false;
15374         }
15375       /* We're done.  */
15376       else
15377         break;
15378     }
15379
15380   /* For an abstract declarator, we might wind up with nothing at this
15381      point.  That's an error; the declarator is not optional.  */
15382   if (!declarator)
15383     cp_parser_error (parser, "expected declarator");
15384
15385   /* If we entered a scope, we must exit it now.  */
15386   if (pushed_scope)
15387     pop_scope (pushed_scope);
15388
15389   parser->default_arg_ok_p = saved_default_arg_ok_p;
15390   parser->in_declarator_p = saved_in_declarator_p;
15391
15392   return declarator;
15393 }
15394
15395 /* Parse a ptr-operator.
15396
15397    ptr-operator:
15398      * cv-qualifier-seq [opt]
15399      &
15400      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15401
15402    GNU Extension:
15403
15404    ptr-operator:
15405      & cv-qualifier-seq [opt]
15406
15407    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15408    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15409    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15410    filled in with the TYPE containing the member.  *CV_QUALS is
15411    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15412    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15413    Note that the tree codes returned by this function have nothing
15414    to do with the types of trees that will be eventually be created
15415    to represent the pointer or reference type being parsed. They are
15416    just constants with suggestive names. */
15417 static enum tree_code
15418 cp_parser_ptr_operator (cp_parser* parser,
15419                         tree* type,
15420                         cp_cv_quals *cv_quals)
15421 {
15422   enum tree_code code = ERROR_MARK;
15423   cp_token *token;
15424
15425   /* Assume that it's not a pointer-to-member.  */
15426   *type = NULL_TREE;
15427   /* And that there are no cv-qualifiers.  */
15428   *cv_quals = TYPE_UNQUALIFIED;
15429
15430   /* Peek at the next token.  */
15431   token = cp_lexer_peek_token (parser->lexer);
15432
15433   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15434   if (token->type == CPP_MULT)
15435     code = INDIRECT_REF;
15436   else if (token->type == CPP_AND)
15437     code = ADDR_EXPR;
15438   else if ((cxx_dialect != cxx98) &&
15439            token->type == CPP_AND_AND) /* C++0x only */
15440     code = NON_LVALUE_EXPR;
15441
15442   if (code != ERROR_MARK)
15443     {
15444       /* Consume the `*', `&' or `&&'.  */
15445       cp_lexer_consume_token (parser->lexer);
15446
15447       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15448          `&', if we are allowing GNU extensions.  (The only qualifier
15449          that can legally appear after `&' is `restrict', but that is
15450          enforced during semantic analysis.  */
15451       if (code == INDIRECT_REF
15452           || cp_parser_allow_gnu_extensions_p (parser))
15453         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15454     }
15455   else
15456     {
15457       /* Try the pointer-to-member case.  */
15458       cp_parser_parse_tentatively (parser);
15459       /* Look for the optional `::' operator.  */
15460       cp_parser_global_scope_opt (parser,
15461                                   /*current_scope_valid_p=*/false);
15462       /* Look for the nested-name specifier.  */
15463       token = cp_lexer_peek_token (parser->lexer);
15464       cp_parser_nested_name_specifier (parser,
15465                                        /*typename_keyword_p=*/false,
15466                                        /*check_dependency_p=*/true,
15467                                        /*type_p=*/false,
15468                                        /*is_declaration=*/false);
15469       /* If we found it, and the next token is a `*', then we are
15470          indeed looking at a pointer-to-member operator.  */
15471       if (!cp_parser_error_occurred (parser)
15472           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15473         {
15474           /* Indicate that the `*' operator was used.  */
15475           code = INDIRECT_REF;
15476
15477           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15478             error_at (token->location, "%qD is a namespace", parser->scope);
15479           else
15480             {
15481               /* The type of which the member is a member is given by the
15482                  current SCOPE.  */
15483               *type = parser->scope;
15484               /* The next name will not be qualified.  */
15485               parser->scope = NULL_TREE;
15486               parser->qualifying_scope = NULL_TREE;
15487               parser->object_scope = NULL_TREE;
15488               /* Look for the optional cv-qualifier-seq.  */
15489               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15490             }
15491         }
15492       /* If that didn't work we don't have a ptr-operator.  */
15493       if (!cp_parser_parse_definitely (parser))
15494         cp_parser_error (parser, "expected ptr-operator");
15495     }
15496
15497   return code;
15498 }
15499
15500 /* Parse an (optional) cv-qualifier-seq.
15501
15502    cv-qualifier-seq:
15503      cv-qualifier cv-qualifier-seq [opt]
15504
15505    cv-qualifier:
15506      const
15507      volatile
15508
15509    GNU Extension:
15510
15511    cv-qualifier:
15512      __restrict__
15513
15514    Returns a bitmask representing the cv-qualifiers.  */
15515
15516 static cp_cv_quals
15517 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15518 {
15519   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15520
15521   while (true)
15522     {
15523       cp_token *token;
15524       cp_cv_quals cv_qualifier;
15525
15526       /* Peek at the next token.  */
15527       token = cp_lexer_peek_token (parser->lexer);
15528       /* See if it's a cv-qualifier.  */
15529       switch (token->keyword)
15530         {
15531         case RID_CONST:
15532           cv_qualifier = TYPE_QUAL_CONST;
15533           break;
15534
15535         case RID_VOLATILE:
15536           cv_qualifier = TYPE_QUAL_VOLATILE;
15537           break;
15538
15539         case RID_RESTRICT:
15540           cv_qualifier = TYPE_QUAL_RESTRICT;
15541           break;
15542
15543         default:
15544           cv_qualifier = TYPE_UNQUALIFIED;
15545           break;
15546         }
15547
15548       if (!cv_qualifier)
15549         break;
15550
15551       if (cv_quals & cv_qualifier)
15552         {
15553           error_at (token->location, "duplicate cv-qualifier");
15554           cp_lexer_purge_token (parser->lexer);
15555         }
15556       else
15557         {
15558           cp_lexer_consume_token (parser->lexer);
15559           cv_quals |= cv_qualifier;
15560         }
15561     }
15562
15563   return cv_quals;
15564 }
15565
15566 /* Parse a late-specified return type, if any.  This is not a separate
15567    non-terminal, but part of a function declarator, which looks like
15568
15569    -> trailing-type-specifier-seq abstract-declarator(opt)
15570
15571    Returns the type indicated by the type-id.  */
15572
15573 static tree
15574 cp_parser_late_return_type_opt (cp_parser* parser)
15575 {
15576   cp_token *token;
15577
15578   /* Peek at the next token.  */
15579   token = cp_lexer_peek_token (parser->lexer);
15580   /* A late-specified return type is indicated by an initial '->'. */
15581   if (token->type != CPP_DEREF)
15582     return NULL_TREE;
15583
15584   /* Consume the ->.  */
15585   cp_lexer_consume_token (parser->lexer);
15586
15587   return cp_parser_trailing_type_id (parser);
15588 }
15589
15590 /* Parse a declarator-id.
15591
15592    declarator-id:
15593      id-expression
15594      :: [opt] nested-name-specifier [opt] type-name
15595
15596    In the `id-expression' case, the value returned is as for
15597    cp_parser_id_expression if the id-expression was an unqualified-id.
15598    If the id-expression was a qualified-id, then a SCOPE_REF is
15599    returned.  The first operand is the scope (either a NAMESPACE_DECL
15600    or TREE_TYPE), but the second is still just a representation of an
15601    unqualified-id.  */
15602
15603 static tree
15604 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15605 {
15606   tree id;
15607   /* The expression must be an id-expression.  Assume that qualified
15608      names are the names of types so that:
15609
15610        template <class T>
15611        int S<T>::R::i = 3;
15612
15613      will work; we must treat `S<T>::R' as the name of a type.
15614      Similarly, assume that qualified names are templates, where
15615      required, so that:
15616
15617        template <class T>
15618        int S<T>::R<T>::i = 3;
15619
15620      will work, too.  */
15621   id = cp_parser_id_expression (parser,
15622                                 /*template_keyword_p=*/false,
15623                                 /*check_dependency_p=*/false,
15624                                 /*template_p=*/NULL,
15625                                 /*declarator_p=*/true,
15626                                 optional_p);
15627   if (id && BASELINK_P (id))
15628     id = BASELINK_FUNCTIONS (id);
15629   return id;
15630 }
15631
15632 /* Parse a type-id.
15633
15634    type-id:
15635      type-specifier-seq abstract-declarator [opt]
15636
15637    Returns the TYPE specified.  */
15638
15639 static tree
15640 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15641                      bool is_trailing_return)
15642 {
15643   cp_decl_specifier_seq type_specifier_seq;
15644   cp_declarator *abstract_declarator;
15645
15646   /* Parse the type-specifier-seq.  */
15647   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15648                                 is_trailing_return,
15649                                 &type_specifier_seq);
15650   if (type_specifier_seq.type == error_mark_node)
15651     return error_mark_node;
15652
15653   /* There might or might not be an abstract declarator.  */
15654   cp_parser_parse_tentatively (parser);
15655   /* Look for the declarator.  */
15656   abstract_declarator
15657     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15658                             /*parenthesized_p=*/NULL,
15659                             /*member_p=*/false);
15660   /* Check to see if there really was a declarator.  */
15661   if (!cp_parser_parse_definitely (parser))
15662     abstract_declarator = NULL;
15663
15664   if (type_specifier_seq.type
15665       && type_uses_auto (type_specifier_seq.type))
15666     {
15667       /* A type-id with type 'auto' is only ok if the abstract declarator
15668          is a function declarator with a late-specified return type.  */
15669       if (abstract_declarator
15670           && abstract_declarator->kind == cdk_function
15671           && abstract_declarator->u.function.late_return_type)
15672         /* OK */;
15673       else
15674         {
15675           error ("invalid use of %<auto%>");
15676           return error_mark_node;
15677         }
15678     }
15679   
15680   return groktypename (&type_specifier_seq, abstract_declarator,
15681                        is_template_arg);
15682 }
15683
15684 static tree cp_parser_type_id (cp_parser *parser)
15685 {
15686   return cp_parser_type_id_1 (parser, false, false);
15687 }
15688
15689 static tree cp_parser_template_type_arg (cp_parser *parser)
15690 {
15691   return cp_parser_type_id_1 (parser, true, false);
15692 }
15693
15694 static tree cp_parser_trailing_type_id (cp_parser *parser)
15695 {
15696   return cp_parser_type_id_1 (parser, false, true);
15697 }
15698
15699 /* Parse a type-specifier-seq.
15700
15701    type-specifier-seq:
15702      type-specifier type-specifier-seq [opt]
15703
15704    GNU extension:
15705
15706    type-specifier-seq:
15707      attributes type-specifier-seq [opt]
15708
15709    If IS_DECLARATION is true, we are at the start of a "condition" or
15710    exception-declaration, so we might be followed by a declarator-id.
15711
15712    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15713    i.e. we've just seen "->".
15714
15715    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15716
15717 static void
15718 cp_parser_type_specifier_seq (cp_parser* parser,
15719                               bool is_declaration,
15720                               bool is_trailing_return,
15721                               cp_decl_specifier_seq *type_specifier_seq)
15722 {
15723   bool seen_type_specifier = false;
15724   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15725   cp_token *start_token = NULL;
15726
15727   /* Clear the TYPE_SPECIFIER_SEQ.  */
15728   clear_decl_specs (type_specifier_seq);
15729
15730   /* In the context of a trailing return type, enum E { } is an
15731      elaborated-type-specifier followed by a function-body, not an
15732      enum-specifier.  */
15733   if (is_trailing_return)
15734     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15735
15736   /* Parse the type-specifiers and attributes.  */
15737   while (true)
15738     {
15739       tree type_specifier;
15740       bool is_cv_qualifier;
15741
15742       /* Check for attributes first.  */
15743       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15744         {
15745           type_specifier_seq->attributes =
15746             chainon (type_specifier_seq->attributes,
15747                      cp_parser_attributes_opt (parser));
15748           continue;
15749         }
15750
15751       /* record the token of the beginning of the type specifier seq,
15752          for error reporting purposes*/
15753      if (!start_token)
15754        start_token = cp_lexer_peek_token (parser->lexer);
15755
15756       /* Look for the type-specifier.  */
15757       type_specifier = cp_parser_type_specifier (parser,
15758                                                  flags,
15759                                                  type_specifier_seq,
15760                                                  /*is_declaration=*/false,
15761                                                  NULL,
15762                                                  &is_cv_qualifier);
15763       if (!type_specifier)
15764         {
15765           /* If the first type-specifier could not be found, this is not a
15766              type-specifier-seq at all.  */
15767           if (!seen_type_specifier)
15768             {
15769               cp_parser_error (parser, "expected type-specifier");
15770               type_specifier_seq->type = error_mark_node;
15771               return;
15772             }
15773           /* If subsequent type-specifiers could not be found, the
15774              type-specifier-seq is complete.  */
15775           break;
15776         }
15777
15778       seen_type_specifier = true;
15779       /* The standard says that a condition can be:
15780
15781             type-specifier-seq declarator = assignment-expression
15782
15783          However, given:
15784
15785            struct S {};
15786            if (int S = ...)
15787
15788          we should treat the "S" as a declarator, not as a
15789          type-specifier.  The standard doesn't say that explicitly for
15790          type-specifier-seq, but it does say that for
15791          decl-specifier-seq in an ordinary declaration.  Perhaps it
15792          would be clearer just to allow a decl-specifier-seq here, and
15793          then add a semantic restriction that if any decl-specifiers
15794          that are not type-specifiers appear, the program is invalid.  */
15795       if (is_declaration && !is_cv_qualifier)
15796         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15797     }
15798
15799   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15800 }
15801
15802 /* Parse a parameter-declaration-clause.
15803
15804    parameter-declaration-clause:
15805      parameter-declaration-list [opt] ... [opt]
15806      parameter-declaration-list , ...
15807
15808    Returns a representation for the parameter declarations.  A return
15809    value of NULL indicates a parameter-declaration-clause consisting
15810    only of an ellipsis.  */
15811
15812 static tree
15813 cp_parser_parameter_declaration_clause (cp_parser* parser)
15814 {
15815   tree parameters;
15816   cp_token *token;
15817   bool ellipsis_p;
15818   bool is_error;
15819
15820   /* Peek at the next token.  */
15821   token = cp_lexer_peek_token (parser->lexer);
15822   /* Check for trivial parameter-declaration-clauses.  */
15823   if (token->type == CPP_ELLIPSIS)
15824     {
15825       /* Consume the `...' token.  */
15826       cp_lexer_consume_token (parser->lexer);
15827       return NULL_TREE;
15828     }
15829   else if (token->type == CPP_CLOSE_PAREN)
15830     /* There are no parameters.  */
15831     {
15832 #ifndef NO_IMPLICIT_EXTERN_C
15833       if (in_system_header && current_class_type == NULL
15834           && current_lang_name == lang_name_c)
15835         return NULL_TREE;
15836       else
15837 #endif
15838         return void_list_node;
15839     }
15840   /* Check for `(void)', too, which is a special case.  */
15841   else if (token->keyword == RID_VOID
15842            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15843                == CPP_CLOSE_PAREN))
15844     {
15845       /* Consume the `void' token.  */
15846       cp_lexer_consume_token (parser->lexer);
15847       /* There are no parameters.  */
15848       return void_list_node;
15849     }
15850
15851   /* Parse the parameter-declaration-list.  */
15852   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15853   /* If a parse error occurred while parsing the
15854      parameter-declaration-list, then the entire
15855      parameter-declaration-clause is erroneous.  */
15856   if (is_error)
15857     return NULL;
15858
15859   /* Peek at the next token.  */
15860   token = cp_lexer_peek_token (parser->lexer);
15861   /* If it's a `,', the clause should terminate with an ellipsis.  */
15862   if (token->type == CPP_COMMA)
15863     {
15864       /* Consume the `,'.  */
15865       cp_lexer_consume_token (parser->lexer);
15866       /* Expect an ellipsis.  */
15867       ellipsis_p
15868         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15869     }
15870   /* It might also be `...' if the optional trailing `,' was
15871      omitted.  */
15872   else if (token->type == CPP_ELLIPSIS)
15873     {
15874       /* Consume the `...' token.  */
15875       cp_lexer_consume_token (parser->lexer);
15876       /* And remember that we saw it.  */
15877       ellipsis_p = true;
15878     }
15879   else
15880     ellipsis_p = false;
15881
15882   /* Finish the parameter list.  */
15883   if (!ellipsis_p)
15884     parameters = chainon (parameters, void_list_node);
15885
15886   return parameters;
15887 }
15888
15889 /* Parse a parameter-declaration-list.
15890
15891    parameter-declaration-list:
15892      parameter-declaration
15893      parameter-declaration-list , parameter-declaration
15894
15895    Returns a representation of the parameter-declaration-list, as for
15896    cp_parser_parameter_declaration_clause.  However, the
15897    `void_list_node' is never appended to the list.  Upon return,
15898    *IS_ERROR will be true iff an error occurred.  */
15899
15900 static tree
15901 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15902 {
15903   tree parameters = NULL_TREE;
15904   tree *tail = &parameters; 
15905   bool saved_in_unbraced_linkage_specification_p;
15906   int index = 0;
15907
15908   /* Assume all will go well.  */
15909   *is_error = false;
15910   /* The special considerations that apply to a function within an
15911      unbraced linkage specifications do not apply to the parameters
15912      to the function.  */
15913   saved_in_unbraced_linkage_specification_p 
15914     = parser->in_unbraced_linkage_specification_p;
15915   parser->in_unbraced_linkage_specification_p = false;
15916
15917   /* Look for more parameters.  */
15918   while (true)
15919     {
15920       cp_parameter_declarator *parameter;
15921       tree decl = error_mark_node;
15922       bool parenthesized_p;
15923       /* Parse the parameter.  */
15924       parameter
15925         = cp_parser_parameter_declaration (parser,
15926                                            /*template_parm_p=*/false,
15927                                            &parenthesized_p);
15928
15929       /* We don't know yet if the enclosing context is deprecated, so wait
15930          and warn in grokparms if appropriate.  */
15931       deprecated_state = DEPRECATED_SUPPRESS;
15932
15933       if (parameter)
15934         decl = grokdeclarator (parameter->declarator,
15935                                &parameter->decl_specifiers,
15936                                PARM,
15937                                parameter->default_argument != NULL_TREE,
15938                                &parameter->decl_specifiers.attributes);
15939
15940       deprecated_state = DEPRECATED_NORMAL;
15941
15942       /* If a parse error occurred parsing the parameter declaration,
15943          then the entire parameter-declaration-list is erroneous.  */
15944       if (decl == error_mark_node)
15945         {
15946           *is_error = true;
15947           parameters = error_mark_node;
15948           break;
15949         }
15950
15951       if (parameter->decl_specifiers.attributes)
15952         cplus_decl_attributes (&decl,
15953                                parameter->decl_specifiers.attributes,
15954                                0);
15955       if (DECL_NAME (decl))
15956         decl = pushdecl (decl);
15957
15958       if (decl != error_mark_node)
15959         {
15960           retrofit_lang_decl (decl);
15961           DECL_PARM_INDEX (decl) = ++index;
15962           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15963         }
15964
15965       /* Add the new parameter to the list.  */
15966       *tail = build_tree_list (parameter->default_argument, decl);
15967       tail = &TREE_CHAIN (*tail);
15968
15969       /* Peek at the next token.  */
15970       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15971           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15972           /* These are for Objective-C++ */
15973           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15974           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15975         /* The parameter-declaration-list is complete.  */
15976         break;
15977       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15978         {
15979           cp_token *token;
15980
15981           /* Peek at the next token.  */
15982           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15983           /* If it's an ellipsis, then the list is complete.  */
15984           if (token->type == CPP_ELLIPSIS)
15985             break;
15986           /* Otherwise, there must be more parameters.  Consume the
15987              `,'.  */
15988           cp_lexer_consume_token (parser->lexer);
15989           /* When parsing something like:
15990
15991                 int i(float f, double d)
15992
15993              we can tell after seeing the declaration for "f" that we
15994              are not looking at an initialization of a variable "i",
15995              but rather at the declaration of a function "i".
15996
15997              Due to the fact that the parsing of template arguments
15998              (as specified to a template-id) requires backtracking we
15999              cannot use this technique when inside a template argument
16000              list.  */
16001           if (!parser->in_template_argument_list_p
16002               && !parser->in_type_id_in_expr_p
16003               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16004               /* However, a parameter-declaration of the form
16005                  "foat(f)" (which is a valid declaration of a
16006                  parameter "f") can also be interpreted as an
16007                  expression (the conversion of "f" to "float").  */
16008               && !parenthesized_p)
16009             cp_parser_commit_to_tentative_parse (parser);
16010         }
16011       else
16012         {
16013           cp_parser_error (parser, "expected %<,%> or %<...%>");
16014           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16015             cp_parser_skip_to_closing_parenthesis (parser,
16016                                                    /*recovering=*/true,
16017                                                    /*or_comma=*/false,
16018                                                    /*consume_paren=*/false);
16019           break;
16020         }
16021     }
16022
16023   parser->in_unbraced_linkage_specification_p
16024     = saved_in_unbraced_linkage_specification_p;
16025
16026   return parameters;
16027 }
16028
16029 /* Parse a parameter declaration.
16030
16031    parameter-declaration:
16032      decl-specifier-seq ... [opt] declarator
16033      decl-specifier-seq declarator = assignment-expression
16034      decl-specifier-seq ... [opt] abstract-declarator [opt]
16035      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16036
16037    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16038    declares a template parameter.  (In that case, a non-nested `>'
16039    token encountered during the parsing of the assignment-expression
16040    is not interpreted as a greater-than operator.)
16041
16042    Returns a representation of the parameter, or NULL if an error
16043    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16044    true iff the declarator is of the form "(p)".  */
16045
16046 static cp_parameter_declarator *
16047 cp_parser_parameter_declaration (cp_parser *parser,
16048                                  bool template_parm_p,
16049                                  bool *parenthesized_p)
16050 {
16051   int declares_class_or_enum;
16052   cp_decl_specifier_seq decl_specifiers;
16053   cp_declarator *declarator;
16054   tree default_argument;
16055   cp_token *token = NULL, *declarator_token_start = NULL;
16056   const char *saved_message;
16057
16058   /* In a template parameter, `>' is not an operator.
16059
16060      [temp.param]
16061
16062      When parsing a default template-argument for a non-type
16063      template-parameter, the first non-nested `>' is taken as the end
16064      of the template parameter-list rather than a greater-than
16065      operator.  */
16066
16067   /* Type definitions may not appear in parameter types.  */
16068   saved_message = parser->type_definition_forbidden_message;
16069   parser->type_definition_forbidden_message
16070     = G_("types may not be defined in parameter types");
16071
16072   /* Parse the declaration-specifiers.  */
16073   cp_parser_decl_specifier_seq (parser,
16074                                 CP_PARSER_FLAGS_NONE,
16075                                 &decl_specifiers,
16076                                 &declares_class_or_enum);
16077
16078   /* Complain about missing 'typename' or other invalid type names.  */
16079   if (!decl_specifiers.any_type_specifiers_p)
16080     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16081
16082   /* If an error occurred, there's no reason to attempt to parse the
16083      rest of the declaration.  */
16084   if (cp_parser_error_occurred (parser))
16085     {
16086       parser->type_definition_forbidden_message = saved_message;
16087       return NULL;
16088     }
16089
16090   /* Peek at the next token.  */
16091   token = cp_lexer_peek_token (parser->lexer);
16092
16093   /* If the next token is a `)', `,', `=', `>', or `...', then there
16094      is no declarator. However, when variadic templates are enabled,
16095      there may be a declarator following `...'.  */
16096   if (token->type == CPP_CLOSE_PAREN
16097       || token->type == CPP_COMMA
16098       || token->type == CPP_EQ
16099       || token->type == CPP_GREATER)
16100     {
16101       declarator = NULL;
16102       if (parenthesized_p)
16103         *parenthesized_p = false;
16104     }
16105   /* Otherwise, there should be a declarator.  */
16106   else
16107     {
16108       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16109       parser->default_arg_ok_p = false;
16110
16111       /* After seeing a decl-specifier-seq, if the next token is not a
16112          "(", there is no possibility that the code is a valid
16113          expression.  Therefore, if parsing tentatively, we commit at
16114          this point.  */
16115       if (!parser->in_template_argument_list_p
16116           /* In an expression context, having seen:
16117
16118                (int((char ...
16119
16120              we cannot be sure whether we are looking at a
16121              function-type (taking a "char" as a parameter) or a cast
16122              of some object of type "char" to "int".  */
16123           && !parser->in_type_id_in_expr_p
16124           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16125           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16126         cp_parser_commit_to_tentative_parse (parser);
16127       /* Parse the declarator.  */
16128       declarator_token_start = token;
16129       declarator = cp_parser_declarator (parser,
16130                                          CP_PARSER_DECLARATOR_EITHER,
16131                                          /*ctor_dtor_or_conv_p=*/NULL,
16132                                          parenthesized_p,
16133                                          /*member_p=*/false);
16134       parser->default_arg_ok_p = saved_default_arg_ok_p;
16135       /* After the declarator, allow more attributes.  */
16136       decl_specifiers.attributes
16137         = chainon (decl_specifiers.attributes,
16138                    cp_parser_attributes_opt (parser));
16139     }
16140
16141   /* If the next token is an ellipsis, and we have not seen a
16142      declarator name, and the type of the declarator contains parameter
16143      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16144      a parameter pack expansion expression. Otherwise, leave the
16145      ellipsis for a C-style variadic function. */
16146   token = cp_lexer_peek_token (parser->lexer);
16147   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16148     {
16149       tree type = decl_specifiers.type;
16150
16151       if (type && DECL_P (type))
16152         type = TREE_TYPE (type);
16153
16154       if (type
16155           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16156           && declarator_can_be_parameter_pack (declarator)
16157           && (!declarator || !declarator->parameter_pack_p)
16158           && uses_parameter_packs (type))
16159         {
16160           /* Consume the `...'. */
16161           cp_lexer_consume_token (parser->lexer);
16162           maybe_warn_variadic_templates ();
16163           
16164           /* Build a pack expansion type */
16165           if (declarator)
16166             declarator->parameter_pack_p = true;
16167           else
16168             decl_specifiers.type = make_pack_expansion (type);
16169         }
16170     }
16171
16172   /* The restriction on defining new types applies only to the type
16173      of the parameter, not to the default argument.  */
16174   parser->type_definition_forbidden_message = saved_message;
16175
16176   /* If the next token is `=', then process a default argument.  */
16177   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16178     {
16179       /* Consume the `='.  */
16180       cp_lexer_consume_token (parser->lexer);
16181
16182       /* If we are defining a class, then the tokens that make up the
16183          default argument must be saved and processed later.  */
16184       if (!template_parm_p && at_class_scope_p ()
16185           && TYPE_BEING_DEFINED (current_class_type)
16186           && !LAMBDA_TYPE_P (current_class_type))
16187         {
16188           unsigned depth = 0;
16189           int maybe_template_id = 0;
16190           cp_token *first_token;
16191           cp_token *token;
16192
16193           /* Add tokens until we have processed the entire default
16194              argument.  We add the range [first_token, token).  */
16195           first_token = cp_lexer_peek_token (parser->lexer);
16196           while (true)
16197             {
16198               bool done = false;
16199
16200               /* Peek at the next token.  */
16201               token = cp_lexer_peek_token (parser->lexer);
16202               /* What we do depends on what token we have.  */
16203               switch (token->type)
16204                 {
16205                   /* In valid code, a default argument must be
16206                      immediately followed by a `,' `)', or `...'.  */
16207                 case CPP_COMMA:
16208                   if (depth == 0 && maybe_template_id)
16209                     {
16210                       /* If we've seen a '<', we might be in a
16211                          template-argument-list.  Until Core issue 325 is
16212                          resolved, we don't know how this situation ought
16213                          to be handled, so try to DTRT.  We check whether
16214                          what comes after the comma is a valid parameter
16215                          declaration list.  If it is, then the comma ends
16216                          the default argument; otherwise the default
16217                          argument continues.  */
16218                       bool error = false;
16219                       tree t;
16220
16221                       /* Set ITALP so cp_parser_parameter_declaration_list
16222                          doesn't decide to commit to this parse.  */
16223                       bool saved_italp = parser->in_template_argument_list_p;
16224                       parser->in_template_argument_list_p = true;
16225
16226                       cp_parser_parse_tentatively (parser);
16227                       cp_lexer_consume_token (parser->lexer);
16228                       begin_scope (sk_function_parms, NULL_TREE);
16229                       cp_parser_parameter_declaration_list (parser, &error);
16230                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16231                         pop_binding (DECL_NAME (t), t);
16232                       leave_scope ();
16233                       if (!cp_parser_error_occurred (parser) && !error)
16234                         done = true;
16235                       cp_parser_abort_tentative_parse (parser);
16236
16237                       parser->in_template_argument_list_p = saved_italp;
16238                       break;
16239                     }
16240                 case CPP_CLOSE_PAREN:
16241                 case CPP_ELLIPSIS:
16242                   /* If we run into a non-nested `;', `}', or `]',
16243                      then the code is invalid -- but the default
16244                      argument is certainly over.  */
16245                 case CPP_SEMICOLON:
16246                 case CPP_CLOSE_BRACE:
16247                 case CPP_CLOSE_SQUARE:
16248                   if (depth == 0)
16249                     done = true;
16250                   /* Update DEPTH, if necessary.  */
16251                   else if (token->type == CPP_CLOSE_PAREN
16252                            || token->type == CPP_CLOSE_BRACE
16253                            || token->type == CPP_CLOSE_SQUARE)
16254                     --depth;
16255                   break;
16256
16257                 case CPP_OPEN_PAREN:
16258                 case CPP_OPEN_SQUARE:
16259                 case CPP_OPEN_BRACE:
16260                   ++depth;
16261                   break;
16262
16263                 case CPP_LESS:
16264                   if (depth == 0)
16265                     /* This might be the comparison operator, or it might
16266                        start a template argument list.  */
16267                     ++maybe_template_id;
16268                   break;
16269
16270                 case CPP_RSHIFT:
16271                   if (cxx_dialect == cxx98)
16272                     break;
16273                   /* Fall through for C++0x, which treats the `>>'
16274                      operator like two `>' tokens in certain
16275                      cases.  */
16276
16277                 case CPP_GREATER:
16278                   if (depth == 0)
16279                     {
16280                       /* This might be an operator, or it might close a
16281                          template argument list.  But if a previous '<'
16282                          started a template argument list, this will have
16283                          closed it, so we can't be in one anymore.  */
16284                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16285                       if (maybe_template_id < 0)
16286                         maybe_template_id = 0;
16287                     }
16288                   break;
16289
16290                   /* If we run out of tokens, issue an error message.  */
16291                 case CPP_EOF:
16292                 case CPP_PRAGMA_EOL:
16293                   error_at (token->location, "file ends in default argument");
16294                   done = true;
16295                   break;
16296
16297                 case CPP_NAME:
16298                 case CPP_SCOPE:
16299                   /* In these cases, we should look for template-ids.
16300                      For example, if the default argument is
16301                      `X<int, double>()', we need to do name lookup to
16302                      figure out whether or not `X' is a template; if
16303                      so, the `,' does not end the default argument.
16304
16305                      That is not yet done.  */
16306                   break;
16307
16308                 default:
16309                   break;
16310                 }
16311
16312               /* If we've reached the end, stop.  */
16313               if (done)
16314                 break;
16315
16316               /* Add the token to the token block.  */
16317               token = cp_lexer_consume_token (parser->lexer);
16318             }
16319
16320           /* Create a DEFAULT_ARG to represent the unparsed default
16321              argument.  */
16322           default_argument = make_node (DEFAULT_ARG);
16323           DEFARG_TOKENS (default_argument)
16324             = cp_token_cache_new (first_token, token);
16325           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16326         }
16327       /* Outside of a class definition, we can just parse the
16328          assignment-expression.  */
16329       else
16330         {
16331           token = cp_lexer_peek_token (parser->lexer);
16332           default_argument 
16333             = cp_parser_default_argument (parser, template_parm_p);
16334         }
16335
16336       if (!parser->default_arg_ok_p)
16337         {
16338           if (flag_permissive)
16339             warning (0, "deprecated use of default argument for parameter of non-function");
16340           else
16341             {
16342               error_at (token->location,
16343                         "default arguments are only "
16344                         "permitted for function parameters");
16345               default_argument = NULL_TREE;
16346             }
16347         }
16348       else if ((declarator && declarator->parameter_pack_p)
16349                || (decl_specifiers.type
16350                    && PACK_EXPANSION_P (decl_specifiers.type)))
16351         {
16352           /* Find the name of the parameter pack.  */     
16353           cp_declarator *id_declarator = declarator;
16354           while (id_declarator && id_declarator->kind != cdk_id)
16355             id_declarator = id_declarator->declarator;
16356           
16357           if (id_declarator && id_declarator->kind == cdk_id)
16358             error_at (declarator_token_start->location,
16359                       template_parm_p 
16360                       ? "template parameter pack %qD"
16361                       " cannot have a default argument"
16362                       : "parameter pack %qD cannot have a default argument",
16363                       id_declarator->u.id.unqualified_name);
16364           else
16365             error_at (declarator_token_start->location,
16366                       template_parm_p 
16367                       ? "template parameter pack cannot have a default argument"
16368                       : "parameter pack cannot have a default argument");
16369           
16370           default_argument = NULL_TREE;
16371         }
16372     }
16373   else
16374     default_argument = NULL_TREE;
16375
16376   return make_parameter_declarator (&decl_specifiers,
16377                                     declarator,
16378                                     default_argument);
16379 }
16380
16381 /* Parse a default argument and return it.
16382
16383    TEMPLATE_PARM_P is true if this is a default argument for a
16384    non-type template parameter.  */
16385 static tree
16386 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16387 {
16388   tree default_argument = NULL_TREE;
16389   bool saved_greater_than_is_operator_p;
16390   bool saved_local_variables_forbidden_p;
16391
16392   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16393      set correctly.  */
16394   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16395   parser->greater_than_is_operator_p = !template_parm_p;
16396   /* Local variable names (and the `this' keyword) may not
16397      appear in a default argument.  */
16398   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16399   parser->local_variables_forbidden_p = true;
16400   /* Parse the assignment-expression.  */
16401   if (template_parm_p)
16402     push_deferring_access_checks (dk_no_deferred);
16403   default_argument
16404     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16405   if (template_parm_p)
16406     pop_deferring_access_checks ();
16407   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16408   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16409
16410   return default_argument;
16411 }
16412
16413 /* Parse a function-body.
16414
16415    function-body:
16416      compound_statement  */
16417
16418 static void
16419 cp_parser_function_body (cp_parser *parser)
16420 {
16421   cp_parser_compound_statement (parser, NULL, false);
16422 }
16423
16424 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16425    true if a ctor-initializer was present.  */
16426
16427 static bool
16428 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16429 {
16430   tree body, list;
16431   bool ctor_initializer_p;
16432   const bool check_body_p =
16433      DECL_CONSTRUCTOR_P (current_function_decl)
16434      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16435   tree last = NULL;
16436
16437   /* Begin the function body.  */
16438   body = begin_function_body ();
16439   /* Parse the optional ctor-initializer.  */
16440   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16441
16442   /* If we're parsing a constexpr constructor definition, we need
16443      to check that the constructor body is indeed empty.  However,
16444      before we get to cp_parser_function_body lot of junk has been
16445      generated, so we can't just check that we have an empty block.
16446      Rather we take a snapshot of the outermost block, and check whether
16447      cp_parser_function_body changed its state.  */
16448   if (check_body_p)
16449     {
16450       list = body;
16451       if (TREE_CODE (list) == BIND_EXPR)
16452         list = BIND_EXPR_BODY (list);
16453       if (TREE_CODE (list) == STATEMENT_LIST
16454           && STATEMENT_LIST_TAIL (list) != NULL)
16455         last = STATEMENT_LIST_TAIL (list)->stmt;
16456     }
16457   /* Parse the function-body.  */
16458   cp_parser_function_body (parser);
16459   if (check_body_p)
16460     check_constexpr_ctor_body (last, list);
16461   /* Finish the function body.  */
16462   finish_function_body (body);
16463
16464   return ctor_initializer_p;
16465 }
16466
16467 /* Parse an initializer.
16468
16469    initializer:
16470      = initializer-clause
16471      ( expression-list )
16472
16473    Returns an expression representing the initializer.  If no
16474    initializer is present, NULL_TREE is returned.
16475
16476    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16477    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16478    set to TRUE if there is no initializer present.  If there is an
16479    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16480    is set to true; otherwise it is set to false.  */
16481
16482 static tree
16483 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16484                        bool* non_constant_p)
16485 {
16486   cp_token *token;
16487   tree init;
16488
16489   /* Peek at the next token.  */
16490   token = cp_lexer_peek_token (parser->lexer);
16491
16492   /* Let our caller know whether or not this initializer was
16493      parenthesized.  */
16494   *is_direct_init = (token->type != CPP_EQ);
16495   /* Assume that the initializer is constant.  */
16496   *non_constant_p = false;
16497
16498   if (token->type == CPP_EQ)
16499     {
16500       /* Consume the `='.  */
16501       cp_lexer_consume_token (parser->lexer);
16502       /* Parse the initializer-clause.  */
16503       init = cp_parser_initializer_clause (parser, non_constant_p);
16504     }
16505   else if (token->type == CPP_OPEN_PAREN)
16506     {
16507       VEC(tree,gc) *vec;
16508       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16509                                                      /*cast_p=*/false,
16510                                                      /*allow_expansion_p=*/true,
16511                                                      non_constant_p);
16512       if (vec == NULL)
16513         return error_mark_node;
16514       init = build_tree_list_vec (vec);
16515       release_tree_vector (vec);
16516     }
16517   else if (token->type == CPP_OPEN_BRACE)
16518     {
16519       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16520       init = cp_parser_braced_list (parser, non_constant_p);
16521       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16522     }
16523   else
16524     {
16525       /* Anything else is an error.  */
16526       cp_parser_error (parser, "expected initializer");
16527       init = error_mark_node;
16528     }
16529
16530   return init;
16531 }
16532
16533 /* Parse an initializer-clause.
16534
16535    initializer-clause:
16536      assignment-expression
16537      braced-init-list
16538
16539    Returns an expression representing the initializer.
16540
16541    If the `assignment-expression' production is used the value
16542    returned is simply a representation for the expression.
16543
16544    Otherwise, calls cp_parser_braced_list.  */
16545
16546 static tree
16547 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16548 {
16549   tree initializer;
16550
16551   /* Assume the expression is constant.  */
16552   *non_constant_p = false;
16553
16554   /* If it is not a `{', then we are looking at an
16555      assignment-expression.  */
16556   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16557     {
16558       initializer
16559         = cp_parser_constant_expression (parser,
16560                                         /*allow_non_constant_p=*/true,
16561                                         non_constant_p);
16562       if (!*non_constant_p)
16563         {
16564           /* We only want to fold if this is really a constant
16565              expression.  FIXME Actually, we don't want to fold here, but in
16566              cp_finish_decl.  */
16567           tree folded = fold_non_dependent_expr (initializer);
16568           folded = maybe_constant_value (folded);
16569           if (TREE_CONSTANT (folded))
16570             initializer = folded;
16571         }
16572     }
16573   else
16574     initializer = cp_parser_braced_list (parser, non_constant_p);
16575
16576   return initializer;
16577 }
16578
16579 /* Parse a brace-enclosed initializer list.
16580
16581    braced-init-list:
16582      { initializer-list , [opt] }
16583      { }
16584
16585    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16586    the elements of the initializer-list (or NULL, if the last
16587    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16588    NULL_TREE.  There is no way to detect whether or not the optional
16589    trailing `,' was provided.  NON_CONSTANT_P is as for
16590    cp_parser_initializer.  */     
16591
16592 static tree
16593 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16594 {
16595   tree initializer;
16596
16597   /* Consume the `{' token.  */
16598   cp_lexer_consume_token (parser->lexer);
16599   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16600   initializer = make_node (CONSTRUCTOR);
16601   /* If it's not a `}', then there is a non-trivial initializer.  */
16602   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16603     {
16604       /* Parse the initializer list.  */
16605       CONSTRUCTOR_ELTS (initializer)
16606         = cp_parser_initializer_list (parser, non_constant_p);
16607       /* A trailing `,' token is allowed.  */
16608       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16609         cp_lexer_consume_token (parser->lexer);
16610     }
16611   /* Now, there should be a trailing `}'.  */
16612   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16613   TREE_TYPE (initializer) = init_list_type_node;
16614   return initializer;
16615 }
16616
16617 /* Parse an initializer-list.
16618
16619    initializer-list:
16620      initializer-clause ... [opt]
16621      initializer-list , initializer-clause ... [opt]
16622
16623    GNU Extension:
16624
16625    initializer-list:
16626      identifier : initializer-clause
16627      initializer-list, identifier : initializer-clause
16628
16629    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16630    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16631    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16632    as for cp_parser_initializer.  */
16633
16634 static VEC(constructor_elt,gc) *
16635 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16636 {
16637   VEC(constructor_elt,gc) *v = NULL;
16638
16639   /* Assume all of the expressions are constant.  */
16640   *non_constant_p = false;
16641
16642   /* Parse the rest of the list.  */
16643   while (true)
16644     {
16645       cp_token *token;
16646       tree identifier;
16647       tree initializer;
16648       bool clause_non_constant_p;
16649
16650       /* If the next token is an identifier and the following one is a
16651          colon, we are looking at the GNU designated-initializer
16652          syntax.  */
16653       if (cp_parser_allow_gnu_extensions_p (parser)
16654           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16655           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16656         {
16657           /* Warn the user that they are using an extension.  */
16658           pedwarn (input_location, OPT_pedantic, 
16659                    "ISO C++ does not allow designated initializers");
16660           /* Consume the identifier.  */
16661           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16662           /* Consume the `:'.  */
16663           cp_lexer_consume_token (parser->lexer);
16664         }
16665       else
16666         identifier = NULL_TREE;
16667
16668       /* Parse the initializer.  */
16669       initializer = cp_parser_initializer_clause (parser,
16670                                                   &clause_non_constant_p);
16671       /* If any clause is non-constant, so is the entire initializer.  */
16672       if (clause_non_constant_p)
16673         *non_constant_p = true;
16674
16675       /* If we have an ellipsis, this is an initializer pack
16676          expansion.  */
16677       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16678         {
16679           /* Consume the `...'.  */
16680           cp_lexer_consume_token (parser->lexer);
16681
16682           /* Turn the initializer into an initializer expansion.  */
16683           initializer = make_pack_expansion (initializer);
16684         }
16685
16686       /* Add it to the vector.  */
16687       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16688
16689       /* If the next token is not a comma, we have reached the end of
16690          the list.  */
16691       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16692         break;
16693
16694       /* Peek at the next token.  */
16695       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16696       /* If the next token is a `}', then we're still done.  An
16697          initializer-clause can have a trailing `,' after the
16698          initializer-list and before the closing `}'.  */
16699       if (token->type == CPP_CLOSE_BRACE)
16700         break;
16701
16702       /* Consume the `,' token.  */
16703       cp_lexer_consume_token (parser->lexer);
16704     }
16705
16706   return v;
16707 }
16708
16709 /* Classes [gram.class] */
16710
16711 /* Parse a class-name.
16712
16713    class-name:
16714      identifier
16715      template-id
16716
16717    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16718    to indicate that names looked up in dependent types should be
16719    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16720    keyword has been used to indicate that the name that appears next
16721    is a template.  TAG_TYPE indicates the explicit tag given before
16722    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16723    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16724    is the class being defined in a class-head.
16725
16726    Returns the TYPE_DECL representing the class.  */
16727
16728 static tree
16729 cp_parser_class_name (cp_parser *parser,
16730                       bool typename_keyword_p,
16731                       bool template_keyword_p,
16732                       enum tag_types tag_type,
16733                       bool check_dependency_p,
16734                       bool class_head_p,
16735                       bool is_declaration)
16736 {
16737   tree decl;
16738   tree scope;
16739   bool typename_p;
16740   cp_token *token;
16741   tree identifier = NULL_TREE;
16742
16743   /* All class-names start with an identifier.  */
16744   token = cp_lexer_peek_token (parser->lexer);
16745   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16746     {
16747       cp_parser_error (parser, "expected class-name");
16748       return error_mark_node;
16749     }
16750
16751   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16752      to a template-id, so we save it here.  */
16753   scope = parser->scope;
16754   if (scope == error_mark_node)
16755     return error_mark_node;
16756
16757   /* Any name names a type if we're following the `typename' keyword
16758      in a qualified name where the enclosing scope is type-dependent.  */
16759   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16760                 && dependent_type_p (scope));
16761   /* Handle the common case (an identifier, but not a template-id)
16762      efficiently.  */
16763   if (token->type == CPP_NAME
16764       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16765     {
16766       cp_token *identifier_token;
16767       bool ambiguous_p;
16768
16769       /* Look for the identifier.  */
16770       identifier_token = cp_lexer_peek_token (parser->lexer);
16771       ambiguous_p = identifier_token->ambiguous_p;
16772       identifier = cp_parser_identifier (parser);
16773       /* If the next token isn't an identifier, we are certainly not
16774          looking at a class-name.  */
16775       if (identifier == error_mark_node)
16776         decl = error_mark_node;
16777       /* If we know this is a type-name, there's no need to look it
16778          up.  */
16779       else if (typename_p)
16780         decl = identifier;
16781       else
16782         {
16783           tree ambiguous_decls;
16784           /* If we already know that this lookup is ambiguous, then
16785              we've already issued an error message; there's no reason
16786              to check again.  */
16787           if (ambiguous_p)
16788             {
16789               cp_parser_simulate_error (parser);
16790               return error_mark_node;
16791             }
16792           /* If the next token is a `::', then the name must be a type
16793              name.
16794
16795              [basic.lookup.qual]
16796
16797              During the lookup for a name preceding the :: scope
16798              resolution operator, object, function, and enumerator
16799              names are ignored.  */
16800           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16801             tag_type = typename_type;
16802           /* Look up the name.  */
16803           decl = cp_parser_lookup_name (parser, identifier,
16804                                         tag_type,
16805                                         /*is_template=*/false,
16806                                         /*is_namespace=*/false,
16807                                         check_dependency_p,
16808                                         &ambiguous_decls,
16809                                         identifier_token->location);
16810           if (ambiguous_decls)
16811             {
16812               if (cp_parser_parsing_tentatively (parser))
16813                 cp_parser_simulate_error (parser);
16814               return error_mark_node;
16815             }
16816         }
16817     }
16818   else
16819     {
16820       /* Try a template-id.  */
16821       decl = cp_parser_template_id (parser, template_keyword_p,
16822                                     check_dependency_p,
16823                                     is_declaration);
16824       if (decl == error_mark_node)
16825         return error_mark_node;
16826     }
16827
16828   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16829
16830   /* If this is a typename, create a TYPENAME_TYPE.  */
16831   if (typename_p && decl != error_mark_node)
16832     {
16833       decl = make_typename_type (scope, decl, typename_type,
16834                                  /*complain=*/tf_error);
16835       if (decl != error_mark_node)
16836         decl = TYPE_NAME (decl);
16837     }
16838
16839   /* Check to see that it is really the name of a class.  */
16840   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16841       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16842       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16843     /* Situations like this:
16844
16845          template <typename T> struct A {
16846            typename T::template X<int>::I i;
16847          };
16848
16849        are problematic.  Is `T::template X<int>' a class-name?  The
16850        standard does not seem to be definitive, but there is no other
16851        valid interpretation of the following `::'.  Therefore, those
16852        names are considered class-names.  */
16853     {
16854       decl = make_typename_type (scope, decl, tag_type, tf_error);
16855       if (decl != error_mark_node)
16856         decl = TYPE_NAME (decl);
16857     }
16858   else if (TREE_CODE (decl) != TYPE_DECL
16859            || TREE_TYPE (decl) == error_mark_node
16860            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16861            /* In Objective-C 2.0, a classname followed by '.' starts a
16862               dot-syntax expression, and it's not a type-name.  */
16863            || (c_dialect_objc ()
16864                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16865                && objc_is_class_name (decl)))
16866     decl = error_mark_node;
16867
16868   if (decl == error_mark_node)
16869     cp_parser_error (parser, "expected class-name");
16870   else if (identifier && !parser->scope)
16871     maybe_note_name_used_in_class (identifier, decl);
16872
16873   return decl;
16874 }
16875
16876 /* Parse a class-specifier.
16877
16878    class-specifier:
16879      class-head { member-specification [opt] }
16880
16881    Returns the TREE_TYPE representing the class.  */
16882
16883 static tree
16884 cp_parser_class_specifier (cp_parser* parser)
16885 {
16886   tree type;
16887   tree attributes = NULL_TREE;
16888   bool nested_name_specifier_p;
16889   unsigned saved_num_template_parameter_lists;
16890   bool saved_in_function_body;
16891   bool saved_in_unbraced_linkage_specification_p;
16892   tree old_scope = NULL_TREE;
16893   tree scope = NULL_TREE;
16894   tree bases;
16895   cp_token *closing_brace;
16896
16897   push_deferring_access_checks (dk_no_deferred);
16898
16899   /* Parse the class-head.  */
16900   type = cp_parser_class_head (parser,
16901                                &nested_name_specifier_p,
16902                                &attributes,
16903                                &bases);
16904   /* If the class-head was a semantic disaster, skip the entire body
16905      of the class.  */
16906   if (!type)
16907     {
16908       cp_parser_skip_to_end_of_block_or_statement (parser);
16909       pop_deferring_access_checks ();
16910       return error_mark_node;
16911     }
16912
16913   /* Look for the `{'.  */
16914   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16915     {
16916       pop_deferring_access_checks ();
16917       return error_mark_node;
16918     }
16919
16920   /* Process the base classes. If they're invalid, skip the 
16921      entire class body.  */
16922   if (!xref_basetypes (type, bases))
16923     {
16924       /* Consuming the closing brace yields better error messages
16925          later on.  */
16926       if (cp_parser_skip_to_closing_brace (parser))
16927         cp_lexer_consume_token (parser->lexer);
16928       pop_deferring_access_checks ();
16929       return error_mark_node;
16930     }
16931
16932   /* Issue an error message if type-definitions are forbidden here.  */
16933   cp_parser_check_type_definition (parser);
16934   /* Remember that we are defining one more class.  */
16935   ++parser->num_classes_being_defined;
16936   /* Inside the class, surrounding template-parameter-lists do not
16937      apply.  */
16938   saved_num_template_parameter_lists
16939     = parser->num_template_parameter_lists;
16940   parser->num_template_parameter_lists = 0;
16941   /* We are not in a function body.  */
16942   saved_in_function_body = parser->in_function_body;
16943   parser->in_function_body = false;
16944   /* We are not immediately inside an extern "lang" block.  */
16945   saved_in_unbraced_linkage_specification_p
16946     = parser->in_unbraced_linkage_specification_p;
16947   parser->in_unbraced_linkage_specification_p = false;
16948
16949   /* Start the class.  */
16950   if (nested_name_specifier_p)
16951     {
16952       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16953       old_scope = push_inner_scope (scope);
16954     }
16955   type = begin_class_definition (type, attributes);
16956
16957   if (type == error_mark_node)
16958     /* If the type is erroneous, skip the entire body of the class.  */
16959     cp_parser_skip_to_closing_brace (parser);
16960   else
16961     /* Parse the member-specification.  */
16962     cp_parser_member_specification_opt (parser);
16963
16964   /* Look for the trailing `}'.  */
16965   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16966   /* Look for trailing attributes to apply to this class.  */
16967   if (cp_parser_allow_gnu_extensions_p (parser))
16968     attributes = cp_parser_attributes_opt (parser);
16969   if (type != error_mark_node)
16970     type = finish_struct (type, attributes);
16971   if (nested_name_specifier_p)
16972     pop_inner_scope (old_scope, scope);
16973
16974   /* We've finished a type definition.  Check for the common syntax
16975      error of forgetting a semicolon after the definition.  We need to
16976      be careful, as we can't just check for not-a-semicolon and be done
16977      with it; the user might have typed:
16978
16979      class X { } c = ...;
16980      class X { } *p = ...;
16981
16982      and so forth.  Instead, enumerate all the possible tokens that
16983      might follow this production; if we don't see one of them, then
16984      complain and silently insert the semicolon.  */
16985   {
16986     cp_token *token = cp_lexer_peek_token (parser->lexer);
16987     bool want_semicolon = true;
16988
16989     switch (token->type)
16990       {
16991       case CPP_NAME:
16992       case CPP_SEMICOLON:
16993       case CPP_MULT:
16994       case CPP_AND:
16995       case CPP_OPEN_PAREN:
16996       case CPP_CLOSE_PAREN:
16997       case CPP_COMMA:
16998         want_semicolon = false;
16999         break;
17000
17001         /* While it's legal for type qualifiers and storage class
17002            specifiers to follow type definitions in the grammar, only
17003            compiler testsuites contain code like that.  Assume that if
17004            we see such code, then what we're really seeing is a case
17005            like:
17006
17007            class X { }
17008            const <type> var = ...;
17009
17010            or
17011
17012            class Y { }
17013            static <type> func (...) ...
17014
17015            i.e. the qualifier or specifier applies to the next
17016            declaration.  To do so, however, we need to look ahead one
17017            more token to see if *that* token is a type specifier.
17018
17019            This code could be improved to handle:
17020
17021            class Z { }
17022            static const <type> var = ...;  */
17023       case CPP_KEYWORD:
17024         if (keyword_is_decl_specifier (token->keyword))
17025           {
17026             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17027
17028             /* Handling user-defined types here would be nice, but very
17029                tricky.  */
17030             want_semicolon
17031               = (lookahead->type == CPP_KEYWORD
17032                  && keyword_begins_type_specifier (lookahead->keyword));
17033           }
17034         break;
17035       default:
17036         break;
17037       }
17038
17039     /* If we don't have a type, then something is very wrong and we
17040        shouldn't try to do anything clever.  Likewise for not seeing the
17041        closing brace.  */
17042     if (closing_brace && TYPE_P (type) && want_semicolon)
17043       {
17044         cp_token_position prev
17045           = cp_lexer_previous_token_position (parser->lexer);
17046         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17047         location_t loc = prev_token->location;
17048
17049         if (CLASSTYPE_DECLARED_CLASS (type))
17050           error_at (loc, "expected %<;%> after class definition");
17051         else if (TREE_CODE (type) == RECORD_TYPE)
17052           error_at (loc, "expected %<;%> after struct definition");
17053         else if (TREE_CODE (type) == UNION_TYPE)
17054           error_at (loc, "expected %<;%> after union definition");
17055         else
17056           gcc_unreachable ();
17057
17058         /* Unget one token and smash it to look as though we encountered
17059            a semicolon in the input stream.  */
17060         cp_lexer_set_token_position (parser->lexer, prev);
17061         token = cp_lexer_peek_token (parser->lexer);
17062         token->type = CPP_SEMICOLON;
17063         token->keyword = RID_MAX;
17064       }
17065   }
17066
17067   /* If this class is not itself within the scope of another class,
17068      then we need to parse the bodies of all of the queued function
17069      definitions.  Note that the queued functions defined in a class
17070      are not always processed immediately following the
17071      class-specifier for that class.  Consider:
17072
17073        struct A {
17074          struct B { void f() { sizeof (A); } };
17075        };
17076
17077      If `f' were processed before the processing of `A' were
17078      completed, there would be no way to compute the size of `A'.
17079      Note that the nesting we are interested in here is lexical --
17080      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17081      for:
17082
17083        struct A { struct B; };
17084        struct A::B { void f() { } };
17085
17086      there is no need to delay the parsing of `A::B::f'.  */
17087   if (--parser->num_classes_being_defined == 0)
17088     {
17089       tree fn;
17090       tree class_type = NULL_TREE;
17091       tree pushed_scope = NULL_TREE;
17092       unsigned ix;
17093       cp_default_arg_entry *e;
17094
17095       /* In a first pass, parse default arguments to the functions.
17096          Then, in a second pass, parse the bodies of the functions.
17097          This two-phased approach handles cases like:
17098
17099             struct S {
17100               void f() { g(); }
17101               void g(int i = 3);
17102             };
17103
17104          */
17105       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17106                         ix, e)
17107         {
17108           fn = e->decl;
17109           /* If there are default arguments that have not yet been processed,
17110              take care of them now.  */
17111           if (class_type != e->class_type)
17112             {
17113               if (pushed_scope)
17114                 pop_scope (pushed_scope);
17115               class_type = e->class_type;
17116               pushed_scope = push_scope (class_type);
17117             }
17118           /* Make sure that any template parameters are in scope.  */
17119           maybe_begin_member_template_processing (fn);
17120           /* Parse the default argument expressions.  */
17121           cp_parser_late_parsing_default_args (parser, fn);
17122           /* Remove any template parameters from the symbol table.  */
17123           maybe_end_member_template_processing ();
17124         }
17125       if (pushed_scope)
17126         pop_scope (pushed_scope);
17127       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17128       /* Now parse the body of the functions.  */
17129       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17130         cp_parser_late_parsing_for_member (parser, fn);
17131       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17132     }
17133
17134   /* Put back any saved access checks.  */
17135   pop_deferring_access_checks ();
17136
17137   /* Restore saved state.  */
17138   parser->in_function_body = saved_in_function_body;
17139   parser->num_template_parameter_lists
17140     = saved_num_template_parameter_lists;
17141   parser->in_unbraced_linkage_specification_p
17142     = saved_in_unbraced_linkage_specification_p;
17143
17144   return type;
17145 }
17146
17147 /* Parse a class-head.
17148
17149    class-head:
17150      class-key identifier [opt] base-clause [opt]
17151      class-key nested-name-specifier identifier base-clause [opt]
17152      class-key nested-name-specifier [opt] template-id
17153        base-clause [opt]
17154
17155    GNU Extensions:
17156      class-key attributes identifier [opt] base-clause [opt]
17157      class-key attributes nested-name-specifier identifier base-clause [opt]
17158      class-key attributes nested-name-specifier [opt] template-id
17159        base-clause [opt]
17160
17161    Upon return BASES is initialized to the list of base classes (or
17162    NULL, if there are none) in the same form returned by
17163    cp_parser_base_clause.
17164
17165    Returns the TYPE of the indicated class.  Sets
17166    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17167    involving a nested-name-specifier was used, and FALSE otherwise.
17168
17169    Returns error_mark_node if this is not a class-head.
17170
17171    Returns NULL_TREE if the class-head is syntactically valid, but
17172    semantically invalid in a way that means we should skip the entire
17173    body of the class.  */
17174
17175 static tree
17176 cp_parser_class_head (cp_parser* parser,
17177                       bool* nested_name_specifier_p,
17178                       tree *attributes_p,
17179                       tree *bases)
17180 {
17181   tree nested_name_specifier;
17182   enum tag_types class_key;
17183   tree id = NULL_TREE;
17184   tree type = NULL_TREE;
17185   tree attributes;
17186   bool template_id_p = false;
17187   bool qualified_p = false;
17188   bool invalid_nested_name_p = false;
17189   bool invalid_explicit_specialization_p = false;
17190   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17191   tree pushed_scope = NULL_TREE;
17192   unsigned num_templates;
17193   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17194   /* Assume no nested-name-specifier will be present.  */
17195   *nested_name_specifier_p = false;
17196   /* Assume no template parameter lists will be used in defining the
17197      type.  */
17198   num_templates = 0;
17199   parser->colon_corrects_to_scope_p = false;
17200
17201   *bases = NULL_TREE;
17202
17203   /* Look for the class-key.  */
17204   class_key = cp_parser_class_key (parser);
17205   if (class_key == none_type)
17206     return error_mark_node;
17207
17208   /* Parse the attributes.  */
17209   attributes = cp_parser_attributes_opt (parser);
17210
17211   /* If the next token is `::', that is invalid -- but sometimes
17212      people do try to write:
17213
17214        struct ::S {};
17215
17216      Handle this gracefully by accepting the extra qualifier, and then
17217      issuing an error about it later if this really is a
17218      class-head.  If it turns out just to be an elaborated type
17219      specifier, remain silent.  */
17220   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17221     qualified_p = true;
17222
17223   push_deferring_access_checks (dk_no_check);
17224
17225   /* Determine the name of the class.  Begin by looking for an
17226      optional nested-name-specifier.  */
17227   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17228   nested_name_specifier
17229     = cp_parser_nested_name_specifier_opt (parser,
17230                                            /*typename_keyword_p=*/false,
17231                                            /*check_dependency_p=*/false,
17232                                            /*type_p=*/false,
17233                                            /*is_declaration=*/false);
17234   /* If there was a nested-name-specifier, then there *must* be an
17235      identifier.  */
17236   if (nested_name_specifier)
17237     {
17238       type_start_token = cp_lexer_peek_token (parser->lexer);
17239       /* Although the grammar says `identifier', it really means
17240          `class-name' or `template-name'.  You are only allowed to
17241          define a class that has already been declared with this
17242          syntax.
17243
17244          The proposed resolution for Core Issue 180 says that wherever
17245          you see `class T::X' you should treat `X' as a type-name.
17246
17247          It is OK to define an inaccessible class; for example:
17248
17249            class A { class B; };
17250            class A::B {};
17251
17252          We do not know if we will see a class-name, or a
17253          template-name.  We look for a class-name first, in case the
17254          class-name is a template-id; if we looked for the
17255          template-name first we would stop after the template-name.  */
17256       cp_parser_parse_tentatively (parser);
17257       type = cp_parser_class_name (parser,
17258                                    /*typename_keyword_p=*/false,
17259                                    /*template_keyword_p=*/false,
17260                                    class_type,
17261                                    /*check_dependency_p=*/false,
17262                                    /*class_head_p=*/true,
17263                                    /*is_declaration=*/false);
17264       /* If that didn't work, ignore the nested-name-specifier.  */
17265       if (!cp_parser_parse_definitely (parser))
17266         {
17267           invalid_nested_name_p = true;
17268           type_start_token = cp_lexer_peek_token (parser->lexer);
17269           id = cp_parser_identifier (parser);
17270           if (id == error_mark_node)
17271             id = NULL_TREE;
17272         }
17273       /* If we could not find a corresponding TYPE, treat this
17274          declaration like an unqualified declaration.  */
17275       if (type == error_mark_node)
17276         nested_name_specifier = NULL_TREE;
17277       /* Otherwise, count the number of templates used in TYPE and its
17278          containing scopes.  */
17279       else
17280         {
17281           tree scope;
17282
17283           for (scope = TREE_TYPE (type);
17284                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17285                scope = (TYPE_P (scope)
17286                         ? TYPE_CONTEXT (scope)
17287                         : DECL_CONTEXT (scope)))
17288             if (TYPE_P (scope)
17289                 && CLASS_TYPE_P (scope)
17290                 && CLASSTYPE_TEMPLATE_INFO (scope)
17291                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17292                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17293               ++num_templates;
17294         }
17295     }
17296   /* Otherwise, the identifier is optional.  */
17297   else
17298     {
17299       /* We don't know whether what comes next is a template-id,
17300          an identifier, or nothing at all.  */
17301       cp_parser_parse_tentatively (parser);
17302       /* Check for a template-id.  */
17303       type_start_token = cp_lexer_peek_token (parser->lexer);
17304       id = cp_parser_template_id (parser,
17305                                   /*template_keyword_p=*/false,
17306                                   /*check_dependency_p=*/true,
17307                                   /*is_declaration=*/true);
17308       /* If that didn't work, it could still be an identifier.  */
17309       if (!cp_parser_parse_definitely (parser))
17310         {
17311           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17312             {
17313               type_start_token = cp_lexer_peek_token (parser->lexer);
17314               id = cp_parser_identifier (parser);
17315             }
17316           else
17317             id = NULL_TREE;
17318         }
17319       else
17320         {
17321           template_id_p = true;
17322           ++num_templates;
17323         }
17324     }
17325
17326   pop_deferring_access_checks ();
17327
17328   if (id)
17329     cp_parser_check_for_invalid_template_id (parser, id,
17330                                              type_start_token->location);
17331
17332   /* If it's not a `:' or a `{' then we can't really be looking at a
17333      class-head, since a class-head only appears as part of a
17334      class-specifier.  We have to detect this situation before calling
17335      xref_tag, since that has irreversible side-effects.  */
17336   if (!cp_parser_next_token_starts_class_definition_p (parser))
17337     {
17338       cp_parser_error (parser, "expected %<{%> or %<:%>");
17339       type = error_mark_node;
17340       goto out;
17341     }
17342
17343   /* At this point, we're going ahead with the class-specifier, even
17344      if some other problem occurs.  */
17345   cp_parser_commit_to_tentative_parse (parser);
17346   /* Issue the error about the overly-qualified name now.  */
17347   if (qualified_p)
17348     {
17349       cp_parser_error (parser,
17350                        "global qualification of class name is invalid");
17351       type = error_mark_node;
17352       goto out;
17353     }
17354   else if (invalid_nested_name_p)
17355     {
17356       cp_parser_error (parser,
17357                        "qualified name does not name a class");
17358       type = error_mark_node;
17359       goto out;
17360     }
17361   else if (nested_name_specifier)
17362     {
17363       tree scope;
17364
17365       /* Reject typedef-names in class heads.  */
17366       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17367         {
17368           error_at (type_start_token->location,
17369                     "invalid class name in declaration of %qD",
17370                     type);
17371           type = NULL_TREE;
17372           goto done;
17373         }
17374
17375       /* Figure out in what scope the declaration is being placed.  */
17376       scope = current_scope ();
17377       /* If that scope does not contain the scope in which the
17378          class was originally declared, the program is invalid.  */
17379       if (scope && !is_ancestor (scope, nested_name_specifier))
17380         {
17381           if (at_namespace_scope_p ())
17382             error_at (type_start_token->location,
17383                       "declaration of %qD in namespace %qD which does not "
17384                       "enclose %qD",
17385                       type, scope, nested_name_specifier);
17386           else
17387             error_at (type_start_token->location,
17388                       "declaration of %qD in %qD which does not enclose %qD",
17389                       type, scope, nested_name_specifier);
17390           type = NULL_TREE;
17391           goto done;
17392         }
17393       /* [dcl.meaning]
17394
17395          A declarator-id shall not be qualified except for the
17396          definition of a ... nested class outside of its class
17397          ... [or] the definition or explicit instantiation of a
17398          class member of a namespace outside of its namespace.  */
17399       if (scope == nested_name_specifier)
17400         {
17401           permerror (nested_name_specifier_token_start->location,
17402                      "extra qualification not allowed");
17403           nested_name_specifier = NULL_TREE;
17404           num_templates = 0;
17405         }
17406     }
17407   /* An explicit-specialization must be preceded by "template <>".  If
17408      it is not, try to recover gracefully.  */
17409   if (at_namespace_scope_p ()
17410       && parser->num_template_parameter_lists == 0
17411       && template_id_p)
17412     {
17413       error_at (type_start_token->location,
17414                 "an explicit specialization must be preceded by %<template <>%>");
17415       invalid_explicit_specialization_p = true;
17416       /* Take the same action that would have been taken by
17417          cp_parser_explicit_specialization.  */
17418       ++parser->num_template_parameter_lists;
17419       begin_specialization ();
17420     }
17421   /* There must be no "return" statements between this point and the
17422      end of this function; set "type "to the correct return value and
17423      use "goto done;" to return.  */
17424   /* Make sure that the right number of template parameters were
17425      present.  */
17426   if (!cp_parser_check_template_parameters (parser, num_templates,
17427                                             type_start_token->location,
17428                                             /*declarator=*/NULL))
17429     {
17430       /* If something went wrong, there is no point in even trying to
17431          process the class-definition.  */
17432       type = NULL_TREE;
17433       goto done;
17434     }
17435
17436   /* Look up the type.  */
17437   if (template_id_p)
17438     {
17439       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17440           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17441               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17442         {
17443           error_at (type_start_token->location,
17444                     "function template %qD redeclared as a class template", id);
17445           type = error_mark_node;
17446         }
17447       else
17448         {
17449           type = TREE_TYPE (id);
17450           type = maybe_process_partial_specialization (type);
17451         }
17452       if (nested_name_specifier)
17453         pushed_scope = push_scope (nested_name_specifier);
17454     }
17455   else if (nested_name_specifier)
17456     {
17457       tree class_type;
17458
17459       /* Given:
17460
17461             template <typename T> struct S { struct T };
17462             template <typename T> struct S<T>::T { };
17463
17464          we will get a TYPENAME_TYPE when processing the definition of
17465          `S::T'.  We need to resolve it to the actual type before we
17466          try to define it.  */
17467       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17468         {
17469           class_type = resolve_typename_type (TREE_TYPE (type),
17470                                               /*only_current_p=*/false);
17471           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17472             type = TYPE_NAME (class_type);
17473           else
17474             {
17475               cp_parser_error (parser, "could not resolve typename type");
17476               type = error_mark_node;
17477             }
17478         }
17479
17480       if (maybe_process_partial_specialization (TREE_TYPE (type))
17481           == error_mark_node)
17482         {
17483           type = NULL_TREE;
17484           goto done;
17485         }
17486
17487       class_type = current_class_type;
17488       /* Enter the scope indicated by the nested-name-specifier.  */
17489       pushed_scope = push_scope (nested_name_specifier);
17490       /* Get the canonical version of this type.  */
17491       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17492       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17493           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17494         {
17495           type = push_template_decl (type);
17496           if (type == error_mark_node)
17497             {
17498               type = NULL_TREE;
17499               goto done;
17500             }
17501         }
17502
17503       type = TREE_TYPE (type);
17504       *nested_name_specifier_p = true;
17505     }
17506   else      /* The name is not a nested name.  */
17507     {
17508       /* If the class was unnamed, create a dummy name.  */
17509       if (!id)
17510         id = make_anon_name ();
17511       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17512                        parser->num_template_parameter_lists);
17513     }
17514
17515   /* Indicate whether this class was declared as a `class' or as a
17516      `struct'.  */
17517   if (TREE_CODE (type) == RECORD_TYPE)
17518     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17519   cp_parser_check_class_key (class_key, type);
17520
17521   /* If this type was already complete, and we see another definition,
17522      that's an error.  */
17523   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17524     {
17525       error_at (type_start_token->location, "redefinition of %q#T",
17526                 type);
17527       error_at (type_start_token->location, "previous definition of %q+#T",
17528                 type);
17529       type = NULL_TREE;
17530       goto done;
17531     }
17532   else if (type == error_mark_node)
17533     type = NULL_TREE;
17534
17535   /* We will have entered the scope containing the class; the names of
17536      base classes should be looked up in that context.  For example:
17537
17538        struct A { struct B {}; struct C; };
17539        struct A::C : B {};
17540
17541      is valid.  */
17542
17543   /* Get the list of base-classes, if there is one.  */
17544   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17545     *bases = cp_parser_base_clause (parser);
17546
17547  done:
17548   /* Leave the scope given by the nested-name-specifier.  We will
17549      enter the class scope itself while processing the members.  */
17550   if (pushed_scope)
17551     pop_scope (pushed_scope);
17552
17553   if (invalid_explicit_specialization_p)
17554     {
17555       end_specialization ();
17556       --parser->num_template_parameter_lists;
17557     }
17558
17559   if (type)
17560     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17561   *attributes_p = attributes;
17562  out:
17563   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17564   return type;
17565 }
17566
17567 /* Parse a class-key.
17568
17569    class-key:
17570      class
17571      struct
17572      union
17573
17574    Returns the kind of class-key specified, or none_type to indicate
17575    error.  */
17576
17577 static enum tag_types
17578 cp_parser_class_key (cp_parser* parser)
17579 {
17580   cp_token *token;
17581   enum tag_types tag_type;
17582
17583   /* Look for the class-key.  */
17584   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17585   if (!token)
17586     return none_type;
17587
17588   /* Check to see if the TOKEN is a class-key.  */
17589   tag_type = cp_parser_token_is_class_key (token);
17590   if (!tag_type)
17591     cp_parser_error (parser, "expected class-key");
17592   return tag_type;
17593 }
17594
17595 /* Parse an (optional) member-specification.
17596
17597    member-specification:
17598      member-declaration member-specification [opt]
17599      access-specifier : member-specification [opt]  */
17600
17601 static void
17602 cp_parser_member_specification_opt (cp_parser* parser)
17603 {
17604   while (true)
17605     {
17606       cp_token *token;
17607       enum rid keyword;
17608
17609       /* Peek at the next token.  */
17610       token = cp_lexer_peek_token (parser->lexer);
17611       /* If it's a `}', or EOF then we've seen all the members.  */
17612       if (token->type == CPP_CLOSE_BRACE
17613           || token->type == CPP_EOF
17614           || token->type == CPP_PRAGMA_EOL)
17615         break;
17616
17617       /* See if this token is a keyword.  */
17618       keyword = token->keyword;
17619       switch (keyword)
17620         {
17621         case RID_PUBLIC:
17622         case RID_PROTECTED:
17623         case RID_PRIVATE:
17624           /* Consume the access-specifier.  */
17625           cp_lexer_consume_token (parser->lexer);
17626           /* Remember which access-specifier is active.  */
17627           current_access_specifier = token->u.value;
17628           /* Look for the `:'.  */
17629           cp_parser_require (parser, CPP_COLON, RT_COLON);
17630           break;
17631
17632         default:
17633           /* Accept #pragmas at class scope.  */
17634           if (token->type == CPP_PRAGMA)
17635             {
17636               cp_parser_pragma (parser, pragma_external);
17637               break;
17638             }
17639
17640           /* Otherwise, the next construction must be a
17641              member-declaration.  */
17642           cp_parser_member_declaration (parser);
17643         }
17644     }
17645 }
17646
17647 /* Parse a member-declaration.
17648
17649    member-declaration:
17650      decl-specifier-seq [opt] member-declarator-list [opt] ;
17651      function-definition ; [opt]
17652      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17653      using-declaration
17654      template-declaration
17655
17656    member-declarator-list:
17657      member-declarator
17658      member-declarator-list , member-declarator
17659
17660    member-declarator:
17661      declarator pure-specifier [opt]
17662      declarator constant-initializer [opt]
17663      identifier [opt] : constant-expression
17664
17665    GNU Extensions:
17666
17667    member-declaration:
17668      __extension__ member-declaration
17669
17670    member-declarator:
17671      declarator attributes [opt] pure-specifier [opt]
17672      declarator attributes [opt] constant-initializer [opt]
17673      identifier [opt] attributes [opt] : constant-expression  
17674
17675    C++0x Extensions:
17676
17677    member-declaration:
17678      static_assert-declaration  */
17679
17680 static void
17681 cp_parser_member_declaration (cp_parser* parser)
17682 {
17683   cp_decl_specifier_seq decl_specifiers;
17684   tree prefix_attributes;
17685   tree decl;
17686   int declares_class_or_enum;
17687   bool friend_p;
17688   cp_token *token = NULL;
17689   cp_token *decl_spec_token_start = NULL;
17690   cp_token *initializer_token_start = NULL;
17691   int saved_pedantic;
17692   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17693
17694   /* Check for the `__extension__' keyword.  */
17695   if (cp_parser_extension_opt (parser, &saved_pedantic))
17696     {
17697       /* Recurse.  */
17698       cp_parser_member_declaration (parser);
17699       /* Restore the old value of the PEDANTIC flag.  */
17700       pedantic = saved_pedantic;
17701
17702       return;
17703     }
17704
17705   /* Check for a template-declaration.  */
17706   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17707     {
17708       /* An explicit specialization here is an error condition, and we
17709          expect the specialization handler to detect and report this.  */
17710       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17711           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17712         cp_parser_explicit_specialization (parser);
17713       else
17714         cp_parser_template_declaration (parser, /*member_p=*/true);
17715
17716       return;
17717     }
17718
17719   /* Check for a using-declaration.  */
17720   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17721     {
17722       /* Parse the using-declaration.  */
17723       cp_parser_using_declaration (parser,
17724                                    /*access_declaration_p=*/false);
17725       return;
17726     }
17727
17728   /* Check for @defs.  */
17729   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17730     {
17731       tree ivar, member;
17732       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17733       ivar = ivar_chains;
17734       while (ivar)
17735         {
17736           member = ivar;
17737           ivar = TREE_CHAIN (member);
17738           TREE_CHAIN (member) = NULL_TREE;
17739           finish_member_declaration (member);
17740         }
17741       return;
17742     }
17743
17744   /* If the next token is `static_assert' we have a static assertion.  */
17745   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17746     {
17747       cp_parser_static_assert (parser, /*member_p=*/true);
17748       return;
17749     }
17750
17751   parser->colon_corrects_to_scope_p = false;
17752
17753   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17754     goto out;
17755
17756   /* Parse the decl-specifier-seq.  */
17757   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17758   cp_parser_decl_specifier_seq (parser,
17759                                 CP_PARSER_FLAGS_OPTIONAL,
17760                                 &decl_specifiers,
17761                                 &declares_class_or_enum);
17762   prefix_attributes = decl_specifiers.attributes;
17763   decl_specifiers.attributes = NULL_TREE;
17764   /* Check for an invalid type-name.  */
17765   if (!decl_specifiers.any_type_specifiers_p
17766       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17767     goto out;
17768   /* If there is no declarator, then the decl-specifier-seq should
17769      specify a type.  */
17770   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17771     {
17772       /* If there was no decl-specifier-seq, and the next token is a
17773          `;', then we have something like:
17774
17775            struct S { ; };
17776
17777          [class.mem]
17778
17779          Each member-declaration shall declare at least one member
17780          name of the class.  */
17781       if (!decl_specifiers.any_specifiers_p)
17782         {
17783           cp_token *token = cp_lexer_peek_token (parser->lexer);
17784           if (!in_system_header_at (token->location))
17785             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17786         }
17787       else
17788         {
17789           tree type;
17790
17791           /* See if this declaration is a friend.  */
17792           friend_p = cp_parser_friend_p (&decl_specifiers);
17793           /* If there were decl-specifiers, check to see if there was
17794              a class-declaration.  */
17795           type = check_tag_decl (&decl_specifiers);
17796           /* Nested classes have already been added to the class, but
17797              a `friend' needs to be explicitly registered.  */
17798           if (friend_p)
17799             {
17800               /* If the `friend' keyword was present, the friend must
17801                  be introduced with a class-key.  */
17802                if (!declares_class_or_enum)
17803                  error_at (decl_spec_token_start->location,
17804                            "a class-key must be used when declaring a friend");
17805                /* In this case:
17806
17807                     template <typename T> struct A {
17808                       friend struct A<T>::B;
17809                     };
17810
17811                   A<T>::B will be represented by a TYPENAME_TYPE, and
17812                   therefore not recognized by check_tag_decl.  */
17813                if (!type
17814                    && decl_specifiers.type
17815                    && TYPE_P (decl_specifiers.type))
17816                  type = decl_specifiers.type;
17817                if (!type || !TYPE_P (type))
17818                  error_at (decl_spec_token_start->location,
17819                            "friend declaration does not name a class or "
17820                            "function");
17821                else
17822                  make_friend_class (current_class_type, type,
17823                                     /*complain=*/true);
17824             }
17825           /* If there is no TYPE, an error message will already have
17826              been issued.  */
17827           else if (!type || type == error_mark_node)
17828             ;
17829           /* An anonymous aggregate has to be handled specially; such
17830              a declaration really declares a data member (with a
17831              particular type), as opposed to a nested class.  */
17832           else if (ANON_AGGR_TYPE_P (type))
17833             {
17834               /* Remove constructors and such from TYPE, now that we
17835                  know it is an anonymous aggregate.  */
17836               fixup_anonymous_aggr (type);
17837               /* And make the corresponding data member.  */
17838               decl = build_decl (decl_spec_token_start->location,
17839                                  FIELD_DECL, NULL_TREE, type);
17840               /* Add it to the class.  */
17841               finish_member_declaration (decl);
17842             }
17843           else
17844             cp_parser_check_access_in_redeclaration
17845                                               (TYPE_NAME (type),
17846                                                decl_spec_token_start->location);
17847         }
17848     }
17849   else
17850     {
17851       bool assume_semicolon = false;
17852
17853       /* See if these declarations will be friends.  */
17854       friend_p = cp_parser_friend_p (&decl_specifiers);
17855
17856       /* Keep going until we hit the `;' at the end of the
17857          declaration.  */
17858       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17859         {
17860           tree attributes = NULL_TREE;
17861           tree first_attribute;
17862
17863           /* Peek at the next token.  */
17864           token = cp_lexer_peek_token (parser->lexer);
17865
17866           /* Check for a bitfield declaration.  */
17867           if (token->type == CPP_COLON
17868               || (token->type == CPP_NAME
17869                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17870                   == CPP_COLON))
17871             {
17872               tree identifier;
17873               tree width;
17874
17875               /* Get the name of the bitfield.  Note that we cannot just
17876                  check TOKEN here because it may have been invalidated by
17877                  the call to cp_lexer_peek_nth_token above.  */
17878               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17879                 identifier = cp_parser_identifier (parser);
17880               else
17881                 identifier = NULL_TREE;
17882
17883               /* Consume the `:' token.  */
17884               cp_lexer_consume_token (parser->lexer);
17885               /* Get the width of the bitfield.  */
17886               width
17887                 = cp_parser_constant_expression (parser,
17888                                                  /*allow_non_constant=*/false,
17889                                                  NULL);
17890
17891               /* Look for attributes that apply to the bitfield.  */
17892               attributes = cp_parser_attributes_opt (parser);
17893               /* Remember which attributes are prefix attributes and
17894                  which are not.  */
17895               first_attribute = attributes;
17896               /* Combine the attributes.  */
17897               attributes = chainon (prefix_attributes, attributes);
17898
17899               /* Create the bitfield declaration.  */
17900               decl = grokbitfield (identifier
17901                                    ? make_id_declarator (NULL_TREE,
17902                                                          identifier,
17903                                                          sfk_none)
17904                                    : NULL,
17905                                    &decl_specifiers,
17906                                    width,
17907                                    attributes);
17908             }
17909           else
17910             {
17911               cp_declarator *declarator;
17912               tree initializer;
17913               tree asm_specification;
17914               int ctor_dtor_or_conv_p;
17915
17916               /* Parse the declarator.  */
17917               declarator
17918                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17919                                         &ctor_dtor_or_conv_p,
17920                                         /*parenthesized_p=*/NULL,
17921                                         /*member_p=*/true);
17922
17923               /* If something went wrong parsing the declarator, make sure
17924                  that we at least consume some tokens.  */
17925               if (declarator == cp_error_declarator)
17926                 {
17927                   /* Skip to the end of the statement.  */
17928                   cp_parser_skip_to_end_of_statement (parser);
17929                   /* If the next token is not a semicolon, that is
17930                      probably because we just skipped over the body of
17931                      a function.  So, we consume a semicolon if
17932                      present, but do not issue an error message if it
17933                      is not present.  */
17934                   if (cp_lexer_next_token_is (parser->lexer,
17935                                               CPP_SEMICOLON))
17936                     cp_lexer_consume_token (parser->lexer);
17937                   goto out;
17938                 }
17939
17940               if (declares_class_or_enum & 2)
17941                 cp_parser_check_for_definition_in_return_type
17942                                             (declarator, decl_specifiers.type,
17943                                              decl_specifiers.type_location);
17944
17945               /* Look for an asm-specification.  */
17946               asm_specification = cp_parser_asm_specification_opt (parser);
17947               /* Look for attributes that apply to the declaration.  */
17948               attributes = cp_parser_attributes_opt (parser);
17949               /* Remember which attributes are prefix attributes and
17950                  which are not.  */
17951               first_attribute = attributes;
17952               /* Combine the attributes.  */
17953               attributes = chainon (prefix_attributes, attributes);
17954
17955               /* If it's an `=', then we have a constant-initializer or a
17956                  pure-specifier.  It is not correct to parse the
17957                  initializer before registering the member declaration
17958                  since the member declaration should be in scope while
17959                  its initializer is processed.  However, the rest of the
17960                  front end does not yet provide an interface that allows
17961                  us to handle this correctly.  */
17962               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17963                 {
17964                   /* In [class.mem]:
17965
17966                      A pure-specifier shall be used only in the declaration of
17967                      a virtual function.
17968
17969                      A member-declarator can contain a constant-initializer
17970                      only if it declares a static member of integral or
17971                      enumeration type.
17972
17973                      Therefore, if the DECLARATOR is for a function, we look
17974                      for a pure-specifier; otherwise, we look for a
17975                      constant-initializer.  When we call `grokfield', it will
17976                      perform more stringent semantics checks.  */
17977                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17978                   if (function_declarator_p (declarator))
17979                     initializer = cp_parser_pure_specifier (parser);
17980                   else
17981                     /* Parse the initializer.  */
17982                     initializer = cp_parser_constant_initializer (parser);
17983                 }
17984               /* Otherwise, there is no initializer.  */
17985               else
17986                 initializer = NULL_TREE;
17987
17988               /* See if we are probably looking at a function
17989                  definition.  We are certainly not looking at a
17990                  member-declarator.  Calling `grokfield' has
17991                  side-effects, so we must not do it unless we are sure
17992                  that we are looking at a member-declarator.  */
17993               if (cp_parser_token_starts_function_definition_p
17994                   (cp_lexer_peek_token (parser->lexer)))
17995                 {
17996                   /* The grammar does not allow a pure-specifier to be
17997                      used when a member function is defined.  (It is
17998                      possible that this fact is an oversight in the
17999                      standard, since a pure function may be defined
18000                      outside of the class-specifier.  */
18001                   if (initializer)
18002                     error_at (initializer_token_start->location,
18003                               "pure-specifier on function-definition");
18004                   decl = cp_parser_save_member_function_body (parser,
18005                                                               &decl_specifiers,
18006                                                               declarator,
18007                                                               attributes);
18008                   /* If the member was not a friend, declare it here.  */
18009                   if (!friend_p)
18010                     finish_member_declaration (decl);
18011                   /* Peek at the next token.  */
18012                   token = cp_lexer_peek_token (parser->lexer);
18013                   /* If the next token is a semicolon, consume it.  */
18014                   if (token->type == CPP_SEMICOLON)
18015                     cp_lexer_consume_token (parser->lexer);
18016                   goto out;
18017                 }
18018               else
18019                 if (declarator->kind == cdk_function)
18020                   declarator->id_loc = token->location;
18021                 /* Create the declaration.  */
18022                 decl = grokfield (declarator, &decl_specifiers,
18023                                   initializer, /*init_const_expr_p=*/true,
18024                                   asm_specification,
18025                                   attributes);
18026             }
18027
18028           /* Reset PREFIX_ATTRIBUTES.  */
18029           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18030             attributes = TREE_CHAIN (attributes);
18031           if (attributes)
18032             TREE_CHAIN (attributes) = NULL_TREE;
18033
18034           /* If there is any qualification still in effect, clear it
18035              now; we will be starting fresh with the next declarator.  */
18036           parser->scope = NULL_TREE;
18037           parser->qualifying_scope = NULL_TREE;
18038           parser->object_scope = NULL_TREE;
18039           /* If it's a `,', then there are more declarators.  */
18040           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18041             cp_lexer_consume_token (parser->lexer);
18042           /* If the next token isn't a `;', then we have a parse error.  */
18043           else if (cp_lexer_next_token_is_not (parser->lexer,
18044                                                CPP_SEMICOLON))
18045             {
18046               /* The next token might be a ways away from where the
18047                  actual semicolon is missing.  Find the previous token
18048                  and use that for our error position.  */
18049               cp_token *token = cp_lexer_previous_token (parser->lexer);
18050               error_at (token->location,
18051                         "expected %<;%> at end of member declaration");
18052
18053               /* Assume that the user meant to provide a semicolon.  If
18054                  we were to cp_parser_skip_to_end_of_statement, we might
18055                  skip to a semicolon inside a member function definition
18056                  and issue nonsensical error messages.  */
18057               assume_semicolon = true;
18058             }
18059
18060           if (decl)
18061             {
18062               /* Add DECL to the list of members.  */
18063               if (!friend_p)
18064                 finish_member_declaration (decl);
18065
18066               if (TREE_CODE (decl) == FUNCTION_DECL)
18067                 cp_parser_save_default_args (parser, decl);
18068             }
18069
18070           if (assume_semicolon)
18071             goto out;
18072         }
18073     }
18074
18075   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18076  out:
18077   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18078 }
18079
18080 /* Parse a pure-specifier.
18081
18082    pure-specifier:
18083      = 0
18084
18085    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18086    Otherwise, ERROR_MARK_NODE is returned.  */
18087
18088 static tree
18089 cp_parser_pure_specifier (cp_parser* parser)
18090 {
18091   cp_token *token;
18092
18093   /* Look for the `=' token.  */
18094   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18095     return error_mark_node;
18096   /* Look for the `0' token.  */
18097   token = cp_lexer_peek_token (parser->lexer);
18098
18099   if (token->type == CPP_EOF
18100       || token->type == CPP_PRAGMA_EOL)
18101     return error_mark_node;
18102
18103   cp_lexer_consume_token (parser->lexer);
18104
18105   /* Accept = default or = delete in c++0x mode.  */
18106   if (token->keyword == RID_DEFAULT
18107       || token->keyword == RID_DELETE)
18108     {
18109       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18110       return token->u.value;
18111     }
18112
18113   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18114   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18115     {
18116       cp_parser_error (parser,
18117                        "invalid pure specifier (only %<= 0%> is allowed)");
18118       cp_parser_skip_to_end_of_statement (parser);
18119       return error_mark_node;
18120     }
18121   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18122     {
18123       error_at (token->location, "templates may not be %<virtual%>");
18124       return error_mark_node;
18125     }
18126
18127   return integer_zero_node;
18128 }
18129
18130 /* Parse a constant-initializer.
18131
18132    constant-initializer:
18133      = constant-expression
18134
18135    Returns a representation of the constant-expression.  */
18136
18137 static tree
18138 cp_parser_constant_initializer (cp_parser* parser)
18139 {
18140   /* Look for the `=' token.  */
18141   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18142     return error_mark_node;
18143
18144   /* It is invalid to write:
18145
18146        struct S { static const int i = { 7 }; };
18147
18148      */
18149   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18150     {
18151       cp_parser_error (parser,
18152                        "a brace-enclosed initializer is not allowed here");
18153       /* Consume the opening brace.  */
18154       cp_lexer_consume_token (parser->lexer);
18155       /* Skip the initializer.  */
18156       cp_parser_skip_to_closing_brace (parser);
18157       /* Look for the trailing `}'.  */
18158       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18159
18160       return error_mark_node;
18161     }
18162
18163   return cp_parser_constant_expression (parser,
18164                                         /*allow_non_constant=*/false,
18165                                         NULL);
18166 }
18167
18168 /* Derived classes [gram.class.derived] */
18169
18170 /* Parse a base-clause.
18171
18172    base-clause:
18173      : base-specifier-list
18174
18175    base-specifier-list:
18176      base-specifier ... [opt]
18177      base-specifier-list , base-specifier ... [opt]
18178
18179    Returns a TREE_LIST representing the base-classes, in the order in
18180    which they were declared.  The representation of each node is as
18181    described by cp_parser_base_specifier.
18182
18183    In the case that no bases are specified, this function will return
18184    NULL_TREE, not ERROR_MARK_NODE.  */
18185
18186 static tree
18187 cp_parser_base_clause (cp_parser* parser)
18188 {
18189   tree bases = NULL_TREE;
18190
18191   /* Look for the `:' that begins the list.  */
18192   cp_parser_require (parser, CPP_COLON, RT_COLON);
18193
18194   /* Scan the base-specifier-list.  */
18195   while (true)
18196     {
18197       cp_token *token;
18198       tree base;
18199       bool pack_expansion_p = false;
18200
18201       /* Look for the base-specifier.  */
18202       base = cp_parser_base_specifier (parser);
18203       /* Look for the (optional) ellipsis. */
18204       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18205         {
18206           /* Consume the `...'. */
18207           cp_lexer_consume_token (parser->lexer);
18208
18209           pack_expansion_p = true;
18210         }
18211
18212       /* Add BASE to the front of the list.  */
18213       if (base != error_mark_node)
18214         {
18215           if (pack_expansion_p)
18216             /* Make this a pack expansion type. */
18217             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18218           
18219
18220           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18221             {
18222               TREE_CHAIN (base) = bases;
18223               bases = base;
18224             }
18225         }
18226       /* Peek at the next token.  */
18227       token = cp_lexer_peek_token (parser->lexer);
18228       /* If it's not a comma, then the list is complete.  */
18229       if (token->type != CPP_COMMA)
18230         break;
18231       /* Consume the `,'.  */
18232       cp_lexer_consume_token (parser->lexer);
18233     }
18234
18235   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18236      base class had a qualified name.  However, the next name that
18237      appears is certainly not qualified.  */
18238   parser->scope = NULL_TREE;
18239   parser->qualifying_scope = NULL_TREE;
18240   parser->object_scope = NULL_TREE;
18241
18242   return nreverse (bases);
18243 }
18244
18245 /* Parse a base-specifier.
18246
18247    base-specifier:
18248      :: [opt] nested-name-specifier [opt] class-name
18249      virtual access-specifier [opt] :: [opt] nested-name-specifier
18250        [opt] class-name
18251      access-specifier virtual [opt] :: [opt] nested-name-specifier
18252        [opt] class-name
18253
18254    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18255    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18256    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18257    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18258
18259 static tree
18260 cp_parser_base_specifier (cp_parser* parser)
18261 {
18262   cp_token *token;
18263   bool done = false;
18264   bool virtual_p = false;
18265   bool duplicate_virtual_error_issued_p = false;
18266   bool duplicate_access_error_issued_p = false;
18267   bool class_scope_p, template_p;
18268   tree access = access_default_node;
18269   tree type;
18270
18271   /* Process the optional `virtual' and `access-specifier'.  */
18272   while (!done)
18273     {
18274       /* Peek at the next token.  */
18275       token = cp_lexer_peek_token (parser->lexer);
18276       /* Process `virtual'.  */
18277       switch (token->keyword)
18278         {
18279         case RID_VIRTUAL:
18280           /* If `virtual' appears more than once, issue an error.  */
18281           if (virtual_p && !duplicate_virtual_error_issued_p)
18282             {
18283               cp_parser_error (parser,
18284                                "%<virtual%> specified more than once in base-specified");
18285               duplicate_virtual_error_issued_p = true;
18286             }
18287
18288           virtual_p = true;
18289
18290           /* Consume the `virtual' token.  */
18291           cp_lexer_consume_token (parser->lexer);
18292
18293           break;
18294
18295         case RID_PUBLIC:
18296         case RID_PROTECTED:
18297         case RID_PRIVATE:
18298           /* If more than one access specifier appears, issue an
18299              error.  */
18300           if (access != access_default_node
18301               && !duplicate_access_error_issued_p)
18302             {
18303               cp_parser_error (parser,
18304                                "more than one access specifier in base-specified");
18305               duplicate_access_error_issued_p = true;
18306             }
18307
18308           access = ridpointers[(int) token->keyword];
18309
18310           /* Consume the access-specifier.  */
18311           cp_lexer_consume_token (parser->lexer);
18312
18313           break;
18314
18315         default:
18316           done = true;
18317           break;
18318         }
18319     }
18320   /* It is not uncommon to see programs mechanically, erroneously, use
18321      the 'typename' keyword to denote (dependent) qualified types
18322      as base classes.  */
18323   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18324     {
18325       token = cp_lexer_peek_token (parser->lexer);
18326       if (!processing_template_decl)
18327         error_at (token->location,
18328                   "keyword %<typename%> not allowed outside of templates");
18329       else
18330         error_at (token->location,
18331                   "keyword %<typename%> not allowed in this context "
18332                   "(the base class is implicitly a type)");
18333       cp_lexer_consume_token (parser->lexer);
18334     }
18335
18336   /* Look for the optional `::' operator.  */
18337   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18338   /* Look for the nested-name-specifier.  The simplest way to
18339      implement:
18340
18341        [temp.res]
18342
18343        The keyword `typename' is not permitted in a base-specifier or
18344        mem-initializer; in these contexts a qualified name that
18345        depends on a template-parameter is implicitly assumed to be a
18346        type name.
18347
18348      is to pretend that we have seen the `typename' keyword at this
18349      point.  */
18350   cp_parser_nested_name_specifier_opt (parser,
18351                                        /*typename_keyword_p=*/true,
18352                                        /*check_dependency_p=*/true,
18353                                        typename_type,
18354                                        /*is_declaration=*/true);
18355   /* If the base class is given by a qualified name, assume that names
18356      we see are type names or templates, as appropriate.  */
18357   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18358   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18359
18360   /* Finally, look for the class-name.  */
18361   type = cp_parser_class_name (parser,
18362                                class_scope_p,
18363                                template_p,
18364                                typename_type,
18365                                /*check_dependency_p=*/true,
18366                                /*class_head_p=*/false,
18367                                /*is_declaration=*/true);
18368
18369   if (type == error_mark_node)
18370     return error_mark_node;
18371
18372   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18373 }
18374
18375 /* Exception handling [gram.exception] */
18376
18377 /* Parse an (optional) exception-specification.
18378
18379    exception-specification:
18380      throw ( type-id-list [opt] )
18381
18382    Returns a TREE_LIST representing the exception-specification.  The
18383    TREE_VALUE of each node is a type.  */
18384
18385 static tree
18386 cp_parser_exception_specification_opt (cp_parser* parser)
18387 {
18388   cp_token *token;
18389   tree type_id_list;
18390   const char *saved_message;
18391
18392   /* Peek at the next token.  */
18393   token = cp_lexer_peek_token (parser->lexer);
18394
18395   /* Is it a noexcept-specification?  */
18396   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18397     {
18398       tree expr;
18399       cp_lexer_consume_token (parser->lexer);
18400
18401       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18402         {
18403           cp_lexer_consume_token (parser->lexer);
18404
18405           /* Types may not be defined in an exception-specification.  */
18406           saved_message = parser->type_definition_forbidden_message;
18407           parser->type_definition_forbidden_message
18408             = G_("types may not be defined in an exception-specification");
18409
18410           expr = cp_parser_constant_expression (parser, false, NULL);
18411
18412           /* Restore the saved message.  */
18413           parser->type_definition_forbidden_message = saved_message;
18414
18415           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18416         }
18417       else
18418         expr = boolean_true_node;
18419
18420       return build_noexcept_spec (expr, tf_warning_or_error);
18421     }
18422
18423   /* If it's not `throw', then there's no exception-specification.  */
18424   if (!cp_parser_is_keyword (token, RID_THROW))
18425     return NULL_TREE;
18426
18427 #if 0
18428   /* Enable this once a lot of code has transitioned to noexcept?  */
18429   if (cxx_dialect == cxx0x && !in_system_header)
18430     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18431              "deprecated in C++0x; use %<noexcept%> instead");
18432 #endif
18433
18434   /* Consume the `throw'.  */
18435   cp_lexer_consume_token (parser->lexer);
18436
18437   /* Look for the `('.  */
18438   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18439
18440   /* Peek at the next token.  */
18441   token = cp_lexer_peek_token (parser->lexer);
18442   /* If it's not a `)', then there is a type-id-list.  */
18443   if (token->type != CPP_CLOSE_PAREN)
18444     {
18445       /* Types may not be defined in an exception-specification.  */
18446       saved_message = parser->type_definition_forbidden_message;
18447       parser->type_definition_forbidden_message
18448         = G_("types may not be defined in an exception-specification");
18449       /* Parse the type-id-list.  */
18450       type_id_list = cp_parser_type_id_list (parser);
18451       /* Restore the saved message.  */
18452       parser->type_definition_forbidden_message = saved_message;
18453     }
18454   else
18455     type_id_list = empty_except_spec;
18456
18457   /* Look for the `)'.  */
18458   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18459
18460   return type_id_list;
18461 }
18462
18463 /* Parse an (optional) type-id-list.
18464
18465    type-id-list:
18466      type-id ... [opt]
18467      type-id-list , type-id ... [opt]
18468
18469    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18470    in the order that the types were presented.  */
18471
18472 static tree
18473 cp_parser_type_id_list (cp_parser* parser)
18474 {
18475   tree types = NULL_TREE;
18476
18477   while (true)
18478     {
18479       cp_token *token;
18480       tree type;
18481
18482       /* Get the next type-id.  */
18483       type = cp_parser_type_id (parser);
18484       /* Parse the optional ellipsis. */
18485       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18486         {
18487           /* Consume the `...'. */
18488           cp_lexer_consume_token (parser->lexer);
18489
18490           /* Turn the type into a pack expansion expression. */
18491           type = make_pack_expansion (type);
18492         }
18493       /* Add it to the list.  */
18494       types = add_exception_specifier (types, type, /*complain=*/1);
18495       /* Peek at the next token.  */
18496       token = cp_lexer_peek_token (parser->lexer);
18497       /* If it is not a `,', we are done.  */
18498       if (token->type != CPP_COMMA)
18499         break;
18500       /* Consume the `,'.  */
18501       cp_lexer_consume_token (parser->lexer);
18502     }
18503
18504   return nreverse (types);
18505 }
18506
18507 /* Parse a try-block.
18508
18509    try-block:
18510      try compound-statement handler-seq  */
18511
18512 static tree
18513 cp_parser_try_block (cp_parser* parser)
18514 {
18515   tree try_block;
18516
18517   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18518   try_block = begin_try_block ();
18519   cp_parser_compound_statement (parser, NULL, true);
18520   finish_try_block (try_block);
18521   cp_parser_handler_seq (parser);
18522   finish_handler_sequence (try_block);
18523
18524   return try_block;
18525 }
18526
18527 /* Parse a function-try-block.
18528
18529    function-try-block:
18530      try ctor-initializer [opt] function-body handler-seq  */
18531
18532 static bool
18533 cp_parser_function_try_block (cp_parser* parser)
18534 {
18535   tree compound_stmt;
18536   tree try_block;
18537   bool ctor_initializer_p;
18538
18539   /* Look for the `try' keyword.  */
18540   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18541     return false;
18542   /* Let the rest of the front end know where we are.  */
18543   try_block = begin_function_try_block (&compound_stmt);
18544   /* Parse the function-body.  */
18545   ctor_initializer_p
18546     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18547   /* We're done with the `try' part.  */
18548   finish_function_try_block (try_block);
18549   /* Parse the handlers.  */
18550   cp_parser_handler_seq (parser);
18551   /* We're done with the handlers.  */
18552   finish_function_handler_sequence (try_block, compound_stmt);
18553
18554   return ctor_initializer_p;
18555 }
18556
18557 /* Parse a handler-seq.
18558
18559    handler-seq:
18560      handler handler-seq [opt]  */
18561
18562 static void
18563 cp_parser_handler_seq (cp_parser* parser)
18564 {
18565   while (true)
18566     {
18567       cp_token *token;
18568
18569       /* Parse the handler.  */
18570       cp_parser_handler (parser);
18571       /* Peek at the next token.  */
18572       token = cp_lexer_peek_token (parser->lexer);
18573       /* If it's not `catch' then there are no more handlers.  */
18574       if (!cp_parser_is_keyword (token, RID_CATCH))
18575         break;
18576     }
18577 }
18578
18579 /* Parse a handler.
18580
18581    handler:
18582      catch ( exception-declaration ) compound-statement  */
18583
18584 static void
18585 cp_parser_handler (cp_parser* parser)
18586 {
18587   tree handler;
18588   tree declaration;
18589
18590   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18591   handler = begin_handler ();
18592   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18593   declaration = cp_parser_exception_declaration (parser);
18594   finish_handler_parms (declaration, handler);
18595   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18596   cp_parser_compound_statement (parser, NULL, false);
18597   finish_handler (handler);
18598 }
18599
18600 /* Parse an exception-declaration.
18601
18602    exception-declaration:
18603      type-specifier-seq declarator
18604      type-specifier-seq abstract-declarator
18605      type-specifier-seq
18606      ...
18607
18608    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18609    ellipsis variant is used.  */
18610
18611 static tree
18612 cp_parser_exception_declaration (cp_parser* parser)
18613 {
18614   cp_decl_specifier_seq type_specifiers;
18615   cp_declarator *declarator;
18616   const char *saved_message;
18617
18618   /* If it's an ellipsis, it's easy to handle.  */
18619   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18620     {
18621       /* Consume the `...' token.  */
18622       cp_lexer_consume_token (parser->lexer);
18623       return NULL_TREE;
18624     }
18625
18626   /* Types may not be defined in exception-declarations.  */
18627   saved_message = parser->type_definition_forbidden_message;
18628   parser->type_definition_forbidden_message
18629     = G_("types may not be defined in exception-declarations");
18630
18631   /* Parse the type-specifier-seq.  */
18632   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18633                                 /*is_trailing_return=*/false,
18634                                 &type_specifiers);
18635   /* If it's a `)', then there is no declarator.  */
18636   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18637     declarator = NULL;
18638   else
18639     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18640                                        /*ctor_dtor_or_conv_p=*/NULL,
18641                                        /*parenthesized_p=*/NULL,
18642                                        /*member_p=*/false);
18643
18644   /* Restore the saved message.  */
18645   parser->type_definition_forbidden_message = saved_message;
18646
18647   if (!type_specifiers.any_specifiers_p)
18648     return error_mark_node;
18649
18650   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18651 }
18652
18653 /* Parse a throw-expression.
18654
18655    throw-expression:
18656      throw assignment-expression [opt]
18657
18658    Returns a THROW_EXPR representing the throw-expression.  */
18659
18660 static tree
18661 cp_parser_throw_expression (cp_parser* parser)
18662 {
18663   tree expression;
18664   cp_token* token;
18665
18666   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18667   token = cp_lexer_peek_token (parser->lexer);
18668   /* Figure out whether or not there is an assignment-expression
18669      following the "throw" keyword.  */
18670   if (token->type == CPP_COMMA
18671       || token->type == CPP_SEMICOLON
18672       || token->type == CPP_CLOSE_PAREN
18673       || token->type == CPP_CLOSE_SQUARE
18674       || token->type == CPP_CLOSE_BRACE
18675       || token->type == CPP_COLON)
18676     expression = NULL_TREE;
18677   else
18678     expression = cp_parser_assignment_expression (parser,
18679                                                   /*cast_p=*/false, NULL);
18680
18681   return build_throw (expression);
18682 }
18683
18684 /* GNU Extensions */
18685
18686 /* Parse an (optional) asm-specification.
18687
18688    asm-specification:
18689      asm ( string-literal )
18690
18691    If the asm-specification is present, returns a STRING_CST
18692    corresponding to the string-literal.  Otherwise, returns
18693    NULL_TREE.  */
18694
18695 static tree
18696 cp_parser_asm_specification_opt (cp_parser* parser)
18697 {
18698   cp_token *token;
18699   tree asm_specification;
18700
18701   /* Peek at the next token.  */
18702   token = cp_lexer_peek_token (parser->lexer);
18703   /* If the next token isn't the `asm' keyword, then there's no
18704      asm-specification.  */
18705   if (!cp_parser_is_keyword (token, RID_ASM))
18706     return NULL_TREE;
18707
18708   /* Consume the `asm' token.  */
18709   cp_lexer_consume_token (parser->lexer);
18710   /* Look for the `('.  */
18711   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18712
18713   /* Look for the string-literal.  */
18714   asm_specification = cp_parser_string_literal (parser, false, false);
18715
18716   /* Look for the `)'.  */
18717   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18718
18719   return asm_specification;
18720 }
18721
18722 /* Parse an asm-operand-list.
18723
18724    asm-operand-list:
18725      asm-operand
18726      asm-operand-list , asm-operand
18727
18728    asm-operand:
18729      string-literal ( expression )
18730      [ string-literal ] string-literal ( expression )
18731
18732    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18733    each node is the expression.  The TREE_PURPOSE is itself a
18734    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18735    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18736    is a STRING_CST for the string literal before the parenthesis. Returns
18737    ERROR_MARK_NODE if any of the operands are invalid.  */
18738
18739 static tree
18740 cp_parser_asm_operand_list (cp_parser* parser)
18741 {
18742   tree asm_operands = NULL_TREE;
18743   bool invalid_operands = false;
18744
18745   while (true)
18746     {
18747       tree string_literal;
18748       tree expression;
18749       tree name;
18750
18751       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18752         {
18753           /* Consume the `[' token.  */
18754           cp_lexer_consume_token (parser->lexer);
18755           /* Read the operand name.  */
18756           name = cp_parser_identifier (parser);
18757           if (name != error_mark_node)
18758             name = build_string (IDENTIFIER_LENGTH (name),
18759                                  IDENTIFIER_POINTER (name));
18760           /* Look for the closing `]'.  */
18761           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18762         }
18763       else
18764         name = NULL_TREE;
18765       /* Look for the string-literal.  */
18766       string_literal = cp_parser_string_literal (parser, false, false);
18767
18768       /* Look for the `('.  */
18769       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18770       /* Parse the expression.  */
18771       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18772       /* Look for the `)'.  */
18773       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18774
18775       if (name == error_mark_node 
18776           || string_literal == error_mark_node 
18777           || expression == error_mark_node)
18778         invalid_operands = true;
18779
18780       /* Add this operand to the list.  */
18781       asm_operands = tree_cons (build_tree_list (name, string_literal),
18782                                 expression,
18783                                 asm_operands);
18784       /* If the next token is not a `,', there are no more
18785          operands.  */
18786       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18787         break;
18788       /* Consume the `,'.  */
18789       cp_lexer_consume_token (parser->lexer);
18790     }
18791
18792   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18793 }
18794
18795 /* Parse an asm-clobber-list.
18796
18797    asm-clobber-list:
18798      string-literal
18799      asm-clobber-list , string-literal
18800
18801    Returns a TREE_LIST, indicating the clobbers in the order that they
18802    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18803
18804 static tree
18805 cp_parser_asm_clobber_list (cp_parser* parser)
18806 {
18807   tree clobbers = NULL_TREE;
18808
18809   while (true)
18810     {
18811       tree string_literal;
18812
18813       /* Look for the string literal.  */
18814       string_literal = cp_parser_string_literal (parser, false, false);
18815       /* Add it to the list.  */
18816       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18817       /* If the next token is not a `,', then the list is
18818          complete.  */
18819       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18820         break;
18821       /* Consume the `,' token.  */
18822       cp_lexer_consume_token (parser->lexer);
18823     }
18824
18825   return clobbers;
18826 }
18827
18828 /* Parse an asm-label-list.
18829
18830    asm-label-list:
18831      identifier
18832      asm-label-list , identifier
18833
18834    Returns a TREE_LIST, indicating the labels in the order that they
18835    appeared.  The TREE_VALUE of each node is a label.  */
18836
18837 static tree
18838 cp_parser_asm_label_list (cp_parser* parser)
18839 {
18840   tree labels = NULL_TREE;
18841
18842   while (true)
18843     {
18844       tree identifier, label, name;
18845
18846       /* Look for the identifier.  */
18847       identifier = cp_parser_identifier (parser);
18848       if (!error_operand_p (identifier))
18849         {
18850           label = lookup_label (identifier);
18851           if (TREE_CODE (label) == LABEL_DECL)
18852             {
18853               TREE_USED (label) = 1;
18854               check_goto (label);
18855               name = build_string (IDENTIFIER_LENGTH (identifier),
18856                                    IDENTIFIER_POINTER (identifier));
18857               labels = tree_cons (name, label, labels);
18858             }
18859         }
18860       /* If the next token is not a `,', then the list is
18861          complete.  */
18862       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18863         break;
18864       /* Consume the `,' token.  */
18865       cp_lexer_consume_token (parser->lexer);
18866     }
18867
18868   return nreverse (labels);
18869 }
18870
18871 /* Parse an (optional) series of attributes.
18872
18873    attributes:
18874      attributes attribute
18875
18876    attribute:
18877      __attribute__ (( attribute-list [opt] ))
18878
18879    The return value is as for cp_parser_attribute_list.  */
18880
18881 static tree
18882 cp_parser_attributes_opt (cp_parser* parser)
18883 {
18884   tree attributes = NULL_TREE;
18885
18886   while (true)
18887     {
18888       cp_token *token;
18889       tree attribute_list;
18890
18891       /* Peek at the next token.  */
18892       token = cp_lexer_peek_token (parser->lexer);
18893       /* If it's not `__attribute__', then we're done.  */
18894       if (token->keyword != RID_ATTRIBUTE)
18895         break;
18896
18897       /* Consume the `__attribute__' keyword.  */
18898       cp_lexer_consume_token (parser->lexer);
18899       /* Look for the two `(' tokens.  */
18900       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18901       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18902
18903       /* Peek at the next token.  */
18904       token = cp_lexer_peek_token (parser->lexer);
18905       if (token->type != CPP_CLOSE_PAREN)
18906         /* Parse the attribute-list.  */
18907         attribute_list = cp_parser_attribute_list (parser);
18908       else
18909         /* If the next token is a `)', then there is no attribute
18910            list.  */
18911         attribute_list = NULL;
18912
18913       /* Look for the two `)' tokens.  */
18914       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18915       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18916
18917       /* Add these new attributes to the list.  */
18918       attributes = chainon (attributes, attribute_list);
18919     }
18920
18921   return attributes;
18922 }
18923
18924 /* Parse an attribute-list.
18925
18926    attribute-list:
18927      attribute
18928      attribute-list , attribute
18929
18930    attribute:
18931      identifier
18932      identifier ( identifier )
18933      identifier ( identifier , expression-list )
18934      identifier ( expression-list )
18935
18936    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18937    to an attribute.  The TREE_PURPOSE of each node is the identifier
18938    indicating which attribute is in use.  The TREE_VALUE represents
18939    the arguments, if any.  */
18940
18941 static tree
18942 cp_parser_attribute_list (cp_parser* parser)
18943 {
18944   tree attribute_list = NULL_TREE;
18945   bool save_translate_strings_p = parser->translate_strings_p;
18946
18947   parser->translate_strings_p = false;
18948   while (true)
18949     {
18950       cp_token *token;
18951       tree identifier;
18952       tree attribute;
18953
18954       /* Look for the identifier.  We also allow keywords here; for
18955          example `__attribute__ ((const))' is legal.  */
18956       token = cp_lexer_peek_token (parser->lexer);
18957       if (token->type == CPP_NAME
18958           || token->type == CPP_KEYWORD)
18959         {
18960           tree arguments = NULL_TREE;
18961
18962           /* Consume the token.  */
18963           token = cp_lexer_consume_token (parser->lexer);
18964
18965           /* Save away the identifier that indicates which attribute
18966              this is.  */
18967           identifier = (token->type == CPP_KEYWORD) 
18968             /* For keywords, use the canonical spelling, not the
18969                parsed identifier.  */
18970             ? ridpointers[(int) token->keyword]
18971             : token->u.value;
18972           
18973           attribute = build_tree_list (identifier, NULL_TREE);
18974
18975           /* Peek at the next token.  */
18976           token = cp_lexer_peek_token (parser->lexer);
18977           /* If it's an `(', then parse the attribute arguments.  */
18978           if (token->type == CPP_OPEN_PAREN)
18979             {
18980               VEC(tree,gc) *vec;
18981               int attr_flag = (attribute_takes_identifier_p (identifier)
18982                                ? id_attr : normal_attr);
18983               vec = cp_parser_parenthesized_expression_list
18984                     (parser, attr_flag, /*cast_p=*/false,
18985                      /*allow_expansion_p=*/false,
18986                      /*non_constant_p=*/NULL);
18987               if (vec == NULL)
18988                 arguments = error_mark_node;
18989               else
18990                 {
18991                   arguments = build_tree_list_vec (vec);
18992                   release_tree_vector (vec);
18993                 }
18994               /* Save the arguments away.  */
18995               TREE_VALUE (attribute) = arguments;
18996             }
18997
18998           if (arguments != error_mark_node)
18999             {
19000               /* Add this attribute to the list.  */
19001               TREE_CHAIN (attribute) = attribute_list;
19002               attribute_list = attribute;
19003             }
19004
19005           token = cp_lexer_peek_token (parser->lexer);
19006         }
19007       /* Now, look for more attributes.  If the next token isn't a
19008          `,', we're done.  */
19009       if (token->type != CPP_COMMA)
19010         break;
19011
19012       /* Consume the comma and keep going.  */
19013       cp_lexer_consume_token (parser->lexer);
19014     }
19015   parser->translate_strings_p = save_translate_strings_p;
19016
19017   /* We built up the list in reverse order.  */
19018   return nreverse (attribute_list);
19019 }
19020
19021 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19022    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19023    current value of the PEDANTIC flag, regardless of whether or not
19024    the `__extension__' keyword is present.  The caller is responsible
19025    for restoring the value of the PEDANTIC flag.  */
19026
19027 static bool
19028 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19029 {
19030   /* Save the old value of the PEDANTIC flag.  */
19031   *saved_pedantic = pedantic;
19032
19033   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19034     {
19035       /* Consume the `__extension__' token.  */
19036       cp_lexer_consume_token (parser->lexer);
19037       /* We're not being pedantic while the `__extension__' keyword is
19038          in effect.  */
19039       pedantic = 0;
19040
19041       return true;
19042     }
19043
19044   return false;
19045 }
19046
19047 /* Parse a label declaration.
19048
19049    label-declaration:
19050      __label__ label-declarator-seq ;
19051
19052    label-declarator-seq:
19053      identifier , label-declarator-seq
19054      identifier  */
19055
19056 static void
19057 cp_parser_label_declaration (cp_parser* parser)
19058 {
19059   /* Look for the `__label__' keyword.  */
19060   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19061
19062   while (true)
19063     {
19064       tree identifier;
19065
19066       /* Look for an identifier.  */
19067       identifier = cp_parser_identifier (parser);
19068       /* If we failed, stop.  */
19069       if (identifier == error_mark_node)
19070         break;
19071       /* Declare it as a label.  */
19072       finish_label_decl (identifier);
19073       /* If the next token is a `;', stop.  */
19074       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19075         break;
19076       /* Look for the `,' separating the label declarations.  */
19077       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19078     }
19079
19080   /* Look for the final `;'.  */
19081   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19082 }
19083
19084 /* Support Functions */
19085
19086 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19087    NAME should have one of the representations used for an
19088    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19089    is returned.  If PARSER->SCOPE is a dependent type, then a
19090    SCOPE_REF is returned.
19091
19092    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19093    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19094    was formed.  Abstractly, such entities should not be passed to this
19095    function, because they do not need to be looked up, but it is
19096    simpler to check for this special case here, rather than at the
19097    call-sites.
19098
19099    In cases not explicitly covered above, this function returns a
19100    DECL, OVERLOAD, or baselink representing the result of the lookup.
19101    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19102    is returned.
19103
19104    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19105    (e.g., "struct") that was used.  In that case bindings that do not
19106    refer to types are ignored.
19107
19108    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19109    ignored.
19110
19111    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19112    are ignored.
19113
19114    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19115    types.
19116
19117    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19118    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19119    NULL_TREE otherwise.  */
19120
19121 static tree
19122 cp_parser_lookup_name (cp_parser *parser, tree name,
19123                        enum tag_types tag_type,
19124                        bool is_template,
19125                        bool is_namespace,
19126                        bool check_dependency,
19127                        tree *ambiguous_decls,
19128                        location_t name_location)
19129 {
19130   int flags = 0;
19131   tree decl;
19132   tree object_type = parser->context->object_type;
19133
19134   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19135     flags |= LOOKUP_COMPLAIN;
19136
19137   /* Assume that the lookup will be unambiguous.  */
19138   if (ambiguous_decls)
19139     *ambiguous_decls = NULL_TREE;
19140
19141   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19142      no longer valid.  Note that if we are parsing tentatively, and
19143      the parse fails, OBJECT_TYPE will be automatically restored.  */
19144   parser->context->object_type = NULL_TREE;
19145
19146   if (name == error_mark_node)
19147     return error_mark_node;
19148
19149   /* A template-id has already been resolved; there is no lookup to
19150      do.  */
19151   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19152     return name;
19153   if (BASELINK_P (name))
19154     {
19155       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19156                   == TEMPLATE_ID_EXPR);
19157       return name;
19158     }
19159
19160   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19161      it should already have been checked to make sure that the name
19162      used matches the type being destroyed.  */
19163   if (TREE_CODE (name) == BIT_NOT_EXPR)
19164     {
19165       tree type;
19166
19167       /* Figure out to which type this destructor applies.  */
19168       if (parser->scope)
19169         type = parser->scope;
19170       else if (object_type)
19171         type = object_type;
19172       else
19173         type = current_class_type;
19174       /* If that's not a class type, there is no destructor.  */
19175       if (!type || !CLASS_TYPE_P (type))
19176         return error_mark_node;
19177       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19178         lazily_declare_fn (sfk_destructor, type);
19179       if (!CLASSTYPE_DESTRUCTORS (type))
19180           return error_mark_node;
19181       /* If it was a class type, return the destructor.  */
19182       return CLASSTYPE_DESTRUCTORS (type);
19183     }
19184
19185   /* By this point, the NAME should be an ordinary identifier.  If
19186      the id-expression was a qualified name, the qualifying scope is
19187      stored in PARSER->SCOPE at this point.  */
19188   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19189
19190   /* Perform the lookup.  */
19191   if (parser->scope)
19192     {
19193       bool dependent_p;
19194
19195       if (parser->scope == error_mark_node)
19196         return error_mark_node;
19197
19198       /* If the SCOPE is dependent, the lookup must be deferred until
19199          the template is instantiated -- unless we are explicitly
19200          looking up names in uninstantiated templates.  Even then, we
19201          cannot look up the name if the scope is not a class type; it
19202          might, for example, be a template type parameter.  */
19203       dependent_p = (TYPE_P (parser->scope)
19204                      && dependent_scope_p (parser->scope));
19205       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19206           && dependent_p)
19207         /* Defer lookup.  */
19208         decl = error_mark_node;
19209       else
19210         {
19211           tree pushed_scope = NULL_TREE;
19212
19213           /* If PARSER->SCOPE is a dependent type, then it must be a
19214              class type, and we must not be checking dependencies;
19215              otherwise, we would have processed this lookup above.  So
19216              that PARSER->SCOPE is not considered a dependent base by
19217              lookup_member, we must enter the scope here.  */
19218           if (dependent_p)
19219             pushed_scope = push_scope (parser->scope);
19220
19221           /* If the PARSER->SCOPE is a template specialization, it
19222              may be instantiated during name lookup.  In that case,
19223              errors may be issued.  Even if we rollback the current
19224              tentative parse, those errors are valid.  */
19225           decl = lookup_qualified_name (parser->scope, name,
19226                                         tag_type != none_type,
19227                                         /*complain=*/true);
19228
19229           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19230              lookup result and the nested-name-specifier nominates a class C:
19231                * if the name specified after the nested-name-specifier, when
19232                looked up in C, is the injected-class-name of C (Clause 9), or
19233                * if the name specified after the nested-name-specifier is the
19234                same as the identifier or the simple-template-id's template-
19235                name in the last component of the nested-name-specifier,
19236              the name is instead considered to name the constructor of
19237              class C. [ Note: for example, the constructor is not an
19238              acceptable lookup result in an elaborated-type-specifier so
19239              the constructor would not be used in place of the
19240              injected-class-name. --end note ] Such a constructor name
19241              shall be used only in the declarator-id of a declaration that
19242              names a constructor or in a using-declaration.  */
19243           if (tag_type == none_type
19244               && DECL_SELF_REFERENCE_P (decl)
19245               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19246             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19247                                           tag_type != none_type,
19248                                           /*complain=*/true);
19249
19250           /* If we have a single function from a using decl, pull it out.  */
19251           if (TREE_CODE (decl) == OVERLOAD
19252               && !really_overloaded_fn (decl))
19253             decl = OVL_FUNCTION (decl);
19254
19255           if (pushed_scope)
19256             pop_scope (pushed_scope);
19257         }
19258
19259       /* If the scope is a dependent type and either we deferred lookup or
19260          we did lookup but didn't find the name, rememeber the name.  */
19261       if (decl == error_mark_node && TYPE_P (parser->scope)
19262           && dependent_type_p (parser->scope))
19263         {
19264           if (tag_type)
19265             {
19266               tree type;
19267
19268               /* The resolution to Core Issue 180 says that `struct
19269                  A::B' should be considered a type-name, even if `A'
19270                  is dependent.  */
19271               type = make_typename_type (parser->scope, name, tag_type,
19272                                          /*complain=*/tf_error);
19273               decl = TYPE_NAME (type);
19274             }
19275           else if (is_template
19276                    && (cp_parser_next_token_ends_template_argument_p (parser)
19277                        || cp_lexer_next_token_is (parser->lexer,
19278                                                   CPP_CLOSE_PAREN)))
19279             decl = make_unbound_class_template (parser->scope,
19280                                                 name, NULL_TREE,
19281                                                 /*complain=*/tf_error);
19282           else
19283             decl = build_qualified_name (/*type=*/NULL_TREE,
19284                                          parser->scope, name,
19285                                          is_template);
19286         }
19287       parser->qualifying_scope = parser->scope;
19288       parser->object_scope = NULL_TREE;
19289     }
19290   else if (object_type)
19291     {
19292       tree object_decl = NULL_TREE;
19293       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19294          OBJECT_TYPE is not a class.  */
19295       if (CLASS_TYPE_P (object_type))
19296         /* If the OBJECT_TYPE is a template specialization, it may
19297            be instantiated during name lookup.  In that case, errors
19298            may be issued.  Even if we rollback the current tentative
19299            parse, those errors are valid.  */
19300         object_decl = lookup_member (object_type,
19301                                      name,
19302                                      /*protect=*/0,
19303                                      tag_type != none_type);
19304       /* Look it up in the enclosing context, too.  */
19305       decl = lookup_name_real (name, tag_type != none_type,
19306                                /*nonclass=*/0,
19307                                /*block_p=*/true, is_namespace, flags);
19308       parser->object_scope = object_type;
19309       parser->qualifying_scope = NULL_TREE;
19310       if (object_decl)
19311         decl = object_decl;
19312     }
19313   else
19314     {
19315       decl = lookup_name_real (name, tag_type != none_type,
19316                                /*nonclass=*/0,
19317                                /*block_p=*/true, is_namespace, flags);
19318       parser->qualifying_scope = NULL_TREE;
19319       parser->object_scope = NULL_TREE;
19320     }
19321
19322   /* If the lookup failed, let our caller know.  */
19323   if (!decl || decl == error_mark_node)
19324     return error_mark_node;
19325
19326   /* Pull out the template from an injected-class-name (or multiple).  */
19327   if (is_template)
19328     decl = maybe_get_template_decl_from_type_decl (decl);
19329
19330   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19331   if (TREE_CODE (decl) == TREE_LIST)
19332     {
19333       if (ambiguous_decls)
19334         *ambiguous_decls = decl;
19335       /* The error message we have to print is too complicated for
19336          cp_parser_error, so we incorporate its actions directly.  */
19337       if (!cp_parser_simulate_error (parser))
19338         {
19339           error_at (name_location, "reference to %qD is ambiguous",
19340                     name);
19341           print_candidates (decl);
19342         }
19343       return error_mark_node;
19344     }
19345
19346   gcc_assert (DECL_P (decl)
19347               || TREE_CODE (decl) == OVERLOAD
19348               || TREE_CODE (decl) == SCOPE_REF
19349               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19350               || BASELINK_P (decl));
19351
19352   /* If we have resolved the name of a member declaration, check to
19353      see if the declaration is accessible.  When the name resolves to
19354      set of overloaded functions, accessibility is checked when
19355      overload resolution is done.
19356
19357      During an explicit instantiation, access is not checked at all,
19358      as per [temp.explicit].  */
19359   if (DECL_P (decl))
19360     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19361
19362   return decl;
19363 }
19364
19365 /* Like cp_parser_lookup_name, but for use in the typical case where
19366    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19367    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19368
19369 static tree
19370 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19371 {
19372   return cp_parser_lookup_name (parser, name,
19373                                 none_type,
19374                                 /*is_template=*/false,
19375                                 /*is_namespace=*/false,
19376                                 /*check_dependency=*/true,
19377                                 /*ambiguous_decls=*/NULL,
19378                                 location);
19379 }
19380
19381 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19382    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19383    true, the DECL indicates the class being defined in a class-head,
19384    or declared in an elaborated-type-specifier.
19385
19386    Otherwise, return DECL.  */
19387
19388 static tree
19389 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19390 {
19391   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19392      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19393
19394        struct A {
19395          template <typename T> struct B;
19396        };
19397
19398        template <typename T> struct A::B {};
19399
19400      Similarly, in an elaborated-type-specifier:
19401
19402        namespace N { struct X{}; }
19403
19404        struct A {
19405          template <typename T> friend struct N::X;
19406        };
19407
19408      However, if the DECL refers to a class type, and we are in
19409      the scope of the class, then the name lookup automatically
19410      finds the TYPE_DECL created by build_self_reference rather
19411      than a TEMPLATE_DECL.  For example, in:
19412
19413        template <class T> struct S {
19414          S s;
19415        };
19416
19417      there is no need to handle such case.  */
19418
19419   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19420     return DECL_TEMPLATE_RESULT (decl);
19421
19422   return decl;
19423 }
19424
19425 /* If too many, or too few, template-parameter lists apply to the
19426    declarator, issue an error message.  Returns TRUE if all went well,
19427    and FALSE otherwise.  */
19428
19429 static bool
19430 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19431                                                 cp_declarator *declarator,
19432                                                 location_t declarator_location)
19433 {
19434   unsigned num_templates;
19435
19436   /* We haven't seen any classes that involve template parameters yet.  */
19437   num_templates = 0;
19438
19439   switch (declarator->kind)
19440     {
19441     case cdk_id:
19442       if (declarator->u.id.qualifying_scope)
19443         {
19444           tree scope;
19445
19446           scope = declarator->u.id.qualifying_scope;
19447
19448           while (scope && CLASS_TYPE_P (scope))
19449             {
19450               /* You're supposed to have one `template <...>'
19451                  for every template class, but you don't need one
19452                  for a full specialization.  For example:
19453
19454                  template <class T> struct S{};
19455                  template <> struct S<int> { void f(); };
19456                  void S<int>::f () {}
19457
19458                  is correct; there shouldn't be a `template <>' for
19459                  the definition of `S<int>::f'.  */
19460               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19461                 /* If SCOPE does not have template information of any
19462                    kind, then it is not a template, nor is it nested
19463                    within a template.  */
19464                 break;
19465               if (explicit_class_specialization_p (scope))
19466                 break;
19467               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19468                 ++num_templates;
19469
19470               scope = TYPE_CONTEXT (scope);
19471             }
19472         }
19473       else if (TREE_CODE (declarator->u.id.unqualified_name)
19474                == TEMPLATE_ID_EXPR)
19475         /* If the DECLARATOR has the form `X<y>' then it uses one
19476            additional level of template parameters.  */
19477         ++num_templates;
19478
19479       return cp_parser_check_template_parameters 
19480         (parser, num_templates, declarator_location, declarator);
19481
19482
19483     case cdk_function:
19484     case cdk_array:
19485     case cdk_pointer:
19486     case cdk_reference:
19487     case cdk_ptrmem:
19488       return (cp_parser_check_declarator_template_parameters
19489               (parser, declarator->declarator, declarator_location));
19490
19491     case cdk_error:
19492       return true;
19493
19494     default:
19495       gcc_unreachable ();
19496     }
19497   return false;
19498 }
19499
19500 /* NUM_TEMPLATES were used in the current declaration.  If that is
19501    invalid, return FALSE and issue an error messages.  Otherwise,
19502    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19503    declarator and we can print more accurate diagnostics.  */
19504
19505 static bool
19506 cp_parser_check_template_parameters (cp_parser* parser,
19507                                      unsigned num_templates,
19508                                      location_t location,
19509                                      cp_declarator *declarator)
19510 {
19511   /* If there are the same number of template classes and parameter
19512      lists, that's OK.  */
19513   if (parser->num_template_parameter_lists == num_templates)
19514     return true;
19515   /* If there are more, but only one more, then we are referring to a
19516      member template.  That's OK too.  */
19517   if (parser->num_template_parameter_lists == num_templates + 1)
19518     return true;
19519   /* If there are more template classes than parameter lists, we have
19520      something like:
19521
19522        template <class T> void S<T>::R<T>::f ();  */
19523   if (parser->num_template_parameter_lists < num_templates)
19524     {
19525       if (declarator && !current_function_decl)
19526         error_at (location, "specializing member %<%T::%E%> "
19527                   "requires %<template<>%> syntax", 
19528                   declarator->u.id.qualifying_scope,
19529                   declarator->u.id.unqualified_name);
19530       else if (declarator)
19531         error_at (location, "invalid declaration of %<%T::%E%>",
19532                   declarator->u.id.qualifying_scope,
19533                   declarator->u.id.unqualified_name);
19534       else 
19535         error_at (location, "too few template-parameter-lists");
19536       return false;
19537     }
19538   /* Otherwise, there are too many template parameter lists.  We have
19539      something like:
19540
19541      template <class T> template <class U> void S::f();  */
19542   error_at (location, "too many template-parameter-lists");
19543   return false;
19544 }
19545
19546 /* Parse an optional `::' token indicating that the following name is
19547    from the global namespace.  If so, PARSER->SCOPE is set to the
19548    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19549    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19550    Returns the new value of PARSER->SCOPE, if the `::' token is
19551    present, and NULL_TREE otherwise.  */
19552
19553 static tree
19554 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19555 {
19556   cp_token *token;
19557
19558   /* Peek at the next token.  */
19559   token = cp_lexer_peek_token (parser->lexer);
19560   /* If we're looking at a `::' token then we're starting from the
19561      global namespace, not our current location.  */
19562   if (token->type == CPP_SCOPE)
19563     {
19564       /* Consume the `::' token.  */
19565       cp_lexer_consume_token (parser->lexer);
19566       /* Set the SCOPE so that we know where to start the lookup.  */
19567       parser->scope = global_namespace;
19568       parser->qualifying_scope = global_namespace;
19569       parser->object_scope = NULL_TREE;
19570
19571       return parser->scope;
19572     }
19573   else if (!current_scope_valid_p)
19574     {
19575       parser->scope = NULL_TREE;
19576       parser->qualifying_scope = NULL_TREE;
19577       parser->object_scope = NULL_TREE;
19578     }
19579
19580   return NULL_TREE;
19581 }
19582
19583 /* Returns TRUE if the upcoming token sequence is the start of a
19584    constructor declarator.  If FRIEND_P is true, the declarator is
19585    preceded by the `friend' specifier.  */
19586
19587 static bool
19588 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19589 {
19590   bool constructor_p;
19591   tree nested_name_specifier;
19592   cp_token *next_token;
19593
19594   /* The common case is that this is not a constructor declarator, so
19595      try to avoid doing lots of work if at all possible.  It's not
19596      valid declare a constructor at function scope.  */
19597   if (parser->in_function_body)
19598     return false;
19599   /* And only certain tokens can begin a constructor declarator.  */
19600   next_token = cp_lexer_peek_token (parser->lexer);
19601   if (next_token->type != CPP_NAME
19602       && next_token->type != CPP_SCOPE
19603       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19604       && next_token->type != CPP_TEMPLATE_ID)
19605     return false;
19606
19607   /* Parse tentatively; we are going to roll back all of the tokens
19608      consumed here.  */
19609   cp_parser_parse_tentatively (parser);
19610   /* Assume that we are looking at a constructor declarator.  */
19611   constructor_p = true;
19612
19613   /* Look for the optional `::' operator.  */
19614   cp_parser_global_scope_opt (parser,
19615                               /*current_scope_valid_p=*/false);
19616   /* Look for the nested-name-specifier.  */
19617   nested_name_specifier
19618     = (cp_parser_nested_name_specifier_opt (parser,
19619                                             /*typename_keyword_p=*/false,
19620                                             /*check_dependency_p=*/false,
19621                                             /*type_p=*/false,
19622                                             /*is_declaration=*/false));
19623   /* Outside of a class-specifier, there must be a
19624      nested-name-specifier.  */
19625   if (!nested_name_specifier &&
19626       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19627        || friend_p))
19628     constructor_p = false;
19629   else if (nested_name_specifier == error_mark_node)
19630     constructor_p = false;
19631
19632   /* If we have a class scope, this is easy; DR 147 says that S::S always
19633      names the constructor, and no other qualified name could.  */
19634   if (constructor_p && nested_name_specifier
19635       && TYPE_P (nested_name_specifier))
19636     {
19637       tree id = cp_parser_unqualified_id (parser,
19638                                           /*template_keyword_p=*/false,
19639                                           /*check_dependency_p=*/false,
19640                                           /*declarator_p=*/true,
19641                                           /*optional_p=*/false);
19642       if (is_overloaded_fn (id))
19643         id = DECL_NAME (get_first_fn (id));
19644       if (!constructor_name_p (id, nested_name_specifier))
19645         constructor_p = false;
19646     }
19647   /* If we still think that this might be a constructor-declarator,
19648      look for a class-name.  */
19649   else if (constructor_p)
19650     {
19651       /* If we have:
19652
19653            template <typename T> struct S {
19654              S();
19655            };
19656
19657          we must recognize that the nested `S' names a class.  */
19658       tree type_decl;
19659       type_decl = cp_parser_class_name (parser,
19660                                         /*typename_keyword_p=*/false,
19661                                         /*template_keyword_p=*/false,
19662                                         none_type,
19663                                         /*check_dependency_p=*/false,
19664                                         /*class_head_p=*/false,
19665                                         /*is_declaration=*/false);
19666       /* If there was no class-name, then this is not a constructor.  */
19667       constructor_p = !cp_parser_error_occurred (parser);
19668
19669       /* If we're still considering a constructor, we have to see a `(',
19670          to begin the parameter-declaration-clause, followed by either a
19671          `)', an `...', or a decl-specifier.  We need to check for a
19672          type-specifier to avoid being fooled into thinking that:
19673
19674            S (f) (int);
19675
19676          is a constructor.  (It is actually a function named `f' that
19677          takes one parameter (of type `int') and returns a value of type
19678          `S'.  */
19679       if (constructor_p
19680           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19681         constructor_p = false;
19682
19683       if (constructor_p
19684           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19685           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19686           /* A parameter declaration begins with a decl-specifier,
19687              which is either the "attribute" keyword, a storage class
19688              specifier, or (usually) a type-specifier.  */
19689           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19690         {
19691           tree type;
19692           tree pushed_scope = NULL_TREE;
19693           unsigned saved_num_template_parameter_lists;
19694
19695           /* Names appearing in the type-specifier should be looked up
19696              in the scope of the class.  */
19697           if (current_class_type)
19698             type = NULL_TREE;
19699           else
19700             {
19701               type = TREE_TYPE (type_decl);
19702               if (TREE_CODE (type) == TYPENAME_TYPE)
19703                 {
19704                   type = resolve_typename_type (type,
19705                                                 /*only_current_p=*/false);
19706                   if (TREE_CODE (type) == TYPENAME_TYPE)
19707                     {
19708                       cp_parser_abort_tentative_parse (parser);
19709                       return false;
19710                     }
19711                 }
19712               pushed_scope = push_scope (type);
19713             }
19714
19715           /* Inside the constructor parameter list, surrounding
19716              template-parameter-lists do not apply.  */
19717           saved_num_template_parameter_lists
19718             = parser->num_template_parameter_lists;
19719           parser->num_template_parameter_lists = 0;
19720
19721           /* Look for the type-specifier.  */
19722           cp_parser_type_specifier (parser,
19723                                     CP_PARSER_FLAGS_NONE,
19724                                     /*decl_specs=*/NULL,
19725                                     /*is_declarator=*/true,
19726                                     /*declares_class_or_enum=*/NULL,
19727                                     /*is_cv_qualifier=*/NULL);
19728
19729           parser->num_template_parameter_lists
19730             = saved_num_template_parameter_lists;
19731
19732           /* Leave the scope of the class.  */
19733           if (pushed_scope)
19734             pop_scope (pushed_scope);
19735
19736           constructor_p = !cp_parser_error_occurred (parser);
19737         }
19738     }
19739
19740   /* We did not really want to consume any tokens.  */
19741   cp_parser_abort_tentative_parse (parser);
19742
19743   return constructor_p;
19744 }
19745
19746 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19747    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19748    they must be performed once we are in the scope of the function.
19749
19750    Returns the function defined.  */
19751
19752 static tree
19753 cp_parser_function_definition_from_specifiers_and_declarator
19754   (cp_parser* parser,
19755    cp_decl_specifier_seq *decl_specifiers,
19756    tree attributes,
19757    const cp_declarator *declarator)
19758 {
19759   tree fn;
19760   bool success_p;
19761
19762   /* Begin the function-definition.  */
19763   success_p = start_function (decl_specifiers, declarator, attributes);
19764
19765   /* The things we're about to see are not directly qualified by any
19766      template headers we've seen thus far.  */
19767   reset_specialization ();
19768
19769   /* If there were names looked up in the decl-specifier-seq that we
19770      did not check, check them now.  We must wait until we are in the
19771      scope of the function to perform the checks, since the function
19772      might be a friend.  */
19773   perform_deferred_access_checks ();
19774
19775   if (!success_p)
19776     {
19777       /* Skip the entire function.  */
19778       cp_parser_skip_to_end_of_block_or_statement (parser);
19779       fn = error_mark_node;
19780     }
19781   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19782     {
19783       /* Seen already, skip it.  An error message has already been output.  */
19784       cp_parser_skip_to_end_of_block_or_statement (parser);
19785       fn = current_function_decl;
19786       current_function_decl = NULL_TREE;
19787       /* If this is a function from a class, pop the nested class.  */
19788       if (current_class_name)
19789         pop_nested_class ();
19790     }
19791   else
19792     fn = cp_parser_function_definition_after_declarator (parser,
19793                                                          /*inline_p=*/false);
19794
19795   return fn;
19796 }
19797
19798 /* Parse the part of a function-definition that follows the
19799    declarator.  INLINE_P is TRUE iff this function is an inline
19800    function defined within a class-specifier.
19801
19802    Returns the function defined.  */
19803
19804 static tree
19805 cp_parser_function_definition_after_declarator (cp_parser* parser,
19806                                                 bool inline_p)
19807 {
19808   tree fn;
19809   bool ctor_initializer_p = false;
19810   bool saved_in_unbraced_linkage_specification_p;
19811   bool saved_in_function_body;
19812   unsigned saved_num_template_parameter_lists;
19813   cp_token *token;
19814
19815   saved_in_function_body = parser->in_function_body;
19816   parser->in_function_body = true;
19817   /* If the next token is `return', then the code may be trying to
19818      make use of the "named return value" extension that G++ used to
19819      support.  */
19820   token = cp_lexer_peek_token (parser->lexer);
19821   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19822     {
19823       /* Consume the `return' keyword.  */
19824       cp_lexer_consume_token (parser->lexer);
19825       /* Look for the identifier that indicates what value is to be
19826          returned.  */
19827       cp_parser_identifier (parser);
19828       /* Issue an error message.  */
19829       error_at (token->location,
19830                 "named return values are no longer supported");
19831       /* Skip tokens until we reach the start of the function body.  */
19832       while (true)
19833         {
19834           cp_token *token = cp_lexer_peek_token (parser->lexer);
19835           if (token->type == CPP_OPEN_BRACE
19836               || token->type == CPP_EOF
19837               || token->type == CPP_PRAGMA_EOL)
19838             break;
19839           cp_lexer_consume_token (parser->lexer);
19840         }
19841     }
19842   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19843      anything declared inside `f'.  */
19844   saved_in_unbraced_linkage_specification_p
19845     = parser->in_unbraced_linkage_specification_p;
19846   parser->in_unbraced_linkage_specification_p = false;
19847   /* Inside the function, surrounding template-parameter-lists do not
19848      apply.  */
19849   saved_num_template_parameter_lists
19850     = parser->num_template_parameter_lists;
19851   parser->num_template_parameter_lists = 0;
19852
19853   start_lambda_scope (current_function_decl);
19854
19855   /* If the next token is `try', then we are looking at a
19856      function-try-block.  */
19857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19858     ctor_initializer_p = cp_parser_function_try_block (parser);
19859   /* A function-try-block includes the function-body, so we only do
19860      this next part if we're not processing a function-try-block.  */
19861   else
19862     ctor_initializer_p
19863       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19864
19865   finish_lambda_scope ();
19866
19867   /* Finish the function.  */
19868   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19869                         (inline_p ? 2 : 0));
19870   /* Generate code for it, if necessary.  */
19871   expand_or_defer_fn (fn);
19872   /* Restore the saved values.  */
19873   parser->in_unbraced_linkage_specification_p
19874     = saved_in_unbraced_linkage_specification_p;
19875   parser->num_template_parameter_lists
19876     = saved_num_template_parameter_lists;
19877   parser->in_function_body = saved_in_function_body;
19878
19879   return fn;
19880 }
19881
19882 /* Parse a template-declaration, assuming that the `export' (and
19883    `extern') keywords, if present, has already been scanned.  MEMBER_P
19884    is as for cp_parser_template_declaration.  */
19885
19886 static void
19887 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19888 {
19889   tree decl = NULL_TREE;
19890   VEC (deferred_access_check,gc) *checks;
19891   tree parameter_list;
19892   bool friend_p = false;
19893   bool need_lang_pop;
19894   cp_token *token;
19895
19896   /* Look for the `template' keyword.  */
19897   token = cp_lexer_peek_token (parser->lexer);
19898   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19899     return;
19900
19901   /* And the `<'.  */
19902   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19903     return;
19904   if (at_class_scope_p () && current_function_decl)
19905     {
19906       /* 14.5.2.2 [temp.mem]
19907
19908          A local class shall not have member templates.  */
19909       error_at (token->location,
19910                 "invalid declaration of member template in local class");
19911       cp_parser_skip_to_end_of_block_or_statement (parser);
19912       return;
19913     }
19914   /* [temp]
19915
19916      A template ... shall not have C linkage.  */
19917   if (current_lang_name == lang_name_c)
19918     {
19919       error_at (token->location, "template with C linkage");
19920       /* Give it C++ linkage to avoid confusing other parts of the
19921          front end.  */
19922       push_lang_context (lang_name_cplusplus);
19923       need_lang_pop = true;
19924     }
19925   else
19926     need_lang_pop = false;
19927
19928   /* We cannot perform access checks on the template parameter
19929      declarations until we know what is being declared, just as we
19930      cannot check the decl-specifier list.  */
19931   push_deferring_access_checks (dk_deferred);
19932
19933   /* If the next token is `>', then we have an invalid
19934      specialization.  Rather than complain about an invalid template
19935      parameter, issue an error message here.  */
19936   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19937     {
19938       cp_parser_error (parser, "invalid explicit specialization");
19939       begin_specialization ();
19940       parameter_list = NULL_TREE;
19941     }
19942   else
19943     {
19944       /* Parse the template parameters.  */
19945       parameter_list = cp_parser_template_parameter_list (parser);
19946       fixup_template_parms ();
19947     }
19948
19949   /* Get the deferred access checks from the parameter list.  These
19950      will be checked once we know what is being declared, as for a
19951      member template the checks must be performed in the scope of the
19952      class containing the member.  */
19953   checks = get_deferred_access_checks ();
19954
19955   /* Look for the `>'.  */
19956   cp_parser_skip_to_end_of_template_parameter_list (parser);
19957   /* We just processed one more parameter list.  */
19958   ++parser->num_template_parameter_lists;
19959   /* If the next token is `template', there are more template
19960      parameters.  */
19961   if (cp_lexer_next_token_is_keyword (parser->lexer,
19962                                       RID_TEMPLATE))
19963     cp_parser_template_declaration_after_export (parser, member_p);
19964   else
19965     {
19966       /* There are no access checks when parsing a template, as we do not
19967          know if a specialization will be a friend.  */
19968       push_deferring_access_checks (dk_no_check);
19969       token = cp_lexer_peek_token (parser->lexer);
19970       decl = cp_parser_single_declaration (parser,
19971                                            checks,
19972                                            member_p,
19973                                            /*explicit_specialization_p=*/false,
19974                                            &friend_p);
19975       pop_deferring_access_checks ();
19976
19977       /* If this is a member template declaration, let the front
19978          end know.  */
19979       if (member_p && !friend_p && decl)
19980         {
19981           if (TREE_CODE (decl) == TYPE_DECL)
19982             cp_parser_check_access_in_redeclaration (decl, token->location);
19983
19984           decl = finish_member_template_decl (decl);
19985         }
19986       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19987         make_friend_class (current_class_type, TREE_TYPE (decl),
19988                            /*complain=*/true);
19989     }
19990   /* We are done with the current parameter list.  */
19991   --parser->num_template_parameter_lists;
19992
19993   pop_deferring_access_checks ();
19994
19995   /* Finish up.  */
19996   finish_template_decl (parameter_list);
19997
19998   /* Register member declarations.  */
19999   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20000     finish_member_declaration (decl);
20001   /* For the erroneous case of a template with C linkage, we pushed an
20002      implicit C++ linkage scope; exit that scope now.  */
20003   if (need_lang_pop)
20004     pop_lang_context ();
20005   /* If DECL is a function template, we must return to parse it later.
20006      (Even though there is no definition, there might be default
20007      arguments that need handling.)  */
20008   if (member_p && decl
20009       && (TREE_CODE (decl) == FUNCTION_DECL
20010           || DECL_FUNCTION_TEMPLATE_P (decl)))
20011     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20012 }
20013
20014 /* Perform the deferred access checks from a template-parameter-list.
20015    CHECKS is a TREE_LIST of access checks, as returned by
20016    get_deferred_access_checks.  */
20017
20018 static void
20019 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20020 {
20021   ++processing_template_parmlist;
20022   perform_access_checks (checks);
20023   --processing_template_parmlist;
20024 }
20025
20026 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20027    `function-definition' sequence.  MEMBER_P is true, this declaration
20028    appears in a class scope.
20029
20030    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20031    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20032
20033 static tree
20034 cp_parser_single_declaration (cp_parser* parser,
20035                               VEC (deferred_access_check,gc)* checks,
20036                               bool member_p,
20037                               bool explicit_specialization_p,
20038                               bool* friend_p)
20039 {
20040   int declares_class_or_enum;
20041   tree decl = NULL_TREE;
20042   cp_decl_specifier_seq decl_specifiers;
20043   bool function_definition_p = false;
20044   cp_token *decl_spec_token_start;
20045
20046   /* This function is only used when processing a template
20047      declaration.  */
20048   gcc_assert (innermost_scope_kind () == sk_template_parms
20049               || innermost_scope_kind () == sk_template_spec);
20050
20051   /* Defer access checks until we know what is being declared.  */
20052   push_deferring_access_checks (dk_deferred);
20053
20054   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20055      alternative.  */
20056   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20057   cp_parser_decl_specifier_seq (parser,
20058                                 CP_PARSER_FLAGS_OPTIONAL,
20059                                 &decl_specifiers,
20060                                 &declares_class_or_enum);
20061   if (friend_p)
20062     *friend_p = cp_parser_friend_p (&decl_specifiers);
20063
20064   /* There are no template typedefs.  */
20065   if (decl_specifiers.specs[(int) ds_typedef])
20066     {
20067       error_at (decl_spec_token_start->location,
20068                 "template declaration of %<typedef%>");
20069       decl = error_mark_node;
20070     }
20071
20072   /* Gather up the access checks that occurred the
20073      decl-specifier-seq.  */
20074   stop_deferring_access_checks ();
20075
20076   /* Check for the declaration of a template class.  */
20077   if (declares_class_or_enum)
20078     {
20079       if (cp_parser_declares_only_class_p (parser))
20080         {
20081           decl = shadow_tag (&decl_specifiers);
20082
20083           /* In this case:
20084
20085                struct C {
20086                  friend template <typename T> struct A<T>::B;
20087                };
20088
20089              A<T>::B will be represented by a TYPENAME_TYPE, and
20090              therefore not recognized by shadow_tag.  */
20091           if (friend_p && *friend_p
20092               && !decl
20093               && decl_specifiers.type
20094               && TYPE_P (decl_specifiers.type))
20095             decl = decl_specifiers.type;
20096
20097           if (decl && decl != error_mark_node)
20098             decl = TYPE_NAME (decl);
20099           else
20100             decl = error_mark_node;
20101
20102           /* Perform access checks for template parameters.  */
20103           cp_parser_perform_template_parameter_access_checks (checks);
20104         }
20105     }
20106
20107   /* Complain about missing 'typename' or other invalid type names.  */
20108   if (!decl_specifiers.any_type_specifiers_p)
20109     cp_parser_parse_and_diagnose_invalid_type_name (parser);
20110
20111   /* If it's not a template class, try for a template function.  If
20112      the next token is a `;', then this declaration does not declare
20113      anything.  But, if there were errors in the decl-specifiers, then
20114      the error might well have come from an attempted class-specifier.
20115      In that case, there's no need to warn about a missing declarator.  */
20116   if (!decl
20117       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20118           || decl_specifiers.type != error_mark_node))
20119     {
20120       decl = cp_parser_init_declarator (parser,
20121                                         &decl_specifiers,
20122                                         checks,
20123                                         /*function_definition_allowed_p=*/true,
20124                                         member_p,
20125                                         declares_class_or_enum,
20126                                         &function_definition_p,
20127                                         NULL);
20128
20129     /* 7.1.1-1 [dcl.stc]
20130
20131        A storage-class-specifier shall not be specified in an explicit
20132        specialization...  */
20133     if (decl
20134         && explicit_specialization_p
20135         && decl_specifiers.storage_class != sc_none)
20136       {
20137         error_at (decl_spec_token_start->location,
20138                   "explicit template specialization cannot have a storage class");
20139         decl = error_mark_node;
20140       }
20141     }
20142
20143   pop_deferring_access_checks ();
20144
20145   /* Clear any current qualification; whatever comes next is the start
20146      of something new.  */
20147   parser->scope = NULL_TREE;
20148   parser->qualifying_scope = NULL_TREE;
20149   parser->object_scope = NULL_TREE;
20150   /* Look for a trailing `;' after the declaration.  */
20151   if (!function_definition_p
20152       && (decl == error_mark_node
20153           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20154     cp_parser_skip_to_end_of_block_or_statement (parser);
20155
20156   return decl;
20157 }
20158
20159 /* Parse a cast-expression that is not the operand of a unary "&".  */
20160
20161 static tree
20162 cp_parser_simple_cast_expression (cp_parser *parser)
20163 {
20164   return cp_parser_cast_expression (parser, /*address_p=*/false,
20165                                     /*cast_p=*/false, NULL);
20166 }
20167
20168 /* Parse a functional cast to TYPE.  Returns an expression
20169    representing the cast.  */
20170
20171 static tree
20172 cp_parser_functional_cast (cp_parser* parser, tree type)
20173 {
20174   VEC(tree,gc) *vec;
20175   tree expression_list;
20176   tree cast;
20177   bool nonconst_p;
20178
20179   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20180     {
20181       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20182       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20183       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20184       if (TREE_CODE (type) == TYPE_DECL)
20185         type = TREE_TYPE (type);
20186       return finish_compound_literal (type, expression_list);
20187     }
20188
20189
20190   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20191                                                  /*cast_p=*/true,
20192                                                  /*allow_expansion_p=*/true,
20193                                                  /*non_constant_p=*/NULL);
20194   if (vec == NULL)
20195     expression_list = error_mark_node;
20196   else
20197     {
20198       expression_list = build_tree_list_vec (vec);
20199       release_tree_vector (vec);
20200     }
20201
20202   cast = build_functional_cast (type, expression_list,
20203                                 tf_warning_or_error);
20204   /* [expr.const]/1: In an integral constant expression "only type
20205      conversions to integral or enumeration type can be used".  */
20206   if (TREE_CODE (type) == TYPE_DECL)
20207     type = TREE_TYPE (type);
20208   if (cast != error_mark_node
20209       && !cast_valid_in_integral_constant_expression_p (type)
20210       && cp_parser_non_integral_constant_expression (parser,
20211                                                      NIC_CONSTRUCTOR))
20212     return error_mark_node;
20213   return cast;
20214 }
20215
20216 /* Save the tokens that make up the body of a member function defined
20217    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20218    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20219    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20220    for the member function.  */
20221
20222 static tree
20223 cp_parser_save_member_function_body (cp_parser* parser,
20224                                      cp_decl_specifier_seq *decl_specifiers,
20225                                      cp_declarator *declarator,
20226                                      tree attributes)
20227 {
20228   cp_token *first;
20229   cp_token *last;
20230   tree fn;
20231
20232   /* Create the FUNCTION_DECL.  */
20233   fn = grokmethod (decl_specifiers, declarator, attributes);
20234   /* If something went badly wrong, bail out now.  */
20235   if (fn == error_mark_node)
20236     {
20237       /* If there's a function-body, skip it.  */
20238       if (cp_parser_token_starts_function_definition_p
20239           (cp_lexer_peek_token (parser->lexer)))
20240         cp_parser_skip_to_end_of_block_or_statement (parser);
20241       return error_mark_node;
20242     }
20243
20244   /* Remember it, if there default args to post process.  */
20245   cp_parser_save_default_args (parser, fn);
20246
20247   /* Save away the tokens that make up the body of the
20248      function.  */
20249   first = parser->lexer->next_token;
20250   /* We can have braced-init-list mem-initializers before the fn body.  */
20251   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20252     {
20253       cp_lexer_consume_token (parser->lexer);
20254       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20255              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20256         {
20257           /* cache_group will stop after an un-nested { } pair, too.  */
20258           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20259             break;
20260
20261           /* variadic mem-inits have ... after the ')'.  */
20262           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20263             cp_lexer_consume_token (parser->lexer);
20264         }
20265     }
20266   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20267   /* Handle function try blocks.  */
20268   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20269     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20270   last = parser->lexer->next_token;
20271
20272   /* Save away the inline definition; we will process it when the
20273      class is complete.  */
20274   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20275   DECL_PENDING_INLINE_P (fn) = 1;
20276
20277   /* We need to know that this was defined in the class, so that
20278      friend templates are handled correctly.  */
20279   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20280
20281   /* Add FN to the queue of functions to be parsed later.  */
20282   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20283
20284   return fn;
20285 }
20286
20287 /* Parse a template-argument-list, as well as the trailing ">" (but
20288    not the opening ">").  See cp_parser_template_argument_list for the
20289    return value.  */
20290
20291 static tree
20292 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20293 {
20294   tree arguments;
20295   tree saved_scope;
20296   tree saved_qualifying_scope;
20297   tree saved_object_scope;
20298   bool saved_greater_than_is_operator_p;
20299   int saved_unevaluated_operand;
20300   int saved_inhibit_evaluation_warnings;
20301
20302   /* [temp.names]
20303
20304      When parsing a template-id, the first non-nested `>' is taken as
20305      the end of the template-argument-list rather than a greater-than
20306      operator.  */
20307   saved_greater_than_is_operator_p
20308     = parser->greater_than_is_operator_p;
20309   parser->greater_than_is_operator_p = false;
20310   /* Parsing the argument list may modify SCOPE, so we save it
20311      here.  */
20312   saved_scope = parser->scope;
20313   saved_qualifying_scope = parser->qualifying_scope;
20314   saved_object_scope = parser->object_scope;
20315   /* We need to evaluate the template arguments, even though this
20316      template-id may be nested within a "sizeof".  */
20317   saved_unevaluated_operand = cp_unevaluated_operand;
20318   cp_unevaluated_operand = 0;
20319   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20320   c_inhibit_evaluation_warnings = 0;
20321   /* Parse the template-argument-list itself.  */
20322   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20323       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20324     arguments = NULL_TREE;
20325   else
20326     arguments = cp_parser_template_argument_list (parser);
20327   /* Look for the `>' that ends the template-argument-list. If we find
20328      a '>>' instead, it's probably just a typo.  */
20329   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20330     {
20331       if (cxx_dialect != cxx98)
20332         {
20333           /* In C++0x, a `>>' in a template argument list or cast
20334              expression is considered to be two separate `>'
20335              tokens. So, change the current token to a `>', but don't
20336              consume it: it will be consumed later when the outer
20337              template argument list (or cast expression) is parsed.
20338              Note that this replacement of `>' for `>>' is necessary
20339              even if we are parsing tentatively: in the tentative
20340              case, after calling
20341              cp_parser_enclosed_template_argument_list we will always
20342              throw away all of the template arguments and the first
20343              closing `>', either because the template argument list
20344              was erroneous or because we are replacing those tokens
20345              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20346              not have been thrown away) is needed either to close an
20347              outer template argument list or to complete a new-style
20348              cast.  */
20349           cp_token *token = cp_lexer_peek_token (parser->lexer);
20350           token->type = CPP_GREATER;
20351         }
20352       else if (!saved_greater_than_is_operator_p)
20353         {
20354           /* If we're in a nested template argument list, the '>>' has
20355             to be a typo for '> >'. We emit the error message, but we
20356             continue parsing and we push a '>' as next token, so that
20357             the argument list will be parsed correctly.  Note that the
20358             global source location is still on the token before the
20359             '>>', so we need to say explicitly where we want it.  */
20360           cp_token *token = cp_lexer_peek_token (parser->lexer);
20361           error_at (token->location, "%<>>%> should be %<> >%> "
20362                     "within a nested template argument list");
20363
20364           token->type = CPP_GREATER;
20365         }
20366       else
20367         {
20368           /* If this is not a nested template argument list, the '>>'
20369             is a typo for '>'. Emit an error message and continue.
20370             Same deal about the token location, but here we can get it
20371             right by consuming the '>>' before issuing the diagnostic.  */
20372           cp_token *token = cp_lexer_consume_token (parser->lexer);
20373           error_at (token->location,
20374                     "spurious %<>>%>, use %<>%> to terminate "
20375                     "a template argument list");
20376         }
20377     }
20378   else
20379     cp_parser_skip_to_end_of_template_parameter_list (parser);
20380   /* The `>' token might be a greater-than operator again now.  */
20381   parser->greater_than_is_operator_p
20382     = saved_greater_than_is_operator_p;
20383   /* Restore the SAVED_SCOPE.  */
20384   parser->scope = saved_scope;
20385   parser->qualifying_scope = saved_qualifying_scope;
20386   parser->object_scope = saved_object_scope;
20387   cp_unevaluated_operand = saved_unevaluated_operand;
20388   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20389
20390   return arguments;
20391 }
20392
20393 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20394    arguments, or the body of the function have not yet been parsed,
20395    parse them now.  */
20396
20397 static void
20398 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20399 {
20400   /* If this member is a template, get the underlying
20401      FUNCTION_DECL.  */
20402   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20403     member_function = DECL_TEMPLATE_RESULT (member_function);
20404
20405   /* There should not be any class definitions in progress at this
20406      point; the bodies of members are only parsed outside of all class
20407      definitions.  */
20408   gcc_assert (parser->num_classes_being_defined == 0);
20409   /* While we're parsing the member functions we might encounter more
20410      classes.  We want to handle them right away, but we don't want
20411      them getting mixed up with functions that are currently in the
20412      queue.  */
20413   push_unparsed_function_queues (parser);
20414
20415   /* Make sure that any template parameters are in scope.  */
20416   maybe_begin_member_template_processing (member_function);
20417
20418   /* If the body of the function has not yet been parsed, parse it
20419      now.  */
20420   if (DECL_PENDING_INLINE_P (member_function))
20421     {
20422       tree function_scope;
20423       cp_token_cache *tokens;
20424
20425       /* The function is no longer pending; we are processing it.  */
20426       tokens = DECL_PENDING_INLINE_INFO (member_function);
20427       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20428       DECL_PENDING_INLINE_P (member_function) = 0;
20429
20430       /* If this is a local class, enter the scope of the containing
20431          function.  */
20432       function_scope = current_function_decl;
20433       if (function_scope)
20434         push_function_context ();
20435
20436       /* Push the body of the function onto the lexer stack.  */
20437       cp_parser_push_lexer_for_tokens (parser, tokens);
20438
20439       /* Let the front end know that we going to be defining this
20440          function.  */
20441       start_preparsed_function (member_function, NULL_TREE,
20442                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20443
20444       /* Don't do access checking if it is a templated function.  */
20445       if (processing_template_decl)
20446         push_deferring_access_checks (dk_no_check);
20447
20448       /* Now, parse the body of the function.  */
20449       cp_parser_function_definition_after_declarator (parser,
20450                                                       /*inline_p=*/true);
20451
20452       if (processing_template_decl)
20453         pop_deferring_access_checks ();
20454
20455       /* Leave the scope of the containing function.  */
20456       if (function_scope)
20457         pop_function_context ();
20458       cp_parser_pop_lexer (parser);
20459     }
20460
20461   /* Remove any template parameters from the symbol table.  */
20462   maybe_end_member_template_processing ();
20463
20464   /* Restore the queue.  */
20465   pop_unparsed_function_queues (parser);
20466 }
20467
20468 /* If DECL contains any default args, remember it on the unparsed
20469    functions queue.  */
20470
20471 static void
20472 cp_parser_save_default_args (cp_parser* parser, tree decl)
20473 {
20474   tree probe;
20475
20476   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20477        probe;
20478        probe = TREE_CHAIN (probe))
20479     if (TREE_PURPOSE (probe))
20480       {
20481         cp_default_arg_entry *entry
20482           = VEC_safe_push (cp_default_arg_entry, gc,
20483                            unparsed_funs_with_default_args, NULL);
20484         entry->class_type = current_class_type;
20485         entry->decl = decl;
20486         break;
20487       }
20488 }
20489
20490 /* FN is a FUNCTION_DECL which may contains a parameter with an
20491    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20492    assumes that the current scope is the scope in which the default
20493    argument should be processed.  */
20494
20495 static void
20496 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20497 {
20498   bool saved_local_variables_forbidden_p;
20499   tree parm, parmdecl;
20500
20501   /* While we're parsing the default args, we might (due to the
20502      statement expression extension) encounter more classes.  We want
20503      to handle them right away, but we don't want them getting mixed
20504      up with default args that are currently in the queue.  */
20505   push_unparsed_function_queues (parser);
20506
20507   /* Local variable names (and the `this' keyword) may not appear
20508      in a default argument.  */
20509   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20510   parser->local_variables_forbidden_p = true;
20511
20512   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20513          parmdecl = DECL_ARGUMENTS (fn);
20514        parm && parm != void_list_node;
20515        parm = TREE_CHAIN (parm),
20516          parmdecl = DECL_CHAIN (parmdecl))
20517     {
20518       cp_token_cache *tokens;
20519       tree default_arg = TREE_PURPOSE (parm);
20520       tree parsed_arg;
20521       VEC(tree,gc) *insts;
20522       tree copy;
20523       unsigned ix;
20524
20525       if (!default_arg)
20526         continue;
20527
20528       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20529         /* This can happen for a friend declaration for a function
20530            already declared with default arguments.  */
20531         continue;
20532
20533        /* Push the saved tokens for the default argument onto the parser's
20534           lexer stack.  */
20535       tokens = DEFARG_TOKENS (default_arg);
20536       cp_parser_push_lexer_for_tokens (parser, tokens);
20537
20538       start_lambda_scope (parmdecl);
20539
20540       /* Parse the assignment-expression.  */
20541       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20542       if (parsed_arg == error_mark_node)
20543         {
20544           cp_parser_pop_lexer (parser);
20545           continue;
20546         }
20547
20548       if (!processing_template_decl)
20549         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20550
20551       TREE_PURPOSE (parm) = parsed_arg;
20552
20553       /* Update any instantiations we've already created.  */
20554       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20555            VEC_iterate (tree, insts, ix, copy); ix++)
20556         TREE_PURPOSE (copy) = parsed_arg;
20557
20558       finish_lambda_scope ();
20559
20560       /* If the token stream has not been completely used up, then
20561          there was extra junk after the end of the default
20562          argument.  */
20563       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20564         cp_parser_error (parser, "expected %<,%>");
20565
20566       /* Revert to the main lexer.  */
20567       cp_parser_pop_lexer (parser);
20568     }
20569
20570   /* Make sure no default arg is missing.  */
20571   check_default_args (fn);
20572
20573   /* Restore the state of local_variables_forbidden_p.  */
20574   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20575
20576   /* Restore the queue.  */
20577   pop_unparsed_function_queues (parser);
20578 }
20579
20580 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20581    either a TYPE or an expression, depending on the form of the
20582    input.  The KEYWORD indicates which kind of expression we have
20583    encountered.  */
20584
20585 static tree
20586 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20587 {
20588   tree expr = NULL_TREE;
20589   const char *saved_message;
20590   char *tmp;
20591   bool saved_integral_constant_expression_p;
20592   bool saved_non_integral_constant_expression_p;
20593   bool pack_expansion_p = false;
20594
20595   /* Types cannot be defined in a `sizeof' expression.  Save away the
20596      old message.  */
20597   saved_message = parser->type_definition_forbidden_message;
20598   /* And create the new one.  */
20599   tmp = concat ("types may not be defined in %<",
20600                 IDENTIFIER_POINTER (ridpointers[keyword]),
20601                 "%> expressions", NULL);
20602   parser->type_definition_forbidden_message = tmp;
20603
20604   /* The restrictions on constant-expressions do not apply inside
20605      sizeof expressions.  */
20606   saved_integral_constant_expression_p
20607     = parser->integral_constant_expression_p;
20608   saved_non_integral_constant_expression_p
20609     = parser->non_integral_constant_expression_p;
20610   parser->integral_constant_expression_p = false;
20611
20612   /* If it's a `...', then we are computing the length of a parameter
20613      pack.  */
20614   if (keyword == RID_SIZEOF
20615       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20616     {
20617       /* Consume the `...'.  */
20618       cp_lexer_consume_token (parser->lexer);
20619       maybe_warn_variadic_templates ();
20620
20621       /* Note that this is an expansion.  */
20622       pack_expansion_p = true;
20623     }
20624
20625   /* Do not actually evaluate the expression.  */
20626   ++cp_unevaluated_operand;
20627   ++c_inhibit_evaluation_warnings;
20628   /* If it's a `(', then we might be looking at the type-id
20629      construction.  */
20630   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20631     {
20632       tree type;
20633       bool saved_in_type_id_in_expr_p;
20634
20635       /* We can't be sure yet whether we're looking at a type-id or an
20636          expression.  */
20637       cp_parser_parse_tentatively (parser);
20638       /* Consume the `('.  */
20639       cp_lexer_consume_token (parser->lexer);
20640       /* Parse the type-id.  */
20641       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20642       parser->in_type_id_in_expr_p = true;
20643       type = cp_parser_type_id (parser);
20644       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20645       /* Now, look for the trailing `)'.  */
20646       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20647       /* If all went well, then we're done.  */
20648       if (cp_parser_parse_definitely (parser))
20649         {
20650           cp_decl_specifier_seq decl_specs;
20651
20652           /* Build a trivial decl-specifier-seq.  */
20653           clear_decl_specs (&decl_specs);
20654           decl_specs.type = type;
20655
20656           /* Call grokdeclarator to figure out what type this is.  */
20657           expr = grokdeclarator (NULL,
20658                                  &decl_specs,
20659                                  TYPENAME,
20660                                  /*initialized=*/0,
20661                                  /*attrlist=*/NULL);
20662         }
20663     }
20664
20665   /* If the type-id production did not work out, then we must be
20666      looking at the unary-expression production.  */
20667   if (!expr)
20668     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20669                                        /*cast_p=*/false, NULL);
20670
20671   if (pack_expansion_p)
20672     /* Build a pack expansion. */
20673     expr = make_pack_expansion (expr);
20674
20675   /* Go back to evaluating expressions.  */
20676   --cp_unevaluated_operand;
20677   --c_inhibit_evaluation_warnings;
20678
20679   /* Free the message we created.  */
20680   free (tmp);
20681   /* And restore the old one.  */
20682   parser->type_definition_forbidden_message = saved_message;
20683   parser->integral_constant_expression_p
20684     = saved_integral_constant_expression_p;
20685   parser->non_integral_constant_expression_p
20686     = saved_non_integral_constant_expression_p;
20687
20688   return expr;
20689 }
20690
20691 /* If the current declaration has no declarator, return true.  */
20692
20693 static bool
20694 cp_parser_declares_only_class_p (cp_parser *parser)
20695 {
20696   /* If the next token is a `;' or a `,' then there is no
20697      declarator.  */
20698   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20699           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20700 }
20701
20702 /* Update the DECL_SPECS to reflect the storage class indicated by
20703    KEYWORD.  */
20704
20705 static void
20706 cp_parser_set_storage_class (cp_parser *parser,
20707                              cp_decl_specifier_seq *decl_specs,
20708                              enum rid keyword,
20709                              location_t location)
20710 {
20711   cp_storage_class storage_class;
20712
20713   if (parser->in_unbraced_linkage_specification_p)
20714     {
20715       error_at (location, "invalid use of %qD in linkage specification",
20716                 ridpointers[keyword]);
20717       return;
20718     }
20719   else if (decl_specs->storage_class != sc_none)
20720     {
20721       decl_specs->conflicting_specifiers_p = true;
20722       return;
20723     }
20724
20725   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20726       && decl_specs->specs[(int) ds_thread])
20727     {
20728       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20729       decl_specs->specs[(int) ds_thread] = 0;
20730     }
20731
20732   switch (keyword)
20733     {
20734     case RID_AUTO:
20735       storage_class = sc_auto;
20736       break;
20737     case RID_REGISTER:
20738       storage_class = sc_register;
20739       break;
20740     case RID_STATIC:
20741       storage_class = sc_static;
20742       break;
20743     case RID_EXTERN:
20744       storage_class = sc_extern;
20745       break;
20746     case RID_MUTABLE:
20747       storage_class = sc_mutable;
20748       break;
20749     default:
20750       gcc_unreachable ();
20751     }
20752   decl_specs->storage_class = storage_class;
20753
20754   /* A storage class specifier cannot be applied alongside a typedef 
20755      specifier. If there is a typedef specifier present then set 
20756      conflicting_specifiers_p which will trigger an error later
20757      on in grokdeclarator. */
20758   if (decl_specs->specs[(int)ds_typedef])
20759     decl_specs->conflicting_specifiers_p = true;
20760 }
20761
20762 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20763    is true, the type is a user-defined type; otherwise it is a
20764    built-in type specified by a keyword.  */
20765
20766 static void
20767 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20768                               tree type_spec,
20769                               location_t location,
20770                               bool user_defined_p)
20771 {
20772   decl_specs->any_specifiers_p = true;
20773
20774   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20775      (with, for example, in "typedef int wchar_t;") we remember that
20776      this is what happened.  In system headers, we ignore these
20777      declarations so that G++ can work with system headers that are not
20778      C++-safe.  */
20779   if (decl_specs->specs[(int) ds_typedef]
20780       && !user_defined_p
20781       && (type_spec == boolean_type_node
20782           || type_spec == char16_type_node
20783           || type_spec == char32_type_node
20784           || type_spec == wchar_type_node)
20785       && (decl_specs->type
20786           || decl_specs->specs[(int) ds_long]
20787           || decl_specs->specs[(int) ds_short]
20788           || decl_specs->specs[(int) ds_unsigned]
20789           || decl_specs->specs[(int) ds_signed]))
20790     {
20791       decl_specs->redefined_builtin_type = type_spec;
20792       if (!decl_specs->type)
20793         {
20794           decl_specs->type = type_spec;
20795           decl_specs->user_defined_type_p = false;
20796           decl_specs->type_location = location;
20797         }
20798     }
20799   else if (decl_specs->type)
20800     decl_specs->multiple_types_p = true;
20801   else
20802     {
20803       decl_specs->type = type_spec;
20804       decl_specs->user_defined_type_p = user_defined_p;
20805       decl_specs->redefined_builtin_type = NULL_TREE;
20806       decl_specs->type_location = location;
20807     }
20808 }
20809
20810 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20811    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20812
20813 static bool
20814 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20815 {
20816   return decl_specifiers->specs[(int) ds_friend] != 0;
20817 }
20818
20819 /* Issue an error message indicating that TOKEN_DESC was expected.
20820    If KEYWORD is true, it indicated this function is called by
20821    cp_parser_require_keword and the required token can only be
20822    a indicated keyword. */
20823
20824 static void
20825 cp_parser_required_error (cp_parser *parser,
20826                           required_token token_desc,
20827                           bool keyword)
20828 {
20829   switch (token_desc)
20830     {
20831       case RT_NEW:
20832         cp_parser_error (parser, "expected %<new%>");
20833         return;
20834       case RT_DELETE:
20835         cp_parser_error (parser, "expected %<delete%>");
20836         return;
20837       case RT_RETURN:
20838         cp_parser_error (parser, "expected %<return%>");
20839         return;
20840       case RT_WHILE:
20841         cp_parser_error (parser, "expected %<while%>");
20842         return;
20843       case RT_EXTERN:
20844         cp_parser_error (parser, "expected %<extern%>");
20845         return;
20846       case RT_STATIC_ASSERT:
20847         cp_parser_error (parser, "expected %<static_assert%>");
20848         return;
20849       case RT_DECLTYPE:
20850         cp_parser_error (parser, "expected %<decltype%>");
20851         return;
20852       case RT_OPERATOR:
20853         cp_parser_error (parser, "expected %<operator%>");
20854         return;
20855       case RT_CLASS:
20856         cp_parser_error (parser, "expected %<class%>");
20857         return;
20858       case RT_TEMPLATE:
20859         cp_parser_error (parser, "expected %<template%>");
20860         return;
20861       case RT_NAMESPACE:
20862         cp_parser_error (parser, "expected %<namespace%>");
20863         return;
20864       case RT_USING:
20865         cp_parser_error (parser, "expected %<using%>");
20866         return;
20867       case RT_ASM:
20868         cp_parser_error (parser, "expected %<asm%>");
20869         return;
20870       case RT_TRY:
20871         cp_parser_error (parser, "expected %<try%>");
20872         return;
20873       case RT_CATCH:
20874         cp_parser_error (parser, "expected %<catch%>");
20875         return;
20876       case RT_THROW:
20877         cp_parser_error (parser, "expected %<throw%>");
20878         return;
20879       case RT_LABEL:
20880         cp_parser_error (parser, "expected %<__label__%>");
20881         return;
20882       case RT_AT_TRY:
20883         cp_parser_error (parser, "expected %<@try%>");
20884         return;
20885       case RT_AT_SYNCHRONIZED:
20886         cp_parser_error (parser, "expected %<@synchronized%>");
20887         return;
20888       case RT_AT_THROW:
20889         cp_parser_error (parser, "expected %<@throw%>");
20890         return;
20891       default:
20892         break;
20893     }
20894   if (!keyword)
20895     {
20896       switch (token_desc)
20897         {
20898           case RT_SEMICOLON:
20899             cp_parser_error (parser, "expected %<;%>");
20900             return;
20901           case RT_OPEN_PAREN:
20902             cp_parser_error (parser, "expected %<(%>");
20903             return;
20904           case RT_CLOSE_BRACE:
20905             cp_parser_error (parser, "expected %<}%>");
20906             return;
20907           case RT_OPEN_BRACE:
20908             cp_parser_error (parser, "expected %<{%>");
20909             return;
20910           case RT_CLOSE_SQUARE:
20911             cp_parser_error (parser, "expected %<]%>");
20912             return;
20913           case RT_OPEN_SQUARE:
20914             cp_parser_error (parser, "expected %<[%>");
20915             return;
20916           case RT_COMMA:
20917             cp_parser_error (parser, "expected %<,%>");
20918             return;
20919           case RT_SCOPE:
20920             cp_parser_error (parser, "expected %<::%>");
20921             return;
20922           case RT_LESS:
20923             cp_parser_error (parser, "expected %<<%>");
20924             return;
20925           case RT_GREATER:
20926             cp_parser_error (parser, "expected %<>%>");
20927             return;
20928           case RT_EQ:
20929             cp_parser_error (parser, "expected %<=%>");
20930             return;
20931           case RT_ELLIPSIS:
20932             cp_parser_error (parser, "expected %<...%>");
20933             return;
20934           case RT_MULT:
20935             cp_parser_error (parser, "expected %<*%>");
20936             return;
20937           case RT_COMPL:
20938             cp_parser_error (parser, "expected %<~%>");
20939             return;
20940           case RT_COLON:
20941             cp_parser_error (parser, "expected %<:%>");
20942             return;
20943           case RT_COLON_SCOPE:
20944             cp_parser_error (parser, "expected %<:%> or %<::%>");
20945             return;
20946           case RT_CLOSE_PAREN:
20947             cp_parser_error (parser, "expected %<)%>");
20948             return;
20949           case RT_COMMA_CLOSE_PAREN:
20950             cp_parser_error (parser, "expected %<,%> or %<)%>");
20951             return;
20952           case RT_PRAGMA_EOL:
20953             cp_parser_error (parser, "expected end of line");
20954             return;
20955           case RT_NAME:
20956             cp_parser_error (parser, "expected identifier");
20957             return;
20958           case RT_SELECT:
20959             cp_parser_error (parser, "expected selection-statement");
20960             return;
20961           case RT_INTERATION:
20962             cp_parser_error (parser, "expected iteration-statement");
20963             return;
20964           case RT_JUMP:
20965             cp_parser_error (parser, "expected jump-statement");
20966             return;
20967           case RT_CLASS_KEY:
20968             cp_parser_error (parser, "expected class-key");
20969             return;
20970           case RT_CLASS_TYPENAME_TEMPLATE:
20971             cp_parser_error (parser,
20972                  "expected %<class%>, %<typename%>, or %<template%>");
20973             return;
20974           default:
20975             gcc_unreachable ();
20976         }
20977     }
20978   else
20979     gcc_unreachable ();
20980 }
20981
20982
20983
20984 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20985    issue an error message indicating that TOKEN_DESC was expected.
20986
20987    Returns the token consumed, if the token had the appropriate type.
20988    Otherwise, returns NULL.  */
20989
20990 static cp_token *
20991 cp_parser_require (cp_parser* parser,
20992                    enum cpp_ttype type,
20993                    required_token token_desc)
20994 {
20995   if (cp_lexer_next_token_is (parser->lexer, type))
20996     return cp_lexer_consume_token (parser->lexer);
20997   else
20998     {
20999       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21000       if (!cp_parser_simulate_error (parser))
21001         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21002       return NULL;
21003     }
21004 }
21005
21006 /* An error message is produced if the next token is not '>'.
21007    All further tokens are skipped until the desired token is
21008    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21009
21010 static void
21011 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21012 {
21013   /* Current level of '< ... >'.  */
21014   unsigned level = 0;
21015   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21016   unsigned nesting_depth = 0;
21017
21018   /* Are we ready, yet?  If not, issue error message.  */
21019   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21020     return;
21021
21022   /* Skip tokens until the desired token is found.  */
21023   while (true)
21024     {
21025       /* Peek at the next token.  */
21026       switch (cp_lexer_peek_token (parser->lexer)->type)
21027         {
21028         case CPP_LESS:
21029           if (!nesting_depth)
21030             ++level;
21031           break;
21032
21033         case CPP_RSHIFT:
21034           if (cxx_dialect == cxx98)
21035             /* C++0x views the `>>' operator as two `>' tokens, but
21036                C++98 does not. */
21037             break;
21038           else if (!nesting_depth && level-- == 0)
21039             {
21040               /* We've hit a `>>' where the first `>' closes the
21041                  template argument list, and the second `>' is
21042                  spurious.  Just consume the `>>' and stop; we've
21043                  already produced at least one error.  */
21044               cp_lexer_consume_token (parser->lexer);
21045               return;
21046             }
21047           /* Fall through for C++0x, so we handle the second `>' in
21048              the `>>'.  */
21049
21050         case CPP_GREATER:
21051           if (!nesting_depth && level-- == 0)
21052             {
21053               /* We've reached the token we want, consume it and stop.  */
21054               cp_lexer_consume_token (parser->lexer);
21055               return;
21056             }
21057           break;
21058
21059         case CPP_OPEN_PAREN:
21060         case CPP_OPEN_SQUARE:
21061           ++nesting_depth;
21062           break;
21063
21064         case CPP_CLOSE_PAREN:
21065         case CPP_CLOSE_SQUARE:
21066           if (nesting_depth-- == 0)
21067             return;
21068           break;
21069
21070         case CPP_EOF:
21071         case CPP_PRAGMA_EOL:
21072         case CPP_SEMICOLON:
21073         case CPP_OPEN_BRACE:
21074         case CPP_CLOSE_BRACE:
21075           /* The '>' was probably forgotten, don't look further.  */
21076           return;
21077
21078         default:
21079           break;
21080         }
21081
21082       /* Consume this token.  */
21083       cp_lexer_consume_token (parser->lexer);
21084     }
21085 }
21086
21087 /* If the next token is the indicated keyword, consume it.  Otherwise,
21088    issue an error message indicating that TOKEN_DESC was expected.
21089
21090    Returns the token consumed, if the token had the appropriate type.
21091    Otherwise, returns NULL.  */
21092
21093 static cp_token *
21094 cp_parser_require_keyword (cp_parser* parser,
21095                            enum rid keyword,
21096                            required_token token_desc)
21097 {
21098   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21099
21100   if (token && token->keyword != keyword)
21101     {
21102       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21103       return NULL;
21104     }
21105
21106   return token;
21107 }
21108
21109 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21110    function-definition.  */
21111
21112 static bool
21113 cp_parser_token_starts_function_definition_p (cp_token* token)
21114 {
21115   return (/* An ordinary function-body begins with an `{'.  */
21116           token->type == CPP_OPEN_BRACE
21117           /* A ctor-initializer begins with a `:'.  */
21118           || token->type == CPP_COLON
21119           /* A function-try-block begins with `try'.  */
21120           || token->keyword == RID_TRY
21121           /* The named return value extension begins with `return'.  */
21122           || token->keyword == RID_RETURN);
21123 }
21124
21125 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21126    definition.  */
21127
21128 static bool
21129 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21130 {
21131   cp_token *token;
21132
21133   token = cp_lexer_peek_token (parser->lexer);
21134   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21135 }
21136
21137 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21138    C++0x) ending a template-argument.  */
21139
21140 static bool
21141 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21142 {
21143   cp_token *token;
21144
21145   token = cp_lexer_peek_token (parser->lexer);
21146   return (token->type == CPP_COMMA 
21147           || token->type == CPP_GREATER
21148           || token->type == CPP_ELLIPSIS
21149           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21150 }
21151
21152 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21153    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21154
21155 static bool
21156 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21157                                                      size_t n)
21158 {
21159   cp_token *token;
21160
21161   token = cp_lexer_peek_nth_token (parser->lexer, n);
21162   if (token->type == CPP_LESS)
21163     return true;
21164   /* Check for the sequence `<::' in the original code. It would be lexed as
21165      `[:', where `[' is a digraph, and there is no whitespace before
21166      `:'.  */
21167   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21168     {
21169       cp_token *token2;
21170       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21171       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21172         return true;
21173     }
21174   return false;
21175 }
21176
21177 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21178    or none_type otherwise.  */
21179
21180 static enum tag_types
21181 cp_parser_token_is_class_key (cp_token* token)
21182 {
21183   switch (token->keyword)
21184     {
21185     case RID_CLASS:
21186       return class_type;
21187     case RID_STRUCT:
21188       return record_type;
21189     case RID_UNION:
21190       return union_type;
21191
21192     default:
21193       return none_type;
21194     }
21195 }
21196
21197 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21198
21199 static void
21200 cp_parser_check_class_key (enum tag_types class_key, tree type)
21201 {
21202   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21203     permerror (input_location, "%qs tag used in naming %q#T",
21204             class_key == union_type ? "union"
21205              : class_key == record_type ? "struct" : "class",
21206              type);
21207 }
21208
21209 /* Issue an error message if DECL is redeclared with different
21210    access than its original declaration [class.access.spec/3].
21211    This applies to nested classes and nested class templates.
21212    [class.mem/1].  */
21213
21214 static void
21215 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21216 {
21217   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21218     return;
21219
21220   if ((TREE_PRIVATE (decl)
21221        != (current_access_specifier == access_private_node))
21222       || (TREE_PROTECTED (decl)
21223           != (current_access_specifier == access_protected_node)))
21224     error_at (location, "%qD redeclared with different access", decl);
21225 }
21226
21227 /* Look for the `template' keyword, as a syntactic disambiguator.
21228    Return TRUE iff it is present, in which case it will be
21229    consumed.  */
21230
21231 static bool
21232 cp_parser_optional_template_keyword (cp_parser *parser)
21233 {
21234   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21235     {
21236       /* The `template' keyword can only be used within templates;
21237          outside templates the parser can always figure out what is a
21238          template and what is not.  */
21239       if (!processing_template_decl)
21240         {
21241           cp_token *token = cp_lexer_peek_token (parser->lexer);
21242           error_at (token->location,
21243                     "%<template%> (as a disambiguator) is only allowed "
21244                     "within templates");
21245           /* If this part of the token stream is rescanned, the same
21246              error message would be generated.  So, we purge the token
21247              from the stream.  */
21248           cp_lexer_purge_token (parser->lexer);
21249           return false;
21250         }
21251       else
21252         {
21253           /* Consume the `template' keyword.  */
21254           cp_lexer_consume_token (parser->lexer);
21255           return true;
21256         }
21257     }
21258
21259   return false;
21260 }
21261
21262 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21263    set PARSER->SCOPE, and perform other related actions.  */
21264
21265 static void
21266 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21267 {
21268   int i;
21269   struct tree_check *check_value;
21270   deferred_access_check *chk;
21271   VEC (deferred_access_check,gc) *checks;
21272
21273   /* Get the stored value.  */
21274   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21275   /* Perform any access checks that were deferred.  */
21276   checks = check_value->checks;
21277   if (checks)
21278     {
21279       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21280         perform_or_defer_access_check (chk->binfo,
21281                                        chk->decl,
21282                                        chk->diag_decl);
21283     }
21284   /* Set the scope from the stored value.  */
21285   parser->scope = check_value->value;
21286   parser->qualifying_scope = check_value->qualifying_scope;
21287   parser->object_scope = NULL_TREE;
21288 }
21289
21290 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21291    encounter the end of a block before what we were looking for.  */
21292
21293 static bool
21294 cp_parser_cache_group (cp_parser *parser,
21295                        enum cpp_ttype end,
21296                        unsigned depth)
21297 {
21298   while (true)
21299     {
21300       cp_token *token = cp_lexer_peek_token (parser->lexer);
21301
21302       /* Abort a parenthesized expression if we encounter a semicolon.  */
21303       if ((end == CPP_CLOSE_PAREN || depth == 0)
21304           && token->type == CPP_SEMICOLON)
21305         return true;
21306       /* If we've reached the end of the file, stop.  */
21307       if (token->type == CPP_EOF
21308           || (end != CPP_PRAGMA_EOL
21309               && token->type == CPP_PRAGMA_EOL))
21310         return true;
21311       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21312         /* We've hit the end of an enclosing block, so there's been some
21313            kind of syntax error.  */
21314         return true;
21315
21316       /* Consume the token.  */
21317       cp_lexer_consume_token (parser->lexer);
21318       /* See if it starts a new group.  */
21319       if (token->type == CPP_OPEN_BRACE)
21320         {
21321           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21322           /* In theory this should probably check end == '}', but
21323              cp_parser_save_member_function_body needs it to exit
21324              after either '}' or ')' when called with ')'.  */
21325           if (depth == 0)
21326             return false;
21327         }
21328       else if (token->type == CPP_OPEN_PAREN)
21329         {
21330           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21331           if (depth == 0 && end == CPP_CLOSE_PAREN)
21332             return false;
21333         }
21334       else if (token->type == CPP_PRAGMA)
21335         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21336       else if (token->type == end)
21337         return false;
21338     }
21339 }
21340
21341 /* Begin parsing tentatively.  We always save tokens while parsing
21342    tentatively so that if the tentative parsing fails we can restore the
21343    tokens.  */
21344
21345 static void
21346 cp_parser_parse_tentatively (cp_parser* parser)
21347 {
21348   /* Enter a new parsing context.  */
21349   parser->context = cp_parser_context_new (parser->context);
21350   /* Begin saving tokens.  */
21351   cp_lexer_save_tokens (parser->lexer);
21352   /* In order to avoid repetitive access control error messages,
21353      access checks are queued up until we are no longer parsing
21354      tentatively.  */
21355   push_deferring_access_checks (dk_deferred);
21356 }
21357
21358 /* Commit to the currently active tentative parse.  */
21359
21360 static void
21361 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21362 {
21363   cp_parser_context *context;
21364   cp_lexer *lexer;
21365
21366   /* Mark all of the levels as committed.  */
21367   lexer = parser->lexer;
21368   for (context = parser->context; context->next; context = context->next)
21369     {
21370       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21371         break;
21372       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21373       while (!cp_lexer_saving_tokens (lexer))
21374         lexer = lexer->next;
21375       cp_lexer_commit_tokens (lexer);
21376     }
21377 }
21378
21379 /* Abort the currently active tentative parse.  All consumed tokens
21380    will be rolled back, and no diagnostics will be issued.  */
21381
21382 static void
21383 cp_parser_abort_tentative_parse (cp_parser* parser)
21384 {
21385   cp_parser_simulate_error (parser);
21386   /* Now, pretend that we want to see if the construct was
21387      successfully parsed.  */
21388   cp_parser_parse_definitely (parser);
21389 }
21390
21391 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21392    token stream.  Otherwise, commit to the tokens we have consumed.
21393    Returns true if no error occurred; false otherwise.  */
21394
21395 static bool
21396 cp_parser_parse_definitely (cp_parser* parser)
21397 {
21398   bool error_occurred;
21399   cp_parser_context *context;
21400
21401   /* Remember whether or not an error occurred, since we are about to
21402      destroy that information.  */
21403   error_occurred = cp_parser_error_occurred (parser);
21404   /* Remove the topmost context from the stack.  */
21405   context = parser->context;
21406   parser->context = context->next;
21407   /* If no parse errors occurred, commit to the tentative parse.  */
21408   if (!error_occurred)
21409     {
21410       /* Commit to the tokens read tentatively, unless that was
21411          already done.  */
21412       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21413         cp_lexer_commit_tokens (parser->lexer);
21414
21415       pop_to_parent_deferring_access_checks ();
21416     }
21417   /* Otherwise, if errors occurred, roll back our state so that things
21418      are just as they were before we began the tentative parse.  */
21419   else
21420     {
21421       cp_lexer_rollback_tokens (parser->lexer);
21422       pop_deferring_access_checks ();
21423     }
21424   /* Add the context to the front of the free list.  */
21425   context->next = cp_parser_context_free_list;
21426   cp_parser_context_free_list = context;
21427
21428   return !error_occurred;
21429 }
21430
21431 /* Returns true if we are parsing tentatively and are not committed to
21432    this tentative parse.  */
21433
21434 static bool
21435 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21436 {
21437   return (cp_parser_parsing_tentatively (parser)
21438           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21439 }
21440
21441 /* Returns nonzero iff an error has occurred during the most recent
21442    tentative parse.  */
21443
21444 static bool
21445 cp_parser_error_occurred (cp_parser* parser)
21446 {
21447   return (cp_parser_parsing_tentatively (parser)
21448           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21449 }
21450
21451 /* Returns nonzero if GNU extensions are allowed.  */
21452
21453 static bool
21454 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21455 {
21456   return parser->allow_gnu_extensions_p;
21457 }
21458 \f
21459 /* Objective-C++ Productions */
21460
21461
21462 /* Parse an Objective-C expression, which feeds into a primary-expression
21463    above.
21464
21465    objc-expression:
21466      objc-message-expression
21467      objc-string-literal
21468      objc-encode-expression
21469      objc-protocol-expression
21470      objc-selector-expression
21471
21472   Returns a tree representation of the expression.  */
21473
21474 static tree
21475 cp_parser_objc_expression (cp_parser* parser)
21476 {
21477   /* Try to figure out what kind of declaration is present.  */
21478   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21479
21480   switch (kwd->type)
21481     {
21482     case CPP_OPEN_SQUARE:
21483       return cp_parser_objc_message_expression (parser);
21484
21485     case CPP_OBJC_STRING:
21486       kwd = cp_lexer_consume_token (parser->lexer);
21487       return objc_build_string_object (kwd->u.value);
21488
21489     case CPP_KEYWORD:
21490       switch (kwd->keyword)
21491         {
21492         case RID_AT_ENCODE:
21493           return cp_parser_objc_encode_expression (parser);
21494
21495         case RID_AT_PROTOCOL:
21496           return cp_parser_objc_protocol_expression (parser);
21497
21498         case RID_AT_SELECTOR:
21499           return cp_parser_objc_selector_expression (parser);
21500
21501         default:
21502           break;
21503         }
21504     default:
21505       error_at (kwd->location,
21506                 "misplaced %<@%D%> Objective-C++ construct",
21507                 kwd->u.value);
21508       cp_parser_skip_to_end_of_block_or_statement (parser);
21509     }
21510
21511   return error_mark_node;
21512 }
21513
21514 /* Parse an Objective-C message expression.
21515
21516    objc-message-expression:
21517      [ objc-message-receiver objc-message-args ]
21518
21519    Returns a representation of an Objective-C message.  */
21520
21521 static tree
21522 cp_parser_objc_message_expression (cp_parser* parser)
21523 {
21524   tree receiver, messageargs;
21525
21526   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21527   receiver = cp_parser_objc_message_receiver (parser);
21528   messageargs = cp_parser_objc_message_args (parser);
21529   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21530
21531   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21532 }
21533
21534 /* Parse an objc-message-receiver.
21535
21536    objc-message-receiver:
21537      expression
21538      simple-type-specifier
21539
21540   Returns a representation of the type or expression.  */
21541
21542 static tree
21543 cp_parser_objc_message_receiver (cp_parser* parser)
21544 {
21545   tree rcv;
21546
21547   /* An Objective-C message receiver may be either (1) a type
21548      or (2) an expression.  */
21549   cp_parser_parse_tentatively (parser);
21550   rcv = cp_parser_expression (parser, false, NULL);
21551
21552   if (cp_parser_parse_definitely (parser))
21553     return rcv;
21554
21555   rcv = cp_parser_simple_type_specifier (parser,
21556                                          /*decl_specs=*/NULL,
21557                                          CP_PARSER_FLAGS_NONE);
21558
21559   return objc_get_class_reference (rcv);
21560 }
21561
21562 /* Parse the arguments and selectors comprising an Objective-C message.
21563
21564    objc-message-args:
21565      objc-selector
21566      objc-selector-args
21567      objc-selector-args , objc-comma-args
21568
21569    objc-selector-args:
21570      objc-selector [opt] : assignment-expression
21571      objc-selector-args objc-selector [opt] : assignment-expression
21572
21573    objc-comma-args:
21574      assignment-expression
21575      objc-comma-args , assignment-expression
21576
21577    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21578    selector arguments and TREE_VALUE containing a list of comma
21579    arguments.  */
21580
21581 static tree
21582 cp_parser_objc_message_args (cp_parser* parser)
21583 {
21584   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21585   bool maybe_unary_selector_p = true;
21586   cp_token *token = cp_lexer_peek_token (parser->lexer);
21587
21588   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21589     {
21590       tree selector = NULL_TREE, arg;
21591
21592       if (token->type != CPP_COLON)
21593         selector = cp_parser_objc_selector (parser);
21594
21595       /* Detect if we have a unary selector.  */
21596       if (maybe_unary_selector_p
21597           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21598         return build_tree_list (selector, NULL_TREE);
21599
21600       maybe_unary_selector_p = false;
21601       cp_parser_require (parser, CPP_COLON, RT_COLON);
21602       arg = cp_parser_assignment_expression (parser, false, NULL);
21603
21604       sel_args
21605         = chainon (sel_args,
21606                    build_tree_list (selector, arg));
21607
21608       token = cp_lexer_peek_token (parser->lexer);
21609     }
21610
21611   /* Handle non-selector arguments, if any. */
21612   while (token->type == CPP_COMMA)
21613     {
21614       tree arg;
21615
21616       cp_lexer_consume_token (parser->lexer);
21617       arg = cp_parser_assignment_expression (parser, false, NULL);
21618
21619       addl_args
21620         = chainon (addl_args,
21621                    build_tree_list (NULL_TREE, arg));
21622
21623       token = cp_lexer_peek_token (parser->lexer);
21624     }
21625
21626   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21627     {
21628       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21629       return build_tree_list (error_mark_node, error_mark_node);
21630     }
21631
21632   return build_tree_list (sel_args, addl_args);
21633 }
21634
21635 /* Parse an Objective-C encode expression.
21636
21637    objc-encode-expression:
21638      @encode objc-typename
21639
21640    Returns an encoded representation of the type argument.  */
21641
21642 static tree
21643 cp_parser_objc_encode_expression (cp_parser* parser)
21644 {
21645   tree type;
21646   cp_token *token;
21647
21648   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21649   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21650   token = cp_lexer_peek_token (parser->lexer);
21651   type = complete_type (cp_parser_type_id (parser));
21652   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21653
21654   if (!type)
21655     {
21656       error_at (token->location, 
21657                 "%<@encode%> must specify a type as an argument");
21658       return error_mark_node;
21659     }
21660
21661   /* This happens if we find @encode(T) (where T is a template
21662      typename or something dependent on a template typename) when
21663      parsing a template.  In that case, we can't compile it
21664      immediately, but we rather create an AT_ENCODE_EXPR which will
21665      need to be instantiated when the template is used.
21666   */
21667   if (dependent_type_p (type))
21668     {
21669       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21670       TREE_READONLY (value) = 1;
21671       return value;
21672     }
21673
21674   return objc_build_encode_expr (type);
21675 }
21676
21677 /* Parse an Objective-C @defs expression.  */
21678
21679 static tree
21680 cp_parser_objc_defs_expression (cp_parser *parser)
21681 {
21682   tree name;
21683
21684   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21685   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21686   name = cp_parser_identifier (parser);
21687   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21688
21689   return objc_get_class_ivars (name);
21690 }
21691
21692 /* Parse an Objective-C protocol expression.
21693
21694   objc-protocol-expression:
21695     @protocol ( identifier )
21696
21697   Returns a representation of the protocol expression.  */
21698
21699 static tree
21700 cp_parser_objc_protocol_expression (cp_parser* parser)
21701 {
21702   tree proto;
21703
21704   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21705   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21706   proto = cp_parser_identifier (parser);
21707   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21708
21709   return objc_build_protocol_expr (proto);
21710 }
21711
21712 /* Parse an Objective-C selector expression.
21713
21714    objc-selector-expression:
21715      @selector ( objc-method-signature )
21716
21717    objc-method-signature:
21718      objc-selector
21719      objc-selector-seq
21720
21721    objc-selector-seq:
21722      objc-selector :
21723      objc-selector-seq objc-selector :
21724
21725   Returns a representation of the method selector.  */
21726
21727 static tree
21728 cp_parser_objc_selector_expression (cp_parser* parser)
21729 {
21730   tree sel_seq = NULL_TREE;
21731   bool maybe_unary_selector_p = true;
21732   cp_token *token;
21733   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21734
21735   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21736   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21737   token = cp_lexer_peek_token (parser->lexer);
21738
21739   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21740          || token->type == CPP_SCOPE)
21741     {
21742       tree selector = NULL_TREE;
21743
21744       if (token->type != CPP_COLON
21745           || token->type == CPP_SCOPE)
21746         selector = cp_parser_objc_selector (parser);
21747
21748       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21749           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21750         {
21751           /* Detect if we have a unary selector.  */
21752           if (maybe_unary_selector_p)
21753             {
21754               sel_seq = selector;
21755               goto finish_selector;
21756             }
21757           else
21758             {
21759               cp_parser_error (parser, "expected %<:%>");
21760             }
21761         }
21762       maybe_unary_selector_p = false;
21763       token = cp_lexer_consume_token (parser->lexer);
21764
21765       if (token->type == CPP_SCOPE)
21766         {
21767           sel_seq
21768             = chainon (sel_seq,
21769                        build_tree_list (selector, NULL_TREE));
21770           sel_seq
21771             = chainon (sel_seq,
21772                        build_tree_list (NULL_TREE, NULL_TREE));
21773         }
21774       else
21775         sel_seq
21776           = chainon (sel_seq,
21777                      build_tree_list (selector, NULL_TREE));
21778
21779       token = cp_lexer_peek_token (parser->lexer);
21780     }
21781
21782  finish_selector:
21783   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21784
21785   return objc_build_selector_expr (loc, sel_seq);
21786 }
21787
21788 /* Parse a list of identifiers.
21789
21790    objc-identifier-list:
21791      identifier
21792      objc-identifier-list , identifier
21793
21794    Returns a TREE_LIST of identifier nodes.  */
21795
21796 static tree
21797 cp_parser_objc_identifier_list (cp_parser* parser)
21798 {
21799   tree identifier;
21800   tree list;
21801   cp_token *sep;
21802
21803   identifier = cp_parser_identifier (parser);
21804   if (identifier == error_mark_node)
21805     return error_mark_node;      
21806
21807   list = build_tree_list (NULL_TREE, identifier);
21808   sep = cp_lexer_peek_token (parser->lexer);
21809
21810   while (sep->type == CPP_COMMA)
21811     {
21812       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21813       identifier = cp_parser_identifier (parser);
21814       if (identifier == error_mark_node)
21815         return list;
21816
21817       list = chainon (list, build_tree_list (NULL_TREE,
21818                                              identifier));
21819       sep = cp_lexer_peek_token (parser->lexer);
21820     }
21821   
21822   return list;
21823 }
21824
21825 /* Parse an Objective-C alias declaration.
21826
21827    objc-alias-declaration:
21828      @compatibility_alias identifier identifier ;
21829
21830    This function registers the alias mapping with the Objective-C front end.
21831    It returns nothing.  */
21832
21833 static void
21834 cp_parser_objc_alias_declaration (cp_parser* parser)
21835 {
21836   tree alias, orig;
21837
21838   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21839   alias = cp_parser_identifier (parser);
21840   orig = cp_parser_identifier (parser);
21841   objc_declare_alias (alias, orig);
21842   cp_parser_consume_semicolon_at_end_of_statement (parser);
21843 }
21844
21845 /* Parse an Objective-C class forward-declaration.
21846
21847    objc-class-declaration:
21848      @class objc-identifier-list ;
21849
21850    The function registers the forward declarations with the Objective-C
21851    front end.  It returns nothing.  */
21852
21853 static void
21854 cp_parser_objc_class_declaration (cp_parser* parser)
21855 {
21856   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21857   objc_declare_class (cp_parser_objc_identifier_list (parser));
21858   cp_parser_consume_semicolon_at_end_of_statement (parser);
21859 }
21860
21861 /* Parse a list of Objective-C protocol references.
21862
21863    objc-protocol-refs-opt:
21864      objc-protocol-refs [opt]
21865
21866    objc-protocol-refs:
21867      < objc-identifier-list >
21868
21869    Returns a TREE_LIST of identifiers, if any.  */
21870
21871 static tree
21872 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21873 {
21874   tree protorefs = NULL_TREE;
21875
21876   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21877     {
21878       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21879       protorefs = cp_parser_objc_identifier_list (parser);
21880       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21881     }
21882
21883   return protorefs;
21884 }
21885
21886 /* Parse a Objective-C visibility specification.  */
21887
21888 static void
21889 cp_parser_objc_visibility_spec (cp_parser* parser)
21890 {
21891   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21892
21893   switch (vis->keyword)
21894     {
21895     case RID_AT_PRIVATE:
21896       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21897       break;
21898     case RID_AT_PROTECTED:
21899       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21900       break;
21901     case RID_AT_PUBLIC:
21902       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21903       break;
21904     case RID_AT_PACKAGE:
21905       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21906       break;
21907     default:
21908       return;
21909     }
21910
21911   /* Eat '@private'/'@protected'/'@public'.  */
21912   cp_lexer_consume_token (parser->lexer);
21913 }
21914
21915 /* Parse an Objective-C method type.  Return 'true' if it is a class
21916    (+) method, and 'false' if it is an instance (-) method.  */
21917
21918 static inline bool
21919 cp_parser_objc_method_type (cp_parser* parser)
21920 {
21921   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21922     return true;
21923   else
21924     return false;
21925 }
21926
21927 /* Parse an Objective-C protocol qualifier.  */
21928
21929 static tree
21930 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21931 {
21932   tree quals = NULL_TREE, node;
21933   cp_token *token = cp_lexer_peek_token (parser->lexer);
21934
21935   node = token->u.value;
21936
21937   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21938          && (node == ridpointers [(int) RID_IN]
21939              || node == ridpointers [(int) RID_OUT]
21940              || node == ridpointers [(int) RID_INOUT]
21941              || node == ridpointers [(int) RID_BYCOPY]
21942              || node == ridpointers [(int) RID_BYREF]
21943              || node == ridpointers [(int) RID_ONEWAY]))
21944     {
21945       quals = tree_cons (NULL_TREE, node, quals);
21946       cp_lexer_consume_token (parser->lexer);
21947       token = cp_lexer_peek_token (parser->lexer);
21948       node = token->u.value;
21949     }
21950
21951   return quals;
21952 }
21953
21954 /* Parse an Objective-C typename.  */
21955
21956 static tree
21957 cp_parser_objc_typename (cp_parser* parser)
21958 {
21959   tree type_name = NULL_TREE;
21960
21961   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21962     {
21963       tree proto_quals, cp_type = NULL_TREE;
21964
21965       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21966       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21967
21968       /* An ObjC type name may consist of just protocol qualifiers, in which
21969          case the type shall default to 'id'.  */
21970       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21971         {
21972           cp_type = cp_parser_type_id (parser);
21973           
21974           /* If the type could not be parsed, an error has already
21975              been produced.  For error recovery, behave as if it had
21976              not been specified, which will use the default type
21977              'id'.  */
21978           if (cp_type == error_mark_node)
21979             {
21980               cp_type = NULL_TREE;
21981               /* We need to skip to the closing parenthesis as
21982                  cp_parser_type_id() does not seem to do it for
21983                  us.  */
21984               cp_parser_skip_to_closing_parenthesis (parser,
21985                                                      /*recovering=*/true,
21986                                                      /*or_comma=*/false,
21987                                                      /*consume_paren=*/false);
21988             }
21989         }
21990
21991       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21992       type_name = build_tree_list (proto_quals, cp_type);
21993     }
21994
21995   return type_name;
21996 }
21997
21998 /* Check to see if TYPE refers to an Objective-C selector name.  */
21999
22000 static bool
22001 cp_parser_objc_selector_p (enum cpp_ttype type)
22002 {
22003   return (type == CPP_NAME || type == CPP_KEYWORD
22004           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22005           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22006           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22007           || type == CPP_XOR || type == CPP_XOR_EQ);
22008 }
22009
22010 /* Parse an Objective-C selector.  */
22011
22012 static tree
22013 cp_parser_objc_selector (cp_parser* parser)
22014 {
22015   cp_token *token = cp_lexer_consume_token (parser->lexer);
22016
22017   if (!cp_parser_objc_selector_p (token->type))
22018     {
22019       error_at (token->location, "invalid Objective-C++ selector name");
22020       return error_mark_node;
22021     }
22022
22023   /* C++ operator names are allowed to appear in ObjC selectors.  */
22024   switch (token->type)
22025     {
22026     case CPP_AND_AND: return get_identifier ("and");
22027     case CPP_AND_EQ: return get_identifier ("and_eq");
22028     case CPP_AND: return get_identifier ("bitand");
22029     case CPP_OR: return get_identifier ("bitor");
22030     case CPP_COMPL: return get_identifier ("compl");
22031     case CPP_NOT: return get_identifier ("not");
22032     case CPP_NOT_EQ: return get_identifier ("not_eq");
22033     case CPP_OR_OR: return get_identifier ("or");
22034     case CPP_OR_EQ: return get_identifier ("or_eq");
22035     case CPP_XOR: return get_identifier ("xor");
22036     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22037     default: return token->u.value;
22038     }
22039 }
22040
22041 /* Parse an Objective-C params list.  */
22042
22043 static tree
22044 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22045 {
22046   tree params = NULL_TREE;
22047   bool maybe_unary_selector_p = true;
22048   cp_token *token = cp_lexer_peek_token (parser->lexer);
22049
22050   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22051     {
22052       tree selector = NULL_TREE, type_name, identifier;
22053       tree parm_attr = NULL_TREE;
22054
22055       if (token->keyword == RID_ATTRIBUTE)
22056         break;
22057
22058       if (token->type != CPP_COLON)
22059         selector = cp_parser_objc_selector (parser);
22060
22061       /* Detect if we have a unary selector.  */
22062       if (maybe_unary_selector_p
22063           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22064         {
22065           params = selector; /* Might be followed by attributes.  */
22066           break;
22067         }
22068
22069       maybe_unary_selector_p = false;
22070       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22071         {
22072           /* Something went quite wrong.  There should be a colon
22073              here, but there is not.  Stop parsing parameters.  */
22074           break;
22075         }
22076       type_name = cp_parser_objc_typename (parser);
22077       /* New ObjC allows attributes on parameters too.  */
22078       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22079         parm_attr = cp_parser_attributes_opt (parser);
22080       identifier = cp_parser_identifier (parser);
22081
22082       params
22083         = chainon (params,
22084                    objc_build_keyword_decl (selector,
22085                                             type_name,
22086                                             identifier,
22087                                             parm_attr));
22088
22089       token = cp_lexer_peek_token (parser->lexer);
22090     }
22091
22092   if (params == NULL_TREE)
22093     {
22094       cp_parser_error (parser, "objective-c++ method declaration is expected");
22095       return error_mark_node;
22096     }
22097
22098   /* We allow tail attributes for the method.  */
22099   if (token->keyword == RID_ATTRIBUTE)
22100     {
22101       *attributes = cp_parser_attributes_opt (parser);
22102       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22103           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22104         return params;
22105       cp_parser_error (parser, 
22106                        "method attributes must be specified at the end");
22107       return error_mark_node;
22108     }
22109
22110   if (params == NULL_TREE)
22111     {
22112       cp_parser_error (parser, "objective-c++ method declaration is expected");
22113       return error_mark_node;
22114     }
22115   return params;
22116 }
22117
22118 /* Parse the non-keyword Objective-C params.  */
22119
22120 static tree
22121 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22122                                        tree* attributes)
22123 {
22124   tree params = make_node (TREE_LIST);
22125   cp_token *token = cp_lexer_peek_token (parser->lexer);
22126   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22127
22128   while (token->type == CPP_COMMA)
22129     {
22130       cp_parameter_declarator *parmdecl;
22131       tree parm;
22132
22133       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22134       token = cp_lexer_peek_token (parser->lexer);
22135
22136       if (token->type == CPP_ELLIPSIS)
22137         {
22138           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22139           *ellipsisp = true;
22140           token = cp_lexer_peek_token (parser->lexer);
22141           break;
22142         }
22143
22144       /* TODO: parse attributes for tail parameters.  */
22145       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22146       parm = grokdeclarator (parmdecl->declarator,
22147                              &parmdecl->decl_specifiers,
22148                              PARM, /*initialized=*/0,
22149                              /*attrlist=*/NULL);
22150
22151       chainon (params, build_tree_list (NULL_TREE, parm));
22152       token = cp_lexer_peek_token (parser->lexer);
22153     }
22154
22155   /* We allow tail attributes for the method.  */
22156   if (token->keyword == RID_ATTRIBUTE)
22157     {
22158       if (*attributes == NULL_TREE)
22159         {
22160           *attributes = cp_parser_attributes_opt (parser);
22161           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22162               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22163             return params;
22164         }
22165       else        
22166         /* We have an error, but parse the attributes, so that we can 
22167            carry on.  */
22168         *attributes = cp_parser_attributes_opt (parser);
22169
22170       cp_parser_error (parser, 
22171                        "method attributes must be specified at the end");
22172       return error_mark_node;
22173     }
22174
22175   return params;
22176 }
22177
22178 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22179
22180 static void
22181 cp_parser_objc_interstitial_code (cp_parser* parser)
22182 {
22183   cp_token *token = cp_lexer_peek_token (parser->lexer);
22184
22185   /* If the next token is `extern' and the following token is a string
22186      literal, then we have a linkage specification.  */
22187   if (token->keyword == RID_EXTERN
22188       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22189     cp_parser_linkage_specification (parser);
22190   /* Handle #pragma, if any.  */
22191   else if (token->type == CPP_PRAGMA)
22192     cp_parser_pragma (parser, pragma_external);
22193   /* Allow stray semicolons.  */
22194   else if (token->type == CPP_SEMICOLON)
22195     cp_lexer_consume_token (parser->lexer);
22196   /* Mark methods as optional or required, when building protocols.  */
22197   else if (token->keyword == RID_AT_OPTIONAL)
22198     {
22199       cp_lexer_consume_token (parser->lexer);
22200       objc_set_method_opt (true);
22201     }
22202   else if (token->keyword == RID_AT_REQUIRED)
22203     {
22204       cp_lexer_consume_token (parser->lexer);
22205       objc_set_method_opt (false);
22206     }
22207   else if (token->keyword == RID_NAMESPACE)
22208     cp_parser_namespace_definition (parser);
22209   /* Other stray characters must generate errors.  */
22210   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22211     {
22212       cp_lexer_consume_token (parser->lexer);
22213       error ("stray %qs between Objective-C++ methods",
22214              token->type == CPP_OPEN_BRACE ? "{" : "}");
22215     }
22216   /* Finally, try to parse a block-declaration, or a function-definition.  */
22217   else
22218     cp_parser_block_declaration (parser, /*statement_p=*/false);
22219 }
22220
22221 /* Parse a method signature.  */
22222
22223 static tree
22224 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22225 {
22226   tree rettype, kwdparms, optparms;
22227   bool ellipsis = false;
22228   bool is_class_method;
22229
22230   is_class_method = cp_parser_objc_method_type (parser);
22231   rettype = cp_parser_objc_typename (parser);
22232   *attributes = NULL_TREE;
22233   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22234   if (kwdparms == error_mark_node)
22235     return error_mark_node;
22236   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22237   if (optparms == error_mark_node)
22238     return error_mark_node;
22239
22240   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22241 }
22242
22243 static bool
22244 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22245 {
22246   tree tattr;  
22247   cp_lexer_save_tokens (parser->lexer);
22248   tattr = cp_parser_attributes_opt (parser);
22249   gcc_assert (tattr) ;
22250   
22251   /* If the attributes are followed by a method introducer, this is not allowed.
22252      Dump the attributes and flag the situation.  */
22253   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22254       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22255     return true;
22256
22257   /* Otherwise, the attributes introduce some interstitial code, possibly so
22258      rewind to allow that check.  */
22259   cp_lexer_rollback_tokens (parser->lexer);
22260   return false;  
22261 }
22262
22263 /* Parse an Objective-C method prototype list.  */
22264
22265 static void
22266 cp_parser_objc_method_prototype_list (cp_parser* parser)
22267 {
22268   cp_token *token = cp_lexer_peek_token (parser->lexer);
22269
22270   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22271     {
22272       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22273         {
22274           tree attributes, sig;
22275           bool is_class_method;
22276           if (token->type == CPP_PLUS)
22277             is_class_method = true;
22278           else
22279             is_class_method = false;
22280           sig = cp_parser_objc_method_signature (parser, &attributes);
22281           if (sig == error_mark_node)
22282             {
22283               cp_parser_skip_to_end_of_block_or_statement (parser);
22284               token = cp_lexer_peek_token (parser->lexer);
22285               continue;
22286             }
22287           objc_add_method_declaration (is_class_method, sig, attributes);
22288           cp_parser_consume_semicolon_at_end_of_statement (parser);
22289         }
22290       else if (token->keyword == RID_AT_PROPERTY)
22291         cp_parser_objc_at_property_declaration (parser);
22292       else if (token->keyword == RID_ATTRIBUTE 
22293                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22294         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22295                     OPT_Wattributes, 
22296                     "prefix attributes are ignored for methods");
22297       else
22298         /* Allow for interspersed non-ObjC++ code.  */
22299         cp_parser_objc_interstitial_code (parser);
22300
22301       token = cp_lexer_peek_token (parser->lexer);
22302     }
22303
22304   if (token->type != CPP_EOF)
22305     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22306   else
22307     cp_parser_error (parser, "expected %<@end%>");
22308
22309   objc_finish_interface ();
22310 }
22311
22312 /* Parse an Objective-C method definition list.  */
22313
22314 static void
22315 cp_parser_objc_method_definition_list (cp_parser* parser)
22316 {
22317   cp_token *token = cp_lexer_peek_token (parser->lexer);
22318
22319   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22320     {
22321       tree meth;
22322
22323       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22324         {
22325           cp_token *ptk;
22326           tree sig, attribute;
22327           bool is_class_method;
22328           if (token->type == CPP_PLUS)
22329             is_class_method = true;
22330           else
22331             is_class_method = false;
22332           push_deferring_access_checks (dk_deferred);
22333           sig = cp_parser_objc_method_signature (parser, &attribute);
22334           if (sig == error_mark_node)
22335             {
22336               cp_parser_skip_to_end_of_block_or_statement (parser);
22337               token = cp_lexer_peek_token (parser->lexer);
22338               continue;
22339             }
22340           objc_start_method_definition (is_class_method, sig, attribute);
22341
22342           /* For historical reasons, we accept an optional semicolon.  */
22343           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22344             cp_lexer_consume_token (parser->lexer);
22345
22346           ptk = cp_lexer_peek_token (parser->lexer);
22347           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22348                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22349             {
22350               perform_deferred_access_checks ();
22351               stop_deferring_access_checks ();
22352               meth = cp_parser_function_definition_after_declarator (parser,
22353                                                                      false);
22354               pop_deferring_access_checks ();
22355               objc_finish_method_definition (meth);
22356             }
22357         }
22358       /* The following case will be removed once @synthesize is
22359          completely implemented.  */
22360       else if (token->keyword == RID_AT_PROPERTY)
22361         cp_parser_objc_at_property_declaration (parser);
22362       else if (token->keyword == RID_AT_SYNTHESIZE)
22363         cp_parser_objc_at_synthesize_declaration (parser);
22364       else if (token->keyword == RID_AT_DYNAMIC)
22365         cp_parser_objc_at_dynamic_declaration (parser);
22366       else if (token->keyword == RID_ATTRIBUTE 
22367                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22368         warning_at (token->location, OPT_Wattributes,
22369                     "prefix attributes are ignored for methods");
22370       else
22371         /* Allow for interspersed non-ObjC++ code.  */
22372         cp_parser_objc_interstitial_code (parser);
22373
22374       token = cp_lexer_peek_token (parser->lexer);
22375     }
22376
22377   if (token->type != CPP_EOF)
22378     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22379   else
22380     cp_parser_error (parser, "expected %<@end%>");
22381
22382   objc_finish_implementation ();
22383 }
22384
22385 /* Parse Objective-C ivars.  */
22386
22387 static void
22388 cp_parser_objc_class_ivars (cp_parser* parser)
22389 {
22390   cp_token *token = cp_lexer_peek_token (parser->lexer);
22391
22392   if (token->type != CPP_OPEN_BRACE)
22393     return;     /* No ivars specified.  */
22394
22395   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22396   token = cp_lexer_peek_token (parser->lexer);
22397
22398   while (token->type != CPP_CLOSE_BRACE 
22399         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22400     {
22401       cp_decl_specifier_seq declspecs;
22402       int decl_class_or_enum_p;
22403       tree prefix_attributes;
22404
22405       cp_parser_objc_visibility_spec (parser);
22406
22407       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22408         break;
22409
22410       cp_parser_decl_specifier_seq (parser,
22411                                     CP_PARSER_FLAGS_OPTIONAL,
22412                                     &declspecs,
22413                                     &decl_class_or_enum_p);
22414
22415       /* auto, register, static, extern, mutable.  */
22416       if (declspecs.storage_class != sc_none)
22417         {
22418           cp_parser_error (parser, "invalid type for instance variable");         
22419           declspecs.storage_class = sc_none;
22420         }
22421
22422       /* __thread.  */
22423       if (declspecs.specs[(int) ds_thread])
22424         {
22425           cp_parser_error (parser, "invalid type for instance variable");
22426           declspecs.specs[(int) ds_thread] = 0;
22427         }
22428       
22429       /* typedef.  */
22430       if (declspecs.specs[(int) ds_typedef])
22431         {
22432           cp_parser_error (parser, "invalid type for instance variable");
22433           declspecs.specs[(int) ds_typedef] = 0;
22434         }
22435
22436       prefix_attributes = declspecs.attributes;
22437       declspecs.attributes = NULL_TREE;
22438
22439       /* Keep going until we hit the `;' at the end of the
22440          declaration.  */
22441       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22442         {
22443           tree width = NULL_TREE, attributes, first_attribute, decl;
22444           cp_declarator *declarator = NULL;
22445           int ctor_dtor_or_conv_p;
22446
22447           /* Check for a (possibly unnamed) bitfield declaration.  */
22448           token = cp_lexer_peek_token (parser->lexer);
22449           if (token->type == CPP_COLON)
22450             goto eat_colon;
22451
22452           if (token->type == CPP_NAME
22453               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22454                   == CPP_COLON))
22455             {
22456               /* Get the name of the bitfield.  */
22457               declarator = make_id_declarator (NULL_TREE,
22458                                                cp_parser_identifier (parser),
22459                                                sfk_none);
22460
22461              eat_colon:
22462               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22463               /* Get the width of the bitfield.  */
22464               width
22465                 = cp_parser_constant_expression (parser,
22466                                                  /*allow_non_constant=*/false,
22467                                                  NULL);
22468             }
22469           else
22470             {
22471               /* Parse the declarator.  */
22472               declarator
22473                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22474                                         &ctor_dtor_or_conv_p,
22475                                         /*parenthesized_p=*/NULL,
22476                                         /*member_p=*/false);
22477             }
22478
22479           /* Look for attributes that apply to the ivar.  */
22480           attributes = cp_parser_attributes_opt (parser);
22481           /* Remember which attributes are prefix attributes and
22482              which are not.  */
22483           first_attribute = attributes;
22484           /* Combine the attributes.  */
22485           attributes = chainon (prefix_attributes, attributes);
22486
22487           if (width)
22488               /* Create the bitfield declaration.  */
22489               decl = grokbitfield (declarator, &declspecs,
22490                                    width,
22491                                    attributes);
22492           else
22493             decl = grokfield (declarator, &declspecs,
22494                               NULL_TREE, /*init_const_expr_p=*/false,
22495                               NULL_TREE, attributes);
22496
22497           /* Add the instance variable.  */
22498           objc_add_instance_variable (decl);
22499
22500           /* Reset PREFIX_ATTRIBUTES.  */
22501           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22502             attributes = TREE_CHAIN (attributes);
22503           if (attributes)
22504             TREE_CHAIN (attributes) = NULL_TREE;
22505
22506           token = cp_lexer_peek_token (parser->lexer);
22507
22508           if (token->type == CPP_COMMA)
22509             {
22510               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22511               continue;
22512             }
22513           break;
22514         }
22515
22516       cp_parser_consume_semicolon_at_end_of_statement (parser);
22517       token = cp_lexer_peek_token (parser->lexer);
22518     }
22519
22520   if (token->keyword == RID_AT_END)
22521     cp_parser_error (parser, "expected %<}%>");
22522
22523   /* Do not consume the RID_AT_END, so it will be read again as terminating
22524      the @interface of @implementation.  */ 
22525   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22526     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22527     
22528   /* For historical reasons, we accept an optional semicolon.  */
22529   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22530     cp_lexer_consume_token (parser->lexer);
22531 }
22532
22533 /* Parse an Objective-C protocol declaration.  */
22534
22535 static void
22536 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22537 {
22538   tree proto, protorefs;
22539   cp_token *tok;
22540
22541   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22542   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22543     {
22544       tok = cp_lexer_peek_token (parser->lexer);
22545       error_at (tok->location, "identifier expected after %<@protocol%>");
22546       goto finish;
22547     }
22548
22549   /* See if we have a forward declaration or a definition.  */
22550   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22551
22552   /* Try a forward declaration first.  */
22553   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22554     {
22555       objc_declare_protocols (cp_parser_objc_identifier_list (parser), 
22556                               attributes);
22557      finish:
22558       cp_parser_consume_semicolon_at_end_of_statement (parser);
22559     }
22560
22561   /* Ok, we got a full-fledged definition (or at least should).  */
22562   else
22563     {
22564       proto = cp_parser_identifier (parser);
22565       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22566       objc_start_protocol (proto, protorefs, attributes);
22567       cp_parser_objc_method_prototype_list (parser);
22568     }
22569 }
22570
22571 /* Parse an Objective-C superclass or category.  */
22572
22573 static void
22574 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22575                                        bool iface_p,
22576                                        tree *super,
22577                                        tree *categ, bool *is_class_extension)
22578 {
22579   cp_token *next = cp_lexer_peek_token (parser->lexer);
22580
22581   *super = *categ = NULL_TREE;
22582   *is_class_extension = false;
22583   if (next->type == CPP_COLON)
22584     {
22585       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22586       *super = cp_parser_identifier (parser);
22587     }
22588   else if (next->type == CPP_OPEN_PAREN)
22589     {
22590       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22591
22592       /* If there is no category name, and this is an @interface, we
22593          have a class extension.  */
22594       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22595         {
22596           *categ = NULL_TREE;
22597           *is_class_extension = true;
22598         }
22599       else
22600         *categ = cp_parser_identifier (parser);
22601
22602       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22603     }
22604 }
22605
22606 /* Parse an Objective-C class interface.  */
22607
22608 static void
22609 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22610 {
22611   tree name, super, categ, protos;
22612   bool is_class_extension;
22613
22614   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22615   name = cp_parser_identifier (parser);
22616   if (name == error_mark_node)
22617     {
22618       /* It's hard to recover because even if valid @interface stuff
22619          is to follow, we can't compile it (or validate it) if we
22620          don't even know which class it refers to.  Let's assume this
22621          was a stray '@interface' token in the stream and skip it.
22622       */
22623       return;
22624     }
22625   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22626                                          &is_class_extension);
22627   protos = cp_parser_objc_protocol_refs_opt (parser);
22628
22629   /* We have either a class or a category on our hands.  */
22630   if (categ || is_class_extension)
22631     objc_start_category_interface (name, categ, protos, attributes);
22632   else
22633     {
22634       objc_start_class_interface (name, super, protos, attributes);
22635       /* Handle instance variable declarations, if any.  */
22636       cp_parser_objc_class_ivars (parser);
22637       objc_continue_interface ();
22638     }
22639
22640   cp_parser_objc_method_prototype_list (parser);
22641 }
22642
22643 /* Parse an Objective-C class implementation.  */
22644
22645 static void
22646 cp_parser_objc_class_implementation (cp_parser* parser)
22647 {
22648   tree name, super, categ;
22649   bool is_class_extension;
22650
22651   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22652   name = cp_parser_identifier (parser);
22653   if (name == error_mark_node)
22654     {
22655       /* It's hard to recover because even if valid @implementation
22656          stuff is to follow, we can't compile it (or validate it) if
22657          we don't even know which class it refers to.  Let's assume
22658          this was a stray '@implementation' token in the stream and
22659          skip it.
22660       */
22661       return;
22662     }
22663   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22664                                          &is_class_extension);
22665
22666   /* We have either a class or a category on our hands.  */
22667   if (categ)
22668     objc_start_category_implementation (name, categ);
22669   else
22670     {
22671       objc_start_class_implementation (name, super);
22672       /* Handle instance variable declarations, if any.  */
22673       cp_parser_objc_class_ivars (parser);
22674       objc_continue_implementation ();
22675     }
22676
22677   cp_parser_objc_method_definition_list (parser);
22678 }
22679
22680 /* Consume the @end token and finish off the implementation.  */
22681
22682 static void
22683 cp_parser_objc_end_implementation (cp_parser* parser)
22684 {
22685   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22686   objc_finish_implementation ();
22687 }
22688
22689 /* Parse an Objective-C declaration.  */
22690
22691 static void
22692 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22693 {
22694   /* Try to figure out what kind of declaration is present.  */
22695   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22696
22697   if (attributes)
22698     switch (kwd->keyword)
22699       {
22700         case RID_AT_ALIAS:
22701         case RID_AT_CLASS:
22702         case RID_AT_END:
22703           error_at (kwd->location, "attributes may not be specified before"
22704                     " the %<@%D%> Objective-C++ keyword",
22705                     kwd->u.value);
22706           attributes = NULL;
22707           break;
22708         case RID_AT_IMPLEMENTATION:
22709           warning_at (kwd->location, OPT_Wattributes,
22710                       "prefix attributes are ignored before %<@%D%>",
22711                       kwd->u.value);
22712           attributes = NULL;
22713         default:
22714           break;
22715       }
22716
22717   switch (kwd->keyword)
22718     {
22719     case RID_AT_ALIAS:
22720       cp_parser_objc_alias_declaration (parser);
22721       break;
22722     case RID_AT_CLASS:
22723       cp_parser_objc_class_declaration (parser);
22724       break;
22725     case RID_AT_PROTOCOL:
22726       cp_parser_objc_protocol_declaration (parser, attributes);
22727       break;
22728     case RID_AT_INTERFACE:
22729       cp_parser_objc_class_interface (parser, attributes);
22730       break;
22731     case RID_AT_IMPLEMENTATION:
22732       cp_parser_objc_class_implementation (parser);
22733       break;
22734     case RID_AT_END:
22735       cp_parser_objc_end_implementation (parser);
22736       break;
22737     default:
22738       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22739                 kwd->u.value);
22740       cp_parser_skip_to_end_of_block_or_statement (parser);
22741     }
22742 }
22743
22744 /* Parse an Objective-C try-catch-finally statement.
22745
22746    objc-try-catch-finally-stmt:
22747      @try compound-statement objc-catch-clause-seq [opt]
22748        objc-finally-clause [opt]
22749
22750    objc-catch-clause-seq:
22751      objc-catch-clause objc-catch-clause-seq [opt]
22752
22753    objc-catch-clause:
22754      @catch ( objc-exception-declaration ) compound-statement
22755
22756    objc-finally-clause:
22757      @finally compound-statement
22758
22759    objc-exception-declaration:
22760      parameter-declaration
22761      '...'
22762
22763    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22764
22765    Returns NULL_TREE.
22766
22767    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22768    for C.  Keep them in sync.  */   
22769
22770 static tree
22771 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22772 {
22773   location_t location;
22774   tree stmt;
22775
22776   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22777   location = cp_lexer_peek_token (parser->lexer)->location;
22778   objc_maybe_warn_exceptions (location);
22779   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22780      node, lest it get absorbed into the surrounding block.  */
22781   stmt = push_stmt_list ();
22782   cp_parser_compound_statement (parser, NULL, false);
22783   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22784
22785   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22786     {
22787       cp_parameter_declarator *parm;
22788       tree parameter_declaration = error_mark_node;
22789       bool seen_open_paren = false;
22790
22791       cp_lexer_consume_token (parser->lexer);
22792       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22793         seen_open_paren = true;
22794       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22795         {
22796           /* We have "@catch (...)" (where the '...' are literally
22797              what is in the code).  Skip the '...'.
22798              parameter_declaration is set to NULL_TREE, and
22799              objc_being_catch_clauses() knows that that means
22800              '...'.  */
22801           cp_lexer_consume_token (parser->lexer);
22802           parameter_declaration = NULL_TREE;
22803         }
22804       else
22805         {
22806           /* We have "@catch (NSException *exception)" or something
22807              like that.  Parse the parameter declaration.  */
22808           parm = cp_parser_parameter_declaration (parser, false, NULL);
22809           if (parm == NULL)
22810             parameter_declaration = error_mark_node;
22811           else
22812             parameter_declaration = grokdeclarator (parm->declarator,
22813                                                     &parm->decl_specifiers,
22814                                                     PARM, /*initialized=*/0,
22815                                                     /*attrlist=*/NULL);
22816         }
22817       if (seen_open_paren)
22818         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22819       else
22820         {
22821           /* If there was no open parenthesis, we are recovering from
22822              an error, and we are trying to figure out what mistake
22823              the user has made.  */
22824
22825           /* If there is an immediate closing parenthesis, the user
22826              probably forgot the opening one (ie, they typed "@catch
22827              NSException *e)".  Parse the closing parenthesis and keep
22828              going.  */
22829           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22830             cp_lexer_consume_token (parser->lexer);
22831           
22832           /* If these is no immediate closing parenthesis, the user
22833              probably doesn't know that parenthesis are required at
22834              all (ie, they typed "@catch NSException *e").  So, just
22835              forget about the closing parenthesis and keep going.  */
22836         }
22837       objc_begin_catch_clause (parameter_declaration);
22838       cp_parser_compound_statement (parser, NULL, false);
22839       objc_finish_catch_clause ();
22840     }
22841   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22842     {
22843       cp_lexer_consume_token (parser->lexer);
22844       location = cp_lexer_peek_token (parser->lexer)->location;
22845       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22846          node, lest it get absorbed into the surrounding block.  */
22847       stmt = push_stmt_list ();
22848       cp_parser_compound_statement (parser, NULL, false);
22849       objc_build_finally_clause (location, pop_stmt_list (stmt));
22850     }
22851
22852   return objc_finish_try_stmt ();
22853 }
22854
22855 /* Parse an Objective-C synchronized statement.
22856
22857    objc-synchronized-stmt:
22858      @synchronized ( expression ) compound-statement
22859
22860    Returns NULL_TREE.  */
22861
22862 static tree
22863 cp_parser_objc_synchronized_statement (cp_parser *parser)
22864 {
22865   location_t location;
22866   tree lock, stmt;
22867
22868   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22869
22870   location = cp_lexer_peek_token (parser->lexer)->location;
22871   objc_maybe_warn_exceptions (location);
22872   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22873   lock = cp_parser_expression (parser, false, NULL);
22874   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22875
22876   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22877      node, lest it get absorbed into the surrounding block.  */
22878   stmt = push_stmt_list ();
22879   cp_parser_compound_statement (parser, NULL, false);
22880
22881   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22882 }
22883
22884 /* Parse an Objective-C throw statement.
22885
22886    objc-throw-stmt:
22887      @throw assignment-expression [opt] ;
22888
22889    Returns a constructed '@throw' statement.  */
22890
22891 static tree
22892 cp_parser_objc_throw_statement (cp_parser *parser)
22893 {
22894   tree expr = NULL_TREE;
22895   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22896
22897   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22898
22899   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22900     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22901
22902   cp_parser_consume_semicolon_at_end_of_statement (parser);
22903
22904   return objc_build_throw_stmt (loc, expr);
22905 }
22906
22907 /* Parse an Objective-C statement.  */
22908
22909 static tree
22910 cp_parser_objc_statement (cp_parser * parser)
22911 {
22912   /* Try to figure out what kind of declaration is present.  */
22913   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22914
22915   switch (kwd->keyword)
22916     {
22917     case RID_AT_TRY:
22918       return cp_parser_objc_try_catch_finally_statement (parser);
22919     case RID_AT_SYNCHRONIZED:
22920       return cp_parser_objc_synchronized_statement (parser);
22921     case RID_AT_THROW:
22922       return cp_parser_objc_throw_statement (parser);
22923     default:
22924       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22925                kwd->u.value);
22926       cp_parser_skip_to_end_of_block_or_statement (parser);
22927     }
22928
22929   return error_mark_node;
22930 }
22931
22932 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22933    look ahead to see if an objc keyword follows the attributes.  This
22934    is to detect the use of prefix attributes on ObjC @interface and 
22935    @protocol.  */
22936
22937 static bool
22938 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22939 {
22940   cp_lexer_save_tokens (parser->lexer);
22941   *attrib = cp_parser_attributes_opt (parser);
22942   gcc_assert (*attrib);
22943   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22944     {
22945       cp_lexer_commit_tokens (parser->lexer);
22946       return true;
22947     }
22948   cp_lexer_rollback_tokens (parser->lexer);
22949   return false;  
22950 }
22951
22952 /* This routine is a minimal replacement for
22953    c_parser_struct_declaration () used when parsing the list of
22954    types/names or ObjC++ properties.  For example, when parsing the
22955    code
22956
22957    @property (readonly) int a, b, c;
22958
22959    this function is responsible for parsing "int a, int b, int c" and
22960    returning the declarations as CHAIN of DECLs.
22961
22962    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22963    similar parsing.  */
22964 static tree
22965 cp_parser_objc_struct_declaration (cp_parser *parser)
22966 {
22967   tree decls = NULL_TREE;
22968   cp_decl_specifier_seq declspecs;
22969   int decl_class_or_enum_p;
22970   tree prefix_attributes;
22971
22972   cp_parser_decl_specifier_seq (parser,
22973                                 CP_PARSER_FLAGS_NONE,
22974                                 &declspecs,
22975                                 &decl_class_or_enum_p);
22976
22977   if (declspecs.type == error_mark_node)
22978     return error_mark_node;
22979
22980   /* auto, register, static, extern, mutable.  */
22981   if (declspecs.storage_class != sc_none)
22982     {
22983       cp_parser_error (parser, "invalid type for property");
22984       declspecs.storage_class = sc_none;
22985     }
22986   
22987   /* __thread.  */
22988   if (declspecs.specs[(int) ds_thread])
22989     {
22990       cp_parser_error (parser, "invalid type for property");
22991       declspecs.specs[(int) ds_thread] = 0;
22992     }
22993   
22994   /* typedef.  */
22995   if (declspecs.specs[(int) ds_typedef])
22996     {
22997       cp_parser_error (parser, "invalid type for property");
22998       declspecs.specs[(int) ds_typedef] = 0;
22999     }
23000
23001   prefix_attributes = declspecs.attributes;
23002   declspecs.attributes = NULL_TREE;
23003
23004   /* Keep going until we hit the `;' at the end of the declaration. */
23005   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23006     {
23007       tree attributes, first_attribute, decl;
23008       cp_declarator *declarator;
23009       cp_token *token;
23010
23011       /* Parse the declarator.  */
23012       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23013                                          NULL, NULL, false);
23014
23015       /* Look for attributes that apply to the ivar.  */
23016       attributes = cp_parser_attributes_opt (parser);
23017       /* Remember which attributes are prefix attributes and
23018          which are not.  */
23019       first_attribute = attributes;
23020       /* Combine the attributes.  */
23021       attributes = chainon (prefix_attributes, attributes);
23022       
23023       decl = grokfield (declarator, &declspecs,
23024                         NULL_TREE, /*init_const_expr_p=*/false,
23025                         NULL_TREE, attributes);
23026
23027       if (decl == error_mark_node || decl == NULL_TREE)
23028         return error_mark_node;
23029       
23030       /* Reset PREFIX_ATTRIBUTES.  */
23031       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23032         attributes = TREE_CHAIN (attributes);
23033       if (attributes)
23034         TREE_CHAIN (attributes) = NULL_TREE;
23035
23036       DECL_CHAIN (decl) = decls;
23037       decls = decl;
23038
23039       token = cp_lexer_peek_token (parser->lexer);
23040       if (token->type == CPP_COMMA)
23041         {
23042           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23043           continue;
23044         }
23045       else
23046         break;
23047     }
23048   return decls;
23049 }
23050
23051 /* Parse an Objective-C @property declaration.  The syntax is:
23052
23053    objc-property-declaration:
23054      '@property' objc-property-attributes[opt] struct-declaration ;
23055
23056    objc-property-attributes:
23057     '(' objc-property-attribute-list ')'
23058
23059    objc-property-attribute-list:
23060      objc-property-attribute
23061      objc-property-attribute-list, objc-property-attribute
23062
23063    objc-property-attribute
23064      'getter' = identifier
23065      'setter' = identifier
23066      'readonly'
23067      'readwrite'
23068      'assign'
23069      'retain'
23070      'copy'
23071      'nonatomic'
23072
23073   For example:
23074     @property NSString *name;
23075     @property (readonly) id object;
23076     @property (retain, nonatomic, getter=getTheName) id name;
23077     @property int a, b, c;
23078
23079    PS: This function is identical to
23080    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23081 static void 
23082 cp_parser_objc_at_property_declaration (cp_parser *parser)
23083 {
23084   /* The following variables hold the attributes of the properties as
23085      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23086      seen.  When we see an attribute, we set them to 'true' (if they
23087      are boolean properties) or to the identifier (if they have an
23088      argument, ie, for getter and setter).  Note that here we only
23089      parse the list of attributes, check the syntax and accumulate the
23090      attributes that we find.  objc_add_property_declaration() will
23091      then process the information.  */
23092   bool property_assign = false;
23093   bool property_copy = false;
23094   tree property_getter_ident = NULL_TREE;
23095   bool property_nonatomic = false;
23096   bool property_readonly = false;
23097   bool property_readwrite = false;
23098   bool property_retain = false;
23099   tree property_setter_ident = NULL_TREE;
23100
23101   /* 'properties' is the list of properties that we read.  Usually a
23102      single one, but maybe more (eg, in "@property int a, b, c;" there
23103      are three).  */
23104   tree properties;
23105   location_t loc;
23106
23107   loc = cp_lexer_peek_token (parser->lexer)->location;
23108
23109   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23110
23111   /* Parse the optional attribute list...  */
23112   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23113     {
23114       /* Eat the '('.  */
23115       cp_lexer_consume_token (parser->lexer);
23116
23117       while (true)
23118         {
23119           bool syntax_error = false;
23120           cp_token *token = cp_lexer_peek_token (parser->lexer);
23121           enum rid keyword;
23122
23123           if (token->type != CPP_NAME)
23124             {
23125               cp_parser_error (parser, "expected identifier");
23126               break;
23127             }
23128           keyword = C_RID_CODE (token->u.value);
23129           cp_lexer_consume_token (parser->lexer);
23130           switch (keyword)
23131             {
23132             case RID_ASSIGN:    property_assign = true;    break;
23133             case RID_COPY:      property_copy = true;      break;
23134             case RID_NONATOMIC: property_nonatomic = true; break;
23135             case RID_READONLY:  property_readonly = true;  break;
23136             case RID_READWRITE: property_readwrite = true; break;
23137             case RID_RETAIN:    property_retain = true;    break;
23138
23139             case RID_GETTER:
23140             case RID_SETTER:
23141               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23142                 {
23143                   if (keyword == RID_GETTER)
23144                     cp_parser_error (parser,
23145                                      "missing %<=%> (after %<getter%> attribute)");
23146                   else
23147                     cp_parser_error (parser,
23148                                      "missing %<=%> (after %<setter%> attribute)");
23149                   syntax_error = true;
23150                   break;
23151                 }
23152               cp_lexer_consume_token (parser->lexer); /* eat the = */
23153               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23154                 {
23155                   cp_parser_error (parser, "expected identifier");
23156                   syntax_error = true;
23157                   break;
23158                 }
23159               if (keyword == RID_SETTER)
23160                 {
23161                   if (property_setter_ident != NULL_TREE)
23162                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23163                   else
23164                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23165                   cp_lexer_consume_token (parser->lexer);
23166                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23167                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23168                   else
23169                     cp_lexer_consume_token (parser->lexer);
23170                 }
23171               else
23172                 {
23173                   if (property_getter_ident != NULL_TREE)
23174                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23175                   else
23176                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23177                   cp_lexer_consume_token (parser->lexer);
23178                 }
23179               break;
23180             default:
23181               cp_parser_error (parser, "unknown property attribute");
23182               syntax_error = true;
23183               break;
23184             }
23185
23186           if (syntax_error)
23187             break;
23188
23189           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23190             cp_lexer_consume_token (parser->lexer);
23191           else
23192             break;
23193         }
23194
23195       /* FIXME: "@property (setter, assign);" will generate a spurious
23196          "error: expected â€˜)’ before â€˜,’ token".  This is because
23197          cp_parser_require, unlike the C counterpart, will produce an
23198          error even if we are in error recovery.  */
23199       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23200         {
23201           cp_parser_skip_to_closing_parenthesis (parser,
23202                                                  /*recovering=*/true,
23203                                                  /*or_comma=*/false,
23204                                                  /*consume_paren=*/true);
23205         }
23206     }
23207
23208   /* ... and the property declaration(s).  */
23209   properties = cp_parser_objc_struct_declaration (parser);
23210
23211   if (properties == error_mark_node)
23212     {
23213       cp_parser_skip_to_end_of_statement (parser);
23214       /* If the next token is now a `;', consume it.  */
23215       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23216         cp_lexer_consume_token (parser->lexer);
23217       return;
23218     }
23219
23220   if (properties == NULL_TREE)
23221     cp_parser_error (parser, "expected identifier");
23222   else
23223     {
23224       /* Comma-separated properties are chained together in
23225          reverse order; add them one by one.  */
23226       properties = nreverse (properties);
23227       
23228       for (; properties; properties = TREE_CHAIN (properties))
23229         objc_add_property_declaration (loc, copy_node (properties),
23230                                        property_readonly, property_readwrite,
23231                                        property_assign, property_retain,
23232                                        property_copy, property_nonatomic,
23233                                        property_getter_ident, property_setter_ident);
23234     }
23235   
23236   cp_parser_consume_semicolon_at_end_of_statement (parser);
23237 }
23238
23239 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23240
23241    objc-synthesize-declaration:
23242      @synthesize objc-synthesize-identifier-list ;
23243
23244    objc-synthesize-identifier-list:
23245      objc-synthesize-identifier
23246      objc-synthesize-identifier-list, objc-synthesize-identifier
23247
23248    objc-synthesize-identifier
23249      identifier
23250      identifier = identifier
23251
23252   For example:
23253     @synthesize MyProperty;
23254     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23255
23256   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23257   for C.  Keep them in sync.
23258 */
23259 static void 
23260 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23261 {
23262   tree list = NULL_TREE;
23263   location_t loc;
23264   loc = cp_lexer_peek_token (parser->lexer)->location;
23265
23266   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23267   while (true)
23268     {
23269       tree property, ivar;
23270       property = cp_parser_identifier (parser);
23271       if (property == error_mark_node)
23272         {
23273           cp_parser_consume_semicolon_at_end_of_statement (parser);
23274           return;
23275         }
23276       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23277         {
23278           cp_lexer_consume_token (parser->lexer);
23279           ivar = cp_parser_identifier (parser);
23280           if (ivar == error_mark_node)
23281             {
23282               cp_parser_consume_semicolon_at_end_of_statement (parser);
23283               return;
23284             }
23285         }
23286       else
23287         ivar = NULL_TREE;
23288       list = chainon (list, build_tree_list (ivar, property));
23289       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23290         cp_lexer_consume_token (parser->lexer);
23291       else
23292         break;
23293     }
23294   cp_parser_consume_semicolon_at_end_of_statement (parser);
23295   objc_add_synthesize_declaration (loc, list);
23296 }
23297
23298 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23299
23300    objc-dynamic-declaration:
23301      @dynamic identifier-list ;
23302
23303    For example:
23304      @dynamic MyProperty;
23305      @dynamic MyProperty, AnotherProperty;
23306
23307   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23308   for C.  Keep them in sync.
23309 */
23310 static void 
23311 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23312 {
23313   tree list = NULL_TREE;
23314   location_t loc;
23315   loc = cp_lexer_peek_token (parser->lexer)->location;
23316
23317   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23318   while (true)
23319     {
23320       tree property;
23321       property = cp_parser_identifier (parser);
23322       if (property == error_mark_node)
23323         {
23324           cp_parser_consume_semicolon_at_end_of_statement (parser);
23325           return;
23326         }
23327       list = chainon (list, build_tree_list (NULL, property));
23328       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23329         cp_lexer_consume_token (parser->lexer);
23330       else
23331         break;
23332     }
23333   cp_parser_consume_semicolon_at_end_of_statement (parser);
23334   objc_add_dynamic_declaration (loc, list);
23335 }
23336
23337 \f
23338 /* OpenMP 2.5 parsing routines.  */
23339
23340 /* Returns name of the next clause.
23341    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23342    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23343    returned and the token is consumed.  */
23344
23345 static pragma_omp_clause
23346 cp_parser_omp_clause_name (cp_parser *parser)
23347 {
23348   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23349
23350   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23351     result = PRAGMA_OMP_CLAUSE_IF;
23352   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23353     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23354   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23355     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23356   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23357     {
23358       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23359       const char *p = IDENTIFIER_POINTER (id);
23360
23361       switch (p[0])
23362         {
23363         case 'c':
23364           if (!strcmp ("collapse", p))
23365             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23366           else if (!strcmp ("copyin", p))
23367             result = PRAGMA_OMP_CLAUSE_COPYIN;
23368           else if (!strcmp ("copyprivate", p))
23369             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23370           break;
23371         case 'f':
23372           if (!strcmp ("firstprivate", p))
23373             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23374           break;
23375         case 'l':
23376           if (!strcmp ("lastprivate", p))
23377             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23378           break;
23379         case 'n':
23380           if (!strcmp ("nowait", p))
23381             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23382           else if (!strcmp ("num_threads", p))
23383             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23384           break;
23385         case 'o':
23386           if (!strcmp ("ordered", p))
23387             result = PRAGMA_OMP_CLAUSE_ORDERED;
23388           break;
23389         case 'r':
23390           if (!strcmp ("reduction", p))
23391             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23392           break;
23393         case 's':
23394           if (!strcmp ("schedule", p))
23395             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23396           else if (!strcmp ("shared", p))
23397             result = PRAGMA_OMP_CLAUSE_SHARED;
23398           break;
23399         case 'u':
23400           if (!strcmp ("untied", p))
23401             result = PRAGMA_OMP_CLAUSE_UNTIED;
23402           break;
23403         }
23404     }
23405
23406   if (result != PRAGMA_OMP_CLAUSE_NONE)
23407     cp_lexer_consume_token (parser->lexer);
23408
23409   return result;
23410 }
23411
23412 /* Validate that a clause of the given type does not already exist.  */
23413
23414 static void
23415 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23416                            const char *name, location_t location)
23417 {
23418   tree c;
23419
23420   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23421     if (OMP_CLAUSE_CODE (c) == code)
23422       {
23423         error_at (location, "too many %qs clauses", name);
23424         break;
23425       }
23426 }
23427
23428 /* OpenMP 2.5:
23429    variable-list:
23430      identifier
23431      variable-list , identifier
23432
23433    In addition, we match a closing parenthesis.  An opening parenthesis
23434    will have been consumed by the caller.
23435
23436    If KIND is nonzero, create the appropriate node and install the decl
23437    in OMP_CLAUSE_DECL and add the node to the head of the list.
23438
23439    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23440    return the list created.  */
23441
23442 static tree
23443 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23444                                 tree list)
23445 {
23446   cp_token *token;
23447   while (1)
23448     {
23449       tree name, decl;
23450
23451       token = cp_lexer_peek_token (parser->lexer);
23452       name = cp_parser_id_expression (parser, /*template_p=*/false,
23453                                       /*check_dependency_p=*/true,
23454                                       /*template_p=*/NULL,
23455                                       /*declarator_p=*/false,
23456                                       /*optional_p=*/false);
23457       if (name == error_mark_node)
23458         goto skip_comma;
23459
23460       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23461       if (decl == error_mark_node)
23462         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23463                                      token->location);
23464       else if (kind != 0)
23465         {
23466           tree u = build_omp_clause (token->location, kind);
23467           OMP_CLAUSE_DECL (u) = decl;
23468           OMP_CLAUSE_CHAIN (u) = list;
23469           list = u;
23470         }
23471       else
23472         list = tree_cons (decl, NULL_TREE, list);
23473
23474     get_comma:
23475       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23476         break;
23477       cp_lexer_consume_token (parser->lexer);
23478     }
23479
23480   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23481     {
23482       int ending;
23483
23484       /* Try to resync to an unnested comma.  Copied from
23485          cp_parser_parenthesized_expression_list.  */
23486     skip_comma:
23487       ending = cp_parser_skip_to_closing_parenthesis (parser,
23488                                                       /*recovering=*/true,
23489                                                       /*or_comma=*/true,
23490                                                       /*consume_paren=*/true);
23491       if (ending < 0)
23492         goto get_comma;
23493     }
23494
23495   return list;
23496 }
23497
23498 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23499    common case for omp clauses.  */
23500
23501 static tree
23502 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23503 {
23504   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23505     return cp_parser_omp_var_list_no_open (parser, kind, list);
23506   return list;
23507 }
23508
23509 /* OpenMP 3.0:
23510    collapse ( constant-expression ) */
23511
23512 static tree
23513 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23514 {
23515   tree c, num;
23516   location_t loc;
23517   HOST_WIDE_INT n;
23518
23519   loc = cp_lexer_peek_token (parser->lexer)->location;
23520   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23521     return list;
23522
23523   num = cp_parser_constant_expression (parser, false, NULL);
23524
23525   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23526     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23527                                            /*or_comma=*/false,
23528                                            /*consume_paren=*/true);
23529
23530   if (num == error_mark_node)
23531     return list;
23532   num = fold_non_dependent_expr (num);
23533   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23534       || !host_integerp (num, 0)
23535       || (n = tree_low_cst (num, 0)) <= 0
23536       || (int) n != n)
23537     {
23538       error_at (loc, "collapse argument needs positive constant integer expression");
23539       return list;
23540     }
23541
23542   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23543   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23544   OMP_CLAUSE_CHAIN (c) = list;
23545   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23546
23547   return c;
23548 }
23549
23550 /* OpenMP 2.5:
23551    default ( shared | none ) */
23552
23553 static tree
23554 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23555 {
23556   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23557   tree c;
23558
23559   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23560     return list;
23561   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23562     {
23563       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23564       const char *p = IDENTIFIER_POINTER (id);
23565
23566       switch (p[0])
23567         {
23568         case 'n':
23569           if (strcmp ("none", p) != 0)
23570             goto invalid_kind;
23571           kind = OMP_CLAUSE_DEFAULT_NONE;
23572           break;
23573
23574         case 's':
23575           if (strcmp ("shared", p) != 0)
23576             goto invalid_kind;
23577           kind = OMP_CLAUSE_DEFAULT_SHARED;
23578           break;
23579
23580         default:
23581           goto invalid_kind;
23582         }
23583
23584       cp_lexer_consume_token (parser->lexer);
23585     }
23586   else
23587     {
23588     invalid_kind:
23589       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23590     }
23591
23592   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23593     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23594                                            /*or_comma=*/false,
23595                                            /*consume_paren=*/true);
23596
23597   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23598     return list;
23599
23600   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23601   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23602   OMP_CLAUSE_CHAIN (c) = list;
23603   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23604
23605   return c;
23606 }
23607
23608 /* OpenMP 2.5:
23609    if ( expression ) */
23610
23611 static tree
23612 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23613 {
23614   tree t, c;
23615
23616   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23617     return list;
23618
23619   t = cp_parser_condition (parser);
23620
23621   if (t == error_mark_node
23622       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23623     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23624                                            /*or_comma=*/false,
23625                                            /*consume_paren=*/true);
23626
23627   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23628
23629   c = build_omp_clause (location, OMP_CLAUSE_IF);
23630   OMP_CLAUSE_IF_EXPR (c) = t;
23631   OMP_CLAUSE_CHAIN (c) = list;
23632
23633   return c;
23634 }
23635
23636 /* OpenMP 2.5:
23637    nowait */
23638
23639 static tree
23640 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23641                              tree list, location_t location)
23642 {
23643   tree c;
23644
23645   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23646
23647   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23648   OMP_CLAUSE_CHAIN (c) = list;
23649   return c;
23650 }
23651
23652 /* OpenMP 2.5:
23653    num_threads ( expression ) */
23654
23655 static tree
23656 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23657                                   location_t location)
23658 {
23659   tree t, c;
23660
23661   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23662     return list;
23663
23664   t = cp_parser_expression (parser, false, NULL);
23665
23666   if (t == error_mark_node
23667       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23668     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23669                                            /*or_comma=*/false,
23670                                            /*consume_paren=*/true);
23671
23672   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23673                              "num_threads", location);
23674
23675   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23676   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23677   OMP_CLAUSE_CHAIN (c) = list;
23678
23679   return c;
23680 }
23681
23682 /* OpenMP 2.5:
23683    ordered */
23684
23685 static tree
23686 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23687                               tree list, location_t location)
23688 {
23689   tree c;
23690
23691   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23692                              "ordered", location);
23693
23694   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23695   OMP_CLAUSE_CHAIN (c) = list;
23696   return c;
23697 }
23698
23699 /* OpenMP 2.5:
23700    reduction ( reduction-operator : variable-list )
23701
23702    reduction-operator:
23703      One of: + * - & ^ | && || */
23704
23705 static tree
23706 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23707 {
23708   enum tree_code code;
23709   tree nlist, c;
23710
23711   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23712     return list;
23713
23714   switch (cp_lexer_peek_token (parser->lexer)->type)
23715     {
23716     case CPP_PLUS:
23717       code = PLUS_EXPR;
23718       break;
23719     case CPP_MULT:
23720       code = MULT_EXPR;
23721       break;
23722     case CPP_MINUS:
23723       code = MINUS_EXPR;
23724       break;
23725     case CPP_AND:
23726       code = BIT_AND_EXPR;
23727       break;
23728     case CPP_XOR:
23729       code = BIT_XOR_EXPR;
23730       break;
23731     case CPP_OR:
23732       code = BIT_IOR_EXPR;
23733       break;
23734     case CPP_AND_AND:
23735       code = TRUTH_ANDIF_EXPR;
23736       break;
23737     case CPP_OR_OR:
23738       code = TRUTH_ORIF_EXPR;
23739       break;
23740     default:
23741       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23742                                "%<|%>, %<&&%>, or %<||%>");
23743     resync_fail:
23744       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23745                                              /*or_comma=*/false,
23746                                              /*consume_paren=*/true);
23747       return list;
23748     }
23749   cp_lexer_consume_token (parser->lexer);
23750
23751   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23752     goto resync_fail;
23753
23754   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23755   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23756     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23757
23758   return nlist;
23759 }
23760
23761 /* OpenMP 2.5:
23762    schedule ( schedule-kind )
23763    schedule ( schedule-kind , expression )
23764
23765    schedule-kind:
23766      static | dynamic | guided | runtime | auto  */
23767
23768 static tree
23769 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23770 {
23771   tree c, t;
23772
23773   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23774     return list;
23775
23776   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23777
23778   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23779     {
23780       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23781       const char *p = IDENTIFIER_POINTER (id);
23782
23783       switch (p[0])
23784         {
23785         case 'd':
23786           if (strcmp ("dynamic", p) != 0)
23787             goto invalid_kind;
23788           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23789           break;
23790
23791         case 'g':
23792           if (strcmp ("guided", p) != 0)
23793             goto invalid_kind;
23794           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23795           break;
23796
23797         case 'r':
23798           if (strcmp ("runtime", p) != 0)
23799             goto invalid_kind;
23800           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23801           break;
23802
23803         default:
23804           goto invalid_kind;
23805         }
23806     }
23807   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23808     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23809   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23810     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23811   else
23812     goto invalid_kind;
23813   cp_lexer_consume_token (parser->lexer);
23814
23815   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23816     {
23817       cp_token *token;
23818       cp_lexer_consume_token (parser->lexer);
23819
23820       token = cp_lexer_peek_token (parser->lexer);
23821       t = cp_parser_assignment_expression (parser, false, NULL);
23822
23823       if (t == error_mark_node)
23824         goto resync_fail;
23825       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23826         error_at (token->location, "schedule %<runtime%> does not take "
23827                   "a %<chunk_size%> parameter");
23828       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23829         error_at (token->location, "schedule %<auto%> does not take "
23830                   "a %<chunk_size%> parameter");
23831       else
23832         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23833
23834       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23835         goto resync_fail;
23836     }
23837   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23838     goto resync_fail;
23839
23840   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23841   OMP_CLAUSE_CHAIN (c) = list;
23842   return c;
23843
23844  invalid_kind:
23845   cp_parser_error (parser, "invalid schedule kind");
23846  resync_fail:
23847   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23848                                          /*or_comma=*/false,
23849                                          /*consume_paren=*/true);
23850   return list;
23851 }
23852
23853 /* OpenMP 3.0:
23854    untied */
23855
23856 static tree
23857 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23858                              tree list, location_t location)
23859 {
23860   tree c;
23861
23862   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23863
23864   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23865   OMP_CLAUSE_CHAIN (c) = list;
23866   return c;
23867 }
23868
23869 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23870    is a bitmask in MASK.  Return the list of clauses found; the result
23871    of clause default goes in *pdefault.  */
23872
23873 static tree
23874 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23875                            const char *where, cp_token *pragma_tok)
23876 {
23877   tree clauses = NULL;
23878   bool first = true;
23879   cp_token *token = NULL;
23880
23881   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23882     {
23883       pragma_omp_clause c_kind;
23884       const char *c_name;
23885       tree prev = clauses;
23886
23887       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23888         cp_lexer_consume_token (parser->lexer);
23889
23890       token = cp_lexer_peek_token (parser->lexer);
23891       c_kind = cp_parser_omp_clause_name (parser);
23892       first = false;
23893
23894       switch (c_kind)
23895         {
23896         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23897           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23898                                                    token->location);
23899           c_name = "collapse";
23900           break;
23901         case PRAGMA_OMP_CLAUSE_COPYIN:
23902           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23903           c_name = "copyin";
23904           break;
23905         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23906           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23907                                             clauses);
23908           c_name = "copyprivate";
23909           break;
23910         case PRAGMA_OMP_CLAUSE_DEFAULT:
23911           clauses = cp_parser_omp_clause_default (parser, clauses,
23912                                                   token->location);
23913           c_name = "default";
23914           break;
23915         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23916           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23917                                             clauses);
23918           c_name = "firstprivate";
23919           break;
23920         case PRAGMA_OMP_CLAUSE_IF:
23921           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23922           c_name = "if";
23923           break;
23924         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23925           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23926                                             clauses);
23927           c_name = "lastprivate";
23928           break;
23929         case PRAGMA_OMP_CLAUSE_NOWAIT:
23930           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23931           c_name = "nowait";
23932           break;
23933         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23934           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23935                                                       token->location);
23936           c_name = "num_threads";
23937           break;
23938         case PRAGMA_OMP_CLAUSE_ORDERED:
23939           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23940                                                   token->location);
23941           c_name = "ordered";
23942           break;
23943         case PRAGMA_OMP_CLAUSE_PRIVATE:
23944           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23945                                             clauses);
23946           c_name = "private";
23947           break;
23948         case PRAGMA_OMP_CLAUSE_REDUCTION:
23949           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23950           c_name = "reduction";
23951           break;
23952         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23953           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23954                                                    token->location);
23955           c_name = "schedule";
23956           break;
23957         case PRAGMA_OMP_CLAUSE_SHARED:
23958           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23959                                             clauses);
23960           c_name = "shared";
23961           break;
23962         case PRAGMA_OMP_CLAUSE_UNTIED:
23963           clauses = cp_parser_omp_clause_untied (parser, clauses,
23964                                                  token->location);
23965           c_name = "nowait";
23966           break;
23967         default:
23968           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23969           goto saw_error;
23970         }
23971
23972       if (((mask >> c_kind) & 1) == 0)
23973         {
23974           /* Remove the invalid clause(s) from the list to avoid
23975              confusing the rest of the compiler.  */
23976           clauses = prev;
23977           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23978         }
23979     }
23980  saw_error:
23981   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23982   return finish_omp_clauses (clauses);
23983 }
23984
23985 /* OpenMP 2.5:
23986    structured-block:
23987      statement
23988
23989    In practice, we're also interested in adding the statement to an
23990    outer node.  So it is convenient if we work around the fact that
23991    cp_parser_statement calls add_stmt.  */
23992
23993 static unsigned
23994 cp_parser_begin_omp_structured_block (cp_parser *parser)
23995 {
23996   unsigned save = parser->in_statement;
23997
23998   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23999      This preserves the "not within loop or switch" style error messages
24000      for nonsense cases like
24001         void foo() {
24002         #pragma omp single
24003           break;
24004         }
24005   */
24006   if (parser->in_statement)
24007     parser->in_statement = IN_OMP_BLOCK;
24008
24009   return save;
24010 }
24011
24012 static void
24013 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24014 {
24015   parser->in_statement = save;
24016 }
24017
24018 static tree
24019 cp_parser_omp_structured_block (cp_parser *parser)
24020 {
24021   tree stmt = begin_omp_structured_block ();
24022   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24023
24024   cp_parser_statement (parser, NULL_TREE, false, NULL);
24025
24026   cp_parser_end_omp_structured_block (parser, save);
24027   return finish_omp_structured_block (stmt);
24028 }
24029
24030 /* OpenMP 2.5:
24031    # pragma omp atomic new-line
24032      expression-stmt
24033
24034    expression-stmt:
24035      x binop= expr | x++ | ++x | x-- | --x
24036    binop:
24037      +, *, -, /, &, ^, |, <<, >>
24038
24039   where x is an lvalue expression with scalar type.  */
24040
24041 static void
24042 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24043 {
24044   tree lhs, rhs;
24045   enum tree_code code;
24046
24047   cp_parser_require_pragma_eol (parser, pragma_tok);
24048
24049   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24050                                     /*cast_p=*/false, NULL);
24051   switch (TREE_CODE (lhs))
24052     {
24053     case ERROR_MARK:
24054       goto saw_error;
24055
24056     case PREINCREMENT_EXPR:
24057     case POSTINCREMENT_EXPR:
24058       lhs = TREE_OPERAND (lhs, 0);
24059       code = PLUS_EXPR;
24060       rhs = integer_one_node;
24061       break;
24062
24063     case PREDECREMENT_EXPR:
24064     case POSTDECREMENT_EXPR:
24065       lhs = TREE_OPERAND (lhs, 0);
24066       code = MINUS_EXPR;
24067       rhs = integer_one_node;
24068       break;
24069
24070     case COMPOUND_EXPR:
24071       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24072          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24073          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24074          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24075          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24076                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24077             == BOOLEAN_TYPE)
24078        /* Undo effects of boolean_increment for post {in,de}crement.  */
24079        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24080       /* FALLTHRU */
24081     case MODIFY_EXPR:
24082       if (TREE_CODE (lhs) == MODIFY_EXPR
24083          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24084        {
24085          /* Undo effects of boolean_increment.  */
24086          if (integer_onep (TREE_OPERAND (lhs, 1)))
24087            {
24088              /* This is pre or post increment.  */
24089              rhs = TREE_OPERAND (lhs, 1);
24090              lhs = TREE_OPERAND (lhs, 0);
24091              code = NOP_EXPR;
24092              break;
24093            }
24094        }
24095       /* FALLTHRU */
24096     default:
24097       switch (cp_lexer_peek_token (parser->lexer)->type)
24098         {
24099         case CPP_MULT_EQ:
24100           code = MULT_EXPR;
24101           break;
24102         case CPP_DIV_EQ:
24103           code = TRUNC_DIV_EXPR;
24104           break;
24105         case CPP_PLUS_EQ:
24106           code = PLUS_EXPR;
24107           break;
24108         case CPP_MINUS_EQ:
24109           code = MINUS_EXPR;
24110           break;
24111         case CPP_LSHIFT_EQ:
24112           code = LSHIFT_EXPR;
24113           break;
24114         case CPP_RSHIFT_EQ:
24115           code = RSHIFT_EXPR;
24116           break;
24117         case CPP_AND_EQ:
24118           code = BIT_AND_EXPR;
24119           break;
24120         case CPP_OR_EQ:
24121           code = BIT_IOR_EXPR;
24122           break;
24123         case CPP_XOR_EQ:
24124           code = BIT_XOR_EXPR;
24125           break;
24126         default:
24127           cp_parser_error (parser,
24128                            "invalid operator for %<#pragma omp atomic%>");
24129           goto saw_error;
24130         }
24131       cp_lexer_consume_token (parser->lexer);
24132
24133       rhs = cp_parser_expression (parser, false, NULL);
24134       if (rhs == error_mark_node)
24135         goto saw_error;
24136       break;
24137     }
24138   finish_omp_atomic (code, lhs, rhs);
24139   cp_parser_consume_semicolon_at_end_of_statement (parser);
24140   return;
24141
24142  saw_error:
24143   cp_parser_skip_to_end_of_block_or_statement (parser);
24144 }
24145
24146
24147 /* OpenMP 2.5:
24148    # pragma omp barrier new-line  */
24149
24150 static void
24151 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24152 {
24153   cp_parser_require_pragma_eol (parser, pragma_tok);
24154   finish_omp_barrier ();
24155 }
24156
24157 /* OpenMP 2.5:
24158    # pragma omp critical [(name)] new-line
24159      structured-block  */
24160
24161 static tree
24162 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24163 {
24164   tree stmt, name = NULL;
24165
24166   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24167     {
24168       cp_lexer_consume_token (parser->lexer);
24169
24170       name = cp_parser_identifier (parser);
24171
24172       if (name == error_mark_node
24173           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24174         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24175                                                /*or_comma=*/false,
24176                                                /*consume_paren=*/true);
24177       if (name == error_mark_node)
24178         name = NULL;
24179     }
24180   cp_parser_require_pragma_eol (parser, pragma_tok);
24181
24182   stmt = cp_parser_omp_structured_block (parser);
24183   return c_finish_omp_critical (input_location, stmt, name);
24184 }
24185
24186 /* OpenMP 2.5:
24187    # pragma omp flush flush-vars[opt] new-line
24188
24189    flush-vars:
24190      ( variable-list ) */
24191
24192 static void
24193 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24194 {
24195   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24196     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24197   cp_parser_require_pragma_eol (parser, pragma_tok);
24198
24199   finish_omp_flush ();
24200 }
24201
24202 /* Helper function, to parse omp for increment expression.  */
24203
24204 static tree
24205 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24206 {
24207   tree cond = cp_parser_binary_expression (parser, false, true,
24208                                            PREC_NOT_OPERATOR, NULL);
24209   bool overloaded_p;
24210
24211   if (cond == error_mark_node
24212       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24213     {
24214       cp_parser_skip_to_end_of_statement (parser);
24215       return error_mark_node;
24216     }
24217
24218   switch (TREE_CODE (cond))
24219     {
24220     case GT_EXPR:
24221     case GE_EXPR:
24222     case LT_EXPR:
24223     case LE_EXPR:
24224       break;
24225     default:
24226       return error_mark_node;
24227     }
24228
24229   /* If decl is an iterator, preserve LHS and RHS of the relational
24230      expr until finish_omp_for.  */
24231   if (decl
24232       && (type_dependent_expression_p (decl)
24233           || CLASS_TYPE_P (TREE_TYPE (decl))))
24234     return cond;
24235
24236   return build_x_binary_op (TREE_CODE (cond),
24237                             TREE_OPERAND (cond, 0), ERROR_MARK,
24238                             TREE_OPERAND (cond, 1), ERROR_MARK,
24239                             &overloaded_p, tf_warning_or_error);
24240 }
24241
24242 /* Helper function, to parse omp for increment expression.  */
24243
24244 static tree
24245 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24246 {
24247   cp_token *token = cp_lexer_peek_token (parser->lexer);
24248   enum tree_code op;
24249   tree lhs, rhs;
24250   cp_id_kind idk;
24251   bool decl_first;
24252
24253   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24254     {
24255       op = (token->type == CPP_PLUS_PLUS
24256             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24257       cp_lexer_consume_token (parser->lexer);
24258       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24259       if (lhs != decl)
24260         return error_mark_node;
24261       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24262     }
24263
24264   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24265   if (lhs != decl)
24266     return error_mark_node;
24267
24268   token = cp_lexer_peek_token (parser->lexer);
24269   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24270     {
24271       op = (token->type == CPP_PLUS_PLUS
24272             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24273       cp_lexer_consume_token (parser->lexer);
24274       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24275     }
24276
24277   op = cp_parser_assignment_operator_opt (parser);
24278   if (op == ERROR_MARK)
24279     return error_mark_node;
24280
24281   if (op != NOP_EXPR)
24282     {
24283       rhs = cp_parser_assignment_expression (parser, false, NULL);
24284       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24285       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24286     }
24287
24288   lhs = cp_parser_binary_expression (parser, false, false,
24289                                      PREC_ADDITIVE_EXPRESSION, NULL);
24290   token = cp_lexer_peek_token (parser->lexer);
24291   decl_first = lhs == decl;
24292   if (decl_first)
24293     lhs = NULL_TREE;
24294   if (token->type != CPP_PLUS
24295       && token->type != CPP_MINUS)
24296     return error_mark_node;
24297
24298   do
24299     {
24300       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24301       cp_lexer_consume_token (parser->lexer);
24302       rhs = cp_parser_binary_expression (parser, false, false,
24303                                          PREC_ADDITIVE_EXPRESSION, NULL);
24304       token = cp_lexer_peek_token (parser->lexer);
24305       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24306         {
24307           if (lhs == NULL_TREE)
24308             {
24309               if (op == PLUS_EXPR)
24310                 lhs = rhs;
24311               else
24312                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24313             }
24314           else
24315             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24316                                      NULL, tf_warning_or_error);
24317         }
24318     }
24319   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24320
24321   if (!decl_first)
24322     {
24323       if (rhs != decl || op == MINUS_EXPR)
24324         return error_mark_node;
24325       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24326     }
24327   else
24328     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24329
24330   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24331 }
24332
24333 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24334
24335 static tree
24336 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24337 {
24338   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24339   tree real_decl, initv, condv, incrv, declv;
24340   tree this_pre_body, cl;
24341   location_t loc_first;
24342   bool collapse_err = false;
24343   int i, collapse = 1, nbraces = 0;
24344   VEC(tree,gc) *for_block = make_tree_vector ();
24345
24346   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24347     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24348       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24349
24350   gcc_assert (collapse >= 1);
24351
24352   declv = make_tree_vec (collapse);
24353   initv = make_tree_vec (collapse);
24354   condv = make_tree_vec (collapse);
24355   incrv = make_tree_vec (collapse);
24356
24357   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24358
24359   for (i = 0; i < collapse; i++)
24360     {
24361       int bracecount = 0;
24362       bool add_private_clause = false;
24363       location_t loc;
24364
24365       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24366         {
24367           cp_parser_error (parser, "for statement expected");
24368           return NULL;
24369         }
24370       loc = cp_lexer_consume_token (parser->lexer)->location;
24371
24372       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24373         return NULL;
24374
24375       init = decl = real_decl = NULL;
24376       this_pre_body = push_stmt_list ();
24377       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24378         {
24379           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24380
24381              init-expr:
24382                        var = lb
24383                        integer-type var = lb
24384                        random-access-iterator-type var = lb
24385                        pointer-type var = lb
24386           */
24387           cp_decl_specifier_seq type_specifiers;
24388
24389           /* First, try to parse as an initialized declaration.  See
24390              cp_parser_condition, from whence the bulk of this is copied.  */
24391
24392           cp_parser_parse_tentatively (parser);
24393           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24394                                         /*is_trailing_return=*/false,
24395                                         &type_specifiers);
24396           if (cp_parser_parse_definitely (parser))
24397             {
24398               /* If parsing a type specifier seq succeeded, then this
24399                  MUST be a initialized declaration.  */
24400               tree asm_specification, attributes;
24401               cp_declarator *declarator;
24402
24403               declarator = cp_parser_declarator (parser,
24404                                                  CP_PARSER_DECLARATOR_NAMED,
24405                                                  /*ctor_dtor_or_conv_p=*/NULL,
24406                                                  /*parenthesized_p=*/NULL,
24407                                                  /*member_p=*/false);
24408               attributes = cp_parser_attributes_opt (parser);
24409               asm_specification = cp_parser_asm_specification_opt (parser);
24410
24411               if (declarator == cp_error_declarator) 
24412                 cp_parser_skip_to_end_of_statement (parser);
24413
24414               else 
24415                 {
24416                   tree pushed_scope, auto_node;
24417
24418                   decl = start_decl (declarator, &type_specifiers,
24419                                      SD_INITIALIZED, attributes,
24420                                      /*prefix_attributes=*/NULL_TREE,
24421                                      &pushed_scope);
24422
24423                   auto_node = type_uses_auto (TREE_TYPE (decl));
24424                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24425                     {
24426                       if (cp_lexer_next_token_is (parser->lexer, 
24427                                                   CPP_OPEN_PAREN))
24428                         error ("parenthesized initialization is not allowed in "
24429                                "OpenMP %<for%> loop");
24430                       else
24431                         /* Trigger an error.  */
24432                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24433
24434                       init = error_mark_node;
24435                       cp_parser_skip_to_end_of_statement (parser);
24436                     }
24437                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24438                            || type_dependent_expression_p (decl)
24439                            || auto_node)
24440                     {
24441                       bool is_direct_init, is_non_constant_init;
24442
24443                       init = cp_parser_initializer (parser,
24444                                                     &is_direct_init,
24445                                                     &is_non_constant_init);
24446
24447                       if (auto_node && describable_type (init))
24448                         {
24449                           TREE_TYPE (decl)
24450                             = do_auto_deduction (TREE_TYPE (decl), init,
24451                                                  auto_node);
24452
24453                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24454                               && !type_dependent_expression_p (decl))
24455                             goto non_class;
24456                         }
24457                       
24458                       cp_finish_decl (decl, init, !is_non_constant_init,
24459                                       asm_specification,
24460                                       LOOKUP_ONLYCONVERTING);
24461                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24462                         {
24463                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24464                           init = NULL_TREE;
24465                         }
24466                       else
24467                         init = pop_stmt_list (this_pre_body);
24468                       this_pre_body = NULL_TREE;
24469                     }
24470                   else
24471                     {
24472                       /* Consume '='.  */
24473                       cp_lexer_consume_token (parser->lexer);
24474                       init = cp_parser_assignment_expression (parser, false, NULL);
24475
24476                     non_class:
24477                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24478                         init = error_mark_node;
24479                       else
24480                         cp_finish_decl (decl, NULL_TREE,
24481                                         /*init_const_expr_p=*/false,
24482                                         asm_specification,
24483                                         LOOKUP_ONLYCONVERTING);
24484                     }
24485
24486                   if (pushed_scope)
24487                     pop_scope (pushed_scope);
24488                 }
24489             }
24490           else 
24491             {
24492               cp_id_kind idk;
24493               /* If parsing a type specifier sequence failed, then
24494                  this MUST be a simple expression.  */
24495               cp_parser_parse_tentatively (parser);
24496               decl = cp_parser_primary_expression (parser, false, false,
24497                                                    false, &idk);
24498               if (!cp_parser_error_occurred (parser)
24499                   && decl
24500                   && DECL_P (decl)
24501                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24502                 {
24503                   tree rhs;
24504
24505                   cp_parser_parse_definitely (parser);
24506                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24507                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24508                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24509                                                          rhs,
24510                                                          tf_warning_or_error));
24511                   add_private_clause = true;
24512                 }
24513               else
24514                 {
24515                   decl = NULL;
24516                   cp_parser_abort_tentative_parse (parser);
24517                   init = cp_parser_expression (parser, false, NULL);
24518                   if (init)
24519                     {
24520                       if (TREE_CODE (init) == MODIFY_EXPR
24521                           || TREE_CODE (init) == MODOP_EXPR)
24522                         real_decl = TREE_OPERAND (init, 0);
24523                     }
24524                 }
24525             }
24526         }
24527       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24528       if (this_pre_body)
24529         {
24530           this_pre_body = pop_stmt_list (this_pre_body);
24531           if (pre_body)
24532             {
24533               tree t = pre_body;
24534               pre_body = push_stmt_list ();
24535               add_stmt (t);
24536               add_stmt (this_pre_body);
24537               pre_body = pop_stmt_list (pre_body);
24538             }
24539           else
24540             pre_body = this_pre_body;
24541         }
24542
24543       if (decl)
24544         real_decl = decl;
24545       if (par_clauses != NULL && real_decl != NULL_TREE)
24546         {
24547           tree *c;
24548           for (c = par_clauses; *c ; )
24549             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24550                 && OMP_CLAUSE_DECL (*c) == real_decl)
24551               {
24552                 error_at (loc, "iteration variable %qD"
24553                           " should not be firstprivate", real_decl);
24554                 *c = OMP_CLAUSE_CHAIN (*c);
24555               }
24556             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24557                      && OMP_CLAUSE_DECL (*c) == real_decl)
24558               {
24559                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24560                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24561                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24562                 OMP_CLAUSE_DECL (l) = real_decl;
24563                 OMP_CLAUSE_CHAIN (l) = clauses;
24564                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24565                 clauses = l;
24566                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24567                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24568                 add_private_clause = false;
24569               }
24570             else
24571               {
24572                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24573                     && OMP_CLAUSE_DECL (*c) == real_decl)
24574                   add_private_clause = false;
24575                 c = &OMP_CLAUSE_CHAIN (*c);
24576               }
24577         }
24578
24579       if (add_private_clause)
24580         {
24581           tree c;
24582           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24583             {
24584               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24585                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24586                   && OMP_CLAUSE_DECL (c) == decl)
24587                 break;
24588               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24589                        && OMP_CLAUSE_DECL (c) == decl)
24590                 error_at (loc, "iteration variable %qD "
24591                           "should not be firstprivate",
24592                           decl);
24593               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24594                        && OMP_CLAUSE_DECL (c) == decl)
24595                 error_at (loc, "iteration variable %qD should not be reduction",
24596                           decl);
24597             }
24598           if (c == NULL)
24599             {
24600               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24601               OMP_CLAUSE_DECL (c) = decl;
24602               c = finish_omp_clauses (c);
24603               if (c)
24604                 {
24605                   OMP_CLAUSE_CHAIN (c) = clauses;
24606                   clauses = c;
24607                 }
24608             }
24609         }
24610
24611       cond = NULL;
24612       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24613         cond = cp_parser_omp_for_cond (parser, decl);
24614       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24615
24616       incr = NULL;
24617       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24618         {
24619           /* If decl is an iterator, preserve the operator on decl
24620              until finish_omp_for.  */
24621           if (decl
24622               && (type_dependent_expression_p (decl)
24623                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24624             incr = cp_parser_omp_for_incr (parser, decl);
24625           else
24626             incr = cp_parser_expression (parser, false, NULL);
24627         }
24628
24629       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24630         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24631                                                /*or_comma=*/false,
24632                                                /*consume_paren=*/true);
24633
24634       TREE_VEC_ELT (declv, i) = decl;
24635       TREE_VEC_ELT (initv, i) = init;
24636       TREE_VEC_ELT (condv, i) = cond;
24637       TREE_VEC_ELT (incrv, i) = incr;
24638
24639       if (i == collapse - 1)
24640         break;
24641
24642       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24643          in between the collapsed for loops to be still considered perfectly
24644          nested.  Hopefully the final version clarifies this.
24645          For now handle (multiple) {'s and empty statements.  */
24646       cp_parser_parse_tentatively (parser);
24647       do
24648         {
24649           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24650             break;
24651           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24652             {
24653               cp_lexer_consume_token (parser->lexer);
24654               bracecount++;
24655             }
24656           else if (bracecount
24657                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24658             cp_lexer_consume_token (parser->lexer);
24659           else
24660             {
24661               loc = cp_lexer_peek_token (parser->lexer)->location;
24662               error_at (loc, "not enough collapsed for loops");
24663               collapse_err = true;
24664               cp_parser_abort_tentative_parse (parser);
24665               declv = NULL_TREE;
24666               break;
24667             }
24668         }
24669       while (1);
24670
24671       if (declv)
24672         {
24673           cp_parser_parse_definitely (parser);
24674           nbraces += bracecount;
24675         }
24676     }
24677
24678   /* Note that we saved the original contents of this flag when we entered
24679      the structured block, and so we don't need to re-save it here.  */
24680   parser->in_statement = IN_OMP_FOR;
24681
24682   /* Note that the grammar doesn't call for a structured block here,
24683      though the loop as a whole is a structured block.  */
24684   body = push_stmt_list ();
24685   cp_parser_statement (parser, NULL_TREE, false, NULL);
24686   body = pop_stmt_list (body);
24687
24688   if (declv == NULL_TREE)
24689     ret = NULL_TREE;
24690   else
24691     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24692                           pre_body, clauses);
24693
24694   while (nbraces)
24695     {
24696       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24697         {
24698           cp_lexer_consume_token (parser->lexer);
24699           nbraces--;
24700         }
24701       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24702         cp_lexer_consume_token (parser->lexer);
24703       else
24704         {
24705           if (!collapse_err)
24706             {
24707               error_at (cp_lexer_peek_token (parser->lexer)->location,
24708                         "collapsed loops not perfectly nested");
24709             }
24710           collapse_err = true;
24711           cp_parser_statement_seq_opt (parser, NULL);
24712           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24713             break;
24714         }
24715     }
24716
24717   while (!VEC_empty (tree, for_block))
24718     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24719   release_tree_vector (for_block);
24720
24721   return ret;
24722 }
24723
24724 /* OpenMP 2.5:
24725    #pragma omp for for-clause[optseq] new-line
24726      for-loop  */
24727
24728 #define OMP_FOR_CLAUSE_MASK                             \
24729         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24730         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24731         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24732         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24733         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24734         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24735         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24736         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24737
24738 static tree
24739 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24740 {
24741   tree clauses, sb, ret;
24742   unsigned int save;
24743
24744   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24745                                        "#pragma omp for", pragma_tok);
24746
24747   sb = begin_omp_structured_block ();
24748   save = cp_parser_begin_omp_structured_block (parser);
24749
24750   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24751
24752   cp_parser_end_omp_structured_block (parser, save);
24753   add_stmt (finish_omp_structured_block (sb));
24754
24755   return ret;
24756 }
24757
24758 /* OpenMP 2.5:
24759    # pragma omp master new-line
24760      structured-block  */
24761
24762 static tree
24763 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24764 {
24765   cp_parser_require_pragma_eol (parser, pragma_tok);
24766   return c_finish_omp_master (input_location,
24767                               cp_parser_omp_structured_block (parser));
24768 }
24769
24770 /* OpenMP 2.5:
24771    # pragma omp ordered new-line
24772      structured-block  */
24773
24774 static tree
24775 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24776 {
24777   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24778   cp_parser_require_pragma_eol (parser, pragma_tok);
24779   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24780 }
24781
24782 /* OpenMP 2.5:
24783
24784    section-scope:
24785      { section-sequence }
24786
24787    section-sequence:
24788      section-directive[opt] structured-block
24789      section-sequence section-directive structured-block  */
24790
24791 static tree
24792 cp_parser_omp_sections_scope (cp_parser *parser)
24793 {
24794   tree stmt, substmt;
24795   bool error_suppress = false;
24796   cp_token *tok;
24797
24798   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24799     return NULL_TREE;
24800
24801   stmt = push_stmt_list ();
24802
24803   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24804     {
24805       unsigned save;
24806
24807       substmt = begin_omp_structured_block ();
24808       save = cp_parser_begin_omp_structured_block (parser);
24809
24810       while (1)
24811         {
24812           cp_parser_statement (parser, NULL_TREE, false, NULL);
24813
24814           tok = cp_lexer_peek_token (parser->lexer);
24815           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24816             break;
24817           if (tok->type == CPP_CLOSE_BRACE)
24818             break;
24819           if (tok->type == CPP_EOF)
24820             break;
24821         }
24822
24823       cp_parser_end_omp_structured_block (parser, save);
24824       substmt = finish_omp_structured_block (substmt);
24825       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24826       add_stmt (substmt);
24827     }
24828
24829   while (1)
24830     {
24831       tok = cp_lexer_peek_token (parser->lexer);
24832       if (tok->type == CPP_CLOSE_BRACE)
24833         break;
24834       if (tok->type == CPP_EOF)
24835         break;
24836
24837       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24838         {
24839           cp_lexer_consume_token (parser->lexer);
24840           cp_parser_require_pragma_eol (parser, tok);
24841           error_suppress = false;
24842         }
24843       else if (!error_suppress)
24844         {
24845           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24846           error_suppress = true;
24847         }
24848
24849       substmt = cp_parser_omp_structured_block (parser);
24850       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24851       add_stmt (substmt);
24852     }
24853   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24854
24855   substmt = pop_stmt_list (stmt);
24856
24857   stmt = make_node (OMP_SECTIONS);
24858   TREE_TYPE (stmt) = void_type_node;
24859   OMP_SECTIONS_BODY (stmt) = substmt;
24860
24861   add_stmt (stmt);
24862   return stmt;
24863 }
24864
24865 /* OpenMP 2.5:
24866    # pragma omp sections sections-clause[optseq] newline
24867      sections-scope  */
24868
24869 #define OMP_SECTIONS_CLAUSE_MASK                        \
24870         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24871         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24872         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24873         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24874         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24875
24876 static tree
24877 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24878 {
24879   tree clauses, ret;
24880
24881   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24882                                        "#pragma omp sections", pragma_tok);
24883
24884   ret = cp_parser_omp_sections_scope (parser);
24885   if (ret)
24886     OMP_SECTIONS_CLAUSES (ret) = clauses;
24887
24888   return ret;
24889 }
24890
24891 /* OpenMP 2.5:
24892    # pragma parallel parallel-clause new-line
24893    # pragma parallel for parallel-for-clause new-line
24894    # pragma parallel sections parallel-sections-clause new-line  */
24895
24896 #define OMP_PARALLEL_CLAUSE_MASK                        \
24897         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24898         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24899         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24900         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24901         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24902         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24903         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24904         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24905
24906 static tree
24907 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24908 {
24909   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24910   const char *p_name = "#pragma omp parallel";
24911   tree stmt, clauses, par_clause, ws_clause, block;
24912   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24913   unsigned int save;
24914   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24915
24916   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24917     {
24918       cp_lexer_consume_token (parser->lexer);
24919       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24920       p_name = "#pragma omp parallel for";
24921       mask |= OMP_FOR_CLAUSE_MASK;
24922       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24923     }
24924   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24925     {
24926       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24927       const char *p = IDENTIFIER_POINTER (id);
24928       if (strcmp (p, "sections") == 0)
24929         {
24930           cp_lexer_consume_token (parser->lexer);
24931           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24932           p_name = "#pragma omp parallel sections";
24933           mask |= OMP_SECTIONS_CLAUSE_MASK;
24934           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24935         }
24936     }
24937
24938   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24939   block = begin_omp_parallel ();
24940   save = cp_parser_begin_omp_structured_block (parser);
24941
24942   switch (p_kind)
24943     {
24944     case PRAGMA_OMP_PARALLEL:
24945       cp_parser_statement (parser, NULL_TREE, false, NULL);
24946       par_clause = clauses;
24947       break;
24948
24949     case PRAGMA_OMP_PARALLEL_FOR:
24950       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24951       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24952       break;
24953
24954     case PRAGMA_OMP_PARALLEL_SECTIONS:
24955       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24956       stmt = cp_parser_omp_sections_scope (parser);
24957       if (stmt)
24958         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24959       break;
24960
24961     default:
24962       gcc_unreachable ();
24963     }
24964
24965   cp_parser_end_omp_structured_block (parser, save);
24966   stmt = finish_omp_parallel (par_clause, block);
24967   if (p_kind != PRAGMA_OMP_PARALLEL)
24968     OMP_PARALLEL_COMBINED (stmt) = 1;
24969   return stmt;
24970 }
24971
24972 /* OpenMP 2.5:
24973    # pragma omp single single-clause[optseq] new-line
24974      structured-block  */
24975
24976 #define OMP_SINGLE_CLAUSE_MASK                          \
24977         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24978         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24979         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24980         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24981
24982 static tree
24983 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24984 {
24985   tree stmt = make_node (OMP_SINGLE);
24986   TREE_TYPE (stmt) = void_type_node;
24987
24988   OMP_SINGLE_CLAUSES (stmt)
24989     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24990                                  "#pragma omp single", pragma_tok);
24991   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24992
24993   return add_stmt (stmt);
24994 }
24995
24996 /* OpenMP 3.0:
24997    # pragma omp task task-clause[optseq] new-line
24998      structured-block  */
24999
25000 #define OMP_TASK_CLAUSE_MASK                            \
25001         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25002         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25003         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25004         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25005         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25006         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25007
25008 static tree
25009 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25010 {
25011   tree clauses, block;
25012   unsigned int save;
25013
25014   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25015                                        "#pragma omp task", pragma_tok);
25016   block = begin_omp_task ();
25017   save = cp_parser_begin_omp_structured_block (parser);
25018   cp_parser_statement (parser, NULL_TREE, false, NULL);
25019   cp_parser_end_omp_structured_block (parser, save);
25020   return finish_omp_task (clauses, block);
25021 }
25022
25023 /* OpenMP 3.0:
25024    # pragma omp taskwait new-line  */
25025
25026 static void
25027 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25028 {
25029   cp_parser_require_pragma_eol (parser, pragma_tok);
25030   finish_omp_taskwait ();
25031 }
25032
25033 /* OpenMP 2.5:
25034    # pragma omp threadprivate (variable-list) */
25035
25036 static void
25037 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25038 {
25039   tree vars;
25040
25041   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25042   cp_parser_require_pragma_eol (parser, pragma_tok);
25043
25044   finish_omp_threadprivate (vars);
25045 }
25046
25047 /* Main entry point to OpenMP statement pragmas.  */
25048
25049 static void
25050 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25051 {
25052   tree stmt;
25053
25054   switch (pragma_tok->pragma_kind)
25055     {
25056     case PRAGMA_OMP_ATOMIC:
25057       cp_parser_omp_atomic (parser, pragma_tok);
25058       return;
25059     case PRAGMA_OMP_CRITICAL:
25060       stmt = cp_parser_omp_critical (parser, pragma_tok);
25061       break;
25062     case PRAGMA_OMP_FOR:
25063       stmt = cp_parser_omp_for (parser, pragma_tok);
25064       break;
25065     case PRAGMA_OMP_MASTER:
25066       stmt = cp_parser_omp_master (parser, pragma_tok);
25067       break;
25068     case PRAGMA_OMP_ORDERED:
25069       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25070       break;
25071     case PRAGMA_OMP_PARALLEL:
25072       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25073       break;
25074     case PRAGMA_OMP_SECTIONS:
25075       stmt = cp_parser_omp_sections (parser, pragma_tok);
25076       break;
25077     case PRAGMA_OMP_SINGLE:
25078       stmt = cp_parser_omp_single (parser, pragma_tok);
25079       break;
25080     case PRAGMA_OMP_TASK:
25081       stmt = cp_parser_omp_task (parser, pragma_tok);
25082       break;
25083     default:
25084       gcc_unreachable ();
25085     }
25086
25087   if (stmt)
25088     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25089 }
25090 \f
25091 /* The parser.  */
25092
25093 static GTY (()) cp_parser *the_parser;
25094
25095 \f
25096 /* Special handling for the first token or line in the file.  The first
25097    thing in the file might be #pragma GCC pch_preprocess, which loads a
25098    PCH file, which is a GC collection point.  So we need to handle this
25099    first pragma without benefit of an existing lexer structure.
25100
25101    Always returns one token to the caller in *FIRST_TOKEN.  This is
25102    either the true first token of the file, or the first token after
25103    the initial pragma.  */
25104
25105 static void
25106 cp_parser_initial_pragma (cp_token *first_token)
25107 {
25108   tree name = NULL;
25109
25110   cp_lexer_get_preprocessor_token (NULL, first_token);
25111   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25112     return;
25113
25114   cp_lexer_get_preprocessor_token (NULL, first_token);
25115   if (first_token->type == CPP_STRING)
25116     {
25117       name = first_token->u.value;
25118
25119       cp_lexer_get_preprocessor_token (NULL, first_token);
25120       if (first_token->type != CPP_PRAGMA_EOL)
25121         error_at (first_token->location,
25122                   "junk at end of %<#pragma GCC pch_preprocess%>");
25123     }
25124   else
25125     error_at (first_token->location, "expected string literal");
25126
25127   /* Skip to the end of the pragma.  */
25128   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25129     cp_lexer_get_preprocessor_token (NULL, first_token);
25130
25131   /* Now actually load the PCH file.  */
25132   if (name)
25133     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25134
25135   /* Read one more token to return to our caller.  We have to do this
25136      after reading the PCH file in, since its pointers have to be
25137      live.  */
25138   cp_lexer_get_preprocessor_token (NULL, first_token);
25139 }
25140
25141 /* Normal parsing of a pragma token.  Here we can (and must) use the
25142    regular lexer.  */
25143
25144 static bool
25145 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25146 {
25147   cp_token *pragma_tok;
25148   unsigned int id;
25149
25150   pragma_tok = cp_lexer_consume_token (parser->lexer);
25151   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25152   parser->lexer->in_pragma = true;
25153
25154   id = pragma_tok->pragma_kind;
25155   switch (id)
25156     {
25157     case PRAGMA_GCC_PCH_PREPROCESS:
25158       error_at (pragma_tok->location,
25159                 "%<#pragma GCC pch_preprocess%> must be first");
25160       break;
25161
25162     case PRAGMA_OMP_BARRIER:
25163       switch (context)
25164         {
25165         case pragma_compound:
25166           cp_parser_omp_barrier (parser, pragma_tok);
25167           return false;
25168         case pragma_stmt:
25169           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25170                     "used in compound statements");
25171           break;
25172         default:
25173           goto bad_stmt;
25174         }
25175       break;
25176
25177     case PRAGMA_OMP_FLUSH:
25178       switch (context)
25179         {
25180         case pragma_compound:
25181           cp_parser_omp_flush (parser, pragma_tok);
25182           return false;
25183         case pragma_stmt:
25184           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25185                     "used in compound statements");
25186           break;
25187         default:
25188           goto bad_stmt;
25189         }
25190       break;
25191
25192     case PRAGMA_OMP_TASKWAIT:
25193       switch (context)
25194         {
25195         case pragma_compound:
25196           cp_parser_omp_taskwait (parser, pragma_tok);
25197           return false;
25198         case pragma_stmt:
25199           error_at (pragma_tok->location,
25200                     "%<#pragma omp taskwait%> may only be "
25201                     "used in compound statements");
25202           break;
25203         default:
25204           goto bad_stmt;
25205         }
25206       break;
25207
25208     case PRAGMA_OMP_THREADPRIVATE:
25209       cp_parser_omp_threadprivate (parser, pragma_tok);
25210       return false;
25211
25212     case PRAGMA_OMP_ATOMIC:
25213     case PRAGMA_OMP_CRITICAL:
25214     case PRAGMA_OMP_FOR:
25215     case PRAGMA_OMP_MASTER:
25216     case PRAGMA_OMP_ORDERED:
25217     case PRAGMA_OMP_PARALLEL:
25218     case PRAGMA_OMP_SECTIONS:
25219     case PRAGMA_OMP_SINGLE:
25220     case PRAGMA_OMP_TASK:
25221       if (context == pragma_external)
25222         goto bad_stmt;
25223       cp_parser_omp_construct (parser, pragma_tok);
25224       return true;
25225
25226     case PRAGMA_OMP_SECTION:
25227       error_at (pragma_tok->location, 
25228                 "%<#pragma omp section%> may only be used in "
25229                 "%<#pragma omp sections%> construct");
25230       break;
25231
25232     default:
25233       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25234       c_invoke_pragma_handler (id);
25235       break;
25236
25237     bad_stmt:
25238       cp_parser_error (parser, "expected declaration specifiers");
25239       break;
25240     }
25241
25242   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25243   return false;
25244 }
25245
25246 /* The interface the pragma parsers have to the lexer.  */
25247
25248 enum cpp_ttype
25249 pragma_lex (tree *value)
25250 {
25251   cp_token *tok;
25252   enum cpp_ttype ret;
25253
25254   tok = cp_lexer_peek_token (the_parser->lexer);
25255
25256   ret = tok->type;
25257   *value = tok->u.value;
25258
25259   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25260     ret = CPP_EOF;
25261   else if (ret == CPP_STRING)
25262     *value = cp_parser_string_literal (the_parser, false, false);
25263   else
25264     {
25265       cp_lexer_consume_token (the_parser->lexer);
25266       if (ret == CPP_KEYWORD)
25267         ret = CPP_NAME;
25268     }
25269
25270   return ret;
25271 }
25272
25273 \f
25274 /* External interface.  */
25275
25276 /* Parse one entire translation unit.  */
25277
25278 void
25279 c_parse_file (void)
25280 {
25281   static bool already_called = false;
25282
25283   if (already_called)
25284     {
25285       sorry ("inter-module optimizations not implemented for C++");
25286       return;
25287     }
25288   already_called = true;
25289
25290   the_parser = cp_parser_new ();
25291   push_deferring_access_checks (flag_access_control
25292                                 ? dk_no_deferred : dk_no_check);
25293   cp_parser_translation_unit (the_parser);
25294   the_parser = NULL;
25295 }
25296
25297 #include "gt-cp-parser.h"