OSDN Git Service

Fix PR c++/47311
[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       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3716       return cp_parser_lambda_expression (parser);
3717
3718     case CPP_OBJC_STRING:
3719       if (c_dialect_objc ())
3720         /* We have an Objective-C++ string literal. */
3721         return cp_parser_objc_expression (parser);
3722       cp_parser_error (parser, "expected primary-expression");
3723       return error_mark_node;
3724
3725     case CPP_KEYWORD:
3726       switch (token->keyword)
3727         {
3728           /* These two are the boolean literals.  */
3729         case RID_TRUE:
3730           cp_lexer_consume_token (parser->lexer);
3731           return boolean_true_node;
3732         case RID_FALSE:
3733           cp_lexer_consume_token (parser->lexer);
3734           return boolean_false_node;
3735
3736           /* The `__null' literal.  */
3737         case RID_NULL:
3738           cp_lexer_consume_token (parser->lexer);
3739           return null_node;
3740
3741           /* The `nullptr' literal.  */
3742         case RID_NULLPTR:
3743           cp_lexer_consume_token (parser->lexer);
3744           return nullptr_node;
3745
3746           /* Recognize the `this' keyword.  */
3747         case RID_THIS:
3748           cp_lexer_consume_token (parser->lexer);
3749           if (parser->local_variables_forbidden_p)
3750             {
3751               error_at (token->location,
3752                         "%<this%> may not be used in this context");
3753               return error_mark_node;
3754             }
3755           /* Pointers cannot appear in constant-expressions.  */
3756           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3757             return error_mark_node;
3758           return finish_this_expr ();
3759
3760           /* The `operator' keyword can be the beginning of an
3761              id-expression.  */
3762         case RID_OPERATOR:
3763           goto id_expression;
3764
3765         case RID_FUNCTION_NAME:
3766         case RID_PRETTY_FUNCTION_NAME:
3767         case RID_C99_FUNCTION_NAME:
3768           {
3769             non_integral_constant name;
3770
3771             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3772                __func__ are the names of variables -- but they are
3773                treated specially.  Therefore, they are handled here,
3774                rather than relying on the generic id-expression logic
3775                below.  Grammatically, these names are id-expressions.
3776
3777                Consume the token.  */
3778             token = cp_lexer_consume_token (parser->lexer);
3779
3780             switch (token->keyword)
3781               {
3782               case RID_FUNCTION_NAME:
3783                 name = NIC_FUNC_NAME;
3784                 break;
3785               case RID_PRETTY_FUNCTION_NAME:
3786                 name = NIC_PRETTY_FUNC;
3787                 break;
3788               case RID_C99_FUNCTION_NAME:
3789                 name = NIC_C99_FUNC;
3790                 break;
3791               default:
3792                 gcc_unreachable ();
3793               }
3794
3795             if (cp_parser_non_integral_constant_expression (parser, name))
3796               return error_mark_node;
3797
3798             /* Look up the name.  */
3799             return finish_fname (token->u.value);
3800           }
3801
3802         case RID_VA_ARG:
3803           {
3804             tree expression;
3805             tree type;
3806
3807             /* The `__builtin_va_arg' construct is used to handle
3808                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3809             cp_lexer_consume_token (parser->lexer);
3810             /* Look for the opening `('.  */
3811             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3812             /* Now, parse the assignment-expression.  */
3813             expression = cp_parser_assignment_expression (parser,
3814                                                           /*cast_p=*/false, NULL);
3815             /* Look for the `,'.  */
3816             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3817             /* Parse the type-id.  */
3818             type = cp_parser_type_id (parser);
3819             /* Look for the closing `)'.  */
3820             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3821             /* Using `va_arg' in a constant-expression is not
3822                allowed.  */
3823             if (cp_parser_non_integral_constant_expression (parser,
3824                                                             NIC_VA_ARG))
3825               return error_mark_node;
3826             return build_x_va_arg (expression, type);
3827           }
3828
3829         case RID_OFFSETOF:
3830           return cp_parser_builtin_offsetof (parser);
3831
3832         case RID_HAS_NOTHROW_ASSIGN:
3833         case RID_HAS_NOTHROW_CONSTRUCTOR:
3834         case RID_HAS_NOTHROW_COPY:        
3835         case RID_HAS_TRIVIAL_ASSIGN:
3836         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3837         case RID_HAS_TRIVIAL_COPY:        
3838         case RID_HAS_TRIVIAL_DESTRUCTOR:
3839         case RID_HAS_VIRTUAL_DESTRUCTOR:
3840         case RID_IS_ABSTRACT:
3841         case RID_IS_BASE_OF:
3842         case RID_IS_CLASS:
3843         case RID_IS_CONVERTIBLE_TO:
3844         case RID_IS_EMPTY:
3845         case RID_IS_ENUM:
3846         case RID_IS_POD:
3847         case RID_IS_POLYMORPHIC:
3848         case RID_IS_STD_LAYOUT:
3849         case RID_IS_TRIVIAL:
3850         case RID_IS_UNION:
3851         case RID_IS_LITERAL_TYPE:
3852           return cp_parser_trait_expr (parser, token->keyword);
3853
3854         /* Objective-C++ expressions.  */
3855         case RID_AT_ENCODE:
3856         case RID_AT_PROTOCOL:
3857         case RID_AT_SELECTOR:
3858           return cp_parser_objc_expression (parser);
3859
3860         case RID_TEMPLATE:
3861           if (parser->in_function_body
3862               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3863                   == CPP_LESS))
3864             {
3865               error_at (token->location,
3866                         "a template declaration cannot appear at block scope");
3867               cp_parser_skip_to_end_of_block_or_statement (parser);
3868               return error_mark_node;
3869             }
3870         default:
3871           cp_parser_error (parser, "expected primary-expression");
3872           return error_mark_node;
3873         }
3874
3875       /* An id-expression can start with either an identifier, a
3876          `::' as the beginning of a qualified-id, or the "operator"
3877          keyword.  */
3878     case CPP_NAME:
3879     case CPP_SCOPE:
3880     case CPP_TEMPLATE_ID:
3881     case CPP_NESTED_NAME_SPECIFIER:
3882       {
3883         tree id_expression;
3884         tree decl;
3885         const char *error_msg;
3886         bool template_p;
3887         bool done;
3888         cp_token *id_expr_token;
3889
3890       id_expression:
3891         /* Parse the id-expression.  */
3892         id_expression
3893           = cp_parser_id_expression (parser,
3894                                      /*template_keyword_p=*/false,
3895                                      /*check_dependency_p=*/true,
3896                                      &template_p,
3897                                      /*declarator_p=*/false,
3898                                      /*optional_p=*/false);
3899         if (id_expression == error_mark_node)
3900           return error_mark_node;
3901         id_expr_token = token;
3902         token = cp_lexer_peek_token (parser->lexer);
3903         done = (token->type != CPP_OPEN_SQUARE
3904                 && token->type != CPP_OPEN_PAREN
3905                 && token->type != CPP_DOT
3906                 && token->type != CPP_DEREF
3907                 && token->type != CPP_PLUS_PLUS
3908                 && token->type != CPP_MINUS_MINUS);
3909         /* If we have a template-id, then no further lookup is
3910            required.  If the template-id was for a template-class, we
3911            will sometimes have a TYPE_DECL at this point.  */
3912         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3913                  || TREE_CODE (id_expression) == TYPE_DECL)
3914           decl = id_expression;
3915         /* Look up the name.  */
3916         else
3917           {
3918             tree ambiguous_decls;
3919
3920             /* If we already know that this lookup is ambiguous, then
3921                we've already issued an error message; there's no reason
3922                to check again.  */
3923             if (id_expr_token->type == CPP_NAME
3924                 && id_expr_token->ambiguous_p)
3925               {
3926                 cp_parser_simulate_error (parser);
3927                 return error_mark_node;
3928               }
3929
3930             decl = cp_parser_lookup_name (parser, id_expression,
3931                                           none_type,
3932                                           template_p,
3933                                           /*is_namespace=*/false,
3934                                           /*check_dependency=*/true,
3935                                           &ambiguous_decls,
3936                                           id_expr_token->location);
3937             /* If the lookup was ambiguous, an error will already have
3938                been issued.  */
3939             if (ambiguous_decls)
3940               return error_mark_node;
3941
3942             /* In Objective-C++, we may have an Objective-C 2.0
3943                dot-syntax for classes here.  */
3944             if (c_dialect_objc ()
3945                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3946                 && TREE_CODE (decl) == TYPE_DECL
3947                 && objc_is_class_name (decl))
3948               {
3949                 tree component;
3950                 cp_lexer_consume_token (parser->lexer);
3951                 component = cp_parser_identifier (parser);
3952                 if (component == error_mark_node)
3953                   return error_mark_node;
3954
3955                 return objc_build_class_component_ref (id_expression, component);
3956               }
3957
3958             /* In Objective-C++, an instance variable (ivar) may be preferred
3959                to whatever cp_parser_lookup_name() found.  */
3960             decl = objc_lookup_ivar (decl, id_expression);
3961
3962             /* If name lookup gives us a SCOPE_REF, then the
3963                qualifying scope was dependent.  */
3964             if (TREE_CODE (decl) == SCOPE_REF)
3965               {
3966                 /* At this point, we do not know if DECL is a valid
3967                    integral constant expression.  We assume that it is
3968                    in fact such an expression, so that code like:
3969
3970                       template <int N> struct A {
3971                         int a[B<N>::i];
3972                       };
3973                      
3974                    is accepted.  At template-instantiation time, we
3975                    will check that B<N>::i is actually a constant.  */
3976                 return decl;
3977               }
3978             /* Check to see if DECL is a local variable in a context
3979                where that is forbidden.  */
3980             if (parser->local_variables_forbidden_p
3981                 && local_variable_p (decl))
3982               {
3983                 /* It might be that we only found DECL because we are
3984                    trying to be generous with pre-ISO scoping rules.
3985                    For example, consider:
3986
3987                      int i;
3988                      void g() {
3989                        for (int i = 0; i < 10; ++i) {}
3990                        extern void f(int j = i);
3991                      }
3992
3993                    Here, name look up will originally find the out
3994                    of scope `i'.  We need to issue a warning message,
3995                    but then use the global `i'.  */
3996                 decl = check_for_out_of_scope_variable (decl);
3997                 if (local_variable_p (decl))
3998                   {
3999                     error_at (id_expr_token->location,
4000                               "local variable %qD may not appear in this context",
4001                               decl);
4002                     return error_mark_node;
4003                   }
4004               }
4005           }
4006
4007         decl = (finish_id_expression
4008                 (id_expression, decl, parser->scope,
4009                  idk,
4010                  parser->integral_constant_expression_p,
4011                  parser->allow_non_integral_constant_expression_p,
4012                  &parser->non_integral_constant_expression_p,
4013                  template_p, done, address_p,
4014                  template_arg_p,
4015                  &error_msg,
4016                  id_expr_token->location));
4017         if (error_msg)
4018           cp_parser_error (parser, error_msg);
4019         return decl;
4020       }
4021
4022       /* Anything else is an error.  */
4023     default:
4024       cp_parser_error (parser, "expected primary-expression");
4025       return error_mark_node;
4026     }
4027 }
4028
4029 /* Parse an id-expression.
4030
4031    id-expression:
4032      unqualified-id
4033      qualified-id
4034
4035    qualified-id:
4036      :: [opt] nested-name-specifier template [opt] unqualified-id
4037      :: identifier
4038      :: operator-function-id
4039      :: template-id
4040
4041    Return a representation of the unqualified portion of the
4042    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4043    a `::' or nested-name-specifier.
4044
4045    Often, if the id-expression was a qualified-id, the caller will
4046    want to make a SCOPE_REF to represent the qualified-id.  This
4047    function does not do this in order to avoid wastefully creating
4048    SCOPE_REFs when they are not required.
4049
4050    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4051    `template' keyword.
4052
4053    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4054    uninstantiated templates.
4055
4056    If *TEMPLATE_P is non-NULL, it is set to true iff the
4057    `template' keyword is used to explicitly indicate that the entity
4058    named is a template.
4059
4060    If DECLARATOR_P is true, the id-expression is appearing as part of
4061    a declarator, rather than as part of an expression.  */
4062
4063 static tree
4064 cp_parser_id_expression (cp_parser *parser,
4065                          bool template_keyword_p,
4066                          bool check_dependency_p,
4067                          bool *template_p,
4068                          bool declarator_p,
4069                          bool optional_p)
4070 {
4071   bool global_scope_p;
4072   bool nested_name_specifier_p;
4073
4074   /* Assume the `template' keyword was not used.  */
4075   if (template_p)
4076     *template_p = template_keyword_p;
4077
4078   /* Look for the optional `::' operator.  */
4079   global_scope_p
4080     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4081        != NULL_TREE);
4082   /* Look for the optional nested-name-specifier.  */
4083   nested_name_specifier_p
4084     = (cp_parser_nested_name_specifier_opt (parser,
4085                                             /*typename_keyword_p=*/false,
4086                                             check_dependency_p,
4087                                             /*type_p=*/false,
4088                                             declarator_p)
4089        != NULL_TREE);
4090   /* If there is a nested-name-specifier, then we are looking at
4091      the first qualified-id production.  */
4092   if (nested_name_specifier_p)
4093     {
4094       tree saved_scope;
4095       tree saved_object_scope;
4096       tree saved_qualifying_scope;
4097       tree unqualified_id;
4098       bool is_template;
4099
4100       /* See if the next token is the `template' keyword.  */
4101       if (!template_p)
4102         template_p = &is_template;
4103       *template_p = cp_parser_optional_template_keyword (parser);
4104       /* Name lookup we do during the processing of the
4105          unqualified-id might obliterate SCOPE.  */
4106       saved_scope = parser->scope;
4107       saved_object_scope = parser->object_scope;
4108       saved_qualifying_scope = parser->qualifying_scope;
4109       /* Process the final unqualified-id.  */
4110       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4111                                                  check_dependency_p,
4112                                                  declarator_p,
4113                                                  /*optional_p=*/false);
4114       /* Restore the SAVED_SCOPE for our caller.  */
4115       parser->scope = saved_scope;
4116       parser->object_scope = saved_object_scope;
4117       parser->qualifying_scope = saved_qualifying_scope;
4118
4119       return unqualified_id;
4120     }
4121   /* Otherwise, if we are in global scope, then we are looking at one
4122      of the other qualified-id productions.  */
4123   else if (global_scope_p)
4124     {
4125       cp_token *token;
4126       tree id;
4127
4128       /* Peek at the next token.  */
4129       token = cp_lexer_peek_token (parser->lexer);
4130
4131       /* If it's an identifier, and the next token is not a "<", then
4132          we can avoid the template-id case.  This is an optimization
4133          for this common case.  */
4134       if (token->type == CPP_NAME
4135           && !cp_parser_nth_token_starts_template_argument_list_p
4136                (parser, 2))
4137         return cp_parser_identifier (parser);
4138
4139       cp_parser_parse_tentatively (parser);
4140       /* Try a template-id.  */
4141       id = cp_parser_template_id (parser,
4142                                   /*template_keyword_p=*/false,
4143                                   /*check_dependency_p=*/true,
4144                                   declarator_p);
4145       /* If that worked, we're done.  */
4146       if (cp_parser_parse_definitely (parser))
4147         return id;
4148
4149       /* Peek at the next token.  (Changes in the token buffer may
4150          have invalidated the pointer obtained above.)  */
4151       token = cp_lexer_peek_token (parser->lexer);
4152
4153       switch (token->type)
4154         {
4155         case CPP_NAME:
4156           return cp_parser_identifier (parser);
4157
4158         case CPP_KEYWORD:
4159           if (token->keyword == RID_OPERATOR)
4160             return cp_parser_operator_function_id (parser);
4161           /* Fall through.  */
4162
4163         default:
4164           cp_parser_error (parser, "expected id-expression");
4165           return error_mark_node;
4166         }
4167     }
4168   else
4169     return cp_parser_unqualified_id (parser, template_keyword_p,
4170                                      /*check_dependency_p=*/true,
4171                                      declarator_p,
4172                                      optional_p);
4173 }
4174
4175 /* Parse an unqualified-id.
4176
4177    unqualified-id:
4178      identifier
4179      operator-function-id
4180      conversion-function-id
4181      ~ class-name
4182      template-id
4183
4184    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4185    keyword, in a construct like `A::template ...'.
4186
4187    Returns a representation of unqualified-id.  For the `identifier'
4188    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4189    production a BIT_NOT_EXPR is returned; the operand of the
4190    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4191    other productions, see the documentation accompanying the
4192    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4193    names are looked up in uninstantiated templates.  If DECLARATOR_P
4194    is true, the unqualified-id is appearing as part of a declarator,
4195    rather than as part of an expression.  */
4196
4197 static tree
4198 cp_parser_unqualified_id (cp_parser* parser,
4199                           bool template_keyword_p,
4200                           bool check_dependency_p,
4201                           bool declarator_p,
4202                           bool optional_p)
4203 {
4204   cp_token *token;
4205
4206   /* Peek at the next token.  */
4207   token = cp_lexer_peek_token (parser->lexer);
4208
4209   switch (token->type)
4210     {
4211     case CPP_NAME:
4212       {
4213         tree id;
4214
4215         /* We don't know yet whether or not this will be a
4216            template-id.  */
4217         cp_parser_parse_tentatively (parser);
4218         /* Try a template-id.  */
4219         id = cp_parser_template_id (parser, template_keyword_p,
4220                                     check_dependency_p,
4221                                     declarator_p);
4222         /* If it worked, we're done.  */
4223         if (cp_parser_parse_definitely (parser))
4224           return id;
4225         /* Otherwise, it's an ordinary identifier.  */
4226         return cp_parser_identifier (parser);
4227       }
4228
4229     case CPP_TEMPLATE_ID:
4230       return cp_parser_template_id (parser, template_keyword_p,
4231                                     check_dependency_p,
4232                                     declarator_p);
4233
4234     case CPP_COMPL:
4235       {
4236         tree type_decl;
4237         tree qualifying_scope;
4238         tree object_scope;
4239         tree scope;
4240         bool done;
4241
4242         /* Consume the `~' token.  */
4243         cp_lexer_consume_token (parser->lexer);
4244         /* Parse the class-name.  The standard, as written, seems to
4245            say that:
4246
4247              template <typename T> struct S { ~S (); };
4248              template <typename T> S<T>::~S() {}
4249
4250            is invalid, since `~' must be followed by a class-name, but
4251            `S<T>' is dependent, and so not known to be a class.
4252            That's not right; we need to look in uninstantiated
4253            templates.  A further complication arises from:
4254
4255              template <typename T> void f(T t) {
4256                t.T::~T();
4257              }
4258
4259            Here, it is not possible to look up `T' in the scope of `T'
4260            itself.  We must look in both the current scope, and the
4261            scope of the containing complete expression.
4262
4263            Yet another issue is:
4264
4265              struct S {
4266                int S;
4267                ~S();
4268              };
4269
4270              S::~S() {}
4271
4272            The standard does not seem to say that the `S' in `~S'
4273            should refer to the type `S' and not the data member
4274            `S::S'.  */
4275
4276         /* DR 244 says that we look up the name after the "~" in the
4277            same scope as we looked up the qualifying name.  That idea
4278            isn't fully worked out; it's more complicated than that.  */
4279         scope = parser->scope;
4280         object_scope = parser->object_scope;
4281         qualifying_scope = parser->qualifying_scope;
4282
4283         /* Check for invalid scopes.  */
4284         if (scope == error_mark_node)
4285           {
4286             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4287               cp_lexer_consume_token (parser->lexer);
4288             return error_mark_node;
4289           }
4290         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4291           {
4292             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4293               error_at (token->location,
4294                         "scope %qT before %<~%> is not a class-name",
4295                         scope);
4296             cp_parser_simulate_error (parser);
4297             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4298               cp_lexer_consume_token (parser->lexer);
4299             return error_mark_node;
4300           }
4301         gcc_assert (!scope || TYPE_P (scope));
4302
4303         /* If the name is of the form "X::~X" it's OK even if X is a
4304            typedef.  */
4305         token = cp_lexer_peek_token (parser->lexer);
4306         if (scope
4307             && token->type == CPP_NAME
4308             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4309                 != CPP_LESS)
4310             && (token->u.value == TYPE_IDENTIFIER (scope)
4311                 || constructor_name_p (token->u.value, scope)))
4312           {
4313             cp_lexer_consume_token (parser->lexer);
4314             return build_nt (BIT_NOT_EXPR, scope);
4315           }
4316
4317         /* If there was an explicit qualification (S::~T), first look
4318            in the scope given by the qualification (i.e., S).
4319
4320            Note: in the calls to cp_parser_class_name below we pass
4321            typename_type so that lookup finds the injected-class-name
4322            rather than the constructor.  */
4323         done = false;
4324         type_decl = NULL_TREE;
4325         if (scope)
4326           {
4327             cp_parser_parse_tentatively (parser);
4328             type_decl = cp_parser_class_name (parser,
4329                                               /*typename_keyword_p=*/false,
4330                                               /*template_keyword_p=*/false,
4331                                               typename_type,
4332                                               /*check_dependency=*/false,
4333                                               /*class_head_p=*/false,
4334                                               declarator_p);
4335             if (cp_parser_parse_definitely (parser))
4336               done = true;
4337           }
4338         /* In "N::S::~S", look in "N" as well.  */
4339         if (!done && scope && qualifying_scope)
4340           {
4341             cp_parser_parse_tentatively (parser);
4342             parser->scope = qualifying_scope;
4343             parser->object_scope = NULL_TREE;
4344             parser->qualifying_scope = NULL_TREE;
4345             type_decl
4346               = cp_parser_class_name (parser,
4347                                       /*typename_keyword_p=*/false,
4348                                       /*template_keyword_p=*/false,
4349                                       typename_type,
4350                                       /*check_dependency=*/false,
4351                                       /*class_head_p=*/false,
4352                                       declarator_p);
4353             if (cp_parser_parse_definitely (parser))
4354               done = true;
4355           }
4356         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4357         else if (!done && object_scope)
4358           {
4359             cp_parser_parse_tentatively (parser);
4360             parser->scope = object_scope;
4361             parser->object_scope = NULL_TREE;
4362             parser->qualifying_scope = NULL_TREE;
4363             type_decl
4364               = cp_parser_class_name (parser,
4365                                       /*typename_keyword_p=*/false,
4366                                       /*template_keyword_p=*/false,
4367                                       typename_type,
4368                                       /*check_dependency=*/false,
4369                                       /*class_head_p=*/false,
4370                                       declarator_p);
4371             if (cp_parser_parse_definitely (parser))
4372               done = true;
4373           }
4374         /* Look in the surrounding context.  */
4375         if (!done)
4376           {
4377             parser->scope = NULL_TREE;
4378             parser->object_scope = NULL_TREE;
4379             parser->qualifying_scope = NULL_TREE;
4380             if (processing_template_decl)
4381               cp_parser_parse_tentatively (parser);
4382             type_decl
4383               = cp_parser_class_name (parser,
4384                                       /*typename_keyword_p=*/false,
4385                                       /*template_keyword_p=*/false,
4386                                       typename_type,
4387                                       /*check_dependency=*/false,
4388                                       /*class_head_p=*/false,
4389                                       declarator_p);
4390             if (processing_template_decl
4391                 && ! cp_parser_parse_definitely (parser))
4392               {
4393                 /* We couldn't find a type with this name, so just accept
4394                    it and check for a match at instantiation time.  */
4395                 type_decl = cp_parser_identifier (parser);
4396                 if (type_decl != error_mark_node)
4397                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4398                 return type_decl;
4399               }
4400           }
4401         /* If an error occurred, assume that the name of the
4402            destructor is the same as the name of the qualifying
4403            class.  That allows us to keep parsing after running
4404            into ill-formed destructor names.  */
4405         if (type_decl == error_mark_node && scope)
4406           return build_nt (BIT_NOT_EXPR, scope);
4407         else if (type_decl == error_mark_node)
4408           return error_mark_node;
4409
4410         /* Check that destructor name and scope match.  */
4411         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4412           {
4413             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4414               error_at (token->location,
4415                         "declaration of %<~%T%> as member of %qT",
4416                         type_decl, scope);
4417             cp_parser_simulate_error (parser);
4418             return error_mark_node;
4419           }
4420
4421         /* [class.dtor]
4422
4423            A typedef-name that names a class shall not be used as the
4424            identifier in the declarator for a destructor declaration.  */
4425         if (declarator_p
4426             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4427             && !DECL_SELF_REFERENCE_P (type_decl)
4428             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4429           error_at (token->location,
4430                     "typedef-name %qD used as destructor declarator",
4431                     type_decl);
4432
4433         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4434       }
4435
4436     case CPP_KEYWORD:
4437       if (token->keyword == RID_OPERATOR)
4438         {
4439           tree id;
4440
4441           /* This could be a template-id, so we try that first.  */
4442           cp_parser_parse_tentatively (parser);
4443           /* Try a template-id.  */
4444           id = cp_parser_template_id (parser, template_keyword_p,
4445                                       /*check_dependency_p=*/true,
4446                                       declarator_p);
4447           /* If that worked, we're done.  */
4448           if (cp_parser_parse_definitely (parser))
4449             return id;
4450           /* We still don't know whether we're looking at an
4451              operator-function-id or a conversion-function-id.  */
4452           cp_parser_parse_tentatively (parser);
4453           /* Try an operator-function-id.  */
4454           id = cp_parser_operator_function_id (parser);
4455           /* If that didn't work, try a conversion-function-id.  */
4456           if (!cp_parser_parse_definitely (parser))
4457             id = cp_parser_conversion_function_id (parser);
4458
4459           return id;
4460         }
4461       /* Fall through.  */
4462
4463     default:
4464       if (optional_p)
4465         return NULL_TREE;
4466       cp_parser_error (parser, "expected unqualified-id");
4467       return error_mark_node;
4468     }
4469 }
4470
4471 /* Parse an (optional) nested-name-specifier.
4472
4473    nested-name-specifier: [C++98]
4474      class-or-namespace-name :: nested-name-specifier [opt]
4475      class-or-namespace-name :: template nested-name-specifier [opt]
4476
4477    nested-name-specifier: [C++0x]
4478      type-name ::
4479      namespace-name ::
4480      nested-name-specifier identifier ::
4481      nested-name-specifier template [opt] simple-template-id ::
4482
4483    PARSER->SCOPE should be set appropriately before this function is
4484    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4485    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4486    in name lookups.
4487
4488    Sets PARSER->SCOPE to the class (TYPE) or namespace
4489    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4490    it unchanged if there is no nested-name-specifier.  Returns the new
4491    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4492
4493    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4494    part of a declaration and/or decl-specifier.  */
4495
4496 static tree
4497 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4498                                      bool typename_keyword_p,
4499                                      bool check_dependency_p,
4500                                      bool type_p,
4501                                      bool is_declaration)
4502 {
4503   bool success = false;
4504   cp_token_position start = 0;
4505   cp_token *token;
4506
4507   /* Remember where the nested-name-specifier starts.  */
4508   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4509     {
4510       start = cp_lexer_token_position (parser->lexer, false);
4511       push_deferring_access_checks (dk_deferred);
4512     }
4513
4514   while (true)
4515     {
4516       tree new_scope;
4517       tree old_scope;
4518       tree saved_qualifying_scope;
4519       bool template_keyword_p;
4520
4521       /* Spot cases that cannot be the beginning of a
4522          nested-name-specifier.  */
4523       token = cp_lexer_peek_token (parser->lexer);
4524
4525       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4526          the already parsed nested-name-specifier.  */
4527       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4528         {
4529           /* Grab the nested-name-specifier and continue the loop.  */
4530           cp_parser_pre_parsed_nested_name_specifier (parser);
4531           /* If we originally encountered this nested-name-specifier
4532              with IS_DECLARATION set to false, we will not have
4533              resolved TYPENAME_TYPEs, so we must do so here.  */
4534           if (is_declaration
4535               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4536             {
4537               new_scope = resolve_typename_type (parser->scope,
4538                                                  /*only_current_p=*/false);
4539               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4540                 parser->scope = new_scope;
4541             }
4542           success = true;
4543           continue;
4544         }
4545
4546       /* Spot cases that cannot be the beginning of a
4547          nested-name-specifier.  On the second and subsequent times
4548          through the loop, we look for the `template' keyword.  */
4549       if (success && token->keyword == RID_TEMPLATE)
4550         ;
4551       /* A template-id can start a nested-name-specifier.  */
4552       else if (token->type == CPP_TEMPLATE_ID)
4553         ;
4554       else
4555         {
4556           /* If the next token is not an identifier, then it is
4557              definitely not a type-name or namespace-name.  */
4558           if (token->type != CPP_NAME)
4559             break;
4560           /* If the following token is neither a `<' (to begin a
4561              template-id), nor a `::', then we are not looking at a
4562              nested-name-specifier.  */
4563           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4564
4565           if (token->type == CPP_COLON
4566               && parser->colon_corrects_to_scope_p
4567               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4568             {
4569               error_at (token->location,
4570                         "found %<:%> in nested-name-specifier, expected %<::%>");
4571               token->type = CPP_SCOPE;
4572             }
4573
4574           if (token->type != CPP_SCOPE
4575               && !cp_parser_nth_token_starts_template_argument_list_p
4576                   (parser, 2))
4577             break;
4578         }
4579
4580       /* The nested-name-specifier is optional, so we parse
4581          tentatively.  */
4582       cp_parser_parse_tentatively (parser);
4583
4584       /* Look for the optional `template' keyword, if this isn't the
4585          first time through the loop.  */
4586       if (success)
4587         template_keyword_p = cp_parser_optional_template_keyword (parser);
4588       else
4589         template_keyword_p = false;
4590
4591       /* Save the old scope since the name lookup we are about to do
4592          might destroy it.  */
4593       old_scope = parser->scope;
4594       saved_qualifying_scope = parser->qualifying_scope;
4595       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4596          look up names in "X<T>::I" in order to determine that "Y" is
4597          a template.  So, if we have a typename at this point, we make
4598          an effort to look through it.  */
4599       if (is_declaration
4600           && !typename_keyword_p
4601           && parser->scope
4602           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4603         parser->scope = resolve_typename_type (parser->scope,
4604                                                /*only_current_p=*/false);
4605       /* Parse the qualifying entity.  */
4606       new_scope
4607         = cp_parser_qualifying_entity (parser,
4608                                        typename_keyword_p,
4609                                        template_keyword_p,
4610                                        check_dependency_p,
4611                                        type_p,
4612                                        is_declaration);
4613       /* Look for the `::' token.  */
4614       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4615
4616       /* If we found what we wanted, we keep going; otherwise, we're
4617          done.  */
4618       if (!cp_parser_parse_definitely (parser))
4619         {
4620           bool error_p = false;
4621
4622           /* Restore the OLD_SCOPE since it was valid before the
4623              failed attempt at finding the last
4624              class-or-namespace-name.  */
4625           parser->scope = old_scope;
4626           parser->qualifying_scope = saved_qualifying_scope;
4627           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4628             break;
4629           /* If the next token is an identifier, and the one after
4630              that is a `::', then any valid interpretation would have
4631              found a class-or-namespace-name.  */
4632           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4633                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4634                      == CPP_SCOPE)
4635                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4636                      != CPP_COMPL))
4637             {
4638               token = cp_lexer_consume_token (parser->lexer);
4639               if (!error_p)
4640                 {
4641                   if (!token->ambiguous_p)
4642                     {
4643                       tree decl;
4644                       tree ambiguous_decls;
4645
4646                       decl = cp_parser_lookup_name (parser, token->u.value,
4647                                                     none_type,
4648                                                     /*is_template=*/false,
4649                                                     /*is_namespace=*/false,
4650                                                     /*check_dependency=*/true,
4651                                                     &ambiguous_decls,
4652                                                     token->location);
4653                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4654                         error_at (token->location,
4655                                   "%qD used without template parameters",
4656                                   decl);
4657                       else if (ambiguous_decls)
4658                         {
4659                           error_at (token->location,
4660                                     "reference to %qD is ambiguous",
4661                                     token->u.value);
4662                           print_candidates (ambiguous_decls);
4663                           decl = error_mark_node;
4664                         }
4665                       else
4666                         {
4667                           if (cxx_dialect != cxx98)
4668                             cp_parser_name_lookup_error
4669                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4670                              token->location);
4671                           else
4672                             cp_parser_name_lookup_error
4673                             (parser, token->u.value, decl, NLE_CXX98,
4674                              token->location);
4675                         }
4676                     }
4677                   parser->scope = error_mark_node;
4678                   error_p = true;
4679                   /* Treat this as a successful nested-name-specifier
4680                      due to:
4681
4682                      [basic.lookup.qual]
4683
4684                      If the name found is not a class-name (clause
4685                      _class_) or namespace-name (_namespace.def_), the
4686                      program is ill-formed.  */
4687                   success = true;
4688                 }
4689               cp_lexer_consume_token (parser->lexer);
4690             }
4691           break;
4692         }
4693       /* We've found one valid nested-name-specifier.  */
4694       success = true;
4695       /* Name lookup always gives us a DECL.  */
4696       if (TREE_CODE (new_scope) == TYPE_DECL)
4697         new_scope = TREE_TYPE (new_scope);
4698       /* Uses of "template" must be followed by actual templates.  */
4699       if (template_keyword_p
4700           && !(CLASS_TYPE_P (new_scope)
4701                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4702                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4703                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4704           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4705                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4706                    == TEMPLATE_ID_EXPR)))
4707         permerror (input_location, TYPE_P (new_scope)
4708                    ? "%qT is not a template"
4709                    : "%qD is not a template",
4710                    new_scope);
4711       /* If it is a class scope, try to complete it; we are about to
4712          be looking up names inside the class.  */
4713       if (TYPE_P (new_scope)
4714           /* Since checking types for dependency can be expensive,
4715              avoid doing it if the type is already complete.  */
4716           && !COMPLETE_TYPE_P (new_scope)
4717           /* Do not try to complete dependent types.  */
4718           && !dependent_type_p (new_scope))
4719         {
4720           new_scope = complete_type (new_scope);
4721           /* If it is a typedef to current class, use the current
4722              class instead, as the typedef won't have any names inside
4723              it yet.  */
4724           if (!COMPLETE_TYPE_P (new_scope)
4725               && currently_open_class (new_scope))
4726             new_scope = TYPE_MAIN_VARIANT (new_scope);
4727         }
4728       /* Make sure we look in the right scope the next time through
4729          the loop.  */
4730       parser->scope = new_scope;
4731     }
4732
4733   /* If parsing tentatively, replace the sequence of tokens that makes
4734      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4735      token.  That way, should we re-parse the token stream, we will
4736      not have to repeat the effort required to do the parse, nor will
4737      we issue duplicate error messages.  */
4738   if (success && start)
4739     {
4740       cp_token *token;
4741
4742       token = cp_lexer_token_at (parser->lexer, start);
4743       /* Reset the contents of the START token.  */
4744       token->type = CPP_NESTED_NAME_SPECIFIER;
4745       /* Retrieve any deferred checks.  Do not pop this access checks yet
4746          so the memory will not be reclaimed during token replacing below.  */
4747       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4748       token->u.tree_check_value->value = parser->scope;
4749       token->u.tree_check_value->checks = get_deferred_access_checks ();
4750       token->u.tree_check_value->qualifying_scope =
4751         parser->qualifying_scope;
4752       token->keyword = RID_MAX;
4753
4754       /* Purge all subsequent tokens.  */
4755       cp_lexer_purge_tokens_after (parser->lexer, start);
4756     }
4757
4758   if (start)
4759     pop_to_parent_deferring_access_checks ();
4760
4761   return success ? parser->scope : NULL_TREE;
4762 }
4763
4764 /* Parse a nested-name-specifier.  See
4765    cp_parser_nested_name_specifier_opt for details.  This function
4766    behaves identically, except that it will an issue an error if no
4767    nested-name-specifier is present.  */
4768
4769 static tree
4770 cp_parser_nested_name_specifier (cp_parser *parser,
4771                                  bool typename_keyword_p,
4772                                  bool check_dependency_p,
4773                                  bool type_p,
4774                                  bool is_declaration)
4775 {
4776   tree scope;
4777
4778   /* Look for the nested-name-specifier.  */
4779   scope = cp_parser_nested_name_specifier_opt (parser,
4780                                                typename_keyword_p,
4781                                                check_dependency_p,
4782                                                type_p,
4783                                                is_declaration);
4784   /* If it was not present, issue an error message.  */
4785   if (!scope)
4786     {
4787       cp_parser_error (parser, "expected nested-name-specifier");
4788       parser->scope = NULL_TREE;
4789     }
4790
4791   return scope;
4792 }
4793
4794 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4795    this is either a class-name or a namespace-name (which corresponds
4796    to the class-or-namespace-name production in the grammar). For
4797    C++0x, it can also be a type-name that refers to an enumeration
4798    type.
4799
4800    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4801    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4802    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4803    TYPE_P is TRUE iff the next name should be taken as a class-name,
4804    even the same name is declared to be another entity in the same
4805    scope.
4806
4807    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4808    specified by the class-or-namespace-name.  If neither is found the
4809    ERROR_MARK_NODE is returned.  */
4810
4811 static tree
4812 cp_parser_qualifying_entity (cp_parser *parser,
4813                              bool typename_keyword_p,
4814                              bool template_keyword_p,
4815                              bool check_dependency_p,
4816                              bool type_p,
4817                              bool is_declaration)
4818 {
4819   tree saved_scope;
4820   tree saved_qualifying_scope;
4821   tree saved_object_scope;
4822   tree scope;
4823   bool only_class_p;
4824   bool successful_parse_p;
4825
4826   /* Before we try to parse the class-name, we must save away the
4827      current PARSER->SCOPE since cp_parser_class_name will destroy
4828      it.  */
4829   saved_scope = parser->scope;
4830   saved_qualifying_scope = parser->qualifying_scope;
4831   saved_object_scope = parser->object_scope;
4832   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4833      there is no need to look for a namespace-name.  */
4834   only_class_p = template_keyword_p 
4835     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4836   if (!only_class_p)
4837     cp_parser_parse_tentatively (parser);
4838   scope = cp_parser_class_name (parser,
4839                                 typename_keyword_p,
4840                                 template_keyword_p,
4841                                 type_p ? class_type : none_type,
4842                                 check_dependency_p,
4843                                 /*class_head_p=*/false,
4844                                 is_declaration);
4845   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4846   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4847   if (!only_class_p 
4848       && cxx_dialect != cxx98
4849       && !successful_parse_p)
4850     {
4851       /* Restore the saved scope.  */
4852       parser->scope = saved_scope;
4853       parser->qualifying_scope = saved_qualifying_scope;
4854       parser->object_scope = saved_object_scope;
4855
4856       /* Parse tentatively.  */
4857       cp_parser_parse_tentatively (parser);
4858      
4859       /* Parse a typedef-name or enum-name.  */
4860       scope = cp_parser_nonclass_name (parser);
4861
4862       /* "If the name found does not designate a namespace or a class,
4863          enumeration, or dependent type, the program is ill-formed."
4864
4865          We cover classes and dependent types above and namespaces below,
4866          so this code is only looking for enums.  */
4867       if (!scope || TREE_CODE (scope) != TYPE_DECL
4868           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4869         cp_parser_simulate_error (parser);
4870
4871       successful_parse_p = cp_parser_parse_definitely (parser);
4872     }
4873   /* If that didn't work, try for a namespace-name.  */
4874   if (!only_class_p && !successful_parse_p)
4875     {
4876       /* Restore the saved scope.  */
4877       parser->scope = saved_scope;
4878       parser->qualifying_scope = saved_qualifying_scope;
4879       parser->object_scope = saved_object_scope;
4880       /* If we are not looking at an identifier followed by the scope
4881          resolution operator, then this is not part of a
4882          nested-name-specifier.  (Note that this function is only used
4883          to parse the components of a nested-name-specifier.)  */
4884       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4885           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4886         return error_mark_node;
4887       scope = cp_parser_namespace_name (parser);
4888     }
4889
4890   return scope;
4891 }
4892
4893 /* Parse a postfix-expression.
4894
4895    postfix-expression:
4896      primary-expression
4897      postfix-expression [ expression ]
4898      postfix-expression ( expression-list [opt] )
4899      simple-type-specifier ( expression-list [opt] )
4900      typename :: [opt] nested-name-specifier identifier
4901        ( expression-list [opt] )
4902      typename :: [opt] nested-name-specifier template [opt] template-id
4903        ( expression-list [opt] )
4904      postfix-expression . template [opt] id-expression
4905      postfix-expression -> template [opt] id-expression
4906      postfix-expression . pseudo-destructor-name
4907      postfix-expression -> pseudo-destructor-name
4908      postfix-expression ++
4909      postfix-expression --
4910      dynamic_cast < type-id > ( expression )
4911      static_cast < type-id > ( expression )
4912      reinterpret_cast < type-id > ( expression )
4913      const_cast < type-id > ( expression )
4914      typeid ( expression )
4915      typeid ( type-id )
4916
4917    GNU Extension:
4918
4919    postfix-expression:
4920      ( type-id ) { initializer-list , [opt] }
4921
4922    This extension is a GNU version of the C99 compound-literal
4923    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4924    but they are essentially the same concept.)
4925
4926    If ADDRESS_P is true, the postfix expression is the operand of the
4927    `&' operator.  CAST_P is true if this expression is the target of a
4928    cast.
4929
4930    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4931    class member access expressions [expr.ref].
4932
4933    Returns a representation of the expression.  */
4934
4935 static tree
4936 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4937                               bool member_access_only_p,
4938                               cp_id_kind * pidk_return)
4939 {
4940   cp_token *token;
4941   enum rid keyword;
4942   cp_id_kind idk = CP_ID_KIND_NONE;
4943   tree postfix_expression = NULL_TREE;
4944   bool is_member_access = false;
4945
4946   /* Peek at the next token.  */
4947   token = cp_lexer_peek_token (parser->lexer);
4948   /* Some of the productions are determined by keywords.  */
4949   keyword = token->keyword;
4950   switch (keyword)
4951     {
4952     case RID_DYNCAST:
4953     case RID_STATCAST:
4954     case RID_REINTCAST:
4955     case RID_CONSTCAST:
4956       {
4957         tree type;
4958         tree expression;
4959         const char *saved_message;
4960
4961         /* All of these can be handled in the same way from the point
4962            of view of parsing.  Begin by consuming the token
4963            identifying the cast.  */
4964         cp_lexer_consume_token (parser->lexer);
4965
4966         /* New types cannot be defined in the cast.  */
4967         saved_message = parser->type_definition_forbidden_message;
4968         parser->type_definition_forbidden_message
4969           = G_("types may not be defined in casts");
4970
4971         /* Look for the opening `<'.  */
4972         cp_parser_require (parser, CPP_LESS, RT_LESS);
4973         /* Parse the type to which we are casting.  */
4974         type = cp_parser_type_id (parser);
4975         /* Look for the closing `>'.  */
4976         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4977         /* Restore the old message.  */
4978         parser->type_definition_forbidden_message = saved_message;
4979
4980         /* And the expression which is being cast.  */
4981         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4982         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4983         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4984
4985         /* Only type conversions to integral or enumeration types
4986            can be used in constant-expressions.  */
4987         if (!cast_valid_in_integral_constant_expression_p (type)
4988             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4989           return error_mark_node;
4990
4991         switch (keyword)
4992           {
4993           case RID_DYNCAST:
4994             postfix_expression
4995               = build_dynamic_cast (type, expression, tf_warning_or_error);
4996             break;
4997           case RID_STATCAST:
4998             postfix_expression
4999               = build_static_cast (type, expression, tf_warning_or_error);
5000             break;
5001           case RID_REINTCAST:
5002             postfix_expression
5003               = build_reinterpret_cast (type, expression, 
5004                                         tf_warning_or_error);
5005             break;
5006           case RID_CONSTCAST:
5007             postfix_expression
5008               = build_const_cast (type, expression, tf_warning_or_error);
5009             break;
5010           default:
5011             gcc_unreachable ();
5012           }
5013       }
5014       break;
5015
5016     case RID_TYPEID:
5017       {
5018         tree type;
5019         const char *saved_message;
5020         bool saved_in_type_id_in_expr_p;
5021
5022         /* Consume the `typeid' token.  */
5023         cp_lexer_consume_token (parser->lexer);
5024         /* Look for the `(' token.  */
5025         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5026         /* Types cannot be defined in a `typeid' expression.  */
5027         saved_message = parser->type_definition_forbidden_message;
5028         parser->type_definition_forbidden_message
5029           = G_("types may not be defined in a %<typeid%> expression");
5030         /* We can't be sure yet whether we're looking at a type-id or an
5031            expression.  */
5032         cp_parser_parse_tentatively (parser);
5033         /* Try a type-id first.  */
5034         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5035         parser->in_type_id_in_expr_p = true;
5036         type = cp_parser_type_id (parser);
5037         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5038         /* Look for the `)' token.  Otherwise, we can't be sure that
5039            we're not looking at an expression: consider `typeid (int
5040            (3))', for example.  */
5041         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5042         /* If all went well, simply lookup the type-id.  */
5043         if (cp_parser_parse_definitely (parser))
5044           postfix_expression = get_typeid (type);
5045         /* Otherwise, fall back to the expression variant.  */
5046         else
5047           {
5048             tree expression;
5049
5050             /* Look for an expression.  */
5051             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5052             /* Compute its typeid.  */
5053             postfix_expression = build_typeid (expression);
5054             /* Look for the `)' token.  */
5055             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5056           }
5057         /* Restore the saved message.  */
5058         parser->type_definition_forbidden_message = saved_message;
5059         /* `typeid' may not appear in an integral constant expression.  */
5060         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5061           return error_mark_node;
5062       }
5063       break;
5064
5065     case RID_TYPENAME:
5066       {
5067         tree type;
5068         /* The syntax permitted here is the same permitted for an
5069            elaborated-type-specifier.  */
5070         type = cp_parser_elaborated_type_specifier (parser,
5071                                                     /*is_friend=*/false,
5072                                                     /*is_declaration=*/false);
5073         postfix_expression = cp_parser_functional_cast (parser, type);
5074       }
5075       break;
5076
5077     default:
5078       {
5079         tree type;
5080
5081         /* If the next thing is a simple-type-specifier, we may be
5082            looking at a functional cast.  We could also be looking at
5083            an id-expression.  So, we try the functional cast, and if
5084            that doesn't work we fall back to the primary-expression.  */
5085         cp_parser_parse_tentatively (parser);
5086         /* Look for the simple-type-specifier.  */
5087         type = cp_parser_simple_type_specifier (parser,
5088                                                 /*decl_specs=*/NULL,
5089                                                 CP_PARSER_FLAGS_NONE);
5090         /* Parse the cast itself.  */
5091         if (!cp_parser_error_occurred (parser))
5092           postfix_expression
5093             = cp_parser_functional_cast (parser, type);
5094         /* If that worked, we're done.  */
5095         if (cp_parser_parse_definitely (parser))
5096           break;
5097
5098         /* If the functional-cast didn't work out, try a
5099            compound-literal.  */
5100         if (cp_parser_allow_gnu_extensions_p (parser)
5101             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5102           {
5103             VEC(constructor_elt,gc) *initializer_list = NULL;
5104             bool saved_in_type_id_in_expr_p;
5105
5106             cp_parser_parse_tentatively (parser);
5107             /* Consume the `('.  */
5108             cp_lexer_consume_token (parser->lexer);
5109             /* Parse the type.  */
5110             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5111             parser->in_type_id_in_expr_p = true;
5112             type = cp_parser_type_id (parser);
5113             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5114             /* Look for the `)'.  */
5115             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5116             /* Look for the `{'.  */
5117             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5118             /* If things aren't going well, there's no need to
5119                keep going.  */
5120             if (!cp_parser_error_occurred (parser))
5121               {
5122                 bool non_constant_p;
5123                 /* Parse the initializer-list.  */
5124                 initializer_list
5125                   = cp_parser_initializer_list (parser, &non_constant_p);
5126                 /* Allow a trailing `,'.  */
5127                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5128                   cp_lexer_consume_token (parser->lexer);
5129                 /* Look for the final `}'.  */
5130                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5131               }
5132             /* If that worked, we're definitely looking at a
5133                compound-literal expression.  */
5134             if (cp_parser_parse_definitely (parser))
5135               {
5136                 /* Warn the user that a compound literal is not
5137                    allowed in standard C++.  */
5138                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5139                 /* For simplicity, we disallow compound literals in
5140                    constant-expressions.  We could
5141                    allow compound literals of integer type, whose
5142                    initializer was a constant, in constant
5143                    expressions.  Permitting that usage, as a further
5144                    extension, would not change the meaning of any
5145                    currently accepted programs.  (Of course, as
5146                    compound literals are not part of ISO C++, the
5147                    standard has nothing to say.)  */
5148                 if (cp_parser_non_integral_constant_expression (parser,
5149                                                                 NIC_NCC))
5150                   {
5151                     postfix_expression = error_mark_node;
5152                     break;
5153                   }
5154                 /* Form the representation of the compound-literal.  */
5155                 postfix_expression
5156                   = (finish_compound_literal
5157                      (type, build_constructor (init_list_type_node,
5158                                                initializer_list)));
5159                 break;
5160               }
5161           }
5162
5163         /* It must be a primary-expression.  */
5164         postfix_expression
5165           = cp_parser_primary_expression (parser, address_p, cast_p,
5166                                           /*template_arg_p=*/false,
5167                                           &idk);
5168       }
5169       break;
5170     }
5171
5172   /* Keep looping until the postfix-expression is complete.  */
5173   while (true)
5174     {
5175       if (idk == CP_ID_KIND_UNQUALIFIED
5176           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5177           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5178         /* It is not a Koenig lookup function call.  */
5179         postfix_expression
5180           = unqualified_name_lookup_error (postfix_expression);
5181
5182       /* Peek at the next token.  */
5183       token = cp_lexer_peek_token (parser->lexer);
5184
5185       switch (token->type)
5186         {
5187         case CPP_OPEN_SQUARE:
5188           postfix_expression
5189             = cp_parser_postfix_open_square_expression (parser,
5190                                                         postfix_expression,
5191                                                         false);
5192           idk = CP_ID_KIND_NONE;
5193           is_member_access = false;
5194           break;
5195
5196         case CPP_OPEN_PAREN:
5197           /* postfix-expression ( expression-list [opt] ) */
5198           {
5199             bool koenig_p;
5200             bool is_builtin_constant_p;
5201             bool saved_integral_constant_expression_p = false;
5202             bool saved_non_integral_constant_expression_p = false;
5203             VEC(tree,gc) *args;
5204
5205             is_member_access = false;
5206
5207             is_builtin_constant_p
5208               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5209             if (is_builtin_constant_p)
5210               {
5211                 /* The whole point of __builtin_constant_p is to allow
5212                    non-constant expressions to appear as arguments.  */
5213                 saved_integral_constant_expression_p
5214                   = parser->integral_constant_expression_p;
5215                 saved_non_integral_constant_expression_p
5216                   = parser->non_integral_constant_expression_p;
5217                 parser->integral_constant_expression_p = false;
5218               }
5219             args = (cp_parser_parenthesized_expression_list
5220                     (parser, non_attr,
5221                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5222                      /*non_constant_p=*/NULL));
5223             if (is_builtin_constant_p)
5224               {
5225                 parser->integral_constant_expression_p
5226                   = saved_integral_constant_expression_p;
5227                 parser->non_integral_constant_expression_p
5228                   = saved_non_integral_constant_expression_p;
5229               }
5230
5231             if (args == NULL)
5232               {
5233                 postfix_expression = error_mark_node;
5234                 break;
5235               }
5236
5237             /* Function calls are not permitted in
5238                constant-expressions.  */
5239             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5240                 && cp_parser_non_integral_constant_expression (parser,
5241                                                                NIC_FUNC_CALL))
5242               {
5243                 postfix_expression = error_mark_node;
5244                 release_tree_vector (args);
5245                 break;
5246               }
5247
5248             koenig_p = false;
5249             if (idk == CP_ID_KIND_UNQUALIFIED
5250                 || idk == CP_ID_KIND_TEMPLATE_ID)
5251               {
5252                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5253                   {
5254                     if (!VEC_empty (tree, args))
5255                       {
5256                         koenig_p = true;
5257                         if (!any_type_dependent_arguments_p (args))
5258                           postfix_expression
5259                             = perform_koenig_lookup (postfix_expression, args,
5260                                                      /*include_std=*/false);
5261                       }
5262                     else
5263                       postfix_expression
5264                         = unqualified_fn_lookup_error (postfix_expression);
5265                   }
5266                 /* We do not perform argument-dependent lookup if
5267                    normal lookup finds a non-function, in accordance
5268                    with the expected resolution of DR 218.  */
5269                 else if (!VEC_empty (tree, args)
5270                          && is_overloaded_fn (postfix_expression))
5271                   {
5272                     tree fn = get_first_fn (postfix_expression);
5273                     fn = STRIP_TEMPLATE (fn);
5274
5275                     /* Do not do argument dependent lookup if regular
5276                        lookup finds a member function or a block-scope
5277                        function declaration.  [basic.lookup.argdep]/3  */
5278                     if (!DECL_FUNCTION_MEMBER_P (fn)
5279                         && !DECL_LOCAL_FUNCTION_P (fn))
5280                       {
5281                         koenig_p = true;
5282                         if (!any_type_dependent_arguments_p (args))
5283                           postfix_expression
5284                             = perform_koenig_lookup (postfix_expression, args,
5285                                                      /*include_std=*/false);
5286                       }
5287                   }
5288               }
5289
5290             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5291               {
5292                 tree instance = TREE_OPERAND (postfix_expression, 0);
5293                 tree fn = TREE_OPERAND (postfix_expression, 1);
5294
5295                 if (processing_template_decl
5296                     && (type_dependent_expression_p (instance)
5297                         || (!BASELINK_P (fn)
5298                             && TREE_CODE (fn) != FIELD_DECL)
5299                         || type_dependent_expression_p (fn)
5300                         || any_type_dependent_arguments_p (args)))
5301                   {
5302                     postfix_expression
5303                       = build_nt_call_vec (postfix_expression, args);
5304                     release_tree_vector (args);
5305                     break;
5306                   }
5307
5308                 if (BASELINK_P (fn))
5309                   {
5310                   postfix_expression
5311                     = (build_new_method_call
5312                        (instance, fn, &args, NULL_TREE,
5313                         (idk == CP_ID_KIND_QUALIFIED
5314                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5315                         /*fn_p=*/NULL,
5316                         tf_warning_or_error));
5317                   }
5318                 else
5319                   postfix_expression
5320                     = finish_call_expr (postfix_expression, &args,
5321                                         /*disallow_virtual=*/false,
5322                                         /*koenig_p=*/false,
5323                                         tf_warning_or_error);
5324               }
5325             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5326                      || TREE_CODE (postfix_expression) == MEMBER_REF
5327                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5328               postfix_expression = (build_offset_ref_call_from_tree
5329                                     (postfix_expression, &args));
5330             else if (idk == CP_ID_KIND_QUALIFIED)
5331               /* A call to a static class member, or a namespace-scope
5332                  function.  */
5333               postfix_expression
5334                 = finish_call_expr (postfix_expression, &args,
5335                                     /*disallow_virtual=*/true,
5336                                     koenig_p,
5337                                     tf_warning_or_error);
5338             else
5339               /* All other function calls.  */
5340               postfix_expression
5341                 = finish_call_expr (postfix_expression, &args,
5342                                     /*disallow_virtual=*/false,
5343                                     koenig_p,
5344                                     tf_warning_or_error);
5345
5346             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5347             idk = CP_ID_KIND_NONE;
5348
5349             release_tree_vector (args);
5350           }
5351           break;
5352
5353         case CPP_DOT:
5354         case CPP_DEREF:
5355           /* postfix-expression . template [opt] id-expression
5356              postfix-expression . pseudo-destructor-name
5357              postfix-expression -> template [opt] id-expression
5358              postfix-expression -> pseudo-destructor-name */
5359
5360           /* Consume the `.' or `->' operator.  */
5361           cp_lexer_consume_token (parser->lexer);
5362
5363           postfix_expression
5364             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5365                                                       postfix_expression,
5366                                                       false, &idk,
5367                                                       token->location);
5368
5369           is_member_access = true;
5370           break;
5371
5372         case CPP_PLUS_PLUS:
5373           /* postfix-expression ++  */
5374           /* Consume the `++' token.  */
5375           cp_lexer_consume_token (parser->lexer);
5376           /* Generate a representation for the complete expression.  */
5377           postfix_expression
5378             = finish_increment_expr (postfix_expression,
5379                                      POSTINCREMENT_EXPR);
5380           /* Increments may not appear in constant-expressions.  */
5381           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5382             postfix_expression = error_mark_node;
5383           idk = CP_ID_KIND_NONE;
5384           is_member_access = false;
5385           break;
5386
5387         case CPP_MINUS_MINUS:
5388           /* postfix-expression -- */
5389           /* Consume the `--' token.  */
5390           cp_lexer_consume_token (parser->lexer);
5391           /* Generate a representation for the complete expression.  */
5392           postfix_expression
5393             = finish_increment_expr (postfix_expression,
5394                                      POSTDECREMENT_EXPR);
5395           /* Decrements may not appear in constant-expressions.  */
5396           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5397             postfix_expression = error_mark_node;
5398           idk = CP_ID_KIND_NONE;
5399           is_member_access = false;
5400           break;
5401
5402         default:
5403           if (pidk_return != NULL)
5404             * pidk_return = idk;
5405           if (member_access_only_p)
5406             return is_member_access? postfix_expression : error_mark_node;
5407           else
5408             return postfix_expression;
5409         }
5410     }
5411
5412   /* We should never get here.  */
5413   gcc_unreachable ();
5414   return error_mark_node;
5415 }
5416
5417 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5418    by cp_parser_builtin_offsetof.  We're looking for
5419
5420      postfix-expression [ expression ]
5421
5422    FOR_OFFSETOF is set if we're being called in that context, which
5423    changes how we deal with integer constant expressions.  */
5424
5425 static tree
5426 cp_parser_postfix_open_square_expression (cp_parser *parser,
5427                                           tree postfix_expression,
5428                                           bool for_offsetof)
5429 {
5430   tree index;
5431
5432   /* Consume the `[' token.  */
5433   cp_lexer_consume_token (parser->lexer);
5434
5435   /* Parse the index expression.  */
5436   /* ??? For offsetof, there is a question of what to allow here.  If
5437      offsetof is not being used in an integral constant expression context,
5438      then we *could* get the right answer by computing the value at runtime.
5439      If we are in an integral constant expression context, then we might
5440      could accept any constant expression; hard to say without analysis.
5441      Rather than open the barn door too wide right away, allow only integer
5442      constant expressions here.  */
5443   if (for_offsetof)
5444     index = cp_parser_constant_expression (parser, false, NULL);
5445   else
5446     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5447
5448   /* Look for the closing `]'.  */
5449   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5450
5451   /* Build the ARRAY_REF.  */
5452   postfix_expression = grok_array_decl (postfix_expression, index);
5453
5454   /* When not doing offsetof, array references are not permitted in
5455      constant-expressions.  */
5456   if (!for_offsetof
5457       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5458     postfix_expression = error_mark_node;
5459
5460   return postfix_expression;
5461 }
5462
5463 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5464    by cp_parser_builtin_offsetof.  We're looking for
5465
5466      postfix-expression . template [opt] id-expression
5467      postfix-expression . pseudo-destructor-name
5468      postfix-expression -> template [opt] id-expression
5469      postfix-expression -> pseudo-destructor-name
5470
5471    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5472    limits what of the above we'll actually accept, but nevermind.
5473    TOKEN_TYPE is the "." or "->" token, which will already have been
5474    removed from the stream.  */
5475
5476 static tree
5477 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5478                                         enum cpp_ttype token_type,
5479                                         tree postfix_expression,
5480                                         bool for_offsetof, cp_id_kind *idk,
5481                                         location_t location)
5482 {
5483   tree name;
5484   bool dependent_p;
5485   bool pseudo_destructor_p;
5486   tree scope = NULL_TREE;
5487
5488   /* If this is a `->' operator, dereference the pointer.  */
5489   if (token_type == CPP_DEREF)
5490     postfix_expression = build_x_arrow (postfix_expression);
5491   /* Check to see whether or not the expression is type-dependent.  */
5492   dependent_p = type_dependent_expression_p (postfix_expression);
5493   /* The identifier following the `->' or `.' is not qualified.  */
5494   parser->scope = NULL_TREE;
5495   parser->qualifying_scope = NULL_TREE;
5496   parser->object_scope = NULL_TREE;
5497   *idk = CP_ID_KIND_NONE;
5498
5499   /* Enter the scope corresponding to the type of the object
5500      given by the POSTFIX_EXPRESSION.  */
5501   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5502     {
5503       scope = TREE_TYPE (postfix_expression);
5504       /* According to the standard, no expression should ever have
5505          reference type.  Unfortunately, we do not currently match
5506          the standard in this respect in that our internal representation
5507          of an expression may have reference type even when the standard
5508          says it does not.  Therefore, we have to manually obtain the
5509          underlying type here.  */
5510       scope = non_reference (scope);
5511       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5512       if (scope == unknown_type_node)
5513         {
5514           error_at (location, "%qE does not have class type",
5515                     postfix_expression);
5516           scope = NULL_TREE;
5517         }
5518       else
5519         scope = complete_type_or_else (scope, NULL_TREE);
5520       /* Let the name lookup machinery know that we are processing a
5521          class member access expression.  */
5522       parser->context->object_type = scope;
5523       /* If something went wrong, we want to be able to discern that case,
5524          as opposed to the case where there was no SCOPE due to the type
5525          of expression being dependent.  */
5526       if (!scope)
5527         scope = error_mark_node;
5528       /* If the SCOPE was erroneous, make the various semantic analysis
5529          functions exit quickly -- and without issuing additional error
5530          messages.  */
5531       if (scope == error_mark_node)
5532         postfix_expression = error_mark_node;
5533     }
5534
5535   /* Assume this expression is not a pseudo-destructor access.  */
5536   pseudo_destructor_p = false;
5537
5538   /* If the SCOPE is a scalar type, then, if this is a valid program,
5539      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5540      is type dependent, it can be pseudo-destructor-name or something else.
5541      Try to parse it as pseudo-destructor-name first.  */
5542   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5543     {
5544       tree s;
5545       tree type;
5546
5547       cp_parser_parse_tentatively (parser);
5548       /* Parse the pseudo-destructor-name.  */
5549       s = NULL_TREE;
5550       cp_parser_pseudo_destructor_name (parser, &s, &type);
5551       if (dependent_p
5552           && (cp_parser_error_occurred (parser)
5553               || TREE_CODE (type) != TYPE_DECL
5554               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5555         cp_parser_abort_tentative_parse (parser);
5556       else if (cp_parser_parse_definitely (parser))
5557         {
5558           pseudo_destructor_p = true;
5559           postfix_expression
5560             = finish_pseudo_destructor_expr (postfix_expression,
5561                                              s, TREE_TYPE (type));
5562         }
5563     }
5564
5565   if (!pseudo_destructor_p)
5566     {
5567       /* If the SCOPE is not a scalar type, we are looking at an
5568          ordinary class member access expression, rather than a
5569          pseudo-destructor-name.  */
5570       bool template_p;
5571       cp_token *token = cp_lexer_peek_token (parser->lexer);
5572       /* Parse the id-expression.  */
5573       name = (cp_parser_id_expression
5574               (parser,
5575                cp_parser_optional_template_keyword (parser),
5576                /*check_dependency_p=*/true,
5577                &template_p,
5578                /*declarator_p=*/false,
5579                /*optional_p=*/false));
5580       /* In general, build a SCOPE_REF if the member name is qualified.
5581          However, if the name was not dependent and has already been
5582          resolved; there is no need to build the SCOPE_REF.  For example;
5583
5584              struct X { void f(); };
5585              template <typename T> void f(T* t) { t->X::f(); }
5586
5587          Even though "t" is dependent, "X::f" is not and has been resolved
5588          to a BASELINK; there is no need to include scope information.  */
5589
5590       /* But we do need to remember that there was an explicit scope for
5591          virtual function calls.  */
5592       if (parser->scope)
5593         *idk = CP_ID_KIND_QUALIFIED;
5594
5595       /* If the name is a template-id that names a type, we will get a
5596          TYPE_DECL here.  That is invalid code.  */
5597       if (TREE_CODE (name) == TYPE_DECL)
5598         {
5599           error_at (token->location, "invalid use of %qD", name);
5600           postfix_expression = error_mark_node;
5601         }
5602       else
5603         {
5604           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5605             {
5606               name = build_qualified_name (/*type=*/NULL_TREE,
5607                                            parser->scope,
5608                                            name,
5609                                            template_p);
5610               parser->scope = NULL_TREE;
5611               parser->qualifying_scope = NULL_TREE;
5612               parser->object_scope = NULL_TREE;
5613             }
5614           if (scope && name && BASELINK_P (name))
5615             adjust_result_of_qualified_name_lookup
5616               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5617           postfix_expression
5618             = finish_class_member_access_expr (postfix_expression, name,
5619                                                template_p, 
5620                                                tf_warning_or_error);
5621         }
5622     }
5623
5624   /* We no longer need to look up names in the scope of the object on
5625      the left-hand side of the `.' or `->' operator.  */
5626   parser->context->object_type = NULL_TREE;
5627
5628   /* Outside of offsetof, these operators may not appear in
5629      constant-expressions.  */
5630   if (!for_offsetof
5631       && (cp_parser_non_integral_constant_expression
5632           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5633     postfix_expression = error_mark_node;
5634
5635   return postfix_expression;
5636 }
5637
5638 /* Parse a parenthesized expression-list.
5639
5640    expression-list:
5641      assignment-expression
5642      expression-list, assignment-expression
5643
5644    attribute-list:
5645      expression-list
5646      identifier
5647      identifier, expression-list
5648
5649    CAST_P is true if this expression is the target of a cast.
5650
5651    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5652    argument pack.
5653
5654    Returns a vector of trees.  Each element is a representation of an
5655    assignment-expression.  NULL is returned if the ( and or ) are
5656    missing.  An empty, but allocated, vector is returned on no
5657    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5658    if we are parsing an attribute list for an attribute that wants a
5659    plain identifier argument, normal_attr for an attribute that wants
5660    an expression, or non_attr if we aren't parsing an attribute list.  If
5661    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5662    not all of the expressions in the list were constant.  */
5663
5664 static VEC(tree,gc) *
5665 cp_parser_parenthesized_expression_list (cp_parser* parser,
5666                                          int is_attribute_list,
5667                                          bool cast_p,
5668                                          bool allow_expansion_p,
5669                                          bool *non_constant_p)
5670 {
5671   VEC(tree,gc) *expression_list;
5672   bool fold_expr_p = is_attribute_list != non_attr;
5673   tree identifier = NULL_TREE;
5674   bool saved_greater_than_is_operator_p;
5675
5676   /* Assume all the expressions will be constant.  */
5677   if (non_constant_p)
5678     *non_constant_p = false;
5679
5680   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5681     return NULL;
5682
5683   expression_list = make_tree_vector ();
5684
5685   /* Within a parenthesized expression, a `>' token is always
5686      the greater-than operator.  */
5687   saved_greater_than_is_operator_p
5688     = parser->greater_than_is_operator_p;
5689   parser->greater_than_is_operator_p = true;
5690
5691   /* Consume expressions until there are no more.  */
5692   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5693     while (true)
5694       {
5695         tree expr;
5696
5697         /* At the beginning of attribute lists, check to see if the
5698            next token is an identifier.  */
5699         if (is_attribute_list == id_attr
5700             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5701           {
5702             cp_token *token;
5703
5704             /* Consume the identifier.  */
5705             token = cp_lexer_consume_token (parser->lexer);
5706             /* Save the identifier.  */
5707             identifier = token->u.value;
5708           }
5709         else
5710           {
5711             bool expr_non_constant_p;
5712
5713             /* Parse the next assignment-expression.  */
5714             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5715               {
5716                 /* A braced-init-list.  */
5717                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5718                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5719                 if (non_constant_p && expr_non_constant_p)
5720                   *non_constant_p = true;
5721               }
5722             else if (non_constant_p)
5723               {
5724                 expr = (cp_parser_constant_expression
5725                         (parser, /*allow_non_constant_p=*/true,
5726                          &expr_non_constant_p));
5727                 if (expr_non_constant_p)
5728                   *non_constant_p = true;
5729               }
5730             else
5731               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5732
5733             if (fold_expr_p)
5734               expr = fold_non_dependent_expr (expr);
5735
5736             /* If we have an ellipsis, then this is an expression
5737                expansion.  */
5738             if (allow_expansion_p
5739                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5740               {
5741                 /* Consume the `...'.  */
5742                 cp_lexer_consume_token (parser->lexer);
5743
5744                 /* Build the argument pack.  */
5745                 expr = make_pack_expansion (expr);
5746               }
5747
5748              /* Add it to the list.  We add error_mark_node
5749                 expressions to the list, so that we can still tell if
5750                 the correct form for a parenthesized expression-list
5751                 is found. That gives better errors.  */
5752             VEC_safe_push (tree, gc, expression_list, expr);
5753
5754             if (expr == error_mark_node)
5755               goto skip_comma;
5756           }
5757
5758         /* After the first item, attribute lists look the same as
5759            expression lists.  */
5760         is_attribute_list = non_attr;
5761
5762       get_comma:;
5763         /* If the next token isn't a `,', then we are done.  */
5764         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5765           break;
5766
5767         /* Otherwise, consume the `,' and keep going.  */
5768         cp_lexer_consume_token (parser->lexer);
5769       }
5770
5771   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5772     {
5773       int ending;
5774
5775     skip_comma:;
5776       /* We try and resync to an unnested comma, as that will give the
5777          user better diagnostics.  */
5778       ending = cp_parser_skip_to_closing_parenthesis (parser,
5779                                                       /*recovering=*/true,
5780                                                       /*or_comma=*/true,
5781                                                       /*consume_paren=*/true);
5782       if (ending < 0)
5783         goto get_comma;
5784       if (!ending)
5785         {
5786           parser->greater_than_is_operator_p
5787             = saved_greater_than_is_operator_p;
5788           return NULL;
5789         }
5790     }
5791
5792   parser->greater_than_is_operator_p
5793     = saved_greater_than_is_operator_p;
5794
5795   if (identifier)
5796     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5797
5798   return expression_list;
5799 }
5800
5801 /* Parse a pseudo-destructor-name.
5802
5803    pseudo-destructor-name:
5804      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5805      :: [opt] nested-name-specifier template template-id :: ~ type-name
5806      :: [opt] nested-name-specifier [opt] ~ type-name
5807
5808    If either of the first two productions is used, sets *SCOPE to the
5809    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5810    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5811    or ERROR_MARK_NODE if the parse fails.  */
5812
5813 static void
5814 cp_parser_pseudo_destructor_name (cp_parser* parser,
5815                                   tree* scope,
5816                                   tree* type)
5817 {
5818   bool nested_name_specifier_p;
5819
5820   /* Assume that things will not work out.  */
5821   *type = error_mark_node;
5822
5823   /* Look for the optional `::' operator.  */
5824   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5825   /* Look for the optional nested-name-specifier.  */
5826   nested_name_specifier_p
5827     = (cp_parser_nested_name_specifier_opt (parser,
5828                                             /*typename_keyword_p=*/false,
5829                                             /*check_dependency_p=*/true,
5830                                             /*type_p=*/false,
5831                                             /*is_declaration=*/false)
5832        != NULL_TREE);
5833   /* Now, if we saw a nested-name-specifier, we might be doing the
5834      second production.  */
5835   if (nested_name_specifier_p
5836       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5837     {
5838       /* Consume the `template' keyword.  */
5839       cp_lexer_consume_token (parser->lexer);
5840       /* Parse the template-id.  */
5841       cp_parser_template_id (parser,
5842                              /*template_keyword_p=*/true,
5843                              /*check_dependency_p=*/false,
5844                              /*is_declaration=*/true);
5845       /* Look for the `::' token.  */
5846       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5847     }
5848   /* If the next token is not a `~', then there might be some
5849      additional qualification.  */
5850   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5851     {
5852       /* At this point, we're looking for "type-name :: ~".  The type-name
5853          must not be a class-name, since this is a pseudo-destructor.  So,
5854          it must be either an enum-name, or a typedef-name -- both of which
5855          are just identifiers.  So, we peek ahead to check that the "::"
5856          and "~" tokens are present; if they are not, then we can avoid
5857          calling type_name.  */
5858       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5859           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5860           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5861         {
5862           cp_parser_error (parser, "non-scalar type");
5863           return;
5864         }
5865
5866       /* Look for the type-name.  */
5867       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5868       if (*scope == error_mark_node)
5869         return;
5870
5871       /* Look for the `::' token.  */
5872       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5873     }
5874   else
5875     *scope = NULL_TREE;
5876
5877   /* Look for the `~'.  */
5878   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5879   /* Look for the type-name again.  We are not responsible for
5880      checking that it matches the first type-name.  */
5881   *type = cp_parser_nonclass_name (parser);
5882 }
5883
5884 /* Parse a unary-expression.
5885
5886    unary-expression:
5887      postfix-expression
5888      ++ cast-expression
5889      -- cast-expression
5890      unary-operator cast-expression
5891      sizeof unary-expression
5892      sizeof ( type-id )
5893      alignof ( type-id )  [C++0x]
5894      new-expression
5895      delete-expression
5896
5897    GNU Extensions:
5898
5899    unary-expression:
5900      __extension__ cast-expression
5901      __alignof__ unary-expression
5902      __alignof__ ( type-id )
5903      alignof unary-expression  [C++0x]
5904      __real__ cast-expression
5905      __imag__ cast-expression
5906      && identifier
5907
5908    ADDRESS_P is true iff the unary-expression is appearing as the
5909    operand of the `&' operator.   CAST_P is true if this expression is
5910    the target of a cast.
5911
5912    Returns a representation of the expression.  */
5913
5914 static tree
5915 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5916                             cp_id_kind * pidk)
5917 {
5918   cp_token *token;
5919   enum tree_code unary_operator;
5920
5921   /* Peek at the next token.  */
5922   token = cp_lexer_peek_token (parser->lexer);
5923   /* Some keywords give away the kind of expression.  */
5924   if (token->type == CPP_KEYWORD)
5925     {
5926       enum rid keyword = token->keyword;
5927
5928       switch (keyword)
5929         {
5930         case RID_ALIGNOF:
5931         case RID_SIZEOF:
5932           {
5933             tree operand;
5934             enum tree_code op;
5935
5936             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5937             /* Consume the token.  */
5938             cp_lexer_consume_token (parser->lexer);
5939             /* Parse the operand.  */
5940             operand = cp_parser_sizeof_operand (parser, keyword);
5941
5942             if (TYPE_P (operand))
5943               return cxx_sizeof_or_alignof_type (operand, op, true);
5944             else
5945               {
5946                 /* ISO C++ defines alignof only with types, not with
5947                    expressions. So pedwarn if alignof is used with a non-
5948                    type expression. However, __alignof__ is ok.  */
5949                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5950                   pedwarn (token->location, OPT_pedantic,
5951                            "ISO C++ does not allow %<alignof%> "
5952                            "with a non-type");
5953
5954                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5955               }
5956           }
5957
5958         case RID_NEW:
5959           return cp_parser_new_expression (parser);
5960
5961         case RID_DELETE:
5962           return cp_parser_delete_expression (parser);
5963
5964         case RID_EXTENSION:
5965           {
5966             /* The saved value of the PEDANTIC flag.  */
5967             int saved_pedantic;
5968             tree expr;
5969
5970             /* Save away the PEDANTIC flag.  */
5971             cp_parser_extension_opt (parser, &saved_pedantic);
5972             /* Parse the cast-expression.  */
5973             expr = cp_parser_simple_cast_expression (parser);
5974             /* Restore the PEDANTIC flag.  */
5975             pedantic = saved_pedantic;
5976
5977             return expr;
5978           }
5979
5980         case RID_REALPART:
5981         case RID_IMAGPART:
5982           {
5983             tree expression;
5984
5985             /* Consume the `__real__' or `__imag__' token.  */
5986             cp_lexer_consume_token (parser->lexer);
5987             /* Parse the cast-expression.  */
5988             expression = cp_parser_simple_cast_expression (parser);
5989             /* Create the complete representation.  */
5990             return build_x_unary_op ((keyword == RID_REALPART
5991                                       ? REALPART_EXPR : IMAGPART_EXPR),
5992                                      expression,
5993                                      tf_warning_or_error);
5994           }
5995           break;
5996
5997         case RID_NOEXCEPT:
5998           {
5999             tree expr;
6000             const char *saved_message;
6001             bool saved_integral_constant_expression_p;
6002             bool saved_non_integral_constant_expression_p;
6003             bool saved_greater_than_is_operator_p;
6004
6005             cp_lexer_consume_token (parser->lexer);
6006             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6007
6008             saved_message = parser->type_definition_forbidden_message;
6009             parser->type_definition_forbidden_message
6010               = G_("types may not be defined in %<noexcept%> expressions");
6011
6012             saved_integral_constant_expression_p
6013               = parser->integral_constant_expression_p;
6014             saved_non_integral_constant_expression_p
6015               = parser->non_integral_constant_expression_p;
6016             parser->integral_constant_expression_p = false;
6017
6018             saved_greater_than_is_operator_p
6019               = parser->greater_than_is_operator_p;
6020             parser->greater_than_is_operator_p = true;
6021
6022             ++cp_unevaluated_operand;
6023             ++c_inhibit_evaluation_warnings;
6024             expr = cp_parser_expression (parser, false, NULL);
6025             --c_inhibit_evaluation_warnings;
6026             --cp_unevaluated_operand;
6027
6028             parser->greater_than_is_operator_p
6029               = saved_greater_than_is_operator_p;
6030
6031             parser->integral_constant_expression_p
6032               = saved_integral_constant_expression_p;
6033             parser->non_integral_constant_expression_p
6034               = saved_non_integral_constant_expression_p;
6035
6036             parser->type_definition_forbidden_message = saved_message;
6037
6038             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6039             return finish_noexcept_expr (expr, tf_warning_or_error);
6040           }
6041
6042         default:
6043           break;
6044         }
6045     }
6046
6047   /* Look for the `:: new' and `:: delete', which also signal the
6048      beginning of a new-expression, or delete-expression,
6049      respectively.  If the next token is `::', then it might be one of
6050      these.  */
6051   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6052     {
6053       enum rid keyword;
6054
6055       /* See if the token after the `::' is one of the keywords in
6056          which we're interested.  */
6057       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6058       /* If it's `new', we have a new-expression.  */
6059       if (keyword == RID_NEW)
6060         return cp_parser_new_expression (parser);
6061       /* Similarly, for `delete'.  */
6062       else if (keyword == RID_DELETE)
6063         return cp_parser_delete_expression (parser);
6064     }
6065
6066   /* Look for a unary operator.  */
6067   unary_operator = cp_parser_unary_operator (token);
6068   /* The `++' and `--' operators can be handled similarly, even though
6069      they are not technically unary-operators in the grammar.  */
6070   if (unary_operator == ERROR_MARK)
6071     {
6072       if (token->type == CPP_PLUS_PLUS)
6073         unary_operator = PREINCREMENT_EXPR;
6074       else if (token->type == CPP_MINUS_MINUS)
6075         unary_operator = PREDECREMENT_EXPR;
6076       /* Handle the GNU address-of-label extension.  */
6077       else if (cp_parser_allow_gnu_extensions_p (parser)
6078                && token->type == CPP_AND_AND)
6079         {
6080           tree identifier;
6081           tree expression;
6082           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6083
6084           /* Consume the '&&' token.  */
6085           cp_lexer_consume_token (parser->lexer);
6086           /* Look for the identifier.  */
6087           identifier = cp_parser_identifier (parser);
6088           /* Create an expression representing the address.  */
6089           expression = finish_label_address_expr (identifier, loc);
6090           if (cp_parser_non_integral_constant_expression (parser,
6091                                                           NIC_ADDR_LABEL))
6092             expression = error_mark_node;
6093           return expression;
6094         }
6095     }
6096   if (unary_operator != ERROR_MARK)
6097     {
6098       tree cast_expression;
6099       tree expression = error_mark_node;
6100       non_integral_constant non_constant_p = NIC_NONE;
6101
6102       /* Consume the operator token.  */
6103       token = cp_lexer_consume_token (parser->lexer);
6104       /* Parse the cast-expression.  */
6105       cast_expression
6106         = cp_parser_cast_expression (parser,
6107                                      unary_operator == ADDR_EXPR,
6108                                      /*cast_p=*/false, pidk);
6109       /* Now, build an appropriate representation.  */
6110       switch (unary_operator)
6111         {
6112         case INDIRECT_REF:
6113           non_constant_p = NIC_STAR;
6114           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6115                                              tf_warning_or_error);
6116           break;
6117
6118         case ADDR_EXPR:
6119            non_constant_p = NIC_ADDR;
6120           /* Fall through.  */
6121         case BIT_NOT_EXPR:
6122           expression = build_x_unary_op (unary_operator, cast_expression,
6123                                          tf_warning_or_error);
6124           break;
6125
6126         case PREINCREMENT_EXPR:
6127         case PREDECREMENT_EXPR:
6128           non_constant_p = unary_operator == PREINCREMENT_EXPR
6129                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6130           /* Fall through.  */
6131         case UNARY_PLUS_EXPR:
6132         case NEGATE_EXPR:
6133         case TRUTH_NOT_EXPR:
6134           expression = finish_unary_op_expr (unary_operator, cast_expression);
6135           break;
6136
6137         default:
6138           gcc_unreachable ();
6139         }
6140
6141       if (non_constant_p != NIC_NONE
6142           && cp_parser_non_integral_constant_expression (parser,
6143                                                          non_constant_p))
6144         expression = error_mark_node;
6145
6146       return expression;
6147     }
6148
6149   return cp_parser_postfix_expression (parser, address_p, cast_p,
6150                                        /*member_access_only_p=*/false,
6151                                        pidk);
6152 }
6153
6154 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6155    unary-operator, the corresponding tree code is returned.  */
6156
6157 static enum tree_code
6158 cp_parser_unary_operator (cp_token* token)
6159 {
6160   switch (token->type)
6161     {
6162     case CPP_MULT:
6163       return INDIRECT_REF;
6164
6165     case CPP_AND:
6166       return ADDR_EXPR;
6167
6168     case CPP_PLUS:
6169       return UNARY_PLUS_EXPR;
6170
6171     case CPP_MINUS:
6172       return NEGATE_EXPR;
6173
6174     case CPP_NOT:
6175       return TRUTH_NOT_EXPR;
6176
6177     case CPP_COMPL:
6178       return BIT_NOT_EXPR;
6179
6180     default:
6181       return ERROR_MARK;
6182     }
6183 }
6184
6185 /* Parse a new-expression.
6186
6187    new-expression:
6188      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6189      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6190
6191    Returns a representation of the expression.  */
6192
6193 static tree
6194 cp_parser_new_expression (cp_parser* parser)
6195 {
6196   bool global_scope_p;
6197   VEC(tree,gc) *placement;
6198   tree type;
6199   VEC(tree,gc) *initializer;
6200   tree nelts;
6201   tree ret;
6202
6203   /* Look for the optional `::' operator.  */
6204   global_scope_p
6205     = (cp_parser_global_scope_opt (parser,
6206                                    /*current_scope_valid_p=*/false)
6207        != NULL_TREE);
6208   /* Look for the `new' operator.  */
6209   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6210   /* There's no easy way to tell a new-placement from the
6211      `( type-id )' construct.  */
6212   cp_parser_parse_tentatively (parser);
6213   /* Look for a new-placement.  */
6214   placement = cp_parser_new_placement (parser);
6215   /* If that didn't work out, there's no new-placement.  */
6216   if (!cp_parser_parse_definitely (parser))
6217     {
6218       if (placement != NULL)
6219         release_tree_vector (placement);
6220       placement = NULL;
6221     }
6222
6223   /* If the next token is a `(', then we have a parenthesized
6224      type-id.  */
6225   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6226     {
6227       cp_token *token;
6228       /* Consume the `('.  */
6229       cp_lexer_consume_token (parser->lexer);
6230       /* Parse the type-id.  */
6231       type = cp_parser_type_id (parser);
6232       /* Look for the closing `)'.  */
6233       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6234       token = cp_lexer_peek_token (parser->lexer);
6235       /* There should not be a direct-new-declarator in this production,
6236          but GCC used to allowed this, so we check and emit a sensible error
6237          message for this case.  */
6238       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6239         {
6240           error_at (token->location,
6241                     "array bound forbidden after parenthesized type-id");
6242           inform (token->location, 
6243                   "try removing the parentheses around the type-id");
6244           cp_parser_direct_new_declarator (parser);
6245         }
6246       nelts = NULL_TREE;
6247     }
6248   /* Otherwise, there must be a new-type-id.  */
6249   else
6250     type = cp_parser_new_type_id (parser, &nelts);
6251
6252   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6253   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6254       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6255     initializer = cp_parser_new_initializer (parser);
6256   else
6257     initializer = NULL;
6258
6259   /* A new-expression may not appear in an integral constant
6260      expression.  */
6261   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6262     ret = error_mark_node;
6263   else
6264     {
6265       /* Create a representation of the new-expression.  */
6266       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6267                        tf_warning_or_error);
6268     }
6269
6270   if (placement != NULL)
6271     release_tree_vector (placement);
6272   if (initializer != NULL)
6273     release_tree_vector (initializer);
6274
6275   return ret;
6276 }
6277
6278 /* Parse a new-placement.
6279
6280    new-placement:
6281      ( expression-list )
6282
6283    Returns the same representation as for an expression-list.  */
6284
6285 static VEC(tree,gc) *
6286 cp_parser_new_placement (cp_parser* parser)
6287 {
6288   VEC(tree,gc) *expression_list;
6289
6290   /* Parse the expression-list.  */
6291   expression_list = (cp_parser_parenthesized_expression_list
6292                      (parser, non_attr, /*cast_p=*/false,
6293                       /*allow_expansion_p=*/true,
6294                       /*non_constant_p=*/NULL));
6295
6296   return expression_list;
6297 }
6298
6299 /* Parse a new-type-id.
6300
6301    new-type-id:
6302      type-specifier-seq new-declarator [opt]
6303
6304    Returns the TYPE allocated.  If the new-type-id indicates an array
6305    type, *NELTS is set to the number of elements in the last array
6306    bound; the TYPE will not include the last array bound.  */
6307
6308 static tree
6309 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6310 {
6311   cp_decl_specifier_seq type_specifier_seq;
6312   cp_declarator *new_declarator;
6313   cp_declarator *declarator;
6314   cp_declarator *outer_declarator;
6315   const char *saved_message;
6316   tree type;
6317
6318   /* The type-specifier sequence must not contain type definitions.
6319      (It cannot contain declarations of new types either, but if they
6320      are not definitions we will catch that because they are not
6321      complete.)  */
6322   saved_message = parser->type_definition_forbidden_message;
6323   parser->type_definition_forbidden_message
6324     = G_("types may not be defined in a new-type-id");
6325   /* Parse the type-specifier-seq.  */
6326   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6327                                 /*is_trailing_return=*/false,
6328                                 &type_specifier_seq);
6329   /* Restore the old message.  */
6330   parser->type_definition_forbidden_message = saved_message;
6331   /* Parse the new-declarator.  */
6332   new_declarator = cp_parser_new_declarator_opt (parser);
6333
6334   /* Determine the number of elements in the last array dimension, if
6335      any.  */
6336   *nelts = NULL_TREE;
6337   /* Skip down to the last array dimension.  */
6338   declarator = new_declarator;
6339   outer_declarator = NULL;
6340   while (declarator && (declarator->kind == cdk_pointer
6341                         || declarator->kind == cdk_ptrmem))
6342     {
6343       outer_declarator = declarator;
6344       declarator = declarator->declarator;
6345     }
6346   while (declarator
6347          && declarator->kind == cdk_array
6348          && declarator->declarator
6349          && declarator->declarator->kind == cdk_array)
6350     {
6351       outer_declarator = declarator;
6352       declarator = declarator->declarator;
6353     }
6354
6355   if (declarator && declarator->kind == cdk_array)
6356     {
6357       *nelts = declarator->u.array.bounds;
6358       if (*nelts == error_mark_node)
6359         *nelts = integer_one_node;
6360
6361       if (outer_declarator)
6362         outer_declarator->declarator = declarator->declarator;
6363       else
6364         new_declarator = NULL;
6365     }
6366
6367   type = groktypename (&type_specifier_seq, new_declarator, false);
6368   return type;
6369 }
6370
6371 /* Parse an (optional) new-declarator.
6372
6373    new-declarator:
6374      ptr-operator new-declarator [opt]
6375      direct-new-declarator
6376
6377    Returns the declarator.  */
6378
6379 static cp_declarator *
6380 cp_parser_new_declarator_opt (cp_parser* parser)
6381 {
6382   enum tree_code code;
6383   tree type;
6384   cp_cv_quals cv_quals;
6385
6386   /* We don't know if there's a ptr-operator next, or not.  */
6387   cp_parser_parse_tentatively (parser);
6388   /* Look for a ptr-operator.  */
6389   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6390   /* If that worked, look for more new-declarators.  */
6391   if (cp_parser_parse_definitely (parser))
6392     {
6393       cp_declarator *declarator;
6394
6395       /* Parse another optional declarator.  */
6396       declarator = cp_parser_new_declarator_opt (parser);
6397
6398       return cp_parser_make_indirect_declarator
6399         (code, type, cv_quals, declarator);
6400     }
6401
6402   /* If the next token is a `[', there is a direct-new-declarator.  */
6403   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6404     return cp_parser_direct_new_declarator (parser);
6405
6406   return NULL;
6407 }
6408
6409 /* Parse a direct-new-declarator.
6410
6411    direct-new-declarator:
6412      [ expression ]
6413      direct-new-declarator [constant-expression]
6414
6415    */
6416
6417 static cp_declarator *
6418 cp_parser_direct_new_declarator (cp_parser* parser)
6419 {
6420   cp_declarator *declarator = NULL;
6421
6422   while (true)
6423     {
6424       tree expression;
6425
6426       /* Look for the opening `['.  */
6427       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6428       /* The first expression is not required to be constant.  */
6429       if (!declarator)
6430         {
6431           cp_token *token = cp_lexer_peek_token (parser->lexer);
6432           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6433           /* The standard requires that the expression have integral
6434              type.  DR 74 adds enumeration types.  We believe that the
6435              real intent is that these expressions be handled like the
6436              expression in a `switch' condition, which also allows
6437              classes with a single conversion to integral or
6438              enumeration type.  */
6439           if (!processing_template_decl)
6440             {
6441               expression
6442                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6443                                               expression,
6444                                               /*complain=*/true);
6445               if (!expression)
6446                 {
6447                   error_at (token->location,
6448                             "expression in new-declarator must have integral "
6449                             "or enumeration type");
6450                   expression = error_mark_node;
6451                 }
6452             }
6453         }
6454       /* But all the other expressions must be.  */
6455       else
6456         expression
6457           = cp_parser_constant_expression (parser,
6458                                            /*allow_non_constant=*/false,
6459                                            NULL);
6460       /* Look for the closing `]'.  */
6461       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6462
6463       /* Add this bound to the declarator.  */
6464       declarator = make_array_declarator (declarator, expression);
6465
6466       /* If the next token is not a `[', then there are no more
6467          bounds.  */
6468       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6469         break;
6470     }
6471
6472   return declarator;
6473 }
6474
6475 /* Parse a new-initializer.
6476
6477    new-initializer:
6478      ( expression-list [opt] )
6479      braced-init-list
6480
6481    Returns a representation of the expression-list.  */
6482
6483 static VEC(tree,gc) *
6484 cp_parser_new_initializer (cp_parser* parser)
6485 {
6486   VEC(tree,gc) *expression_list;
6487
6488   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6489     {
6490       tree t;
6491       bool expr_non_constant_p;
6492       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6493       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6494       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6495       expression_list = make_tree_vector_single (t);
6496     }
6497   else
6498     expression_list = (cp_parser_parenthesized_expression_list
6499                        (parser, non_attr, /*cast_p=*/false,
6500                         /*allow_expansion_p=*/true,
6501                         /*non_constant_p=*/NULL));
6502
6503   return expression_list;
6504 }
6505
6506 /* Parse a delete-expression.
6507
6508    delete-expression:
6509      :: [opt] delete cast-expression
6510      :: [opt] delete [ ] cast-expression
6511
6512    Returns a representation of the expression.  */
6513
6514 static tree
6515 cp_parser_delete_expression (cp_parser* parser)
6516 {
6517   bool global_scope_p;
6518   bool array_p;
6519   tree expression;
6520
6521   /* Look for the optional `::' operator.  */
6522   global_scope_p
6523     = (cp_parser_global_scope_opt (parser,
6524                                    /*current_scope_valid_p=*/false)
6525        != NULL_TREE);
6526   /* Look for the `delete' keyword.  */
6527   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6528   /* See if the array syntax is in use.  */
6529   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6530     {
6531       /* Consume the `[' token.  */
6532       cp_lexer_consume_token (parser->lexer);
6533       /* Look for the `]' token.  */
6534       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6535       /* Remember that this is the `[]' construct.  */
6536       array_p = true;
6537     }
6538   else
6539     array_p = false;
6540
6541   /* Parse the cast-expression.  */
6542   expression = cp_parser_simple_cast_expression (parser);
6543
6544   /* A delete-expression may not appear in an integral constant
6545      expression.  */
6546   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6547     return error_mark_node;
6548
6549   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6550 }
6551
6552 /* Returns true if TOKEN may start a cast-expression and false
6553    otherwise.  */
6554
6555 static bool
6556 cp_parser_token_starts_cast_expression (cp_token *token)
6557 {
6558   switch (token->type)
6559     {
6560     case CPP_COMMA:
6561     case CPP_SEMICOLON:
6562     case CPP_QUERY:
6563     case CPP_COLON:
6564     case CPP_CLOSE_SQUARE:
6565     case CPP_CLOSE_PAREN:
6566     case CPP_CLOSE_BRACE:
6567     case CPP_DOT:
6568     case CPP_DOT_STAR:
6569     case CPP_DEREF:
6570     case CPP_DEREF_STAR:
6571     case CPP_DIV:
6572     case CPP_MOD:
6573     case CPP_LSHIFT:
6574     case CPP_RSHIFT:
6575     case CPP_LESS:
6576     case CPP_GREATER:
6577     case CPP_LESS_EQ:
6578     case CPP_GREATER_EQ:
6579     case CPP_EQ_EQ:
6580     case CPP_NOT_EQ:
6581     case CPP_EQ:
6582     case CPP_MULT_EQ:
6583     case CPP_DIV_EQ:
6584     case CPP_MOD_EQ:
6585     case CPP_PLUS_EQ:
6586     case CPP_MINUS_EQ:
6587     case CPP_RSHIFT_EQ:
6588     case CPP_LSHIFT_EQ:
6589     case CPP_AND_EQ:
6590     case CPP_XOR_EQ:
6591     case CPP_OR_EQ:
6592     case CPP_XOR:
6593     case CPP_OR:
6594     case CPP_OR_OR:
6595     case CPP_EOF:
6596       return false;
6597
6598       /* '[' may start a primary-expression in obj-c++.  */
6599     case CPP_OPEN_SQUARE:
6600       return c_dialect_objc ();
6601
6602     default:
6603       return true;
6604     }
6605 }
6606
6607 /* Parse a cast-expression.
6608
6609    cast-expression:
6610      unary-expression
6611      ( type-id ) cast-expression
6612
6613    ADDRESS_P is true iff the unary-expression is appearing as the
6614    operand of the `&' operator.   CAST_P is true if this expression is
6615    the target of a cast.
6616
6617    Returns a representation of the expression.  */
6618
6619 static tree
6620 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6621                            cp_id_kind * pidk)
6622 {
6623   /* If it's a `(', then we might be looking at a cast.  */
6624   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6625     {
6626       tree type = NULL_TREE;
6627       tree expr = NULL_TREE;
6628       bool compound_literal_p;
6629       const char *saved_message;
6630
6631       /* There's no way to know yet whether or not this is a cast.
6632          For example, `(int (3))' is a unary-expression, while `(int)
6633          3' is a cast.  So, we resort to parsing tentatively.  */
6634       cp_parser_parse_tentatively (parser);
6635       /* Types may not be defined in a cast.  */
6636       saved_message = parser->type_definition_forbidden_message;
6637       parser->type_definition_forbidden_message
6638         = G_("types may not be defined in casts");
6639       /* Consume the `('.  */
6640       cp_lexer_consume_token (parser->lexer);
6641       /* A very tricky bit is that `(struct S) { 3 }' is a
6642          compound-literal (which we permit in C++ as an extension).
6643          But, that construct is not a cast-expression -- it is a
6644          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6645          is legal; if the compound-literal were a cast-expression,
6646          you'd need an extra set of parentheses.)  But, if we parse
6647          the type-id, and it happens to be a class-specifier, then we
6648          will commit to the parse at that point, because we cannot
6649          undo the action that is done when creating a new class.  So,
6650          then we cannot back up and do a postfix-expression.
6651
6652          Therefore, we scan ahead to the closing `)', and check to see
6653          if the token after the `)' is a `{'.  If so, we are not
6654          looking at a cast-expression.
6655
6656          Save tokens so that we can put them back.  */
6657       cp_lexer_save_tokens (parser->lexer);
6658       /* Skip tokens until the next token is a closing parenthesis.
6659          If we find the closing `)', and the next token is a `{', then
6660          we are looking at a compound-literal.  */
6661       compound_literal_p
6662         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6663                                                   /*consume_paren=*/true)
6664            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6665       /* Roll back the tokens we skipped.  */
6666       cp_lexer_rollback_tokens (parser->lexer);
6667       /* If we were looking at a compound-literal, simulate an error
6668          so that the call to cp_parser_parse_definitely below will
6669          fail.  */
6670       if (compound_literal_p)
6671         cp_parser_simulate_error (parser);
6672       else
6673         {
6674           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6675           parser->in_type_id_in_expr_p = true;
6676           /* Look for the type-id.  */
6677           type = cp_parser_type_id (parser);
6678           /* Look for the closing `)'.  */
6679           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6680           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6681         }
6682
6683       /* Restore the saved message.  */
6684       parser->type_definition_forbidden_message = saved_message;
6685
6686       /* At this point this can only be either a cast or a
6687          parenthesized ctor such as `(T ())' that looks like a cast to
6688          function returning T.  */
6689       if (!cp_parser_error_occurred (parser)
6690           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6691                                                      (parser->lexer)))
6692         {
6693           cp_parser_parse_definitely (parser);
6694           expr = cp_parser_cast_expression (parser,
6695                                             /*address_p=*/false,
6696                                             /*cast_p=*/true, pidk);
6697
6698           /* Warn about old-style casts, if so requested.  */
6699           if (warn_old_style_cast
6700               && !in_system_header
6701               && !VOID_TYPE_P (type)
6702               && current_lang_name != lang_name_c)
6703             warning (OPT_Wold_style_cast, "use of old-style cast");
6704
6705           /* Only type conversions to integral or enumeration types
6706              can be used in constant-expressions.  */
6707           if (!cast_valid_in_integral_constant_expression_p (type)
6708               && cp_parser_non_integral_constant_expression (parser,
6709                                                              NIC_CAST))
6710             return error_mark_node;
6711
6712           /* Perform the cast.  */
6713           expr = build_c_cast (input_location, type, expr);
6714           return expr;
6715         }
6716       else 
6717         cp_parser_abort_tentative_parse (parser);
6718     }
6719
6720   /* If we get here, then it's not a cast, so it must be a
6721      unary-expression.  */
6722   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6723 }
6724
6725 /* Parse a binary expression of the general form:
6726
6727    pm-expression:
6728      cast-expression
6729      pm-expression .* cast-expression
6730      pm-expression ->* cast-expression
6731
6732    multiplicative-expression:
6733      pm-expression
6734      multiplicative-expression * pm-expression
6735      multiplicative-expression / pm-expression
6736      multiplicative-expression % pm-expression
6737
6738    additive-expression:
6739      multiplicative-expression
6740      additive-expression + multiplicative-expression
6741      additive-expression - multiplicative-expression
6742
6743    shift-expression:
6744      additive-expression
6745      shift-expression << additive-expression
6746      shift-expression >> additive-expression
6747
6748    relational-expression:
6749      shift-expression
6750      relational-expression < shift-expression
6751      relational-expression > shift-expression
6752      relational-expression <= shift-expression
6753      relational-expression >= shift-expression
6754
6755   GNU Extension:
6756
6757    relational-expression:
6758      relational-expression <? shift-expression
6759      relational-expression >? shift-expression
6760
6761    equality-expression:
6762      relational-expression
6763      equality-expression == relational-expression
6764      equality-expression != relational-expression
6765
6766    and-expression:
6767      equality-expression
6768      and-expression & equality-expression
6769
6770    exclusive-or-expression:
6771      and-expression
6772      exclusive-or-expression ^ and-expression
6773
6774    inclusive-or-expression:
6775      exclusive-or-expression
6776      inclusive-or-expression | exclusive-or-expression
6777
6778    logical-and-expression:
6779      inclusive-or-expression
6780      logical-and-expression && inclusive-or-expression
6781
6782    logical-or-expression:
6783      logical-and-expression
6784      logical-or-expression || logical-and-expression
6785
6786    All these are implemented with a single function like:
6787
6788    binary-expression:
6789      simple-cast-expression
6790      binary-expression <token> binary-expression
6791
6792    CAST_P is true if this expression is the target of a cast.
6793
6794    The binops_by_token map is used to get the tree codes for each <token> type.
6795    binary-expressions are associated according to a precedence table.  */
6796
6797 #define TOKEN_PRECEDENCE(token)                              \
6798 (((token->type == CPP_GREATER                                \
6799    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6800   && !parser->greater_than_is_operator_p)                    \
6801  ? PREC_NOT_OPERATOR                                         \
6802  : binops_by_token[token->type].prec)
6803
6804 static tree
6805 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6806                              bool no_toplevel_fold_p,
6807                              enum cp_parser_prec prec,
6808                              cp_id_kind * pidk)
6809 {
6810   cp_parser_expression_stack stack;
6811   cp_parser_expression_stack_entry *sp = &stack[0];
6812   tree lhs, rhs;
6813   cp_token *token;
6814   enum tree_code tree_type, lhs_type, rhs_type;
6815   enum cp_parser_prec new_prec, lookahead_prec;
6816   bool overloaded_p;
6817
6818   /* Parse the first expression.  */
6819   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6820   lhs_type = ERROR_MARK;
6821
6822   for (;;)
6823     {
6824       /* Get an operator token.  */
6825       token = cp_lexer_peek_token (parser->lexer);
6826
6827       if (warn_cxx0x_compat
6828           && token->type == CPP_RSHIFT
6829           && !parser->greater_than_is_operator_p)
6830         {
6831           if (warning_at (token->location, OPT_Wc__0x_compat, 
6832                           "%<>>%> operator will be treated as"
6833                           " two right angle brackets in C++0x"))
6834             inform (token->location,
6835                     "suggest parentheses around %<>>%> expression");
6836         }
6837
6838       new_prec = TOKEN_PRECEDENCE (token);
6839
6840       /* Popping an entry off the stack means we completed a subexpression:
6841          - either we found a token which is not an operator (`>' where it is not
6842            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6843            will happen repeatedly;
6844          - or, we found an operator which has lower priority.  This is the case
6845            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6846            parsing `3 * 4'.  */
6847       if (new_prec <= prec)
6848         {
6849           if (sp == stack)
6850             break;
6851           else
6852             goto pop;
6853         }
6854
6855      get_rhs:
6856       tree_type = binops_by_token[token->type].tree_type;
6857
6858       /* We used the operator token.  */
6859       cp_lexer_consume_token (parser->lexer);
6860
6861       /* For "false && x" or "true || x", x will never be executed;
6862          disable warnings while evaluating it.  */
6863       if (tree_type == TRUTH_ANDIF_EXPR)
6864         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6865       else if (tree_type == TRUTH_ORIF_EXPR)
6866         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6867
6868       /* Extract another operand.  It may be the RHS of this expression
6869          or the LHS of a new, higher priority expression.  */
6870       rhs = cp_parser_simple_cast_expression (parser);
6871       rhs_type = ERROR_MARK;
6872
6873       /* Get another operator token.  Look up its precedence to avoid
6874          building a useless (immediately popped) stack entry for common
6875          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6876       token = cp_lexer_peek_token (parser->lexer);
6877       lookahead_prec = TOKEN_PRECEDENCE (token);
6878       if (lookahead_prec > new_prec)
6879         {
6880           /* ... and prepare to parse the RHS of the new, higher priority
6881              expression.  Since precedence levels on the stack are
6882              monotonically increasing, we do not have to care about
6883              stack overflows.  */
6884           sp->prec = prec;
6885           sp->tree_type = tree_type;
6886           sp->lhs = lhs;
6887           sp->lhs_type = lhs_type;
6888           sp++;
6889           lhs = rhs;
6890           lhs_type = rhs_type;
6891           prec = new_prec;
6892           new_prec = lookahead_prec;
6893           goto get_rhs;
6894
6895          pop:
6896           lookahead_prec = new_prec;
6897           /* If the stack is not empty, we have parsed into LHS the right side
6898              (`4' in the example above) of an expression we had suspended.
6899              We can use the information on the stack to recover the LHS (`3')
6900              from the stack together with the tree code (`MULT_EXPR'), and
6901              the precedence of the higher level subexpression
6902              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6903              which will be used to actually build the additive expression.  */
6904           --sp;
6905           prec = sp->prec;
6906           tree_type = sp->tree_type;
6907           rhs = lhs;
6908           rhs_type = lhs_type;
6909           lhs = sp->lhs;
6910           lhs_type = sp->lhs_type;
6911         }
6912
6913       /* Undo the disabling of warnings done above.  */
6914       if (tree_type == TRUTH_ANDIF_EXPR)
6915         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6916       else if (tree_type == TRUTH_ORIF_EXPR)
6917         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6918
6919       overloaded_p = false;
6920       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6921          ERROR_MARK for everything that is not a binary expression.
6922          This makes warn_about_parentheses miss some warnings that
6923          involve unary operators.  For unary expressions we should
6924          pass the correct tree_code unless the unary expression was
6925          surrounded by parentheses.
6926       */
6927       if (no_toplevel_fold_p
6928           && lookahead_prec <= prec
6929           && sp == stack
6930           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6931         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6932       else
6933         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6934                                  &overloaded_p, tf_warning_or_error);
6935       lhs_type = tree_type;
6936
6937       /* If the binary operator required the use of an overloaded operator,
6938          then this expression cannot be an integral constant-expression.
6939          An overloaded operator can be used even if both operands are
6940          otherwise permissible in an integral constant-expression if at
6941          least one of the operands is of enumeration type.  */
6942
6943       if (overloaded_p
6944           && cp_parser_non_integral_constant_expression (parser,
6945                                                          NIC_OVERLOADED))
6946         return error_mark_node;
6947     }
6948
6949   return lhs;
6950 }
6951
6952
6953 /* Parse the `? expression : assignment-expression' part of a
6954    conditional-expression.  The LOGICAL_OR_EXPR is the
6955    logical-or-expression that started the conditional-expression.
6956    Returns a representation of the entire conditional-expression.
6957
6958    This routine is used by cp_parser_assignment_expression.
6959
6960      ? expression : assignment-expression
6961
6962    GNU Extensions:
6963
6964      ? : assignment-expression */
6965
6966 static tree
6967 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6968 {
6969   tree expr;
6970   tree assignment_expr;
6971   struct cp_token *token;
6972
6973   /* Consume the `?' token.  */
6974   cp_lexer_consume_token (parser->lexer);
6975   token = cp_lexer_peek_token (parser->lexer);
6976   if (cp_parser_allow_gnu_extensions_p (parser)
6977       && token->type == CPP_COLON)
6978     {
6979       pedwarn (token->location, OPT_pedantic, 
6980                "ISO C++ does not allow ?: with omitted middle operand");
6981       /* Implicit true clause.  */
6982       expr = NULL_TREE;
6983       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6984       warn_for_omitted_condop (token->location, logical_or_expr);
6985     }
6986   else
6987     {
6988       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6989       parser->colon_corrects_to_scope_p = false;
6990       /* Parse the expression.  */
6991       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6992       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6993       c_inhibit_evaluation_warnings +=
6994         ((logical_or_expr == truthvalue_true_node)
6995          - (logical_or_expr == truthvalue_false_node));
6996       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6997     }
6998
6999   /* The next token should be a `:'.  */
7000   cp_parser_require (parser, CPP_COLON, RT_COLON);
7001   /* Parse the assignment-expression.  */
7002   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7003   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7004
7005   /* Build the conditional-expression.  */
7006   return build_x_conditional_expr (logical_or_expr,
7007                                    expr,
7008                                    assignment_expr,
7009                                    tf_warning_or_error);
7010 }
7011
7012 /* Parse an assignment-expression.
7013
7014    assignment-expression:
7015      conditional-expression
7016      logical-or-expression assignment-operator assignment_expression
7017      throw-expression
7018
7019    CAST_P is true if this expression is the target of a cast.
7020
7021    Returns a representation for the expression.  */
7022
7023 static tree
7024 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7025                                  cp_id_kind * pidk)
7026 {
7027   tree expr;
7028
7029   /* If the next token is the `throw' keyword, then we're looking at
7030      a throw-expression.  */
7031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7032     expr = cp_parser_throw_expression (parser);
7033   /* Otherwise, it must be that we are looking at a
7034      logical-or-expression.  */
7035   else
7036     {
7037       /* Parse the binary expressions (logical-or-expression).  */
7038       expr = cp_parser_binary_expression (parser, cast_p, false,
7039                                           PREC_NOT_OPERATOR, pidk);
7040       /* If the next token is a `?' then we're actually looking at a
7041          conditional-expression.  */
7042       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7043         return cp_parser_question_colon_clause (parser, expr);
7044       else
7045         {
7046           enum tree_code assignment_operator;
7047
7048           /* If it's an assignment-operator, we're using the second
7049              production.  */
7050           assignment_operator
7051             = cp_parser_assignment_operator_opt (parser);
7052           if (assignment_operator != ERROR_MARK)
7053             {
7054               bool non_constant_p;
7055
7056               /* Parse the right-hand side of the assignment.  */
7057               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7058
7059               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7060                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7061
7062               /* An assignment may not appear in a
7063                  constant-expression.  */
7064               if (cp_parser_non_integral_constant_expression (parser,
7065                                                               NIC_ASSIGNMENT))
7066                 return error_mark_node;
7067               /* Build the assignment expression.  */
7068               expr = build_x_modify_expr (expr,
7069                                           assignment_operator,
7070                                           rhs,
7071                                           tf_warning_or_error);
7072             }
7073         }
7074     }
7075
7076   return expr;
7077 }
7078
7079 /* Parse an (optional) assignment-operator.
7080
7081    assignment-operator: one of
7082      = *= /= %= += -= >>= <<= &= ^= |=
7083
7084    GNU Extension:
7085
7086    assignment-operator: one of
7087      <?= >?=
7088
7089    If the next token is an assignment operator, the corresponding tree
7090    code is returned, and the token is consumed.  For example, for
7091    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7092    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7093    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7094    operator, ERROR_MARK is returned.  */
7095
7096 static enum tree_code
7097 cp_parser_assignment_operator_opt (cp_parser* parser)
7098 {
7099   enum tree_code op;
7100   cp_token *token;
7101
7102   /* Peek at the next token.  */
7103   token = cp_lexer_peek_token (parser->lexer);
7104
7105   switch (token->type)
7106     {
7107     case CPP_EQ:
7108       op = NOP_EXPR;
7109       break;
7110
7111     case CPP_MULT_EQ:
7112       op = MULT_EXPR;
7113       break;
7114
7115     case CPP_DIV_EQ:
7116       op = TRUNC_DIV_EXPR;
7117       break;
7118
7119     case CPP_MOD_EQ:
7120       op = TRUNC_MOD_EXPR;
7121       break;
7122
7123     case CPP_PLUS_EQ:
7124       op = PLUS_EXPR;
7125       break;
7126
7127     case CPP_MINUS_EQ:
7128       op = MINUS_EXPR;
7129       break;
7130
7131     case CPP_RSHIFT_EQ:
7132       op = RSHIFT_EXPR;
7133       break;
7134
7135     case CPP_LSHIFT_EQ:
7136       op = LSHIFT_EXPR;
7137       break;
7138
7139     case CPP_AND_EQ:
7140       op = BIT_AND_EXPR;
7141       break;
7142
7143     case CPP_XOR_EQ:
7144       op = BIT_XOR_EXPR;
7145       break;
7146
7147     case CPP_OR_EQ:
7148       op = BIT_IOR_EXPR;
7149       break;
7150
7151     default:
7152       /* Nothing else is an assignment operator.  */
7153       op = ERROR_MARK;
7154     }
7155
7156   /* If it was an assignment operator, consume it.  */
7157   if (op != ERROR_MARK)
7158     cp_lexer_consume_token (parser->lexer);
7159
7160   return op;
7161 }
7162
7163 /* Parse an expression.
7164
7165    expression:
7166      assignment-expression
7167      expression , assignment-expression
7168
7169    CAST_P is true if this expression is the target of a cast.
7170
7171    Returns a representation of the expression.  */
7172
7173 static tree
7174 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7175 {
7176   tree expression = NULL_TREE;
7177
7178   while (true)
7179     {
7180       tree assignment_expression;
7181
7182       /* Parse the next assignment-expression.  */
7183       assignment_expression
7184         = cp_parser_assignment_expression (parser, cast_p, pidk);
7185       /* If this is the first assignment-expression, we can just
7186          save it away.  */
7187       if (!expression)
7188         expression = assignment_expression;
7189       else
7190         expression = build_x_compound_expr (expression,
7191                                             assignment_expression,
7192                                             tf_warning_or_error);
7193       /* If the next token is not a comma, then we are done with the
7194          expression.  */
7195       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7196         break;
7197       /* Consume the `,'.  */
7198       cp_lexer_consume_token (parser->lexer);
7199       /* A comma operator cannot appear in a constant-expression.  */
7200       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7201         expression = error_mark_node;
7202     }
7203
7204   return expression;
7205 }
7206
7207 /* Parse a constant-expression.
7208
7209    constant-expression:
7210      conditional-expression
7211
7212   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7213   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7214   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7215   is false, NON_CONSTANT_P should be NULL.  */
7216
7217 static tree
7218 cp_parser_constant_expression (cp_parser* parser,
7219                                bool allow_non_constant_p,
7220                                bool *non_constant_p)
7221 {
7222   bool saved_integral_constant_expression_p;
7223   bool saved_allow_non_integral_constant_expression_p;
7224   bool saved_non_integral_constant_expression_p;
7225   tree expression;
7226
7227   /* It might seem that we could simply parse the
7228      conditional-expression, and then check to see if it were
7229      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7230      one that the compiler can figure out is constant, possibly after
7231      doing some simplifications or optimizations.  The standard has a
7232      precise definition of constant-expression, and we must honor
7233      that, even though it is somewhat more restrictive.
7234
7235      For example:
7236
7237        int i[(2, 3)];
7238
7239      is not a legal declaration, because `(2, 3)' is not a
7240      constant-expression.  The `,' operator is forbidden in a
7241      constant-expression.  However, GCC's constant-folding machinery
7242      will fold this operation to an INTEGER_CST for `3'.  */
7243
7244   /* Save the old settings.  */
7245   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7246   saved_allow_non_integral_constant_expression_p
7247     = parser->allow_non_integral_constant_expression_p;
7248   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7249   /* We are now parsing a constant-expression.  */
7250   parser->integral_constant_expression_p = true;
7251   parser->allow_non_integral_constant_expression_p
7252     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7253   parser->non_integral_constant_expression_p = false;
7254   /* Although the grammar says "conditional-expression", we parse an
7255      "assignment-expression", which also permits "throw-expression"
7256      and the use of assignment operators.  In the case that
7257      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7258      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7259      actually essential that we look for an assignment-expression.
7260      For example, cp_parser_initializer_clauses uses this function to
7261      determine whether a particular assignment-expression is in fact
7262      constant.  */
7263   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7264   /* Restore the old settings.  */
7265   parser->integral_constant_expression_p
7266     = saved_integral_constant_expression_p;
7267   parser->allow_non_integral_constant_expression_p
7268     = saved_allow_non_integral_constant_expression_p;
7269   if (allow_non_constant_p)
7270     *non_constant_p = parser->non_integral_constant_expression_p;
7271   else if (parser->non_integral_constant_expression_p
7272            && cxx_dialect < cxx0x)
7273     expression = error_mark_node;
7274   parser->non_integral_constant_expression_p
7275     = saved_non_integral_constant_expression_p;
7276
7277   return expression;
7278 }
7279
7280 /* Parse __builtin_offsetof.
7281
7282    offsetof-expression:
7283      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7284
7285    offsetof-member-designator:
7286      id-expression
7287      | offsetof-member-designator "." id-expression
7288      | offsetof-member-designator "[" expression "]"
7289      | offsetof-member-designator "->" id-expression  */
7290
7291 static tree
7292 cp_parser_builtin_offsetof (cp_parser *parser)
7293 {
7294   int save_ice_p, save_non_ice_p;
7295   tree type, expr;
7296   cp_id_kind dummy;
7297   cp_token *token;
7298
7299   /* We're about to accept non-integral-constant things, but will
7300      definitely yield an integral constant expression.  Save and
7301      restore these values around our local parsing.  */
7302   save_ice_p = parser->integral_constant_expression_p;
7303   save_non_ice_p = parser->non_integral_constant_expression_p;
7304
7305   /* Consume the "__builtin_offsetof" token.  */
7306   cp_lexer_consume_token (parser->lexer);
7307   /* Consume the opening `('.  */
7308   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7309   /* Parse the type-id.  */
7310   type = cp_parser_type_id (parser);
7311   /* Look for the `,'.  */
7312   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7313   token = cp_lexer_peek_token (parser->lexer);
7314
7315   /* Build the (type *)null that begins the traditional offsetof macro.  */
7316   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7317                             tf_warning_or_error);
7318
7319   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7320   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7321                                                  true, &dummy, token->location);
7322   while (true)
7323     {
7324       token = cp_lexer_peek_token (parser->lexer);
7325       switch (token->type)
7326         {
7327         case CPP_OPEN_SQUARE:
7328           /* offsetof-member-designator "[" expression "]" */
7329           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7330           break;
7331
7332         case CPP_DEREF:
7333           /* offsetof-member-designator "->" identifier */
7334           expr = grok_array_decl (expr, integer_zero_node);
7335           /* FALLTHRU */
7336
7337         case CPP_DOT:
7338           /* offsetof-member-designator "." identifier */
7339           cp_lexer_consume_token (parser->lexer);
7340           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7341                                                          expr, true, &dummy,
7342                                                          token->location);
7343           break;
7344
7345         case CPP_CLOSE_PAREN:
7346           /* Consume the ")" token.  */
7347           cp_lexer_consume_token (parser->lexer);
7348           goto success;
7349
7350         default:
7351           /* Error.  We know the following require will fail, but
7352              that gives the proper error message.  */
7353           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7354           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7355           expr = error_mark_node;
7356           goto failure;
7357         }
7358     }
7359
7360  success:
7361   /* If we're processing a template, we can't finish the semantics yet.
7362      Otherwise we can fold the entire expression now.  */
7363   if (processing_template_decl)
7364     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7365   else
7366     expr = finish_offsetof (expr);
7367
7368  failure:
7369   parser->integral_constant_expression_p = save_ice_p;
7370   parser->non_integral_constant_expression_p = save_non_ice_p;
7371
7372   return expr;
7373 }
7374
7375 /* Parse a trait expression.  */
7376
7377 static tree
7378 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7379 {
7380   cp_trait_kind kind;
7381   tree type1, type2 = NULL_TREE;
7382   bool binary = false;
7383   cp_decl_specifier_seq decl_specs;
7384
7385   switch (keyword)
7386     {
7387     case RID_HAS_NOTHROW_ASSIGN:
7388       kind = CPTK_HAS_NOTHROW_ASSIGN;
7389       break;
7390     case RID_HAS_NOTHROW_CONSTRUCTOR:
7391       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7392       break;
7393     case RID_HAS_NOTHROW_COPY:
7394       kind = CPTK_HAS_NOTHROW_COPY;
7395       break;
7396     case RID_HAS_TRIVIAL_ASSIGN:
7397       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7398       break;
7399     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7400       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7401       break;
7402     case RID_HAS_TRIVIAL_COPY:
7403       kind = CPTK_HAS_TRIVIAL_COPY;
7404       break;
7405     case RID_HAS_TRIVIAL_DESTRUCTOR:
7406       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7407       break;
7408     case RID_HAS_VIRTUAL_DESTRUCTOR:
7409       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7410       break;
7411     case RID_IS_ABSTRACT:
7412       kind = CPTK_IS_ABSTRACT;
7413       break;
7414     case RID_IS_BASE_OF:
7415       kind = CPTK_IS_BASE_OF;
7416       binary = true;
7417       break;
7418     case RID_IS_CLASS:
7419       kind = CPTK_IS_CLASS;
7420       break;
7421     case RID_IS_CONVERTIBLE_TO:
7422       kind = CPTK_IS_CONVERTIBLE_TO;
7423       binary = true;
7424       break;
7425     case RID_IS_EMPTY:
7426       kind = CPTK_IS_EMPTY;
7427       break;
7428     case RID_IS_ENUM:
7429       kind = CPTK_IS_ENUM;
7430       break;
7431     case RID_IS_POD:
7432       kind = CPTK_IS_POD;
7433       break;
7434     case RID_IS_POLYMORPHIC:
7435       kind = CPTK_IS_POLYMORPHIC;
7436       break;
7437     case RID_IS_STD_LAYOUT:
7438       kind = CPTK_IS_STD_LAYOUT;
7439       break;
7440     case RID_IS_TRIVIAL:
7441       kind = CPTK_IS_TRIVIAL;
7442       break;
7443     case RID_IS_UNION:
7444       kind = CPTK_IS_UNION;
7445       break;
7446     case RID_IS_LITERAL_TYPE:
7447       kind = CPTK_IS_LITERAL_TYPE;
7448       break;
7449     default:
7450       gcc_unreachable ();
7451     }
7452
7453   /* Consume the token.  */
7454   cp_lexer_consume_token (parser->lexer);
7455
7456   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7457
7458   type1 = cp_parser_type_id (parser);
7459
7460   if (type1 == error_mark_node)
7461     return error_mark_node;
7462
7463   /* Build a trivial decl-specifier-seq.  */
7464   clear_decl_specs (&decl_specs);
7465   decl_specs.type = type1;
7466
7467   /* Call grokdeclarator to figure out what type this is.  */
7468   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7469                           /*initialized=*/0, /*attrlist=*/NULL);
7470
7471   if (binary)
7472     {
7473       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7474  
7475       type2 = cp_parser_type_id (parser);
7476
7477       if (type2 == error_mark_node)
7478         return error_mark_node;
7479
7480       /* Build a trivial decl-specifier-seq.  */
7481       clear_decl_specs (&decl_specs);
7482       decl_specs.type = type2;
7483
7484       /* Call grokdeclarator to figure out what type this is.  */
7485       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7486                               /*initialized=*/0, /*attrlist=*/NULL);
7487     }
7488
7489   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7490
7491   /* Complete the trait expression, which may mean either processing
7492      the trait expr now or saving it for template instantiation.  */
7493   return finish_trait_expr (kind, type1, type2);
7494 }
7495
7496 /* Lambdas that appear in variable initializer or default argument scope
7497    get that in their mangling, so we need to record it.  We might as well
7498    use the count for function and namespace scopes as well.  */
7499 static GTY(()) tree lambda_scope;
7500 static GTY(()) int lambda_count;
7501 typedef struct GTY(()) tree_int
7502 {
7503   tree t;
7504   int i;
7505 } tree_int;
7506 DEF_VEC_O(tree_int);
7507 DEF_VEC_ALLOC_O(tree_int,gc);
7508 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7509
7510 static void
7511 start_lambda_scope (tree decl)
7512 {
7513   tree_int ti;
7514   gcc_assert (decl);
7515   /* Once we're inside a function, we ignore other scopes and just push
7516      the function again so that popping works properly.  */
7517   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7518     decl = current_function_decl;
7519   ti.t = lambda_scope;
7520   ti.i = lambda_count;
7521   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7522   if (lambda_scope != decl)
7523     {
7524       /* Don't reset the count if we're still in the same function.  */
7525       lambda_scope = decl;
7526       lambda_count = 0;
7527     }
7528 }
7529
7530 static void
7531 record_lambda_scope (tree lambda)
7532 {
7533   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7534   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7535 }
7536
7537 static void
7538 finish_lambda_scope (void)
7539 {
7540   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7541   if (lambda_scope != p->t)
7542     {
7543       lambda_scope = p->t;
7544       lambda_count = p->i;
7545     }
7546   VEC_pop (tree_int, lambda_scope_stack);
7547 }
7548
7549 /* Parse a lambda expression.
7550
7551    lambda-expression:
7552      lambda-introducer lambda-declarator [opt] compound-statement
7553
7554    Returns a representation of the expression.  */
7555
7556 static tree
7557 cp_parser_lambda_expression (cp_parser* parser)
7558 {
7559   tree lambda_expr = build_lambda_expr ();
7560   tree type;
7561
7562   LAMBDA_EXPR_LOCATION (lambda_expr)
7563     = cp_lexer_peek_token (parser->lexer)->location;
7564
7565   if (cp_unevaluated_operand)
7566     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7567               "lambda-expression in unevaluated context");
7568
7569   /* We may be in the middle of deferred access check.  Disable
7570      it now.  */
7571   push_deferring_access_checks (dk_no_deferred);
7572
7573   cp_parser_lambda_introducer (parser, lambda_expr);
7574
7575   type = begin_lambda_type (lambda_expr);
7576
7577   record_lambda_scope (lambda_expr);
7578
7579   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7580   determine_visibility (TYPE_NAME (type));
7581
7582   /* Now that we've started the type, add the capture fields for any
7583      explicit captures.  */
7584   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7585
7586   {
7587     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7588     unsigned int saved_num_template_parameter_lists
7589         = parser->num_template_parameter_lists;
7590
7591     parser->num_template_parameter_lists = 0;
7592
7593     /* By virtue of defining a local class, a lambda expression has access to
7594        the private variables of enclosing classes.  */
7595
7596     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7597
7598     cp_parser_lambda_body (parser, lambda_expr);
7599
7600     /* The capture list was built up in reverse order; fix that now.  */
7601     {
7602       tree newlist = NULL_TREE;
7603       tree elt, next;
7604
7605       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7606            elt; elt = next)
7607         {
7608           tree field = TREE_PURPOSE (elt);
7609           char *buf;
7610
7611           next = TREE_CHAIN (elt);
7612           TREE_CHAIN (elt) = newlist;
7613           newlist = elt;
7614
7615           /* Also add __ to the beginning of the field name so that code
7616              outside the lambda body can't see the captured name.  We could
7617              just remove the name entirely, but this is more useful for
7618              debugging.  */
7619           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7620             /* The 'this' capture already starts with __.  */
7621             continue;
7622
7623           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7624           buf[1] = buf[0] = '_';
7625           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7626                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7627           DECL_NAME (field) = get_identifier (buf);
7628         }
7629       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7630     }
7631
7632     maybe_add_lambda_conv_op (type);
7633
7634     type = finish_struct (type, /*attributes=*/NULL_TREE);
7635
7636     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7637   }
7638
7639   pop_deferring_access_checks ();
7640
7641   return build_lambda_object (lambda_expr);
7642 }
7643
7644 /* Parse the beginning of a lambda expression.
7645
7646    lambda-introducer:
7647      [ lambda-capture [opt] ]
7648
7649    LAMBDA_EXPR is the current representation of the lambda expression.  */
7650
7651 static void
7652 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7653 {
7654   /* Need commas after the first capture.  */
7655   bool first = true;
7656
7657   /* Eat the leading `['.  */
7658   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7659
7660   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7661   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7662       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7663     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7664   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7665     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7666
7667   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7668     {
7669       cp_lexer_consume_token (parser->lexer);
7670       first = false;
7671     }
7672
7673   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7674     {
7675       cp_token* capture_token;
7676       tree capture_id;
7677       tree capture_init_expr;
7678       cp_id_kind idk = CP_ID_KIND_NONE;
7679       bool explicit_init_p = false;
7680
7681       enum capture_kind_type
7682       {
7683         BY_COPY,
7684         BY_REFERENCE
7685       };
7686       enum capture_kind_type capture_kind = BY_COPY;
7687
7688       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7689         {
7690           error ("expected end of capture-list");
7691           return;
7692         }
7693
7694       if (first)
7695         first = false;
7696       else
7697         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7698
7699       /* Possibly capture `this'.  */
7700       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7701         {
7702           cp_lexer_consume_token (parser->lexer);
7703           add_capture (lambda_expr,
7704                        /*id=*/get_identifier ("__this"),
7705                        /*initializer=*/finish_this_expr(),
7706                        /*by_reference_p=*/false,
7707                        explicit_init_p);
7708           continue;
7709         }
7710
7711       /* Remember whether we want to capture as a reference or not.  */
7712       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7713         {
7714           capture_kind = BY_REFERENCE;
7715           cp_lexer_consume_token (parser->lexer);
7716         }
7717
7718       /* Get the identifier.  */
7719       capture_token = cp_lexer_peek_token (parser->lexer);
7720       capture_id = cp_parser_identifier (parser);
7721
7722       if (capture_id == error_mark_node)
7723         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7724            delimiters, but I modified this to stop on unnested ']' as well.  It
7725            was already changed to stop on unnested '}', so the
7726            "closing_parenthesis" name is no more misleading with my change.  */
7727         {
7728           cp_parser_skip_to_closing_parenthesis (parser,
7729                                                  /*recovering=*/true,
7730                                                  /*or_comma=*/true,
7731                                                  /*consume_paren=*/true);
7732           break;
7733         }
7734
7735       /* Find the initializer for this capture.  */
7736       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7737         {
7738           /* An explicit expression exists.  */
7739           cp_lexer_consume_token (parser->lexer);
7740           pedwarn (input_location, OPT_pedantic,
7741                    "ISO C++ does not allow initializers "
7742                    "in lambda expression capture lists");
7743           capture_init_expr = cp_parser_assignment_expression (parser,
7744                                                                /*cast_p=*/true,
7745                                                                &idk);
7746           explicit_init_p = true;
7747         }
7748       else
7749         {
7750           const char* error_msg;
7751
7752           /* Turn the identifier into an id-expression.  */
7753           capture_init_expr
7754             = cp_parser_lookup_name
7755                 (parser,
7756                  capture_id,
7757                  none_type,
7758                  /*is_template=*/false,
7759                  /*is_namespace=*/false,
7760                  /*check_dependency=*/true,
7761                  /*ambiguous_decls=*/NULL,
7762                  capture_token->location);
7763
7764           capture_init_expr
7765             = finish_id_expression
7766                 (capture_id,
7767                  capture_init_expr,
7768                  parser->scope,
7769                  &idk,
7770                  /*integral_constant_expression_p=*/false,
7771                  /*allow_non_integral_constant_expression_p=*/false,
7772                  /*non_integral_constant_expression_p=*/NULL,
7773                  /*template_p=*/false,
7774                  /*done=*/true,
7775                  /*address_p=*/false,
7776                  /*template_arg_p=*/false,
7777                  &error_msg,
7778                  capture_token->location);
7779         }
7780
7781       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7782         capture_init_expr
7783           = unqualified_name_lookup_error (capture_init_expr);
7784
7785       add_capture (lambda_expr,
7786                    capture_id,
7787                    capture_init_expr,
7788                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7789                    explicit_init_p);
7790     }
7791
7792   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7793 }
7794
7795 /* Parse the (optional) middle of a lambda expression.
7796
7797    lambda-declarator:
7798      ( parameter-declaration-clause [opt] )
7799        attribute-specifier [opt]
7800        mutable [opt]
7801        exception-specification [opt]
7802        lambda-return-type-clause [opt]
7803
7804    LAMBDA_EXPR is the current representation of the lambda expression.  */
7805
7806 static void
7807 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7808 {
7809   /* 5.1.1.4 of the standard says:
7810        If a lambda-expression does not include a lambda-declarator, it is as if
7811        the lambda-declarator were ().
7812      This means an empty parameter list, no attributes, and no exception
7813      specification.  */
7814   tree param_list = void_list_node;
7815   tree attributes = NULL_TREE;
7816   tree exception_spec = NULL_TREE;
7817   tree t;
7818
7819   /* The lambda-declarator is optional, but must begin with an opening
7820      parenthesis if present.  */
7821   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7822     {
7823       cp_lexer_consume_token (parser->lexer);
7824
7825       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7826
7827       /* Parse parameters.  */
7828       param_list = cp_parser_parameter_declaration_clause (parser);
7829
7830       /* Default arguments shall not be specified in the
7831          parameter-declaration-clause of a lambda-declarator.  */
7832       for (t = param_list; t; t = TREE_CHAIN (t))
7833         if (TREE_PURPOSE (t))
7834           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7835                    "default argument specified for lambda parameter");
7836
7837       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7838
7839       attributes = cp_parser_attributes_opt (parser);
7840
7841       /* Parse optional `mutable' keyword.  */
7842       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7843         {
7844           cp_lexer_consume_token (parser->lexer);
7845           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7846         }
7847
7848       /* Parse optional exception specification.  */
7849       exception_spec = cp_parser_exception_specification_opt (parser);
7850
7851       /* Parse optional trailing return type.  */
7852       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7853         {
7854           cp_lexer_consume_token (parser->lexer);
7855           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7856         }
7857
7858       /* The function parameters must be in scope all the way until after the
7859          trailing-return-type in case of decltype.  */
7860       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7861         pop_binding (DECL_NAME (t), t);
7862
7863       leave_scope ();
7864     }
7865
7866   /* Create the function call operator.
7867
7868      Messing with declarators like this is no uglier than building up the
7869      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7870      other code.  */
7871   {
7872     cp_decl_specifier_seq return_type_specs;
7873     cp_declarator* declarator;
7874     tree fco;
7875     int quals;
7876     void *p;
7877
7878     clear_decl_specs (&return_type_specs);
7879     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7880       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7881     else
7882       /* Maybe we will deduce the return type later, but we can use void
7883          as a placeholder return type anyways.  */
7884       return_type_specs.type = void_type_node;
7885
7886     p = obstack_alloc (&declarator_obstack, 0);
7887
7888     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7889                                      sfk_none);
7890
7891     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7892              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7893     declarator = make_call_declarator (declarator, param_list, quals,
7894                                        exception_spec,
7895                                        /*late_return_type=*/NULL_TREE);
7896     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7897
7898     fco = grokmethod (&return_type_specs,
7899                       declarator,
7900                       attributes);
7901     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7902     DECL_ARTIFICIAL (fco) = 1;
7903
7904     finish_member_declaration (fco);
7905
7906     obstack_free (&declarator_obstack, p);
7907   }
7908 }
7909
7910 /* Parse the body of a lambda expression, which is simply
7911
7912    compound-statement
7913
7914    but which requires special handling.
7915    LAMBDA_EXPR is the current representation of the lambda expression.  */
7916
7917 static void
7918 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7919 {
7920   bool nested = (current_function_decl != NULL_TREE);
7921   if (nested)
7922     push_function_context ();
7923
7924   /* Finish the function call operator
7925      - class_specifier
7926      + late_parsing_for_member
7927      + function_definition_after_declarator
7928      + ctor_initializer_opt_and_function_body  */
7929   {
7930     tree fco = lambda_function (lambda_expr);
7931     tree body;
7932     bool done = false;
7933
7934     /* Let the front end know that we are going to be defining this
7935        function.  */
7936     start_preparsed_function (fco,
7937                               NULL_TREE,
7938                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7939
7940     start_lambda_scope (fco);
7941     body = begin_function_body ();
7942
7943     /* 5.1.1.4 of the standard says:
7944          If a lambda-expression does not include a trailing-return-type, it
7945          is as if the trailing-return-type denotes the following type:
7946           * if the compound-statement is of the form
7947                { return attribute-specifier [opt] expression ; }
7948              the type of the returned expression after lvalue-to-rvalue
7949              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7950              (_conv.array_ 4.2), and function-to-pointer conversion
7951              (_conv.func_ 4.3);
7952           * otherwise, void.  */
7953
7954     /* In a lambda that has neither a lambda-return-type-clause
7955        nor a deducible form, errors should be reported for return statements
7956        in the body.  Since we used void as the placeholder return type, parsing
7957        the body as usual will give such desired behavior.  */
7958     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7959         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7960         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7961         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7962       {
7963         tree compound_stmt;
7964         tree expr = NULL_TREE;
7965         cp_id_kind idk = CP_ID_KIND_NONE;
7966
7967         /* Parse tentatively in case there's more after the initial return
7968            statement.  */
7969         cp_parser_parse_tentatively (parser);
7970
7971         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7972         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7973
7974         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7975
7976         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7977         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7978
7979         if (cp_parser_parse_definitely (parser))
7980           {
7981             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7982
7983             compound_stmt = begin_compound_stmt (0);
7984             /* Will get error here if type not deduced yet.  */
7985             finish_return_stmt (expr);
7986             finish_compound_stmt (compound_stmt);
7987
7988             done = true;
7989           }
7990       }
7991
7992     if (!done)
7993       {
7994         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7995           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7996         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7997            cp_parser_compound_stmt does not pass it.  */
7998         cp_parser_function_body (parser);
7999         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8000       }
8001
8002     finish_function_body (body);
8003     finish_lambda_scope ();
8004
8005     /* Finish the function and generate code for it if necessary.  */
8006     expand_or_defer_fn (finish_function (/*inline*/2));
8007   }
8008
8009   if (nested)
8010     pop_function_context();
8011 }
8012
8013 /* Statements [gram.stmt.stmt]  */
8014
8015 /* Parse a statement.
8016
8017    statement:
8018      labeled-statement
8019      expression-statement
8020      compound-statement
8021      selection-statement
8022      iteration-statement
8023      jump-statement
8024      declaration-statement
8025      try-block
8026
8027   IN_COMPOUND is true when the statement is nested inside a
8028   cp_parser_compound_statement; this matters for certain pragmas.
8029
8030   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8031   is a (possibly labeled) if statement which is not enclosed in braces
8032   and has an else clause.  This is used to implement -Wparentheses.  */
8033
8034 static void
8035 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8036                      bool in_compound, bool *if_p)
8037 {
8038   tree statement;
8039   cp_token *token;
8040   location_t statement_location;
8041
8042  restart:
8043   if (if_p != NULL)
8044     *if_p = false;
8045   /* There is no statement yet.  */
8046   statement = NULL_TREE;
8047   /* Peek at the next token.  */
8048   token = cp_lexer_peek_token (parser->lexer);
8049   /* Remember the location of the first token in the statement.  */
8050   statement_location = token->location;
8051   /* If this is a keyword, then that will often determine what kind of
8052      statement we have.  */
8053   if (token->type == CPP_KEYWORD)
8054     {
8055       enum rid keyword = token->keyword;
8056
8057       switch (keyword)
8058         {
8059         case RID_CASE:
8060         case RID_DEFAULT:
8061           /* Looks like a labeled-statement with a case label.
8062              Parse the label, and then use tail recursion to parse
8063              the statement.  */
8064           cp_parser_label_for_labeled_statement (parser);
8065           goto restart;
8066
8067         case RID_IF:
8068         case RID_SWITCH:
8069           statement = cp_parser_selection_statement (parser, if_p);
8070           break;
8071
8072         case RID_WHILE:
8073         case RID_DO:
8074         case RID_FOR:
8075           statement = cp_parser_iteration_statement (parser);
8076           break;
8077
8078         case RID_BREAK:
8079         case RID_CONTINUE:
8080         case RID_RETURN:
8081         case RID_GOTO:
8082           statement = cp_parser_jump_statement (parser);
8083           break;
8084
8085           /* Objective-C++ exception-handling constructs.  */
8086         case RID_AT_TRY:
8087         case RID_AT_CATCH:
8088         case RID_AT_FINALLY:
8089         case RID_AT_SYNCHRONIZED:
8090         case RID_AT_THROW:
8091           statement = cp_parser_objc_statement (parser);
8092           break;
8093
8094         case RID_TRY:
8095           statement = cp_parser_try_block (parser);
8096           break;
8097
8098         case RID_NAMESPACE:
8099           /* This must be a namespace alias definition.  */
8100           cp_parser_declaration_statement (parser);
8101           return;
8102           
8103         default:
8104           /* It might be a keyword like `int' that can start a
8105              declaration-statement.  */
8106           break;
8107         }
8108     }
8109   else if (token->type == CPP_NAME)
8110     {
8111       /* If the next token is a `:', then we are looking at a
8112          labeled-statement.  */
8113       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8114       if (token->type == CPP_COLON)
8115         {
8116           /* Looks like a labeled-statement with an ordinary label.
8117              Parse the label, and then use tail recursion to parse
8118              the statement.  */
8119           cp_parser_label_for_labeled_statement (parser);
8120           goto restart;
8121         }
8122     }
8123   /* Anything that starts with a `{' must be a compound-statement.  */
8124   else if (token->type == CPP_OPEN_BRACE)
8125     statement = cp_parser_compound_statement (parser, NULL, false);
8126   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8127      a statement all its own.  */
8128   else if (token->type == CPP_PRAGMA)
8129     {
8130       /* Only certain OpenMP pragmas are attached to statements, and thus
8131          are considered statements themselves.  All others are not.  In
8132          the context of a compound, accept the pragma as a "statement" and
8133          return so that we can check for a close brace.  Otherwise we
8134          require a real statement and must go back and read one.  */
8135       if (in_compound)
8136         cp_parser_pragma (parser, pragma_compound);
8137       else if (!cp_parser_pragma (parser, pragma_stmt))
8138         goto restart;
8139       return;
8140     }
8141   else if (token->type == CPP_EOF)
8142     {
8143       cp_parser_error (parser, "expected statement");
8144       return;
8145     }
8146
8147   /* Everything else must be a declaration-statement or an
8148      expression-statement.  Try for the declaration-statement
8149      first, unless we are looking at a `;', in which case we know that
8150      we have an expression-statement.  */
8151   if (!statement)
8152     {
8153       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8154         {
8155           cp_parser_parse_tentatively (parser);
8156           /* Try to parse the declaration-statement.  */
8157           cp_parser_declaration_statement (parser);
8158           /* If that worked, we're done.  */
8159           if (cp_parser_parse_definitely (parser))
8160             return;
8161         }
8162       /* Look for an expression-statement instead.  */
8163       statement = cp_parser_expression_statement (parser, in_statement_expr);
8164     }
8165
8166   /* Set the line number for the statement.  */
8167   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8168     SET_EXPR_LOCATION (statement, statement_location);
8169 }
8170
8171 /* Parse the label for a labeled-statement, i.e.
8172
8173    identifier :
8174    case constant-expression :
8175    default :
8176
8177    GNU Extension:
8178    case constant-expression ... constant-expression : statement
8179
8180    When a label is parsed without errors, the label is added to the
8181    parse tree by the finish_* functions, so this function doesn't
8182    have to return the label.  */
8183
8184 static void
8185 cp_parser_label_for_labeled_statement (cp_parser* parser)
8186 {
8187   cp_token *token;
8188   tree label = NULL_TREE;
8189   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8190
8191   /* The next token should be an identifier.  */
8192   token = cp_lexer_peek_token (parser->lexer);
8193   if (token->type != CPP_NAME
8194       && token->type != CPP_KEYWORD)
8195     {
8196       cp_parser_error (parser, "expected labeled-statement");
8197       return;
8198     }
8199
8200   parser->colon_corrects_to_scope_p = false;
8201   switch (token->keyword)
8202     {
8203     case RID_CASE:
8204       {
8205         tree expr, expr_hi;
8206         cp_token *ellipsis;
8207
8208         /* Consume the `case' token.  */
8209         cp_lexer_consume_token (parser->lexer);
8210         /* Parse the constant-expression.  */
8211         expr = cp_parser_constant_expression (parser,
8212                                               /*allow_non_constant_p=*/false,
8213                                               NULL);
8214
8215         ellipsis = cp_lexer_peek_token (parser->lexer);
8216         if (ellipsis->type == CPP_ELLIPSIS)
8217           {
8218             /* Consume the `...' token.  */
8219             cp_lexer_consume_token (parser->lexer);
8220             expr_hi =
8221               cp_parser_constant_expression (parser,
8222                                              /*allow_non_constant_p=*/false,
8223                                              NULL);
8224             /* We don't need to emit warnings here, as the common code
8225                will do this for us.  */
8226           }
8227         else
8228           expr_hi = NULL_TREE;
8229
8230         if (parser->in_switch_statement_p)
8231           finish_case_label (token->location, expr, expr_hi);
8232         else
8233           error_at (token->location,
8234                     "case label %qE not within a switch statement",
8235                     expr);
8236       }
8237       break;
8238
8239     case RID_DEFAULT:
8240       /* Consume the `default' token.  */
8241       cp_lexer_consume_token (parser->lexer);
8242
8243       if (parser->in_switch_statement_p)
8244         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8245       else
8246         error_at (token->location, "case label not within a switch statement");
8247       break;
8248
8249     default:
8250       /* Anything else must be an ordinary label.  */
8251       label = finish_label_stmt (cp_parser_identifier (parser));
8252       break;
8253     }
8254
8255   /* Require the `:' token.  */
8256   cp_parser_require (parser, CPP_COLON, RT_COLON);
8257
8258   /* An ordinary label may optionally be followed by attributes.
8259      However, this is only permitted if the attributes are then
8260      followed by a semicolon.  This is because, for backward
8261      compatibility, when parsing
8262        lab: __attribute__ ((unused)) int i;
8263      we want the attribute to attach to "i", not "lab".  */
8264   if (label != NULL_TREE
8265       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8266     {
8267       tree attrs;
8268
8269       cp_parser_parse_tentatively (parser);
8270       attrs = cp_parser_attributes_opt (parser);
8271       if (attrs == NULL_TREE
8272           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8273         cp_parser_abort_tentative_parse (parser);
8274       else if (!cp_parser_parse_definitely (parser))
8275         ;
8276       else
8277         cplus_decl_attributes (&label, attrs, 0);
8278     }
8279
8280   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8281 }
8282
8283 /* Parse an expression-statement.
8284
8285    expression-statement:
8286      expression [opt] ;
8287
8288    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8289    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8290    indicates whether this expression-statement is part of an
8291    expression statement.  */
8292
8293 static tree
8294 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8295 {
8296   tree statement = NULL_TREE;
8297   cp_token *token = cp_lexer_peek_token (parser->lexer);
8298
8299   /* If the next token is a ';', then there is no expression
8300      statement.  */
8301   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8302     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8303
8304   /* Give a helpful message for "A<T>::type t;" and the like.  */
8305   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8306       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8307     {
8308       if (TREE_CODE (statement) == SCOPE_REF)
8309         error_at (token->location, "need %<typename%> before %qE because "
8310                   "%qT is a dependent scope",
8311                   statement, TREE_OPERAND (statement, 0));
8312       else if (is_overloaded_fn (statement)
8313                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8314         {
8315           /* A::A a; */
8316           tree fn = get_first_fn (statement);
8317           error_at (token->location,
8318                     "%<%T::%D%> names the constructor, not the type",
8319                     DECL_CONTEXT (fn), DECL_NAME (fn));
8320         }
8321     }
8322
8323   /* Consume the final `;'.  */
8324   cp_parser_consume_semicolon_at_end_of_statement (parser);
8325
8326   if (in_statement_expr
8327       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8328     /* This is the final expression statement of a statement
8329        expression.  */
8330     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8331   else if (statement)
8332     statement = finish_expr_stmt (statement);
8333   else
8334     finish_stmt ();
8335
8336   return statement;
8337 }
8338
8339 /* Parse a compound-statement.
8340
8341    compound-statement:
8342      { statement-seq [opt] }
8343
8344    GNU extension:
8345
8346    compound-statement:
8347      { label-declaration-seq [opt] statement-seq [opt] }
8348
8349    label-declaration-seq:
8350      label-declaration
8351      label-declaration-seq label-declaration
8352
8353    Returns a tree representing the statement.  */
8354
8355 static tree
8356 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8357                               bool in_try)
8358 {
8359   tree compound_stmt;
8360
8361   /* Consume the `{'.  */
8362   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8363     return error_mark_node;
8364   /* Begin the compound-statement.  */
8365   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8366   /* If the next keyword is `__label__' we have a label declaration.  */
8367   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8368     cp_parser_label_declaration (parser);
8369   /* Parse an (optional) statement-seq.  */
8370   cp_parser_statement_seq_opt (parser, in_statement_expr);
8371   /* Finish the compound-statement.  */
8372   finish_compound_stmt (compound_stmt);
8373   /* Consume the `}'.  */
8374   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8375
8376   return compound_stmt;
8377 }
8378
8379 /* Parse an (optional) statement-seq.
8380
8381    statement-seq:
8382      statement
8383      statement-seq [opt] statement  */
8384
8385 static void
8386 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8387 {
8388   /* Scan statements until there aren't any more.  */
8389   while (true)
8390     {
8391       cp_token *token = cp_lexer_peek_token (parser->lexer);
8392
8393       /* If we are looking at a `}', then we have run out of
8394          statements; the same is true if we have reached the end
8395          of file, or have stumbled upon a stray '@end'.  */
8396       if (token->type == CPP_CLOSE_BRACE
8397           || token->type == CPP_EOF
8398           || token->type == CPP_PRAGMA_EOL
8399           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8400         break;
8401       
8402       /* If we are in a compound statement and find 'else' then
8403          something went wrong.  */
8404       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8405         {
8406           if (parser->in_statement & IN_IF_STMT) 
8407             break;
8408           else
8409             {
8410               token = cp_lexer_consume_token (parser->lexer);
8411               error_at (token->location, "%<else%> without a previous %<if%>");
8412             }
8413         }
8414
8415       /* Parse the statement.  */
8416       cp_parser_statement (parser, in_statement_expr, true, NULL);
8417     }
8418 }
8419
8420 /* Parse a selection-statement.
8421
8422    selection-statement:
8423      if ( condition ) statement
8424      if ( condition ) statement else statement
8425      switch ( condition ) statement
8426
8427    Returns the new IF_STMT or SWITCH_STMT.
8428
8429    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8430    is a (possibly labeled) if statement which is not enclosed in
8431    braces and has an else clause.  This is used to implement
8432    -Wparentheses.  */
8433
8434 static tree
8435 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8436 {
8437   cp_token *token;
8438   enum rid keyword;
8439
8440   if (if_p != NULL)
8441     *if_p = false;
8442
8443   /* Peek at the next token.  */
8444   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8445
8446   /* See what kind of keyword it is.  */
8447   keyword = token->keyword;
8448   switch (keyword)
8449     {
8450     case RID_IF:
8451     case RID_SWITCH:
8452       {
8453         tree statement;
8454         tree condition;
8455
8456         /* Look for the `('.  */
8457         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8458           {
8459             cp_parser_skip_to_end_of_statement (parser);
8460             return error_mark_node;
8461           }
8462
8463         /* Begin the selection-statement.  */
8464         if (keyword == RID_IF)
8465           statement = begin_if_stmt ();
8466         else
8467           statement = begin_switch_stmt ();
8468
8469         /* Parse the condition.  */
8470         condition = cp_parser_condition (parser);
8471         /* Look for the `)'.  */
8472         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8473           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8474                                                  /*consume_paren=*/true);
8475
8476         if (keyword == RID_IF)
8477           {
8478             bool nested_if;
8479             unsigned char in_statement;
8480
8481             /* Add the condition.  */
8482             finish_if_stmt_cond (condition, statement);
8483
8484             /* Parse the then-clause.  */
8485             in_statement = parser->in_statement;
8486             parser->in_statement |= IN_IF_STMT;
8487             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8488               {
8489                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8490                 add_stmt (build_empty_stmt (loc));
8491                 cp_lexer_consume_token (parser->lexer);
8492                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8493                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8494                               "empty body in an %<if%> statement");
8495                 nested_if = false;
8496               }
8497             else
8498               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8499             parser->in_statement = in_statement;
8500
8501             finish_then_clause (statement);
8502
8503             /* If the next token is `else', parse the else-clause.  */
8504             if (cp_lexer_next_token_is_keyword (parser->lexer,
8505                                                 RID_ELSE))
8506               {
8507                 /* Consume the `else' keyword.  */
8508                 cp_lexer_consume_token (parser->lexer);
8509                 begin_else_clause (statement);
8510                 /* Parse the else-clause.  */
8511                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8512                   {
8513                     location_t loc;
8514                     loc = cp_lexer_peek_token (parser->lexer)->location;
8515                     warning_at (loc,
8516                                 OPT_Wempty_body, "suggest braces around "
8517                                 "empty body in an %<else%> statement");
8518                     add_stmt (build_empty_stmt (loc));
8519                     cp_lexer_consume_token (parser->lexer);
8520                   }
8521                 else
8522                   cp_parser_implicitly_scoped_statement (parser, NULL);
8523
8524                 finish_else_clause (statement);
8525
8526                 /* If we are currently parsing a then-clause, then
8527                    IF_P will not be NULL.  We set it to true to
8528                    indicate that this if statement has an else clause.
8529                    This may trigger the Wparentheses warning below
8530                    when we get back up to the parent if statement.  */
8531                 if (if_p != NULL)
8532                   *if_p = true;
8533               }
8534             else
8535               {
8536                 /* This if statement does not have an else clause.  If
8537                    NESTED_IF is true, then the then-clause is an if
8538                    statement which does have an else clause.  We warn
8539                    about the potential ambiguity.  */
8540                 if (nested_if)
8541                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8542                               "suggest explicit braces to avoid ambiguous"
8543                               " %<else%>");
8544               }
8545
8546             /* Now we're all done with the if-statement.  */
8547             finish_if_stmt (statement);
8548           }
8549         else
8550           {
8551             bool in_switch_statement_p;
8552             unsigned char in_statement;
8553
8554             /* Add the condition.  */
8555             finish_switch_cond (condition, statement);
8556
8557             /* Parse the body of the switch-statement.  */
8558             in_switch_statement_p = parser->in_switch_statement_p;
8559             in_statement = parser->in_statement;
8560             parser->in_switch_statement_p = true;
8561             parser->in_statement |= IN_SWITCH_STMT;
8562             cp_parser_implicitly_scoped_statement (parser, NULL);
8563             parser->in_switch_statement_p = in_switch_statement_p;
8564             parser->in_statement = in_statement;
8565
8566             /* Now we're all done with the switch-statement.  */
8567             finish_switch_stmt (statement);
8568           }
8569
8570         return statement;
8571       }
8572       break;
8573
8574     default:
8575       cp_parser_error (parser, "expected selection-statement");
8576       return error_mark_node;
8577     }
8578 }
8579
8580 /* Parse a condition.
8581
8582    condition:
8583      expression
8584      type-specifier-seq declarator = initializer-clause
8585      type-specifier-seq declarator braced-init-list
8586
8587    GNU Extension:
8588
8589    condition:
8590      type-specifier-seq declarator asm-specification [opt]
8591        attributes [opt] = assignment-expression
8592
8593    Returns the expression that should be tested.  */
8594
8595 static tree
8596 cp_parser_condition (cp_parser* parser)
8597 {
8598   cp_decl_specifier_seq type_specifiers;
8599   const char *saved_message;
8600   int declares_class_or_enum;
8601
8602   /* Try the declaration first.  */
8603   cp_parser_parse_tentatively (parser);
8604   /* New types are not allowed in the type-specifier-seq for a
8605      condition.  */
8606   saved_message = parser->type_definition_forbidden_message;
8607   parser->type_definition_forbidden_message
8608     = G_("types may not be defined in conditions");
8609   /* Parse the type-specifier-seq.  */
8610   cp_parser_decl_specifier_seq (parser,
8611                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8612                                 &type_specifiers,
8613                                 &declares_class_or_enum);
8614   /* Restore the saved message.  */
8615   parser->type_definition_forbidden_message = saved_message;
8616   /* If all is well, we might be looking at a declaration.  */
8617   if (!cp_parser_error_occurred (parser))
8618     {
8619       tree decl;
8620       tree asm_specification;
8621       tree attributes;
8622       cp_declarator *declarator;
8623       tree initializer = NULL_TREE;
8624
8625       /* Parse the declarator.  */
8626       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8627                                          /*ctor_dtor_or_conv_p=*/NULL,
8628                                          /*parenthesized_p=*/NULL,
8629                                          /*member_p=*/false);
8630       /* Parse the attributes.  */
8631       attributes = cp_parser_attributes_opt (parser);
8632       /* Parse the asm-specification.  */
8633       asm_specification = cp_parser_asm_specification_opt (parser);
8634       /* If the next token is not an `=' or '{', then we might still be
8635          looking at an expression.  For example:
8636
8637            if (A(a).x)
8638
8639          looks like a decl-specifier-seq and a declarator -- but then
8640          there is no `=', so this is an expression.  */
8641       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8642           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8643         cp_parser_simulate_error (parser);
8644         
8645       /* If we did see an `=' or '{', then we are looking at a declaration
8646          for sure.  */
8647       if (cp_parser_parse_definitely (parser))
8648         {
8649           tree pushed_scope;
8650           bool non_constant_p;
8651           bool flags = LOOKUP_ONLYCONVERTING;
8652
8653           /* Create the declaration.  */
8654           decl = start_decl (declarator, &type_specifiers,
8655                              /*initialized_p=*/true,
8656                              attributes, /*prefix_attributes=*/NULL_TREE,
8657                              &pushed_scope);
8658
8659           /* Parse the initializer.  */
8660           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8661             {
8662               initializer = cp_parser_braced_list (parser, &non_constant_p);
8663               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8664               flags = 0;
8665             }
8666           else
8667             {
8668               /* Consume the `='.  */
8669               cp_parser_require (parser, CPP_EQ, RT_EQ);
8670               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8671             }
8672           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8673             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8674
8675           if (!non_constant_p)
8676             initializer = fold_non_dependent_expr (initializer);
8677
8678           /* Process the initializer.  */
8679           cp_finish_decl (decl,
8680                           initializer, !non_constant_p,
8681                           asm_specification,
8682                           flags);
8683
8684           if (pushed_scope)
8685             pop_scope (pushed_scope);
8686
8687           return convert_from_reference (decl);
8688         }
8689     }
8690   /* If we didn't even get past the declarator successfully, we are
8691      definitely not looking at a declaration.  */
8692   else
8693     cp_parser_abort_tentative_parse (parser);
8694
8695   /* Otherwise, we are looking at an expression.  */
8696   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8697 }
8698
8699 /* Parses a for-statement or range-for-statement until the closing ')',
8700    not included. */
8701
8702 static tree
8703 cp_parser_for (cp_parser *parser)
8704 {
8705   tree init, scope, decl;
8706   bool is_range_for;
8707
8708   /* Begin the for-statement.  */
8709   scope = begin_for_scope (&init);
8710
8711   /* Parse the initialization.  */
8712   is_range_for = cp_parser_for_init_statement (parser, &decl);
8713
8714   if (is_range_for)
8715     return cp_parser_range_for (parser, scope, init, decl);
8716   else
8717     return cp_parser_c_for (parser, scope, init);
8718 }
8719
8720 static tree
8721 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8722 {
8723   /* Normal for loop */
8724   tree condition = NULL_TREE;
8725   tree expression = NULL_TREE;
8726   tree stmt;
8727
8728   stmt = begin_for_stmt (scope, init);
8729   /* The for-init-statement has already been parsed in
8730      cp_parser_for_init_statement, so no work is needed here.  */
8731   finish_for_init_stmt (stmt);
8732
8733   /* If there's a condition, process it.  */
8734   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8735     condition = cp_parser_condition (parser);
8736   finish_for_cond (condition, stmt);
8737   /* Look for the `;'.  */
8738   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8739
8740   /* If there's an expression, process it.  */
8741   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8742     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8743   finish_for_expr (expression, stmt);
8744
8745   return stmt;
8746 }
8747
8748 /* Tries to parse a range-based for-statement:
8749
8750   range-based-for:
8751     decl-specifier-seq declarator : expression
8752
8753   The decl-specifier-seq declarator and the `:' are already parsed by
8754   cp_parser_for_init_statement. If processing_template_decl it returns a
8755   newly created RANGE_FOR_STMT; if not, it is converted to a
8756   regular FOR_STMT.  */
8757
8758 static tree
8759 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8760 {
8761   tree stmt, range_expr;
8762
8763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8764     {
8765       bool expr_non_constant_p;
8766       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8767     }
8768   else
8769     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8770
8771   /* If in template, STMT is converted to a normal for-statement
8772      at instantiation. If not, it is done just ahead. */
8773   if (processing_template_decl)
8774     {
8775       stmt = begin_range_for_stmt (scope, init);
8776       finish_range_for_decl (stmt, range_decl, range_expr);
8777     }
8778   else
8779     {
8780       stmt = begin_for_stmt (scope, init);
8781       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8782     }
8783   return stmt;
8784 }
8785
8786 /* Converts a range-based for-statement into a normal
8787    for-statement, as per the definition.
8788
8789       for (RANGE_DECL : RANGE_EXPR)
8790         BLOCK
8791
8792    should be equivalent to:
8793
8794       {
8795         auto &&__range = RANGE_EXPR;
8796         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8797               __begin != __end;
8798               ++__begin)
8799           {
8800               RANGE_DECL = *__begin;
8801               BLOCK
8802           }
8803       }
8804
8805    If RANGE_EXPR is an array:
8806        BEGIN_EXPR = __range
8807        END_EXPR = __range + ARRAY_SIZE(__range)
8808    Else:
8809         BEGIN_EXPR = begin(__range)
8810         END_EXPR = end(__range);
8811
8812    When calling begin()/end() we must use argument dependent
8813    lookup, but always considering 'std' as an associated namespace.  */
8814
8815 tree
8816 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8817 {
8818   tree range_type, range_temp;
8819   tree begin, end;
8820   tree iter_type, begin_expr, end_expr;
8821   tree condition, expression;
8822
8823   if (range_decl == error_mark_node || range_expr == error_mark_node)
8824     /* If an error happened previously do nothing or else a lot of
8825        unhelpful errors would be issued.  */
8826     begin_expr = end_expr = iter_type = error_mark_node;
8827   else
8828     {
8829       /* Find out the type deduced by the declaration
8830        * `auto &&__range = range_expr' */
8831       range_type = cp_build_reference_type (make_auto (), true);
8832       range_type = do_auto_deduction (range_type, range_expr,
8833                                       type_uses_auto (range_type));
8834
8835       /* Create the __range variable */
8836       range_temp = build_decl (input_location, VAR_DECL,
8837                                get_identifier ("__for_range"), range_type);
8838       TREE_USED (range_temp) = 1;
8839       DECL_ARTIFICIAL (range_temp) = 1;
8840       pushdecl (range_temp);
8841       cp_finish_decl (range_temp, range_expr,
8842                       /*is_constant_init*/false, NULL_TREE,
8843                       LOOKUP_ONLYCONVERTING);
8844
8845       range_temp = convert_from_reference (range_temp);
8846
8847       if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8848         {
8849           /* If RANGE_TEMP is an array we will use pointer arithmetic */
8850           iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8851           begin_expr = range_temp;
8852           end_expr
8853               = build_binary_op (input_location, PLUS_EXPR,
8854                                  range_temp,
8855                                  array_type_nelts_top (TREE_TYPE (range_temp)),
8856                                  0);
8857         }
8858       else
8859         {
8860           /* If it is not an array, we must call begin(__range)/end__range() */
8861           VEC(tree,gc) *vec;
8862
8863           begin_expr = get_identifier ("begin");
8864           vec = make_tree_vector ();
8865           VEC_safe_push (tree, gc, vec, range_temp);
8866           begin_expr = perform_koenig_lookup (begin_expr, vec,
8867                                               /*include_std=*/true);
8868           begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8869                                          tf_warning_or_error);
8870           release_tree_vector (vec);
8871
8872           end_expr = get_identifier ("end");
8873           vec = make_tree_vector ();
8874           VEC_safe_push (tree, gc, vec, range_temp);
8875           end_expr = perform_koenig_lookup (end_expr, vec,
8876                                             /*include_std=*/true);
8877           end_expr = finish_call_expr (end_expr, &vec, false, true,
8878                                        tf_warning_or_error);
8879           release_tree_vector (vec);
8880
8881           /* The unqualified type of the __begin and __end temporaries should
8882            * be the same as required by the multiple auto declaration */
8883           iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8884           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8885             error ("inconsistent begin/end types in range-based for: %qT and %qT",
8886                    TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8887         }
8888     }
8889
8890   /* The new for initialization statement */
8891   begin = build_decl (input_location, VAR_DECL,
8892                       get_identifier ("__for_begin"), iter_type);
8893   TREE_USED (begin) = 1;
8894   DECL_ARTIFICIAL (begin) = 1;
8895   pushdecl (begin);
8896   cp_finish_decl (begin, begin_expr,
8897                   /*is_constant_init*/false, NULL_TREE,
8898                   LOOKUP_ONLYCONVERTING);
8899
8900   end = build_decl (input_location, VAR_DECL,
8901                     get_identifier ("__for_end"), iter_type);
8902   TREE_USED (end) = 1;
8903   DECL_ARTIFICIAL (end) = 1;
8904   pushdecl (end);
8905   cp_finish_decl (end, end_expr,
8906                   /*is_constant_init*/false, NULL_TREE,
8907                   LOOKUP_ONLYCONVERTING);
8908
8909   finish_for_init_stmt (statement);
8910
8911 /* The new for condition */
8912   condition = build_x_binary_op (NE_EXPR,
8913                                  begin, ERROR_MARK,
8914                                  end, ERROR_MARK,
8915                                  NULL, tf_warning_or_error);
8916   finish_for_cond (condition, statement);
8917
8918   /* The new increment expression */
8919   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8920   finish_for_expr (expression, statement);
8921
8922   /* The declaration is initialized with *__begin inside the loop body */
8923   cp_finish_decl (range_decl,
8924                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8925                   /*is_constant_init*/false, NULL_TREE,
8926                   LOOKUP_ONLYCONVERTING);
8927
8928   return statement;
8929 }
8930
8931
8932 /* Parse an iteration-statement.
8933
8934    iteration-statement:
8935      while ( condition ) statement
8936      do statement while ( expression ) ;
8937      for ( for-init-statement condition [opt] ; expression [opt] )
8938        statement
8939
8940    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8941
8942 static tree
8943 cp_parser_iteration_statement (cp_parser* parser)
8944 {
8945   cp_token *token;
8946   enum rid keyword;
8947   tree statement;
8948   unsigned char in_statement;
8949
8950   /* Peek at the next token.  */
8951   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8952   if (!token)
8953     return error_mark_node;
8954
8955   /* Remember whether or not we are already within an iteration
8956      statement.  */
8957   in_statement = parser->in_statement;
8958
8959   /* See what kind of keyword it is.  */
8960   keyword = token->keyword;
8961   switch (keyword)
8962     {
8963     case RID_WHILE:
8964       {
8965         tree condition;
8966
8967         /* Begin the while-statement.  */
8968         statement = begin_while_stmt ();
8969         /* Look for the `('.  */
8970         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8971         /* Parse the condition.  */
8972         condition = cp_parser_condition (parser);
8973         finish_while_stmt_cond (condition, statement);
8974         /* Look for the `)'.  */
8975         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8976         /* Parse the dependent statement.  */
8977         parser->in_statement = IN_ITERATION_STMT;
8978         cp_parser_already_scoped_statement (parser);
8979         parser->in_statement = in_statement;
8980         /* We're done with the while-statement.  */
8981         finish_while_stmt (statement);
8982       }
8983       break;
8984
8985     case RID_DO:
8986       {
8987         tree expression;
8988
8989         /* Begin the do-statement.  */
8990         statement = begin_do_stmt ();
8991         /* Parse the body of the do-statement.  */
8992         parser->in_statement = IN_ITERATION_STMT;
8993         cp_parser_implicitly_scoped_statement (parser, NULL);
8994         parser->in_statement = in_statement;
8995         finish_do_body (statement);
8996         /* Look for the `while' keyword.  */
8997         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8998         /* Look for the `('.  */
8999         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9000         /* Parse the expression.  */
9001         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9002         /* We're done with the do-statement.  */
9003         finish_do_stmt (expression, statement);
9004         /* Look for the `)'.  */
9005         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9006         /* Look for the `;'.  */
9007         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9008       }
9009       break;
9010
9011     case RID_FOR:
9012       {
9013         /* Look for the `('.  */
9014         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9015
9016         statement = cp_parser_for (parser);
9017
9018         /* Look for the `)'.  */
9019         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9020
9021         /* Parse the body of the for-statement.  */
9022         parser->in_statement = IN_ITERATION_STMT;
9023         cp_parser_already_scoped_statement (parser);
9024         parser->in_statement = in_statement;
9025
9026         /* We're done with the for-statement.  */
9027         finish_for_stmt (statement);
9028       }
9029       break;
9030
9031     default:
9032       cp_parser_error (parser, "expected iteration-statement");
9033       statement = error_mark_node;
9034       break;
9035     }
9036
9037   return statement;
9038 }
9039
9040 /* Parse a for-init-statement or the declarator of a range-based-for.
9041    Returns true if a range-based-for declaration is seen.
9042
9043    for-init-statement:
9044      expression-statement
9045      simple-declaration  */
9046
9047 static bool
9048 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9049 {
9050   /* If the next token is a `;', then we have an empty
9051      expression-statement.  Grammatically, this is also a
9052      simple-declaration, but an invalid one, because it does not
9053      declare anything.  Therefore, if we did not handle this case
9054      specially, we would issue an error message about an invalid
9055      declaration.  */
9056   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9057     {
9058       bool is_range_for = false;
9059       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9060
9061       parser->colon_corrects_to_scope_p = false;
9062
9063       /* We're going to speculatively look for a declaration, falling back
9064          to an expression, if necessary.  */
9065       cp_parser_parse_tentatively (parser);
9066       /* Parse the declaration.  */
9067       cp_parser_simple_declaration (parser,
9068                                     /*function_definition_allowed_p=*/false,
9069                                     decl);
9070       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9071       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9072         {
9073           /* It is a range-for, consume the ':' */
9074           cp_lexer_consume_token (parser->lexer);
9075           is_range_for = true;
9076           if (cxx_dialect < cxx0x)
9077             {
9078               error_at (cp_lexer_peek_token (parser->lexer)->location,
9079                         "range-based-for loops are not allowed "
9080                         "in C++98 mode");
9081               *decl = error_mark_node;
9082             }
9083         }
9084       else
9085           /* The ';' is not consumed yet because we told
9086              cp_parser_simple_declaration not to.  */
9087           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9088
9089       if (cp_parser_parse_definitely (parser))
9090         return is_range_for;
9091       /* If the tentative parse failed, then we shall need to look for an
9092          expression-statement.  */
9093     }
9094   /* If we are here, it is an expression-statement.  */
9095   cp_parser_expression_statement (parser, NULL_TREE);
9096   return false;
9097 }
9098
9099 /* Parse a jump-statement.
9100
9101    jump-statement:
9102      break ;
9103      continue ;
9104      return expression [opt] ;
9105      return braced-init-list ;
9106      goto identifier ;
9107
9108    GNU extension:
9109
9110    jump-statement:
9111      goto * expression ;
9112
9113    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9114
9115 static tree
9116 cp_parser_jump_statement (cp_parser* parser)
9117 {
9118   tree statement = error_mark_node;
9119   cp_token *token;
9120   enum rid keyword;
9121   unsigned char in_statement;
9122
9123   /* Peek at the next token.  */
9124   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9125   if (!token)
9126     return error_mark_node;
9127
9128   /* See what kind of keyword it is.  */
9129   keyword = token->keyword;
9130   switch (keyword)
9131     {
9132     case RID_BREAK:
9133       in_statement = parser->in_statement & ~IN_IF_STMT;      
9134       switch (in_statement)
9135         {
9136         case 0:
9137           error_at (token->location, "break statement not within loop or switch");
9138           break;
9139         default:
9140           gcc_assert ((in_statement & IN_SWITCH_STMT)
9141                       || in_statement == IN_ITERATION_STMT);
9142           statement = finish_break_stmt ();
9143           break;
9144         case IN_OMP_BLOCK:
9145           error_at (token->location, "invalid exit from OpenMP structured block");
9146           break;
9147         case IN_OMP_FOR:
9148           error_at (token->location, "break statement used with OpenMP for loop");
9149           break;
9150         }
9151       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9152       break;
9153
9154     case RID_CONTINUE:
9155       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9156         {
9157         case 0:
9158           error_at (token->location, "continue statement not within a loop");
9159           break;
9160         case IN_ITERATION_STMT:
9161         case IN_OMP_FOR:
9162           statement = finish_continue_stmt ();
9163           break;
9164         case IN_OMP_BLOCK:
9165           error_at (token->location, "invalid exit from OpenMP structured block");
9166           break;
9167         default:
9168           gcc_unreachable ();
9169         }
9170       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9171       break;
9172
9173     case RID_RETURN:
9174       {
9175         tree expr;
9176         bool expr_non_constant_p;
9177
9178         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9179           {
9180             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9181             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9182           }
9183         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9184           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9185         else
9186           /* If the next token is a `;', then there is no
9187              expression.  */
9188           expr = NULL_TREE;
9189         /* Build the return-statement.  */
9190         statement = finish_return_stmt (expr);
9191         /* Look for the final `;'.  */
9192         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9193       }
9194       break;
9195
9196     case RID_GOTO:
9197       /* Create the goto-statement.  */
9198       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9199         {
9200           /* Issue a warning about this use of a GNU extension.  */
9201           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9202           /* Consume the '*' token.  */
9203           cp_lexer_consume_token (parser->lexer);
9204           /* Parse the dependent expression.  */
9205           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9206         }
9207       else
9208         finish_goto_stmt (cp_parser_identifier (parser));
9209       /* Look for the final `;'.  */
9210       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9211       break;
9212
9213     default:
9214       cp_parser_error (parser, "expected jump-statement");
9215       break;
9216     }
9217
9218   return statement;
9219 }
9220
9221 /* Parse a declaration-statement.
9222
9223    declaration-statement:
9224      block-declaration  */
9225
9226 static void
9227 cp_parser_declaration_statement (cp_parser* parser)
9228 {
9229   void *p;
9230
9231   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9232   p = obstack_alloc (&declarator_obstack, 0);
9233
9234  /* Parse the block-declaration.  */
9235   cp_parser_block_declaration (parser, /*statement_p=*/true);
9236
9237   /* Free any declarators allocated.  */
9238   obstack_free (&declarator_obstack, p);
9239
9240   /* Finish off the statement.  */
9241   finish_stmt ();
9242 }
9243
9244 /* Some dependent statements (like `if (cond) statement'), are
9245    implicitly in their own scope.  In other words, if the statement is
9246    a single statement (as opposed to a compound-statement), it is
9247    none-the-less treated as if it were enclosed in braces.  Any
9248    declarations appearing in the dependent statement are out of scope
9249    after control passes that point.  This function parses a statement,
9250    but ensures that is in its own scope, even if it is not a
9251    compound-statement.
9252
9253    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9254    is a (possibly labeled) if statement which is not enclosed in
9255    braces and has an else clause.  This is used to implement
9256    -Wparentheses.
9257
9258    Returns the new statement.  */
9259
9260 static tree
9261 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9262 {
9263   tree statement;
9264
9265   if (if_p != NULL)
9266     *if_p = false;
9267
9268   /* Mark if () ; with a special NOP_EXPR.  */
9269   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9270     {
9271       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9272       cp_lexer_consume_token (parser->lexer);
9273       statement = add_stmt (build_empty_stmt (loc));
9274     }
9275   /* if a compound is opened, we simply parse the statement directly.  */
9276   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9277     statement = cp_parser_compound_statement (parser, NULL, false);
9278   /* If the token is not a `{', then we must take special action.  */
9279   else
9280     {
9281       /* Create a compound-statement.  */
9282       statement = begin_compound_stmt (0);
9283       /* Parse the dependent-statement.  */
9284       cp_parser_statement (parser, NULL_TREE, false, if_p);
9285       /* Finish the dummy compound-statement.  */
9286       finish_compound_stmt (statement);
9287     }
9288
9289   /* Return the statement.  */
9290   return statement;
9291 }
9292
9293 /* For some dependent statements (like `while (cond) statement'), we
9294    have already created a scope.  Therefore, even if the dependent
9295    statement is a compound-statement, we do not want to create another
9296    scope.  */
9297
9298 static void
9299 cp_parser_already_scoped_statement (cp_parser* parser)
9300 {
9301   /* If the token is a `{', then we must take special action.  */
9302   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9303     cp_parser_statement (parser, NULL_TREE, false, NULL);
9304   else
9305     {
9306       /* Avoid calling cp_parser_compound_statement, so that we
9307          don't create a new scope.  Do everything else by hand.  */
9308       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9309       /* If the next keyword is `__label__' we have a label declaration.  */
9310       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9311         cp_parser_label_declaration (parser);
9312       /* Parse an (optional) statement-seq.  */
9313       cp_parser_statement_seq_opt (parser, NULL_TREE);
9314       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9315     }
9316 }
9317
9318 /* Declarations [gram.dcl.dcl] */
9319
9320 /* Parse an optional declaration-sequence.
9321
9322    declaration-seq:
9323      declaration
9324      declaration-seq declaration  */
9325
9326 static void
9327 cp_parser_declaration_seq_opt (cp_parser* parser)
9328 {
9329   while (true)
9330     {
9331       cp_token *token;
9332
9333       token = cp_lexer_peek_token (parser->lexer);
9334
9335       if (token->type == CPP_CLOSE_BRACE
9336           || token->type == CPP_EOF
9337           || token->type == CPP_PRAGMA_EOL)
9338         break;
9339
9340       if (token->type == CPP_SEMICOLON)
9341         {
9342           /* A declaration consisting of a single semicolon is
9343              invalid.  Allow it unless we're being pedantic.  */
9344           cp_lexer_consume_token (parser->lexer);
9345           if (!in_system_header)
9346             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9347           continue;
9348         }
9349
9350       /* If we're entering or exiting a region that's implicitly
9351          extern "C", modify the lang context appropriately.  */
9352       if (!parser->implicit_extern_c && token->implicit_extern_c)
9353         {
9354           push_lang_context (lang_name_c);
9355           parser->implicit_extern_c = true;
9356         }
9357       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9358         {
9359           pop_lang_context ();
9360           parser->implicit_extern_c = false;
9361         }
9362
9363       if (token->type == CPP_PRAGMA)
9364         {
9365           /* A top-level declaration can consist solely of a #pragma.
9366              A nested declaration cannot, so this is done here and not
9367              in cp_parser_declaration.  (A #pragma at block scope is
9368              handled in cp_parser_statement.)  */
9369           cp_parser_pragma (parser, pragma_external);
9370           continue;
9371         }
9372
9373       /* Parse the declaration itself.  */
9374       cp_parser_declaration (parser);
9375     }
9376 }
9377
9378 /* Parse a declaration.
9379
9380    declaration:
9381      block-declaration
9382      function-definition
9383      template-declaration
9384      explicit-instantiation
9385      explicit-specialization
9386      linkage-specification
9387      namespace-definition
9388
9389    GNU extension:
9390
9391    declaration:
9392       __extension__ declaration */
9393
9394 static void
9395 cp_parser_declaration (cp_parser* parser)
9396 {
9397   cp_token token1;
9398   cp_token token2;
9399   int saved_pedantic;
9400   void *p;
9401   tree attributes = NULL_TREE;
9402
9403   /* Check for the `__extension__' keyword.  */
9404   if (cp_parser_extension_opt (parser, &saved_pedantic))
9405     {
9406       /* Parse the qualified declaration.  */
9407       cp_parser_declaration (parser);
9408       /* Restore the PEDANTIC flag.  */
9409       pedantic = saved_pedantic;
9410
9411       return;
9412     }
9413
9414   /* Try to figure out what kind of declaration is present.  */
9415   token1 = *cp_lexer_peek_token (parser->lexer);
9416
9417   if (token1.type != CPP_EOF)
9418     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9419   else
9420     {
9421       token2.type = CPP_EOF;
9422       token2.keyword = RID_MAX;
9423     }
9424
9425   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9426   p = obstack_alloc (&declarator_obstack, 0);
9427
9428   /* If the next token is `extern' and the following token is a string
9429      literal, then we have a linkage specification.  */
9430   if (token1.keyword == RID_EXTERN
9431       && cp_parser_is_string_literal (&token2))
9432     cp_parser_linkage_specification (parser);
9433   /* If the next token is `template', then we have either a template
9434      declaration, an explicit instantiation, or an explicit
9435      specialization.  */
9436   else if (token1.keyword == RID_TEMPLATE)
9437     {
9438       /* `template <>' indicates a template specialization.  */
9439       if (token2.type == CPP_LESS
9440           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9441         cp_parser_explicit_specialization (parser);
9442       /* `template <' indicates a template declaration.  */
9443       else if (token2.type == CPP_LESS)
9444         cp_parser_template_declaration (parser, /*member_p=*/false);
9445       /* Anything else must be an explicit instantiation.  */
9446       else
9447         cp_parser_explicit_instantiation (parser);
9448     }
9449   /* If the next token is `export', then we have a template
9450      declaration.  */
9451   else if (token1.keyword == RID_EXPORT)
9452     cp_parser_template_declaration (parser, /*member_p=*/false);
9453   /* If the next token is `extern', 'static' or 'inline' and the one
9454      after that is `template', we have a GNU extended explicit
9455      instantiation directive.  */
9456   else if (cp_parser_allow_gnu_extensions_p (parser)
9457            && (token1.keyword == RID_EXTERN
9458                || token1.keyword == RID_STATIC
9459                || token1.keyword == RID_INLINE)
9460            && token2.keyword == RID_TEMPLATE)
9461     cp_parser_explicit_instantiation (parser);
9462   /* If the next token is `namespace', check for a named or unnamed
9463      namespace definition.  */
9464   else if (token1.keyword == RID_NAMESPACE
9465            && (/* A named namespace definition.  */
9466                (token2.type == CPP_NAME
9467                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9468                     != CPP_EQ))
9469                /* An unnamed namespace definition.  */
9470                || token2.type == CPP_OPEN_BRACE
9471                || token2.keyword == RID_ATTRIBUTE))
9472     cp_parser_namespace_definition (parser);
9473   /* An inline (associated) namespace definition.  */
9474   else if (token1.keyword == RID_INLINE
9475            && token2.keyword == RID_NAMESPACE)
9476     cp_parser_namespace_definition (parser);
9477   /* Objective-C++ declaration/definition.  */
9478   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9479     cp_parser_objc_declaration (parser, NULL_TREE);
9480   else if (c_dialect_objc ()
9481            && token1.keyword == RID_ATTRIBUTE
9482            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9483     cp_parser_objc_declaration (parser, attributes);
9484   /* We must have either a block declaration or a function
9485      definition.  */
9486   else
9487     /* Try to parse a block-declaration, or a function-definition.  */
9488     cp_parser_block_declaration (parser, /*statement_p=*/false);
9489
9490   /* Free any declarators allocated.  */
9491   obstack_free (&declarator_obstack, p);
9492 }
9493
9494 /* Parse a block-declaration.
9495
9496    block-declaration:
9497      simple-declaration
9498      asm-definition
9499      namespace-alias-definition
9500      using-declaration
9501      using-directive
9502
9503    GNU Extension:
9504
9505    block-declaration:
9506      __extension__ block-declaration
9507
9508    C++0x Extension:
9509
9510    block-declaration:
9511      static_assert-declaration
9512
9513    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9514    part of a declaration-statement.  */
9515
9516 static void
9517 cp_parser_block_declaration (cp_parser *parser,
9518                              bool      statement_p)
9519 {
9520   cp_token *token1;
9521   int saved_pedantic;
9522
9523   /* Check for the `__extension__' keyword.  */
9524   if (cp_parser_extension_opt (parser, &saved_pedantic))
9525     {
9526       /* Parse the qualified declaration.  */
9527       cp_parser_block_declaration (parser, statement_p);
9528       /* Restore the PEDANTIC flag.  */
9529       pedantic = saved_pedantic;
9530
9531       return;
9532     }
9533
9534   /* Peek at the next token to figure out which kind of declaration is
9535      present.  */
9536   token1 = cp_lexer_peek_token (parser->lexer);
9537
9538   /* If the next keyword is `asm', we have an asm-definition.  */
9539   if (token1->keyword == RID_ASM)
9540     {
9541       if (statement_p)
9542         cp_parser_commit_to_tentative_parse (parser);
9543       cp_parser_asm_definition (parser);
9544     }
9545   /* If the next keyword is `namespace', we have a
9546      namespace-alias-definition.  */
9547   else if (token1->keyword == RID_NAMESPACE)
9548     cp_parser_namespace_alias_definition (parser);
9549   /* If the next keyword is `using', we have either a
9550      using-declaration or a using-directive.  */
9551   else if (token1->keyword == RID_USING)
9552     {
9553       cp_token *token2;
9554
9555       if (statement_p)
9556         cp_parser_commit_to_tentative_parse (parser);
9557       /* If the token after `using' is `namespace', then we have a
9558          using-directive.  */
9559       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9560       if (token2->keyword == RID_NAMESPACE)
9561         cp_parser_using_directive (parser);
9562       /* Otherwise, it's a using-declaration.  */
9563       else
9564         cp_parser_using_declaration (parser,
9565                                      /*access_declaration_p=*/false);
9566     }
9567   /* If the next keyword is `__label__' we have a misplaced label
9568      declaration.  */
9569   else if (token1->keyword == RID_LABEL)
9570     {
9571       cp_lexer_consume_token (parser->lexer);
9572       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9573       cp_parser_skip_to_end_of_statement (parser);
9574       /* If the next token is now a `;', consume it.  */
9575       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9576         cp_lexer_consume_token (parser->lexer);
9577     }
9578   /* If the next token is `static_assert' we have a static assertion.  */
9579   else if (token1->keyword == RID_STATIC_ASSERT)
9580     cp_parser_static_assert (parser, /*member_p=*/false);
9581   /* Anything else must be a simple-declaration.  */
9582   else
9583     cp_parser_simple_declaration (parser, !statement_p,
9584                                   /*maybe_range_for_decl*/NULL);
9585 }
9586
9587 /* Parse a simple-declaration.
9588
9589    simple-declaration:
9590      decl-specifier-seq [opt] init-declarator-list [opt] ;
9591
9592    init-declarator-list:
9593      init-declarator
9594      init-declarator-list , init-declarator
9595
9596    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9597    function-definition as a simple-declaration.
9598
9599    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9600    parsed declaration if it is an uninitialized single declarator not followed
9601    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9602    if present, will not be consumed.  */
9603
9604 static void
9605 cp_parser_simple_declaration (cp_parser* parser,
9606                               bool function_definition_allowed_p,
9607                               tree *maybe_range_for_decl)
9608 {
9609   cp_decl_specifier_seq decl_specifiers;
9610   int declares_class_or_enum;
9611   bool saw_declarator;
9612
9613   if (maybe_range_for_decl)
9614     *maybe_range_for_decl = NULL_TREE;
9615
9616   /* Defer access checks until we know what is being declared; the
9617      checks for names appearing in the decl-specifier-seq should be
9618      done as if we were in the scope of the thing being declared.  */
9619   push_deferring_access_checks (dk_deferred);
9620
9621   /* Parse the decl-specifier-seq.  We have to keep track of whether
9622      or not the decl-specifier-seq declares a named class or
9623      enumeration type, since that is the only case in which the
9624      init-declarator-list is allowed to be empty.
9625
9626      [dcl.dcl]
9627
9628      In a simple-declaration, the optional init-declarator-list can be
9629      omitted only when declaring a class or enumeration, that is when
9630      the decl-specifier-seq contains either a class-specifier, an
9631      elaborated-type-specifier, or an enum-specifier.  */
9632   cp_parser_decl_specifier_seq (parser,
9633                                 CP_PARSER_FLAGS_OPTIONAL,
9634                                 &decl_specifiers,
9635                                 &declares_class_or_enum);
9636   /* We no longer need to defer access checks.  */
9637   stop_deferring_access_checks ();
9638
9639   /* In a block scope, a valid declaration must always have a
9640      decl-specifier-seq.  By not trying to parse declarators, we can
9641      resolve the declaration/expression ambiguity more quickly.  */
9642   if (!function_definition_allowed_p
9643       && !decl_specifiers.any_specifiers_p)
9644     {
9645       cp_parser_error (parser, "expected declaration");
9646       goto done;
9647     }
9648
9649   /* If the next two tokens are both identifiers, the code is
9650      erroneous. The usual cause of this situation is code like:
9651
9652        T t;
9653
9654      where "T" should name a type -- but does not.  */
9655   if (!decl_specifiers.any_type_specifiers_p
9656       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9657     {
9658       /* If parsing tentatively, we should commit; we really are
9659          looking at a declaration.  */
9660       cp_parser_commit_to_tentative_parse (parser);
9661       /* Give up.  */
9662       goto done;
9663     }
9664
9665   /* If we have seen at least one decl-specifier, and the next token
9666      is not a parenthesis, then we must be looking at a declaration.
9667      (After "int (" we might be looking at a functional cast.)  */
9668   if (decl_specifiers.any_specifiers_p
9669       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9670       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9671       && !cp_parser_error_occurred (parser))
9672     cp_parser_commit_to_tentative_parse (parser);
9673
9674   /* Keep going until we hit the `;' at the end of the simple
9675      declaration.  */
9676   saw_declarator = false;
9677   while (cp_lexer_next_token_is_not (parser->lexer,
9678                                      CPP_SEMICOLON))
9679     {
9680       cp_token *token;
9681       bool function_definition_p;
9682       tree decl;
9683
9684       if (saw_declarator)
9685         {
9686           /* If we are processing next declarator, coma is expected */
9687           token = cp_lexer_peek_token (parser->lexer);
9688           gcc_assert (token->type == CPP_COMMA);
9689           cp_lexer_consume_token (parser->lexer);
9690           if (maybe_range_for_decl)
9691             *maybe_range_for_decl = error_mark_node;
9692         }
9693       else
9694         saw_declarator = true;
9695
9696       /* Parse the init-declarator.  */
9697       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9698                                         /*checks=*/NULL,
9699                                         function_definition_allowed_p,
9700                                         /*member_p=*/false,
9701                                         declares_class_or_enum,
9702                                         &function_definition_p,
9703                                         maybe_range_for_decl);
9704       /* If an error occurred while parsing tentatively, exit quickly.
9705          (That usually happens when in the body of a function; each
9706          statement is treated as a declaration-statement until proven
9707          otherwise.)  */
9708       if (cp_parser_error_occurred (parser))
9709         goto done;
9710       /* Handle function definitions specially.  */
9711       if (function_definition_p)
9712         {
9713           /* If the next token is a `,', then we are probably
9714              processing something like:
9715
9716                void f() {}, *p;
9717
9718              which is erroneous.  */
9719           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9720             {
9721               cp_token *token = cp_lexer_peek_token (parser->lexer);
9722               error_at (token->location,
9723                         "mixing"
9724                         " declarations and function-definitions is forbidden");
9725             }
9726           /* Otherwise, we're done with the list of declarators.  */
9727           else
9728             {
9729               pop_deferring_access_checks ();
9730               return;
9731             }
9732         }
9733       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9734         *maybe_range_for_decl = decl;
9735       /* The next token should be either a `,' or a `;'.  */
9736       token = cp_lexer_peek_token (parser->lexer);
9737       /* If it's a `,', there are more declarators to come.  */
9738       if (token->type == CPP_COMMA)
9739         /* will be consumed next time around */;
9740       /* If it's a `;', we are done.  */
9741       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9742         break;
9743       /* Anything else is an error.  */
9744       else
9745         {
9746           /* If we have already issued an error message we don't need
9747              to issue another one.  */
9748           if (decl != error_mark_node
9749               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9750             cp_parser_error (parser, "expected %<,%> or %<;%>");
9751           /* Skip tokens until we reach the end of the statement.  */
9752           cp_parser_skip_to_end_of_statement (parser);
9753           /* If the next token is now a `;', consume it.  */
9754           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9755             cp_lexer_consume_token (parser->lexer);
9756           goto done;
9757         }
9758       /* After the first time around, a function-definition is not
9759          allowed -- even if it was OK at first.  For example:
9760
9761            int i, f() {}
9762
9763          is not valid.  */
9764       function_definition_allowed_p = false;
9765     }
9766
9767   /* Issue an error message if no declarators are present, and the
9768      decl-specifier-seq does not itself declare a class or
9769      enumeration.  */
9770   if (!saw_declarator)
9771     {
9772       if (cp_parser_declares_only_class_p (parser))
9773         shadow_tag (&decl_specifiers);
9774       /* Perform any deferred access checks.  */
9775       perform_deferred_access_checks ();
9776     }
9777
9778   /* Consume the `;'.  */
9779   if (!maybe_range_for_decl)
9780       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9781
9782  done:
9783   pop_deferring_access_checks ();
9784 }
9785
9786 /* Parse a decl-specifier-seq.
9787
9788    decl-specifier-seq:
9789      decl-specifier-seq [opt] decl-specifier
9790
9791    decl-specifier:
9792      storage-class-specifier
9793      type-specifier
9794      function-specifier
9795      friend
9796      typedef
9797
9798    GNU Extension:
9799
9800    decl-specifier:
9801      attributes
9802
9803    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9804
9805    The parser flags FLAGS is used to control type-specifier parsing.
9806
9807    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9808    flags:
9809
9810      1: one of the decl-specifiers is an elaborated-type-specifier
9811         (i.e., a type declaration)
9812      2: one of the decl-specifiers is an enum-specifier or a
9813         class-specifier (i.e., a type definition)
9814
9815    */
9816
9817 static void
9818 cp_parser_decl_specifier_seq (cp_parser* parser,
9819                               cp_parser_flags flags,
9820                               cp_decl_specifier_seq *decl_specs,
9821                               int* declares_class_or_enum)
9822 {
9823   bool constructor_possible_p = !parser->in_declarator_p;
9824   cp_token *start_token = NULL;
9825
9826   /* Clear DECL_SPECS.  */
9827   clear_decl_specs (decl_specs);
9828
9829   /* Assume no class or enumeration type is declared.  */
9830   *declares_class_or_enum = 0;
9831
9832   /* Keep reading specifiers until there are no more to read.  */
9833   while (true)
9834     {
9835       bool constructor_p;
9836       bool found_decl_spec;
9837       cp_token *token;
9838
9839       /* Peek at the next token.  */
9840       token = cp_lexer_peek_token (parser->lexer);
9841
9842       /* Save the first token of the decl spec list for error
9843          reporting.  */
9844       if (!start_token)
9845         start_token = token;
9846       /* Handle attributes.  */
9847       if (token->keyword == RID_ATTRIBUTE)
9848         {
9849           /* Parse the attributes.  */
9850           decl_specs->attributes
9851             = chainon (decl_specs->attributes,
9852                        cp_parser_attributes_opt (parser));
9853           continue;
9854         }
9855       /* Assume we will find a decl-specifier keyword.  */
9856       found_decl_spec = true;
9857       /* If the next token is an appropriate keyword, we can simply
9858          add it to the list.  */
9859       switch (token->keyword)
9860         {
9861           /* decl-specifier:
9862                friend
9863                constexpr */
9864         case RID_FRIEND:
9865           if (!at_class_scope_p ())
9866             {
9867               error_at (token->location, "%<friend%> used outside of class");
9868               cp_lexer_purge_token (parser->lexer);
9869             }
9870           else
9871             {
9872               ++decl_specs->specs[(int) ds_friend];
9873               /* Consume the token.  */
9874               cp_lexer_consume_token (parser->lexer);
9875             }
9876           break;
9877
9878         case RID_CONSTEXPR:
9879           ++decl_specs->specs[(int) ds_constexpr];
9880           cp_lexer_consume_token (parser->lexer);
9881           break;
9882
9883           /* function-specifier:
9884                inline
9885                virtual
9886                explicit  */
9887         case RID_INLINE:
9888         case RID_VIRTUAL:
9889         case RID_EXPLICIT:
9890           cp_parser_function_specifier_opt (parser, decl_specs);
9891           break;
9892
9893           /* decl-specifier:
9894                typedef  */
9895         case RID_TYPEDEF:
9896           ++decl_specs->specs[(int) ds_typedef];
9897           /* Consume the token.  */
9898           cp_lexer_consume_token (parser->lexer);
9899           /* A constructor declarator cannot appear in a typedef.  */
9900           constructor_possible_p = false;
9901           /* The "typedef" keyword can only occur in a declaration; we
9902              may as well commit at this point.  */
9903           cp_parser_commit_to_tentative_parse (parser);
9904
9905           if (decl_specs->storage_class != sc_none)
9906             decl_specs->conflicting_specifiers_p = true;
9907           break;
9908
9909           /* storage-class-specifier:
9910                auto
9911                register
9912                static
9913                extern
9914                mutable
9915
9916              GNU Extension:
9917                thread  */
9918         case RID_AUTO:
9919           if (cxx_dialect == cxx98) 
9920             {
9921               /* Consume the token.  */
9922               cp_lexer_consume_token (parser->lexer);
9923
9924               /* Complain about `auto' as a storage specifier, if
9925                  we're complaining about C++0x compatibility.  */
9926               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9927                           " will change meaning in C++0x; please remove it");
9928
9929               /* Set the storage class anyway.  */
9930               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9931                                            token->location);
9932             }
9933           else
9934             /* C++0x auto type-specifier.  */
9935             found_decl_spec = false;
9936           break;
9937
9938         case RID_REGISTER:
9939         case RID_STATIC:
9940         case RID_EXTERN:
9941         case RID_MUTABLE:
9942           /* Consume the token.  */
9943           cp_lexer_consume_token (parser->lexer);
9944           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9945                                        token->location);
9946           break;
9947         case RID_THREAD:
9948           /* Consume the token.  */
9949           cp_lexer_consume_token (parser->lexer);
9950           ++decl_specs->specs[(int) ds_thread];
9951           break;
9952
9953         default:
9954           /* We did not yet find a decl-specifier yet.  */
9955           found_decl_spec = false;
9956           break;
9957         }
9958
9959       if (found_decl_spec
9960           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9961           && token->keyword != RID_CONSTEXPR)
9962         error ("decl-specifier invalid in condition");
9963
9964       /* Constructors are a special case.  The `S' in `S()' is not a
9965          decl-specifier; it is the beginning of the declarator.  */
9966       constructor_p
9967         = (!found_decl_spec
9968            && constructor_possible_p
9969            && (cp_parser_constructor_declarator_p
9970                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9971
9972       /* If we don't have a DECL_SPEC yet, then we must be looking at
9973          a type-specifier.  */
9974       if (!found_decl_spec && !constructor_p)
9975         {
9976           int decl_spec_declares_class_or_enum;
9977           bool is_cv_qualifier;
9978           tree type_spec;
9979
9980           type_spec
9981             = cp_parser_type_specifier (parser, flags,
9982                                         decl_specs,
9983                                         /*is_declaration=*/true,
9984                                         &decl_spec_declares_class_or_enum,
9985                                         &is_cv_qualifier);
9986           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9987
9988           /* If this type-specifier referenced a user-defined type
9989              (a typedef, class-name, etc.), then we can't allow any
9990              more such type-specifiers henceforth.
9991
9992              [dcl.spec]
9993
9994              The longest sequence of decl-specifiers that could
9995              possibly be a type name is taken as the
9996              decl-specifier-seq of a declaration.  The sequence shall
9997              be self-consistent as described below.
9998
9999              [dcl.type]
10000
10001              As a general rule, at most one type-specifier is allowed
10002              in the complete decl-specifier-seq of a declaration.  The
10003              only exceptions are the following:
10004
10005              -- const or volatile can be combined with any other
10006                 type-specifier.
10007
10008              -- signed or unsigned can be combined with char, long,
10009                 short, or int.
10010
10011              -- ..
10012
10013              Example:
10014
10015                typedef char* Pc;
10016                void g (const int Pc);
10017
10018              Here, Pc is *not* part of the decl-specifier seq; it's
10019              the declarator.  Therefore, once we see a type-specifier
10020              (other than a cv-qualifier), we forbid any additional
10021              user-defined types.  We *do* still allow things like `int
10022              int' to be considered a decl-specifier-seq, and issue the
10023              error message later.  */
10024           if (type_spec && !is_cv_qualifier)
10025             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10026           /* A constructor declarator cannot follow a type-specifier.  */
10027           if (type_spec)
10028             {
10029               constructor_possible_p = false;
10030               found_decl_spec = true;
10031               if (!is_cv_qualifier)
10032                 decl_specs->any_type_specifiers_p = true;
10033             }
10034         }
10035
10036       /* If we still do not have a DECL_SPEC, then there are no more
10037          decl-specifiers.  */
10038       if (!found_decl_spec)
10039         break;
10040
10041       decl_specs->any_specifiers_p = true;
10042       /* After we see one decl-specifier, further decl-specifiers are
10043          always optional.  */
10044       flags |= CP_PARSER_FLAGS_OPTIONAL;
10045     }
10046
10047   cp_parser_check_decl_spec (decl_specs, start_token->location);
10048
10049   /* Don't allow a friend specifier with a class definition.  */
10050   if (decl_specs->specs[(int) ds_friend] != 0
10051       && (*declares_class_or_enum & 2))
10052     error_at (start_token->location,
10053               "class definition may not be declared a friend");
10054 }
10055
10056 /* Parse an (optional) storage-class-specifier.
10057
10058    storage-class-specifier:
10059      auto
10060      register
10061      static
10062      extern
10063      mutable
10064
10065    GNU Extension:
10066
10067    storage-class-specifier:
10068      thread
10069
10070    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10071
10072 static tree
10073 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10074 {
10075   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10076     {
10077     case RID_AUTO:
10078       if (cxx_dialect != cxx98)
10079         return NULL_TREE;
10080       /* Fall through for C++98.  */
10081
10082     case RID_REGISTER:
10083     case RID_STATIC:
10084     case RID_EXTERN:
10085     case RID_MUTABLE:
10086     case RID_THREAD:
10087       /* Consume the token.  */
10088       return cp_lexer_consume_token (parser->lexer)->u.value;
10089
10090     default:
10091       return NULL_TREE;
10092     }
10093 }
10094
10095 /* Parse an (optional) function-specifier.
10096
10097    function-specifier:
10098      inline
10099      virtual
10100      explicit
10101
10102    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10103    Updates DECL_SPECS, if it is non-NULL.  */
10104
10105 static tree
10106 cp_parser_function_specifier_opt (cp_parser* parser,
10107                                   cp_decl_specifier_seq *decl_specs)
10108 {
10109   cp_token *token = cp_lexer_peek_token (parser->lexer);
10110   switch (token->keyword)
10111     {
10112     case RID_INLINE:
10113       if (decl_specs)
10114         ++decl_specs->specs[(int) ds_inline];
10115       break;
10116
10117     case RID_VIRTUAL:
10118       /* 14.5.2.3 [temp.mem]
10119
10120          A member function template shall not be virtual.  */
10121       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10122         error_at (token->location, "templates may not be %<virtual%>");
10123       else if (decl_specs)
10124         ++decl_specs->specs[(int) ds_virtual];
10125       break;
10126
10127     case RID_EXPLICIT:
10128       if (decl_specs)
10129         ++decl_specs->specs[(int) ds_explicit];
10130       break;
10131
10132     default:
10133       return NULL_TREE;
10134     }
10135
10136   /* Consume the token.  */
10137   return cp_lexer_consume_token (parser->lexer)->u.value;
10138 }
10139
10140 /* Parse a linkage-specification.
10141
10142    linkage-specification:
10143      extern string-literal { declaration-seq [opt] }
10144      extern string-literal declaration  */
10145
10146 static void
10147 cp_parser_linkage_specification (cp_parser* parser)
10148 {
10149   tree linkage;
10150
10151   /* Look for the `extern' keyword.  */
10152   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10153
10154   /* Look for the string-literal.  */
10155   linkage = cp_parser_string_literal (parser, false, false);
10156
10157   /* Transform the literal into an identifier.  If the literal is a
10158      wide-character string, or contains embedded NULs, then we can't
10159      handle it as the user wants.  */
10160   if (strlen (TREE_STRING_POINTER (linkage))
10161       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10162     {
10163       cp_parser_error (parser, "invalid linkage-specification");
10164       /* Assume C++ linkage.  */
10165       linkage = lang_name_cplusplus;
10166     }
10167   else
10168     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10169
10170   /* We're now using the new linkage.  */
10171   push_lang_context (linkage);
10172
10173   /* If the next token is a `{', then we're using the first
10174      production.  */
10175   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10176     {
10177       /* Consume the `{' token.  */
10178       cp_lexer_consume_token (parser->lexer);
10179       /* Parse the declarations.  */
10180       cp_parser_declaration_seq_opt (parser);
10181       /* Look for the closing `}'.  */
10182       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10183     }
10184   /* Otherwise, there's just one declaration.  */
10185   else
10186     {
10187       bool saved_in_unbraced_linkage_specification_p;
10188
10189       saved_in_unbraced_linkage_specification_p
10190         = parser->in_unbraced_linkage_specification_p;
10191       parser->in_unbraced_linkage_specification_p = true;
10192       cp_parser_declaration (parser);
10193       parser->in_unbraced_linkage_specification_p
10194         = saved_in_unbraced_linkage_specification_p;
10195     }
10196
10197   /* We're done with the linkage-specification.  */
10198   pop_lang_context ();
10199 }
10200
10201 /* Parse a static_assert-declaration.
10202
10203    static_assert-declaration:
10204      static_assert ( constant-expression , string-literal ) ; 
10205
10206    If MEMBER_P, this static_assert is a class member.  */
10207
10208 static void 
10209 cp_parser_static_assert(cp_parser *parser, bool member_p)
10210 {
10211   tree condition;
10212   tree message;
10213   cp_token *token;
10214   location_t saved_loc;
10215
10216   /* Peek at the `static_assert' token so we can keep track of exactly
10217      where the static assertion started.  */
10218   token = cp_lexer_peek_token (parser->lexer);
10219   saved_loc = token->location;
10220
10221   /* Look for the `static_assert' keyword.  */
10222   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10223                                   RT_STATIC_ASSERT))
10224     return;
10225
10226   /*  We know we are in a static assertion; commit to any tentative
10227       parse.  */
10228   if (cp_parser_parsing_tentatively (parser))
10229     cp_parser_commit_to_tentative_parse (parser);
10230
10231   /* Parse the `(' starting the static assertion condition.  */
10232   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10233
10234   /* Parse the constant-expression.  */
10235   condition = 
10236     cp_parser_constant_expression (parser,
10237                                    /*allow_non_constant_p=*/false,
10238                                    /*non_constant_p=*/NULL);
10239
10240   /* Parse the separating `,'.  */
10241   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10242
10243   /* Parse the string-literal message.  */
10244   message = cp_parser_string_literal (parser, 
10245                                       /*translate=*/false,
10246                                       /*wide_ok=*/true);
10247
10248   /* A `)' completes the static assertion.  */
10249   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10250     cp_parser_skip_to_closing_parenthesis (parser, 
10251                                            /*recovering=*/true, 
10252                                            /*or_comma=*/false,
10253                                            /*consume_paren=*/true);
10254
10255   /* A semicolon terminates the declaration.  */
10256   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10257
10258   /* Complete the static assertion, which may mean either processing 
10259      the static assert now or saving it for template instantiation.  */
10260   finish_static_assert (condition, message, saved_loc, member_p);
10261 }
10262
10263 /* Parse a `decltype' type. Returns the type. 
10264
10265    simple-type-specifier:
10266      decltype ( expression )  */
10267
10268 static tree
10269 cp_parser_decltype (cp_parser *parser)
10270 {
10271   tree expr;
10272   bool id_expression_or_member_access_p = false;
10273   const char *saved_message;
10274   bool saved_integral_constant_expression_p;
10275   bool saved_non_integral_constant_expression_p;
10276   cp_token *id_expr_start_token;
10277
10278   /* Look for the `decltype' token.  */
10279   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10280     return error_mark_node;
10281
10282   /* Types cannot be defined in a `decltype' expression.  Save away the
10283      old message.  */
10284   saved_message = parser->type_definition_forbidden_message;
10285
10286   /* And create the new one.  */
10287   parser->type_definition_forbidden_message
10288     = G_("types may not be defined in %<decltype%> expressions");
10289
10290   /* The restrictions on constant-expressions do not apply inside
10291      decltype expressions.  */
10292   saved_integral_constant_expression_p
10293     = parser->integral_constant_expression_p;
10294   saved_non_integral_constant_expression_p
10295     = parser->non_integral_constant_expression_p;
10296   parser->integral_constant_expression_p = false;
10297
10298   /* Do not actually evaluate the expression.  */
10299   ++cp_unevaluated_operand;
10300
10301   /* Do not warn about problems with the expression.  */
10302   ++c_inhibit_evaluation_warnings;
10303
10304   /* Parse the opening `('.  */
10305   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10306     return error_mark_node;
10307   
10308   /* First, try parsing an id-expression.  */
10309   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10310   cp_parser_parse_tentatively (parser);
10311   expr = cp_parser_id_expression (parser,
10312                                   /*template_keyword_p=*/false,
10313                                   /*check_dependency_p=*/true,
10314                                   /*template_p=*/NULL,
10315                                   /*declarator_p=*/false,
10316                                   /*optional_p=*/false);
10317
10318   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10319     {
10320       bool non_integral_constant_expression_p = false;
10321       tree id_expression = expr;
10322       cp_id_kind idk;
10323       const char *error_msg;
10324
10325       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10326         /* Lookup the name we got back from the id-expression.  */
10327         expr = cp_parser_lookup_name (parser, expr,
10328                                       none_type,
10329                                       /*is_template=*/false,
10330                                       /*is_namespace=*/false,
10331                                       /*check_dependency=*/true,
10332                                       /*ambiguous_decls=*/NULL,
10333                                       id_expr_start_token->location);
10334
10335       if (expr
10336           && expr != error_mark_node
10337           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10338           && TREE_CODE (expr) != TYPE_DECL
10339           && (TREE_CODE (expr) != BIT_NOT_EXPR
10340               || !TYPE_P (TREE_OPERAND (expr, 0)))
10341           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10342         {
10343           /* Complete lookup of the id-expression.  */
10344           expr = (finish_id_expression
10345                   (id_expression, expr, parser->scope, &idk,
10346                    /*integral_constant_expression_p=*/false,
10347                    /*allow_non_integral_constant_expression_p=*/true,
10348                    &non_integral_constant_expression_p,
10349                    /*template_p=*/false,
10350                    /*done=*/true,
10351                    /*address_p=*/false,
10352                    /*template_arg_p=*/false,
10353                    &error_msg,
10354                    id_expr_start_token->location));
10355
10356           if (expr == error_mark_node)
10357             /* We found an id-expression, but it was something that we
10358                should not have found. This is an error, not something
10359                we can recover from, so note that we found an
10360                id-expression and we'll recover as gracefully as
10361                possible.  */
10362             id_expression_or_member_access_p = true;
10363         }
10364
10365       if (expr 
10366           && expr != error_mark_node
10367           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10368         /* We have an id-expression.  */
10369         id_expression_or_member_access_p = true;
10370     }
10371
10372   if (!id_expression_or_member_access_p)
10373     {
10374       /* Abort the id-expression parse.  */
10375       cp_parser_abort_tentative_parse (parser);
10376
10377       /* Parsing tentatively, again.  */
10378       cp_parser_parse_tentatively (parser);
10379
10380       /* Parse a class member access.  */
10381       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10382                                            /*cast_p=*/false,
10383                                            /*member_access_only_p=*/true, NULL);
10384
10385       if (expr 
10386           && expr != error_mark_node
10387           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10388         /* We have an id-expression.  */
10389         id_expression_or_member_access_p = true;
10390     }
10391
10392   if (id_expression_or_member_access_p)
10393     /* We have parsed the complete id-expression or member access.  */
10394     cp_parser_parse_definitely (parser);
10395   else
10396     {
10397       bool saved_greater_than_is_operator_p;
10398
10399       /* Abort our attempt to parse an id-expression or member access
10400          expression.  */
10401       cp_parser_abort_tentative_parse (parser);
10402
10403       /* Within a parenthesized expression, a `>' token is always
10404          the greater-than operator.  */
10405       saved_greater_than_is_operator_p
10406         = parser->greater_than_is_operator_p;
10407       parser->greater_than_is_operator_p = true;
10408
10409       /* Parse a full expression.  */
10410       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10411
10412       /* The `>' token might be the end of a template-id or
10413          template-parameter-list now.  */
10414       parser->greater_than_is_operator_p
10415         = saved_greater_than_is_operator_p;
10416     }
10417
10418   /* Go back to evaluating expressions.  */
10419   --cp_unevaluated_operand;
10420   --c_inhibit_evaluation_warnings;
10421
10422   /* Restore the old message and the integral constant expression
10423      flags.  */
10424   parser->type_definition_forbidden_message = saved_message;
10425   parser->integral_constant_expression_p
10426     = saved_integral_constant_expression_p;
10427   parser->non_integral_constant_expression_p
10428     = saved_non_integral_constant_expression_p;
10429
10430   if (expr == error_mark_node)
10431     {
10432       /* Skip everything up to the closing `)'.  */
10433       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10434                                              /*consume_paren=*/true);
10435       return error_mark_node;
10436     }
10437   
10438   /* Parse to the closing `)'.  */
10439   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10440     {
10441       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10442                                              /*consume_paren=*/true);
10443       return error_mark_node;
10444     }
10445
10446   return finish_decltype_type (expr, id_expression_or_member_access_p);
10447 }
10448
10449 /* Special member functions [gram.special] */
10450
10451 /* Parse a conversion-function-id.
10452
10453    conversion-function-id:
10454      operator conversion-type-id
10455
10456    Returns an IDENTIFIER_NODE representing the operator.  */
10457
10458 static tree
10459 cp_parser_conversion_function_id (cp_parser* parser)
10460 {
10461   tree type;
10462   tree saved_scope;
10463   tree saved_qualifying_scope;
10464   tree saved_object_scope;
10465   tree pushed_scope = NULL_TREE;
10466
10467   /* Look for the `operator' token.  */
10468   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10469     return error_mark_node;
10470   /* When we parse the conversion-type-id, the current scope will be
10471      reset.  However, we need that information in able to look up the
10472      conversion function later, so we save it here.  */
10473   saved_scope = parser->scope;
10474   saved_qualifying_scope = parser->qualifying_scope;
10475   saved_object_scope = parser->object_scope;
10476   /* We must enter the scope of the class so that the names of
10477      entities declared within the class are available in the
10478      conversion-type-id.  For example, consider:
10479
10480        struct S {
10481          typedef int I;
10482          operator I();
10483        };
10484
10485        S::operator I() { ... }
10486
10487      In order to see that `I' is a type-name in the definition, we
10488      must be in the scope of `S'.  */
10489   if (saved_scope)
10490     pushed_scope = push_scope (saved_scope);
10491   /* Parse the conversion-type-id.  */
10492   type = cp_parser_conversion_type_id (parser);
10493   /* Leave the scope of the class, if any.  */
10494   if (pushed_scope)
10495     pop_scope (pushed_scope);
10496   /* Restore the saved scope.  */
10497   parser->scope = saved_scope;
10498   parser->qualifying_scope = saved_qualifying_scope;
10499   parser->object_scope = saved_object_scope;
10500   /* If the TYPE is invalid, indicate failure.  */
10501   if (type == error_mark_node)
10502     return error_mark_node;
10503   return mangle_conv_op_name_for_type (type);
10504 }
10505
10506 /* Parse a conversion-type-id:
10507
10508    conversion-type-id:
10509      type-specifier-seq conversion-declarator [opt]
10510
10511    Returns the TYPE specified.  */
10512
10513 static tree
10514 cp_parser_conversion_type_id (cp_parser* parser)
10515 {
10516   tree attributes;
10517   cp_decl_specifier_seq type_specifiers;
10518   cp_declarator *declarator;
10519   tree type_specified;
10520
10521   /* Parse the attributes.  */
10522   attributes = cp_parser_attributes_opt (parser);
10523   /* Parse the type-specifiers.  */
10524   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10525                                 /*is_trailing_return=*/false,
10526                                 &type_specifiers);
10527   /* If that didn't work, stop.  */
10528   if (type_specifiers.type == error_mark_node)
10529     return error_mark_node;
10530   /* Parse the conversion-declarator.  */
10531   declarator = cp_parser_conversion_declarator_opt (parser);
10532
10533   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10534                                     /*initialized=*/0, &attributes);
10535   if (attributes)
10536     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10537
10538   /* Don't give this error when parsing tentatively.  This happens to
10539      work because we always parse this definitively once.  */
10540   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10541       && type_uses_auto (type_specified))
10542     {
10543       error ("invalid use of %<auto%> in conversion operator");
10544       return error_mark_node;
10545     }
10546
10547   return type_specified;
10548 }
10549
10550 /* Parse an (optional) conversion-declarator.
10551
10552    conversion-declarator:
10553      ptr-operator conversion-declarator [opt]
10554
10555    */
10556
10557 static cp_declarator *
10558 cp_parser_conversion_declarator_opt (cp_parser* parser)
10559 {
10560   enum tree_code code;
10561   tree class_type;
10562   cp_cv_quals cv_quals;
10563
10564   /* We don't know if there's a ptr-operator next, or not.  */
10565   cp_parser_parse_tentatively (parser);
10566   /* Try the ptr-operator.  */
10567   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10568   /* If it worked, look for more conversion-declarators.  */
10569   if (cp_parser_parse_definitely (parser))
10570     {
10571       cp_declarator *declarator;
10572
10573       /* Parse another optional declarator.  */
10574       declarator = cp_parser_conversion_declarator_opt (parser);
10575
10576       return cp_parser_make_indirect_declarator
10577         (code, class_type, cv_quals, declarator);
10578    }
10579
10580   return NULL;
10581 }
10582
10583 /* Parse an (optional) ctor-initializer.
10584
10585    ctor-initializer:
10586      : mem-initializer-list
10587
10588    Returns TRUE iff the ctor-initializer was actually present.  */
10589
10590 static bool
10591 cp_parser_ctor_initializer_opt (cp_parser* parser)
10592 {
10593   /* If the next token is not a `:', then there is no
10594      ctor-initializer.  */
10595   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10596     {
10597       /* Do default initialization of any bases and members.  */
10598       if (DECL_CONSTRUCTOR_P (current_function_decl))
10599         finish_mem_initializers (NULL_TREE);
10600
10601       return false;
10602     }
10603
10604   /* Consume the `:' token.  */
10605   cp_lexer_consume_token (parser->lexer);
10606   /* And the mem-initializer-list.  */
10607   cp_parser_mem_initializer_list (parser);
10608
10609   return true;
10610 }
10611
10612 /* Parse a mem-initializer-list.
10613
10614    mem-initializer-list:
10615      mem-initializer ... [opt]
10616      mem-initializer ... [opt] , mem-initializer-list  */
10617
10618 static void
10619 cp_parser_mem_initializer_list (cp_parser* parser)
10620 {
10621   tree mem_initializer_list = NULL_TREE;
10622   cp_token *token = cp_lexer_peek_token (parser->lexer);
10623
10624   /* Let the semantic analysis code know that we are starting the
10625      mem-initializer-list.  */
10626   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10627     error_at (token->location,
10628               "only constructors take member initializers");
10629
10630   /* Loop through the list.  */
10631   while (true)
10632     {
10633       tree mem_initializer;
10634
10635       token = cp_lexer_peek_token (parser->lexer);
10636       /* Parse the mem-initializer.  */
10637       mem_initializer = cp_parser_mem_initializer (parser);
10638       /* If the next token is a `...', we're expanding member initializers. */
10639       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10640         {
10641           /* Consume the `...'. */
10642           cp_lexer_consume_token (parser->lexer);
10643
10644           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10645              can be expanded but members cannot. */
10646           if (mem_initializer != error_mark_node
10647               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10648             {
10649               error_at (token->location,
10650                         "cannot expand initializer for member %<%D%>",
10651                         TREE_PURPOSE (mem_initializer));
10652               mem_initializer = error_mark_node;
10653             }
10654
10655           /* Construct the pack expansion type. */
10656           if (mem_initializer != error_mark_node)
10657             mem_initializer = make_pack_expansion (mem_initializer);
10658         }
10659       /* Add it to the list, unless it was erroneous.  */
10660       if (mem_initializer != error_mark_node)
10661         {
10662           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10663           mem_initializer_list = mem_initializer;
10664         }
10665       /* If the next token is not a `,', we're done.  */
10666       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10667         break;
10668       /* Consume the `,' token.  */
10669       cp_lexer_consume_token (parser->lexer);
10670     }
10671
10672   /* Perform semantic analysis.  */
10673   if (DECL_CONSTRUCTOR_P (current_function_decl))
10674     finish_mem_initializers (mem_initializer_list);
10675 }
10676
10677 /* Parse a mem-initializer.
10678
10679    mem-initializer:
10680      mem-initializer-id ( expression-list [opt] )
10681      mem-initializer-id braced-init-list
10682
10683    GNU extension:
10684
10685    mem-initializer:
10686      ( expression-list [opt] )
10687
10688    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10689    class) or FIELD_DECL (for a non-static data member) to initialize;
10690    the TREE_VALUE is the expression-list.  An empty initialization
10691    list is represented by void_list_node.  */
10692
10693 static tree
10694 cp_parser_mem_initializer (cp_parser* parser)
10695 {
10696   tree mem_initializer_id;
10697   tree expression_list;
10698   tree member;
10699   cp_token *token = cp_lexer_peek_token (parser->lexer);
10700
10701   /* Find out what is being initialized.  */
10702   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10703     {
10704       permerror (token->location,
10705                  "anachronistic old-style base class initializer");
10706       mem_initializer_id = NULL_TREE;
10707     }
10708   else
10709     {
10710       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10711       if (mem_initializer_id == error_mark_node)
10712         return mem_initializer_id;
10713     }
10714   member = expand_member_init (mem_initializer_id);
10715   if (member && !DECL_P (member))
10716     in_base_initializer = 1;
10717
10718   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10719     {
10720       bool expr_non_constant_p;
10721       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10722       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10723       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10724       expression_list = build_tree_list (NULL_TREE, expression_list);
10725     }
10726   else
10727     {
10728       VEC(tree,gc)* vec;
10729       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10730                                                      /*cast_p=*/false,
10731                                                      /*allow_expansion_p=*/true,
10732                                                      /*non_constant_p=*/NULL);
10733       if (vec == NULL)
10734         return error_mark_node;
10735       expression_list = build_tree_list_vec (vec);
10736       release_tree_vector (vec);
10737     }
10738
10739   if (expression_list == error_mark_node)
10740     return error_mark_node;
10741   if (!expression_list)
10742     expression_list = void_type_node;
10743
10744   in_base_initializer = 0;
10745
10746   return member ? build_tree_list (member, expression_list) : error_mark_node;
10747 }
10748
10749 /* Parse a mem-initializer-id.
10750
10751    mem-initializer-id:
10752      :: [opt] nested-name-specifier [opt] class-name
10753      identifier
10754
10755    Returns a TYPE indicating the class to be initializer for the first
10756    production.  Returns an IDENTIFIER_NODE indicating the data member
10757    to be initialized for the second production.  */
10758
10759 static tree
10760 cp_parser_mem_initializer_id (cp_parser* parser)
10761 {
10762   bool global_scope_p;
10763   bool nested_name_specifier_p;
10764   bool template_p = false;
10765   tree id;
10766
10767   cp_token *token = cp_lexer_peek_token (parser->lexer);
10768
10769   /* `typename' is not allowed in this context ([temp.res]).  */
10770   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10771     {
10772       error_at (token->location, 
10773                 "keyword %<typename%> not allowed in this context (a qualified "
10774                 "member initializer is implicitly a type)");
10775       cp_lexer_consume_token (parser->lexer);
10776     }
10777   /* Look for the optional `::' operator.  */
10778   global_scope_p
10779     = (cp_parser_global_scope_opt (parser,
10780                                    /*current_scope_valid_p=*/false)
10781        != NULL_TREE);
10782   /* Look for the optional nested-name-specifier.  The simplest way to
10783      implement:
10784
10785        [temp.res]
10786
10787        The keyword `typename' is not permitted in a base-specifier or
10788        mem-initializer; in these contexts a qualified name that
10789        depends on a template-parameter is implicitly assumed to be a
10790        type name.
10791
10792      is to assume that we have seen the `typename' keyword at this
10793      point.  */
10794   nested_name_specifier_p
10795     = (cp_parser_nested_name_specifier_opt (parser,
10796                                             /*typename_keyword_p=*/true,
10797                                             /*check_dependency_p=*/true,
10798                                             /*type_p=*/true,
10799                                             /*is_declaration=*/true)
10800        != NULL_TREE);
10801   if (nested_name_specifier_p)
10802     template_p = cp_parser_optional_template_keyword (parser);
10803   /* If there is a `::' operator or a nested-name-specifier, then we
10804      are definitely looking for a class-name.  */
10805   if (global_scope_p || nested_name_specifier_p)
10806     return cp_parser_class_name (parser,
10807                                  /*typename_keyword_p=*/true,
10808                                  /*template_keyword_p=*/template_p,
10809                                  typename_type,
10810                                  /*check_dependency_p=*/true,
10811                                  /*class_head_p=*/false,
10812                                  /*is_declaration=*/true);
10813   /* Otherwise, we could also be looking for an ordinary identifier.  */
10814   cp_parser_parse_tentatively (parser);
10815   /* Try a class-name.  */
10816   id = cp_parser_class_name (parser,
10817                              /*typename_keyword_p=*/true,
10818                              /*template_keyword_p=*/false,
10819                              none_type,
10820                              /*check_dependency_p=*/true,
10821                              /*class_head_p=*/false,
10822                              /*is_declaration=*/true);
10823   /* If we found one, we're done.  */
10824   if (cp_parser_parse_definitely (parser))
10825     return id;
10826   /* Otherwise, look for an ordinary identifier.  */
10827   return cp_parser_identifier (parser);
10828 }
10829
10830 /* Overloading [gram.over] */
10831
10832 /* Parse an operator-function-id.
10833
10834    operator-function-id:
10835      operator operator
10836
10837    Returns an IDENTIFIER_NODE for the operator which is a
10838    human-readable spelling of the identifier, e.g., `operator +'.  */
10839
10840 static tree
10841 cp_parser_operator_function_id (cp_parser* parser)
10842 {
10843   /* Look for the `operator' keyword.  */
10844   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10845     return error_mark_node;
10846   /* And then the name of the operator itself.  */
10847   return cp_parser_operator (parser);
10848 }
10849
10850 /* Parse an operator.
10851
10852    operator:
10853      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10854      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10855      || ++ -- , ->* -> () []
10856
10857    GNU Extensions:
10858
10859    operator:
10860      <? >? <?= >?=
10861
10862    Returns an IDENTIFIER_NODE for the operator which is a
10863    human-readable spelling of the identifier, e.g., `operator +'.  */
10864
10865 static tree
10866 cp_parser_operator (cp_parser* parser)
10867 {
10868   tree id = NULL_TREE;
10869   cp_token *token;
10870
10871   /* Peek at the next token.  */
10872   token = cp_lexer_peek_token (parser->lexer);
10873   /* Figure out which operator we have.  */
10874   switch (token->type)
10875     {
10876     case CPP_KEYWORD:
10877       {
10878         enum tree_code op;
10879
10880         /* The keyword should be either `new' or `delete'.  */
10881         if (token->keyword == RID_NEW)
10882           op = NEW_EXPR;
10883         else if (token->keyword == RID_DELETE)
10884           op = DELETE_EXPR;
10885         else
10886           break;
10887
10888         /* Consume the `new' or `delete' token.  */
10889         cp_lexer_consume_token (parser->lexer);
10890
10891         /* Peek at the next token.  */
10892         token = cp_lexer_peek_token (parser->lexer);
10893         /* If it's a `[' token then this is the array variant of the
10894            operator.  */
10895         if (token->type == CPP_OPEN_SQUARE)
10896           {
10897             /* Consume the `[' token.  */
10898             cp_lexer_consume_token (parser->lexer);
10899             /* Look for the `]' token.  */
10900             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10901             id = ansi_opname (op == NEW_EXPR
10902                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10903           }
10904         /* Otherwise, we have the non-array variant.  */
10905         else
10906           id = ansi_opname (op);
10907
10908         return id;
10909       }
10910
10911     case CPP_PLUS:
10912       id = ansi_opname (PLUS_EXPR);
10913       break;
10914
10915     case CPP_MINUS:
10916       id = ansi_opname (MINUS_EXPR);
10917       break;
10918
10919     case CPP_MULT:
10920       id = ansi_opname (MULT_EXPR);
10921       break;
10922
10923     case CPP_DIV:
10924       id = ansi_opname (TRUNC_DIV_EXPR);
10925       break;
10926
10927     case CPP_MOD:
10928       id = ansi_opname (TRUNC_MOD_EXPR);
10929       break;
10930
10931     case CPP_XOR:
10932       id = ansi_opname (BIT_XOR_EXPR);
10933       break;
10934
10935     case CPP_AND:
10936       id = ansi_opname (BIT_AND_EXPR);
10937       break;
10938
10939     case CPP_OR:
10940       id = ansi_opname (BIT_IOR_EXPR);
10941       break;
10942
10943     case CPP_COMPL:
10944       id = ansi_opname (BIT_NOT_EXPR);
10945       break;
10946
10947     case CPP_NOT:
10948       id = ansi_opname (TRUTH_NOT_EXPR);
10949       break;
10950
10951     case CPP_EQ:
10952       id = ansi_assopname (NOP_EXPR);
10953       break;
10954
10955     case CPP_LESS:
10956       id = ansi_opname (LT_EXPR);
10957       break;
10958
10959     case CPP_GREATER:
10960       id = ansi_opname (GT_EXPR);
10961       break;
10962
10963     case CPP_PLUS_EQ:
10964       id = ansi_assopname (PLUS_EXPR);
10965       break;
10966
10967     case CPP_MINUS_EQ:
10968       id = ansi_assopname (MINUS_EXPR);
10969       break;
10970
10971     case CPP_MULT_EQ:
10972       id = ansi_assopname (MULT_EXPR);
10973       break;
10974
10975     case CPP_DIV_EQ:
10976       id = ansi_assopname (TRUNC_DIV_EXPR);
10977       break;
10978
10979     case CPP_MOD_EQ:
10980       id = ansi_assopname (TRUNC_MOD_EXPR);
10981       break;
10982
10983     case CPP_XOR_EQ:
10984       id = ansi_assopname (BIT_XOR_EXPR);
10985       break;
10986
10987     case CPP_AND_EQ:
10988       id = ansi_assopname (BIT_AND_EXPR);
10989       break;
10990
10991     case CPP_OR_EQ:
10992       id = ansi_assopname (BIT_IOR_EXPR);
10993       break;
10994
10995     case CPP_LSHIFT:
10996       id = ansi_opname (LSHIFT_EXPR);
10997       break;
10998
10999     case CPP_RSHIFT:
11000       id = ansi_opname (RSHIFT_EXPR);
11001       break;
11002
11003     case CPP_LSHIFT_EQ:
11004       id = ansi_assopname (LSHIFT_EXPR);
11005       break;
11006
11007     case CPP_RSHIFT_EQ:
11008       id = ansi_assopname (RSHIFT_EXPR);
11009       break;
11010
11011     case CPP_EQ_EQ:
11012       id = ansi_opname (EQ_EXPR);
11013       break;
11014
11015     case CPP_NOT_EQ:
11016       id = ansi_opname (NE_EXPR);
11017       break;
11018
11019     case CPP_LESS_EQ:
11020       id = ansi_opname (LE_EXPR);
11021       break;
11022
11023     case CPP_GREATER_EQ:
11024       id = ansi_opname (GE_EXPR);
11025       break;
11026
11027     case CPP_AND_AND:
11028       id = ansi_opname (TRUTH_ANDIF_EXPR);
11029       break;
11030
11031     case CPP_OR_OR:
11032       id = ansi_opname (TRUTH_ORIF_EXPR);
11033       break;
11034
11035     case CPP_PLUS_PLUS:
11036       id = ansi_opname (POSTINCREMENT_EXPR);
11037       break;
11038
11039     case CPP_MINUS_MINUS:
11040       id = ansi_opname (PREDECREMENT_EXPR);
11041       break;
11042
11043     case CPP_COMMA:
11044       id = ansi_opname (COMPOUND_EXPR);
11045       break;
11046
11047     case CPP_DEREF_STAR:
11048       id = ansi_opname (MEMBER_REF);
11049       break;
11050
11051     case CPP_DEREF:
11052       id = ansi_opname (COMPONENT_REF);
11053       break;
11054
11055     case CPP_OPEN_PAREN:
11056       /* Consume the `('.  */
11057       cp_lexer_consume_token (parser->lexer);
11058       /* Look for the matching `)'.  */
11059       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11060       return ansi_opname (CALL_EXPR);
11061
11062     case CPP_OPEN_SQUARE:
11063       /* Consume the `['.  */
11064       cp_lexer_consume_token (parser->lexer);
11065       /* Look for the matching `]'.  */
11066       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11067       return ansi_opname (ARRAY_REF);
11068
11069     default:
11070       /* Anything else is an error.  */
11071       break;
11072     }
11073
11074   /* If we have selected an identifier, we need to consume the
11075      operator token.  */
11076   if (id)
11077     cp_lexer_consume_token (parser->lexer);
11078   /* Otherwise, no valid operator name was present.  */
11079   else
11080     {
11081       cp_parser_error (parser, "expected operator");
11082       id = error_mark_node;
11083     }
11084
11085   return id;
11086 }
11087
11088 /* Parse a template-declaration.
11089
11090    template-declaration:
11091      export [opt] template < template-parameter-list > declaration
11092
11093    If MEMBER_P is TRUE, this template-declaration occurs within a
11094    class-specifier.
11095
11096    The grammar rule given by the standard isn't correct.  What
11097    is really meant is:
11098
11099    template-declaration:
11100      export [opt] template-parameter-list-seq
11101        decl-specifier-seq [opt] init-declarator [opt] ;
11102      export [opt] template-parameter-list-seq
11103        function-definition
11104
11105    template-parameter-list-seq:
11106      template-parameter-list-seq [opt]
11107      template < template-parameter-list >  */
11108
11109 static void
11110 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11111 {
11112   /* Check for `export'.  */
11113   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11114     {
11115       /* Consume the `export' token.  */
11116       cp_lexer_consume_token (parser->lexer);
11117       /* Warn that we do not support `export'.  */
11118       warning (0, "keyword %<export%> not implemented, and will be ignored");
11119     }
11120
11121   cp_parser_template_declaration_after_export (parser, member_p);
11122 }
11123
11124 /* Parse a template-parameter-list.
11125
11126    template-parameter-list:
11127      template-parameter
11128      template-parameter-list , template-parameter
11129
11130    Returns a TREE_LIST.  Each node represents a template parameter.
11131    The nodes are connected via their TREE_CHAINs.  */
11132
11133 static tree
11134 cp_parser_template_parameter_list (cp_parser* parser)
11135 {
11136   tree parameter_list = NULL_TREE;
11137
11138   begin_template_parm_list ();
11139
11140   /* The loop below parses the template parms.  We first need to know
11141      the total number of template parms to be able to compute proper
11142      canonical types of each dependent type. So after the loop, when
11143      we know the total number of template parms,
11144      end_template_parm_list computes the proper canonical types and
11145      fixes up the dependent types accordingly.  */
11146   while (true)
11147     {
11148       tree parameter;
11149       bool is_non_type;
11150       bool is_parameter_pack;
11151       location_t parm_loc;
11152
11153       /* Parse the template-parameter.  */
11154       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11155       parameter = cp_parser_template_parameter (parser, 
11156                                                 &is_non_type,
11157                                                 &is_parameter_pack);
11158       /* Add it to the list.  */
11159       if (parameter != error_mark_node)
11160         parameter_list = process_template_parm (parameter_list,
11161                                                 parm_loc,
11162                                                 parameter,
11163                                                 is_non_type,
11164                                                 is_parameter_pack,
11165                                                 0);
11166       else
11167        {
11168          tree err_parm = build_tree_list (parameter, parameter);
11169          parameter_list = chainon (parameter_list, err_parm);
11170        }
11171
11172       /* If the next token is not a `,', we're done.  */
11173       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11174         break;
11175       /* Otherwise, consume the `,' token.  */
11176       cp_lexer_consume_token (parser->lexer);
11177     }
11178
11179   return end_template_parm_list (parameter_list);
11180 }
11181
11182 /* Parse a template-parameter.
11183
11184    template-parameter:
11185      type-parameter
11186      parameter-declaration
11187
11188    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11189    the parameter.  The TREE_PURPOSE is the default value, if any.
11190    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11191    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11192    set to true iff this parameter is a parameter pack. */
11193
11194 static tree
11195 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11196                               bool *is_parameter_pack)
11197 {
11198   cp_token *token;
11199   cp_parameter_declarator *parameter_declarator;
11200   cp_declarator *id_declarator;
11201   tree parm;
11202
11203   /* Assume it is a type parameter or a template parameter.  */
11204   *is_non_type = false;
11205   /* Assume it not a parameter pack. */
11206   *is_parameter_pack = false;
11207   /* Peek at the next token.  */
11208   token = cp_lexer_peek_token (parser->lexer);
11209   /* If it is `class' or `template', we have a type-parameter.  */
11210   if (token->keyword == RID_TEMPLATE)
11211     return cp_parser_type_parameter (parser, is_parameter_pack);
11212   /* If it is `class' or `typename' we do not know yet whether it is a
11213      type parameter or a non-type parameter.  Consider:
11214
11215        template <typename T, typename T::X X> ...
11216
11217      or:
11218
11219        template <class C, class D*> ...
11220
11221      Here, the first parameter is a type parameter, and the second is
11222      a non-type parameter.  We can tell by looking at the token after
11223      the identifier -- if it is a `,', `=', or `>' then we have a type
11224      parameter.  */
11225   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11226     {
11227       /* Peek at the token after `class' or `typename'.  */
11228       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11229       /* If it's an ellipsis, we have a template type parameter
11230          pack. */
11231       if (token->type == CPP_ELLIPSIS)
11232         return cp_parser_type_parameter (parser, is_parameter_pack);
11233       /* If it's an identifier, skip it.  */
11234       if (token->type == CPP_NAME)
11235         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11236       /* Now, see if the token looks like the end of a template
11237          parameter.  */
11238       if (token->type == CPP_COMMA
11239           || token->type == CPP_EQ
11240           || token->type == CPP_GREATER)
11241         return cp_parser_type_parameter (parser, is_parameter_pack);
11242     }
11243
11244   /* Otherwise, it is a non-type parameter.
11245
11246      [temp.param]
11247
11248      When parsing a default template-argument for a non-type
11249      template-parameter, the first non-nested `>' is taken as the end
11250      of the template parameter-list rather than a greater-than
11251      operator.  */
11252   *is_non_type = true;
11253   parameter_declarator
11254      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11255                                         /*parenthesized_p=*/NULL);
11256
11257   /* If the parameter declaration is marked as a parameter pack, set
11258      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11259      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11260      grokdeclarator. */
11261   if (parameter_declarator
11262       && parameter_declarator->declarator
11263       && parameter_declarator->declarator->parameter_pack_p)
11264     {
11265       *is_parameter_pack = true;
11266       parameter_declarator->declarator->parameter_pack_p = false;
11267     }
11268
11269   /* If the next token is an ellipsis, and we don't already have it
11270      marked as a parameter pack, then we have a parameter pack (that
11271      has no declarator).  */
11272   if (!*is_parameter_pack
11273       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11274       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11275     {
11276       /* Consume the `...'.  */
11277       cp_lexer_consume_token (parser->lexer);
11278       maybe_warn_variadic_templates ();
11279       
11280       *is_parameter_pack = true;
11281     }
11282   /* We might end up with a pack expansion as the type of the non-type
11283      template parameter, in which case this is a non-type template
11284      parameter pack.  */
11285   else if (parameter_declarator
11286            && parameter_declarator->decl_specifiers.type
11287            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11288     {
11289       *is_parameter_pack = true;
11290       parameter_declarator->decl_specifiers.type = 
11291         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11292     }
11293
11294   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11295     {
11296       /* Parameter packs cannot have default arguments.  However, a
11297          user may try to do so, so we'll parse them and give an
11298          appropriate diagnostic here.  */
11299
11300       /* Consume the `='.  */
11301       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11302       cp_lexer_consume_token (parser->lexer);
11303       
11304       /* Find the name of the parameter pack.  */     
11305       id_declarator = parameter_declarator->declarator;
11306       while (id_declarator && id_declarator->kind != cdk_id)
11307         id_declarator = id_declarator->declarator;
11308       
11309       if (id_declarator && id_declarator->kind == cdk_id)
11310         error_at (start_token->location,
11311                   "template parameter pack %qD cannot have a default argument",
11312                   id_declarator->u.id.unqualified_name);
11313       else
11314         error_at (start_token->location,
11315                   "template parameter pack cannot have a default argument");
11316       
11317       /* Parse the default argument, but throw away the result.  */
11318       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11319     }
11320
11321   parm = grokdeclarator (parameter_declarator->declarator,
11322                          &parameter_declarator->decl_specifiers,
11323                          TPARM, /*initialized=*/0,
11324                          /*attrlist=*/NULL);
11325   if (parm == error_mark_node)
11326     return error_mark_node;
11327
11328   return build_tree_list (parameter_declarator->default_argument, parm);
11329 }
11330
11331 /* Parse a type-parameter.
11332
11333    type-parameter:
11334      class identifier [opt]
11335      class identifier [opt] = type-id
11336      typename identifier [opt]
11337      typename identifier [opt] = type-id
11338      template < template-parameter-list > class identifier [opt]
11339      template < template-parameter-list > class identifier [opt]
11340        = id-expression
11341
11342    GNU Extension (variadic templates):
11343
11344    type-parameter:
11345      class ... identifier [opt]
11346      typename ... identifier [opt]
11347
11348    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11349    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11350    the declaration of the parameter.
11351
11352    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11353
11354 static tree
11355 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11356 {
11357   cp_token *token;
11358   tree parameter;
11359
11360   /* Look for a keyword to tell us what kind of parameter this is.  */
11361   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11362   if (!token)
11363     return error_mark_node;
11364
11365   switch (token->keyword)
11366     {
11367     case RID_CLASS:
11368     case RID_TYPENAME:
11369       {
11370         tree identifier;
11371         tree default_argument;
11372
11373         /* If the next token is an ellipsis, we have a template
11374            argument pack. */
11375         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11376           {
11377             /* Consume the `...' token. */
11378             cp_lexer_consume_token (parser->lexer);
11379             maybe_warn_variadic_templates ();
11380
11381             *is_parameter_pack = true;
11382           }
11383
11384         /* If the next token is an identifier, then it names the
11385            parameter.  */
11386         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11387           identifier = cp_parser_identifier (parser);
11388         else
11389           identifier = NULL_TREE;
11390
11391         /* Create the parameter.  */
11392         parameter = finish_template_type_parm (class_type_node, identifier);
11393
11394         /* If the next token is an `=', we have a default argument.  */
11395         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11396           {
11397             /* Consume the `=' token.  */
11398             cp_lexer_consume_token (parser->lexer);
11399             /* Parse the default-argument.  */
11400             push_deferring_access_checks (dk_no_deferred);
11401             default_argument = cp_parser_type_id (parser);
11402
11403             /* Template parameter packs cannot have default
11404                arguments. */
11405             if (*is_parameter_pack)
11406               {
11407                 if (identifier)
11408                   error_at (token->location,
11409                             "template parameter pack %qD cannot have a "
11410                             "default argument", identifier);
11411                 else
11412                   error_at (token->location,
11413                             "template parameter packs cannot have "
11414                             "default arguments");
11415                 default_argument = NULL_TREE;
11416               }
11417             pop_deferring_access_checks ();
11418           }
11419         else
11420           default_argument = NULL_TREE;
11421
11422         /* Create the combined representation of the parameter and the
11423            default argument.  */
11424         parameter = build_tree_list (default_argument, parameter);
11425       }
11426       break;
11427
11428     case RID_TEMPLATE:
11429       {
11430         tree identifier;
11431         tree default_argument;
11432
11433         /* Look for the `<'.  */
11434         cp_parser_require (parser, CPP_LESS, RT_LESS);
11435         /* Parse the template-parameter-list.  */
11436         cp_parser_template_parameter_list (parser);
11437         /* Look for the `>'.  */
11438         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11439         /* Look for the `class' keyword.  */
11440         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11441         /* If the next token is an ellipsis, we have a template
11442            argument pack. */
11443         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11444           {
11445             /* Consume the `...' token. */
11446             cp_lexer_consume_token (parser->lexer);
11447             maybe_warn_variadic_templates ();
11448
11449             *is_parameter_pack = true;
11450           }
11451         /* If the next token is an `=', then there is a
11452            default-argument.  If the next token is a `>', we are at
11453            the end of the parameter-list.  If the next token is a `,',
11454            then we are at the end of this parameter.  */
11455         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11456             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11457             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11458           {
11459             identifier = cp_parser_identifier (parser);
11460             /* Treat invalid names as if the parameter were nameless.  */
11461             if (identifier == error_mark_node)
11462               identifier = NULL_TREE;
11463           }
11464         else
11465           identifier = NULL_TREE;
11466
11467         /* Create the template parameter.  */
11468         parameter = finish_template_template_parm (class_type_node,
11469                                                    identifier);
11470
11471         /* If the next token is an `=', then there is a
11472            default-argument.  */
11473         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11474           {
11475             bool is_template;
11476
11477             /* Consume the `='.  */
11478             cp_lexer_consume_token (parser->lexer);
11479             /* Parse the id-expression.  */
11480             push_deferring_access_checks (dk_no_deferred);
11481             /* save token before parsing the id-expression, for error
11482                reporting */
11483             token = cp_lexer_peek_token (parser->lexer);
11484             default_argument
11485               = cp_parser_id_expression (parser,
11486                                          /*template_keyword_p=*/false,
11487                                          /*check_dependency_p=*/true,
11488                                          /*template_p=*/&is_template,
11489                                          /*declarator_p=*/false,
11490                                          /*optional_p=*/false);
11491             if (TREE_CODE (default_argument) == TYPE_DECL)
11492               /* If the id-expression was a template-id that refers to
11493                  a template-class, we already have the declaration here,
11494                  so no further lookup is needed.  */
11495                  ;
11496             else
11497               /* Look up the name.  */
11498               default_argument
11499                 = cp_parser_lookup_name (parser, default_argument,
11500                                          none_type,
11501                                          /*is_template=*/is_template,
11502                                          /*is_namespace=*/false,
11503                                          /*check_dependency=*/true,
11504                                          /*ambiguous_decls=*/NULL,
11505                                          token->location);
11506             /* See if the default argument is valid.  */
11507             default_argument
11508               = check_template_template_default_arg (default_argument);
11509
11510             /* Template parameter packs cannot have default
11511                arguments. */
11512             if (*is_parameter_pack)
11513               {
11514                 if (identifier)
11515                   error_at (token->location,
11516                             "template parameter pack %qD cannot "
11517                             "have a default argument",
11518                             identifier);
11519                 else
11520                   error_at (token->location, "template parameter packs cannot "
11521                             "have default arguments");
11522                 default_argument = NULL_TREE;
11523               }
11524             pop_deferring_access_checks ();
11525           }
11526         else
11527           default_argument = NULL_TREE;
11528
11529         /* Create the combined representation of the parameter and the
11530            default argument.  */
11531         parameter = build_tree_list (default_argument, parameter);
11532       }
11533       break;
11534
11535     default:
11536       gcc_unreachable ();
11537       break;
11538     }
11539
11540   return parameter;
11541 }
11542
11543 /* Parse a template-id.
11544
11545    template-id:
11546      template-name < template-argument-list [opt] >
11547
11548    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11549    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11550    returned.  Otherwise, if the template-name names a function, or set
11551    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11552    names a class, returns a TYPE_DECL for the specialization.
11553
11554    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11555    uninstantiated templates.  */
11556
11557 static tree
11558 cp_parser_template_id (cp_parser *parser,
11559                        bool template_keyword_p,
11560                        bool check_dependency_p,
11561                        bool is_declaration)
11562 {
11563   int i;
11564   tree templ;
11565   tree arguments;
11566   tree template_id;
11567   cp_token_position start_of_id = 0;
11568   deferred_access_check *chk;
11569   VEC (deferred_access_check,gc) *access_check;
11570   cp_token *next_token = NULL, *next_token_2 = NULL;
11571   bool is_identifier;
11572
11573   /* If the next token corresponds to a template-id, there is no need
11574      to reparse it.  */
11575   next_token = cp_lexer_peek_token (parser->lexer);
11576   if (next_token->type == CPP_TEMPLATE_ID)
11577     {
11578       struct tree_check *check_value;
11579
11580       /* Get the stored value.  */
11581       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11582       /* Perform any access checks that were deferred.  */
11583       access_check = check_value->checks;
11584       if (access_check)
11585         {
11586           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11587             perform_or_defer_access_check (chk->binfo,
11588                                            chk->decl,
11589                                            chk->diag_decl);
11590         }
11591       /* Return the stored value.  */
11592       return check_value->value;
11593     }
11594
11595   /* Avoid performing name lookup if there is no possibility of
11596      finding a template-id.  */
11597   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11598       || (next_token->type == CPP_NAME
11599           && !cp_parser_nth_token_starts_template_argument_list_p
11600                (parser, 2)))
11601     {
11602       cp_parser_error (parser, "expected template-id");
11603       return error_mark_node;
11604     }
11605
11606   /* Remember where the template-id starts.  */
11607   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11608     start_of_id = cp_lexer_token_position (parser->lexer, false);
11609
11610   push_deferring_access_checks (dk_deferred);
11611
11612   /* Parse the template-name.  */
11613   is_identifier = false;
11614   templ = cp_parser_template_name (parser, template_keyword_p,
11615                                    check_dependency_p,
11616                                    is_declaration,
11617                                    &is_identifier);
11618   if (templ == error_mark_node || is_identifier)
11619     {
11620       pop_deferring_access_checks ();
11621       return templ;
11622     }
11623
11624   /* If we find the sequence `[:' after a template-name, it's probably
11625      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11626      parse correctly the argument list.  */
11627   next_token = cp_lexer_peek_token (parser->lexer);
11628   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11629   if (next_token->type == CPP_OPEN_SQUARE
11630       && next_token->flags & DIGRAPH
11631       && next_token_2->type == CPP_COLON
11632       && !(next_token_2->flags & PREV_WHITE))
11633     {
11634       cp_parser_parse_tentatively (parser);
11635       /* Change `:' into `::'.  */
11636       next_token_2->type = CPP_SCOPE;
11637       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11638          CPP_LESS.  */
11639       cp_lexer_consume_token (parser->lexer);
11640
11641       /* Parse the arguments.  */
11642       arguments = cp_parser_enclosed_template_argument_list (parser);
11643       if (!cp_parser_parse_definitely (parser))
11644         {
11645           /* If we couldn't parse an argument list, then we revert our changes
11646              and return simply an error. Maybe this is not a template-id
11647              after all.  */
11648           next_token_2->type = CPP_COLON;
11649           cp_parser_error (parser, "expected %<<%>");
11650           pop_deferring_access_checks ();
11651           return error_mark_node;
11652         }
11653       /* Otherwise, emit an error about the invalid digraph, but continue
11654          parsing because we got our argument list.  */
11655       if (permerror (next_token->location,
11656                      "%<<::%> cannot begin a template-argument list"))
11657         {
11658           static bool hint = false;
11659           inform (next_token->location,
11660                   "%<<:%> is an alternate spelling for %<[%>."
11661                   " Insert whitespace between %<<%> and %<::%>");
11662           if (!hint && !flag_permissive)
11663             {
11664               inform (next_token->location, "(if you use %<-fpermissive%>"
11665                       " G++ will accept your code)");
11666               hint = true;
11667             }
11668         }
11669     }
11670   else
11671     {
11672       /* Look for the `<' that starts the template-argument-list.  */
11673       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11674         {
11675           pop_deferring_access_checks ();
11676           return error_mark_node;
11677         }
11678       /* Parse the arguments.  */
11679       arguments = cp_parser_enclosed_template_argument_list (parser);
11680     }
11681
11682   /* Build a representation of the specialization.  */
11683   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11684     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11685   else if (DECL_CLASS_TEMPLATE_P (templ)
11686            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11687     {
11688       bool entering_scope;
11689       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11690          template (rather than some instantiation thereof) only if
11691          is not nested within some other construct.  For example, in
11692          "template <typename T> void f(T) { A<T>::", A<T> is just an
11693          instantiation of A.  */
11694       entering_scope = (template_parm_scope_p ()
11695                         && cp_lexer_next_token_is (parser->lexer,
11696                                                    CPP_SCOPE));
11697       template_id
11698         = finish_template_type (templ, arguments, entering_scope);
11699     }
11700   else
11701     {
11702       /* If it's not a class-template or a template-template, it should be
11703          a function-template.  */
11704       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11705                    || TREE_CODE (templ) == OVERLOAD
11706                    || BASELINK_P (templ)));
11707
11708       template_id = lookup_template_function (templ, arguments);
11709     }
11710
11711   /* If parsing tentatively, replace the sequence of tokens that makes
11712      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11713      should we re-parse the token stream, we will not have to repeat
11714      the effort required to do the parse, nor will we issue duplicate
11715      error messages about problems during instantiation of the
11716      template.  */
11717   if (start_of_id)
11718     {
11719       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11720
11721       /* Reset the contents of the START_OF_ID token.  */
11722       token->type = CPP_TEMPLATE_ID;
11723       /* Retrieve any deferred checks.  Do not pop this access checks yet
11724          so the memory will not be reclaimed during token replacing below.  */
11725       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11726       token->u.tree_check_value->value = template_id;
11727       token->u.tree_check_value->checks = get_deferred_access_checks ();
11728       token->keyword = RID_MAX;
11729
11730       /* Purge all subsequent tokens.  */
11731       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11732
11733       /* ??? Can we actually assume that, if template_id ==
11734          error_mark_node, we will have issued a diagnostic to the
11735          user, as opposed to simply marking the tentative parse as
11736          failed?  */
11737       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11738         error_at (token->location, "parse error in template argument list");
11739     }
11740
11741   pop_deferring_access_checks ();
11742   return template_id;
11743 }
11744
11745 /* Parse a template-name.
11746
11747    template-name:
11748      identifier
11749
11750    The standard should actually say:
11751
11752    template-name:
11753      identifier
11754      operator-function-id
11755
11756    A defect report has been filed about this issue.
11757
11758    A conversion-function-id cannot be a template name because they cannot
11759    be part of a template-id. In fact, looking at this code:
11760
11761    a.operator K<int>()
11762
11763    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11764    It is impossible to call a templated conversion-function-id with an
11765    explicit argument list, since the only allowed template parameter is
11766    the type to which it is converting.
11767
11768    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11769    `template' keyword, in a construction like:
11770
11771      T::template f<3>()
11772
11773    In that case `f' is taken to be a template-name, even though there
11774    is no way of knowing for sure.
11775
11776    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11777    name refers to a set of overloaded functions, at least one of which
11778    is a template, or an IDENTIFIER_NODE with the name of the template,
11779    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11780    names are looked up inside uninstantiated templates.  */
11781
11782 static tree
11783 cp_parser_template_name (cp_parser* parser,
11784                          bool template_keyword_p,
11785                          bool check_dependency_p,
11786                          bool is_declaration,
11787                          bool *is_identifier)
11788 {
11789   tree identifier;
11790   tree decl;
11791   tree fns;
11792   cp_token *token = cp_lexer_peek_token (parser->lexer);
11793
11794   /* If the next token is `operator', then we have either an
11795      operator-function-id or a conversion-function-id.  */
11796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11797     {
11798       /* We don't know whether we're looking at an
11799          operator-function-id or a conversion-function-id.  */
11800       cp_parser_parse_tentatively (parser);
11801       /* Try an operator-function-id.  */
11802       identifier = cp_parser_operator_function_id (parser);
11803       /* If that didn't work, try a conversion-function-id.  */
11804       if (!cp_parser_parse_definitely (parser))
11805         {
11806           cp_parser_error (parser, "expected template-name");
11807           return error_mark_node;
11808         }
11809     }
11810   /* Look for the identifier.  */
11811   else
11812     identifier = cp_parser_identifier (parser);
11813
11814   /* If we didn't find an identifier, we don't have a template-id.  */
11815   if (identifier == error_mark_node)
11816     return error_mark_node;
11817
11818   /* If the name immediately followed the `template' keyword, then it
11819      is a template-name.  However, if the next token is not `<', then
11820      we do not treat it as a template-name, since it is not being used
11821      as part of a template-id.  This enables us to handle constructs
11822      like:
11823
11824        template <typename T> struct S { S(); };
11825        template <typename T> S<T>::S();
11826
11827      correctly.  We would treat `S' as a template -- if it were `S<T>'
11828      -- but we do not if there is no `<'.  */
11829
11830   if (processing_template_decl
11831       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11832     {
11833       /* In a declaration, in a dependent context, we pretend that the
11834          "template" keyword was present in order to improve error
11835          recovery.  For example, given:
11836
11837            template <typename T> void f(T::X<int>);
11838
11839          we want to treat "X<int>" as a template-id.  */
11840       if (is_declaration
11841           && !template_keyword_p
11842           && parser->scope && TYPE_P (parser->scope)
11843           && check_dependency_p
11844           && dependent_scope_p (parser->scope)
11845           /* Do not do this for dtors (or ctors), since they never
11846              need the template keyword before their name.  */
11847           && !constructor_name_p (identifier, parser->scope))
11848         {
11849           cp_token_position start = 0;
11850
11851           /* Explain what went wrong.  */
11852           error_at (token->location, "non-template %qD used as template",
11853                     identifier);
11854           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11855                   parser->scope, identifier);
11856           /* If parsing tentatively, find the location of the "<" token.  */
11857           if (cp_parser_simulate_error (parser))
11858             start = cp_lexer_token_position (parser->lexer, true);
11859           /* Parse the template arguments so that we can issue error
11860              messages about them.  */
11861           cp_lexer_consume_token (parser->lexer);
11862           cp_parser_enclosed_template_argument_list (parser);
11863           /* Skip tokens until we find a good place from which to
11864              continue parsing.  */
11865           cp_parser_skip_to_closing_parenthesis (parser,
11866                                                  /*recovering=*/true,
11867                                                  /*or_comma=*/true,
11868                                                  /*consume_paren=*/false);
11869           /* If parsing tentatively, permanently remove the
11870              template argument list.  That will prevent duplicate
11871              error messages from being issued about the missing
11872              "template" keyword.  */
11873           if (start)
11874             cp_lexer_purge_tokens_after (parser->lexer, start);
11875           if (is_identifier)
11876             *is_identifier = true;
11877           return identifier;
11878         }
11879
11880       /* If the "template" keyword is present, then there is generally
11881          no point in doing name-lookup, so we just return IDENTIFIER.
11882          But, if the qualifying scope is non-dependent then we can
11883          (and must) do name-lookup normally.  */
11884       if (template_keyword_p
11885           && (!parser->scope
11886               || (TYPE_P (parser->scope)
11887                   && dependent_type_p (parser->scope))))
11888         return identifier;
11889     }
11890
11891   /* Look up the name.  */
11892   decl = cp_parser_lookup_name (parser, identifier,
11893                                 none_type,
11894                                 /*is_template=*/true,
11895                                 /*is_namespace=*/false,
11896                                 check_dependency_p,
11897                                 /*ambiguous_decls=*/NULL,
11898                                 token->location);
11899
11900   /* If DECL is a template, then the name was a template-name.  */
11901   if (TREE_CODE (decl) == TEMPLATE_DECL)
11902     ;
11903   else
11904     {
11905       tree fn = NULL_TREE;
11906
11907       /* The standard does not explicitly indicate whether a name that
11908          names a set of overloaded declarations, some of which are
11909          templates, is a template-name.  However, such a name should
11910          be a template-name; otherwise, there is no way to form a
11911          template-id for the overloaded templates.  */
11912       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11913       if (TREE_CODE (fns) == OVERLOAD)
11914         for (fn = fns; fn; fn = OVL_NEXT (fn))
11915           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11916             break;
11917
11918       if (!fn)
11919         {
11920           /* The name does not name a template.  */
11921           cp_parser_error (parser, "expected template-name");
11922           return error_mark_node;
11923         }
11924     }
11925
11926   /* If DECL is dependent, and refers to a function, then just return
11927      its name; we will look it up again during template instantiation.  */
11928   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11929     {
11930       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11931       if (TYPE_P (scope) && dependent_type_p (scope))
11932         return identifier;
11933     }
11934
11935   return decl;
11936 }
11937
11938 /* Parse a template-argument-list.
11939
11940    template-argument-list:
11941      template-argument ... [opt]
11942      template-argument-list , template-argument ... [opt]
11943
11944    Returns a TREE_VEC containing the arguments.  */
11945
11946 static tree
11947 cp_parser_template_argument_list (cp_parser* parser)
11948 {
11949   tree fixed_args[10];
11950   unsigned n_args = 0;
11951   unsigned alloced = 10;
11952   tree *arg_ary = fixed_args;
11953   tree vec;
11954   bool saved_in_template_argument_list_p;
11955   bool saved_ice_p;
11956   bool saved_non_ice_p;
11957
11958   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11959   parser->in_template_argument_list_p = true;
11960   /* Even if the template-id appears in an integral
11961      constant-expression, the contents of the argument list do
11962      not.  */
11963   saved_ice_p = parser->integral_constant_expression_p;
11964   parser->integral_constant_expression_p = false;
11965   saved_non_ice_p = parser->non_integral_constant_expression_p;
11966   parser->non_integral_constant_expression_p = false;
11967   /* Parse the arguments.  */
11968   do
11969     {
11970       tree argument;
11971
11972       if (n_args)
11973         /* Consume the comma.  */
11974         cp_lexer_consume_token (parser->lexer);
11975
11976       /* Parse the template-argument.  */
11977       argument = cp_parser_template_argument (parser);
11978
11979       /* If the next token is an ellipsis, we're expanding a template
11980          argument pack. */
11981       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11982         {
11983           if (argument == error_mark_node)
11984             {
11985               cp_token *token = cp_lexer_peek_token (parser->lexer);
11986               error_at (token->location,
11987                         "expected parameter pack before %<...%>");
11988             }
11989           /* Consume the `...' token. */
11990           cp_lexer_consume_token (parser->lexer);
11991
11992           /* Make the argument into a TYPE_PACK_EXPANSION or
11993              EXPR_PACK_EXPANSION. */
11994           argument = make_pack_expansion (argument);
11995         }
11996
11997       if (n_args == alloced)
11998         {
11999           alloced *= 2;
12000
12001           if (arg_ary == fixed_args)
12002             {
12003               arg_ary = XNEWVEC (tree, alloced);
12004               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12005             }
12006           else
12007             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12008         }
12009       arg_ary[n_args++] = argument;
12010     }
12011   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12012
12013   vec = make_tree_vec (n_args);
12014
12015   while (n_args--)
12016     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12017
12018   if (arg_ary != fixed_args)
12019     free (arg_ary);
12020   parser->non_integral_constant_expression_p = saved_non_ice_p;
12021   parser->integral_constant_expression_p = saved_ice_p;
12022   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12023 #ifdef ENABLE_CHECKING
12024   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12025 #endif
12026   return vec;
12027 }
12028
12029 /* Parse a template-argument.
12030
12031    template-argument:
12032      assignment-expression
12033      type-id
12034      id-expression
12035
12036    The representation is that of an assignment-expression, type-id, or
12037    id-expression -- except that the qualified id-expression is
12038    evaluated, so that the value returned is either a DECL or an
12039    OVERLOAD.
12040
12041    Although the standard says "assignment-expression", it forbids
12042    throw-expressions or assignments in the template argument.
12043    Therefore, we use "conditional-expression" instead.  */
12044
12045 static tree
12046 cp_parser_template_argument (cp_parser* parser)
12047 {
12048   tree argument;
12049   bool template_p;
12050   bool address_p;
12051   bool maybe_type_id = false;
12052   cp_token *token = NULL, *argument_start_token = NULL;
12053   cp_id_kind idk;
12054
12055   /* There's really no way to know what we're looking at, so we just
12056      try each alternative in order.
12057
12058        [temp.arg]
12059
12060        In a template-argument, an ambiguity between a type-id and an
12061        expression is resolved to a type-id, regardless of the form of
12062        the corresponding template-parameter.
12063
12064      Therefore, we try a type-id first.  */
12065   cp_parser_parse_tentatively (parser);
12066   argument = cp_parser_template_type_arg (parser);
12067   /* If there was no error parsing the type-id but the next token is a
12068      '>>', our behavior depends on which dialect of C++ we're
12069      parsing. In C++98, we probably found a typo for '> >'. But there
12070      are type-id which are also valid expressions. For instance:
12071
12072      struct X { int operator >> (int); };
12073      template <int V> struct Foo {};
12074      Foo<X () >> 5> r;
12075
12076      Here 'X()' is a valid type-id of a function type, but the user just
12077      wanted to write the expression "X() >> 5". Thus, we remember that we
12078      found a valid type-id, but we still try to parse the argument as an
12079      expression to see what happens. 
12080
12081      In C++0x, the '>>' will be considered two separate '>'
12082      tokens.  */
12083   if (!cp_parser_error_occurred (parser)
12084       && cxx_dialect == cxx98
12085       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12086     {
12087       maybe_type_id = true;
12088       cp_parser_abort_tentative_parse (parser);
12089     }
12090   else
12091     {
12092       /* If the next token isn't a `,' or a `>', then this argument wasn't
12093       really finished. This means that the argument is not a valid
12094       type-id.  */
12095       if (!cp_parser_next_token_ends_template_argument_p (parser))
12096         cp_parser_error (parser, "expected template-argument");
12097       /* If that worked, we're done.  */
12098       if (cp_parser_parse_definitely (parser))
12099         return argument;
12100     }
12101   /* We're still not sure what the argument will be.  */
12102   cp_parser_parse_tentatively (parser);
12103   /* Try a template.  */
12104   argument_start_token = cp_lexer_peek_token (parser->lexer);
12105   argument = cp_parser_id_expression (parser,
12106                                       /*template_keyword_p=*/false,
12107                                       /*check_dependency_p=*/true,
12108                                       &template_p,
12109                                       /*declarator_p=*/false,
12110                                       /*optional_p=*/false);
12111   /* If the next token isn't a `,' or a `>', then this argument wasn't
12112      really finished.  */
12113   if (!cp_parser_next_token_ends_template_argument_p (parser))
12114     cp_parser_error (parser, "expected template-argument");
12115   if (!cp_parser_error_occurred (parser))
12116     {
12117       /* Figure out what is being referred to.  If the id-expression
12118          was for a class template specialization, then we will have a
12119          TYPE_DECL at this point.  There is no need to do name lookup
12120          at this point in that case.  */
12121       if (TREE_CODE (argument) != TYPE_DECL)
12122         argument = cp_parser_lookup_name (parser, argument,
12123                                           none_type,
12124                                           /*is_template=*/template_p,
12125                                           /*is_namespace=*/false,
12126                                           /*check_dependency=*/true,
12127                                           /*ambiguous_decls=*/NULL,
12128                                           argument_start_token->location);
12129       if (TREE_CODE (argument) != TEMPLATE_DECL
12130           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12131         cp_parser_error (parser, "expected template-name");
12132     }
12133   if (cp_parser_parse_definitely (parser))
12134     return argument;
12135   /* It must be a non-type argument.  There permitted cases are given
12136      in [temp.arg.nontype]:
12137
12138      -- an integral constant-expression of integral or enumeration
12139         type; or
12140
12141      -- the name of a non-type template-parameter; or
12142
12143      -- the name of an object or function with external linkage...
12144
12145      -- the address of an object or function with external linkage...
12146
12147      -- a pointer to member...  */
12148   /* Look for a non-type template parameter.  */
12149   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12150     {
12151       cp_parser_parse_tentatively (parser);
12152       argument = cp_parser_primary_expression (parser,
12153                                                /*address_p=*/false,
12154                                                /*cast_p=*/false,
12155                                                /*template_arg_p=*/true,
12156                                                &idk);
12157       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12158           || !cp_parser_next_token_ends_template_argument_p (parser))
12159         cp_parser_simulate_error (parser);
12160       if (cp_parser_parse_definitely (parser))
12161         return argument;
12162     }
12163
12164   /* If the next token is "&", the argument must be the address of an
12165      object or function with external linkage.  */
12166   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12167   if (address_p)
12168     cp_lexer_consume_token (parser->lexer);
12169   /* See if we might have an id-expression.  */
12170   token = cp_lexer_peek_token (parser->lexer);
12171   if (token->type == CPP_NAME
12172       || token->keyword == RID_OPERATOR
12173       || token->type == CPP_SCOPE
12174       || token->type == CPP_TEMPLATE_ID
12175       || token->type == CPP_NESTED_NAME_SPECIFIER)
12176     {
12177       cp_parser_parse_tentatively (parser);
12178       argument = cp_parser_primary_expression (parser,
12179                                                address_p,
12180                                                /*cast_p=*/false,
12181                                                /*template_arg_p=*/true,
12182                                                &idk);
12183       if (cp_parser_error_occurred (parser)
12184           || !cp_parser_next_token_ends_template_argument_p (parser))
12185         cp_parser_abort_tentative_parse (parser);
12186       else
12187         {
12188           tree probe;
12189
12190           if (TREE_CODE (argument) == INDIRECT_REF)
12191             {
12192               gcc_assert (REFERENCE_REF_P (argument));
12193               argument = TREE_OPERAND (argument, 0);
12194             }
12195
12196           /* If we're in a template, we represent a qualified-id referring
12197              to a static data member as a SCOPE_REF even if the scope isn't
12198              dependent so that we can check access control later.  */
12199           probe = argument;
12200           if (TREE_CODE (probe) == SCOPE_REF)
12201             probe = TREE_OPERAND (probe, 1);
12202           if (TREE_CODE (probe) == VAR_DECL)
12203             {
12204               /* A variable without external linkage might still be a
12205                  valid constant-expression, so no error is issued here
12206                  if the external-linkage check fails.  */
12207               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12208                 cp_parser_simulate_error (parser);
12209             }
12210           else if (is_overloaded_fn (argument))
12211             /* All overloaded functions are allowed; if the external
12212                linkage test does not pass, an error will be issued
12213                later.  */
12214             ;
12215           else if (address_p
12216                    && (TREE_CODE (argument) == OFFSET_REF
12217                        || TREE_CODE (argument) == SCOPE_REF))
12218             /* A pointer-to-member.  */
12219             ;
12220           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12221             ;
12222           else
12223             cp_parser_simulate_error (parser);
12224
12225           if (cp_parser_parse_definitely (parser))
12226             {
12227               if (address_p)
12228                 argument = build_x_unary_op (ADDR_EXPR, argument,
12229                                              tf_warning_or_error);
12230               return argument;
12231             }
12232         }
12233     }
12234   /* If the argument started with "&", there are no other valid
12235      alternatives at this point.  */
12236   if (address_p)
12237     {
12238       cp_parser_error (parser, "invalid non-type template argument");
12239       return error_mark_node;
12240     }
12241
12242   /* If the argument wasn't successfully parsed as a type-id followed
12243      by '>>', the argument can only be a constant expression now.
12244      Otherwise, we try parsing the constant-expression tentatively,
12245      because the argument could really be a type-id.  */
12246   if (maybe_type_id)
12247     cp_parser_parse_tentatively (parser);
12248   argument = cp_parser_constant_expression (parser,
12249                                             /*allow_non_constant_p=*/false,
12250                                             /*non_constant_p=*/NULL);
12251   argument = fold_non_dependent_expr (argument);
12252   if (!maybe_type_id)
12253     return argument;
12254   if (!cp_parser_next_token_ends_template_argument_p (parser))
12255     cp_parser_error (parser, "expected template-argument");
12256   if (cp_parser_parse_definitely (parser))
12257     return argument;
12258   /* We did our best to parse the argument as a non type-id, but that
12259      was the only alternative that matched (albeit with a '>' after
12260      it). We can assume it's just a typo from the user, and a
12261      diagnostic will then be issued.  */
12262   return cp_parser_template_type_arg (parser);
12263 }
12264
12265 /* Parse an explicit-instantiation.
12266
12267    explicit-instantiation:
12268      template declaration
12269
12270    Although the standard says `declaration', what it really means is:
12271
12272    explicit-instantiation:
12273      template decl-specifier-seq [opt] declarator [opt] ;
12274
12275    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12276    supposed to be allowed.  A defect report has been filed about this
12277    issue.
12278
12279    GNU Extension:
12280
12281    explicit-instantiation:
12282      storage-class-specifier template
12283        decl-specifier-seq [opt] declarator [opt] ;
12284      function-specifier template
12285        decl-specifier-seq [opt] declarator [opt] ;  */
12286
12287 static void
12288 cp_parser_explicit_instantiation (cp_parser* parser)
12289 {
12290   int declares_class_or_enum;
12291   cp_decl_specifier_seq decl_specifiers;
12292   tree extension_specifier = NULL_TREE;
12293
12294   /* Look for an (optional) storage-class-specifier or
12295      function-specifier.  */
12296   if (cp_parser_allow_gnu_extensions_p (parser))
12297     {
12298       extension_specifier
12299         = cp_parser_storage_class_specifier_opt (parser);
12300       if (!extension_specifier)
12301         extension_specifier
12302           = cp_parser_function_specifier_opt (parser,
12303                                               /*decl_specs=*/NULL);
12304     }
12305
12306   /* Look for the `template' keyword.  */
12307   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12308   /* Let the front end know that we are processing an explicit
12309      instantiation.  */
12310   begin_explicit_instantiation ();
12311   /* [temp.explicit] says that we are supposed to ignore access
12312      control while processing explicit instantiation directives.  */
12313   push_deferring_access_checks (dk_no_check);
12314   /* Parse a decl-specifier-seq.  */
12315   cp_parser_decl_specifier_seq (parser,
12316                                 CP_PARSER_FLAGS_OPTIONAL,
12317                                 &decl_specifiers,
12318                                 &declares_class_or_enum);
12319   /* If there was exactly one decl-specifier, and it declared a class,
12320      and there's no declarator, then we have an explicit type
12321      instantiation.  */
12322   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12323     {
12324       tree type;
12325
12326       type = check_tag_decl (&decl_specifiers);
12327       /* Turn access control back on for names used during
12328          template instantiation.  */
12329       pop_deferring_access_checks ();
12330       if (type)
12331         do_type_instantiation (type, extension_specifier,
12332                                /*complain=*/tf_error);
12333     }
12334   else
12335     {
12336       cp_declarator *declarator;
12337       tree decl;
12338
12339       /* Parse the declarator.  */
12340       declarator
12341         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12342                                 /*ctor_dtor_or_conv_p=*/NULL,
12343                                 /*parenthesized_p=*/NULL,
12344                                 /*member_p=*/false);
12345       if (declares_class_or_enum & 2)
12346         cp_parser_check_for_definition_in_return_type (declarator,
12347                                                        decl_specifiers.type,
12348                                                        decl_specifiers.type_location);
12349       if (declarator != cp_error_declarator)
12350         {
12351           if (decl_specifiers.specs[(int)ds_inline])
12352             permerror (input_location, "explicit instantiation shall not use"
12353                        " %<inline%> specifier");
12354           if (decl_specifiers.specs[(int)ds_constexpr])
12355             permerror (input_location, "explicit instantiation shall not use"
12356                        " %<constexpr%> specifier");
12357
12358           decl = grokdeclarator (declarator, &decl_specifiers,
12359                                  NORMAL, 0, &decl_specifiers.attributes);
12360           /* Turn access control back on for names used during
12361              template instantiation.  */
12362           pop_deferring_access_checks ();
12363           /* Do the explicit instantiation.  */
12364           do_decl_instantiation (decl, extension_specifier);
12365         }
12366       else
12367         {
12368           pop_deferring_access_checks ();
12369           /* Skip the body of the explicit instantiation.  */
12370           cp_parser_skip_to_end_of_statement (parser);
12371         }
12372     }
12373   /* We're done with the instantiation.  */
12374   end_explicit_instantiation ();
12375
12376   cp_parser_consume_semicolon_at_end_of_statement (parser);
12377 }
12378
12379 /* Parse an explicit-specialization.
12380
12381    explicit-specialization:
12382      template < > declaration
12383
12384    Although the standard says `declaration', what it really means is:
12385
12386    explicit-specialization:
12387      template <> decl-specifier [opt] init-declarator [opt] ;
12388      template <> function-definition
12389      template <> explicit-specialization
12390      template <> template-declaration  */
12391
12392 static void
12393 cp_parser_explicit_specialization (cp_parser* parser)
12394 {
12395   bool need_lang_pop;
12396   cp_token *token = cp_lexer_peek_token (parser->lexer);
12397
12398   /* Look for the `template' keyword.  */
12399   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12400   /* Look for the `<'.  */
12401   cp_parser_require (parser, CPP_LESS, RT_LESS);
12402   /* Look for the `>'.  */
12403   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12404   /* We have processed another parameter list.  */
12405   ++parser->num_template_parameter_lists;
12406   /* [temp]
12407
12408      A template ... explicit specialization ... shall not have C
12409      linkage.  */
12410   if (current_lang_name == lang_name_c)
12411     {
12412       error_at (token->location, "template specialization with C linkage");
12413       /* Give it C++ linkage to avoid confusing other parts of the
12414          front end.  */
12415       push_lang_context (lang_name_cplusplus);
12416       need_lang_pop = true;
12417     }
12418   else
12419     need_lang_pop = false;
12420   /* Let the front end know that we are beginning a specialization.  */
12421   if (!begin_specialization ())
12422     {
12423       end_specialization ();
12424       return;
12425     }
12426
12427   /* If the next keyword is `template', we need to figure out whether
12428      or not we're looking a template-declaration.  */
12429   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12430     {
12431       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12432           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12433         cp_parser_template_declaration_after_export (parser,
12434                                                      /*member_p=*/false);
12435       else
12436         cp_parser_explicit_specialization (parser);
12437     }
12438   else
12439     /* Parse the dependent declaration.  */
12440     cp_parser_single_declaration (parser,
12441                                   /*checks=*/NULL,
12442                                   /*member_p=*/false,
12443                                   /*explicit_specialization_p=*/true,
12444                                   /*friend_p=*/NULL);
12445   /* We're done with the specialization.  */
12446   end_specialization ();
12447   /* For the erroneous case of a template with C linkage, we pushed an
12448      implicit C++ linkage scope; exit that scope now.  */
12449   if (need_lang_pop)
12450     pop_lang_context ();
12451   /* We're done with this parameter list.  */
12452   --parser->num_template_parameter_lists;
12453 }
12454
12455 /* Parse a type-specifier.
12456
12457    type-specifier:
12458      simple-type-specifier
12459      class-specifier
12460      enum-specifier
12461      elaborated-type-specifier
12462      cv-qualifier
12463
12464    GNU Extension:
12465
12466    type-specifier:
12467      __complex__
12468
12469    Returns a representation of the type-specifier.  For a
12470    class-specifier, enum-specifier, or elaborated-type-specifier, a
12471    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12472
12473    The parser flags FLAGS is used to control type-specifier parsing.
12474
12475    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12476    in a decl-specifier-seq.
12477
12478    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12479    class-specifier, enum-specifier, or elaborated-type-specifier, then
12480    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12481    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12482    zero.
12483
12484    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12485    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12486    is set to FALSE.  */
12487
12488 static tree
12489 cp_parser_type_specifier (cp_parser* parser,
12490                           cp_parser_flags flags,
12491                           cp_decl_specifier_seq *decl_specs,
12492                           bool is_declaration,
12493                           int* declares_class_or_enum,
12494                           bool* is_cv_qualifier)
12495 {
12496   tree type_spec = NULL_TREE;
12497   cp_token *token;
12498   enum rid keyword;
12499   cp_decl_spec ds = ds_last;
12500
12501   /* Assume this type-specifier does not declare a new type.  */
12502   if (declares_class_or_enum)
12503     *declares_class_or_enum = 0;
12504   /* And that it does not specify a cv-qualifier.  */
12505   if (is_cv_qualifier)
12506     *is_cv_qualifier = false;
12507   /* Peek at the next token.  */
12508   token = cp_lexer_peek_token (parser->lexer);
12509
12510   /* If we're looking at a keyword, we can use that to guide the
12511      production we choose.  */
12512   keyword = token->keyword;
12513   switch (keyword)
12514     {
12515     case RID_ENUM:
12516       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12517         goto elaborated_type_specifier;
12518
12519       /* Look for the enum-specifier.  */
12520       type_spec = cp_parser_enum_specifier (parser);
12521       /* If that worked, we're done.  */
12522       if (type_spec)
12523         {
12524           if (declares_class_or_enum)
12525             *declares_class_or_enum = 2;
12526           if (decl_specs)
12527             cp_parser_set_decl_spec_type (decl_specs,
12528                                           type_spec,
12529                                           token->location,
12530                                           /*user_defined_p=*/true);
12531           return type_spec;
12532         }
12533       else
12534         goto elaborated_type_specifier;
12535
12536       /* Any of these indicate either a class-specifier, or an
12537          elaborated-type-specifier.  */
12538     case RID_CLASS:
12539     case RID_STRUCT:
12540     case RID_UNION:
12541       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12542         goto elaborated_type_specifier;
12543
12544       /* Parse tentatively so that we can back up if we don't find a
12545          class-specifier.  */
12546       cp_parser_parse_tentatively (parser);
12547       /* Look for the class-specifier.  */
12548       type_spec = cp_parser_class_specifier (parser);
12549       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12550       /* If that worked, we're done.  */
12551       if (cp_parser_parse_definitely (parser))
12552         {
12553           if (declares_class_or_enum)
12554             *declares_class_or_enum = 2;
12555           if (decl_specs)
12556             cp_parser_set_decl_spec_type (decl_specs,
12557                                           type_spec,
12558                                           token->location,
12559                                           /*user_defined_p=*/true);
12560           return type_spec;
12561         }
12562
12563       /* Fall through.  */
12564     elaborated_type_specifier:
12565       /* We're declaring (not defining) a class or enum.  */
12566       if (declares_class_or_enum)
12567         *declares_class_or_enum = 1;
12568
12569       /* Fall through.  */
12570     case RID_TYPENAME:
12571       /* Look for an elaborated-type-specifier.  */
12572       type_spec
12573         = (cp_parser_elaborated_type_specifier
12574            (parser,
12575             decl_specs && decl_specs->specs[(int) ds_friend],
12576             is_declaration));
12577       if (decl_specs)
12578         cp_parser_set_decl_spec_type (decl_specs,
12579                                       type_spec,
12580                                       token->location,
12581                                       /*user_defined_p=*/true);
12582       return type_spec;
12583
12584     case RID_CONST:
12585       ds = ds_const;
12586       if (is_cv_qualifier)
12587         *is_cv_qualifier = true;
12588       break;
12589
12590     case RID_VOLATILE:
12591       ds = ds_volatile;
12592       if (is_cv_qualifier)
12593         *is_cv_qualifier = true;
12594       break;
12595
12596     case RID_RESTRICT:
12597       ds = ds_restrict;
12598       if (is_cv_qualifier)
12599         *is_cv_qualifier = true;
12600       break;
12601
12602     case RID_COMPLEX:
12603       /* The `__complex__' keyword is a GNU extension.  */
12604       ds = ds_complex;
12605       break;
12606
12607     default:
12608       break;
12609     }
12610
12611   /* Handle simple keywords.  */
12612   if (ds != ds_last)
12613     {
12614       if (decl_specs)
12615         {
12616           ++decl_specs->specs[(int)ds];
12617           decl_specs->any_specifiers_p = true;
12618         }
12619       return cp_lexer_consume_token (parser->lexer)->u.value;
12620     }
12621
12622   /* If we do not already have a type-specifier, assume we are looking
12623      at a simple-type-specifier.  */
12624   type_spec = cp_parser_simple_type_specifier (parser,
12625                                                decl_specs,
12626                                                flags);
12627
12628   /* If we didn't find a type-specifier, and a type-specifier was not
12629      optional in this context, issue an error message.  */
12630   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12631     {
12632       cp_parser_error (parser, "expected type specifier");
12633       return error_mark_node;
12634     }
12635
12636   return type_spec;
12637 }
12638
12639 /* Parse a simple-type-specifier.
12640
12641    simple-type-specifier:
12642      :: [opt] nested-name-specifier [opt] type-name
12643      :: [opt] nested-name-specifier template template-id
12644      char
12645      wchar_t
12646      bool
12647      short
12648      int
12649      long
12650      signed
12651      unsigned
12652      float
12653      double
12654      void
12655
12656    C++0x Extension:
12657
12658    simple-type-specifier:
12659      auto
12660      decltype ( expression )   
12661      char16_t
12662      char32_t
12663
12664    GNU Extension:
12665
12666    simple-type-specifier:
12667      __int128
12668      __typeof__ unary-expression
12669      __typeof__ ( type-id )
12670
12671    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12672    appropriately updated.  */
12673
12674 static tree
12675 cp_parser_simple_type_specifier (cp_parser* parser,
12676                                  cp_decl_specifier_seq *decl_specs,
12677                                  cp_parser_flags flags)
12678 {
12679   tree type = NULL_TREE;
12680   cp_token *token;
12681
12682   /* Peek at the next token.  */
12683   token = cp_lexer_peek_token (parser->lexer);
12684
12685   /* If we're looking at a keyword, things are easy.  */
12686   switch (token->keyword)
12687     {
12688     case RID_CHAR:
12689       if (decl_specs)
12690         decl_specs->explicit_char_p = true;
12691       type = char_type_node;
12692       break;
12693     case RID_CHAR16:
12694       type = char16_type_node;
12695       break;
12696     case RID_CHAR32:
12697       type = char32_type_node;
12698       break;
12699     case RID_WCHAR:
12700       type = wchar_type_node;
12701       break;
12702     case RID_BOOL:
12703       type = boolean_type_node;
12704       break;
12705     case RID_SHORT:
12706       if (decl_specs)
12707         ++decl_specs->specs[(int) ds_short];
12708       type = short_integer_type_node;
12709       break;
12710     case RID_INT:
12711       if (decl_specs)
12712         decl_specs->explicit_int_p = true;
12713       type = integer_type_node;
12714       break;
12715     case RID_INT128:
12716       if (!int128_integer_type_node)
12717         break;
12718       if (decl_specs)
12719         decl_specs->explicit_int128_p = true;
12720       type = int128_integer_type_node;
12721       break;
12722     case RID_LONG:
12723       if (decl_specs)
12724         ++decl_specs->specs[(int) ds_long];
12725       type = long_integer_type_node;
12726       break;
12727     case RID_SIGNED:
12728       if (decl_specs)
12729         ++decl_specs->specs[(int) ds_signed];
12730       type = integer_type_node;
12731       break;
12732     case RID_UNSIGNED:
12733       if (decl_specs)
12734         ++decl_specs->specs[(int) ds_unsigned];
12735       type = unsigned_type_node;
12736       break;
12737     case RID_FLOAT:
12738       type = float_type_node;
12739       break;
12740     case RID_DOUBLE:
12741       type = double_type_node;
12742       break;
12743     case RID_VOID:
12744       type = void_type_node;
12745       break;
12746       
12747     case RID_AUTO:
12748       maybe_warn_cpp0x (CPP0X_AUTO);
12749       type = make_auto ();
12750       break;
12751
12752     case RID_DECLTYPE:
12753       /* Parse the `decltype' type.  */
12754       type = cp_parser_decltype (parser);
12755
12756       if (decl_specs)
12757         cp_parser_set_decl_spec_type (decl_specs, type,
12758                                       token->location,
12759                                       /*user_defined_p=*/true);
12760
12761       return type;
12762
12763     case RID_TYPEOF:
12764       /* Consume the `typeof' token.  */
12765       cp_lexer_consume_token (parser->lexer);
12766       /* Parse the operand to `typeof'.  */
12767       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12768       /* If it is not already a TYPE, take its type.  */
12769       if (!TYPE_P (type))
12770         type = finish_typeof (type);
12771
12772       if (decl_specs)
12773         cp_parser_set_decl_spec_type (decl_specs, type,
12774                                       token->location,
12775                                       /*user_defined_p=*/true);
12776
12777       return type;
12778
12779     default:
12780       break;
12781     }
12782
12783   /* If the type-specifier was for a built-in type, we're done.  */
12784   if (type)
12785     {
12786       /* Record the type.  */
12787       if (decl_specs
12788           && (token->keyword != RID_SIGNED
12789               && token->keyword != RID_UNSIGNED
12790               && token->keyword != RID_SHORT
12791               && token->keyword != RID_LONG))
12792         cp_parser_set_decl_spec_type (decl_specs,
12793                                       type,
12794                                       token->location,
12795                                       /*user_defined=*/false);
12796       if (decl_specs)
12797         decl_specs->any_specifiers_p = true;
12798
12799       /* Consume the token.  */
12800       cp_lexer_consume_token (parser->lexer);
12801
12802       /* There is no valid C++ program where a non-template type is
12803          followed by a "<".  That usually indicates that the user thought
12804          that the type was a template.  */
12805       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12806
12807       return TYPE_NAME (type);
12808     }
12809
12810   /* The type-specifier must be a user-defined type.  */
12811   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12812     {
12813       bool qualified_p;
12814       bool global_p;
12815
12816       /* Don't gobble tokens or issue error messages if this is an
12817          optional type-specifier.  */
12818       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12819         cp_parser_parse_tentatively (parser);
12820
12821       /* Look for the optional `::' operator.  */
12822       global_p
12823         = (cp_parser_global_scope_opt (parser,
12824                                        /*current_scope_valid_p=*/false)
12825            != NULL_TREE);
12826       /* Look for the nested-name specifier.  */
12827       qualified_p
12828         = (cp_parser_nested_name_specifier_opt (parser,
12829                                                 /*typename_keyword_p=*/false,
12830                                                 /*check_dependency_p=*/true,
12831                                                 /*type_p=*/false,
12832                                                 /*is_declaration=*/false)
12833            != NULL_TREE);
12834       token = cp_lexer_peek_token (parser->lexer);
12835       /* If we have seen a nested-name-specifier, and the next token
12836          is `template', then we are using the template-id production.  */
12837       if (parser->scope
12838           && cp_parser_optional_template_keyword (parser))
12839         {
12840           /* Look for the template-id.  */
12841           type = cp_parser_template_id (parser,
12842                                         /*template_keyword_p=*/true,
12843                                         /*check_dependency_p=*/true,
12844                                         /*is_declaration=*/false);
12845           /* If the template-id did not name a type, we are out of
12846              luck.  */
12847           if (TREE_CODE (type) != TYPE_DECL)
12848             {
12849               cp_parser_error (parser, "expected template-id for type");
12850               type = NULL_TREE;
12851             }
12852         }
12853       /* Otherwise, look for a type-name.  */
12854       else
12855         type = cp_parser_type_name (parser);
12856       /* Keep track of all name-lookups performed in class scopes.  */
12857       if (type
12858           && !global_p
12859           && !qualified_p
12860           && TREE_CODE (type) == TYPE_DECL
12861           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12862         maybe_note_name_used_in_class (DECL_NAME (type), type);
12863       /* If it didn't work out, we don't have a TYPE.  */
12864       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12865           && !cp_parser_parse_definitely (parser))
12866         type = NULL_TREE;
12867       if (type && decl_specs)
12868         cp_parser_set_decl_spec_type (decl_specs, type,
12869                                       token->location,
12870                                       /*user_defined=*/true);
12871     }
12872
12873   /* If we didn't get a type-name, issue an error message.  */
12874   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12875     {
12876       cp_parser_error (parser, "expected type-name");
12877       return error_mark_node;
12878     }
12879
12880   if (type && type != error_mark_node)
12881     {
12882       /* See if TYPE is an Objective-C type, and if so, parse and
12883          accept any protocol references following it.  Do this before
12884          the cp_parser_check_for_invalid_template_id() call, because
12885          Objective-C types can be followed by '<...>' which would
12886          enclose protocol names rather than template arguments, and so
12887          everything is fine.  */
12888       if (c_dialect_objc () && !parser->scope
12889           && (objc_is_id (type) || objc_is_class_name (type)))
12890         {
12891           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12892           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12893
12894           /* Clobber the "unqualified" type previously entered into
12895              DECL_SPECS with the new, improved protocol-qualified version.  */
12896           if (decl_specs)
12897             decl_specs->type = qual_type;
12898
12899           return qual_type;
12900         }
12901
12902       /* There is no valid C++ program where a non-template type is
12903          followed by a "<".  That usually indicates that the user
12904          thought that the type was a template.  */
12905       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12906                                                token->location);
12907     }
12908
12909   return type;
12910 }
12911
12912 /* Parse a type-name.
12913
12914    type-name:
12915      class-name
12916      enum-name
12917      typedef-name
12918
12919    enum-name:
12920      identifier
12921
12922    typedef-name:
12923      identifier
12924
12925    Returns a TYPE_DECL for the type.  */
12926
12927 static tree
12928 cp_parser_type_name (cp_parser* parser)
12929 {
12930   tree type_decl;
12931
12932   /* We can't know yet whether it is a class-name or not.  */
12933   cp_parser_parse_tentatively (parser);
12934   /* Try a class-name.  */
12935   type_decl = cp_parser_class_name (parser,
12936                                     /*typename_keyword_p=*/false,
12937                                     /*template_keyword_p=*/false,
12938                                     none_type,
12939                                     /*check_dependency_p=*/true,
12940                                     /*class_head_p=*/false,
12941                                     /*is_declaration=*/false);
12942   /* If it's not a class-name, keep looking.  */
12943   if (!cp_parser_parse_definitely (parser))
12944     {
12945       /* It must be a typedef-name or an enum-name.  */
12946       return cp_parser_nonclass_name (parser);
12947     }
12948
12949   return type_decl;
12950 }
12951
12952 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12953
12954    enum-name:
12955      identifier
12956
12957    typedef-name:
12958      identifier
12959
12960    Returns a TYPE_DECL for the type.  */
12961
12962 static tree
12963 cp_parser_nonclass_name (cp_parser* parser)
12964 {
12965   tree type_decl;
12966   tree identifier;
12967
12968   cp_token *token = cp_lexer_peek_token (parser->lexer);
12969   identifier = cp_parser_identifier (parser);
12970   if (identifier == error_mark_node)
12971     return error_mark_node;
12972
12973   /* Look up the type-name.  */
12974   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12975
12976   if (TREE_CODE (type_decl) != TYPE_DECL
12977       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12978     {
12979       /* See if this is an Objective-C type.  */
12980       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12981       tree type = objc_get_protocol_qualified_type (identifier, protos);
12982       if (type)
12983         type_decl = TYPE_NAME (type);
12984     }
12985
12986   /* Issue an error if we did not find a type-name.  */
12987   if (TREE_CODE (type_decl) != TYPE_DECL
12988       /* In Objective-C, we have the complication that class names are
12989          normally type names and start declarations (eg, the
12990          "NSObject" in "NSObject *object;"), but can be used in an
12991          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12992          is an expression.  So, a classname followed by a dot is not a
12993          valid type-name.  */
12994       || (objc_is_class_name (TREE_TYPE (type_decl))
12995           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12996     {
12997       if (!cp_parser_simulate_error (parser))
12998         cp_parser_name_lookup_error (parser, identifier, type_decl,
12999                                      NLE_TYPE, token->location);
13000       return error_mark_node;
13001     }
13002   /* Remember that the name was used in the definition of the
13003      current class so that we can check later to see if the
13004      meaning would have been different after the class was
13005      entirely defined.  */
13006   else if (type_decl != error_mark_node
13007            && !parser->scope)
13008     maybe_note_name_used_in_class (identifier, type_decl);
13009   
13010   return type_decl;
13011 }
13012
13013 /* Parse an elaborated-type-specifier.  Note that the grammar given
13014    here incorporates the resolution to DR68.
13015
13016    elaborated-type-specifier:
13017      class-key :: [opt] nested-name-specifier [opt] identifier
13018      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13019      enum-key :: [opt] nested-name-specifier [opt] identifier
13020      typename :: [opt] nested-name-specifier identifier
13021      typename :: [opt] nested-name-specifier template [opt]
13022        template-id
13023
13024    GNU extension:
13025
13026    elaborated-type-specifier:
13027      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13028      class-key attributes :: [opt] nested-name-specifier [opt]
13029                template [opt] template-id
13030      enum attributes :: [opt] nested-name-specifier [opt] identifier
13031
13032    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13033    declared `friend'.  If IS_DECLARATION is TRUE, then this
13034    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13035    something is being declared.
13036
13037    Returns the TYPE specified.  */
13038
13039 static tree
13040 cp_parser_elaborated_type_specifier (cp_parser* parser,
13041                                      bool is_friend,
13042                                      bool is_declaration)
13043 {
13044   enum tag_types tag_type;
13045   tree identifier;
13046   tree type = NULL_TREE;
13047   tree attributes = NULL_TREE;
13048   tree globalscope;
13049   cp_token *token = NULL;
13050
13051   /* See if we're looking at the `enum' keyword.  */
13052   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13053     {
13054       /* Consume the `enum' token.  */
13055       cp_lexer_consume_token (parser->lexer);
13056       /* Remember that it's an enumeration type.  */
13057       tag_type = enum_type;
13058       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13059          enums) is used here.  */
13060       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13061           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13062         {
13063             pedwarn (input_location, 0, "elaborated-type-specifier "
13064                       "for a scoped enum must not use the %<%D%> keyword",
13065                       cp_lexer_peek_token (parser->lexer)->u.value);
13066           /* Consume the `struct' or `class' and parse it anyway.  */
13067           cp_lexer_consume_token (parser->lexer);
13068         }
13069       /* Parse the attributes.  */
13070       attributes = cp_parser_attributes_opt (parser);
13071     }
13072   /* Or, it might be `typename'.  */
13073   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13074                                            RID_TYPENAME))
13075     {
13076       /* Consume the `typename' token.  */
13077       cp_lexer_consume_token (parser->lexer);
13078       /* Remember that it's a `typename' type.  */
13079       tag_type = typename_type;
13080     }
13081   /* Otherwise it must be a class-key.  */
13082   else
13083     {
13084       tag_type = cp_parser_class_key (parser);
13085       if (tag_type == none_type)
13086         return error_mark_node;
13087       /* Parse the attributes.  */
13088       attributes = cp_parser_attributes_opt (parser);
13089     }
13090
13091   /* Look for the `::' operator.  */
13092   globalscope =  cp_parser_global_scope_opt (parser,
13093                                              /*current_scope_valid_p=*/false);
13094   /* Look for the nested-name-specifier.  */
13095   if (tag_type == typename_type && !globalscope)
13096     {
13097       if (!cp_parser_nested_name_specifier (parser,
13098                                            /*typename_keyword_p=*/true,
13099                                            /*check_dependency_p=*/true,
13100                                            /*type_p=*/true,
13101                                             is_declaration))
13102         return error_mark_node;
13103     }
13104   else
13105     /* Even though `typename' is not present, the proposed resolution
13106        to Core Issue 180 says that in `class A<T>::B', `B' should be
13107        considered a type-name, even if `A<T>' is dependent.  */
13108     cp_parser_nested_name_specifier_opt (parser,
13109                                          /*typename_keyword_p=*/true,
13110                                          /*check_dependency_p=*/true,
13111                                          /*type_p=*/true,
13112                                          is_declaration);
13113  /* For everything but enumeration types, consider a template-id.
13114     For an enumeration type, consider only a plain identifier.  */
13115   if (tag_type != enum_type)
13116     {
13117       bool template_p = false;
13118       tree decl;
13119
13120       /* Allow the `template' keyword.  */
13121       template_p = cp_parser_optional_template_keyword (parser);
13122       /* If we didn't see `template', we don't know if there's a
13123          template-id or not.  */
13124       if (!template_p)
13125         cp_parser_parse_tentatively (parser);
13126       /* Parse the template-id.  */
13127       token = cp_lexer_peek_token (parser->lexer);
13128       decl = cp_parser_template_id (parser, template_p,
13129                                     /*check_dependency_p=*/true,
13130                                     is_declaration);
13131       /* If we didn't find a template-id, look for an ordinary
13132          identifier.  */
13133       if (!template_p && !cp_parser_parse_definitely (parser))
13134         ;
13135       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13136          in effect, then we must assume that, upon instantiation, the
13137          template will correspond to a class.  */
13138       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13139                && tag_type == typename_type)
13140         type = make_typename_type (parser->scope, decl,
13141                                    typename_type,
13142                                    /*complain=*/tf_error);
13143       /* If the `typename' keyword is in effect and DECL is not a type
13144          decl. Then type is non existant.   */
13145       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13146         type = NULL_TREE; 
13147       else 
13148         type = TREE_TYPE (decl);
13149     }
13150
13151   if (!type)
13152     {
13153       token = cp_lexer_peek_token (parser->lexer);
13154       identifier = cp_parser_identifier (parser);
13155
13156       if (identifier == error_mark_node)
13157         {
13158           parser->scope = NULL_TREE;
13159           return error_mark_node;
13160         }
13161
13162       /* For a `typename', we needn't call xref_tag.  */
13163       if (tag_type == typename_type
13164           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13165         return cp_parser_make_typename_type (parser, parser->scope,
13166                                              identifier,
13167                                              token->location);
13168       /* Look up a qualified name in the usual way.  */
13169       if (parser->scope)
13170         {
13171           tree decl;
13172           tree ambiguous_decls;
13173
13174           decl = cp_parser_lookup_name (parser, identifier,
13175                                         tag_type,
13176                                         /*is_template=*/false,
13177                                         /*is_namespace=*/false,
13178                                         /*check_dependency=*/true,
13179                                         &ambiguous_decls,
13180                                         token->location);
13181
13182           /* If the lookup was ambiguous, an error will already have been
13183              issued.  */
13184           if (ambiguous_decls)
13185             return error_mark_node;
13186
13187           /* If we are parsing friend declaration, DECL may be a
13188              TEMPLATE_DECL tree node here.  However, we need to check
13189              whether this TEMPLATE_DECL results in valid code.  Consider
13190              the following example:
13191
13192                namespace N {
13193                  template <class T> class C {};
13194                }
13195                class X {
13196                  template <class T> friend class N::C; // #1, valid code
13197                };
13198                template <class T> class Y {
13199                  friend class N::C;                    // #2, invalid code
13200                };
13201
13202              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13203              name lookup of `N::C'.  We see that friend declaration must
13204              be template for the code to be valid.  Note that
13205              processing_template_decl does not work here since it is
13206              always 1 for the above two cases.  */
13207
13208           decl = (cp_parser_maybe_treat_template_as_class
13209                   (decl, /*tag_name_p=*/is_friend
13210                          && parser->num_template_parameter_lists));
13211
13212           if (TREE_CODE (decl) != TYPE_DECL)
13213             {
13214               cp_parser_diagnose_invalid_type_name (parser,
13215                                                     parser->scope,
13216                                                     identifier,
13217                                                     token->location);
13218               return error_mark_node;
13219             }
13220
13221           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13222             {
13223               bool allow_template = (parser->num_template_parameter_lists
13224                                       || DECL_SELF_REFERENCE_P (decl));
13225               type = check_elaborated_type_specifier (tag_type, decl, 
13226                                                       allow_template);
13227
13228               if (type == error_mark_node)
13229                 return error_mark_node;
13230             }
13231
13232           /* Forward declarations of nested types, such as
13233
13234                class C1::C2;
13235                class C1::C2::C3;
13236
13237              are invalid unless all components preceding the final '::'
13238              are complete.  If all enclosing types are complete, these
13239              declarations become merely pointless.
13240
13241              Invalid forward declarations of nested types are errors
13242              caught elsewhere in parsing.  Those that are pointless arrive
13243              here.  */
13244
13245           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13246               && !is_friend && !processing_explicit_instantiation)
13247             warning (0, "declaration %qD does not declare anything", decl);
13248
13249           type = TREE_TYPE (decl);
13250         }
13251       else
13252         {
13253           /* An elaborated-type-specifier sometimes introduces a new type and
13254              sometimes names an existing type.  Normally, the rule is that it
13255              introduces a new type only if there is not an existing type of
13256              the same name already in scope.  For example, given:
13257
13258                struct S {};
13259                void f() { struct S s; }
13260
13261              the `struct S' in the body of `f' is the same `struct S' as in
13262              the global scope; the existing definition is used.  However, if
13263              there were no global declaration, this would introduce a new
13264              local class named `S'.
13265
13266              An exception to this rule applies to the following code:
13267
13268                namespace N { struct S; }
13269
13270              Here, the elaborated-type-specifier names a new type
13271              unconditionally; even if there is already an `S' in the
13272              containing scope this declaration names a new type.
13273              This exception only applies if the elaborated-type-specifier
13274              forms the complete declaration:
13275
13276                [class.name]
13277
13278                A declaration consisting solely of `class-key identifier ;' is
13279                either a redeclaration of the name in the current scope or a
13280                forward declaration of the identifier as a class name.  It
13281                introduces the name into the current scope.
13282
13283              We are in this situation precisely when the next token is a `;'.
13284
13285              An exception to the exception is that a `friend' declaration does
13286              *not* name a new type; i.e., given:
13287
13288                struct S { friend struct T; };
13289
13290              `T' is not a new type in the scope of `S'.
13291
13292              Also, `new struct S' or `sizeof (struct S)' never results in the
13293              definition of a new type; a new type can only be declared in a
13294              declaration context.  */
13295
13296           tag_scope ts;
13297           bool template_p;
13298
13299           if (is_friend)
13300             /* Friends have special name lookup rules.  */
13301             ts = ts_within_enclosing_non_class;
13302           else if (is_declaration
13303                    && cp_lexer_next_token_is (parser->lexer,
13304                                               CPP_SEMICOLON))
13305             /* This is a `class-key identifier ;' */
13306             ts = ts_current;
13307           else
13308             ts = ts_global;
13309
13310           template_p =
13311             (parser->num_template_parameter_lists
13312              && (cp_parser_next_token_starts_class_definition_p (parser)
13313                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13314           /* An unqualified name was used to reference this type, so
13315              there were no qualifying templates.  */
13316           if (!cp_parser_check_template_parameters (parser,
13317                                                     /*num_templates=*/0,
13318                                                     token->location,
13319                                                     /*declarator=*/NULL))
13320             return error_mark_node;
13321           type = xref_tag (tag_type, identifier, ts, template_p);
13322         }
13323     }
13324
13325   if (type == error_mark_node)
13326     return error_mark_node;
13327
13328   /* Allow attributes on forward declarations of classes.  */
13329   if (attributes)
13330     {
13331       if (TREE_CODE (type) == TYPENAME_TYPE)
13332         warning (OPT_Wattributes,
13333                  "attributes ignored on uninstantiated type");
13334       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13335                && ! processing_explicit_instantiation)
13336         warning (OPT_Wattributes,
13337                  "attributes ignored on template instantiation");
13338       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13339         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13340       else
13341         warning (OPT_Wattributes,
13342                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13343     }
13344
13345   if (tag_type != enum_type)
13346     cp_parser_check_class_key (tag_type, type);
13347
13348   /* A "<" cannot follow an elaborated type specifier.  If that
13349      happens, the user was probably trying to form a template-id.  */
13350   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13351
13352   return type;
13353 }
13354
13355 /* Parse an enum-specifier.
13356
13357    enum-specifier:
13358      enum-head { enumerator-list [opt] }
13359
13360    enum-head:
13361      enum-key identifier [opt] enum-base [opt]
13362      enum-key nested-name-specifier identifier enum-base [opt]
13363
13364    enum-key:
13365      enum
13366      enum class   [C++0x]
13367      enum struct  [C++0x]
13368
13369    enum-base:   [C++0x]
13370      : type-specifier-seq
13371
13372    opaque-enum-specifier:
13373      enum-key identifier enum-base [opt] ;
13374
13375    GNU Extensions:
13376      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13377        { enumerator-list [opt] }attributes[opt]
13378
13379    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13380    if the token stream isn't an enum-specifier after all.  */
13381
13382 static tree
13383 cp_parser_enum_specifier (cp_parser* parser)
13384 {
13385   tree identifier;
13386   tree type = NULL_TREE;
13387   tree prev_scope;
13388   tree nested_name_specifier = NULL_TREE;
13389   tree attributes;
13390   bool scoped_enum_p = false;
13391   bool has_underlying_type = false;
13392   bool nested_being_defined = false;
13393   bool new_value_list = false;
13394   bool is_new_type = false;
13395   bool is_anonymous = false;
13396   tree underlying_type = NULL_TREE;
13397   cp_token *type_start_token = NULL;
13398   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13399
13400   parser->colon_corrects_to_scope_p = false;
13401
13402   /* Parse tentatively so that we can back up if we don't find a
13403      enum-specifier.  */
13404   cp_parser_parse_tentatively (parser);
13405
13406   /* Caller guarantees that the current token is 'enum', an identifier
13407      possibly follows, and the token after that is an opening brace.
13408      If we don't have an identifier, fabricate an anonymous name for
13409      the enumeration being defined.  */
13410   cp_lexer_consume_token (parser->lexer);
13411
13412   /* Parse the "class" or "struct", which indicates a scoped
13413      enumeration type in C++0x.  */
13414   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13415       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13416     {
13417       if (cxx_dialect < cxx0x)
13418         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13419
13420       /* Consume the `struct' or `class' token.  */
13421       cp_lexer_consume_token (parser->lexer);
13422
13423       scoped_enum_p = true;
13424     }
13425
13426   attributes = cp_parser_attributes_opt (parser);
13427
13428   /* Clear the qualification.  */
13429   parser->scope = NULL_TREE;
13430   parser->qualifying_scope = NULL_TREE;
13431   parser->object_scope = NULL_TREE;
13432
13433   /* Figure out in what scope the declaration is being placed.  */
13434   prev_scope = current_scope ();
13435
13436   type_start_token = cp_lexer_peek_token (parser->lexer);
13437
13438   push_deferring_access_checks (dk_no_check);
13439   nested_name_specifier
13440       = cp_parser_nested_name_specifier_opt (parser,
13441                                              /*typename_keyword_p=*/true,
13442                                              /*check_dependency_p=*/false,
13443                                              /*type_p=*/false,
13444                                              /*is_declaration=*/false);
13445
13446   if (nested_name_specifier)
13447     {
13448       tree name;
13449
13450       identifier = cp_parser_identifier (parser);
13451       name =  cp_parser_lookup_name (parser, identifier,
13452                                      enum_type,
13453                                      /*is_template=*/false,
13454                                      /*is_namespace=*/false,
13455                                      /*check_dependency=*/true,
13456                                      /*ambiguous_decls=*/NULL,
13457                                      input_location);
13458       if (name)
13459         {
13460           type = TREE_TYPE (name);
13461           if (TREE_CODE (type) == TYPENAME_TYPE)
13462             {
13463               /* Are template enums allowed in ISO? */
13464               if (template_parm_scope_p ())
13465                 pedwarn (type_start_token->location, OPT_pedantic,
13466                          "%qD is an enumeration template", name);
13467               /* ignore a typename reference, for it will be solved by name
13468                  in start_enum.  */
13469               type = NULL_TREE;
13470             }
13471         }
13472       else
13473         error_at (type_start_token->location,
13474                   "%qD is not an enumerator-name", identifier);
13475     }
13476   else
13477     {
13478       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13479         identifier = cp_parser_identifier (parser);
13480       else
13481         {
13482           identifier = make_anon_name ();
13483           is_anonymous = true;
13484         }
13485     }
13486   pop_deferring_access_checks ();
13487
13488   /* Check for the `:' that denotes a specified underlying type in C++0x.
13489      Note that a ':' could also indicate a bitfield width, however.  */
13490   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13491     {
13492       cp_decl_specifier_seq type_specifiers;
13493
13494       /* Consume the `:'.  */
13495       cp_lexer_consume_token (parser->lexer);
13496
13497       /* Parse the type-specifier-seq.  */
13498       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13499                                     /*is_trailing_return=*/false,
13500                                     &type_specifiers);
13501
13502       /* At this point this is surely not elaborated type specifier.  */
13503       if (!cp_parser_parse_definitely (parser))
13504         return NULL_TREE;
13505
13506       if (cxx_dialect < cxx0x)
13507         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13508
13509       has_underlying_type = true;
13510
13511       /* If that didn't work, stop.  */
13512       if (type_specifiers.type != error_mark_node)
13513         {
13514           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13515                                             /*initialized=*/0, NULL);
13516           if (underlying_type == error_mark_node)
13517             underlying_type = NULL_TREE;
13518         }
13519     }
13520
13521   /* Look for the `{' but don't consume it yet.  */
13522   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13523     {
13524       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13525         {
13526           cp_parser_error (parser, "expected %<{%>");
13527           if (has_underlying_type)
13528             {
13529               type = NULL_TREE;
13530               goto out;
13531             }
13532         }
13533       /* An opaque-enum-specifier must have a ';' here.  */
13534       if ((scoped_enum_p || underlying_type)
13535           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13536         {
13537           cp_parser_error (parser, "expected %<;%> or %<{%>");
13538           if (has_underlying_type)
13539             {
13540               type = NULL_TREE;
13541               goto out;
13542             }
13543         }
13544     }
13545
13546   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13547     return NULL_TREE;
13548
13549   if (nested_name_specifier)
13550     {
13551       if (CLASS_TYPE_P (nested_name_specifier))
13552         {
13553           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13554           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13555           push_scope (nested_name_specifier);
13556         }
13557       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13558         {
13559           push_nested_namespace (nested_name_specifier);
13560         }
13561     }
13562
13563   /* Issue an error message if type-definitions are forbidden here.  */
13564   if (!cp_parser_check_type_definition (parser))
13565     type = error_mark_node;
13566   else
13567     /* Create the new type.  We do this before consuming the opening
13568        brace so the enum will be recorded as being on the line of its
13569        tag (or the 'enum' keyword, if there is no tag).  */
13570     type = start_enum (identifier, type, underlying_type,
13571                        scoped_enum_p, &is_new_type);
13572
13573   /* If the next token is not '{' it is an opaque-enum-specifier or an
13574      elaborated-type-specifier.  */
13575   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13576     {
13577       if (nested_name_specifier)
13578         {
13579           /* The following catches invalid code such as:
13580              enum class S<int>::E { A, B, C }; */
13581           if (!processing_specialization
13582               && CLASS_TYPE_P (nested_name_specifier)
13583               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13584             error_at (type_start_token->location, "cannot add an enumerator "
13585                       "list to a template instantiation");
13586
13587           /* If that scope does not contain the scope in which the
13588              class was originally declared, the program is invalid.  */
13589           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13590             {
13591               if (at_namespace_scope_p ())
13592                 error_at (type_start_token->location,
13593                           "declaration of %qD in namespace %qD which does not "
13594                           "enclose %qD",
13595                           type, prev_scope, nested_name_specifier);
13596               else
13597                 error_at (type_start_token->location,
13598                           "declaration of %qD in %qD which does not enclose %qD",
13599                           type, prev_scope, nested_name_specifier);
13600               type = error_mark_node;
13601             }
13602         }
13603
13604       if (scoped_enum_p)
13605         begin_scope (sk_scoped_enum, type);
13606
13607       /* Consume the opening brace.  */
13608       cp_lexer_consume_token (parser->lexer);
13609
13610       if (type == error_mark_node)
13611         ; /* Nothing to add */
13612       else if (OPAQUE_ENUM_P (type)
13613                || (cxx_dialect > cxx98 && processing_specialization))
13614         {
13615           new_value_list = true;
13616           SET_OPAQUE_ENUM_P (type, false);
13617           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13618         }
13619       else
13620         {
13621           error_at (type_start_token->location, "multiple definition of %q#T", type);
13622           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13623                     "previous definition here");
13624           type = error_mark_node;
13625         }
13626
13627       if (type == error_mark_node)
13628         cp_parser_skip_to_end_of_block_or_statement (parser);
13629       /* If the next token is not '}', then there are some enumerators.  */
13630       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13631         cp_parser_enumerator_list (parser, type);
13632
13633       /* Consume the final '}'.  */
13634       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13635
13636       if (scoped_enum_p)
13637         finish_scope ();
13638     }
13639   else
13640     {
13641       /* If a ';' follows, then it is an opaque-enum-specifier
13642         and additional restrictions apply.  */
13643       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13644         {
13645           if (is_anonymous)
13646             error_at (type_start_token->location,
13647                       "opaque-enum-specifier without name");
13648           else if (nested_name_specifier)
13649             error_at (type_start_token->location,
13650                       "opaque-enum-specifier must use a simple identifier");
13651         }
13652     }
13653
13654   /* Look for trailing attributes to apply to this enumeration, and
13655      apply them if appropriate.  */
13656   if (cp_parser_allow_gnu_extensions_p (parser))
13657     {
13658       tree trailing_attr = cp_parser_attributes_opt (parser);
13659       trailing_attr = chainon (trailing_attr, attributes);
13660       cplus_decl_attributes (&type,
13661                              trailing_attr,
13662                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13663     }
13664
13665   /* Finish up the enumeration.  */
13666   if (type != error_mark_node)
13667     {
13668       if (new_value_list)
13669         finish_enum_value_list (type);
13670       if (is_new_type)
13671         finish_enum (type);
13672     }
13673
13674   if (nested_name_specifier)
13675     {
13676       if (CLASS_TYPE_P (nested_name_specifier))
13677         {
13678           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13679           pop_scope (nested_name_specifier);
13680         }
13681       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13682         {
13683           pop_nested_namespace (nested_name_specifier);
13684         }
13685     }
13686  out:
13687   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13688   return type;
13689 }
13690
13691 /* Parse an enumerator-list.  The enumerators all have the indicated
13692    TYPE.
13693
13694    enumerator-list:
13695      enumerator-definition
13696      enumerator-list , enumerator-definition  */
13697
13698 static void
13699 cp_parser_enumerator_list (cp_parser* parser, tree type)
13700 {
13701   while (true)
13702     {
13703       /* Parse an enumerator-definition.  */
13704       cp_parser_enumerator_definition (parser, type);
13705
13706       /* If the next token is not a ',', we've reached the end of
13707          the list.  */
13708       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13709         break;
13710       /* Otherwise, consume the `,' and keep going.  */
13711       cp_lexer_consume_token (parser->lexer);
13712       /* If the next token is a `}', there is a trailing comma.  */
13713       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13714         {
13715           if (!in_system_header)
13716             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13717           break;
13718         }
13719     }
13720 }
13721
13722 /* Parse an enumerator-definition.  The enumerator has the indicated
13723    TYPE.
13724
13725    enumerator-definition:
13726      enumerator
13727      enumerator = constant-expression
13728
13729    enumerator:
13730      identifier  */
13731
13732 static void
13733 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13734 {
13735   tree identifier;
13736   tree value;
13737   location_t loc;
13738
13739   /* Save the input location because we are interested in the location
13740      of the identifier and not the location of the explicit value.  */
13741   loc = cp_lexer_peek_token (parser->lexer)->location;
13742
13743   /* Look for the identifier.  */
13744   identifier = cp_parser_identifier (parser);
13745   if (identifier == error_mark_node)
13746     return;
13747
13748   /* If the next token is an '=', then there is an explicit value.  */
13749   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13750     {
13751       /* Consume the `=' token.  */
13752       cp_lexer_consume_token (parser->lexer);
13753       /* Parse the value.  */
13754       value = cp_parser_constant_expression (parser,
13755                                              /*allow_non_constant_p=*/false,
13756                                              NULL);
13757     }
13758   else
13759     value = NULL_TREE;
13760
13761   /* If we are processing a template, make sure the initializer of the
13762      enumerator doesn't contain any bare template parameter pack.  */
13763   if (check_for_bare_parameter_packs (value))
13764     value = error_mark_node;
13765
13766   /* Create the enumerator.  */
13767   build_enumerator (identifier, value, type, loc);
13768 }
13769
13770 /* Parse a namespace-name.
13771
13772    namespace-name:
13773      original-namespace-name
13774      namespace-alias
13775
13776    Returns the NAMESPACE_DECL for the namespace.  */
13777
13778 static tree
13779 cp_parser_namespace_name (cp_parser* parser)
13780 {
13781   tree identifier;
13782   tree namespace_decl;
13783
13784   cp_token *token = cp_lexer_peek_token (parser->lexer);
13785
13786   /* Get the name of the namespace.  */
13787   identifier = cp_parser_identifier (parser);
13788   if (identifier == error_mark_node)
13789     return error_mark_node;
13790
13791   /* Look up the identifier in the currently active scope.  Look only
13792      for namespaces, due to:
13793
13794        [basic.lookup.udir]
13795
13796        When looking up a namespace-name in a using-directive or alias
13797        definition, only namespace names are considered.
13798
13799      And:
13800
13801        [basic.lookup.qual]
13802
13803        During the lookup of a name preceding the :: scope resolution
13804        operator, object, function, and enumerator names are ignored.
13805
13806      (Note that cp_parser_qualifying_entity only calls this
13807      function if the token after the name is the scope resolution
13808      operator.)  */
13809   namespace_decl = cp_parser_lookup_name (parser, identifier,
13810                                           none_type,
13811                                           /*is_template=*/false,
13812                                           /*is_namespace=*/true,
13813                                           /*check_dependency=*/true,
13814                                           /*ambiguous_decls=*/NULL,
13815                                           token->location);
13816   /* If it's not a namespace, issue an error.  */
13817   if (namespace_decl == error_mark_node
13818       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13819     {
13820       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13821         error_at (token->location, "%qD is not a namespace-name", identifier);
13822       cp_parser_error (parser, "expected namespace-name");
13823       namespace_decl = error_mark_node;
13824     }
13825
13826   return namespace_decl;
13827 }
13828
13829 /* Parse a namespace-definition.
13830
13831    namespace-definition:
13832      named-namespace-definition
13833      unnamed-namespace-definition
13834
13835    named-namespace-definition:
13836      original-namespace-definition
13837      extension-namespace-definition
13838
13839    original-namespace-definition:
13840      namespace identifier { namespace-body }
13841
13842    extension-namespace-definition:
13843      namespace original-namespace-name { namespace-body }
13844
13845    unnamed-namespace-definition:
13846      namespace { namespace-body } */
13847
13848 static void
13849 cp_parser_namespace_definition (cp_parser* parser)
13850 {
13851   tree identifier, attribs;
13852   bool has_visibility;
13853   bool is_inline;
13854
13855   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13856     {
13857       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13858       is_inline = true;
13859       cp_lexer_consume_token (parser->lexer);
13860     }
13861   else
13862     is_inline = false;
13863
13864   /* Look for the `namespace' keyword.  */
13865   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13866
13867   /* Get the name of the namespace.  We do not attempt to distinguish
13868      between an original-namespace-definition and an
13869      extension-namespace-definition at this point.  The semantic
13870      analysis routines are responsible for that.  */
13871   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13872     identifier = cp_parser_identifier (parser);
13873   else
13874     identifier = NULL_TREE;
13875
13876   /* Parse any specified attributes.  */
13877   attribs = cp_parser_attributes_opt (parser);
13878
13879   /* Look for the `{' to start the namespace.  */
13880   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13881   /* Start the namespace.  */
13882   push_namespace (identifier);
13883
13884   /* "inline namespace" is equivalent to a stub namespace definition
13885      followed by a strong using directive.  */
13886   if (is_inline)
13887     {
13888       tree name_space = current_namespace;
13889       /* Set up namespace association.  */
13890       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13891         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13892                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13893       /* Import the contents of the inline namespace.  */
13894       pop_namespace ();
13895       do_using_directive (name_space);
13896       push_namespace (identifier);
13897     }
13898
13899   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13900
13901   /* Parse the body of the namespace.  */
13902   cp_parser_namespace_body (parser);
13903
13904   if (has_visibility)
13905     pop_visibility (1);
13906
13907   /* Finish the namespace.  */
13908   pop_namespace ();
13909   /* Look for the final `}'.  */
13910   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13911 }
13912
13913 /* Parse a namespace-body.
13914
13915    namespace-body:
13916      declaration-seq [opt]  */
13917
13918 static void
13919 cp_parser_namespace_body (cp_parser* parser)
13920 {
13921   cp_parser_declaration_seq_opt (parser);
13922 }
13923
13924 /* Parse a namespace-alias-definition.
13925
13926    namespace-alias-definition:
13927      namespace identifier = qualified-namespace-specifier ;  */
13928
13929 static void
13930 cp_parser_namespace_alias_definition (cp_parser* parser)
13931 {
13932   tree identifier;
13933   tree namespace_specifier;
13934
13935   cp_token *token = cp_lexer_peek_token (parser->lexer);
13936
13937   /* Look for the `namespace' keyword.  */
13938   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13939   /* Look for the identifier.  */
13940   identifier = cp_parser_identifier (parser);
13941   if (identifier == error_mark_node)
13942     return;
13943   /* Look for the `=' token.  */
13944   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13945       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13946     {
13947       error_at (token->location, "%<namespace%> definition is not allowed here");
13948       /* Skip the definition.  */
13949       cp_lexer_consume_token (parser->lexer);
13950       if (cp_parser_skip_to_closing_brace (parser))
13951         cp_lexer_consume_token (parser->lexer);
13952       return;
13953     }
13954   cp_parser_require (parser, CPP_EQ, RT_EQ);
13955   /* Look for the qualified-namespace-specifier.  */
13956   namespace_specifier
13957     = cp_parser_qualified_namespace_specifier (parser);
13958   /* Look for the `;' token.  */
13959   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13960
13961   /* Register the alias in the symbol table.  */
13962   do_namespace_alias (identifier, namespace_specifier);
13963 }
13964
13965 /* Parse a qualified-namespace-specifier.
13966
13967    qualified-namespace-specifier:
13968      :: [opt] nested-name-specifier [opt] namespace-name
13969
13970    Returns a NAMESPACE_DECL corresponding to the specified
13971    namespace.  */
13972
13973 static tree
13974 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13975 {
13976   /* Look for the optional `::'.  */
13977   cp_parser_global_scope_opt (parser,
13978                               /*current_scope_valid_p=*/false);
13979
13980   /* Look for the optional nested-name-specifier.  */
13981   cp_parser_nested_name_specifier_opt (parser,
13982                                        /*typename_keyword_p=*/false,
13983                                        /*check_dependency_p=*/true,
13984                                        /*type_p=*/false,
13985                                        /*is_declaration=*/true);
13986
13987   return cp_parser_namespace_name (parser);
13988 }
13989
13990 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13991    access declaration.
13992
13993    using-declaration:
13994      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13995      using :: unqualified-id ;  
13996
13997    access-declaration:
13998      qualified-id ;  
13999
14000    */
14001
14002 static bool
14003 cp_parser_using_declaration (cp_parser* parser, 
14004                              bool access_declaration_p)
14005 {
14006   cp_token *token;
14007   bool typename_p = false;
14008   bool global_scope_p;
14009   tree decl;
14010   tree identifier;
14011   tree qscope;
14012
14013   if (access_declaration_p)
14014     cp_parser_parse_tentatively (parser);
14015   else
14016     {
14017       /* Look for the `using' keyword.  */
14018       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14019       
14020       /* Peek at the next token.  */
14021       token = cp_lexer_peek_token (parser->lexer);
14022       /* See if it's `typename'.  */
14023       if (token->keyword == RID_TYPENAME)
14024         {
14025           /* Remember that we've seen it.  */
14026           typename_p = true;
14027           /* Consume the `typename' token.  */
14028           cp_lexer_consume_token (parser->lexer);
14029         }
14030     }
14031
14032   /* Look for the optional global scope qualification.  */
14033   global_scope_p
14034     = (cp_parser_global_scope_opt (parser,
14035                                    /*current_scope_valid_p=*/false)
14036        != NULL_TREE);
14037
14038   /* If we saw `typename', or didn't see `::', then there must be a
14039      nested-name-specifier present.  */
14040   if (typename_p || !global_scope_p)
14041     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14042                                               /*check_dependency_p=*/true,
14043                                               /*type_p=*/false,
14044                                               /*is_declaration=*/true);
14045   /* Otherwise, we could be in either of the two productions.  In that
14046      case, treat the nested-name-specifier as optional.  */
14047   else
14048     qscope = cp_parser_nested_name_specifier_opt (parser,
14049                                                   /*typename_keyword_p=*/false,
14050                                                   /*check_dependency_p=*/true,
14051                                                   /*type_p=*/false,
14052                                                   /*is_declaration=*/true);
14053   if (!qscope)
14054     qscope = global_namespace;
14055
14056   if (access_declaration_p && cp_parser_error_occurred (parser))
14057     /* Something has already gone wrong; there's no need to parse
14058        further.  Since an error has occurred, the return value of
14059        cp_parser_parse_definitely will be false, as required.  */
14060     return cp_parser_parse_definitely (parser);
14061
14062   token = cp_lexer_peek_token (parser->lexer);
14063   /* Parse the unqualified-id.  */
14064   identifier = cp_parser_unqualified_id (parser,
14065                                          /*template_keyword_p=*/false,
14066                                          /*check_dependency_p=*/true,
14067                                          /*declarator_p=*/true,
14068                                          /*optional_p=*/false);
14069
14070   if (access_declaration_p)
14071     {
14072       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14073         cp_parser_simulate_error (parser);
14074       if (!cp_parser_parse_definitely (parser))
14075         return false;
14076     }
14077
14078   /* The function we call to handle a using-declaration is different
14079      depending on what scope we are in.  */
14080   if (qscope == error_mark_node || identifier == error_mark_node)
14081     ;
14082   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14083            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14084     /* [namespace.udecl]
14085
14086        A using declaration shall not name a template-id.  */
14087     error_at (token->location,
14088               "a template-id may not appear in a using-declaration");
14089   else
14090     {
14091       if (at_class_scope_p ())
14092         {
14093           /* Create the USING_DECL.  */
14094           decl = do_class_using_decl (parser->scope, identifier);
14095
14096           if (check_for_bare_parameter_packs (decl))
14097             return false;
14098           else
14099             /* Add it to the list of members in this class.  */
14100             finish_member_declaration (decl);
14101         }
14102       else
14103         {
14104           decl = cp_parser_lookup_name_simple (parser,
14105                                                identifier,
14106                                                token->location);
14107           if (decl == error_mark_node)
14108             cp_parser_name_lookup_error (parser, identifier,
14109                                          decl, NLE_NULL,
14110                                          token->location);
14111           else if (check_for_bare_parameter_packs (decl))
14112             return false;
14113           else if (!at_namespace_scope_p ())
14114             do_local_using_decl (decl, qscope, identifier);
14115           else
14116             do_toplevel_using_decl (decl, qscope, identifier);
14117         }
14118     }
14119
14120   /* Look for the final `;'.  */
14121   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14122   
14123   return true;
14124 }
14125
14126 /* Parse a using-directive.
14127
14128    using-directive:
14129      using namespace :: [opt] nested-name-specifier [opt]
14130        namespace-name ;  */
14131
14132 static void
14133 cp_parser_using_directive (cp_parser* parser)
14134 {
14135   tree namespace_decl;
14136   tree attribs;
14137
14138   /* Look for the `using' keyword.  */
14139   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14140   /* And the `namespace' keyword.  */
14141   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14142   /* Look for the optional `::' operator.  */
14143   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14144   /* And the optional nested-name-specifier.  */
14145   cp_parser_nested_name_specifier_opt (parser,
14146                                        /*typename_keyword_p=*/false,
14147                                        /*check_dependency_p=*/true,
14148                                        /*type_p=*/false,
14149                                        /*is_declaration=*/true);
14150   /* Get the namespace being used.  */
14151   namespace_decl = cp_parser_namespace_name (parser);
14152   /* And any specified attributes.  */
14153   attribs = cp_parser_attributes_opt (parser);
14154   /* Update the symbol table.  */
14155   parse_using_directive (namespace_decl, attribs);
14156   /* Look for the final `;'.  */
14157   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14158 }
14159
14160 /* Parse an asm-definition.
14161
14162    asm-definition:
14163      asm ( string-literal ) ;
14164
14165    GNU Extension:
14166
14167    asm-definition:
14168      asm volatile [opt] ( string-literal ) ;
14169      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14170      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14171                           : asm-operand-list [opt] ) ;
14172      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14173                           : asm-operand-list [opt]
14174                           : asm-clobber-list [opt] ) ;
14175      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14176                                : asm-clobber-list [opt]
14177                                : asm-goto-list ) ;  */
14178
14179 static void
14180 cp_parser_asm_definition (cp_parser* parser)
14181 {
14182   tree string;
14183   tree outputs = NULL_TREE;
14184   tree inputs = NULL_TREE;
14185   tree clobbers = NULL_TREE;
14186   tree labels = NULL_TREE;
14187   tree asm_stmt;
14188   bool volatile_p = false;
14189   bool extended_p = false;
14190   bool invalid_inputs_p = false;
14191   bool invalid_outputs_p = false;
14192   bool goto_p = false;
14193   required_token missing = RT_NONE;
14194
14195   /* Look for the `asm' keyword.  */
14196   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14197   /* See if the next token is `volatile'.  */
14198   if (cp_parser_allow_gnu_extensions_p (parser)
14199       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14200     {
14201       /* Remember that we saw the `volatile' keyword.  */
14202       volatile_p = true;
14203       /* Consume the token.  */
14204       cp_lexer_consume_token (parser->lexer);
14205     }
14206   if (cp_parser_allow_gnu_extensions_p (parser)
14207       && parser->in_function_body
14208       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14209     {
14210       /* Remember that we saw the `goto' keyword.  */
14211       goto_p = true;
14212       /* Consume the token.  */
14213       cp_lexer_consume_token (parser->lexer);
14214     }
14215   /* Look for the opening `('.  */
14216   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14217     return;
14218   /* Look for the string.  */
14219   string = cp_parser_string_literal (parser, false, false);
14220   if (string == error_mark_node)
14221     {
14222       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14223                                              /*consume_paren=*/true);
14224       return;
14225     }
14226
14227   /* If we're allowing GNU extensions, check for the extended assembly
14228      syntax.  Unfortunately, the `:' tokens need not be separated by
14229      a space in C, and so, for compatibility, we tolerate that here
14230      too.  Doing that means that we have to treat the `::' operator as
14231      two `:' tokens.  */
14232   if (cp_parser_allow_gnu_extensions_p (parser)
14233       && parser->in_function_body
14234       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14235           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14236     {
14237       bool inputs_p = false;
14238       bool clobbers_p = false;
14239       bool labels_p = false;
14240
14241       /* The extended syntax was used.  */
14242       extended_p = true;
14243
14244       /* Look for outputs.  */
14245       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14246         {
14247           /* Consume the `:'.  */
14248           cp_lexer_consume_token (parser->lexer);
14249           /* Parse the output-operands.  */
14250           if (cp_lexer_next_token_is_not (parser->lexer,
14251                                           CPP_COLON)
14252               && cp_lexer_next_token_is_not (parser->lexer,
14253                                              CPP_SCOPE)
14254               && cp_lexer_next_token_is_not (parser->lexer,
14255                                              CPP_CLOSE_PAREN)
14256               && !goto_p)
14257             outputs = cp_parser_asm_operand_list (parser);
14258
14259             if (outputs == error_mark_node)
14260               invalid_outputs_p = true;
14261         }
14262       /* If the next token is `::', there are no outputs, and the
14263          next token is the beginning of the inputs.  */
14264       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14265         /* The inputs are coming next.  */
14266         inputs_p = true;
14267
14268       /* Look for inputs.  */
14269       if (inputs_p
14270           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14271         {
14272           /* Consume the `:' or `::'.  */
14273           cp_lexer_consume_token (parser->lexer);
14274           /* Parse the output-operands.  */
14275           if (cp_lexer_next_token_is_not (parser->lexer,
14276                                           CPP_COLON)
14277               && cp_lexer_next_token_is_not (parser->lexer,
14278                                              CPP_SCOPE)
14279               && cp_lexer_next_token_is_not (parser->lexer,
14280                                              CPP_CLOSE_PAREN))
14281             inputs = cp_parser_asm_operand_list (parser);
14282
14283             if (inputs == error_mark_node)
14284               invalid_inputs_p = true;
14285         }
14286       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14287         /* The clobbers are coming next.  */
14288         clobbers_p = true;
14289
14290       /* Look for clobbers.  */
14291       if (clobbers_p
14292           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14293         {
14294           clobbers_p = true;
14295           /* Consume the `:' or `::'.  */
14296           cp_lexer_consume_token (parser->lexer);
14297           /* Parse the clobbers.  */
14298           if (cp_lexer_next_token_is_not (parser->lexer,
14299                                           CPP_COLON)
14300               && cp_lexer_next_token_is_not (parser->lexer,
14301                                              CPP_CLOSE_PAREN))
14302             clobbers = cp_parser_asm_clobber_list (parser);
14303         }
14304       else if (goto_p
14305                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14306         /* The labels are coming next.  */
14307         labels_p = true;
14308
14309       /* Look for labels.  */
14310       if (labels_p
14311           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14312         {
14313           labels_p = true;
14314           /* Consume the `:' or `::'.  */
14315           cp_lexer_consume_token (parser->lexer);
14316           /* Parse the labels.  */
14317           labels = cp_parser_asm_label_list (parser);
14318         }
14319
14320       if (goto_p && !labels_p)
14321         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14322     }
14323   else if (goto_p)
14324     missing = RT_COLON_SCOPE;
14325
14326   /* Look for the closing `)'.  */
14327   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14328                           missing ? missing : RT_CLOSE_PAREN))
14329     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14330                                            /*consume_paren=*/true);
14331   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14332
14333   if (!invalid_inputs_p && !invalid_outputs_p)
14334     {
14335       /* Create the ASM_EXPR.  */
14336       if (parser->in_function_body)
14337         {
14338           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14339                                       inputs, clobbers, labels);
14340           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14341           if (!extended_p)
14342             {
14343               tree temp = asm_stmt;
14344               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14345                 temp = TREE_OPERAND (temp, 0);
14346
14347               ASM_INPUT_P (temp) = 1;
14348             }
14349         }
14350       else
14351         cgraph_add_asm_node (string);
14352     }
14353 }
14354
14355 /* Declarators [gram.dcl.decl] */
14356
14357 /* Parse an init-declarator.
14358
14359    init-declarator:
14360      declarator initializer [opt]
14361
14362    GNU Extension:
14363
14364    init-declarator:
14365      declarator asm-specification [opt] attributes [opt] initializer [opt]
14366
14367    function-definition:
14368      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14369        function-body
14370      decl-specifier-seq [opt] declarator function-try-block
14371
14372    GNU Extension:
14373
14374    function-definition:
14375      __extension__ function-definition
14376
14377    The DECL_SPECIFIERS apply to this declarator.  Returns a
14378    representation of the entity declared.  If MEMBER_P is TRUE, then
14379    this declarator appears in a class scope.  The new DECL created by
14380    this declarator is returned.
14381
14382    The CHECKS are access checks that should be performed once we know
14383    what entity is being declared (and, therefore, what classes have
14384    befriended it).
14385
14386    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14387    for a function-definition here as well.  If the declarator is a
14388    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14389    be TRUE upon return.  By that point, the function-definition will
14390    have been completely parsed.
14391
14392    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14393    is FALSE.
14394
14395    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14396    parsed declaration if it is an uninitialized single declarator not followed
14397    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14398    if present, will not be consumed.  If returned, this declarator will be
14399    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14400
14401 static tree
14402 cp_parser_init_declarator (cp_parser* parser,
14403                            cp_decl_specifier_seq *decl_specifiers,
14404                            VEC (deferred_access_check,gc)* checks,
14405                            bool function_definition_allowed_p,
14406                            bool member_p,
14407                            int declares_class_or_enum,
14408                            bool* function_definition_p,
14409                            tree* maybe_range_for_decl)
14410 {
14411   cp_token *token = NULL, *asm_spec_start_token = NULL,
14412            *attributes_start_token = NULL;
14413   cp_declarator *declarator;
14414   tree prefix_attributes;
14415   tree attributes;
14416   tree asm_specification;
14417   tree initializer;
14418   tree decl = NULL_TREE;
14419   tree scope;
14420   int is_initialized;
14421   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14422      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14423      "(...)".  */
14424   enum cpp_ttype initialization_kind;
14425   bool is_direct_init = false;
14426   bool is_non_constant_init;
14427   int ctor_dtor_or_conv_p;
14428   bool friend_p;
14429   tree pushed_scope = NULL;
14430   bool range_for_decl_p = false;
14431
14432   /* Gather the attributes that were provided with the
14433      decl-specifiers.  */
14434   prefix_attributes = decl_specifiers->attributes;
14435
14436   /* Assume that this is not the declarator for a function
14437      definition.  */
14438   if (function_definition_p)
14439     *function_definition_p = false;
14440
14441   /* Defer access checks while parsing the declarator; we cannot know
14442      what names are accessible until we know what is being
14443      declared.  */
14444   resume_deferring_access_checks ();
14445
14446   /* Parse the declarator.  */
14447   token = cp_lexer_peek_token (parser->lexer);
14448   declarator
14449     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14450                             &ctor_dtor_or_conv_p,
14451                             /*parenthesized_p=*/NULL,
14452                             /*member_p=*/false);
14453   /* Gather up the deferred checks.  */
14454   stop_deferring_access_checks ();
14455
14456   /* If the DECLARATOR was erroneous, there's no need to go
14457      further.  */
14458   if (declarator == cp_error_declarator)
14459     return error_mark_node;
14460
14461   /* Check that the number of template-parameter-lists is OK.  */
14462   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14463                                                        token->location))
14464     return error_mark_node;
14465
14466   if (declares_class_or_enum & 2)
14467     cp_parser_check_for_definition_in_return_type (declarator,
14468                                                    decl_specifiers->type,
14469                                                    decl_specifiers->type_location);
14470
14471   /* Figure out what scope the entity declared by the DECLARATOR is
14472      located in.  `grokdeclarator' sometimes changes the scope, so
14473      we compute it now.  */
14474   scope = get_scope_of_declarator (declarator);
14475
14476   /* Perform any lookups in the declared type which were thought to be
14477      dependent, but are not in the scope of the declarator.  */
14478   decl_specifiers->type
14479     = maybe_update_decl_type (decl_specifiers->type, scope);
14480
14481   /* If we're allowing GNU extensions, look for an asm-specification
14482      and attributes.  */
14483   if (cp_parser_allow_gnu_extensions_p (parser))
14484     {
14485       /* Look for an asm-specification.  */
14486       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14487       asm_specification = cp_parser_asm_specification_opt (parser);
14488       /* And attributes.  */
14489       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14490       attributes = cp_parser_attributes_opt (parser);
14491     }
14492   else
14493     {
14494       asm_specification = NULL_TREE;
14495       attributes = NULL_TREE;
14496     }
14497
14498   /* Peek at the next token.  */
14499   token = cp_lexer_peek_token (parser->lexer);
14500   /* Check to see if the token indicates the start of a
14501      function-definition.  */
14502   if (function_declarator_p (declarator)
14503       && cp_parser_token_starts_function_definition_p (token))
14504     {
14505       if (!function_definition_allowed_p)
14506         {
14507           /* If a function-definition should not appear here, issue an
14508              error message.  */
14509           cp_parser_error (parser,
14510                            "a function-definition is not allowed here");
14511           return error_mark_node;
14512         }
14513       else
14514         {
14515           location_t func_brace_location
14516             = cp_lexer_peek_token (parser->lexer)->location;
14517
14518           /* Neither attributes nor an asm-specification are allowed
14519              on a function-definition.  */
14520           if (asm_specification)
14521             error_at (asm_spec_start_token->location,
14522                       "an asm-specification is not allowed "
14523                       "on a function-definition");
14524           if (attributes)
14525             error_at (attributes_start_token->location,
14526                       "attributes are not allowed on a function-definition");
14527           /* This is a function-definition.  */
14528           *function_definition_p = true;
14529
14530           /* Parse the function definition.  */
14531           if (member_p)
14532             decl = cp_parser_save_member_function_body (parser,
14533                                                         decl_specifiers,
14534                                                         declarator,
14535                                                         prefix_attributes);
14536           else
14537             decl
14538               = (cp_parser_function_definition_from_specifiers_and_declarator
14539                  (parser, decl_specifiers, prefix_attributes, declarator));
14540
14541           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14542             {
14543               /* This is where the prologue starts...  */
14544               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14545                 = func_brace_location;
14546             }
14547
14548           return decl;
14549         }
14550     }
14551
14552   /* [dcl.dcl]
14553
14554      Only in function declarations for constructors, destructors, and
14555      type conversions can the decl-specifier-seq be omitted.
14556
14557      We explicitly postpone this check past the point where we handle
14558      function-definitions because we tolerate function-definitions
14559      that are missing their return types in some modes.  */
14560   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14561     {
14562       cp_parser_error (parser,
14563                        "expected constructor, destructor, or type conversion");
14564       return error_mark_node;
14565     }
14566
14567   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14568   if (token->type == CPP_EQ
14569       || token->type == CPP_OPEN_PAREN
14570       || token->type == CPP_OPEN_BRACE)
14571     {
14572       is_initialized = SD_INITIALIZED;
14573       initialization_kind = token->type;
14574       if (maybe_range_for_decl)
14575         *maybe_range_for_decl = error_mark_node;
14576
14577       if (token->type == CPP_EQ
14578           && function_declarator_p (declarator))
14579         {
14580           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14581           if (t2->keyword == RID_DEFAULT)
14582             is_initialized = SD_DEFAULTED;
14583           else if (t2->keyword == RID_DELETE)
14584             is_initialized = SD_DELETED;
14585         }
14586     }
14587   else
14588     {
14589       /* If the init-declarator isn't initialized and isn't followed by a
14590          `,' or `;', it's not a valid init-declarator.  */
14591       if (token->type != CPP_COMMA
14592           && token->type != CPP_SEMICOLON)
14593         {
14594           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14595             range_for_decl_p = true;
14596           else
14597             {
14598               cp_parser_error (parser, "expected initializer");
14599               return error_mark_node;
14600             }
14601         }
14602       is_initialized = SD_UNINITIALIZED;
14603       initialization_kind = CPP_EOF;
14604     }
14605
14606   /* Because start_decl has side-effects, we should only call it if we
14607      know we're going ahead.  By this point, we know that we cannot
14608      possibly be looking at any other construct.  */
14609   cp_parser_commit_to_tentative_parse (parser);
14610
14611   /* If the decl specifiers were bad, issue an error now that we're
14612      sure this was intended to be a declarator.  Then continue
14613      declaring the variable(s), as int, to try to cut down on further
14614      errors.  */
14615   if (decl_specifiers->any_specifiers_p
14616       && decl_specifiers->type == error_mark_node)
14617     {
14618       cp_parser_error (parser, "invalid type in declaration");
14619       decl_specifiers->type = integer_type_node;
14620     }
14621
14622   /* Check to see whether or not this declaration is a friend.  */
14623   friend_p = cp_parser_friend_p (decl_specifiers);
14624
14625   /* Enter the newly declared entry in the symbol table.  If we're
14626      processing a declaration in a class-specifier, we wait until
14627      after processing the initializer.  */
14628   if (!member_p)
14629     {
14630       if (parser->in_unbraced_linkage_specification_p)
14631         decl_specifiers->storage_class = sc_extern;
14632       decl = start_decl (declarator, decl_specifiers,
14633                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14634                          attributes, prefix_attributes,
14635                          &pushed_scope);
14636       /* Adjust location of decl if declarator->id_loc is more appropriate:
14637          set, and decl wasn't merged with another decl, in which case its
14638          location would be different from input_location, and more accurate.  */
14639       if (DECL_P (decl)
14640           && declarator->id_loc != UNKNOWN_LOCATION
14641           && DECL_SOURCE_LOCATION (decl) == input_location)
14642         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14643     }
14644   else if (scope)
14645     /* Enter the SCOPE.  That way unqualified names appearing in the
14646        initializer will be looked up in SCOPE.  */
14647     pushed_scope = push_scope (scope);
14648
14649   /* Perform deferred access control checks, now that we know in which
14650      SCOPE the declared entity resides.  */
14651   if (!member_p && decl)
14652     {
14653       tree saved_current_function_decl = NULL_TREE;
14654
14655       /* If the entity being declared is a function, pretend that we
14656          are in its scope.  If it is a `friend', it may have access to
14657          things that would not otherwise be accessible.  */
14658       if (TREE_CODE (decl) == FUNCTION_DECL)
14659         {
14660           saved_current_function_decl = current_function_decl;
14661           current_function_decl = decl;
14662         }
14663
14664       /* Perform access checks for template parameters.  */
14665       cp_parser_perform_template_parameter_access_checks (checks);
14666
14667       /* Perform the access control checks for the declarator and the
14668          decl-specifiers.  */
14669       perform_deferred_access_checks ();
14670
14671       /* Restore the saved value.  */
14672       if (TREE_CODE (decl) == FUNCTION_DECL)
14673         current_function_decl = saved_current_function_decl;
14674     }
14675
14676   /* Parse the initializer.  */
14677   initializer = NULL_TREE;
14678   is_direct_init = false;
14679   is_non_constant_init = true;
14680   if (is_initialized)
14681     {
14682       if (function_declarator_p (declarator))
14683         {
14684           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14685            if (initialization_kind == CPP_EQ)
14686              initializer = cp_parser_pure_specifier (parser);
14687            else
14688              {
14689                /* If the declaration was erroneous, we don't really
14690                   know what the user intended, so just silently
14691                   consume the initializer.  */
14692                if (decl != error_mark_node)
14693                  error_at (initializer_start_token->location,
14694                            "initializer provided for function");
14695                cp_parser_skip_to_closing_parenthesis (parser,
14696                                                       /*recovering=*/true,
14697                                                       /*or_comma=*/false,
14698                                                       /*consume_paren=*/true);
14699              }
14700         }
14701       else
14702         {
14703           /* We want to record the extra mangling scope for in-class
14704              initializers of class members and initializers of static data
14705              member templates.  The former is a C++0x feature which isn't
14706              implemented yet, and I expect it will involve deferring
14707              parsing of the initializer until end of class as with default
14708              arguments.  So right here we only handle the latter.  */
14709           if (!member_p && processing_template_decl)
14710             start_lambda_scope (decl);
14711           initializer = cp_parser_initializer (parser,
14712                                                &is_direct_init,
14713                                                &is_non_constant_init);
14714           if (!member_p && processing_template_decl)
14715             finish_lambda_scope ();
14716         }
14717     }
14718
14719   /* The old parser allows attributes to appear after a parenthesized
14720      initializer.  Mark Mitchell proposed removing this functionality
14721      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14722      attributes -- but ignores them.  */
14723   if (cp_parser_allow_gnu_extensions_p (parser)
14724       && initialization_kind == CPP_OPEN_PAREN)
14725     if (cp_parser_attributes_opt (parser))
14726       warning (OPT_Wattributes,
14727                "attributes after parenthesized initializer ignored");
14728
14729   /* For an in-class declaration, use `grokfield' to create the
14730      declaration.  */
14731   if (member_p)
14732     {
14733       if (pushed_scope)
14734         {
14735           pop_scope (pushed_scope);
14736           pushed_scope = false;
14737         }
14738       decl = grokfield (declarator, decl_specifiers,
14739                         initializer, !is_non_constant_init,
14740                         /*asmspec=*/NULL_TREE,
14741                         prefix_attributes);
14742       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14743         cp_parser_save_default_args (parser, decl);
14744     }
14745
14746   /* Finish processing the declaration.  But, skip friend
14747      declarations.  */
14748   if (!friend_p && decl && decl != error_mark_node && !range_for_decl_p)
14749     {
14750       cp_finish_decl (decl,
14751                       initializer, !is_non_constant_init,
14752                       asm_specification,
14753                       /* If the initializer is in parentheses, then this is
14754                          a direct-initialization, which means that an
14755                          `explicit' constructor is OK.  Otherwise, an
14756                          `explicit' constructor cannot be used.  */
14757                       ((is_direct_init || !is_initialized)
14758                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14759     }
14760   else if ((cxx_dialect != cxx98) && friend_p
14761            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14762     /* Core issue #226 (C++0x only): A default template-argument
14763        shall not be specified in a friend class template
14764        declaration. */
14765     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14766                              /*is_partial=*/0, /*is_friend_decl=*/1);
14767
14768   if (!friend_p && pushed_scope)
14769     pop_scope (pushed_scope);
14770
14771   return decl;
14772 }
14773
14774 /* Parse a declarator.
14775
14776    declarator:
14777      direct-declarator
14778      ptr-operator declarator
14779
14780    abstract-declarator:
14781      ptr-operator abstract-declarator [opt]
14782      direct-abstract-declarator
14783
14784    GNU Extensions:
14785
14786    declarator:
14787      attributes [opt] direct-declarator
14788      attributes [opt] ptr-operator declarator
14789
14790    abstract-declarator:
14791      attributes [opt] ptr-operator abstract-declarator [opt]
14792      attributes [opt] direct-abstract-declarator
14793
14794    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14795    detect constructor, destructor or conversion operators. It is set
14796    to -1 if the declarator is a name, and +1 if it is a
14797    function. Otherwise it is set to zero. Usually you just want to
14798    test for >0, but internally the negative value is used.
14799
14800    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14801    a decl-specifier-seq unless it declares a constructor, destructor,
14802    or conversion.  It might seem that we could check this condition in
14803    semantic analysis, rather than parsing, but that makes it difficult
14804    to handle something like `f()'.  We want to notice that there are
14805    no decl-specifiers, and therefore realize that this is an
14806    expression, not a declaration.)
14807
14808    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14809    the declarator is a direct-declarator of the form "(...)".
14810
14811    MEMBER_P is true iff this declarator is a member-declarator.  */
14812
14813 static cp_declarator *
14814 cp_parser_declarator (cp_parser* parser,
14815                       cp_parser_declarator_kind dcl_kind,
14816                       int* ctor_dtor_or_conv_p,
14817                       bool* parenthesized_p,
14818                       bool member_p)
14819 {
14820   cp_declarator *declarator;
14821   enum tree_code code;
14822   cp_cv_quals cv_quals;
14823   tree class_type;
14824   tree attributes = NULL_TREE;
14825
14826   /* Assume this is not a constructor, destructor, or type-conversion
14827      operator.  */
14828   if (ctor_dtor_or_conv_p)
14829     *ctor_dtor_or_conv_p = 0;
14830
14831   if (cp_parser_allow_gnu_extensions_p (parser))
14832     attributes = cp_parser_attributes_opt (parser);
14833
14834   /* Check for the ptr-operator production.  */
14835   cp_parser_parse_tentatively (parser);
14836   /* Parse the ptr-operator.  */
14837   code = cp_parser_ptr_operator (parser,
14838                                  &class_type,
14839                                  &cv_quals);
14840   /* If that worked, then we have a ptr-operator.  */
14841   if (cp_parser_parse_definitely (parser))
14842     {
14843       /* If a ptr-operator was found, then this declarator was not
14844          parenthesized.  */
14845       if (parenthesized_p)
14846         *parenthesized_p = true;
14847       /* The dependent declarator is optional if we are parsing an
14848          abstract-declarator.  */
14849       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14850         cp_parser_parse_tentatively (parser);
14851
14852       /* Parse the dependent declarator.  */
14853       declarator = cp_parser_declarator (parser, dcl_kind,
14854                                          /*ctor_dtor_or_conv_p=*/NULL,
14855                                          /*parenthesized_p=*/NULL,
14856                                          /*member_p=*/false);
14857
14858       /* If we are parsing an abstract-declarator, we must handle the
14859          case where the dependent declarator is absent.  */
14860       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14861           && !cp_parser_parse_definitely (parser))
14862         declarator = NULL;
14863
14864       declarator = cp_parser_make_indirect_declarator
14865         (code, class_type, cv_quals, declarator);
14866     }
14867   /* Everything else is a direct-declarator.  */
14868   else
14869     {
14870       if (parenthesized_p)
14871         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14872                                                    CPP_OPEN_PAREN);
14873       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14874                                                 ctor_dtor_or_conv_p,
14875                                                 member_p);
14876     }
14877
14878   if (attributes && declarator && declarator != cp_error_declarator)
14879     declarator->attributes = attributes;
14880
14881   return declarator;
14882 }
14883
14884 /* Parse a direct-declarator or direct-abstract-declarator.
14885
14886    direct-declarator:
14887      declarator-id
14888      direct-declarator ( parameter-declaration-clause )
14889        cv-qualifier-seq [opt]
14890        exception-specification [opt]
14891      direct-declarator [ constant-expression [opt] ]
14892      ( declarator )
14893
14894    direct-abstract-declarator:
14895      direct-abstract-declarator [opt]
14896        ( parameter-declaration-clause )
14897        cv-qualifier-seq [opt]
14898        exception-specification [opt]
14899      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14900      ( abstract-declarator )
14901
14902    Returns a representation of the declarator.  DCL_KIND is
14903    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14904    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14905    we are parsing a direct-declarator.  It is
14906    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14907    of ambiguity we prefer an abstract declarator, as per
14908    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14909    cp_parser_declarator.  */
14910
14911 static cp_declarator *
14912 cp_parser_direct_declarator (cp_parser* parser,
14913                              cp_parser_declarator_kind dcl_kind,
14914                              int* ctor_dtor_or_conv_p,
14915                              bool member_p)
14916 {
14917   cp_token *token;
14918   cp_declarator *declarator = NULL;
14919   tree scope = NULL_TREE;
14920   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14921   bool saved_in_declarator_p = parser->in_declarator_p;
14922   bool first = true;
14923   tree pushed_scope = NULL_TREE;
14924
14925   while (true)
14926     {
14927       /* Peek at the next token.  */
14928       token = cp_lexer_peek_token (parser->lexer);
14929       if (token->type == CPP_OPEN_PAREN)
14930         {
14931           /* This is either a parameter-declaration-clause, or a
14932              parenthesized declarator. When we know we are parsing a
14933              named declarator, it must be a parenthesized declarator
14934              if FIRST is true. For instance, `(int)' is a
14935              parameter-declaration-clause, with an omitted
14936              direct-abstract-declarator. But `((*))', is a
14937              parenthesized abstract declarator. Finally, when T is a
14938              template parameter `(T)' is a
14939              parameter-declaration-clause, and not a parenthesized
14940              named declarator.
14941
14942              We first try and parse a parameter-declaration-clause,
14943              and then try a nested declarator (if FIRST is true).
14944
14945              It is not an error for it not to be a
14946              parameter-declaration-clause, even when FIRST is
14947              false. Consider,
14948
14949                int i (int);
14950                int i (3);
14951
14952              The first is the declaration of a function while the
14953              second is the definition of a variable, including its
14954              initializer.
14955
14956              Having seen only the parenthesis, we cannot know which of
14957              these two alternatives should be selected.  Even more
14958              complex are examples like:
14959
14960                int i (int (a));
14961                int i (int (3));
14962
14963              The former is a function-declaration; the latter is a
14964              variable initialization.
14965
14966              Thus again, we try a parameter-declaration-clause, and if
14967              that fails, we back out and return.  */
14968
14969           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14970             {
14971               tree params;
14972               unsigned saved_num_template_parameter_lists;
14973               bool is_declarator = false;
14974               tree t;
14975
14976               /* In a member-declarator, the only valid interpretation
14977                  of a parenthesis is the start of a
14978                  parameter-declaration-clause.  (It is invalid to
14979                  initialize a static data member with a parenthesized
14980                  initializer; only the "=" form of initialization is
14981                  permitted.)  */
14982               if (!member_p)
14983                 cp_parser_parse_tentatively (parser);
14984
14985               /* Consume the `('.  */
14986               cp_lexer_consume_token (parser->lexer);
14987               if (first)
14988                 {
14989                   /* If this is going to be an abstract declarator, we're
14990                      in a declarator and we can't have default args.  */
14991                   parser->default_arg_ok_p = false;
14992                   parser->in_declarator_p = true;
14993                 }
14994
14995               /* Inside the function parameter list, surrounding
14996                  template-parameter-lists do not apply.  */
14997               saved_num_template_parameter_lists
14998                 = parser->num_template_parameter_lists;
14999               parser->num_template_parameter_lists = 0;
15000
15001               begin_scope (sk_function_parms, NULL_TREE);
15002
15003               /* Parse the parameter-declaration-clause.  */
15004               params = cp_parser_parameter_declaration_clause (parser);
15005
15006               parser->num_template_parameter_lists
15007                 = saved_num_template_parameter_lists;
15008
15009               /* If all went well, parse the cv-qualifier-seq and the
15010                  exception-specification.  */
15011               if (member_p || cp_parser_parse_definitely (parser))
15012                 {
15013                   cp_cv_quals cv_quals;
15014                   tree exception_specification;
15015                   tree late_return;
15016
15017                   is_declarator = true;
15018
15019                   if (ctor_dtor_or_conv_p)
15020                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15021                   first = false;
15022                   /* Consume the `)'.  */
15023                   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15024
15025                   /* Parse the cv-qualifier-seq.  */
15026                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15027                   /* And the exception-specification.  */
15028                   exception_specification
15029                     = cp_parser_exception_specification_opt (parser);
15030
15031                   late_return
15032                     = cp_parser_late_return_type_opt (parser);
15033
15034                   /* Create the function-declarator.  */
15035                   declarator = make_call_declarator (declarator,
15036                                                      params,
15037                                                      cv_quals,
15038                                                      exception_specification,
15039                                                      late_return);
15040                   /* Any subsequent parameter lists are to do with
15041                      return type, so are not those of the declared
15042                      function.  */
15043                   parser->default_arg_ok_p = false;
15044                 }
15045
15046               /* Remove the function parms from scope.  */
15047               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15048                 pop_binding (DECL_NAME (t), t);
15049               leave_scope();
15050
15051               if (is_declarator)
15052                 /* Repeat the main loop.  */
15053                 continue;
15054             }
15055
15056           /* If this is the first, we can try a parenthesized
15057              declarator.  */
15058           if (first)
15059             {
15060               bool saved_in_type_id_in_expr_p;
15061
15062               parser->default_arg_ok_p = saved_default_arg_ok_p;
15063               parser->in_declarator_p = saved_in_declarator_p;
15064
15065               /* Consume the `('.  */
15066               cp_lexer_consume_token (parser->lexer);
15067               /* Parse the nested declarator.  */
15068               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15069               parser->in_type_id_in_expr_p = true;
15070               declarator
15071                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15072                                         /*parenthesized_p=*/NULL,
15073                                         member_p);
15074               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15075               first = false;
15076               /* Expect a `)'.  */
15077               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15078                 declarator = cp_error_declarator;
15079               if (declarator == cp_error_declarator)
15080                 break;
15081
15082               goto handle_declarator;
15083             }
15084           /* Otherwise, we must be done.  */
15085           else
15086             break;
15087         }
15088       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15089                && token->type == CPP_OPEN_SQUARE)
15090         {
15091           /* Parse an array-declarator.  */
15092           tree bounds;
15093
15094           if (ctor_dtor_or_conv_p)
15095             *ctor_dtor_or_conv_p = 0;
15096
15097           first = false;
15098           parser->default_arg_ok_p = false;
15099           parser->in_declarator_p = true;
15100           /* Consume the `['.  */
15101           cp_lexer_consume_token (parser->lexer);
15102           /* Peek at the next token.  */
15103           token = cp_lexer_peek_token (parser->lexer);
15104           /* If the next token is `]', then there is no
15105              constant-expression.  */
15106           if (token->type != CPP_CLOSE_SQUARE)
15107             {
15108               bool non_constant_p;
15109
15110               bounds
15111                 = cp_parser_constant_expression (parser,
15112                                                  /*allow_non_constant=*/true,
15113                                                  &non_constant_p);
15114               if (!non_constant_p || cxx_dialect >= cxx0x)
15115                 /* OK */;
15116               /* Normally, the array bound must be an integral constant
15117                  expression.  However, as an extension, we allow VLAs
15118                  in function scopes as long as they aren't part of a
15119                  parameter declaration.  */
15120               else if (!parser->in_function_body
15121                        || current_binding_level->kind == sk_function_parms)
15122                 {
15123                   cp_parser_error (parser,
15124                                    "array bound is not an integer constant");
15125                   bounds = error_mark_node;
15126                 }
15127               else if (processing_template_decl && !error_operand_p (bounds))
15128                 {
15129                   /* Remember this wasn't a constant-expression.  */
15130                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15131                   TREE_SIDE_EFFECTS (bounds) = 1;
15132                 }
15133             }
15134           else
15135             bounds = NULL_TREE;
15136           /* Look for the closing `]'.  */
15137           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15138             {
15139               declarator = cp_error_declarator;
15140               break;
15141             }
15142
15143           declarator = make_array_declarator (declarator, bounds);
15144         }
15145       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15146         {
15147           {
15148             tree qualifying_scope;
15149             tree unqualified_name;
15150             special_function_kind sfk;
15151             bool abstract_ok;
15152             bool pack_expansion_p = false;
15153             cp_token *declarator_id_start_token;
15154
15155             /* Parse a declarator-id */
15156             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15157             if (abstract_ok)
15158               {
15159                 cp_parser_parse_tentatively (parser);
15160
15161                 /* If we see an ellipsis, we should be looking at a
15162                    parameter pack. */
15163                 if (token->type == CPP_ELLIPSIS)
15164                   {
15165                     /* Consume the `...' */
15166                     cp_lexer_consume_token (parser->lexer);
15167
15168                     pack_expansion_p = true;
15169                   }
15170               }
15171
15172             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15173             unqualified_name
15174               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15175             qualifying_scope = parser->scope;
15176             if (abstract_ok)
15177               {
15178                 bool okay = false;
15179
15180                 if (!unqualified_name && pack_expansion_p)
15181                   {
15182                     /* Check whether an error occurred. */
15183                     okay = !cp_parser_error_occurred (parser);
15184
15185                     /* We already consumed the ellipsis to mark a
15186                        parameter pack, but we have no way to report it,
15187                        so abort the tentative parse. We will be exiting
15188                        immediately anyway. */
15189                     cp_parser_abort_tentative_parse (parser);
15190                   }
15191                 else
15192                   okay = cp_parser_parse_definitely (parser);
15193
15194                 if (!okay)
15195                   unqualified_name = error_mark_node;
15196                 else if (unqualified_name
15197                          && (qualifying_scope
15198                              || (TREE_CODE (unqualified_name)
15199                                  != IDENTIFIER_NODE)))
15200                   {
15201                     cp_parser_error (parser, "expected unqualified-id");
15202                     unqualified_name = error_mark_node;
15203                   }
15204               }
15205
15206             if (!unqualified_name)
15207               return NULL;
15208             if (unqualified_name == error_mark_node)
15209               {
15210                 declarator = cp_error_declarator;
15211                 pack_expansion_p = false;
15212                 declarator->parameter_pack_p = false;
15213                 break;
15214               }
15215
15216             if (qualifying_scope && at_namespace_scope_p ()
15217                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15218               {
15219                 /* In the declaration of a member of a template class
15220                    outside of the class itself, the SCOPE will sometimes
15221                    be a TYPENAME_TYPE.  For example, given:
15222
15223                    template <typename T>
15224                    int S<T>::R::i = 3;
15225
15226                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15227                    this context, we must resolve S<T>::R to an ordinary
15228                    type, rather than a typename type.
15229
15230                    The reason we normally avoid resolving TYPENAME_TYPEs
15231                    is that a specialization of `S' might render
15232                    `S<T>::R' not a type.  However, if `S' is
15233                    specialized, then this `i' will not be used, so there
15234                    is no harm in resolving the types here.  */
15235                 tree type;
15236
15237                 /* Resolve the TYPENAME_TYPE.  */
15238                 type = resolve_typename_type (qualifying_scope,
15239                                               /*only_current_p=*/false);
15240                 /* If that failed, the declarator is invalid.  */
15241                 if (TREE_CODE (type) == TYPENAME_TYPE)
15242                   {
15243                     if (typedef_variant_p (type))
15244                       error_at (declarator_id_start_token->location,
15245                                 "cannot define member of dependent typedef "
15246                                 "%qT", type);
15247                     else
15248                       error_at (declarator_id_start_token->location,
15249                                 "%<%T::%E%> is not a type",
15250                                 TYPE_CONTEXT (qualifying_scope),
15251                                 TYPE_IDENTIFIER (qualifying_scope));
15252                   }
15253                 qualifying_scope = type;
15254               }
15255
15256             sfk = sfk_none;
15257
15258             if (unqualified_name)
15259               {
15260                 tree class_type;
15261
15262                 if (qualifying_scope
15263                     && CLASS_TYPE_P (qualifying_scope))
15264                   class_type = qualifying_scope;
15265                 else
15266                   class_type = current_class_type;
15267
15268                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15269                   {
15270                     tree name_type = TREE_TYPE (unqualified_name);
15271                     if (class_type && same_type_p (name_type, class_type))
15272                       {
15273                         if (qualifying_scope
15274                             && CLASSTYPE_USE_TEMPLATE (name_type))
15275                           {
15276                             error_at (declarator_id_start_token->location,
15277                                       "invalid use of constructor as a template");
15278                             inform (declarator_id_start_token->location,
15279                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15280                                     "name the constructor in a qualified name",
15281                                     class_type,
15282                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15283                                     class_type, name_type);
15284                             declarator = cp_error_declarator;
15285                             break;
15286                           }
15287                         else
15288                           unqualified_name = constructor_name (class_type);
15289                       }
15290                     else
15291                       {
15292                         /* We do not attempt to print the declarator
15293                            here because we do not have enough
15294                            information about its original syntactic
15295                            form.  */
15296                         cp_parser_error (parser, "invalid declarator");
15297                         declarator = cp_error_declarator;
15298                         break;
15299                       }
15300                   }
15301
15302                 if (class_type)
15303                   {
15304                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15305                       sfk = sfk_destructor;
15306                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15307                       sfk = sfk_conversion;
15308                     else if (/* There's no way to declare a constructor
15309                                 for an anonymous type, even if the type
15310                                 got a name for linkage purposes.  */
15311                              !TYPE_WAS_ANONYMOUS (class_type)
15312                              && constructor_name_p (unqualified_name,
15313                                                     class_type))
15314                       {
15315                         unqualified_name = constructor_name (class_type);
15316                         sfk = sfk_constructor;
15317                       }
15318                     else if (is_overloaded_fn (unqualified_name)
15319                              && DECL_CONSTRUCTOR_P (get_first_fn
15320                                                     (unqualified_name)))
15321                       sfk = sfk_constructor;
15322
15323                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15324                       *ctor_dtor_or_conv_p = -1;
15325                   }
15326               }
15327             declarator = make_id_declarator (qualifying_scope,
15328                                              unqualified_name,
15329                                              sfk);
15330             declarator->id_loc = token->location;
15331             declarator->parameter_pack_p = pack_expansion_p;
15332
15333             if (pack_expansion_p)
15334               maybe_warn_variadic_templates ();
15335           }
15336
15337         handle_declarator:;
15338           scope = get_scope_of_declarator (declarator);
15339           if (scope)
15340             /* Any names that appear after the declarator-id for a
15341                member are looked up in the containing scope.  */
15342             pushed_scope = push_scope (scope);
15343           parser->in_declarator_p = true;
15344           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15345               || (declarator && declarator->kind == cdk_id))
15346             /* Default args are only allowed on function
15347                declarations.  */
15348             parser->default_arg_ok_p = saved_default_arg_ok_p;
15349           else
15350             parser->default_arg_ok_p = false;
15351
15352           first = false;
15353         }
15354       /* We're done.  */
15355       else
15356         break;
15357     }
15358
15359   /* For an abstract declarator, we might wind up with nothing at this
15360      point.  That's an error; the declarator is not optional.  */
15361   if (!declarator)
15362     cp_parser_error (parser, "expected declarator");
15363
15364   /* If we entered a scope, we must exit it now.  */
15365   if (pushed_scope)
15366     pop_scope (pushed_scope);
15367
15368   parser->default_arg_ok_p = saved_default_arg_ok_p;
15369   parser->in_declarator_p = saved_in_declarator_p;
15370
15371   return declarator;
15372 }
15373
15374 /* Parse a ptr-operator.
15375
15376    ptr-operator:
15377      * cv-qualifier-seq [opt]
15378      &
15379      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15380
15381    GNU Extension:
15382
15383    ptr-operator:
15384      & cv-qualifier-seq [opt]
15385
15386    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15387    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15388    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15389    filled in with the TYPE containing the member.  *CV_QUALS is
15390    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15391    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15392    Note that the tree codes returned by this function have nothing
15393    to do with the types of trees that will be eventually be created
15394    to represent the pointer or reference type being parsed. They are
15395    just constants with suggestive names. */
15396 static enum tree_code
15397 cp_parser_ptr_operator (cp_parser* parser,
15398                         tree* type,
15399                         cp_cv_quals *cv_quals)
15400 {
15401   enum tree_code code = ERROR_MARK;
15402   cp_token *token;
15403
15404   /* Assume that it's not a pointer-to-member.  */
15405   *type = NULL_TREE;
15406   /* And that there are no cv-qualifiers.  */
15407   *cv_quals = TYPE_UNQUALIFIED;
15408
15409   /* Peek at the next token.  */
15410   token = cp_lexer_peek_token (parser->lexer);
15411
15412   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15413   if (token->type == CPP_MULT)
15414     code = INDIRECT_REF;
15415   else if (token->type == CPP_AND)
15416     code = ADDR_EXPR;
15417   else if ((cxx_dialect != cxx98) &&
15418            token->type == CPP_AND_AND) /* C++0x only */
15419     code = NON_LVALUE_EXPR;
15420
15421   if (code != ERROR_MARK)
15422     {
15423       /* Consume the `*', `&' or `&&'.  */
15424       cp_lexer_consume_token (parser->lexer);
15425
15426       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15427          `&', if we are allowing GNU extensions.  (The only qualifier
15428          that can legally appear after `&' is `restrict', but that is
15429          enforced during semantic analysis.  */
15430       if (code == INDIRECT_REF
15431           || cp_parser_allow_gnu_extensions_p (parser))
15432         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15433     }
15434   else
15435     {
15436       /* Try the pointer-to-member case.  */
15437       cp_parser_parse_tentatively (parser);
15438       /* Look for the optional `::' operator.  */
15439       cp_parser_global_scope_opt (parser,
15440                                   /*current_scope_valid_p=*/false);
15441       /* Look for the nested-name specifier.  */
15442       token = cp_lexer_peek_token (parser->lexer);
15443       cp_parser_nested_name_specifier (parser,
15444                                        /*typename_keyword_p=*/false,
15445                                        /*check_dependency_p=*/true,
15446                                        /*type_p=*/false,
15447                                        /*is_declaration=*/false);
15448       /* If we found it, and the next token is a `*', then we are
15449          indeed looking at a pointer-to-member operator.  */
15450       if (!cp_parser_error_occurred (parser)
15451           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15452         {
15453           /* Indicate that the `*' operator was used.  */
15454           code = INDIRECT_REF;
15455
15456           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15457             error_at (token->location, "%qD is a namespace", parser->scope);
15458           else
15459             {
15460               /* The type of which the member is a member is given by the
15461                  current SCOPE.  */
15462               *type = parser->scope;
15463               /* The next name will not be qualified.  */
15464               parser->scope = NULL_TREE;
15465               parser->qualifying_scope = NULL_TREE;
15466               parser->object_scope = NULL_TREE;
15467               /* Look for the optional cv-qualifier-seq.  */
15468               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15469             }
15470         }
15471       /* If that didn't work we don't have a ptr-operator.  */
15472       if (!cp_parser_parse_definitely (parser))
15473         cp_parser_error (parser, "expected ptr-operator");
15474     }
15475
15476   return code;
15477 }
15478
15479 /* Parse an (optional) cv-qualifier-seq.
15480
15481    cv-qualifier-seq:
15482      cv-qualifier cv-qualifier-seq [opt]
15483
15484    cv-qualifier:
15485      const
15486      volatile
15487
15488    GNU Extension:
15489
15490    cv-qualifier:
15491      __restrict__
15492
15493    Returns a bitmask representing the cv-qualifiers.  */
15494
15495 static cp_cv_quals
15496 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15497 {
15498   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15499
15500   while (true)
15501     {
15502       cp_token *token;
15503       cp_cv_quals cv_qualifier;
15504
15505       /* Peek at the next token.  */
15506       token = cp_lexer_peek_token (parser->lexer);
15507       /* See if it's a cv-qualifier.  */
15508       switch (token->keyword)
15509         {
15510         case RID_CONST:
15511           cv_qualifier = TYPE_QUAL_CONST;
15512           break;
15513
15514         case RID_VOLATILE:
15515           cv_qualifier = TYPE_QUAL_VOLATILE;
15516           break;
15517
15518         case RID_RESTRICT:
15519           cv_qualifier = TYPE_QUAL_RESTRICT;
15520           break;
15521
15522         default:
15523           cv_qualifier = TYPE_UNQUALIFIED;
15524           break;
15525         }
15526
15527       if (!cv_qualifier)
15528         break;
15529
15530       if (cv_quals & cv_qualifier)
15531         {
15532           error_at (token->location, "duplicate cv-qualifier");
15533           cp_lexer_purge_token (parser->lexer);
15534         }
15535       else
15536         {
15537           cp_lexer_consume_token (parser->lexer);
15538           cv_quals |= cv_qualifier;
15539         }
15540     }
15541
15542   return cv_quals;
15543 }
15544
15545 /* Parse a late-specified return type, if any.  This is not a separate
15546    non-terminal, but part of a function declarator, which looks like
15547
15548    -> trailing-type-specifier-seq abstract-declarator(opt)
15549
15550    Returns the type indicated by the type-id.  */
15551
15552 static tree
15553 cp_parser_late_return_type_opt (cp_parser* parser)
15554 {
15555   cp_token *token;
15556
15557   /* Peek at the next token.  */
15558   token = cp_lexer_peek_token (parser->lexer);
15559   /* A late-specified return type is indicated by an initial '->'. */
15560   if (token->type != CPP_DEREF)
15561     return NULL_TREE;
15562
15563   /* Consume the ->.  */
15564   cp_lexer_consume_token (parser->lexer);
15565
15566   return cp_parser_trailing_type_id (parser);
15567 }
15568
15569 /* Parse a declarator-id.
15570
15571    declarator-id:
15572      id-expression
15573      :: [opt] nested-name-specifier [opt] type-name
15574
15575    In the `id-expression' case, the value returned is as for
15576    cp_parser_id_expression if the id-expression was an unqualified-id.
15577    If the id-expression was a qualified-id, then a SCOPE_REF is
15578    returned.  The first operand is the scope (either a NAMESPACE_DECL
15579    or TREE_TYPE), but the second is still just a representation of an
15580    unqualified-id.  */
15581
15582 static tree
15583 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15584 {
15585   tree id;
15586   /* The expression must be an id-expression.  Assume that qualified
15587      names are the names of types so that:
15588
15589        template <class T>
15590        int S<T>::R::i = 3;
15591
15592      will work; we must treat `S<T>::R' as the name of a type.
15593      Similarly, assume that qualified names are templates, where
15594      required, so that:
15595
15596        template <class T>
15597        int S<T>::R<T>::i = 3;
15598
15599      will work, too.  */
15600   id = cp_parser_id_expression (parser,
15601                                 /*template_keyword_p=*/false,
15602                                 /*check_dependency_p=*/false,
15603                                 /*template_p=*/NULL,
15604                                 /*declarator_p=*/true,
15605                                 optional_p);
15606   if (id && BASELINK_P (id))
15607     id = BASELINK_FUNCTIONS (id);
15608   return id;
15609 }
15610
15611 /* Parse a type-id.
15612
15613    type-id:
15614      type-specifier-seq abstract-declarator [opt]
15615
15616    Returns the TYPE specified.  */
15617
15618 static tree
15619 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15620                      bool is_trailing_return)
15621 {
15622   cp_decl_specifier_seq type_specifier_seq;
15623   cp_declarator *abstract_declarator;
15624
15625   /* Parse the type-specifier-seq.  */
15626   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15627                                 is_trailing_return,
15628                                 &type_specifier_seq);
15629   if (type_specifier_seq.type == error_mark_node)
15630     return error_mark_node;
15631
15632   /* There might or might not be an abstract declarator.  */
15633   cp_parser_parse_tentatively (parser);
15634   /* Look for the declarator.  */
15635   abstract_declarator
15636     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15637                             /*parenthesized_p=*/NULL,
15638                             /*member_p=*/false);
15639   /* Check to see if there really was a declarator.  */
15640   if (!cp_parser_parse_definitely (parser))
15641     abstract_declarator = NULL;
15642
15643   if (type_specifier_seq.type
15644       && type_uses_auto (type_specifier_seq.type))
15645     {
15646       /* A type-id with type 'auto' is only ok if the abstract declarator
15647          is a function declarator with a late-specified return type.  */
15648       if (abstract_declarator
15649           && abstract_declarator->kind == cdk_function
15650           && abstract_declarator->u.function.late_return_type)
15651         /* OK */;
15652       else
15653         {
15654           error ("invalid use of %<auto%>");
15655           return error_mark_node;
15656         }
15657     }
15658   
15659   return groktypename (&type_specifier_seq, abstract_declarator,
15660                        is_template_arg);
15661 }
15662
15663 static tree cp_parser_type_id (cp_parser *parser)
15664 {
15665   return cp_parser_type_id_1 (parser, false, false);
15666 }
15667
15668 static tree cp_parser_template_type_arg (cp_parser *parser)
15669 {
15670   return cp_parser_type_id_1 (parser, true, false);
15671 }
15672
15673 static tree cp_parser_trailing_type_id (cp_parser *parser)
15674 {
15675   return cp_parser_type_id_1 (parser, false, true);
15676 }
15677
15678 /* Parse a type-specifier-seq.
15679
15680    type-specifier-seq:
15681      type-specifier type-specifier-seq [opt]
15682
15683    GNU extension:
15684
15685    type-specifier-seq:
15686      attributes type-specifier-seq [opt]
15687
15688    If IS_DECLARATION is true, we are at the start of a "condition" or
15689    exception-declaration, so we might be followed by a declarator-id.
15690
15691    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15692    i.e. we've just seen "->".
15693
15694    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15695
15696 static void
15697 cp_parser_type_specifier_seq (cp_parser* parser,
15698                               bool is_declaration,
15699                               bool is_trailing_return,
15700                               cp_decl_specifier_seq *type_specifier_seq)
15701 {
15702   bool seen_type_specifier = false;
15703   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15704   cp_token *start_token = NULL;
15705
15706   /* Clear the TYPE_SPECIFIER_SEQ.  */
15707   clear_decl_specs (type_specifier_seq);
15708
15709   /* In the context of a trailing return type, enum E { } is an
15710      elaborated-type-specifier followed by a function-body, not an
15711      enum-specifier.  */
15712   if (is_trailing_return)
15713     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15714
15715   /* Parse the type-specifiers and attributes.  */
15716   while (true)
15717     {
15718       tree type_specifier;
15719       bool is_cv_qualifier;
15720
15721       /* Check for attributes first.  */
15722       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15723         {
15724           type_specifier_seq->attributes =
15725             chainon (type_specifier_seq->attributes,
15726                      cp_parser_attributes_opt (parser));
15727           continue;
15728         }
15729
15730       /* record the token of the beginning of the type specifier seq,
15731          for error reporting purposes*/
15732      if (!start_token)
15733        start_token = cp_lexer_peek_token (parser->lexer);
15734
15735       /* Look for the type-specifier.  */
15736       type_specifier = cp_parser_type_specifier (parser,
15737                                                  flags,
15738                                                  type_specifier_seq,
15739                                                  /*is_declaration=*/false,
15740                                                  NULL,
15741                                                  &is_cv_qualifier);
15742       if (!type_specifier)
15743         {
15744           /* If the first type-specifier could not be found, this is not a
15745              type-specifier-seq at all.  */
15746           if (!seen_type_specifier)
15747             {
15748               cp_parser_error (parser, "expected type-specifier");
15749               type_specifier_seq->type = error_mark_node;
15750               return;
15751             }
15752           /* If subsequent type-specifiers could not be found, the
15753              type-specifier-seq is complete.  */
15754           break;
15755         }
15756
15757       seen_type_specifier = true;
15758       /* The standard says that a condition can be:
15759
15760             type-specifier-seq declarator = assignment-expression
15761
15762          However, given:
15763
15764            struct S {};
15765            if (int S = ...)
15766
15767          we should treat the "S" as a declarator, not as a
15768          type-specifier.  The standard doesn't say that explicitly for
15769          type-specifier-seq, but it does say that for
15770          decl-specifier-seq in an ordinary declaration.  Perhaps it
15771          would be clearer just to allow a decl-specifier-seq here, and
15772          then add a semantic restriction that if any decl-specifiers
15773          that are not type-specifiers appear, the program is invalid.  */
15774       if (is_declaration && !is_cv_qualifier)
15775         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15776     }
15777
15778   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15779 }
15780
15781 /* Parse a parameter-declaration-clause.
15782
15783    parameter-declaration-clause:
15784      parameter-declaration-list [opt] ... [opt]
15785      parameter-declaration-list , ...
15786
15787    Returns a representation for the parameter declarations.  A return
15788    value of NULL indicates a parameter-declaration-clause consisting
15789    only of an ellipsis.  */
15790
15791 static tree
15792 cp_parser_parameter_declaration_clause (cp_parser* parser)
15793 {
15794   tree parameters;
15795   cp_token *token;
15796   bool ellipsis_p;
15797   bool is_error;
15798
15799   /* Peek at the next token.  */
15800   token = cp_lexer_peek_token (parser->lexer);
15801   /* Check for trivial parameter-declaration-clauses.  */
15802   if (token->type == CPP_ELLIPSIS)
15803     {
15804       /* Consume the `...' token.  */
15805       cp_lexer_consume_token (parser->lexer);
15806       return NULL_TREE;
15807     }
15808   else if (token->type == CPP_CLOSE_PAREN)
15809     /* There are no parameters.  */
15810     {
15811 #ifndef NO_IMPLICIT_EXTERN_C
15812       if (in_system_header && current_class_type == NULL
15813           && current_lang_name == lang_name_c)
15814         return NULL_TREE;
15815       else
15816 #endif
15817         return void_list_node;
15818     }
15819   /* Check for `(void)', too, which is a special case.  */
15820   else if (token->keyword == RID_VOID
15821            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15822                == CPP_CLOSE_PAREN))
15823     {
15824       /* Consume the `void' token.  */
15825       cp_lexer_consume_token (parser->lexer);
15826       /* There are no parameters.  */
15827       return void_list_node;
15828     }
15829
15830   /* Parse the parameter-declaration-list.  */
15831   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15832   /* If a parse error occurred while parsing the
15833      parameter-declaration-list, then the entire
15834      parameter-declaration-clause is erroneous.  */
15835   if (is_error)
15836     return NULL;
15837
15838   /* Peek at the next token.  */
15839   token = cp_lexer_peek_token (parser->lexer);
15840   /* If it's a `,', the clause should terminate with an ellipsis.  */
15841   if (token->type == CPP_COMMA)
15842     {
15843       /* Consume the `,'.  */
15844       cp_lexer_consume_token (parser->lexer);
15845       /* Expect an ellipsis.  */
15846       ellipsis_p
15847         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15848     }
15849   /* It might also be `...' if the optional trailing `,' was
15850      omitted.  */
15851   else if (token->type == CPP_ELLIPSIS)
15852     {
15853       /* Consume the `...' token.  */
15854       cp_lexer_consume_token (parser->lexer);
15855       /* And remember that we saw it.  */
15856       ellipsis_p = true;
15857     }
15858   else
15859     ellipsis_p = false;
15860
15861   /* Finish the parameter list.  */
15862   if (!ellipsis_p)
15863     parameters = chainon (parameters, void_list_node);
15864
15865   return parameters;
15866 }
15867
15868 /* Parse a parameter-declaration-list.
15869
15870    parameter-declaration-list:
15871      parameter-declaration
15872      parameter-declaration-list , parameter-declaration
15873
15874    Returns a representation of the parameter-declaration-list, as for
15875    cp_parser_parameter_declaration_clause.  However, the
15876    `void_list_node' is never appended to the list.  Upon return,
15877    *IS_ERROR will be true iff an error occurred.  */
15878
15879 static tree
15880 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15881 {
15882   tree parameters = NULL_TREE;
15883   tree *tail = &parameters; 
15884   bool saved_in_unbraced_linkage_specification_p;
15885   int index = 0;
15886
15887   /* Assume all will go well.  */
15888   *is_error = false;
15889   /* The special considerations that apply to a function within an
15890      unbraced linkage specifications do not apply to the parameters
15891      to the function.  */
15892   saved_in_unbraced_linkage_specification_p 
15893     = parser->in_unbraced_linkage_specification_p;
15894   parser->in_unbraced_linkage_specification_p = false;
15895
15896   /* Look for more parameters.  */
15897   while (true)
15898     {
15899       cp_parameter_declarator *parameter;
15900       tree decl = error_mark_node;
15901       bool parenthesized_p;
15902       /* Parse the parameter.  */
15903       parameter
15904         = cp_parser_parameter_declaration (parser,
15905                                            /*template_parm_p=*/false,
15906                                            &parenthesized_p);
15907
15908       /* We don't know yet if the enclosing context is deprecated, so wait
15909          and warn in grokparms if appropriate.  */
15910       deprecated_state = DEPRECATED_SUPPRESS;
15911
15912       if (parameter)
15913         decl = grokdeclarator (parameter->declarator,
15914                                &parameter->decl_specifiers,
15915                                PARM,
15916                                parameter->default_argument != NULL_TREE,
15917                                &parameter->decl_specifiers.attributes);
15918
15919       deprecated_state = DEPRECATED_NORMAL;
15920
15921       /* If a parse error occurred parsing the parameter declaration,
15922          then the entire parameter-declaration-list is erroneous.  */
15923       if (decl == error_mark_node)
15924         {
15925           *is_error = true;
15926           parameters = error_mark_node;
15927           break;
15928         }
15929
15930       if (parameter->decl_specifiers.attributes)
15931         cplus_decl_attributes (&decl,
15932                                parameter->decl_specifiers.attributes,
15933                                0);
15934       if (DECL_NAME (decl))
15935         decl = pushdecl (decl);
15936
15937       if (decl != error_mark_node)
15938         {
15939           retrofit_lang_decl (decl);
15940           DECL_PARM_INDEX (decl) = ++index;
15941         }
15942
15943       /* Add the new parameter to the list.  */
15944       *tail = build_tree_list (parameter->default_argument, decl);
15945       tail = &TREE_CHAIN (*tail);
15946
15947       /* Peek at the next token.  */
15948       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15949           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15950           /* These are for Objective-C++ */
15951           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15952           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15953         /* The parameter-declaration-list is complete.  */
15954         break;
15955       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15956         {
15957           cp_token *token;
15958
15959           /* Peek at the next token.  */
15960           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15961           /* If it's an ellipsis, then the list is complete.  */
15962           if (token->type == CPP_ELLIPSIS)
15963             break;
15964           /* Otherwise, there must be more parameters.  Consume the
15965              `,'.  */
15966           cp_lexer_consume_token (parser->lexer);
15967           /* When parsing something like:
15968
15969                 int i(float f, double d)
15970
15971              we can tell after seeing the declaration for "f" that we
15972              are not looking at an initialization of a variable "i",
15973              but rather at the declaration of a function "i".
15974
15975              Due to the fact that the parsing of template arguments
15976              (as specified to a template-id) requires backtracking we
15977              cannot use this technique when inside a template argument
15978              list.  */
15979           if (!parser->in_template_argument_list_p
15980               && !parser->in_type_id_in_expr_p
15981               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15982               /* However, a parameter-declaration of the form
15983                  "foat(f)" (which is a valid declaration of a
15984                  parameter "f") can also be interpreted as an
15985                  expression (the conversion of "f" to "float").  */
15986               && !parenthesized_p)
15987             cp_parser_commit_to_tentative_parse (parser);
15988         }
15989       else
15990         {
15991           cp_parser_error (parser, "expected %<,%> or %<...%>");
15992           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15993             cp_parser_skip_to_closing_parenthesis (parser,
15994                                                    /*recovering=*/true,
15995                                                    /*or_comma=*/false,
15996                                                    /*consume_paren=*/false);
15997           break;
15998         }
15999     }
16000
16001   parser->in_unbraced_linkage_specification_p
16002     = saved_in_unbraced_linkage_specification_p;
16003
16004   return parameters;
16005 }
16006
16007 /* Parse a parameter declaration.
16008
16009    parameter-declaration:
16010      decl-specifier-seq ... [opt] declarator
16011      decl-specifier-seq declarator = assignment-expression
16012      decl-specifier-seq ... [opt] abstract-declarator [opt]
16013      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16014
16015    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16016    declares a template parameter.  (In that case, a non-nested `>'
16017    token encountered during the parsing of the assignment-expression
16018    is not interpreted as a greater-than operator.)
16019
16020    Returns a representation of the parameter, or NULL if an error
16021    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16022    true iff the declarator is of the form "(p)".  */
16023
16024 static cp_parameter_declarator *
16025 cp_parser_parameter_declaration (cp_parser *parser,
16026                                  bool template_parm_p,
16027                                  bool *parenthesized_p)
16028 {
16029   int declares_class_or_enum;
16030   cp_decl_specifier_seq decl_specifiers;
16031   cp_declarator *declarator;
16032   tree default_argument;
16033   cp_token *token = NULL, *declarator_token_start = NULL;
16034   const char *saved_message;
16035
16036   /* In a template parameter, `>' is not an operator.
16037
16038      [temp.param]
16039
16040      When parsing a default template-argument for a non-type
16041      template-parameter, the first non-nested `>' is taken as the end
16042      of the template parameter-list rather than a greater-than
16043      operator.  */
16044
16045   /* Type definitions may not appear in parameter types.  */
16046   saved_message = parser->type_definition_forbidden_message;
16047   parser->type_definition_forbidden_message
16048     = G_("types may not be defined in parameter types");
16049
16050   /* Parse the declaration-specifiers.  */
16051   cp_parser_decl_specifier_seq (parser,
16052                                 CP_PARSER_FLAGS_NONE,
16053                                 &decl_specifiers,
16054                                 &declares_class_or_enum);
16055
16056   /* Complain about missing 'typename' or other invalid type names.  */
16057   if (!decl_specifiers.any_type_specifiers_p)
16058     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16059
16060   /* If an error occurred, there's no reason to attempt to parse the
16061      rest of the declaration.  */
16062   if (cp_parser_error_occurred (parser))
16063     {
16064       parser->type_definition_forbidden_message = saved_message;
16065       return NULL;
16066     }
16067
16068   /* Peek at the next token.  */
16069   token = cp_lexer_peek_token (parser->lexer);
16070
16071   /* If the next token is a `)', `,', `=', `>', or `...', then there
16072      is no declarator. However, when variadic templates are enabled,
16073      there may be a declarator following `...'.  */
16074   if (token->type == CPP_CLOSE_PAREN
16075       || token->type == CPP_COMMA
16076       || token->type == CPP_EQ
16077       || token->type == CPP_GREATER)
16078     {
16079       declarator = NULL;
16080       if (parenthesized_p)
16081         *parenthesized_p = false;
16082     }
16083   /* Otherwise, there should be a declarator.  */
16084   else
16085     {
16086       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16087       parser->default_arg_ok_p = false;
16088
16089       /* After seeing a decl-specifier-seq, if the next token is not a
16090          "(", there is no possibility that the code is a valid
16091          expression.  Therefore, if parsing tentatively, we commit at
16092          this point.  */
16093       if (!parser->in_template_argument_list_p
16094           /* In an expression context, having seen:
16095
16096                (int((char ...
16097
16098              we cannot be sure whether we are looking at a
16099              function-type (taking a "char" as a parameter) or a cast
16100              of some object of type "char" to "int".  */
16101           && !parser->in_type_id_in_expr_p
16102           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16103           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16104         cp_parser_commit_to_tentative_parse (parser);
16105       /* Parse the declarator.  */
16106       declarator_token_start = token;
16107       declarator = cp_parser_declarator (parser,
16108                                          CP_PARSER_DECLARATOR_EITHER,
16109                                          /*ctor_dtor_or_conv_p=*/NULL,
16110                                          parenthesized_p,
16111                                          /*member_p=*/false);
16112       parser->default_arg_ok_p = saved_default_arg_ok_p;
16113       /* After the declarator, allow more attributes.  */
16114       decl_specifiers.attributes
16115         = chainon (decl_specifiers.attributes,
16116                    cp_parser_attributes_opt (parser));
16117     }
16118
16119   /* If the next token is an ellipsis, and we have not seen a
16120      declarator name, and the type of the declarator contains parameter
16121      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16122      a parameter pack expansion expression. Otherwise, leave the
16123      ellipsis for a C-style variadic function. */
16124   token = cp_lexer_peek_token (parser->lexer);
16125   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16126     {
16127       tree type = decl_specifiers.type;
16128
16129       if (type && DECL_P (type))
16130         type = TREE_TYPE (type);
16131
16132       if (type
16133           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16134           && declarator_can_be_parameter_pack (declarator)
16135           && (!declarator || !declarator->parameter_pack_p)
16136           && uses_parameter_packs (type))
16137         {
16138           /* Consume the `...'. */
16139           cp_lexer_consume_token (parser->lexer);
16140           maybe_warn_variadic_templates ();
16141           
16142           /* Build a pack expansion type */
16143           if (declarator)
16144             declarator->parameter_pack_p = true;
16145           else
16146             decl_specifiers.type = make_pack_expansion (type);
16147         }
16148     }
16149
16150   /* The restriction on defining new types applies only to the type
16151      of the parameter, not to the default argument.  */
16152   parser->type_definition_forbidden_message = saved_message;
16153
16154   /* If the next token is `=', then process a default argument.  */
16155   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16156     {
16157       /* Consume the `='.  */
16158       cp_lexer_consume_token (parser->lexer);
16159
16160       /* If we are defining a class, then the tokens that make up the
16161          default argument must be saved and processed later.  */
16162       if (!template_parm_p && at_class_scope_p ()
16163           && TYPE_BEING_DEFINED (current_class_type)
16164           && !LAMBDA_TYPE_P (current_class_type))
16165         {
16166           unsigned depth = 0;
16167           int maybe_template_id = 0;
16168           cp_token *first_token;
16169           cp_token *token;
16170
16171           /* Add tokens until we have processed the entire default
16172              argument.  We add the range [first_token, token).  */
16173           first_token = cp_lexer_peek_token (parser->lexer);
16174           while (true)
16175             {
16176               bool done = false;
16177
16178               /* Peek at the next token.  */
16179               token = cp_lexer_peek_token (parser->lexer);
16180               /* What we do depends on what token we have.  */
16181               switch (token->type)
16182                 {
16183                   /* In valid code, a default argument must be
16184                      immediately followed by a `,' `)', or `...'.  */
16185                 case CPP_COMMA:
16186                   if (depth == 0 && maybe_template_id)
16187                     {
16188                       /* If we've seen a '<', we might be in a
16189                          template-argument-list.  Until Core issue 325 is
16190                          resolved, we don't know how this situation ought
16191                          to be handled, so try to DTRT.  We check whether
16192                          what comes after the comma is a valid parameter
16193                          declaration list.  If it is, then the comma ends
16194                          the default argument; otherwise the default
16195                          argument continues.  */
16196                       bool error = false;
16197                       tree t;
16198
16199                       /* Set ITALP so cp_parser_parameter_declaration_list
16200                          doesn't decide to commit to this parse.  */
16201                       bool saved_italp = parser->in_template_argument_list_p;
16202                       parser->in_template_argument_list_p = true;
16203
16204                       cp_parser_parse_tentatively (parser);
16205                       cp_lexer_consume_token (parser->lexer);
16206                       begin_scope (sk_function_parms, NULL_TREE);
16207                       cp_parser_parameter_declaration_list (parser, &error);
16208                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16209                         pop_binding (DECL_NAME (t), t);
16210                       leave_scope ();
16211                       if (!cp_parser_error_occurred (parser) && !error)
16212                         done = true;
16213                       cp_parser_abort_tentative_parse (parser);
16214
16215                       parser->in_template_argument_list_p = saved_italp;
16216                       break;
16217                     }
16218                 case CPP_CLOSE_PAREN:
16219                 case CPP_ELLIPSIS:
16220                   /* If we run into a non-nested `;', `}', or `]',
16221                      then the code is invalid -- but the default
16222                      argument is certainly over.  */
16223                 case CPP_SEMICOLON:
16224                 case CPP_CLOSE_BRACE:
16225                 case CPP_CLOSE_SQUARE:
16226                   if (depth == 0)
16227                     done = true;
16228                   /* Update DEPTH, if necessary.  */
16229                   else if (token->type == CPP_CLOSE_PAREN
16230                            || token->type == CPP_CLOSE_BRACE
16231                            || token->type == CPP_CLOSE_SQUARE)
16232                     --depth;
16233                   break;
16234
16235                 case CPP_OPEN_PAREN:
16236                 case CPP_OPEN_SQUARE:
16237                 case CPP_OPEN_BRACE:
16238                   ++depth;
16239                   break;
16240
16241                 case CPP_LESS:
16242                   if (depth == 0)
16243                     /* This might be the comparison operator, or it might
16244                        start a template argument list.  */
16245                     ++maybe_template_id;
16246                   break;
16247
16248                 case CPP_RSHIFT:
16249                   if (cxx_dialect == cxx98)
16250                     break;
16251                   /* Fall through for C++0x, which treats the `>>'
16252                      operator like two `>' tokens in certain
16253                      cases.  */
16254
16255                 case CPP_GREATER:
16256                   if (depth == 0)
16257                     {
16258                       /* This might be an operator, or it might close a
16259                          template argument list.  But if a previous '<'
16260                          started a template argument list, this will have
16261                          closed it, so we can't be in one anymore.  */
16262                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16263                       if (maybe_template_id < 0)
16264                         maybe_template_id = 0;
16265                     }
16266                   break;
16267
16268                   /* If we run out of tokens, issue an error message.  */
16269                 case CPP_EOF:
16270                 case CPP_PRAGMA_EOL:
16271                   error_at (token->location, "file ends in default argument");
16272                   done = true;
16273                   break;
16274
16275                 case CPP_NAME:
16276                 case CPP_SCOPE:
16277                   /* In these cases, we should look for template-ids.
16278                      For example, if the default argument is
16279                      `X<int, double>()', we need to do name lookup to
16280                      figure out whether or not `X' is a template; if
16281                      so, the `,' does not end the default argument.
16282
16283                      That is not yet done.  */
16284                   break;
16285
16286                 default:
16287                   break;
16288                 }
16289
16290               /* If we've reached the end, stop.  */
16291               if (done)
16292                 break;
16293
16294               /* Add the token to the token block.  */
16295               token = cp_lexer_consume_token (parser->lexer);
16296             }
16297
16298           /* Create a DEFAULT_ARG to represent the unparsed default
16299              argument.  */
16300           default_argument = make_node (DEFAULT_ARG);
16301           DEFARG_TOKENS (default_argument)
16302             = cp_token_cache_new (first_token, token);
16303           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16304         }
16305       /* Outside of a class definition, we can just parse the
16306          assignment-expression.  */
16307       else
16308         {
16309           token = cp_lexer_peek_token (parser->lexer);
16310           default_argument 
16311             = cp_parser_default_argument (parser, template_parm_p);
16312         }
16313
16314       if (!parser->default_arg_ok_p)
16315         {
16316           if (flag_permissive)
16317             warning (0, "deprecated use of default argument for parameter of non-function");
16318           else
16319             {
16320               error_at (token->location,
16321                         "default arguments are only "
16322                         "permitted for function parameters");
16323               default_argument = NULL_TREE;
16324             }
16325         }
16326       else if ((declarator && declarator->parameter_pack_p)
16327                || (decl_specifiers.type
16328                    && PACK_EXPANSION_P (decl_specifiers.type)))
16329         {
16330           /* Find the name of the parameter pack.  */     
16331           cp_declarator *id_declarator = declarator;
16332           while (id_declarator && id_declarator->kind != cdk_id)
16333             id_declarator = id_declarator->declarator;
16334           
16335           if (id_declarator && id_declarator->kind == cdk_id)
16336             error_at (declarator_token_start->location,
16337                       template_parm_p 
16338                       ? "template parameter pack %qD"
16339                       " cannot have a default argument"
16340                       : "parameter pack %qD cannot have a default argument",
16341                       id_declarator->u.id.unqualified_name);
16342           else
16343             error_at (declarator_token_start->location,
16344                       template_parm_p 
16345                       ? "template parameter pack cannot have a default argument"
16346                       : "parameter pack cannot have a default argument");
16347           
16348           default_argument = NULL_TREE;
16349         }
16350     }
16351   else
16352     default_argument = NULL_TREE;
16353
16354   return make_parameter_declarator (&decl_specifiers,
16355                                     declarator,
16356                                     default_argument);
16357 }
16358
16359 /* Parse a default argument and return it.
16360
16361    TEMPLATE_PARM_P is true if this is a default argument for a
16362    non-type template parameter.  */
16363 static tree
16364 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16365 {
16366   tree default_argument = NULL_TREE;
16367   bool saved_greater_than_is_operator_p;
16368   bool saved_local_variables_forbidden_p;
16369
16370   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16371      set correctly.  */
16372   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16373   parser->greater_than_is_operator_p = !template_parm_p;
16374   /* Local variable names (and the `this' keyword) may not
16375      appear in a default argument.  */
16376   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16377   parser->local_variables_forbidden_p = true;
16378   /* Parse the assignment-expression.  */
16379   if (template_parm_p)
16380     push_deferring_access_checks (dk_no_deferred);
16381   default_argument
16382     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16383   if (template_parm_p)
16384     pop_deferring_access_checks ();
16385   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16386   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16387
16388   return default_argument;
16389 }
16390
16391 /* Parse a function-body.
16392
16393    function-body:
16394      compound_statement  */
16395
16396 static void
16397 cp_parser_function_body (cp_parser *parser)
16398 {
16399   cp_parser_compound_statement (parser, NULL, false);
16400 }
16401
16402 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16403    true if a ctor-initializer was present.  */
16404
16405 static bool
16406 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16407 {
16408   tree body, list;
16409   bool ctor_initializer_p;
16410   const bool check_body_p =
16411      DECL_CONSTRUCTOR_P (current_function_decl)
16412      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16413   tree last = NULL;
16414
16415   /* Begin the function body.  */
16416   body = begin_function_body ();
16417   /* Parse the optional ctor-initializer.  */
16418   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16419
16420   /* If we're parsing a constexpr constructor definition, we need
16421      to check that the constructor body is indeed empty.  However,
16422      before we get to cp_parser_function_body lot of junk has been
16423      generated, so we can't just check that we have an empty block.
16424      Rather we take a snapshot of the outermost block, and check whether
16425      cp_parser_function_body changed its state.  */
16426   if (check_body_p)
16427     {
16428       list = body;
16429       if (TREE_CODE (list) == BIND_EXPR)
16430         list = BIND_EXPR_BODY (list);
16431       if (TREE_CODE (list) == STATEMENT_LIST
16432           && STATEMENT_LIST_TAIL (list) != NULL)
16433         last = STATEMENT_LIST_TAIL (list)->stmt;
16434     }
16435   /* Parse the function-body.  */
16436   cp_parser_function_body (parser);
16437   if (check_body_p)
16438     check_constexpr_ctor_body (last, list);
16439   /* Finish the function body.  */
16440   finish_function_body (body);
16441
16442   return ctor_initializer_p;
16443 }
16444
16445 /* Parse an initializer.
16446
16447    initializer:
16448      = initializer-clause
16449      ( expression-list )
16450
16451    Returns an expression representing the initializer.  If no
16452    initializer is present, NULL_TREE is returned.
16453
16454    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16455    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16456    set to TRUE if there is no initializer present.  If there is an
16457    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16458    is set to true; otherwise it is set to false.  */
16459
16460 static tree
16461 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16462                        bool* non_constant_p)
16463 {
16464   cp_token *token;
16465   tree init;
16466
16467   /* Peek at the next token.  */
16468   token = cp_lexer_peek_token (parser->lexer);
16469
16470   /* Let our caller know whether or not this initializer was
16471      parenthesized.  */
16472   *is_direct_init = (token->type != CPP_EQ);
16473   /* Assume that the initializer is constant.  */
16474   *non_constant_p = false;
16475
16476   if (token->type == CPP_EQ)
16477     {
16478       /* Consume the `='.  */
16479       cp_lexer_consume_token (parser->lexer);
16480       /* Parse the initializer-clause.  */
16481       init = cp_parser_initializer_clause (parser, non_constant_p);
16482     }
16483   else if (token->type == CPP_OPEN_PAREN)
16484     {
16485       VEC(tree,gc) *vec;
16486       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16487                                                      /*cast_p=*/false,
16488                                                      /*allow_expansion_p=*/true,
16489                                                      non_constant_p);
16490       if (vec == NULL)
16491         return error_mark_node;
16492       init = build_tree_list_vec (vec);
16493       release_tree_vector (vec);
16494     }
16495   else if (token->type == CPP_OPEN_BRACE)
16496     {
16497       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16498       init = cp_parser_braced_list (parser, non_constant_p);
16499       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16500     }
16501   else
16502     {
16503       /* Anything else is an error.  */
16504       cp_parser_error (parser, "expected initializer");
16505       init = error_mark_node;
16506     }
16507
16508   return init;
16509 }
16510
16511 /* Parse an initializer-clause.
16512
16513    initializer-clause:
16514      assignment-expression
16515      braced-init-list
16516
16517    Returns an expression representing the initializer.
16518
16519    If the `assignment-expression' production is used the value
16520    returned is simply a representation for the expression.
16521
16522    Otherwise, calls cp_parser_braced_list.  */
16523
16524 static tree
16525 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16526 {
16527   tree initializer;
16528
16529   /* Assume the expression is constant.  */
16530   *non_constant_p = false;
16531
16532   /* If it is not a `{', then we are looking at an
16533      assignment-expression.  */
16534   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16535     {
16536       initializer
16537         = cp_parser_constant_expression (parser,
16538                                         /*allow_non_constant_p=*/true,
16539                                         non_constant_p);
16540       if (!*non_constant_p)
16541         {
16542           /* We only want to fold if this is really a constant
16543              expression.  FIXME Actually, we don't want to fold here, but in
16544              cp_finish_decl.  */
16545           tree folded = fold_non_dependent_expr (initializer);
16546           folded = maybe_constant_value (folded);
16547           if (TREE_CONSTANT (folded))
16548             initializer = folded;
16549         }
16550     }
16551   else
16552     initializer = cp_parser_braced_list (parser, non_constant_p);
16553
16554   return initializer;
16555 }
16556
16557 /* Parse a brace-enclosed initializer list.
16558
16559    braced-init-list:
16560      { initializer-list , [opt] }
16561      { }
16562
16563    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16564    the elements of the initializer-list (or NULL, if the last
16565    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16566    NULL_TREE.  There is no way to detect whether or not the optional
16567    trailing `,' was provided.  NON_CONSTANT_P is as for
16568    cp_parser_initializer.  */     
16569
16570 static tree
16571 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16572 {
16573   tree initializer;
16574
16575   /* Consume the `{' token.  */
16576   cp_lexer_consume_token (parser->lexer);
16577   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16578   initializer = make_node (CONSTRUCTOR);
16579   /* If it's not a `}', then there is a non-trivial initializer.  */
16580   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16581     {
16582       /* Parse the initializer list.  */
16583       CONSTRUCTOR_ELTS (initializer)
16584         = cp_parser_initializer_list (parser, non_constant_p);
16585       /* A trailing `,' token is allowed.  */
16586       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16587         cp_lexer_consume_token (parser->lexer);
16588     }
16589   /* Now, there should be a trailing `}'.  */
16590   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16591   TREE_TYPE (initializer) = init_list_type_node;
16592   return initializer;
16593 }
16594
16595 /* Parse an initializer-list.
16596
16597    initializer-list:
16598      initializer-clause ... [opt]
16599      initializer-list , initializer-clause ... [opt]
16600
16601    GNU Extension:
16602
16603    initializer-list:
16604      identifier : initializer-clause
16605      initializer-list, identifier : initializer-clause
16606
16607    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16608    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16609    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16610    as for cp_parser_initializer.  */
16611
16612 static VEC(constructor_elt,gc) *
16613 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16614 {
16615   VEC(constructor_elt,gc) *v = NULL;
16616
16617   /* Assume all of the expressions are constant.  */
16618   *non_constant_p = false;
16619
16620   /* Parse the rest of the list.  */
16621   while (true)
16622     {
16623       cp_token *token;
16624       tree identifier;
16625       tree initializer;
16626       bool clause_non_constant_p;
16627
16628       /* If the next token is an identifier and the following one is a
16629          colon, we are looking at the GNU designated-initializer
16630          syntax.  */
16631       if (cp_parser_allow_gnu_extensions_p (parser)
16632           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16633           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16634         {
16635           /* Warn the user that they are using an extension.  */
16636           pedwarn (input_location, OPT_pedantic, 
16637                    "ISO C++ does not allow designated initializers");
16638           /* Consume the identifier.  */
16639           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16640           /* Consume the `:'.  */
16641           cp_lexer_consume_token (parser->lexer);
16642         }
16643       else
16644         identifier = NULL_TREE;
16645
16646       /* Parse the initializer.  */
16647       initializer = cp_parser_initializer_clause (parser,
16648                                                   &clause_non_constant_p);
16649       /* If any clause is non-constant, so is the entire initializer.  */
16650       if (clause_non_constant_p)
16651         *non_constant_p = true;
16652
16653       /* If we have an ellipsis, this is an initializer pack
16654          expansion.  */
16655       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16656         {
16657           /* Consume the `...'.  */
16658           cp_lexer_consume_token (parser->lexer);
16659
16660           /* Turn the initializer into an initializer expansion.  */
16661           initializer = make_pack_expansion (initializer);
16662         }
16663
16664       /* Add it to the vector.  */
16665       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16666
16667       /* If the next token is not a comma, we have reached the end of
16668          the list.  */
16669       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16670         break;
16671
16672       /* Peek at the next token.  */
16673       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16674       /* If the next token is a `}', then we're still done.  An
16675          initializer-clause can have a trailing `,' after the
16676          initializer-list and before the closing `}'.  */
16677       if (token->type == CPP_CLOSE_BRACE)
16678         break;
16679
16680       /* Consume the `,' token.  */
16681       cp_lexer_consume_token (parser->lexer);
16682     }
16683
16684   return v;
16685 }
16686
16687 /* Classes [gram.class] */
16688
16689 /* Parse a class-name.
16690
16691    class-name:
16692      identifier
16693      template-id
16694
16695    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16696    to indicate that names looked up in dependent types should be
16697    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16698    keyword has been used to indicate that the name that appears next
16699    is a template.  TAG_TYPE indicates the explicit tag given before
16700    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16701    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16702    is the class being defined in a class-head.
16703
16704    Returns the TYPE_DECL representing the class.  */
16705
16706 static tree
16707 cp_parser_class_name (cp_parser *parser,
16708                       bool typename_keyword_p,
16709                       bool template_keyword_p,
16710                       enum tag_types tag_type,
16711                       bool check_dependency_p,
16712                       bool class_head_p,
16713                       bool is_declaration)
16714 {
16715   tree decl;
16716   tree scope;
16717   bool typename_p;
16718   cp_token *token;
16719   tree identifier = NULL_TREE;
16720
16721   /* All class-names start with an identifier.  */
16722   token = cp_lexer_peek_token (parser->lexer);
16723   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16724     {
16725       cp_parser_error (parser, "expected class-name");
16726       return error_mark_node;
16727     }
16728
16729   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16730      to a template-id, so we save it here.  */
16731   scope = parser->scope;
16732   if (scope == error_mark_node)
16733     return error_mark_node;
16734
16735   /* Any name names a type if we're following the `typename' keyword
16736      in a qualified name where the enclosing scope is type-dependent.  */
16737   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16738                 && dependent_type_p (scope));
16739   /* Handle the common case (an identifier, but not a template-id)
16740      efficiently.  */
16741   if (token->type == CPP_NAME
16742       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16743     {
16744       cp_token *identifier_token;
16745       bool ambiguous_p;
16746
16747       /* Look for the identifier.  */
16748       identifier_token = cp_lexer_peek_token (parser->lexer);
16749       ambiguous_p = identifier_token->ambiguous_p;
16750       identifier = cp_parser_identifier (parser);
16751       /* If the next token isn't an identifier, we are certainly not
16752          looking at a class-name.  */
16753       if (identifier == error_mark_node)
16754         decl = error_mark_node;
16755       /* If we know this is a type-name, there's no need to look it
16756          up.  */
16757       else if (typename_p)
16758         decl = identifier;
16759       else
16760         {
16761           tree ambiguous_decls;
16762           /* If we already know that this lookup is ambiguous, then
16763              we've already issued an error message; there's no reason
16764              to check again.  */
16765           if (ambiguous_p)
16766             {
16767               cp_parser_simulate_error (parser);
16768               return error_mark_node;
16769             }
16770           /* If the next token is a `::', then the name must be a type
16771              name.
16772
16773              [basic.lookup.qual]
16774
16775              During the lookup for a name preceding the :: scope
16776              resolution operator, object, function, and enumerator
16777              names are ignored.  */
16778           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16779             tag_type = typename_type;
16780           /* Look up the name.  */
16781           decl = cp_parser_lookup_name (parser, identifier,
16782                                         tag_type,
16783                                         /*is_template=*/false,
16784                                         /*is_namespace=*/false,
16785                                         check_dependency_p,
16786                                         &ambiguous_decls,
16787                                         identifier_token->location);
16788           if (ambiguous_decls)
16789             {
16790               if (cp_parser_parsing_tentatively (parser))
16791                 cp_parser_simulate_error (parser);
16792               return error_mark_node;
16793             }
16794         }
16795     }
16796   else
16797     {
16798       /* Try a template-id.  */
16799       decl = cp_parser_template_id (parser, template_keyword_p,
16800                                     check_dependency_p,
16801                                     is_declaration);
16802       if (decl == error_mark_node)
16803         return error_mark_node;
16804     }
16805
16806   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16807
16808   /* If this is a typename, create a TYPENAME_TYPE.  */
16809   if (typename_p && decl != error_mark_node)
16810     {
16811       decl = make_typename_type (scope, decl, typename_type,
16812                                  /*complain=*/tf_error);
16813       if (decl != error_mark_node)
16814         decl = TYPE_NAME (decl);
16815     }
16816
16817   /* Check to see that it is really the name of a class.  */
16818   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16819       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16820       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16821     /* Situations like this:
16822
16823          template <typename T> struct A {
16824            typename T::template X<int>::I i;
16825          };
16826
16827        are problematic.  Is `T::template X<int>' a class-name?  The
16828        standard does not seem to be definitive, but there is no other
16829        valid interpretation of the following `::'.  Therefore, those
16830        names are considered class-names.  */
16831     {
16832       decl = make_typename_type (scope, decl, tag_type, tf_error);
16833       if (decl != error_mark_node)
16834         decl = TYPE_NAME (decl);
16835     }
16836   else if (TREE_CODE (decl) != TYPE_DECL
16837            || TREE_TYPE (decl) == error_mark_node
16838            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16839            /* In Objective-C 2.0, a classname followed by '.' starts a
16840               dot-syntax expression, and it's not a type-name.  */
16841            || (c_dialect_objc ()
16842                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16843                && objc_is_class_name (decl)))
16844     decl = error_mark_node;
16845
16846   if (decl == error_mark_node)
16847     cp_parser_error (parser, "expected class-name");
16848   else if (identifier && !parser->scope)
16849     maybe_note_name_used_in_class (identifier, decl);
16850
16851   return decl;
16852 }
16853
16854 /* Parse a class-specifier.
16855
16856    class-specifier:
16857      class-head { member-specification [opt] }
16858
16859    Returns the TREE_TYPE representing the class.  */
16860
16861 static tree
16862 cp_parser_class_specifier (cp_parser* parser)
16863 {
16864   tree type;
16865   tree attributes = NULL_TREE;
16866   bool nested_name_specifier_p;
16867   unsigned saved_num_template_parameter_lists;
16868   bool saved_in_function_body;
16869   bool saved_in_unbraced_linkage_specification_p;
16870   tree old_scope = NULL_TREE;
16871   tree scope = NULL_TREE;
16872   tree bases;
16873
16874   push_deferring_access_checks (dk_no_deferred);
16875
16876   /* Parse the class-head.  */
16877   type = cp_parser_class_head (parser,
16878                                &nested_name_specifier_p,
16879                                &attributes,
16880                                &bases);
16881   /* If the class-head was a semantic disaster, skip the entire body
16882      of the class.  */
16883   if (!type)
16884     {
16885       cp_parser_skip_to_end_of_block_or_statement (parser);
16886       pop_deferring_access_checks ();
16887       return error_mark_node;
16888     }
16889
16890   /* Look for the `{'.  */
16891   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16892     {
16893       pop_deferring_access_checks ();
16894       return error_mark_node;
16895     }
16896
16897   /* Process the base classes. If they're invalid, skip the 
16898      entire class body.  */
16899   if (!xref_basetypes (type, bases))
16900     {
16901       /* Consuming the closing brace yields better error messages
16902          later on.  */
16903       if (cp_parser_skip_to_closing_brace (parser))
16904         cp_lexer_consume_token (parser->lexer);
16905       pop_deferring_access_checks ();
16906       return error_mark_node;
16907     }
16908
16909   /* Issue an error message if type-definitions are forbidden here.  */
16910   cp_parser_check_type_definition (parser);
16911   /* Remember that we are defining one more class.  */
16912   ++parser->num_classes_being_defined;
16913   /* Inside the class, surrounding template-parameter-lists do not
16914      apply.  */
16915   saved_num_template_parameter_lists
16916     = parser->num_template_parameter_lists;
16917   parser->num_template_parameter_lists = 0;
16918   /* We are not in a function body.  */
16919   saved_in_function_body = parser->in_function_body;
16920   parser->in_function_body = false;
16921   /* We are not immediately inside an extern "lang" block.  */
16922   saved_in_unbraced_linkage_specification_p
16923     = parser->in_unbraced_linkage_specification_p;
16924   parser->in_unbraced_linkage_specification_p = false;
16925
16926   /* Start the class.  */
16927   if (nested_name_specifier_p)
16928     {
16929       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16930       old_scope = push_inner_scope (scope);
16931     }
16932   type = begin_class_definition (type, attributes);
16933
16934   if (type == error_mark_node)
16935     /* If the type is erroneous, skip the entire body of the class.  */
16936     cp_parser_skip_to_closing_brace (parser);
16937   else
16938     /* Parse the member-specification.  */
16939     cp_parser_member_specification_opt (parser);
16940
16941   /* Look for the trailing `}'.  */
16942   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16943   /* Look for trailing attributes to apply to this class.  */
16944   if (cp_parser_allow_gnu_extensions_p (parser))
16945     attributes = cp_parser_attributes_opt (parser);
16946   if (type != error_mark_node)
16947     type = finish_struct (type, attributes);
16948   if (nested_name_specifier_p)
16949     pop_inner_scope (old_scope, scope);
16950
16951   /* We've finished a type definition.  Check for the common syntax
16952      error of forgetting a semicolon after the definition.  We need to
16953      be careful, as we can't just check for not-a-semicolon and be done
16954      with it; the user might have typed:
16955
16956      class X { } c = ...;
16957      class X { } *p = ...;
16958
16959      and so forth.  Instead, enumerate all the possible tokens that
16960      might follow this production; if we don't see one of them, then
16961      complain and silently insert the semicolon.  */
16962   {
16963     cp_token *token = cp_lexer_peek_token (parser->lexer);
16964     bool want_semicolon = true;
16965
16966     switch (token->type)
16967       {
16968       case CPP_NAME:
16969       case CPP_SEMICOLON:
16970       case CPP_MULT:
16971       case CPP_AND:
16972       case CPP_OPEN_PAREN:
16973       case CPP_CLOSE_PAREN:
16974       case CPP_COMMA:
16975         want_semicolon = false;
16976         break;
16977
16978         /* While it's legal for type qualifiers and storage class
16979            specifiers to follow type definitions in the grammar, only
16980            compiler testsuites contain code like that.  Assume that if
16981            we see such code, then what we're really seeing is a case
16982            like:
16983
16984            class X { }
16985            const <type> var = ...;
16986
16987            or
16988
16989            class Y { }
16990            static <type> func (...) ...
16991
16992            i.e. the qualifier or specifier applies to the next
16993            declaration.  To do so, however, we need to look ahead one
16994            more token to see if *that* token is a type specifier.
16995
16996            This code could be improved to handle:
16997
16998            class Z { }
16999            static const <type> var = ...;  */
17000       case CPP_KEYWORD:
17001         if (keyword_is_storage_class_specifier (token->keyword)
17002             || keyword_is_type_qualifier (token->keyword))
17003           {
17004             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17005
17006             if (lookahead->type == CPP_KEYWORD
17007                 && !keyword_begins_type_specifier (lookahead->keyword))
17008               want_semicolon = false;
17009             else if (lookahead->type == CPP_NAME)
17010               /* Handling user-defined types here would be nice, but
17011                  very tricky.  */
17012               want_semicolon = false;
17013           }
17014         break;
17015       default:
17016         break;
17017       }
17018
17019     /* If we don't have a type, then something is very wrong and we
17020        shouldn't try to do anything clever.  */
17021     if (TYPE_P (type) && want_semicolon)
17022       {
17023         cp_token_position prev
17024           = cp_lexer_previous_token_position (parser->lexer);
17025         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17026         location_t loc = prev_token->location;
17027
17028         if (CLASSTYPE_DECLARED_CLASS (type))
17029           error_at (loc, "expected %<;%> after class definition");
17030         else if (TREE_CODE (type) == RECORD_TYPE)
17031           error_at (loc, "expected %<;%> after struct definition");
17032         else if (TREE_CODE (type) == UNION_TYPE)
17033           error_at (loc, "expected %<;%> after union definition");
17034         else
17035           gcc_unreachable ();
17036
17037         /* Unget one token and smash it to look as though we encountered
17038            a semicolon in the input stream.  */
17039         cp_lexer_set_token_position (parser->lexer, prev);
17040         token = cp_lexer_peek_token (parser->lexer);
17041         token->type = CPP_SEMICOLON;
17042         token->keyword = RID_MAX;
17043       }
17044   }
17045
17046   /* If this class is not itself within the scope of another class,
17047      then we need to parse the bodies of all of the queued function
17048      definitions.  Note that the queued functions defined in a class
17049      are not always processed immediately following the
17050      class-specifier for that class.  Consider:
17051
17052        struct A {
17053          struct B { void f() { sizeof (A); } };
17054        };
17055
17056      If `f' were processed before the processing of `A' were
17057      completed, there would be no way to compute the size of `A'.
17058      Note that the nesting we are interested in here is lexical --
17059      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17060      for:
17061
17062        struct A { struct B; };
17063        struct A::B { void f() { } };
17064
17065      there is no need to delay the parsing of `A::B::f'.  */
17066   if (--parser->num_classes_being_defined == 0)
17067     {
17068       tree fn;
17069       tree class_type = NULL_TREE;
17070       tree pushed_scope = NULL_TREE;
17071       unsigned ix;
17072       cp_default_arg_entry *e;
17073
17074       /* In a first pass, parse default arguments to the functions.
17075          Then, in a second pass, parse the bodies of the functions.
17076          This two-phased approach handles cases like:
17077
17078             struct S {
17079               void f() { g(); }
17080               void g(int i = 3);
17081             };
17082
17083          */
17084       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17085                         ix, e)
17086         {
17087           fn = e->decl;
17088           /* If there are default arguments that have not yet been processed,
17089              take care of them now.  */
17090           if (class_type != e->class_type)
17091             {
17092               if (pushed_scope)
17093                 pop_scope (pushed_scope);
17094               class_type = e->class_type;
17095               pushed_scope = push_scope (class_type);
17096             }
17097           /* Make sure that any template parameters are in scope.  */
17098           maybe_begin_member_template_processing (fn);
17099           /* Parse the default argument expressions.  */
17100           cp_parser_late_parsing_default_args (parser, fn);
17101           /* Remove any template parameters from the symbol table.  */
17102           maybe_end_member_template_processing ();
17103         }
17104       if (pushed_scope)
17105         pop_scope (pushed_scope);
17106       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17107       /* Now parse the body of the functions.  */
17108       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17109         cp_parser_late_parsing_for_member (parser, fn);
17110       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17111     }
17112
17113   /* Put back any saved access checks.  */
17114   pop_deferring_access_checks ();
17115
17116   /* Restore saved state.  */
17117   parser->in_function_body = saved_in_function_body;
17118   parser->num_template_parameter_lists
17119     = saved_num_template_parameter_lists;
17120   parser->in_unbraced_linkage_specification_p
17121     = saved_in_unbraced_linkage_specification_p;
17122
17123   return type;
17124 }
17125
17126 /* Parse a class-head.
17127
17128    class-head:
17129      class-key identifier [opt] base-clause [opt]
17130      class-key nested-name-specifier identifier base-clause [opt]
17131      class-key nested-name-specifier [opt] template-id
17132        base-clause [opt]
17133
17134    GNU Extensions:
17135      class-key attributes identifier [opt] base-clause [opt]
17136      class-key attributes nested-name-specifier identifier base-clause [opt]
17137      class-key attributes nested-name-specifier [opt] template-id
17138        base-clause [opt]
17139
17140    Upon return BASES is initialized to the list of base classes (or
17141    NULL, if there are none) in the same form returned by
17142    cp_parser_base_clause.
17143
17144    Returns the TYPE of the indicated class.  Sets
17145    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17146    involving a nested-name-specifier was used, and FALSE otherwise.
17147
17148    Returns error_mark_node if this is not a class-head.
17149
17150    Returns NULL_TREE if the class-head is syntactically valid, but
17151    semantically invalid in a way that means we should skip the entire
17152    body of the class.  */
17153
17154 static tree
17155 cp_parser_class_head (cp_parser* parser,
17156                       bool* nested_name_specifier_p,
17157                       tree *attributes_p,
17158                       tree *bases)
17159 {
17160   tree nested_name_specifier;
17161   enum tag_types class_key;
17162   tree id = NULL_TREE;
17163   tree type = NULL_TREE;
17164   tree attributes;
17165   bool template_id_p = false;
17166   bool qualified_p = false;
17167   bool invalid_nested_name_p = false;
17168   bool invalid_explicit_specialization_p = false;
17169   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17170   tree pushed_scope = NULL_TREE;
17171   unsigned num_templates;
17172   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17173   /* Assume no nested-name-specifier will be present.  */
17174   *nested_name_specifier_p = false;
17175   /* Assume no template parameter lists will be used in defining the
17176      type.  */
17177   num_templates = 0;
17178   parser->colon_corrects_to_scope_p = false;
17179
17180   *bases = NULL_TREE;
17181
17182   /* Look for the class-key.  */
17183   class_key = cp_parser_class_key (parser);
17184   if (class_key == none_type)
17185     return error_mark_node;
17186
17187   /* Parse the attributes.  */
17188   attributes = cp_parser_attributes_opt (parser);
17189
17190   /* If the next token is `::', that is invalid -- but sometimes
17191      people do try to write:
17192
17193        struct ::S {};
17194
17195      Handle this gracefully by accepting the extra qualifier, and then
17196      issuing an error about it later if this really is a
17197      class-head.  If it turns out just to be an elaborated type
17198      specifier, remain silent.  */
17199   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17200     qualified_p = true;
17201
17202   push_deferring_access_checks (dk_no_check);
17203
17204   /* Determine the name of the class.  Begin by looking for an
17205      optional nested-name-specifier.  */
17206   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17207   nested_name_specifier
17208     = cp_parser_nested_name_specifier_opt (parser,
17209                                            /*typename_keyword_p=*/false,
17210                                            /*check_dependency_p=*/false,
17211                                            /*type_p=*/false,
17212                                            /*is_declaration=*/false);
17213   /* If there was a nested-name-specifier, then there *must* be an
17214      identifier.  */
17215   if (nested_name_specifier)
17216     {
17217       type_start_token = cp_lexer_peek_token (parser->lexer);
17218       /* Although the grammar says `identifier', it really means
17219          `class-name' or `template-name'.  You are only allowed to
17220          define a class that has already been declared with this
17221          syntax.
17222
17223          The proposed resolution for Core Issue 180 says that wherever
17224          you see `class T::X' you should treat `X' as a type-name.
17225
17226          It is OK to define an inaccessible class; for example:
17227
17228            class A { class B; };
17229            class A::B {};
17230
17231          We do not know if we will see a class-name, or a
17232          template-name.  We look for a class-name first, in case the
17233          class-name is a template-id; if we looked for the
17234          template-name first we would stop after the template-name.  */
17235       cp_parser_parse_tentatively (parser);
17236       type = cp_parser_class_name (parser,
17237                                    /*typename_keyword_p=*/false,
17238                                    /*template_keyword_p=*/false,
17239                                    class_type,
17240                                    /*check_dependency_p=*/false,
17241                                    /*class_head_p=*/true,
17242                                    /*is_declaration=*/false);
17243       /* If that didn't work, ignore the nested-name-specifier.  */
17244       if (!cp_parser_parse_definitely (parser))
17245         {
17246           invalid_nested_name_p = true;
17247           type_start_token = cp_lexer_peek_token (parser->lexer);
17248           id = cp_parser_identifier (parser);
17249           if (id == error_mark_node)
17250             id = NULL_TREE;
17251         }
17252       /* If we could not find a corresponding TYPE, treat this
17253          declaration like an unqualified declaration.  */
17254       if (type == error_mark_node)
17255         nested_name_specifier = NULL_TREE;
17256       /* Otherwise, count the number of templates used in TYPE and its
17257          containing scopes.  */
17258       else
17259         {
17260           tree scope;
17261
17262           for (scope = TREE_TYPE (type);
17263                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17264                scope = (TYPE_P (scope)
17265                         ? TYPE_CONTEXT (scope)
17266                         : DECL_CONTEXT (scope)))
17267             if (TYPE_P (scope)
17268                 && CLASS_TYPE_P (scope)
17269                 && CLASSTYPE_TEMPLATE_INFO (scope)
17270                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17271                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17272               ++num_templates;
17273         }
17274     }
17275   /* Otherwise, the identifier is optional.  */
17276   else
17277     {
17278       /* We don't know whether what comes next is a template-id,
17279          an identifier, or nothing at all.  */
17280       cp_parser_parse_tentatively (parser);
17281       /* Check for a template-id.  */
17282       type_start_token = cp_lexer_peek_token (parser->lexer);
17283       id = cp_parser_template_id (parser,
17284                                   /*template_keyword_p=*/false,
17285                                   /*check_dependency_p=*/true,
17286                                   /*is_declaration=*/true);
17287       /* If that didn't work, it could still be an identifier.  */
17288       if (!cp_parser_parse_definitely (parser))
17289         {
17290           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17291             {
17292               type_start_token = cp_lexer_peek_token (parser->lexer);
17293               id = cp_parser_identifier (parser);
17294             }
17295           else
17296             id = NULL_TREE;
17297         }
17298       else
17299         {
17300           template_id_p = true;
17301           ++num_templates;
17302         }
17303     }
17304
17305   pop_deferring_access_checks ();
17306
17307   if (id)
17308     cp_parser_check_for_invalid_template_id (parser, id,
17309                                              type_start_token->location);
17310
17311   /* If it's not a `:' or a `{' then we can't really be looking at a
17312      class-head, since a class-head only appears as part of a
17313      class-specifier.  We have to detect this situation before calling
17314      xref_tag, since that has irreversible side-effects.  */
17315   if (!cp_parser_next_token_starts_class_definition_p (parser))
17316     {
17317       cp_parser_error (parser, "expected %<{%> or %<:%>");
17318       type = error_mark_node;
17319       goto out;
17320     }
17321
17322   /* At this point, we're going ahead with the class-specifier, even
17323      if some other problem occurs.  */
17324   cp_parser_commit_to_tentative_parse (parser);
17325   /* Issue the error about the overly-qualified name now.  */
17326   if (qualified_p)
17327     {
17328       cp_parser_error (parser,
17329                        "global qualification of class name is invalid");
17330       type = error_mark_node;
17331       goto out;
17332     }
17333   else if (invalid_nested_name_p)
17334     {
17335       cp_parser_error (parser,
17336                        "qualified name does not name a class");
17337       type = error_mark_node;
17338       goto out;
17339     }
17340   else if (nested_name_specifier)
17341     {
17342       tree scope;
17343
17344       /* Reject typedef-names in class heads.  */
17345       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17346         {
17347           error_at (type_start_token->location,
17348                     "invalid class name in declaration of %qD",
17349                     type);
17350           type = NULL_TREE;
17351           goto done;
17352         }
17353
17354       /* Figure out in what scope the declaration is being placed.  */
17355       scope = current_scope ();
17356       /* If that scope does not contain the scope in which the
17357          class was originally declared, the program is invalid.  */
17358       if (scope && !is_ancestor (scope, nested_name_specifier))
17359         {
17360           if (at_namespace_scope_p ())
17361             error_at (type_start_token->location,
17362                       "declaration of %qD in namespace %qD which does not "
17363                       "enclose %qD",
17364                       type, scope, nested_name_specifier);
17365           else
17366             error_at (type_start_token->location,
17367                       "declaration of %qD in %qD which does not enclose %qD",
17368                       type, scope, nested_name_specifier);
17369           type = NULL_TREE;
17370           goto done;
17371         }
17372       /* [dcl.meaning]
17373
17374          A declarator-id shall not be qualified except for the
17375          definition of a ... nested class outside of its class
17376          ... [or] the definition or explicit instantiation of a
17377          class member of a namespace outside of its namespace.  */
17378       if (scope == nested_name_specifier)
17379         {
17380           permerror (nested_name_specifier_token_start->location,
17381                      "extra qualification not allowed");
17382           nested_name_specifier = NULL_TREE;
17383           num_templates = 0;
17384         }
17385     }
17386   /* An explicit-specialization must be preceded by "template <>".  If
17387      it is not, try to recover gracefully.  */
17388   if (at_namespace_scope_p ()
17389       && parser->num_template_parameter_lists == 0
17390       && template_id_p)
17391     {
17392       error_at (type_start_token->location,
17393                 "an explicit specialization must be preceded by %<template <>%>");
17394       invalid_explicit_specialization_p = true;
17395       /* Take the same action that would have been taken by
17396          cp_parser_explicit_specialization.  */
17397       ++parser->num_template_parameter_lists;
17398       begin_specialization ();
17399     }
17400   /* There must be no "return" statements between this point and the
17401      end of this function; set "type "to the correct return value and
17402      use "goto done;" to return.  */
17403   /* Make sure that the right number of template parameters were
17404      present.  */
17405   if (!cp_parser_check_template_parameters (parser, num_templates,
17406                                             type_start_token->location,
17407                                             /*declarator=*/NULL))
17408     {
17409       /* If something went wrong, there is no point in even trying to
17410          process the class-definition.  */
17411       type = NULL_TREE;
17412       goto done;
17413     }
17414
17415   /* Look up the type.  */
17416   if (template_id_p)
17417     {
17418       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17419           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17420               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17421         {
17422           error_at (type_start_token->location,
17423                     "function template %qD redeclared as a class template", id);
17424           type = error_mark_node;
17425         }
17426       else
17427         {
17428           type = TREE_TYPE (id);
17429           type = maybe_process_partial_specialization (type);
17430         }
17431       if (nested_name_specifier)
17432         pushed_scope = push_scope (nested_name_specifier);
17433     }
17434   else if (nested_name_specifier)
17435     {
17436       tree class_type;
17437
17438       /* Given:
17439
17440             template <typename T> struct S { struct T };
17441             template <typename T> struct S<T>::T { };
17442
17443          we will get a TYPENAME_TYPE when processing the definition of
17444          `S::T'.  We need to resolve it to the actual type before we
17445          try to define it.  */
17446       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17447         {
17448           class_type = resolve_typename_type (TREE_TYPE (type),
17449                                               /*only_current_p=*/false);
17450           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17451             type = TYPE_NAME (class_type);
17452           else
17453             {
17454               cp_parser_error (parser, "could not resolve typename type");
17455               type = error_mark_node;
17456             }
17457         }
17458
17459       if (maybe_process_partial_specialization (TREE_TYPE (type))
17460           == error_mark_node)
17461         {
17462           type = NULL_TREE;
17463           goto done;
17464         }
17465
17466       class_type = current_class_type;
17467       /* Enter the scope indicated by the nested-name-specifier.  */
17468       pushed_scope = push_scope (nested_name_specifier);
17469       /* Get the canonical version of this type.  */
17470       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17471       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17472           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17473         {
17474           type = push_template_decl (type);
17475           if (type == error_mark_node)
17476             {
17477               type = NULL_TREE;
17478               goto done;
17479             }
17480         }
17481
17482       type = TREE_TYPE (type);
17483       *nested_name_specifier_p = true;
17484     }
17485   else      /* The name is not a nested name.  */
17486     {
17487       /* If the class was unnamed, create a dummy name.  */
17488       if (!id)
17489         id = make_anon_name ();
17490       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17491                        parser->num_template_parameter_lists);
17492     }
17493
17494   /* Indicate whether this class was declared as a `class' or as a
17495      `struct'.  */
17496   if (TREE_CODE (type) == RECORD_TYPE)
17497     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17498   cp_parser_check_class_key (class_key, type);
17499
17500   /* If this type was already complete, and we see another definition,
17501      that's an error.  */
17502   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17503     {
17504       error_at (type_start_token->location, "redefinition of %q#T",
17505                 type);
17506       error_at (type_start_token->location, "previous definition of %q+#T",
17507                 type);
17508       type = NULL_TREE;
17509       goto done;
17510     }
17511   else if (type == error_mark_node)
17512     type = NULL_TREE;
17513
17514   /* We will have entered the scope containing the class; the names of
17515      base classes should be looked up in that context.  For example:
17516
17517        struct A { struct B {}; struct C; };
17518        struct A::C : B {};
17519
17520      is valid.  */
17521
17522   /* Get the list of base-classes, if there is one.  */
17523   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17524     *bases = cp_parser_base_clause (parser);
17525
17526  done:
17527   /* Leave the scope given by the nested-name-specifier.  We will
17528      enter the class scope itself while processing the members.  */
17529   if (pushed_scope)
17530     pop_scope (pushed_scope);
17531
17532   if (invalid_explicit_specialization_p)
17533     {
17534       end_specialization ();
17535       --parser->num_template_parameter_lists;
17536     }
17537
17538   if (type)
17539     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17540   *attributes_p = attributes;
17541  out:
17542   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17543   return type;
17544 }
17545
17546 /* Parse a class-key.
17547
17548    class-key:
17549      class
17550      struct
17551      union
17552
17553    Returns the kind of class-key specified, or none_type to indicate
17554    error.  */
17555
17556 static enum tag_types
17557 cp_parser_class_key (cp_parser* parser)
17558 {
17559   cp_token *token;
17560   enum tag_types tag_type;
17561
17562   /* Look for the class-key.  */
17563   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17564   if (!token)
17565     return none_type;
17566
17567   /* Check to see if the TOKEN is a class-key.  */
17568   tag_type = cp_parser_token_is_class_key (token);
17569   if (!tag_type)
17570     cp_parser_error (parser, "expected class-key");
17571   return tag_type;
17572 }
17573
17574 /* Parse an (optional) member-specification.
17575
17576    member-specification:
17577      member-declaration member-specification [opt]
17578      access-specifier : member-specification [opt]  */
17579
17580 static void
17581 cp_parser_member_specification_opt (cp_parser* parser)
17582 {
17583   while (true)
17584     {
17585       cp_token *token;
17586       enum rid keyword;
17587
17588       /* Peek at the next token.  */
17589       token = cp_lexer_peek_token (parser->lexer);
17590       /* If it's a `}', or EOF then we've seen all the members.  */
17591       if (token->type == CPP_CLOSE_BRACE
17592           || token->type == CPP_EOF
17593           || token->type == CPP_PRAGMA_EOL)
17594         break;
17595
17596       /* See if this token is a keyword.  */
17597       keyword = token->keyword;
17598       switch (keyword)
17599         {
17600         case RID_PUBLIC:
17601         case RID_PROTECTED:
17602         case RID_PRIVATE:
17603           /* Consume the access-specifier.  */
17604           cp_lexer_consume_token (parser->lexer);
17605           /* Remember which access-specifier is active.  */
17606           current_access_specifier = token->u.value;
17607           /* Look for the `:'.  */
17608           cp_parser_require (parser, CPP_COLON, RT_COLON);
17609           break;
17610
17611         default:
17612           /* Accept #pragmas at class scope.  */
17613           if (token->type == CPP_PRAGMA)
17614             {
17615               cp_parser_pragma (parser, pragma_external);
17616               break;
17617             }
17618
17619           /* Otherwise, the next construction must be a
17620              member-declaration.  */
17621           cp_parser_member_declaration (parser);
17622         }
17623     }
17624 }
17625
17626 /* Parse a member-declaration.
17627
17628    member-declaration:
17629      decl-specifier-seq [opt] member-declarator-list [opt] ;
17630      function-definition ; [opt]
17631      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17632      using-declaration
17633      template-declaration
17634
17635    member-declarator-list:
17636      member-declarator
17637      member-declarator-list , member-declarator
17638
17639    member-declarator:
17640      declarator pure-specifier [opt]
17641      declarator constant-initializer [opt]
17642      identifier [opt] : constant-expression
17643
17644    GNU Extensions:
17645
17646    member-declaration:
17647      __extension__ member-declaration
17648
17649    member-declarator:
17650      declarator attributes [opt] pure-specifier [opt]
17651      declarator attributes [opt] constant-initializer [opt]
17652      identifier [opt] attributes [opt] : constant-expression  
17653
17654    C++0x Extensions:
17655
17656    member-declaration:
17657      static_assert-declaration  */
17658
17659 static void
17660 cp_parser_member_declaration (cp_parser* parser)
17661 {
17662   cp_decl_specifier_seq decl_specifiers;
17663   tree prefix_attributes;
17664   tree decl;
17665   int declares_class_or_enum;
17666   bool friend_p;
17667   cp_token *token = NULL;
17668   cp_token *decl_spec_token_start = NULL;
17669   cp_token *initializer_token_start = NULL;
17670   int saved_pedantic;
17671   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17672
17673   /* Check for the `__extension__' keyword.  */
17674   if (cp_parser_extension_opt (parser, &saved_pedantic))
17675     {
17676       /* Recurse.  */
17677       cp_parser_member_declaration (parser);
17678       /* Restore the old value of the PEDANTIC flag.  */
17679       pedantic = saved_pedantic;
17680
17681       return;
17682     }
17683
17684   /* Check for a template-declaration.  */
17685   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17686     {
17687       /* An explicit specialization here is an error condition, and we
17688          expect the specialization handler to detect and report this.  */
17689       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17690           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17691         cp_parser_explicit_specialization (parser);
17692       else
17693         cp_parser_template_declaration (parser, /*member_p=*/true);
17694
17695       return;
17696     }
17697
17698   /* Check for a using-declaration.  */
17699   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17700     {
17701       /* Parse the using-declaration.  */
17702       cp_parser_using_declaration (parser,
17703                                    /*access_declaration_p=*/false);
17704       return;
17705     }
17706
17707   /* Check for @defs.  */
17708   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17709     {
17710       tree ivar, member;
17711       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17712       ivar = ivar_chains;
17713       while (ivar)
17714         {
17715           member = ivar;
17716           ivar = TREE_CHAIN (member);
17717           TREE_CHAIN (member) = NULL_TREE;
17718           finish_member_declaration (member);
17719         }
17720       return;
17721     }
17722
17723   /* If the next token is `static_assert' we have a static assertion.  */
17724   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17725     {
17726       cp_parser_static_assert (parser, /*member_p=*/true);
17727       return;
17728     }
17729
17730   parser->colon_corrects_to_scope_p = false;
17731
17732   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17733     goto out;
17734
17735   /* Parse the decl-specifier-seq.  */
17736   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17737   cp_parser_decl_specifier_seq (parser,
17738                                 CP_PARSER_FLAGS_OPTIONAL,
17739                                 &decl_specifiers,
17740                                 &declares_class_or_enum);
17741   prefix_attributes = decl_specifiers.attributes;
17742   decl_specifiers.attributes = NULL_TREE;
17743   /* Check for an invalid type-name.  */
17744   if (!decl_specifiers.any_type_specifiers_p
17745       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17746     goto out;
17747   /* If there is no declarator, then the decl-specifier-seq should
17748      specify a type.  */
17749   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17750     {
17751       /* If there was no decl-specifier-seq, and the next token is a
17752          `;', then we have something like:
17753
17754            struct S { ; };
17755
17756          [class.mem]
17757
17758          Each member-declaration shall declare at least one member
17759          name of the class.  */
17760       if (!decl_specifiers.any_specifiers_p)
17761         {
17762           cp_token *token = cp_lexer_peek_token (parser->lexer);
17763           if (!in_system_header_at (token->location))
17764             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17765         }
17766       else
17767         {
17768           tree type;
17769
17770           /* See if this declaration is a friend.  */
17771           friend_p = cp_parser_friend_p (&decl_specifiers);
17772           /* If there were decl-specifiers, check to see if there was
17773              a class-declaration.  */
17774           type = check_tag_decl (&decl_specifiers);
17775           /* Nested classes have already been added to the class, but
17776              a `friend' needs to be explicitly registered.  */
17777           if (friend_p)
17778             {
17779               /* If the `friend' keyword was present, the friend must
17780                  be introduced with a class-key.  */
17781                if (!declares_class_or_enum)
17782                  error_at (decl_spec_token_start->location,
17783                            "a class-key must be used when declaring a friend");
17784                /* In this case:
17785
17786                     template <typename T> struct A {
17787                       friend struct A<T>::B;
17788                     };
17789
17790                   A<T>::B will be represented by a TYPENAME_TYPE, and
17791                   therefore not recognized by check_tag_decl.  */
17792                if (!type
17793                    && decl_specifiers.type
17794                    && TYPE_P (decl_specifiers.type))
17795                  type = decl_specifiers.type;
17796                if (!type || !TYPE_P (type))
17797                  error_at (decl_spec_token_start->location,
17798                            "friend declaration does not name a class or "
17799                            "function");
17800                else
17801                  make_friend_class (current_class_type, type,
17802                                     /*complain=*/true);
17803             }
17804           /* If there is no TYPE, an error message will already have
17805              been issued.  */
17806           else if (!type || type == error_mark_node)
17807             ;
17808           /* An anonymous aggregate has to be handled specially; such
17809              a declaration really declares a data member (with a
17810              particular type), as opposed to a nested class.  */
17811           else if (ANON_AGGR_TYPE_P (type))
17812             {
17813               /* Remove constructors and such from TYPE, now that we
17814                  know it is an anonymous aggregate.  */
17815               fixup_anonymous_aggr (type);
17816               /* And make the corresponding data member.  */
17817               decl = build_decl (decl_spec_token_start->location,
17818                                  FIELD_DECL, NULL_TREE, type);
17819               /* Add it to the class.  */
17820               finish_member_declaration (decl);
17821             }
17822           else
17823             cp_parser_check_access_in_redeclaration
17824                                               (TYPE_NAME (type),
17825                                                decl_spec_token_start->location);
17826         }
17827     }
17828   else
17829     {
17830       bool assume_semicolon = false;
17831
17832       /* See if these declarations will be friends.  */
17833       friend_p = cp_parser_friend_p (&decl_specifiers);
17834
17835       /* Keep going until we hit the `;' at the end of the
17836          declaration.  */
17837       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17838         {
17839           tree attributes = NULL_TREE;
17840           tree first_attribute;
17841
17842           /* Peek at the next token.  */
17843           token = cp_lexer_peek_token (parser->lexer);
17844
17845           /* Check for a bitfield declaration.  */
17846           if (token->type == CPP_COLON
17847               || (token->type == CPP_NAME
17848                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17849                   == CPP_COLON))
17850             {
17851               tree identifier;
17852               tree width;
17853
17854               /* Get the name of the bitfield.  Note that we cannot just
17855                  check TOKEN here because it may have been invalidated by
17856                  the call to cp_lexer_peek_nth_token above.  */
17857               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17858                 identifier = cp_parser_identifier (parser);
17859               else
17860                 identifier = NULL_TREE;
17861
17862               /* Consume the `:' token.  */
17863               cp_lexer_consume_token (parser->lexer);
17864               /* Get the width of the bitfield.  */
17865               width
17866                 = cp_parser_constant_expression (parser,
17867                                                  /*allow_non_constant=*/false,
17868                                                  NULL);
17869
17870               /* Look for attributes that apply to the bitfield.  */
17871               attributes = cp_parser_attributes_opt (parser);
17872               /* Remember which attributes are prefix attributes and
17873                  which are not.  */
17874               first_attribute = attributes;
17875               /* Combine the attributes.  */
17876               attributes = chainon (prefix_attributes, attributes);
17877
17878               /* Create the bitfield declaration.  */
17879               decl = grokbitfield (identifier
17880                                    ? make_id_declarator (NULL_TREE,
17881                                                          identifier,
17882                                                          sfk_none)
17883                                    : NULL,
17884                                    &decl_specifiers,
17885                                    width,
17886                                    attributes);
17887             }
17888           else
17889             {
17890               cp_declarator *declarator;
17891               tree initializer;
17892               tree asm_specification;
17893               int ctor_dtor_or_conv_p;
17894
17895               /* Parse the declarator.  */
17896               declarator
17897                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17898                                         &ctor_dtor_or_conv_p,
17899                                         /*parenthesized_p=*/NULL,
17900                                         /*member_p=*/true);
17901
17902               /* If something went wrong parsing the declarator, make sure
17903                  that we at least consume some tokens.  */
17904               if (declarator == cp_error_declarator)
17905                 {
17906                   /* Skip to the end of the statement.  */
17907                   cp_parser_skip_to_end_of_statement (parser);
17908                   /* If the next token is not a semicolon, that is
17909                      probably because we just skipped over the body of
17910                      a function.  So, we consume a semicolon if
17911                      present, but do not issue an error message if it
17912                      is not present.  */
17913                   if (cp_lexer_next_token_is (parser->lexer,
17914                                               CPP_SEMICOLON))
17915                     cp_lexer_consume_token (parser->lexer);
17916                   goto out;
17917                 }
17918
17919               if (declares_class_or_enum & 2)
17920                 cp_parser_check_for_definition_in_return_type
17921                                             (declarator, decl_specifiers.type,
17922                                              decl_specifiers.type_location);
17923
17924               /* Look for an asm-specification.  */
17925               asm_specification = cp_parser_asm_specification_opt (parser);
17926               /* Look for attributes that apply to the declaration.  */
17927               attributes = cp_parser_attributes_opt (parser);
17928               /* Remember which attributes are prefix attributes and
17929                  which are not.  */
17930               first_attribute = attributes;
17931               /* Combine the attributes.  */
17932               attributes = chainon (prefix_attributes, attributes);
17933
17934               /* If it's an `=', then we have a constant-initializer or a
17935                  pure-specifier.  It is not correct to parse the
17936                  initializer before registering the member declaration
17937                  since the member declaration should be in scope while
17938                  its initializer is processed.  However, the rest of the
17939                  front end does not yet provide an interface that allows
17940                  us to handle this correctly.  */
17941               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17942                 {
17943                   /* In [class.mem]:
17944
17945                      A pure-specifier shall be used only in the declaration of
17946                      a virtual function.
17947
17948                      A member-declarator can contain a constant-initializer
17949                      only if it declares a static member of integral or
17950                      enumeration type.
17951
17952                      Therefore, if the DECLARATOR is for a function, we look
17953                      for a pure-specifier; otherwise, we look for a
17954                      constant-initializer.  When we call `grokfield', it will
17955                      perform more stringent semantics checks.  */
17956                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17957                   if (function_declarator_p (declarator))
17958                     initializer = cp_parser_pure_specifier (parser);
17959                   else
17960                     /* Parse the initializer.  */
17961                     initializer = cp_parser_constant_initializer (parser);
17962                 }
17963               /* Otherwise, there is no initializer.  */
17964               else
17965                 initializer = NULL_TREE;
17966
17967               /* See if we are probably looking at a function
17968                  definition.  We are certainly not looking at a
17969                  member-declarator.  Calling `grokfield' has
17970                  side-effects, so we must not do it unless we are sure
17971                  that we are looking at a member-declarator.  */
17972               if (cp_parser_token_starts_function_definition_p
17973                   (cp_lexer_peek_token (parser->lexer)))
17974                 {
17975                   /* The grammar does not allow a pure-specifier to be
17976                      used when a member function is defined.  (It is
17977                      possible that this fact is an oversight in the
17978                      standard, since a pure function may be defined
17979                      outside of the class-specifier.  */
17980                   if (initializer)
17981                     error_at (initializer_token_start->location,
17982                               "pure-specifier on function-definition");
17983                   decl = cp_parser_save_member_function_body (parser,
17984                                                               &decl_specifiers,
17985                                                               declarator,
17986                                                               attributes);
17987                   /* If the member was not a friend, declare it here.  */
17988                   if (!friend_p)
17989                     finish_member_declaration (decl);
17990                   /* Peek at the next token.  */
17991                   token = cp_lexer_peek_token (parser->lexer);
17992                   /* If the next token is a semicolon, consume it.  */
17993                   if (token->type == CPP_SEMICOLON)
17994                     cp_lexer_consume_token (parser->lexer);
17995                   goto out;
17996                 }
17997               else
17998                 if (declarator->kind == cdk_function)
17999                   declarator->id_loc = token->location;
18000                 /* Create the declaration.  */
18001                 decl = grokfield (declarator, &decl_specifiers,
18002                                   initializer, /*init_const_expr_p=*/true,
18003                                   asm_specification,
18004                                   attributes);
18005             }
18006
18007           /* Reset PREFIX_ATTRIBUTES.  */
18008           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18009             attributes = TREE_CHAIN (attributes);
18010           if (attributes)
18011             TREE_CHAIN (attributes) = NULL_TREE;
18012
18013           /* If there is any qualification still in effect, clear it
18014              now; we will be starting fresh with the next declarator.  */
18015           parser->scope = NULL_TREE;
18016           parser->qualifying_scope = NULL_TREE;
18017           parser->object_scope = NULL_TREE;
18018           /* If it's a `,', then there are more declarators.  */
18019           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18020             cp_lexer_consume_token (parser->lexer);
18021           /* If the next token isn't a `;', then we have a parse error.  */
18022           else if (cp_lexer_next_token_is_not (parser->lexer,
18023                                                CPP_SEMICOLON))
18024             {
18025               /* The next token might be a ways away from where the
18026                  actual semicolon is missing.  Find the previous token
18027                  and use that for our error position.  */
18028               cp_token *token = cp_lexer_previous_token (parser->lexer);
18029               error_at (token->location,
18030                         "expected %<;%> at end of member declaration");
18031
18032               /* Assume that the user meant to provide a semicolon.  If
18033                  we were to cp_parser_skip_to_end_of_statement, we might
18034                  skip to a semicolon inside a member function definition
18035                  and issue nonsensical error messages.  */
18036               assume_semicolon = true;
18037             }
18038
18039           if (decl)
18040             {
18041               /* Add DECL to the list of members.  */
18042               if (!friend_p)
18043                 finish_member_declaration (decl);
18044
18045               if (TREE_CODE (decl) == FUNCTION_DECL)
18046                 cp_parser_save_default_args (parser, decl);
18047             }
18048
18049           if (assume_semicolon)
18050             goto out;
18051         }
18052     }
18053
18054   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18055  out:
18056   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18057 }
18058
18059 /* Parse a pure-specifier.
18060
18061    pure-specifier:
18062      = 0
18063
18064    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18065    Otherwise, ERROR_MARK_NODE is returned.  */
18066
18067 static tree
18068 cp_parser_pure_specifier (cp_parser* parser)
18069 {
18070   cp_token *token;
18071
18072   /* Look for the `=' token.  */
18073   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18074     return error_mark_node;
18075   /* Look for the `0' token.  */
18076   token = cp_lexer_peek_token (parser->lexer);
18077
18078   if (token->type == CPP_EOF
18079       || token->type == CPP_PRAGMA_EOL)
18080     return error_mark_node;
18081
18082   cp_lexer_consume_token (parser->lexer);
18083
18084   /* Accept = default or = delete in c++0x mode.  */
18085   if (token->keyword == RID_DEFAULT
18086       || token->keyword == RID_DELETE)
18087     {
18088       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18089       return token->u.value;
18090     }
18091
18092   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18093   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18094     {
18095       cp_parser_error (parser,
18096                        "invalid pure specifier (only %<= 0%> is allowed)");
18097       cp_parser_skip_to_end_of_statement (parser);
18098       return error_mark_node;
18099     }
18100   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18101     {
18102       error_at (token->location, "templates may not be %<virtual%>");
18103       return error_mark_node;
18104     }
18105
18106   return integer_zero_node;
18107 }
18108
18109 /* Parse a constant-initializer.
18110
18111    constant-initializer:
18112      = constant-expression
18113
18114    Returns a representation of the constant-expression.  */
18115
18116 static tree
18117 cp_parser_constant_initializer (cp_parser* parser)
18118 {
18119   /* Look for the `=' token.  */
18120   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18121     return error_mark_node;
18122
18123   /* It is invalid to write:
18124
18125        struct S { static const int i = { 7 }; };
18126
18127      */
18128   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18129     {
18130       cp_parser_error (parser,
18131                        "a brace-enclosed initializer is not allowed here");
18132       /* Consume the opening brace.  */
18133       cp_lexer_consume_token (parser->lexer);
18134       /* Skip the initializer.  */
18135       cp_parser_skip_to_closing_brace (parser);
18136       /* Look for the trailing `}'.  */
18137       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18138
18139       return error_mark_node;
18140     }
18141
18142   return cp_parser_constant_expression (parser,
18143                                         /*allow_non_constant=*/false,
18144                                         NULL);
18145 }
18146
18147 /* Derived classes [gram.class.derived] */
18148
18149 /* Parse a base-clause.
18150
18151    base-clause:
18152      : base-specifier-list
18153
18154    base-specifier-list:
18155      base-specifier ... [opt]
18156      base-specifier-list , base-specifier ... [opt]
18157
18158    Returns a TREE_LIST representing the base-classes, in the order in
18159    which they were declared.  The representation of each node is as
18160    described by cp_parser_base_specifier.
18161
18162    In the case that no bases are specified, this function will return
18163    NULL_TREE, not ERROR_MARK_NODE.  */
18164
18165 static tree
18166 cp_parser_base_clause (cp_parser* parser)
18167 {
18168   tree bases = NULL_TREE;
18169
18170   /* Look for the `:' that begins the list.  */
18171   cp_parser_require (parser, CPP_COLON, RT_COLON);
18172
18173   /* Scan the base-specifier-list.  */
18174   while (true)
18175     {
18176       cp_token *token;
18177       tree base;
18178       bool pack_expansion_p = false;
18179
18180       /* Look for the base-specifier.  */
18181       base = cp_parser_base_specifier (parser);
18182       /* Look for the (optional) ellipsis. */
18183       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18184         {
18185           /* Consume the `...'. */
18186           cp_lexer_consume_token (parser->lexer);
18187
18188           pack_expansion_p = true;
18189         }
18190
18191       /* Add BASE to the front of the list.  */
18192       if (base != error_mark_node)
18193         {
18194           if (pack_expansion_p)
18195             /* Make this a pack expansion type. */
18196             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18197           
18198
18199           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18200             {
18201               TREE_CHAIN (base) = bases;
18202               bases = base;
18203             }
18204         }
18205       /* Peek at the next token.  */
18206       token = cp_lexer_peek_token (parser->lexer);
18207       /* If it's not a comma, then the list is complete.  */
18208       if (token->type != CPP_COMMA)
18209         break;
18210       /* Consume the `,'.  */
18211       cp_lexer_consume_token (parser->lexer);
18212     }
18213
18214   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18215      base class had a qualified name.  However, the next name that
18216      appears is certainly not qualified.  */
18217   parser->scope = NULL_TREE;
18218   parser->qualifying_scope = NULL_TREE;
18219   parser->object_scope = NULL_TREE;
18220
18221   return nreverse (bases);
18222 }
18223
18224 /* Parse a base-specifier.
18225
18226    base-specifier:
18227      :: [opt] nested-name-specifier [opt] class-name
18228      virtual access-specifier [opt] :: [opt] nested-name-specifier
18229        [opt] class-name
18230      access-specifier virtual [opt] :: [opt] nested-name-specifier
18231        [opt] class-name
18232
18233    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18234    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18235    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18236    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18237
18238 static tree
18239 cp_parser_base_specifier (cp_parser* parser)
18240 {
18241   cp_token *token;
18242   bool done = false;
18243   bool virtual_p = false;
18244   bool duplicate_virtual_error_issued_p = false;
18245   bool duplicate_access_error_issued_p = false;
18246   bool class_scope_p, template_p;
18247   tree access = access_default_node;
18248   tree type;
18249
18250   /* Process the optional `virtual' and `access-specifier'.  */
18251   while (!done)
18252     {
18253       /* Peek at the next token.  */
18254       token = cp_lexer_peek_token (parser->lexer);
18255       /* Process `virtual'.  */
18256       switch (token->keyword)
18257         {
18258         case RID_VIRTUAL:
18259           /* If `virtual' appears more than once, issue an error.  */
18260           if (virtual_p && !duplicate_virtual_error_issued_p)
18261             {
18262               cp_parser_error (parser,
18263                                "%<virtual%> specified more than once in base-specified");
18264               duplicate_virtual_error_issued_p = true;
18265             }
18266
18267           virtual_p = true;
18268
18269           /* Consume the `virtual' token.  */
18270           cp_lexer_consume_token (parser->lexer);
18271
18272           break;
18273
18274         case RID_PUBLIC:
18275         case RID_PROTECTED:
18276         case RID_PRIVATE:
18277           /* If more than one access specifier appears, issue an
18278              error.  */
18279           if (access != access_default_node
18280               && !duplicate_access_error_issued_p)
18281             {
18282               cp_parser_error (parser,
18283                                "more than one access specifier in base-specified");
18284               duplicate_access_error_issued_p = true;
18285             }
18286
18287           access = ridpointers[(int) token->keyword];
18288
18289           /* Consume the access-specifier.  */
18290           cp_lexer_consume_token (parser->lexer);
18291
18292           break;
18293
18294         default:
18295           done = true;
18296           break;
18297         }
18298     }
18299   /* It is not uncommon to see programs mechanically, erroneously, use
18300      the 'typename' keyword to denote (dependent) qualified types
18301      as base classes.  */
18302   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18303     {
18304       token = cp_lexer_peek_token (parser->lexer);
18305       if (!processing_template_decl)
18306         error_at (token->location,
18307                   "keyword %<typename%> not allowed outside of templates");
18308       else
18309         error_at (token->location,
18310                   "keyword %<typename%> not allowed in this context "
18311                   "(the base class is implicitly a type)");
18312       cp_lexer_consume_token (parser->lexer);
18313     }
18314
18315   /* Look for the optional `::' operator.  */
18316   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18317   /* Look for the nested-name-specifier.  The simplest way to
18318      implement:
18319
18320        [temp.res]
18321
18322        The keyword `typename' is not permitted in a base-specifier or
18323        mem-initializer; in these contexts a qualified name that
18324        depends on a template-parameter is implicitly assumed to be a
18325        type name.
18326
18327      is to pretend that we have seen the `typename' keyword at this
18328      point.  */
18329   cp_parser_nested_name_specifier_opt (parser,
18330                                        /*typename_keyword_p=*/true,
18331                                        /*check_dependency_p=*/true,
18332                                        typename_type,
18333                                        /*is_declaration=*/true);
18334   /* If the base class is given by a qualified name, assume that names
18335      we see are type names or templates, as appropriate.  */
18336   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18337   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18338
18339   /* Finally, look for the class-name.  */
18340   type = cp_parser_class_name (parser,
18341                                class_scope_p,
18342                                template_p,
18343                                typename_type,
18344                                /*check_dependency_p=*/true,
18345                                /*class_head_p=*/false,
18346                                /*is_declaration=*/true);
18347
18348   if (type == error_mark_node)
18349     return error_mark_node;
18350
18351   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18352 }
18353
18354 /* Exception handling [gram.exception] */
18355
18356 /* Parse an (optional) exception-specification.
18357
18358    exception-specification:
18359      throw ( type-id-list [opt] )
18360
18361    Returns a TREE_LIST representing the exception-specification.  The
18362    TREE_VALUE of each node is a type.  */
18363
18364 static tree
18365 cp_parser_exception_specification_opt (cp_parser* parser)
18366 {
18367   cp_token *token;
18368   tree type_id_list;
18369   const char *saved_message;
18370
18371   /* Peek at the next token.  */
18372   token = cp_lexer_peek_token (parser->lexer);
18373
18374   /* Is it a noexcept-specification?  */
18375   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18376     {
18377       tree expr;
18378       cp_lexer_consume_token (parser->lexer);
18379
18380       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18381         {
18382           cp_lexer_consume_token (parser->lexer);
18383
18384           /* Types may not be defined in an exception-specification.  */
18385           saved_message = parser->type_definition_forbidden_message;
18386           parser->type_definition_forbidden_message
18387             = G_("types may not be defined in an exception-specification");
18388
18389           expr = cp_parser_constant_expression (parser, false, NULL);
18390
18391           /* Restore the saved message.  */
18392           parser->type_definition_forbidden_message = saved_message;
18393
18394           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18395         }
18396       else
18397         expr = boolean_true_node;
18398
18399       return build_noexcept_spec (expr, tf_warning_or_error);
18400     }
18401
18402   /* If it's not `throw', then there's no exception-specification.  */
18403   if (!cp_parser_is_keyword (token, RID_THROW))
18404     return NULL_TREE;
18405
18406 #if 0
18407   /* Enable this once a lot of code has transitioned to noexcept?  */
18408   if (cxx_dialect == cxx0x && !in_system_header)
18409     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18410              "deprecated in C++0x; use %<noexcept%> instead");
18411 #endif
18412
18413   /* Consume the `throw'.  */
18414   cp_lexer_consume_token (parser->lexer);
18415
18416   /* Look for the `('.  */
18417   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18418
18419   /* Peek at the next token.  */
18420   token = cp_lexer_peek_token (parser->lexer);
18421   /* If it's not a `)', then there is a type-id-list.  */
18422   if (token->type != CPP_CLOSE_PAREN)
18423     {
18424       /* Types may not be defined in an exception-specification.  */
18425       saved_message = parser->type_definition_forbidden_message;
18426       parser->type_definition_forbidden_message
18427         = G_("types may not be defined in an exception-specification");
18428       /* Parse the type-id-list.  */
18429       type_id_list = cp_parser_type_id_list (parser);
18430       /* Restore the saved message.  */
18431       parser->type_definition_forbidden_message = saved_message;
18432     }
18433   else
18434     type_id_list = empty_except_spec;
18435
18436   /* Look for the `)'.  */
18437   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18438
18439   return type_id_list;
18440 }
18441
18442 /* Parse an (optional) type-id-list.
18443
18444    type-id-list:
18445      type-id ... [opt]
18446      type-id-list , type-id ... [opt]
18447
18448    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18449    in the order that the types were presented.  */
18450
18451 static tree
18452 cp_parser_type_id_list (cp_parser* parser)
18453 {
18454   tree types = NULL_TREE;
18455
18456   while (true)
18457     {
18458       cp_token *token;
18459       tree type;
18460
18461       /* Get the next type-id.  */
18462       type = cp_parser_type_id (parser);
18463       /* Parse the optional ellipsis. */
18464       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18465         {
18466           /* Consume the `...'. */
18467           cp_lexer_consume_token (parser->lexer);
18468
18469           /* Turn the type into a pack expansion expression. */
18470           type = make_pack_expansion (type);
18471         }
18472       /* Add it to the list.  */
18473       types = add_exception_specifier (types, type, /*complain=*/1);
18474       /* Peek at the next token.  */
18475       token = cp_lexer_peek_token (parser->lexer);
18476       /* If it is not a `,', we are done.  */
18477       if (token->type != CPP_COMMA)
18478         break;
18479       /* Consume the `,'.  */
18480       cp_lexer_consume_token (parser->lexer);
18481     }
18482
18483   return nreverse (types);
18484 }
18485
18486 /* Parse a try-block.
18487
18488    try-block:
18489      try compound-statement handler-seq  */
18490
18491 static tree
18492 cp_parser_try_block (cp_parser* parser)
18493 {
18494   tree try_block;
18495
18496   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18497   try_block = begin_try_block ();
18498   cp_parser_compound_statement (parser, NULL, true);
18499   finish_try_block (try_block);
18500   cp_parser_handler_seq (parser);
18501   finish_handler_sequence (try_block);
18502
18503   return try_block;
18504 }
18505
18506 /* Parse a function-try-block.
18507
18508    function-try-block:
18509      try ctor-initializer [opt] function-body handler-seq  */
18510
18511 static bool
18512 cp_parser_function_try_block (cp_parser* parser)
18513 {
18514   tree compound_stmt;
18515   tree try_block;
18516   bool ctor_initializer_p;
18517
18518   /* Look for the `try' keyword.  */
18519   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18520     return false;
18521   /* Let the rest of the front end know where we are.  */
18522   try_block = begin_function_try_block (&compound_stmt);
18523   /* Parse the function-body.  */
18524   ctor_initializer_p
18525     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18526   /* We're done with the `try' part.  */
18527   finish_function_try_block (try_block);
18528   /* Parse the handlers.  */
18529   cp_parser_handler_seq (parser);
18530   /* We're done with the handlers.  */
18531   finish_function_handler_sequence (try_block, compound_stmt);
18532
18533   return ctor_initializer_p;
18534 }
18535
18536 /* Parse a handler-seq.
18537
18538    handler-seq:
18539      handler handler-seq [opt]  */
18540
18541 static void
18542 cp_parser_handler_seq (cp_parser* parser)
18543 {
18544   while (true)
18545     {
18546       cp_token *token;
18547
18548       /* Parse the handler.  */
18549       cp_parser_handler (parser);
18550       /* Peek at the next token.  */
18551       token = cp_lexer_peek_token (parser->lexer);
18552       /* If it's not `catch' then there are no more handlers.  */
18553       if (!cp_parser_is_keyword (token, RID_CATCH))
18554         break;
18555     }
18556 }
18557
18558 /* Parse a handler.
18559
18560    handler:
18561      catch ( exception-declaration ) compound-statement  */
18562
18563 static void
18564 cp_parser_handler (cp_parser* parser)
18565 {
18566   tree handler;
18567   tree declaration;
18568
18569   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18570   handler = begin_handler ();
18571   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18572   declaration = cp_parser_exception_declaration (parser);
18573   finish_handler_parms (declaration, handler);
18574   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18575   cp_parser_compound_statement (parser, NULL, false);
18576   finish_handler (handler);
18577 }
18578
18579 /* Parse an exception-declaration.
18580
18581    exception-declaration:
18582      type-specifier-seq declarator
18583      type-specifier-seq abstract-declarator
18584      type-specifier-seq
18585      ...
18586
18587    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18588    ellipsis variant is used.  */
18589
18590 static tree
18591 cp_parser_exception_declaration (cp_parser* parser)
18592 {
18593   cp_decl_specifier_seq type_specifiers;
18594   cp_declarator *declarator;
18595   const char *saved_message;
18596
18597   /* If it's an ellipsis, it's easy to handle.  */
18598   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18599     {
18600       /* Consume the `...' token.  */
18601       cp_lexer_consume_token (parser->lexer);
18602       return NULL_TREE;
18603     }
18604
18605   /* Types may not be defined in exception-declarations.  */
18606   saved_message = parser->type_definition_forbidden_message;
18607   parser->type_definition_forbidden_message
18608     = G_("types may not be defined in exception-declarations");
18609
18610   /* Parse the type-specifier-seq.  */
18611   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18612                                 /*is_trailing_return=*/false,
18613                                 &type_specifiers);
18614   /* If it's a `)', then there is no declarator.  */
18615   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18616     declarator = NULL;
18617   else
18618     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18619                                        /*ctor_dtor_or_conv_p=*/NULL,
18620                                        /*parenthesized_p=*/NULL,
18621                                        /*member_p=*/false);
18622
18623   /* Restore the saved message.  */
18624   parser->type_definition_forbidden_message = saved_message;
18625
18626   if (!type_specifiers.any_specifiers_p)
18627     return error_mark_node;
18628
18629   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18630 }
18631
18632 /* Parse a throw-expression.
18633
18634    throw-expression:
18635      throw assignment-expression [opt]
18636
18637    Returns a THROW_EXPR representing the throw-expression.  */
18638
18639 static tree
18640 cp_parser_throw_expression (cp_parser* parser)
18641 {
18642   tree expression;
18643   cp_token* token;
18644
18645   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18646   token = cp_lexer_peek_token (parser->lexer);
18647   /* Figure out whether or not there is an assignment-expression
18648      following the "throw" keyword.  */
18649   if (token->type == CPP_COMMA
18650       || token->type == CPP_SEMICOLON
18651       || token->type == CPP_CLOSE_PAREN
18652       || token->type == CPP_CLOSE_SQUARE
18653       || token->type == CPP_CLOSE_BRACE
18654       || token->type == CPP_COLON)
18655     expression = NULL_TREE;
18656   else
18657     expression = cp_parser_assignment_expression (parser,
18658                                                   /*cast_p=*/false, NULL);
18659
18660   return build_throw (expression);
18661 }
18662
18663 /* GNU Extensions */
18664
18665 /* Parse an (optional) asm-specification.
18666
18667    asm-specification:
18668      asm ( string-literal )
18669
18670    If the asm-specification is present, returns a STRING_CST
18671    corresponding to the string-literal.  Otherwise, returns
18672    NULL_TREE.  */
18673
18674 static tree
18675 cp_parser_asm_specification_opt (cp_parser* parser)
18676 {
18677   cp_token *token;
18678   tree asm_specification;
18679
18680   /* Peek at the next token.  */
18681   token = cp_lexer_peek_token (parser->lexer);
18682   /* If the next token isn't the `asm' keyword, then there's no
18683      asm-specification.  */
18684   if (!cp_parser_is_keyword (token, RID_ASM))
18685     return NULL_TREE;
18686
18687   /* Consume the `asm' token.  */
18688   cp_lexer_consume_token (parser->lexer);
18689   /* Look for the `('.  */
18690   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18691
18692   /* Look for the string-literal.  */
18693   asm_specification = cp_parser_string_literal (parser, false, false);
18694
18695   /* Look for the `)'.  */
18696   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18697
18698   return asm_specification;
18699 }
18700
18701 /* Parse an asm-operand-list.
18702
18703    asm-operand-list:
18704      asm-operand
18705      asm-operand-list , asm-operand
18706
18707    asm-operand:
18708      string-literal ( expression )
18709      [ string-literal ] string-literal ( expression )
18710
18711    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18712    each node is the expression.  The TREE_PURPOSE is itself a
18713    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18714    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18715    is a STRING_CST for the string literal before the parenthesis. Returns
18716    ERROR_MARK_NODE if any of the operands are invalid.  */
18717
18718 static tree
18719 cp_parser_asm_operand_list (cp_parser* parser)
18720 {
18721   tree asm_operands = NULL_TREE;
18722   bool invalid_operands = false;
18723
18724   while (true)
18725     {
18726       tree string_literal;
18727       tree expression;
18728       tree name;
18729
18730       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18731         {
18732           /* Consume the `[' token.  */
18733           cp_lexer_consume_token (parser->lexer);
18734           /* Read the operand name.  */
18735           name = cp_parser_identifier (parser);
18736           if (name != error_mark_node)
18737             name = build_string (IDENTIFIER_LENGTH (name),
18738                                  IDENTIFIER_POINTER (name));
18739           /* Look for the closing `]'.  */
18740           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18741         }
18742       else
18743         name = NULL_TREE;
18744       /* Look for the string-literal.  */
18745       string_literal = cp_parser_string_literal (parser, false, false);
18746
18747       /* Look for the `('.  */
18748       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18749       /* Parse the expression.  */
18750       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18751       /* Look for the `)'.  */
18752       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18753
18754       if (name == error_mark_node 
18755           || string_literal == error_mark_node 
18756           || expression == error_mark_node)
18757         invalid_operands = true;
18758
18759       /* Add this operand to the list.  */
18760       asm_operands = tree_cons (build_tree_list (name, string_literal),
18761                                 expression,
18762                                 asm_operands);
18763       /* If the next token is not a `,', there are no more
18764          operands.  */
18765       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18766         break;
18767       /* Consume the `,'.  */
18768       cp_lexer_consume_token (parser->lexer);
18769     }
18770
18771   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18772 }
18773
18774 /* Parse an asm-clobber-list.
18775
18776    asm-clobber-list:
18777      string-literal
18778      asm-clobber-list , string-literal
18779
18780    Returns a TREE_LIST, indicating the clobbers in the order that they
18781    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18782
18783 static tree
18784 cp_parser_asm_clobber_list (cp_parser* parser)
18785 {
18786   tree clobbers = NULL_TREE;
18787
18788   while (true)
18789     {
18790       tree string_literal;
18791
18792       /* Look for the string literal.  */
18793       string_literal = cp_parser_string_literal (parser, false, false);
18794       /* Add it to the list.  */
18795       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18796       /* If the next token is not a `,', then the list is
18797          complete.  */
18798       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18799         break;
18800       /* Consume the `,' token.  */
18801       cp_lexer_consume_token (parser->lexer);
18802     }
18803
18804   return clobbers;
18805 }
18806
18807 /* Parse an asm-label-list.
18808
18809    asm-label-list:
18810      identifier
18811      asm-label-list , identifier
18812
18813    Returns a TREE_LIST, indicating the labels in the order that they
18814    appeared.  The TREE_VALUE of each node is a label.  */
18815
18816 static tree
18817 cp_parser_asm_label_list (cp_parser* parser)
18818 {
18819   tree labels = NULL_TREE;
18820
18821   while (true)
18822     {
18823       tree identifier, label, name;
18824
18825       /* Look for the identifier.  */
18826       identifier = cp_parser_identifier (parser);
18827       if (!error_operand_p (identifier))
18828         {
18829           label = lookup_label (identifier);
18830           if (TREE_CODE (label) == LABEL_DECL)
18831             {
18832               TREE_USED (label) = 1;
18833               check_goto (label);
18834               name = build_string (IDENTIFIER_LENGTH (identifier),
18835                                    IDENTIFIER_POINTER (identifier));
18836               labels = tree_cons (name, label, labels);
18837             }
18838         }
18839       /* If the next token is not a `,', then the list is
18840          complete.  */
18841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18842         break;
18843       /* Consume the `,' token.  */
18844       cp_lexer_consume_token (parser->lexer);
18845     }
18846
18847   return nreverse (labels);
18848 }
18849
18850 /* Parse an (optional) series of attributes.
18851
18852    attributes:
18853      attributes attribute
18854
18855    attribute:
18856      __attribute__ (( attribute-list [opt] ))
18857
18858    The return value is as for cp_parser_attribute_list.  */
18859
18860 static tree
18861 cp_parser_attributes_opt (cp_parser* parser)
18862 {
18863   tree attributes = NULL_TREE;
18864
18865   while (true)
18866     {
18867       cp_token *token;
18868       tree attribute_list;
18869
18870       /* Peek at the next token.  */
18871       token = cp_lexer_peek_token (parser->lexer);
18872       /* If it's not `__attribute__', then we're done.  */
18873       if (token->keyword != RID_ATTRIBUTE)
18874         break;
18875
18876       /* Consume the `__attribute__' keyword.  */
18877       cp_lexer_consume_token (parser->lexer);
18878       /* Look for the two `(' tokens.  */
18879       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18880       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18881
18882       /* Peek at the next token.  */
18883       token = cp_lexer_peek_token (parser->lexer);
18884       if (token->type != CPP_CLOSE_PAREN)
18885         /* Parse the attribute-list.  */
18886         attribute_list = cp_parser_attribute_list (parser);
18887       else
18888         /* If the next token is a `)', then there is no attribute
18889            list.  */
18890         attribute_list = NULL;
18891
18892       /* Look for the two `)' tokens.  */
18893       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18894       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18895
18896       /* Add these new attributes to the list.  */
18897       attributes = chainon (attributes, attribute_list);
18898     }
18899
18900   return attributes;
18901 }
18902
18903 /* Parse an attribute-list.
18904
18905    attribute-list:
18906      attribute
18907      attribute-list , attribute
18908
18909    attribute:
18910      identifier
18911      identifier ( identifier )
18912      identifier ( identifier , expression-list )
18913      identifier ( expression-list )
18914
18915    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18916    to an attribute.  The TREE_PURPOSE of each node is the identifier
18917    indicating which attribute is in use.  The TREE_VALUE represents
18918    the arguments, if any.  */
18919
18920 static tree
18921 cp_parser_attribute_list (cp_parser* parser)
18922 {
18923   tree attribute_list = NULL_TREE;
18924   bool save_translate_strings_p = parser->translate_strings_p;
18925
18926   parser->translate_strings_p = false;
18927   while (true)
18928     {
18929       cp_token *token;
18930       tree identifier;
18931       tree attribute;
18932
18933       /* Look for the identifier.  We also allow keywords here; for
18934          example `__attribute__ ((const))' is legal.  */
18935       token = cp_lexer_peek_token (parser->lexer);
18936       if (token->type == CPP_NAME
18937           || token->type == CPP_KEYWORD)
18938         {
18939           tree arguments = NULL_TREE;
18940
18941           /* Consume the token.  */
18942           token = cp_lexer_consume_token (parser->lexer);
18943
18944           /* Save away the identifier that indicates which attribute
18945              this is.  */
18946           identifier = (token->type == CPP_KEYWORD) 
18947             /* For keywords, use the canonical spelling, not the
18948                parsed identifier.  */
18949             ? ridpointers[(int) token->keyword]
18950             : token->u.value;
18951           
18952           attribute = build_tree_list (identifier, NULL_TREE);
18953
18954           /* Peek at the next token.  */
18955           token = cp_lexer_peek_token (parser->lexer);
18956           /* If it's an `(', then parse the attribute arguments.  */
18957           if (token->type == CPP_OPEN_PAREN)
18958             {
18959               VEC(tree,gc) *vec;
18960               int attr_flag = (attribute_takes_identifier_p (identifier)
18961                                ? id_attr : normal_attr);
18962               vec = cp_parser_parenthesized_expression_list
18963                     (parser, attr_flag, /*cast_p=*/false,
18964                      /*allow_expansion_p=*/false,
18965                      /*non_constant_p=*/NULL);
18966               if (vec == NULL)
18967                 arguments = error_mark_node;
18968               else
18969                 {
18970                   arguments = build_tree_list_vec (vec);
18971                   release_tree_vector (vec);
18972                 }
18973               /* Save the arguments away.  */
18974               TREE_VALUE (attribute) = arguments;
18975             }
18976
18977           if (arguments != error_mark_node)
18978             {
18979               /* Add this attribute to the list.  */
18980               TREE_CHAIN (attribute) = attribute_list;
18981               attribute_list = attribute;
18982             }
18983
18984           token = cp_lexer_peek_token (parser->lexer);
18985         }
18986       /* Now, look for more attributes.  If the next token isn't a
18987          `,', we're done.  */
18988       if (token->type != CPP_COMMA)
18989         break;
18990
18991       /* Consume the comma and keep going.  */
18992       cp_lexer_consume_token (parser->lexer);
18993     }
18994   parser->translate_strings_p = save_translate_strings_p;
18995
18996   /* We built up the list in reverse order.  */
18997   return nreverse (attribute_list);
18998 }
18999
19000 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19001    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19002    current value of the PEDANTIC flag, regardless of whether or not
19003    the `__extension__' keyword is present.  The caller is responsible
19004    for restoring the value of the PEDANTIC flag.  */
19005
19006 static bool
19007 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19008 {
19009   /* Save the old value of the PEDANTIC flag.  */
19010   *saved_pedantic = pedantic;
19011
19012   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19013     {
19014       /* Consume the `__extension__' token.  */
19015       cp_lexer_consume_token (parser->lexer);
19016       /* We're not being pedantic while the `__extension__' keyword is
19017          in effect.  */
19018       pedantic = 0;
19019
19020       return true;
19021     }
19022
19023   return false;
19024 }
19025
19026 /* Parse a label declaration.
19027
19028    label-declaration:
19029      __label__ label-declarator-seq ;
19030
19031    label-declarator-seq:
19032      identifier , label-declarator-seq
19033      identifier  */
19034
19035 static void
19036 cp_parser_label_declaration (cp_parser* parser)
19037 {
19038   /* Look for the `__label__' keyword.  */
19039   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19040
19041   while (true)
19042     {
19043       tree identifier;
19044
19045       /* Look for an identifier.  */
19046       identifier = cp_parser_identifier (parser);
19047       /* If we failed, stop.  */
19048       if (identifier == error_mark_node)
19049         break;
19050       /* Declare it as a label.  */
19051       finish_label_decl (identifier);
19052       /* If the next token is a `;', stop.  */
19053       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19054         break;
19055       /* Look for the `,' separating the label declarations.  */
19056       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19057     }
19058
19059   /* Look for the final `;'.  */
19060   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19061 }
19062
19063 /* Support Functions */
19064
19065 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19066    NAME should have one of the representations used for an
19067    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19068    is returned.  If PARSER->SCOPE is a dependent type, then a
19069    SCOPE_REF is returned.
19070
19071    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19072    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19073    was formed.  Abstractly, such entities should not be passed to this
19074    function, because they do not need to be looked up, but it is
19075    simpler to check for this special case here, rather than at the
19076    call-sites.
19077
19078    In cases not explicitly covered above, this function returns a
19079    DECL, OVERLOAD, or baselink representing the result of the lookup.
19080    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19081    is returned.
19082
19083    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19084    (e.g., "struct") that was used.  In that case bindings that do not
19085    refer to types are ignored.
19086
19087    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19088    ignored.
19089
19090    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19091    are ignored.
19092
19093    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19094    types.
19095
19096    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19097    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19098    NULL_TREE otherwise.  */
19099
19100 static tree
19101 cp_parser_lookup_name (cp_parser *parser, tree name,
19102                        enum tag_types tag_type,
19103                        bool is_template,
19104                        bool is_namespace,
19105                        bool check_dependency,
19106                        tree *ambiguous_decls,
19107                        location_t name_location)
19108 {
19109   int flags = 0;
19110   tree decl;
19111   tree object_type = parser->context->object_type;
19112
19113   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19114     flags |= LOOKUP_COMPLAIN;
19115
19116   /* Assume that the lookup will be unambiguous.  */
19117   if (ambiguous_decls)
19118     *ambiguous_decls = NULL_TREE;
19119
19120   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19121      no longer valid.  Note that if we are parsing tentatively, and
19122      the parse fails, OBJECT_TYPE will be automatically restored.  */
19123   parser->context->object_type = NULL_TREE;
19124
19125   if (name == error_mark_node)
19126     return error_mark_node;
19127
19128   /* A template-id has already been resolved; there is no lookup to
19129      do.  */
19130   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19131     return name;
19132   if (BASELINK_P (name))
19133     {
19134       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19135                   == TEMPLATE_ID_EXPR);
19136       return name;
19137     }
19138
19139   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19140      it should already have been checked to make sure that the name
19141      used matches the type being destroyed.  */
19142   if (TREE_CODE (name) == BIT_NOT_EXPR)
19143     {
19144       tree type;
19145
19146       /* Figure out to which type this destructor applies.  */
19147       if (parser->scope)
19148         type = parser->scope;
19149       else if (object_type)
19150         type = object_type;
19151       else
19152         type = current_class_type;
19153       /* If that's not a class type, there is no destructor.  */
19154       if (!type || !CLASS_TYPE_P (type))
19155         return error_mark_node;
19156       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19157         lazily_declare_fn (sfk_destructor, type);
19158       if (!CLASSTYPE_DESTRUCTORS (type))
19159           return error_mark_node;
19160       /* If it was a class type, return the destructor.  */
19161       return CLASSTYPE_DESTRUCTORS (type);
19162     }
19163
19164   /* By this point, the NAME should be an ordinary identifier.  If
19165      the id-expression was a qualified name, the qualifying scope is
19166      stored in PARSER->SCOPE at this point.  */
19167   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19168
19169   /* Perform the lookup.  */
19170   if (parser->scope)
19171     {
19172       bool dependent_p;
19173
19174       if (parser->scope == error_mark_node)
19175         return error_mark_node;
19176
19177       /* If the SCOPE is dependent, the lookup must be deferred until
19178          the template is instantiated -- unless we are explicitly
19179          looking up names in uninstantiated templates.  Even then, we
19180          cannot look up the name if the scope is not a class type; it
19181          might, for example, be a template type parameter.  */
19182       dependent_p = (TYPE_P (parser->scope)
19183                      && dependent_scope_p (parser->scope));
19184       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19185           && dependent_p)
19186         /* Defer lookup.  */
19187         decl = error_mark_node;
19188       else
19189         {
19190           tree pushed_scope = NULL_TREE;
19191
19192           /* If PARSER->SCOPE is a dependent type, then it must be a
19193              class type, and we must not be checking dependencies;
19194              otherwise, we would have processed this lookup above.  So
19195              that PARSER->SCOPE is not considered a dependent base by
19196              lookup_member, we must enter the scope here.  */
19197           if (dependent_p)
19198             pushed_scope = push_scope (parser->scope);
19199
19200           /* If the PARSER->SCOPE is a template specialization, it
19201              may be instantiated during name lookup.  In that case,
19202              errors may be issued.  Even if we rollback the current
19203              tentative parse, those errors are valid.  */
19204           decl = lookup_qualified_name (parser->scope, name,
19205                                         tag_type != none_type,
19206                                         /*complain=*/true);
19207
19208           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19209              lookup result and the nested-name-specifier nominates a class C:
19210                * if the name specified after the nested-name-specifier, when
19211                looked up in C, is the injected-class-name of C (Clause 9), or
19212                * if the name specified after the nested-name-specifier is the
19213                same as the identifier or the simple-template-id's template-
19214                name in the last component of the nested-name-specifier,
19215              the name is instead considered to name the constructor of
19216              class C. [ Note: for example, the constructor is not an
19217              acceptable lookup result in an elaborated-type-specifier so
19218              the constructor would not be used in place of the
19219              injected-class-name. --end note ] Such a constructor name
19220              shall be used only in the declarator-id of a declaration that
19221              names a constructor or in a using-declaration.  */
19222           if (tag_type == none_type
19223               && DECL_SELF_REFERENCE_P (decl)
19224               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19225             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19226                                           tag_type != none_type,
19227                                           /*complain=*/true);
19228
19229           /* If we have a single function from a using decl, pull it out.  */
19230           if (TREE_CODE (decl) == OVERLOAD
19231               && !really_overloaded_fn (decl))
19232             decl = OVL_FUNCTION (decl);
19233
19234           if (pushed_scope)
19235             pop_scope (pushed_scope);
19236         }
19237
19238       /* If the scope is a dependent type and either we deferred lookup or
19239          we did lookup but didn't find the name, rememeber the name.  */
19240       if (decl == error_mark_node && TYPE_P (parser->scope)
19241           && dependent_type_p (parser->scope))
19242         {
19243           if (tag_type)
19244             {
19245               tree type;
19246
19247               /* The resolution to Core Issue 180 says that `struct
19248                  A::B' should be considered a type-name, even if `A'
19249                  is dependent.  */
19250               type = make_typename_type (parser->scope, name, tag_type,
19251                                          /*complain=*/tf_error);
19252               decl = TYPE_NAME (type);
19253             }
19254           else if (is_template
19255                    && (cp_parser_next_token_ends_template_argument_p (parser)
19256                        || cp_lexer_next_token_is (parser->lexer,
19257                                                   CPP_CLOSE_PAREN)))
19258             decl = make_unbound_class_template (parser->scope,
19259                                                 name, NULL_TREE,
19260                                                 /*complain=*/tf_error);
19261           else
19262             decl = build_qualified_name (/*type=*/NULL_TREE,
19263                                          parser->scope, name,
19264                                          is_template);
19265         }
19266       parser->qualifying_scope = parser->scope;
19267       parser->object_scope = NULL_TREE;
19268     }
19269   else if (object_type)
19270     {
19271       tree object_decl = NULL_TREE;
19272       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19273          OBJECT_TYPE is not a class.  */
19274       if (CLASS_TYPE_P (object_type))
19275         /* If the OBJECT_TYPE is a template specialization, it may
19276            be instantiated during name lookup.  In that case, errors
19277            may be issued.  Even if we rollback the current tentative
19278            parse, those errors are valid.  */
19279         object_decl = lookup_member (object_type,
19280                                      name,
19281                                      /*protect=*/0,
19282                                      tag_type != none_type);
19283       /* Look it up in the enclosing context, too.  */
19284       decl = lookup_name_real (name, tag_type != none_type,
19285                                /*nonclass=*/0,
19286                                /*block_p=*/true, is_namespace, flags);
19287       parser->object_scope = object_type;
19288       parser->qualifying_scope = NULL_TREE;
19289       if (object_decl)
19290         decl = object_decl;
19291     }
19292   else
19293     {
19294       decl = lookup_name_real (name, tag_type != none_type,
19295                                /*nonclass=*/0,
19296                                /*block_p=*/true, is_namespace, flags);
19297       parser->qualifying_scope = NULL_TREE;
19298       parser->object_scope = NULL_TREE;
19299     }
19300
19301   /* If the lookup failed, let our caller know.  */
19302   if (!decl || decl == error_mark_node)
19303     return error_mark_node;
19304
19305   /* Pull out the template from an injected-class-name (or multiple).  */
19306   if (is_template)
19307     decl = maybe_get_template_decl_from_type_decl (decl);
19308
19309   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19310   if (TREE_CODE (decl) == TREE_LIST)
19311     {
19312       if (ambiguous_decls)
19313         *ambiguous_decls = decl;
19314       /* The error message we have to print is too complicated for
19315          cp_parser_error, so we incorporate its actions directly.  */
19316       if (!cp_parser_simulate_error (parser))
19317         {
19318           error_at (name_location, "reference to %qD is ambiguous",
19319                     name);
19320           print_candidates (decl);
19321         }
19322       return error_mark_node;
19323     }
19324
19325   gcc_assert (DECL_P (decl)
19326               || TREE_CODE (decl) == OVERLOAD
19327               || TREE_CODE (decl) == SCOPE_REF
19328               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19329               || BASELINK_P (decl));
19330
19331   /* If we have resolved the name of a member declaration, check to
19332      see if the declaration is accessible.  When the name resolves to
19333      set of overloaded functions, accessibility is checked when
19334      overload resolution is done.
19335
19336      During an explicit instantiation, access is not checked at all,
19337      as per [temp.explicit].  */
19338   if (DECL_P (decl))
19339     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19340
19341   return decl;
19342 }
19343
19344 /* Like cp_parser_lookup_name, but for use in the typical case where
19345    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19346    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19347
19348 static tree
19349 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19350 {
19351   return cp_parser_lookup_name (parser, name,
19352                                 none_type,
19353                                 /*is_template=*/false,
19354                                 /*is_namespace=*/false,
19355                                 /*check_dependency=*/true,
19356                                 /*ambiguous_decls=*/NULL,
19357                                 location);
19358 }
19359
19360 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19361    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19362    true, the DECL indicates the class being defined in a class-head,
19363    or declared in an elaborated-type-specifier.
19364
19365    Otherwise, return DECL.  */
19366
19367 static tree
19368 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19369 {
19370   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19371      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19372
19373        struct A {
19374          template <typename T> struct B;
19375        };
19376
19377        template <typename T> struct A::B {};
19378
19379      Similarly, in an elaborated-type-specifier:
19380
19381        namespace N { struct X{}; }
19382
19383        struct A {
19384          template <typename T> friend struct N::X;
19385        };
19386
19387      However, if the DECL refers to a class type, and we are in
19388      the scope of the class, then the name lookup automatically
19389      finds the TYPE_DECL created by build_self_reference rather
19390      than a TEMPLATE_DECL.  For example, in:
19391
19392        template <class T> struct S {
19393          S s;
19394        };
19395
19396      there is no need to handle such case.  */
19397
19398   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19399     return DECL_TEMPLATE_RESULT (decl);
19400
19401   return decl;
19402 }
19403
19404 /* If too many, or too few, template-parameter lists apply to the
19405    declarator, issue an error message.  Returns TRUE if all went well,
19406    and FALSE otherwise.  */
19407
19408 static bool
19409 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19410                                                 cp_declarator *declarator,
19411                                                 location_t declarator_location)
19412 {
19413   unsigned num_templates;
19414
19415   /* We haven't seen any classes that involve template parameters yet.  */
19416   num_templates = 0;
19417
19418   switch (declarator->kind)
19419     {
19420     case cdk_id:
19421       if (declarator->u.id.qualifying_scope)
19422         {
19423           tree scope;
19424
19425           scope = declarator->u.id.qualifying_scope;
19426
19427           while (scope && CLASS_TYPE_P (scope))
19428             {
19429               /* You're supposed to have one `template <...>'
19430                  for every template class, but you don't need one
19431                  for a full specialization.  For example:
19432
19433                  template <class T> struct S{};
19434                  template <> struct S<int> { void f(); };
19435                  void S<int>::f () {}
19436
19437                  is correct; there shouldn't be a `template <>' for
19438                  the definition of `S<int>::f'.  */
19439               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19440                 /* If SCOPE does not have template information of any
19441                    kind, then it is not a template, nor is it nested
19442                    within a template.  */
19443                 break;
19444               if (explicit_class_specialization_p (scope))
19445                 break;
19446               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19447                 ++num_templates;
19448
19449               scope = TYPE_CONTEXT (scope);
19450             }
19451         }
19452       else if (TREE_CODE (declarator->u.id.unqualified_name)
19453                == TEMPLATE_ID_EXPR)
19454         /* If the DECLARATOR has the form `X<y>' then it uses one
19455            additional level of template parameters.  */
19456         ++num_templates;
19457
19458       return cp_parser_check_template_parameters 
19459         (parser, num_templates, declarator_location, declarator);
19460
19461
19462     case cdk_function:
19463     case cdk_array:
19464     case cdk_pointer:
19465     case cdk_reference:
19466     case cdk_ptrmem:
19467       return (cp_parser_check_declarator_template_parameters
19468               (parser, declarator->declarator, declarator_location));
19469
19470     case cdk_error:
19471       return true;
19472
19473     default:
19474       gcc_unreachable ();
19475     }
19476   return false;
19477 }
19478
19479 /* NUM_TEMPLATES were used in the current declaration.  If that is
19480    invalid, return FALSE and issue an error messages.  Otherwise,
19481    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19482    declarator and we can print more accurate diagnostics.  */
19483
19484 static bool
19485 cp_parser_check_template_parameters (cp_parser* parser,
19486                                      unsigned num_templates,
19487                                      location_t location,
19488                                      cp_declarator *declarator)
19489 {
19490   /* If there are the same number of template classes and parameter
19491      lists, that's OK.  */
19492   if (parser->num_template_parameter_lists == num_templates)
19493     return true;
19494   /* If there are more, but only one more, then we are referring to a
19495      member template.  That's OK too.  */
19496   if (parser->num_template_parameter_lists == num_templates + 1)
19497     return true;
19498   /* If there are more template classes than parameter lists, we have
19499      something like:
19500
19501        template <class T> void S<T>::R<T>::f ();  */
19502   if (parser->num_template_parameter_lists < num_templates)
19503     {
19504       if (declarator && !current_function_decl)
19505         error_at (location, "specializing member %<%T::%E%> "
19506                   "requires %<template<>%> syntax", 
19507                   declarator->u.id.qualifying_scope,
19508                   declarator->u.id.unqualified_name);
19509       else if (declarator)
19510         error_at (location, "invalid declaration of %<%T::%E%>",
19511                   declarator->u.id.qualifying_scope,
19512                   declarator->u.id.unqualified_name);
19513       else 
19514         error_at (location, "too few template-parameter-lists");
19515       return false;
19516     }
19517   /* Otherwise, there are too many template parameter lists.  We have
19518      something like:
19519
19520      template <class T> template <class U> void S::f();  */
19521   error_at (location, "too many template-parameter-lists");
19522   return false;
19523 }
19524
19525 /* Parse an optional `::' token indicating that the following name is
19526    from the global namespace.  If so, PARSER->SCOPE is set to the
19527    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19528    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19529    Returns the new value of PARSER->SCOPE, if the `::' token is
19530    present, and NULL_TREE otherwise.  */
19531
19532 static tree
19533 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19534 {
19535   cp_token *token;
19536
19537   /* Peek at the next token.  */
19538   token = cp_lexer_peek_token (parser->lexer);
19539   /* If we're looking at a `::' token then we're starting from the
19540      global namespace, not our current location.  */
19541   if (token->type == CPP_SCOPE)
19542     {
19543       /* Consume the `::' token.  */
19544       cp_lexer_consume_token (parser->lexer);
19545       /* Set the SCOPE so that we know where to start the lookup.  */
19546       parser->scope = global_namespace;
19547       parser->qualifying_scope = global_namespace;
19548       parser->object_scope = NULL_TREE;
19549
19550       return parser->scope;
19551     }
19552   else if (!current_scope_valid_p)
19553     {
19554       parser->scope = NULL_TREE;
19555       parser->qualifying_scope = NULL_TREE;
19556       parser->object_scope = NULL_TREE;
19557     }
19558
19559   return NULL_TREE;
19560 }
19561
19562 /* Returns TRUE if the upcoming token sequence is the start of a
19563    constructor declarator.  If FRIEND_P is true, the declarator is
19564    preceded by the `friend' specifier.  */
19565
19566 static bool
19567 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19568 {
19569   bool constructor_p;
19570   tree nested_name_specifier;
19571   cp_token *next_token;
19572
19573   /* The common case is that this is not a constructor declarator, so
19574      try to avoid doing lots of work if at all possible.  It's not
19575      valid declare a constructor at function scope.  */
19576   if (parser->in_function_body)
19577     return false;
19578   /* And only certain tokens can begin a constructor declarator.  */
19579   next_token = cp_lexer_peek_token (parser->lexer);
19580   if (next_token->type != CPP_NAME
19581       && next_token->type != CPP_SCOPE
19582       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19583       && next_token->type != CPP_TEMPLATE_ID)
19584     return false;
19585
19586   /* Parse tentatively; we are going to roll back all of the tokens
19587      consumed here.  */
19588   cp_parser_parse_tentatively (parser);
19589   /* Assume that we are looking at a constructor declarator.  */
19590   constructor_p = true;
19591
19592   /* Look for the optional `::' operator.  */
19593   cp_parser_global_scope_opt (parser,
19594                               /*current_scope_valid_p=*/false);
19595   /* Look for the nested-name-specifier.  */
19596   nested_name_specifier
19597     = (cp_parser_nested_name_specifier_opt (parser,
19598                                             /*typename_keyword_p=*/false,
19599                                             /*check_dependency_p=*/false,
19600                                             /*type_p=*/false,
19601                                             /*is_declaration=*/false));
19602   /* Outside of a class-specifier, there must be a
19603      nested-name-specifier.  */
19604   if (!nested_name_specifier &&
19605       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19606        || friend_p))
19607     constructor_p = false;
19608   else if (nested_name_specifier == error_mark_node)
19609     constructor_p = false;
19610
19611   /* If we have a class scope, this is easy; DR 147 says that S::S always
19612      names the constructor, and no other qualified name could.  */
19613   if (constructor_p && nested_name_specifier
19614       && TYPE_P (nested_name_specifier))
19615     {
19616       tree id = cp_parser_unqualified_id (parser,
19617                                           /*template_keyword_p=*/false,
19618                                           /*check_dependency_p=*/false,
19619                                           /*declarator_p=*/true,
19620                                           /*optional_p=*/false);
19621       if (is_overloaded_fn (id))
19622         id = DECL_NAME (get_first_fn (id));
19623       if (!constructor_name_p (id, nested_name_specifier))
19624         constructor_p = false;
19625     }
19626   /* If we still think that this might be a constructor-declarator,
19627      look for a class-name.  */
19628   else if (constructor_p)
19629     {
19630       /* If we have:
19631
19632            template <typename T> struct S {
19633              S();
19634            };
19635
19636          we must recognize that the nested `S' names a class.  */
19637       tree type_decl;
19638       type_decl = cp_parser_class_name (parser,
19639                                         /*typename_keyword_p=*/false,
19640                                         /*template_keyword_p=*/false,
19641                                         none_type,
19642                                         /*check_dependency_p=*/false,
19643                                         /*class_head_p=*/false,
19644                                         /*is_declaration=*/false);
19645       /* If there was no class-name, then this is not a constructor.  */
19646       constructor_p = !cp_parser_error_occurred (parser);
19647
19648       /* If we're still considering a constructor, we have to see a `(',
19649          to begin the parameter-declaration-clause, followed by either a
19650          `)', an `...', or a decl-specifier.  We need to check for a
19651          type-specifier to avoid being fooled into thinking that:
19652
19653            S (f) (int);
19654
19655          is a constructor.  (It is actually a function named `f' that
19656          takes one parameter (of type `int') and returns a value of type
19657          `S'.  */
19658       if (constructor_p
19659           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19660         constructor_p = false;
19661
19662       if (constructor_p
19663           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19664           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19665           /* A parameter declaration begins with a decl-specifier,
19666              which is either the "attribute" keyword, a storage class
19667              specifier, or (usually) a type-specifier.  */
19668           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19669         {
19670           tree type;
19671           tree pushed_scope = NULL_TREE;
19672           unsigned saved_num_template_parameter_lists;
19673
19674           /* Names appearing in the type-specifier should be looked up
19675              in the scope of the class.  */
19676           if (current_class_type)
19677             type = NULL_TREE;
19678           else
19679             {
19680               type = TREE_TYPE (type_decl);
19681               if (TREE_CODE (type) == TYPENAME_TYPE)
19682                 {
19683                   type = resolve_typename_type (type,
19684                                                 /*only_current_p=*/false);
19685                   if (TREE_CODE (type) == TYPENAME_TYPE)
19686                     {
19687                       cp_parser_abort_tentative_parse (parser);
19688                       return false;
19689                     }
19690                 }
19691               pushed_scope = push_scope (type);
19692             }
19693
19694           /* Inside the constructor parameter list, surrounding
19695              template-parameter-lists do not apply.  */
19696           saved_num_template_parameter_lists
19697             = parser->num_template_parameter_lists;
19698           parser->num_template_parameter_lists = 0;
19699
19700           /* Look for the type-specifier.  */
19701           cp_parser_type_specifier (parser,
19702                                     CP_PARSER_FLAGS_NONE,
19703                                     /*decl_specs=*/NULL,
19704                                     /*is_declarator=*/true,
19705                                     /*declares_class_or_enum=*/NULL,
19706                                     /*is_cv_qualifier=*/NULL);
19707
19708           parser->num_template_parameter_lists
19709             = saved_num_template_parameter_lists;
19710
19711           /* Leave the scope of the class.  */
19712           if (pushed_scope)
19713             pop_scope (pushed_scope);
19714
19715           constructor_p = !cp_parser_error_occurred (parser);
19716         }
19717     }
19718
19719   /* We did not really want to consume any tokens.  */
19720   cp_parser_abort_tentative_parse (parser);
19721
19722   return constructor_p;
19723 }
19724
19725 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19726    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19727    they must be performed once we are in the scope of the function.
19728
19729    Returns the function defined.  */
19730
19731 static tree
19732 cp_parser_function_definition_from_specifiers_and_declarator
19733   (cp_parser* parser,
19734    cp_decl_specifier_seq *decl_specifiers,
19735    tree attributes,
19736    const cp_declarator *declarator)
19737 {
19738   tree fn;
19739   bool success_p;
19740
19741   /* Begin the function-definition.  */
19742   success_p = start_function (decl_specifiers, declarator, attributes);
19743
19744   /* The things we're about to see are not directly qualified by any
19745      template headers we've seen thus far.  */
19746   reset_specialization ();
19747
19748   /* If there were names looked up in the decl-specifier-seq that we
19749      did not check, check them now.  We must wait until we are in the
19750      scope of the function to perform the checks, since the function
19751      might be a friend.  */
19752   perform_deferred_access_checks ();
19753
19754   if (!success_p)
19755     {
19756       /* Skip the entire function.  */
19757       cp_parser_skip_to_end_of_block_or_statement (parser);
19758       fn = error_mark_node;
19759     }
19760   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19761     {
19762       /* Seen already, skip it.  An error message has already been output.  */
19763       cp_parser_skip_to_end_of_block_or_statement (parser);
19764       fn = current_function_decl;
19765       current_function_decl = NULL_TREE;
19766       /* If this is a function from a class, pop the nested class.  */
19767       if (current_class_name)
19768         pop_nested_class ();
19769     }
19770   else
19771     fn = cp_parser_function_definition_after_declarator (parser,
19772                                                          /*inline_p=*/false);
19773
19774   return fn;
19775 }
19776
19777 /* Parse the part of a function-definition that follows the
19778    declarator.  INLINE_P is TRUE iff this function is an inline
19779    function defined within a class-specifier.
19780
19781    Returns the function defined.  */
19782
19783 static tree
19784 cp_parser_function_definition_after_declarator (cp_parser* parser,
19785                                                 bool inline_p)
19786 {
19787   tree fn;
19788   bool ctor_initializer_p = false;
19789   bool saved_in_unbraced_linkage_specification_p;
19790   bool saved_in_function_body;
19791   unsigned saved_num_template_parameter_lists;
19792   cp_token *token;
19793
19794   saved_in_function_body = parser->in_function_body;
19795   parser->in_function_body = true;
19796   /* If the next token is `return', then the code may be trying to
19797      make use of the "named return value" extension that G++ used to
19798      support.  */
19799   token = cp_lexer_peek_token (parser->lexer);
19800   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19801     {
19802       /* Consume the `return' keyword.  */
19803       cp_lexer_consume_token (parser->lexer);
19804       /* Look for the identifier that indicates what value is to be
19805          returned.  */
19806       cp_parser_identifier (parser);
19807       /* Issue an error message.  */
19808       error_at (token->location,
19809                 "named return values are no longer supported");
19810       /* Skip tokens until we reach the start of the function body.  */
19811       while (true)
19812         {
19813           cp_token *token = cp_lexer_peek_token (parser->lexer);
19814           if (token->type == CPP_OPEN_BRACE
19815               || token->type == CPP_EOF
19816               || token->type == CPP_PRAGMA_EOL)
19817             break;
19818           cp_lexer_consume_token (parser->lexer);
19819         }
19820     }
19821   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19822      anything declared inside `f'.  */
19823   saved_in_unbraced_linkage_specification_p
19824     = parser->in_unbraced_linkage_specification_p;
19825   parser->in_unbraced_linkage_specification_p = false;
19826   /* Inside the function, surrounding template-parameter-lists do not
19827      apply.  */
19828   saved_num_template_parameter_lists
19829     = parser->num_template_parameter_lists;
19830   parser->num_template_parameter_lists = 0;
19831
19832   start_lambda_scope (current_function_decl);
19833
19834   /* If the next token is `try', then we are looking at a
19835      function-try-block.  */
19836   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19837     ctor_initializer_p = cp_parser_function_try_block (parser);
19838   /* A function-try-block includes the function-body, so we only do
19839      this next part if we're not processing a function-try-block.  */
19840   else
19841     ctor_initializer_p
19842       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19843
19844   finish_lambda_scope ();
19845
19846   /* Finish the function.  */
19847   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19848                         (inline_p ? 2 : 0));
19849   /* Generate code for it, if necessary.  */
19850   expand_or_defer_fn (fn);
19851   /* Restore the saved values.  */
19852   parser->in_unbraced_linkage_specification_p
19853     = saved_in_unbraced_linkage_specification_p;
19854   parser->num_template_parameter_lists
19855     = saved_num_template_parameter_lists;
19856   parser->in_function_body = saved_in_function_body;
19857
19858   return fn;
19859 }
19860
19861 /* Parse a template-declaration, assuming that the `export' (and
19862    `extern') keywords, if present, has already been scanned.  MEMBER_P
19863    is as for cp_parser_template_declaration.  */
19864
19865 static void
19866 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19867 {
19868   tree decl = NULL_TREE;
19869   VEC (deferred_access_check,gc) *checks;
19870   tree parameter_list;
19871   bool friend_p = false;
19872   bool need_lang_pop;
19873   cp_token *token;
19874
19875   /* Look for the `template' keyword.  */
19876   token = cp_lexer_peek_token (parser->lexer);
19877   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19878     return;
19879
19880   /* And the `<'.  */
19881   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19882     return;
19883   if (at_class_scope_p () && current_function_decl)
19884     {
19885       /* 14.5.2.2 [temp.mem]
19886
19887          A local class shall not have member templates.  */
19888       error_at (token->location,
19889                 "invalid declaration of member template in local class");
19890       cp_parser_skip_to_end_of_block_or_statement (parser);
19891       return;
19892     }
19893   /* [temp]
19894
19895      A template ... shall not have C linkage.  */
19896   if (current_lang_name == lang_name_c)
19897     {
19898       error_at (token->location, "template with C linkage");
19899       /* Give it C++ linkage to avoid confusing other parts of the
19900          front end.  */
19901       push_lang_context (lang_name_cplusplus);
19902       need_lang_pop = true;
19903     }
19904   else
19905     need_lang_pop = false;
19906
19907   /* We cannot perform access checks on the template parameter
19908      declarations until we know what is being declared, just as we
19909      cannot check the decl-specifier list.  */
19910   push_deferring_access_checks (dk_deferred);
19911
19912   /* If the next token is `>', then we have an invalid
19913      specialization.  Rather than complain about an invalid template
19914      parameter, issue an error message here.  */
19915   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19916     {
19917       cp_parser_error (parser, "invalid explicit specialization");
19918       begin_specialization ();
19919       parameter_list = NULL_TREE;
19920     }
19921   else
19922     {
19923       /* Parse the template parameters.  */
19924       parameter_list = cp_parser_template_parameter_list (parser);
19925       fixup_template_parms ();
19926     }
19927
19928   /* Get the deferred access checks from the parameter list.  These
19929      will be checked once we know what is being declared, as for a
19930      member template the checks must be performed in the scope of the
19931      class containing the member.  */
19932   checks = get_deferred_access_checks ();
19933
19934   /* Look for the `>'.  */
19935   cp_parser_skip_to_end_of_template_parameter_list (parser);
19936   /* We just processed one more parameter list.  */
19937   ++parser->num_template_parameter_lists;
19938   /* If the next token is `template', there are more template
19939      parameters.  */
19940   if (cp_lexer_next_token_is_keyword (parser->lexer,
19941                                       RID_TEMPLATE))
19942     cp_parser_template_declaration_after_export (parser, member_p);
19943   else
19944     {
19945       /* There are no access checks when parsing a template, as we do not
19946          know if a specialization will be a friend.  */
19947       push_deferring_access_checks (dk_no_check);
19948       token = cp_lexer_peek_token (parser->lexer);
19949       decl = cp_parser_single_declaration (parser,
19950                                            checks,
19951                                            member_p,
19952                                            /*explicit_specialization_p=*/false,
19953                                            &friend_p);
19954       pop_deferring_access_checks ();
19955
19956       /* If this is a member template declaration, let the front
19957          end know.  */
19958       if (member_p && !friend_p && decl)
19959         {
19960           if (TREE_CODE (decl) == TYPE_DECL)
19961             cp_parser_check_access_in_redeclaration (decl, token->location);
19962
19963           decl = finish_member_template_decl (decl);
19964         }
19965       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19966         make_friend_class (current_class_type, TREE_TYPE (decl),
19967                            /*complain=*/true);
19968     }
19969   /* We are done with the current parameter list.  */
19970   --parser->num_template_parameter_lists;
19971
19972   pop_deferring_access_checks ();
19973
19974   /* Finish up.  */
19975   finish_template_decl (parameter_list);
19976
19977   /* Register member declarations.  */
19978   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19979     finish_member_declaration (decl);
19980   /* For the erroneous case of a template with C linkage, we pushed an
19981      implicit C++ linkage scope; exit that scope now.  */
19982   if (need_lang_pop)
19983     pop_lang_context ();
19984   /* If DECL is a function template, we must return to parse it later.
19985      (Even though there is no definition, there might be default
19986      arguments that need handling.)  */
19987   if (member_p && decl
19988       && (TREE_CODE (decl) == FUNCTION_DECL
19989           || DECL_FUNCTION_TEMPLATE_P (decl)))
19990     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19991 }
19992
19993 /* Perform the deferred access checks from a template-parameter-list.
19994    CHECKS is a TREE_LIST of access checks, as returned by
19995    get_deferred_access_checks.  */
19996
19997 static void
19998 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19999 {
20000   ++processing_template_parmlist;
20001   perform_access_checks (checks);
20002   --processing_template_parmlist;
20003 }
20004
20005 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20006    `function-definition' sequence.  MEMBER_P is true, this declaration
20007    appears in a class scope.
20008
20009    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20010    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20011
20012 static tree
20013 cp_parser_single_declaration (cp_parser* parser,
20014                               VEC (deferred_access_check,gc)* checks,
20015                               bool member_p,
20016                               bool explicit_specialization_p,
20017                               bool* friend_p)
20018 {
20019   int declares_class_or_enum;
20020   tree decl = NULL_TREE;
20021   cp_decl_specifier_seq decl_specifiers;
20022   bool function_definition_p = false;
20023   cp_token *decl_spec_token_start;
20024
20025   /* This function is only used when processing a template
20026      declaration.  */
20027   gcc_assert (innermost_scope_kind () == sk_template_parms
20028               || innermost_scope_kind () == sk_template_spec);
20029
20030   /* Defer access checks until we know what is being declared.  */
20031   push_deferring_access_checks (dk_deferred);
20032
20033   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20034      alternative.  */
20035   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20036   cp_parser_decl_specifier_seq (parser,
20037                                 CP_PARSER_FLAGS_OPTIONAL,
20038                                 &decl_specifiers,
20039                                 &declares_class_or_enum);
20040   if (friend_p)
20041     *friend_p = cp_parser_friend_p (&decl_specifiers);
20042
20043   /* There are no template typedefs.  */
20044   if (decl_specifiers.specs[(int) ds_typedef])
20045     {
20046       error_at (decl_spec_token_start->location,
20047                 "template declaration of %<typedef%>");
20048       decl = error_mark_node;
20049     }
20050
20051   /* Gather up the access checks that occurred the
20052      decl-specifier-seq.  */
20053   stop_deferring_access_checks ();
20054
20055   /* Check for the declaration of a template class.  */
20056   if (declares_class_or_enum)
20057     {
20058       if (cp_parser_declares_only_class_p (parser))
20059         {
20060           decl = shadow_tag (&decl_specifiers);
20061
20062           /* In this case:
20063
20064                struct C {
20065                  friend template <typename T> struct A<T>::B;
20066                };
20067
20068              A<T>::B will be represented by a TYPENAME_TYPE, and
20069              therefore not recognized by shadow_tag.  */
20070           if (friend_p && *friend_p
20071               && !decl
20072               && decl_specifiers.type
20073               && TYPE_P (decl_specifiers.type))
20074             decl = decl_specifiers.type;
20075
20076           if (decl && decl != error_mark_node)
20077             decl = TYPE_NAME (decl);
20078           else
20079             decl = error_mark_node;
20080
20081           /* Perform access checks for template parameters.  */
20082           cp_parser_perform_template_parameter_access_checks (checks);
20083         }
20084     }
20085
20086   /* Complain about missing 'typename' or other invalid type names.  */
20087   if (!decl_specifiers.any_type_specifiers_p)
20088     cp_parser_parse_and_diagnose_invalid_type_name (parser);
20089
20090   /* If it's not a template class, try for a template function.  If
20091      the next token is a `;', then this declaration does not declare
20092      anything.  But, if there were errors in the decl-specifiers, then
20093      the error might well have come from an attempted class-specifier.
20094      In that case, there's no need to warn about a missing declarator.  */
20095   if (!decl
20096       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20097           || decl_specifiers.type != error_mark_node))
20098     {
20099       decl = cp_parser_init_declarator (parser,
20100                                         &decl_specifiers,
20101                                         checks,
20102                                         /*function_definition_allowed_p=*/true,
20103                                         member_p,
20104                                         declares_class_or_enum,
20105                                         &function_definition_p,
20106                                         NULL);
20107
20108     /* 7.1.1-1 [dcl.stc]
20109
20110        A storage-class-specifier shall not be specified in an explicit
20111        specialization...  */
20112     if (decl
20113         && explicit_specialization_p
20114         && decl_specifiers.storage_class != sc_none)
20115       {
20116         error_at (decl_spec_token_start->location,
20117                   "explicit template specialization cannot have a storage class");
20118         decl = error_mark_node;
20119       }
20120     }
20121
20122   pop_deferring_access_checks ();
20123
20124   /* Clear any current qualification; whatever comes next is the start
20125      of something new.  */
20126   parser->scope = NULL_TREE;
20127   parser->qualifying_scope = NULL_TREE;
20128   parser->object_scope = NULL_TREE;
20129   /* Look for a trailing `;' after the declaration.  */
20130   if (!function_definition_p
20131       && (decl == error_mark_node
20132           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20133     cp_parser_skip_to_end_of_block_or_statement (parser);
20134
20135   return decl;
20136 }
20137
20138 /* Parse a cast-expression that is not the operand of a unary "&".  */
20139
20140 static tree
20141 cp_parser_simple_cast_expression (cp_parser *parser)
20142 {
20143   return cp_parser_cast_expression (parser, /*address_p=*/false,
20144                                     /*cast_p=*/false, NULL);
20145 }
20146
20147 /* Parse a functional cast to TYPE.  Returns an expression
20148    representing the cast.  */
20149
20150 static tree
20151 cp_parser_functional_cast (cp_parser* parser, tree type)
20152 {
20153   VEC(tree,gc) *vec;
20154   tree expression_list;
20155   tree cast;
20156   bool nonconst_p;
20157
20158   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20159     {
20160       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20161       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20162       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20163       if (TREE_CODE (type) == TYPE_DECL)
20164         type = TREE_TYPE (type);
20165       return finish_compound_literal (type, expression_list);
20166     }
20167
20168
20169   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20170                                                  /*cast_p=*/true,
20171                                                  /*allow_expansion_p=*/true,
20172                                                  /*non_constant_p=*/NULL);
20173   if (vec == NULL)
20174     expression_list = error_mark_node;
20175   else
20176     {
20177       expression_list = build_tree_list_vec (vec);
20178       release_tree_vector (vec);
20179     }
20180
20181   cast = build_functional_cast (type, expression_list,
20182                                 tf_warning_or_error);
20183   /* [expr.const]/1: In an integral constant expression "only type
20184      conversions to integral or enumeration type can be used".  */
20185   if (TREE_CODE (type) == TYPE_DECL)
20186     type = TREE_TYPE (type);
20187   if (cast != error_mark_node
20188       && !cast_valid_in_integral_constant_expression_p (type)
20189       && cp_parser_non_integral_constant_expression (parser,
20190                                                      NIC_CONSTRUCTOR))
20191     return error_mark_node;
20192   return cast;
20193 }
20194
20195 /* Save the tokens that make up the body of a member function defined
20196    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20197    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20198    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20199    for the member function.  */
20200
20201 static tree
20202 cp_parser_save_member_function_body (cp_parser* parser,
20203                                      cp_decl_specifier_seq *decl_specifiers,
20204                                      cp_declarator *declarator,
20205                                      tree attributes)
20206 {
20207   cp_token *first;
20208   cp_token *last;
20209   tree fn;
20210
20211   /* Create the FUNCTION_DECL.  */
20212   fn = grokmethod (decl_specifiers, declarator, attributes);
20213   /* If something went badly wrong, bail out now.  */
20214   if (fn == error_mark_node)
20215     {
20216       /* If there's a function-body, skip it.  */
20217       if (cp_parser_token_starts_function_definition_p
20218           (cp_lexer_peek_token (parser->lexer)))
20219         cp_parser_skip_to_end_of_block_or_statement (parser);
20220       return error_mark_node;
20221     }
20222
20223   /* Remember it, if there default args to post process.  */
20224   cp_parser_save_default_args (parser, fn);
20225
20226   /* Save away the tokens that make up the body of the
20227      function.  */
20228   first = parser->lexer->next_token;
20229   /* We can have braced-init-list mem-initializers before the fn body.  */
20230   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20231     {
20232       cp_lexer_consume_token (parser->lexer);
20233       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20234              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20235         {
20236           /* cache_group will stop after an un-nested { } pair, too.  */
20237           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20238             break;
20239
20240           /* variadic mem-inits have ... after the ')'.  */
20241           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20242             cp_lexer_consume_token (parser->lexer);
20243         }
20244     }
20245   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20246   /* Handle function try blocks.  */
20247   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20248     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20249   last = parser->lexer->next_token;
20250
20251   /* Save away the inline definition; we will process it when the
20252      class is complete.  */
20253   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20254   DECL_PENDING_INLINE_P (fn) = 1;
20255
20256   /* We need to know that this was defined in the class, so that
20257      friend templates are handled correctly.  */
20258   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20259
20260   /* Add FN to the queue of functions to be parsed later.  */
20261   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20262
20263   return fn;
20264 }
20265
20266 /* Parse a template-argument-list, as well as the trailing ">" (but
20267    not the opening ">").  See cp_parser_template_argument_list for the
20268    return value.  */
20269
20270 static tree
20271 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20272 {
20273   tree arguments;
20274   tree saved_scope;
20275   tree saved_qualifying_scope;
20276   tree saved_object_scope;
20277   bool saved_greater_than_is_operator_p;
20278   int saved_unevaluated_operand;
20279   int saved_inhibit_evaluation_warnings;
20280
20281   /* [temp.names]
20282
20283      When parsing a template-id, the first non-nested `>' is taken as
20284      the end of the template-argument-list rather than a greater-than
20285      operator.  */
20286   saved_greater_than_is_operator_p
20287     = parser->greater_than_is_operator_p;
20288   parser->greater_than_is_operator_p = false;
20289   /* Parsing the argument list may modify SCOPE, so we save it
20290      here.  */
20291   saved_scope = parser->scope;
20292   saved_qualifying_scope = parser->qualifying_scope;
20293   saved_object_scope = parser->object_scope;
20294   /* We need to evaluate the template arguments, even though this
20295      template-id may be nested within a "sizeof".  */
20296   saved_unevaluated_operand = cp_unevaluated_operand;
20297   cp_unevaluated_operand = 0;
20298   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20299   c_inhibit_evaluation_warnings = 0;
20300   /* Parse the template-argument-list itself.  */
20301   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20302       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20303     arguments = NULL_TREE;
20304   else
20305     arguments = cp_parser_template_argument_list (parser);
20306   /* Look for the `>' that ends the template-argument-list. If we find
20307      a '>>' instead, it's probably just a typo.  */
20308   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20309     {
20310       if (cxx_dialect != cxx98)
20311         {
20312           /* In C++0x, a `>>' in a template argument list or cast
20313              expression is considered to be two separate `>'
20314              tokens. So, change the current token to a `>', but don't
20315              consume it: it will be consumed later when the outer
20316              template argument list (or cast expression) is parsed.
20317              Note that this replacement of `>' for `>>' is necessary
20318              even if we are parsing tentatively: in the tentative
20319              case, after calling
20320              cp_parser_enclosed_template_argument_list we will always
20321              throw away all of the template arguments and the first
20322              closing `>', either because the template argument list
20323              was erroneous or because we are replacing those tokens
20324              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20325              not have been thrown away) is needed either to close an
20326              outer template argument list or to complete a new-style
20327              cast.  */
20328           cp_token *token = cp_lexer_peek_token (parser->lexer);
20329           token->type = CPP_GREATER;
20330         }
20331       else if (!saved_greater_than_is_operator_p)
20332         {
20333           /* If we're in a nested template argument list, the '>>' has
20334             to be a typo for '> >'. We emit the error message, but we
20335             continue parsing and we push a '>' as next token, so that
20336             the argument list will be parsed correctly.  Note that the
20337             global source location is still on the token before the
20338             '>>', so we need to say explicitly where we want it.  */
20339           cp_token *token = cp_lexer_peek_token (parser->lexer);
20340           error_at (token->location, "%<>>%> should be %<> >%> "
20341                     "within a nested template argument list");
20342
20343           token->type = CPP_GREATER;
20344         }
20345       else
20346         {
20347           /* If this is not a nested template argument list, the '>>'
20348             is a typo for '>'. Emit an error message and continue.
20349             Same deal about the token location, but here we can get it
20350             right by consuming the '>>' before issuing the diagnostic.  */
20351           cp_token *token = cp_lexer_consume_token (parser->lexer);
20352           error_at (token->location,
20353                     "spurious %<>>%>, use %<>%> to terminate "
20354                     "a template argument list");
20355         }
20356     }
20357   else
20358     cp_parser_skip_to_end_of_template_parameter_list (parser);
20359   /* The `>' token might be a greater-than operator again now.  */
20360   parser->greater_than_is_operator_p
20361     = saved_greater_than_is_operator_p;
20362   /* Restore the SAVED_SCOPE.  */
20363   parser->scope = saved_scope;
20364   parser->qualifying_scope = saved_qualifying_scope;
20365   parser->object_scope = saved_object_scope;
20366   cp_unevaluated_operand = saved_unevaluated_operand;
20367   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20368
20369   return arguments;
20370 }
20371
20372 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20373    arguments, or the body of the function have not yet been parsed,
20374    parse them now.  */
20375
20376 static void
20377 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20378 {
20379   /* If this member is a template, get the underlying
20380      FUNCTION_DECL.  */
20381   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20382     member_function = DECL_TEMPLATE_RESULT (member_function);
20383
20384   /* There should not be any class definitions in progress at this
20385      point; the bodies of members are only parsed outside of all class
20386      definitions.  */
20387   gcc_assert (parser->num_classes_being_defined == 0);
20388   /* While we're parsing the member functions we might encounter more
20389      classes.  We want to handle them right away, but we don't want
20390      them getting mixed up with functions that are currently in the
20391      queue.  */
20392   push_unparsed_function_queues (parser);
20393
20394   /* Make sure that any template parameters are in scope.  */
20395   maybe_begin_member_template_processing (member_function);
20396
20397   /* If the body of the function has not yet been parsed, parse it
20398      now.  */
20399   if (DECL_PENDING_INLINE_P (member_function))
20400     {
20401       tree function_scope;
20402       cp_token_cache *tokens;
20403
20404       /* The function is no longer pending; we are processing it.  */
20405       tokens = DECL_PENDING_INLINE_INFO (member_function);
20406       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20407       DECL_PENDING_INLINE_P (member_function) = 0;
20408
20409       /* If this is a local class, enter the scope of the containing
20410          function.  */
20411       function_scope = current_function_decl;
20412       if (function_scope)
20413         push_function_context ();
20414
20415       /* Push the body of the function onto the lexer stack.  */
20416       cp_parser_push_lexer_for_tokens (parser, tokens);
20417
20418       /* Let the front end know that we going to be defining this
20419          function.  */
20420       start_preparsed_function (member_function, NULL_TREE,
20421                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20422
20423       /* Don't do access checking if it is a templated function.  */
20424       if (processing_template_decl)
20425         push_deferring_access_checks (dk_no_check);
20426
20427       /* Now, parse the body of the function.  */
20428       cp_parser_function_definition_after_declarator (parser,
20429                                                       /*inline_p=*/true);
20430
20431       if (processing_template_decl)
20432         pop_deferring_access_checks ();
20433
20434       /* Leave the scope of the containing function.  */
20435       if (function_scope)
20436         pop_function_context ();
20437       cp_parser_pop_lexer (parser);
20438     }
20439
20440   /* Remove any template parameters from the symbol table.  */
20441   maybe_end_member_template_processing ();
20442
20443   /* Restore the queue.  */
20444   pop_unparsed_function_queues (parser);
20445 }
20446
20447 /* If DECL contains any default args, remember it on the unparsed
20448    functions queue.  */
20449
20450 static void
20451 cp_parser_save_default_args (cp_parser* parser, tree decl)
20452 {
20453   tree probe;
20454
20455   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20456        probe;
20457        probe = TREE_CHAIN (probe))
20458     if (TREE_PURPOSE (probe))
20459       {
20460         cp_default_arg_entry *entry
20461           = VEC_safe_push (cp_default_arg_entry, gc,
20462                            unparsed_funs_with_default_args, NULL);
20463         entry->class_type = current_class_type;
20464         entry->decl = decl;
20465         break;
20466       }
20467 }
20468
20469 /* FN is a FUNCTION_DECL which may contains a parameter with an
20470    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20471    assumes that the current scope is the scope in which the default
20472    argument should be processed.  */
20473
20474 static void
20475 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20476 {
20477   bool saved_local_variables_forbidden_p;
20478   tree parm, parmdecl;
20479
20480   /* While we're parsing the default args, we might (due to the
20481      statement expression extension) encounter more classes.  We want
20482      to handle them right away, but we don't want them getting mixed
20483      up with default args that are currently in the queue.  */
20484   push_unparsed_function_queues (parser);
20485
20486   /* Local variable names (and the `this' keyword) may not appear
20487      in a default argument.  */
20488   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20489   parser->local_variables_forbidden_p = true;
20490
20491   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20492          parmdecl = DECL_ARGUMENTS (fn);
20493        parm && parm != void_list_node;
20494        parm = TREE_CHAIN (parm),
20495          parmdecl = DECL_CHAIN (parmdecl))
20496     {
20497       cp_token_cache *tokens;
20498       tree default_arg = TREE_PURPOSE (parm);
20499       tree parsed_arg;
20500       VEC(tree,gc) *insts;
20501       tree copy;
20502       unsigned ix;
20503
20504       if (!default_arg)
20505         continue;
20506
20507       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20508         /* This can happen for a friend declaration for a function
20509            already declared with default arguments.  */
20510         continue;
20511
20512        /* Push the saved tokens for the default argument onto the parser's
20513           lexer stack.  */
20514       tokens = DEFARG_TOKENS (default_arg);
20515       cp_parser_push_lexer_for_tokens (parser, tokens);
20516
20517       start_lambda_scope (parmdecl);
20518
20519       /* Parse the assignment-expression.  */
20520       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20521       if (parsed_arg == error_mark_node)
20522         {
20523           cp_parser_pop_lexer (parser);
20524           continue;
20525         }
20526
20527       if (!processing_template_decl)
20528         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20529
20530       TREE_PURPOSE (parm) = parsed_arg;
20531
20532       /* Update any instantiations we've already created.  */
20533       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20534            VEC_iterate (tree, insts, ix, copy); ix++)
20535         TREE_PURPOSE (copy) = parsed_arg;
20536
20537       finish_lambda_scope ();
20538
20539       /* If the token stream has not been completely used up, then
20540          there was extra junk after the end of the default
20541          argument.  */
20542       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20543         cp_parser_error (parser, "expected %<,%>");
20544
20545       /* Revert to the main lexer.  */
20546       cp_parser_pop_lexer (parser);
20547     }
20548
20549   /* Make sure no default arg is missing.  */
20550   check_default_args (fn);
20551
20552   /* Restore the state of local_variables_forbidden_p.  */
20553   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20554
20555   /* Restore the queue.  */
20556   pop_unparsed_function_queues (parser);
20557 }
20558
20559 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20560    either a TYPE or an expression, depending on the form of the
20561    input.  The KEYWORD indicates which kind of expression we have
20562    encountered.  */
20563
20564 static tree
20565 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20566 {
20567   tree expr = NULL_TREE;
20568   const char *saved_message;
20569   char *tmp;
20570   bool saved_integral_constant_expression_p;
20571   bool saved_non_integral_constant_expression_p;
20572   bool pack_expansion_p = false;
20573
20574   /* Types cannot be defined in a `sizeof' expression.  Save away the
20575      old message.  */
20576   saved_message = parser->type_definition_forbidden_message;
20577   /* And create the new one.  */
20578   tmp = concat ("types may not be defined in %<",
20579                 IDENTIFIER_POINTER (ridpointers[keyword]),
20580                 "%> expressions", NULL);
20581   parser->type_definition_forbidden_message = tmp;
20582
20583   /* The restrictions on constant-expressions do not apply inside
20584      sizeof expressions.  */
20585   saved_integral_constant_expression_p
20586     = parser->integral_constant_expression_p;
20587   saved_non_integral_constant_expression_p
20588     = parser->non_integral_constant_expression_p;
20589   parser->integral_constant_expression_p = false;
20590
20591   /* If it's a `...', then we are computing the length of a parameter
20592      pack.  */
20593   if (keyword == RID_SIZEOF
20594       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20595     {
20596       /* Consume the `...'.  */
20597       cp_lexer_consume_token (parser->lexer);
20598       maybe_warn_variadic_templates ();
20599
20600       /* Note that this is an expansion.  */
20601       pack_expansion_p = true;
20602     }
20603
20604   /* Do not actually evaluate the expression.  */
20605   ++cp_unevaluated_operand;
20606   ++c_inhibit_evaluation_warnings;
20607   /* If it's a `(', then we might be looking at the type-id
20608      construction.  */
20609   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20610     {
20611       tree type;
20612       bool saved_in_type_id_in_expr_p;
20613
20614       /* We can't be sure yet whether we're looking at a type-id or an
20615          expression.  */
20616       cp_parser_parse_tentatively (parser);
20617       /* Consume the `('.  */
20618       cp_lexer_consume_token (parser->lexer);
20619       /* Parse the type-id.  */
20620       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20621       parser->in_type_id_in_expr_p = true;
20622       type = cp_parser_type_id (parser);
20623       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20624       /* Now, look for the trailing `)'.  */
20625       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20626       /* If all went well, then we're done.  */
20627       if (cp_parser_parse_definitely (parser))
20628         {
20629           cp_decl_specifier_seq decl_specs;
20630
20631           /* Build a trivial decl-specifier-seq.  */
20632           clear_decl_specs (&decl_specs);
20633           decl_specs.type = type;
20634
20635           /* Call grokdeclarator to figure out what type this is.  */
20636           expr = grokdeclarator (NULL,
20637                                  &decl_specs,
20638                                  TYPENAME,
20639                                  /*initialized=*/0,
20640                                  /*attrlist=*/NULL);
20641         }
20642     }
20643
20644   /* If the type-id production did not work out, then we must be
20645      looking at the unary-expression production.  */
20646   if (!expr)
20647     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20648                                        /*cast_p=*/false, NULL);
20649
20650   if (pack_expansion_p)
20651     /* Build a pack expansion. */
20652     expr = make_pack_expansion (expr);
20653
20654   /* Go back to evaluating expressions.  */
20655   --cp_unevaluated_operand;
20656   --c_inhibit_evaluation_warnings;
20657
20658   /* Free the message we created.  */
20659   free (tmp);
20660   /* And restore the old one.  */
20661   parser->type_definition_forbidden_message = saved_message;
20662   parser->integral_constant_expression_p
20663     = saved_integral_constant_expression_p;
20664   parser->non_integral_constant_expression_p
20665     = saved_non_integral_constant_expression_p;
20666
20667   return expr;
20668 }
20669
20670 /* If the current declaration has no declarator, return true.  */
20671
20672 static bool
20673 cp_parser_declares_only_class_p (cp_parser *parser)
20674 {
20675   /* If the next token is a `;' or a `,' then there is no
20676      declarator.  */
20677   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20678           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20679 }
20680
20681 /* Update the DECL_SPECS to reflect the storage class indicated by
20682    KEYWORD.  */
20683
20684 static void
20685 cp_parser_set_storage_class (cp_parser *parser,
20686                              cp_decl_specifier_seq *decl_specs,
20687                              enum rid keyword,
20688                              location_t location)
20689 {
20690   cp_storage_class storage_class;
20691
20692   if (parser->in_unbraced_linkage_specification_p)
20693     {
20694       error_at (location, "invalid use of %qD in linkage specification",
20695                 ridpointers[keyword]);
20696       return;
20697     }
20698   else if (decl_specs->storage_class != sc_none)
20699     {
20700       decl_specs->conflicting_specifiers_p = true;
20701       return;
20702     }
20703
20704   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20705       && decl_specs->specs[(int) ds_thread])
20706     {
20707       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20708       decl_specs->specs[(int) ds_thread] = 0;
20709     }
20710
20711   switch (keyword)
20712     {
20713     case RID_AUTO:
20714       storage_class = sc_auto;
20715       break;
20716     case RID_REGISTER:
20717       storage_class = sc_register;
20718       break;
20719     case RID_STATIC:
20720       storage_class = sc_static;
20721       break;
20722     case RID_EXTERN:
20723       storage_class = sc_extern;
20724       break;
20725     case RID_MUTABLE:
20726       storage_class = sc_mutable;
20727       break;
20728     default:
20729       gcc_unreachable ();
20730     }
20731   decl_specs->storage_class = storage_class;
20732
20733   /* A storage class specifier cannot be applied alongside a typedef 
20734      specifier. If there is a typedef specifier present then set 
20735      conflicting_specifiers_p which will trigger an error later
20736      on in grokdeclarator. */
20737   if (decl_specs->specs[(int)ds_typedef])
20738     decl_specs->conflicting_specifiers_p = true;
20739 }
20740
20741 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20742    is true, the type is a user-defined type; otherwise it is a
20743    built-in type specified by a keyword.  */
20744
20745 static void
20746 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20747                               tree type_spec,
20748                               location_t location,
20749                               bool user_defined_p)
20750 {
20751   decl_specs->any_specifiers_p = true;
20752
20753   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20754      (with, for example, in "typedef int wchar_t;") we remember that
20755      this is what happened.  In system headers, we ignore these
20756      declarations so that G++ can work with system headers that are not
20757      C++-safe.  */
20758   if (decl_specs->specs[(int) ds_typedef]
20759       && !user_defined_p
20760       && (type_spec == boolean_type_node
20761           || type_spec == char16_type_node
20762           || type_spec == char32_type_node
20763           || type_spec == wchar_type_node)
20764       && (decl_specs->type
20765           || decl_specs->specs[(int) ds_long]
20766           || decl_specs->specs[(int) ds_short]
20767           || decl_specs->specs[(int) ds_unsigned]
20768           || decl_specs->specs[(int) ds_signed]))
20769     {
20770       decl_specs->redefined_builtin_type = type_spec;
20771       if (!decl_specs->type)
20772         {
20773           decl_specs->type = type_spec;
20774           decl_specs->user_defined_type_p = false;
20775           decl_specs->type_location = location;
20776         }
20777     }
20778   else if (decl_specs->type)
20779     decl_specs->multiple_types_p = true;
20780   else
20781     {
20782       decl_specs->type = type_spec;
20783       decl_specs->user_defined_type_p = user_defined_p;
20784       decl_specs->redefined_builtin_type = NULL_TREE;
20785       decl_specs->type_location = location;
20786     }
20787 }
20788
20789 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20790    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20791
20792 static bool
20793 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20794 {
20795   return decl_specifiers->specs[(int) ds_friend] != 0;
20796 }
20797
20798 /* Issue an error message indicating that TOKEN_DESC was expected.
20799    If KEYWORD is true, it indicated this function is called by
20800    cp_parser_require_keword and the required token can only be
20801    a indicated keyword. */
20802
20803 static void
20804 cp_parser_required_error (cp_parser *parser,
20805                           required_token token_desc,
20806                           bool keyword)
20807 {
20808   switch (token_desc)
20809     {
20810       case RT_NEW:
20811         cp_parser_error (parser, "expected %<new%>");
20812         return;
20813       case RT_DELETE:
20814         cp_parser_error (parser, "expected %<delete%>");
20815         return;
20816       case RT_RETURN:
20817         cp_parser_error (parser, "expected %<return%>");
20818         return;
20819       case RT_WHILE:
20820         cp_parser_error (parser, "expected %<while%>");
20821         return;
20822       case RT_EXTERN:
20823         cp_parser_error (parser, "expected %<extern%>");
20824         return;
20825       case RT_STATIC_ASSERT:
20826         cp_parser_error (parser, "expected %<static_assert%>");
20827         return;
20828       case RT_DECLTYPE:
20829         cp_parser_error (parser, "expected %<decltype%>");
20830         return;
20831       case RT_OPERATOR:
20832         cp_parser_error (parser, "expected %<operator%>");
20833         return;
20834       case RT_CLASS:
20835         cp_parser_error (parser, "expected %<class%>");
20836         return;
20837       case RT_TEMPLATE:
20838         cp_parser_error (parser, "expected %<template%>");
20839         return;
20840       case RT_NAMESPACE:
20841         cp_parser_error (parser, "expected %<namespace%>");
20842         return;
20843       case RT_USING:
20844         cp_parser_error (parser, "expected %<using%>");
20845         return;
20846       case RT_ASM:
20847         cp_parser_error (parser, "expected %<asm%>");
20848         return;
20849       case RT_TRY:
20850         cp_parser_error (parser, "expected %<try%>");
20851         return;
20852       case RT_CATCH:
20853         cp_parser_error (parser, "expected %<catch%>");
20854         return;
20855       case RT_THROW:
20856         cp_parser_error (parser, "expected %<throw%>");
20857         return;
20858       case RT_LABEL:
20859         cp_parser_error (parser, "expected %<__label__%>");
20860         return;
20861       case RT_AT_TRY:
20862         cp_parser_error (parser, "expected %<@try%>");
20863         return;
20864       case RT_AT_SYNCHRONIZED:
20865         cp_parser_error (parser, "expected %<@synchronized%>");
20866         return;
20867       case RT_AT_THROW:
20868         cp_parser_error (parser, "expected %<@throw%>");
20869         return;
20870       default:
20871         break;
20872     }
20873   if (!keyword)
20874     {
20875       switch (token_desc)
20876         {
20877           case RT_SEMICOLON:
20878             cp_parser_error (parser, "expected %<;%>");
20879             return;
20880           case RT_OPEN_PAREN:
20881             cp_parser_error (parser, "expected %<(%>");
20882             return;
20883           case RT_CLOSE_BRACE:
20884             cp_parser_error (parser, "expected %<}%>");
20885             return;
20886           case RT_OPEN_BRACE:
20887             cp_parser_error (parser, "expected %<{%>");
20888             return;
20889           case RT_CLOSE_SQUARE:
20890             cp_parser_error (parser, "expected %<]%>");
20891             return;
20892           case RT_OPEN_SQUARE:
20893             cp_parser_error (parser, "expected %<[%>");
20894             return;
20895           case RT_COMMA:
20896             cp_parser_error (parser, "expected %<,%>");
20897             return;
20898           case RT_SCOPE:
20899             cp_parser_error (parser, "expected %<::%>");
20900             return;
20901           case RT_LESS:
20902             cp_parser_error (parser, "expected %<<%>");
20903             return;
20904           case RT_GREATER:
20905             cp_parser_error (parser, "expected %<>%>");
20906             return;
20907           case RT_EQ:
20908             cp_parser_error (parser, "expected %<=%>");
20909             return;
20910           case RT_ELLIPSIS:
20911             cp_parser_error (parser, "expected %<...%>");
20912             return;
20913           case RT_MULT:
20914             cp_parser_error (parser, "expected %<*%>");
20915             return;
20916           case RT_COMPL:
20917             cp_parser_error (parser, "expected %<~%>");
20918             return;
20919           case RT_COLON:
20920             cp_parser_error (parser, "expected %<:%>");
20921             return;
20922           case RT_COLON_SCOPE:
20923             cp_parser_error (parser, "expected %<:%> or %<::%>");
20924             return;
20925           case RT_CLOSE_PAREN:
20926             cp_parser_error (parser, "expected %<)%>");
20927             return;
20928           case RT_COMMA_CLOSE_PAREN:
20929             cp_parser_error (parser, "expected %<,%> or %<)%>");
20930             return;
20931           case RT_PRAGMA_EOL:
20932             cp_parser_error (parser, "expected end of line");
20933             return;
20934           case RT_NAME:
20935             cp_parser_error (parser, "expected identifier");
20936             return;
20937           case RT_SELECT:
20938             cp_parser_error (parser, "expected selection-statement");
20939             return;
20940           case RT_INTERATION:
20941             cp_parser_error (parser, "expected iteration-statement");
20942             return;
20943           case RT_JUMP:
20944             cp_parser_error (parser, "expected jump-statement");
20945             return;
20946           case RT_CLASS_KEY:
20947             cp_parser_error (parser, "expected class-key");
20948             return;
20949           case RT_CLASS_TYPENAME_TEMPLATE:
20950             cp_parser_error (parser,
20951                  "expected %<class%>, %<typename%>, or %<template%>");
20952             return;
20953           default:
20954             gcc_unreachable ();
20955         }
20956     }
20957   else
20958     gcc_unreachable ();
20959 }
20960
20961
20962
20963 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20964    issue an error message indicating that TOKEN_DESC was expected.
20965
20966    Returns the token consumed, if the token had the appropriate type.
20967    Otherwise, returns NULL.  */
20968
20969 static cp_token *
20970 cp_parser_require (cp_parser* parser,
20971                    enum cpp_ttype type,
20972                    required_token token_desc)
20973 {
20974   if (cp_lexer_next_token_is (parser->lexer, type))
20975     return cp_lexer_consume_token (parser->lexer);
20976   else
20977     {
20978       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20979       if (!cp_parser_simulate_error (parser))
20980         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20981       return NULL;
20982     }
20983 }
20984
20985 /* An error message is produced if the next token is not '>'.
20986    All further tokens are skipped until the desired token is
20987    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20988
20989 static void
20990 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20991 {
20992   /* Current level of '< ... >'.  */
20993   unsigned level = 0;
20994   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
20995   unsigned nesting_depth = 0;
20996
20997   /* Are we ready, yet?  If not, issue error message.  */
20998   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20999     return;
21000
21001   /* Skip tokens until the desired token is found.  */
21002   while (true)
21003     {
21004       /* Peek at the next token.  */
21005       switch (cp_lexer_peek_token (parser->lexer)->type)
21006         {
21007         case CPP_LESS:
21008           if (!nesting_depth)
21009             ++level;
21010           break;
21011
21012         case CPP_RSHIFT:
21013           if (cxx_dialect == cxx98)
21014             /* C++0x views the `>>' operator as two `>' tokens, but
21015                C++98 does not. */
21016             break;
21017           else if (!nesting_depth && level-- == 0)
21018             {
21019               /* We've hit a `>>' where the first `>' closes the
21020                  template argument list, and the second `>' is
21021                  spurious.  Just consume the `>>' and stop; we've
21022                  already produced at least one error.  */
21023               cp_lexer_consume_token (parser->lexer);
21024               return;
21025             }
21026           /* Fall through for C++0x, so we handle the second `>' in
21027              the `>>'.  */
21028
21029         case CPP_GREATER:
21030           if (!nesting_depth && level-- == 0)
21031             {
21032               /* We've reached the token we want, consume it and stop.  */
21033               cp_lexer_consume_token (parser->lexer);
21034               return;
21035             }
21036           break;
21037
21038         case CPP_OPEN_PAREN:
21039         case CPP_OPEN_SQUARE:
21040           ++nesting_depth;
21041           break;
21042
21043         case CPP_CLOSE_PAREN:
21044         case CPP_CLOSE_SQUARE:
21045           if (nesting_depth-- == 0)
21046             return;
21047           break;
21048
21049         case CPP_EOF:
21050         case CPP_PRAGMA_EOL:
21051         case CPP_SEMICOLON:
21052         case CPP_OPEN_BRACE:
21053         case CPP_CLOSE_BRACE:
21054           /* The '>' was probably forgotten, don't look further.  */
21055           return;
21056
21057         default:
21058           break;
21059         }
21060
21061       /* Consume this token.  */
21062       cp_lexer_consume_token (parser->lexer);
21063     }
21064 }
21065
21066 /* If the next token is the indicated keyword, consume it.  Otherwise,
21067    issue an error message indicating that TOKEN_DESC was expected.
21068
21069    Returns the token consumed, if the token had the appropriate type.
21070    Otherwise, returns NULL.  */
21071
21072 static cp_token *
21073 cp_parser_require_keyword (cp_parser* parser,
21074                            enum rid keyword,
21075                            required_token token_desc)
21076 {
21077   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21078
21079   if (token && token->keyword != keyword)
21080     {
21081       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21082       return NULL;
21083     }
21084
21085   return token;
21086 }
21087
21088 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21089    function-definition.  */
21090
21091 static bool
21092 cp_parser_token_starts_function_definition_p (cp_token* token)
21093 {
21094   return (/* An ordinary function-body begins with an `{'.  */
21095           token->type == CPP_OPEN_BRACE
21096           /* A ctor-initializer begins with a `:'.  */
21097           || token->type == CPP_COLON
21098           /* A function-try-block begins with `try'.  */
21099           || token->keyword == RID_TRY
21100           /* The named return value extension begins with `return'.  */
21101           || token->keyword == RID_RETURN);
21102 }
21103
21104 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21105    definition.  */
21106
21107 static bool
21108 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21109 {
21110   cp_token *token;
21111
21112   token = cp_lexer_peek_token (parser->lexer);
21113   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21114 }
21115
21116 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21117    C++0x) ending a template-argument.  */
21118
21119 static bool
21120 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21121 {
21122   cp_token *token;
21123
21124   token = cp_lexer_peek_token (parser->lexer);
21125   return (token->type == CPP_COMMA 
21126           || token->type == CPP_GREATER
21127           || token->type == CPP_ELLIPSIS
21128           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21129 }
21130
21131 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21132    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21133
21134 static bool
21135 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21136                                                      size_t n)
21137 {
21138   cp_token *token;
21139
21140   token = cp_lexer_peek_nth_token (parser->lexer, n);
21141   if (token->type == CPP_LESS)
21142     return true;
21143   /* Check for the sequence `<::' in the original code. It would be lexed as
21144      `[:', where `[' is a digraph, and there is no whitespace before
21145      `:'.  */
21146   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21147     {
21148       cp_token *token2;
21149       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21150       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21151         return true;
21152     }
21153   return false;
21154 }
21155
21156 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21157    or none_type otherwise.  */
21158
21159 static enum tag_types
21160 cp_parser_token_is_class_key (cp_token* token)
21161 {
21162   switch (token->keyword)
21163     {
21164     case RID_CLASS:
21165       return class_type;
21166     case RID_STRUCT:
21167       return record_type;
21168     case RID_UNION:
21169       return union_type;
21170
21171     default:
21172       return none_type;
21173     }
21174 }
21175
21176 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21177
21178 static void
21179 cp_parser_check_class_key (enum tag_types class_key, tree type)
21180 {
21181   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21182     permerror (input_location, "%qs tag used in naming %q#T",
21183             class_key == union_type ? "union"
21184              : class_key == record_type ? "struct" : "class",
21185              type);
21186 }
21187
21188 /* Issue an error message if DECL is redeclared with different
21189    access than its original declaration [class.access.spec/3].
21190    This applies to nested classes and nested class templates.
21191    [class.mem/1].  */
21192
21193 static void
21194 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21195 {
21196   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21197     return;
21198
21199   if ((TREE_PRIVATE (decl)
21200        != (current_access_specifier == access_private_node))
21201       || (TREE_PROTECTED (decl)
21202           != (current_access_specifier == access_protected_node)))
21203     error_at (location, "%qD redeclared with different access", decl);
21204 }
21205
21206 /* Look for the `template' keyword, as a syntactic disambiguator.
21207    Return TRUE iff it is present, in which case it will be
21208    consumed.  */
21209
21210 static bool
21211 cp_parser_optional_template_keyword (cp_parser *parser)
21212 {
21213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21214     {
21215       /* The `template' keyword can only be used within templates;
21216          outside templates the parser can always figure out what is a
21217          template and what is not.  */
21218       if (!processing_template_decl)
21219         {
21220           cp_token *token = cp_lexer_peek_token (parser->lexer);
21221           error_at (token->location,
21222                     "%<template%> (as a disambiguator) is only allowed "
21223                     "within templates");
21224           /* If this part of the token stream is rescanned, the same
21225              error message would be generated.  So, we purge the token
21226              from the stream.  */
21227           cp_lexer_purge_token (parser->lexer);
21228           return false;
21229         }
21230       else
21231         {
21232           /* Consume the `template' keyword.  */
21233           cp_lexer_consume_token (parser->lexer);
21234           return true;
21235         }
21236     }
21237
21238   return false;
21239 }
21240
21241 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21242    set PARSER->SCOPE, and perform other related actions.  */
21243
21244 static void
21245 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21246 {
21247   int i;
21248   struct tree_check *check_value;
21249   deferred_access_check *chk;
21250   VEC (deferred_access_check,gc) *checks;
21251
21252   /* Get the stored value.  */
21253   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21254   /* Perform any access checks that were deferred.  */
21255   checks = check_value->checks;
21256   if (checks)
21257     {
21258       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21259         perform_or_defer_access_check (chk->binfo,
21260                                        chk->decl,
21261                                        chk->diag_decl);
21262     }
21263   /* Set the scope from the stored value.  */
21264   parser->scope = check_value->value;
21265   parser->qualifying_scope = check_value->qualifying_scope;
21266   parser->object_scope = NULL_TREE;
21267 }
21268
21269 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21270    encounter the end of a block before what we were looking for.  */
21271
21272 static bool
21273 cp_parser_cache_group (cp_parser *parser,
21274                        enum cpp_ttype end,
21275                        unsigned depth)
21276 {
21277   while (true)
21278     {
21279       cp_token *token = cp_lexer_peek_token (parser->lexer);
21280
21281       /* Abort a parenthesized expression if we encounter a semicolon.  */
21282       if ((end == CPP_CLOSE_PAREN || depth == 0)
21283           && token->type == CPP_SEMICOLON)
21284         return true;
21285       /* If we've reached the end of the file, stop.  */
21286       if (token->type == CPP_EOF
21287           || (end != CPP_PRAGMA_EOL
21288               && token->type == CPP_PRAGMA_EOL))
21289         return true;
21290       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21291         /* We've hit the end of an enclosing block, so there's been some
21292            kind of syntax error.  */
21293         return true;
21294
21295       /* Consume the token.  */
21296       cp_lexer_consume_token (parser->lexer);
21297       /* See if it starts a new group.  */
21298       if (token->type == CPP_OPEN_BRACE)
21299         {
21300           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21301           /* In theory this should probably check end == '}', but
21302              cp_parser_save_member_function_body needs it to exit
21303              after either '}' or ')' when called with ')'.  */
21304           if (depth == 0)
21305             return false;
21306         }
21307       else if (token->type == CPP_OPEN_PAREN)
21308         {
21309           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21310           if (depth == 0 && end == CPP_CLOSE_PAREN)
21311             return false;
21312         }
21313       else if (token->type == CPP_PRAGMA)
21314         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21315       else if (token->type == end)
21316         return false;
21317     }
21318 }
21319
21320 /* Begin parsing tentatively.  We always save tokens while parsing
21321    tentatively so that if the tentative parsing fails we can restore the
21322    tokens.  */
21323
21324 static void
21325 cp_parser_parse_tentatively (cp_parser* parser)
21326 {
21327   /* Enter a new parsing context.  */
21328   parser->context = cp_parser_context_new (parser->context);
21329   /* Begin saving tokens.  */
21330   cp_lexer_save_tokens (parser->lexer);
21331   /* In order to avoid repetitive access control error messages,
21332      access checks are queued up until we are no longer parsing
21333      tentatively.  */
21334   push_deferring_access_checks (dk_deferred);
21335 }
21336
21337 /* Commit to the currently active tentative parse.  */
21338
21339 static void
21340 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21341 {
21342   cp_parser_context *context;
21343   cp_lexer *lexer;
21344
21345   /* Mark all of the levels as committed.  */
21346   lexer = parser->lexer;
21347   for (context = parser->context; context->next; context = context->next)
21348     {
21349       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21350         break;
21351       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21352       while (!cp_lexer_saving_tokens (lexer))
21353         lexer = lexer->next;
21354       cp_lexer_commit_tokens (lexer);
21355     }
21356 }
21357
21358 /* Abort the currently active tentative parse.  All consumed tokens
21359    will be rolled back, and no diagnostics will be issued.  */
21360
21361 static void
21362 cp_parser_abort_tentative_parse (cp_parser* parser)
21363 {
21364   cp_parser_simulate_error (parser);
21365   /* Now, pretend that we want to see if the construct was
21366      successfully parsed.  */
21367   cp_parser_parse_definitely (parser);
21368 }
21369
21370 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21371    token stream.  Otherwise, commit to the tokens we have consumed.
21372    Returns true if no error occurred; false otherwise.  */
21373
21374 static bool
21375 cp_parser_parse_definitely (cp_parser* parser)
21376 {
21377   bool error_occurred;
21378   cp_parser_context *context;
21379
21380   /* Remember whether or not an error occurred, since we are about to
21381      destroy that information.  */
21382   error_occurred = cp_parser_error_occurred (parser);
21383   /* Remove the topmost context from the stack.  */
21384   context = parser->context;
21385   parser->context = context->next;
21386   /* If no parse errors occurred, commit to the tentative parse.  */
21387   if (!error_occurred)
21388     {
21389       /* Commit to the tokens read tentatively, unless that was
21390          already done.  */
21391       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21392         cp_lexer_commit_tokens (parser->lexer);
21393
21394       pop_to_parent_deferring_access_checks ();
21395     }
21396   /* Otherwise, if errors occurred, roll back our state so that things
21397      are just as they were before we began the tentative parse.  */
21398   else
21399     {
21400       cp_lexer_rollback_tokens (parser->lexer);
21401       pop_deferring_access_checks ();
21402     }
21403   /* Add the context to the front of the free list.  */
21404   context->next = cp_parser_context_free_list;
21405   cp_parser_context_free_list = context;
21406
21407   return !error_occurred;
21408 }
21409
21410 /* Returns true if we are parsing tentatively and are not committed to
21411    this tentative parse.  */
21412
21413 static bool
21414 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21415 {
21416   return (cp_parser_parsing_tentatively (parser)
21417           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21418 }
21419
21420 /* Returns nonzero iff an error has occurred during the most recent
21421    tentative parse.  */
21422
21423 static bool
21424 cp_parser_error_occurred (cp_parser* parser)
21425 {
21426   return (cp_parser_parsing_tentatively (parser)
21427           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21428 }
21429
21430 /* Returns nonzero if GNU extensions are allowed.  */
21431
21432 static bool
21433 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21434 {
21435   return parser->allow_gnu_extensions_p;
21436 }
21437 \f
21438 /* Objective-C++ Productions */
21439
21440
21441 /* Parse an Objective-C expression, which feeds into a primary-expression
21442    above.
21443
21444    objc-expression:
21445      objc-message-expression
21446      objc-string-literal
21447      objc-encode-expression
21448      objc-protocol-expression
21449      objc-selector-expression
21450
21451   Returns a tree representation of the expression.  */
21452
21453 static tree
21454 cp_parser_objc_expression (cp_parser* parser)
21455 {
21456   /* Try to figure out what kind of declaration is present.  */
21457   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21458
21459   switch (kwd->type)
21460     {
21461     case CPP_OPEN_SQUARE:
21462       return cp_parser_objc_message_expression (parser);
21463
21464     case CPP_OBJC_STRING:
21465       kwd = cp_lexer_consume_token (parser->lexer);
21466       return objc_build_string_object (kwd->u.value);
21467
21468     case CPP_KEYWORD:
21469       switch (kwd->keyword)
21470         {
21471         case RID_AT_ENCODE:
21472           return cp_parser_objc_encode_expression (parser);
21473
21474         case RID_AT_PROTOCOL:
21475           return cp_parser_objc_protocol_expression (parser);
21476
21477         case RID_AT_SELECTOR:
21478           return cp_parser_objc_selector_expression (parser);
21479
21480         default:
21481           break;
21482         }
21483     default:
21484       error_at (kwd->location,
21485                 "misplaced %<@%D%> Objective-C++ construct",
21486                 kwd->u.value);
21487       cp_parser_skip_to_end_of_block_or_statement (parser);
21488     }
21489
21490   return error_mark_node;
21491 }
21492
21493 /* Parse an Objective-C message expression.
21494
21495    objc-message-expression:
21496      [ objc-message-receiver objc-message-args ]
21497
21498    Returns a representation of an Objective-C message.  */
21499
21500 static tree
21501 cp_parser_objc_message_expression (cp_parser* parser)
21502 {
21503   tree receiver, messageargs;
21504
21505   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21506   receiver = cp_parser_objc_message_receiver (parser);
21507   messageargs = cp_parser_objc_message_args (parser);
21508   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21509
21510   return objc_build_message_expr (build_tree_list (receiver, messageargs));
21511 }
21512
21513 /* Parse an objc-message-receiver.
21514
21515    objc-message-receiver:
21516      expression
21517      simple-type-specifier
21518
21519   Returns a representation of the type or expression.  */
21520
21521 static tree
21522 cp_parser_objc_message_receiver (cp_parser* parser)
21523 {
21524   tree rcv;
21525
21526   /* An Objective-C message receiver may be either (1) a type
21527      or (2) an expression.  */
21528   cp_parser_parse_tentatively (parser);
21529   rcv = cp_parser_expression (parser, false, NULL);
21530
21531   if (cp_parser_parse_definitely (parser))
21532     return rcv;
21533
21534   rcv = cp_parser_simple_type_specifier (parser,
21535                                          /*decl_specs=*/NULL,
21536                                          CP_PARSER_FLAGS_NONE);
21537
21538   return objc_get_class_reference (rcv);
21539 }
21540
21541 /* Parse the arguments and selectors comprising an Objective-C message.
21542
21543    objc-message-args:
21544      objc-selector
21545      objc-selector-args
21546      objc-selector-args , objc-comma-args
21547
21548    objc-selector-args:
21549      objc-selector [opt] : assignment-expression
21550      objc-selector-args objc-selector [opt] : assignment-expression
21551
21552    objc-comma-args:
21553      assignment-expression
21554      objc-comma-args , assignment-expression
21555
21556    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21557    selector arguments and TREE_VALUE containing a list of comma
21558    arguments.  */
21559
21560 static tree
21561 cp_parser_objc_message_args (cp_parser* parser)
21562 {
21563   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21564   bool maybe_unary_selector_p = true;
21565   cp_token *token = cp_lexer_peek_token (parser->lexer);
21566
21567   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21568     {
21569       tree selector = NULL_TREE, arg;
21570
21571       if (token->type != CPP_COLON)
21572         selector = cp_parser_objc_selector (parser);
21573
21574       /* Detect if we have a unary selector.  */
21575       if (maybe_unary_selector_p
21576           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21577         return build_tree_list (selector, NULL_TREE);
21578
21579       maybe_unary_selector_p = false;
21580       cp_parser_require (parser, CPP_COLON, RT_COLON);
21581       arg = cp_parser_assignment_expression (parser, false, NULL);
21582
21583       sel_args
21584         = chainon (sel_args,
21585                    build_tree_list (selector, arg));
21586
21587       token = cp_lexer_peek_token (parser->lexer);
21588     }
21589
21590   /* Handle non-selector arguments, if any. */
21591   while (token->type == CPP_COMMA)
21592     {
21593       tree arg;
21594
21595       cp_lexer_consume_token (parser->lexer);
21596       arg = cp_parser_assignment_expression (parser, false, NULL);
21597
21598       addl_args
21599         = chainon (addl_args,
21600                    build_tree_list (NULL_TREE, arg));
21601
21602       token = cp_lexer_peek_token (parser->lexer);
21603     }
21604
21605   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21606     {
21607       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21608       return build_tree_list (error_mark_node, error_mark_node);
21609     }
21610
21611   return build_tree_list (sel_args, addl_args);
21612 }
21613
21614 /* Parse an Objective-C encode expression.
21615
21616    objc-encode-expression:
21617      @encode objc-typename
21618
21619    Returns an encoded representation of the type argument.  */
21620
21621 static tree
21622 cp_parser_objc_encode_expression (cp_parser* parser)
21623 {
21624   tree type;
21625   cp_token *token;
21626
21627   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21628   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21629   token = cp_lexer_peek_token (parser->lexer);
21630   type = complete_type (cp_parser_type_id (parser));
21631   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21632
21633   if (!type)
21634     {
21635       error_at (token->location, 
21636                 "%<@encode%> must specify a type as an argument");
21637       return error_mark_node;
21638     }
21639
21640   /* This happens if we find @encode(T) (where T is a template
21641      typename or something dependent on a template typename) when
21642      parsing a template.  In that case, we can't compile it
21643      immediately, but we rather create an AT_ENCODE_EXPR which will
21644      need to be instantiated when the template is used.
21645   */
21646   if (dependent_type_p (type))
21647     {
21648       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21649       TREE_READONLY (value) = 1;
21650       return value;
21651     }
21652
21653   return objc_build_encode_expr (type);
21654 }
21655
21656 /* Parse an Objective-C @defs expression.  */
21657
21658 static tree
21659 cp_parser_objc_defs_expression (cp_parser *parser)
21660 {
21661   tree name;
21662
21663   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21664   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21665   name = cp_parser_identifier (parser);
21666   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21667
21668   return objc_get_class_ivars (name);
21669 }
21670
21671 /* Parse an Objective-C protocol expression.
21672
21673   objc-protocol-expression:
21674     @protocol ( identifier )
21675
21676   Returns a representation of the protocol expression.  */
21677
21678 static tree
21679 cp_parser_objc_protocol_expression (cp_parser* parser)
21680 {
21681   tree proto;
21682
21683   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21684   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21685   proto = cp_parser_identifier (parser);
21686   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21687
21688   return objc_build_protocol_expr (proto);
21689 }
21690
21691 /* Parse an Objective-C selector expression.
21692
21693    objc-selector-expression:
21694      @selector ( objc-method-signature )
21695
21696    objc-method-signature:
21697      objc-selector
21698      objc-selector-seq
21699
21700    objc-selector-seq:
21701      objc-selector :
21702      objc-selector-seq objc-selector :
21703
21704   Returns a representation of the method selector.  */
21705
21706 static tree
21707 cp_parser_objc_selector_expression (cp_parser* parser)
21708 {
21709   tree sel_seq = NULL_TREE;
21710   bool maybe_unary_selector_p = true;
21711   cp_token *token;
21712   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21713
21714   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21715   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21716   token = cp_lexer_peek_token (parser->lexer);
21717
21718   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21719          || token->type == CPP_SCOPE)
21720     {
21721       tree selector = NULL_TREE;
21722
21723       if (token->type != CPP_COLON
21724           || token->type == CPP_SCOPE)
21725         selector = cp_parser_objc_selector (parser);
21726
21727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21728           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21729         {
21730           /* Detect if we have a unary selector.  */
21731           if (maybe_unary_selector_p)
21732             {
21733               sel_seq = selector;
21734               goto finish_selector;
21735             }
21736           else
21737             {
21738               cp_parser_error (parser, "expected %<:%>");
21739             }
21740         }
21741       maybe_unary_selector_p = false;
21742       token = cp_lexer_consume_token (parser->lexer);
21743
21744       if (token->type == CPP_SCOPE)
21745         {
21746           sel_seq
21747             = chainon (sel_seq,
21748                        build_tree_list (selector, NULL_TREE));
21749           sel_seq
21750             = chainon (sel_seq,
21751                        build_tree_list (NULL_TREE, NULL_TREE));
21752         }
21753       else
21754         sel_seq
21755           = chainon (sel_seq,
21756                      build_tree_list (selector, NULL_TREE));
21757
21758       token = cp_lexer_peek_token (parser->lexer);
21759     }
21760
21761  finish_selector:
21762   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21763
21764   return objc_build_selector_expr (loc, sel_seq);
21765 }
21766
21767 /* Parse a list of identifiers.
21768
21769    objc-identifier-list:
21770      identifier
21771      objc-identifier-list , identifier
21772
21773    Returns a TREE_LIST of identifier nodes.  */
21774
21775 static tree
21776 cp_parser_objc_identifier_list (cp_parser* parser)
21777 {
21778   tree identifier;
21779   tree list;
21780   cp_token *sep;
21781
21782   identifier = cp_parser_identifier (parser);
21783   if (identifier == error_mark_node)
21784     return error_mark_node;      
21785
21786   list = build_tree_list (NULL_TREE, identifier);
21787   sep = cp_lexer_peek_token (parser->lexer);
21788
21789   while (sep->type == CPP_COMMA)
21790     {
21791       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21792       identifier = cp_parser_identifier (parser);
21793       if (identifier == error_mark_node)
21794         return list;
21795
21796       list = chainon (list, build_tree_list (NULL_TREE,
21797                                              identifier));
21798       sep = cp_lexer_peek_token (parser->lexer);
21799     }
21800   
21801   return list;
21802 }
21803
21804 /* Parse an Objective-C alias declaration.
21805
21806    objc-alias-declaration:
21807      @compatibility_alias identifier identifier ;
21808
21809    This function registers the alias mapping with the Objective-C front end.
21810    It returns nothing.  */
21811
21812 static void
21813 cp_parser_objc_alias_declaration (cp_parser* parser)
21814 {
21815   tree alias, orig;
21816
21817   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21818   alias = cp_parser_identifier (parser);
21819   orig = cp_parser_identifier (parser);
21820   objc_declare_alias (alias, orig);
21821   cp_parser_consume_semicolon_at_end_of_statement (parser);
21822 }
21823
21824 /* Parse an Objective-C class forward-declaration.
21825
21826    objc-class-declaration:
21827      @class objc-identifier-list ;
21828
21829    The function registers the forward declarations with the Objective-C
21830    front end.  It returns nothing.  */
21831
21832 static void
21833 cp_parser_objc_class_declaration (cp_parser* parser)
21834 {
21835   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21836   objc_declare_class (cp_parser_objc_identifier_list (parser));
21837   cp_parser_consume_semicolon_at_end_of_statement (parser);
21838 }
21839
21840 /* Parse a list of Objective-C protocol references.
21841
21842    objc-protocol-refs-opt:
21843      objc-protocol-refs [opt]
21844
21845    objc-protocol-refs:
21846      < objc-identifier-list >
21847
21848    Returns a TREE_LIST of identifiers, if any.  */
21849
21850 static tree
21851 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21852 {
21853   tree protorefs = NULL_TREE;
21854
21855   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21856     {
21857       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21858       protorefs = cp_parser_objc_identifier_list (parser);
21859       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21860     }
21861
21862   return protorefs;
21863 }
21864
21865 /* Parse a Objective-C visibility specification.  */
21866
21867 static void
21868 cp_parser_objc_visibility_spec (cp_parser* parser)
21869 {
21870   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21871
21872   switch (vis->keyword)
21873     {
21874     case RID_AT_PRIVATE:
21875       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21876       break;
21877     case RID_AT_PROTECTED:
21878       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21879       break;
21880     case RID_AT_PUBLIC:
21881       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21882       break;
21883     case RID_AT_PACKAGE:
21884       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21885       break;
21886     default:
21887       return;
21888     }
21889
21890   /* Eat '@private'/'@protected'/'@public'.  */
21891   cp_lexer_consume_token (parser->lexer);
21892 }
21893
21894 /* Parse an Objective-C method type.  Return 'true' if it is a class
21895    (+) method, and 'false' if it is an instance (-) method.  */
21896
21897 static inline bool
21898 cp_parser_objc_method_type (cp_parser* parser)
21899 {
21900   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21901     return true;
21902   else
21903     return false;
21904 }
21905
21906 /* Parse an Objective-C protocol qualifier.  */
21907
21908 static tree
21909 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21910 {
21911   tree quals = NULL_TREE, node;
21912   cp_token *token = cp_lexer_peek_token (parser->lexer);
21913
21914   node = token->u.value;
21915
21916   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21917          && (node == ridpointers [(int) RID_IN]
21918              || node == ridpointers [(int) RID_OUT]
21919              || node == ridpointers [(int) RID_INOUT]
21920              || node == ridpointers [(int) RID_BYCOPY]
21921              || node == ridpointers [(int) RID_BYREF]
21922              || node == ridpointers [(int) RID_ONEWAY]))
21923     {
21924       quals = tree_cons (NULL_TREE, node, quals);
21925       cp_lexer_consume_token (parser->lexer);
21926       token = cp_lexer_peek_token (parser->lexer);
21927       node = token->u.value;
21928     }
21929
21930   return quals;
21931 }
21932
21933 /* Parse an Objective-C typename.  */
21934
21935 static tree
21936 cp_parser_objc_typename (cp_parser* parser)
21937 {
21938   tree type_name = NULL_TREE;
21939
21940   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21941     {
21942       tree proto_quals, cp_type = NULL_TREE;
21943
21944       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21945       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21946
21947       /* An ObjC type name may consist of just protocol qualifiers, in which
21948          case the type shall default to 'id'.  */
21949       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21950         {
21951           cp_type = cp_parser_type_id (parser);
21952           
21953           /* If the type could not be parsed, an error has already
21954              been produced.  For error recovery, behave as if it had
21955              not been specified, which will use the default type
21956              'id'.  */
21957           if (cp_type == error_mark_node)
21958             {
21959               cp_type = NULL_TREE;
21960               /* We need to skip to the closing parenthesis as
21961                  cp_parser_type_id() does not seem to do it for
21962                  us.  */
21963               cp_parser_skip_to_closing_parenthesis (parser,
21964                                                      /*recovering=*/true,
21965                                                      /*or_comma=*/false,
21966                                                      /*consume_paren=*/false);
21967             }
21968         }
21969
21970       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21971       type_name = build_tree_list (proto_quals, cp_type);
21972     }
21973
21974   return type_name;
21975 }
21976
21977 /* Check to see if TYPE refers to an Objective-C selector name.  */
21978
21979 static bool
21980 cp_parser_objc_selector_p (enum cpp_ttype type)
21981 {
21982   return (type == CPP_NAME || type == CPP_KEYWORD
21983           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21984           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21985           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21986           || type == CPP_XOR || type == CPP_XOR_EQ);
21987 }
21988
21989 /* Parse an Objective-C selector.  */
21990
21991 static tree
21992 cp_parser_objc_selector (cp_parser* parser)
21993 {
21994   cp_token *token = cp_lexer_consume_token (parser->lexer);
21995
21996   if (!cp_parser_objc_selector_p (token->type))
21997     {
21998       error_at (token->location, "invalid Objective-C++ selector name");
21999       return error_mark_node;
22000     }
22001
22002   /* C++ operator names are allowed to appear in ObjC selectors.  */
22003   switch (token->type)
22004     {
22005     case CPP_AND_AND: return get_identifier ("and");
22006     case CPP_AND_EQ: return get_identifier ("and_eq");
22007     case CPP_AND: return get_identifier ("bitand");
22008     case CPP_OR: return get_identifier ("bitor");
22009     case CPP_COMPL: return get_identifier ("compl");
22010     case CPP_NOT: return get_identifier ("not");
22011     case CPP_NOT_EQ: return get_identifier ("not_eq");
22012     case CPP_OR_OR: return get_identifier ("or");
22013     case CPP_OR_EQ: return get_identifier ("or_eq");
22014     case CPP_XOR: return get_identifier ("xor");
22015     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22016     default: return token->u.value;
22017     }
22018 }
22019
22020 /* Parse an Objective-C params list.  */
22021
22022 static tree
22023 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22024 {
22025   tree params = NULL_TREE;
22026   bool maybe_unary_selector_p = true;
22027   cp_token *token = cp_lexer_peek_token (parser->lexer);
22028
22029   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22030     {
22031       tree selector = NULL_TREE, type_name, identifier;
22032       tree parm_attr = NULL_TREE;
22033
22034       if (token->keyword == RID_ATTRIBUTE)
22035         break;
22036
22037       if (token->type != CPP_COLON)
22038         selector = cp_parser_objc_selector (parser);
22039
22040       /* Detect if we have a unary selector.  */
22041       if (maybe_unary_selector_p
22042           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22043         {
22044           params = selector; /* Might be followed by attributes.  */
22045           break;
22046         }
22047
22048       maybe_unary_selector_p = false;
22049       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22050         {
22051           /* Something went quite wrong.  There should be a colon
22052              here, but there is not.  Stop parsing parameters.  */
22053           break;
22054         }
22055       type_name = cp_parser_objc_typename (parser);
22056       /* New ObjC allows attributes on parameters too.  */
22057       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22058         parm_attr = cp_parser_attributes_opt (parser);
22059       identifier = cp_parser_identifier (parser);
22060
22061       params
22062         = chainon (params,
22063                    objc_build_keyword_decl (selector,
22064                                             type_name,
22065                                             identifier,
22066                                             parm_attr));
22067
22068       token = cp_lexer_peek_token (parser->lexer);
22069     }
22070
22071   if (params == NULL_TREE)
22072     {
22073       cp_parser_error (parser, "objective-c++ method declaration is expected");
22074       return error_mark_node;
22075     }
22076
22077   /* We allow tail attributes for the method.  */
22078   if (token->keyword == RID_ATTRIBUTE)
22079     {
22080       *attributes = cp_parser_attributes_opt (parser);
22081       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22082           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22083         return params;
22084       cp_parser_error (parser, 
22085                        "method attributes must be specified at the end");
22086       return error_mark_node;
22087     }
22088
22089   if (params == NULL_TREE)
22090     {
22091       cp_parser_error (parser, "objective-c++ method declaration is expected");
22092       return error_mark_node;
22093     }
22094   return params;
22095 }
22096
22097 /* Parse the non-keyword Objective-C params.  */
22098
22099 static tree
22100 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22101                                        tree* attributes)
22102 {
22103   tree params = make_node (TREE_LIST);
22104   cp_token *token = cp_lexer_peek_token (parser->lexer);
22105   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22106
22107   while (token->type == CPP_COMMA)
22108     {
22109       cp_parameter_declarator *parmdecl;
22110       tree parm;
22111
22112       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22113       token = cp_lexer_peek_token (parser->lexer);
22114
22115       if (token->type == CPP_ELLIPSIS)
22116         {
22117           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22118           *ellipsisp = true;
22119           token = cp_lexer_peek_token (parser->lexer);
22120           break;
22121         }
22122
22123       /* TODO: parse attributes for tail parameters.  */
22124       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22125       parm = grokdeclarator (parmdecl->declarator,
22126                              &parmdecl->decl_specifiers,
22127                              PARM, /*initialized=*/0,
22128                              /*attrlist=*/NULL);
22129
22130       chainon (params, build_tree_list (NULL_TREE, parm));
22131       token = cp_lexer_peek_token (parser->lexer);
22132     }
22133
22134   /* We allow tail attributes for the method.  */
22135   if (token->keyword == RID_ATTRIBUTE)
22136     {
22137       if (*attributes == NULL_TREE)
22138         {
22139           *attributes = cp_parser_attributes_opt (parser);
22140           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22141               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22142             return params;
22143         }
22144       else        
22145         /* We have an error, but parse the attributes, so that we can 
22146            carry on.  */
22147         *attributes = cp_parser_attributes_opt (parser);
22148
22149       cp_parser_error (parser, 
22150                        "method attributes must be specified at the end");
22151       return error_mark_node;
22152     }
22153
22154   return params;
22155 }
22156
22157 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22158
22159 static void
22160 cp_parser_objc_interstitial_code (cp_parser* parser)
22161 {
22162   cp_token *token = cp_lexer_peek_token (parser->lexer);
22163
22164   /* If the next token is `extern' and the following token is a string
22165      literal, then we have a linkage specification.  */
22166   if (token->keyword == RID_EXTERN
22167       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22168     cp_parser_linkage_specification (parser);
22169   /* Handle #pragma, if any.  */
22170   else if (token->type == CPP_PRAGMA)
22171     cp_parser_pragma (parser, pragma_external);
22172   /* Allow stray semicolons.  */
22173   else if (token->type == CPP_SEMICOLON)
22174     cp_lexer_consume_token (parser->lexer);
22175   /* Mark methods as optional or required, when building protocols.  */
22176   else if (token->keyword == RID_AT_OPTIONAL)
22177     {
22178       cp_lexer_consume_token (parser->lexer);
22179       objc_set_method_opt (true);
22180     }
22181   else if (token->keyword == RID_AT_REQUIRED)
22182     {
22183       cp_lexer_consume_token (parser->lexer);
22184       objc_set_method_opt (false);
22185     }
22186   else if (token->keyword == RID_NAMESPACE)
22187     cp_parser_namespace_definition (parser);
22188   /* Other stray characters must generate errors.  */
22189   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22190     {
22191       cp_lexer_consume_token (parser->lexer);
22192       error ("stray %qs between Objective-C++ methods",
22193              token->type == CPP_OPEN_BRACE ? "{" : "}");
22194     }
22195   /* Finally, try to parse a block-declaration, or a function-definition.  */
22196   else
22197     cp_parser_block_declaration (parser, /*statement_p=*/false);
22198 }
22199
22200 /* Parse a method signature.  */
22201
22202 static tree
22203 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22204 {
22205   tree rettype, kwdparms, optparms;
22206   bool ellipsis = false;
22207   bool is_class_method;
22208
22209   is_class_method = cp_parser_objc_method_type (parser);
22210   rettype = cp_parser_objc_typename (parser);
22211   *attributes = NULL_TREE;
22212   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22213   if (kwdparms == error_mark_node)
22214     return error_mark_node;
22215   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22216   if (optparms == error_mark_node)
22217     return error_mark_node;
22218
22219   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22220 }
22221
22222 static bool
22223 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22224 {
22225   tree tattr;  
22226   cp_lexer_save_tokens (parser->lexer);
22227   tattr = cp_parser_attributes_opt (parser);
22228   gcc_assert (tattr) ;
22229   
22230   /* If the attributes are followed by a method introducer, this is not allowed.
22231      Dump the attributes and flag the situation.  */
22232   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22233       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22234     return true;
22235
22236   /* Otherwise, the attributes introduce some interstitial code, possibly so
22237      rewind to allow that check.  */
22238   cp_lexer_rollback_tokens (parser->lexer);
22239   return false;  
22240 }
22241
22242 /* Parse an Objective-C method prototype list.  */
22243
22244 static void
22245 cp_parser_objc_method_prototype_list (cp_parser* parser)
22246 {
22247   cp_token *token = cp_lexer_peek_token (parser->lexer);
22248
22249   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22250     {
22251       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22252         {
22253           tree attributes, sig;
22254           bool is_class_method;
22255           if (token->type == CPP_PLUS)
22256             is_class_method = true;
22257           else
22258             is_class_method = false;
22259           sig = cp_parser_objc_method_signature (parser, &attributes);
22260           if (sig == error_mark_node)
22261             {
22262               cp_parser_skip_to_end_of_block_or_statement (parser);
22263               token = cp_lexer_peek_token (parser->lexer);
22264               continue;
22265             }
22266           objc_add_method_declaration (is_class_method, sig, attributes);
22267           cp_parser_consume_semicolon_at_end_of_statement (parser);
22268         }
22269       else if (token->keyword == RID_AT_PROPERTY)
22270         cp_parser_objc_at_property_declaration (parser);
22271       else if (token->keyword == RID_ATTRIBUTE 
22272                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22273         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22274                     OPT_Wattributes, 
22275                     "prefix attributes are ignored for methods");
22276       else
22277         /* Allow for interspersed non-ObjC++ code.  */
22278         cp_parser_objc_interstitial_code (parser);
22279
22280       token = cp_lexer_peek_token (parser->lexer);
22281     }
22282
22283   if (token->type != CPP_EOF)
22284     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22285   else
22286     cp_parser_error (parser, "expected %<@end%>");
22287
22288   objc_finish_interface ();
22289 }
22290
22291 /* Parse an Objective-C method definition list.  */
22292
22293 static void
22294 cp_parser_objc_method_definition_list (cp_parser* parser)
22295 {
22296   cp_token *token = cp_lexer_peek_token (parser->lexer);
22297
22298   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22299     {
22300       tree meth;
22301
22302       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22303         {
22304           cp_token *ptk;
22305           tree sig, attribute;
22306           bool is_class_method;
22307           if (token->type == CPP_PLUS)
22308             is_class_method = true;
22309           else
22310             is_class_method = false;
22311           push_deferring_access_checks (dk_deferred);
22312           sig = cp_parser_objc_method_signature (parser, &attribute);
22313           if (sig == error_mark_node)
22314             {
22315               cp_parser_skip_to_end_of_block_or_statement (parser);
22316               token = cp_lexer_peek_token (parser->lexer);
22317               continue;
22318             }
22319           objc_start_method_definition (is_class_method, sig, attribute);
22320
22321           /* For historical reasons, we accept an optional semicolon.  */
22322           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22323             cp_lexer_consume_token (parser->lexer);
22324
22325           ptk = cp_lexer_peek_token (parser->lexer);
22326           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22327                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22328             {
22329               perform_deferred_access_checks ();
22330               stop_deferring_access_checks ();
22331               meth = cp_parser_function_definition_after_declarator (parser,
22332                                                                      false);
22333               pop_deferring_access_checks ();
22334               objc_finish_method_definition (meth);
22335             }
22336         }
22337       /* The following case will be removed once @synthesize is
22338          completely implemented.  */
22339       else if (token->keyword == RID_AT_PROPERTY)
22340         cp_parser_objc_at_property_declaration (parser);
22341       else if (token->keyword == RID_AT_SYNTHESIZE)
22342         cp_parser_objc_at_synthesize_declaration (parser);
22343       else if (token->keyword == RID_AT_DYNAMIC)
22344         cp_parser_objc_at_dynamic_declaration (parser);
22345       else if (token->keyword == RID_ATTRIBUTE 
22346                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22347         warning_at (token->location, OPT_Wattributes,
22348                     "prefix attributes are ignored for methods");
22349       else
22350         /* Allow for interspersed non-ObjC++ code.  */
22351         cp_parser_objc_interstitial_code (parser);
22352
22353       token = cp_lexer_peek_token (parser->lexer);
22354     }
22355
22356   if (token->type != CPP_EOF)
22357     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22358   else
22359     cp_parser_error (parser, "expected %<@end%>");
22360
22361   objc_finish_implementation ();
22362 }
22363
22364 /* Parse Objective-C ivars.  */
22365
22366 static void
22367 cp_parser_objc_class_ivars (cp_parser* parser)
22368 {
22369   cp_token *token = cp_lexer_peek_token (parser->lexer);
22370
22371   if (token->type != CPP_OPEN_BRACE)
22372     return;     /* No ivars specified.  */
22373
22374   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22375   token = cp_lexer_peek_token (parser->lexer);
22376
22377   while (token->type != CPP_CLOSE_BRACE 
22378         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22379     {
22380       cp_decl_specifier_seq declspecs;
22381       int decl_class_or_enum_p;
22382       tree prefix_attributes;
22383
22384       cp_parser_objc_visibility_spec (parser);
22385
22386       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22387         break;
22388
22389       cp_parser_decl_specifier_seq (parser,
22390                                     CP_PARSER_FLAGS_OPTIONAL,
22391                                     &declspecs,
22392                                     &decl_class_or_enum_p);
22393
22394       /* auto, register, static, extern, mutable.  */
22395       if (declspecs.storage_class != sc_none)
22396         {
22397           cp_parser_error (parser, "invalid type for instance variable");         
22398           declspecs.storage_class = sc_none;
22399         }
22400
22401       /* __thread.  */
22402       if (declspecs.specs[(int) ds_thread])
22403         {
22404           cp_parser_error (parser, "invalid type for instance variable");
22405           declspecs.specs[(int) ds_thread] = 0;
22406         }
22407       
22408       /* typedef.  */
22409       if (declspecs.specs[(int) ds_typedef])
22410         {
22411           cp_parser_error (parser, "invalid type for instance variable");
22412           declspecs.specs[(int) ds_typedef] = 0;
22413         }
22414
22415       prefix_attributes = declspecs.attributes;
22416       declspecs.attributes = NULL_TREE;
22417
22418       /* Keep going until we hit the `;' at the end of the
22419          declaration.  */
22420       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22421         {
22422           tree width = NULL_TREE, attributes, first_attribute, decl;
22423           cp_declarator *declarator = NULL;
22424           int ctor_dtor_or_conv_p;
22425
22426           /* Check for a (possibly unnamed) bitfield declaration.  */
22427           token = cp_lexer_peek_token (parser->lexer);
22428           if (token->type == CPP_COLON)
22429             goto eat_colon;
22430
22431           if (token->type == CPP_NAME
22432               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22433                   == CPP_COLON))
22434             {
22435               /* Get the name of the bitfield.  */
22436               declarator = make_id_declarator (NULL_TREE,
22437                                                cp_parser_identifier (parser),
22438                                                sfk_none);
22439
22440              eat_colon:
22441               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22442               /* Get the width of the bitfield.  */
22443               width
22444                 = cp_parser_constant_expression (parser,
22445                                                  /*allow_non_constant=*/false,
22446                                                  NULL);
22447             }
22448           else
22449             {
22450               /* Parse the declarator.  */
22451               declarator
22452                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22453                                         &ctor_dtor_or_conv_p,
22454                                         /*parenthesized_p=*/NULL,
22455                                         /*member_p=*/false);
22456             }
22457
22458           /* Look for attributes that apply to the ivar.  */
22459           attributes = cp_parser_attributes_opt (parser);
22460           /* Remember which attributes are prefix attributes and
22461              which are not.  */
22462           first_attribute = attributes;
22463           /* Combine the attributes.  */
22464           attributes = chainon (prefix_attributes, attributes);
22465
22466           if (width)
22467               /* Create the bitfield declaration.  */
22468               decl = grokbitfield (declarator, &declspecs,
22469                                    width,
22470                                    attributes);
22471           else
22472             decl = grokfield (declarator, &declspecs,
22473                               NULL_TREE, /*init_const_expr_p=*/false,
22474                               NULL_TREE, attributes);
22475
22476           /* Add the instance variable.  */
22477           objc_add_instance_variable (decl);
22478
22479           /* Reset PREFIX_ATTRIBUTES.  */
22480           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22481             attributes = TREE_CHAIN (attributes);
22482           if (attributes)
22483             TREE_CHAIN (attributes) = NULL_TREE;
22484
22485           token = cp_lexer_peek_token (parser->lexer);
22486
22487           if (token->type == CPP_COMMA)
22488             {
22489               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22490               continue;
22491             }
22492           break;
22493         }
22494
22495       cp_parser_consume_semicolon_at_end_of_statement (parser);
22496       token = cp_lexer_peek_token (parser->lexer);
22497     }
22498
22499   if (token->keyword == RID_AT_END)
22500     cp_parser_error (parser, "expected %<}%>");
22501
22502   /* Do not consume the RID_AT_END, so it will be read again as terminating
22503      the @interface of @implementation.  */ 
22504   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22505     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22506     
22507   /* For historical reasons, we accept an optional semicolon.  */
22508   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22509     cp_lexer_consume_token (parser->lexer);
22510 }
22511
22512 /* Parse an Objective-C protocol declaration.  */
22513
22514 static void
22515 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22516 {
22517   tree proto, protorefs;
22518   cp_token *tok;
22519
22520   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22521   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22522     {
22523       tok = cp_lexer_peek_token (parser->lexer);
22524       error_at (tok->location, "identifier expected after %<@protocol%>");
22525       goto finish;
22526     }
22527
22528   /* See if we have a forward declaration or a definition.  */
22529   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22530
22531   /* Try a forward declaration first.  */
22532   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22533     {
22534       objc_declare_protocols (cp_parser_objc_identifier_list (parser), 
22535                               attributes);
22536      finish:
22537       cp_parser_consume_semicolon_at_end_of_statement (parser);
22538     }
22539
22540   /* Ok, we got a full-fledged definition (or at least should).  */
22541   else
22542     {
22543       proto = cp_parser_identifier (parser);
22544       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22545       objc_start_protocol (proto, protorefs, attributes);
22546       cp_parser_objc_method_prototype_list (parser);
22547     }
22548 }
22549
22550 /* Parse an Objective-C superclass or category.  */
22551
22552 static void
22553 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22554                                        bool iface_p,
22555                                        tree *super,
22556                                        tree *categ, bool *is_class_extension)
22557 {
22558   cp_token *next = cp_lexer_peek_token (parser->lexer);
22559
22560   *super = *categ = NULL_TREE;
22561   *is_class_extension = false;
22562   if (next->type == CPP_COLON)
22563     {
22564       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22565       *super = cp_parser_identifier (parser);
22566     }
22567   else if (next->type == CPP_OPEN_PAREN)
22568     {
22569       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22570
22571       /* If there is no category name, and this is an @interface, we
22572          have a class extension.  */
22573       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22574         {
22575           *categ = NULL_TREE;
22576           *is_class_extension = true;
22577         }
22578       else
22579         *categ = cp_parser_identifier (parser);
22580
22581       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22582     }
22583 }
22584
22585 /* Parse an Objective-C class interface.  */
22586
22587 static void
22588 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22589 {
22590   tree name, super, categ, protos;
22591   bool is_class_extension;
22592
22593   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22594   name = cp_parser_identifier (parser);
22595   if (name == error_mark_node)
22596     {
22597       /* It's hard to recover because even if valid @interface stuff
22598          is to follow, we can't compile it (or validate it) if we
22599          don't even know which class it refers to.  Let's assume this
22600          was a stray '@interface' token in the stream and skip it.
22601       */
22602       return;
22603     }
22604   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22605                                          &is_class_extension);
22606   protos = cp_parser_objc_protocol_refs_opt (parser);
22607
22608   /* We have either a class or a category on our hands.  */
22609   if (categ || is_class_extension)
22610     objc_start_category_interface (name, categ, protos, attributes);
22611   else
22612     {
22613       objc_start_class_interface (name, super, protos, attributes);
22614       /* Handle instance variable declarations, if any.  */
22615       cp_parser_objc_class_ivars (parser);
22616       objc_continue_interface ();
22617     }
22618
22619   cp_parser_objc_method_prototype_list (parser);
22620 }
22621
22622 /* Parse an Objective-C class implementation.  */
22623
22624 static void
22625 cp_parser_objc_class_implementation (cp_parser* parser)
22626 {
22627   tree name, super, categ;
22628   bool is_class_extension;
22629
22630   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22631   name = cp_parser_identifier (parser);
22632   if (name == error_mark_node)
22633     {
22634       /* It's hard to recover because even if valid @implementation
22635          stuff is to follow, we can't compile it (or validate it) if
22636          we don't even know which class it refers to.  Let's assume
22637          this was a stray '@implementation' token in the stream and
22638          skip it.
22639       */
22640       return;
22641     }
22642   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22643                                          &is_class_extension);
22644
22645   /* We have either a class or a category on our hands.  */
22646   if (categ)
22647     objc_start_category_implementation (name, categ);
22648   else
22649     {
22650       objc_start_class_implementation (name, super);
22651       /* Handle instance variable declarations, if any.  */
22652       cp_parser_objc_class_ivars (parser);
22653       objc_continue_implementation ();
22654     }
22655
22656   cp_parser_objc_method_definition_list (parser);
22657 }
22658
22659 /* Consume the @end token and finish off the implementation.  */
22660
22661 static void
22662 cp_parser_objc_end_implementation (cp_parser* parser)
22663 {
22664   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22665   objc_finish_implementation ();
22666 }
22667
22668 /* Parse an Objective-C declaration.  */
22669
22670 static void
22671 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22672 {
22673   /* Try to figure out what kind of declaration is present.  */
22674   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22675
22676   if (attributes)
22677     switch (kwd->keyword)
22678       {
22679         case RID_AT_ALIAS:
22680         case RID_AT_CLASS:
22681         case RID_AT_END:
22682           error_at (kwd->location, "attributes may not be specified before"
22683                     " the %<@%D%> Objective-C++ keyword",
22684                     kwd->u.value);
22685           attributes = NULL;
22686           break;
22687         case RID_AT_IMPLEMENTATION:
22688           warning_at (kwd->location, OPT_Wattributes,
22689                       "prefix attributes are ignored before %<@%D%>",
22690                       kwd->u.value);
22691           attributes = NULL;
22692         default:
22693           break;
22694       }
22695
22696   switch (kwd->keyword)
22697     {
22698     case RID_AT_ALIAS:
22699       cp_parser_objc_alias_declaration (parser);
22700       break;
22701     case RID_AT_CLASS:
22702       cp_parser_objc_class_declaration (parser);
22703       break;
22704     case RID_AT_PROTOCOL:
22705       cp_parser_objc_protocol_declaration (parser, attributes);
22706       break;
22707     case RID_AT_INTERFACE:
22708       cp_parser_objc_class_interface (parser, attributes);
22709       break;
22710     case RID_AT_IMPLEMENTATION:
22711       cp_parser_objc_class_implementation (parser);
22712       break;
22713     case RID_AT_END:
22714       cp_parser_objc_end_implementation (parser);
22715       break;
22716     default:
22717       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22718                 kwd->u.value);
22719       cp_parser_skip_to_end_of_block_or_statement (parser);
22720     }
22721 }
22722
22723 /* Parse an Objective-C try-catch-finally statement.
22724
22725    objc-try-catch-finally-stmt:
22726      @try compound-statement objc-catch-clause-seq [opt]
22727        objc-finally-clause [opt]
22728
22729    objc-catch-clause-seq:
22730      objc-catch-clause objc-catch-clause-seq [opt]
22731
22732    objc-catch-clause:
22733      @catch ( objc-exception-declaration ) compound-statement
22734
22735    objc-finally-clause:
22736      @finally compound-statement
22737
22738    objc-exception-declaration:
22739      parameter-declaration
22740      '...'
22741
22742    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22743
22744    Returns NULL_TREE.
22745
22746    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22747    for C.  Keep them in sync.  */   
22748
22749 static tree
22750 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22751 {
22752   location_t location;
22753   tree stmt;
22754
22755   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22756   location = cp_lexer_peek_token (parser->lexer)->location;
22757   objc_maybe_warn_exceptions (location);
22758   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22759      node, lest it get absorbed into the surrounding block.  */
22760   stmt = push_stmt_list ();
22761   cp_parser_compound_statement (parser, NULL, false);
22762   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22763
22764   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22765     {
22766       cp_parameter_declarator *parm;
22767       tree parameter_declaration = error_mark_node;
22768       bool seen_open_paren = false;
22769
22770       cp_lexer_consume_token (parser->lexer);
22771       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22772         seen_open_paren = true;
22773       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22774         {
22775           /* We have "@catch (...)" (where the '...' are literally
22776              what is in the code).  Skip the '...'.
22777              parameter_declaration is set to NULL_TREE, and
22778              objc_being_catch_clauses() knows that that means
22779              '...'.  */
22780           cp_lexer_consume_token (parser->lexer);
22781           parameter_declaration = NULL_TREE;
22782         }
22783       else
22784         {
22785           /* We have "@catch (NSException *exception)" or something
22786              like that.  Parse the parameter declaration.  */
22787           parm = cp_parser_parameter_declaration (parser, false, NULL);
22788           if (parm == NULL)
22789             parameter_declaration = error_mark_node;
22790           else
22791             parameter_declaration = grokdeclarator (parm->declarator,
22792                                                     &parm->decl_specifiers,
22793                                                     PARM, /*initialized=*/0,
22794                                                     /*attrlist=*/NULL);
22795         }
22796       if (seen_open_paren)
22797         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22798       else
22799         {
22800           /* If there was no open parenthesis, we are recovering from
22801              an error, and we are trying to figure out what mistake
22802              the user has made.  */
22803
22804           /* If there is an immediate closing parenthesis, the user
22805              probably forgot the opening one (ie, they typed "@catch
22806              NSException *e)".  Parse the closing parenthesis and keep
22807              going.  */
22808           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22809             cp_lexer_consume_token (parser->lexer);
22810           
22811           /* If these is no immediate closing parenthesis, the user
22812              probably doesn't know that parenthesis are required at
22813              all (ie, they typed "@catch NSException *e").  So, just
22814              forget about the closing parenthesis and keep going.  */
22815         }
22816       objc_begin_catch_clause (parameter_declaration);
22817       cp_parser_compound_statement (parser, NULL, false);
22818       objc_finish_catch_clause ();
22819     }
22820   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22821     {
22822       cp_lexer_consume_token (parser->lexer);
22823       location = cp_lexer_peek_token (parser->lexer)->location;
22824       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22825          node, lest it get absorbed into the surrounding block.  */
22826       stmt = push_stmt_list ();
22827       cp_parser_compound_statement (parser, NULL, false);
22828       objc_build_finally_clause (location, pop_stmt_list (stmt));
22829     }
22830
22831   return objc_finish_try_stmt ();
22832 }
22833
22834 /* Parse an Objective-C synchronized statement.
22835
22836    objc-synchronized-stmt:
22837      @synchronized ( expression ) compound-statement
22838
22839    Returns NULL_TREE.  */
22840
22841 static tree
22842 cp_parser_objc_synchronized_statement (cp_parser *parser)
22843 {
22844   location_t location;
22845   tree lock, stmt;
22846
22847   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22848
22849   location = cp_lexer_peek_token (parser->lexer)->location;
22850   objc_maybe_warn_exceptions (location);
22851   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22852   lock = cp_parser_expression (parser, false, NULL);
22853   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22854
22855   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22856      node, lest it get absorbed into the surrounding block.  */
22857   stmt = push_stmt_list ();
22858   cp_parser_compound_statement (parser, NULL, false);
22859
22860   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22861 }
22862
22863 /* Parse an Objective-C throw statement.
22864
22865    objc-throw-stmt:
22866      @throw assignment-expression [opt] ;
22867
22868    Returns a constructed '@throw' statement.  */
22869
22870 static tree
22871 cp_parser_objc_throw_statement (cp_parser *parser)
22872 {
22873   tree expr = NULL_TREE;
22874   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22875
22876   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22877
22878   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22879     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22880
22881   cp_parser_consume_semicolon_at_end_of_statement (parser);
22882
22883   return objc_build_throw_stmt (loc, expr);
22884 }
22885
22886 /* Parse an Objective-C statement.  */
22887
22888 static tree
22889 cp_parser_objc_statement (cp_parser * parser)
22890 {
22891   /* Try to figure out what kind of declaration is present.  */
22892   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22893
22894   switch (kwd->keyword)
22895     {
22896     case RID_AT_TRY:
22897       return cp_parser_objc_try_catch_finally_statement (parser);
22898     case RID_AT_SYNCHRONIZED:
22899       return cp_parser_objc_synchronized_statement (parser);
22900     case RID_AT_THROW:
22901       return cp_parser_objc_throw_statement (parser);
22902     default:
22903       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22904                kwd->u.value);
22905       cp_parser_skip_to_end_of_block_or_statement (parser);
22906     }
22907
22908   return error_mark_node;
22909 }
22910
22911 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22912    look ahead to see if an objc keyword follows the attributes.  This
22913    is to detect the use of prefix attributes on ObjC @interface and 
22914    @protocol.  */
22915
22916 static bool
22917 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22918 {
22919   cp_lexer_save_tokens (parser->lexer);
22920   *attrib = cp_parser_attributes_opt (parser);
22921   gcc_assert (*attrib);
22922   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22923     {
22924       cp_lexer_commit_tokens (parser->lexer);
22925       return true;
22926     }
22927   cp_lexer_rollback_tokens (parser->lexer);
22928   return false;  
22929 }
22930
22931 /* This routine is a minimal replacement for
22932    c_parser_struct_declaration () used when parsing the list of
22933    types/names or ObjC++ properties.  For example, when parsing the
22934    code
22935
22936    @property (readonly) int a, b, c;
22937
22938    this function is responsible for parsing "int a, int b, int c" and
22939    returning the declarations as CHAIN of DECLs.
22940
22941    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22942    similar parsing.  */
22943 static tree
22944 cp_parser_objc_struct_declaration (cp_parser *parser)
22945 {
22946   tree decls = NULL_TREE;
22947   cp_decl_specifier_seq declspecs;
22948   int decl_class_or_enum_p;
22949   tree prefix_attributes;
22950
22951   cp_parser_decl_specifier_seq (parser,
22952                                 CP_PARSER_FLAGS_NONE,
22953                                 &declspecs,
22954                                 &decl_class_or_enum_p);
22955
22956   if (declspecs.type == error_mark_node)
22957     return error_mark_node;
22958
22959   /* auto, register, static, extern, mutable.  */
22960   if (declspecs.storage_class != sc_none)
22961     {
22962       cp_parser_error (parser, "invalid type for property");
22963       declspecs.storage_class = sc_none;
22964     }
22965   
22966   /* __thread.  */
22967   if (declspecs.specs[(int) ds_thread])
22968     {
22969       cp_parser_error (parser, "invalid type for property");
22970       declspecs.specs[(int) ds_thread] = 0;
22971     }
22972   
22973   /* typedef.  */
22974   if (declspecs.specs[(int) ds_typedef])
22975     {
22976       cp_parser_error (parser, "invalid type for property");
22977       declspecs.specs[(int) ds_typedef] = 0;
22978     }
22979
22980   prefix_attributes = declspecs.attributes;
22981   declspecs.attributes = NULL_TREE;
22982
22983   /* Keep going until we hit the `;' at the end of the declaration. */
22984   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22985     {
22986       tree attributes, first_attribute, decl;
22987       cp_declarator *declarator;
22988       cp_token *token;
22989
22990       /* Parse the declarator.  */
22991       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22992                                          NULL, NULL, false);
22993
22994       /* Look for attributes that apply to the ivar.  */
22995       attributes = cp_parser_attributes_opt (parser);
22996       /* Remember which attributes are prefix attributes and
22997          which are not.  */
22998       first_attribute = attributes;
22999       /* Combine the attributes.  */
23000       attributes = chainon (prefix_attributes, attributes);
23001       
23002       decl = grokfield (declarator, &declspecs,
23003                         NULL_TREE, /*init_const_expr_p=*/false,
23004                         NULL_TREE, attributes);
23005
23006       if (decl == error_mark_node || decl == NULL_TREE)
23007         return error_mark_node;
23008       
23009       /* Reset PREFIX_ATTRIBUTES.  */
23010       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23011         attributes = TREE_CHAIN (attributes);
23012       if (attributes)
23013         TREE_CHAIN (attributes) = NULL_TREE;
23014
23015       DECL_CHAIN (decl) = decls;
23016       decls = decl;
23017
23018       token = cp_lexer_peek_token (parser->lexer);
23019       if (token->type == CPP_COMMA)
23020         {
23021           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23022           continue;
23023         }
23024       else
23025         break;
23026     }
23027   return decls;
23028 }
23029
23030 /* Parse an Objective-C @property declaration.  The syntax is:
23031
23032    objc-property-declaration:
23033      '@property' objc-property-attributes[opt] struct-declaration ;
23034
23035    objc-property-attributes:
23036     '(' objc-property-attribute-list ')'
23037
23038    objc-property-attribute-list:
23039      objc-property-attribute
23040      objc-property-attribute-list, objc-property-attribute
23041
23042    objc-property-attribute
23043      'getter' = identifier
23044      'setter' = identifier
23045      'readonly'
23046      'readwrite'
23047      'assign'
23048      'retain'
23049      'copy'
23050      'nonatomic'
23051
23052   For example:
23053     @property NSString *name;
23054     @property (readonly) id object;
23055     @property (retain, nonatomic, getter=getTheName) id name;
23056     @property int a, b, c;
23057
23058    PS: This function is identical to
23059    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23060 static void 
23061 cp_parser_objc_at_property_declaration (cp_parser *parser)
23062 {
23063   /* The following variables hold the attributes of the properties as
23064      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23065      seen.  When we see an attribute, we set them to 'true' (if they
23066      are boolean properties) or to the identifier (if they have an
23067      argument, ie, for getter and setter).  Note that here we only
23068      parse the list of attributes, check the syntax and accumulate the
23069      attributes that we find.  objc_add_property_declaration() will
23070      then process the information.  */
23071   bool property_assign = false;
23072   bool property_copy = false;
23073   tree property_getter_ident = NULL_TREE;
23074   bool property_nonatomic = false;
23075   bool property_readonly = false;
23076   bool property_readwrite = false;
23077   bool property_retain = false;
23078   tree property_setter_ident = NULL_TREE;
23079
23080   /* 'properties' is the list of properties that we read.  Usually a
23081      single one, but maybe more (eg, in "@property int a, b, c;" there
23082      are three).  */
23083   tree properties;
23084   location_t loc;
23085
23086   loc = cp_lexer_peek_token (parser->lexer)->location;
23087
23088   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23089
23090   /* Parse the optional attribute list...  */
23091   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23092     {
23093       /* Eat the '('.  */
23094       cp_lexer_consume_token (parser->lexer);
23095
23096       while (true)
23097         {
23098           bool syntax_error = false;
23099           cp_token *token = cp_lexer_peek_token (parser->lexer);
23100           enum rid keyword;
23101
23102           if (token->type != CPP_NAME)
23103             {
23104               cp_parser_error (parser, "expected identifier");
23105               break;
23106             }
23107           keyword = C_RID_CODE (token->u.value);
23108           cp_lexer_consume_token (parser->lexer);
23109           switch (keyword)
23110             {
23111             case RID_ASSIGN:    property_assign = true;    break;
23112             case RID_COPY:      property_copy = true;      break;
23113             case RID_NONATOMIC: property_nonatomic = true; break;
23114             case RID_READONLY:  property_readonly = true;  break;
23115             case RID_READWRITE: property_readwrite = true; break;
23116             case RID_RETAIN:    property_retain = true;    break;
23117
23118             case RID_GETTER:
23119             case RID_SETTER:
23120               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23121                 {
23122                   if (keyword == RID_GETTER)
23123                     cp_parser_error (parser,
23124                                      "missing %<=%> (after %<getter%> attribute)");
23125                   else
23126                     cp_parser_error (parser,
23127                                      "missing %<=%> (after %<setter%> attribute)");
23128                   syntax_error = true;
23129                   break;
23130                 }
23131               cp_lexer_consume_token (parser->lexer); /* eat the = */
23132               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23133                 {
23134                   cp_parser_error (parser, "expected identifier");
23135                   syntax_error = true;
23136                   break;
23137                 }
23138               if (keyword == RID_SETTER)
23139                 {
23140                   if (property_setter_ident != NULL_TREE)
23141                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23142                   else
23143                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23144                   cp_lexer_consume_token (parser->lexer);
23145                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23146                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23147                   else
23148                     cp_lexer_consume_token (parser->lexer);
23149                 }
23150               else
23151                 {
23152                   if (property_getter_ident != NULL_TREE)
23153                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23154                   else
23155                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23156                   cp_lexer_consume_token (parser->lexer);
23157                 }
23158               break;
23159             default:
23160               cp_parser_error (parser, "unknown property attribute");
23161               syntax_error = true;
23162               break;
23163             }
23164
23165           if (syntax_error)
23166             break;
23167
23168           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23169             cp_lexer_consume_token (parser->lexer);
23170           else
23171             break;
23172         }
23173
23174       /* FIXME: "@property (setter, assign);" will generate a spurious
23175          "error: expected â€˜)’ before â€˜,’ token".  This is because
23176          cp_parser_require, unlike the C counterpart, will produce an
23177          error even if we are in error recovery.  */
23178       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23179         {
23180           cp_parser_skip_to_closing_parenthesis (parser,
23181                                                  /*recovering=*/true,
23182                                                  /*or_comma=*/false,
23183                                                  /*consume_paren=*/true);
23184         }
23185     }
23186
23187   /* ... and the property declaration(s).  */
23188   properties = cp_parser_objc_struct_declaration (parser);
23189
23190   if (properties == error_mark_node)
23191     {
23192       cp_parser_skip_to_end_of_statement (parser);
23193       /* If the next token is now a `;', consume it.  */
23194       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23195         cp_lexer_consume_token (parser->lexer);
23196       return;
23197     }
23198
23199   if (properties == NULL_TREE)
23200     cp_parser_error (parser, "expected identifier");
23201   else
23202     {
23203       /* Comma-separated properties are chained together in
23204          reverse order; add them one by one.  */
23205       properties = nreverse (properties);
23206       
23207       for (; properties; properties = TREE_CHAIN (properties))
23208         objc_add_property_declaration (loc, copy_node (properties),
23209                                        property_readonly, property_readwrite,
23210                                        property_assign, property_retain,
23211                                        property_copy, property_nonatomic,
23212                                        property_getter_ident, property_setter_ident);
23213     }
23214   
23215   cp_parser_consume_semicolon_at_end_of_statement (parser);
23216 }
23217
23218 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23219
23220    objc-synthesize-declaration:
23221      @synthesize objc-synthesize-identifier-list ;
23222
23223    objc-synthesize-identifier-list:
23224      objc-synthesize-identifier
23225      objc-synthesize-identifier-list, objc-synthesize-identifier
23226
23227    objc-synthesize-identifier
23228      identifier
23229      identifier = identifier
23230
23231   For example:
23232     @synthesize MyProperty;
23233     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23234
23235   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23236   for C.  Keep them in sync.
23237 */
23238 static void 
23239 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23240 {
23241   tree list = NULL_TREE;
23242   location_t loc;
23243   loc = cp_lexer_peek_token (parser->lexer)->location;
23244
23245   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23246   while (true)
23247     {
23248       tree property, ivar;
23249       property = cp_parser_identifier (parser);
23250       if (property == error_mark_node)
23251         {
23252           cp_parser_consume_semicolon_at_end_of_statement (parser);
23253           return;
23254         }
23255       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23256         {
23257           cp_lexer_consume_token (parser->lexer);
23258           ivar = cp_parser_identifier (parser);
23259           if (ivar == error_mark_node)
23260             {
23261               cp_parser_consume_semicolon_at_end_of_statement (parser);
23262               return;
23263             }
23264         }
23265       else
23266         ivar = NULL_TREE;
23267       list = chainon (list, build_tree_list (ivar, property));
23268       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23269         cp_lexer_consume_token (parser->lexer);
23270       else
23271         break;
23272     }
23273   cp_parser_consume_semicolon_at_end_of_statement (parser);
23274   objc_add_synthesize_declaration (loc, list);
23275 }
23276
23277 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23278
23279    objc-dynamic-declaration:
23280      @dynamic identifier-list ;
23281
23282    For example:
23283      @dynamic MyProperty;
23284      @dynamic MyProperty, AnotherProperty;
23285
23286   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23287   for C.  Keep them in sync.
23288 */
23289 static void 
23290 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23291 {
23292   tree list = NULL_TREE;
23293   location_t loc;
23294   loc = cp_lexer_peek_token (parser->lexer)->location;
23295
23296   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23297   while (true)
23298     {
23299       tree property;
23300       property = cp_parser_identifier (parser);
23301       if (property == error_mark_node)
23302         {
23303           cp_parser_consume_semicolon_at_end_of_statement (parser);
23304           return;
23305         }
23306       list = chainon (list, build_tree_list (NULL, property));
23307       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23308         cp_lexer_consume_token (parser->lexer);
23309       else
23310         break;
23311     }
23312   cp_parser_consume_semicolon_at_end_of_statement (parser);
23313   objc_add_dynamic_declaration (loc, list);
23314 }
23315
23316 \f
23317 /* OpenMP 2.5 parsing routines.  */
23318
23319 /* Returns name of the next clause.
23320    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23321    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23322    returned and the token is consumed.  */
23323
23324 static pragma_omp_clause
23325 cp_parser_omp_clause_name (cp_parser *parser)
23326 {
23327   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23328
23329   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23330     result = PRAGMA_OMP_CLAUSE_IF;
23331   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23332     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23333   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23334     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23335   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23336     {
23337       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23338       const char *p = IDENTIFIER_POINTER (id);
23339
23340       switch (p[0])
23341         {
23342         case 'c':
23343           if (!strcmp ("collapse", p))
23344             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23345           else if (!strcmp ("copyin", p))
23346             result = PRAGMA_OMP_CLAUSE_COPYIN;
23347           else if (!strcmp ("copyprivate", p))
23348             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23349           break;
23350         case 'f':
23351           if (!strcmp ("firstprivate", p))
23352             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23353           break;
23354         case 'l':
23355           if (!strcmp ("lastprivate", p))
23356             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23357           break;
23358         case 'n':
23359           if (!strcmp ("nowait", p))
23360             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23361           else if (!strcmp ("num_threads", p))
23362             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23363           break;
23364         case 'o':
23365           if (!strcmp ("ordered", p))
23366             result = PRAGMA_OMP_CLAUSE_ORDERED;
23367           break;
23368         case 'r':
23369           if (!strcmp ("reduction", p))
23370             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23371           break;
23372         case 's':
23373           if (!strcmp ("schedule", p))
23374             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23375           else if (!strcmp ("shared", p))
23376             result = PRAGMA_OMP_CLAUSE_SHARED;
23377           break;
23378         case 'u':
23379           if (!strcmp ("untied", p))
23380             result = PRAGMA_OMP_CLAUSE_UNTIED;
23381           break;
23382         }
23383     }
23384
23385   if (result != PRAGMA_OMP_CLAUSE_NONE)
23386     cp_lexer_consume_token (parser->lexer);
23387
23388   return result;
23389 }
23390
23391 /* Validate that a clause of the given type does not already exist.  */
23392
23393 static void
23394 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23395                            const char *name, location_t location)
23396 {
23397   tree c;
23398
23399   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23400     if (OMP_CLAUSE_CODE (c) == code)
23401       {
23402         error_at (location, "too many %qs clauses", name);
23403         break;
23404       }
23405 }
23406
23407 /* OpenMP 2.5:
23408    variable-list:
23409      identifier
23410      variable-list , identifier
23411
23412    In addition, we match a closing parenthesis.  An opening parenthesis
23413    will have been consumed by the caller.
23414
23415    If KIND is nonzero, create the appropriate node and install the decl
23416    in OMP_CLAUSE_DECL and add the node to the head of the list.
23417
23418    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23419    return the list created.  */
23420
23421 static tree
23422 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23423                                 tree list)
23424 {
23425   cp_token *token;
23426   while (1)
23427     {
23428       tree name, decl;
23429
23430       token = cp_lexer_peek_token (parser->lexer);
23431       name = cp_parser_id_expression (parser, /*template_p=*/false,
23432                                       /*check_dependency_p=*/true,
23433                                       /*template_p=*/NULL,
23434                                       /*declarator_p=*/false,
23435                                       /*optional_p=*/false);
23436       if (name == error_mark_node)
23437         goto skip_comma;
23438
23439       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23440       if (decl == error_mark_node)
23441         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23442                                      token->location);
23443       else if (kind != 0)
23444         {
23445           tree u = build_omp_clause (token->location, kind);
23446           OMP_CLAUSE_DECL (u) = decl;
23447           OMP_CLAUSE_CHAIN (u) = list;
23448           list = u;
23449         }
23450       else
23451         list = tree_cons (decl, NULL_TREE, list);
23452
23453     get_comma:
23454       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23455         break;
23456       cp_lexer_consume_token (parser->lexer);
23457     }
23458
23459   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23460     {
23461       int ending;
23462
23463       /* Try to resync to an unnested comma.  Copied from
23464          cp_parser_parenthesized_expression_list.  */
23465     skip_comma:
23466       ending = cp_parser_skip_to_closing_parenthesis (parser,
23467                                                       /*recovering=*/true,
23468                                                       /*or_comma=*/true,
23469                                                       /*consume_paren=*/true);
23470       if (ending < 0)
23471         goto get_comma;
23472     }
23473
23474   return list;
23475 }
23476
23477 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23478    common case for omp clauses.  */
23479
23480 static tree
23481 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23482 {
23483   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23484     return cp_parser_omp_var_list_no_open (parser, kind, list);
23485   return list;
23486 }
23487
23488 /* OpenMP 3.0:
23489    collapse ( constant-expression ) */
23490
23491 static tree
23492 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23493 {
23494   tree c, num;
23495   location_t loc;
23496   HOST_WIDE_INT n;
23497
23498   loc = cp_lexer_peek_token (parser->lexer)->location;
23499   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23500     return list;
23501
23502   num = cp_parser_constant_expression (parser, false, NULL);
23503
23504   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23505     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23506                                            /*or_comma=*/false,
23507                                            /*consume_paren=*/true);
23508
23509   if (num == error_mark_node)
23510     return list;
23511   num = fold_non_dependent_expr (num);
23512   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23513       || !host_integerp (num, 0)
23514       || (n = tree_low_cst (num, 0)) <= 0
23515       || (int) n != n)
23516     {
23517       error_at (loc, "collapse argument needs positive constant integer expression");
23518       return list;
23519     }
23520
23521   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23522   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23523   OMP_CLAUSE_CHAIN (c) = list;
23524   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23525
23526   return c;
23527 }
23528
23529 /* OpenMP 2.5:
23530    default ( shared | none ) */
23531
23532 static tree
23533 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23534 {
23535   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23536   tree c;
23537
23538   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23539     return list;
23540   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23541     {
23542       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23543       const char *p = IDENTIFIER_POINTER (id);
23544
23545       switch (p[0])
23546         {
23547         case 'n':
23548           if (strcmp ("none", p) != 0)
23549             goto invalid_kind;
23550           kind = OMP_CLAUSE_DEFAULT_NONE;
23551           break;
23552
23553         case 's':
23554           if (strcmp ("shared", p) != 0)
23555             goto invalid_kind;
23556           kind = OMP_CLAUSE_DEFAULT_SHARED;
23557           break;
23558
23559         default:
23560           goto invalid_kind;
23561         }
23562
23563       cp_lexer_consume_token (parser->lexer);
23564     }
23565   else
23566     {
23567     invalid_kind:
23568       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23569     }
23570
23571   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23572     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23573                                            /*or_comma=*/false,
23574                                            /*consume_paren=*/true);
23575
23576   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23577     return list;
23578
23579   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23580   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23581   OMP_CLAUSE_CHAIN (c) = list;
23582   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23583
23584   return c;
23585 }
23586
23587 /* OpenMP 2.5:
23588    if ( expression ) */
23589
23590 static tree
23591 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23592 {
23593   tree t, c;
23594
23595   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23596     return list;
23597
23598   t = cp_parser_condition (parser);
23599
23600   if (t == error_mark_node
23601       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23602     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23603                                            /*or_comma=*/false,
23604                                            /*consume_paren=*/true);
23605
23606   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23607
23608   c = build_omp_clause (location, OMP_CLAUSE_IF);
23609   OMP_CLAUSE_IF_EXPR (c) = t;
23610   OMP_CLAUSE_CHAIN (c) = list;
23611
23612   return c;
23613 }
23614
23615 /* OpenMP 2.5:
23616    nowait */
23617
23618 static tree
23619 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23620                              tree list, location_t location)
23621 {
23622   tree c;
23623
23624   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23625
23626   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23627   OMP_CLAUSE_CHAIN (c) = list;
23628   return c;
23629 }
23630
23631 /* OpenMP 2.5:
23632    num_threads ( expression ) */
23633
23634 static tree
23635 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23636                                   location_t location)
23637 {
23638   tree t, c;
23639
23640   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23641     return list;
23642
23643   t = cp_parser_expression (parser, false, NULL);
23644
23645   if (t == error_mark_node
23646       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23647     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23648                                            /*or_comma=*/false,
23649                                            /*consume_paren=*/true);
23650
23651   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23652                              "num_threads", location);
23653
23654   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23655   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23656   OMP_CLAUSE_CHAIN (c) = list;
23657
23658   return c;
23659 }
23660
23661 /* OpenMP 2.5:
23662    ordered */
23663
23664 static tree
23665 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23666                               tree list, location_t location)
23667 {
23668   tree c;
23669
23670   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23671                              "ordered", location);
23672
23673   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23674   OMP_CLAUSE_CHAIN (c) = list;
23675   return c;
23676 }
23677
23678 /* OpenMP 2.5:
23679    reduction ( reduction-operator : variable-list )
23680
23681    reduction-operator:
23682      One of: + * - & ^ | && || */
23683
23684 static tree
23685 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23686 {
23687   enum tree_code code;
23688   tree nlist, c;
23689
23690   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23691     return list;
23692
23693   switch (cp_lexer_peek_token (parser->lexer)->type)
23694     {
23695     case CPP_PLUS:
23696       code = PLUS_EXPR;
23697       break;
23698     case CPP_MULT:
23699       code = MULT_EXPR;
23700       break;
23701     case CPP_MINUS:
23702       code = MINUS_EXPR;
23703       break;
23704     case CPP_AND:
23705       code = BIT_AND_EXPR;
23706       break;
23707     case CPP_XOR:
23708       code = BIT_XOR_EXPR;
23709       break;
23710     case CPP_OR:
23711       code = BIT_IOR_EXPR;
23712       break;
23713     case CPP_AND_AND:
23714       code = TRUTH_ANDIF_EXPR;
23715       break;
23716     case CPP_OR_OR:
23717       code = TRUTH_ORIF_EXPR;
23718       break;
23719     default:
23720       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23721                                "%<|%>, %<&&%>, or %<||%>");
23722     resync_fail:
23723       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23724                                              /*or_comma=*/false,
23725                                              /*consume_paren=*/true);
23726       return list;
23727     }
23728   cp_lexer_consume_token (parser->lexer);
23729
23730   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23731     goto resync_fail;
23732
23733   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23734   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23735     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23736
23737   return nlist;
23738 }
23739
23740 /* OpenMP 2.5:
23741    schedule ( schedule-kind )
23742    schedule ( schedule-kind , expression )
23743
23744    schedule-kind:
23745      static | dynamic | guided | runtime | auto  */
23746
23747 static tree
23748 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23749 {
23750   tree c, t;
23751
23752   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23753     return list;
23754
23755   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23756
23757   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23758     {
23759       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23760       const char *p = IDENTIFIER_POINTER (id);
23761
23762       switch (p[0])
23763         {
23764         case 'd':
23765           if (strcmp ("dynamic", p) != 0)
23766             goto invalid_kind;
23767           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23768           break;
23769
23770         case 'g':
23771           if (strcmp ("guided", p) != 0)
23772             goto invalid_kind;
23773           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23774           break;
23775
23776         case 'r':
23777           if (strcmp ("runtime", p) != 0)
23778             goto invalid_kind;
23779           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23780           break;
23781
23782         default:
23783           goto invalid_kind;
23784         }
23785     }
23786   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23787     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23788   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23789     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23790   else
23791     goto invalid_kind;
23792   cp_lexer_consume_token (parser->lexer);
23793
23794   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23795     {
23796       cp_token *token;
23797       cp_lexer_consume_token (parser->lexer);
23798
23799       token = cp_lexer_peek_token (parser->lexer);
23800       t = cp_parser_assignment_expression (parser, false, NULL);
23801
23802       if (t == error_mark_node)
23803         goto resync_fail;
23804       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23805         error_at (token->location, "schedule %<runtime%> does not take "
23806                   "a %<chunk_size%> parameter");
23807       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23808         error_at (token->location, "schedule %<auto%> does not take "
23809                   "a %<chunk_size%> parameter");
23810       else
23811         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23812
23813       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23814         goto resync_fail;
23815     }
23816   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23817     goto resync_fail;
23818
23819   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23820   OMP_CLAUSE_CHAIN (c) = list;
23821   return c;
23822
23823  invalid_kind:
23824   cp_parser_error (parser, "invalid schedule kind");
23825  resync_fail:
23826   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23827                                          /*or_comma=*/false,
23828                                          /*consume_paren=*/true);
23829   return list;
23830 }
23831
23832 /* OpenMP 3.0:
23833    untied */
23834
23835 static tree
23836 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23837                              tree list, location_t location)
23838 {
23839   tree c;
23840
23841   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23842
23843   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23844   OMP_CLAUSE_CHAIN (c) = list;
23845   return c;
23846 }
23847
23848 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23849    is a bitmask in MASK.  Return the list of clauses found; the result
23850    of clause default goes in *pdefault.  */
23851
23852 static tree
23853 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23854                            const char *where, cp_token *pragma_tok)
23855 {
23856   tree clauses = NULL;
23857   bool first = true;
23858   cp_token *token = NULL;
23859
23860   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23861     {
23862       pragma_omp_clause c_kind;
23863       const char *c_name;
23864       tree prev = clauses;
23865
23866       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23867         cp_lexer_consume_token (parser->lexer);
23868
23869       token = cp_lexer_peek_token (parser->lexer);
23870       c_kind = cp_parser_omp_clause_name (parser);
23871       first = false;
23872
23873       switch (c_kind)
23874         {
23875         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23876           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23877                                                    token->location);
23878           c_name = "collapse";
23879           break;
23880         case PRAGMA_OMP_CLAUSE_COPYIN:
23881           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23882           c_name = "copyin";
23883           break;
23884         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23885           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23886                                             clauses);
23887           c_name = "copyprivate";
23888           break;
23889         case PRAGMA_OMP_CLAUSE_DEFAULT:
23890           clauses = cp_parser_omp_clause_default (parser, clauses,
23891                                                   token->location);
23892           c_name = "default";
23893           break;
23894         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23895           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23896                                             clauses);
23897           c_name = "firstprivate";
23898           break;
23899         case PRAGMA_OMP_CLAUSE_IF:
23900           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23901           c_name = "if";
23902           break;
23903         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23904           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23905                                             clauses);
23906           c_name = "lastprivate";
23907           break;
23908         case PRAGMA_OMP_CLAUSE_NOWAIT:
23909           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23910           c_name = "nowait";
23911           break;
23912         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23913           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23914                                                       token->location);
23915           c_name = "num_threads";
23916           break;
23917         case PRAGMA_OMP_CLAUSE_ORDERED:
23918           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23919                                                   token->location);
23920           c_name = "ordered";
23921           break;
23922         case PRAGMA_OMP_CLAUSE_PRIVATE:
23923           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23924                                             clauses);
23925           c_name = "private";
23926           break;
23927         case PRAGMA_OMP_CLAUSE_REDUCTION:
23928           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23929           c_name = "reduction";
23930           break;
23931         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23932           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23933                                                    token->location);
23934           c_name = "schedule";
23935           break;
23936         case PRAGMA_OMP_CLAUSE_SHARED:
23937           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23938                                             clauses);
23939           c_name = "shared";
23940           break;
23941         case PRAGMA_OMP_CLAUSE_UNTIED:
23942           clauses = cp_parser_omp_clause_untied (parser, clauses,
23943                                                  token->location);
23944           c_name = "nowait";
23945           break;
23946         default:
23947           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23948           goto saw_error;
23949         }
23950
23951       if (((mask >> c_kind) & 1) == 0)
23952         {
23953           /* Remove the invalid clause(s) from the list to avoid
23954              confusing the rest of the compiler.  */
23955           clauses = prev;
23956           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23957         }
23958     }
23959  saw_error:
23960   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23961   return finish_omp_clauses (clauses);
23962 }
23963
23964 /* OpenMP 2.5:
23965    structured-block:
23966      statement
23967
23968    In practice, we're also interested in adding the statement to an
23969    outer node.  So it is convenient if we work around the fact that
23970    cp_parser_statement calls add_stmt.  */
23971
23972 static unsigned
23973 cp_parser_begin_omp_structured_block (cp_parser *parser)
23974 {
23975   unsigned save = parser->in_statement;
23976
23977   /* Only move the values to IN_OMP_BLOCK if they weren't false.
23978      This preserves the "not within loop or switch" style error messages
23979      for nonsense cases like
23980         void foo() {
23981         #pragma omp single
23982           break;
23983         }
23984   */
23985   if (parser->in_statement)
23986     parser->in_statement = IN_OMP_BLOCK;
23987
23988   return save;
23989 }
23990
23991 static void
23992 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23993 {
23994   parser->in_statement = save;
23995 }
23996
23997 static tree
23998 cp_parser_omp_structured_block (cp_parser *parser)
23999 {
24000   tree stmt = begin_omp_structured_block ();
24001   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24002
24003   cp_parser_statement (parser, NULL_TREE, false, NULL);
24004
24005   cp_parser_end_omp_structured_block (parser, save);
24006   return finish_omp_structured_block (stmt);
24007 }
24008
24009 /* OpenMP 2.5:
24010    # pragma omp atomic new-line
24011      expression-stmt
24012
24013    expression-stmt:
24014      x binop= expr | x++ | ++x | x-- | --x
24015    binop:
24016      +, *, -, /, &, ^, |, <<, >>
24017
24018   where x is an lvalue expression with scalar type.  */
24019
24020 static void
24021 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24022 {
24023   tree lhs, rhs;
24024   enum tree_code code;
24025
24026   cp_parser_require_pragma_eol (parser, pragma_tok);
24027
24028   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24029                                     /*cast_p=*/false, NULL);
24030   switch (TREE_CODE (lhs))
24031     {
24032     case ERROR_MARK:
24033       goto saw_error;
24034
24035     case PREINCREMENT_EXPR:
24036     case POSTINCREMENT_EXPR:
24037       lhs = TREE_OPERAND (lhs, 0);
24038       code = PLUS_EXPR;
24039       rhs = integer_one_node;
24040       break;
24041
24042     case PREDECREMENT_EXPR:
24043     case POSTDECREMENT_EXPR:
24044       lhs = TREE_OPERAND (lhs, 0);
24045       code = MINUS_EXPR;
24046       rhs = integer_one_node;
24047       break;
24048
24049     case COMPOUND_EXPR:
24050       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24051          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24052          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24053          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24054          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24055                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24056             == BOOLEAN_TYPE)
24057        /* Undo effects of boolean_increment for post {in,de}crement.  */
24058        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24059       /* FALLTHRU */
24060     case MODIFY_EXPR:
24061       if (TREE_CODE (lhs) == MODIFY_EXPR
24062          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24063        {
24064          /* Undo effects of boolean_increment.  */
24065          if (integer_onep (TREE_OPERAND (lhs, 1)))
24066            {
24067              /* This is pre or post increment.  */
24068              rhs = TREE_OPERAND (lhs, 1);
24069              lhs = TREE_OPERAND (lhs, 0);
24070              code = NOP_EXPR;
24071              break;
24072            }
24073        }
24074       /* FALLTHRU */
24075     default:
24076       switch (cp_lexer_peek_token (parser->lexer)->type)
24077         {
24078         case CPP_MULT_EQ:
24079           code = MULT_EXPR;
24080           break;
24081         case CPP_DIV_EQ:
24082           code = TRUNC_DIV_EXPR;
24083           break;
24084         case CPP_PLUS_EQ:
24085           code = PLUS_EXPR;
24086           break;
24087         case CPP_MINUS_EQ:
24088           code = MINUS_EXPR;
24089           break;
24090         case CPP_LSHIFT_EQ:
24091           code = LSHIFT_EXPR;
24092           break;
24093         case CPP_RSHIFT_EQ:
24094           code = RSHIFT_EXPR;
24095           break;
24096         case CPP_AND_EQ:
24097           code = BIT_AND_EXPR;
24098           break;
24099         case CPP_OR_EQ:
24100           code = BIT_IOR_EXPR;
24101           break;
24102         case CPP_XOR_EQ:
24103           code = BIT_XOR_EXPR;
24104           break;
24105         default:
24106           cp_parser_error (parser,
24107                            "invalid operator for %<#pragma omp atomic%>");
24108           goto saw_error;
24109         }
24110       cp_lexer_consume_token (parser->lexer);
24111
24112       rhs = cp_parser_expression (parser, false, NULL);
24113       if (rhs == error_mark_node)
24114         goto saw_error;
24115       break;
24116     }
24117   finish_omp_atomic (code, lhs, rhs);
24118   cp_parser_consume_semicolon_at_end_of_statement (parser);
24119   return;
24120
24121  saw_error:
24122   cp_parser_skip_to_end_of_block_or_statement (parser);
24123 }
24124
24125
24126 /* OpenMP 2.5:
24127    # pragma omp barrier new-line  */
24128
24129 static void
24130 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24131 {
24132   cp_parser_require_pragma_eol (parser, pragma_tok);
24133   finish_omp_barrier ();
24134 }
24135
24136 /* OpenMP 2.5:
24137    # pragma omp critical [(name)] new-line
24138      structured-block  */
24139
24140 static tree
24141 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24142 {
24143   tree stmt, name = NULL;
24144
24145   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24146     {
24147       cp_lexer_consume_token (parser->lexer);
24148
24149       name = cp_parser_identifier (parser);
24150
24151       if (name == error_mark_node
24152           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24153         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24154                                                /*or_comma=*/false,
24155                                                /*consume_paren=*/true);
24156       if (name == error_mark_node)
24157         name = NULL;
24158     }
24159   cp_parser_require_pragma_eol (parser, pragma_tok);
24160
24161   stmt = cp_parser_omp_structured_block (parser);
24162   return c_finish_omp_critical (input_location, stmt, name);
24163 }
24164
24165 /* OpenMP 2.5:
24166    # pragma omp flush flush-vars[opt] new-line
24167
24168    flush-vars:
24169      ( variable-list ) */
24170
24171 static void
24172 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24173 {
24174   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24175     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24176   cp_parser_require_pragma_eol (parser, pragma_tok);
24177
24178   finish_omp_flush ();
24179 }
24180
24181 /* Helper function, to parse omp for increment expression.  */
24182
24183 static tree
24184 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24185 {
24186   tree cond = cp_parser_binary_expression (parser, false, true,
24187                                            PREC_NOT_OPERATOR, NULL);
24188   bool overloaded_p;
24189
24190   if (cond == error_mark_node
24191       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24192     {
24193       cp_parser_skip_to_end_of_statement (parser);
24194       return error_mark_node;
24195     }
24196
24197   switch (TREE_CODE (cond))
24198     {
24199     case GT_EXPR:
24200     case GE_EXPR:
24201     case LT_EXPR:
24202     case LE_EXPR:
24203       break;
24204     default:
24205       return error_mark_node;
24206     }
24207
24208   /* If decl is an iterator, preserve LHS and RHS of the relational
24209      expr until finish_omp_for.  */
24210   if (decl
24211       && (type_dependent_expression_p (decl)
24212           || CLASS_TYPE_P (TREE_TYPE (decl))))
24213     return cond;
24214
24215   return build_x_binary_op (TREE_CODE (cond),
24216                             TREE_OPERAND (cond, 0), ERROR_MARK,
24217                             TREE_OPERAND (cond, 1), ERROR_MARK,
24218                             &overloaded_p, tf_warning_or_error);
24219 }
24220
24221 /* Helper function, to parse omp for increment expression.  */
24222
24223 static tree
24224 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24225 {
24226   cp_token *token = cp_lexer_peek_token (parser->lexer);
24227   enum tree_code op;
24228   tree lhs, rhs;
24229   cp_id_kind idk;
24230   bool decl_first;
24231
24232   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24233     {
24234       op = (token->type == CPP_PLUS_PLUS
24235             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24236       cp_lexer_consume_token (parser->lexer);
24237       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24238       if (lhs != decl)
24239         return error_mark_node;
24240       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24241     }
24242
24243   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24244   if (lhs != decl)
24245     return error_mark_node;
24246
24247   token = cp_lexer_peek_token (parser->lexer);
24248   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24249     {
24250       op = (token->type == CPP_PLUS_PLUS
24251             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24252       cp_lexer_consume_token (parser->lexer);
24253       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24254     }
24255
24256   op = cp_parser_assignment_operator_opt (parser);
24257   if (op == ERROR_MARK)
24258     return error_mark_node;
24259
24260   if (op != NOP_EXPR)
24261     {
24262       rhs = cp_parser_assignment_expression (parser, false, NULL);
24263       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24264       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24265     }
24266
24267   lhs = cp_parser_binary_expression (parser, false, false,
24268                                      PREC_ADDITIVE_EXPRESSION, NULL);
24269   token = cp_lexer_peek_token (parser->lexer);
24270   decl_first = lhs == decl;
24271   if (decl_first)
24272     lhs = NULL_TREE;
24273   if (token->type != CPP_PLUS
24274       && token->type != CPP_MINUS)
24275     return error_mark_node;
24276
24277   do
24278     {
24279       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24280       cp_lexer_consume_token (parser->lexer);
24281       rhs = cp_parser_binary_expression (parser, false, false,
24282                                          PREC_ADDITIVE_EXPRESSION, NULL);
24283       token = cp_lexer_peek_token (parser->lexer);
24284       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24285         {
24286           if (lhs == NULL_TREE)
24287             {
24288               if (op == PLUS_EXPR)
24289                 lhs = rhs;
24290               else
24291                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24292             }
24293           else
24294             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24295                                      NULL, tf_warning_or_error);
24296         }
24297     }
24298   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24299
24300   if (!decl_first)
24301     {
24302       if (rhs != decl || op == MINUS_EXPR)
24303         return error_mark_node;
24304       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24305     }
24306   else
24307     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24308
24309   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24310 }
24311
24312 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24313
24314 static tree
24315 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24316 {
24317   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24318   tree real_decl, initv, condv, incrv, declv;
24319   tree this_pre_body, cl;
24320   location_t loc_first;
24321   bool collapse_err = false;
24322   int i, collapse = 1, nbraces = 0;
24323   VEC(tree,gc) *for_block = make_tree_vector ();
24324
24325   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24326     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24327       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24328
24329   gcc_assert (collapse >= 1);
24330
24331   declv = make_tree_vec (collapse);
24332   initv = make_tree_vec (collapse);
24333   condv = make_tree_vec (collapse);
24334   incrv = make_tree_vec (collapse);
24335
24336   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24337
24338   for (i = 0; i < collapse; i++)
24339     {
24340       int bracecount = 0;
24341       bool add_private_clause = false;
24342       location_t loc;
24343
24344       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24345         {
24346           cp_parser_error (parser, "for statement expected");
24347           return NULL;
24348         }
24349       loc = cp_lexer_consume_token (parser->lexer)->location;
24350
24351       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24352         return NULL;
24353
24354       init = decl = real_decl = NULL;
24355       this_pre_body = push_stmt_list ();
24356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24357         {
24358           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24359
24360              init-expr:
24361                        var = lb
24362                        integer-type var = lb
24363                        random-access-iterator-type var = lb
24364                        pointer-type var = lb
24365           */
24366           cp_decl_specifier_seq type_specifiers;
24367
24368           /* First, try to parse as an initialized declaration.  See
24369              cp_parser_condition, from whence the bulk of this is copied.  */
24370
24371           cp_parser_parse_tentatively (parser);
24372           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24373                                         /*is_trailing_return=*/false,
24374                                         &type_specifiers);
24375           if (cp_parser_parse_definitely (parser))
24376             {
24377               /* If parsing a type specifier seq succeeded, then this
24378                  MUST be a initialized declaration.  */
24379               tree asm_specification, attributes;
24380               cp_declarator *declarator;
24381
24382               declarator = cp_parser_declarator (parser,
24383                                                  CP_PARSER_DECLARATOR_NAMED,
24384                                                  /*ctor_dtor_or_conv_p=*/NULL,
24385                                                  /*parenthesized_p=*/NULL,
24386                                                  /*member_p=*/false);
24387               attributes = cp_parser_attributes_opt (parser);
24388               asm_specification = cp_parser_asm_specification_opt (parser);
24389
24390               if (declarator == cp_error_declarator) 
24391                 cp_parser_skip_to_end_of_statement (parser);
24392
24393               else 
24394                 {
24395                   tree pushed_scope, auto_node;
24396
24397                   decl = start_decl (declarator, &type_specifiers,
24398                                      SD_INITIALIZED, attributes,
24399                                      /*prefix_attributes=*/NULL_TREE,
24400                                      &pushed_scope);
24401
24402                   auto_node = type_uses_auto (TREE_TYPE (decl));
24403                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24404                     {
24405                       if (cp_lexer_next_token_is (parser->lexer, 
24406                                                   CPP_OPEN_PAREN))
24407                         error ("parenthesized initialization is not allowed in "
24408                                "OpenMP %<for%> loop");
24409                       else
24410                         /* Trigger an error.  */
24411                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24412
24413                       init = error_mark_node;
24414                       cp_parser_skip_to_end_of_statement (parser);
24415                     }
24416                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24417                            || type_dependent_expression_p (decl)
24418                            || auto_node)
24419                     {
24420                       bool is_direct_init, is_non_constant_init;
24421
24422                       init = cp_parser_initializer (parser,
24423                                                     &is_direct_init,
24424                                                     &is_non_constant_init);
24425
24426                       if (auto_node && describable_type (init))
24427                         {
24428                           TREE_TYPE (decl)
24429                             = do_auto_deduction (TREE_TYPE (decl), init,
24430                                                  auto_node);
24431
24432                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24433                               && !type_dependent_expression_p (decl))
24434                             goto non_class;
24435                         }
24436                       
24437                       cp_finish_decl (decl, init, !is_non_constant_init,
24438                                       asm_specification,
24439                                       LOOKUP_ONLYCONVERTING);
24440                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24441                         {
24442                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24443                           init = NULL_TREE;
24444                         }
24445                       else
24446                         init = pop_stmt_list (this_pre_body);
24447                       this_pre_body = NULL_TREE;
24448                     }
24449                   else
24450                     {
24451                       /* Consume '='.  */
24452                       cp_lexer_consume_token (parser->lexer);
24453                       init = cp_parser_assignment_expression (parser, false, NULL);
24454
24455                     non_class:
24456                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24457                         init = error_mark_node;
24458                       else
24459                         cp_finish_decl (decl, NULL_TREE,
24460                                         /*init_const_expr_p=*/false,
24461                                         asm_specification,
24462                                         LOOKUP_ONLYCONVERTING);
24463                     }
24464
24465                   if (pushed_scope)
24466                     pop_scope (pushed_scope);
24467                 }
24468             }
24469           else 
24470             {
24471               cp_id_kind idk;
24472               /* If parsing a type specifier sequence failed, then
24473                  this MUST be a simple expression.  */
24474               cp_parser_parse_tentatively (parser);
24475               decl = cp_parser_primary_expression (parser, false, false,
24476                                                    false, &idk);
24477               if (!cp_parser_error_occurred (parser)
24478                   && decl
24479                   && DECL_P (decl)
24480                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24481                 {
24482                   tree rhs;
24483
24484                   cp_parser_parse_definitely (parser);
24485                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24486                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24487                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24488                                                          rhs,
24489                                                          tf_warning_or_error));
24490                   add_private_clause = true;
24491                 }
24492               else
24493                 {
24494                   decl = NULL;
24495                   cp_parser_abort_tentative_parse (parser);
24496                   init = cp_parser_expression (parser, false, NULL);
24497                   if (init)
24498                     {
24499                       if (TREE_CODE (init) == MODIFY_EXPR
24500                           || TREE_CODE (init) == MODOP_EXPR)
24501                         real_decl = TREE_OPERAND (init, 0);
24502                     }
24503                 }
24504             }
24505         }
24506       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24507       if (this_pre_body)
24508         {
24509           this_pre_body = pop_stmt_list (this_pre_body);
24510           if (pre_body)
24511             {
24512               tree t = pre_body;
24513               pre_body = push_stmt_list ();
24514               add_stmt (t);
24515               add_stmt (this_pre_body);
24516               pre_body = pop_stmt_list (pre_body);
24517             }
24518           else
24519             pre_body = this_pre_body;
24520         }
24521
24522       if (decl)
24523         real_decl = decl;
24524       if (par_clauses != NULL && real_decl != NULL_TREE)
24525         {
24526           tree *c;
24527           for (c = par_clauses; *c ; )
24528             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24529                 && OMP_CLAUSE_DECL (*c) == real_decl)
24530               {
24531                 error_at (loc, "iteration variable %qD"
24532                           " should not be firstprivate", real_decl);
24533                 *c = OMP_CLAUSE_CHAIN (*c);
24534               }
24535             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24536                      && OMP_CLAUSE_DECL (*c) == real_decl)
24537               {
24538                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24539                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24540                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24541                 OMP_CLAUSE_DECL (l) = real_decl;
24542                 OMP_CLAUSE_CHAIN (l) = clauses;
24543                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24544                 clauses = l;
24545                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24546                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24547                 add_private_clause = false;
24548               }
24549             else
24550               {
24551                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24552                     && OMP_CLAUSE_DECL (*c) == real_decl)
24553                   add_private_clause = false;
24554                 c = &OMP_CLAUSE_CHAIN (*c);
24555               }
24556         }
24557
24558       if (add_private_clause)
24559         {
24560           tree c;
24561           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24562             {
24563               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24564                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24565                   && OMP_CLAUSE_DECL (c) == decl)
24566                 break;
24567               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24568                        && OMP_CLAUSE_DECL (c) == decl)
24569                 error_at (loc, "iteration variable %qD "
24570                           "should not be firstprivate",
24571                           decl);
24572               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24573                        && OMP_CLAUSE_DECL (c) == decl)
24574                 error_at (loc, "iteration variable %qD should not be reduction",
24575                           decl);
24576             }
24577           if (c == NULL)
24578             {
24579               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24580               OMP_CLAUSE_DECL (c) = decl;
24581               c = finish_omp_clauses (c);
24582               if (c)
24583                 {
24584                   OMP_CLAUSE_CHAIN (c) = clauses;
24585                   clauses = c;
24586                 }
24587             }
24588         }
24589
24590       cond = NULL;
24591       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24592         cond = cp_parser_omp_for_cond (parser, decl);
24593       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24594
24595       incr = NULL;
24596       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24597         {
24598           /* If decl is an iterator, preserve the operator on decl
24599              until finish_omp_for.  */
24600           if (decl
24601               && (type_dependent_expression_p (decl)
24602                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24603             incr = cp_parser_omp_for_incr (parser, decl);
24604           else
24605             incr = cp_parser_expression (parser, false, NULL);
24606         }
24607
24608       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24609         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24610                                                /*or_comma=*/false,
24611                                                /*consume_paren=*/true);
24612
24613       TREE_VEC_ELT (declv, i) = decl;
24614       TREE_VEC_ELT (initv, i) = init;
24615       TREE_VEC_ELT (condv, i) = cond;
24616       TREE_VEC_ELT (incrv, i) = incr;
24617
24618       if (i == collapse - 1)
24619         break;
24620
24621       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24622          in between the collapsed for loops to be still considered perfectly
24623          nested.  Hopefully the final version clarifies this.
24624          For now handle (multiple) {'s and empty statements.  */
24625       cp_parser_parse_tentatively (parser);
24626       do
24627         {
24628           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24629             break;
24630           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24631             {
24632               cp_lexer_consume_token (parser->lexer);
24633               bracecount++;
24634             }
24635           else if (bracecount
24636                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24637             cp_lexer_consume_token (parser->lexer);
24638           else
24639             {
24640               loc = cp_lexer_peek_token (parser->lexer)->location;
24641               error_at (loc, "not enough collapsed for loops");
24642               collapse_err = true;
24643               cp_parser_abort_tentative_parse (parser);
24644               declv = NULL_TREE;
24645               break;
24646             }
24647         }
24648       while (1);
24649
24650       if (declv)
24651         {
24652           cp_parser_parse_definitely (parser);
24653           nbraces += bracecount;
24654         }
24655     }
24656
24657   /* Note that we saved the original contents of this flag when we entered
24658      the structured block, and so we don't need to re-save it here.  */
24659   parser->in_statement = IN_OMP_FOR;
24660
24661   /* Note that the grammar doesn't call for a structured block here,
24662      though the loop as a whole is a structured block.  */
24663   body = push_stmt_list ();
24664   cp_parser_statement (parser, NULL_TREE, false, NULL);
24665   body = pop_stmt_list (body);
24666
24667   if (declv == NULL_TREE)
24668     ret = NULL_TREE;
24669   else
24670     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24671                           pre_body, clauses);
24672
24673   while (nbraces)
24674     {
24675       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24676         {
24677           cp_lexer_consume_token (parser->lexer);
24678           nbraces--;
24679         }
24680       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24681         cp_lexer_consume_token (parser->lexer);
24682       else
24683         {
24684           if (!collapse_err)
24685             {
24686               error_at (cp_lexer_peek_token (parser->lexer)->location,
24687                         "collapsed loops not perfectly nested");
24688             }
24689           collapse_err = true;
24690           cp_parser_statement_seq_opt (parser, NULL);
24691           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24692             break;
24693         }
24694     }
24695
24696   while (!VEC_empty (tree, for_block))
24697     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24698   release_tree_vector (for_block);
24699
24700   return ret;
24701 }
24702
24703 /* OpenMP 2.5:
24704    #pragma omp for for-clause[optseq] new-line
24705      for-loop  */
24706
24707 #define OMP_FOR_CLAUSE_MASK                             \
24708         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24709         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24710         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24711         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24712         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24713         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24714         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24715         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24716
24717 static tree
24718 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24719 {
24720   tree clauses, sb, ret;
24721   unsigned int save;
24722
24723   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24724                                        "#pragma omp for", pragma_tok);
24725
24726   sb = begin_omp_structured_block ();
24727   save = cp_parser_begin_omp_structured_block (parser);
24728
24729   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24730
24731   cp_parser_end_omp_structured_block (parser, save);
24732   add_stmt (finish_omp_structured_block (sb));
24733
24734   return ret;
24735 }
24736
24737 /* OpenMP 2.5:
24738    # pragma omp master new-line
24739      structured-block  */
24740
24741 static tree
24742 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24743 {
24744   cp_parser_require_pragma_eol (parser, pragma_tok);
24745   return c_finish_omp_master (input_location,
24746                               cp_parser_omp_structured_block (parser));
24747 }
24748
24749 /* OpenMP 2.5:
24750    # pragma omp ordered new-line
24751      structured-block  */
24752
24753 static tree
24754 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24755 {
24756   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24757   cp_parser_require_pragma_eol (parser, pragma_tok);
24758   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24759 }
24760
24761 /* OpenMP 2.5:
24762
24763    section-scope:
24764      { section-sequence }
24765
24766    section-sequence:
24767      section-directive[opt] structured-block
24768      section-sequence section-directive structured-block  */
24769
24770 static tree
24771 cp_parser_omp_sections_scope (cp_parser *parser)
24772 {
24773   tree stmt, substmt;
24774   bool error_suppress = false;
24775   cp_token *tok;
24776
24777   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24778     return NULL_TREE;
24779
24780   stmt = push_stmt_list ();
24781
24782   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24783     {
24784       unsigned save;
24785
24786       substmt = begin_omp_structured_block ();
24787       save = cp_parser_begin_omp_structured_block (parser);
24788
24789       while (1)
24790         {
24791           cp_parser_statement (parser, NULL_TREE, false, NULL);
24792
24793           tok = cp_lexer_peek_token (parser->lexer);
24794           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24795             break;
24796           if (tok->type == CPP_CLOSE_BRACE)
24797             break;
24798           if (tok->type == CPP_EOF)
24799             break;
24800         }
24801
24802       cp_parser_end_omp_structured_block (parser, save);
24803       substmt = finish_omp_structured_block (substmt);
24804       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24805       add_stmt (substmt);
24806     }
24807
24808   while (1)
24809     {
24810       tok = cp_lexer_peek_token (parser->lexer);
24811       if (tok->type == CPP_CLOSE_BRACE)
24812         break;
24813       if (tok->type == CPP_EOF)
24814         break;
24815
24816       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24817         {
24818           cp_lexer_consume_token (parser->lexer);
24819           cp_parser_require_pragma_eol (parser, tok);
24820           error_suppress = false;
24821         }
24822       else if (!error_suppress)
24823         {
24824           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24825           error_suppress = true;
24826         }
24827
24828       substmt = cp_parser_omp_structured_block (parser);
24829       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24830       add_stmt (substmt);
24831     }
24832   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24833
24834   substmt = pop_stmt_list (stmt);
24835
24836   stmt = make_node (OMP_SECTIONS);
24837   TREE_TYPE (stmt) = void_type_node;
24838   OMP_SECTIONS_BODY (stmt) = substmt;
24839
24840   add_stmt (stmt);
24841   return stmt;
24842 }
24843
24844 /* OpenMP 2.5:
24845    # pragma omp sections sections-clause[optseq] newline
24846      sections-scope  */
24847
24848 #define OMP_SECTIONS_CLAUSE_MASK                        \
24849         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24850         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24851         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24852         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24853         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24854
24855 static tree
24856 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24857 {
24858   tree clauses, ret;
24859
24860   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24861                                        "#pragma omp sections", pragma_tok);
24862
24863   ret = cp_parser_omp_sections_scope (parser);
24864   if (ret)
24865     OMP_SECTIONS_CLAUSES (ret) = clauses;
24866
24867   return ret;
24868 }
24869
24870 /* OpenMP 2.5:
24871    # pragma parallel parallel-clause new-line
24872    # pragma parallel for parallel-for-clause new-line
24873    # pragma parallel sections parallel-sections-clause new-line  */
24874
24875 #define OMP_PARALLEL_CLAUSE_MASK                        \
24876         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24877         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24878         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24879         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24880         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24881         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24882         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24883         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24884
24885 static tree
24886 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24887 {
24888   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24889   const char *p_name = "#pragma omp parallel";
24890   tree stmt, clauses, par_clause, ws_clause, block;
24891   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24892   unsigned int save;
24893   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24894
24895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24896     {
24897       cp_lexer_consume_token (parser->lexer);
24898       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24899       p_name = "#pragma omp parallel for";
24900       mask |= OMP_FOR_CLAUSE_MASK;
24901       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24902     }
24903   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24904     {
24905       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24906       const char *p = IDENTIFIER_POINTER (id);
24907       if (strcmp (p, "sections") == 0)
24908         {
24909           cp_lexer_consume_token (parser->lexer);
24910           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24911           p_name = "#pragma omp parallel sections";
24912           mask |= OMP_SECTIONS_CLAUSE_MASK;
24913           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24914         }
24915     }
24916
24917   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24918   block = begin_omp_parallel ();
24919   save = cp_parser_begin_omp_structured_block (parser);
24920
24921   switch (p_kind)
24922     {
24923     case PRAGMA_OMP_PARALLEL:
24924       cp_parser_statement (parser, NULL_TREE, false, NULL);
24925       par_clause = clauses;
24926       break;
24927
24928     case PRAGMA_OMP_PARALLEL_FOR:
24929       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24930       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24931       break;
24932
24933     case PRAGMA_OMP_PARALLEL_SECTIONS:
24934       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24935       stmt = cp_parser_omp_sections_scope (parser);
24936       if (stmt)
24937         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24938       break;
24939
24940     default:
24941       gcc_unreachable ();
24942     }
24943
24944   cp_parser_end_omp_structured_block (parser, save);
24945   stmt = finish_omp_parallel (par_clause, block);
24946   if (p_kind != PRAGMA_OMP_PARALLEL)
24947     OMP_PARALLEL_COMBINED (stmt) = 1;
24948   return stmt;
24949 }
24950
24951 /* OpenMP 2.5:
24952    # pragma omp single single-clause[optseq] new-line
24953      structured-block  */
24954
24955 #define OMP_SINGLE_CLAUSE_MASK                          \
24956         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24957         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24958         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24959         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24960
24961 static tree
24962 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24963 {
24964   tree stmt = make_node (OMP_SINGLE);
24965   TREE_TYPE (stmt) = void_type_node;
24966
24967   OMP_SINGLE_CLAUSES (stmt)
24968     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24969                                  "#pragma omp single", pragma_tok);
24970   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24971
24972   return add_stmt (stmt);
24973 }
24974
24975 /* OpenMP 3.0:
24976    # pragma omp task task-clause[optseq] new-line
24977      structured-block  */
24978
24979 #define OMP_TASK_CLAUSE_MASK                            \
24980         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24981         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
24982         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24983         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24984         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24985         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24986
24987 static tree
24988 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24989 {
24990   tree clauses, block;
24991   unsigned int save;
24992
24993   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24994                                        "#pragma omp task", pragma_tok);
24995   block = begin_omp_task ();
24996   save = cp_parser_begin_omp_structured_block (parser);
24997   cp_parser_statement (parser, NULL_TREE, false, NULL);
24998   cp_parser_end_omp_structured_block (parser, save);
24999   return finish_omp_task (clauses, block);
25000 }
25001
25002 /* OpenMP 3.0:
25003    # pragma omp taskwait new-line  */
25004
25005 static void
25006 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25007 {
25008   cp_parser_require_pragma_eol (parser, pragma_tok);
25009   finish_omp_taskwait ();
25010 }
25011
25012 /* OpenMP 2.5:
25013    # pragma omp threadprivate (variable-list) */
25014
25015 static void
25016 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25017 {
25018   tree vars;
25019
25020   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25021   cp_parser_require_pragma_eol (parser, pragma_tok);
25022
25023   finish_omp_threadprivate (vars);
25024 }
25025
25026 /* Main entry point to OpenMP statement pragmas.  */
25027
25028 static void
25029 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25030 {
25031   tree stmt;
25032
25033   switch (pragma_tok->pragma_kind)
25034     {
25035     case PRAGMA_OMP_ATOMIC:
25036       cp_parser_omp_atomic (parser, pragma_tok);
25037       return;
25038     case PRAGMA_OMP_CRITICAL:
25039       stmt = cp_parser_omp_critical (parser, pragma_tok);
25040       break;
25041     case PRAGMA_OMP_FOR:
25042       stmt = cp_parser_omp_for (parser, pragma_tok);
25043       break;
25044     case PRAGMA_OMP_MASTER:
25045       stmt = cp_parser_omp_master (parser, pragma_tok);
25046       break;
25047     case PRAGMA_OMP_ORDERED:
25048       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25049       break;
25050     case PRAGMA_OMP_PARALLEL:
25051       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25052       break;
25053     case PRAGMA_OMP_SECTIONS:
25054       stmt = cp_parser_omp_sections (parser, pragma_tok);
25055       break;
25056     case PRAGMA_OMP_SINGLE:
25057       stmt = cp_parser_omp_single (parser, pragma_tok);
25058       break;
25059     case PRAGMA_OMP_TASK:
25060       stmt = cp_parser_omp_task (parser, pragma_tok);
25061       break;
25062     default:
25063       gcc_unreachable ();
25064     }
25065
25066   if (stmt)
25067     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25068 }
25069 \f
25070 /* The parser.  */
25071
25072 static GTY (()) cp_parser *the_parser;
25073
25074 \f
25075 /* Special handling for the first token or line in the file.  The first
25076    thing in the file might be #pragma GCC pch_preprocess, which loads a
25077    PCH file, which is a GC collection point.  So we need to handle this
25078    first pragma without benefit of an existing lexer structure.
25079
25080    Always returns one token to the caller in *FIRST_TOKEN.  This is
25081    either the true first token of the file, or the first token after
25082    the initial pragma.  */
25083
25084 static void
25085 cp_parser_initial_pragma (cp_token *first_token)
25086 {
25087   tree name = NULL;
25088
25089   cp_lexer_get_preprocessor_token (NULL, first_token);
25090   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25091     return;
25092
25093   cp_lexer_get_preprocessor_token (NULL, first_token);
25094   if (first_token->type == CPP_STRING)
25095     {
25096       name = first_token->u.value;
25097
25098       cp_lexer_get_preprocessor_token (NULL, first_token);
25099       if (first_token->type != CPP_PRAGMA_EOL)
25100         error_at (first_token->location,
25101                   "junk at end of %<#pragma GCC pch_preprocess%>");
25102     }
25103   else
25104     error_at (first_token->location, "expected string literal");
25105
25106   /* Skip to the end of the pragma.  */
25107   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25108     cp_lexer_get_preprocessor_token (NULL, first_token);
25109
25110   /* Now actually load the PCH file.  */
25111   if (name)
25112     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25113
25114   /* Read one more token to return to our caller.  We have to do this
25115      after reading the PCH file in, since its pointers have to be
25116      live.  */
25117   cp_lexer_get_preprocessor_token (NULL, first_token);
25118 }
25119
25120 /* Normal parsing of a pragma token.  Here we can (and must) use the
25121    regular lexer.  */
25122
25123 static bool
25124 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25125 {
25126   cp_token *pragma_tok;
25127   unsigned int id;
25128
25129   pragma_tok = cp_lexer_consume_token (parser->lexer);
25130   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25131   parser->lexer->in_pragma = true;
25132
25133   id = pragma_tok->pragma_kind;
25134   switch (id)
25135     {
25136     case PRAGMA_GCC_PCH_PREPROCESS:
25137       error_at (pragma_tok->location,
25138                 "%<#pragma GCC pch_preprocess%> must be first");
25139       break;
25140
25141     case PRAGMA_OMP_BARRIER:
25142       switch (context)
25143         {
25144         case pragma_compound:
25145           cp_parser_omp_barrier (parser, pragma_tok);
25146           return false;
25147         case pragma_stmt:
25148           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25149                     "used in compound statements");
25150           break;
25151         default:
25152           goto bad_stmt;
25153         }
25154       break;
25155
25156     case PRAGMA_OMP_FLUSH:
25157       switch (context)
25158         {
25159         case pragma_compound:
25160           cp_parser_omp_flush (parser, pragma_tok);
25161           return false;
25162         case pragma_stmt:
25163           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25164                     "used in compound statements");
25165           break;
25166         default:
25167           goto bad_stmt;
25168         }
25169       break;
25170
25171     case PRAGMA_OMP_TASKWAIT:
25172       switch (context)
25173         {
25174         case pragma_compound:
25175           cp_parser_omp_taskwait (parser, pragma_tok);
25176           return false;
25177         case pragma_stmt:
25178           error_at (pragma_tok->location,
25179                     "%<#pragma omp taskwait%> may only be "
25180                     "used in compound statements");
25181           break;
25182         default:
25183           goto bad_stmt;
25184         }
25185       break;
25186
25187     case PRAGMA_OMP_THREADPRIVATE:
25188       cp_parser_omp_threadprivate (parser, pragma_tok);
25189       return false;
25190
25191     case PRAGMA_OMP_ATOMIC:
25192     case PRAGMA_OMP_CRITICAL:
25193     case PRAGMA_OMP_FOR:
25194     case PRAGMA_OMP_MASTER:
25195     case PRAGMA_OMP_ORDERED:
25196     case PRAGMA_OMP_PARALLEL:
25197     case PRAGMA_OMP_SECTIONS:
25198     case PRAGMA_OMP_SINGLE:
25199     case PRAGMA_OMP_TASK:
25200       if (context == pragma_external)
25201         goto bad_stmt;
25202       cp_parser_omp_construct (parser, pragma_tok);
25203       return true;
25204
25205     case PRAGMA_OMP_SECTION:
25206       error_at (pragma_tok->location, 
25207                 "%<#pragma omp section%> may only be used in "
25208                 "%<#pragma omp sections%> construct");
25209       break;
25210
25211     default:
25212       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25213       c_invoke_pragma_handler (id);
25214       break;
25215
25216     bad_stmt:
25217       cp_parser_error (parser, "expected declaration specifiers");
25218       break;
25219     }
25220
25221   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25222   return false;
25223 }
25224
25225 /* The interface the pragma parsers have to the lexer.  */
25226
25227 enum cpp_ttype
25228 pragma_lex (tree *value)
25229 {
25230   cp_token *tok;
25231   enum cpp_ttype ret;
25232
25233   tok = cp_lexer_peek_token (the_parser->lexer);
25234
25235   ret = tok->type;
25236   *value = tok->u.value;
25237
25238   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25239     ret = CPP_EOF;
25240   else if (ret == CPP_STRING)
25241     *value = cp_parser_string_literal (the_parser, false, false);
25242   else
25243     {
25244       cp_lexer_consume_token (the_parser->lexer);
25245       if (ret == CPP_KEYWORD)
25246         ret = CPP_NAME;
25247     }
25248
25249   return ret;
25250 }
25251
25252 \f
25253 /* External interface.  */
25254
25255 /* Parse one entire translation unit.  */
25256
25257 void
25258 c_parse_file (void)
25259 {
25260   static bool already_called = false;
25261
25262   if (already_called)
25263     {
25264       sorry ("inter-module optimizations not implemented for C++");
25265       return;
25266     }
25267   already_called = true;
25268
25269   the_parser = cp_parser_new ();
25270   push_deferring_access_checks (flag_access_control
25271                                 ? dk_no_deferred : dk_no_check);
25272   cp_parser_translation_unit (the_parser);
25273   the_parser = NULL;
25274 }
25275
25276 #include "gt-cp-parser.h"